]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/dirmac.cpp
corrected wxMenuBar::Insert , ::Append for a correct title even when the menubar...
[wxWidgets.git] / src / mac / carbon / dirmac.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/dir.cpp
3 // Purpose: wxDir implementation for Mac
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 08.12.99
7 // RCS-ID: $Id$
8 // Copyright: (c) 1999 Stefan Csomor <csomor@advanced.ch>
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "dir.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #ifndef WX_PRECOMP
32 #include "wx/intl.h"
33 #include "wx/log.h"
34 #endif // PCH
35
36 #include "wx/dir.h"
37 #include "wx/filefn.h" // for wxPathExists()
38
39 #ifndef __DARWIN__
40 #include <windows.h>
41 #endif
42
43 #include "wx/mac/private.h"
44
45 #include "MoreFiles.h"
46 #include "MoreFilesExtras.h"
47
48 // ----------------------------------------------------------------------------
49 // constants
50 // ----------------------------------------------------------------------------
51
52 #ifndef MAX_PATH
53 #define MAX_PATH 260 // from VC++ headers
54 #endif
55
56 // ----------------------------------------------------------------------------
57 // macros
58 // ----------------------------------------------------------------------------
59
60 #define M_DIR ((wxDirData *)m_data)
61
62 // ----------------------------------------------------------------------------
63 // private classes
64 // ----------------------------------------------------------------------------
65
66 // this class stores everything we need to enumerate the files
67 class wxDirData
68 {
69 public:
70 wxDirData(const wxString& dirname);
71 ~wxDirData();
72
73 void SetFileSpec(const wxString& filespec) { m_filespec = filespec; }
74 void SetFlags(int flags) { m_flags = flags; }
75
76 bool Read(wxString *filename); // reads the next
77 void Rewind() ;
78
79 const wxString& GetName() const { return m_dirname; }
80
81 private:
82 CInfoPBRec m_CPB ;
83 wxInt16 m_index ;
84 long m_dirId ;
85 Str255 m_name ;
86 Boolean m_isDir ;
87
88 wxString m_dirname;
89 wxString m_filespec;
90
91 int m_flags;
92 };
93
94 // ============================================================================
95 // implementation
96 // ============================================================================
97
98 // ----------------------------------------------------------------------------
99 // wxDirData
100 // ----------------------------------------------------------------------------
101
102 wxDirData::wxDirData(const wxString& dirname)
103 : m_dirname(dirname)
104 {
105 // throw away the trailing slashes
106 size_t n = m_dirname.length();
107 wxCHECK_RET( n, _T("empty dir name in wxDir") );
108
109 while ( n > 0 && wxIsPathSeparator(m_dirname[--n]) )
110 ;
111
112 m_dirname.Truncate(n + 1);
113
114 FSSpec fsspec ;
115
116 wxMacFilename2FSSpec( m_dirname , &fsspec ) ;
117 m_CPB.hFileInfo.ioVRefNum = fsspec.vRefNum ;
118 m_CPB.hFileInfo.ioNamePtr = m_name ;
119 m_index = 0 ;
120
121 FSpGetDirectoryID( &fsspec , &m_dirId , &m_isDir ) ;
122 }
123
124 wxDirData::~wxDirData()
125 {
126 }
127
128 void wxDirData::Rewind()
129 {
130 m_index = 0 ;
131 }
132
133 bool wxDirData::Read(wxString *filename)
134 {
135 if ( !m_isDir )
136 return FALSE ;
137
138 #if TARGET_CARBON
139 char c_name[256] ;
140 #endif
141 wxString result;
142
143 short err = noErr ;
144
145 while ( err == noErr )
146 {
147 m_index++ ;
148 m_CPB.dirInfo.ioFDirIndex = m_index;
149 m_CPB.dirInfo.ioDrDirID = m_dirId; /* we need to do this every time */
150 err = PBGetCatInfoSync((CInfoPBPtr)&m_CPB);
151 if ( err != noErr )
152 break ;
153
154 #if TARGET_CARBON
155 p2cstrcpy( c_name, m_name ) ;
156 strcpy( (char *)m_name, c_name);
157 #else
158 p2cstr( m_name ) ;
159 #endif
160 if ( ( m_CPB.dirInfo.ioFlAttrib & ioDirMask) != 0 && (m_flags & wxDIR_DIRS) ) // we have a directory
161 break ;
162
163 if ( ( m_CPB.dirInfo.ioFlAttrib & ioDirMask) == 0 && !(m_flags & wxDIR_FILES ) ) // its a file but we don't want it
164 continue ;
165
166 if ( ( m_CPB.hFileInfo.ioFlFndrInfo.fdFlags & kIsInvisible ) && !(m_flags & wxDIR_HIDDEN) ) // its hidden but we don't want it
167 continue ;
168
169 wxString file( m_name ) ;
170 if ( m_filespec.IsEmpty() || m_filespec == "*.*" || m_filespec == "*" )
171 {
172 }
173 else if ( m_filespec.Length() > 1 && m_filespec.Left(1) =="*" )
174 {
175 if ( file.Right( m_filespec.Length() - 1 ).Upper() != m_filespec.Mid(1).Upper() )
176 {
177 continue ;
178 }
179 }
180 else if ( m_filespec.Length() > 1 && m_filespec.Right(1) == "*" )
181 {
182 if ( file.Left( m_filespec.Length() - 1 ).Upper() != m_filespec.Left( m_filespec.Length() - 1 ).Upper() )
183 {
184 continue ;
185 }
186 }
187 else if ( file.Upper() != m_filespec.Upper() )
188 {
189 continue ;
190 }
191
192 break ;
193 }
194 if ( err != noErr )
195 {
196 return FALSE ;
197 }
198
199 *filename = (char*) m_name ;
200
201 return TRUE;
202 }
203
204 // ----------------------------------------------------------------------------
205 // wxDir helpers
206 // ----------------------------------------------------------------------------
207
208 /* static */
209 bool wxDir::Exists(const wxString& dir)
210 {
211 return wxPathExists(dir);
212 }
213
214 // ----------------------------------------------------------------------------
215 // wxDir construction/destruction
216 // ----------------------------------------------------------------------------
217
218 wxDir::wxDir(const wxString& dirname)
219 {
220 m_data = NULL;
221
222 (void)Open(dirname);
223 }
224
225 bool wxDir::Open(const wxString& dirname)
226 {
227 delete M_DIR;
228 m_data = new wxDirData(dirname);
229
230 return TRUE;
231 }
232
233 bool wxDir::IsOpened() const
234 {
235 return m_data != NULL;
236 }
237
238 wxString wxDir::GetName() const
239 {
240 wxString name;
241 if ( m_data )
242 {
243 name = M_DIR->GetName();
244 if ( !name.empty() && (name.Last() == _T('/')) )
245 {
246 // chop off the last (back)slash
247 name.Truncate(name.length() - 1);
248 }
249 }
250
251 return name;
252 }
253
254 wxDir::~wxDir()
255 {
256 delete M_DIR;
257 }
258
259 // ----------------------------------------------------------------------------
260 // wxDir enumerating
261 // ----------------------------------------------------------------------------
262
263 bool wxDir::GetFirst(wxString *filename,
264 const wxString& filespec,
265 int flags) const
266 {
267 wxCHECK_MSG( IsOpened(), FALSE, _T("must wxDir::Open() first") );
268
269 M_DIR->Rewind();
270
271 M_DIR->SetFileSpec(filespec);
272 M_DIR->SetFlags(flags);
273
274 return GetNext(filename);
275 }
276
277 bool wxDir::GetNext(wxString *filename) const
278 {
279 wxCHECK_MSG( IsOpened(), FALSE, _T("must wxDir::Open() first") );
280
281 wxCHECK_MSG( filename, FALSE, _T("bad pointer in wxDir::GetNext()") );
282
283 return M_DIR->Read(filename);
284 }