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