]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/dirmac.cpp
added OnMouse which will trigger context menu events, added better DoMenuPopup implem...
[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 #ifdef __DARWIN__
46 # include "MoreFilesX.h"
47 #else
48 # include "MoreFiles.h"
49 # include "MoreFilesExtras.h"
50 #endif
51
52 // ----------------------------------------------------------------------------
53 // constants
54 // ----------------------------------------------------------------------------
55
56 #ifndef MAX_PATH
57 #define MAX_PATH 260 // from VC++ headers
58 #endif
59
60 // ----------------------------------------------------------------------------
61 // macros
62 // ----------------------------------------------------------------------------
63
64 #define M_DIR ((wxDirData *)m_data)
65
66 // ----------------------------------------------------------------------------
67 // private classes
68 // ----------------------------------------------------------------------------
69
70 // this class stores everything we need to enumerate the files
71 class wxDirData
72 {
73 public:
74 wxDirData(const wxString& dirname);
75 ~wxDirData();
76
77 void SetFileSpec(const wxString& filespec) { m_filespec = filespec; }
78 void SetFlags(int flags) { m_flags = flags; }
79
80 bool Read(wxString *filename); // reads the next
81 void Rewind() ;
82
83 const wxString& GetName() const { return m_dirname; }
84
85 private:
86 CInfoPBRec m_CPB ;
87 wxInt16 m_index ;
88 long m_dirId ;
89 Str255 m_name ;
90 Boolean m_isDir ;
91
92 wxString m_dirname;
93 wxString m_filespec;
94
95 int m_flags;
96 };
97
98 // ============================================================================
99 // implementation
100 // ============================================================================
101
102 // ----------------------------------------------------------------------------
103 // wxDirData
104 // ----------------------------------------------------------------------------
105
106 wxDirData::wxDirData(const wxString& dirname)
107 : m_dirname(dirname)
108 {
109 OSErr err;
110
111 // throw away the trailing slashes
112 size_t n = m_dirname.length();
113 wxCHECK_RET( n, _T("empty dir name in wxDir") );
114
115 while ( n > 0 && wxIsPathSeparator(m_dirname[--n]) )
116 ;
117
118 m_dirname.Truncate(n + 1);
119
120 #ifdef __DARWIN__
121 FSRef theRef;
122
123 // get the FSRef associated with the POSIX path
124 err = FSPathMakeRef((const UInt8 *) m_dirname.c_str(), &theRef, NULL);
125 FSGetVRefNum(&theRef, &(m_CPB.hFileInfo.ioVRefNum));
126
127 err = FSGetNodeID( &theRef , &m_dirId , &m_isDir ) ;
128 #else
129 FSSpec fsspec ;
130
131 wxMacFilename2FSSpec( m_dirname , &fsspec ) ;
132 m_CPB.hFileInfo.ioVRefNum = fsspec.vRefNum ;
133
134 err = FSpGetDirectoryID( &fsspec , &m_dirId , &m_isDir ) ;
135 #endif
136 wxASSERT_MSG( (err == noErr) || (err == nsvErr) , "Error accessing directory " + m_dirname) ;
137
138 m_CPB.hFileInfo.ioNamePtr = m_name ;
139 m_index = 0 ;
140 }
141
142 wxDirData::~wxDirData()
143 {
144 }
145
146 void wxDirData::Rewind()
147 {
148 m_index = 0 ;
149 }
150
151 bool wxDirData::Read(wxString *filename)
152 {
153 if ( !m_isDir )
154 return FALSE ;
155
156 #if TARGET_CARBON
157 char c_name[256] ;
158 #endif
159 wxString result;
160
161 short err = noErr ;
162
163 while ( err == noErr )
164 {
165 m_index++ ;
166 m_CPB.dirInfo.ioFDirIndex = m_index;
167 m_CPB.dirInfo.ioDrDirID = m_dirId; /* we need to do this every time */
168 err = PBGetCatInfoSync((CInfoPBPtr)&m_CPB);
169 if ( err != noErr )
170 break ;
171
172 #if TARGET_CARBON
173 p2cstrcpy( c_name, m_name ) ;
174 strcpy( (char *)m_name, c_name);
175 #else
176 p2cstr( m_name ) ;
177 #endif
178 // its hidden but we don't want it
179 if ( ( m_CPB.hFileInfo.ioFlFndrInfo.fdFlags & kIsInvisible ) && !(m_flags & wxDIR_HIDDEN) )
180 continue ;
181 #ifdef __DARWIN__
182 // under X, names that start with '.' are hidden
183 if ( ( m_name[0] == '.' ) && !(m_flags & wxDIR_HIDDEN) )
184 continue;
185 #endif
186 #if TARGET_CARBON
187 // under X thats the way the mounting points look like
188 if ( ( m_CPB.dirInfo.ioDrDirID == 0 ) && ( m_flags & wxDIR_DIRS) )
189 break ;
190 #endif
191 // we have a directory
192 if ( ( m_CPB.dirInfo.ioFlAttrib & ioDirMask) != 0 && (m_flags & wxDIR_DIRS) )
193 break ;
194
195 // its a file but we don't want it
196 if ( ( m_CPB.dirInfo.ioFlAttrib & ioDirMask) == 0 && !(m_flags & wxDIR_FILES ) )
197 continue ;
198
199 wxString file( m_name ) ;
200 if ( m_filespec.IsEmpty() || m_filespec == "*.*" || m_filespec == "*" )
201 {
202 }
203 else if ( m_filespec.Length() > 1 && m_filespec.Left(1) =="*" )
204 {
205 if ( file.Right( m_filespec.Length() - 1 ).Upper() != m_filespec.Mid(1).Upper() )
206 {
207 continue ;
208 }
209 }
210 else if ( m_filespec.Length() > 1 && m_filespec.Right(1) == "*" )
211 {
212 if ( file.Left( m_filespec.Length() - 1 ).Upper() != m_filespec.Left( m_filespec.Length() - 1 ).Upper() )
213 {
214 continue ;
215 }
216 }
217 else if ( file.Upper() != m_filespec.Upper() )
218 {
219 continue ;
220 }
221
222 break ;
223 }
224 if ( err != noErr )
225 {
226 return FALSE ;
227 }
228
229 *filename = (char*) m_name ;
230
231 return TRUE;
232 }
233
234 // ----------------------------------------------------------------------------
235 // wxDir helpers
236 // ----------------------------------------------------------------------------
237
238 /* static */
239 bool wxDir::Exists(const wxString& dir)
240 {
241 return wxPathExists(dir);
242 }
243
244 // ----------------------------------------------------------------------------
245 // wxDir construction/destruction
246 // ----------------------------------------------------------------------------
247
248 wxDir::wxDir(const wxString& dirname)
249 {
250 m_data = NULL;
251
252 (void)Open(dirname);
253 }
254
255 bool wxDir::Open(const wxString& dirname)
256 {
257 delete M_DIR;
258 m_data = new wxDirData(dirname);
259
260 return TRUE;
261 }
262
263 bool wxDir::IsOpened() const
264 {
265 return m_data != NULL;
266 }
267
268 wxString wxDir::GetName() const
269 {
270 wxString name;
271 if ( m_data )
272 {
273 name = M_DIR->GetName();
274 if ( !name.empty() && (name.Last() == _T('/')) )
275 {
276 // chop off the last (back)slash
277 name.Truncate(name.length() - 1);
278 }
279 }
280
281 return name;
282 }
283
284 wxDir::~wxDir()
285 {
286 if (M_DIR != NULL) {
287 delete M_DIR;
288 m_data = NULL;
289 }
290 }
291
292 // ----------------------------------------------------------------------------
293 // wxDir enumerating
294 // ----------------------------------------------------------------------------
295
296 bool wxDir::GetFirst(wxString *filename,
297 const wxString& filespec,
298 int flags) const
299 {
300 wxCHECK_MSG( IsOpened(), FALSE, _T("must wxDir::Open() first") );
301
302 M_DIR->Rewind();
303
304 M_DIR->SetFileSpec(filespec);
305 M_DIR->SetFlags(flags);
306
307 return GetNext(filename);
308 }
309
310 bool wxDir::GetNext(wxString *filename) const
311 {
312 wxCHECK_MSG( IsOpened(), FALSE, _T("must wxDir::Open() first") );
313
314 wxCHECK_MSG( filename, FALSE, _T("bad pointer in wxDir::GetNext()") );
315
316 return M_DIR->Read(filename);
317 }