]>
Commit | Line | Data |
---|---|---|
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 | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifdef __GNUG__ | |
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 | ||
1e6feb95 | 23 | #if wxUSE_DIRDLG |
51a58d8b JS |
24 | |
25 | #include "wx/utils.h" | |
26 | #include "wx/dialog.h" | |
27 | #include "wx/button.h" | |
28 | #include "wx/layout.h" | |
29 | #include "wx/msgdlg.h" | |
618a5e38 | 30 | #include "wx/textctrl.h" |
51a58d8b JS |
31 | #include "wx/textdlg.h" |
32 | #include "wx/filefn.h" | |
33 | #include "wx/cmndata.h" | |
34 | #include "wx/gdicmn.h" | |
35 | #include "wx/intl.h" | |
36 | #include "wx/imaglist.h" | |
37 | #include "wx/icon.h" | |
38 | #include "wx/log.h" | |
39 | #include "wx/sizer.h" | |
40 | #include "wx/tokenzr.h" | |
41 | #include "wx/dir.h" | |
3fa4bd0e | 42 | #include "wx/settings.h" |
51a58d8b JS |
43 | |
44 | #if wxUSE_STATLINE | |
45 | #include "wx/statline.h" | |
46 | #endif | |
47 | ||
48 | #include "wx/generic/dirctrlg.h" | |
49 | ||
76a5e5d2 SC |
50 | #if defined(__WXMAC__) |
51 | #include "wx/mac/private.h" // includes mac headers | |
52 | #endif | |
53 | ||
51a58d8b | 54 | #ifdef __WXMSW__ |
2736b3ce | 55 | #include <windows.h> |
7328394a | 56 | |
c9ecda60 VS |
57 | // FIXME - Mingw32 1.0 has both _getdrive() and _chdrive(). For now, let's assume |
58 | // older releases don't, but it should be verified and the checks modified | |
59 | // accordingly. | |
60 | #if !defined(__GNUWIN32__) || \ | |
61 | (defined(__MINGW32_MAJOR_VERSION) && __MINGW32_MAJOR_VERSION >= 1) | |
62 | #include <direct.h> | |
63 | #include <stdlib.h> | |
64 | #include <ctype.h> | |
51a58d8b JS |
65 | #endif |
66 | ||
7328394a JS |
67 | #endif |
68 | ||
ec1b28a3 DW |
69 | #ifdef __WXPM__ |
70 | ||
71 | #define INCL_BASE | |
72 | #include <os2.h> | |
73 | #include <direct.h> | |
74 | #include <stdlib.h> | |
75 | #include <ctype.h> | |
76 | ||
77 | #endif // __WXPM__ | |
78 | ||
f4ac0693 GD |
79 | #if defined(__WXMAC__) |
80 | # include "MoreFilesExtras.h" | |
bedaf53e SC |
81 | #endif |
82 | ||
267a7108 JS |
83 | #ifdef __BORLANDC__ |
84 | #include "dos.h" | |
85 | #endif | |
86 | ||
51a58d8b JS |
87 | // If compiled under Windows, this macro can cause problems |
88 | #ifdef GetFirstChild | |
89 | #undef GetFirstChild | |
90 | #endif | |
91 | ||
51a58d8b | 92 | /* Closed folder */ |
90350682 | 93 | static const char * icon1_xpm[] = { |
51a58d8b JS |
94 | /* width height ncolors chars_per_pixel */ |
95 | "16 16 6 1", | |
96 | /* colors */ | |
97 | " s None c None", | |
98 | ". c #000000", | |
99 | "+ c #c0c0c0", | |
100 | "@ c #808080", | |
101 | "# c #ffff00", | |
102 | "$ c #ffffff", | |
103 | /* pixels */ | |
104 | " ", | |
105 | " @@@@@ ", | |
106 | " @#+#+#@ ", | |
107 | " @#+#+#+#@@@@@@ ", | |
108 | " @$$$$$$$$$$$$@.", | |
109 | " @$#+#+#+#+#+#@.", | |
110 | " @$+#+#+#+#+#+@.", | |
111 | " @$#+#+#+#+#+#@.", | |
112 | " @$+#+#+#+#+#+@.", | |
113 | " @$#+#+#+#+#+#@.", | |
114 | " @$+#+#+#+#+#+@.", | |
115 | " @$#+#+#+#+#+#@.", | |
116 | " @@@@@@@@@@@@@@.", | |
117 | " ..............", | |
118 | " ", | |
119 | " "}; | |
120 | ||
121 | /* Open folder */ | |
90350682 | 122 | static const char * icon2_xpm[] = { |
51a58d8b JS |
123 | /* width height ncolors chars_per_pixel */ |
124 | "16 16 6 1", | |
125 | /* colors */ | |
126 | " s None c None", | |
127 | ". c #000000", | |
128 | "+ c #c0c0c0", | |
129 | "@ c #808080", | |
130 | "# c #ffff00", | |
131 | "$ c #ffffff", | |
132 | /* pixels */ | |
133 | " ", | |
134 | " @@@@@ ", | |
135 | " @$$$$$@ ", | |
136 | " @$#+#+#$@@@@@@ ", | |
137 | " @$+#+#+$$$$$$@.", | |
138 | " @$#+#+#+#+#+#@.", | |
139 | "@@@@@@@@@@@@@#@.", | |
140 | "@$$$$$$$$$$@@+@.", | |
141 | "@$#+#+#+#+##.@@.", | |
142 | " @$#+#+#+#+#+.@.", | |
143 | " @$+#+#+#+#+#.@.", | |
144 | " @$+#+#+#+##@..", | |
145 | " @@@@@@@@@@@@@.", | |
146 | " .............", | |
147 | " ", | |
148 | " "}; | |
149 | ||
150 | /* File */ | |
90350682 | 151 | static const char * icon3_xpm[] = { |
51a58d8b JS |
152 | /* width height ncolors chars_per_pixel */ |
153 | "16 16 3 1", | |
154 | /* colors */ | |
33fed835 MB |
155 | " s None c None", |
156 | ". c #000000", | |
157 | "+ c #ffffff", | |
51a58d8b JS |
158 | /* pixels */ |
159 | " ", | |
160 | " ........ ", | |
161 | " .++++++.. ", | |
162 | " .+.+.++.+. ", | |
163 | " .++++++.... ", | |
164 | " .+.+.+++++. ", | |
165 | " .+++++++++. ", | |
166 | " .+.+.+.+.+. ", | |
167 | " .+++++++++. ", | |
168 | " .+.+.+.+.+. ", | |
169 | " .+++++++++. ", | |
170 | " .+.+.+.+.+. ", | |
171 | " .+++++++++. ", | |
172 | " ........... ", | |
173 | " ", | |
174 | " "}; | |
175 | ||
176 | /* Computer */ | |
90350682 | 177 | static const char * icon4_xpm[] = { |
51a58d8b | 178 | "16 16 7 1", |
33fed835 MB |
179 | " s None c None", |
180 | ". c #808080", | |
181 | "X c #c0c0c0", | |
182 | "o c Black", | |
183 | "O c Gray100", | |
184 | "+ c #008080", | |
185 | "@ c Blue", | |
51a58d8b JS |
186 | " ........... ", |
187 | " .XXXXXXXXXX.o", | |
188 | " .OOOOOOOOO..o", | |
189 | " .OoooooooX..o", | |
190 | " .Oo+...@+X..o", | |
191 | " .Oo+XXX.+X..o", | |
192 | " .Oo+....+X..o", | |
193 | " .Oo++++++X..o", | |
194 | " .OXXXXXXXX.oo", | |
195 | " ..........o.o", | |
196 | " ...........Xo", | |
197 | " .XXXXXXXXXX.o", | |
198 | " .o.o.o.o.o...o", | |
199 | " .oXoXoXoXoXo.o ", | |
200 | ".XOXXXXXXXXX.o ", | |
201 | "............o "}; | |
202 | ||
203 | /* Drive */ | |
90350682 | 204 | static const char * icon5_xpm[] = { |
51a58d8b | 205 | "16 16 7 1", |
33fed835 MB |
206 | " s None c None", |
207 | ". c #808080", | |
208 | "X c #c0c0c0", | |
209 | "o c Black", | |
210 | "O c Gray100", | |
211 | "+ c Green", | |
212 | "@ c #008000", | |
51a58d8b JS |
213 | " ", |
214 | " ", | |
215 | " ", | |
216 | " ", | |
217 | " ............. ", | |
218 | " .XXXXXXXXXXXX.o", | |
219 | ".OOOOOOOOOOOO..o", | |
220 | ".XXXXXXXXX+@X..o", | |
221 | ".XXXXXXXXXXXX..o", | |
222 | ".X..........X..o", | |
223 | ".XOOOOOOOOOOX..o", | |
224 | "..............o ", | |
225 | " ooooooooooooo ", | |
226 | " ", | |
227 | " ", | |
228 | " "}; | |
229 | ||
230 | /* CD-ROM */ | |
90350682 | 231 | static const char *icon6_xpm[] = { |
51a58d8b | 232 | "16 16 10 1", |
33fed835 MB |
233 | " s None c None", |
234 | ". c #808080", | |
235 | "X c #c0c0c0", | |
236 | "o c Yellow", | |
237 | "O c Blue", | |
238 | "+ c Black", | |
239 | "@ c Gray100", | |
240 | "# c #008080", | |
241 | "$ c Green", | |
242 | "% c #008000", | |
51a58d8b JS |
243 | " ... ", |
244 | " ..XoX.. ", | |
245 | " .O.XoXXX+ ", | |
246 | " ...O.oXXXX+ ", | |
247 | " .O..X.XXXX+ ", | |
248 | " ....X.+..XXX+", | |
249 | " .XXX.+@+.XXX+", | |
250 | " .X@XX.+.X@@X+", | |
251 | " .....X...#XX@+ ", | |
252 | ".@@@...XXo.O@X+ ", | |
253 | ".@XXX..XXoXOO+ ", | |
254 | ".@++++..XoX+++ ", | |
255 | ".@$%@@XX+++X.+ ", | |
256 | ".............+ ", | |
257 | " ++++++++++++ ", | |
258 | " "}; | |
259 | ||
260 | /* Floppy */ | |
90350682 | 261 | static const char * icon7_xpm[] = { |
51a58d8b | 262 | "16 16 7 1", |
33fed835 MB |
263 | " s None c None", |
264 | ". c #808080", | |
265 | "X c Gray100", | |
266 | "o c #c0c0c0", | |
267 | "O c Black", | |
268 | "+ c Cyan", | |
269 | "@ c Red", | |
51a58d8b JS |
270 | " ......X", |
271 | " .ooooooO", | |
272 | " .+++++OO", | |
273 | " .++++++O", | |
274 | " .++++++O", | |
275 | " .ooooooO", | |
276 | " .......o....oO", | |
277 | " .oooooo.o.O.XoO", | |
278 | ".XXXXXXXXOOOOOO ", | |
279 | ".ooooooooo@o..O ", | |
280 | ".ooo....oooo..O ", | |
281 | ".o..OOOO...o..O ", | |
282 | ".oooXXXXoooo..O ", | |
283 | ".............O ", | |
284 | " OOOOOOOOOOOO ", | |
285 | " "}; | |
286 | ||
287 | /* Removeable */ | |
90350682 | 288 | static const char * icon8_xpm[] = { |
51a58d8b | 289 | "16 16 7 1", |
33fed835 MB |
290 | " s None c None", |
291 | ". c #808080", | |
292 | "X c #c0c0c0", | |
293 | "o c Black", | |
294 | "O c Gray100", | |
295 | "+ c Red", | |
296 | "@ c #800000", | |
51a58d8b JS |
297 | " ", |
298 | " ", | |
299 | " ", | |
300 | " ............. ", | |
301 | " .XXXXXXXXXXXX.o", | |
302 | ".OOOOOOOOOOOO..o", | |
303 | ".OXXXXXXXXXXX..o", | |
304 | ".O+@.oooooo.X..o", | |
305 | ".OXXOooooooOX..o", | |
306 | ".OXXXOOOOOOXX..o", | |
307 | ".OXXXXXXXXXXX..o", | |
308 | ".O............o ", | |
309 | " ooooooooooooo ", | |
310 | " ", | |
311 | " ", | |
312 | " "}; | |
51a58d8b | 313 | |
748fcded VS |
314 | |
315 | #define wxID_TREECTRL 7000 | |
316 | #define wxID_FILTERLISTCTRL 7001 | |
317 | ||
318 | #if defined(__DOS__) | |
db5333a5 | 319 | |
37fd1c97 | 320 | bool wxIsDriveAvailable(const wxString& dirName) |
db5333a5 | 321 | { |
6157794c VS |
322 | // FIXME_MGL - this method leads to hang up under Watcom for some reason |
323 | #ifndef __WATCOMC__ | |
db5333a5 VS |
324 | if ( dirName.Len() == 3 && dirName[1u] == wxT(':') ) |
325 | { | |
326 | wxString dirNameLower(dirName.Lower()); | |
327 | // VS: always return TRUE for removable media, since Win95 doesn't | |
328 | // like it when MS-DOS app accesses empty floppy drive | |
329 | return (dirNameLower[0u] == wxT('a') || | |
330 | dirNameLower[0u] == wxT('b') || | |
331 | wxPathExists(dirNameLower)); | |
332 | } | |
333 | else | |
6157794c | 334 | #endif |
db5333a5 VS |
335 | return TRUE; |
336 | } | |
337 | ||
338 | #elif defined(__WINDOWS__) || defined(__WXPM__) | |
339 | ||
33ac7e6f KB |
340 | int setdrive(int drive) |
341 | { | |
c9ecda60 VS |
342 | #if defined(__GNUWIN32__) && \ |
343 | (defined(__MINGW32_MAJOR_VERSION) && __MINGW32_MAJOR_VERSION >= 1) | |
344 | return _chdrive(drive); | |
345 | #else | |
e987a489 | 346 | wxChar newdrive[3]; |
33ac7e6f KB |
347 | |
348 | if (drive < 1 || drive > 31) | |
349 | return -1; | |
e987a489 VS |
350 | newdrive[0] = (wxChar)(wxT('A') + drive - 1); |
351 | newdrive[1] = wxT(':'); | |
352 | newdrive[2] = wxT('\0'); | |
50c76ce1 | 353 | #if defined(__WXMSW__) |
788722ac JS |
354 | #ifdef __WIN16__ |
355 | if (wxSetWorkingDirectory(newdrive)) | |
356 | #else | |
e987a489 | 357 | if (::SetCurrentDirectory(newdrive)) |
788722ac | 358 | #endif |
50c76ce1 DW |
359 | #else |
360 | // VA doesn't know what LPSTR is and has its own set | |
361 | if (DosSetCurrentDir((PSZ)newdrive)) | |
362 | #endif | |
33ac7e6f KB |
363 | return 0; |
364 | else | |
365 | return -1; | |
c9ecda60 | 366 | #endif // !GNUWIN32 |
33ac7e6f KB |
367 | } |
368 | ||
37fd1c97 | 369 | bool wxIsDriveAvailable(const wxString& dirName) |
ad9cd15c JS |
370 | { |
371 | #ifdef __WIN32__ | |
33fed835 | 372 | UINT errorMode = SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX); |
ad9cd15c | 373 | #endif |
33fed835 | 374 | bool success = TRUE; |
ad9cd15c JS |
375 | |
376 | // Check if this is a root directory and if so, | |
377 | // whether the drive is avaiable. | |
33fed835 | 378 | if (dirName.Len() == 3 && dirName[(size_t)1] == wxT(':')) |
ad9cd15c | 379 | { |
33fed835 | 380 | wxString dirNameLower(dirName.Lower()); |
c9ecda60 VS |
381 | #if defined(__GNUWIN32__) && \ |
382 | !(defined(__MINGW32_MAJOR_VERSION) && __MINGW32_MAJOR_VERSION >= 1) | |
7328394a JS |
383 | success = wxPathExists(dirNameLower); |
384 | #else | |
ad9cd15c | 385 | int currentDrive = _getdrive(); |
33fed835 | 386 | int thisDrive = (int) (dirNameLower[(size_t)0] - 'a' + 1) ; |
33ac7e6f KB |
387 | int err = setdrive( thisDrive ) ; |
388 | setdrive( currentDrive ); | |
ad9cd15c JS |
389 | |
390 | if (err == -1) | |
391 | { | |
392 | success = FALSE; | |
393 | } | |
7328394a | 394 | #endif |
ad9cd15c JS |
395 | } |
396 | #ifdef __WIN32__ | |
33fed835 | 397 | (void) SetErrorMode(errorMode); |
ad9cd15c JS |
398 | #endif |
399 | ||
33fed835 | 400 | return success; |
ad9cd15c | 401 | } |
db5333a5 | 402 | #endif // __WINDOWS__ || __WXPM__ |
ad9cd15c | 403 | |
57e26a09 JS |
404 | // Function which is called by quick sort. We want to override the default wxArrayString behaviour, |
405 | // and sort regardless of case. | |
406 | static int LINKAGEMODE wxDirCtrlStringCompareFunction(const void *first, const void *second) | |
407 | { | |
408 | wxString *strFirst = (wxString *)first; | |
409 | wxString *strSecond = (wxString *)second; | |
410 | ||
411 | return strFirst->CmpNoCase(*strSecond); | |
412 | } | |
413 | ||
51a58d8b | 414 | //----------------------------------------------------------------------------- |
748fcded | 415 | // wxDirItemData |
51a58d8b JS |
416 | //----------------------------------------------------------------------------- |
417 | ||
748fcded VS |
418 | wxDirItemData::wxDirItemData(const wxString& path, const wxString& name, |
419 | bool isDir) | |
51a58d8b JS |
420 | { |
421 | m_path = path; | |
422 | m_name = name; | |
423 | /* Insert logic to detect hidden files here | |
424 | * In UnixLand we just check whether the first char is a dot | |
425 | * For FileNameFromPath read LastDirNameInThisPath ;-) */ | |
426 | // m_isHidden = (bool)(wxFileNameFromPath(*m_path)[0] == '.'); | |
427 | m_isHidden = FALSE; | |
51a58d8b JS |
428 | m_isExpanded = FALSE; |
429 | m_isDir = isDir; | |
430 | } | |
431 | ||
748fcded | 432 | wxDirItemData::~wxDirItemData() |
51a58d8b JS |
433 | { |
434 | } | |
435 | ||
748fcded | 436 | void wxDirItemData::SetNewDirName(const wxString& path) |
51a58d8b JS |
437 | { |
438 | m_path = path; | |
748fcded VS |
439 | m_name = wxFileNameFromPath(path); |
440 | } | |
441 | ||
442 | bool wxDirItemData::HasSubDirs() const | |
443 | { | |
444 | if (m_path.IsEmpty()) | |
445 | return FALSE; | |
446 | ||
447 | wxDir dir; | |
448 | { | |
449 | wxLogNull nolog; | |
450 | if ( !dir.Open(m_path) ) | |
451 | return FALSE; | |
452 | } | |
453 | ||
454 | return dir.HasSubDirs(); | |
455 | } | |
456 | ||
457 | bool wxDirItemData::HasFiles(const wxString& spec) const | |
458 | { | |
459 | if (m_path.IsEmpty()) | |
460 | return FALSE; | |
461 | ||
462 | wxDir dir; | |
463 | { | |
464 | wxLogNull nolog; | |
465 | if ( !dir.Open(m_path) ) | |
466 | return FALSE; | |
467 | } | |
468 | ||
469 | return dir.HasFiles(); | |
51a58d8b JS |
470 | } |
471 | ||
51a58d8b JS |
472 | //----------------------------------------------------------------------------- |
473 | // wxGenericDirCtrl | |
474 | //----------------------------------------------------------------------------- | |
475 | ||
476 | IMPLEMENT_DYNAMIC_CLASS(wxGenericDirCtrl, wxControl) | |
477 | ||
478 | BEGIN_EVENT_TABLE(wxGenericDirCtrl, wxControl) | |
479 | EVT_TREE_ITEM_EXPANDING (-1, wxGenericDirCtrl::OnExpandItem) | |
480 | EVT_TREE_ITEM_COLLAPSED (-1, wxGenericDirCtrl::OnCollapseItem) | |
481 | EVT_TREE_BEGIN_LABEL_EDIT (-1, wxGenericDirCtrl::OnBeginEditItem) | |
482 | EVT_TREE_END_LABEL_EDIT (-1, wxGenericDirCtrl::OnEndEditItem) | |
483 | EVT_SIZE (wxGenericDirCtrl::OnSize) | |
484 | END_EVENT_TABLE() | |
485 | ||
486 | wxGenericDirCtrl::wxGenericDirCtrl(void) | |
487 | { | |
488 | Init(); | |
489 | } | |
490 | ||
491 | bool wxGenericDirCtrl::Create(wxWindow *parent, | |
748fcded VS |
492 | const wxWindowID id, |
493 | const wxString& dir, | |
494 | const wxPoint& pos, | |
495 | const wxSize& size, | |
496 | long style, | |
497 | const wxString& filter, | |
498 | int defaultFilter, | |
499 | const wxString& name) | |
51a58d8b JS |
500 | { |
501 | if (!wxControl::Create(parent, id, pos, size, style, wxDefaultValidator, name)) | |
502 | return FALSE; | |
503 | ||
db5333a5 | 504 | SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE)); |
51a58d8b JS |
505 | |
506 | Init(); | |
507 | ||
748fcded | 508 | long treeStyle = wxTR_HAS_BUTTONS | wxTR_EDIT_LABELS | wxTR_HIDE_ROOT; |
51a58d8b JS |
509 | if ((style & wxDIRCTRL_3D_INTERNAL) == 0) |
510 | treeStyle |= wxNO_BORDER; | |
511 | ||
512 | long filterStyle = 0; | |
513 | if ((style & wxDIRCTRL_3D_INTERNAL) == 0) | |
514 | filterStyle |= wxNO_BORDER; | |
515 | ||
516 | m_treeCtrl = new wxTreeCtrl(this, wxID_TREECTRL, pos, size, treeStyle); | |
517 | ||
518 | if (!filter.IsEmpty() && (style & wxDIRCTRL_SHOW_FILTERS)) | |
519 | m_filterListCtrl = new wxDirFilterListCtrl(this, wxID_FILTERLISTCTRL, wxDefaultPosition, wxDefaultSize, filterStyle); | |
520 | ||
521 | m_defaultPath = dir; | |
522 | m_filter = filter; | |
523 | ||
524 | SetFilterIndex(defaultFilter); | |
525 | ||
526 | if (m_filterListCtrl) | |
527 | m_filterListCtrl->FillFilterList(filter, defaultFilter); | |
528 | ||
529 | m_imageList = new wxImageList(16, 16, TRUE); | |
51a58d8b JS |
530 | m_imageList->Add(wxIcon(icon1_xpm)); |
531 | m_imageList->Add(wxIcon(icon2_xpm)); | |
532 | m_imageList->Add(wxIcon(icon3_xpm)); | |
533 | m_imageList->Add(wxIcon(icon4_xpm)); | |
534 | m_imageList->Add(wxIcon(icon5_xpm)); | |
535 | m_imageList->Add(wxIcon(icon6_xpm)); | |
536 | m_imageList->Add(wxIcon(icon7_xpm)); | |
537 | m_imageList->Add(wxIcon(icon8_xpm)); | |
748fcded | 538 | m_treeCtrl->AssignImageList(m_imageList); |
51a58d8b JS |
539 | |
540 | m_showHidden = FALSE; | |
748fcded | 541 | wxDirItemData* rootData = new wxDirItemData(wxT(""), wxT(""), TRUE); |
51a58d8b JS |
542 | |
543 | wxString rootName; | |
544 | ||
db5333a5 | 545 | #if defined(__WINDOWS__) || defined(__WXPM__) || defined(__DOS__) |
51a58d8b JS |
546 | rootName = _("Computer"); |
547 | #else | |
548 | rootName = _("Sections"); | |
549 | #endif | |
550 | ||
551 | m_rootId = m_treeCtrl->AddRoot( rootName, 3, -1, rootData); | |
552 | m_treeCtrl->SetItemHasChildren(m_rootId); | |
553 | m_treeCtrl->Expand(m_rootId); // automatically expand first level | |
554 | ||
555 | // Expand and select the default path | |
556 | if (!m_defaultPath.IsEmpty()) | |
557 | ExpandPath(m_defaultPath); | |
558 | ||
559 | DoResize(); | |
560 | ||
561 | return TRUE; | |
562 | } | |
563 | ||
564 | wxGenericDirCtrl::~wxGenericDirCtrl() | |
565 | { | |
51a58d8b JS |
566 | } |
567 | ||
568 | void wxGenericDirCtrl::Init() | |
569 | { | |
570 | m_showHidden = FALSE; | |
571 | m_imageList = NULL; | |
51a58d8b JS |
572 | m_currentFilter = 0; |
573 | m_currentFilterStr = wxEmptyString; // Default: any file | |
574 | m_treeCtrl = NULL; | |
575 | m_filterListCtrl = NULL; | |
576 | } | |
577 | ||
42dcacf0 RR |
578 | void wxGenericDirCtrl::ShowHidden( bool show ) |
579 | { | |
580 | m_showHidden = show; | |
581 | ||
582 | // reparse FIXME | |
583 | } | |
584 | ||
51a58d8b JS |
585 | void wxGenericDirCtrl::AddSection(const wxString& path, const wxString& name, int imageId) |
586 | { | |
748fcded | 587 | wxDirItemData *dir_item = new wxDirItemData(path,name,TRUE); |
51a58d8b | 588 | |
51a58d8b | 589 | wxTreeItemId id = m_treeCtrl->AppendItem( m_rootId, name, imageId, -1, dir_item); |
51a58d8b JS |
590 | |
591 | m_treeCtrl->SetItemHasChildren(id); | |
592 | } | |
593 | ||
594 | void wxGenericDirCtrl::SetupSections() | |
595 | { | |
db5333a5 | 596 | #if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXPM__) |
51a58d8b | 597 | |
748fcded | 598 | #ifdef __WIN32__ |
51a58d8b JS |
599 | wxChar driveBuffer[256]; |
600 | size_t n = (size_t) GetLogicalDriveStrings(255, driveBuffer); | |
601 | size_t i = 0; | |
602 | while (i < n) | |
603 | { | |
604 | wxString path, name; | |
605 | path.Printf(wxT("%c:\\"), driveBuffer[i]); | |
606 | name.Printf(wxT("(%c:)"), driveBuffer[i]); | |
607 | ||
608 | int imageId = 4; | |
609 | int driveType = ::GetDriveType(path); | |
610 | switch (driveType) | |
611 | { | |
612 | case DRIVE_REMOVABLE: | |
613 | if (path == wxT("a:\\") || path == wxT("b:\\")) | |
614 | imageId = 6; // Floppy | |
615 | else | |
616 | imageId = 7; | |
617 | break; | |
618 | case DRIVE_FIXED: | |
619 | imageId = 4; | |
620 | break; | |
621 | case DRIVE_REMOTE: | |
622 | imageId = 4; | |
623 | break; | |
624 | case DRIVE_CDROM: | |
625 | imageId = 5; | |
626 | break; | |
627 | default: | |
628 | imageId = 4; | |
629 | break; | |
630 | } | |
631 | ||
632 | AddSection(path, name, imageId); | |
633 | ||
634 | while (driveBuffer[i] != wxT('\0')) | |
635 | i ++; | |
636 | i ++; | |
637 | if (driveBuffer[i] == wxT('\0')) | |
638 | break; | |
639 | } | |
db5333a5 | 640 | #else // !__WIN32__ |
51a58d8b | 641 | int drive; |
51a58d8b | 642 | |
51a58d8b JS |
643 | /* If we can switch to the drive, it exists. */ |
644 | for( drive = 1; drive <= 26; drive++ ) | |
645 | { | |
646 | wxString path, name; | |
647 | path.Printf(wxT("%c:\\"), (char) (drive + 'a' - 1)); | |
db5333a5 | 648 | name.Printf(wxT("(%c:)"), (char) (drive + 'A' - 1)); |
51a58d8b | 649 | |
33fed835 | 650 | if (wxIsDriveAvailable(path)) |
51a58d8b | 651 | { |
db5333a5 | 652 | AddSection(path, name, (drive <= 2) ? 6/*floppy*/ : 4/*disk*/); |
51a58d8b JS |
653 | } |
654 | } | |
db5333a5 VS |
655 | #endif // __WIN32__/!__WIN32__ |
656 | ||
f4ac0693 | 657 | #elif defined(__WXMAC__) |
bedaf53e SC |
658 | FSSpec volume ; |
659 | short index = 1 ; | |
660 | while(1) { | |
661 | short actualCount = 0 ; | |
662 | if ( OnLine( &volume , 1 , &actualCount , &index ) != noErr || actualCount == 0 ) | |
663 | break ; | |
664 | ||
665 | wxString name = wxMacFSSpec2MacFilename( &volume ) ; | |
a044cb72 | 666 | AddSection(name + wxFILE_SEP_PATH, name, 0); |
bedaf53e | 667 | } |
db5333a5 | 668 | #elif defined(__UNIX__) |
748fcded | 669 | AddSection(wxT("/"), wxT("/"), 3/*computer icon*/); |
db5333a5 VS |
670 | #else |
671 | #error "Unsupported platform in wxGenericDirCtrl!" | |
51a58d8b JS |
672 | #endif |
673 | } | |
674 | ||
675 | void wxGenericDirCtrl::OnBeginEditItem(wxTreeEvent &event) | |
676 | { | |
677 | // don't rename the main entry "Sections" | |
678 | if (event.GetItem() == m_rootId) | |
679 | { | |
680 | event.Veto(); | |
681 | return; | |
682 | } | |
683 | ||
684 | // don't rename the individual sections | |
685 | if (m_treeCtrl->GetParent( event.GetItem() ) == m_rootId) | |
686 | { | |
687 | event.Veto(); | |
688 | return; | |
689 | } | |
690 | } | |
691 | ||
692 | void wxGenericDirCtrl::OnEndEditItem(wxTreeEvent &event) | |
693 | { | |
694 | if ((event.GetLabel().IsEmpty()) || | |
695 | (event.GetLabel() == _(".")) || | |
696 | (event.GetLabel() == _("..")) || | |
697 | (event.GetLabel().First( wxT("/") ) != wxNOT_FOUND)) | |
698 | { | |
699 | wxMessageDialog dialog(this, _("Illegal directory name."), _("Error"), wxOK | wxICON_ERROR ); | |
700 | dialog.ShowModal(); | |
701 | event.Veto(); | |
702 | return; | |
703 | } | |
704 | ||
705 | wxTreeItemId id = event.GetItem(); | |
748fcded | 706 | wxDirItemData *data = (wxDirItemData*)m_treeCtrl->GetItemData( id ); |
51a58d8b JS |
707 | wxASSERT( data ); |
708 | ||
709 | wxString new_name( wxPathOnly( data->m_path ) ); | |
ad9cd15c | 710 | new_name += wxString(wxFILE_SEP_PATH); |
51a58d8b JS |
711 | new_name += event.GetLabel(); |
712 | ||
713 | wxLogNull log; | |
714 | ||
715 | if (wxFileExists(new_name)) | |
716 | { | |
717 | wxMessageDialog dialog(this, _("File name exists already."), _("Error"), wxOK | wxICON_ERROR ); | |
718 | dialog.ShowModal(); | |
719 | event.Veto(); | |
720 | } | |
721 | ||
722 | if (wxRenameFile(data->m_path,new_name)) | |
723 | { | |
724 | data->SetNewDirName( new_name ); | |
725 | } | |
726 | else | |
727 | { | |
728 | wxMessageDialog dialog(this, _("Operation not permitted."), _("Error"), wxOK | wxICON_ERROR ); | |
729 | dialog.ShowModal(); | |
730 | event.Veto(); | |
731 | } | |
732 | } | |
733 | ||
734 | void wxGenericDirCtrl::OnExpandItem(wxTreeEvent &event) | |
735 | { | |
736 | wxTreeItemId parentId = event.GetItem(); | |
737 | ||
748fcded VS |
738 | // VS: this is needed because the event handler is called from wxTreeCtrl |
739 | // ctor when wxTR_HIDE_ROOT was specified | |
740 | if (m_rootId == 0) | |
741 | m_rootId = m_treeCtrl->GetRootItem(); | |
742 | ||
51a58d8b JS |
743 | ExpandDir(parentId); |
744 | } | |
745 | ||
746 | void wxGenericDirCtrl::OnCollapseItem(wxTreeEvent &event ) | |
747 | { | |
748 | wxTreeItemId child, parent = event.GetItem(); | |
749 | ||
748fcded | 750 | wxDirItemData *data = (wxDirItemData *) m_treeCtrl->GetItemData(event.GetItem()); |
51a58d8b JS |
751 | if (!data->m_isExpanded) |
752 | return; | |
753 | ||
754 | data->m_isExpanded = FALSE; | |
755 | long cookie; | |
756 | /* Workaround because DeleteChildren has disapeared (why?) and | |
757 | * CollapseAndReset doesn't work as advertised (deletes parent too) */ | |
758 | child = m_treeCtrl->GetFirstChild(parent, cookie); | |
759 | while (child.IsOk()) | |
760 | { | |
761 | m_treeCtrl->Delete(child); | |
762 | /* Not GetNextChild below, because the cookie mechanism can't | |
763 | * handle disappearing children! */ | |
764 | child = m_treeCtrl->GetFirstChild(parent, cookie); | |
765 | } | |
766 | } | |
767 | ||
768 | void wxGenericDirCtrl::ExpandDir(wxTreeItemId parentId) | |
769 | { | |
748fcded | 770 | wxDirItemData *data = (wxDirItemData *) m_treeCtrl->GetItemData(parentId); |
51a58d8b JS |
771 | |
772 | if (data->m_isExpanded) | |
773 | return; | |
774 | ||
775 | data->m_isExpanded = TRUE; | |
776 | ||
748fcded | 777 | if (parentId == m_treeCtrl->GetRootItem()) |
51a58d8b JS |
778 | { |
779 | SetupSections(); | |
780 | return; | |
781 | } | |
782 | ||
783 | wxASSERT(data); | |
784 | ||
785 | wxString search,path,filename; | |
786 | ||
787 | wxString dirName(data->m_path); | |
788 | ||
db5333a5 | 789 | #if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXPM__) |
51a58d8b JS |
790 | // Check if this is a root directory and if so, |
791 | // whether the drive is avaiable. | |
33fed835 MB |
792 | if (!wxIsDriveAvailable(dirName)) |
793 | { | |
ad9cd15c | 794 | data->m_isExpanded = FALSE; |
7328394a JS |
795 | //wxMessageBox(wxT("Sorry, this drive is not available.")); |
796 | return; | |
33fed835 | 797 | } |
51a58d8b JS |
798 | #endif |
799 | ||
800 | // This may take a longish time. Go to busy cursor | |
801 | wxBusyCursor busy; | |
802 | ||
db5333a5 | 803 | #if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXPM__) |
51a58d8b JS |
804 | if (dirName.Last() == ':') |
805 | dirName += wxString(wxFILE_SEP_PATH); | |
806 | #endif | |
807 | ||
808 | wxArrayString dirs; | |
809 | wxArrayString filenames; | |
810 | ||
811 | wxDir d; | |
812 | wxString eachFilename; | |
813 | ||
f9c165b1 | 814 | wxLogNull log; |
51a58d8b JS |
815 | d.Open(dirName); |
816 | ||
817 | if (d.IsOpened()) | |
818 | { | |
42dcacf0 RR |
819 | int style = wxDIR_DIRS; |
820 | if (m_showHidden) style |= wxDIR_HIDDEN; | |
821 | if (d.GetFirst(& eachFilename, wxEmptyString, style)) | |
51a58d8b JS |
822 | { |
823 | do | |
824 | { | |
825 | if ((eachFilename != wxT(".")) && (eachFilename != wxT(".."))) | |
826 | { | |
827 | dirs.Add(eachFilename); | |
828 | } | |
829 | } | |
748fcded | 830 | while (d.GetNext(& eachFilename)); |
51a58d8b JS |
831 | } |
832 | } | |
57e26a09 | 833 | dirs.Sort((wxArrayString::CompareFunction) wxDirCtrlStringCompareFunction); |
51a58d8b JS |
834 | |
835 | // Now do the filenames -- but only if we're allowed to | |
836 | if ((GetWindowStyle() & wxDIRCTRL_DIR_ONLY) == 0) | |
837 | { | |
f9c165b1 JS |
838 | wxLogNull log; |
839 | ||
51a58d8b | 840 | d.Open(dirName); |
ec1b28a3 | 841 | |
51a58d8b JS |
842 | if (d.IsOpened()) |
843 | { | |
844 | if (d.GetFirst(& eachFilename, m_currentFilterStr, wxDIR_FILES)) | |
845 | { | |
846 | do | |
847 | { | |
848 | if ((eachFilename != wxT(".")) && (eachFilename != wxT(".."))) | |
849 | { | |
850 | filenames.Add(eachFilename); | |
851 | } | |
852 | } | |
748fcded | 853 | while (d.GetNext(& eachFilename)); |
51a58d8b JS |
854 | } |
855 | } | |
57e26a09 | 856 | filenames.Sort((wxArrayString::CompareFunction) wxDirCtrlStringCompareFunction); |
51a58d8b JS |
857 | } |
858 | ||
859 | // Add the sorted dirs | |
860 | size_t i; | |
861 | for (i = 0; i < dirs.Count(); i++) | |
862 | { | |
863 | wxString eachFilename(dirs[i]); | |
864 | path = dirName; | |
865 | if (path.Last() != wxFILE_SEP_PATH) | |
866 | path += wxString(wxFILE_SEP_PATH); | |
867 | path += eachFilename; | |
868 | ||
748fcded | 869 | wxDirItemData *dir_item = new wxDirItemData(path,eachFilename,TRUE); |
51a58d8b JS |
870 | wxTreeItemId id = m_treeCtrl->AppendItem( parentId, eachFilename, 0, -1, dir_item); |
871 | m_treeCtrl->SetItemImage( id, 1, wxTreeItemIcon_Expanded ); | |
ec1b28a3 | 872 | |
51a58d8b | 873 | // Has this got any children? If so, make it expandable. |
748fcded VS |
874 | // (There are two situations when a dir has children: either it |
875 | // has subdirectories or it contains files that weren't filtered | |
876 | // out. The latter only applies to dirctrl with files.) | |
877 | if ( dir_item->HasSubDirs() || | |
878 | (((GetWindowStyle() & wxDIRCTRL_DIR_ONLY) == 0) && | |
879 | dir_item->HasFiles(m_currentFilterStr)) ) | |
51a58d8b | 880 | { |
748fcded | 881 | m_treeCtrl->SetItemHasChildren(id); |
51a58d8b | 882 | } |
51a58d8b JS |
883 | } |
884 | ||
885 | // Add the sorted filenames | |
886 | if ((GetWindowStyle() & wxDIRCTRL_DIR_ONLY) == 0) | |
887 | { | |
888 | for (i = 0; i < filenames.Count(); i++) | |
889 | { | |
890 | wxString eachFilename(filenames[i]); | |
891 | path = dirName; | |
892 | if (path.Last() != wxFILE_SEP_PATH) | |
893 | path += wxString(wxFILE_SEP_PATH); | |
894 | path += eachFilename; | |
895 | //path = dirName + wxString(wxT("/")) + eachFilename; | |
748fcded | 896 | wxDirItemData *dir_item = new wxDirItemData(path,eachFilename,FALSE); |
3fa4bd0e | 897 | (void)m_treeCtrl->AppendItem( parentId, eachFilename, 2, -1, dir_item); |
51a58d8b JS |
898 | } |
899 | } | |
900 | } | |
901 | ||
902 | // Find the child that matches the first part of 'path'. | |
903 | // E.g. if a child path is "/usr" and 'path' is "/usr/include" | |
904 | // then the child for /usr is returned. | |
905 | wxTreeItemId wxGenericDirCtrl::FindChild(wxTreeItemId parentId, const wxString& path, bool& done) | |
906 | { | |
907 | wxString path2(path); | |
ec1b28a3 | 908 | |
51a58d8b JS |
909 | // Make sure all separators are as per the current platform |
910 | path2.Replace(wxT("\\"), wxString(wxFILE_SEP_PATH)); | |
911 | path2.Replace(wxT("/"), wxString(wxFILE_SEP_PATH)); | |
ec1b28a3 | 912 | |
51a58d8b JS |
913 | // Append a separator to foil bogus substring matching |
914 | path2 += wxString(wxFILE_SEP_PATH); | |
ec1b28a3 DW |
915 | |
916 | // In MSW or PM, case is not significant | |
db5333a5 | 917 | #if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXPM__) |
51a58d8b JS |
918 | path2.MakeLower(); |
919 | #endif | |
ec1b28a3 | 920 | |
51a58d8b JS |
921 | long cookie; |
922 | wxTreeItemId childId = m_treeCtrl->GetFirstChild(parentId, cookie); | |
53ccf1c0 | 923 | while (childId.IsOk()) |
51a58d8b | 924 | { |
748fcded | 925 | wxDirItemData* data = (wxDirItemData*) m_treeCtrl->GetItemData(childId); |
ec1b28a3 | 926 | |
748fcded | 927 | if (data && !data->m_path.IsEmpty()) |
51a58d8b JS |
928 | { |
929 | wxString childPath(data->m_path); | |
930 | if (childPath.Last() != wxFILE_SEP_PATH) | |
931 | childPath += wxString(wxFILE_SEP_PATH); | |
ec1b28a3 DW |
932 | |
933 | // In MSW and PM, case is not significant | |
db5333a5 | 934 | #if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXPM__) |
51a58d8b JS |
935 | childPath.MakeLower(); |
936 | #endif | |
ec1b28a3 | 937 | |
51a58d8b JS |
938 | if (childPath.Len() <= path2.Len()) |
939 | { | |
940 | wxString path3 = path2.Mid(0, childPath.Len()); | |
941 | if (childPath == path3) | |
942 | { | |
943 | if (path3.Len() == path2.Len()) | |
944 | done = TRUE; | |
945 | else | |
946 | done = FALSE; | |
947 | return childId; | |
948 | } | |
949 | } | |
950 | } | |
ec1b28a3 | 951 | |
748fcded | 952 | childId = m_treeCtrl->GetNextChild(parentId, cookie); |
51a58d8b | 953 | } |
3fa4bd0e VS |
954 | wxTreeItemId invalid; |
955 | return invalid; | |
51a58d8b JS |
956 | } |
957 | ||
958 | // Try to expand as much of the given path as possible, | |
959 | // and select the given tree item. | |
960 | bool wxGenericDirCtrl::ExpandPath(const wxString& path) | |
961 | { | |
962 | bool done = FALSE; | |
963 | wxTreeItemId id = FindChild(m_rootId, path, done); | |
964 | wxTreeItemId lastId = id; // The last non-zero id | |
237387ad | 965 | while (id.IsOk() && !done) |
51a58d8b JS |
966 | { |
967 | ExpandDir(id); | |
968 | ||
969 | id = FindChild(id, path, done); | |
53ccf1c0 | 970 | if (id.IsOk()) |
51a58d8b JS |
971 | lastId = id; |
972 | } | |
53ccf1c0 | 973 | if (lastId.IsOk()) |
51a58d8b | 974 | { |
748fcded | 975 | wxDirItemData *data = (wxDirItemData *) m_treeCtrl->GetItemData(lastId); |
51a58d8b JS |
976 | if (data->m_isDir) |
977 | { | |
978 | m_treeCtrl->Expand(lastId); | |
979 | } | |
980 | if ((GetWindowStyle() & wxDIRCTRL_SELECT_FIRST) && data->m_isDir) | |
981 | { | |
982 | // Find the first file in this directory | |
983 | long cookie; | |
984 | wxTreeItemId childId = m_treeCtrl->GetFirstChild(lastId, cookie); | |
985 | bool selectedChild = FALSE; | |
53ccf1c0 | 986 | while (childId.IsOk()) |
51a58d8b | 987 | { |
748fcded | 988 | wxDirItemData* data = (wxDirItemData*) m_treeCtrl->GetItemData(childId); |
ec1b28a3 | 989 | |
51a58d8b JS |
990 | if (data && data->m_path != "" && !data->m_isDir) |
991 | { | |
992 | m_treeCtrl->SelectItem(childId); | |
993 | m_treeCtrl->EnsureVisible(childId); | |
994 | selectedChild = TRUE; | |
995 | break; | |
996 | } | |
997 | childId = m_treeCtrl->GetNextChild(lastId, cookie); | |
998 | } | |
999 | if (!selectedChild) | |
1000 | { | |
1001 | m_treeCtrl->SelectItem(lastId); | |
1002 | m_treeCtrl->EnsureVisible(lastId); | |
1003 | } | |
1004 | } | |
1005 | else | |
1006 | { | |
1007 | m_treeCtrl->SelectItem(lastId); | |
1008 | m_treeCtrl->EnsureVisible(lastId); | |
1009 | } | |
1010 | ||
1011 | return TRUE; | |
1012 | } | |
1013 | else | |
1014 | return FALSE; | |
1015 | } | |
1016 | ||
1017 | wxString wxGenericDirCtrl::GetPath() const | |
1018 | { | |
1019 | wxTreeItemId id = m_treeCtrl->GetSelection(); | |
1020 | if (id) | |
1021 | { | |
748fcded | 1022 | wxDirItemData* data = (wxDirItemData*) m_treeCtrl->GetItemData(id); |
51a58d8b JS |
1023 | return data->m_path; |
1024 | } | |
1025 | else | |
1026 | return wxEmptyString; | |
1027 | } | |
1028 | ||
1029 | wxString wxGenericDirCtrl::GetFilePath() const | |
1030 | { | |
1031 | wxTreeItemId id = m_treeCtrl->GetSelection(); | |
1032 | if (id) | |
1033 | { | |
748fcded | 1034 | wxDirItemData* data = (wxDirItemData*) m_treeCtrl->GetItemData(id); |
51a58d8b JS |
1035 | if (data->m_isDir) |
1036 | return wxEmptyString; | |
1037 | else | |
1038 | return data->m_path; | |
1039 | } | |
1040 | else | |
1041 | return wxEmptyString; | |
1042 | } | |
1043 | ||
1044 | void wxGenericDirCtrl::SetPath(const wxString& path) | |
1045 | { | |
1046 | m_defaultPath = path; | |
1047 | if (m_rootId) | |
1048 | ExpandPath(path); | |
1049 | } | |
1050 | ||
1051 | // Not used | |
1052 | #if 0 | |
1053 | void wxGenericDirCtrl::FindChildFiles(wxTreeItemId id, int dirFlags, wxArrayString& filenames) | |
1054 | { | |
748fcded | 1055 | wxDirItemData *data = (wxDirItemData *) m_treeCtrl->GetItemData(id); |
51a58d8b JS |
1056 | |
1057 | // This may take a longish time. Go to busy cursor | |
1058 | wxBusyCursor busy; | |
1059 | ||
1060 | wxASSERT(data); | |
1061 | ||
1062 | wxString search,path,filename; | |
1063 | ||
1064 | wxString dirName(data->m_path); | |
1065 | ||
ec1b28a3 | 1066 | #if defined(__WXMSW__) || defined(__WXPM__) |
51a58d8b JS |
1067 | if (dirName.Last() == ':') |
1068 | dirName += wxString(wxFILE_SEP_PATH); | |
1069 | #endif | |
1070 | ||
1071 | wxDir d; | |
1072 | wxString eachFilename; | |
1073 | ||
f9c165b1 | 1074 | wxLogNull log; |
51a58d8b JS |
1075 | d.Open(dirName); |
1076 | ||
1077 | if (d.IsOpened()) | |
1078 | { | |
1079 | if (d.GetFirst(& eachFilename, m_currentFilterStr, dirFlags)) | |
1080 | { | |
1081 | do | |
1082 | { | |
1083 | if ((eachFilename != wxT(".")) && (eachFilename != wxT(".."))) | |
1084 | { | |
1085 | filenames.Add(eachFilename); | |
1086 | } | |
1087 | } | |
1088 | while (d.GetNext(& eachFilename)) ; | |
1089 | } | |
1090 | } | |
1091 | } | |
1092 | #endif | |
1093 | ||
1094 | void wxGenericDirCtrl::SetFilterIndex(int n) | |
1095 | { | |
1096 | m_currentFilter = n; | |
1097 | ||
1098 | wxString f, d; | |
1099 | if (ExtractWildcard(m_filter, n, f, d)) | |
1100 | m_currentFilterStr = f; | |
1101 | else | |
1102 | m_currentFilterStr = wxT("*.*"); | |
1103 | } | |
1104 | ||
1105 | void wxGenericDirCtrl::SetFilter(const wxString& filter) | |
1106 | { | |
1107 | m_filter = filter; | |
1108 | ||
1109 | wxString f, d; | |
1110 | if (ExtractWildcard(m_filter, m_currentFilter, f, d)) | |
1111 | m_currentFilterStr = f; | |
1112 | else | |
1113 | m_currentFilterStr = wxT("*.*"); | |
1114 | } | |
1115 | ||
1116 | // Extract description and actual filter from overall filter string | |
1117 | bool wxGenericDirCtrl::ExtractWildcard(const wxString& filterStr, int n, wxString& filter, wxString& description) | |
1118 | { | |
1119 | wxArrayString filters, descriptions; | |
1120 | int count = ParseFilter(filterStr, filters, descriptions); | |
1121 | if (count > 0 && n < count) | |
1122 | { | |
1123 | filter = filters[n]; | |
1124 | description = descriptions[n]; | |
1125 | return TRUE; | |
1126 | } | |
5716a1ab VZ |
1127 | |
1128 | return FALSE; | |
51a58d8b JS |
1129 | } |
1130 | ||
1131 | // Parses the global filter, returning the number of filters. | |
1132 | // Returns 0 if none or if there's a problem. | |
1133 | // filterStr is in the form: | |
1134 | // | |
1135 | // "All files (*.*)|*.*|JPEG Files (*.jpeg)|*.jpg" | |
1136 | ||
1137 | int wxGenericDirCtrl::ParseFilter(const wxString& filterStr, wxArrayString& filters, wxArrayString& descriptions) | |
1138 | { | |
1139 | wxString str(filterStr); | |
1140 | ||
1141 | wxString description, filter; | |
1142 | int pos; | |
1143 | bool finished = FALSE; | |
1144 | do | |
1145 | { | |
1146 | pos = str.Find(wxT('|')); | |
1147 | if (pos == -1) | |
1148 | return 0; // Problem | |
1149 | description = str.Left(pos); | |
1150 | str = str.Mid(pos+1); | |
1151 | pos = str.Find(wxT('|')); | |
1152 | if (pos == -1) | |
1153 | { | |
1154 | filter = str; | |
1155 | finished = TRUE; | |
1156 | } | |
1157 | else | |
1158 | { | |
1159 | filter = str.Left(pos); | |
1160 | str = str.Mid(pos+1); | |
1161 | } | |
1162 | descriptions.Add(description); | |
1163 | filters.Add(filter); | |
1164 | } | |
1165 | while (!finished) ; | |
1166 | ||
1167 | return filters.Count(); | |
1168 | } | |
1169 | ||
1170 | void wxGenericDirCtrl::DoResize() | |
1171 | { | |
1172 | wxSize sz = GetClientSize(); | |
1173 | int verticalSpacing = 3; | |
1174 | if (m_treeCtrl) | |
1175 | { | |
1176 | wxSize filterSz ; | |
1177 | if (m_filterListCtrl) | |
1178 | { | |
1179 | filterSz = m_filterListCtrl->GetSize(); | |
1180 | sz.y -= (filterSz.y + verticalSpacing); | |
1181 | } | |
1182 | m_treeCtrl->SetSize(0, 0, sz.x, sz.y); | |
1183 | if (m_filterListCtrl) | |
1184 | { | |
1185 | m_filterListCtrl->SetSize(0, sz.y + verticalSpacing, sz.x, filterSz.y); | |
1186 | // Don't know why, but this needs refreshing after a resize (wxMSW) | |
1187 | m_filterListCtrl->Refresh(); | |
1188 | } | |
1189 | } | |
1190 | } | |
1191 | ||
1192 | ||
33ac7e6f | 1193 | void wxGenericDirCtrl::OnSize(wxSizeEvent& WXUNUSED(event)) |
51a58d8b JS |
1194 | { |
1195 | DoResize(); | |
1196 | } | |
1197 | ||
1198 | //----------------------------------------------------------------------------- | |
1199 | // wxDirFilterListCtrl | |
1200 | //----------------------------------------------------------------------------- | |
1201 | ||
1202 | IMPLEMENT_CLASS(wxDirFilterListCtrl, wxChoice) | |
1203 | ||
1204 | BEGIN_EVENT_TABLE(wxDirFilterListCtrl, wxChoice) | |
1205 | EVT_CHOICE(-1, wxDirFilterListCtrl::OnSelFilter) | |
1206 | END_EVENT_TABLE() | |
1207 | ||
1208 | bool wxDirFilterListCtrl::Create(wxGenericDirCtrl* parent, const wxWindowID id, | |
1209 | const wxPoint& pos, | |
1210 | const wxSize& size, | |
1211 | long style) | |
1212 | { | |
1213 | m_dirCtrl = parent; | |
1214 | return wxChoice::Create(parent, id, pos, size, 0, NULL, style); | |
1215 | } | |
1216 | ||
1217 | void wxDirFilterListCtrl::Init() | |
1218 | { | |
1219 | m_dirCtrl = NULL; | |
1220 | } | |
1221 | ||
33ac7e6f | 1222 | void wxDirFilterListCtrl::OnSelFilter(wxCommandEvent& WXUNUSED(event)) |
51a58d8b JS |
1223 | { |
1224 | int sel = GetSelection(); | |
1225 | ||
1226 | wxString currentPath = m_dirCtrl->GetPath(); | |
ec1b28a3 | 1227 | |
51a58d8b JS |
1228 | m_dirCtrl->SetFilterIndex(sel); |
1229 | ||
1230 | // If the filter has changed, the view is out of date, so | |
1231 | // collapse the tree. | |
1232 | m_dirCtrl->GetTreeCtrl()->Collapse(m_dirCtrl->GetRootId()); | |
1233 | m_dirCtrl->GetTreeCtrl()->Expand(m_dirCtrl->GetRootId()); | |
1234 | ||
1235 | // Try to restore the selection, or at least the directory | |
1236 | m_dirCtrl->ExpandPath(currentPath); | |
1237 | } | |
1238 | ||
1239 | void wxDirFilterListCtrl::FillFilterList(const wxString& filter, int defaultFilter) | |
1240 | { | |
1241 | Clear(); | |
1242 | wxArrayString descriptions, filters; | |
1243 | size_t n = (size_t) m_dirCtrl->ParseFilter(filter, filters, descriptions); | |
1244 | ||
1245 | if (n > 0 && defaultFilter < (int) n) | |
1246 | { | |
1247 | size_t i = 0; | |
1248 | for (i = 0; i < n; i++) | |
1249 | Append(descriptions[i]); | |
1250 | SetSelection(defaultFilter); | |
1251 | } | |
1252 | } | |
1253 | ||
1e6feb95 | 1254 | #endif // wxUSE_DIRDLG |