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