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