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