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