]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/dirmac.cpp
setting the current event record correctly (needed for control text handling)
[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 OSErr err = 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 TARGET_CARBON
161 // under X thats the way the mounting points look like
162 if ( ( m_CPB.dirInfo.ioDrDirID == 0 ) && ( m_flags & wxDIR_DIRS) )
163 break ;
164 #endif
165 if ( ( m_CPB.dirInfo.ioFlAttrib & ioDirMask) != 0 && (m_flags & wxDIR_DIRS) ) // we have a directory
166 break ;
167
168 if ( ( m_CPB.dirInfo.ioFlAttrib & ioDirMask) == 0 && !(m_flags & wxDIR_FILES ) ) // its a file but we don't want it
169 continue ;
170
171 if ( ( m_CPB.hFileInfo.ioFlFndrInfo.fdFlags & kIsInvisible ) && !(m_flags & wxDIR_HIDDEN) ) // its hidden but we don't want it
172 continue ;
173
174 wxString file( m_name ) ;
175 if ( m_filespec.IsEmpty() || m_filespec == "*.*" || m_filespec == "*" )
176 {
177 }
178 else if ( m_filespec.Length() > 1 && m_filespec.Left(1) =="*" )
179 {
180 if ( file.Right( m_filespec.Length() - 1 ).Upper() != m_filespec.Mid(1).Upper() )
181 {
182 continue ;
183 }
184 }
185 else if ( m_filespec.Length() > 1 && m_filespec.Right(1) == "*" )
186 {
187 if ( file.Left( m_filespec.Length() - 1 ).Upper() != m_filespec.Left( m_filespec.Length() - 1 ).Upper() )
188 {
189 continue ;
190 }
191 }
192 else if ( file.Upper() != m_filespec.Upper() )
193 {
194 continue ;
195 }
196
197 break ;
198 }
199 if ( err != noErr )
200 {
201 return FALSE ;
202 }
203
204 *filename = (char*) m_name ;
205
206 return TRUE;
207 }
208
209 // ----------------------------------------------------------------------------
210 // wxDir helpers
211 // ----------------------------------------------------------------------------
212
213 /* static */
214 bool wxDir::Exists(const wxString& dir)
215 {
216 return wxPathExists(dir);
217 }
218
219 // ----------------------------------------------------------------------------
220 // wxDir construction/destruction
221 // ----------------------------------------------------------------------------
222
223 wxDir::wxDir(const wxString& dirname)
224 {
225 m_data = NULL;
226
227 (void)Open(dirname);
228 }
229
230 bool wxDir::Open(const wxString& dirname)
231 {
232 delete M_DIR;
233 m_data = new wxDirData(dirname);
234
235 return TRUE;
236 }
237
238 bool wxDir::IsOpened() const
239 {
240 return m_data != NULL;
241 }
242
243 wxString wxDir::GetName() const
244 {
245 wxString name;
246 if ( m_data )
247 {
248 name = M_DIR->GetName();
249 if ( !name.empty() && (name.Last() == _T('/')) )
250 {
251 // chop off the last (back)slash
252 name.Truncate(name.length() - 1);
253 }
254 }
255
256 return name;
257 }
258
259 wxDir::~wxDir()
260 {
261 if (M_DIR != NULL) {
262 delete M_DIR;
263 m_data = NULL;
264 }
265 }
266
267 // ----------------------------------------------------------------------------
268 // wxDir enumerating
269 // ----------------------------------------------------------------------------
270
271 bool wxDir::GetFirst(wxString *filename,
272 const wxString& filespec,
273 int flags) const
274 {
275 wxCHECK_MSG( IsOpened(), FALSE, _T("must wxDir::Open() first") );
276
277 M_DIR->Rewind();
278
279 M_DIR->SetFileSpec(filespec);
280 M_DIR->SetFlags(flags);
281
282 return GetNext(filename);
283 }
284
285 bool wxDir::GetNext(wxString *filename) const
286 {
287 wxCHECK_MSG( IsOpened(), FALSE, _T("must wxDir::Open() first") );
288
289 wxCHECK_MSG( filename, FALSE, _T("bad pointer in wxDir::GetNext()") );
290
291 return M_DIR->Read(filename);
292 }