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