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