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