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