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