]> git.saurik.com Git - wxWidgets.git/blame - src/generic/dirctrlg.cpp
Set the find button to be the default button.
[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') ||
da865fdd 262 wxDirExists(dirNameLower));
db5333a5
VS
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)
da865fdd 321 success = wxDirExists(dirNameLower);
7328394a 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
c06dde42
JS
490void wxGenericDirCtrl::ExpandRoot()
491{
492 ExpandDir(m_rootId); // automatically expand first level
493
494 // Expand and select the default path
495 if (!m_defaultPath.empty())
496 {
497 ExpandPath(m_defaultPath);
498 }
499#ifdef __UNIX__
500 else
501 {
502 // On Unix, there's only one node under the (hidden) root node. It
503 // represents the / path, so the user would always have to expand it;
504 // let's do it ourselves
505 ExpandPath( wxT("/") );
506 }
507#endif
508}
509
51a58d8b 510bool wxGenericDirCtrl::Create(wxWindow *parent,
748fcded
VS
511 const wxWindowID id,
512 const wxString& dir,
513 const wxPoint& pos,
514 const wxSize& size,
515 long style,
516 const wxString& filter,
517 int defaultFilter,
518 const wxString& name)
51a58d8b
JS
519{
520 if (!wxControl::Create(parent, id, pos, size, style, wxDefaultValidator, name))
ca65c044 521 return false;
51a58d8b 522
db5333a5 523 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));
51a58d8b
JS
524
525 Init();
526
3bd8a405
JS
527 long treeStyle = wxTR_HAS_BUTTONS;
528
529 // On Windows CE, if you hide the root, you get a crash when
530 // attempting to access data for children of the root item.
531#ifndef __WXWINCE__
532 treeStyle |= wxTR_HIDE_ROOT;
533#endif
2997ca30 534
82c1f2a3
RR
535#ifdef __WXGTK20__
536 treeStyle |= wxTR_NO_LINES;
537#endif
fd775aae 538
dabd1377 539 if (style & wxDIRCTRL_EDIT_LABELS)
fd775aae
JS
540 treeStyle |= wxTR_EDIT_LABELS;
541
51a58d8b
JS
542 if ((style & wxDIRCTRL_3D_INTERNAL) == 0)
543 treeStyle |= wxNO_BORDER;
27c73976
JS
544 else
545 treeStyle |= wxBORDER_SUNKEN;
51a58d8b
JS
546
547 long filterStyle = 0;
548 if ((style & wxDIRCTRL_3D_INTERNAL) == 0)
549 filterStyle |= wxNO_BORDER;
27c73976
JS
550 else
551 filterStyle |= wxBORDER_SUNKEN;
51a58d8b 552
a78955e3 553 m_treeCtrl = CreateTreeCtrl(this, wxID_TREECTRL,
c47addef 554 wxPoint(0,0), GetClientSize(), treeStyle);
51a58d8b 555
44d60c0b 556 if (!filter.empty() && (style & wxDIRCTRL_SHOW_FILTERS))
51a58d8b
JS
557 m_filterListCtrl = new wxDirFilterListCtrl(this, wxID_FILTERLISTCTRL, wxDefaultPosition, wxDefaultSize, filterStyle);
558
559 m_defaultPath = dir;
560 m_filter = filter;
561
dd0138d9
RR
562 if (m_filter.empty())
563#ifdef __UNIX__
564 m_filter = wxT("*");
565#else
566 m_filter = wxT("*.*");
567#endif
568
51a58d8b
JS
569 SetFilterIndex(defaultFilter);
570
571 if (m_filterListCtrl)
572 m_filterListCtrl->FillFilterList(filter, defaultFilter);
573
06cc1fb9 574 m_treeCtrl->SetImageList(wxTheFileIconsTable->GetSmallImageList());
51a58d8b 575
ca65c044
WS
576 m_showHidden = false;
577 wxDirItemData* rootData = new wxDirItemData(wxEmptyString, wxEmptyString, true);
51a58d8b
JS
578
579 wxString rootName;
580
0d853c54 581#if defined(__WINDOWS__) || defined(__OS2__) || defined(__DOS__)
51a58d8b
JS
582 rootName = _("Computer");
583#else
584 rootName = _("Sections");
585#endif
586
587 m_rootId = m_treeCtrl->AddRoot( rootName, 3, -1, rootData);
588 m_treeCtrl->SetItemHasChildren(m_rootId);
2997ca30 589
c06dde42 590 ExpandRoot();
51a58d8b 591
19d8dd12 592 SetBestSize(size);
51a58d8b
JS
593 DoResize();
594
ca65c044 595 return true;
51a58d8b
JS
596}
597
598wxGenericDirCtrl::~wxGenericDirCtrl()
599{
51a58d8b
JS
600}
601
602void wxGenericDirCtrl::Init()
603{
ca65c044 604 m_showHidden = false;
51a58d8b
JS
605 m_currentFilter = 0;
606 m_currentFilterStr = wxEmptyString; // Default: any file
607 m_treeCtrl = NULL;
608 m_filterListCtrl = NULL;
609}
610
a78955e3
VS
611wxTreeCtrl* wxGenericDirCtrl::CreateTreeCtrl(wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long treeStyle)
612{
613 return new wxTreeCtrl(parent, id, pos, size, treeStyle);
614}
615
42dcacf0
RR
616void wxGenericDirCtrl::ShowHidden( bool show )
617{
618 m_showHidden = show;
574c939e 619
3b423cdd 620 wxString path = GetPath();
08887820 621 ReCreateTree();
3b423cdd 622 SetPath(path);
42dcacf0
RR
623}
624
22328fa4
JS
625const wxTreeItemId
626wxGenericDirCtrl::AddSection(const wxString& path, const wxString& name, int imageId)
51a58d8b 627{
ca65c044 628 wxDirItemData *dir_item = new wxDirItemData(path,name,true);
51a58d8b 629
22328fa4 630 wxTreeItemId id = AppendItem( m_rootId, name, imageId, -1, dir_item);
4f5c180e 631
06cc1fb9 632 m_treeCtrl->SetItemHasChildren(id);
22328fa4
JS
633
634 return id;
06cc1fb9 635}
51a58d8b 636
06cc1fb9
JS
637void wxGenericDirCtrl::SetupSections()
638{
639 wxArrayString paths, names;
640 wxArrayInt icons;
2d4e4f80 641
06cc1fb9 642 size_t n, count = wxGetAvailableDrives(paths, names, icons);
4f5c180e 643
82c1f2a3
RR
644#ifdef __WXGTK20__
645 wxString home = wxGetHomeDir();
646 AddSection( home, _("Home directory"), 1);
647 home += wxT("/Desktop");
648 AddSection( home, _("Desktop"), 1);
649#endif
650
06cc1fb9 651 for (n = 0; n < count; n++)
06cc1fb9 652 AddSection(paths[n], names[n], icons[n]);
51a58d8b
JS
653}
654
655void wxGenericDirCtrl::OnBeginEditItem(wxTreeEvent &event)
656{
657 // don't rename the main entry "Sections"
658 if (event.GetItem() == m_rootId)
659 {
660 event.Veto();
661 return;
662 }
663
664 // don't rename the individual sections
99006e44 665 if (m_treeCtrl->GetItemParent( event.GetItem() ) == m_rootId)
51a58d8b
JS
666 {
667 event.Veto();
668 return;
669 }
670}
671
672void wxGenericDirCtrl::OnEndEditItem(wxTreeEvent &event)
673{
44d60c0b 674 if ((event.GetLabel().empty()) ||
51a58d8b
JS
675 (event.GetLabel() == _(".")) ||
676 (event.GetLabel() == _("..")) ||
06cc1fb9
JS
677 (event.GetLabel().Find(wxT('/')) != wxNOT_FOUND) ||
678 (event.GetLabel().Find(wxT('\\')) != wxNOT_FOUND) ||
679 (event.GetLabel().Find(wxT('|')) != wxNOT_FOUND))
51a58d8b
JS
680 {
681 wxMessageDialog dialog(this, _("Illegal directory name."), _("Error"), wxOK | wxICON_ERROR );
682 dialog.ShowModal();
683 event.Veto();
684 return;
685 }
686
687 wxTreeItemId id = event.GetItem();
748fcded 688 wxDirItemData *data = (wxDirItemData*)m_treeCtrl->GetItemData( id );
51a58d8b
JS
689 wxASSERT( data );
690
691 wxString new_name( wxPathOnly( data->m_path ) );
ad9cd15c 692 new_name += wxString(wxFILE_SEP_PATH);
51a58d8b
JS
693 new_name += event.GetLabel();
694
695 wxLogNull log;
696
697 if (wxFileExists(new_name))
698 {
699 wxMessageDialog dialog(this, _("File name exists already."), _("Error"), wxOK | wxICON_ERROR );
700 dialog.ShowModal();
701 event.Veto();
702 }
703
704 if (wxRenameFile(data->m_path,new_name))
705 {
706 data->SetNewDirName( new_name );
707 }
708 else
709 {
710 wxMessageDialog dialog(this, _("Operation not permitted."), _("Error"), wxOK | wxICON_ERROR );
711 dialog.ShowModal();
712 event.Veto();
713 }
714}
715
716void wxGenericDirCtrl::OnExpandItem(wxTreeEvent &event)
717{
718 wxTreeItemId parentId = event.GetItem();
719
748fcded
VS
720 // VS: this is needed because the event handler is called from wxTreeCtrl
721 // ctor when wxTR_HIDE_ROOT was specified
4ded51f2
CE
722
723 if (!m_rootId.IsOk())
724
748fcded
VS
725 m_rootId = m_treeCtrl->GetRootItem();
726
51a58d8b
JS
727 ExpandDir(parentId);
728}
729
730void wxGenericDirCtrl::OnCollapseItem(wxTreeEvent &event )
731{
08887820
VS
732 CollapseDir(event.GetItem());
733}
734
735void wxGenericDirCtrl::CollapseDir(wxTreeItemId parentId)
736{
737 wxTreeItemId child;
51a58d8b 738
08887820 739 wxDirItemData *data = (wxDirItemData *) m_treeCtrl->GetItemData(parentId);
51a58d8b
JS
740 if (!data->m_isExpanded)
741 return;
742
ca65c044 743 data->m_isExpanded = false;
2d75caaa 744 wxTreeItemIdValue cookie;
51a58d8b
JS
745 /* Workaround because DeleteChildren has disapeared (why?) and
746 * CollapseAndReset doesn't work as advertised (deletes parent too) */
08887820 747 child = m_treeCtrl->GetFirstChild(parentId, cookie);
51a58d8b
JS
748 while (child.IsOk())
749 {
750 m_treeCtrl->Delete(child);
751 /* Not GetNextChild below, because the cookie mechanism can't
752 * handle disappearing children! */
08887820 753 child = m_treeCtrl->GetFirstChild(parentId, cookie);
51a58d8b 754 }
c06dde42
JS
755 if (parentId != m_treeCtrl->GetRootItem())
756 m_treeCtrl->Collapse(parentId);
51a58d8b
JS
757}
758
759void wxGenericDirCtrl::ExpandDir(wxTreeItemId parentId)
760{
748fcded 761 wxDirItemData *data = (wxDirItemData *) m_treeCtrl->GetItemData(parentId);
51a58d8b
JS
762
763 if (data->m_isExpanded)
764 return;
765
ca65c044 766 data->m_isExpanded = true;
51a58d8b 767
748fcded 768 if (parentId == m_treeCtrl->GetRootItem())
51a58d8b
JS
769 {
770 SetupSections();
771 return;
772 }
773
774 wxASSERT(data);
775
776 wxString search,path,filename;
777
778 wxString dirName(data->m_path);
779
3bd8a405 780#if (defined(__WINDOWS__) && !defined(__WXWINCE__)) || defined(__DOS__) || defined(__OS2__)
51a58d8b
JS
781 // Check if this is a root directory and if so,
782 // whether the drive is avaiable.
33fed835
MB
783 if (!wxIsDriveAvailable(dirName))
784 {
ca65c044 785 data->m_isExpanded = false;
7328394a
JS
786 //wxMessageBox(wxT("Sorry, this drive is not available."));
787 return;
33fed835 788 }
51a58d8b
JS
789#endif
790
791 // This may take a longish time. Go to busy cursor
792 wxBusyCursor busy;
793
0d853c54 794#if defined(__WINDOWS__) || defined(__DOS__) || defined(__OS2__)
51a58d8b
JS
795 if (dirName.Last() == ':')
796 dirName += wxString(wxFILE_SEP_PATH);
797#endif
798
799 wxArrayString dirs;
800 wxArrayString filenames;
801
802 wxDir d;
803 wxString eachFilename;
804
f9c165b1 805 wxLogNull log;
51a58d8b
JS
806 d.Open(dirName);
807
808 if (d.IsOpened())
809 {
42dcacf0
RR
810 int style = wxDIR_DIRS;
811 if (m_showHidden) style |= wxDIR_HIDDEN;
812 if (d.GetFirst(& eachFilename, wxEmptyString, style))
51a58d8b
JS
813 {
814 do
815 {
816 if ((eachFilename != wxT(".")) && (eachFilename != wxT("..")))
817 {
818 dirs.Add(eachFilename);
819 }
820 }
2b0a7c09 821 while (d.GetNext(&eachFilename));
51a58d8b
JS
822 }
823 }
222ed1d6 824 dirs.Sort(wxDirCtrlStringCompareFunction);
51a58d8b
JS
825
826 // Now do the filenames -- but only if we're allowed to
827 if ((GetWindowStyle() & wxDIRCTRL_DIR_ONLY) == 0)
828 {
f9c165b1
JS
829 wxLogNull log;
830
51a58d8b 831 d.Open(dirName);
ec1b28a3 832
51a58d8b
JS
833 if (d.IsOpened())
834 {
2b0a7c09
RN
835 int style = wxDIR_FILES;
836 if (m_showHidden) style |= wxDIR_HIDDEN;
3da4e4bd
JS
837 // Process each filter (ex: "JPEG Files (*.jpg;*.jpeg)|*.jpg;*.jpeg")
838 wxStringTokenizer strTok;
839 wxString curFilter;
840 strTok.SetString(m_currentFilterStr,wxT(";"));
841 while(strTok.HasMoreTokens())
51a58d8b 842 {
3da4e4bd 843 curFilter = strTok.GetNextToken();
f0e5a44d 844 if (d.GetFirst(& eachFilename, curFilter, style))
51a58d8b 845 {
3da4e4bd 846 do
51a58d8b 847 {
3da4e4bd
JS
848 if ((eachFilename != wxT(".")) && (eachFilename != wxT("..")))
849 {
850 filenames.Add(eachFilename);
851 }
51a58d8b 852 }
3da4e4bd 853 while (d.GetNext(& eachFilename));
51a58d8b 854 }
51a58d8b
JS
855 }
856 }
222ed1d6 857 filenames.Sort(wxDirCtrlStringCompareFunction);
51a58d8b
JS
858 }
859
860 // Add the sorted dirs
861 size_t i;
862 for (i = 0; i < dirs.Count(); i++)
863 {
864 wxString eachFilename(dirs[i]);
865 path = dirName;
083f7497 866 if (!wxEndsWithPathSeparator(path))
51a58d8b
JS
867 path += wxString(wxFILE_SEP_PATH);
868 path += eachFilename;
869
ca65c044 870 wxDirItemData *dir_item = new wxDirItemData(path,eachFilename,true);
22328fa4 871 wxTreeItemId id = AppendItem( parentId, eachFilename,
06cc1fb9
JS
872 wxFileIconsTable::folder, -1, dir_item);
873 m_treeCtrl->SetItemImage( id, wxFileIconsTable::folder_open,
874 wxTreeItemIcon_Expanded );
ec1b28a3 875
51a58d8b 876 // Has this got any children? If so, make it expandable.
748fcded
VS
877 // (There are two situations when a dir has children: either it
878 // has subdirectories or it contains files that weren't filtered
879 // out. The latter only applies to dirctrl with files.)
880 if ( dir_item->HasSubDirs() ||
881 (((GetWindowStyle() & wxDIRCTRL_DIR_ONLY) == 0) &&
882 dir_item->HasFiles(m_currentFilterStr)) )
51a58d8b 883 {
748fcded 884 m_treeCtrl->SetItemHasChildren(id);
51a58d8b 885 }
51a58d8b
JS
886 }
887
888 // Add the sorted filenames
889 if ((GetWindowStyle() & wxDIRCTRL_DIR_ONLY) == 0)
890 {
891 for (i = 0; i < filenames.Count(); i++)
892 {
893 wxString eachFilename(filenames[i]);
894 path = dirName;
083f7497 895 if (!wxEndsWithPathSeparator(path))
51a58d8b
JS
896 path += wxString(wxFILE_SEP_PATH);
897 path += eachFilename;
898 //path = dirName + wxString(wxT("/")) + eachFilename;
ca65c044 899 wxDirItemData *dir_item = new wxDirItemData(path,eachFilename,false);
06cc1fb9
JS
900 int image_id = wxFileIconsTable::file;
901 if (eachFilename.Find(wxT('.')) != wxNOT_FOUND)
902 image_id = wxTheFileIconsTable->GetIconID(eachFilename.AfterLast(wxT('.')));
22328fa4 903 (void) AppendItem( parentId, eachFilename, image_id, -1, dir_item);
51a58d8b
JS
904 }
905 }
906}
907
08887820
VS
908void wxGenericDirCtrl::ReCreateTree()
909{
910 CollapseDir(m_treeCtrl->GetRootItem());
c06dde42
JS
911 ExpandRoot();
912}
913
914void wxGenericDirCtrl::CollapseTree()
915{
916 wxTreeItemIdValue cookie;
917 wxTreeItemId child = m_treeCtrl->GetFirstChild(m_rootId, cookie);
918 while (child.IsOk())
919 {
920 CollapseDir(child);
921 child = m_treeCtrl->GetNextChild(m_rootId, cookie);
922 }
08887820
VS
923}
924
51a58d8b
JS
925// Find the child that matches the first part of 'path'.
926// E.g. if a child path is "/usr" and 'path' is "/usr/include"
927// then the child for /usr is returned.
928wxTreeItemId wxGenericDirCtrl::FindChild(wxTreeItemId parentId, const wxString& path, bool& done)
929{
930 wxString path2(path);
ec1b28a3 931
51a58d8b
JS
932 // Make sure all separators are as per the current platform
933 path2.Replace(wxT("\\"), wxString(wxFILE_SEP_PATH));
934 path2.Replace(wxT("/"), wxString(wxFILE_SEP_PATH));
ec1b28a3 935
51a58d8b
JS
936 // Append a separator to foil bogus substring matching
937 path2 += wxString(wxFILE_SEP_PATH);
ec1b28a3
DW
938
939 // In MSW or PM, case is not significant
0d853c54 940#if defined(__WINDOWS__) || defined(__DOS__) || defined(__OS2__)
51a58d8b
JS
941 path2.MakeLower();
942#endif
ec1b28a3 943
2d75caaa 944 wxTreeItemIdValue cookie;
51a58d8b 945 wxTreeItemId childId = m_treeCtrl->GetFirstChild(parentId, cookie);
53ccf1c0 946 while (childId.IsOk())
51a58d8b 947 {
748fcded 948 wxDirItemData* data = (wxDirItemData*) m_treeCtrl->GetItemData(childId);
ec1b28a3 949
44d60c0b 950 if (data && !data->m_path.empty())
51a58d8b
JS
951 {
952 wxString childPath(data->m_path);
083f7497 953 if (!wxEndsWithPathSeparator(childPath))
51a58d8b 954 childPath += wxString(wxFILE_SEP_PATH);
ec1b28a3
DW
955
956 // In MSW and PM, case is not significant
0d853c54 957#if defined(__WINDOWS__) || defined(__DOS__) || defined(__OS2__)
51a58d8b
JS
958 childPath.MakeLower();
959#endif
ec1b28a3 960
51a58d8b
JS
961 if (childPath.Len() <= path2.Len())
962 {
963 wxString path3 = path2.Mid(0, childPath.Len());
964 if (childPath == path3)
965 {
966 if (path3.Len() == path2.Len())
ca65c044 967 done = true;
51a58d8b 968 else
ca65c044 969 done = false;
51a58d8b
JS
970 return childId;
971 }
972 }
973 }
ec1b28a3 974
748fcded 975 childId = m_treeCtrl->GetNextChild(parentId, cookie);
51a58d8b 976 }
3fa4bd0e
VS
977 wxTreeItemId invalid;
978 return invalid;
51a58d8b
JS
979}
980
981// Try to expand as much of the given path as possible,
982// and select the given tree item.
983bool wxGenericDirCtrl::ExpandPath(const wxString& path)
984{
ca65c044 985 bool done = false;
51a58d8b
JS
986 wxTreeItemId id = FindChild(m_rootId, path, done);
987 wxTreeItemId lastId = id; // The last non-zero id
237387ad 988 while (id.IsOk() && !done)
51a58d8b
JS
989 {
990 ExpandDir(id);
991
992 id = FindChild(id, path, done);
53ccf1c0 993 if (id.IsOk())
51a58d8b
JS
994 lastId = id;
995 }
53ccf1c0 996 if (lastId.IsOk())
51a58d8b 997 {
748fcded 998 wxDirItemData *data = (wxDirItemData *) m_treeCtrl->GetItemData(lastId);
51a58d8b
JS
999 if (data->m_isDir)
1000 {
1001 m_treeCtrl->Expand(lastId);
1002 }
1003 if ((GetWindowStyle() & wxDIRCTRL_SELECT_FIRST) && data->m_isDir)
1004 {
1005 // Find the first file in this directory
2d75caaa 1006 wxTreeItemIdValue cookie;
51a58d8b 1007 wxTreeItemId childId = m_treeCtrl->GetFirstChild(lastId, cookie);
ca65c044 1008 bool selectedChild = false;
53ccf1c0 1009 while (childId.IsOk())
51a58d8b 1010 {
748fcded 1011 wxDirItemData* data = (wxDirItemData*) m_treeCtrl->GetItemData(childId);
ec1b28a3 1012
ca65c044 1013 if (data && data->m_path != wxEmptyString && !data->m_isDir)
51a58d8b
JS
1014 {
1015 m_treeCtrl->SelectItem(childId);
1016 m_treeCtrl->EnsureVisible(childId);
ca65c044 1017 selectedChild = true;
51a58d8b
JS
1018 break;
1019 }
1020 childId = m_treeCtrl->GetNextChild(lastId, cookie);
1021 }
1022 if (!selectedChild)
1023 {
1024 m_treeCtrl->SelectItem(lastId);
1025 m_treeCtrl->EnsureVisible(lastId);
1026 }
1027 }
1028 else
1029 {
1030 m_treeCtrl->SelectItem(lastId);
1031 m_treeCtrl->EnsureVisible(lastId);
1032 }
1033
ca65c044 1034 return true;
51a58d8b
JS
1035 }
1036 else
ca65c044 1037 return false;
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__
06cc1fb9
JS
1298/* Computer */
1299static const char * file_icons_tbl_computer_xpm[] = {
1300"16 16 7 1",
1301" s None c None",
1302". c #808080",
1303"X c #c0c0c0",
1304"o c Black",
1305"O c Gray100",
1306"+ c #008080",
1307"@ c Blue",
1308" ........... ",
1309" .XXXXXXXXXX.o",
1310" .OOOOOOOOO..o",
1311" .OoooooooX..o",
1312" .Oo+...@+X..o",
1313" .Oo+XXX.+X..o",
1314" .Oo+....+X..o",
1315" .Oo++++++X..o",
1316" .OXXXXXXXX.oo",
1317" ..........o.o",
1318" ...........Xo",
1319" .XXXXXXXXXX.o",
1320" .o.o.o.o.o...o",
1321" .oXoXoXoXoXo.o ",
1322".XOXXXXXXXXX.o ",
1323"............o "};
d5f330f9 1324#endif // GTK+ < 2.4
06cc1fb9 1325
06cc1fb9
JS
1326// ----------------------------------------------------------------------------
1327// wxFileIconsTable & friends
1328// ----------------------------------------------------------------------------
1329
1330// global instance of a wxFileIconsTable
1331wxFileIconsTable* wxTheFileIconsTable = (wxFileIconsTable *)NULL;
1332
1333// A module to allow icons table cleanup
1334
1335class wxFileIconsTableModule: public wxModule
1336{
1337DECLARE_DYNAMIC_CLASS(wxFileIconsTableModule)
1338public:
1339 wxFileIconsTableModule() {}
ca65c044 1340 bool OnInit() { wxTheFileIconsTable = new wxFileIconsTable; return true; }
06cc1fb9
JS
1341 void OnExit()
1342 {
1343 if (wxTheFileIconsTable)
1344 {
1345 delete wxTheFileIconsTable;
1346 wxTheFileIconsTable = NULL;
1347 }
1348 }
1349};
1350
1351IMPLEMENT_DYNAMIC_CLASS(wxFileIconsTableModule, wxModule)
1352
1353class wxFileIconEntry : public wxObject
1354{
1355public:
1356 wxFileIconEntry(int i) { id = i; }
1357
1358 int id;
1359};
1360
1361wxFileIconsTable::wxFileIconsTable()
1362{
1363 m_HashTable = NULL;
1364 m_smallImageList = NULL;
1365}
1366
1367wxFileIconsTable::~wxFileIconsTable()
1368{
222ed1d6
MB
1369 if (m_HashTable)
1370 {
1371 WX_CLEAR_HASH_TABLE(*m_HashTable);
1372 delete m_HashTable;
1373 }
06cc1fb9
JS
1374 if (m_smallImageList) delete m_smallImageList;
1375}
1376
1377// delayed initialization - wait until first use (wxArtProv not created yet)
1378void wxFileIconsTable::Create()
1379{
1380 wxCHECK_RET(!m_smallImageList && !m_HashTable, wxT("creating icons twice"));
06cc1fb9
JS
1381 m_HashTable = new wxHashTable(wxKEY_STRING);
1382 m_smallImageList = new wxImageList(16, 16);
1383
06cc1fb9 1384 // folder:
4b92a33f
VS
1385 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_FOLDER,
1386 wxART_CMN_DIALOG,
1387 wxSize(16, 16)));
06cc1fb9 1388 // folder_open
82c1f2a3
RR
1389 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_FOLDER_OPEN,
1390 wxART_CMN_DIALOG,
1391 wxSize(16, 16)));
06cc1fb9 1392 // computer
82c1f2a3
RR
1393#ifdef __WXGTK24__
1394 // GTK24 uses this icon in the file open dialog
1395 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_HARDDISK,
1396 wxART_CMN_DIALOG,
1397 wxSize(16, 16)));
1398#else
06cc1fb9 1399 m_smallImageList->Add(wxIcon(file_icons_tbl_computer_xpm));
82c1f2a3 1400#endif
06cc1fb9 1401 // drive
82c1f2a3
RR
1402 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_HARDDISK,
1403 wxART_CMN_DIALOG,
1404 wxSize(16, 16)));
06cc1fb9 1405 // cdrom
82c1f2a3
RR
1406 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_CDROM,
1407 wxART_CMN_DIALOG,
1408 wxSize(16, 16)));
06cc1fb9 1409 // floppy
82c1f2a3
RR
1410 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_FLOPPY,
1411 wxART_CMN_DIALOG,
1412 wxSize(16, 16)));
06cc1fb9 1413 // removeable
82c1f2a3
RR
1414 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_REMOVABLE,
1415 wxART_CMN_DIALOG,
1416 wxSize(16, 16)));
06cc1fb9 1417 // file
4b92a33f
VS
1418 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_NORMAL_FILE,
1419 wxART_CMN_DIALOG,
1420 wxSize(16, 16)));
06cc1fb9
JS
1421 // executable
1422 if (GetIconID(wxEmptyString, _T("application/x-executable")) == file)
1423 {
4b92a33f
VS
1424 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_EXECUTABLE_FILE,
1425 wxART_CMN_DIALOG,
1426 wxSize(16, 16)));
222ed1d6 1427 delete m_HashTable->Get(_T("exe"));
06cc1fb9
JS
1428 m_HashTable->Delete(_T("exe"));
1429 m_HashTable->Put(_T("exe"), new wxFileIconEntry(executable));
1430 }
1431 /* else put into list by GetIconID
1432 (KDE defines application/x-executable for *.exe and has nice icon)
1433 */
1434}
1435
1436wxImageList *wxFileIconsTable::GetSmallImageList()
1437{
1438 if (!m_smallImageList)
1439 Create();
1440
1441 return m_smallImageList;
1442}
1443
1904aa72 1444#if wxUSE_MIMETYPE && wxUSE_IMAGE
06cc1fb9
JS
1445// VS: we don't need this function w/o wxMimeTypesManager because we'll only have
1446// one icon and we won't resize it
1447
1448static wxBitmap CreateAntialiasedBitmap(const wxImage& img)
1449{
1d529ef7 1450 const unsigned int size = 16;
ca65c044 1451
1d529ef7 1452 wxImage smallimg (size, size);
06cc1fb9
JS
1453 unsigned char *p1, *p2, *ps;
1454 unsigned char mr = img.GetMaskRed(),
1455 mg = img.GetMaskGreen(),
1456 mb = img.GetMaskBlue();
1457
1458 unsigned x, y;
1459 unsigned sr, sg, sb, smask;
1460
1d529ef7 1461 p1 = img.GetData(), p2 = img.GetData() + 3 * size*2, ps = smallimg.GetData();
06cc1fb9
JS
1462 smallimg.SetMaskColour(mr, mr, mr);
1463
1d529ef7 1464 for (y = 0; y < size; y++)
06cc1fb9 1465 {
1d529ef7 1466 for (x = 0; x < size; x++)
06cc1fb9
JS
1467 {
1468 sr = sg = sb = smask = 0;
1469 if (p1[0] != mr || p1[1] != mg || p1[2] != mb)
1470 sr += p1[0], sg += p1[1], sb += p1[2];
1471 else smask++;
1472 p1 += 3;
1473 if (p1[0] != mr || p1[1] != mg || p1[2] != mb)
1474 sr += p1[0], sg += p1[1], sb += p1[2];
1475 else smask++;
1476 p1 += 3;
1477 if (p2[0] != mr || p2[1] != mg || p2[2] != mb)
1478 sr += p2[0], sg += p2[1], sb += p2[2];
1479 else smask++;
1480 p2 += 3;
1481 if (p2[0] != mr || p2[1] != mg || p2[2] != mb)
1482 sr += p2[0], sg += p2[1], sb += p2[2];
1483 else smask++;
1484 p2 += 3;
1485
1486 if (smask > 2)
1487 ps[0] = ps[1] = ps[2] = mr;
1488 else
8253f2e0
WS
1489 {
1490 ps[0] = (unsigned char)(sr >> 2);
1491 ps[1] = (unsigned char)(sg >> 2);
1492 ps[2] = (unsigned char)(sb >> 2);
1493 }
06cc1fb9
JS
1494 ps += 3;
1495 }
1d529ef7 1496 p1 += size*2 * 3, p2 += size*2 * 3;
06cc1fb9 1497 }
ca65c044 1498
06cc1fb9
JS
1499 return wxBitmap(smallimg);
1500}
1501
b126fe3a
DS
1502// This function is currently not unused anymore
1503#if 0
06cc1fb9
JS
1504// finds empty borders and return non-empty area of image:
1505static wxImage CutEmptyBorders(const wxImage& img)
1506{
1507 unsigned char mr = img.GetMaskRed(),
1508 mg = img.GetMaskGreen(),
1509 mb = img.GetMaskBlue();
1510 unsigned char *dt = img.GetData(), *dttmp;
1511 unsigned w = img.GetWidth(), h = img.GetHeight();
1512
1513 unsigned top, bottom, left, right, i;
1514 bool empt;
1515
1516#define MK_DTTMP(x,y) dttmp = dt + ((x + y * w) * 3)
ca65c044 1517#define NOEMPTY_PIX(empt) if (dttmp[0] != mr || dttmp[1] != mg || dttmp[2] != mb) {empt = false; break;}
06cc1fb9 1518
ca65c044 1519 for (empt = true, top = 0; empt && top < h; top++)
06cc1fb9
JS
1520 {
1521 MK_DTTMP(0, top);
1522 for (i = 0; i < w; i++, dttmp+=3)
1523 NOEMPTY_PIX(empt)
1524 }
ca65c044 1525 for (empt = true, bottom = h-1; empt && bottom > top; bottom--)
06cc1fb9
JS
1526 {
1527 MK_DTTMP(0, bottom);
1528 for (i = 0; i < w; i++, dttmp+=3)
1529 NOEMPTY_PIX(empt)
1530 }
ca65c044 1531 for (empt = true, left = 0; empt && left < w; left++)
06cc1fb9
JS
1532 {
1533 MK_DTTMP(left, 0);
1534 for (i = 0; i < h; i++, dttmp+=3*w)
1535 NOEMPTY_PIX(empt)
1536 }
ca65c044 1537 for (empt = true, right = w-1; empt && right > left; right--)
06cc1fb9
JS
1538 {
1539 MK_DTTMP(right, 0);
1540 for (i = 0; i < h; i++, dttmp+=3*w)
1541 NOEMPTY_PIX(empt)
1542 }
1543 top--, left--, bottom++, right++;
1544
1545 return img.GetSubImage(wxRect(left, top, right - left + 1, bottom - top + 1));
1546}
b126fe3a
DS
1547#endif // #if 0
1548
06cc1fb9
JS
1549#endif // wxUSE_MIMETYPE
1550
1551int wxFileIconsTable::GetIconID(const wxString& extension, const wxString& mime)
1552{
1553 if (!m_smallImageList)
1554 Create();
1555
1556#if wxUSE_MIMETYPE
44d60c0b 1557 if (!extension.empty())
06cc1fb9
JS
1558 {
1559 wxFileIconEntry *entry = (wxFileIconEntry*) m_HashTable->Get(extension);
1560 if (entry) return (entry -> id);
1561 }
1562
44d60c0b 1563 wxFileType *ft = (mime.empty()) ?
06cc1fb9
JS
1564 wxTheMimeTypesManager -> GetFileTypeFromExtension(extension) :
1565 wxTheMimeTypesManager -> GetFileTypeFromMimeType(mime);
55d0aaa3
VZ
1566
1567 wxIconLocation iconLoc;
06cc1fb9 1568 wxIcon ic;
02dd0487 1569
55d0aaa3 1570 {
02dd0487
JS
1571 wxLogNull logNull;
1572 if ( ft && ft->GetIcon(&iconLoc) )
1573 {
8e5c6521 1574 ic = wxIcon( iconLoc );
02dd0487 1575 }
55d0aaa3 1576 }
ca65c044 1577
55d0aaa3
VZ
1578 delete ft;
1579
1580 if ( !ic.Ok() )
06cc1fb9
JS
1581 {
1582 int newid = file;
1583 m_HashTable->Put(extension, new wxFileIconEntry(newid));
1584 return newid;
1585 }
1586
3fc93ebd
JS
1587 wxBitmap bmp;
1588 bmp.CopyFromIcon(ic);
06cc1fb9 1589
b6668c25
JS
1590 if ( !bmp.Ok() )
1591 {
1592 int newid = file;
1593 m_HashTable->Put(extension, new wxFileIconEntry(newid));
1594 return newid;
1595 }
1596
1d529ef7 1597 const unsigned int size = 16;
ca65c044 1598
06cc1fb9 1599 int id = m_smallImageList->GetImageCount();
1aa81b17 1600 if ((bmp.GetWidth() == (int) size) && (bmp.GetHeight() == (int) size))
1d529ef7 1601 {
3fc93ebd 1602 m_smallImageList->Add(bmp);
1d529ef7 1603 }
1904aa72 1604#if wxUSE_IMAGE
06cc1fb9
JS
1605 else
1606 {
3fc93ebd
JS
1607 wxImage img = bmp.ConvertToImage();
1608
1d529ef7
RR
1609 if ((img.GetWidth() != size*2) || (img.GetHeight() != size*2))
1610// m_smallImageList->Add(CreateAntialiasedBitmap(CutEmptyBorders(img).Rescale(size*2, size*2)));
1611 m_smallImageList->Add(CreateAntialiasedBitmap(img.Rescale(size*2, size*2)));
06cc1fb9
JS
1612 else
1613 m_smallImageList->Add(CreateAntialiasedBitmap(img));
1614 }
1904aa72
DS
1615#endif // wxUSE_IMAGE
1616
06cc1fb9
JS
1617 m_HashTable->Put(extension, new wxFileIconEntry(id));
1618 return id;
1619
1620#else // !wxUSE_MIMETYPE
1621
79e162f5 1622 wxUnusedVar(mime);
06cc1fb9
JS
1623 if (extension == wxT("exe"))
1624 return executable;
1625 else
1626 return file;
1627#endif // wxUSE_MIMETYPE/!wxUSE_MIMETYPE
1628}
1629
13de0c8c 1630#endif // wxUSE_DIRDLG || wxUSE_FILEDLG