]> git.saurik.com Git - wxWidgets.git/blame - src/generic/dirctrlg.cpp
[ 1509599 ] 'Split pickers page in widgets sample' with more icons and rebaking.
[wxWidgets.git] / src / generic / dirctrlg.cpp
CommitLineData
51a58d8b 1/////////////////////////////////////////////////////////////////////////////
7fc65a03 2// Name: src/generic/dirctrlg.cpp
51a58d8b
JS
3// Purpose: wxGenericDirCtrl
4// Author: Harm van der Heijden, Robert Roebling, Julian Smart
5// Modified by:
6// Created: 12/12/98
7// RCS-ID: $Id$
8// Copyright: (c) Harm van der Heijden, Robert Roebling and Julian Smart
65571936 9// Licence: wxWindows licence
51a58d8b
JS
10/////////////////////////////////////////////////////////////////////////////
11
51a58d8b
JS
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
15#ifdef __BORLANDC__
32d4c30a 16 #pragma hdrstop
51a58d8b
JS
17#endif
18
13de0c8c 19#if wxUSE_DIRDLG || wxUSE_FILEDLG
51a58d8b 20
88a7a4e1
WS
21#include "wx/generic/dirctrlg.h"
22
32d4c30a
WS
23#ifndef WX_PRECOMP
24 #include "wx/hash.h"
88a7a4e1 25 #include "wx/intl.h"
e4db172a 26 #include "wx/log.h"
de6185e2 27 #include "wx/utils.h"
f1e01716 28 #include "wx/button.h"
923d28da 29 #include "wx/icon.h"
9eddec69 30 #include "wx/settings.h"
246c5004 31 #include "wx/msgdlg.h"
ce5d92e1 32 #include "wx/cmndata.h"
b36e08d0 33 #include "wx/choice.h"
fec9cc08 34 #include "wx/textctrl.h"
ed2fbeb8
WS
35 #include "wx/layout.h"
36 #include "wx/sizer.h"
949c9f74 37 #include "wx/textdlg.h"
dd05139a 38 #include "wx/gdicmn.h"
32d4c30a
WS
39#endif
40
e624f5ba 41#include "wx/module.h"
51a58d8b 42#include "wx/filefn.h"
51a58d8b 43#include "wx/imaglist.h"
51a58d8b
JS
44#include "wx/tokenzr.h"
45#include "wx/dir.h"
06cc1fb9 46#include "wx/artprov.h"
06cc1fb9
JS
47#include "wx/mimetype.h"
48#include "wx/image.h"
51a58d8b
JS
49
50#if wxUSE_STATLINE
51 #include "wx/statline.h"
52#endif
53
76a5e5d2 54#if defined(__WXMAC__)
32d4c30a 55 #include "wx/mac/private.h" // includes mac headers
76a5e5d2
SC
56#endif
57
51a58d8b 58#ifdef __WXMSW__
2736b3ce 59#include <windows.h>
7acf6a92 60#include "wx/msw/winundef.h"
7328394a 61
c9ecda60
VS
62// FIXME - Mingw32 1.0 has both _getdrive() and _chdrive(). For now, let's assume
63// older releases don't, but it should be verified and the checks modified
64// accordingly.
b4da152e 65#if !defined(__GNUWIN32__) || (defined(__MINGW32_MAJOR_VERSION) && __MINGW32_MAJOR_VERSION >= 1)
1c193821 66#if !defined(__WXWINCE__)
32d4c30a 67 #include <direct.h>
1c193821 68#endif
32d4c30a
WS
69 #include <stdlib.h>
70 #include <ctype.h>
51a58d8b
JS
71#endif
72
7328394a
JS
73#endif
74
1c53456f 75#if defined(__OS2__) || defined(__DOS__)
ed2fbeb8
WS
76 #ifdef __OS2__
77 #define INCL_BASE
78 #include <os2.h>
79 #ifndef __EMX__
80 #include <direct.h>
81 #endif
82 #include <stdlib.h>
83 #include <ctype.h>
1c53456f 84 #endif
ed2fbeb8 85 extern bool wxIsDriveAvailable(const wxString& dirName);
0d853c54 86#endif // __OS2__
ec1b28a3 87
f4ac0693 88#if defined(__WXMAC__)
32d4c30a 89 #include "MoreFilesX.h"
bedaf53e
SC
90#endif
91
267a7108 92#ifdef __BORLANDC__
32d4c30a 93 #include "dos.h"
267a7108
JS
94#endif
95
51a58d8b
JS
96// If compiled under Windows, this macro can cause problems
97#ifdef GetFirstChild
98#undef GetFirstChild
99#endif
100
06cc1fb9 101// ----------------------------------------------------------------------------
0d853c54 102// wxGetAvailableDrives, for WINDOWS, DOS, OS2, MAC, UNIX (returns "/")
06cc1fb9 103// ----------------------------------------------------------------------------
51a58d8b 104
06cc1fb9
JS
105size_t wxGetAvailableDrives(wxArrayString &paths, wxArrayString &names, wxArrayInt &icon_ids)
106{
0d853c54 107#if defined(__WINDOWS__) || defined(__DOS__) || defined(__OS2__)
51a58d8b 108
4676948b
JS
109#ifdef __WXWINCE__
110 // No logical drives; return "\"
111 paths.Add(wxT("\\"));
112 names.Add(wxT("\\"));
1ab36908 113 icon_ids.Add(wxFileIconsTable::computer);
4676948b 114#elif defined(__WIN32__)
06cc1fb9
JS
115 wxChar driveBuffer[256];
116 size_t n = (size_t) GetLogicalDriveStrings(255, driveBuffer);
117 size_t i = 0;
118 while (i < n)
119 {
120 wxString path, name;
121 path.Printf(wxT("%c:\\"), driveBuffer[i]);
122 name.Printf(wxT("%c:"), driveBuffer[i]);
51a58d8b 123
0026e0f2 124#if !defined(__WXWINCE__)
2934338f
VZ
125 wxChar pname[52]; // FIXME: why 52 and not MAX_PATH or whatever?
126 if ( GetVolumeInformation(path, pname, WXSIZEOF(pname),
127 NULL, NULL, NULL, NULL, 0) )
0026e0f2 128 {
0e5edcda 129 name << _T(' ') << pname;
0026e0f2 130 }
0e5edcda 131#endif // __WXWINCE__
0026e0f2 132
999836aa 133 int imageId;
06cc1fb9
JS
134 int driveType = ::GetDriveType(path);
135 switch (driveType)
136 {
137 case DRIVE_REMOVABLE:
138 if (path == wxT("a:\\") || path == wxT("b:\\"))
139 imageId = wxFileIconsTable::floppy;
140 else
141 imageId = wxFileIconsTable::removeable;
142 break;
143 case DRIVE_CDROM:
144 imageId = wxFileIconsTable::cdrom;
145 break;
146 case DRIVE_REMOTE:
147 case DRIVE_FIXED:
148 default:
149 imageId = wxFileIconsTable::drive;
150 break;
151 }
51a58d8b 152
06cc1fb9
JS
153 paths.Add(path);
154 names.Add(name);
155 icon_ids.Add(imageId);
51a58d8b 156
06cc1fb9
JS
157 while (driveBuffer[i] != wxT('\0'))
158 i ++;
159 i ++;
160 if (driveBuffer[i] == wxT('\0'))
161 break;
162 }
0d853c54
SN
163#elif defined(__OS2__)
164 APIRET rc;
165 ULONG ulDriveNum = 0;
166 ULONG ulDriveMap = 0;
167 rc = ::DosQueryCurrentDisk(&ulDriveNum, &ulDriveMap);
ca65c044
WS
168 if ( rc == 0)
169 {
0d853c54
SN
170 size_t i = 0;
171 while (i < 26)
172 {
ca65c044
WS
173 if (ulDriveMap & ( 1 << i ))
174 {
175 wxString path, name;
176 path.Printf(wxT("%c:\\"), 'A' + i);
177 name.Printf(wxT("%c:"), 'A' + i);
178
be283c77
SN
179 // Note: If _filesys is unsupported by some compilers,
180 // we can always replace it by DosQueryFSAttach
181 char filesysname[20];
7fc65a03
WS
182#ifdef __WATCOMC__
183 ULONG cbBuffer = sizeof(filesysname);
184 PFSQBUFFER2 pfsqBuffer = (PFSQBUFFER2)filesysname;
185 APIRET rc = ::DosQueryFSAttach(name.fn_str(),0,FSAIL_QUERYNAME,pfsqBuffer,&cbBuffer);
186 if (rc != NO_ERROR)
187 {
188 filesysname[0] = '\0';
189 }
190#else
be283c77 191 _filesys(name.fn_str(), filesysname, sizeof(filesysname));
7fc65a03 192#endif
be283c77 193 /* FAT, LAN, HPFS, CDFS, NFS */
ca65c044
WS
194 int imageId;
195 if (path == wxT("A:\\") || path == wxT("B:\\"))
196 imageId = wxFileIconsTable::floppy;
be283c77
SN
197 else if (!strcmp(filesysname, "CDFS"))
198 imageId = wxFileIconsTable::cdrom;
199 else if (!strcmp(filesysname, "LAN") ||
200 !strcmp(filesysname, "NFS"))
201 imageId = wxFileIconsTable::drive;
ca65c044
WS
202 else
203 imageId = wxFileIconsTable::drive;
204 paths.Add(path);
205 names.Add(name);
206 icon_ids.Add(imageId);
207 }
0d853c54 208 i ++;
ca65c044
WS
209 }
210 }
0d853c54 211#else // !__WIN32__, !__OS2__
06cc1fb9 212 int drive;
51a58d8b 213
06cc1fb9
JS
214 /* If we can switch to the drive, it exists. */
215 for( drive = 1; drive <= 26; drive++ )
216 {
217 wxString path, name;
218 path.Printf(wxT("%c:\\"), (char) (drive + 'a' - 1));
219 name.Printf(wxT("%c:"), (char) (drive + 'A' - 1));
51a58d8b 220
06cc1fb9
JS
221 if (wxIsDriveAvailable(path))
222 {
223 paths.Add(path);
224 names.Add(name);
225 icon_ids.Add((drive <= 2) ? wxFileIconsTable::floppy : wxFileIconsTable::drive);
226 }
227 }
228#endif // __WIN32__/!__WIN32__
229
230#elif defined(__WXMAC__)
06cc1fb9 231
a2b77260
SC
232 ItemCount volumeIndex = 1;
233 OSErr err = noErr ;
44d60c0b 234
a2b77260
SC
235 while( noErr == err )
236 {
237 HFSUniStr255 volumeName ;
238 FSRef fsRef ;
239 FSVolumeInfo volumeInfo ;
44d60c0b
WS
240 err = FSGetVolumeInfo(0, volumeIndex, NULL, kFSVolInfoFlags , &volumeInfo , &volumeName, &fsRef);
241 if( noErr == err )
242 {
243 wxString path = wxMacFSRefToPath( &fsRef ) ;
a2b77260 244 wxString name = wxMacHFSUniStrToString( &volumeName ) ;
44d60c0b 245
a2b77260 246 if ( (volumeInfo.flags & kFSVolFlagSoftwareLockedMask) || (volumeInfo.flags & kFSVolFlagHardwareLockedMask) )
06cc1fb9 247 {
a2b77260 248 icon_ids.Add(wxFileIconsTable::cdrom);
06cc1fb9 249 }
a2b77260
SC
250 else
251 {
252 icon_ids.Add(wxFileIconsTable::drive);
06cc1fb9 253 }
a2b77260 254 // todo other removable
44d60c0b 255
06cc1fb9
JS
256 paths.Add(path);
257 names.Add(name);
a2b77260 258 volumeIndex++ ;
44d60c0b 259 }
06cc1fb9 260 }
06cc1fb9
JS
261
262#elif defined(__UNIX__)
263 paths.Add(wxT("/"));
264 names.Add(wxT("/"));
265 icon_ids.Add(wxFileIconsTable::computer);
266#else
267 #error "Unsupported platform in wxGenericDirCtrl!"
268#endif
e9890600
WS
269 wxASSERT_MSG( (paths.GetCount() == names.GetCount()), wxT("The number of paths and their human readable names should be equal in number."));
270 wxASSERT_MSG( (paths.GetCount() == icon_ids.GetCount()), wxT("Wrong number of icons for available drives."));
06cc1fb9
JS
271 return paths.GetCount();
272}
51a58d8b 273
06cc1fb9
JS
274// ----------------------------------------------------------------------------
275// wxIsDriveAvailable
276// ----------------------------------------------------------------------------
748fcded 277
748fcded 278#if defined(__DOS__)
db5333a5 279
37fd1c97 280bool wxIsDriveAvailable(const wxString& dirName)
db5333a5 281{
6157794c 282 // FIXME_MGL - this method leads to hang up under Watcom for some reason
127eab18
WS
283#ifdef __WATCOMC__
284 wxUnusedVar(dirName);
285#else
b36e08d0 286 if ( dirName.length() == 3 && dirName[1u] == wxT(':') )
db5333a5
VS
287 {
288 wxString dirNameLower(dirName.Lower());
ca65c044 289 // VS: always return true for removable media, since Win95 doesn't
db5333a5
VS
290 // like it when MS-DOS app accesses empty floppy drive
291 return (dirNameLower[0u] == wxT('a') ||
292 dirNameLower[0u] == wxT('b') ||
da865fdd 293 wxDirExists(dirNameLower));
db5333a5
VS
294 }
295 else
6157794c 296#endif
ca65c044 297 return true;
db5333a5
VS
298}
299
0d853c54 300#elif defined(__WINDOWS__) || defined(__OS2__)
db5333a5 301
0c0d1521 302int setdrive(int WXUNUSED_IN_WINCE(drive))
33ac7e6f 303{
4676948b
JS
304#ifdef __WXWINCE__
305 return 0;
306#elif defined(__GNUWIN32__) && \
c9ecda60
VS
307 (defined(__MINGW32_MAJOR_VERSION) && __MINGW32_MAJOR_VERSION >= 1)
308 return _chdrive(drive);
309#else
ca65c044 310 wxChar newdrive[4];
33ac7e6f 311
ca65c044
WS
312 if (drive < 1 || drive > 31)
313 return -1;
314 newdrive[0] = (wxChar)(wxT('A') + drive - 1);
315 newdrive[1] = wxT(':');
0d853c54 316#ifdef __OS2__
ca65c044
WS
317 newdrive[2] = wxT('\\');
318 newdrive[3] = wxT('\0');
0d853c54 319#else
ca65c044 320 newdrive[2] = wxT('\0');
0d853c54 321#endif
50c76ce1 322#if defined(__WXMSW__)
ca65c044 323 if (::SetCurrentDirectory(newdrive))
50c76ce1
DW
324#else
325 // VA doesn't know what LPSTR is and has its own set
ca65c044 326 if (!DosSetCurrentDir((PSZ)newdrive))
50c76ce1 327#endif
ca65c044
WS
328 return 0;
329 else
330 return -1;
c9ecda60 331#endif // !GNUWIN32
33ac7e6f
KB
332}
333
0c0d1521 334bool wxIsDriveAvailable(const wxString& WXUNUSED_IN_WINCE(dirName))
ad9cd15c 335{
4676948b 336#ifdef __WXWINCE__
ca65c044 337 return false;
4676948b 338#else
ad9cd15c 339#ifdef __WIN32__
33fed835 340 UINT errorMode = SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX);
ad9cd15c 341#endif
ca65c044 342 bool success = true;
ad9cd15c
JS
343
344 // Check if this is a root directory and if so,
2d4e4f80 345 // whether the drive is available.
b36e08d0 346 if (dirName.length() == 3 && dirName[(size_t)1] == wxT(':'))
ad9cd15c 347 {
33fed835 348 wxString dirNameLower(dirName.Lower());
b4da152e 349#if defined(__GNUWIN32__) && !(defined(__MINGW32_MAJOR_VERSION) && __MINGW32_MAJOR_VERSION >= 1)
da865fdd 350 success = wxDirExists(dirNameLower);
7328394a 351#else
0d853c54
SN
352 #if defined(__OS2__)
353 // Avoid changing to drive since no media may be inserted.
354 if (dirNameLower[(size_t)0] == 'a' || dirNameLower[(size_t)0] == 'b')
355 return success;
356 #endif
ad9cd15c 357 int currentDrive = _getdrive();
33fed835 358 int thisDrive = (int) (dirNameLower[(size_t)0] - 'a' + 1) ;
33ac7e6f
KB
359 int err = setdrive( thisDrive ) ;
360 setdrive( currentDrive );
ad9cd15c
JS
361
362 if (err == -1)
363 {
ca65c044 364 success = false;
ad9cd15c 365 }
7328394a 366#endif
ad9cd15c
JS
367 }
368#ifdef __WIN32__
33fed835 369 (void) SetErrorMode(errorMode);
ad9cd15c
JS
370#endif
371
33fed835 372 return success;
4676948b 373#endif
ad9cd15c 374}
0d853c54 375#endif // __WINDOWS__ || __OS2__
ad9cd15c 376
13de0c8c
WS
377#endif // wxUSE_DIRDLG || wxUSE_FILEDLG
378
379
380
381#if wxUSE_DIRDLG
b600ed13 382
57e26a09
JS
383// Function which is called by quick sort. We want to override the default wxArrayString behaviour,
384// and sort regardless of case.
a2a03d78 385static int wxCMPFUNC_CONV wxDirCtrlStringCompareFunction(const wxString& strFirst, const wxString& strSecond)
57e26a09 386{
a2a03d78 387 return strFirst.CmpNoCase(strSecond);
57e26a09
JS
388}
389
51a58d8b 390//-----------------------------------------------------------------------------
748fcded 391// wxDirItemData
51a58d8b
JS
392//-----------------------------------------------------------------------------
393
748fcded
VS
394wxDirItemData::wxDirItemData(const wxString& path, const wxString& name,
395 bool isDir)
51a58d8b
JS
396{
397 m_path = path;
398 m_name = name;
399 /* Insert logic to detect hidden files here
400 * In UnixLand we just check whether the first char is a dot
401 * For FileNameFromPath read LastDirNameInThisPath ;-) */
402 // m_isHidden = (bool)(wxFileNameFromPath(*m_path)[0] == '.');
ca65c044
WS
403 m_isHidden = false;
404 m_isExpanded = false;
51a58d8b
JS
405 m_isDir = isDir;
406}
407
748fcded 408void wxDirItemData::SetNewDirName(const wxString& path)
51a58d8b
JS
409{
410 m_path = path;
748fcded
VS
411 m_name = wxFileNameFromPath(path);
412}
413
414bool wxDirItemData::HasSubDirs() const
415{
44d60c0b 416 if (m_path.empty())
ca65c044 417 return false;
748fcded
VS
418
419 wxDir dir;
420 {
421 wxLogNull nolog;
422 if ( !dir.Open(m_path) )
ca65c044 423 return false;
748fcded
VS
424 }
425
426 return dir.HasSubDirs();
427}
428
574c939e 429bool wxDirItemData::HasFiles(const wxString& WXUNUSED(spec)) const
748fcded 430{
44d60c0b 431 if (m_path.empty())
ca65c044 432 return false;
748fcded
VS
433
434 wxDir dir;
435 {
436 wxLogNull nolog;
437 if ( !dir.Open(m_path) )
ca65c044 438 return false;
748fcded
VS
439 }
440
441 return dir.HasFiles();
51a58d8b
JS
442}
443
51a58d8b
JS
444//-----------------------------------------------------------------------------
445// wxGenericDirCtrl
446//-----------------------------------------------------------------------------
447
51a58d8b 448
b0a877ec 449#if wxUSE_EXTENDED_RTTI
73c36334
JS
450WX_DEFINE_FLAGS( wxGenericDirCtrlStyle )
451
3ff066a4 452wxBEGIN_FLAGS( wxGenericDirCtrlStyle )
73c36334
JS
453 // new style border flags, we put them first to
454 // use them for streaming out
3ff066a4
SC
455 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
456 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
457 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
458 wxFLAGS_MEMBER(wxBORDER_RAISED)
459 wxFLAGS_MEMBER(wxBORDER_STATIC)
460 wxFLAGS_MEMBER(wxBORDER_NONE)
ca65c044 461
73c36334 462 // old style border flags
3ff066a4
SC
463 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
464 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
465 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
466 wxFLAGS_MEMBER(wxRAISED_BORDER)
467 wxFLAGS_MEMBER(wxSTATIC_BORDER)
cb0afb26 468 wxFLAGS_MEMBER(wxBORDER)
73c36334
JS
469
470 // standard window styles
3ff066a4
SC
471 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
472 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
473 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
474 wxFLAGS_MEMBER(wxWANTS_CHARS)
cb0afb26 475 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
3ff066a4
SC
476 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
477 wxFLAGS_MEMBER(wxVSCROLL)
478 wxFLAGS_MEMBER(wxHSCROLL)
479
480 wxFLAGS_MEMBER(wxDIRCTRL_DIR_ONLY)
481 wxFLAGS_MEMBER(wxDIRCTRL_3D_INTERNAL)
482 wxFLAGS_MEMBER(wxDIRCTRL_SELECT_FIRST)
483 wxFLAGS_MEMBER(wxDIRCTRL_SHOW_FILTERS)
484
485wxEND_FLAGS( wxGenericDirCtrlStyle )
73c36334 486
b0a877ec
SC
487IMPLEMENT_DYNAMIC_CLASS_XTI(wxGenericDirCtrl, wxControl,"wx/dirctrl.h")
488
3ff066a4
SC
489wxBEGIN_PROPERTIES_TABLE(wxGenericDirCtrl)
490 wxHIDE_PROPERTY( Children )
ca65c044
WS
491 wxPROPERTY( DefaultPath , wxString , SetDefaultPath , GetDefaultPath , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
492 wxPROPERTY( Filter , wxString , SetFilter , GetFilter , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
493 wxPROPERTY( DefaultFilter , int , SetFilterIndex, GetFilterIndex, EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
af498247 494 wxPROPERTY_FLAGS( WindowStyle, wxGenericDirCtrlStyle, long, SetWindowStyleFlag, GetWindowStyleFlag, EMPTY_MACROVALUE , 0, wxT("Helpstring"), wxT("group") )
3ff066a4 495wxEND_PROPERTIES_TABLE()
b0a877ec 496
3ff066a4
SC
497wxBEGIN_HANDLERS_TABLE(wxGenericDirCtrl)
498wxEND_HANDLERS_TABLE()
b0a877ec 499
ca65c044
WS
500wxCONSTRUCTOR_8( wxGenericDirCtrl , wxWindow* , Parent , wxWindowID , Id , wxString , DefaultPath ,
501 wxPoint , Position , wxSize , Size , long , WindowStyle , wxString , Filter , int , DefaultFilter )
b0a877ec
SC
502#else
503IMPLEMENT_DYNAMIC_CLASS(wxGenericDirCtrl, wxControl)
504#endif
066f1b7a 505
51a58d8b 506BEGIN_EVENT_TABLE(wxGenericDirCtrl, wxControl)
6b707fc3
JS
507 EVT_TREE_ITEM_EXPANDING (wxID_TREECTRL, wxGenericDirCtrl::OnExpandItem)
508 EVT_TREE_ITEM_COLLAPSED (wxID_TREECTRL, wxGenericDirCtrl::OnCollapseItem)
509 EVT_TREE_BEGIN_LABEL_EDIT (wxID_TREECTRL, wxGenericDirCtrl::OnBeginEditItem)
510 EVT_TREE_END_LABEL_EDIT (wxID_TREECTRL, wxGenericDirCtrl::OnEndEditItem)
51a58d8b
JS
511 EVT_SIZE (wxGenericDirCtrl::OnSize)
512END_EVENT_TABLE()
513
514wxGenericDirCtrl::wxGenericDirCtrl(void)
515{
516 Init();
517}
518
c06dde42
JS
519void wxGenericDirCtrl::ExpandRoot()
520{
521 ExpandDir(m_rootId); // automatically expand first level
522
523 // Expand and select the default path
524 if (!m_defaultPath.empty())
525 {
526 ExpandPath(m_defaultPath);
527 }
528#ifdef __UNIX__
529 else
530 {
531 // On Unix, there's only one node under the (hidden) root node. It
532 // represents the / path, so the user would always have to expand it;
533 // let's do it ourselves
534 ExpandPath( wxT("/") );
535 }
536#endif
537}
538
51a58d8b 539bool wxGenericDirCtrl::Create(wxWindow *parent,
748fcded
VS
540 const wxWindowID id,
541 const wxString& dir,
542 const wxPoint& pos,
543 const wxSize& size,
544 long style,
545 const wxString& filter,
546 int defaultFilter,
547 const wxString& name)
51a58d8b
JS
548{
549 if (!wxControl::Create(parent, id, pos, size, style, wxDefaultValidator, name))
ca65c044 550 return false;
51a58d8b 551
db5333a5 552 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));
51a58d8b
JS
553
554 Init();
555
3bd8a405
JS
556 long treeStyle = wxTR_HAS_BUTTONS;
557
558 // On Windows CE, if you hide the root, you get a crash when
559 // attempting to access data for children of the root item.
560#ifndef __WXWINCE__
561 treeStyle |= wxTR_HIDE_ROOT;
562#endif
2997ca30 563
82c1f2a3
RR
564#ifdef __WXGTK20__
565 treeStyle |= wxTR_NO_LINES;
566#endif
fd775aae 567
dabd1377 568 if (style & wxDIRCTRL_EDIT_LABELS)
fd775aae
JS
569 treeStyle |= wxTR_EDIT_LABELS;
570
51a58d8b
JS
571 if ((style & wxDIRCTRL_3D_INTERNAL) == 0)
572 treeStyle |= wxNO_BORDER;
27c73976
JS
573 else
574 treeStyle |= wxBORDER_SUNKEN;
51a58d8b
JS
575
576 long filterStyle = 0;
577 if ((style & wxDIRCTRL_3D_INTERNAL) == 0)
578 filterStyle |= wxNO_BORDER;
27c73976
JS
579 else
580 filterStyle |= wxBORDER_SUNKEN;
51a58d8b 581
a78955e3 582 m_treeCtrl = CreateTreeCtrl(this, wxID_TREECTRL,
c47addef 583 wxPoint(0,0), GetClientSize(), treeStyle);
51a58d8b 584
44d60c0b 585 if (!filter.empty() && (style & wxDIRCTRL_SHOW_FILTERS))
51a58d8b
JS
586 m_filterListCtrl = new wxDirFilterListCtrl(this, wxID_FILTERLISTCTRL, wxDefaultPosition, wxDefaultSize, filterStyle);
587
588 m_defaultPath = dir;
589 m_filter = filter;
590
dd0138d9
RR
591 if (m_filter.empty())
592#ifdef __UNIX__
593 m_filter = wxT("*");
594#else
595 m_filter = wxT("*.*");
596#endif
597
51a58d8b
JS
598 SetFilterIndex(defaultFilter);
599
600 if (m_filterListCtrl)
601 m_filterListCtrl->FillFilterList(filter, defaultFilter);
602
06cc1fb9 603 m_treeCtrl->SetImageList(wxTheFileIconsTable->GetSmallImageList());
51a58d8b 604
ca65c044
WS
605 m_showHidden = false;
606 wxDirItemData* rootData = new wxDirItemData(wxEmptyString, wxEmptyString, true);
51a58d8b
JS
607
608 wxString rootName;
609
0d853c54 610#if defined(__WINDOWS__) || defined(__OS2__) || defined(__DOS__)
51a58d8b
JS
611 rootName = _("Computer");
612#else
613 rootName = _("Sections");
614#endif
615
616 m_rootId = m_treeCtrl->AddRoot( rootName, 3, -1, rootData);
617 m_treeCtrl->SetItemHasChildren(m_rootId);
2997ca30 618
c06dde42 619 ExpandRoot();
51a58d8b 620
19d8dd12 621 SetBestSize(size);
51a58d8b
JS
622 DoResize();
623
ca65c044 624 return true;
51a58d8b
JS
625}
626
627wxGenericDirCtrl::~wxGenericDirCtrl()
628{
51a58d8b
JS
629}
630
631void wxGenericDirCtrl::Init()
632{
ca65c044 633 m_showHidden = false;
51a58d8b
JS
634 m_currentFilter = 0;
635 m_currentFilterStr = wxEmptyString; // Default: any file
636 m_treeCtrl = NULL;
637 m_filterListCtrl = NULL;
638}
639
a78955e3
VS
640wxTreeCtrl* wxGenericDirCtrl::CreateTreeCtrl(wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long treeStyle)
641{
642 return new wxTreeCtrl(parent, id, pos, size, treeStyle);
643}
644
42dcacf0
RR
645void wxGenericDirCtrl::ShowHidden( bool show )
646{
647 m_showHidden = show;
574c939e 648
3b423cdd 649 wxString path = GetPath();
08887820 650 ReCreateTree();
3b423cdd 651 SetPath(path);
42dcacf0
RR
652}
653
22328fa4
JS
654const wxTreeItemId
655wxGenericDirCtrl::AddSection(const wxString& path, const wxString& name, int imageId)
51a58d8b 656{
ca65c044 657 wxDirItemData *dir_item = new wxDirItemData(path,name,true);
51a58d8b 658
22328fa4 659 wxTreeItemId id = AppendItem( m_rootId, name, imageId, -1, dir_item);
4f5c180e 660
06cc1fb9 661 m_treeCtrl->SetItemHasChildren(id);
22328fa4
JS
662
663 return id;
06cc1fb9 664}
51a58d8b 665
06cc1fb9
JS
666void wxGenericDirCtrl::SetupSections()
667{
668 wxArrayString paths, names;
669 wxArrayInt icons;
2d4e4f80 670
06cc1fb9 671 size_t n, count = wxGetAvailableDrives(paths, names, icons);
4f5c180e 672
82c1f2a3
RR
673#ifdef __WXGTK20__
674 wxString home = wxGetHomeDir();
675 AddSection( home, _("Home directory"), 1);
676 home += wxT("/Desktop");
677 AddSection( home, _("Desktop"), 1);
678#endif
679
06cc1fb9 680 for (n = 0; n < count; n++)
06cc1fb9 681 AddSection(paths[n], names[n], icons[n]);
51a58d8b
JS
682}
683
684void wxGenericDirCtrl::OnBeginEditItem(wxTreeEvent &event)
685{
686 // don't rename the main entry "Sections"
687 if (event.GetItem() == m_rootId)
688 {
689 event.Veto();
690 return;
691 }
692
693 // don't rename the individual sections
99006e44 694 if (m_treeCtrl->GetItemParent( event.GetItem() ) == m_rootId)
51a58d8b
JS
695 {
696 event.Veto();
697 return;
698 }
699}
700
701void wxGenericDirCtrl::OnEndEditItem(wxTreeEvent &event)
702{
6e86472f
VZ
703 if (event.IsEditCancelled())
704 return;
705
44d60c0b 706 if ((event.GetLabel().empty()) ||
51a58d8b
JS
707 (event.GetLabel() == _(".")) ||
708 (event.GetLabel() == _("..")) ||
06cc1fb9
JS
709 (event.GetLabel().Find(wxT('/')) != wxNOT_FOUND) ||
710 (event.GetLabel().Find(wxT('\\')) != wxNOT_FOUND) ||
711 (event.GetLabel().Find(wxT('|')) != wxNOT_FOUND))
51a58d8b
JS
712 {
713 wxMessageDialog dialog(this, _("Illegal directory name."), _("Error"), wxOK | wxICON_ERROR );
714 dialog.ShowModal();
715 event.Veto();
716 return;
717 }
718
719 wxTreeItemId id = event.GetItem();
748fcded 720 wxDirItemData *data = (wxDirItemData*)m_treeCtrl->GetItemData( id );
51a58d8b
JS
721 wxASSERT( data );
722
723 wxString new_name( wxPathOnly( data->m_path ) );
ad9cd15c 724 new_name += wxString(wxFILE_SEP_PATH);
51a58d8b
JS
725 new_name += event.GetLabel();
726
727 wxLogNull log;
728
729 if (wxFileExists(new_name))
730 {
731 wxMessageDialog dialog(this, _("File name exists already."), _("Error"), wxOK | wxICON_ERROR );
732 dialog.ShowModal();
733 event.Veto();
734 }
735
736 if (wxRenameFile(data->m_path,new_name))
737 {
738 data->SetNewDirName( new_name );
739 }
740 else
741 {
742 wxMessageDialog dialog(this, _("Operation not permitted."), _("Error"), wxOK | wxICON_ERROR );
743 dialog.ShowModal();
744 event.Veto();
745 }
746}
747
748void wxGenericDirCtrl::OnExpandItem(wxTreeEvent &event)
749{
750 wxTreeItemId parentId = event.GetItem();
751
748fcded
VS
752 // VS: this is needed because the event handler is called from wxTreeCtrl
753 // ctor when wxTR_HIDE_ROOT was specified
4ded51f2
CE
754
755 if (!m_rootId.IsOk())
756
748fcded
VS
757 m_rootId = m_treeCtrl->GetRootItem();
758
51a58d8b
JS
759 ExpandDir(parentId);
760}
761
762void wxGenericDirCtrl::OnCollapseItem(wxTreeEvent &event )
763{
08887820
VS
764 CollapseDir(event.GetItem());
765}
766
767void wxGenericDirCtrl::CollapseDir(wxTreeItemId parentId)
768{
769 wxTreeItemId child;
51a58d8b 770
08887820 771 wxDirItemData *data = (wxDirItemData *) m_treeCtrl->GetItemData(parentId);
51a58d8b
JS
772 if (!data->m_isExpanded)
773 return;
774
ca65c044 775 data->m_isExpanded = false;
2d75caaa 776 wxTreeItemIdValue cookie;
51a58d8b
JS
777 /* Workaround because DeleteChildren has disapeared (why?) and
778 * CollapseAndReset doesn't work as advertised (deletes parent too) */
08887820 779 child = m_treeCtrl->GetFirstChild(parentId, cookie);
51a58d8b
JS
780 while (child.IsOk())
781 {
782 m_treeCtrl->Delete(child);
783 /* Not GetNextChild below, because the cookie mechanism can't
784 * handle disappearing children! */
08887820 785 child = m_treeCtrl->GetFirstChild(parentId, cookie);
51a58d8b 786 }
c06dde42
JS
787 if (parentId != m_treeCtrl->GetRootItem())
788 m_treeCtrl->Collapse(parentId);
51a58d8b
JS
789}
790
791void wxGenericDirCtrl::ExpandDir(wxTreeItemId parentId)
792{
748fcded 793 wxDirItemData *data = (wxDirItemData *) m_treeCtrl->GetItemData(parentId);
51a58d8b
JS
794
795 if (data->m_isExpanded)
796 return;
797
ca65c044 798 data->m_isExpanded = true;
51a58d8b 799
748fcded 800 if (parentId == m_treeCtrl->GetRootItem())
51a58d8b
JS
801 {
802 SetupSections();
803 return;
804 }
805
806 wxASSERT(data);
807
808 wxString search,path,filename;
809
810 wxString dirName(data->m_path);
811
3bd8a405 812#if (defined(__WINDOWS__) && !defined(__WXWINCE__)) || defined(__DOS__) || defined(__OS2__)
51a58d8b
JS
813 // Check if this is a root directory and if so,
814 // whether the drive is avaiable.
33fed835
MB
815 if (!wxIsDriveAvailable(dirName))
816 {
ca65c044 817 data->m_isExpanded = false;
7328394a
JS
818 //wxMessageBox(wxT("Sorry, this drive is not available."));
819 return;
33fed835 820 }
51a58d8b
JS
821#endif
822
823 // This may take a longish time. Go to busy cursor
824 wxBusyCursor busy;
825
0d853c54 826#if defined(__WINDOWS__) || defined(__DOS__) || defined(__OS2__)
51a58d8b
JS
827 if (dirName.Last() == ':')
828 dirName += wxString(wxFILE_SEP_PATH);
829#endif
830
831 wxArrayString dirs;
832 wxArrayString filenames;
833
834 wxDir d;
835 wxString eachFilename;
836
f9c165b1 837 wxLogNull log;
51a58d8b
JS
838 d.Open(dirName);
839
840 if (d.IsOpened())
841 {
42dcacf0
RR
842 int style = wxDIR_DIRS;
843 if (m_showHidden) style |= wxDIR_HIDDEN;
844 if (d.GetFirst(& eachFilename, wxEmptyString, style))
51a58d8b
JS
845 {
846 do
847 {
848 if ((eachFilename != wxT(".")) && (eachFilename != wxT("..")))
849 {
850 dirs.Add(eachFilename);
851 }
852 }
2b0a7c09 853 while (d.GetNext(&eachFilename));
51a58d8b
JS
854 }
855 }
222ed1d6 856 dirs.Sort(wxDirCtrlStringCompareFunction);
51a58d8b
JS
857
858 // Now do the filenames -- but only if we're allowed to
859 if ((GetWindowStyle() & wxDIRCTRL_DIR_ONLY) == 0)
860 {
861 d.Open(dirName);
ec1b28a3 862
51a58d8b
JS
863 if (d.IsOpened())
864 {
2b0a7c09
RN
865 int style = wxDIR_FILES;
866 if (m_showHidden) style |= wxDIR_HIDDEN;
3da4e4bd
JS
867 // Process each filter (ex: "JPEG Files (*.jpg;*.jpeg)|*.jpg;*.jpeg")
868 wxStringTokenizer strTok;
869 wxString curFilter;
870 strTok.SetString(m_currentFilterStr,wxT(";"));
871 while(strTok.HasMoreTokens())
51a58d8b 872 {
3da4e4bd 873 curFilter = strTok.GetNextToken();
f0e5a44d 874 if (d.GetFirst(& eachFilename, curFilter, style))
51a58d8b 875 {
3da4e4bd 876 do
51a58d8b 877 {
3da4e4bd
JS
878 if ((eachFilename != wxT(".")) && (eachFilename != wxT("..")))
879 {
880 filenames.Add(eachFilename);
881 }
51a58d8b 882 }
3da4e4bd 883 while (d.GetNext(& eachFilename));
51a58d8b 884 }
51a58d8b
JS
885 }
886 }
222ed1d6 887 filenames.Sort(wxDirCtrlStringCompareFunction);
51a58d8b
JS
888 }
889
890 // Add the sorted dirs
891 size_t i;
892 for (i = 0; i < dirs.Count(); i++)
893 {
4e115ed2 894 eachFilename = dirs[i];
51a58d8b 895 path = dirName;
083f7497 896 if (!wxEndsWithPathSeparator(path))
51a58d8b
JS
897 path += wxString(wxFILE_SEP_PATH);
898 path += eachFilename;
899
ca65c044 900 wxDirItemData *dir_item = new wxDirItemData(path,eachFilename,true);
22328fa4 901 wxTreeItemId id = AppendItem( parentId, eachFilename,
06cc1fb9
JS
902 wxFileIconsTable::folder, -1, dir_item);
903 m_treeCtrl->SetItemImage( id, wxFileIconsTable::folder_open,
904 wxTreeItemIcon_Expanded );
ec1b28a3 905
51a58d8b 906 // Has this got any children? If so, make it expandable.
748fcded
VS
907 // (There are two situations when a dir has children: either it
908 // has subdirectories or it contains files that weren't filtered
909 // out. The latter only applies to dirctrl with files.)
910 if ( dir_item->HasSubDirs() ||
911 (((GetWindowStyle() & wxDIRCTRL_DIR_ONLY) == 0) &&
912 dir_item->HasFiles(m_currentFilterStr)) )
51a58d8b 913 {
748fcded 914 m_treeCtrl->SetItemHasChildren(id);
51a58d8b 915 }
51a58d8b
JS
916 }
917
918 // Add the sorted filenames
919 if ((GetWindowStyle() & wxDIRCTRL_DIR_ONLY) == 0)
920 {
921 for (i = 0; i < filenames.Count(); i++)
922 {
4e115ed2 923 eachFilename = filenames[i];
51a58d8b 924 path = dirName;
083f7497 925 if (!wxEndsWithPathSeparator(path))
51a58d8b
JS
926 path += wxString(wxFILE_SEP_PATH);
927 path += eachFilename;
928 //path = dirName + wxString(wxT("/")) + eachFilename;
ca65c044 929 wxDirItemData *dir_item = new wxDirItemData(path,eachFilename,false);
06cc1fb9
JS
930 int image_id = wxFileIconsTable::file;
931 if (eachFilename.Find(wxT('.')) != wxNOT_FOUND)
932 image_id = wxTheFileIconsTable->GetIconID(eachFilename.AfterLast(wxT('.')));
22328fa4 933 (void) AppendItem( parentId, eachFilename, image_id, -1, dir_item);
51a58d8b
JS
934 }
935 }
936}
937
08887820
VS
938void wxGenericDirCtrl::ReCreateTree()
939{
940 CollapseDir(m_treeCtrl->GetRootItem());
c06dde42
JS
941 ExpandRoot();
942}
943
944void wxGenericDirCtrl::CollapseTree()
945{
946 wxTreeItemIdValue cookie;
947 wxTreeItemId child = m_treeCtrl->GetFirstChild(m_rootId, cookie);
948 while (child.IsOk())
949 {
950 CollapseDir(child);
951 child = m_treeCtrl->GetNextChild(m_rootId, cookie);
952 }
08887820
VS
953}
954
51a58d8b
JS
955// Find the child that matches the first part of 'path'.
956// E.g. if a child path is "/usr" and 'path' is "/usr/include"
957// then the child for /usr is returned.
958wxTreeItemId wxGenericDirCtrl::FindChild(wxTreeItemId parentId, const wxString& path, bool& done)
959{
960 wxString path2(path);
ec1b28a3 961
51a58d8b
JS
962 // Make sure all separators are as per the current platform
963 path2.Replace(wxT("\\"), wxString(wxFILE_SEP_PATH));
964 path2.Replace(wxT("/"), wxString(wxFILE_SEP_PATH));
ec1b28a3 965
51a58d8b
JS
966 // Append a separator to foil bogus substring matching
967 path2 += wxString(wxFILE_SEP_PATH);
ec1b28a3
DW
968
969 // In MSW or PM, case is not significant
0d853c54 970#if defined(__WINDOWS__) || defined(__DOS__) || defined(__OS2__)
51a58d8b
JS
971 path2.MakeLower();
972#endif
ec1b28a3 973
2d75caaa 974 wxTreeItemIdValue cookie;
51a58d8b 975 wxTreeItemId childId = m_treeCtrl->GetFirstChild(parentId, cookie);
53ccf1c0 976 while (childId.IsOk())
51a58d8b 977 {
748fcded 978 wxDirItemData* data = (wxDirItemData*) m_treeCtrl->GetItemData(childId);
ec1b28a3 979
44d60c0b 980 if (data && !data->m_path.empty())
51a58d8b
JS
981 {
982 wxString childPath(data->m_path);
083f7497 983 if (!wxEndsWithPathSeparator(childPath))
51a58d8b 984 childPath += wxString(wxFILE_SEP_PATH);
ec1b28a3
DW
985
986 // In MSW and PM, case is not significant
0d853c54 987#if defined(__WINDOWS__) || defined(__DOS__) || defined(__OS2__)
51a58d8b
JS
988 childPath.MakeLower();
989#endif
ec1b28a3 990
b36e08d0 991 if (childPath.length() <= path2.length())
51a58d8b 992 {
b36e08d0 993 wxString path3 = path2.Mid(0, childPath.length());
51a58d8b
JS
994 if (childPath == path3)
995 {
b36e08d0 996 if (path3.length() == path2.length())
ca65c044 997 done = true;
51a58d8b 998 else
ca65c044 999 done = false;
51a58d8b
JS
1000 return childId;
1001 }
1002 }
1003 }
ec1b28a3 1004
748fcded 1005 childId = m_treeCtrl->GetNextChild(parentId, cookie);
51a58d8b 1006 }
3fa4bd0e
VS
1007 wxTreeItemId invalid;
1008 return invalid;
51a58d8b
JS
1009}
1010
1011// Try to expand as much of the given path as possible,
1012// and select the given tree item.
1013bool wxGenericDirCtrl::ExpandPath(const wxString& path)
1014{
ca65c044 1015 bool done = false;
51a58d8b
JS
1016 wxTreeItemId id = FindChild(m_rootId, path, done);
1017 wxTreeItemId lastId = id; // The last non-zero id
237387ad 1018 while (id.IsOk() && !done)
51a58d8b
JS
1019 {
1020 ExpandDir(id);
1021
1022 id = FindChild(id, path, done);
53ccf1c0 1023 if (id.IsOk())
51a58d8b
JS
1024 lastId = id;
1025 }
4e115ed2
VZ
1026 if (!lastId.IsOk())
1027 return false;
1028
1029 wxDirItemData *data = (wxDirItemData *) m_treeCtrl->GetItemData(lastId);
1030 if (data->m_isDir)
51a58d8b 1031 {
4e115ed2
VZ
1032 m_treeCtrl->Expand(lastId);
1033 }
1034 if ((GetWindowStyle() & wxDIRCTRL_SELECT_FIRST) && data->m_isDir)
1035 {
1036 // Find the first file in this directory
1037 wxTreeItemIdValue cookie;
1038 wxTreeItemId childId = m_treeCtrl->GetFirstChild(lastId, cookie);
1039 bool selectedChild = false;
1040 while (childId.IsOk())
51a58d8b 1041 {
4e115ed2 1042 data = (wxDirItemData*) m_treeCtrl->GetItemData(childId);
ec1b28a3 1043
4e115ed2 1044 if (data && data->m_path != wxEmptyString && !data->m_isDir)
51a58d8b 1045 {
4e115ed2
VZ
1046 m_treeCtrl->SelectItem(childId);
1047 m_treeCtrl->EnsureVisible(childId);
1048 selectedChild = true;
1049 break;
51a58d8b 1050 }
4e115ed2 1051 childId = m_treeCtrl->GetNextChild(lastId, cookie);
51a58d8b 1052 }
4e115ed2 1053 if (!selectedChild)
51a58d8b
JS
1054 {
1055 m_treeCtrl->SelectItem(lastId);
1056 m_treeCtrl->EnsureVisible(lastId);
1057 }
51a58d8b
JS
1058 }
1059 else
4e115ed2
VZ
1060 {
1061 m_treeCtrl->SelectItem(lastId);
1062 m_treeCtrl->EnsureVisible(lastId);
1063 }
1064
1065 return true;
51a58d8b
JS
1066}
1067
f395a825
VZ
1068
1069bool wxGenericDirCtrl::CollapsePath(const wxString& path)
1070{
1071 bool done = false;
1072 wxTreeItemId id = FindChild(m_rootId, path, done);
1073 wxTreeItemId lastId = id; // The last non-zero id
1074
1075 while ( id.IsOk() && !done )
1076 {
1077 CollapseDir(id);
1078
1079 id = FindChild(id, path, done);
1080
1081 if ( id.IsOk() )
1082 lastId = id;
1083 }
1084
1085 if ( !lastId.IsOk() )
1086 return false;
1087
1088 m_treeCtrl->SelectItem(lastId);
1089 m_treeCtrl->EnsureVisible(lastId);
1090
1091 return true;
1092}
1093
1094
51a58d8b
JS
1095wxString wxGenericDirCtrl::GetPath() const
1096{
1097 wxTreeItemId id = m_treeCtrl->GetSelection();
1098 if (id)
1099 {
748fcded 1100 wxDirItemData* data = (wxDirItemData*) m_treeCtrl->GetItemData(id);
51a58d8b
JS
1101 return data->m_path;
1102 }
1103 else
1104 return wxEmptyString;
1105}
1106
1107wxString wxGenericDirCtrl::GetFilePath() const
1108{
1109 wxTreeItemId id = m_treeCtrl->GetSelection();
1110 if (id)
1111 {
748fcded 1112 wxDirItemData* data = (wxDirItemData*) m_treeCtrl->GetItemData(id);
51a58d8b
JS
1113 if (data->m_isDir)
1114 return wxEmptyString;
1115 else
1116 return data->m_path;
1117 }
1118 else
1119 return wxEmptyString;
1120}
1121
1122void wxGenericDirCtrl::SetPath(const wxString& path)
1123{
1124 m_defaultPath = path;
1125 if (m_rootId)
1126 ExpandPath(path);
1127}
1128
1129// Not used
1130#if 0
1131void wxGenericDirCtrl::FindChildFiles(wxTreeItemId id, int dirFlags, wxArrayString& filenames)
1132{
748fcded 1133 wxDirItemData *data = (wxDirItemData *) m_treeCtrl->GetItemData(id);
51a58d8b
JS
1134
1135 // This may take a longish time. Go to busy cursor
1136 wxBusyCursor busy;
1137
1138 wxASSERT(data);
1139
1140 wxString search,path,filename;
1141
1142 wxString dirName(data->m_path);
1143
0d853c54 1144#if defined(__WXMSW__) || defined(__OS2__)
51a58d8b
JS
1145 if (dirName.Last() == ':')
1146 dirName += wxString(wxFILE_SEP_PATH);
1147#endif
1148
1149 wxDir d;
1150 wxString eachFilename;
1151
f9c165b1 1152 wxLogNull log;
51a58d8b
JS
1153 d.Open(dirName);
1154
1155 if (d.IsOpened())
1156 {
1157 if (d.GetFirst(& eachFilename, m_currentFilterStr, dirFlags))
1158 {
1159 do
1160 {
1161 if ((eachFilename != wxT(".")) && (eachFilename != wxT("..")))
1162 {
1163 filenames.Add(eachFilename);
1164 }
1165 }
1166 while (d.GetNext(& eachFilename)) ;
1167 }
1168 }
1169}
1170#endif
1171
1172void wxGenericDirCtrl::SetFilterIndex(int n)
1173{
1174 m_currentFilter = n;
1175
1176 wxString f, d;
1177 if (ExtractWildcard(m_filter, n, f, d))
1178 m_currentFilterStr = f;
1179 else
dd0138d9
RR
1180#ifdef __UNIX__
1181 m_currentFilterStr = wxT("*");
1182#else
51a58d8b 1183 m_currentFilterStr = wxT("*.*");
dd0138d9 1184#endif
51a58d8b
JS
1185}
1186
1187void wxGenericDirCtrl::SetFilter(const wxString& filter)
1188{
1189 m_filter = filter;
1190
1191 wxString f, d;
1192 if (ExtractWildcard(m_filter, m_currentFilter, f, d))
1193 m_currentFilterStr = f;
1194 else
dd0138d9
RR
1195#ifdef __UNIX__
1196 m_currentFilterStr = wxT("*");
1197#else
51a58d8b 1198 m_currentFilterStr = wxT("*.*");
dd0138d9 1199#endif
51a58d8b
JS
1200}
1201
1202// Extract description and actual filter from overall filter string
1203bool wxGenericDirCtrl::ExtractWildcard(const wxString& filterStr, int n, wxString& filter, wxString& description)
1204{
1205 wxArrayString filters, descriptions;
daf32463 1206 int count = wxParseCommonDialogsFilter(filterStr, descriptions, filters);
51a58d8b
JS
1207 if (count > 0 && n < count)
1208 {
1209 filter = filters[n];
1210 description = descriptions[n];
ca65c044 1211 return true;
51a58d8b 1212 }
5716a1ab 1213
ca65c044 1214 return false;
51a58d8b
JS
1215}
1216
9e152a55 1217#if WXWIN_COMPATIBILITY_2_4
51a58d8b
JS
1218// Parses the global filter, returning the number of filters.
1219// Returns 0 if none or if there's a problem.
b600ed13 1220// filterStr is in the form: "All files (*.*)|*.*|JPEG Files (*.jpeg)|*.jpg"
51a58d8b
JS
1221int wxGenericDirCtrl::ParseFilter(const wxString& filterStr, wxArrayString& filters, wxArrayString& descriptions)
1222{
daf32463 1223 return wxParseCommonDialogsFilter(filterStr, descriptions, filters );
51a58d8b 1224}
9e152a55 1225#endif // WXWIN_COMPATIBILITY_2_4
51a58d8b
JS
1226
1227void wxGenericDirCtrl::DoResize()
1228{
1229 wxSize sz = GetClientSize();
1230 int verticalSpacing = 3;
1231 if (m_treeCtrl)
1232 {
1233 wxSize filterSz ;
1234 if (m_filterListCtrl)
1235 {
1236 filterSz = m_filterListCtrl->GetSize();
1237 sz.y -= (filterSz.y + verticalSpacing);
1238 }
1239 m_treeCtrl->SetSize(0, 0, sz.x, sz.y);
1240 if (m_filterListCtrl)
1241 {
1242 m_filterListCtrl->SetSize(0, sz.y + verticalSpacing, sz.x, filterSz.y);
1243 // Don't know why, but this needs refreshing after a resize (wxMSW)
1244 m_filterListCtrl->Refresh();
1245 }
1246 }
1247}
1248
1249
33ac7e6f 1250void wxGenericDirCtrl::OnSize(wxSizeEvent& WXUNUSED(event))
51a58d8b
JS
1251{
1252 DoResize();
1253}
1254
22328fa4 1255wxTreeItemId wxGenericDirCtrl::AppendItem (const wxTreeItemId & parent,
ca65c044
WS
1256 const wxString & text,
1257 int image, int selectedImage,
1258 wxTreeItemData * data)
22328fa4
JS
1259{
1260 wxTreeCtrl *treeCtrl = GetTreeCtrl ();
1261
1262 wxASSERT (treeCtrl);
1263
1264 if (treeCtrl)
1265 {
1266 return treeCtrl->AppendItem (parent, text, image, selectedImage, data);
1267 }
1268 else
1269 {
1270 return wxTreeItemId();
1271 }
1272}
1273
1274
51a58d8b
JS
1275//-----------------------------------------------------------------------------
1276// wxDirFilterListCtrl
1277//-----------------------------------------------------------------------------
1278
1279IMPLEMENT_CLASS(wxDirFilterListCtrl, wxChoice)
1280
1281BEGIN_EVENT_TABLE(wxDirFilterListCtrl, wxChoice)
ca65c044 1282 EVT_CHOICE(wxID_ANY, wxDirFilterListCtrl::OnSelFilter)
51a58d8b
JS
1283END_EVENT_TABLE()
1284
1285bool wxDirFilterListCtrl::Create(wxGenericDirCtrl* parent, const wxWindowID id,
1286 const wxPoint& pos,
1287 const wxSize& size,
1288 long style)
1289{
1290 m_dirCtrl = parent;
1291 return wxChoice::Create(parent, id, pos, size, 0, NULL, style);
1292}
1293
1294void wxDirFilterListCtrl::Init()
1295{
1296 m_dirCtrl = NULL;
1297}
1298
33ac7e6f 1299void wxDirFilterListCtrl::OnSelFilter(wxCommandEvent& WXUNUSED(event))
51a58d8b
JS
1300{
1301 int sel = GetSelection();
1302
1303 wxString currentPath = m_dirCtrl->GetPath();
ec1b28a3 1304
51a58d8b
JS
1305 m_dirCtrl->SetFilterIndex(sel);
1306
1307 // If the filter has changed, the view is out of date, so
1308 // collapse the tree.
08887820 1309 m_dirCtrl->ReCreateTree();
51a58d8b
JS
1310
1311 // Try to restore the selection, or at least the directory
1312 m_dirCtrl->ExpandPath(currentPath);
1313}
1314
1315void wxDirFilterListCtrl::FillFilterList(const wxString& filter, int defaultFilter)
1316{
1317 Clear();
1318 wxArrayString descriptions, filters;
866918a8 1319 size_t n = (size_t) wxParseCommonDialogsFilter(filter, descriptions, filters);
51a58d8b
JS
1320
1321 if (n > 0 && defaultFilter < (int) n)
1322 {
999836aa 1323 for (size_t i = 0; i < n; i++)
51a58d8b
JS
1324 Append(descriptions[i]);
1325 SetSelection(defaultFilter);
1326 }
1327}
13de0c8c 1328#endif // wxUSE_DIRDLG
51a58d8b 1329
13de0c8c 1330#if wxUSE_DIRDLG || wxUSE_FILEDLG
22328fa4 1331
06cc1fb9
JS
1332// ----------------------------------------------------------------------------
1333// wxFileIconsTable icons
1334// ----------------------------------------------------------------------------
1335
d5f330f9 1336#ifndef __WXGTK24__
13b22a67 1337/* Computer (c) Julian Smart */
06cc1fb9 1338static const char * file_icons_tbl_computer_xpm[] = {
13b22a67
JS
1339/* columns rows colors chars-per-pixel */
1340"16 16 42 1",
1341"r c #4E7FD0",
1342"$ c #7198D9",
1343"; c #DCE6F6",
1344"q c #FFFFFF",
1345"u c #4A7CCE",
1346"# c #779DDB",
1347"w c #95B2E3",
1348"y c #7FA2DD",
1349"f c #3263B4",
1350"= c #EAF0FA",
1351"< c #B1C7EB",
1352"% c #6992D7",
1353"9 c #D9E4F5",
1354"o c #9BB7E5",
1355"6 c #F7F9FD",
1356", c #BED0EE",
1357"3 c #F0F5FC",
1358"1 c #A8C0E8",
1359" c None",
1360"0 c #FDFEFF",
1361"4 c #C4D5F0",
1362"@ c #81A4DD",
1363"e c #4377CD",
1364"- c #E2EAF8",
1365"i c #9FB9E5",
1366"> c #CCDAF2",
1367"+ c #89A9DF",
1368"s c #5584D1",
1369"t c #5D89D3",
1370": c #D2DFF4",
1371"5 c #FAFCFE",
1372"2 c #F5F8FD",
1373"8 c #DFE8F7",
1374"& c #5E8AD4",
1375"X c #638ED5",
1376"a c #CEDCF2",
1377"p c #90AFE2",
1378"d c #2F5DA9",
1379"* c #5282D0",
1380"7 c #E5EDF9",
1381". c #A2BCE6",
1382"O c #8CACE0",
1383/* pixels */
1384" ",
1385" .XXXXXXXXXXX ",
1386" oXO++@#$%&*X ",
1387" oX=-;:>,<1%X ",
1388" oX23=-;:4,$X ",
1389" oX5633789:@X ",
1390" oX05623=78+X ",
1391" oXqq05623=OX ",
1392" oX,,,,,<<<$X ",
1393" wXXXXXXXXXXe ",
1394" XrtX%$$y@+O,, ",
1395" uyiiiiiiiii@< ",
1396" ouiiiiiiiiiip<a",
1397" rustX%$$y@+Ow,,",
1398" dfffffffffffffd",
1399" "
1400};
d5f330f9 1401#endif // GTK+ < 2.4
06cc1fb9 1402
06cc1fb9
JS
1403// ----------------------------------------------------------------------------
1404// wxFileIconsTable & friends
1405// ----------------------------------------------------------------------------
1406
1407// global instance of a wxFileIconsTable
1408wxFileIconsTable* wxTheFileIconsTable = (wxFileIconsTable *)NULL;
1409
1410// A module to allow icons table cleanup
1411
1412class wxFileIconsTableModule: public wxModule
1413{
1414DECLARE_DYNAMIC_CLASS(wxFileIconsTableModule)
1415public:
1416 wxFileIconsTableModule() {}
ca65c044 1417 bool OnInit() { wxTheFileIconsTable = new wxFileIconsTable; return true; }
06cc1fb9
JS
1418 void OnExit()
1419 {
1420 if (wxTheFileIconsTable)
1421 {
1422 delete wxTheFileIconsTable;
1423 wxTheFileIconsTable = NULL;
1424 }
1425 }
1426};
1427
1428IMPLEMENT_DYNAMIC_CLASS(wxFileIconsTableModule, wxModule)
1429
1430class wxFileIconEntry : public wxObject
1431{
1432public:
1433 wxFileIconEntry(int i) { id = i; }
1434
1435 int id;
1436};
1437
1438wxFileIconsTable::wxFileIconsTable()
1439{
1440 m_HashTable = NULL;
1441 m_smallImageList = NULL;
1442}
1443
1444wxFileIconsTable::~wxFileIconsTable()
1445{
222ed1d6
MB
1446 if (m_HashTable)
1447 {
1448 WX_CLEAR_HASH_TABLE(*m_HashTable);
1449 delete m_HashTable;
1450 }
06cc1fb9
JS
1451 if (m_smallImageList) delete m_smallImageList;
1452}
1453
1454// delayed initialization - wait until first use (wxArtProv not created yet)
1455void wxFileIconsTable::Create()
1456{
1457 wxCHECK_RET(!m_smallImageList && !m_HashTable, wxT("creating icons twice"));
06cc1fb9
JS
1458 m_HashTable = new wxHashTable(wxKEY_STRING);
1459 m_smallImageList = new wxImageList(16, 16);
1460
06cc1fb9 1461 // folder:
4b92a33f
VS
1462 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_FOLDER,
1463 wxART_CMN_DIALOG,
1464 wxSize(16, 16)));
06cc1fb9 1465 // folder_open
82c1f2a3
RR
1466 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_FOLDER_OPEN,
1467 wxART_CMN_DIALOG,
1468 wxSize(16, 16)));
06cc1fb9 1469 // computer
82c1f2a3
RR
1470#ifdef __WXGTK24__
1471 // GTK24 uses this icon in the file open dialog
1472 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_HARDDISK,
1473 wxART_CMN_DIALOG,
1474 wxSize(16, 16)));
1475#else
06cc1fb9 1476 m_smallImageList->Add(wxIcon(file_icons_tbl_computer_xpm));
82c1f2a3 1477#endif
06cc1fb9 1478 // drive
82c1f2a3
RR
1479 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_HARDDISK,
1480 wxART_CMN_DIALOG,
1481 wxSize(16, 16)));
06cc1fb9 1482 // cdrom
82c1f2a3
RR
1483 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_CDROM,
1484 wxART_CMN_DIALOG,
1485 wxSize(16, 16)));
06cc1fb9 1486 // floppy
82c1f2a3
RR
1487 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_FLOPPY,
1488 wxART_CMN_DIALOG,
1489 wxSize(16, 16)));
06cc1fb9 1490 // removeable
82c1f2a3
RR
1491 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_REMOVABLE,
1492 wxART_CMN_DIALOG,
1493 wxSize(16, 16)));
06cc1fb9 1494 // file
4b92a33f
VS
1495 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_NORMAL_FILE,
1496 wxART_CMN_DIALOG,
1497 wxSize(16, 16)));
06cc1fb9
JS
1498 // executable
1499 if (GetIconID(wxEmptyString, _T("application/x-executable")) == file)
1500 {
4b92a33f
VS
1501 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_EXECUTABLE_FILE,
1502 wxART_CMN_DIALOG,
1503 wxSize(16, 16)));
222ed1d6 1504 delete m_HashTable->Get(_T("exe"));
06cc1fb9
JS
1505 m_HashTable->Delete(_T("exe"));
1506 m_HashTable->Put(_T("exe"), new wxFileIconEntry(executable));
1507 }
1508 /* else put into list by GetIconID
1509 (KDE defines application/x-executable for *.exe and has nice icon)
1510 */
1511}
1512
1513wxImageList *wxFileIconsTable::GetSmallImageList()
1514{
1515 if (!m_smallImageList)
1516 Create();
1517
1518 return m_smallImageList;
1519}
1520
64c288fa 1521#if wxUSE_MIMETYPE && wxUSE_IMAGE && (!defined(__WXMSW__) || wxUSE_WXDIB)
06cc1fb9
JS
1522// VS: we don't need this function w/o wxMimeTypesManager because we'll only have
1523// one icon and we won't resize it
1524
1525static wxBitmap CreateAntialiasedBitmap(const wxImage& img)
1526{
1d529ef7 1527 const unsigned int size = 16;
ca65c044 1528
1d529ef7 1529 wxImage smallimg (size, size);
06cc1fb9
JS
1530 unsigned char *p1, *p2, *ps;
1531 unsigned char mr = img.GetMaskRed(),
1532 mg = img.GetMaskGreen(),
1533 mb = img.GetMaskBlue();
1534
1535 unsigned x, y;
1536 unsigned sr, sg, sb, smask;
1537
1d529ef7 1538 p1 = img.GetData(), p2 = img.GetData() + 3 * size*2, ps = smallimg.GetData();
06cc1fb9
JS
1539 smallimg.SetMaskColour(mr, mr, mr);
1540
1d529ef7 1541 for (y = 0; y < size; y++)
06cc1fb9 1542 {
1d529ef7 1543 for (x = 0; x < size; x++)
06cc1fb9
JS
1544 {
1545 sr = sg = sb = smask = 0;
1546 if (p1[0] != mr || p1[1] != mg || p1[2] != mb)
1547 sr += p1[0], sg += p1[1], sb += p1[2];
1548 else smask++;
1549 p1 += 3;
1550 if (p1[0] != mr || p1[1] != mg || p1[2] != mb)
1551 sr += p1[0], sg += p1[1], sb += p1[2];
1552 else smask++;
1553 p1 += 3;
1554 if (p2[0] != mr || p2[1] != mg || p2[2] != mb)
1555 sr += p2[0], sg += p2[1], sb += p2[2];
1556 else smask++;
1557 p2 += 3;
1558 if (p2[0] != mr || p2[1] != mg || p2[2] != mb)
1559 sr += p2[0], sg += p2[1], sb += p2[2];
1560 else smask++;
1561 p2 += 3;
1562
1563 if (smask > 2)
1564 ps[0] = ps[1] = ps[2] = mr;
1565 else
8253f2e0
WS
1566 {
1567 ps[0] = (unsigned char)(sr >> 2);
1568 ps[1] = (unsigned char)(sg >> 2);
1569 ps[2] = (unsigned char)(sb >> 2);
1570 }
06cc1fb9
JS
1571 ps += 3;
1572 }
1d529ef7 1573 p1 += size*2 * 3, p2 += size*2 * 3;
06cc1fb9 1574 }
ca65c044 1575
06cc1fb9
JS
1576 return wxBitmap(smallimg);
1577}
1578
b126fe3a
DS
1579// This function is currently not unused anymore
1580#if 0
06cc1fb9
JS
1581// finds empty borders and return non-empty area of image:
1582static wxImage CutEmptyBorders(const wxImage& img)
1583{
1584 unsigned char mr = img.GetMaskRed(),
1585 mg = img.GetMaskGreen(),
1586 mb = img.GetMaskBlue();
1587 unsigned char *dt = img.GetData(), *dttmp;
1588 unsigned w = img.GetWidth(), h = img.GetHeight();
1589
1590 unsigned top, bottom, left, right, i;
1591 bool empt;
1592
1593#define MK_DTTMP(x,y) dttmp = dt + ((x + y * w) * 3)
ca65c044 1594#define NOEMPTY_PIX(empt) if (dttmp[0] != mr || dttmp[1] != mg || dttmp[2] != mb) {empt = false; break;}
06cc1fb9 1595
ca65c044 1596 for (empt = true, top = 0; empt && top < h; top++)
06cc1fb9
JS
1597 {
1598 MK_DTTMP(0, top);
1599 for (i = 0; i < w; i++, dttmp+=3)
1600 NOEMPTY_PIX(empt)
1601 }
ca65c044 1602 for (empt = true, bottom = h-1; empt && bottom > top; bottom--)
06cc1fb9
JS
1603 {
1604 MK_DTTMP(0, bottom);
1605 for (i = 0; i < w; i++, dttmp+=3)
1606 NOEMPTY_PIX(empt)
1607 }
ca65c044 1608 for (empt = true, left = 0; empt && left < w; left++)
06cc1fb9
JS
1609 {
1610 MK_DTTMP(left, 0);
1611 for (i = 0; i < h; i++, dttmp+=3*w)
1612 NOEMPTY_PIX(empt)
1613 }
ca65c044 1614 for (empt = true, right = w-1; empt && right > left; right--)
06cc1fb9
JS
1615 {
1616 MK_DTTMP(right, 0);
1617 for (i = 0; i < h; i++, dttmp+=3*w)
1618 NOEMPTY_PIX(empt)
1619 }
1620 top--, left--, bottom++, right++;
1621
1622 return img.GetSubImage(wxRect(left, top, right - left + 1, bottom - top + 1));
1623}
b126fe3a
DS
1624#endif // #if 0
1625
06cc1fb9
JS
1626#endif // wxUSE_MIMETYPE
1627
1628int wxFileIconsTable::GetIconID(const wxString& extension, const wxString& mime)
1629{
1630 if (!m_smallImageList)
1631 Create();
1632
1633#if wxUSE_MIMETYPE
44d60c0b 1634 if (!extension.empty())
06cc1fb9
JS
1635 {
1636 wxFileIconEntry *entry = (wxFileIconEntry*) m_HashTable->Get(extension);
1637 if (entry) return (entry -> id);
1638 }
1639
44d60c0b 1640 wxFileType *ft = (mime.empty()) ?
06cc1fb9
JS
1641 wxTheMimeTypesManager -> GetFileTypeFromExtension(extension) :
1642 wxTheMimeTypesManager -> GetFileTypeFromMimeType(mime);
55d0aaa3
VZ
1643
1644 wxIconLocation iconLoc;
06cc1fb9 1645 wxIcon ic;
02dd0487 1646
55d0aaa3 1647 {
02dd0487
JS
1648 wxLogNull logNull;
1649 if ( ft && ft->GetIcon(&iconLoc) )
1650 {
8e5c6521 1651 ic = wxIcon( iconLoc );
02dd0487 1652 }
55d0aaa3 1653 }
ca65c044 1654
55d0aaa3
VZ
1655 delete ft;
1656
1657 if ( !ic.Ok() )
06cc1fb9
JS
1658 {
1659 int newid = file;
1660 m_HashTable->Put(extension, new wxFileIconEntry(newid));
1661 return newid;
1662 }
1663
3fc93ebd
JS
1664 wxBitmap bmp;
1665 bmp.CopyFromIcon(ic);
06cc1fb9 1666
b6668c25
JS
1667 if ( !bmp.Ok() )
1668 {
1669 int newid = file;
1670 m_HashTable->Put(extension, new wxFileIconEntry(newid));
1671 return newid;
1672 }
1673
1d529ef7 1674 const unsigned int size = 16;
ca65c044 1675
06cc1fb9 1676 int id = m_smallImageList->GetImageCount();
1aa81b17 1677 if ((bmp.GetWidth() == (int) size) && (bmp.GetHeight() == (int) size))
1d529ef7 1678 {
3fc93ebd 1679 m_smallImageList->Add(bmp);
1d529ef7 1680 }
64c288fa 1681#if wxUSE_IMAGE && (!defined(__WXMSW__) || wxUSE_WXDIB)
06cc1fb9
JS
1682 else
1683 {
3fc93ebd
JS
1684 wxImage img = bmp.ConvertToImage();
1685
1d529ef7
RR
1686 if ((img.GetWidth() != size*2) || (img.GetHeight() != size*2))
1687// m_smallImageList->Add(CreateAntialiasedBitmap(CutEmptyBorders(img).Rescale(size*2, size*2)));
1688 m_smallImageList->Add(CreateAntialiasedBitmap(img.Rescale(size*2, size*2)));
06cc1fb9
JS
1689 else
1690 m_smallImageList->Add(CreateAntialiasedBitmap(img));
1691 }
1904aa72
DS
1692#endif // wxUSE_IMAGE
1693
06cc1fb9
JS
1694 m_HashTable->Put(extension, new wxFileIconEntry(id));
1695 return id;
1696
1697#else // !wxUSE_MIMETYPE
1698
79e162f5 1699 wxUnusedVar(mime);
06cc1fb9
JS
1700 if (extension == wxT("exe"))
1701 return executable;
1702 else
1703 return file;
1704#endif // wxUSE_MIMETYPE/!wxUSE_MIMETYPE
1705}
1706
13de0c8c 1707#endif // wxUSE_DIRDLG || wxUSE_FILEDLG