]> git.saurik.com Git - wxWidgets.git/blame - src/msw/dir.cpp
Modified mouse ENTER/LEAVE events so that they also
[wxWidgets.git] / src / msw / dir.cpp
CommitLineData
1944c6bd
VZ
1/////////////////////////////////////////////////////////////////////////////
2// Name: msw/dir.cpp
3// Purpose: wxDir implementation for Win32
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 08.12.99
7// RCS-ID: $Id$
8// Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
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
f6081a04
JS
31// For _A_SUBDIR, etc.
32#if defined(__BORLANDC__) && defined(__WIN16__)
33#include <dos.h>
34#endif
35
1944c6bd
VZ
36#ifndef WX_PRECOMP
37 #include "wx/intl.h"
38 #include "wx/log.h"
39#endif // PCH
40
41#include "wx/dir.h"
42#include "wx/filefn.h" // for wxPathExists()
43
8f177c8e
VZ
44// ----------------------------------------------------------------------------
45// define the types and functions used for file searching
46// ----------------------------------------------------------------------------
47
48// under Win16 use compiler-specific functions
49#ifdef __WIN16__
50 #ifdef __VISUALC__
51 #include <dos.h>
52 #include <errno.h>
53
54 typedef struct _find_t FIND_STRUCT;
55 #elif defined(__BORLANDC__)
56 #include <dir.h>
57
58 typedef struct ffblk FIND_STRUCT;
59 #else
60 #error "No directory searching functions for this compiler"
61 #endif
62
63 typedef FIND_STRUCT *FIND_DATA;
64 typedef char FIND_ATTR;
65
66 static inline FIND_DATA InitFindData() { return (FIND_DATA)NULL; }
67 static inline bool IsFindDataOk(FIND_DATA fd) { return fd != NULL; }
68 static inline void FreeFindData(FIND_DATA fd) { free(fd); }
69
70 static inline FIND_DATA FindFirst(const wxString& spec,
71 FIND_STRUCT * WXUNUSED(finddata))
72 {
73 // attribute to find all files
74 static const FIND_ATTR attr = 0x3F;
75
76 FIND_DATA fd = (FIND_DATA)malloc(sizeof(FIND_STRUCT));
77
78 if (
79 #ifdef __VISUALC__
80 _dos_findfirst(spec, attr, fd) == 0
81 #else // Borland
82 findfirst(spec, fd, attr) == 0
83 #endif
84 )
85 {
86 return fd;
87 }
88 else
89 {
90 free(fd);
91
92 return NULL;
93 }
94 }
95
96 static inline bool FindNext(FIND_DATA fd, FIND_STRUCT * WXUNUSED(finddata))
97 {
98 #ifdef __VISUALC__
99 return _dos_findnext(fd) == 0;
100 #else // Borland
101 return findnext(fd) == 0;
102 #endif
103 }
104
105 static const wxChar *GetNameFromFindData(FIND_STRUCT *finddata)
106 {
107 #ifdef __VISUALC__
108 return finddata->name;
109 #else // Borland
110 return finddata->ff_name;
111 #endif
112 }
113
114 static const FIND_ATTR GetAttrFromFindData(FIND_STRUCT *finddata)
115 {
116 #ifdef __VISUALC__
117 return finddata->attrib;
118 #else // Borland
119 return finddata->ff_attrib;
120 #endif
121 }
122
123 static inline bool IsDir(FIND_ATTR attr)
124 {
125 return (attr & _A_SUBDIR) != 0;
126 }
127
128 static inline bool IsHidden(FIND_ATTR attr)
129 {
130 return (attr & (_A_SYSTEM | _A_HIDDEN)) != 0;
131 }
132#else // Win32
133 #include <windows.h>
134
135 typedef WIN32_FIND_DATA FIND_STRUCT;
136 typedef HANDLE FIND_DATA;
137 typedef DWORD FIND_ATTR;
138
139 static inline FIND_DATA InitFindData() { return INVALID_HANDLE_VALUE; }
140
141 static inline bool IsFindDataOk(FIND_DATA fd)
142 {
143 return fd != INVALID_HANDLE_VALUE;
144 }
145
146 static inline void FreeFindData(FIND_DATA fd)
147 {
148 if ( !::FindClose(fd) )
149 {
150 wxLogLastError(_T("FindClose"));
151 }
152 }
153
154 static inline FIND_DATA FindFirst(const wxString& spec,
155 FIND_STRUCT *finddata)
156 {
c5a1681b 157 return ::FindFirstFile(spec, finddata);
8f177c8e
VZ
158 }
159
160 static inline bool FindNext(FIND_DATA fd, FIND_STRUCT *finddata)
161 {
162 return ::FindNextFile(fd, finddata) != 0;
163 }
164
165 static const wxChar *GetNameFromFindData(FIND_STRUCT *finddata)
166 {
167 return finddata->cFileName;
168 }
169
170 static const FIND_ATTR GetAttrFromFindData(FIND_STRUCT *finddata)
171 {
172 return finddata->dwFileAttributes;
173 }
174
175 static inline bool IsDir(FIND_ATTR attr)
176 {
177 return (attr & FILE_ATTRIBUTE_DIRECTORY) != 0;
178 }
179
180 static inline bool IsHidden(FIND_ATTR attr)
181 {
182 return (attr & (FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM)) != 0;
183 }
184#endif // __WIN16__
1944c6bd
VZ
185
186// ----------------------------------------------------------------------------
187// constants
188// ----------------------------------------------------------------------------
189
190#ifndef MAX_PATH
191 #define MAX_PATH 260 // from VC++ headers
192#endif
193
194// ----------------------------------------------------------------------------
195// macros
196// ----------------------------------------------------------------------------
197
198#define M_DIR ((wxDirData *)m_data)
199
200// ----------------------------------------------------------------------------
201// private classes
202// ----------------------------------------------------------------------------
203
204// this class stores everything we need to enumerate the files
205class wxDirData
206{
207public:
208 wxDirData(const wxString& dirname);
209 ~wxDirData();
210
211 void SetFileSpec(const wxString& filespec) { m_filespec = filespec; }
212 void SetFlags(int flags) { m_flags = flags; }
213
214 void Close();
215 void Rewind();
216 bool Read(wxString *filename);
217
218private:
8f177c8e 219 FIND_DATA m_finddata;
1944c6bd
VZ
220
221 wxString m_dirname;
222 wxString m_filespec;
223
224 int m_flags;
225};
226
227// ============================================================================
228// implementation
229// ============================================================================
230
231// ----------------------------------------------------------------------------
232// wxDirData
233// ----------------------------------------------------------------------------
234
235wxDirData::wxDirData(const wxString& dirname)
236 : m_dirname(dirname)
237{
8f177c8e 238 m_finddata = InitFindData();
1944c6bd
VZ
239}
240
241wxDirData::~wxDirData()
242{
243 Close();
244}
245
246void wxDirData::Close()
247{
8f177c8e 248 if ( IsFindDataOk(m_finddata) )
1944c6bd 249 {
8f177c8e 250 FreeFindData(m_finddata);
4afd7529 251
8f177c8e 252 m_finddata = InitFindData();
1944c6bd
VZ
253 }
254}
255
256void wxDirData::Rewind()
257{
258 Close();
259}
260
261bool wxDirData::Read(wxString *filename)
262{
263 bool first = FALSE;
264
8f177c8e 265#ifdef __WIN32__
1944c6bd 266 WIN32_FIND_DATA finddata;
8f177c8e
VZ
267 #define PTR_TO_FINDDATA (&finddata)
268#else // Win16
269 #define PTR_TO_FINDDATA (m_finddata)
270#endif
271
272 if ( !IsFindDataOk(m_finddata) )
1944c6bd
VZ
273 {
274 // open first
ad0dc53b
VZ
275 wxString filespec = m_dirname;
276 if ( !wxEndsWithPathSeparator(filespec) )
277 {
278 filespec += _T('\\');
279 }
280 filespec += (!m_filespec ? _T("*.*") : m_filespec.c_str());
4afd7529 281
8f177c8e 282 m_finddata = FindFirst(filespec, PTR_TO_FINDDATA);
1944c6bd
VZ
283
284 first = TRUE;
285 }
286
8f177c8e 287 if ( !IsFindDataOk(m_finddata) )
1944c6bd 288 {
8f177c8e 289#ifdef __WIN32__
1944c6bd
VZ
290 DWORD err = ::GetLastError();
291
4afd7529 292 if ( err != ERROR_FILE_NOT_FOUND )
1944c6bd
VZ
293 {
294 wxLogSysError(err, _("Can not enumerate files in directory '%s'"),
295 m_dirname.c_str());
296 }
8f177c8e 297#endif // __WIN32__
1944c6bd
VZ
298 //else: not an error, just no (such) files
299
300 return FALSE;
301 }
302
1944c6bd 303 const wxChar *name;
8f177c8e 304 FIND_ATTR attr;
1944c6bd 305
4afd7529 306 for ( ;; )
1944c6bd
VZ
307 {
308 if ( first )
309 {
310 first = FALSE;
311 }
312 else
313 {
8f177c8e 314 if ( !FindNext(m_finddata, PTR_TO_FINDDATA) )
1944c6bd 315 {
8f177c8e 316#ifdef __WIN32__
1944c6bd
VZ
317 DWORD err = ::GetLastError();
318
319 if ( err != ERROR_NO_MORE_FILES )
320 {
321 wxLogLastError(_T("FindNext"));
322 }
8f177c8e 323#endif // __WIN32__
1944c6bd
VZ
324 //else: not an error, just no more (such) files
325
326 return FALSE;
327 }
328 }
329
8f177c8e
VZ
330 name = GetNameFromFindData(PTR_TO_FINDDATA);
331 attr = GetAttrFromFindData(PTR_TO_FINDDATA);
1944c6bd
VZ
332
333 // don't return "." and ".." unless asked for
334 if ( name[0] == _T('.') &&
335 ((name[1] == _T('.') && name[2] == _T('\0')) ||
336 (name[1] == _T('\0'))) )
337 {
338 if ( !(m_flags & wxDIR_DOTDOT) )
339 continue;
340 }
341
342 // check the type now
8f177c8e 343 if ( !(m_flags & wxDIR_FILES) && !IsDir(attr) )
1944c6bd
VZ
344 {
345 // it's a file, but we don't want them
346 continue;
347 }
8f177c8e 348 else if ( !(m_flags & wxDIR_DIRS) && IsDir(attr) )
1944c6bd
VZ
349 {
350 // it's a dir, and we don't want it
351 continue;
352 }
353
354 // finally, check whether it's a hidden file
355 if ( !(m_flags & wxDIR_HIDDEN) )
356 {
8f177c8e 357 if ( IsHidden(attr) )
4afd7529
VZ
358 {
359 // it's a hidden file, skip it
360 continue;
361 }
1944c6bd 362 }
1944c6bd 363
4afd7529
VZ
364 *filename = name;
365
366 break;
367 }
1944c6bd
VZ
368
369 return TRUE;
370}
371
372// ----------------------------------------------------------------------------
373// wxDir helpers
374// ----------------------------------------------------------------------------
375
376/* static */
377bool wxDir::Exists(const wxString& dir)
378{
379 return wxPathExists(dir);
380}
381
382// ----------------------------------------------------------------------------
383// wxDir construction/destruction
384// ----------------------------------------------------------------------------
385
386wxDir::wxDir(const wxString& dirname)
387{
388 m_data = NULL;
389
390 (void)Open(dirname);
391}
392
393bool wxDir::Open(const wxString& dirname)
394{
395 delete M_DIR;
396 m_data = new wxDirData(dirname);
397
398 return TRUE;
399}
400
401bool wxDir::IsOpened() const
402{
403 return m_data != NULL;
404}
405
406wxDir::~wxDir()
407{
408 delete M_DIR;
409}
410
411// ----------------------------------------------------------------------------
412// wxDir enumerating
413// ----------------------------------------------------------------------------
414
415bool wxDir::GetFirst(wxString *filename,
416 const wxString& filespec,
417 int flags) const
418{
419 wxCHECK_MSG( IsOpened(), FALSE, _T("must wxDir::Open() first") );
420
421 M_DIR->Rewind();
422
423 M_DIR->SetFileSpec(filespec);
424 M_DIR->SetFlags(flags);
425
426 return GetNext(filename);
427}
428
429bool wxDir::GetNext(wxString *filename) const
430{
431 wxCHECK_MSG( IsOpened(), FALSE, _T("must wxDir::Open() first") );
432
433 wxCHECK_MSG( filename, FALSE, _T("bad pointer in wxDir::GetNext()") );
434
435 return M_DIR->Read(filename);
436}