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