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