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