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