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