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