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