]> git.saurik.com Git - wxWidgets.git/blame - src/generic/dirctrlg.cpp
remove miniframe stuff from GtkOnSize(), it's handled by wxFrame
[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)
473 wxFLAGS_MEMBER(wxDIRCTRL_SHOW_FILTERS)
474
475wxEND_FLAGS( wxGenericDirCtrlStyle )
73c36334 476
b0a877ec
SC
477IMPLEMENT_DYNAMIC_CLASS_XTI(wxGenericDirCtrl, wxControl,"wx/dirctrl.h")
478
3ff066a4
SC
479wxBEGIN_PROPERTIES_TABLE(wxGenericDirCtrl)
480 wxHIDE_PROPERTY( Children )
ca65c044
WS
481 wxPROPERTY( DefaultPath , wxString , SetDefaultPath , GetDefaultPath , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
482 wxPROPERTY( Filter , wxString , SetFilter , GetFilter , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
483 wxPROPERTY( DefaultFilter , int , SetFilterIndex, GetFilterIndex, EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
af498247 484 wxPROPERTY_FLAGS( WindowStyle, wxGenericDirCtrlStyle, long, SetWindowStyleFlag, GetWindowStyleFlag, EMPTY_MACROVALUE , 0, wxT("Helpstring"), wxT("group") )
3ff066a4 485wxEND_PROPERTIES_TABLE()
b0a877ec 486
3ff066a4
SC
487wxBEGIN_HANDLERS_TABLE(wxGenericDirCtrl)
488wxEND_HANDLERS_TABLE()
b0a877ec 489
ca65c044
WS
490wxCONSTRUCTOR_8( wxGenericDirCtrl , wxWindow* , Parent , wxWindowID , Id , wxString , DefaultPath ,
491 wxPoint , Position , wxSize , Size , long , WindowStyle , wxString , Filter , int , DefaultFilter )
b0a877ec
SC
492#else
493IMPLEMENT_DYNAMIC_CLASS(wxGenericDirCtrl, wxControl)
494#endif
066f1b7a 495
51a58d8b 496BEGIN_EVENT_TABLE(wxGenericDirCtrl, wxControl)
6b707fc3
JS
497 EVT_TREE_ITEM_EXPANDING (wxID_TREECTRL, wxGenericDirCtrl::OnExpandItem)
498 EVT_TREE_ITEM_COLLAPSED (wxID_TREECTRL, wxGenericDirCtrl::OnCollapseItem)
499 EVT_TREE_BEGIN_LABEL_EDIT (wxID_TREECTRL, wxGenericDirCtrl::OnBeginEditItem)
500 EVT_TREE_END_LABEL_EDIT (wxID_TREECTRL, wxGenericDirCtrl::OnEndEditItem)
51a58d8b
JS
501 EVT_SIZE (wxGenericDirCtrl::OnSize)
502END_EVENT_TABLE()
503
504wxGenericDirCtrl::wxGenericDirCtrl(void)
505{
506 Init();
507}
508
c06dde42
JS
509void wxGenericDirCtrl::ExpandRoot()
510{
511 ExpandDir(m_rootId); // automatically expand first level
512
513 // Expand and select the default path
514 if (!m_defaultPath.empty())
515 {
516 ExpandPath(m_defaultPath);
517 }
518#ifdef __UNIX__
519 else
520 {
521 // On Unix, there's only one node under the (hidden) root node. It
522 // represents the / path, so the user would always have to expand it;
523 // let's do it ourselves
524 ExpandPath( wxT("/") );
525 }
526#endif
527}
528
51a58d8b 529bool wxGenericDirCtrl::Create(wxWindow *parent,
748fcded
VS
530 const wxWindowID id,
531 const wxString& dir,
532 const wxPoint& pos,
533 const wxSize& size,
534 long style,
535 const wxString& filter,
536 int defaultFilter,
537 const wxString& name)
51a58d8b
JS
538{
539 if (!wxControl::Create(parent, id, pos, size, style, wxDefaultValidator, name))
ca65c044 540 return false;
51a58d8b 541
db5333a5 542 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));
e1957703 543 SetForegroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT));
51a58d8b
JS
544
545 Init();
546
3bd8a405
JS
547 long treeStyle = wxTR_HAS_BUTTONS;
548
549 // On Windows CE, if you hide the root, you get a crash when
550 // attempting to access data for children of the root item.
551#ifndef __WXWINCE__
552 treeStyle |= wxTR_HIDE_ROOT;
553#endif
2997ca30 554
82c1f2a3
RR
555#ifdef __WXGTK20__
556 treeStyle |= wxTR_NO_LINES;
557#endif
fd775aae 558
dabd1377 559 if (style & wxDIRCTRL_EDIT_LABELS)
fd775aae
JS
560 treeStyle |= wxTR_EDIT_LABELS;
561
51a58d8b
JS
562 if ((style & wxDIRCTRL_3D_INTERNAL) == 0)
563 treeStyle |= wxNO_BORDER;
27c73976
JS
564 else
565 treeStyle |= wxBORDER_SUNKEN;
51a58d8b
JS
566
567 long filterStyle = 0;
568 if ((style & wxDIRCTRL_3D_INTERNAL) == 0)
569 filterStyle |= wxNO_BORDER;
27c73976
JS
570 else
571 filterStyle |= wxBORDER_SUNKEN;
51a58d8b 572
a78955e3 573 m_treeCtrl = CreateTreeCtrl(this, wxID_TREECTRL,
c47addef 574 wxPoint(0,0), GetClientSize(), treeStyle);
51a58d8b 575
44d60c0b 576 if (!filter.empty() && (style & wxDIRCTRL_SHOW_FILTERS))
51a58d8b
JS
577 m_filterListCtrl = new wxDirFilterListCtrl(this, wxID_FILTERLISTCTRL, wxDefaultPosition, wxDefaultSize, filterStyle);
578
579 m_defaultPath = dir;
580 m_filter = filter;
581
dd0138d9
RR
582 if (m_filter.empty())
583#ifdef __UNIX__
584 m_filter = wxT("*");
585#else
586 m_filter = wxT("*.*");
587#endif
588
51a58d8b
JS
589 SetFilterIndex(defaultFilter);
590
591 if (m_filterListCtrl)
592 m_filterListCtrl->FillFilterList(filter, defaultFilter);
593
06cc1fb9 594 m_treeCtrl->SetImageList(wxTheFileIconsTable->GetSmallImageList());
51a58d8b 595
ca65c044
WS
596 m_showHidden = false;
597 wxDirItemData* rootData = new wxDirItemData(wxEmptyString, wxEmptyString, true);
51a58d8b
JS
598
599 wxString rootName;
600
0d853c54 601#if defined(__WINDOWS__) || defined(__OS2__) || defined(__DOS__)
51a58d8b
JS
602 rootName = _("Computer");
603#else
604 rootName = _("Sections");
605#endif
606
607 m_rootId = m_treeCtrl->AddRoot( rootName, 3, -1, rootData);
608 m_treeCtrl->SetItemHasChildren(m_rootId);
2997ca30 609
c06dde42 610 ExpandRoot();
51a58d8b 611
4a89f0e9 612 SetInitialSize(size);
51a58d8b
JS
613 DoResize();
614
ca65c044 615 return true;
51a58d8b
JS
616}
617
618wxGenericDirCtrl::~wxGenericDirCtrl()
619{
51a58d8b
JS
620}
621
622void wxGenericDirCtrl::Init()
623{
ca65c044 624 m_showHidden = false;
51a58d8b
JS
625 m_currentFilter = 0;
626 m_currentFilterStr = wxEmptyString; // Default: any file
627 m_treeCtrl = NULL;
628 m_filterListCtrl = NULL;
629}
630
a78955e3
VS
631wxTreeCtrl* wxGenericDirCtrl::CreateTreeCtrl(wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long treeStyle)
632{
633 return new wxTreeCtrl(parent, id, pos, size, treeStyle);
634}
635
42dcacf0
RR
636void wxGenericDirCtrl::ShowHidden( bool show )
637{
9092371c
VZ
638 if ( m_showHidden == show )
639 return;
640
42dcacf0 641 m_showHidden = show;
574c939e 642
3b423cdd 643 wxString path = GetPath();
08887820 644 ReCreateTree();
3b423cdd 645 SetPath(path);
42dcacf0
RR
646}
647
22328fa4
JS
648const wxTreeItemId
649wxGenericDirCtrl::AddSection(const wxString& path, const wxString& name, int imageId)
51a58d8b 650{
ca65c044 651 wxDirItemData *dir_item = new wxDirItemData(path,name,true);
51a58d8b 652
22328fa4 653 wxTreeItemId id = AppendItem( m_rootId, name, imageId, -1, dir_item);
4f5c180e 654
06cc1fb9 655 m_treeCtrl->SetItemHasChildren(id);
22328fa4
JS
656
657 return id;
06cc1fb9 658}
51a58d8b 659
06cc1fb9
JS
660void wxGenericDirCtrl::SetupSections()
661{
662 wxArrayString paths, names;
663 wxArrayInt icons;
2d4e4f80 664
06cc1fb9 665 size_t n, count = wxGetAvailableDrives(paths, names, icons);
4f5c180e 666
82c1f2a3
RR
667#ifdef __WXGTK20__
668 wxString home = wxGetHomeDir();
669 AddSection( home, _("Home directory"), 1);
670 home += wxT("/Desktop");
671 AddSection( home, _("Desktop"), 1);
672#endif
673
06cc1fb9 674 for (n = 0; n < count; n++)
06cc1fb9 675 AddSection(paths[n], names[n], icons[n]);
51a58d8b
JS
676}
677
678void wxGenericDirCtrl::OnBeginEditItem(wxTreeEvent &event)
679{
680 // don't rename the main entry "Sections"
681 if (event.GetItem() == m_rootId)
682 {
683 event.Veto();
684 return;
685 }
686
687 // don't rename the individual sections
99006e44 688 if (m_treeCtrl->GetItemParent( event.GetItem() ) == m_rootId)
51a58d8b
JS
689 {
690 event.Veto();
691 return;
692 }
693}
694
695void wxGenericDirCtrl::OnEndEditItem(wxTreeEvent &event)
696{
6e86472f
VZ
697 if (event.IsEditCancelled())
698 return;
699
44d60c0b 700 if ((event.GetLabel().empty()) ||
47b8afad
MR
701 (event.GetLabel() == wxT(".")) ||
702 (event.GetLabel() == wxT("..")) ||
06cc1fb9
JS
703 (event.GetLabel().Find(wxT('/')) != wxNOT_FOUND) ||
704 (event.GetLabel().Find(wxT('\\')) != wxNOT_FOUND) ||
705 (event.GetLabel().Find(wxT('|')) != wxNOT_FOUND))
51a58d8b
JS
706 {
707 wxMessageDialog dialog(this, _("Illegal directory name."), _("Error"), wxOK | wxICON_ERROR );
708 dialog.ShowModal();
709 event.Veto();
710 return;
711 }
712
713 wxTreeItemId id = event.GetItem();
748fcded 714 wxDirItemData *data = (wxDirItemData*)m_treeCtrl->GetItemData( id );
51a58d8b
JS
715 wxASSERT( data );
716
717 wxString new_name( wxPathOnly( data->m_path ) );
ad9cd15c 718 new_name += wxString(wxFILE_SEP_PATH);
51a58d8b
JS
719 new_name += event.GetLabel();
720
721 wxLogNull log;
722
723 if (wxFileExists(new_name))
724 {
725 wxMessageDialog dialog(this, _("File name exists already."), _("Error"), wxOK | wxICON_ERROR );
726 dialog.ShowModal();
727 event.Veto();
728 }
729
730 if (wxRenameFile(data->m_path,new_name))
731 {
732 data->SetNewDirName( new_name );
733 }
734 else
735 {
736 wxMessageDialog dialog(this, _("Operation not permitted."), _("Error"), wxOK | wxICON_ERROR );
737 dialog.ShowModal();
738 event.Veto();
739 }
740}
741
742void wxGenericDirCtrl::OnExpandItem(wxTreeEvent &event)
743{
744 wxTreeItemId parentId = event.GetItem();
745
748fcded
VS
746 // VS: this is needed because the event handler is called from wxTreeCtrl
747 // ctor when wxTR_HIDE_ROOT was specified
4ded51f2
CE
748
749 if (!m_rootId.IsOk())
750
748fcded
VS
751 m_rootId = m_treeCtrl->GetRootItem();
752
51a58d8b
JS
753 ExpandDir(parentId);
754}
755
756void wxGenericDirCtrl::OnCollapseItem(wxTreeEvent &event )
757{
08887820
VS
758 CollapseDir(event.GetItem());
759}
760
761void wxGenericDirCtrl::CollapseDir(wxTreeItemId parentId)
762{
763 wxTreeItemId child;
51a58d8b 764
08887820 765 wxDirItemData *data = (wxDirItemData *) m_treeCtrl->GetItemData(parentId);
51a58d8b
JS
766 if (!data->m_isExpanded)
767 return;
768
ca65c044 769 data->m_isExpanded = false;
2d75caaa 770 wxTreeItemIdValue cookie;
51a58d8b
JS
771 /* Workaround because DeleteChildren has disapeared (why?) and
772 * CollapseAndReset doesn't work as advertised (deletes parent too) */
08887820 773 child = m_treeCtrl->GetFirstChild(parentId, cookie);
51a58d8b
JS
774 while (child.IsOk())
775 {
776 m_treeCtrl->Delete(child);
777 /* Not GetNextChild below, because the cookie mechanism can't
778 * handle disappearing children! */
08887820 779 child = m_treeCtrl->GetFirstChild(parentId, cookie);
51a58d8b 780 }
c06dde42
JS
781 if (parentId != m_treeCtrl->GetRootItem())
782 m_treeCtrl->Collapse(parentId);
51a58d8b
JS
783}
784
785void wxGenericDirCtrl::ExpandDir(wxTreeItemId parentId)
786{
748fcded 787 wxDirItemData *data = (wxDirItemData *) m_treeCtrl->GetItemData(parentId);
51a58d8b
JS
788
789 if (data->m_isExpanded)
790 return;
791
ca65c044 792 data->m_isExpanded = true;
51a58d8b 793
748fcded 794 if (parentId == m_treeCtrl->GetRootItem())
51a58d8b
JS
795 {
796 SetupSections();
797 return;
798 }
799
800 wxASSERT(data);
801
802 wxString search,path,filename;
803
804 wxString dirName(data->m_path);
805
3bd8a405 806#if (defined(__WINDOWS__) && !defined(__WXWINCE__)) || defined(__DOS__) || defined(__OS2__)
51a58d8b
JS
807 // Check if this is a root directory and if so,
808 // whether the drive is avaiable.
33fed835
MB
809 if (!wxIsDriveAvailable(dirName))
810 {
ca65c044 811 data->m_isExpanded = false;
7328394a
JS
812 //wxMessageBox(wxT("Sorry, this drive is not available."));
813 return;
33fed835 814 }
51a58d8b
JS
815#endif
816
817 // This may take a longish time. Go to busy cursor
818 wxBusyCursor busy;
819
0d853c54 820#if defined(__WINDOWS__) || defined(__DOS__) || defined(__OS2__)
51a58d8b
JS
821 if (dirName.Last() == ':')
822 dirName += wxString(wxFILE_SEP_PATH);
823#endif
824
825 wxArrayString dirs;
826 wxArrayString filenames;
827
828 wxDir d;
829 wxString eachFilename;
830
f9c165b1 831 wxLogNull log;
51a58d8b
JS
832 d.Open(dirName);
833
834 if (d.IsOpened())
835 {
42dcacf0
RR
836 int style = wxDIR_DIRS;
837 if (m_showHidden) style |= wxDIR_HIDDEN;
838 if (d.GetFirst(& eachFilename, wxEmptyString, style))
51a58d8b
JS
839 {
840 do
841 {
842 if ((eachFilename != wxT(".")) && (eachFilename != wxT("..")))
843 {
844 dirs.Add(eachFilename);
845 }
846 }
2b0a7c09 847 while (d.GetNext(&eachFilename));
51a58d8b
JS
848 }
849 }
222ed1d6 850 dirs.Sort(wxDirCtrlStringCompareFunction);
51a58d8b
JS
851
852 // Now do the filenames -- but only if we're allowed to
853 if ((GetWindowStyle() & wxDIRCTRL_DIR_ONLY) == 0)
854 {
855 d.Open(dirName);
ec1b28a3 856
51a58d8b
JS
857 if (d.IsOpened())
858 {
2b0a7c09
RN
859 int style = wxDIR_FILES;
860 if (m_showHidden) style |= wxDIR_HIDDEN;
3da4e4bd
JS
861 // Process each filter (ex: "JPEG Files (*.jpg;*.jpeg)|*.jpg;*.jpeg")
862 wxStringTokenizer strTok;
863 wxString curFilter;
864 strTok.SetString(m_currentFilterStr,wxT(";"));
865 while(strTok.HasMoreTokens())
51a58d8b 866 {
3da4e4bd 867 curFilter = strTok.GetNextToken();
f0e5a44d 868 if (d.GetFirst(& eachFilename, curFilter, style))
51a58d8b 869 {
3da4e4bd 870 do
51a58d8b 871 {
3da4e4bd
JS
872 if ((eachFilename != wxT(".")) && (eachFilename != wxT("..")))
873 {
874 filenames.Add(eachFilename);
875 }
51a58d8b 876 }
3da4e4bd 877 while (d.GetNext(& eachFilename));
51a58d8b 878 }
51a58d8b
JS
879 }
880 }
222ed1d6 881 filenames.Sort(wxDirCtrlStringCompareFunction);
51a58d8b
JS
882 }
883
884 // Add the sorted dirs
885 size_t i;
b4a980f4 886 for (i = 0; i < dirs.GetCount(); i++)
51a58d8b 887 {
4e115ed2 888 eachFilename = dirs[i];
51a58d8b 889 path = dirName;
083f7497 890 if (!wxEndsWithPathSeparator(path))
51a58d8b
JS
891 path += wxString(wxFILE_SEP_PATH);
892 path += eachFilename;
893
ca65c044 894 wxDirItemData *dir_item = new wxDirItemData(path,eachFilename,true);
22328fa4 895 wxTreeItemId id = AppendItem( parentId, eachFilename,
06cc1fb9
JS
896 wxFileIconsTable::folder, -1, dir_item);
897 m_treeCtrl->SetItemImage( id, wxFileIconsTable::folder_open,
898 wxTreeItemIcon_Expanded );
ec1b28a3 899
51a58d8b 900 // Has this got any children? If so, make it expandable.
748fcded
VS
901 // (There are two situations when a dir has children: either it
902 // has subdirectories or it contains files that weren't filtered
903 // out. The latter only applies to dirctrl with files.)
904 if ( dir_item->HasSubDirs() ||
905 (((GetWindowStyle() & wxDIRCTRL_DIR_ONLY) == 0) &&
906 dir_item->HasFiles(m_currentFilterStr)) )
51a58d8b 907 {
748fcded 908 m_treeCtrl->SetItemHasChildren(id);
51a58d8b 909 }
51a58d8b
JS
910 }
911
912 // Add the sorted filenames
913 if ((GetWindowStyle() & wxDIRCTRL_DIR_ONLY) == 0)
914 {
b4a980f4 915 for (i = 0; i < filenames.GetCount(); i++)
51a58d8b 916 {
4e115ed2 917 eachFilename = filenames[i];
51a58d8b 918 path = dirName;
083f7497 919 if (!wxEndsWithPathSeparator(path))
51a58d8b
JS
920 path += wxString(wxFILE_SEP_PATH);
921 path += eachFilename;
922 //path = dirName + wxString(wxT("/")) + eachFilename;
ca65c044 923 wxDirItemData *dir_item = new wxDirItemData(path,eachFilename,false);
06cc1fb9
JS
924 int image_id = wxFileIconsTable::file;
925 if (eachFilename.Find(wxT('.')) != wxNOT_FOUND)
926 image_id = wxTheFileIconsTable->GetIconID(eachFilename.AfterLast(wxT('.')));
22328fa4 927 (void) AppendItem( parentId, eachFilename, image_id, -1, dir_item);
51a58d8b
JS
928 }
929 }
930}
931
08887820
VS
932void wxGenericDirCtrl::ReCreateTree()
933{
934 CollapseDir(m_treeCtrl->GetRootItem());
c06dde42
JS
935 ExpandRoot();
936}
937
938void wxGenericDirCtrl::CollapseTree()
939{
940 wxTreeItemIdValue cookie;
941 wxTreeItemId child = m_treeCtrl->GetFirstChild(m_rootId, cookie);
942 while (child.IsOk())
943 {
944 CollapseDir(child);
945 child = m_treeCtrl->GetNextChild(m_rootId, cookie);
946 }
08887820
VS
947}
948
51a58d8b
JS
949// Find the child that matches the first part of 'path'.
950// E.g. if a child path is "/usr" and 'path' is "/usr/include"
951// then the child for /usr is returned.
952wxTreeItemId wxGenericDirCtrl::FindChild(wxTreeItemId parentId, const wxString& path, bool& done)
953{
954 wxString path2(path);
ec1b28a3 955
51a58d8b
JS
956 // Make sure all separators are as per the current platform
957 path2.Replace(wxT("\\"), wxString(wxFILE_SEP_PATH));
958 path2.Replace(wxT("/"), wxString(wxFILE_SEP_PATH));
ec1b28a3 959
51a58d8b
JS
960 // Append a separator to foil bogus substring matching
961 path2 += wxString(wxFILE_SEP_PATH);
ec1b28a3
DW
962
963 // In MSW or PM, case is not significant
0d853c54 964#if defined(__WINDOWS__) || defined(__DOS__) || defined(__OS2__)
51a58d8b
JS
965 path2.MakeLower();
966#endif
ec1b28a3 967
2d75caaa 968 wxTreeItemIdValue cookie;
51a58d8b 969 wxTreeItemId childId = m_treeCtrl->GetFirstChild(parentId, cookie);
53ccf1c0 970 while (childId.IsOk())
51a58d8b 971 {
748fcded 972 wxDirItemData* data = (wxDirItemData*) m_treeCtrl->GetItemData(childId);
ec1b28a3 973
44d60c0b 974 if (data && !data->m_path.empty())
51a58d8b
JS
975 {
976 wxString childPath(data->m_path);
083f7497 977 if (!wxEndsWithPathSeparator(childPath))
51a58d8b 978 childPath += wxString(wxFILE_SEP_PATH);
ec1b28a3
DW
979
980 // In MSW and PM, case is not significant
0d853c54 981#if defined(__WINDOWS__) || defined(__DOS__) || defined(__OS2__)
51a58d8b
JS
982 childPath.MakeLower();
983#endif
ec1b28a3 984
b36e08d0 985 if (childPath.length() <= path2.length())
51a58d8b 986 {
b36e08d0 987 wxString path3 = path2.Mid(0, childPath.length());
51a58d8b
JS
988 if (childPath == path3)
989 {
b36e08d0 990 if (path3.length() == path2.length())
ca65c044 991 done = true;
51a58d8b 992 else
ca65c044 993 done = false;
51a58d8b
JS
994 return childId;
995 }
996 }
997 }
ec1b28a3 998
748fcded 999 childId = m_treeCtrl->GetNextChild(parentId, cookie);
51a58d8b 1000 }
3fa4bd0e
VS
1001 wxTreeItemId invalid;
1002 return invalid;
51a58d8b
JS
1003}
1004
1005// Try to expand as much of the given path as possible,
1006// and select the given tree item.
1007bool wxGenericDirCtrl::ExpandPath(const wxString& path)
1008{
ca65c044 1009 bool done = false;
51a58d8b
JS
1010 wxTreeItemId id = FindChild(m_rootId, path, done);
1011 wxTreeItemId lastId = id; // The last non-zero id
237387ad 1012 while (id.IsOk() && !done)
51a58d8b
JS
1013 {
1014 ExpandDir(id);
1015
1016 id = FindChild(id, path, done);
53ccf1c0 1017 if (id.IsOk())
51a58d8b
JS
1018 lastId = id;
1019 }
4e115ed2
VZ
1020 if (!lastId.IsOk())
1021 return false;
1022
1023 wxDirItemData *data = (wxDirItemData *) m_treeCtrl->GetItemData(lastId);
1024 if (data->m_isDir)
51a58d8b 1025 {
4e115ed2
VZ
1026 m_treeCtrl->Expand(lastId);
1027 }
1028 if ((GetWindowStyle() & wxDIRCTRL_SELECT_FIRST) && data->m_isDir)
1029 {
1030 // Find the first file in this directory
1031 wxTreeItemIdValue cookie;
1032 wxTreeItemId childId = m_treeCtrl->GetFirstChild(lastId, cookie);
1033 bool selectedChild = false;
1034 while (childId.IsOk())
51a58d8b 1035 {
4e115ed2 1036 data = (wxDirItemData*) m_treeCtrl->GetItemData(childId);
ec1b28a3 1037
4e115ed2 1038 if (data && data->m_path != wxEmptyString && !data->m_isDir)
51a58d8b 1039 {
4e115ed2
VZ
1040 m_treeCtrl->SelectItem(childId);
1041 m_treeCtrl->EnsureVisible(childId);
1042 selectedChild = true;
1043 break;
51a58d8b 1044 }
4e115ed2 1045 childId = m_treeCtrl->GetNextChild(lastId, cookie);
51a58d8b 1046 }
4e115ed2 1047 if (!selectedChild)
51a58d8b
JS
1048 {
1049 m_treeCtrl->SelectItem(lastId);
1050 m_treeCtrl->EnsureVisible(lastId);
1051 }
51a58d8b
JS
1052 }
1053 else
4e115ed2
VZ
1054 {
1055 m_treeCtrl->SelectItem(lastId);
1056 m_treeCtrl->EnsureVisible(lastId);
1057 }
1058
1059 return true;
51a58d8b
JS
1060}
1061
f395a825
VZ
1062
1063bool wxGenericDirCtrl::CollapsePath(const wxString& path)
1064{
1065 bool done = false;
1066 wxTreeItemId id = FindChild(m_rootId, path, done);
1067 wxTreeItemId lastId = id; // The last non-zero id
1068
1069 while ( id.IsOk() && !done )
1070 {
1071 CollapseDir(id);
1072
1073 id = FindChild(id, path, done);
1074
1075 if ( id.IsOk() )
1076 lastId = id;
1077 }
1078
1079 if ( !lastId.IsOk() )
1080 return false;
1081
1082 m_treeCtrl->SelectItem(lastId);
1083 m_treeCtrl->EnsureVisible(lastId);
1084
1085 return true;
1086}
1087
1088
51a58d8b
JS
1089wxString wxGenericDirCtrl::GetPath() const
1090{
1091 wxTreeItemId id = m_treeCtrl->GetSelection();
1092 if (id)
1093 {
748fcded 1094 wxDirItemData* data = (wxDirItemData*) m_treeCtrl->GetItemData(id);
51a58d8b
JS
1095 return data->m_path;
1096 }
1097 else
1098 return wxEmptyString;
1099}
1100
1101wxString wxGenericDirCtrl::GetFilePath() const
1102{
1103 wxTreeItemId id = m_treeCtrl->GetSelection();
1104 if (id)
1105 {
748fcded 1106 wxDirItemData* data = (wxDirItemData*) m_treeCtrl->GetItemData(id);
51a58d8b
JS
1107 if (data->m_isDir)
1108 return wxEmptyString;
1109 else
1110 return data->m_path;
1111 }
1112 else
1113 return wxEmptyString;
1114}
1115
1116void wxGenericDirCtrl::SetPath(const wxString& path)
1117{
1118 m_defaultPath = path;
1119 if (m_rootId)
1120 ExpandPath(path);
1121}
1122
1123// Not used
1124#if 0
1125void wxGenericDirCtrl::FindChildFiles(wxTreeItemId id, int dirFlags, wxArrayString& filenames)
1126{
748fcded 1127 wxDirItemData *data = (wxDirItemData *) m_treeCtrl->GetItemData(id);
51a58d8b
JS
1128
1129 // This may take a longish time. Go to busy cursor
1130 wxBusyCursor busy;
1131
1132 wxASSERT(data);
1133
1134 wxString search,path,filename;
1135
1136 wxString dirName(data->m_path);
1137
0d853c54 1138#if defined(__WXMSW__) || defined(__OS2__)
51a58d8b
JS
1139 if (dirName.Last() == ':')
1140 dirName += wxString(wxFILE_SEP_PATH);
1141#endif
1142
1143 wxDir d;
1144 wxString eachFilename;
1145
f9c165b1 1146 wxLogNull log;
51a58d8b
JS
1147 d.Open(dirName);
1148
1149 if (d.IsOpened())
1150 {
1151 if (d.GetFirst(& eachFilename, m_currentFilterStr, dirFlags))
1152 {
1153 do
1154 {
1155 if ((eachFilename != wxT(".")) && (eachFilename != wxT("..")))
1156 {
1157 filenames.Add(eachFilename);
1158 }
1159 }
1160 while (d.GetNext(& eachFilename)) ;
1161 }
1162 }
1163}
1164#endif
1165
1166void wxGenericDirCtrl::SetFilterIndex(int n)
1167{
1168 m_currentFilter = n;
1169
1170 wxString f, d;
1171 if (ExtractWildcard(m_filter, n, f, d))
1172 m_currentFilterStr = f;
1173 else
dd0138d9
RR
1174#ifdef __UNIX__
1175 m_currentFilterStr = wxT("*");
1176#else
51a58d8b 1177 m_currentFilterStr = wxT("*.*");
dd0138d9 1178#endif
51a58d8b
JS
1179}
1180
1181void wxGenericDirCtrl::SetFilter(const wxString& filter)
1182{
1183 m_filter = filter;
1184
1185 wxString f, d;
1186 if (ExtractWildcard(m_filter, m_currentFilter, f, d))
1187 m_currentFilterStr = f;
1188 else
dd0138d9
RR
1189#ifdef __UNIX__
1190 m_currentFilterStr = wxT("*");
1191#else
51a58d8b 1192 m_currentFilterStr = wxT("*.*");
dd0138d9 1193#endif
51a58d8b
JS
1194}
1195
1196// Extract description and actual filter from overall filter string
1197bool wxGenericDirCtrl::ExtractWildcard(const wxString& filterStr, int n, wxString& filter, wxString& description)
1198{
1199 wxArrayString filters, descriptions;
daf32463 1200 int count = wxParseCommonDialogsFilter(filterStr, descriptions, filters);
51a58d8b
JS
1201 if (count > 0 && n < count)
1202 {
1203 filter = filters[n];
1204 description = descriptions[n];
ca65c044 1205 return true;
51a58d8b 1206 }
5716a1ab 1207
ca65c044 1208 return false;
51a58d8b
JS
1209}
1210
51a58d8b
JS
1211
1212void wxGenericDirCtrl::DoResize()
1213{
1214 wxSize sz = GetClientSize();
1215 int verticalSpacing = 3;
1216 if (m_treeCtrl)
1217 {
1218 wxSize filterSz ;
1219 if (m_filterListCtrl)
1220 {
1221 filterSz = m_filterListCtrl->GetSize();
1222 sz.y -= (filterSz.y + verticalSpacing);
1223 }
1224 m_treeCtrl->SetSize(0, 0, sz.x, sz.y);
1225 if (m_filterListCtrl)
1226 {
1227 m_filterListCtrl->SetSize(0, sz.y + verticalSpacing, sz.x, filterSz.y);
1228 // Don't know why, but this needs refreshing after a resize (wxMSW)
1229 m_filterListCtrl->Refresh();
1230 }
1231 }
1232}
1233
1234
33ac7e6f 1235void wxGenericDirCtrl::OnSize(wxSizeEvent& WXUNUSED(event))
51a58d8b
JS
1236{
1237 DoResize();
1238}
1239
22328fa4 1240wxTreeItemId wxGenericDirCtrl::AppendItem (const wxTreeItemId & parent,
ca65c044
WS
1241 const wxString & text,
1242 int image, int selectedImage,
1243 wxTreeItemData * data)
22328fa4
JS
1244{
1245 wxTreeCtrl *treeCtrl = GetTreeCtrl ();
1246
1247 wxASSERT (treeCtrl);
1248
1249 if (treeCtrl)
1250 {
1251 return treeCtrl->AppendItem (parent, text, image, selectedImage, data);
1252 }
1253 else
1254 {
1255 return wxTreeItemId();
1256 }
1257}
1258
1259
51a58d8b
JS
1260//-----------------------------------------------------------------------------
1261// wxDirFilterListCtrl
1262//-----------------------------------------------------------------------------
1263
1264IMPLEMENT_CLASS(wxDirFilterListCtrl, wxChoice)
1265
1266BEGIN_EVENT_TABLE(wxDirFilterListCtrl, wxChoice)
ca65c044 1267 EVT_CHOICE(wxID_ANY, wxDirFilterListCtrl::OnSelFilter)
51a58d8b
JS
1268END_EVENT_TABLE()
1269
1270bool wxDirFilterListCtrl::Create(wxGenericDirCtrl* parent, const wxWindowID id,
1271 const wxPoint& pos,
1272 const wxSize& size,
1273 long style)
1274{
1275 m_dirCtrl = parent;
1276 return wxChoice::Create(parent, id, pos, size, 0, NULL, style);
1277}
1278
1279void wxDirFilterListCtrl::Init()
1280{
1281 m_dirCtrl = NULL;
1282}
1283
33ac7e6f 1284void wxDirFilterListCtrl::OnSelFilter(wxCommandEvent& WXUNUSED(event))
51a58d8b
JS
1285{
1286 int sel = GetSelection();
1287
1288 wxString currentPath = m_dirCtrl->GetPath();
ec1b28a3 1289
51a58d8b
JS
1290 m_dirCtrl->SetFilterIndex(sel);
1291
1292 // If the filter has changed, the view is out of date, so
1293 // collapse the tree.
08887820 1294 m_dirCtrl->ReCreateTree();
51a58d8b
JS
1295
1296 // Try to restore the selection, or at least the directory
1297 m_dirCtrl->ExpandPath(currentPath);
1298}
1299
1300void wxDirFilterListCtrl::FillFilterList(const wxString& filter, int defaultFilter)
1301{
1302 Clear();
1303 wxArrayString descriptions, filters;
866918a8 1304 size_t n = (size_t) wxParseCommonDialogsFilter(filter, descriptions, filters);
51a58d8b
JS
1305
1306 if (n > 0 && defaultFilter < (int) n)
1307 {
999836aa 1308 for (size_t i = 0; i < n; i++)
51a58d8b
JS
1309 Append(descriptions[i]);
1310 SetSelection(defaultFilter);
1311 }
1312}
13de0c8c 1313#endif // wxUSE_DIRDLG
51a58d8b 1314
13de0c8c 1315#if wxUSE_DIRDLG || wxUSE_FILEDLG
22328fa4 1316
06cc1fb9
JS
1317// ----------------------------------------------------------------------------
1318// wxFileIconsTable icons
1319// ----------------------------------------------------------------------------
1320
d5f330f9 1321#ifndef __WXGTK24__
13b22a67 1322/* Computer (c) Julian Smart */
06cc1fb9 1323static const char * file_icons_tbl_computer_xpm[] = {
13b22a67
JS
1324/* columns rows colors chars-per-pixel */
1325"16 16 42 1",
1326"r c #4E7FD0",
1327"$ c #7198D9",
1328"; c #DCE6F6",
1329"q c #FFFFFF",
1330"u c #4A7CCE",
1331"# c #779DDB",
1332"w c #95B2E3",
1333"y c #7FA2DD",
1334"f c #3263B4",
1335"= c #EAF0FA",
1336"< c #B1C7EB",
1337"% c #6992D7",
1338"9 c #D9E4F5",
1339"o c #9BB7E5",
1340"6 c #F7F9FD",
1341", c #BED0EE",
1342"3 c #F0F5FC",
1343"1 c #A8C0E8",
1344" c None",
1345"0 c #FDFEFF",
1346"4 c #C4D5F0",
1347"@ c #81A4DD",
1348"e c #4377CD",
1349"- c #E2EAF8",
1350"i c #9FB9E5",
1351"> c #CCDAF2",
1352"+ c #89A9DF",
1353"s c #5584D1",
1354"t c #5D89D3",
1355": c #D2DFF4",
1356"5 c #FAFCFE",
1357"2 c #F5F8FD",
1358"8 c #DFE8F7",
1359"& c #5E8AD4",
1360"X c #638ED5",
1361"a c #CEDCF2",
1362"p c #90AFE2",
1363"d c #2F5DA9",
1364"* c #5282D0",
1365"7 c #E5EDF9",
1366". c #A2BCE6",
1367"O c #8CACE0",
1368/* pixels */
1369" ",
1370" .XXXXXXXXXXX ",
1371" oXO++@#$%&*X ",
1372" oX=-;:>,<1%X ",
1373" oX23=-;:4,$X ",
1374" oX5633789:@X ",
1375" oX05623=78+X ",
1376" oXqq05623=OX ",
1377" oX,,,,,<<<$X ",
1378" wXXXXXXXXXXe ",
1379" XrtX%$$y@+O,, ",
1380" uyiiiiiiiii@< ",
1381" ouiiiiiiiiiip<a",
1382" rustX%$$y@+Ow,,",
1383" dfffffffffffffd",
1384" "
1385};
d5f330f9 1386#endif // GTK+ < 2.4
06cc1fb9 1387
06cc1fb9
JS
1388// ----------------------------------------------------------------------------
1389// wxFileIconsTable & friends
1390// ----------------------------------------------------------------------------
1391
1392// global instance of a wxFileIconsTable
1393wxFileIconsTable* wxTheFileIconsTable = (wxFileIconsTable *)NULL;
1394
1395// A module to allow icons table cleanup
1396
1397class wxFileIconsTableModule: public wxModule
1398{
1399DECLARE_DYNAMIC_CLASS(wxFileIconsTableModule)
1400public:
1401 wxFileIconsTableModule() {}
ca65c044 1402 bool OnInit() { wxTheFileIconsTable = new wxFileIconsTable; return true; }
06cc1fb9
JS
1403 void OnExit()
1404 {
1405 if (wxTheFileIconsTable)
1406 {
1407 delete wxTheFileIconsTable;
1408 wxTheFileIconsTable = NULL;
1409 }
1410 }
1411};
1412
1413IMPLEMENT_DYNAMIC_CLASS(wxFileIconsTableModule, wxModule)
1414
1415class wxFileIconEntry : public wxObject
1416{
1417public:
1418 wxFileIconEntry(int i) { id = i; }
1419
1420 int id;
1421};
1422
1423wxFileIconsTable::wxFileIconsTable()
1424{
1425 m_HashTable = NULL;
1426 m_smallImageList = NULL;
1427}
1428
1429wxFileIconsTable::~wxFileIconsTable()
1430{
222ed1d6
MB
1431 if (m_HashTable)
1432 {
1433 WX_CLEAR_HASH_TABLE(*m_HashTable);
1434 delete m_HashTable;
1435 }
06cc1fb9
JS
1436 if (m_smallImageList) delete m_smallImageList;
1437}
1438
1439// delayed initialization - wait until first use (wxArtProv not created yet)
1440void wxFileIconsTable::Create()
1441{
1442 wxCHECK_RET(!m_smallImageList && !m_HashTable, wxT("creating icons twice"));
06cc1fb9
JS
1443 m_HashTable = new wxHashTable(wxKEY_STRING);
1444 m_smallImageList = new wxImageList(16, 16);
1445
06cc1fb9 1446 // folder:
4b92a33f
VS
1447 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_FOLDER,
1448 wxART_CMN_DIALOG,
1449 wxSize(16, 16)));
06cc1fb9 1450 // folder_open
82c1f2a3
RR
1451 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_FOLDER_OPEN,
1452 wxART_CMN_DIALOG,
1453 wxSize(16, 16)));
06cc1fb9 1454 // computer
82c1f2a3
RR
1455#ifdef __WXGTK24__
1456 // GTK24 uses this icon in the file open dialog
1457 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_HARDDISK,
1458 wxART_CMN_DIALOG,
1459 wxSize(16, 16)));
1460#else
06cc1fb9 1461 m_smallImageList->Add(wxIcon(file_icons_tbl_computer_xpm));
82c1f2a3 1462#endif
06cc1fb9 1463 // drive
82c1f2a3
RR
1464 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_HARDDISK,
1465 wxART_CMN_DIALOG,
1466 wxSize(16, 16)));
06cc1fb9 1467 // cdrom
82c1f2a3
RR
1468 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_CDROM,
1469 wxART_CMN_DIALOG,
1470 wxSize(16, 16)));
06cc1fb9 1471 // floppy
82c1f2a3
RR
1472 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_FLOPPY,
1473 wxART_CMN_DIALOG,
1474 wxSize(16, 16)));
06cc1fb9 1475 // removeable
82c1f2a3
RR
1476 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_REMOVABLE,
1477 wxART_CMN_DIALOG,
1478 wxSize(16, 16)));
06cc1fb9 1479 // file
4b92a33f
VS
1480 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_NORMAL_FILE,
1481 wxART_CMN_DIALOG,
1482 wxSize(16, 16)));
06cc1fb9
JS
1483 // executable
1484 if (GetIconID(wxEmptyString, _T("application/x-executable")) == file)
1485 {
4b92a33f
VS
1486 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_EXECUTABLE_FILE,
1487 wxART_CMN_DIALOG,
1488 wxSize(16, 16)));
222ed1d6 1489 delete m_HashTable->Get(_T("exe"));
06cc1fb9
JS
1490 m_HashTable->Delete(_T("exe"));
1491 m_HashTable->Put(_T("exe"), new wxFileIconEntry(executable));
1492 }
1493 /* else put into list by GetIconID
1494 (KDE defines application/x-executable for *.exe and has nice icon)
1495 */
1496}
1497
1498wxImageList *wxFileIconsTable::GetSmallImageList()
1499{
1500 if (!m_smallImageList)
1501 Create();
1502
1503 return m_smallImageList;
1504}
1505
64c288fa 1506#if wxUSE_MIMETYPE && wxUSE_IMAGE && (!defined(__WXMSW__) || wxUSE_WXDIB)
06cc1fb9
JS
1507// VS: we don't need this function w/o wxMimeTypesManager because we'll only have
1508// one icon and we won't resize it
1509
1510static wxBitmap CreateAntialiasedBitmap(const wxImage& img)
1511{
1d529ef7 1512 const unsigned int size = 16;
ca65c044 1513
1d529ef7 1514 wxImage smallimg (size, size);
06cc1fb9
JS
1515 unsigned char *p1, *p2, *ps;
1516 unsigned char mr = img.GetMaskRed(),
1517 mg = img.GetMaskGreen(),
1518 mb = img.GetMaskBlue();
1519
1520 unsigned x, y;
1521 unsigned sr, sg, sb, smask;
1522
1d529ef7 1523 p1 = img.GetData(), p2 = img.GetData() + 3 * size*2, ps = smallimg.GetData();
06cc1fb9
JS
1524 smallimg.SetMaskColour(mr, mr, mr);
1525
1d529ef7 1526 for (y = 0; y < size; y++)
06cc1fb9 1527 {
1d529ef7 1528 for (x = 0; x < size; x++)
06cc1fb9
JS
1529 {
1530 sr = sg = sb = smask = 0;
1531 if (p1[0] != mr || p1[1] != mg || p1[2] != mb)
1532 sr += p1[0], sg += p1[1], sb += p1[2];
1533 else smask++;
1534 p1 += 3;
1535 if (p1[0] != mr || p1[1] != mg || p1[2] != mb)
1536 sr += p1[0], sg += p1[1], sb += p1[2];
1537 else smask++;
1538 p1 += 3;
1539 if (p2[0] != mr || p2[1] != mg || p2[2] != mb)
1540 sr += p2[0], sg += p2[1], sb += p2[2];
1541 else smask++;
1542 p2 += 3;
1543 if (p2[0] != mr || p2[1] != mg || p2[2] != mb)
1544 sr += p2[0], sg += p2[1], sb += p2[2];
1545 else smask++;
1546 p2 += 3;
1547
1548 if (smask > 2)
1549 ps[0] = ps[1] = ps[2] = mr;
1550 else
8253f2e0
WS
1551 {
1552 ps[0] = (unsigned char)(sr >> 2);
1553 ps[1] = (unsigned char)(sg >> 2);
1554 ps[2] = (unsigned char)(sb >> 2);
1555 }
06cc1fb9
JS
1556 ps += 3;
1557 }
1d529ef7 1558 p1 += size*2 * 3, p2 += size*2 * 3;
06cc1fb9 1559 }
ca65c044 1560
06cc1fb9
JS
1561 return wxBitmap(smallimg);
1562}
1563
b126fe3a
DS
1564// This function is currently not unused anymore
1565#if 0
06cc1fb9
JS
1566// finds empty borders and return non-empty area of image:
1567static wxImage CutEmptyBorders(const wxImage& img)
1568{
1569 unsigned char mr = img.GetMaskRed(),
1570 mg = img.GetMaskGreen(),
1571 mb = img.GetMaskBlue();
1572 unsigned char *dt = img.GetData(), *dttmp;
1573 unsigned w = img.GetWidth(), h = img.GetHeight();
1574
1575 unsigned top, bottom, left, right, i;
1576 bool empt;
1577
1578#define MK_DTTMP(x,y) dttmp = dt + ((x + y * w) * 3)
ca65c044 1579#define NOEMPTY_PIX(empt) if (dttmp[0] != mr || dttmp[1] != mg || dttmp[2] != mb) {empt = false; break;}
06cc1fb9 1580
ca65c044 1581 for (empt = true, top = 0; empt && top < h; top++)
06cc1fb9
JS
1582 {
1583 MK_DTTMP(0, top);
1584 for (i = 0; i < w; i++, dttmp+=3)
1585 NOEMPTY_PIX(empt)
1586 }
ca65c044 1587 for (empt = true, bottom = h-1; empt && bottom > top; bottom--)
06cc1fb9
JS
1588 {
1589 MK_DTTMP(0, bottom);
1590 for (i = 0; i < w; i++, dttmp+=3)
1591 NOEMPTY_PIX(empt)
1592 }
ca65c044 1593 for (empt = true, left = 0; empt && left < w; left++)
06cc1fb9
JS
1594 {
1595 MK_DTTMP(left, 0);
1596 for (i = 0; i < h; i++, dttmp+=3*w)
1597 NOEMPTY_PIX(empt)
1598 }
ca65c044 1599 for (empt = true, right = w-1; empt && right > left; right--)
06cc1fb9
JS
1600 {
1601 MK_DTTMP(right, 0);
1602 for (i = 0; i < h; i++, dttmp+=3*w)
1603 NOEMPTY_PIX(empt)
1604 }
1605 top--, left--, bottom++, right++;
1606
1607 return img.GetSubImage(wxRect(left, top, right - left + 1, bottom - top + 1));
1608}
b126fe3a
DS
1609#endif // #if 0
1610
06cc1fb9
JS
1611#endif // wxUSE_MIMETYPE
1612
1613int wxFileIconsTable::GetIconID(const wxString& extension, const wxString& mime)
1614{
1615 if (!m_smallImageList)
1616 Create();
1617
1618#if wxUSE_MIMETYPE
44d60c0b 1619 if (!extension.empty())
06cc1fb9
JS
1620 {
1621 wxFileIconEntry *entry = (wxFileIconEntry*) m_HashTable->Get(extension);
1622 if (entry) return (entry -> id);
1623 }
1624
44d60c0b 1625 wxFileType *ft = (mime.empty()) ?
06cc1fb9
JS
1626 wxTheMimeTypesManager -> GetFileTypeFromExtension(extension) :
1627 wxTheMimeTypesManager -> GetFileTypeFromMimeType(mime);
55d0aaa3
VZ
1628
1629 wxIconLocation iconLoc;
06cc1fb9 1630 wxIcon ic;
02dd0487 1631
55d0aaa3 1632 {
02dd0487
JS
1633 wxLogNull logNull;
1634 if ( ft && ft->GetIcon(&iconLoc) )
1635 {
8e5c6521 1636 ic = wxIcon( iconLoc );
02dd0487 1637 }
55d0aaa3 1638 }
ca65c044 1639
55d0aaa3
VZ
1640 delete ft;
1641
1642 if ( !ic.Ok() )
06cc1fb9
JS
1643 {
1644 int newid = file;
1645 m_HashTable->Put(extension, new wxFileIconEntry(newid));
1646 return newid;
1647 }
1648
3fc93ebd
JS
1649 wxBitmap bmp;
1650 bmp.CopyFromIcon(ic);
06cc1fb9 1651
b6668c25
JS
1652 if ( !bmp.Ok() )
1653 {
1654 int newid = file;
1655 m_HashTable->Put(extension, new wxFileIconEntry(newid));
1656 return newid;
1657 }
1658
1d529ef7 1659 const unsigned int size = 16;
ca65c044 1660
06cc1fb9 1661 int id = m_smallImageList->GetImageCount();
1aa81b17 1662 if ((bmp.GetWidth() == (int) size) && (bmp.GetHeight() == (int) size))
1d529ef7 1663 {
3fc93ebd 1664 m_smallImageList->Add(bmp);
1d529ef7 1665 }
64c288fa 1666#if wxUSE_IMAGE && (!defined(__WXMSW__) || wxUSE_WXDIB)
06cc1fb9
JS
1667 else
1668 {
3fc93ebd
JS
1669 wxImage img = bmp.ConvertToImage();
1670
1d529ef7
RR
1671 if ((img.GetWidth() != size*2) || (img.GetHeight() != size*2))
1672// m_smallImageList->Add(CreateAntialiasedBitmap(CutEmptyBorders(img).Rescale(size*2, size*2)));
1673 m_smallImageList->Add(CreateAntialiasedBitmap(img.Rescale(size*2, size*2)));
06cc1fb9
JS
1674 else
1675 m_smallImageList->Add(CreateAntialiasedBitmap(img));
1676 }
1904aa72
DS
1677#endif // wxUSE_IMAGE
1678
06cc1fb9
JS
1679 m_HashTable->Put(extension, new wxFileIconEntry(id));
1680 return id;
1681
1682#else // !wxUSE_MIMETYPE
1683
79e162f5 1684 wxUnusedVar(mime);
06cc1fb9
JS
1685 if (extension == wxT("exe"))
1686 return executable;
1687 else
1688 return file;
1689#endif // wxUSE_MIMETYPE/!wxUSE_MIMETYPE
1690}
1691
13de0c8c 1692#endif // wxUSE_DIRDLG || wxUSE_FILEDLG