Update the directory icon in wxGenericDirCtrl::ExpandDir().
[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
441 #if wxUSE_EXTENDED_RTTI
442 WX_DEFINE_FLAGS( wxGenericDirCtrlStyle )
443
444 wxBEGIN_FLAGS( wxGenericDirCtrlStyle )
445 // new style border flags, we put them first to
446 // use them for streaming out
447 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
448 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
449 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
450 wxFLAGS_MEMBER(wxBORDER_RAISED)
451 wxFLAGS_MEMBER(wxBORDER_STATIC)
452 wxFLAGS_MEMBER(wxBORDER_NONE)
453
454 // old style border flags
455 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
456 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
457 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
458 wxFLAGS_MEMBER(wxRAISED_BORDER)
459 wxFLAGS_MEMBER(wxSTATIC_BORDER)
460 wxFLAGS_MEMBER(wxBORDER)
461
462 // standard window styles
463 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
464 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
465 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
466 wxFLAGS_MEMBER(wxWANTS_CHARS)
467 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
468 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
469 wxFLAGS_MEMBER(wxVSCROLL)
470 wxFLAGS_MEMBER(wxHSCROLL)
471
472 wxFLAGS_MEMBER(wxDIRCTRL_DIR_ONLY)
473 wxFLAGS_MEMBER(wxDIRCTRL_3D_INTERNAL)
474 wxFLAGS_MEMBER(wxDIRCTRL_SELECT_FIRST)
475 wxFLAGS_MEMBER(wxDIRCTRL_MULTIPLE)
476
477 wxEND_FLAGS( wxGenericDirCtrlStyle )
478
479 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGenericDirCtrl, wxControl,"wx/dirctrl.h")
480
481 wxBEGIN_PROPERTIES_TABLE(wxGenericDirCtrl)
482 wxHIDE_PROPERTY( Children )
483 wxPROPERTY( DefaultPath , wxString , SetDefaultPath , GetDefaultPath , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
484 wxPROPERTY( Filter , wxString , SetFilter , GetFilter , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
485 wxPROPERTY( DefaultFilter , int , SetFilterIndex, GetFilterIndex, EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
486 wxPROPERTY_FLAGS( WindowStyle, wxGenericDirCtrlStyle, long, SetWindowStyleFlag, GetWindowStyleFlag, EMPTY_MACROVALUE , 0, wxT("Helpstring"), wxT("group") )
487 wxEND_PROPERTIES_TABLE()
488
489 wxBEGIN_HANDLERS_TABLE(wxGenericDirCtrl)
490 wxEND_HANDLERS_TABLE()
491
492 wxCONSTRUCTOR_8( wxGenericDirCtrl , wxWindow* , Parent , wxWindowID , Id , wxString , DefaultPath ,
493 wxPoint , Position , wxSize , Size , long , WindowStyle , wxString , Filter , int , DefaultFilter )
494 #else
495 IMPLEMENT_DYNAMIC_CLASS(wxGenericDirCtrl, wxControl)
496 #endif
497
498 BEGIN_EVENT_TABLE(wxGenericDirCtrl, wxControl)
499 EVT_TREE_ITEM_EXPANDING (wxID_TREECTRL, wxGenericDirCtrl::OnExpandItem)
500 EVT_TREE_ITEM_COLLAPSED (wxID_TREECTRL, wxGenericDirCtrl::OnCollapseItem)
501 EVT_TREE_BEGIN_LABEL_EDIT (wxID_TREECTRL, wxGenericDirCtrl::OnBeginEditItem)
502 EVT_TREE_END_LABEL_EDIT (wxID_TREECTRL, wxGenericDirCtrl::OnEndEditItem)
503 EVT_SIZE (wxGenericDirCtrl::OnSize)
504 END_EVENT_TABLE()
505
506 wxGenericDirCtrl::wxGenericDirCtrl(void)
507 {
508 Init();
509 }
510
511 void wxGenericDirCtrl::ExpandRoot()
512 {
513 ExpandDir(m_rootId); // automatically expand first level
514
515 // Expand and select the default path
516 if (!m_defaultPath.empty())
517 {
518 ExpandPath(m_defaultPath);
519 }
520 #ifdef __UNIX__
521 else
522 {
523 // On Unix, there's only one node under the (hidden) root node. It
524 // represents the / path, so the user would always have to expand it;
525 // let's do it ourselves
526 ExpandPath( wxT("/") );
527 }
528 #endif
529 }
530
531 bool wxGenericDirCtrl::Create(wxWindow *parent,
532 const wxWindowID id,
533 const wxString& dir,
534 const wxPoint& pos,
535 const wxSize& size,
536 long style,
537 const wxString& filter,
538 int defaultFilter,
539 const wxString& name)
540 {
541 if (!wxControl::Create(parent, id, pos, size, style, wxDefaultValidator, name))
542 return false;
543
544 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));
545 SetForegroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT));
546
547 Init();
548
549 long treeStyle = wxTR_HAS_BUTTONS;
550
551 // On Windows CE, if you hide the root, you get a crash when
552 // attempting to access data for children of the root item.
553 #ifndef __WXWINCE__
554 treeStyle |= wxTR_HIDE_ROOT;
555 #endif
556
557 #ifdef __WXGTK20__
558 treeStyle |= wxTR_NO_LINES;
559 #endif
560
561 if (style & wxDIRCTRL_EDIT_LABELS)
562 treeStyle |= wxTR_EDIT_LABELS;
563
564 if (style & wxDIRCTRL_MULTIPLE)
565 treeStyle |= wxTR_MULTIPLE;
566
567 if ((style & wxDIRCTRL_3D_INTERNAL) == 0)
568 treeStyle |= wxNO_BORDER;
569
570 m_treeCtrl = CreateTreeCtrl(this, wxID_TREECTRL,
571 wxPoint(0,0), GetClientSize(), treeStyle);
572
573 if (!filter.empty())
574 m_filterListCtrl = new wxDirFilterListCtrl(this, wxID_FILTERLISTCTRL);
575
576 m_defaultPath = dir;
577 m_filter = filter;
578
579 if (m_filter.empty())
580 m_filter = wxFileSelectorDefaultWildcardStr;
581
582 SetFilterIndex(defaultFilter);
583
584 if (m_filterListCtrl)
585 m_filterListCtrl->FillFilterList(filter, defaultFilter);
586
587 m_treeCtrl->SetImageList(wxTheFileIconsTable->GetSmallImageList());
588
589 m_showHidden = false;
590 wxDirItemData* rootData = new wxDirItemData(wxEmptyString, wxEmptyString, true);
591
592 wxString rootName;
593
594 #if defined(__WINDOWS__) || defined(__OS2__) || defined(__DOS__)
595 rootName = _("Computer");
596 #else
597 rootName = _("Sections");
598 #endif
599
600 m_rootId = m_treeCtrl->AddRoot( rootName, 3, -1, rootData);
601 m_treeCtrl->SetItemHasChildren(m_rootId);
602
603 ExpandRoot();
604
605 SetInitialSize(size);
606 DoResize();
607
608 return true;
609 }
610
611 wxGenericDirCtrl::~wxGenericDirCtrl()
612 {
613 }
614
615 void wxGenericDirCtrl::Init()
616 {
617 m_showHidden = false;
618 m_currentFilter = 0;
619 m_currentFilterStr = wxEmptyString; // Default: any file
620 m_treeCtrl = NULL;
621 m_filterListCtrl = NULL;
622 }
623
624 wxTreeCtrl* wxGenericDirCtrl::CreateTreeCtrl(wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long treeStyle)
625 {
626 return new wxTreeCtrl(parent, id, pos, size, treeStyle);
627 }
628
629 void wxGenericDirCtrl::ShowHidden( bool show )
630 {
631 if ( m_showHidden == show )
632 return;
633
634 m_showHidden = show;
635
636 if ( HasFlag(wxDIRCTRL_MULTIPLE) )
637 {
638 wxArrayString paths;
639 GetPaths(paths);
640 ReCreateTree();
641 for ( unsigned n = 0; n < paths.size(); n++ )
642 {
643 ExpandPath(paths[n]);
644 }
645 }
646 else
647 {
648 wxString path = GetPath();
649 ReCreateTree();
650 SetPath(path);
651 }
652 }
653
654 const wxTreeItemId
655 wxGenericDirCtrl::AddSection(const wxString& path, const wxString& name, int imageId)
656 {
657 wxDirItemData *dir_item = new wxDirItemData(path,name,true);
658
659 wxTreeItemId id = AppendItem( m_rootId, name, imageId, -1, dir_item);
660
661 m_treeCtrl->SetItemHasChildren(id);
662
663 return id;
664 }
665
666 void wxGenericDirCtrl::SetupSections()
667 {
668 wxArrayString paths, names;
669 wxArrayInt icons;
670
671 size_t n, count = wxGetAvailableDrives(paths, names, icons);
672
673 #ifdef __WXGTK20__
674 wxString home = wxGetHomeDir();
675 AddSection( home, _("Home directory"), 1);
676 home += wxT("/Desktop");
677 AddSection( home, _("Desktop"), 1);
678 #endif
679
680 for (n = 0; n < count; n++)
681 AddSection(paths[n], names[n], icons[n]);
682 }
683
684 void wxGenericDirCtrl::SetFocus()
685 {
686 // we don't need focus ourselves, give it to the tree so that the user
687 // could navigate it
688 if (m_treeCtrl)
689 m_treeCtrl->SetFocus();
690 }
691
692 void wxGenericDirCtrl::OnBeginEditItem(wxTreeEvent &event)
693 {
694 // don't rename the main entry "Sections"
695 if (event.GetItem() == m_rootId)
696 {
697 event.Veto();
698 return;
699 }
700
701 // don't rename the individual sections
702 if (m_treeCtrl->GetItemParent( event.GetItem() ) == m_rootId)
703 {
704 event.Veto();
705 return;
706 }
707 }
708
709 void wxGenericDirCtrl::OnEndEditItem(wxTreeEvent &event)
710 {
711 if (event.IsEditCancelled())
712 return;
713
714 if ((event.GetLabel().empty()) ||
715 (event.GetLabel() == wxT(".")) ||
716 (event.GetLabel() == wxT("..")) ||
717 (event.GetLabel().Find(wxT('/')) != wxNOT_FOUND) ||
718 (event.GetLabel().Find(wxT('\\')) != wxNOT_FOUND) ||
719 (event.GetLabel().Find(wxT('|')) != wxNOT_FOUND))
720 {
721 wxMessageDialog dialog(this, _("Illegal directory name."), _("Error"), wxOK | wxICON_ERROR );
722 dialog.ShowModal();
723 event.Veto();
724 return;
725 }
726
727 wxTreeItemId id = event.GetItem();
728 wxDirItemData *data = (wxDirItemData*)m_treeCtrl->GetItemData( id );
729 wxASSERT( data );
730
731 wxString new_name( wxPathOnly( data->m_path ) );
732 new_name += wxString(wxFILE_SEP_PATH);
733 new_name += event.GetLabel();
734
735 wxLogNull log;
736
737 if (wxFileExists(new_name))
738 {
739 wxMessageDialog dialog(this, _("File name exists already."), _("Error"), wxOK | wxICON_ERROR );
740 dialog.ShowModal();
741 event.Veto();
742 }
743
744 if (wxRenameFile(data->m_path,new_name))
745 {
746 data->SetNewDirName( new_name );
747 }
748 else
749 {
750 wxMessageDialog dialog(this, _("Operation not permitted."), _("Error"), wxOK | wxICON_ERROR );
751 dialog.ShowModal();
752 event.Veto();
753 }
754 }
755
756 void wxGenericDirCtrl::OnExpandItem(wxTreeEvent &event)
757 {
758 wxTreeItemId parentId = event.GetItem();
759
760 // VS: this is needed because the event handler is called from wxTreeCtrl
761 // ctor when wxTR_HIDE_ROOT was specified
762
763 if (!m_rootId.IsOk())
764 m_rootId = m_treeCtrl->GetRootItem();
765
766 ExpandDir(parentId);
767 }
768
769 void wxGenericDirCtrl::OnCollapseItem(wxTreeEvent &event )
770 {
771 CollapseDir(event.GetItem());
772 }
773
774 void wxGenericDirCtrl::CollapseDir(wxTreeItemId parentId)
775 {
776 wxTreeItemId child;
777
778 wxDirItemData *data = (wxDirItemData *) m_treeCtrl->GetItemData(parentId);
779 if (!data->m_isExpanded)
780 return;
781
782 data->m_isExpanded = false;
783
784 m_treeCtrl->Freeze();
785 if (parentId != m_treeCtrl->GetRootItem())
786 m_treeCtrl->CollapseAndReset(parentId);
787 m_treeCtrl->DeleteChildren(parentId);
788 m_treeCtrl->Thaw();
789 }
790
791 void wxGenericDirCtrl::ExpandDir(wxTreeItemId parentId)
792 {
793 wxDirItemData *data = (wxDirItemData *) m_treeCtrl->GetItemData(parentId);
794
795 if (data->m_isExpanded)
796 return;
797
798 data->m_isExpanded = true;
799
800 if (parentId == m_treeCtrl->GetRootItem())
801 {
802 SetupSections();
803 return;
804 }
805
806 wxASSERT(data);
807
808 wxString search,path,filename;
809
810 wxString dirName(data->m_path);
811
812 #if (defined(__WINDOWS__) && !defined(__WXWINCE__)) || defined(__DOS__) || defined(__OS2__)
813 // Check if this is a root directory and if so,
814 // whether the drive is avaiable.
815 if (!wxIsDriveAvailable(dirName))
816 {
817 data->m_isExpanded = false;
818 //wxMessageBox(wxT("Sorry, this drive is not available."));
819 return;
820 }
821 #endif
822
823 // This may take a longish time. Go to busy cursor
824 wxBusyCursor busy;
825
826 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__OS2__)
827 if (dirName.Last() == ':')
828 dirName += wxString(wxFILE_SEP_PATH);
829 #endif
830
831 wxArrayString dirs;
832 wxArrayString filenames;
833
834 wxDir d;
835 wxString eachFilename;
836
837 wxLogNull log;
838 d.Open(dirName);
839
840 if (d.IsOpened())
841 {
842 int style = wxDIR_DIRS;
843 if (m_showHidden) style |= wxDIR_HIDDEN;
844 if (d.GetFirst(& eachFilename, wxEmptyString, style))
845 {
846 do
847 {
848 if ((eachFilename != wxT(".")) && (eachFilename != wxT("..")))
849 {
850 dirs.Add(eachFilename);
851 }
852 }
853 while (d.GetNext(&eachFilename));
854 }
855 }
856 dirs.Sort(wxDirCtrlStringCompareFunction);
857
858 // Now do the filenames -- but only if we're allowed to
859 if (!HasFlag(wxDIRCTRL_DIR_ONLY))
860 {
861 d.Open(dirName);
862
863 if (d.IsOpened())
864 {
865 int style = wxDIR_FILES;
866 if (m_showHidden) style |= wxDIR_HIDDEN;
867 // Process each filter (ex: "JPEG Files (*.jpg;*.jpeg)|*.jpg;*.jpeg")
868 wxStringTokenizer strTok;
869 wxString curFilter;
870 strTok.SetString(m_currentFilterStr,wxT(";"));
871 while(strTok.HasMoreTokens())
872 {
873 curFilter = strTok.GetNextToken();
874 if (d.GetFirst(& eachFilename, curFilter, style))
875 {
876 do
877 {
878 if ((eachFilename != wxT(".")) && (eachFilename != wxT("..")))
879 {
880 filenames.Add(eachFilename);
881 }
882 }
883 while (d.GetNext(& eachFilename));
884 }
885 }
886 }
887 filenames.Sort(wxDirCtrlStringCompareFunction);
888 }
889
890 // Now we really know whether we have any children so tell the tree control
891 // about it.
892 m_treeCtrl->SetItemHasChildren(parentId, !dirs.empty() || !filenames.empty());
893
894 // Add the sorted dirs
895 size_t i;
896 for (i = 0; i < dirs.GetCount(); i++)
897 {
898 eachFilename = dirs[i];
899 path = dirName;
900 if (!wxEndsWithPathSeparator(path))
901 path += wxString(wxFILE_SEP_PATH);
902 path += eachFilename;
903
904 wxDirItemData *dir_item = new wxDirItemData(path,eachFilename,true);
905 wxTreeItemId id = AppendItem( parentId, eachFilename,
906 wxFileIconsTable::folder, -1, dir_item);
907 m_treeCtrl->SetItemImage( id, wxFileIconsTable::folder_open,
908 wxTreeItemIcon_Expanded );
909
910 // assume that it does have children by default as it can take a long
911 // time to really check for this (think remote drives...)
912 //
913 // and if we're wrong, we'll correct the icon later if
914 // the user really tries to open this item
915 m_treeCtrl->SetItemHasChildren(id);
916 }
917
918 // Add the sorted filenames
919 if (!HasFlag(wxDIRCTRL_DIR_ONLY))
920 {
921 for (i = 0; i < filenames.GetCount(); i++)
922 {
923 eachFilename = filenames[i];
924 path = dirName;
925 if (!wxEndsWithPathSeparator(path))
926 path += wxString(wxFILE_SEP_PATH);
927 path += eachFilename;
928 //path = dirName + wxString(wxT("/")) + eachFilename;
929 wxDirItemData *dir_item = new wxDirItemData(path,eachFilename,false);
930 int image_id = wxFileIconsTable::file;
931 if (eachFilename.Find(wxT('.')) != wxNOT_FOUND)
932 image_id = wxTheFileIconsTable->GetIconID(eachFilename.AfterLast(wxT('.')));
933 (void) AppendItem( parentId, eachFilename, image_id, -1, dir_item);
934 }
935 }
936 }
937
938 void wxGenericDirCtrl::ReCreateTree()
939 {
940 CollapseDir(m_treeCtrl->GetRootItem());
941 ExpandRoot();
942 }
943
944 void wxGenericDirCtrl::CollapseTree()
945 {
946 wxTreeItemIdValue cookie;
947 wxTreeItemId child = m_treeCtrl->GetFirstChild(m_rootId, cookie);
948 while (child.IsOk())
949 {
950 CollapseDir(child);
951 child = m_treeCtrl->GetNextChild(m_rootId, cookie);
952 }
953 }
954
955 // Find the child that matches the first part of 'path'.
956 // E.g. if a child path is "/usr" and 'path' is "/usr/include"
957 // then the child for /usr is returned.
958 wxTreeItemId wxGenericDirCtrl::FindChild(wxTreeItemId parentId, const wxString& path, bool& done)
959 {
960 wxString path2(path);
961
962 // Make sure all separators are as per the current platform
963 path2.Replace(wxT("\\"), wxString(wxFILE_SEP_PATH));
964 path2.Replace(wxT("/"), wxString(wxFILE_SEP_PATH));
965
966 // Append a separator to foil bogus substring matching
967 path2 += wxString(wxFILE_SEP_PATH);
968
969 // In MSW or PM, case is not significant
970 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__OS2__)
971 path2.MakeLower();
972 #endif
973
974 wxTreeItemIdValue cookie;
975 wxTreeItemId childId = m_treeCtrl->GetFirstChild(parentId, cookie);
976 while (childId.IsOk())
977 {
978 wxDirItemData* data = (wxDirItemData*) m_treeCtrl->GetItemData(childId);
979
980 if (data && !data->m_path.empty())
981 {
982 wxString childPath(data->m_path);
983 if (!wxEndsWithPathSeparator(childPath))
984 childPath += wxString(wxFILE_SEP_PATH);
985
986 // In MSW and PM, case is not significant
987 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__OS2__)
988 childPath.MakeLower();
989 #endif
990
991 if (childPath.length() <= path2.length())
992 {
993 wxString path3 = path2.Mid(0, childPath.length());
994 if (childPath == path3)
995 {
996 if (path3.length() == path2.length())
997 done = true;
998 else
999 done = false;
1000 return childId;
1001 }
1002 }
1003 }
1004
1005 childId = m_treeCtrl->GetNextChild(parentId, cookie);
1006 }
1007 wxTreeItemId invalid;
1008 return invalid;
1009 }
1010
1011 // Try to expand as much of the given path as possible,
1012 // and select the given tree item.
1013 bool wxGenericDirCtrl::ExpandPath(const wxString& path)
1014 {
1015 bool done = false;
1016 wxTreeItemId id = FindChild(m_rootId, path, done);
1017 wxTreeItemId lastId = id; // The last non-zero id
1018 while (id.IsOk() && !done)
1019 {
1020 ExpandDir(id);
1021
1022 id = FindChild(id, path, done);
1023 if (id.IsOk())
1024 lastId = id;
1025 }
1026 if (!lastId.IsOk())
1027 return false;
1028
1029 wxDirItemData *data = (wxDirItemData *) m_treeCtrl->GetItemData(lastId);
1030 if (data->m_isDir)
1031 {
1032 m_treeCtrl->Expand(lastId);
1033 }
1034 if (HasFlag(wxDIRCTRL_SELECT_FIRST) && data->m_isDir)
1035 {
1036 // Find the first file in this directory
1037 wxTreeItemIdValue cookie;
1038 wxTreeItemId childId = m_treeCtrl->GetFirstChild(lastId, cookie);
1039 bool selectedChild = false;
1040 while (childId.IsOk())
1041 {
1042 data = (wxDirItemData*) m_treeCtrl->GetItemData(childId);
1043
1044 if (data && data->m_path != wxEmptyString && !data->m_isDir)
1045 {
1046 m_treeCtrl->SelectItem(childId);
1047 m_treeCtrl->EnsureVisible(childId);
1048 selectedChild = true;
1049 break;
1050 }
1051 childId = m_treeCtrl->GetNextChild(lastId, cookie);
1052 }
1053 if (!selectedChild)
1054 {
1055 m_treeCtrl->SelectItem(lastId);
1056 m_treeCtrl->EnsureVisible(lastId);
1057 }
1058 }
1059 else
1060 {
1061 m_treeCtrl->SelectItem(lastId);
1062 m_treeCtrl->EnsureVisible(lastId);
1063 }
1064
1065 return true;
1066 }
1067
1068
1069 bool wxGenericDirCtrl::CollapsePath(const wxString& path)
1070 {
1071 bool done = false;
1072 wxTreeItemId id = FindChild(m_rootId, path, done);
1073 wxTreeItemId lastId = id; // The last non-zero id
1074
1075 while ( id.IsOk() && !done )
1076 {
1077 CollapseDir(id);
1078
1079 id = FindChild(id, path, done);
1080
1081 if ( id.IsOk() )
1082 lastId = id;
1083 }
1084
1085 if ( !lastId.IsOk() )
1086 return false;
1087
1088 m_treeCtrl->SelectItem(lastId);
1089 m_treeCtrl->EnsureVisible(lastId);
1090
1091 return true;
1092 }
1093
1094
1095 wxString wxGenericDirCtrl::GetPath() const
1096 {
1097 // Allow calling GetPath() in multiple selection from OnSelFilter
1098 if (m_treeCtrl->HasFlag(wxTR_MULTIPLE))
1099 {
1100 wxArrayTreeItemIds items;
1101 m_treeCtrl->GetSelections(items);
1102 if (items.size() > 0)
1103 {
1104 // return first string only
1105 wxTreeItemId id = items[0];
1106 wxDirItemData* data = (wxDirItemData*) m_treeCtrl->GetItemData(id);
1107 return data->m_path;
1108 }
1109
1110 return wxEmptyString;
1111 }
1112
1113 wxTreeItemId id = m_treeCtrl->GetSelection();
1114 if (id)
1115 {
1116 wxDirItemData* data = (wxDirItemData*) m_treeCtrl->GetItemData(id);
1117 return data->m_path;
1118 }
1119 else
1120 return wxEmptyString;
1121 }
1122
1123 void wxGenericDirCtrl::GetPaths(wxArrayString& paths) const
1124 {
1125 paths.clear();
1126
1127 wxArrayTreeItemIds items;
1128 m_treeCtrl->GetSelections(items);
1129 for ( unsigned n = 0; n < items.size(); n++ )
1130 {
1131 wxTreeItemId id = items[n];
1132 wxDirItemData* data = (wxDirItemData*) m_treeCtrl->GetItemData(id);
1133 paths.Add(data->m_path);
1134 }
1135 }
1136
1137 wxString wxGenericDirCtrl::GetFilePath() const
1138 {
1139 wxTreeItemId id = m_treeCtrl->GetSelection();
1140 if (id)
1141 {
1142 wxDirItemData* data = (wxDirItemData*) m_treeCtrl->GetItemData(id);
1143 if (data->m_isDir)
1144 return wxEmptyString;
1145 else
1146 return data->m_path;
1147 }
1148 else
1149 return wxEmptyString;
1150 }
1151
1152 void wxGenericDirCtrl::GetFilePaths(wxArrayString& paths) const
1153 {
1154 paths.clear();
1155
1156 wxArrayTreeItemIds items;
1157 m_treeCtrl->GetSelections(items);
1158 for ( unsigned n = 0; n < items.size(); n++ )
1159 {
1160 wxTreeItemId id = items[n];
1161 wxDirItemData* data = (wxDirItemData*) m_treeCtrl->GetItemData(id);
1162 if ( !data->m_isDir )
1163 paths.Add(data->m_path);
1164 }
1165 }
1166
1167 void wxGenericDirCtrl::SetPath(const wxString& path)
1168 {
1169 m_defaultPath = path;
1170 if (m_rootId)
1171 ExpandPath(path);
1172 }
1173
1174 void wxGenericDirCtrl::SelectPath(const wxString& path, bool select)
1175 {
1176 bool done = false;
1177 wxTreeItemId id = FindChild(m_rootId, path, done);
1178 wxTreeItemId lastId = id; // The last non-zero id
1179 while ( id.IsOk() && !done )
1180 {
1181 id = FindChild(id, path, done);
1182 if ( id.IsOk() )
1183 lastId = id;
1184 }
1185 if ( !lastId.IsOk() )
1186 return;
1187
1188 if ( done )
1189 {
1190 m_treeCtrl->SelectItem(id, select);
1191 }
1192 }
1193
1194 void wxGenericDirCtrl::SelectPaths(const wxArrayString& paths)
1195 {
1196 if ( HasFlag(wxDIRCTRL_MULTIPLE) )
1197 {
1198 UnselectAll();
1199 for ( unsigned n = 0; n < paths.size(); n++ )
1200 {
1201 SelectPath(paths[n]);
1202 }
1203 }
1204 }
1205
1206 void wxGenericDirCtrl::UnselectAll()
1207 {
1208 m_treeCtrl->UnselectAll();
1209 }
1210
1211 // Not used
1212 #if 0
1213 void wxGenericDirCtrl::FindChildFiles(wxTreeItemId id, int dirFlags, wxArrayString& filenames)
1214 {
1215 wxDirItemData *data = (wxDirItemData *) m_treeCtrl->GetItemData(id);
1216
1217 // This may take a longish time. Go to busy cursor
1218 wxBusyCursor busy;
1219
1220 wxASSERT(data);
1221
1222 wxString search,path,filename;
1223
1224 wxString dirName(data->m_path);
1225
1226 #if defined(__WXMSW__) || defined(__OS2__)
1227 if (dirName.Last() == ':')
1228 dirName += wxString(wxFILE_SEP_PATH);
1229 #endif
1230
1231 wxDir d;
1232 wxString eachFilename;
1233
1234 wxLogNull log;
1235 d.Open(dirName);
1236
1237 if (d.IsOpened())
1238 {
1239 if (d.GetFirst(& eachFilename, m_currentFilterStr, dirFlags))
1240 {
1241 do
1242 {
1243 if ((eachFilename != wxT(".")) && (eachFilename != wxT("..")))
1244 {
1245 filenames.Add(eachFilename);
1246 }
1247 }
1248 while (d.GetNext(& eachFilename)) ;
1249 }
1250 }
1251 }
1252 #endif
1253
1254 void wxGenericDirCtrl::SetFilterIndex(int n)
1255 {
1256 m_currentFilter = n;
1257
1258 wxString f, d;
1259 if (ExtractWildcard(m_filter, n, f, d))
1260 m_currentFilterStr = f;
1261 else
1262 #ifdef __UNIX__
1263 m_currentFilterStr = wxT("*");
1264 #else
1265 m_currentFilterStr = wxT("*.*");
1266 #endif
1267 }
1268
1269 void wxGenericDirCtrl::SetFilter(const wxString& filter)
1270 {
1271 m_filter = filter;
1272
1273 if (!filter.empty() && !m_filterListCtrl)
1274 m_filterListCtrl = new wxDirFilterListCtrl(this, wxID_FILTERLISTCTRL);
1275 else if (filter.empty() && m_filterListCtrl)
1276 {
1277 m_filterListCtrl->Destroy();
1278 m_filterListCtrl = NULL;
1279 }
1280
1281 wxString f, d;
1282 if (ExtractWildcard(m_filter, m_currentFilter, f, d))
1283 m_currentFilterStr = f;
1284 else
1285 #ifdef __UNIX__
1286 m_currentFilterStr = wxT("*");
1287 #else
1288 m_currentFilterStr = wxT("*.*");
1289 #endif
1290 // current filter index is meaningless after filter change, set it to zero
1291 SetFilterIndex(0);
1292 if (m_filterListCtrl)
1293 m_filterListCtrl->FillFilterList(m_filter, 0);
1294 }
1295
1296 // Extract description and actual filter from overall filter string
1297 bool wxGenericDirCtrl::ExtractWildcard(const wxString& filterStr, int n, wxString& filter, wxString& description)
1298 {
1299 wxArrayString filters, descriptions;
1300 int count = wxParseCommonDialogsFilter(filterStr, descriptions, filters);
1301 if (count > 0 && n < count)
1302 {
1303 filter = filters[n];
1304 description = descriptions[n];
1305 return true;
1306 }
1307
1308 return false;
1309 }
1310
1311
1312 void wxGenericDirCtrl::DoResize()
1313 {
1314 wxSize sz = GetClientSize();
1315 int verticalSpacing = 3;
1316 if (m_treeCtrl)
1317 {
1318 wxSize filterSz ;
1319 if (m_filterListCtrl)
1320 {
1321 filterSz = m_filterListCtrl->GetSize();
1322 sz.y -= (filterSz.y + verticalSpacing);
1323 }
1324 m_treeCtrl->SetSize(0, 0, sz.x, sz.y);
1325 if (m_filterListCtrl)
1326 {
1327 m_filterListCtrl->SetSize(0, sz.y + verticalSpacing, sz.x, filterSz.y);
1328 // Don't know why, but this needs refreshing after a resize (wxMSW)
1329 m_filterListCtrl->Refresh();
1330 }
1331 }
1332 }
1333
1334
1335 void wxGenericDirCtrl::OnSize(wxSizeEvent& WXUNUSED(event))
1336 {
1337 DoResize();
1338 }
1339
1340 wxTreeItemId wxGenericDirCtrl::AppendItem (const wxTreeItemId & parent,
1341 const wxString & text,
1342 int image, int selectedImage,
1343 wxTreeItemData * data)
1344 {
1345 wxTreeCtrl *treeCtrl = GetTreeCtrl ();
1346
1347 wxASSERT (treeCtrl);
1348
1349 if (treeCtrl)
1350 {
1351 return treeCtrl->AppendItem (parent, text, image, selectedImage, data);
1352 }
1353 else
1354 {
1355 return wxTreeItemId();
1356 }
1357 }
1358
1359
1360 //-----------------------------------------------------------------------------
1361 // wxDirFilterListCtrl
1362 //-----------------------------------------------------------------------------
1363
1364 IMPLEMENT_CLASS(wxDirFilterListCtrl, wxChoice)
1365
1366 BEGIN_EVENT_TABLE(wxDirFilterListCtrl, wxChoice)
1367 EVT_CHOICE(wxID_ANY, wxDirFilterListCtrl::OnSelFilter)
1368 END_EVENT_TABLE()
1369
1370 bool wxDirFilterListCtrl::Create(wxGenericDirCtrl* parent,
1371 const wxWindowID id,
1372 const wxPoint& pos,
1373 const wxSize& size,
1374 long style)
1375 {
1376 m_dirCtrl = parent;
1377
1378 // by default our border style is determined by the style of our parent
1379 if ( !(style & wxBORDER_MASK) )
1380 {
1381 style |= parent->HasFlag(wxDIRCTRL_3D_INTERNAL) ? wxBORDER_SUNKEN
1382 : wxBORDER_NONE;
1383 }
1384
1385 return wxChoice::Create(parent, id, pos, size, 0, NULL, style);
1386 }
1387
1388 void wxDirFilterListCtrl::Init()
1389 {
1390 m_dirCtrl = NULL;
1391 }
1392
1393 void wxDirFilterListCtrl::OnSelFilter(wxCommandEvent& WXUNUSED(event))
1394 {
1395 int sel = GetSelection();
1396
1397 if (m_dirCtrl->HasFlag(wxDIRCTRL_MULTIPLE))
1398 {
1399 wxArrayString paths;
1400 m_dirCtrl->GetPaths(paths);
1401
1402 m_dirCtrl->SetFilterIndex(sel);
1403
1404 // If the filter has changed, the view is out of date, so
1405 // collapse the tree.
1406 m_dirCtrl->ReCreateTree();
1407
1408 // Expand and select the previously selected paths
1409 for (unsigned int i = 0; i < paths.GetCount(); i++)
1410 {
1411 m_dirCtrl->ExpandPath(paths.Item(i));
1412 }
1413 }
1414 else
1415 {
1416 wxString currentPath = m_dirCtrl->GetPath();
1417
1418 m_dirCtrl->SetFilterIndex(sel);
1419 m_dirCtrl->ReCreateTree();
1420
1421 // Try to restore the selection, or at least the directory
1422 m_dirCtrl->ExpandPath(currentPath);
1423 }
1424 }
1425
1426 void wxDirFilterListCtrl::FillFilterList(const wxString& filter, int defaultFilter)
1427 {
1428 Clear();
1429 wxArrayString descriptions, filters;
1430 size_t n = (size_t) wxParseCommonDialogsFilter(filter, descriptions, filters);
1431
1432 if (n > 0 && defaultFilter < (int) n)
1433 {
1434 for (size_t i = 0; i < n; i++)
1435 Append(descriptions[i]);
1436 SetSelection(defaultFilter);
1437 }
1438 }
1439 #endif // wxUSE_DIRDLG
1440
1441 #if wxUSE_DIRDLG || wxUSE_FILEDLG
1442
1443 // ----------------------------------------------------------------------------
1444 // wxFileIconsTable icons
1445 // ----------------------------------------------------------------------------
1446
1447 #ifndef __WXGTK20__
1448 /* Computer (c) Julian Smart */
1449 static const char* const file_icons_tbl_computer_xpm[] = {
1450 /* columns rows colors chars-per-pixel */
1451 "16 16 42 1",
1452 "r c #4E7FD0",
1453 "$ c #7198D9",
1454 "; c #DCE6F6",
1455 "q c #FFFFFF",
1456 "u c #4A7CCE",
1457 "# c #779DDB",
1458 "w c #95B2E3",
1459 "y c #7FA2DD",
1460 "f c #3263B4",
1461 "= c #EAF0FA",
1462 "< c #B1C7EB",
1463 "% c #6992D7",
1464 "9 c #D9E4F5",
1465 "o c #9BB7E5",
1466 "6 c #F7F9FD",
1467 ", c #BED0EE",
1468 "3 c #F0F5FC",
1469 "1 c #A8C0E8",
1470 " c None",
1471 "0 c #FDFEFF",
1472 "4 c #C4D5F0",
1473 "@ c #81A4DD",
1474 "e c #4377CD",
1475 "- c #E2EAF8",
1476 "i c #9FB9E5",
1477 "> c #CCDAF2",
1478 "+ c #89A9DF",
1479 "s c #5584D1",
1480 "t c #5D89D3",
1481 ": c #D2DFF4",
1482 "5 c #FAFCFE",
1483 "2 c #F5F8FD",
1484 "8 c #DFE8F7",
1485 "& c #5E8AD4",
1486 "X c #638ED5",
1487 "a c #CEDCF2",
1488 "p c #90AFE2",
1489 "d c #2F5DA9",
1490 "* c #5282D0",
1491 "7 c #E5EDF9",
1492 ". c #A2BCE6",
1493 "O c #8CACE0",
1494 /* pixels */
1495 " ",
1496 " .XXXXXXXXXXX ",
1497 " oXO++@#$%&*X ",
1498 " oX=-;:>,<1%X ",
1499 " oX23=-;:4,$X ",
1500 " oX5633789:@X ",
1501 " oX05623=78+X ",
1502 " oXqq05623=OX ",
1503 " oX,,,,,<<<$X ",
1504 " wXXXXXXXXXXe ",
1505 " XrtX%$$y@+O,, ",
1506 " uyiiiiiiiii@< ",
1507 " ouiiiiiiiiiip<a",
1508 " rustX%$$y@+Ow,,",
1509 " dfffffffffffffd",
1510 " "
1511 };
1512 #endif // !GTK+ 2
1513
1514 // ----------------------------------------------------------------------------
1515 // wxFileIconsTable & friends
1516 // ----------------------------------------------------------------------------
1517
1518 // global instance of a wxFileIconsTable
1519 wxFileIconsTable* wxTheFileIconsTable = NULL;
1520
1521 // A module to allow icons table cleanup
1522
1523 class wxFileIconsTableModule: public wxModule
1524 {
1525 DECLARE_DYNAMIC_CLASS(wxFileIconsTableModule)
1526 public:
1527 wxFileIconsTableModule() {}
1528 bool OnInit() { wxTheFileIconsTable = new wxFileIconsTable; return true; }
1529 void OnExit()
1530 {
1531 wxDELETE(wxTheFileIconsTable);
1532 }
1533 };
1534
1535 IMPLEMENT_DYNAMIC_CLASS(wxFileIconsTableModule, wxModule)
1536
1537 class wxFileIconEntry : public wxObject
1538 {
1539 public:
1540 wxFileIconEntry(int i) { id = i; }
1541
1542 int id;
1543 };
1544
1545 wxFileIconsTable::wxFileIconsTable()
1546 {
1547 m_HashTable = NULL;
1548 m_smallImageList = NULL;
1549 }
1550
1551 wxFileIconsTable::~wxFileIconsTable()
1552 {
1553 if (m_HashTable)
1554 {
1555 WX_CLEAR_HASH_TABLE(*m_HashTable);
1556 delete m_HashTable;
1557 }
1558 if (m_smallImageList) delete m_smallImageList;
1559 }
1560
1561 // delayed initialization - wait until first use (wxArtProv not created yet)
1562 void wxFileIconsTable::Create()
1563 {
1564 wxCHECK_RET(!m_smallImageList && !m_HashTable, wxT("creating icons twice"));
1565 m_HashTable = new wxHashTable(wxKEY_STRING);
1566 m_smallImageList = new wxImageList(16, 16);
1567
1568 // folder:
1569 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_FOLDER,
1570 wxART_CMN_DIALOG,
1571 wxSize(16, 16)));
1572 // folder_open
1573 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_FOLDER_OPEN,
1574 wxART_CMN_DIALOG,
1575 wxSize(16, 16)));
1576 // computer
1577 #ifdef __WXGTK20__
1578 // GTK24 uses this icon in the file open dialog
1579 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_HARDDISK,
1580 wxART_CMN_DIALOG,
1581 wxSize(16, 16)));
1582 #else
1583 m_smallImageList->Add(wxIcon(file_icons_tbl_computer_xpm));
1584 #endif
1585 // drive
1586 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_HARDDISK,
1587 wxART_CMN_DIALOG,
1588 wxSize(16, 16)));
1589 // cdrom
1590 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_CDROM,
1591 wxART_CMN_DIALOG,
1592 wxSize(16, 16)));
1593 // floppy
1594 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_FLOPPY,
1595 wxART_CMN_DIALOG,
1596 wxSize(16, 16)));
1597 // removeable
1598 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_REMOVABLE,
1599 wxART_CMN_DIALOG,
1600 wxSize(16, 16)));
1601 // file
1602 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_NORMAL_FILE,
1603 wxART_CMN_DIALOG,
1604 wxSize(16, 16)));
1605 // executable
1606 if (GetIconID(wxEmptyString, wxT("application/x-executable")) == file)
1607 {
1608 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_EXECUTABLE_FILE,
1609 wxART_CMN_DIALOG,
1610 wxSize(16, 16)));
1611 delete m_HashTable->Get(wxT("exe"));
1612 m_HashTable->Delete(wxT("exe"));
1613 m_HashTable->Put(wxT("exe"), new wxFileIconEntry(executable));
1614 }
1615 /* else put into list by GetIconID
1616 (KDE defines application/x-executable for *.exe and has nice icon)
1617 */
1618 }
1619
1620 wxImageList *wxFileIconsTable::GetSmallImageList()
1621 {
1622 if (!m_smallImageList)
1623 Create();
1624
1625 return m_smallImageList;
1626 }
1627
1628 #if wxUSE_MIMETYPE && wxUSE_IMAGE && (!defined(__WXMSW__) || wxUSE_WXDIB)
1629 // VS: we don't need this function w/o wxMimeTypesManager because we'll only have
1630 // one icon and we won't resize it
1631
1632 static wxBitmap CreateAntialiasedBitmap(const wxImage& img)
1633 {
1634 const unsigned int size = 16;
1635
1636 wxImage smallimg (size, size);
1637 unsigned char *p1, *p2, *ps;
1638 unsigned char mr = img.GetMaskRed(),
1639 mg = img.GetMaskGreen(),
1640 mb = img.GetMaskBlue();
1641
1642 unsigned x, y;
1643 unsigned sr, sg, sb, smask;
1644
1645 p1 = img.GetData(), p2 = img.GetData() + 3 * size*2, ps = smallimg.GetData();
1646 smallimg.SetMaskColour(mr, mr, mr);
1647
1648 for (y = 0; y < size; y++)
1649 {
1650 for (x = 0; x < size; x++)
1651 {
1652 sr = sg = sb = smask = 0;
1653 if (p1[0] != mr || p1[1] != mg || p1[2] != mb)
1654 sr += p1[0], sg += p1[1], sb += p1[2];
1655 else smask++;
1656 p1 += 3;
1657 if (p1[0] != mr || p1[1] != mg || p1[2] != mb)
1658 sr += p1[0], sg += p1[1], sb += p1[2];
1659 else smask++;
1660 p1 += 3;
1661 if (p2[0] != mr || p2[1] != mg || p2[2] != mb)
1662 sr += p2[0], sg += p2[1], sb += p2[2];
1663 else smask++;
1664 p2 += 3;
1665 if (p2[0] != mr || p2[1] != mg || p2[2] != mb)
1666 sr += p2[0], sg += p2[1], sb += p2[2];
1667 else smask++;
1668 p2 += 3;
1669
1670 if (smask > 2)
1671 ps[0] = ps[1] = ps[2] = mr;
1672 else
1673 {
1674 ps[0] = (unsigned char)(sr >> 2);
1675 ps[1] = (unsigned char)(sg >> 2);
1676 ps[2] = (unsigned char)(sb >> 2);
1677 }
1678 ps += 3;
1679 }
1680 p1 += size*2 * 3, p2 += size*2 * 3;
1681 }
1682
1683 return wxBitmap(smallimg);
1684 }
1685
1686 // This function is currently not unused anymore
1687 #if 0
1688 // finds empty borders and return non-empty area of image:
1689 static wxImage CutEmptyBorders(const wxImage& img)
1690 {
1691 unsigned char mr = img.GetMaskRed(),
1692 mg = img.GetMaskGreen(),
1693 mb = img.GetMaskBlue();
1694 unsigned char *dt = img.GetData(), *dttmp;
1695 unsigned w = img.GetWidth(), h = img.GetHeight();
1696
1697 unsigned top, bottom, left, right, i;
1698 bool empt;
1699
1700 #define MK_DTTMP(x,y) dttmp = dt + ((x + y * w) * 3)
1701 #define NOEMPTY_PIX(empt) if (dttmp[0] != mr || dttmp[1] != mg || dttmp[2] != mb) {empt = false; break;}
1702
1703 for (empt = true, top = 0; empt && top < h; top++)
1704 {
1705 MK_DTTMP(0, top);
1706 for (i = 0; i < w; i++, dttmp+=3)
1707 NOEMPTY_PIX(empt)
1708 }
1709 for (empt = true, bottom = h-1; empt && bottom > top; bottom--)
1710 {
1711 MK_DTTMP(0, bottom);
1712 for (i = 0; i < w; i++, dttmp+=3)
1713 NOEMPTY_PIX(empt)
1714 }
1715 for (empt = true, left = 0; empt && left < w; left++)
1716 {
1717 MK_DTTMP(left, 0);
1718 for (i = 0; i < h; i++, dttmp+=3*w)
1719 NOEMPTY_PIX(empt)
1720 }
1721 for (empt = true, right = w-1; empt && right > left; right--)
1722 {
1723 MK_DTTMP(right, 0);
1724 for (i = 0; i < h; i++, dttmp+=3*w)
1725 NOEMPTY_PIX(empt)
1726 }
1727 top--, left--, bottom++, right++;
1728
1729 return img.GetSubImage(wxRect(left, top, right - left + 1, bottom - top + 1));
1730 }
1731 #endif // #if 0
1732
1733 #endif // wxUSE_MIMETYPE
1734
1735 int wxFileIconsTable::GetIconID(const wxString& extension, const wxString& mime)
1736 {
1737 if (!m_smallImageList)
1738 Create();
1739
1740 #if wxUSE_MIMETYPE
1741 if (!extension.empty())
1742 {
1743 wxFileIconEntry *entry = (wxFileIconEntry*) m_HashTable->Get(extension);
1744 if (entry) return (entry -> id);
1745 }
1746
1747 wxFileType *ft = (mime.empty()) ?
1748 wxTheMimeTypesManager -> GetFileTypeFromExtension(extension) :
1749 wxTheMimeTypesManager -> GetFileTypeFromMimeType(mime);
1750
1751 wxIconLocation iconLoc;
1752 wxIcon ic;
1753
1754 {
1755 wxLogNull logNull;
1756 if ( ft && ft->GetIcon(&iconLoc) )
1757 {
1758 ic = wxIcon( iconLoc );
1759 }
1760 }
1761
1762 delete ft;
1763
1764 if ( !ic.Ok() )
1765 {
1766 int newid = file;
1767 m_HashTable->Put(extension, new wxFileIconEntry(newid));
1768 return newid;
1769 }
1770
1771 wxBitmap bmp;
1772 bmp.CopyFromIcon(ic);
1773
1774 if ( !bmp.Ok() )
1775 {
1776 int newid = file;
1777 m_HashTable->Put(extension, new wxFileIconEntry(newid));
1778 return newid;
1779 }
1780
1781 const unsigned int size = 16;
1782
1783 int id = m_smallImageList->GetImageCount();
1784 if ((bmp.GetWidth() == (int) size) && (bmp.GetHeight() == (int) size))
1785 {
1786 m_smallImageList->Add(bmp);
1787 }
1788 #if wxUSE_IMAGE && (!defined(__WXMSW__) || wxUSE_WXDIB)
1789 else
1790 {
1791 wxImage img = bmp.ConvertToImage();
1792
1793 if ((img.GetWidth() != size*2) || (img.GetHeight() != size*2))
1794 // m_smallImageList->Add(CreateAntialiasedBitmap(CutEmptyBorders(img).Rescale(size*2, size*2)));
1795 m_smallImageList->Add(CreateAntialiasedBitmap(img.Rescale(size*2, size*2)));
1796 else
1797 m_smallImageList->Add(CreateAntialiasedBitmap(img));
1798 }
1799 #endif // wxUSE_IMAGE
1800
1801 m_HashTable->Put(extension, new wxFileIconEntry(id));
1802 return id;
1803
1804 #else // !wxUSE_MIMETYPE
1805
1806 wxUnusedVar(mime);
1807 if (extension == wxT("exe"))
1808 return executable;
1809 else
1810 return file;
1811 #endif // wxUSE_MIMETYPE/!wxUSE_MIMETYPE
1812 }
1813
1814 #endif // wxUSE_DIRDLG || wxUSE_FILEDLG