]>
Commit | Line | Data |
---|---|---|
51a58d8b | 1 | ///////////////////////////////////////////////////////////////////////////// |
7fc65a03 | 2 | // Name: src/generic/dirctrlg.cpp |
51a58d8b JS |
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 | ||
51a58d8b JS |
12 | // For compilers that support precompilation, includes "wx.h". |
13 | #include "wx/wxprec.h" | |
14 | ||
15 | #ifdef __BORLANDC__ | |
32d4c30a | 16 | #pragma hdrstop |
51a58d8b JS |
17 | #endif |
18 | ||
13de0c8c | 19 | #if wxUSE_DIRDLG || wxUSE_FILEDLG |
51a58d8b | 20 | |
88a7a4e1 WS |
21 | #include "wx/generic/dirctrlg.h" |
22 | ||
32d4c30a WS |
23 | #ifndef WX_PRECOMP |
24 | #include "wx/hash.h" | |
88a7a4e1 | 25 | #include "wx/intl.h" |
e4db172a | 26 | #include "wx/log.h" |
de6185e2 | 27 | #include "wx/utils.h" |
f1e01716 | 28 | #include "wx/button.h" |
923d28da | 29 | #include "wx/icon.h" |
9eddec69 | 30 | #include "wx/settings.h" |
246c5004 | 31 | #include "wx/msgdlg.h" |
b36e08d0 | 32 | #include "wx/choice.h" |
fec9cc08 | 33 | #include "wx/textctrl.h" |
ed2fbeb8 WS |
34 | #include "wx/layout.h" |
35 | #include "wx/sizer.h" | |
949c9f74 | 36 | #include "wx/textdlg.h" |
dd05139a | 37 | #include "wx/gdicmn.h" |
155ecd4c | 38 | #include "wx/image.h" |
02761f6c | 39 | #include "wx/module.h" |
32d4c30a WS |
40 | #endif |
41 | ||
35c2aa4f | 42 | #include "wx/filename.h" |
51a58d8b | 43 | #include "wx/filefn.h" |
51a58d8b | 44 | #include "wx/imaglist.h" |
51a58d8b JS |
45 | #include "wx/tokenzr.h" |
46 | #include "wx/dir.h" | |
06cc1fb9 | 47 | #include "wx/artprov.h" |
06cc1fb9 | 48 | #include "wx/mimetype.h" |
51a58d8b JS |
49 | |
50 | #if wxUSE_STATLINE | |
51 | #include "wx/statline.h" | |
52 | #endif | |
53 | ||
76a5e5d2 | 54 | #if defined(__WXMAC__) |
33ddeaba | 55 | #include "wx/osx/private.h" // includes mac headers |
76a5e5d2 SC |
56 | #endif |
57 | ||
bb5a9514 | 58 | #ifdef __WINDOWS__ |
2736b3ce | 59 | #include <windows.h> |
7acf6a92 | 60 | #include "wx/msw/winundef.h" |
006b7af2 | 61 | #include "wx/volume.h" |
7328394a | 62 | |
c9ecda60 VS |
63 | // FIXME - Mingw32 1.0 has both _getdrive() and _chdrive(). For now, let's assume |
64 | // older releases don't, but it should be verified and the checks modified | |
65 | // accordingly. | |
b4da152e | 66 | #if !defined(__GNUWIN32__) || (defined(__MINGW32_MAJOR_VERSION) && __MINGW32_MAJOR_VERSION >= 1) |
1c193821 | 67 | #if !defined(__WXWINCE__) |
32d4c30a | 68 | #include <direct.h> |
1c193821 | 69 | #endif |
32d4c30a WS |
70 | #include <stdlib.h> |
71 | #include <ctype.h> | |
51a58d8b JS |
72 | #endif |
73 | ||
bb5a9514 | 74 | #endif // __WINDOWS__ |
7328394a | 75 | |
1c53456f | 76 | #if defined(__OS2__) || defined(__DOS__) |
ed2fbeb8 WS |
77 | #ifdef __OS2__ |
78 | #define INCL_BASE | |
79 | #include <os2.h> | |
80 | #ifndef __EMX__ | |
81 | #include <direct.h> | |
82 | #endif | |
83 | #include <stdlib.h> | |
84 | #include <ctype.h> | |
1c53456f | 85 | #endif |
0d853c54 | 86 | #endif // __OS2__ |
ec1b28a3 | 87 | |
f4ac0693 | 88 | #if defined(__WXMAC__) |
e1f31102 | 89 | // #include "MoreFilesX.h" |
bedaf53e SC |
90 | #endif |
91 | ||
267a7108 | 92 | #ifdef __BORLANDC__ |
32d4c30a | 93 | #include "dos.h" |
267a7108 JS |
94 | #endif |
95 | ||
f36e602b | 96 | extern WXDLLEXPORT_DATA(const char) wxFileSelectorDefaultWildcardStr[]; |
a3e70e1f | 97 | |
51a58d8b JS |
98 | // If compiled under Windows, this macro can cause problems |
99 | #ifdef GetFirstChild | |
100 | #undef GetFirstChild | |
101 | #endif | |
102 | ||
207ddcc9 VZ |
103 | bool wxIsDriveAvailable(const wxString& dirName); |
104 | ||
84605707 VZ |
105 | // ---------------------------------------------------------------------------- |
106 | // events | |
107 | // ---------------------------------------------------------------------------- | |
108 | ||
109 | wxDEFINE_EVENT( wxEVT_COMMAND_DIRCTRL_CHANGED, wxTreeEvent ); | |
110 | ||
06cc1fb9 | 111 | // ---------------------------------------------------------------------------- |
0d853c54 | 112 | // wxGetAvailableDrives, for WINDOWS, DOS, OS2, MAC, UNIX (returns "/") |
06cc1fb9 | 113 | // ---------------------------------------------------------------------------- |
51a58d8b | 114 | |
06cc1fb9 JS |
115 | size_t wxGetAvailableDrives(wxArrayString &paths, wxArrayString &names, wxArrayInt &icon_ids) |
116 | { | |
35c2aa4f | 117 | #ifdef wxHAS_FILESYSTEM_VOLUMES |
51a58d8b | 118 | |
4676948b JS |
119 | #ifdef __WXWINCE__ |
120 | // No logical drives; return "\" | |
121 | paths.Add(wxT("\\")); | |
122 | names.Add(wxT("\\")); | |
1ab36908 | 123 | icon_ids.Add(wxFileIconsTable::computer); |
006b7af2 VZ |
124 | #elif defined(__WIN32__) && wxUSE_FSVOLUME |
125 | // TODO: this code (using wxFSVolumeBase) should be used for all platforms | |
126 | // but unfortunately wxFSVolumeBase is not implemented everywhere | |
127 | const wxArrayString as = wxFSVolumeBase::GetVolumes(); | |
0026e0f2 | 128 | |
006b7af2 VZ |
129 | for (size_t i = 0; i < as.GetCount(); i++) |
130 | { | |
131 | wxString path = as[i]; | |
132 | wxFSVolume vol(path); | |
999836aa | 133 | int imageId; |
006b7af2 | 134 | switch (vol.GetKind()) |
06cc1fb9 | 135 | { |
006b7af2 VZ |
136 | case wxFS_VOL_FLOPPY: |
137 | if ( (path == wxT("a:\\")) || (path == wxT("b:\\")) ) | |
06cc1fb9 JS |
138 | imageId = wxFileIconsTable::floppy; |
139 | else | |
140 | imageId = wxFileIconsTable::removeable; | |
141 | break; | |
006b7af2 VZ |
142 | case wxFS_VOL_DVDROM: |
143 | case wxFS_VOL_CDROM: | |
06cc1fb9 JS |
144 | imageId = wxFileIconsTable::cdrom; |
145 | break; | |
006b7af2 VZ |
146 | case wxFS_VOL_NETWORK: |
147 | if (path[0] == wxT('\\')) | |
148 | continue; // skip "\\computer\folder" | |
149 | imageId = wxFileIconsTable::drive; | |
150 | break; | |
151 | case wxFS_VOL_DISK: | |
152 | case wxFS_VOL_OTHER: | |
06cc1fb9 JS |
153 | default: |
154 | imageId = wxFileIconsTable::drive; | |
155 | break; | |
156 | } | |
06cc1fb9 | 157 | paths.Add(path); |
006b7af2 | 158 | names.Add(vol.GetDisplayName()); |
06cc1fb9 | 159 | icon_ids.Add(imageId); |
06cc1fb9 | 160 | } |
0d853c54 SN |
161 | #elif defined(__OS2__) |
162 | APIRET rc; | |
163 | ULONG ulDriveNum = 0; | |
164 | ULONG ulDriveMap = 0; | |
165 | rc = ::DosQueryCurrentDisk(&ulDriveNum, &ulDriveMap); | |
ca65c044 WS |
166 | if ( rc == 0) |
167 | { | |
0d853c54 SN |
168 | size_t i = 0; |
169 | while (i < 26) | |
170 | { | |
ca65c044 WS |
171 | if (ulDriveMap & ( 1 << i )) |
172 | { | |
35c2aa4f VZ |
173 | const wxString path = wxFileName::GetVolumeString( |
174 | 'A' + i, wxPATH_GET_SEPARATOR); | |
175 | const wxString name = wxFileName::GetVolumeString( | |
176 | 'A' + i, wxPATH_NO_SEPARATOR); | |
ca65c044 | 177 | |
be283c77 SN |
178 | // Note: If _filesys is unsupported by some compilers, |
179 | // we can always replace it by DosQueryFSAttach | |
180 | char filesysname[20]; | |
7fc65a03 WS |
181 | #ifdef __WATCOMC__ |
182 | ULONG cbBuffer = sizeof(filesysname); | |
183 | PFSQBUFFER2 pfsqBuffer = (PFSQBUFFER2)filesysname; | |
184 | APIRET rc = ::DosQueryFSAttach(name.fn_str(),0,FSAIL_QUERYNAME,pfsqBuffer,&cbBuffer); | |
185 | if (rc != NO_ERROR) | |
186 | { | |
187 | filesysname[0] = '\0'; | |
188 | } | |
189 | #else | |
be283c77 | 190 | _filesys(name.fn_str(), filesysname, sizeof(filesysname)); |
7fc65a03 | 191 | #endif |
be283c77 | 192 | /* FAT, LAN, HPFS, CDFS, NFS */ |
ca65c044 WS |
193 | int imageId; |
194 | if (path == wxT("A:\\") || path == wxT("B:\\")) | |
195 | imageId = wxFileIconsTable::floppy; | |
be283c77 SN |
196 | else if (!strcmp(filesysname, "CDFS")) |
197 | imageId = wxFileIconsTable::cdrom; | |
198 | else if (!strcmp(filesysname, "LAN") || | |
199 | !strcmp(filesysname, "NFS")) | |
200 | imageId = wxFileIconsTable::drive; | |
ca65c044 WS |
201 | else |
202 | imageId = wxFileIconsTable::drive; | |
203 | paths.Add(path); | |
204 | names.Add(name); | |
205 | icon_ids.Add(imageId); | |
206 | } | |
0d853c54 | 207 | i ++; |
ca65c044 WS |
208 | } |
209 | } | |
0d853c54 | 210 | #else // !__WIN32__, !__OS2__ |
06cc1fb9 | 211 | /* If we can switch to the drive, it exists. */ |
35c2aa4f | 212 | for ( char drive = 'A'; drive <= 'Z'; drive++ ) |
06cc1fb9 | 213 | { |
35c2aa4f VZ |
214 | const wxString |
215 | path = wxFileName::GetVolumeString(drive, wxPATH_GET_SEPARATOR); | |
51a58d8b | 216 | |
06cc1fb9 JS |
217 | if (wxIsDriveAvailable(path)) |
218 | { | |
219 | paths.Add(path); | |
35c2aa4f VZ |
220 | names.Add(wxFileName::GetVolumeString(drive, wxPATH_NO_SEPARATOR)); |
221 | icon_ids.Add(drive <= 2 ? wxFileIconsTable::floppy | |
222 | : wxFileIconsTable::drive); | |
06cc1fb9 JS |
223 | } |
224 | } | |
225 | #endif // __WIN32__/!__WIN32__ | |
226 | ||
96f04e1d | 227 | #elif defined(__WXMAC__) && wxOSX_USE_COCOA_OR_CARBON |
06cc1fb9 | 228 | |
a2b77260 SC |
229 | ItemCount volumeIndex = 1; |
230 | OSErr err = noErr ; | |
44d60c0b | 231 | |
a2b77260 SC |
232 | while( noErr == err ) |
233 | { | |
234 | HFSUniStr255 volumeName ; | |
235 | FSRef fsRef ; | |
236 | FSVolumeInfo volumeInfo ; | |
44d60c0b WS |
237 | err = FSGetVolumeInfo(0, volumeIndex, NULL, kFSVolInfoFlags , &volumeInfo , &volumeName, &fsRef); |
238 | if( noErr == err ) | |
239 | { | |
240 | wxString path = wxMacFSRefToPath( &fsRef ) ; | |
a2b77260 | 241 | wxString name = wxMacHFSUniStrToString( &volumeName ) ; |
44d60c0b | 242 | |
a2b77260 | 243 | if ( (volumeInfo.flags & kFSVolFlagSoftwareLockedMask) || (volumeInfo.flags & kFSVolFlagHardwareLockedMask) ) |
06cc1fb9 | 244 | { |
a2b77260 | 245 | icon_ids.Add(wxFileIconsTable::cdrom); |
06cc1fb9 | 246 | } |
a2b77260 SC |
247 | else |
248 | { | |
249 | icon_ids.Add(wxFileIconsTable::drive); | |
06cc1fb9 | 250 | } |
a2b77260 | 251 | // todo other removable |
44d60c0b | 252 | |
06cc1fb9 JS |
253 | paths.Add(path); |
254 | names.Add(name); | |
a2b77260 | 255 | volumeIndex++ ; |
44d60c0b | 256 | } |
06cc1fb9 | 257 | } |
06cc1fb9 | 258 | |
bd362275 | 259 | #elif defined(__UNIX__) |
06cc1fb9 JS |
260 | paths.Add(wxT("/")); |
261 | names.Add(wxT("/")); | |
262 | icon_ids.Add(wxFileIconsTable::computer); | |
263 | #else | |
264 | #error "Unsupported platform in wxGenericDirCtrl!" | |
265 | #endif | |
e9890600 WS |
266 | wxASSERT_MSG( (paths.GetCount() == names.GetCount()), wxT("The number of paths and their human readable names should be equal in number.")); |
267 | wxASSERT_MSG( (paths.GetCount() == icon_ids.GetCount()), wxT("Wrong number of icons for available drives.")); | |
06cc1fb9 JS |
268 | return paths.GetCount(); |
269 | } | |
51a58d8b | 270 | |
06cc1fb9 JS |
271 | // ---------------------------------------------------------------------------- |
272 | // wxIsDriveAvailable | |
273 | // ---------------------------------------------------------------------------- | |
748fcded | 274 | |
748fcded | 275 | #if defined(__DOS__) |
db5333a5 | 276 | |
37fd1c97 | 277 | bool wxIsDriveAvailable(const wxString& dirName) |
db5333a5 | 278 | { |
0e1f8ea4 | 279 | // FIXME: this method leads to hang up under Watcom for some reason |
127eab18 WS |
280 | #ifdef __WATCOMC__ |
281 | wxUnusedVar(dirName); | |
282 | #else | |
b36e08d0 | 283 | if ( dirName.length() == 3 && dirName[1u] == wxT(':') ) |
db5333a5 VS |
284 | { |
285 | wxString dirNameLower(dirName.Lower()); | |
ca65c044 | 286 | // VS: always return true for removable media, since Win95 doesn't |
db5333a5 VS |
287 | // like it when MS-DOS app accesses empty floppy drive |
288 | return (dirNameLower[0u] == wxT('a') || | |
289 | dirNameLower[0u] == wxT('b') || | |
da865fdd | 290 | wxDirExists(dirNameLower)); |
db5333a5 VS |
291 | } |
292 | else | |
6157794c | 293 | #endif |
ca65c044 | 294 | return true; |
db5333a5 VS |
295 | } |
296 | ||
0d853c54 | 297 | #elif defined(__WINDOWS__) || defined(__OS2__) |
db5333a5 | 298 | |
0c0d1521 | 299 | int setdrive(int WXUNUSED_IN_WINCE(drive)) |
33ac7e6f | 300 | { |
4676948b JS |
301 | #ifdef __WXWINCE__ |
302 | return 0; | |
303 | #elif defined(__GNUWIN32__) && \ | |
c9ecda60 VS |
304 | (defined(__MINGW32_MAJOR_VERSION) && __MINGW32_MAJOR_VERSION >= 1) |
305 | return _chdrive(drive); | |
306 | #else | |
ca65c044 | 307 | wxChar newdrive[4]; |
33ac7e6f | 308 | |
ca65c044 WS |
309 | if (drive < 1 || drive > 31) |
310 | return -1; | |
311 | newdrive[0] = (wxChar)(wxT('A') + drive - 1); | |
312 | newdrive[1] = wxT(':'); | |
0d853c54 | 313 | #ifdef __OS2__ |
ca65c044 WS |
314 | newdrive[2] = wxT('\\'); |
315 | newdrive[3] = wxT('\0'); | |
0d853c54 | 316 | #else |
ca65c044 | 317 | newdrive[2] = wxT('\0'); |
0d853c54 | 318 | #endif |
bb5a9514 | 319 | #if defined(__WINDOWS__) |
ca65c044 | 320 | if (::SetCurrentDirectory(newdrive)) |
50c76ce1 DW |
321 | #else |
322 | // VA doesn't know what LPSTR is and has its own set | |
ca65c044 | 323 | if (!DosSetCurrentDir((PSZ)newdrive)) |
50c76ce1 | 324 | #endif |
ca65c044 WS |
325 | return 0; |
326 | else | |
327 | return -1; | |
c9ecda60 | 328 | #endif // !GNUWIN32 |
33ac7e6f KB |
329 | } |
330 | ||
0c0d1521 | 331 | bool wxIsDriveAvailable(const wxString& WXUNUSED_IN_WINCE(dirName)) |
ad9cd15c | 332 | { |
4676948b | 333 | #ifdef __WXWINCE__ |
ca65c044 | 334 | return false; |
4676948b | 335 | #else |
ad9cd15c | 336 | #ifdef __WIN32__ |
33fed835 | 337 | UINT errorMode = SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX); |
ad9cd15c | 338 | #endif |
ca65c044 | 339 | bool success = true; |
ad9cd15c JS |
340 | |
341 | // Check if this is a root directory and if so, | |
2d4e4f80 | 342 | // whether the drive is available. |
b36e08d0 | 343 | if (dirName.length() == 3 && dirName[(size_t)1] == wxT(':')) |
ad9cd15c | 344 | { |
33fed835 | 345 | wxString dirNameLower(dirName.Lower()); |
b4da152e | 346 | #if defined(__GNUWIN32__) && !(defined(__MINGW32_MAJOR_VERSION) && __MINGW32_MAJOR_VERSION >= 1) |
da865fdd | 347 | success = wxDirExists(dirNameLower); |
7328394a | 348 | #else |
0d853c54 SN |
349 | #if defined(__OS2__) |
350 | // Avoid changing to drive since no media may be inserted. | |
351 | if (dirNameLower[(size_t)0] == 'a' || dirNameLower[(size_t)0] == 'b') | |
352 | return success; | |
353 | #endif | |
ad9cd15c | 354 | int currentDrive = _getdrive(); |
33fed835 | 355 | int thisDrive = (int) (dirNameLower[(size_t)0] - 'a' + 1) ; |
33ac7e6f KB |
356 | int err = setdrive( thisDrive ) ; |
357 | setdrive( currentDrive ); | |
ad9cd15c JS |
358 | |
359 | if (err == -1) | |
360 | { | |
ca65c044 | 361 | success = false; |
ad9cd15c | 362 | } |
7328394a | 363 | #endif |
ad9cd15c JS |
364 | } |
365 | #ifdef __WIN32__ | |
33fed835 | 366 | (void) SetErrorMode(errorMode); |
ad9cd15c JS |
367 | #endif |
368 | ||
33fed835 | 369 | return success; |
4676948b | 370 | #endif |
ad9cd15c | 371 | } |
0d853c54 | 372 | #endif // __WINDOWS__ || __OS2__ |
ad9cd15c | 373 | |
13de0c8c WS |
374 | #endif // wxUSE_DIRDLG || wxUSE_FILEDLG |
375 | ||
376 | ||
377 | ||
378 | #if wxUSE_DIRDLG | |
b600ed13 | 379 | |
57e26a09 JS |
380 | // Function which is called by quick sort. We want to override the default wxArrayString behaviour, |
381 | // and sort regardless of case. | |
a2a03d78 | 382 | static int wxCMPFUNC_CONV wxDirCtrlStringCompareFunction(const wxString& strFirst, const wxString& strSecond) |
57e26a09 | 383 | { |
a2a03d78 | 384 | return strFirst.CmpNoCase(strSecond); |
57e26a09 JS |
385 | } |
386 | ||
51a58d8b | 387 | //----------------------------------------------------------------------------- |
748fcded | 388 | // wxDirItemData |
51a58d8b JS |
389 | //----------------------------------------------------------------------------- |
390 | ||
748fcded VS |
391 | wxDirItemData::wxDirItemData(const wxString& path, const wxString& name, |
392 | bool isDir) | |
51a58d8b JS |
393 | { |
394 | m_path = path; | |
395 | m_name = name; | |
396 | /* Insert logic to detect hidden files here | |
397 | * In UnixLand we just check whether the first char is a dot | |
398 | * For FileNameFromPath read LastDirNameInThisPath ;-) */ | |
399 | // m_isHidden = (bool)(wxFileNameFromPath(*m_path)[0] == '.'); | |
ca65c044 WS |
400 | m_isHidden = false; |
401 | m_isExpanded = false; | |
51a58d8b JS |
402 | m_isDir = isDir; |
403 | } | |
404 | ||
748fcded | 405 | void wxDirItemData::SetNewDirName(const wxString& path) |
51a58d8b JS |
406 | { |
407 | m_path = path; | |
748fcded VS |
408 | m_name = wxFileNameFromPath(path); |
409 | } | |
410 | ||
411 | bool wxDirItemData::HasSubDirs() const | |
412 | { | |
44d60c0b | 413 | if (m_path.empty()) |
ca65c044 | 414 | return false; |
748fcded VS |
415 | |
416 | wxDir dir; | |
417 | { | |
418 | wxLogNull nolog; | |
419 | if ( !dir.Open(m_path) ) | |
ca65c044 | 420 | return false; |
748fcded VS |
421 | } |
422 | ||
423 | return dir.HasSubDirs(); | |
424 | } | |
425 | ||
574c939e | 426 | bool wxDirItemData::HasFiles(const wxString& WXUNUSED(spec)) const |
748fcded | 427 | { |
44d60c0b | 428 | if (m_path.empty()) |
ca65c044 | 429 | return false; |
748fcded VS |
430 | |
431 | wxDir dir; | |
432 | { | |
433 | wxLogNull nolog; | |
434 | if ( !dir.Open(m_path) ) | |
ca65c044 | 435 | return false; |
748fcded VS |
436 | } |
437 | ||
438 | return dir.HasFiles(); | |
51a58d8b JS |
439 | } |
440 | ||
51a58d8b JS |
441 | //----------------------------------------------------------------------------- |
442 | // wxGenericDirCtrl | |
443 | //----------------------------------------------------------------------------- | |
444 | ||
51a58d8b | 445 | BEGIN_EVENT_TABLE(wxGenericDirCtrl, wxControl) |
6b707fc3 JS |
446 | EVT_TREE_ITEM_EXPANDING (wxID_TREECTRL, wxGenericDirCtrl::OnExpandItem) |
447 | EVT_TREE_ITEM_COLLAPSED (wxID_TREECTRL, wxGenericDirCtrl::OnCollapseItem) | |
448 | EVT_TREE_BEGIN_LABEL_EDIT (wxID_TREECTRL, wxGenericDirCtrl::OnBeginEditItem) | |
449 | EVT_TREE_END_LABEL_EDIT (wxID_TREECTRL, wxGenericDirCtrl::OnEndEditItem) | |
84605707 | 450 | EVT_TREE_SEL_CHANGED (wxID_TREECTRL, wxGenericDirCtrl::OnTreeSelChange) |
51a58d8b JS |
451 | EVT_SIZE (wxGenericDirCtrl::OnSize) |
452 | END_EVENT_TABLE() | |
453 | ||
454 | wxGenericDirCtrl::wxGenericDirCtrl(void) | |
455 | { | |
456 | Init(); | |
457 | } | |
458 | ||
c06dde42 JS |
459 | void wxGenericDirCtrl::ExpandRoot() |
460 | { | |
461 | ExpandDir(m_rootId); // automatically expand first level | |
462 | ||
463 | // Expand and select the default path | |
464 | if (!m_defaultPath.empty()) | |
465 | { | |
466 | ExpandPath(m_defaultPath); | |
467 | } | |
468 | #ifdef __UNIX__ | |
469 | else | |
470 | { | |
471 | // On Unix, there's only one node under the (hidden) root node. It | |
472 | // represents the / path, so the user would always have to expand it; | |
473 | // let's do it ourselves | |
474 | ExpandPath( wxT("/") ); | |
475 | } | |
476 | #endif | |
477 | } | |
478 | ||
51a58d8b | 479 | bool wxGenericDirCtrl::Create(wxWindow *parent, |
c1ec7ee8 | 480 | const wxWindowID treeid, |
748fcded VS |
481 | const wxString& dir, |
482 | const wxPoint& pos, | |
483 | const wxSize& size, | |
484 | long style, | |
485 | const wxString& filter, | |
486 | int defaultFilter, | |
487 | const wxString& name) | |
51a58d8b | 488 | { |
c1ec7ee8 | 489 | if (!wxControl::Create(parent, treeid, pos, size, style, wxDefaultValidator, name)) |
ca65c044 | 490 | return false; |
51a58d8b | 491 | |
db5333a5 | 492 | SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE)); |
e1957703 | 493 | SetForegroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT)); |
51a58d8b JS |
494 | |
495 | Init(); | |
496 | ||
3bd8a405 JS |
497 | long treeStyle = wxTR_HAS_BUTTONS; |
498 | ||
499 | // On Windows CE, if you hide the root, you get a crash when | |
500 | // attempting to access data for children of the root item. | |
501 | #ifndef __WXWINCE__ | |
502 | treeStyle |= wxTR_HIDE_ROOT; | |
503 | #endif | |
2997ca30 | 504 | |
82c1f2a3 RR |
505 | #ifdef __WXGTK20__ |
506 | treeStyle |= wxTR_NO_LINES; | |
507 | #endif | |
fd775aae | 508 | |
dabd1377 | 509 | if (style & wxDIRCTRL_EDIT_LABELS) |
fd775aae JS |
510 | treeStyle |= wxTR_EDIT_LABELS; |
511 | ||
80f624ec VZ |
512 | if (style & wxDIRCTRL_MULTIPLE) |
513 | treeStyle |= wxTR_MULTIPLE; | |
514 | ||
51a58d8b JS |
515 | if ((style & wxDIRCTRL_3D_INTERNAL) == 0) |
516 | treeStyle |= wxNO_BORDER; | |
517 | ||
a78955e3 | 518 | m_treeCtrl = CreateTreeCtrl(this, wxID_TREECTRL, |
c47addef | 519 | wxPoint(0,0), GetClientSize(), treeStyle); |
51a58d8b | 520 | |
d0bc78e2 VZ |
521 | if (!filter.empty()) |
522 | m_filterListCtrl = new wxDirFilterListCtrl(this, wxID_FILTERLISTCTRL); | |
51a58d8b JS |
523 | |
524 | m_defaultPath = dir; | |
525 | m_filter = filter; | |
526 | ||
dd0138d9 | 527 | if (m_filter.empty()) |
d0bc78e2 | 528 | m_filter = wxFileSelectorDefaultWildcardStr; |
dd0138d9 | 529 | |
51a58d8b JS |
530 | SetFilterIndex(defaultFilter); |
531 | ||
532 | if (m_filterListCtrl) | |
533 | m_filterListCtrl->FillFilterList(filter, defaultFilter); | |
534 | ||
06cc1fb9 | 535 | m_treeCtrl->SetImageList(wxTheFileIconsTable->GetSmallImageList()); |
51a58d8b | 536 | |
ca65c044 WS |
537 | m_showHidden = false; |
538 | wxDirItemData* rootData = new wxDirItemData(wxEmptyString, wxEmptyString, true); | |
51a58d8b JS |
539 | |
540 | wxString rootName; | |
541 | ||
0d853c54 | 542 | #if defined(__WINDOWS__) || defined(__OS2__) || defined(__DOS__) |
51a58d8b JS |
543 | rootName = _("Computer"); |
544 | #else | |
545 | rootName = _("Sections"); | |
546 | #endif | |
547 | ||
548 | m_rootId = m_treeCtrl->AddRoot( rootName, 3, -1, rootData); | |
549 | m_treeCtrl->SetItemHasChildren(m_rootId); | |
2997ca30 | 550 | |
c06dde42 | 551 | ExpandRoot(); |
51a58d8b | 552 | |
4a89f0e9 | 553 | SetInitialSize(size); |
51a58d8b JS |
554 | DoResize(); |
555 | ||
ca65c044 | 556 | return true; |
51a58d8b JS |
557 | } |
558 | ||
559 | wxGenericDirCtrl::~wxGenericDirCtrl() | |
560 | { | |
51a58d8b JS |
561 | } |
562 | ||
563 | void wxGenericDirCtrl::Init() | |
564 | { | |
ca65c044 | 565 | m_showHidden = false; |
51a58d8b JS |
566 | m_currentFilter = 0; |
567 | m_currentFilterStr = wxEmptyString; // Default: any file | |
568 | m_treeCtrl = NULL; | |
569 | m_filterListCtrl = NULL; | |
570 | } | |
571 | ||
c1ec7ee8 | 572 | wxTreeCtrl* wxGenericDirCtrl::CreateTreeCtrl(wxWindow *parent, wxWindowID treeid, const wxPoint& pos, const wxSize& size, long treeStyle) |
a78955e3 | 573 | { |
c1ec7ee8 | 574 | return new wxTreeCtrl(parent, treeid, pos, size, treeStyle); |
a78955e3 VS |
575 | } |
576 | ||
42dcacf0 RR |
577 | void wxGenericDirCtrl::ShowHidden( bool show ) |
578 | { | |
9092371c VZ |
579 | if ( m_showHidden == show ) |
580 | return; | |
581 | ||
42dcacf0 | 582 | m_showHidden = show; |
574c939e | 583 | |
80f624ec VZ |
584 | if ( HasFlag(wxDIRCTRL_MULTIPLE) ) |
585 | { | |
586 | wxArrayString paths; | |
587 | GetPaths(paths); | |
588 | ReCreateTree(); | |
589 | for ( unsigned n = 0; n < paths.size(); n++ ) | |
590 | { | |
591 | ExpandPath(paths[n]); | |
592 | } | |
593 | } | |
594 | else | |
595 | { | |
596 | wxString path = GetPath(); | |
597 | ReCreateTree(); | |
598 | SetPath(path); | |
599 | } | |
42dcacf0 RR |
600 | } |
601 | ||
22328fa4 JS |
602 | const wxTreeItemId |
603 | wxGenericDirCtrl::AddSection(const wxString& path, const wxString& name, int imageId) | |
51a58d8b | 604 | { |
ca65c044 | 605 | wxDirItemData *dir_item = new wxDirItemData(path,name,true); |
51a58d8b | 606 | |
c1ec7ee8 | 607 | wxTreeItemId treeid = AppendItem( m_rootId, name, imageId, -1, dir_item); |
4f5c180e | 608 | |
c1ec7ee8 | 609 | m_treeCtrl->SetItemHasChildren(treeid); |
22328fa4 | 610 | |
c1ec7ee8 | 611 | return treeid; |
06cc1fb9 | 612 | } |
51a58d8b | 613 | |
06cc1fb9 JS |
614 | void wxGenericDirCtrl::SetupSections() |
615 | { | |
616 | wxArrayString paths, names; | |
617 | wxArrayInt icons; | |
2d4e4f80 | 618 | |
06cc1fb9 | 619 | size_t n, count = wxGetAvailableDrives(paths, names, icons); |
4f5c180e | 620 | |
82c1f2a3 RR |
621 | #ifdef __WXGTK20__ |
622 | wxString home = wxGetHomeDir(); | |
623 | AddSection( home, _("Home directory"), 1); | |
624 | home += wxT("/Desktop"); | |
625 | AddSection( home, _("Desktop"), 1); | |
626 | #endif | |
627 | ||
06cc1fb9 | 628 | for (n = 0; n < count; n++) |
06cc1fb9 | 629 | AddSection(paths[n], names[n], icons[n]); |
51a58d8b JS |
630 | } |
631 | ||
905e0905 VZ |
632 | void wxGenericDirCtrl::SetFocus() |
633 | { | |
634 | // we don't need focus ourselves, give it to the tree so that the user | |
635 | // could navigate it | |
d095f867 RR |
636 | if (m_treeCtrl) |
637 | m_treeCtrl->SetFocus(); | |
905e0905 VZ |
638 | } |
639 | ||
51a58d8b JS |
640 | void wxGenericDirCtrl::OnBeginEditItem(wxTreeEvent &event) |
641 | { | |
642 | // don't rename the main entry "Sections" | |
643 | if (event.GetItem() == m_rootId) | |
644 | { | |
645 | event.Veto(); | |
646 | return; | |
647 | } | |
648 | ||
649 | // don't rename the individual sections | |
99006e44 | 650 | if (m_treeCtrl->GetItemParent( event.GetItem() ) == m_rootId) |
51a58d8b JS |
651 | { |
652 | event.Veto(); | |
653 | return; | |
654 | } | |
655 | } | |
656 | ||
657 | void wxGenericDirCtrl::OnEndEditItem(wxTreeEvent &event) | |
658 | { | |
6e86472f VZ |
659 | if (event.IsEditCancelled()) |
660 | return; | |
661 | ||
44d60c0b | 662 | if ((event.GetLabel().empty()) || |
47b8afad MR |
663 | (event.GetLabel() == wxT(".")) || |
664 | (event.GetLabel() == wxT("..")) || | |
06cc1fb9 JS |
665 | (event.GetLabel().Find(wxT('/')) != wxNOT_FOUND) || |
666 | (event.GetLabel().Find(wxT('\\')) != wxNOT_FOUND) || | |
667 | (event.GetLabel().Find(wxT('|')) != wxNOT_FOUND)) | |
51a58d8b JS |
668 | { |
669 | wxMessageDialog dialog(this, _("Illegal directory name."), _("Error"), wxOK | wxICON_ERROR ); | |
670 | dialog.ShowModal(); | |
671 | event.Veto(); | |
672 | return; | |
673 | } | |
674 | ||
c1ec7ee8 | 675 | wxTreeItemId treeid = event.GetItem(); |
0d6f66f4 | 676 | wxDirItemData *data = GetItemData( treeid ); |
51a58d8b JS |
677 | wxASSERT( data ); |
678 | ||
679 | wxString new_name( wxPathOnly( data->m_path ) ); | |
ad9cd15c | 680 | new_name += wxString(wxFILE_SEP_PATH); |
51a58d8b JS |
681 | new_name += event.GetLabel(); |
682 | ||
683 | wxLogNull log; | |
684 | ||
685 | if (wxFileExists(new_name)) | |
686 | { | |
687 | wxMessageDialog dialog(this, _("File name exists already."), _("Error"), wxOK | wxICON_ERROR ); | |
688 | dialog.ShowModal(); | |
689 | event.Veto(); | |
690 | } | |
691 | ||
692 | if (wxRenameFile(data->m_path,new_name)) | |
693 | { | |
694 | data->SetNewDirName( new_name ); | |
695 | } | |
696 | else | |
697 | { | |
698 | wxMessageDialog dialog(this, _("Operation not permitted."), _("Error"), wxOK | wxICON_ERROR ); | |
699 | dialog.ShowModal(); | |
700 | event.Veto(); | |
701 | } | |
702 | } | |
703 | ||
84605707 VZ |
704 | void wxGenericDirCtrl::OnTreeSelChange(wxTreeEvent &event) |
705 | { | |
706 | wxTreeEvent changedEvent(wxEVT_COMMAND_DIRCTRL_CHANGED, GetId()); | |
707 | ||
9cf565e2 | 708 | changedEvent.SetEventObject(this); |
84605707 VZ |
709 | changedEvent.SetItem(event.GetItem()); |
710 | changedEvent.SetClientObject(m_treeCtrl->GetItemData(event.GetItem())); | |
711 | ||
9cf565e2 VZ |
712 | if (GetEventHandler()->SafelyProcessEvent(changedEvent) && !changedEvent.IsAllowed()) |
713 | { | |
714 | event.Veto(); | |
715 | } | |
716 | else | |
717 | { | |
718 | event.Skip(); | |
719 | } | |
84605707 VZ |
720 | } |
721 | ||
51a58d8b JS |
722 | void wxGenericDirCtrl::OnExpandItem(wxTreeEvent &event) |
723 | { | |
724 | wxTreeItemId parentId = event.GetItem(); | |
725 | ||
748fcded VS |
726 | // VS: this is needed because the event handler is called from wxTreeCtrl |
727 | // ctor when wxTR_HIDE_ROOT was specified | |
4ded51f2 CE |
728 | |
729 | if (!m_rootId.IsOk()) | |
748fcded VS |
730 | m_rootId = m_treeCtrl->GetRootItem(); |
731 | ||
51a58d8b JS |
732 | ExpandDir(parentId); |
733 | } | |
734 | ||
735 | void wxGenericDirCtrl::OnCollapseItem(wxTreeEvent &event ) | |
736 | { | |
08887820 VS |
737 | CollapseDir(event.GetItem()); |
738 | } | |
739 | ||
740 | void wxGenericDirCtrl::CollapseDir(wxTreeItemId parentId) | |
741 | { | |
742 | wxTreeItemId child; | |
51a58d8b | 743 | |
0d6f66f4 | 744 | wxDirItemData *data = GetItemData(parentId); |
51a58d8b JS |
745 | if (!data->m_isExpanded) |
746 | return; | |
747 | ||
ca65c044 | 748 | data->m_isExpanded = false; |
5b806097 JS |
749 | |
750 | m_treeCtrl->Freeze(); | |
c06dde42 | 751 | if (parentId != m_treeCtrl->GetRootItem()) |
5b806097 JS |
752 | m_treeCtrl->CollapseAndReset(parentId); |
753 | m_treeCtrl->DeleteChildren(parentId); | |
754 | m_treeCtrl->Thaw(); | |
51a58d8b JS |
755 | } |
756 | ||
ef57807f | 757 | void wxGenericDirCtrl::PopulateNode(wxTreeItemId parentId) |
51a58d8b | 758 | { |
0d6f66f4 | 759 | wxDirItemData *data = GetItemData(parentId); |
51a58d8b JS |
760 | |
761 | if (data->m_isExpanded) | |
762 | return; | |
763 | ||
ca65c044 | 764 | data->m_isExpanded = true; |
51a58d8b | 765 | |
748fcded | 766 | if (parentId == m_treeCtrl->GetRootItem()) |
51a58d8b JS |
767 | { |
768 | SetupSections(); | |
769 | return; | |
770 | } | |
771 | ||
772 | wxASSERT(data); | |
773 | ||
774 | wxString search,path,filename; | |
775 | ||
776 | wxString dirName(data->m_path); | |
777 | ||
3bd8a405 | 778 | #if (defined(__WINDOWS__) && !defined(__WXWINCE__)) || defined(__DOS__) || defined(__OS2__) |
51a58d8b JS |
779 | // Check if this is a root directory and if so, |
780 | // whether the drive is avaiable. | |
33fed835 MB |
781 | if (!wxIsDriveAvailable(dirName)) |
782 | { | |
ca65c044 | 783 | data->m_isExpanded = false; |
7328394a JS |
784 | //wxMessageBox(wxT("Sorry, this drive is not available.")); |
785 | return; | |
33fed835 | 786 | } |
51a58d8b JS |
787 | #endif |
788 | ||
789 | // This may take a longish time. Go to busy cursor | |
790 | wxBusyCursor busy; | |
791 | ||
0d853c54 | 792 | #if defined(__WINDOWS__) || defined(__DOS__) || defined(__OS2__) |
51a58d8b JS |
793 | if (dirName.Last() == ':') |
794 | dirName += wxString(wxFILE_SEP_PATH); | |
795 | #endif | |
796 | ||
797 | wxArrayString dirs; | |
798 | wxArrayString filenames; | |
799 | ||
800 | wxDir d; | |
801 | wxString eachFilename; | |
802 | ||
f9c165b1 | 803 | wxLogNull log; |
51a58d8b JS |
804 | d.Open(dirName); |
805 | ||
806 | if (d.IsOpened()) | |
807 | { | |
42dcacf0 RR |
808 | int style = wxDIR_DIRS; |
809 | if (m_showHidden) style |= wxDIR_HIDDEN; | |
810 | if (d.GetFirst(& eachFilename, wxEmptyString, style)) | |
51a58d8b JS |
811 | { |
812 | do | |
813 | { | |
814 | if ((eachFilename != wxT(".")) && (eachFilename != wxT(".."))) | |
815 | { | |
816 | dirs.Add(eachFilename); | |
817 | } | |
818 | } | |
2b0a7c09 | 819 | while (d.GetNext(&eachFilename)); |
51a58d8b JS |
820 | } |
821 | } | |
222ed1d6 | 822 | dirs.Sort(wxDirCtrlStringCompareFunction); |
51a58d8b JS |
823 | |
824 | // Now do the filenames -- but only if we're allowed to | |
d9c98a9a | 825 | if (!HasFlag(wxDIRCTRL_DIR_ONLY)) |
51a58d8b JS |
826 | { |
827 | d.Open(dirName); | |
ec1b28a3 | 828 | |
51a58d8b JS |
829 | if (d.IsOpened()) |
830 | { | |
2b0a7c09 RN |
831 | int style = wxDIR_FILES; |
832 | if (m_showHidden) style |= wxDIR_HIDDEN; | |
3da4e4bd JS |
833 | // Process each filter (ex: "JPEG Files (*.jpg;*.jpeg)|*.jpg;*.jpeg") |
834 | wxStringTokenizer strTok; | |
835 | wxString curFilter; | |
836 | strTok.SetString(m_currentFilterStr,wxT(";")); | |
837 | while(strTok.HasMoreTokens()) | |
51a58d8b | 838 | { |
3da4e4bd | 839 | curFilter = strTok.GetNextToken(); |
f0e5a44d | 840 | if (d.GetFirst(& eachFilename, curFilter, style)) |
51a58d8b | 841 | { |
3da4e4bd | 842 | do |
51a58d8b | 843 | { |
3da4e4bd JS |
844 | if ((eachFilename != wxT(".")) && (eachFilename != wxT(".."))) |
845 | { | |
846 | filenames.Add(eachFilename); | |
847 | } | |
51a58d8b | 848 | } |
3da4e4bd | 849 | while (d.GetNext(& eachFilename)); |
51a58d8b | 850 | } |
51a58d8b JS |
851 | } |
852 | } | |
222ed1d6 | 853 | filenames.Sort(wxDirCtrlStringCompareFunction); |
51a58d8b JS |
854 | } |
855 | ||
c72fa1cd VZ |
856 | // Now we really know whether we have any children so tell the tree control |
857 | // about it. | |
858 | m_treeCtrl->SetItemHasChildren(parentId, !dirs.empty() || !filenames.empty()); | |
859 | ||
51a58d8b JS |
860 | // Add the sorted dirs |
861 | size_t i; | |
b4a980f4 | 862 | for (i = 0; i < dirs.GetCount(); i++) |
51a58d8b | 863 | { |
4e115ed2 | 864 | eachFilename = dirs[i]; |
51a58d8b | 865 | path = dirName; |
083f7497 | 866 | if (!wxEndsWithPathSeparator(path)) |
51a58d8b JS |
867 | path += wxString(wxFILE_SEP_PATH); |
868 | path += eachFilename; | |
869 | ||
ca65c044 | 870 | wxDirItemData *dir_item = new wxDirItemData(path,eachFilename,true); |
c1ec7ee8 | 871 | wxTreeItemId treeid = AppendItem( parentId, eachFilename, |
06cc1fb9 | 872 | wxFileIconsTable::folder, -1, dir_item); |
c1ec7ee8 | 873 | m_treeCtrl->SetItemImage( treeid, wxFileIconsTable::folder_open, |
06cc1fb9 | 874 | wxTreeItemIcon_Expanded ); |
ec1b28a3 | 875 | |
d9c98a9a VZ |
876 | // assume that it does have children by default as it can take a long |
877 | // time to really check for this (think remote drives...) | |
878 | // | |
c72fa1cd | 879 | // and if we're wrong, we'll correct the icon later if |
d9c98a9a | 880 | // the user really tries to open this item |
c1ec7ee8 | 881 | m_treeCtrl->SetItemHasChildren(treeid); |
51a58d8b JS |
882 | } |
883 | ||
884 | // Add the sorted filenames | |
d9c98a9a | 885 | if (!HasFlag(wxDIRCTRL_DIR_ONLY)) |
51a58d8b | 886 | { |
b4a980f4 | 887 | for (i = 0; i < filenames.GetCount(); i++) |
51a58d8b | 888 | { |
4e115ed2 | 889 | eachFilename = filenames[i]; |
51a58d8b | 890 | path = dirName; |
083f7497 | 891 | if (!wxEndsWithPathSeparator(path)) |
51a58d8b JS |
892 | path += wxString(wxFILE_SEP_PATH); |
893 | path += eachFilename; | |
894 | //path = dirName + wxString(wxT("/")) + eachFilename; | |
ca65c044 | 895 | wxDirItemData *dir_item = new wxDirItemData(path,eachFilename,false); |
06cc1fb9 JS |
896 | int image_id = wxFileIconsTable::file; |
897 | if (eachFilename.Find(wxT('.')) != wxNOT_FOUND) | |
898 | image_id = wxTheFileIconsTable->GetIconID(eachFilename.AfterLast(wxT('.'))); | |
22328fa4 | 899 | (void) AppendItem( parentId, eachFilename, image_id, -1, dir_item); |
51a58d8b JS |
900 | } |
901 | } | |
902 | } | |
903 | ||
ef57807f VZ |
904 | void wxGenericDirCtrl::ExpandDir(wxTreeItemId parentId) |
905 | { | |
906 | // ExpandDir() will not actually expand the tree node, just populate it | |
907 | PopulateNode(parentId); | |
908 | } | |
909 | ||
08887820 VS |
910 | void wxGenericDirCtrl::ReCreateTree() |
911 | { | |
912 | CollapseDir(m_treeCtrl->GetRootItem()); | |
c06dde42 JS |
913 | ExpandRoot(); |
914 | } | |
915 | ||
916 | void wxGenericDirCtrl::CollapseTree() | |
917 | { | |
918 | wxTreeItemIdValue cookie; | |
919 | wxTreeItemId child = m_treeCtrl->GetFirstChild(m_rootId, cookie); | |
920 | while (child.IsOk()) | |
921 | { | |
922 | CollapseDir(child); | |
923 | child = m_treeCtrl->GetNextChild(m_rootId, cookie); | |
924 | } | |
08887820 VS |
925 | } |
926 | ||
51a58d8b JS |
927 | // Find the child that matches the first part of 'path'. |
928 | // E.g. if a child path is "/usr" and 'path' is "/usr/include" | |
929 | // then the child for /usr is returned. | |
930 | wxTreeItemId wxGenericDirCtrl::FindChild(wxTreeItemId parentId, const wxString& path, bool& done) | |
931 | { | |
932 | wxString path2(path); | |
ec1b28a3 | 933 | |
51a58d8b JS |
934 | // Make sure all separators are as per the current platform |
935 | path2.Replace(wxT("\\"), wxString(wxFILE_SEP_PATH)); | |
936 | path2.Replace(wxT("/"), wxString(wxFILE_SEP_PATH)); | |
ec1b28a3 | 937 | |
51a58d8b JS |
938 | // Append a separator to foil bogus substring matching |
939 | path2 += wxString(wxFILE_SEP_PATH); | |
ec1b28a3 DW |
940 | |
941 | // In MSW or PM, case is not significant | |
0d853c54 | 942 | #if defined(__WINDOWS__) || defined(__DOS__) || defined(__OS2__) |
51a58d8b JS |
943 | path2.MakeLower(); |
944 | #endif | |
ec1b28a3 | 945 | |
2d75caaa | 946 | wxTreeItemIdValue cookie; |
51a58d8b | 947 | wxTreeItemId childId = m_treeCtrl->GetFirstChild(parentId, cookie); |
53ccf1c0 | 948 | while (childId.IsOk()) |
51a58d8b | 949 | { |
0d6f66f4 | 950 | wxDirItemData* data = GetItemData(childId); |
ec1b28a3 | 951 | |
44d60c0b | 952 | if (data && !data->m_path.empty()) |
51a58d8b JS |
953 | { |
954 | wxString childPath(data->m_path); | |
083f7497 | 955 | if (!wxEndsWithPathSeparator(childPath)) |
51a58d8b | 956 | childPath += wxString(wxFILE_SEP_PATH); |
ec1b28a3 DW |
957 | |
958 | // In MSW and PM, case is not significant | |
0d853c54 | 959 | #if defined(__WINDOWS__) || defined(__DOS__) || defined(__OS2__) |
51a58d8b JS |
960 | childPath.MakeLower(); |
961 | #endif | |
ec1b28a3 | 962 | |
b36e08d0 | 963 | if (childPath.length() <= path2.length()) |
51a58d8b | 964 | { |
b36e08d0 | 965 | wxString path3 = path2.Mid(0, childPath.length()); |
51a58d8b JS |
966 | if (childPath == path3) |
967 | { | |
b36e08d0 | 968 | if (path3.length() == path2.length()) |
ca65c044 | 969 | done = true; |
51a58d8b | 970 | else |
ca65c044 | 971 | done = false; |
51a58d8b JS |
972 | return childId; |
973 | } | |
974 | } | |
975 | } | |
ec1b28a3 | 976 | |
748fcded | 977 | childId = m_treeCtrl->GetNextChild(parentId, cookie); |
51a58d8b | 978 | } |
3fa4bd0e VS |
979 | wxTreeItemId invalid; |
980 | return invalid; | |
51a58d8b JS |
981 | } |
982 | ||
983 | // Try to expand as much of the given path as possible, | |
984 | // and select the given tree item. | |
985 | bool wxGenericDirCtrl::ExpandPath(const wxString& path) | |
986 | { | |
ca65c044 | 987 | bool done = false; |
c1ec7ee8 SC |
988 | wxTreeItemId treeid = FindChild(m_rootId, path, done); |
989 | wxTreeItemId lastId = treeid; // The last non-zero treeid | |
990 | while (treeid.IsOk() && !done) | |
51a58d8b | 991 | { |
c1ec7ee8 | 992 | ExpandDir(treeid); |
51a58d8b | 993 | |
c1ec7ee8 SC |
994 | treeid = FindChild(treeid, path, done); |
995 | if (treeid.IsOk()) | |
996 | lastId = treeid; | |
51a58d8b | 997 | } |
4e115ed2 VZ |
998 | if (!lastId.IsOk()) |
999 | return false; | |
1000 | ||
0d6f66f4 | 1001 | wxDirItemData *data = GetItemData(lastId); |
4e115ed2 | 1002 | if (data->m_isDir) |
51a58d8b | 1003 | { |
4e115ed2 VZ |
1004 | m_treeCtrl->Expand(lastId); |
1005 | } | |
d9c98a9a | 1006 | if (HasFlag(wxDIRCTRL_SELECT_FIRST) && data->m_isDir) |
4e115ed2 VZ |
1007 | { |
1008 | // Find the first file in this directory | |
1009 | wxTreeItemIdValue cookie; | |
1010 | wxTreeItemId childId = m_treeCtrl->GetFirstChild(lastId, cookie); | |
1011 | bool selectedChild = false; | |
1012 | while (childId.IsOk()) | |
51a58d8b | 1013 | { |
0d6f66f4 | 1014 | data = GetItemData(childId); |
ec1b28a3 | 1015 | |
4e115ed2 | 1016 | if (data && data->m_path != wxEmptyString && !data->m_isDir) |
51a58d8b | 1017 | { |
4e115ed2 VZ |
1018 | m_treeCtrl->SelectItem(childId); |
1019 | m_treeCtrl->EnsureVisible(childId); | |
1020 | selectedChild = true; | |
1021 | break; | |
51a58d8b | 1022 | } |
4e115ed2 | 1023 | childId = m_treeCtrl->GetNextChild(lastId, cookie); |
51a58d8b | 1024 | } |
4e115ed2 | 1025 | if (!selectedChild) |
51a58d8b JS |
1026 | { |
1027 | m_treeCtrl->SelectItem(lastId); | |
1028 | m_treeCtrl->EnsureVisible(lastId); | |
1029 | } | |
51a58d8b JS |
1030 | } |
1031 | else | |
4e115ed2 VZ |
1032 | { |
1033 | m_treeCtrl->SelectItem(lastId); | |
1034 | m_treeCtrl->EnsureVisible(lastId); | |
1035 | } | |
1036 | ||
1037 | return true; | |
51a58d8b JS |
1038 | } |
1039 | ||
f395a825 VZ |
1040 | |
1041 | bool wxGenericDirCtrl::CollapsePath(const wxString& path) | |
1042 | { | |
1043 | bool done = false; | |
c1ec7ee8 SC |
1044 | wxTreeItemId treeid = FindChild(m_rootId, path, done); |
1045 | wxTreeItemId lastId = treeid; // The last non-zero treeid | |
f395a825 | 1046 | |
c1ec7ee8 | 1047 | while ( treeid.IsOk() && !done ) |
f395a825 | 1048 | { |
c1ec7ee8 | 1049 | CollapseDir(treeid); |
f395a825 | 1050 | |
c1ec7ee8 | 1051 | treeid = FindChild(treeid, path, done); |
f395a825 | 1052 | |
c1ec7ee8 SC |
1053 | if ( treeid.IsOk() ) |
1054 | lastId = treeid; | |
f395a825 VZ |
1055 | } |
1056 | ||
1057 | if ( !lastId.IsOk() ) | |
1058 | return false; | |
1059 | ||
1060 | m_treeCtrl->SelectItem(lastId); | |
1061 | m_treeCtrl->EnsureVisible(lastId); | |
1062 | ||
1063 | return true; | |
1064 | } | |
1065 | ||
0d6f66f4 VZ |
1066 | wxDirItemData* wxGenericDirCtrl::GetItemData(wxTreeItemId itemId) |
1067 | { | |
1068 | return static_cast<wxDirItemData*>(m_treeCtrl->GetItemData(itemId)); | |
1069 | } | |
1070 | ||
e3f084fd VZ |
1071 | wxString wxGenericDirCtrl::GetPath(wxTreeItemId itemId) const |
1072 | { | |
1073 | const wxDirItemData* | |
1074 | data = static_cast<wxDirItemData*>(m_treeCtrl->GetItemData(itemId)); | |
1075 | ||
1076 | return data->m_path; | |
1077 | } | |
f395a825 | 1078 | |
51a58d8b JS |
1079 | wxString wxGenericDirCtrl::GetPath() const |
1080 | { | |
2dd62dc0 RR |
1081 | // Allow calling GetPath() in multiple selection from OnSelFilter |
1082 | if (m_treeCtrl->HasFlag(wxTR_MULTIPLE)) | |
1083 | { | |
1084 | wxArrayTreeItemIds items; | |
1085 | m_treeCtrl->GetSelections(items); | |
1086 | if (items.size() > 0) | |
1087 | { | |
1088 | // return first string only | |
c1ec7ee8 | 1089 | wxTreeItemId treeid = items[0]; |
e3f084fd | 1090 | return GetPath(treeid); |
2dd62dc0 | 1091 | } |
ce00f59b | 1092 | |
2dd62dc0 RR |
1093 | return wxEmptyString; |
1094 | } | |
1095 | ||
c1ec7ee8 SC |
1096 | wxTreeItemId treeid = m_treeCtrl->GetSelection(); |
1097 | if (treeid) | |
51a58d8b | 1098 | { |
e3f084fd | 1099 | return GetPath(treeid); |
51a58d8b JS |
1100 | } |
1101 | else | |
1102 | return wxEmptyString; | |
1103 | } | |
1104 | ||
80f624ec VZ |
1105 | void wxGenericDirCtrl::GetPaths(wxArrayString& paths) const |
1106 | { | |
1107 | paths.clear(); | |
1108 | ||
1109 | wxArrayTreeItemIds items; | |
1110 | m_treeCtrl->GetSelections(items); | |
1111 | for ( unsigned n = 0; n < items.size(); n++ ) | |
1112 | { | |
c1ec7ee8 | 1113 | wxTreeItemId treeid = items[n]; |
e3f084fd | 1114 | paths.push_back(GetPath(treeid)); |
80f624ec VZ |
1115 | } |
1116 | } | |
1117 | ||
51a58d8b JS |
1118 | wxString wxGenericDirCtrl::GetFilePath() const |
1119 | { | |
c1ec7ee8 SC |
1120 | wxTreeItemId treeid = m_treeCtrl->GetSelection(); |
1121 | if (treeid) | |
51a58d8b | 1122 | { |
c1ec7ee8 | 1123 | wxDirItemData* data = (wxDirItemData*) m_treeCtrl->GetItemData(treeid); |
51a58d8b JS |
1124 | if (data->m_isDir) |
1125 | return wxEmptyString; | |
1126 | else | |
1127 | return data->m_path; | |
1128 | } | |
1129 | else | |
1130 | return wxEmptyString; | |
1131 | } | |
1132 | ||
80f624ec VZ |
1133 | void wxGenericDirCtrl::GetFilePaths(wxArrayString& paths) const |
1134 | { | |
1135 | paths.clear(); | |
1136 | ||
1137 | wxArrayTreeItemIds items; | |
1138 | m_treeCtrl->GetSelections(items); | |
1139 | for ( unsigned n = 0; n < items.size(); n++ ) | |
1140 | { | |
c1ec7ee8 SC |
1141 | wxTreeItemId treeid = items[n]; |
1142 | wxDirItemData* data = (wxDirItemData*) m_treeCtrl->GetItemData(treeid); | |
80f624ec VZ |
1143 | if ( !data->m_isDir ) |
1144 | paths.Add(data->m_path); | |
1145 | } | |
1146 | } | |
1147 | ||
51a58d8b JS |
1148 | void wxGenericDirCtrl::SetPath(const wxString& path) |
1149 | { | |
1150 | m_defaultPath = path; | |
1151 | if (m_rootId) | |
1152 | ExpandPath(path); | |
1153 | } | |
1154 | ||
80f624ec VZ |
1155 | void wxGenericDirCtrl::SelectPath(const wxString& path, bool select) |
1156 | { | |
1157 | bool done = false; | |
c1ec7ee8 SC |
1158 | wxTreeItemId treeid = FindChild(m_rootId, path, done); |
1159 | wxTreeItemId lastId = treeid; // The last non-zero treeid | |
1160 | while ( treeid.IsOk() && !done ) | |
80f624ec | 1161 | { |
c1ec7ee8 SC |
1162 | treeid = FindChild(treeid, path, done); |
1163 | if ( treeid.IsOk() ) | |
1164 | lastId = treeid; | |
80f624ec VZ |
1165 | } |
1166 | if ( !lastId.IsOk() ) | |
1167 | return; | |
1168 | ||
1169 | if ( done ) | |
1170 | { | |
c1ec7ee8 | 1171 | m_treeCtrl->SelectItem(treeid, select); |
80f624ec VZ |
1172 | } |
1173 | } | |
1174 | ||
1175 | void wxGenericDirCtrl::SelectPaths(const wxArrayString& paths) | |
1176 | { | |
1177 | if ( HasFlag(wxDIRCTRL_MULTIPLE) ) | |
1178 | { | |
1179 | UnselectAll(); | |
1180 | for ( unsigned n = 0; n < paths.size(); n++ ) | |
1181 | { | |
1182 | SelectPath(paths[n]); | |
1183 | } | |
1184 | } | |
1185 | } | |
1186 | ||
1187 | void wxGenericDirCtrl::UnselectAll() | |
1188 | { | |
1189 | m_treeCtrl->UnselectAll(); | |
1190 | } | |
1191 | ||
51a58d8b JS |
1192 | // Not used |
1193 | #if 0 | |
c1ec7ee8 | 1194 | void wxGenericDirCtrl::FindChildFiles(wxTreeItemId treeid, int dirFlags, wxArrayString& filenames) |
51a58d8b | 1195 | { |
c1ec7ee8 | 1196 | wxDirItemData *data = (wxDirItemData *) m_treeCtrl->GetItemData(treeid); |
51a58d8b JS |
1197 | |
1198 | // This may take a longish time. Go to busy cursor | |
1199 | wxBusyCursor busy; | |
1200 | ||
1201 | wxASSERT(data); | |
1202 | ||
1203 | wxString search,path,filename; | |
1204 | ||
1205 | wxString dirName(data->m_path); | |
1206 | ||
bb5a9514 | 1207 | #if defined(__WINDOWS__) || defined(__OS2__) |
51a58d8b JS |
1208 | if (dirName.Last() == ':') |
1209 | dirName += wxString(wxFILE_SEP_PATH); | |
1210 | #endif | |
1211 | ||
1212 | wxDir d; | |
1213 | wxString eachFilename; | |
1214 | ||
f9c165b1 | 1215 | wxLogNull log; |
51a58d8b JS |
1216 | d.Open(dirName); |
1217 | ||
1218 | if (d.IsOpened()) | |
1219 | { | |
1220 | if (d.GetFirst(& eachFilename, m_currentFilterStr, dirFlags)) | |
1221 | { | |
1222 | do | |
1223 | { | |
1224 | if ((eachFilename != wxT(".")) && (eachFilename != wxT(".."))) | |
1225 | { | |
1226 | filenames.Add(eachFilename); | |
1227 | } | |
1228 | } | |
1229 | while (d.GetNext(& eachFilename)) ; | |
1230 | } | |
1231 | } | |
1232 | } | |
1233 | #endif | |
1234 | ||
1235 | void wxGenericDirCtrl::SetFilterIndex(int n) | |
1236 | { | |
1237 | m_currentFilter = n; | |
1238 | ||
1239 | wxString f, d; | |
1240 | if (ExtractWildcard(m_filter, n, f, d)) | |
1241 | m_currentFilterStr = f; | |
1242 | else | |
dd0138d9 RR |
1243 | #ifdef __UNIX__ |
1244 | m_currentFilterStr = wxT("*"); | |
1245 | #else | |
51a58d8b | 1246 | m_currentFilterStr = wxT("*.*"); |
dd0138d9 | 1247 | #endif |
51a58d8b JS |
1248 | } |
1249 | ||
1250 | void wxGenericDirCtrl::SetFilter(const wxString& filter) | |
1251 | { | |
1252 | m_filter = filter; | |
1253 | ||
d0bc78e2 VZ |
1254 | if (!filter.empty() && !m_filterListCtrl) |
1255 | m_filterListCtrl = new wxDirFilterListCtrl(this, wxID_FILTERLISTCTRL); | |
1256 | else if (filter.empty() && m_filterListCtrl) | |
1257 | { | |
1258 | m_filterListCtrl->Destroy(); | |
1259 | m_filterListCtrl = NULL; | |
1260 | } | |
1261 | ||
51a58d8b JS |
1262 | wxString f, d; |
1263 | if (ExtractWildcard(m_filter, m_currentFilter, f, d)) | |
1264 | m_currentFilterStr = f; | |
1265 | else | |
dd0138d9 RR |
1266 | #ifdef __UNIX__ |
1267 | m_currentFilterStr = wxT("*"); | |
1268 | #else | |
51a58d8b | 1269 | m_currentFilterStr = wxT("*.*"); |
dd0138d9 | 1270 | #endif |
d0bc78e2 VZ |
1271 | // current filter index is meaningless after filter change, set it to zero |
1272 | SetFilterIndex(0); | |
1273 | if (m_filterListCtrl) | |
1274 | m_filterListCtrl->FillFilterList(m_filter, 0); | |
51a58d8b JS |
1275 | } |
1276 | ||
1277 | // Extract description and actual filter from overall filter string | |
1278 | bool wxGenericDirCtrl::ExtractWildcard(const wxString& filterStr, int n, wxString& filter, wxString& description) | |
1279 | { | |
1280 | wxArrayString filters, descriptions; | |
daf32463 | 1281 | int count = wxParseCommonDialogsFilter(filterStr, descriptions, filters); |
51a58d8b JS |
1282 | if (count > 0 && n < count) |
1283 | { | |
1284 | filter = filters[n]; | |
1285 | description = descriptions[n]; | |
ca65c044 | 1286 | return true; |
51a58d8b | 1287 | } |
5716a1ab | 1288 | |
ca65c044 | 1289 | return false; |
51a58d8b JS |
1290 | } |
1291 | ||
51a58d8b JS |
1292 | |
1293 | void wxGenericDirCtrl::DoResize() | |
1294 | { | |
1295 | wxSize sz = GetClientSize(); | |
1296 | int verticalSpacing = 3; | |
1297 | if (m_treeCtrl) | |
1298 | { | |
1299 | wxSize filterSz ; | |
1300 | if (m_filterListCtrl) | |
1301 | { | |
1302 | filterSz = m_filterListCtrl->GetSize(); | |
1303 | sz.y -= (filterSz.y + verticalSpacing); | |
1304 | } | |
1305 | m_treeCtrl->SetSize(0, 0, sz.x, sz.y); | |
1306 | if (m_filterListCtrl) | |
1307 | { | |
1308 | m_filterListCtrl->SetSize(0, sz.y + verticalSpacing, sz.x, filterSz.y); | |
1309 | // Don't know why, but this needs refreshing after a resize (wxMSW) | |
1310 | m_filterListCtrl->Refresh(); | |
1311 | } | |
1312 | } | |
1313 | } | |
1314 | ||
1315 | ||
33ac7e6f | 1316 | void wxGenericDirCtrl::OnSize(wxSizeEvent& WXUNUSED(event)) |
51a58d8b JS |
1317 | { |
1318 | DoResize(); | |
1319 | } | |
1320 | ||
22328fa4 | 1321 | wxTreeItemId wxGenericDirCtrl::AppendItem (const wxTreeItemId & parent, |
ca65c044 WS |
1322 | const wxString & text, |
1323 | int image, int selectedImage, | |
1324 | wxTreeItemData * data) | |
22328fa4 JS |
1325 | { |
1326 | wxTreeCtrl *treeCtrl = GetTreeCtrl (); | |
1327 | ||
1328 | wxASSERT (treeCtrl); | |
1329 | ||
1330 | if (treeCtrl) | |
1331 | { | |
1332 | return treeCtrl->AppendItem (parent, text, image, selectedImage, data); | |
1333 | } | |
1334 | else | |
1335 | { | |
1336 | return wxTreeItemId(); | |
1337 | } | |
1338 | } | |
1339 | ||
1340 | ||
51a58d8b JS |
1341 | //----------------------------------------------------------------------------- |
1342 | // wxDirFilterListCtrl | |
1343 | //----------------------------------------------------------------------------- | |
1344 | ||
1345 | IMPLEMENT_CLASS(wxDirFilterListCtrl, wxChoice) | |
1346 | ||
1347 | BEGIN_EVENT_TABLE(wxDirFilterListCtrl, wxChoice) | |
ca65c044 | 1348 | EVT_CHOICE(wxID_ANY, wxDirFilterListCtrl::OnSelFilter) |
51a58d8b JS |
1349 | END_EVENT_TABLE() |
1350 | ||
d0bc78e2 | 1351 | bool wxDirFilterListCtrl::Create(wxGenericDirCtrl* parent, |
c1ec7ee8 | 1352 | const wxWindowID treeid, |
d0bc78e2 VZ |
1353 | const wxPoint& pos, |
1354 | const wxSize& size, | |
1355 | long style) | |
51a58d8b JS |
1356 | { |
1357 | m_dirCtrl = parent; | |
d0bc78e2 VZ |
1358 | |
1359 | // by default our border style is determined by the style of our parent | |
1360 | if ( !(style & wxBORDER_MASK) ) | |
1361 | { | |
1362 | style |= parent->HasFlag(wxDIRCTRL_3D_INTERNAL) ? wxBORDER_SUNKEN | |
1363 | : wxBORDER_NONE; | |
1364 | } | |
1365 | ||
c1ec7ee8 | 1366 | return wxChoice::Create(parent, treeid, pos, size, 0, NULL, style); |
51a58d8b JS |
1367 | } |
1368 | ||
1369 | void wxDirFilterListCtrl::Init() | |
1370 | { | |
1371 | m_dirCtrl = NULL; | |
1372 | } | |
1373 | ||
33ac7e6f | 1374 | void wxDirFilterListCtrl::OnSelFilter(wxCommandEvent& WXUNUSED(event)) |
51a58d8b JS |
1375 | { |
1376 | int sel = GetSelection(); | |
1377 | ||
9e48865c SL |
1378 | if (m_dirCtrl->HasFlag(wxDIRCTRL_MULTIPLE)) |
1379 | { | |
1380 | wxArrayString paths; | |
1381 | m_dirCtrl->GetPaths(paths); | |
1382 | ||
1383 | m_dirCtrl->SetFilterIndex(sel); | |
ec1b28a3 | 1384 | |
9e48865c SL |
1385 | // If the filter has changed, the view is out of date, so |
1386 | // collapse the tree. | |
1387 | m_dirCtrl->ReCreateTree(); | |
1388 | ||
1389 | // Expand and select the previously selected paths | |
1390 | for (unsigned int i = 0; i < paths.GetCount(); i++) | |
1391 | { | |
1392 | m_dirCtrl->ExpandPath(paths.Item(i)); | |
1393 | } | |
1394 | } | |
1395 | else | |
1396 | { | |
1397 | wxString currentPath = m_dirCtrl->GetPath(); | |
51a58d8b | 1398 | |
9e48865c SL |
1399 | m_dirCtrl->SetFilterIndex(sel); |
1400 | m_dirCtrl->ReCreateTree(); | |
51a58d8b | 1401 | |
9e48865c SL |
1402 | // Try to restore the selection, or at least the directory |
1403 | m_dirCtrl->ExpandPath(currentPath); | |
1404 | } | |
51a58d8b JS |
1405 | } |
1406 | ||
1407 | void wxDirFilterListCtrl::FillFilterList(const wxString& filter, int defaultFilter) | |
1408 | { | |
1409 | Clear(); | |
1410 | wxArrayString descriptions, filters; | |
866918a8 | 1411 | size_t n = (size_t) wxParseCommonDialogsFilter(filter, descriptions, filters); |
51a58d8b JS |
1412 | |
1413 | if (n > 0 && defaultFilter < (int) n) | |
1414 | { | |
999836aa | 1415 | for (size_t i = 0; i < n; i++) |
51a58d8b JS |
1416 | Append(descriptions[i]); |
1417 | SetSelection(defaultFilter); | |
1418 | } | |
1419 | } | |
13de0c8c | 1420 | #endif // wxUSE_DIRDLG |
51a58d8b | 1421 | |
13de0c8c | 1422 | #if wxUSE_DIRDLG || wxUSE_FILEDLG |
22328fa4 | 1423 | |
06cc1fb9 JS |
1424 | // ---------------------------------------------------------------------------- |
1425 | // wxFileIconsTable icons | |
1426 | // ---------------------------------------------------------------------------- | |
1427 | ||
ff654490 | 1428 | #ifndef __WXGTK20__ |
13b22a67 | 1429 | /* Computer (c) Julian Smart */ |
a243da29 | 1430 | static const char* const file_icons_tbl_computer_xpm[] = { |
13b22a67 JS |
1431 | /* columns rows colors chars-per-pixel */ |
1432 | "16 16 42 1", | |
1433 | "r c #4E7FD0", | |
1434 | "$ c #7198D9", | |
1435 | "; c #DCE6F6", | |
1436 | "q c #FFFFFF", | |
1437 | "u c #4A7CCE", | |
1438 | "# c #779DDB", | |
1439 | "w c #95B2E3", | |
1440 | "y c #7FA2DD", | |
1441 | "f c #3263B4", | |
1442 | "= c #EAF0FA", | |
1443 | "< c #B1C7EB", | |
1444 | "% c #6992D7", | |
1445 | "9 c #D9E4F5", | |
1446 | "o c #9BB7E5", | |
1447 | "6 c #F7F9FD", | |
1448 | ", c #BED0EE", | |
1449 | "3 c #F0F5FC", | |
1450 | "1 c #A8C0E8", | |
1451 | " c None", | |
1452 | "0 c #FDFEFF", | |
1453 | "4 c #C4D5F0", | |
1454 | "@ c #81A4DD", | |
1455 | "e c #4377CD", | |
1456 | "- c #E2EAF8", | |
1457 | "i c #9FB9E5", | |
1458 | "> c #CCDAF2", | |
1459 | "+ c #89A9DF", | |
1460 | "s c #5584D1", | |
1461 | "t c #5D89D3", | |
1462 | ": c #D2DFF4", | |
1463 | "5 c #FAFCFE", | |
1464 | "2 c #F5F8FD", | |
1465 | "8 c #DFE8F7", | |
1466 | "& c #5E8AD4", | |
1467 | "X c #638ED5", | |
1468 | "a c #CEDCF2", | |
1469 | "p c #90AFE2", | |
1470 | "d c #2F5DA9", | |
1471 | "* c #5282D0", | |
1472 | "7 c #E5EDF9", | |
1473 | ". c #A2BCE6", | |
1474 | "O c #8CACE0", | |
1475 | /* pixels */ | |
1476 | " ", | |
1477 | " .XXXXXXXXXXX ", | |
1478 | " oXO++@#$%&*X ", | |
1479 | " oX=-;:>,<1%X ", | |
1480 | " oX23=-;:4,$X ", | |
1481 | " oX5633789:@X ", | |
1482 | " oX05623=78+X ", | |
1483 | " oXqq05623=OX ", | |
1484 | " oX,,,,,<<<$X ", | |
1485 | " wXXXXXXXXXXe ", | |
1486 | " XrtX%$$y@+O,, ", | |
1487 | " uyiiiiiiiii@< ", | |
1488 | " ouiiiiiiiiiip<a", | |
1489 | " rustX%$$y@+Ow,,", | |
1490 | " dfffffffffffffd", | |
1491 | " " | |
1492 | }; | |
ff654490 | 1493 | #endif // !GTK+ 2 |
06cc1fb9 | 1494 | |
06cc1fb9 JS |
1495 | // ---------------------------------------------------------------------------- |
1496 | // wxFileIconsTable & friends | |
1497 | // ---------------------------------------------------------------------------- | |
1498 | ||
1499 | // global instance of a wxFileIconsTable | |
d3b9f782 | 1500 | wxFileIconsTable* wxTheFileIconsTable = NULL; |
06cc1fb9 JS |
1501 | |
1502 | // A module to allow icons table cleanup | |
1503 | ||
1504 | class wxFileIconsTableModule: public wxModule | |
1505 | { | |
1506 | DECLARE_DYNAMIC_CLASS(wxFileIconsTableModule) | |
1507 | public: | |
1508 | wxFileIconsTableModule() {} | |
ca65c044 | 1509 | bool OnInit() { wxTheFileIconsTable = new wxFileIconsTable; return true; } |
06cc1fb9 JS |
1510 | void OnExit() |
1511 | { | |
5276b0a5 | 1512 | wxDELETE(wxTheFileIconsTable); |
06cc1fb9 JS |
1513 | } |
1514 | }; | |
1515 | ||
1516 | IMPLEMENT_DYNAMIC_CLASS(wxFileIconsTableModule, wxModule) | |
1517 | ||
1518 | class wxFileIconEntry : public wxObject | |
1519 | { | |
1520 | public: | |
c1ec7ee8 | 1521 | wxFileIconEntry(int i) { iconid = i; } |
06cc1fb9 | 1522 | |
c1ec7ee8 | 1523 | int iconid; |
06cc1fb9 JS |
1524 | }; |
1525 | ||
1526 | wxFileIconsTable::wxFileIconsTable() | |
1527 | { | |
1528 | m_HashTable = NULL; | |
1529 | m_smallImageList = NULL; | |
1530 | } | |
1531 | ||
1532 | wxFileIconsTable::~wxFileIconsTable() | |
1533 | { | |
222ed1d6 MB |
1534 | if (m_HashTable) |
1535 | { | |
1536 | WX_CLEAR_HASH_TABLE(*m_HashTable); | |
1537 | delete m_HashTable; | |
1538 | } | |
06cc1fb9 JS |
1539 | if (m_smallImageList) delete m_smallImageList; |
1540 | } | |
1541 | ||
1542 | // delayed initialization - wait until first use (wxArtProv not created yet) | |
1543 | void wxFileIconsTable::Create() | |
1544 | { | |
1545 | wxCHECK_RET(!m_smallImageList && !m_HashTable, wxT("creating icons twice")); | |
06cc1fb9 JS |
1546 | m_HashTable = new wxHashTable(wxKEY_STRING); |
1547 | m_smallImageList = new wxImageList(16, 16); | |
1548 | ||
06cc1fb9 | 1549 | // folder: |
4b92a33f VS |
1550 | m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_FOLDER, |
1551 | wxART_CMN_DIALOG, | |
1552 | wxSize(16, 16))); | |
06cc1fb9 | 1553 | // folder_open |
82c1f2a3 RR |
1554 | m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_FOLDER_OPEN, |
1555 | wxART_CMN_DIALOG, | |
1556 | wxSize(16, 16))); | |
06cc1fb9 | 1557 | // computer |
ff654490 | 1558 | #ifdef __WXGTK20__ |
82c1f2a3 RR |
1559 | // GTK24 uses this icon in the file open dialog |
1560 | m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_HARDDISK, | |
1561 | wxART_CMN_DIALOG, | |
1562 | wxSize(16, 16))); | |
1563 | #else | |
06cc1fb9 | 1564 | m_smallImageList->Add(wxIcon(file_icons_tbl_computer_xpm)); |
82c1f2a3 | 1565 | #endif |
06cc1fb9 | 1566 | // drive |
82c1f2a3 RR |
1567 | m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_HARDDISK, |
1568 | wxART_CMN_DIALOG, | |
1569 | wxSize(16, 16))); | |
06cc1fb9 | 1570 | // cdrom |
82c1f2a3 RR |
1571 | m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_CDROM, |
1572 | wxART_CMN_DIALOG, | |
1573 | wxSize(16, 16))); | |
06cc1fb9 | 1574 | // floppy |
82c1f2a3 RR |
1575 | m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_FLOPPY, |
1576 | wxART_CMN_DIALOG, | |
1577 | wxSize(16, 16))); | |
06cc1fb9 | 1578 | // removeable |
82c1f2a3 RR |
1579 | m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_REMOVABLE, |
1580 | wxART_CMN_DIALOG, | |
1581 | wxSize(16, 16))); | |
06cc1fb9 | 1582 | // file |
4b92a33f VS |
1583 | m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_NORMAL_FILE, |
1584 | wxART_CMN_DIALOG, | |
1585 | wxSize(16, 16))); | |
06cc1fb9 | 1586 | // executable |
9a83f860 | 1587 | if (GetIconID(wxEmptyString, wxT("application/x-executable")) == file) |
06cc1fb9 | 1588 | { |
4b92a33f VS |
1589 | m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_EXECUTABLE_FILE, |
1590 | wxART_CMN_DIALOG, | |
1591 | wxSize(16, 16))); | |
9a83f860 VZ |
1592 | delete m_HashTable->Get(wxT("exe")); |
1593 | m_HashTable->Delete(wxT("exe")); | |
1594 | m_HashTable->Put(wxT("exe"), new wxFileIconEntry(executable)); | |
06cc1fb9 JS |
1595 | } |
1596 | /* else put into list by GetIconID | |
1597 | (KDE defines application/x-executable for *.exe and has nice icon) | |
1598 | */ | |
1599 | } | |
1600 | ||
1601 | wxImageList *wxFileIconsTable::GetSmallImageList() | |
1602 | { | |
1603 | if (!m_smallImageList) | |
1604 | Create(); | |
1605 | ||
1606 | return m_smallImageList; | |
1607 | } | |
1608 | ||
bb5a9514 | 1609 | #if wxUSE_MIMETYPE && wxUSE_IMAGE && (!defined(__WINDOWS__) || wxUSE_WXDIB) |
06cc1fb9 JS |
1610 | // VS: we don't need this function w/o wxMimeTypesManager because we'll only have |
1611 | // one icon and we won't resize it | |
1612 | ||
1613 | static wxBitmap CreateAntialiasedBitmap(const wxImage& img) | |
1614 | { | |
1d529ef7 | 1615 | const unsigned int size = 16; |
ca65c044 | 1616 | |
1d529ef7 | 1617 | wxImage smallimg (size, size); |
06cc1fb9 JS |
1618 | unsigned char *p1, *p2, *ps; |
1619 | unsigned char mr = img.GetMaskRed(), | |
1620 | mg = img.GetMaskGreen(), | |
1621 | mb = img.GetMaskBlue(); | |
1622 | ||
1623 | unsigned x, y; | |
1624 | unsigned sr, sg, sb, smask; | |
1625 | ||
1d529ef7 | 1626 | p1 = img.GetData(), p2 = img.GetData() + 3 * size*2, ps = smallimg.GetData(); |
06cc1fb9 JS |
1627 | smallimg.SetMaskColour(mr, mr, mr); |
1628 | ||
1d529ef7 | 1629 | for (y = 0; y < size; y++) |
06cc1fb9 | 1630 | { |
1d529ef7 | 1631 | for (x = 0; x < size; x++) |
06cc1fb9 JS |
1632 | { |
1633 | sr = sg = sb = smask = 0; | |
1634 | if (p1[0] != mr || p1[1] != mg || p1[2] != mb) | |
1635 | sr += p1[0], sg += p1[1], sb += p1[2]; | |
1636 | else smask++; | |
1637 | p1 += 3; | |
1638 | if (p1[0] != mr || p1[1] != mg || p1[2] != mb) | |
1639 | sr += p1[0], sg += p1[1], sb += p1[2]; | |
1640 | else smask++; | |
1641 | p1 += 3; | |
1642 | if (p2[0] != mr || p2[1] != mg || p2[2] != mb) | |
1643 | sr += p2[0], sg += p2[1], sb += p2[2]; | |
1644 | else smask++; | |
1645 | p2 += 3; | |
1646 | if (p2[0] != mr || p2[1] != mg || p2[2] != mb) | |
1647 | sr += p2[0], sg += p2[1], sb += p2[2]; | |
1648 | else smask++; | |
1649 | p2 += 3; | |
1650 | ||
1651 | if (smask > 2) | |
1652 | ps[0] = ps[1] = ps[2] = mr; | |
1653 | else | |
8253f2e0 WS |
1654 | { |
1655 | ps[0] = (unsigned char)(sr >> 2); | |
1656 | ps[1] = (unsigned char)(sg >> 2); | |
1657 | ps[2] = (unsigned char)(sb >> 2); | |
1658 | } | |
06cc1fb9 JS |
1659 | ps += 3; |
1660 | } | |
1d529ef7 | 1661 | p1 += size*2 * 3, p2 += size*2 * 3; |
06cc1fb9 | 1662 | } |
ca65c044 | 1663 | |
06cc1fb9 JS |
1664 | return wxBitmap(smallimg); |
1665 | } | |
1666 | ||
b126fe3a DS |
1667 | // This function is currently not unused anymore |
1668 | #if 0 | |
06cc1fb9 JS |
1669 | // finds empty borders and return non-empty area of image: |
1670 | static wxImage CutEmptyBorders(const wxImage& img) | |
1671 | { | |
1672 | unsigned char mr = img.GetMaskRed(), | |
1673 | mg = img.GetMaskGreen(), | |
1674 | mb = img.GetMaskBlue(); | |
1675 | unsigned char *dt = img.GetData(), *dttmp; | |
1676 | unsigned w = img.GetWidth(), h = img.GetHeight(); | |
1677 | ||
1678 | unsigned top, bottom, left, right, i; | |
1679 | bool empt; | |
1680 | ||
1681 | #define MK_DTTMP(x,y) dttmp = dt + ((x + y * w) * 3) | |
ca65c044 | 1682 | #define NOEMPTY_PIX(empt) if (dttmp[0] != mr || dttmp[1] != mg || dttmp[2] != mb) {empt = false; break;} |
06cc1fb9 | 1683 | |
ca65c044 | 1684 | for (empt = true, top = 0; empt && top < h; top++) |
06cc1fb9 JS |
1685 | { |
1686 | MK_DTTMP(0, top); | |
1687 | for (i = 0; i < w; i++, dttmp+=3) | |
1688 | NOEMPTY_PIX(empt) | |
1689 | } | |
ca65c044 | 1690 | for (empt = true, bottom = h-1; empt && bottom > top; bottom--) |
06cc1fb9 JS |
1691 | { |
1692 | MK_DTTMP(0, bottom); | |
1693 | for (i = 0; i < w; i++, dttmp+=3) | |
1694 | NOEMPTY_PIX(empt) | |
1695 | } | |
ca65c044 | 1696 | for (empt = true, left = 0; empt && left < w; left++) |
06cc1fb9 JS |
1697 | { |
1698 | MK_DTTMP(left, 0); | |
1699 | for (i = 0; i < h; i++, dttmp+=3*w) | |
1700 | NOEMPTY_PIX(empt) | |
1701 | } | |
ca65c044 | 1702 | for (empt = true, right = w-1; empt && right > left; right--) |
06cc1fb9 JS |
1703 | { |
1704 | MK_DTTMP(right, 0); | |
1705 | for (i = 0; i < h; i++, dttmp+=3*w) | |
1706 | NOEMPTY_PIX(empt) | |
1707 | } | |
1708 | top--, left--, bottom++, right++; | |
1709 | ||
1710 | return img.GetSubImage(wxRect(left, top, right - left + 1, bottom - top + 1)); | |
1711 | } | |
b126fe3a DS |
1712 | #endif // #if 0 |
1713 | ||
06cc1fb9 JS |
1714 | #endif // wxUSE_MIMETYPE |
1715 | ||
1716 | int wxFileIconsTable::GetIconID(const wxString& extension, const wxString& mime) | |
1717 | { | |
1718 | if (!m_smallImageList) | |
1719 | Create(); | |
1720 | ||
1721 | #if wxUSE_MIMETYPE | |
44d60c0b | 1722 | if (!extension.empty()) |
06cc1fb9 JS |
1723 | { |
1724 | wxFileIconEntry *entry = (wxFileIconEntry*) m_HashTable->Get(extension); | |
c1ec7ee8 | 1725 | if (entry) return (entry -> iconid); |
06cc1fb9 JS |
1726 | } |
1727 | ||
44d60c0b | 1728 | wxFileType *ft = (mime.empty()) ? |
06cc1fb9 JS |
1729 | wxTheMimeTypesManager -> GetFileTypeFromExtension(extension) : |
1730 | wxTheMimeTypesManager -> GetFileTypeFromMimeType(mime); | |
55d0aaa3 VZ |
1731 | |
1732 | wxIconLocation iconLoc; | |
06cc1fb9 | 1733 | wxIcon ic; |
02dd0487 | 1734 | |
55d0aaa3 | 1735 | { |
02dd0487 JS |
1736 | wxLogNull logNull; |
1737 | if ( ft && ft->GetIcon(&iconLoc) ) | |
1738 | { | |
8e5c6521 | 1739 | ic = wxIcon( iconLoc ); |
02dd0487 | 1740 | } |
55d0aaa3 | 1741 | } |
ca65c044 | 1742 | |
55d0aaa3 VZ |
1743 | delete ft; |
1744 | ||
a1b806b9 | 1745 | if ( !ic.IsOk() ) |
06cc1fb9 JS |
1746 | { |
1747 | int newid = file; | |
1748 | m_HashTable->Put(extension, new wxFileIconEntry(newid)); | |
1749 | return newid; | |
1750 | } | |
1751 | ||
3fc93ebd JS |
1752 | wxBitmap bmp; |
1753 | bmp.CopyFromIcon(ic); | |
06cc1fb9 | 1754 | |
a1b806b9 | 1755 | if ( !bmp.IsOk() ) |
b6668c25 JS |
1756 | { |
1757 | int newid = file; | |
1758 | m_HashTable->Put(extension, new wxFileIconEntry(newid)); | |
1759 | return newid; | |
1760 | } | |
1761 | ||
1d529ef7 | 1762 | const unsigned int size = 16; |
ca65c044 | 1763 | |
c1ec7ee8 | 1764 | int treeid = m_smallImageList->GetImageCount(); |
1aa81b17 | 1765 | if ((bmp.GetWidth() == (int) size) && (bmp.GetHeight() == (int) size)) |
1d529ef7 | 1766 | { |
3fc93ebd | 1767 | m_smallImageList->Add(bmp); |
1d529ef7 | 1768 | } |
bb5a9514 | 1769 | #if wxUSE_IMAGE && (!defined(__WINDOWS__) || wxUSE_WXDIB) |
06cc1fb9 JS |
1770 | else |
1771 | { | |
3fc93ebd JS |
1772 | wxImage img = bmp.ConvertToImage(); |
1773 | ||
1d529ef7 RR |
1774 | if ((img.GetWidth() != size*2) || (img.GetHeight() != size*2)) |
1775 | // m_smallImageList->Add(CreateAntialiasedBitmap(CutEmptyBorders(img).Rescale(size*2, size*2))); | |
1776 | m_smallImageList->Add(CreateAntialiasedBitmap(img.Rescale(size*2, size*2))); | |
06cc1fb9 JS |
1777 | else |
1778 | m_smallImageList->Add(CreateAntialiasedBitmap(img)); | |
1779 | } | |
1904aa72 DS |
1780 | #endif // wxUSE_IMAGE |
1781 | ||
c1ec7ee8 SC |
1782 | m_HashTable->Put(extension, new wxFileIconEntry(treeid)); |
1783 | return treeid; | |
06cc1fb9 JS |
1784 | |
1785 | #else // !wxUSE_MIMETYPE | |
1786 | ||
79e162f5 | 1787 | wxUnusedVar(mime); |
06cc1fb9 JS |
1788 | if (extension == wxT("exe")) |
1789 | return executable; | |
1790 | else | |
1791 | return file; | |
1792 | #endif // wxUSE_MIMETYPE/!wxUSE_MIMETYPE | |
1793 | } | |
1794 | ||
13de0c8c | 1795 | #endif // wxUSE_DIRDLG || wxUSE_FILEDLG |