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