]> git.saurik.com Git - wxWidgets.git/blame - src/msw/dir.cpp
Don't use any icon for items inserted without one in wxMSW wxListCtrl.
[wxWidgets.git] / src / msw / dir.cpp
CommitLineData
1944c6bd 1/////////////////////////////////////////////////////////////////////////////
80fdcdb9 2// Name: src/msw/dir.cpp
1944c6bd
VZ
3// Purpose: wxDir implementation for Win32
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 08.12.99
1944c6bd 7// Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
65571936 8// Licence: wxWindows licence
1944c6bd
VZ
9/////////////////////////////////////////////////////////////////////////////
10
11// ============================================================================
12// declarations
13// ============================================================================
14
15// ----------------------------------------------------------------------------
16// headers
17// ----------------------------------------------------------------------------
18
1944c6bd
VZ
19// For compilers that support precompilation, includes "wx.h".
20#include "wx/wxprec.h"
21
22#ifdef __BORLANDC__
23 #pragma hdrstop
24#endif
25
26#ifndef WX_PRECOMP
27 #include "wx/intl.h"
28 #include "wx/log.h"
29#endif // PCH
30
31#include "wx/dir.h"
1944c6bd 32
532d575b 33#ifdef __WINDOWS__
4676948b
JS
34 #include "wx/msw/private.h"
35#endif
36
8f177c8e
VZ
37// ----------------------------------------------------------------------------
38// define the types and functions used for file searching
39// ----------------------------------------------------------------------------
40
145bbf1f
VZ
41namespace
42{
43
3a5bcc4d
VZ
44typedef WIN32_FIND_DATA FIND_STRUCT;
45typedef HANDLE FIND_DATA;
46typedef DWORD FIND_ATTR;
8f177c8e 47
145bbf1f
VZ
48inline FIND_DATA InitFindData()
49{
50 return INVALID_HANDLE_VALUE;
51}
8f177c8e 52
145bbf1f 53inline bool IsFindDataOk(FIND_DATA fd)
3a5bcc4d 54{
8f177c8e 55 return fd != INVALID_HANDLE_VALUE;
3a5bcc4d 56}
8f177c8e 57
145bbf1f 58inline void FreeFindData(FIND_DATA fd)
3a5bcc4d 59{
145bbf1f
VZ
60 if ( !::FindClose(fd) )
61 {
9a83f860 62 wxLogLastError(wxT("FindClose"));
145bbf1f 63 }
3a5bcc4d 64}
8f177c8e 65
4daceaac 66const wxChar *GetNameFromFindData(const FIND_STRUCT *finddata)
3a5bcc4d 67{
4daceaac 68 return finddata->cFileName;
3a5bcc4d 69}
8f177c8e 70
4daceaac
VZ
71// Helper function checking that the contents of the given FIND_STRUCT really
72// match our filter. We need to do it ourselves as native Windows functions
73// apply the filter to both the long and the short names of the file, so
74// something like "*.bar" matches "foo.bar.baz" too and not only "foo.bar", so
75// we have to double check that we have a real match.
76inline bool
77CheckFoundMatch(const FIND_STRUCT* finddata, const wxString& filter)
3a5bcc4d 78{
512b03db
VZ
79 // If there is no filter, the found file must be the one we really are
80 // looking for.
81 if ( filter.empty() )
82 return true;
83
84 // Otherwise do check the match validity. Notice that we must do it
85 // case-insensitively because the case of the file names is not supposed to
86 // matter under Windows.
87 wxString fn(GetNameFromFindData(finddata));
88
89 // However if the filter contains only special characters (which is a
90 // common case), we can skip the case conversion.
91 if ( filter.find_first_not_of(wxS("*?.")) == wxString::npos )
92 return fn.Matches(filter);
93
94 return fn.MakeUpper().Matches(filter.Upper());
3a5bcc4d 95}
8f177c8e 96
4daceaac
VZ
97inline bool
98FindNext(FIND_DATA fd, const wxString& filter, FIND_STRUCT *finddata)
3a5bcc4d 99{
4daceaac
VZ
100 for ( ;; )
101 {
102 if ( !::FindNextFile(fd, finddata) )
103 return false;
104
105 // If we did find something, check that it really matches.
106 if ( CheckFoundMatch(finddata, filter) )
107 return true;
108 }
109}
110
111inline FIND_DATA
112FindFirst(const wxString& spec,
113 const wxString& filter,
114 FIND_STRUCT *finddata)
115{
116 FIND_DATA fd = ::FindFirstFile(spec.t_str(), finddata);
117
118 // As in FindNext() above, we need to check that the file name we found
119 // really matches our filter and look for the next match if it doesn't.
120 if ( IsFindDataOk(fd) && !CheckFoundMatch(finddata, filter) )
121 {
122 if ( !FindNext(fd, filter, finddata) )
123 {
124 // As we return the invalid handle from here to indicate that we
125 // didn't find anything, close the one we initially received
126 // ourselves.
127 FreeFindData(fd);
128
129 return INVALID_HANDLE_VALUE;
130 }
131 }
132
133 return fd;
3a5bcc4d 134}
8f177c8e 135
145bbf1f 136inline FIND_ATTR GetAttrFromFindData(FIND_STRUCT *finddata)
3a5bcc4d 137{
145bbf1f 138 return finddata->dwFileAttributes;
3a5bcc4d 139}
8f177c8e 140
145bbf1f 141inline bool IsDir(FIND_ATTR attr)
3a5bcc4d 142{
145bbf1f 143 return (attr & FILE_ATTRIBUTE_DIRECTORY) != 0;
3a5bcc4d 144}
8f177c8e 145
145bbf1f 146inline bool IsHidden(FIND_ATTR attr)
3a5bcc4d 147{
145bbf1f 148 return (attr & (FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM)) != 0;
3a5bcc4d 149}
1944c6bd 150
145bbf1f
VZ
151} // anonymous namespace
152
1944c6bd
VZ
153// ----------------------------------------------------------------------------
154// constants
155// ----------------------------------------------------------------------------
156
157#ifndef MAX_PATH
158 #define MAX_PATH 260 // from VC++ headers
159#endif
160
161// ----------------------------------------------------------------------------
162// macros
163// ----------------------------------------------------------------------------
164
165#define M_DIR ((wxDirData *)m_data)
166
167// ----------------------------------------------------------------------------
168// private classes
169// ----------------------------------------------------------------------------
170
171// this class stores everything we need to enumerate the files
172class wxDirData
173{
174public:
175 wxDirData(const wxString& dirname);
176 ~wxDirData();
177
178 void SetFileSpec(const wxString& filespec) { m_filespec = filespec; }
179 void SetFlags(int flags) { m_flags = flags; }
180
181 void Close();
182 void Rewind();
183 bool Read(wxString *filename);
184
35332784
VZ
185 const wxString& GetName() const { return m_dirname; }
186
1944c6bd 187private:
8f177c8e 188 FIND_DATA m_finddata;
1944c6bd
VZ
189
190 wxString m_dirname;
191 wxString m_filespec;
192
193 int m_flags;
22f3361e 194
c0c133e1 195 wxDECLARE_NO_COPY_CLASS(wxDirData);
1944c6bd
VZ
196};
197
198// ============================================================================
199// implementation
200// ============================================================================
201
202// ----------------------------------------------------------------------------
203// wxDirData
204// ----------------------------------------------------------------------------
205
206wxDirData::wxDirData(const wxString& dirname)
207 : m_dirname(dirname)
208{
8f177c8e 209 m_finddata = InitFindData();
1944c6bd
VZ
210}
211
212wxDirData::~wxDirData()
213{
214 Close();
215}
216
217void wxDirData::Close()
218{
8f177c8e 219 if ( IsFindDataOk(m_finddata) )
1944c6bd 220 {
8f177c8e 221 FreeFindData(m_finddata);
4afd7529 222
8f177c8e 223 m_finddata = InitFindData();
1944c6bd
VZ
224 }
225}
226
227void wxDirData::Rewind()
228{
229 Close();
230}
231
232bool wxDirData::Read(wxString *filename)
233{
d71cc120 234 bool first = false;
1944c6bd
VZ
235
236 WIN32_FIND_DATA finddata;
8f177c8e 237 #define PTR_TO_FINDDATA (&finddata)
8f177c8e
VZ
238
239 if ( !IsFindDataOk(m_finddata) )
1944c6bd
VZ
240 {
241 // open first
ad0dc53b
VZ
242 wxString filespec = m_dirname;
243 if ( !wxEndsWithPathSeparator(filespec) )
244 {
9a83f860 245 filespec += wxT('\\');
ad0dc53b 246 }
c9f78968 247 if ( !m_filespec )
9a83f860 248 filespec += wxT("*.*");
c9f78968
VS
249 else
250 filespec += m_filespec;
4afd7529 251
4daceaac 252 m_finddata = FindFirst(filespec, m_filespec, PTR_TO_FINDDATA);
1944c6bd 253
d71cc120 254 first = true;
1944c6bd
VZ
255 }
256
8f177c8e 257 if ( !IsFindDataOk(m_finddata) )
1944c6bd 258 {
8f177c8e 259#ifdef __WIN32__
1944c6bd
VZ
260 DWORD err = ::GetLastError();
261
5613bc20 262 if ( err != ERROR_FILE_NOT_FOUND && err != ERROR_NO_MORE_FILES )
1944c6bd 263 {
4c51a665 264 wxLogSysError(err, _("Cannot enumerate files in directory '%s'"),
1944c6bd
VZ
265 m_dirname.c_str());
266 }
8f177c8e 267#endif // __WIN32__
1944c6bd
VZ
268 //else: not an error, just no (such) files
269
d71cc120 270 return false;
1944c6bd
VZ
271 }
272
1944c6bd 273 const wxChar *name;
8f177c8e 274 FIND_ATTR attr;
1944c6bd 275
4afd7529 276 for ( ;; )
1944c6bd
VZ
277 {
278 if ( first )
279 {
d71cc120 280 first = false;
1944c6bd
VZ
281 }
282 else
283 {
4daceaac 284 if ( !FindNext(m_finddata, m_filespec, PTR_TO_FINDDATA) )
1944c6bd 285 {
8f177c8e 286#ifdef __WIN32__
1944c6bd
VZ
287 DWORD err = ::GetLastError();
288
289 if ( err != ERROR_NO_MORE_FILES )
290 {
9a83f860 291 wxLogLastError(wxT("FindNext"));
1944c6bd 292 }
8f177c8e 293#endif // __WIN32__
1944c6bd
VZ
294 //else: not an error, just no more (such) files
295
d71cc120 296 return false;
1944c6bd
VZ
297 }
298 }
299
8f177c8e
VZ
300 name = GetNameFromFindData(PTR_TO_FINDDATA);
301 attr = GetAttrFromFindData(PTR_TO_FINDDATA);
1944c6bd
VZ
302
303 // don't return "." and ".." unless asked for
9a83f860
VZ
304 if ( name[0] == wxT('.') &&
305 ((name[1] == wxT('.') && name[2] == wxT('\0')) ||
306 (name[1] == wxT('\0'))) )
1944c6bd
VZ
307 {
308 if ( !(m_flags & wxDIR_DOTDOT) )
309 continue;
310 }
311
312 // check the type now
8f177c8e 313 if ( !(m_flags & wxDIR_FILES) && !IsDir(attr) )
1944c6bd
VZ
314 {
315 // it's a file, but we don't want them
316 continue;
317 }
8f177c8e 318 else if ( !(m_flags & wxDIR_DIRS) && IsDir(attr) )
1944c6bd
VZ
319 {
320 // it's a dir, and we don't want it
321 continue;
322 }
323
324 // finally, check whether it's a hidden file
325 if ( !(m_flags & wxDIR_HIDDEN) )
326 {
8f177c8e 327 if ( IsHidden(attr) )
4afd7529
VZ
328 {
329 // it's a hidden file, skip it
330 continue;
331 }
1944c6bd 332 }
1944c6bd 333
4afd7529
VZ
334 *filename = name;
335
336 break;
337 }
1944c6bd 338
d71cc120 339 return true;
1944c6bd
VZ
340}
341
1944c6bd
VZ
342// ----------------------------------------------------------------------------
343// wxDir construction/destruction
344// ----------------------------------------------------------------------------
345
346wxDir::wxDir(const wxString& dirname)
347{
348 m_data = NULL;
349
350 (void)Open(dirname);
351}
352
353bool wxDir::Open(const wxString& dirname)
354{
355 delete M_DIR;
03647350 356
89432923
RR
357 // The Unix code does a similar test
358 if (wxDirExists(dirname))
359 {
360 m_data = new wxDirData(dirname);
1944c6bd 361
89432923
RR
362 return true;
363 }
364 else
365 {
366 m_data = NULL;
03647350 367
89432923
RR
368 return false;
369 }
1944c6bd
VZ
370}
371
372bool wxDir::IsOpened() const
373{
374 return m_data != NULL;
375}
376
35332784
VZ
377wxString wxDir::GetName() const
378{
379 wxString name;
380 if ( m_data )
381 {
382 name = M_DIR->GetName();
383 if ( !name.empty() )
384 {
385 // bring to canonical Windows form
9a83f860 386 name.Replace(wxT("/"), wxT("\\"));
35332784 387
9a83f860 388 if ( name.Last() == wxT('\\') )
35332784
VZ
389 {
390 // chop off the last (back)slash
391 name.Truncate(name.length() - 1);
392 }
393 }
394 }
395
396 return name;
397}
398
6619c4af 399void wxDir::Close()
1944c6bd 400{
6619c4af
VZ
401 if ( m_data )
402 {
403 delete m_data;
404 m_data = NULL;
405 }
1944c6bd
VZ
406}
407
408// ----------------------------------------------------------------------------
409// wxDir enumerating
410// ----------------------------------------------------------------------------
411
412bool wxDir::GetFirst(wxString *filename,
413 const wxString& filespec,
414 int flags) const
415{
9a83f860 416 wxCHECK_MSG( IsOpened(), false, wxT("must wxDir::Open() first") );
1944c6bd
VZ
417
418 M_DIR->Rewind();
419
420 M_DIR->SetFileSpec(filespec);
421 M_DIR->SetFlags(flags);
422
423 return GetNext(filename);
424}
425
426bool wxDir::GetNext(wxString *filename) const
427{
9a83f860 428 wxCHECK_MSG( IsOpened(), false, wxT("must wxDir::Open() first") );
1944c6bd 429
9a83f860 430 wxCHECK_MSG( filename, false, wxT("bad pointer in wxDir::GetNext()") );
1944c6bd
VZ
431
432 return M_DIR->Read(filename);
433}
2b5f62a0
VZ
434
435// ----------------------------------------------------------------------------
436// wxGetDirectoryTimes: used by wxFileName::GetTimes()
437// ----------------------------------------------------------------------------
438
439#ifdef __WIN32__
440
441extern bool
442wxGetDirectoryTimes(const wxString& dirname,
443 FILETIME *ftAccess, FILETIME *ftCreate, FILETIME *ftMod)
444{
4de1b652
JS
445#ifdef __WXWINCE__
446 // FindFirst() is going to fail
447 wxASSERT_MSG( !dirname.empty(),
9a83f860 448 wxT("incorrect directory name format in wxGetDirectoryTimes") );
4de1b652 449#else
2b5f62a0 450 // FindFirst() is going to fail
9a83f860
VZ
451 wxASSERT_MSG( !dirname.empty() && dirname.Last() != wxT('\\'),
452 wxT("incorrect directory name format in wxGetDirectoryTimes") );
4de1b652 453#endif
2b5f62a0
VZ
454
455 FIND_STRUCT fs;
4daceaac 456 FIND_DATA fd = FindFirst(dirname, wxEmptyString, &fs);
2b5f62a0
VZ
457 if ( !IsFindDataOk(fd) )
458 {
d71cc120 459 return false;
2b5f62a0
VZ
460 }
461
462 *ftAccess = fs.ftLastAccessTime;
463 *ftCreate = fs.ftCreationTime;
464 *ftMod = fs.ftLastWriteTime;
465
466 FindClose(fd);
467
d71cc120 468 return true;
2b5f62a0
VZ
469}
470
471#endif // __WIN32__
472