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