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