]> git.saurik.com Git - wxWidgets.git/blame - src/generic/dirctrlg.cpp
never return NULL_BRUSH from WM_CTLCOLOR handler, it doesn't do much for most control...
[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 ;
44d60c0b 204
a2b77260
SC
205 while( noErr == err )
206 {
207 HFSUniStr255 volumeName ;
208 FSRef fsRef ;
209 FSVolumeInfo volumeInfo ;
44d60c0b
WS
210 err = FSGetVolumeInfo(0, volumeIndex, NULL, kFSVolInfoFlags , &volumeInfo , &volumeName, &fsRef);
211 if( noErr == err )
212 {
213 wxString path = wxMacFSRefToPath( &fsRef ) ;
a2b77260 214 wxString name = wxMacHFSUniStrToString( &volumeName ) ;
44d60c0b 215
a2b77260 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 224 // todo other removable
44d60c0b 225
06cc1fb9
JS
226 paths.Add(path);
227 names.Add(name);
a2b77260 228 volumeIndex++ ;
44d60c0b 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{
44d60c0b 386 if (m_path.empty())
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 400{
44d60c0b 401 if (m_path.empty())
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;
82c1f2a3
RR
507
508#ifdef __WXGTK20__
509 treeStyle |= wxTR_NO_LINES;
510#endif
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
a78955e3 526 m_treeCtrl = CreateTreeCtrl(this, wxID_TREECTRL,
cdfd8b1e 527 wxPoint(0,0), GetClientSize(), treeStyle);
51a58d8b 528
44d60c0b 529 if (!filter.empty() && (style & wxDIRCTRL_SHOW_FILTERS))
51a58d8b
JS
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
82c1f2a3 556
51a58d8b 557 // Expand and select the default path
09a65e63
VS
558 if (!m_defaultPath.empty())
559 {
51a58d8b 560 ExpandPath(m_defaultPath);
09a65e63
VS
561 }
562#ifdef __UNIX__
563 else
564 {
565 // On Unix, there's only one node under the (hidden) root node. It
566 // represents the / path, so the user would always have to expand it;
567 // let's do it ourselves
82c1f2a3 568 ExpandPath( wxT("/") );
09a65e63
VS
569 }
570#endif
51a58d8b 571
19d8dd12 572 SetBestSize(size);
51a58d8b
JS
573 DoResize();
574
ca65c044 575 return true;
51a58d8b
JS
576}
577
578wxGenericDirCtrl::~wxGenericDirCtrl()
579{
51a58d8b
JS
580}
581
582void wxGenericDirCtrl::Init()
583{
ca65c044 584 m_showHidden = false;
51a58d8b
JS
585 m_currentFilter = 0;
586 m_currentFilterStr = wxEmptyString; // Default: any file
587 m_treeCtrl = NULL;
588 m_filterListCtrl = NULL;
589}
590
a78955e3
VS
591wxTreeCtrl* wxGenericDirCtrl::CreateTreeCtrl(wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long treeStyle)
592{
593 return new wxTreeCtrl(parent, id, pos, size, treeStyle);
594}
595
42dcacf0
RR
596void wxGenericDirCtrl::ShowHidden( bool show )
597{
598 m_showHidden = show;
574c939e 599
3b423cdd 600 wxString path = GetPath();
08887820 601 ReCreateTree();
3b423cdd 602 SetPath(path);
42dcacf0
RR
603}
604
22328fa4
JS
605const wxTreeItemId
606wxGenericDirCtrl::AddSection(const wxString& path, const wxString& name, int imageId)
51a58d8b 607{
ca65c044 608 wxDirItemData *dir_item = new wxDirItemData(path,name,true);
51a58d8b 609
22328fa4 610 wxTreeItemId id = AppendItem( m_rootId, name, imageId, -1, dir_item);
4f5c180e 611
06cc1fb9 612 m_treeCtrl->SetItemHasChildren(id);
22328fa4
JS
613
614 return id;
06cc1fb9 615}
51a58d8b 616
06cc1fb9
JS
617void wxGenericDirCtrl::SetupSections()
618{
619 wxArrayString paths, names;
620 wxArrayInt icons;
2d4e4f80 621
06cc1fb9 622 size_t n, count = wxGetAvailableDrives(paths, names, icons);
4f5c180e 623
82c1f2a3
RR
624#ifdef __WXGTK20__
625 wxString home = wxGetHomeDir();
626 AddSection( home, _("Home directory"), 1);
627 home += wxT("/Desktop");
628 AddSection( home, _("Desktop"), 1);
629#endif
630
06cc1fb9 631 for (n = 0; n < count; n++)
06cc1fb9 632 AddSection(paths[n], names[n], icons[n]);
51a58d8b
JS
633}
634
635void wxGenericDirCtrl::OnBeginEditItem(wxTreeEvent &event)
636{
637 // don't rename the main entry "Sections"
638 if (event.GetItem() == m_rootId)
639 {
640 event.Veto();
641 return;
642 }
643
644 // don't rename the individual sections
99006e44 645 if (m_treeCtrl->GetItemParent( event.GetItem() ) == m_rootId)
51a58d8b
JS
646 {
647 event.Veto();
648 return;
649 }
650}
651
652void wxGenericDirCtrl::OnEndEditItem(wxTreeEvent &event)
653{
44d60c0b 654 if ((event.GetLabel().empty()) ||
51a58d8b
JS
655 (event.GetLabel() == _(".")) ||
656 (event.GetLabel() == _("..")) ||
06cc1fb9
JS
657 (event.GetLabel().Find(wxT('/')) != wxNOT_FOUND) ||
658 (event.GetLabel().Find(wxT('\\')) != wxNOT_FOUND) ||
659 (event.GetLabel().Find(wxT('|')) != wxNOT_FOUND))
51a58d8b
JS
660 {
661 wxMessageDialog dialog(this, _("Illegal directory name."), _("Error"), wxOK | wxICON_ERROR );
662 dialog.ShowModal();
663 event.Veto();
664 return;
665 }
666
667 wxTreeItemId id = event.GetItem();
748fcded 668 wxDirItemData *data = (wxDirItemData*)m_treeCtrl->GetItemData( id );
51a58d8b
JS
669 wxASSERT( data );
670
671 wxString new_name( wxPathOnly( data->m_path ) );
ad9cd15c 672 new_name += wxString(wxFILE_SEP_PATH);
51a58d8b
JS
673 new_name += event.GetLabel();
674
675 wxLogNull log;
676
677 if (wxFileExists(new_name))
678 {
679 wxMessageDialog dialog(this, _("File name exists already."), _("Error"), wxOK | wxICON_ERROR );
680 dialog.ShowModal();
681 event.Veto();
682 }
683
684 if (wxRenameFile(data->m_path,new_name))
685 {
686 data->SetNewDirName( new_name );
687 }
688 else
689 {
690 wxMessageDialog dialog(this, _("Operation not permitted."), _("Error"), wxOK | wxICON_ERROR );
691 dialog.ShowModal();
692 event.Veto();
693 }
694}
695
696void wxGenericDirCtrl::OnExpandItem(wxTreeEvent &event)
697{
698 wxTreeItemId parentId = event.GetItem();
699
748fcded
VS
700 // VS: this is needed because the event handler is called from wxTreeCtrl
701 // ctor when wxTR_HIDE_ROOT was specified
4ded51f2
CE
702
703 if (!m_rootId.IsOk())
704
748fcded
VS
705 m_rootId = m_treeCtrl->GetRootItem();
706
51a58d8b
JS
707 ExpandDir(parentId);
708}
709
710void wxGenericDirCtrl::OnCollapseItem(wxTreeEvent &event )
711{
08887820
VS
712 CollapseDir(event.GetItem());
713}
714
715void wxGenericDirCtrl::CollapseDir(wxTreeItemId parentId)
716{
717 wxTreeItemId child;
51a58d8b 718
08887820 719 wxDirItemData *data = (wxDirItemData *) m_treeCtrl->GetItemData(parentId);
51a58d8b
JS
720 if (!data->m_isExpanded)
721 return;
722
ca65c044 723 data->m_isExpanded = false;
2d75caaa 724 wxTreeItemIdValue cookie;
51a58d8b
JS
725 /* Workaround because DeleteChildren has disapeared (why?) and
726 * CollapseAndReset doesn't work as advertised (deletes parent too) */
08887820 727 child = m_treeCtrl->GetFirstChild(parentId, cookie);
51a58d8b
JS
728 while (child.IsOk())
729 {
730 m_treeCtrl->Delete(child);
731 /* Not GetNextChild below, because the cookie mechanism can't
732 * handle disappearing children! */
08887820 733 child = m_treeCtrl->GetFirstChild(parentId, cookie);
51a58d8b
JS
734 }
735}
736
737void wxGenericDirCtrl::ExpandDir(wxTreeItemId parentId)
738{
748fcded 739 wxDirItemData *data = (wxDirItemData *) m_treeCtrl->GetItemData(parentId);
51a58d8b
JS
740
741 if (data->m_isExpanded)
742 return;
743
ca65c044 744 data->m_isExpanded = true;
51a58d8b 745
748fcded 746 if (parentId == m_treeCtrl->GetRootItem())
51a58d8b
JS
747 {
748 SetupSections();
749 return;
750 }
751
752 wxASSERT(data);
753
754 wxString search,path,filename;
755
756 wxString dirName(data->m_path);
757
0d853c54 758#if defined(__WINDOWS__) || defined(__DOS__) || defined(__OS2__)
51a58d8b
JS
759 // Check if this is a root directory and if so,
760 // whether the drive is avaiable.
33fed835
MB
761 if (!wxIsDriveAvailable(dirName))
762 {
ca65c044 763 data->m_isExpanded = false;
7328394a
JS
764 //wxMessageBox(wxT("Sorry, this drive is not available."));
765 return;
33fed835 766 }
51a58d8b
JS
767#endif
768
769 // This may take a longish time. Go to busy cursor
770 wxBusyCursor busy;
771
0d853c54 772#if defined(__WINDOWS__) || defined(__DOS__) || defined(__OS2__)
51a58d8b
JS
773 if (dirName.Last() == ':')
774 dirName += wxString(wxFILE_SEP_PATH);
775#endif
776
777 wxArrayString dirs;
778 wxArrayString filenames;
779
780 wxDir d;
781 wxString eachFilename;
782
f9c165b1 783 wxLogNull log;
51a58d8b
JS
784 d.Open(dirName);
785
786 if (d.IsOpened())
787 {
42dcacf0
RR
788 int style = wxDIR_DIRS;
789 if (m_showHidden) style |= wxDIR_HIDDEN;
790 if (d.GetFirst(& eachFilename, wxEmptyString, style))
51a58d8b
JS
791 {
792 do
793 {
794 if ((eachFilename != wxT(".")) && (eachFilename != wxT("..")))
795 {
796 dirs.Add(eachFilename);
797 }
798 }
2b0a7c09 799 while (d.GetNext(&eachFilename));
51a58d8b
JS
800 }
801 }
222ed1d6 802 dirs.Sort(wxDirCtrlStringCompareFunction);
51a58d8b
JS
803
804 // Now do the filenames -- but only if we're allowed to
805 if ((GetWindowStyle() & wxDIRCTRL_DIR_ONLY) == 0)
806 {
f9c165b1
JS
807 wxLogNull log;
808
51a58d8b 809 d.Open(dirName);
ec1b28a3 810
51a58d8b
JS
811 if (d.IsOpened())
812 {
2b0a7c09
RN
813 int style = wxDIR_FILES;
814 if (m_showHidden) style |= wxDIR_HIDDEN;
3da4e4bd
JS
815 // Process each filter (ex: "JPEG Files (*.jpg;*.jpeg)|*.jpg;*.jpeg")
816 wxStringTokenizer strTok;
817 wxString curFilter;
818 strTok.SetString(m_currentFilterStr,wxT(";"));
819 while(strTok.HasMoreTokens())
51a58d8b 820 {
3da4e4bd
JS
821 curFilter = strTok.GetNextToken();
822 if (d.GetFirst(& eachFilename, m_currentFilterStr, style))
51a58d8b 823 {
3da4e4bd 824 do
51a58d8b 825 {
3da4e4bd
JS
826 if ((eachFilename != wxT(".")) && (eachFilename != wxT("..")))
827 {
828 filenames.Add(eachFilename);
829 }
51a58d8b 830 }
3da4e4bd 831 while (d.GetNext(& eachFilename));
51a58d8b 832 }
51a58d8b
JS
833 }
834 }
222ed1d6 835 filenames.Sort(wxDirCtrlStringCompareFunction);
51a58d8b
JS
836 }
837
838 // Add the sorted dirs
839 size_t i;
840 for (i = 0; i < dirs.Count(); i++)
841 {
842 wxString eachFilename(dirs[i]);
843 path = dirName;
083f7497 844 if (!wxEndsWithPathSeparator(path))
51a58d8b
JS
845 path += wxString(wxFILE_SEP_PATH);
846 path += eachFilename;
847
ca65c044 848 wxDirItemData *dir_item = new wxDirItemData(path,eachFilename,true);
22328fa4 849 wxTreeItemId id = AppendItem( parentId, eachFilename,
06cc1fb9
JS
850 wxFileIconsTable::folder, -1, dir_item);
851 m_treeCtrl->SetItemImage( id, wxFileIconsTable::folder_open,
852 wxTreeItemIcon_Expanded );
ec1b28a3 853
51a58d8b 854 // Has this got any children? If so, make it expandable.
748fcded
VS
855 // (There are two situations when a dir has children: either it
856 // has subdirectories or it contains files that weren't filtered
857 // out. The latter only applies to dirctrl with files.)
858 if ( dir_item->HasSubDirs() ||
859 (((GetWindowStyle() & wxDIRCTRL_DIR_ONLY) == 0) &&
860 dir_item->HasFiles(m_currentFilterStr)) )
51a58d8b 861 {
748fcded 862 m_treeCtrl->SetItemHasChildren(id);
51a58d8b 863 }
51a58d8b
JS
864 }
865
866 // Add the sorted filenames
867 if ((GetWindowStyle() & wxDIRCTRL_DIR_ONLY) == 0)
868 {
869 for (i = 0; i < filenames.Count(); i++)
870 {
871 wxString eachFilename(filenames[i]);
872 path = dirName;
083f7497 873 if (!wxEndsWithPathSeparator(path))
51a58d8b
JS
874 path += wxString(wxFILE_SEP_PATH);
875 path += eachFilename;
876 //path = dirName + wxString(wxT("/")) + eachFilename;
ca65c044 877 wxDirItemData *dir_item = new wxDirItemData(path,eachFilename,false);
06cc1fb9
JS
878 int image_id = wxFileIconsTable::file;
879 if (eachFilename.Find(wxT('.')) != wxNOT_FOUND)
880 image_id = wxTheFileIconsTable->GetIconID(eachFilename.AfterLast(wxT('.')));
22328fa4 881 (void) AppendItem( parentId, eachFilename, image_id, -1, dir_item);
51a58d8b
JS
882 }
883 }
884}
885
08887820
VS
886void wxGenericDirCtrl::ReCreateTree()
887{
888 CollapseDir(m_treeCtrl->GetRootItem());
889 ExpandDir(m_treeCtrl->GetRootItem());
890}
891
51a58d8b
JS
892// Find the child that matches the first part of 'path'.
893// E.g. if a child path is "/usr" and 'path' is "/usr/include"
894// then the child for /usr is returned.
895wxTreeItemId wxGenericDirCtrl::FindChild(wxTreeItemId parentId, const wxString& path, bool& done)
896{
897 wxString path2(path);
ec1b28a3 898
51a58d8b
JS
899 // Make sure all separators are as per the current platform
900 path2.Replace(wxT("\\"), wxString(wxFILE_SEP_PATH));
901 path2.Replace(wxT("/"), wxString(wxFILE_SEP_PATH));
ec1b28a3 902
51a58d8b
JS
903 // Append a separator to foil bogus substring matching
904 path2 += wxString(wxFILE_SEP_PATH);
ec1b28a3
DW
905
906 // In MSW or PM, case is not significant
0d853c54 907#if defined(__WINDOWS__) || defined(__DOS__) || defined(__OS2__)
51a58d8b
JS
908 path2.MakeLower();
909#endif
ec1b28a3 910
2d75caaa 911 wxTreeItemIdValue cookie;
51a58d8b 912 wxTreeItemId childId = m_treeCtrl->GetFirstChild(parentId, cookie);
53ccf1c0 913 while (childId.IsOk())
51a58d8b 914 {
748fcded 915 wxDirItemData* data = (wxDirItemData*) m_treeCtrl->GetItemData(childId);
ec1b28a3 916
44d60c0b 917 if (data && !data->m_path.empty())
51a58d8b
JS
918 {
919 wxString childPath(data->m_path);
083f7497 920 if (!wxEndsWithPathSeparator(childPath))
51a58d8b 921 childPath += wxString(wxFILE_SEP_PATH);
ec1b28a3
DW
922
923 // In MSW and PM, case is not significant
0d853c54 924#if defined(__WINDOWS__) || defined(__DOS__) || defined(__OS2__)
51a58d8b
JS
925 childPath.MakeLower();
926#endif
ec1b28a3 927
51a58d8b
JS
928 if (childPath.Len() <= path2.Len())
929 {
930 wxString path3 = path2.Mid(0, childPath.Len());
931 if (childPath == path3)
932 {
933 if (path3.Len() == path2.Len())
ca65c044 934 done = true;
51a58d8b 935 else
ca65c044 936 done = false;
51a58d8b
JS
937 return childId;
938 }
939 }
940 }
ec1b28a3 941
748fcded 942 childId = m_treeCtrl->GetNextChild(parentId, cookie);
51a58d8b 943 }
3fa4bd0e
VS
944 wxTreeItemId invalid;
945 return invalid;
51a58d8b
JS
946}
947
948// Try to expand as much of the given path as possible,
949// and select the given tree item.
950bool wxGenericDirCtrl::ExpandPath(const wxString& path)
951{
ca65c044 952 bool done = false;
51a58d8b
JS
953 wxTreeItemId id = FindChild(m_rootId, path, done);
954 wxTreeItemId lastId = id; // The last non-zero id
237387ad 955 while (id.IsOk() && !done)
51a58d8b
JS
956 {
957 ExpandDir(id);
958
959 id = FindChild(id, path, done);
53ccf1c0 960 if (id.IsOk())
51a58d8b
JS
961 lastId = id;
962 }
53ccf1c0 963 if (lastId.IsOk())
51a58d8b 964 {
748fcded 965 wxDirItemData *data = (wxDirItemData *) m_treeCtrl->GetItemData(lastId);
51a58d8b
JS
966 if (data->m_isDir)
967 {
968 m_treeCtrl->Expand(lastId);
969 }
970 if ((GetWindowStyle() & wxDIRCTRL_SELECT_FIRST) && data->m_isDir)
971 {
972 // Find the first file in this directory
2d75caaa 973 wxTreeItemIdValue cookie;
51a58d8b 974 wxTreeItemId childId = m_treeCtrl->GetFirstChild(lastId, cookie);
ca65c044 975 bool selectedChild = false;
53ccf1c0 976 while (childId.IsOk())
51a58d8b 977 {
748fcded 978 wxDirItemData* data = (wxDirItemData*) m_treeCtrl->GetItemData(childId);
ec1b28a3 979
ca65c044 980 if (data && data->m_path != wxEmptyString && !data->m_isDir)
51a58d8b
JS
981 {
982 m_treeCtrl->SelectItem(childId);
983 m_treeCtrl->EnsureVisible(childId);
ca65c044 984 selectedChild = true;
51a58d8b
JS
985 break;
986 }
987 childId = m_treeCtrl->GetNextChild(lastId, cookie);
988 }
989 if (!selectedChild)
990 {
991 m_treeCtrl->SelectItem(lastId);
992 m_treeCtrl->EnsureVisible(lastId);
993 }
994 }
995 else
996 {
997 m_treeCtrl->SelectItem(lastId);
998 m_treeCtrl->EnsureVisible(lastId);
999 }
1000
ca65c044 1001 return true;
51a58d8b
JS
1002 }
1003 else
ca65c044 1004 return false;
51a58d8b
JS
1005}
1006
1007wxString wxGenericDirCtrl::GetPath() const
1008{
1009 wxTreeItemId id = m_treeCtrl->GetSelection();
1010 if (id)
1011 {
748fcded 1012 wxDirItemData* data = (wxDirItemData*) m_treeCtrl->GetItemData(id);
51a58d8b
JS
1013 return data->m_path;
1014 }
1015 else
1016 return wxEmptyString;
1017}
1018
1019wxString wxGenericDirCtrl::GetFilePath() const
1020{
1021 wxTreeItemId id = m_treeCtrl->GetSelection();
1022 if (id)
1023 {
748fcded 1024 wxDirItemData* data = (wxDirItemData*) m_treeCtrl->GetItemData(id);
51a58d8b
JS
1025 if (data->m_isDir)
1026 return wxEmptyString;
1027 else
1028 return data->m_path;
1029 }
1030 else
1031 return wxEmptyString;
1032}
1033
1034void wxGenericDirCtrl::SetPath(const wxString& path)
1035{
1036 m_defaultPath = path;
1037 if (m_rootId)
1038 ExpandPath(path);
1039}
1040
1041// Not used
1042#if 0
1043void wxGenericDirCtrl::FindChildFiles(wxTreeItemId id, int dirFlags, wxArrayString& filenames)
1044{
748fcded 1045 wxDirItemData *data = (wxDirItemData *) m_treeCtrl->GetItemData(id);
51a58d8b
JS
1046
1047 // This may take a longish time. Go to busy cursor
1048 wxBusyCursor busy;
1049
1050 wxASSERT(data);
1051
1052 wxString search,path,filename;
1053
1054 wxString dirName(data->m_path);
1055
0d853c54 1056#if defined(__WXMSW__) || defined(__OS2__)
51a58d8b
JS
1057 if (dirName.Last() == ':')
1058 dirName += wxString(wxFILE_SEP_PATH);
1059#endif
1060
1061 wxDir d;
1062 wxString eachFilename;
1063
f9c165b1 1064 wxLogNull log;
51a58d8b
JS
1065 d.Open(dirName);
1066
1067 if (d.IsOpened())
1068 {
1069 if (d.GetFirst(& eachFilename, m_currentFilterStr, dirFlags))
1070 {
1071 do
1072 {
1073 if ((eachFilename != wxT(".")) && (eachFilename != wxT("..")))
1074 {
1075 filenames.Add(eachFilename);
1076 }
1077 }
1078 while (d.GetNext(& eachFilename)) ;
1079 }
1080 }
1081}
1082#endif
1083
1084void wxGenericDirCtrl::SetFilterIndex(int n)
1085{
1086 m_currentFilter = n;
1087
1088 wxString f, d;
1089 if (ExtractWildcard(m_filter, n, f, d))
1090 m_currentFilterStr = f;
1091 else
1092 m_currentFilterStr = wxT("*.*");
1093}
1094
1095void wxGenericDirCtrl::SetFilter(const wxString& filter)
1096{
1097 m_filter = filter;
1098
1099 wxString f, d;
1100 if (ExtractWildcard(m_filter, m_currentFilter, f, d))
1101 m_currentFilterStr = f;
1102 else
1103 m_currentFilterStr = wxT("*.*");
1104}
1105
1106// Extract description and actual filter from overall filter string
1107bool wxGenericDirCtrl::ExtractWildcard(const wxString& filterStr, int n, wxString& filter, wxString& description)
1108{
1109 wxArrayString filters, descriptions;
daf32463 1110 int count = wxParseCommonDialogsFilter(filterStr, descriptions, filters);
51a58d8b
JS
1111 if (count > 0 && n < count)
1112 {
1113 filter = filters[n];
1114 description = descriptions[n];
ca65c044 1115 return true;
51a58d8b 1116 }
5716a1ab 1117
ca65c044 1118 return false;
51a58d8b
JS
1119}
1120
9e152a55 1121#if WXWIN_COMPATIBILITY_2_4
51a58d8b
JS
1122// Parses the global filter, returning the number of filters.
1123// Returns 0 if none or if there's a problem.
b600ed13 1124// filterStr is in the form: "All files (*.*)|*.*|JPEG Files (*.jpeg)|*.jpg"
51a58d8b
JS
1125int wxGenericDirCtrl::ParseFilter(const wxString& filterStr, wxArrayString& filters, wxArrayString& descriptions)
1126{
daf32463 1127 return wxParseCommonDialogsFilter(filterStr, descriptions, filters );
51a58d8b 1128}
9e152a55 1129#endif // WXWIN_COMPATIBILITY_2_4
51a58d8b
JS
1130
1131void wxGenericDirCtrl::DoResize()
1132{
1133 wxSize sz = GetClientSize();
1134 int verticalSpacing = 3;
1135 if (m_treeCtrl)
1136 {
1137 wxSize filterSz ;
1138 if (m_filterListCtrl)
1139 {
232f35cb
JS
1140#ifdef __WXMSW__
1141 // For some reason, this is required in order for the
1142 // correct control height to always be returned, rather
1143 // than the drop-down list height which is sometimes returned.
1144 wxSize oldSize = m_filterListCtrl->GetSize();
422d0ff0
WS
1145 m_filterListCtrl->SetSize(wxDefaultCoord,
1146 wxDefaultCoord,
ca65c044 1147 oldSize.x+10,
422d0ff0 1148 wxDefaultCoord,
ca65c044 1149 wxSIZE_USE_EXISTING);
422d0ff0
WS
1150 m_filterListCtrl->SetSize(wxDefaultCoord,
1151 wxDefaultCoord,
ca65c044 1152 oldSize.x,
422d0ff0 1153 wxDefaultCoord,
ca65c044 1154 wxSIZE_USE_EXISTING);
232f35cb 1155#endif
51a58d8b
JS
1156 filterSz = m_filterListCtrl->GetSize();
1157 sz.y -= (filterSz.y + verticalSpacing);
1158 }
1159 m_treeCtrl->SetSize(0, 0, sz.x, sz.y);
1160 if (m_filterListCtrl)
1161 {
1162 m_filterListCtrl->SetSize(0, sz.y + verticalSpacing, sz.x, filterSz.y);
1163 // Don't know why, but this needs refreshing after a resize (wxMSW)
1164 m_filterListCtrl->Refresh();
1165 }
1166 }
1167}
1168
1169
33ac7e6f 1170void wxGenericDirCtrl::OnSize(wxSizeEvent& WXUNUSED(event))
51a58d8b
JS
1171{
1172 DoResize();
1173}
1174
22328fa4 1175wxTreeItemId wxGenericDirCtrl::AppendItem (const wxTreeItemId & parent,
ca65c044
WS
1176 const wxString & text,
1177 int image, int selectedImage,
1178 wxTreeItemData * data)
22328fa4
JS
1179{
1180 wxTreeCtrl *treeCtrl = GetTreeCtrl ();
1181
1182 wxASSERT (treeCtrl);
1183
1184 if (treeCtrl)
1185 {
1186 return treeCtrl->AppendItem (parent, text, image, selectedImage, data);
1187 }
1188 else
1189 {
1190 return wxTreeItemId();
1191 }
1192}
1193
1194
51a58d8b
JS
1195//-----------------------------------------------------------------------------
1196// wxDirFilterListCtrl
1197//-----------------------------------------------------------------------------
1198
1199IMPLEMENT_CLASS(wxDirFilterListCtrl, wxChoice)
1200
1201BEGIN_EVENT_TABLE(wxDirFilterListCtrl, wxChoice)
ca65c044 1202 EVT_CHOICE(wxID_ANY, wxDirFilterListCtrl::OnSelFilter)
51a58d8b
JS
1203END_EVENT_TABLE()
1204
1205bool wxDirFilterListCtrl::Create(wxGenericDirCtrl* parent, const wxWindowID id,
1206 const wxPoint& pos,
1207 const wxSize& size,
1208 long style)
1209{
1210 m_dirCtrl = parent;
1211 return wxChoice::Create(parent, id, pos, size, 0, NULL, style);
1212}
1213
1214void wxDirFilterListCtrl::Init()
1215{
1216 m_dirCtrl = NULL;
1217}
1218
33ac7e6f 1219void wxDirFilterListCtrl::OnSelFilter(wxCommandEvent& WXUNUSED(event))
51a58d8b
JS
1220{
1221 int sel = GetSelection();
1222
1223 wxString currentPath = m_dirCtrl->GetPath();
ec1b28a3 1224
51a58d8b
JS
1225 m_dirCtrl->SetFilterIndex(sel);
1226
1227 // If the filter has changed, the view is out of date, so
1228 // collapse the tree.
08887820 1229 m_dirCtrl->ReCreateTree();
51a58d8b
JS
1230
1231 // Try to restore the selection, or at least the directory
1232 m_dirCtrl->ExpandPath(currentPath);
1233}
1234
1235void wxDirFilterListCtrl::FillFilterList(const wxString& filter, int defaultFilter)
1236{
1237 Clear();
1238 wxArrayString descriptions, filters;
866918a8 1239 size_t n = (size_t) wxParseCommonDialogsFilter(filter, descriptions, filters);
51a58d8b
JS
1240
1241 if (n > 0 && defaultFilter < (int) n)
1242 {
999836aa 1243 for (size_t i = 0; i < n; i++)
51a58d8b
JS
1244 Append(descriptions[i]);
1245 SetSelection(defaultFilter);
1246 }
1247}
13de0c8c 1248#endif // wxUSE_DIRDLG
51a58d8b 1249
13de0c8c 1250#if wxUSE_DIRDLG || wxUSE_FILEDLG
22328fa4 1251
06cc1fb9
JS
1252// ----------------------------------------------------------------------------
1253// wxFileIconsTable icons
1254// ----------------------------------------------------------------------------
1255
06cc1fb9
JS
1256/* Computer */
1257static const char * file_icons_tbl_computer_xpm[] = {
1258"16 16 7 1",
1259" s None c None",
1260". c #808080",
1261"X c #c0c0c0",
1262"o c Black",
1263"O c Gray100",
1264"+ c #008080",
1265"@ c Blue",
1266" ........... ",
1267" .XXXXXXXXXX.o",
1268" .OOOOOOOOO..o",
1269" .OoooooooX..o",
1270" .Oo+...@+X..o",
1271" .Oo+XXX.+X..o",
1272" .Oo+....+X..o",
1273" .Oo++++++X..o",
1274" .OXXXXXXXX.oo",
1275" ..........o.o",
1276" ...........Xo",
1277" .XXXXXXXXXX.o",
1278" .o.o.o.o.o...o",
1279" .oXoXoXoXoXo.o ",
1280".XOXXXXXXXXX.o ",
1281"............o "};
1282
06cc1fb9
JS
1283// ----------------------------------------------------------------------------
1284// wxFileIconsTable & friends
1285// ----------------------------------------------------------------------------
1286
1287// global instance of a wxFileIconsTable
1288wxFileIconsTable* wxTheFileIconsTable = (wxFileIconsTable *)NULL;
1289
1290// A module to allow icons table cleanup
1291
1292class wxFileIconsTableModule: public wxModule
1293{
1294DECLARE_DYNAMIC_CLASS(wxFileIconsTableModule)
1295public:
1296 wxFileIconsTableModule() {}
ca65c044 1297 bool OnInit() { wxTheFileIconsTable = new wxFileIconsTable; return true; }
06cc1fb9
JS
1298 void OnExit()
1299 {
1300 if (wxTheFileIconsTable)
1301 {
1302 delete wxTheFileIconsTable;
1303 wxTheFileIconsTable = NULL;
1304 }
1305 }
1306};
1307
1308IMPLEMENT_DYNAMIC_CLASS(wxFileIconsTableModule, wxModule)
1309
1310class wxFileIconEntry : public wxObject
1311{
1312public:
1313 wxFileIconEntry(int i) { id = i; }
1314
1315 int id;
1316};
1317
1318wxFileIconsTable::wxFileIconsTable()
1319{
1320 m_HashTable = NULL;
1321 m_smallImageList = NULL;
1322}
1323
1324wxFileIconsTable::~wxFileIconsTable()
1325{
222ed1d6
MB
1326 if (m_HashTable)
1327 {
1328 WX_CLEAR_HASH_TABLE(*m_HashTable);
1329 delete m_HashTable;
1330 }
06cc1fb9
JS
1331 if (m_smallImageList) delete m_smallImageList;
1332}
1333
1334// delayed initialization - wait until first use (wxArtProv not created yet)
1335void wxFileIconsTable::Create()
1336{
1337 wxCHECK_RET(!m_smallImageList && !m_HashTable, wxT("creating icons twice"));
06cc1fb9
JS
1338 m_HashTable = new wxHashTable(wxKEY_STRING);
1339 m_smallImageList = new wxImageList(16, 16);
1340
06cc1fb9 1341 // folder:
4b92a33f
VS
1342 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_FOLDER,
1343 wxART_CMN_DIALOG,
1344 wxSize(16, 16)));
06cc1fb9 1345 // folder_open
82c1f2a3
RR
1346 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_FOLDER_OPEN,
1347 wxART_CMN_DIALOG,
1348 wxSize(16, 16)));
06cc1fb9 1349 // computer
82c1f2a3
RR
1350#ifdef __WXGTK24__
1351 // GTK24 uses this icon in the file open dialog
1352 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_HARDDISK,
1353 wxART_CMN_DIALOG,
1354 wxSize(16, 16)));
1355#else
06cc1fb9 1356 m_smallImageList->Add(wxIcon(file_icons_tbl_computer_xpm));
82c1f2a3 1357#endif
06cc1fb9 1358 // drive
82c1f2a3
RR
1359 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_HARDDISK,
1360 wxART_CMN_DIALOG,
1361 wxSize(16, 16)));
06cc1fb9 1362 // cdrom
82c1f2a3
RR
1363 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_CDROM,
1364 wxART_CMN_DIALOG,
1365 wxSize(16, 16)));
06cc1fb9 1366 // floppy
82c1f2a3
RR
1367 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_FLOPPY,
1368 wxART_CMN_DIALOG,
1369 wxSize(16, 16)));
06cc1fb9 1370 // removeable
82c1f2a3
RR
1371 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_REMOVABLE,
1372 wxART_CMN_DIALOG,
1373 wxSize(16, 16)));
06cc1fb9 1374 // file
4b92a33f
VS
1375 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_NORMAL_FILE,
1376 wxART_CMN_DIALOG,
1377 wxSize(16, 16)));
06cc1fb9
JS
1378 // executable
1379 if (GetIconID(wxEmptyString, _T("application/x-executable")) == file)
1380 {
4b92a33f
VS
1381 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_EXECUTABLE_FILE,
1382 wxART_CMN_DIALOG,
1383 wxSize(16, 16)));
222ed1d6 1384 delete m_HashTable->Get(_T("exe"));
06cc1fb9
JS
1385 m_HashTable->Delete(_T("exe"));
1386 m_HashTable->Put(_T("exe"), new wxFileIconEntry(executable));
1387 }
1388 /* else put into list by GetIconID
1389 (KDE defines application/x-executable for *.exe and has nice icon)
1390 */
1391}
1392
1393wxImageList *wxFileIconsTable::GetSmallImageList()
1394{
1395 if (!m_smallImageList)
1396 Create();
1397
1398 return m_smallImageList;
1399}
1400
1904aa72 1401#if wxUSE_MIMETYPE && wxUSE_IMAGE
06cc1fb9
JS
1402// VS: we don't need this function w/o wxMimeTypesManager because we'll only have
1403// one icon and we won't resize it
1404
1405static wxBitmap CreateAntialiasedBitmap(const wxImage& img)
1406{
1d529ef7 1407 const unsigned int size = 16;
ca65c044 1408
1d529ef7 1409 wxImage smallimg (size, size);
06cc1fb9
JS
1410 unsigned char *p1, *p2, *ps;
1411 unsigned char mr = img.GetMaskRed(),
1412 mg = img.GetMaskGreen(),
1413 mb = img.GetMaskBlue();
1414
1415 unsigned x, y;
1416 unsigned sr, sg, sb, smask;
1417
1d529ef7 1418 p1 = img.GetData(), p2 = img.GetData() + 3 * size*2, ps = smallimg.GetData();
06cc1fb9
JS
1419 smallimg.SetMaskColour(mr, mr, mr);
1420
1d529ef7 1421 for (y = 0; y < size; y++)
06cc1fb9 1422 {
1d529ef7 1423 for (x = 0; x < size; x++)
06cc1fb9
JS
1424 {
1425 sr = sg = sb = smask = 0;
1426 if (p1[0] != mr || p1[1] != mg || p1[2] != mb)
1427 sr += p1[0], sg += p1[1], sb += p1[2];
1428 else smask++;
1429 p1 += 3;
1430 if (p1[0] != mr || p1[1] != mg || p1[2] != mb)
1431 sr += p1[0], sg += p1[1], sb += p1[2];
1432 else smask++;
1433 p1 += 3;
1434 if (p2[0] != mr || p2[1] != mg || p2[2] != mb)
1435 sr += p2[0], sg += p2[1], sb += p2[2];
1436 else smask++;
1437 p2 += 3;
1438 if (p2[0] != mr || p2[1] != mg || p2[2] != mb)
1439 sr += p2[0], sg += p2[1], sb += p2[2];
1440 else smask++;
1441 p2 += 3;
1442
1443 if (smask > 2)
1444 ps[0] = ps[1] = ps[2] = mr;
1445 else
8253f2e0
WS
1446 {
1447 ps[0] = (unsigned char)(sr >> 2);
1448 ps[1] = (unsigned char)(sg >> 2);
1449 ps[2] = (unsigned char)(sb >> 2);
1450 }
06cc1fb9
JS
1451 ps += 3;
1452 }
1d529ef7 1453 p1 += size*2 * 3, p2 += size*2 * 3;
06cc1fb9 1454 }
ca65c044 1455
06cc1fb9
JS
1456 return wxBitmap(smallimg);
1457}
1458
b126fe3a
DS
1459// This function is currently not unused anymore
1460#if 0
06cc1fb9
JS
1461// finds empty borders and return non-empty area of image:
1462static wxImage CutEmptyBorders(const wxImage& img)
1463{
1464 unsigned char mr = img.GetMaskRed(),
1465 mg = img.GetMaskGreen(),
1466 mb = img.GetMaskBlue();
1467 unsigned char *dt = img.GetData(), *dttmp;
1468 unsigned w = img.GetWidth(), h = img.GetHeight();
1469
1470 unsigned top, bottom, left, right, i;
1471 bool empt;
1472
1473#define MK_DTTMP(x,y) dttmp = dt + ((x + y * w) * 3)
ca65c044 1474#define NOEMPTY_PIX(empt) if (dttmp[0] != mr || dttmp[1] != mg || dttmp[2] != mb) {empt = false; break;}
06cc1fb9 1475
ca65c044 1476 for (empt = true, top = 0; empt && top < h; top++)
06cc1fb9
JS
1477 {
1478 MK_DTTMP(0, top);
1479 for (i = 0; i < w; i++, dttmp+=3)
1480 NOEMPTY_PIX(empt)
1481 }
ca65c044 1482 for (empt = true, bottom = h-1; empt && bottom > top; bottom--)
06cc1fb9
JS
1483 {
1484 MK_DTTMP(0, bottom);
1485 for (i = 0; i < w; i++, dttmp+=3)
1486 NOEMPTY_PIX(empt)
1487 }
ca65c044 1488 for (empt = true, left = 0; empt && left < w; left++)
06cc1fb9
JS
1489 {
1490 MK_DTTMP(left, 0);
1491 for (i = 0; i < h; i++, dttmp+=3*w)
1492 NOEMPTY_PIX(empt)
1493 }
ca65c044 1494 for (empt = true, right = w-1; empt && right > left; right--)
06cc1fb9
JS
1495 {
1496 MK_DTTMP(right, 0);
1497 for (i = 0; i < h; i++, dttmp+=3*w)
1498 NOEMPTY_PIX(empt)
1499 }
1500 top--, left--, bottom++, right++;
1501
1502 return img.GetSubImage(wxRect(left, top, right - left + 1, bottom - top + 1));
1503}
b126fe3a
DS
1504#endif // #if 0
1505
06cc1fb9
JS
1506#endif // wxUSE_MIMETYPE
1507
1508int wxFileIconsTable::GetIconID(const wxString& extension, const wxString& mime)
1509{
1510 if (!m_smallImageList)
1511 Create();
1512
1513#if wxUSE_MIMETYPE
44d60c0b 1514 if (!extension.empty())
06cc1fb9
JS
1515 {
1516 wxFileIconEntry *entry = (wxFileIconEntry*) m_HashTable->Get(extension);
1517 if (entry) return (entry -> id);
1518 }
1519
44d60c0b 1520 wxFileType *ft = (mime.empty()) ?
06cc1fb9
JS
1521 wxTheMimeTypesManager -> GetFileTypeFromExtension(extension) :
1522 wxTheMimeTypesManager -> GetFileTypeFromMimeType(mime);
55d0aaa3
VZ
1523
1524 wxIconLocation iconLoc;
06cc1fb9 1525 wxIcon ic;
02dd0487 1526
55d0aaa3 1527 {
02dd0487
JS
1528 wxLogNull logNull;
1529 if ( ft && ft->GetIcon(&iconLoc) )
1530 {
8e5c6521 1531 ic = wxIcon( iconLoc );
02dd0487 1532 }
55d0aaa3 1533 }
ca65c044 1534
55d0aaa3
VZ
1535 delete ft;
1536
1537 if ( !ic.Ok() )
06cc1fb9
JS
1538 {
1539 int newid = file;
1540 m_HashTable->Put(extension, new wxFileIconEntry(newid));
1541 return newid;
1542 }
1543
3fc93ebd
JS
1544 wxBitmap bmp;
1545 bmp.CopyFromIcon(ic);
06cc1fb9 1546
b6668c25
JS
1547 if ( !bmp.Ok() )
1548 {
1549 int newid = file;
1550 m_HashTable->Put(extension, new wxFileIconEntry(newid));
1551 return newid;
1552 }
1553
1d529ef7 1554 const unsigned int size = 16;
ca65c044 1555
06cc1fb9 1556 int id = m_smallImageList->GetImageCount();
1aa81b17 1557 if ((bmp.GetWidth() == (int) size) && (bmp.GetHeight() == (int) size))
1d529ef7 1558 {
3fc93ebd 1559 m_smallImageList->Add(bmp);
1d529ef7 1560 }
1904aa72 1561#if wxUSE_IMAGE
06cc1fb9
JS
1562 else
1563 {
3fc93ebd
JS
1564 wxImage img = bmp.ConvertToImage();
1565
1d529ef7
RR
1566 if ((img.GetWidth() != size*2) || (img.GetHeight() != size*2))
1567// m_smallImageList->Add(CreateAntialiasedBitmap(CutEmptyBorders(img).Rescale(size*2, size*2)));
1568 m_smallImageList->Add(CreateAntialiasedBitmap(img.Rescale(size*2, size*2)));
06cc1fb9
JS
1569 else
1570 m_smallImageList->Add(CreateAntialiasedBitmap(img));
1571 }
1904aa72
DS
1572#endif // wxUSE_IMAGE
1573
06cc1fb9
JS
1574 m_HashTable->Put(extension, new wxFileIconEntry(id));
1575 return id;
1576
1577#else // !wxUSE_MIMETYPE
1578
79e162f5 1579 wxUnusedVar(mime);
06cc1fb9
JS
1580 if (extension == wxT("exe"))
1581 return executable;
1582 else
1583 return file;
1584#endif // wxUSE_MIMETYPE/!wxUSE_MIMETYPE
1585}
1586
13de0c8c 1587#endif // wxUSE_DIRDLG || wxUSE_FILEDLG