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