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