]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/dirmac.cpp
changing guard to support popupwindows as well
[wxWidgets.git] / src / mac / carbon / dirmac.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: mac/dirmac.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 licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #include "wx/dir.h"
28
29 #ifndef WX_PRECOMP
30 #include "wx/intl.h"
31 #include "wx/log.h"
32 #endif // PCH
33
34 #include "wx/filefn.h" // for wxDirExists()
35 #include "wx/filename.h"
36 #include "wx/mac/private.h"
37
38 // ----------------------------------------------------------------------------
39 // private classes
40 // ----------------------------------------------------------------------------
41
42 // this class stores everything we need to enumerate the files
43 class wxDirData
44 {
45 public:
46 wxDirData(const wxString& dirname);
47 ~wxDirData();
48
49 void Close() ;
50 void SetFileSpec(const wxString& filespec) { m_filespec = filespec; }
51 void SetFlags(int flags) { m_flags = flags; }
52
53 bool Read(wxString *filename); // reads the next
54 void Rewind() ;
55
56 const wxString& GetName() const { return m_dirname; }
57
58 private:
59 FSIterator m_iterator ;
60
61 wxString m_dirname;
62 wxString m_filespec;
63
64 int m_flags;
65 };
66
67 // ============================================================================
68 // implementation
69 // ============================================================================
70
71 // ----------------------------------------------------------------------------
72 // wxDirData
73 // ----------------------------------------------------------------------------
74
75 wxDirData::wxDirData(const wxString& dirname)
76 : m_dirname(dirname)
77 {
78 // throw away the trailing slashes
79 size_t n = m_dirname.length();
80 wxCHECK_RET( n, _T("empty dir name in wxDir") );
81
82 while ( n > 0 && wxIsPathSeparator(m_dirname[--n]) )
83 ;
84
85 m_dirname.Truncate(n + 1);
86 m_iterator = NULL ;
87 }
88
89 wxDirData::~wxDirData()
90 {
91 Close() ;
92 }
93
94 void wxDirData::Close()
95 {
96 if ( m_iterator )
97 {
98 FSCloseIterator( m_iterator ) ;
99 m_iterator = NULL ;
100 }
101 }
102
103 void wxDirData::Rewind()
104 {
105 Close() ;
106 }
107
108 bool wxDirData::Read(wxString *filename)
109 {
110 wxString result;
111 OSStatus err = noErr ;
112 if ( NULL == m_iterator )
113 {
114 FSRef dirRef;
115 err = wxMacPathToFSRef( m_dirname , &dirRef ) ;
116 if ( err == noErr )
117 {
118 err = FSOpenIterator(&dirRef, kFSIterateFlat, &m_iterator);
119 }
120 if ( err )
121 {
122 Close() ;
123 return false ;
124 }
125 }
126
127 wxString name ;
128
129 while( noErr == err )
130 {
131 HFSUniStr255 uniname ;
132 FSRef fileRef;
133 FSCatalogInfo catalogInfo;
134 ItemCount fetched = 0;
135
136 err = FSGetCatalogInfoBulk( m_iterator, 1, &fetched, NULL, kFSCatInfoNodeFlags | kFSCatInfoFinderInfo , &catalogInfo , &fileRef, NULL, &uniname );
137
138 // expected error codes
139
140 if ( errFSNoMoreItems == err )
141 return false ;
142 if ( afpAccessDenied == err )
143 return false ;
144
145 if ( noErr != err )
146 break ;
147
148 name = wxMacHFSUniStrToString( &uniname ) ;
149
150 if ( ( name == wxT(".") || name == wxT("..") ) && !(m_flags & wxDIR_DOTDOT) )
151 continue;
152
153 if ( ( name[0U] == '.' ) && !(m_flags & wxDIR_HIDDEN ) )
154 continue ;
155
156 if ( (((FileInfo*)&catalogInfo.finderInfo)->finderFlags & kIsInvisible ) && !(m_flags & wxDIR_HIDDEN ) )
157 continue ;
158
159 // its a dir and we don't want it
160 if ( (catalogInfo.nodeFlags & kFSNodeIsDirectoryMask) && !(m_flags & wxDIR_DIRS) )
161 continue ;
162
163 // its a file but we don't want it
164 if ( (catalogInfo.nodeFlags & kFSNodeIsDirectoryMask) == 0 && !(m_flags & wxDIR_FILES ) )
165 continue ;
166
167 if ( m_filespec.empty() || m_filespec == wxT("*.*") || m_filespec == wxT("*") )
168 {
169 }
170 else if ( !wxMatchWild(m_filespec, name , false) )
171 {
172 continue ;
173 }
174
175 break ;
176 }
177 if ( err != noErr )
178 {
179 return false ;
180 }
181
182 *filename = name ;
183 return true;
184 }
185
186 // ----------------------------------------------------------------------------
187 // wxDir helpers
188 // ----------------------------------------------------------------------------
189
190 /* static */
191 bool wxDir::Exists(const wxString& dir)
192 {
193 return wxDirExists(dir);
194 }
195
196 // ----------------------------------------------------------------------------
197 // wxDir construction/destruction
198 // ----------------------------------------------------------------------------
199
200 wxDir::wxDir(const wxString& dirname)
201 {
202 m_data = NULL;
203
204 (void)Open(dirname);
205 }
206
207 bool wxDir::Open(const wxString& dirname)
208 {
209 delete m_data;
210 m_data = new wxDirData(dirname);
211
212 return true;
213 }
214
215 bool wxDir::IsOpened() const
216 {
217 return m_data != NULL;
218 }
219
220 wxString wxDir::GetName() const
221 {
222 wxString name;
223 if ( m_data )
224 {
225 name = m_data->GetName();
226 if ( !name.empty() && (name.Last() == _T('/')) )
227 {
228 // chop off the last (back)slash
229 name.Truncate(name.length() - 1);
230 }
231 }
232
233 return name;
234 }
235
236 wxDir::~wxDir()
237 {
238 delete m_data;
239 m_data = NULL;
240 }
241
242 // ----------------------------------------------------------------------------
243 // wxDir enumerating
244 // ----------------------------------------------------------------------------
245
246 bool wxDir::GetFirst(wxString *filename,
247 const wxString& filespec,
248 int flags) const
249 {
250 wxCHECK_MSG( IsOpened(), false, _T("must wxDir::Open() first") );
251
252 m_data->Rewind();
253
254 m_data->SetFileSpec(filespec);
255 m_data->SetFlags(flags);
256
257 return GetNext(filename);
258 }
259
260 bool wxDir::GetNext(wxString *filename) const
261 {
262 wxCHECK_MSG( IsOpened(), false, _T("must wxDir::Open() first") );
263
264 wxCHECK_MSG( filename, false, _T("bad pointer in wxDir::GetNext()") );
265
266 return m_data->Read(filename);
267 }