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