]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/dirmac.cpp
Use GetCursorPos on WinCE wherever possible (otherwise the wrong
[wxWidgets.git] / src / mac / carbon / dirmac.cpp
CommitLineData
0207122d
SC
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>
65571936 9// Licence: wxWindows licence
0207122d
SC
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
3d1a4878 20#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
0207122d
SC
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"
da865fdd 37#include "wx/filefn.h" // for wxDirExists()
0207122d 38
f5c6eb5c 39#ifndef __DARWIN__
03e11df5
GD
40 #include <windows.h>
41#endif
0207122d 42
a2b77260 43#include "wx/filename.h"
76a5e5d2
SC
44#include "wx/mac/private.h"
45
a2b77260 46#include "MoreFilesX.h"
0207122d
SC
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
67class wxDirData
68{
69public:
70 wxDirData(const wxString& dirname);
71 ~wxDirData();
da865fdd 72
a2b77260 73 void Close() ;
0207122d
SC
74 void SetFileSpec(const wxString& filespec) { m_filespec = filespec; }
75 void SetFlags(int flags) { m_flags = flags; }
76
da865fdd 77 bool Read(wxString *filename); // reads the next
0207122d
SC
78 void Rewind() ;
79
f4ac0693
GD
80 const wxString& GetName() const { return m_dirname; }
81
0207122d 82private:
a2b77260 83 FSIterator m_iterator ;
0207122d
SC
84
85 wxString m_dirname;
86 wxString m_filespec;
87
88 int m_flags;
89};
90
91// ============================================================================
92// implementation
93// ============================================================================
94
95// ----------------------------------------------------------------------------
96// wxDirData
97// ----------------------------------------------------------------------------
98
99wxDirData::wxDirData(const wxString& dirname)
100 : m_dirname(dirname)
101{
102 // throw away the trailing slashes
103 size_t n = m_dirname.length();
104 wxCHECK_RET( n, _T("empty dir name in wxDir") );
105
106 while ( n > 0 && wxIsPathSeparator(m_dirname[--n]) )
107 ;
108
109 m_dirname.Truncate(n + 1);
a2b77260 110 m_iterator = NULL ;
0207122d
SC
111}
112
113wxDirData::~wxDirData()
114{
a2b77260
SC
115 Close() ;
116}
117
118void wxDirData::Close()
da865fdd 119{
a2b77260
SC
120 if ( m_iterator )
121 {
122 FSCloseIterator( m_iterator ) ;
123 m_iterator = NULL ;
124 }
0207122d
SC
125}
126
da865fdd 127void wxDirData::Rewind()
0207122d 128{
a2b77260 129 Close() ;
0207122d
SC
130}
131
132bool wxDirData::Read(wxString *filename)
da865fdd 133{
0207122d 134 wxString result;
a2b77260
SC
135 OSStatus err = noErr ;
136 if ( NULL == m_iterator )
137 {
138 FSRef dirRef;
139 err = wxMacPathToFSRef( m_dirname , &dirRef ) ;
140 if ( err == noErr )
141 {
da865fdd
WS
142 err = FSOpenIterator(&dirRef, kFSIterateFlat, &m_iterator);
143 }
144 if ( err )
145 {
146 Close() ;
147 return false ;
148 }
a2b77260 149 }
da865fdd 150
a2b77260 151 wxString name ;
da865fdd 152
a2b77260 153 while( noErr == err )
2d4e4f80 154 {
a2b77260
SC
155 HFSUniStr255 uniname ;
156 FSRef fileRef;
157 FSCatalogInfo catalogInfo;
158 UInt32 fetched = 0;
159
160 err = FSGetCatalogInfoBulk( m_iterator, 1, &fetched, NULL, kFSCatInfoNodeFlags | kFSCatInfoFinderInfo , &catalogInfo , &fileRef, NULL, &uniname );
161 if ( errFSNoMoreItems == err )
162 return false ;
da865fdd 163
a2b77260 164 wxASSERT( noErr == err ) ;
da865fdd 165
a2b77260 166 if ( noErr != err )
2d4e4f80 167 break ;
da865fdd 168
a2b77260
SC
169 name = wxMacHFSUniStrToString( &uniname ) ;
170
171 if ( ( name == wxT(".") || name == wxT("..") ) && !(m_flags & wxDIR_DOTDOT) )
172 continue;
173
174 if ( ( name[0U] == '.' ) && !(m_flags & wxDIR_HIDDEN ) )
175 continue ;
176
177 if ( (((FileInfo*)&catalogInfo.finderInfo)->finderFlags & kIsInvisible ) && !(m_flags & wxDIR_HIDDEN ) )
178 continue ;
da865fdd 179
7c19f4d2
RN
180 // its a dir and we don't want it
181 if ( (catalogInfo.nodeFlags & kFSNodeIsDirectoryMask) && !(m_flags & wxDIR_DIRS) )
182 continue ;
a2b77260 183
2d4e4f80 184 // its a file but we don't want it
a2b77260 185 if ( (catalogInfo.nodeFlags & kFSNodeIsDirectoryMask) == 0 && !(m_flags & wxDIR_FILES ) )
2d4e4f80 186 continue ;
da865fdd
WS
187
188 if ( m_filespec.empty() || m_filespec == wxT("*.*") || m_filespec == wxT("*") )
2d4e4f80
GD
189 {
190 }
da865fdd 191 else if ( !wxMatchWild(m_filespec, name , false) )
2d4e4f80
GD
192 {
193 continue ;
194 }
da865fdd 195
2d4e4f80
GD
196 break ;
197 }
198 if ( err != noErr )
199 {
da865fdd 200 return false ;
2d4e4f80 201 }
da865fdd 202
a2b77260 203 *filename = name ;
da865fdd 204 return true;
0207122d
SC
205}
206
207// ----------------------------------------------------------------------------
208// wxDir helpers
209// ----------------------------------------------------------------------------
210
211/* static */
212bool wxDir::Exists(const wxString& dir)
213{
da865fdd 214 return wxDirExists(dir);
0207122d
SC
215}
216
217// ----------------------------------------------------------------------------
218// wxDir construction/destruction
219// ----------------------------------------------------------------------------
220
221wxDir::wxDir(const wxString& dirname)
222{
223 m_data = NULL;
224
225 (void)Open(dirname);
226}
227
228bool wxDir::Open(const wxString& dirname)
229{
230 delete M_DIR;
231 m_data = new wxDirData(dirname);
facd6764 232
da865fdd 233 return true;
0207122d
SC
234}
235
236bool wxDir::IsOpened() const
237{
238 return m_data != NULL;
239}
240
f4ac0693
GD
241wxString wxDir::GetName() const
242{
243 wxString name;
244 if ( m_data )
245 {
e40298d5
JS
246 name = M_DIR->GetName();
247 if ( !name.empty() && (name.Last() == _T('/')) )
248 {
249 // chop off the last (back)slash
250 name.Truncate(name.length() - 1);
251 }
f4ac0693
GD
252 }
253
254 return name;
255}
256
0207122d
SC
257wxDir::~wxDir()
258{
f5bb2251
GD
259 if (M_DIR != NULL) {
260 delete M_DIR;
261 m_data = NULL;
262 }
0207122d
SC
263}
264
265// ----------------------------------------------------------------------------
266// wxDir enumerating
267// ----------------------------------------------------------------------------
268
269bool wxDir::GetFirst(wxString *filename,
270 const wxString& filespec,
271 int flags) const
272{
da865fdd 273 wxCHECK_MSG( IsOpened(), false, _T("must wxDir::Open() first") );
0207122d
SC
274
275 M_DIR->Rewind();
276
277 M_DIR->SetFileSpec(filespec);
278 M_DIR->SetFlags(flags);
279
280 return GetNext(filename);
281}
282
283bool wxDir::GetNext(wxString *filename) const
284{
da865fdd 285 wxCHECK_MSG( IsOpened(), false, _T("must wxDir::Open() first") );
0207122d 286
da865fdd 287 wxCHECK_MSG( filename, false, _T("bad pointer in wxDir::GetNext()") );
0207122d
SC
288
289 return M_DIR->Read(filename);
290}