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