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