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