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