]> git.saurik.com Git - wxWidgets.git/blob - src/mgl/dirmgl.cpp
Give spin button the same height as the associated text control
[wxWidgets.git] / src / mgl / dirmgl.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mgl/dir.cpp
3 // Purpose: wxDir implementation for MGL
4 // Author: Vaclav Slavik, Vadim Zeitlin
5 // Modified by:
6 // Created: 2001/12/09
7 // RCS-ID: $Id$
8 // Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // (c) 2001-2002 SciTech Software, Inc. (www.scitechsoft.com)
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
12
13 // For compilers that support precompilation, includes "wx.h".
14 #include "wx/wxprec.h"
15
16 #ifdef __BORLANDC__
17 #pragma hdrstop
18 #endif
19
20 // ============================================================================
21 // declarations
22 // ============================================================================
23
24 // ----------------------------------------------------------------------------
25 // headers
26 // ----------------------------------------------------------------------------
27
28 #ifndef __UNIX__
29
30 #ifndef WX_PRECOMP
31 #include "wx/intl.h"
32 #include "wx/log.h"
33 #endif // PCH
34
35 #include "wx/dir.h"
36 #include "wx/filefn.h" // for wxMatchWild
37 #include "wx/mgl/private.h"
38
39 // ----------------------------------------------------------------------------
40 // macros
41 // ----------------------------------------------------------------------------
42
43 #define M_DIR ((wxDirData *)m_data)
44
45 // ----------------------------------------------------------------------------
46 // private classes
47 // ----------------------------------------------------------------------------
48
49 // this class stores everything we need to enumerate the files
50 class wxDirData
51 {
52 public:
53 wxDirData(const wxString& dirname);
54 ~wxDirData();
55
56 bool IsOk() const { return m_dir != NULL; }
57
58 void SetFileSpec(const wxString& filespec);
59 void SetFlags(int flags) { m_flags = flags; }
60
61 void Rewind();
62 bool Read(wxString *filename);
63
64 const wxString& GetName() const { return m_dirname; }
65
66 private:
67 void *m_dir;
68
69 wxString m_dirname;
70 wxString m_filespec;
71
72 int m_flags;
73 };
74
75 // ============================================================================
76 // implementation
77 // ============================================================================
78
79 // ----------------------------------------------------------------------------
80 // wxDirData
81 // ----------------------------------------------------------------------------
82
83 wxDirData::wxDirData(const wxString& dirname)
84 : m_dirname(dirname)
85 {
86 m_dir = NULL;
87
88 // throw away the trailing slashes
89 size_t n = m_dirname.length();
90 wxCHECK_RET( n, _T("empty dir name in wxDir") );
91
92 while ( n > 0 && m_dirname[--n] == wxFILE_SEP_PATH ) {}
93
94 m_dirname.Truncate(n + 1);
95 }
96
97 wxDirData::~wxDirData()
98 {
99 if ( m_dir )
100 PM_findClose(m_dir);
101 }
102
103 void wxDirData::SetFileSpec(const wxString& filespec)
104 {
105 #ifdef __DOS__
106 if ( filespec.empty() )
107 m_filespec = _T("*.*");
108 else
109 #endif
110 m_filespec = filespec;
111 }
112
113 void wxDirData::Rewind()
114 {
115 if ( m_dir )
116 {
117 PM_findClose(m_dir);
118 m_dir = NULL;
119 }
120 }
121
122 bool wxDirData::Read(wxString *filename)
123 {
124 PM_findData data;
125 bool matches = false;
126
127 data.dwSize = sizeof(data);
128
129 wxString path = m_dirname;
130 path += wxFILE_SEP_PATH;
131 path.reserve(path.length() + 255); // speed up string concatenation
132
133 while ( !matches )
134 {
135 if ( m_dir )
136 {
137 if ( !PM_findNextFile(m_dir, &data) )
138 return false;
139 }
140 else
141 {
142 m_dir = PM_findFirstFile(path + m_filespec , &data);
143 if ( m_dir == PM_FILE_INVALID )
144 {
145 m_dir = NULL;
146 return false;
147 }
148 }
149
150 // don't return "." and ".." unless asked for
151 if ( data.name[0] == '.' &&
152 ((data.name[1] == '.' && data.name[2] == '\0') ||
153 (data.name[1] == '\0')) )
154 {
155 if ( !(m_flags & wxDIR_DOTDOT) )
156 continue;
157
158 // we found a valid match
159 break;
160 }
161
162 // check the type now
163 if ( !(m_flags & wxDIR_FILES) && !(data.attrib & PM_FILE_DIRECTORY) )
164 {
165 // it's a file, but we don't want them
166 continue;
167 }
168 else if ( !(m_flags & wxDIR_DIRS) && (data.attrib & PM_FILE_DIRECTORY) )
169 {
170 // it's a dir, and we don't want it
171 continue;
172 }
173
174 matches = m_flags & wxDIR_HIDDEN ? true : !(data.attrib & PM_FILE_HIDDEN);
175 }
176
177 *filename = data.name;
178
179 return true;
180 }
181
182
183 // ----------------------------------------------------------------------------
184 // wxDir helpers
185 // ----------------------------------------------------------------------------
186
187 /* static */
188 bool wxDir::Exists(const wxString& dir)
189 {
190 return wxDirExists(dir);
191 }
192
193 // ----------------------------------------------------------------------------
194 // wxDir construction/destruction
195 // ----------------------------------------------------------------------------
196
197 wxDir::wxDir(const wxString& dirname)
198 {
199 m_data = NULL;
200
201 (void)Open(dirname);
202 }
203
204 bool wxDir::Open(const wxString& dirname)
205 {
206 delete M_DIR;
207 m_data = NULL;
208
209 if ( !wxDir::Exists(dirname) )
210 {
211 wxLogError(_("Directory '%s' doesn't exist!"), dirname.c_str());
212 return false;
213 }
214
215 m_data = new wxDirData(dirname);
216 return true;
217 }
218
219 bool wxDir::IsOpened() const
220 {
221 return m_data != NULL;
222 }
223
224 wxString wxDir::GetName() const
225 {
226 wxString name;
227 if ( m_data )
228 {
229 name = M_DIR->GetName();
230 if ( !name.empty() && (name.Last() == wxFILE_SEP_PATH) )
231 {
232 // chop off the last (back)slash
233 name.Truncate(name.length() - 1);
234 }
235 }
236
237 return name;
238 }
239
240 wxDir::~wxDir()
241 {
242 delete M_DIR;
243 }
244
245 // ----------------------------------------------------------------------------
246 // wxDir enumerating
247 // ----------------------------------------------------------------------------
248
249 bool wxDir::GetFirst(wxString *filename,
250 const wxString& filespec,
251 int flags) const
252 {
253 wxCHECK_MSG( IsOpened(), false, _T("must wxDir::Open() first") );
254
255 M_DIR->Rewind();
256
257 M_DIR->SetFileSpec(filespec);
258 M_DIR->SetFlags(flags);
259
260 return GetNext(filename);
261 }
262
263 bool wxDir::GetNext(wxString *filename) const
264 {
265 wxCHECK_MSG( IsOpened(), false, _T("must wxDir::Open() first") );
266
267 wxCHECK_MSG( filename, false, _T("bad pointer in wxDir::GetNext()") );
268
269 return M_DIR->Read(filename);
270 }
271
272 #endif // !__UNIX__