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