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