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