]> git.saurik.com Git - wxWidgets.git/blame - src/generic/dirctrlg.cpp
Add test for absence of events from wxSpinCtrlDouble ctor.
[wxWidgets.git] / src / generic / dirctrlg.cpp
CommitLineData
51a58d8b 1/////////////////////////////////////////////////////////////////////////////
7fc65a03 2// Name: src/generic/dirctrlg.cpp
51a58d8b
JS
3// Purpose: wxGenericDirCtrl
4// Author: Harm van der Heijden, Robert Roebling, Julian Smart
5// Modified by:
6// Created: 12/12/98
51a58d8b 7// Copyright: (c) Harm van der Heijden, Robert Roebling and Julian Smart
65571936 8// Licence: wxWindows licence
51a58d8b
JS
9/////////////////////////////////////////////////////////////////////////////
10
51a58d8b
JS
11// For compilers that support precompilation, includes "wx.h".
12#include "wx/wxprec.h"
13
14#ifdef __BORLANDC__
32d4c30a 15 #pragma hdrstop
51a58d8b
JS
16#endif
17
13de0c8c 18#if wxUSE_DIRDLG || wxUSE_FILEDLG
51a58d8b 19
88a7a4e1
WS
20#include "wx/generic/dirctrlg.h"
21
32d4c30a
WS
22#ifndef WX_PRECOMP
23 #include "wx/hash.h"
88a7a4e1 24 #include "wx/intl.h"
e4db172a 25 #include "wx/log.h"
de6185e2 26 #include "wx/utils.h"
f1e01716 27 #include "wx/button.h"
923d28da 28 #include "wx/icon.h"
9eddec69 29 #include "wx/settings.h"
246c5004 30 #include "wx/msgdlg.h"
b36e08d0 31 #include "wx/choice.h"
fec9cc08 32 #include "wx/textctrl.h"
ed2fbeb8
WS
33 #include "wx/layout.h"
34 #include "wx/sizer.h"
949c9f74 35 #include "wx/textdlg.h"
dd05139a 36 #include "wx/gdicmn.h"
155ecd4c 37 #include "wx/image.h"
02761f6c 38 #include "wx/module.h"
32d4c30a
WS
39#endif
40
35c2aa4f 41#include "wx/filename.h"
51a58d8b 42#include "wx/filefn.h"
51a58d8b 43#include "wx/imaglist.h"
51a58d8b
JS
44#include "wx/tokenzr.h"
45#include "wx/dir.h"
06cc1fb9 46#include "wx/artprov.h"
06cc1fb9 47#include "wx/mimetype.h"
51a58d8b
JS
48
49#if wxUSE_STATLINE
50 #include "wx/statline.h"
51#endif
52
76a5e5d2 53#if defined(__WXMAC__)
33ddeaba 54 #include "wx/osx/private.h" // includes mac headers
76a5e5d2
SC
55#endif
56
bb5a9514 57#ifdef __WINDOWS__
2736b3ce 58#include <windows.h>
7acf6a92 59#include "wx/msw/winundef.h"
006b7af2 60#include "wx/volume.h"
7328394a 61
c9ecda60
VS
62// FIXME - Mingw32 1.0 has both _getdrive() and _chdrive(). For now, let's assume
63// older releases don't, but it should be verified and the checks modified
64// accordingly.
b4da152e 65#if !defined(__GNUWIN32__) || (defined(__MINGW32_MAJOR_VERSION) && __MINGW32_MAJOR_VERSION >= 1)
1c193821 66#if !defined(__WXWINCE__)
32d4c30a 67 #include <direct.h>
1c193821 68#endif
32d4c30a
WS
69 #include <stdlib.h>
70 #include <ctype.h>
51a58d8b
JS
71#endif
72
bb5a9514 73#endif // __WINDOWS__
7328394a 74
1c53456f 75#if defined(__OS2__) || defined(__DOS__)
ed2fbeb8
WS
76 #ifdef __OS2__
77 #define INCL_BASE
78 #include <os2.h>
79 #ifndef __EMX__
80 #include <direct.h>
81 #endif
82 #include <stdlib.h>
83 #include <ctype.h>
1c53456f 84 #endif
0d853c54 85#endif // __OS2__
ec1b28a3 86
f4ac0693 87#if defined(__WXMAC__)
e1f31102 88// #include "MoreFilesX.h"
bedaf53e
SC
89#endif
90
267a7108 91#ifdef __BORLANDC__
32d4c30a 92 #include "dos.h"
267a7108
JS
93#endif
94
f36e602b 95extern WXDLLEXPORT_DATA(const char) wxFileSelectorDefaultWildcardStr[];
a3e70e1f 96
51a58d8b
JS
97// If compiled under Windows, this macro can cause problems
98#ifdef GetFirstChild
99#undef GetFirstChild
100#endif
101
207ddcc9
VZ
102bool wxIsDriveAvailable(const wxString& dirName);
103
84605707
VZ
104// ----------------------------------------------------------------------------
105// events
106// ----------------------------------------------------------------------------
107
40c7c7f4 108wxDEFINE_EVENT( wxEVT_DIRCTRL_SELECTIONCHANGED, wxTreeEvent );
4d623b67 109wxDEFINE_EVENT( wxEVT_DIRCTRL_FILEACTIVATED, wxTreeEvent );
84605707 110
06cc1fb9 111// ----------------------------------------------------------------------------
0d853c54 112// wxGetAvailableDrives, for WINDOWS, DOS, OS2, MAC, UNIX (returns "/")
06cc1fb9 113// ----------------------------------------------------------------------------
51a58d8b 114
06cc1fb9
JS
115size_t wxGetAvailableDrives(wxArrayString &paths, wxArrayString &names, wxArrayInt &icon_ids)
116{
35c2aa4f 117#ifdef wxHAS_FILESYSTEM_VOLUMES
51a58d8b 118
4676948b
JS
119#ifdef __WXWINCE__
120 // No logical drives; return "\"
121 paths.Add(wxT("\\"));
122 names.Add(wxT("\\"));
1ab36908 123 icon_ids.Add(wxFileIconsTable::computer);
006b7af2
VZ
124#elif defined(__WIN32__) && wxUSE_FSVOLUME
125 // TODO: this code (using wxFSVolumeBase) should be used for all platforms
126 // but unfortunately wxFSVolumeBase is not implemented everywhere
127 const wxArrayString as = wxFSVolumeBase::GetVolumes();
0026e0f2 128
006b7af2
VZ
129 for (size_t i = 0; i < as.GetCount(); i++)
130 {
131 wxString path = as[i];
132 wxFSVolume vol(path);
999836aa 133 int imageId;
006b7af2 134 switch (vol.GetKind())
06cc1fb9 135 {
006b7af2
VZ
136 case wxFS_VOL_FLOPPY:
137 if ( (path == wxT("a:\\")) || (path == wxT("b:\\")) )
06cc1fb9
JS
138 imageId = wxFileIconsTable::floppy;
139 else
140 imageId = wxFileIconsTable::removeable;
141 break;
006b7af2
VZ
142 case wxFS_VOL_DVDROM:
143 case wxFS_VOL_CDROM:
06cc1fb9
JS
144 imageId = wxFileIconsTable::cdrom;
145 break;
006b7af2
VZ
146 case wxFS_VOL_NETWORK:
147 if (path[0] == wxT('\\'))
148 continue; // skip "\\computer\folder"
149 imageId = wxFileIconsTable::drive;
150 break;
151 case wxFS_VOL_DISK:
152 case wxFS_VOL_OTHER:
06cc1fb9
JS
153 default:
154 imageId = wxFileIconsTable::drive;
155 break;
156 }
06cc1fb9 157 paths.Add(path);
006b7af2 158 names.Add(vol.GetDisplayName());
06cc1fb9 159 icon_ids.Add(imageId);
06cc1fb9 160 }
0d853c54
SN
161#elif defined(__OS2__)
162 APIRET rc;
163 ULONG ulDriveNum = 0;
164 ULONG ulDriveMap = 0;
165 rc = ::DosQueryCurrentDisk(&ulDriveNum, &ulDriveMap);
ca65c044
WS
166 if ( rc == 0)
167 {
0d853c54
SN
168 size_t i = 0;
169 while (i < 26)
170 {
ca65c044
WS
171 if (ulDriveMap & ( 1 << i ))
172 {
35c2aa4f
VZ
173 const wxString path = wxFileName::GetVolumeString(
174 'A' + i, wxPATH_GET_SEPARATOR);
175 const wxString name = wxFileName::GetVolumeString(
176 'A' + i, wxPATH_NO_SEPARATOR);
ca65c044 177
be283c77
SN
178 // Note: If _filesys is unsupported by some compilers,
179 // we can always replace it by DosQueryFSAttach
180 char filesysname[20];
7fc65a03
WS
181#ifdef __WATCOMC__
182 ULONG cbBuffer = sizeof(filesysname);
183 PFSQBUFFER2 pfsqBuffer = (PFSQBUFFER2)filesysname;
184 APIRET rc = ::DosQueryFSAttach(name.fn_str(),0,FSAIL_QUERYNAME,pfsqBuffer,&cbBuffer);
185 if (rc != NO_ERROR)
186 {
187 filesysname[0] = '\0';
188 }
189#else
be283c77 190 _filesys(name.fn_str(), filesysname, sizeof(filesysname));
7fc65a03 191#endif
be283c77 192 /* FAT, LAN, HPFS, CDFS, NFS */
ca65c044
WS
193 int imageId;
194 if (path == wxT("A:\\") || path == wxT("B:\\"))
195 imageId = wxFileIconsTable::floppy;
be283c77
SN
196 else if (!strcmp(filesysname, "CDFS"))
197 imageId = wxFileIconsTable::cdrom;
198 else if (!strcmp(filesysname, "LAN") ||
199 !strcmp(filesysname, "NFS"))
200 imageId = wxFileIconsTable::drive;
ca65c044
WS
201 else
202 imageId = wxFileIconsTable::drive;
203 paths.Add(path);
204 names.Add(name);
205 icon_ids.Add(imageId);
206 }
0d853c54 207 i ++;
ca65c044
WS
208 }
209 }
0d853c54 210#else // !__WIN32__, !__OS2__
06cc1fb9 211 /* If we can switch to the drive, it exists. */
35c2aa4f 212 for ( char drive = 'A'; drive <= 'Z'; drive++ )
06cc1fb9 213 {
35c2aa4f
VZ
214 const wxString
215 path = wxFileName::GetVolumeString(drive, wxPATH_GET_SEPARATOR);
51a58d8b 216
06cc1fb9
JS
217 if (wxIsDriveAvailable(path))
218 {
219 paths.Add(path);
35c2aa4f
VZ
220 names.Add(wxFileName::GetVolumeString(drive, wxPATH_NO_SEPARATOR));
221 icon_ids.Add(drive <= 2 ? wxFileIconsTable::floppy
222 : wxFileIconsTable::drive);
06cc1fb9
JS
223 }
224 }
225#endif // __WIN32__/!__WIN32__
226
96f04e1d 227#elif defined(__WXMAC__) && wxOSX_USE_COCOA_OR_CARBON
06cc1fb9 228
a2b77260
SC
229 ItemCount volumeIndex = 1;
230 OSErr err = noErr ;
44d60c0b 231
a2b77260
SC
232 while( noErr == err )
233 {
234 HFSUniStr255 volumeName ;
235 FSRef fsRef ;
236 FSVolumeInfo volumeInfo ;
44d60c0b
WS
237 err = FSGetVolumeInfo(0, volumeIndex, NULL, kFSVolInfoFlags , &volumeInfo , &volumeName, &fsRef);
238 if( noErr == err )
239 {
240 wxString path = wxMacFSRefToPath( &fsRef ) ;
a2b77260 241 wxString name = wxMacHFSUniStrToString( &volumeName ) ;
44d60c0b 242
a2b77260 243 if ( (volumeInfo.flags & kFSVolFlagSoftwareLockedMask) || (volumeInfo.flags & kFSVolFlagHardwareLockedMask) )
06cc1fb9 244 {
a2b77260 245 icon_ids.Add(wxFileIconsTable::cdrom);
06cc1fb9 246 }
a2b77260
SC
247 else
248 {
249 icon_ids.Add(wxFileIconsTable::drive);
06cc1fb9 250 }
a2b77260 251 // todo other removable
44d60c0b 252
06cc1fb9
JS
253 paths.Add(path);
254 names.Add(name);
a2b77260 255 volumeIndex++ ;
44d60c0b 256 }
06cc1fb9 257 }
06cc1fb9 258
bd362275 259#elif defined(__UNIX__)
06cc1fb9
JS
260 paths.Add(wxT("/"));
261 names.Add(wxT("/"));
262 icon_ids.Add(wxFileIconsTable::computer);
263#else
264 #error "Unsupported platform in wxGenericDirCtrl!"
265#endif
e9890600
WS
266 wxASSERT_MSG( (paths.GetCount() == names.GetCount()), wxT("The number of paths and their human readable names should be equal in number."));
267 wxASSERT_MSG( (paths.GetCount() == icon_ids.GetCount()), wxT("Wrong number of icons for available drives."));
06cc1fb9
JS
268 return paths.GetCount();
269}
51a58d8b 270
06cc1fb9
JS
271// ----------------------------------------------------------------------------
272// wxIsDriveAvailable
273// ----------------------------------------------------------------------------
748fcded 274
748fcded 275#if defined(__DOS__)
db5333a5 276
37fd1c97 277bool wxIsDriveAvailable(const wxString& dirName)
db5333a5 278{
0e1f8ea4 279 // FIXME: this method leads to hang up under Watcom for some reason
127eab18
WS
280#ifdef __WATCOMC__
281 wxUnusedVar(dirName);
282#else
b36e08d0 283 if ( dirName.length() == 3 && dirName[1u] == wxT(':') )
db5333a5
VS
284 {
285 wxString dirNameLower(dirName.Lower());
ca65c044 286 // VS: always return true for removable media, since Win95 doesn't
db5333a5
VS
287 // like it when MS-DOS app accesses empty floppy drive
288 return (dirNameLower[0u] == wxT('a') ||
289 dirNameLower[0u] == wxT('b') ||
da865fdd 290 wxDirExists(dirNameLower));
db5333a5
VS
291 }
292 else
6157794c 293#endif
ca65c044 294 return true;
db5333a5
VS
295}
296
0d853c54 297#elif defined(__WINDOWS__) || defined(__OS2__)
db5333a5 298
0c0d1521 299int setdrive(int WXUNUSED_IN_WINCE(drive))
33ac7e6f 300{
4676948b
JS
301#ifdef __WXWINCE__
302 return 0;
303#elif defined(__GNUWIN32__) && \
c9ecda60
VS
304 (defined(__MINGW32_MAJOR_VERSION) && __MINGW32_MAJOR_VERSION >= 1)
305 return _chdrive(drive);
306#else
ca65c044 307 wxChar newdrive[4];
33ac7e6f 308
ca65c044
WS
309 if (drive < 1 || drive > 31)
310 return -1;
311 newdrive[0] = (wxChar)(wxT('A') + drive - 1);
312 newdrive[1] = wxT(':');
0d853c54 313#ifdef __OS2__
ca65c044
WS
314 newdrive[2] = wxT('\\');
315 newdrive[3] = wxT('\0');
0d853c54 316#else
ca65c044 317 newdrive[2] = wxT('\0');
0d853c54 318#endif
bb5a9514 319#if defined(__WINDOWS__)
ca65c044 320 if (::SetCurrentDirectory(newdrive))
50c76ce1
DW
321#else
322 // VA doesn't know what LPSTR is and has its own set
ca65c044 323 if (!DosSetCurrentDir((PSZ)newdrive))
50c76ce1 324#endif
ca65c044
WS
325 return 0;
326 else
327 return -1;
c9ecda60 328#endif // !GNUWIN32
33ac7e6f
KB
329}
330
0c0d1521 331bool wxIsDriveAvailable(const wxString& WXUNUSED_IN_WINCE(dirName))
ad9cd15c 332{
4676948b 333#ifdef __WXWINCE__
ca65c044 334 return false;
4676948b 335#else
ad9cd15c 336#ifdef __WIN32__
33fed835 337 UINT errorMode = SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX);
ad9cd15c 338#endif
ca65c044 339 bool success = true;
ad9cd15c
JS
340
341 // Check if this is a root directory and if so,
2d4e4f80 342 // whether the drive is available.
b36e08d0 343 if (dirName.length() == 3 && dirName[(size_t)1] == wxT(':'))
ad9cd15c 344 {
33fed835 345 wxString dirNameLower(dirName.Lower());
b4da152e 346#if defined(__GNUWIN32__) && !(defined(__MINGW32_MAJOR_VERSION) && __MINGW32_MAJOR_VERSION >= 1)
da865fdd 347 success = wxDirExists(dirNameLower);
7328394a 348#else
0d853c54
SN
349 #if defined(__OS2__)
350 // Avoid changing to drive since no media may be inserted.
351 if (dirNameLower[(size_t)0] == 'a' || dirNameLower[(size_t)0] == 'b')
352 return success;
353 #endif
ad9cd15c 354 int currentDrive = _getdrive();
33fed835 355 int thisDrive = (int) (dirNameLower[(size_t)0] - 'a' + 1) ;
33ac7e6f
KB
356 int err = setdrive( thisDrive ) ;
357 setdrive( currentDrive );
ad9cd15c
JS
358
359 if (err == -1)
360 {
ca65c044 361 success = false;
ad9cd15c 362 }
7328394a 363#endif
ad9cd15c
JS
364 }
365#ifdef __WIN32__
33fed835 366 (void) SetErrorMode(errorMode);
ad9cd15c
JS
367#endif
368
33fed835 369 return success;
4676948b 370#endif
ad9cd15c 371}
0d853c54 372#endif // __WINDOWS__ || __OS2__
ad9cd15c 373
13de0c8c
WS
374#endif // wxUSE_DIRDLG || wxUSE_FILEDLG
375
376
377
378#if wxUSE_DIRDLG
b600ed13 379
57e26a09
JS
380// Function which is called by quick sort. We want to override the default wxArrayString behaviour,
381// and sort regardless of case.
a2a03d78 382static int wxCMPFUNC_CONV wxDirCtrlStringCompareFunction(const wxString& strFirst, const wxString& strSecond)
57e26a09 383{
a2a03d78 384 return strFirst.CmpNoCase(strSecond);
57e26a09
JS
385}
386
51a58d8b 387//-----------------------------------------------------------------------------
748fcded 388// wxDirItemData
51a58d8b
JS
389//-----------------------------------------------------------------------------
390
748fcded
VS
391wxDirItemData::wxDirItemData(const wxString& path, const wxString& name,
392 bool isDir)
51a58d8b
JS
393{
394 m_path = path;
395 m_name = name;
396 /* Insert logic to detect hidden files here
397 * In UnixLand we just check whether the first char is a dot
398 * For FileNameFromPath read LastDirNameInThisPath ;-) */
399 // m_isHidden = (bool)(wxFileNameFromPath(*m_path)[0] == '.');
ca65c044
WS
400 m_isHidden = false;
401 m_isExpanded = false;
51a58d8b
JS
402 m_isDir = isDir;
403}
404
748fcded 405void wxDirItemData::SetNewDirName(const wxString& path)
51a58d8b
JS
406{
407 m_path = path;
748fcded
VS
408 m_name = wxFileNameFromPath(path);
409}
410
411bool wxDirItemData::HasSubDirs() const
412{
44d60c0b 413 if (m_path.empty())
ca65c044 414 return false;
748fcded
VS
415
416 wxDir dir;
417 {
418 wxLogNull nolog;
419 if ( !dir.Open(m_path) )
ca65c044 420 return false;
748fcded
VS
421 }
422
423 return dir.HasSubDirs();
424}
425
574c939e 426bool wxDirItemData::HasFiles(const wxString& WXUNUSED(spec)) const
748fcded 427{
44d60c0b 428 if (m_path.empty())
ca65c044 429 return false;
748fcded
VS
430
431 wxDir dir;
432 {
433 wxLogNull nolog;
434 if ( !dir.Open(m_path) )
ca65c044 435 return false;
748fcded
VS
436 }
437
438 return dir.HasFiles();
51a58d8b
JS
439}
440
51a58d8b
JS
441//-----------------------------------------------------------------------------
442// wxGenericDirCtrl
443//-----------------------------------------------------------------------------
444
51a58d8b 445BEGIN_EVENT_TABLE(wxGenericDirCtrl, wxControl)
6b707fc3
JS
446 EVT_TREE_ITEM_EXPANDING (wxID_TREECTRL, wxGenericDirCtrl::OnExpandItem)
447 EVT_TREE_ITEM_COLLAPSED (wxID_TREECTRL, wxGenericDirCtrl::OnCollapseItem)
448 EVT_TREE_BEGIN_LABEL_EDIT (wxID_TREECTRL, wxGenericDirCtrl::OnBeginEditItem)
449 EVT_TREE_END_LABEL_EDIT (wxID_TREECTRL, wxGenericDirCtrl::OnEndEditItem)
84605707 450 EVT_TREE_SEL_CHANGED (wxID_TREECTRL, wxGenericDirCtrl::OnTreeSelChange)
4d623b67 451 EVT_TREE_ITEM_ACTIVATED (wxID_TREECTRL, wxGenericDirCtrl::OnItemActivated)
51a58d8b
JS
452 EVT_SIZE (wxGenericDirCtrl::OnSize)
453END_EVENT_TABLE()
454
455wxGenericDirCtrl::wxGenericDirCtrl(void)
456{
457 Init();
458}
459
c06dde42
JS
460void wxGenericDirCtrl::ExpandRoot()
461{
462 ExpandDir(m_rootId); // automatically expand first level
463
464 // Expand and select the default path
465 if (!m_defaultPath.empty())
466 {
467 ExpandPath(m_defaultPath);
468 }
469#ifdef __UNIX__
470 else
471 {
472 // On Unix, there's only one node under the (hidden) root node. It
473 // represents the / path, so the user would always have to expand it;
474 // let's do it ourselves
475 ExpandPath( wxT("/") );
476 }
477#endif
478}
479
51a58d8b 480bool wxGenericDirCtrl::Create(wxWindow *parent,
c1ec7ee8 481 const wxWindowID treeid,
748fcded
VS
482 const wxString& dir,
483 const wxPoint& pos,
484 const wxSize& size,
485 long style,
486 const wxString& filter,
487 int defaultFilter,
488 const wxString& name)
51a58d8b 489{
c1ec7ee8 490 if (!wxControl::Create(parent, treeid, pos, size, style, wxDefaultValidator, name))
ca65c044 491 return false;
51a58d8b 492
db5333a5 493 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));
e1957703 494 SetForegroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT));
51a58d8b
JS
495
496 Init();
497
3bd8a405
JS
498 long treeStyle = wxTR_HAS_BUTTONS;
499
500 // On Windows CE, if you hide the root, you get a crash when
501 // attempting to access data for children of the root item.
502#ifndef __WXWINCE__
503 treeStyle |= wxTR_HIDE_ROOT;
504#endif
2997ca30 505
82c1f2a3
RR
506#ifdef __WXGTK20__
507 treeStyle |= wxTR_NO_LINES;
508#endif
fd775aae 509
dabd1377 510 if (style & wxDIRCTRL_EDIT_LABELS)
fd775aae
JS
511 treeStyle |= wxTR_EDIT_LABELS;
512
80f624ec
VZ
513 if (style & wxDIRCTRL_MULTIPLE)
514 treeStyle |= wxTR_MULTIPLE;
515
51a58d8b
JS
516 if ((style & wxDIRCTRL_3D_INTERNAL) == 0)
517 treeStyle |= wxNO_BORDER;
518
a78955e3 519 m_treeCtrl = CreateTreeCtrl(this, wxID_TREECTRL,
c47addef 520 wxPoint(0,0), GetClientSize(), treeStyle);
51a58d8b 521
aa9453d6 522 if (!filter.empty() && (style & wxDIRCTRL_SHOW_FILTERS))
d0bc78e2 523 m_filterListCtrl = new wxDirFilterListCtrl(this, wxID_FILTERLISTCTRL);
51a58d8b
JS
524
525 m_defaultPath = dir;
526 m_filter = filter;
527
dd0138d9 528 if (m_filter.empty())
d0bc78e2 529 m_filter = wxFileSelectorDefaultWildcardStr;
dd0138d9 530
51a58d8b
JS
531 SetFilterIndex(defaultFilter);
532
533 if (m_filterListCtrl)
534 m_filterListCtrl->FillFilterList(filter, defaultFilter);
535
06cc1fb9 536 m_treeCtrl->SetImageList(wxTheFileIconsTable->GetSmallImageList());
51a58d8b 537
ca65c044
WS
538 m_showHidden = false;
539 wxDirItemData* rootData = new wxDirItemData(wxEmptyString, wxEmptyString, true);
51a58d8b
JS
540
541 wxString rootName;
542
0d853c54 543#if defined(__WINDOWS__) || defined(__OS2__) || defined(__DOS__)
51a58d8b
JS
544 rootName = _("Computer");
545#else
546 rootName = _("Sections");
547#endif
548
549 m_rootId = m_treeCtrl->AddRoot( rootName, 3, -1, rootData);
550 m_treeCtrl->SetItemHasChildren(m_rootId);
2997ca30 551
c06dde42 552 ExpandRoot();
51a58d8b 553
4a89f0e9 554 SetInitialSize(size);
51a58d8b
JS
555 DoResize();
556
ca65c044 557 return true;
51a58d8b
JS
558}
559
560wxGenericDirCtrl::~wxGenericDirCtrl()
561{
51a58d8b
JS
562}
563
564void wxGenericDirCtrl::Init()
565{
ca65c044 566 m_showHidden = false;
51a58d8b
JS
567 m_currentFilter = 0;
568 m_currentFilterStr = wxEmptyString; // Default: any file
569 m_treeCtrl = NULL;
570 m_filterListCtrl = NULL;
571}
572
c1ec7ee8 573wxTreeCtrl* wxGenericDirCtrl::CreateTreeCtrl(wxWindow *parent, wxWindowID treeid, const wxPoint& pos, const wxSize& size, long treeStyle)
a78955e3 574{
c1ec7ee8 575 return new wxTreeCtrl(parent, treeid, pos, size, treeStyle);
a78955e3
VS
576}
577
42dcacf0
RR
578void wxGenericDirCtrl::ShowHidden( bool show )
579{
9092371c
VZ
580 if ( m_showHidden == show )
581 return;
582
42dcacf0 583 m_showHidden = show;
574c939e 584
80f624ec
VZ
585 if ( HasFlag(wxDIRCTRL_MULTIPLE) )
586 {
587 wxArrayString paths;
588 GetPaths(paths);
589 ReCreateTree();
590 for ( unsigned n = 0; n < paths.size(); n++ )
591 {
592 ExpandPath(paths[n]);
593 }
594 }
595 else
596 {
597 wxString path = GetPath();
598 ReCreateTree();
599 SetPath(path);
600 }
42dcacf0
RR
601}
602
22328fa4
JS
603const wxTreeItemId
604wxGenericDirCtrl::AddSection(const wxString& path, const wxString& name, int imageId)
51a58d8b 605{
ca65c044 606 wxDirItemData *dir_item = new wxDirItemData(path,name,true);
51a58d8b 607
c1ec7ee8 608 wxTreeItemId treeid = AppendItem( m_rootId, name, imageId, -1, dir_item);
4f5c180e 609
c1ec7ee8 610 m_treeCtrl->SetItemHasChildren(treeid);
22328fa4 611
c1ec7ee8 612 return treeid;
06cc1fb9 613}
51a58d8b 614
06cc1fb9
JS
615void wxGenericDirCtrl::SetupSections()
616{
617 wxArrayString paths, names;
618 wxArrayInt icons;
2d4e4f80 619
06cc1fb9 620 size_t n, count = wxGetAvailableDrives(paths, names, icons);
4f5c180e 621
82c1f2a3
RR
622#ifdef __WXGTK20__
623 wxString home = wxGetHomeDir();
624 AddSection( home, _("Home directory"), 1);
625 home += wxT("/Desktop");
626 AddSection( home, _("Desktop"), 1);
627#endif
628
06cc1fb9 629 for (n = 0; n < count; n++)
06cc1fb9 630 AddSection(paths[n], names[n], icons[n]);
51a58d8b
JS
631}
632
905e0905
VZ
633void wxGenericDirCtrl::SetFocus()
634{
635 // we don't need focus ourselves, give it to the tree so that the user
636 // could navigate it
d095f867
RR
637 if (m_treeCtrl)
638 m_treeCtrl->SetFocus();
905e0905
VZ
639}
640
51a58d8b
JS
641void wxGenericDirCtrl::OnBeginEditItem(wxTreeEvent &event)
642{
643 // don't rename the main entry "Sections"
644 if (event.GetItem() == m_rootId)
645 {
646 event.Veto();
647 return;
648 }
649
650 // don't rename the individual sections
99006e44 651 if (m_treeCtrl->GetItemParent( event.GetItem() ) == m_rootId)
51a58d8b
JS
652 {
653 event.Veto();
654 return;
655 }
656}
657
658void wxGenericDirCtrl::OnEndEditItem(wxTreeEvent &event)
659{
6e86472f
VZ
660 if (event.IsEditCancelled())
661 return;
662
44d60c0b 663 if ((event.GetLabel().empty()) ||
47b8afad
MR
664 (event.GetLabel() == wxT(".")) ||
665 (event.GetLabel() == wxT("..")) ||
06cc1fb9
JS
666 (event.GetLabel().Find(wxT('/')) != wxNOT_FOUND) ||
667 (event.GetLabel().Find(wxT('\\')) != wxNOT_FOUND) ||
668 (event.GetLabel().Find(wxT('|')) != wxNOT_FOUND))
51a58d8b
JS
669 {
670 wxMessageDialog dialog(this, _("Illegal directory name."), _("Error"), wxOK | wxICON_ERROR );
671 dialog.ShowModal();
672 event.Veto();
673 return;
674 }
675
c1ec7ee8 676 wxTreeItemId treeid = event.GetItem();
0d6f66f4 677 wxDirItemData *data = GetItemData( treeid );
51a58d8b
JS
678 wxASSERT( data );
679
680 wxString new_name( wxPathOnly( data->m_path ) );
ad9cd15c 681 new_name += wxString(wxFILE_SEP_PATH);
51a58d8b
JS
682 new_name += event.GetLabel();
683
684 wxLogNull log;
685
686 if (wxFileExists(new_name))
687 {
688 wxMessageDialog dialog(this, _("File name exists already."), _("Error"), wxOK | wxICON_ERROR );
689 dialog.ShowModal();
690 event.Veto();
691 }
692
693 if (wxRenameFile(data->m_path,new_name))
694 {
695 data->SetNewDirName( new_name );
696 }
697 else
698 {
699 wxMessageDialog dialog(this, _("Operation not permitted."), _("Error"), wxOK | wxICON_ERROR );
700 dialog.ShowModal();
701 event.Veto();
702 }
703}
704
84605707
VZ
705void wxGenericDirCtrl::OnTreeSelChange(wxTreeEvent &event)
706{
40c7c7f4 707 wxTreeEvent changedEvent(wxEVT_DIRCTRL_SELECTIONCHANGED, GetId());
84605707 708
9cf565e2 709 changedEvent.SetEventObject(this);
84605707
VZ
710 changedEvent.SetItem(event.GetItem());
711 changedEvent.SetClientObject(m_treeCtrl->GetItemData(event.GetItem()));
712
9cf565e2 713 if (GetEventHandler()->SafelyProcessEvent(changedEvent) && !changedEvent.IsAllowed())
9cf565e2 714 event.Veto();
9cf565e2 715 else
4d623b67
VZ
716 event.Skip();
717}
718
719void wxGenericDirCtrl::OnItemActivated(wxTreeEvent &event)
720{
721 wxTreeItemId treeid = event.GetItem();
722 const wxDirItemData *data = GetItemData(treeid);
723
724 if (data->m_isDir)
9cf565e2 725 {
4d623b67 726 // is dir
9cf565e2
VZ
727 event.Skip();
728 }
4d623b67
VZ
729 else
730 {
731 // is file
732 wxTreeEvent changedEvent(wxEVT_DIRCTRL_FILEACTIVATED, GetId());
733
734 changedEvent.SetEventObject(this);
735 changedEvent.SetItem(treeid);
736 changedEvent.SetClientObject(m_treeCtrl->GetItemData(treeid));
737
738 if (GetEventHandler()->SafelyProcessEvent(changedEvent) && !changedEvent.IsAllowed())
739 event.Veto();
740 else
741 event.Skip();
742 }
84605707
VZ
743}
744
51a58d8b
JS
745void wxGenericDirCtrl::OnExpandItem(wxTreeEvent &event)
746{
747 wxTreeItemId parentId = event.GetItem();
748
748fcded
VS
749 // VS: this is needed because the event handler is called from wxTreeCtrl
750 // ctor when wxTR_HIDE_ROOT was specified
4ded51f2
CE
751
752 if (!m_rootId.IsOk())
748fcded
VS
753 m_rootId = m_treeCtrl->GetRootItem();
754
51a58d8b
JS
755 ExpandDir(parentId);
756}
757
758void wxGenericDirCtrl::OnCollapseItem(wxTreeEvent &event )
759{
08887820
VS
760 CollapseDir(event.GetItem());
761}
762
763void wxGenericDirCtrl::CollapseDir(wxTreeItemId parentId)
764{
765 wxTreeItemId child;
51a58d8b 766
0d6f66f4 767 wxDirItemData *data = GetItemData(parentId);
51a58d8b
JS
768 if (!data->m_isExpanded)
769 return;
770
ca65c044 771 data->m_isExpanded = false;
5b806097
JS
772
773 m_treeCtrl->Freeze();
c06dde42 774 if (parentId != m_treeCtrl->GetRootItem())
5b806097
JS
775 m_treeCtrl->CollapseAndReset(parentId);
776 m_treeCtrl->DeleteChildren(parentId);
777 m_treeCtrl->Thaw();
51a58d8b
JS
778}
779
ef57807f 780void wxGenericDirCtrl::PopulateNode(wxTreeItemId parentId)
51a58d8b 781{
0d6f66f4 782 wxDirItemData *data = GetItemData(parentId);
51a58d8b
JS
783
784 if (data->m_isExpanded)
785 return;
786
ca65c044 787 data->m_isExpanded = true;
51a58d8b 788
748fcded 789 if (parentId == m_treeCtrl->GetRootItem())
51a58d8b
JS
790 {
791 SetupSections();
792 return;
793 }
794
795 wxASSERT(data);
796
797 wxString search,path,filename;
798
799 wxString dirName(data->m_path);
800
3bd8a405 801#if (defined(__WINDOWS__) && !defined(__WXWINCE__)) || defined(__DOS__) || defined(__OS2__)
51a58d8b
JS
802 // Check if this is a root directory and if so,
803 // whether the drive is avaiable.
33fed835
MB
804 if (!wxIsDriveAvailable(dirName))
805 {
ca65c044 806 data->m_isExpanded = false;
7328394a
JS
807 //wxMessageBox(wxT("Sorry, this drive is not available."));
808 return;
33fed835 809 }
51a58d8b
JS
810#endif
811
812 // This may take a longish time. Go to busy cursor
813 wxBusyCursor busy;
814
0d853c54 815#if defined(__WINDOWS__) || defined(__DOS__) || defined(__OS2__)
51a58d8b
JS
816 if (dirName.Last() == ':')
817 dirName += wxString(wxFILE_SEP_PATH);
818#endif
819
820 wxArrayString dirs;
821 wxArrayString filenames;
822
823 wxDir d;
824 wxString eachFilename;
825
f9c165b1 826 wxLogNull log;
51a58d8b
JS
827 d.Open(dirName);
828
829 if (d.IsOpened())
830 {
42dcacf0
RR
831 int style = wxDIR_DIRS;
832 if (m_showHidden) style |= wxDIR_HIDDEN;
833 if (d.GetFirst(& eachFilename, wxEmptyString, style))
51a58d8b
JS
834 {
835 do
836 {
837 if ((eachFilename != wxT(".")) && (eachFilename != wxT("..")))
838 {
839 dirs.Add(eachFilename);
840 }
841 }
2b0a7c09 842 while (d.GetNext(&eachFilename));
51a58d8b
JS
843 }
844 }
222ed1d6 845 dirs.Sort(wxDirCtrlStringCompareFunction);
51a58d8b
JS
846
847 // Now do the filenames -- but only if we're allowed to
d9c98a9a 848 if (!HasFlag(wxDIRCTRL_DIR_ONLY))
51a58d8b
JS
849 {
850 d.Open(dirName);
ec1b28a3 851
51a58d8b
JS
852 if (d.IsOpened())
853 {
2b0a7c09
RN
854 int style = wxDIR_FILES;
855 if (m_showHidden) style |= wxDIR_HIDDEN;
3da4e4bd
JS
856 // Process each filter (ex: "JPEG Files (*.jpg;*.jpeg)|*.jpg;*.jpeg")
857 wxStringTokenizer strTok;
858 wxString curFilter;
859 strTok.SetString(m_currentFilterStr,wxT(";"));
860 while(strTok.HasMoreTokens())
51a58d8b 861 {
3da4e4bd 862 curFilter = strTok.GetNextToken();
f0e5a44d 863 if (d.GetFirst(& eachFilename, curFilter, style))
51a58d8b 864 {
3da4e4bd 865 do
51a58d8b 866 {
3da4e4bd
JS
867 if ((eachFilename != wxT(".")) && (eachFilename != wxT("..")))
868 {
869 filenames.Add(eachFilename);
870 }
51a58d8b 871 }
3da4e4bd 872 while (d.GetNext(& eachFilename));
51a58d8b 873 }
51a58d8b
JS
874 }
875 }
222ed1d6 876 filenames.Sort(wxDirCtrlStringCompareFunction);
51a58d8b
JS
877 }
878
c72fa1cd
VZ
879 // Now we really know whether we have any children so tell the tree control
880 // about it.
881 m_treeCtrl->SetItemHasChildren(parentId, !dirs.empty() || !filenames.empty());
882
51a58d8b
JS
883 // Add the sorted dirs
884 size_t i;
b4a980f4 885 for (i = 0; i < dirs.GetCount(); i++)
51a58d8b 886 {
4e115ed2 887 eachFilename = dirs[i];
51a58d8b 888 path = dirName;
083f7497 889 if (!wxEndsWithPathSeparator(path))
51a58d8b
JS
890 path += wxString(wxFILE_SEP_PATH);
891 path += eachFilename;
892
ca65c044 893 wxDirItemData *dir_item = new wxDirItemData(path,eachFilename,true);
c1ec7ee8 894 wxTreeItemId treeid = AppendItem( parentId, eachFilename,
06cc1fb9 895 wxFileIconsTable::folder, -1, dir_item);
c1ec7ee8 896 m_treeCtrl->SetItemImage( treeid, wxFileIconsTable::folder_open,
06cc1fb9 897 wxTreeItemIcon_Expanded );
ec1b28a3 898
d9c98a9a
VZ
899 // assume that it does have children by default as it can take a long
900 // time to really check for this (think remote drives...)
901 //
c72fa1cd 902 // and if we're wrong, we'll correct the icon later if
d9c98a9a 903 // the user really tries to open this item
c1ec7ee8 904 m_treeCtrl->SetItemHasChildren(treeid);
51a58d8b
JS
905 }
906
907 // Add the sorted filenames
d9c98a9a 908 if (!HasFlag(wxDIRCTRL_DIR_ONLY))
51a58d8b 909 {
b4a980f4 910 for (i = 0; i < filenames.GetCount(); i++)
51a58d8b 911 {
4e115ed2 912 eachFilename = filenames[i];
51a58d8b 913 path = dirName;
083f7497 914 if (!wxEndsWithPathSeparator(path))
51a58d8b
JS
915 path += wxString(wxFILE_SEP_PATH);
916 path += eachFilename;
917 //path = dirName + wxString(wxT("/")) + eachFilename;
ca65c044 918 wxDirItemData *dir_item = new wxDirItemData(path,eachFilename,false);
06cc1fb9
JS
919 int image_id = wxFileIconsTable::file;
920 if (eachFilename.Find(wxT('.')) != wxNOT_FOUND)
921 image_id = wxTheFileIconsTable->GetIconID(eachFilename.AfterLast(wxT('.')));
22328fa4 922 (void) AppendItem( parentId, eachFilename, image_id, -1, dir_item);
51a58d8b
JS
923 }
924 }
925}
926
ef57807f
VZ
927void wxGenericDirCtrl::ExpandDir(wxTreeItemId parentId)
928{
929 // ExpandDir() will not actually expand the tree node, just populate it
930 PopulateNode(parentId);
931}
932
08887820
VS
933void wxGenericDirCtrl::ReCreateTree()
934{
935 CollapseDir(m_treeCtrl->GetRootItem());
c06dde42
JS
936 ExpandRoot();
937}
938
939void wxGenericDirCtrl::CollapseTree()
940{
941 wxTreeItemIdValue cookie;
942 wxTreeItemId child = m_treeCtrl->GetFirstChild(m_rootId, cookie);
943 while (child.IsOk())
944 {
945 CollapseDir(child);
946 child = m_treeCtrl->GetNextChild(m_rootId, cookie);
947 }
08887820
VS
948}
949
51a58d8b
JS
950// Find the child that matches the first part of 'path'.
951// E.g. if a child path is "/usr" and 'path' is "/usr/include"
952// then the child for /usr is returned.
953wxTreeItemId wxGenericDirCtrl::FindChild(wxTreeItemId parentId, const wxString& path, bool& done)
954{
955 wxString path2(path);
ec1b28a3 956
51a58d8b
JS
957 // Make sure all separators are as per the current platform
958 path2.Replace(wxT("\\"), wxString(wxFILE_SEP_PATH));
959 path2.Replace(wxT("/"), wxString(wxFILE_SEP_PATH));
ec1b28a3 960
51a58d8b
JS
961 // Append a separator to foil bogus substring matching
962 path2 += wxString(wxFILE_SEP_PATH);
ec1b28a3
DW
963
964 // In MSW or PM, case is not significant
0d853c54 965#if defined(__WINDOWS__) || defined(__DOS__) || defined(__OS2__)
51a58d8b
JS
966 path2.MakeLower();
967#endif
ec1b28a3 968
2d75caaa 969 wxTreeItemIdValue cookie;
51a58d8b 970 wxTreeItemId childId = m_treeCtrl->GetFirstChild(parentId, cookie);
53ccf1c0 971 while (childId.IsOk())
51a58d8b 972 {
0d6f66f4 973 wxDirItemData* data = GetItemData(childId);
ec1b28a3 974
44d60c0b 975 if (data && !data->m_path.empty())
51a58d8b
JS
976 {
977 wxString childPath(data->m_path);
083f7497 978 if (!wxEndsWithPathSeparator(childPath))
51a58d8b 979 childPath += wxString(wxFILE_SEP_PATH);
ec1b28a3
DW
980
981 // In MSW and PM, case is not significant
0d853c54 982#if defined(__WINDOWS__) || defined(__DOS__) || defined(__OS2__)
51a58d8b
JS
983 childPath.MakeLower();
984#endif
ec1b28a3 985
b36e08d0 986 if (childPath.length() <= path2.length())
51a58d8b 987 {
b36e08d0 988 wxString path3 = path2.Mid(0, childPath.length());
51a58d8b
JS
989 if (childPath == path3)
990 {
b36e08d0 991 if (path3.length() == path2.length())
ca65c044 992 done = true;
51a58d8b 993 else
ca65c044 994 done = false;
51a58d8b
JS
995 return childId;
996 }
997 }
998 }
ec1b28a3 999
748fcded 1000 childId = m_treeCtrl->GetNextChild(parentId, cookie);
51a58d8b 1001 }
3fa4bd0e
VS
1002 wxTreeItemId invalid;
1003 return invalid;
51a58d8b
JS
1004}
1005
1006// Try to expand as much of the given path as possible,
1007// and select the given tree item.
1008bool wxGenericDirCtrl::ExpandPath(const wxString& path)
1009{
ca65c044 1010 bool done = false;
c1ec7ee8
SC
1011 wxTreeItemId treeid = FindChild(m_rootId, path, done);
1012 wxTreeItemId lastId = treeid; // The last non-zero treeid
1013 while (treeid.IsOk() && !done)
51a58d8b 1014 {
c1ec7ee8 1015 ExpandDir(treeid);
51a58d8b 1016
c1ec7ee8
SC
1017 treeid = FindChild(treeid, path, done);
1018 if (treeid.IsOk())
1019 lastId = treeid;
51a58d8b 1020 }
4e115ed2
VZ
1021 if (!lastId.IsOk())
1022 return false;
1023
0d6f66f4 1024 wxDirItemData *data = GetItemData(lastId);
4e115ed2 1025 if (data->m_isDir)
51a58d8b 1026 {
4e115ed2
VZ
1027 m_treeCtrl->Expand(lastId);
1028 }
d9c98a9a 1029 if (HasFlag(wxDIRCTRL_SELECT_FIRST) && data->m_isDir)
4e115ed2
VZ
1030 {
1031 // Find the first file in this directory
1032 wxTreeItemIdValue cookie;
1033 wxTreeItemId childId = m_treeCtrl->GetFirstChild(lastId, cookie);
1034 bool selectedChild = false;
1035 while (childId.IsOk())
51a58d8b 1036 {
0d6f66f4 1037 data = GetItemData(childId);
ec1b28a3 1038
4e115ed2 1039 if (data && data->m_path != wxEmptyString && !data->m_isDir)
51a58d8b 1040 {
4e115ed2
VZ
1041 m_treeCtrl->SelectItem(childId);
1042 m_treeCtrl->EnsureVisible(childId);
1043 selectedChild = true;
1044 break;
51a58d8b 1045 }
4e115ed2 1046 childId = m_treeCtrl->GetNextChild(lastId, cookie);
51a58d8b 1047 }
4e115ed2 1048 if (!selectedChild)
51a58d8b
JS
1049 {
1050 m_treeCtrl->SelectItem(lastId);
1051 m_treeCtrl->EnsureVisible(lastId);
1052 }
51a58d8b
JS
1053 }
1054 else
4e115ed2
VZ
1055 {
1056 m_treeCtrl->SelectItem(lastId);
1057 m_treeCtrl->EnsureVisible(lastId);
1058 }
1059
1060 return true;
51a58d8b
JS
1061}
1062
f395a825
VZ
1063
1064bool wxGenericDirCtrl::CollapsePath(const wxString& path)
1065{
1066 bool done = false;
c1ec7ee8
SC
1067 wxTreeItemId treeid = FindChild(m_rootId, path, done);
1068 wxTreeItemId lastId = treeid; // The last non-zero treeid
f395a825 1069
c1ec7ee8 1070 while ( treeid.IsOk() && !done )
f395a825 1071 {
c1ec7ee8 1072 CollapseDir(treeid);
f395a825 1073
c1ec7ee8 1074 treeid = FindChild(treeid, path, done);
f395a825 1075
c1ec7ee8
SC
1076 if ( treeid.IsOk() )
1077 lastId = treeid;
f395a825
VZ
1078 }
1079
1080 if ( !lastId.IsOk() )
1081 return false;
1082
1083 m_treeCtrl->SelectItem(lastId);
1084 m_treeCtrl->EnsureVisible(lastId);
1085
1086 return true;
1087}
1088
0d6f66f4
VZ
1089wxDirItemData* wxGenericDirCtrl::GetItemData(wxTreeItemId itemId)
1090{
1091 return static_cast<wxDirItemData*>(m_treeCtrl->GetItemData(itemId));
1092}
1093
e3f084fd
VZ
1094wxString wxGenericDirCtrl::GetPath(wxTreeItemId itemId) const
1095{
1096 const wxDirItemData*
1097 data = static_cast<wxDirItemData*>(m_treeCtrl->GetItemData(itemId));
1098
1099 return data->m_path;
1100}
f395a825 1101
51a58d8b
JS
1102wxString wxGenericDirCtrl::GetPath() const
1103{
2dd62dc0
RR
1104 // Allow calling GetPath() in multiple selection from OnSelFilter
1105 if (m_treeCtrl->HasFlag(wxTR_MULTIPLE))
1106 {
1107 wxArrayTreeItemIds items;
1108 m_treeCtrl->GetSelections(items);
1109 if (items.size() > 0)
1110 {
1111 // return first string only
c1ec7ee8 1112 wxTreeItemId treeid = items[0];
e3f084fd 1113 return GetPath(treeid);
2dd62dc0 1114 }
ce00f59b 1115
2dd62dc0
RR
1116 return wxEmptyString;
1117 }
1118
c1ec7ee8
SC
1119 wxTreeItemId treeid = m_treeCtrl->GetSelection();
1120 if (treeid)
51a58d8b 1121 {
e3f084fd 1122 return GetPath(treeid);
51a58d8b
JS
1123 }
1124 else
1125 return wxEmptyString;
1126}
1127
80f624ec
VZ
1128void wxGenericDirCtrl::GetPaths(wxArrayString& paths) const
1129{
1130 paths.clear();
1131
1132 wxArrayTreeItemIds items;
1133 m_treeCtrl->GetSelections(items);
1134 for ( unsigned n = 0; n < items.size(); n++ )
1135 {
c1ec7ee8 1136 wxTreeItemId treeid = items[n];
e3f084fd 1137 paths.push_back(GetPath(treeid));
80f624ec
VZ
1138 }
1139}
1140
51a58d8b
JS
1141wxString wxGenericDirCtrl::GetFilePath() const
1142{
c1ec7ee8
SC
1143 wxTreeItemId treeid = m_treeCtrl->GetSelection();
1144 if (treeid)
51a58d8b 1145 {
c1ec7ee8 1146 wxDirItemData* data = (wxDirItemData*) m_treeCtrl->GetItemData(treeid);
51a58d8b
JS
1147 if (data->m_isDir)
1148 return wxEmptyString;
1149 else
1150 return data->m_path;
1151 }
1152 else
1153 return wxEmptyString;
1154}
1155
80f624ec
VZ
1156void wxGenericDirCtrl::GetFilePaths(wxArrayString& paths) const
1157{
1158 paths.clear();
1159
1160 wxArrayTreeItemIds items;
1161 m_treeCtrl->GetSelections(items);
1162 for ( unsigned n = 0; n < items.size(); n++ )
1163 {
c1ec7ee8
SC
1164 wxTreeItemId treeid = items[n];
1165 wxDirItemData* data = (wxDirItemData*) m_treeCtrl->GetItemData(treeid);
80f624ec
VZ
1166 if ( !data->m_isDir )
1167 paths.Add(data->m_path);
1168 }
1169}
1170
51a58d8b
JS
1171void wxGenericDirCtrl::SetPath(const wxString& path)
1172{
1173 m_defaultPath = path;
1174 if (m_rootId)
1175 ExpandPath(path);
1176}
1177
80f624ec
VZ
1178void wxGenericDirCtrl::SelectPath(const wxString& path, bool select)
1179{
1180 bool done = false;
c1ec7ee8
SC
1181 wxTreeItemId treeid = FindChild(m_rootId, path, done);
1182 wxTreeItemId lastId = treeid; // The last non-zero treeid
1183 while ( treeid.IsOk() && !done )
80f624ec 1184 {
c1ec7ee8
SC
1185 treeid = FindChild(treeid, path, done);
1186 if ( treeid.IsOk() )
1187 lastId = treeid;
80f624ec
VZ
1188 }
1189 if ( !lastId.IsOk() )
1190 return;
1191
1192 if ( done )
1193 {
c1ec7ee8 1194 m_treeCtrl->SelectItem(treeid, select);
80f624ec
VZ
1195 }
1196}
1197
1198void wxGenericDirCtrl::SelectPaths(const wxArrayString& paths)
1199{
1200 if ( HasFlag(wxDIRCTRL_MULTIPLE) )
1201 {
1202 UnselectAll();
1203 for ( unsigned n = 0; n < paths.size(); n++ )
1204 {
1205 SelectPath(paths[n]);
1206 }
1207 }
1208}
1209
1210void wxGenericDirCtrl::UnselectAll()
1211{
1212 m_treeCtrl->UnselectAll();
1213}
1214
51a58d8b
JS
1215// Not used
1216#if 0
c1ec7ee8 1217void wxGenericDirCtrl::FindChildFiles(wxTreeItemId treeid, int dirFlags, wxArrayString& filenames)
51a58d8b 1218{
c1ec7ee8 1219 wxDirItemData *data = (wxDirItemData *) m_treeCtrl->GetItemData(treeid);
51a58d8b
JS
1220
1221 // This may take a longish time. Go to busy cursor
1222 wxBusyCursor busy;
1223
1224 wxASSERT(data);
1225
1226 wxString search,path,filename;
1227
1228 wxString dirName(data->m_path);
1229
bb5a9514 1230#if defined(__WINDOWS__) || defined(__OS2__)
51a58d8b
JS
1231 if (dirName.Last() == ':')
1232 dirName += wxString(wxFILE_SEP_PATH);
1233#endif
1234
1235 wxDir d;
1236 wxString eachFilename;
1237
f9c165b1 1238 wxLogNull log;
51a58d8b
JS
1239 d.Open(dirName);
1240
1241 if (d.IsOpened())
1242 {
1243 if (d.GetFirst(& eachFilename, m_currentFilterStr, dirFlags))
1244 {
1245 do
1246 {
1247 if ((eachFilename != wxT(".")) && (eachFilename != wxT("..")))
1248 {
1249 filenames.Add(eachFilename);
1250 }
1251 }
1252 while (d.GetNext(& eachFilename)) ;
1253 }
1254 }
1255}
1256#endif
1257
1258void wxGenericDirCtrl::SetFilterIndex(int n)
1259{
1260 m_currentFilter = n;
1261
1262 wxString f, d;
1263 if (ExtractWildcard(m_filter, n, f, d))
1264 m_currentFilterStr = f;
1265 else
dd0138d9
RR
1266#ifdef __UNIX__
1267 m_currentFilterStr = wxT("*");
1268#else
51a58d8b 1269 m_currentFilterStr = wxT("*.*");
dd0138d9 1270#endif
51a58d8b
JS
1271}
1272
1273void wxGenericDirCtrl::SetFilter(const wxString& filter)
1274{
1275 m_filter = filter;
1276
aa9453d6 1277 if (!filter.empty() && !m_filterListCtrl && HasFlag(wxDIRCTRL_SHOW_FILTERS))
d0bc78e2
VZ
1278 m_filterListCtrl = new wxDirFilterListCtrl(this, wxID_FILTERLISTCTRL);
1279 else if (filter.empty() && m_filterListCtrl)
1280 {
1281 m_filterListCtrl->Destroy();
1282 m_filterListCtrl = NULL;
1283 }
1284
51a58d8b
JS
1285 wxString f, d;
1286 if (ExtractWildcard(m_filter, m_currentFilter, f, d))
1287 m_currentFilterStr = f;
1288 else
dd0138d9
RR
1289#ifdef __UNIX__
1290 m_currentFilterStr = wxT("*");
1291#else
51a58d8b 1292 m_currentFilterStr = wxT("*.*");
dd0138d9 1293#endif
d0bc78e2
VZ
1294 // current filter index is meaningless after filter change, set it to zero
1295 SetFilterIndex(0);
1296 if (m_filterListCtrl)
1297 m_filterListCtrl->FillFilterList(m_filter, 0);
51a58d8b
JS
1298}
1299
1300// Extract description and actual filter from overall filter string
1301bool wxGenericDirCtrl::ExtractWildcard(const wxString& filterStr, int n, wxString& filter, wxString& description)
1302{
1303 wxArrayString filters, descriptions;
daf32463 1304 int count = wxParseCommonDialogsFilter(filterStr, descriptions, filters);
51a58d8b
JS
1305 if (count > 0 && n < count)
1306 {
1307 filter = filters[n];
1308 description = descriptions[n];
ca65c044 1309 return true;
51a58d8b 1310 }
5716a1ab 1311
ca65c044 1312 return false;
51a58d8b
JS
1313}
1314
51a58d8b
JS
1315
1316void wxGenericDirCtrl::DoResize()
1317{
1318 wxSize sz = GetClientSize();
1319 int verticalSpacing = 3;
1320 if (m_treeCtrl)
1321 {
1322 wxSize filterSz ;
1323 if (m_filterListCtrl)
1324 {
1325 filterSz = m_filterListCtrl->GetSize();
1326 sz.y -= (filterSz.y + verticalSpacing);
1327 }
1328 m_treeCtrl->SetSize(0, 0, sz.x, sz.y);
1329 if (m_filterListCtrl)
1330 {
1331 m_filterListCtrl->SetSize(0, sz.y + verticalSpacing, sz.x, filterSz.y);
1332 // Don't know why, but this needs refreshing after a resize (wxMSW)
1333 m_filterListCtrl->Refresh();
1334 }
1335 }
1336}
1337
1338
33ac7e6f 1339void wxGenericDirCtrl::OnSize(wxSizeEvent& WXUNUSED(event))
51a58d8b
JS
1340{
1341 DoResize();
1342}
1343
22328fa4 1344wxTreeItemId wxGenericDirCtrl::AppendItem (const wxTreeItemId & parent,
ca65c044
WS
1345 const wxString & text,
1346 int image, int selectedImage,
1347 wxTreeItemData * data)
22328fa4
JS
1348{
1349 wxTreeCtrl *treeCtrl = GetTreeCtrl ();
1350
1351 wxASSERT (treeCtrl);
1352
1353 if (treeCtrl)
1354 {
1355 return treeCtrl->AppendItem (parent, text, image, selectedImage, data);
1356 }
1357 else
1358 {
1359 return wxTreeItemId();
1360 }
1361}
1362
1363
51a58d8b
JS
1364//-----------------------------------------------------------------------------
1365// wxDirFilterListCtrl
1366//-----------------------------------------------------------------------------
1367
1368IMPLEMENT_CLASS(wxDirFilterListCtrl, wxChoice)
1369
1370BEGIN_EVENT_TABLE(wxDirFilterListCtrl, wxChoice)
ca65c044 1371 EVT_CHOICE(wxID_ANY, wxDirFilterListCtrl::OnSelFilter)
51a58d8b
JS
1372END_EVENT_TABLE()
1373
d0bc78e2 1374bool wxDirFilterListCtrl::Create(wxGenericDirCtrl* parent,
c1ec7ee8 1375 const wxWindowID treeid,
d0bc78e2
VZ
1376 const wxPoint& pos,
1377 const wxSize& size,
1378 long style)
51a58d8b
JS
1379{
1380 m_dirCtrl = parent;
d0bc78e2
VZ
1381
1382 // by default our border style is determined by the style of our parent
1383 if ( !(style & wxBORDER_MASK) )
1384 {
1385 style |= parent->HasFlag(wxDIRCTRL_3D_INTERNAL) ? wxBORDER_SUNKEN
1386 : wxBORDER_NONE;
1387 }
1388
c1ec7ee8 1389 return wxChoice::Create(parent, treeid, pos, size, 0, NULL, style);
51a58d8b
JS
1390}
1391
1392void wxDirFilterListCtrl::Init()
1393{
1394 m_dirCtrl = NULL;
1395}
1396
33ac7e6f 1397void wxDirFilterListCtrl::OnSelFilter(wxCommandEvent& WXUNUSED(event))
51a58d8b
JS
1398{
1399 int sel = GetSelection();
1400
9e48865c
SL
1401 if (m_dirCtrl->HasFlag(wxDIRCTRL_MULTIPLE))
1402 {
1403 wxArrayString paths;
1404 m_dirCtrl->GetPaths(paths);
1405
1406 m_dirCtrl->SetFilterIndex(sel);
ec1b28a3 1407
9e48865c
SL
1408 // If the filter has changed, the view is out of date, so
1409 // collapse the tree.
1410 m_dirCtrl->ReCreateTree();
1411
1412 // Expand and select the previously selected paths
1413 for (unsigned int i = 0; i < paths.GetCount(); i++)
1414 {
1415 m_dirCtrl->ExpandPath(paths.Item(i));
1416 }
1417 }
1418 else
1419 {
1420 wxString currentPath = m_dirCtrl->GetPath();
51a58d8b 1421
9e48865c
SL
1422 m_dirCtrl->SetFilterIndex(sel);
1423 m_dirCtrl->ReCreateTree();
51a58d8b 1424
9e48865c
SL
1425 // Try to restore the selection, or at least the directory
1426 m_dirCtrl->ExpandPath(currentPath);
1427 }
51a58d8b
JS
1428}
1429
1430void wxDirFilterListCtrl::FillFilterList(const wxString& filter, int defaultFilter)
1431{
1432 Clear();
1433 wxArrayString descriptions, filters;
866918a8 1434 size_t n = (size_t) wxParseCommonDialogsFilter(filter, descriptions, filters);
51a58d8b
JS
1435
1436 if (n > 0 && defaultFilter < (int) n)
1437 {
999836aa 1438 for (size_t i = 0; i < n; i++)
51a58d8b
JS
1439 Append(descriptions[i]);
1440 SetSelection(defaultFilter);
1441 }
1442}
13de0c8c 1443#endif // wxUSE_DIRDLG
51a58d8b 1444
13de0c8c 1445#if wxUSE_DIRDLG || wxUSE_FILEDLG
22328fa4 1446
06cc1fb9
JS
1447// ----------------------------------------------------------------------------
1448// wxFileIconsTable icons
1449// ----------------------------------------------------------------------------
1450
ff654490 1451#ifndef __WXGTK20__
13b22a67 1452/* Computer (c) Julian Smart */
a243da29 1453static const char* const file_icons_tbl_computer_xpm[] = {
13b22a67
JS
1454/* columns rows colors chars-per-pixel */
1455"16 16 42 1",
1456"r c #4E7FD0",
1457"$ c #7198D9",
1458"; c #DCE6F6",
1459"q c #FFFFFF",
1460"u c #4A7CCE",
1461"# c #779DDB",
1462"w c #95B2E3",
1463"y c #7FA2DD",
1464"f c #3263B4",
1465"= c #EAF0FA",
1466"< c #B1C7EB",
1467"% c #6992D7",
1468"9 c #D9E4F5",
1469"o c #9BB7E5",
1470"6 c #F7F9FD",
1471", c #BED0EE",
1472"3 c #F0F5FC",
1473"1 c #A8C0E8",
1474" c None",
1475"0 c #FDFEFF",
1476"4 c #C4D5F0",
1477"@ c #81A4DD",
1478"e c #4377CD",
1479"- c #E2EAF8",
1480"i c #9FB9E5",
1481"> c #CCDAF2",
1482"+ c #89A9DF",
1483"s c #5584D1",
1484"t c #5D89D3",
1485": c #D2DFF4",
1486"5 c #FAFCFE",
1487"2 c #F5F8FD",
1488"8 c #DFE8F7",
1489"& c #5E8AD4",
1490"X c #638ED5",
1491"a c #CEDCF2",
1492"p c #90AFE2",
1493"d c #2F5DA9",
1494"* c #5282D0",
1495"7 c #E5EDF9",
1496". c #A2BCE6",
1497"O c #8CACE0",
1498/* pixels */
1499" ",
1500" .XXXXXXXXXXX ",
1501" oXO++@#$%&*X ",
1502" oX=-;:>,<1%X ",
1503" oX23=-;:4,$X ",
1504" oX5633789:@X ",
1505" oX05623=78+X ",
1506" oXqq05623=OX ",
1507" oX,,,,,<<<$X ",
1508" wXXXXXXXXXXe ",
1509" XrtX%$$y@+O,, ",
1510" uyiiiiiiiii@< ",
1511" ouiiiiiiiiiip<a",
1512" rustX%$$y@+Ow,,",
1513" dfffffffffffffd",
1514" "
1515};
ff654490 1516#endif // !GTK+ 2
06cc1fb9 1517
06cc1fb9
JS
1518// ----------------------------------------------------------------------------
1519// wxFileIconsTable & friends
1520// ----------------------------------------------------------------------------
1521
1522// global instance of a wxFileIconsTable
d3b9f782 1523wxFileIconsTable* wxTheFileIconsTable = NULL;
06cc1fb9
JS
1524
1525// A module to allow icons table cleanup
1526
1527class wxFileIconsTableModule: public wxModule
1528{
1529DECLARE_DYNAMIC_CLASS(wxFileIconsTableModule)
1530public:
1531 wxFileIconsTableModule() {}
ca65c044 1532 bool OnInit() { wxTheFileIconsTable = new wxFileIconsTable; return true; }
06cc1fb9
JS
1533 void OnExit()
1534 {
5276b0a5 1535 wxDELETE(wxTheFileIconsTable);
06cc1fb9
JS
1536 }
1537};
1538
1539IMPLEMENT_DYNAMIC_CLASS(wxFileIconsTableModule, wxModule)
1540
1541class wxFileIconEntry : public wxObject
1542{
1543public:
c1ec7ee8 1544 wxFileIconEntry(int i) { iconid = i; }
06cc1fb9 1545
c1ec7ee8 1546 int iconid;
06cc1fb9
JS
1547};
1548
1549wxFileIconsTable::wxFileIconsTable()
1550{
1551 m_HashTable = NULL;
1552 m_smallImageList = NULL;
1553}
1554
1555wxFileIconsTable::~wxFileIconsTable()
1556{
222ed1d6
MB
1557 if (m_HashTable)
1558 {
1559 WX_CLEAR_HASH_TABLE(*m_HashTable);
1560 delete m_HashTable;
1561 }
06cc1fb9
JS
1562 if (m_smallImageList) delete m_smallImageList;
1563}
1564
1565// delayed initialization - wait until first use (wxArtProv not created yet)
1566void wxFileIconsTable::Create()
1567{
1568 wxCHECK_RET(!m_smallImageList && !m_HashTable, wxT("creating icons twice"));
06cc1fb9
JS
1569 m_HashTable = new wxHashTable(wxKEY_STRING);
1570 m_smallImageList = new wxImageList(16, 16);
1571
06cc1fb9 1572 // folder:
4b92a33f
VS
1573 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_FOLDER,
1574 wxART_CMN_DIALOG,
1575 wxSize(16, 16)));
06cc1fb9 1576 // folder_open
82c1f2a3
RR
1577 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_FOLDER_OPEN,
1578 wxART_CMN_DIALOG,
1579 wxSize(16, 16)));
06cc1fb9 1580 // computer
ff654490 1581#ifdef __WXGTK20__
82c1f2a3
RR
1582 // GTK24 uses this icon in the file open dialog
1583 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_HARDDISK,
1584 wxART_CMN_DIALOG,
1585 wxSize(16, 16)));
1586#else
06cc1fb9 1587 m_smallImageList->Add(wxIcon(file_icons_tbl_computer_xpm));
82c1f2a3 1588#endif
06cc1fb9 1589 // drive
82c1f2a3
RR
1590 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_HARDDISK,
1591 wxART_CMN_DIALOG,
1592 wxSize(16, 16)));
06cc1fb9 1593 // cdrom
82c1f2a3
RR
1594 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_CDROM,
1595 wxART_CMN_DIALOG,
1596 wxSize(16, 16)));
06cc1fb9 1597 // floppy
82c1f2a3
RR
1598 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_FLOPPY,
1599 wxART_CMN_DIALOG,
1600 wxSize(16, 16)));
06cc1fb9 1601 // removeable
82c1f2a3
RR
1602 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_REMOVABLE,
1603 wxART_CMN_DIALOG,
1604 wxSize(16, 16)));
06cc1fb9 1605 // file
4b92a33f
VS
1606 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_NORMAL_FILE,
1607 wxART_CMN_DIALOG,
1608 wxSize(16, 16)));
06cc1fb9 1609 // executable
9a83f860 1610 if (GetIconID(wxEmptyString, wxT("application/x-executable")) == file)
06cc1fb9 1611 {
4b92a33f
VS
1612 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_EXECUTABLE_FILE,
1613 wxART_CMN_DIALOG,
1614 wxSize(16, 16)));
9a83f860
VZ
1615 delete m_HashTable->Get(wxT("exe"));
1616 m_HashTable->Delete(wxT("exe"));
1617 m_HashTable->Put(wxT("exe"), new wxFileIconEntry(executable));
06cc1fb9
JS
1618 }
1619 /* else put into list by GetIconID
1620 (KDE defines application/x-executable for *.exe and has nice icon)
1621 */
1622}
1623
1624wxImageList *wxFileIconsTable::GetSmallImageList()
1625{
1626 if (!m_smallImageList)
1627 Create();
1628
1629 return m_smallImageList;
1630}
1631
bb5a9514 1632#if wxUSE_MIMETYPE && wxUSE_IMAGE && (!defined(__WINDOWS__) || wxUSE_WXDIB)
06cc1fb9
JS
1633// VS: we don't need this function w/o wxMimeTypesManager because we'll only have
1634// one icon and we won't resize it
1635
1636static wxBitmap CreateAntialiasedBitmap(const wxImage& img)
1637{
1d529ef7 1638 const unsigned int size = 16;
ca65c044 1639
1d529ef7 1640 wxImage smallimg (size, size);
06cc1fb9
JS
1641 unsigned char *p1, *p2, *ps;
1642 unsigned char mr = img.GetMaskRed(),
1643 mg = img.GetMaskGreen(),
1644 mb = img.GetMaskBlue();
1645
1646 unsigned x, y;
1647 unsigned sr, sg, sb, smask;
1648
1d529ef7 1649 p1 = img.GetData(), p2 = img.GetData() + 3 * size*2, ps = smallimg.GetData();
06cc1fb9
JS
1650 smallimg.SetMaskColour(mr, mr, mr);
1651
1d529ef7 1652 for (y = 0; y < size; y++)
06cc1fb9 1653 {
1d529ef7 1654 for (x = 0; x < size; x++)
06cc1fb9
JS
1655 {
1656 sr = sg = sb = smask = 0;
1657 if (p1[0] != mr || p1[1] != mg || p1[2] != mb)
1658 sr += p1[0], sg += p1[1], sb += p1[2];
1659 else smask++;
1660 p1 += 3;
1661 if (p1[0] != mr || p1[1] != mg || p1[2] != mb)
1662 sr += p1[0], sg += p1[1], sb += p1[2];
1663 else smask++;
1664 p1 += 3;
1665 if (p2[0] != mr || p2[1] != mg || p2[2] != mb)
1666 sr += p2[0], sg += p2[1], sb += p2[2];
1667 else smask++;
1668 p2 += 3;
1669 if (p2[0] != mr || p2[1] != mg || p2[2] != mb)
1670 sr += p2[0], sg += p2[1], sb += p2[2];
1671 else smask++;
1672 p2 += 3;
1673
1674 if (smask > 2)
1675 ps[0] = ps[1] = ps[2] = mr;
1676 else
8253f2e0
WS
1677 {
1678 ps[0] = (unsigned char)(sr >> 2);
1679 ps[1] = (unsigned char)(sg >> 2);
1680 ps[2] = (unsigned char)(sb >> 2);
1681 }
06cc1fb9
JS
1682 ps += 3;
1683 }
1d529ef7 1684 p1 += size*2 * 3, p2 += size*2 * 3;
06cc1fb9 1685 }
ca65c044 1686
06cc1fb9
JS
1687 return wxBitmap(smallimg);
1688}
1689
b126fe3a
DS
1690// This function is currently not unused anymore
1691#if 0
06cc1fb9
JS
1692// finds empty borders and return non-empty area of image:
1693static wxImage CutEmptyBorders(const wxImage& img)
1694{
1695 unsigned char mr = img.GetMaskRed(),
1696 mg = img.GetMaskGreen(),
1697 mb = img.GetMaskBlue();
1698 unsigned char *dt = img.GetData(), *dttmp;
1699 unsigned w = img.GetWidth(), h = img.GetHeight();
1700
1701 unsigned top, bottom, left, right, i;
1702 bool empt;
1703
1704#define MK_DTTMP(x,y) dttmp = dt + ((x + y * w) * 3)
ca65c044 1705#define NOEMPTY_PIX(empt) if (dttmp[0] != mr || dttmp[1] != mg || dttmp[2] != mb) {empt = false; break;}
06cc1fb9 1706
ca65c044 1707 for (empt = true, top = 0; empt && top < h; top++)
06cc1fb9
JS
1708 {
1709 MK_DTTMP(0, top);
1710 for (i = 0; i < w; i++, dttmp+=3)
1711 NOEMPTY_PIX(empt)
1712 }
ca65c044 1713 for (empt = true, bottom = h-1; empt && bottom > top; bottom--)
06cc1fb9
JS
1714 {
1715 MK_DTTMP(0, bottom);
1716 for (i = 0; i < w; i++, dttmp+=3)
1717 NOEMPTY_PIX(empt)
1718 }
ca65c044 1719 for (empt = true, left = 0; empt && left < w; left++)
06cc1fb9
JS
1720 {
1721 MK_DTTMP(left, 0);
1722 for (i = 0; i < h; i++, dttmp+=3*w)
1723 NOEMPTY_PIX(empt)
1724 }
ca65c044 1725 for (empt = true, right = w-1; empt && right > left; right--)
06cc1fb9
JS
1726 {
1727 MK_DTTMP(right, 0);
1728 for (i = 0; i < h; i++, dttmp+=3*w)
1729 NOEMPTY_PIX(empt)
1730 }
1731 top--, left--, bottom++, right++;
1732
1733 return img.GetSubImage(wxRect(left, top, right - left + 1, bottom - top + 1));
1734}
b126fe3a
DS
1735#endif // #if 0
1736
06cc1fb9
JS
1737#endif // wxUSE_MIMETYPE
1738
1739int wxFileIconsTable::GetIconID(const wxString& extension, const wxString& mime)
1740{
1741 if (!m_smallImageList)
1742 Create();
1743
1744#if wxUSE_MIMETYPE
44d60c0b 1745 if (!extension.empty())
06cc1fb9
JS
1746 {
1747 wxFileIconEntry *entry = (wxFileIconEntry*) m_HashTable->Get(extension);
c1ec7ee8 1748 if (entry) return (entry -> iconid);
06cc1fb9
JS
1749 }
1750
44d60c0b 1751 wxFileType *ft = (mime.empty()) ?
06cc1fb9
JS
1752 wxTheMimeTypesManager -> GetFileTypeFromExtension(extension) :
1753 wxTheMimeTypesManager -> GetFileTypeFromMimeType(mime);
55d0aaa3
VZ
1754
1755 wxIconLocation iconLoc;
06cc1fb9 1756 wxIcon ic;
02dd0487 1757
55d0aaa3 1758 {
02dd0487
JS
1759 wxLogNull logNull;
1760 if ( ft && ft->GetIcon(&iconLoc) )
1761 {
8e5c6521 1762 ic = wxIcon( iconLoc );
02dd0487 1763 }
55d0aaa3 1764 }
ca65c044 1765
55d0aaa3
VZ
1766 delete ft;
1767
a1b806b9 1768 if ( !ic.IsOk() )
06cc1fb9
JS
1769 {
1770 int newid = file;
1771 m_HashTable->Put(extension, new wxFileIconEntry(newid));
1772 return newid;
1773 }
1774
3fc93ebd
JS
1775 wxBitmap bmp;
1776 bmp.CopyFromIcon(ic);
06cc1fb9 1777
a1b806b9 1778 if ( !bmp.IsOk() )
b6668c25
JS
1779 {
1780 int newid = file;
1781 m_HashTable->Put(extension, new wxFileIconEntry(newid));
1782 return newid;
1783 }
1784
1d529ef7 1785 const unsigned int size = 16;
ca65c044 1786
c1ec7ee8 1787 int treeid = m_smallImageList->GetImageCount();
1aa81b17 1788 if ((bmp.GetWidth() == (int) size) && (bmp.GetHeight() == (int) size))
1d529ef7 1789 {
3fc93ebd 1790 m_smallImageList->Add(bmp);
1d529ef7 1791 }
bb5a9514 1792#if wxUSE_IMAGE && (!defined(__WINDOWS__) || wxUSE_WXDIB)
06cc1fb9
JS
1793 else
1794 {
3fc93ebd
JS
1795 wxImage img = bmp.ConvertToImage();
1796
1d529ef7
RR
1797 if ((img.GetWidth() != size*2) || (img.GetHeight() != size*2))
1798// m_smallImageList->Add(CreateAntialiasedBitmap(CutEmptyBorders(img).Rescale(size*2, size*2)));
1799 m_smallImageList->Add(CreateAntialiasedBitmap(img.Rescale(size*2, size*2)));
06cc1fb9
JS
1800 else
1801 m_smallImageList->Add(CreateAntialiasedBitmap(img));
1802 }
1904aa72
DS
1803#endif // wxUSE_IMAGE
1804
c1ec7ee8
SC
1805 m_HashTable->Put(extension, new wxFileIconEntry(treeid));
1806 return treeid;
06cc1fb9
JS
1807
1808#else // !wxUSE_MIMETYPE
1809
79e162f5 1810 wxUnusedVar(mime);
06cc1fb9
JS
1811 if (extension == wxT("exe"))
1812 return executable;
1813 else
1814 return file;
1815#endif // wxUSE_MIMETYPE/!wxUSE_MIMETYPE
1816}
1817
13de0c8c 1818#endif // wxUSE_DIRDLG || wxUSE_FILEDLG