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