1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/dirctrlg.cpp
3 // Purpose: wxGenericDirCtrl
4 // Author: Harm van der Heijden, Robert Roebling, Julian Smart
8 // Copyright: (c) Harm van der Heijden, Robert Roebling and Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
19 #if wxUSE_DIRDLG || wxUSE_FILEDLG
21 #include "wx/generic/dirctrlg.h"
28 #include "wx/button.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"
37 #include "wx/textdlg.h"
38 #include "wx/gdicmn.h"
40 #include "wx/module.h"
43 #include "wx/filefn.h"
44 #include "wx/imaglist.h"
45 #include "wx/tokenzr.h"
47 #include "wx/artprov.h"
48 #include "wx/mimetype.h"
51 #include "wx/statline.h"
54 #if defined(__WXMAC__)
55 #include "wx/mac/private.h" // includes mac headers
60 #include "wx/msw/winundef.h"
61 #include "wx/volume.h"
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
66 #if !defined(__GNUWIN32__) || (defined(__MINGW32_MAJOR_VERSION) && __MINGW32_MAJOR_VERSION >= 1)
67 #if !defined(__WXWINCE__)
76 #if defined(__OS2__) || defined(__DOS__)
88 #if defined(__WXMAC__)
89 #include "MoreFilesX.h"
96 // If compiled under Windows, this macro can cause problems
101 bool wxIsDriveAvailable(const wxString
& dirName
);
103 // ----------------------------------------------------------------------------
104 // wxGetAvailableDrives, for WINDOWS, DOS, OS2, MAC, UNIX (returns "/")
105 // ----------------------------------------------------------------------------
107 size_t wxGetAvailableDrives(wxArrayString
&paths
, wxArrayString
&names
, wxArrayInt
&icon_ids
)
109 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__OS2__)
112 // No logical drives; return "\"
113 paths
.Add(wxT("\\"));
114 names
.Add(wxT("\\"));
115 icon_ids
.Add(wxFileIconsTable::computer
);
116 #elif defined(__WIN32__) && wxUSE_FSVOLUME
117 // TODO: this code (using wxFSVolumeBase) should be used for all platforms
118 // but unfortunately wxFSVolumeBase is not implemented everywhere
119 const wxArrayString as
= wxFSVolumeBase::GetVolumes();
121 for (size_t i
= 0; i
< as
.GetCount(); i
++)
123 wxString path
= as
[i
];
124 wxFSVolume
vol(path
);
126 switch (vol
.GetKind())
128 case wxFS_VOL_FLOPPY
:
129 if ( (path
== wxT("a:\\")) || (path
== wxT("b:\\")) )
130 imageId
= wxFileIconsTable::floppy
;
132 imageId
= wxFileIconsTable::removeable
;
134 case wxFS_VOL_DVDROM
:
136 imageId
= wxFileIconsTable::cdrom
;
138 case wxFS_VOL_NETWORK
:
139 if (path
[0] == wxT('\\'))
140 continue; // skip "\\computer\folder"
141 imageId
= wxFileIconsTable::drive
;
146 imageId
= wxFileIconsTable::drive
;
150 names
.Add(vol
.GetDisplayName());
151 icon_ids
.Add(imageId
);
153 #elif defined(__OS2__)
155 ULONG ulDriveNum
= 0;
156 ULONG ulDriveMap
= 0;
157 rc
= ::DosQueryCurrentDisk(&ulDriveNum
, &ulDriveMap
);
163 if (ulDriveMap
& ( 1 << i
))
166 path
.Printf(wxT("%c:\\"), 'A' + i
);
167 name
.Printf(wxT("%c:"), 'A' + i
);
169 // Note: If _filesys is unsupported by some compilers,
170 // we can always replace it by DosQueryFSAttach
171 char filesysname
[20];
173 ULONG cbBuffer
= sizeof(filesysname
);
174 PFSQBUFFER2 pfsqBuffer
= (PFSQBUFFER2
)filesysname
;
175 APIRET rc
= ::DosQueryFSAttach(name
.fn_str(),0,FSAIL_QUERYNAME
,pfsqBuffer
,&cbBuffer
);
178 filesysname
[0] = '\0';
181 _filesys(name
.fn_str(), filesysname
, sizeof(filesysname
));
183 /* FAT, LAN, HPFS, CDFS, NFS */
185 if (path
== wxT("A:\\") || path
== wxT("B:\\"))
186 imageId
= wxFileIconsTable::floppy
;
187 else if (!strcmp(filesysname
, "CDFS"))
188 imageId
= wxFileIconsTable::cdrom
;
189 else if (!strcmp(filesysname
, "LAN") ||
190 !strcmp(filesysname
, "NFS"))
191 imageId
= wxFileIconsTable::drive
;
193 imageId
= wxFileIconsTable::drive
;
196 icon_ids
.Add(imageId
);
201 #else // !__WIN32__, !__OS2__
204 /* If we can switch to the drive, it exists. */
205 for( drive
= 1; drive
<= 26; drive
++ )
208 path
.Printf(wxT("%c:\\"), (char) (drive
+ 'a' - 1));
209 name
.Printf(wxT("%c:"), (char) (drive
+ 'A' - 1));
211 if (wxIsDriveAvailable(path
))
215 icon_ids
.Add((drive
<= 2) ? wxFileIconsTable::floppy
: wxFileIconsTable::drive
);
218 #endif // __WIN32__/!__WIN32__
220 #elif defined(__WXMAC__)
222 ItemCount volumeIndex
= 1;
225 while( noErr
== err
)
227 HFSUniStr255 volumeName
;
229 FSVolumeInfo volumeInfo
;
230 err
= FSGetVolumeInfo(0, volumeIndex
, NULL
, kFSVolInfoFlags
, &volumeInfo
, &volumeName
, &fsRef
);
233 wxString path
= wxMacFSRefToPath( &fsRef
) ;
234 wxString name
= wxMacHFSUniStrToString( &volumeName
) ;
236 if ( (volumeInfo
.flags
& kFSVolFlagSoftwareLockedMask
) || (volumeInfo
.flags
& kFSVolFlagHardwareLockedMask
) )
238 icon_ids
.Add(wxFileIconsTable::cdrom
);
242 icon_ids
.Add(wxFileIconsTable::drive
);
244 // todo other removable
252 #elif defined(__UNIX__)
255 icon_ids
.Add(wxFileIconsTable::computer
);
257 #error "Unsupported platform in wxGenericDirCtrl!"
259 wxASSERT_MSG( (paths
.GetCount() == names
.GetCount()), wxT("The number of paths and their human readable names should be equal in number."));
260 wxASSERT_MSG( (paths
.GetCount() == icon_ids
.GetCount()), wxT("Wrong number of icons for available drives."));
261 return paths
.GetCount();
264 // ----------------------------------------------------------------------------
265 // wxIsDriveAvailable
266 // ----------------------------------------------------------------------------
270 bool wxIsDriveAvailable(const wxString
& dirName
)
272 // FIXME_MGL - this method leads to hang up under Watcom for some reason
274 wxUnusedVar(dirName
);
276 if ( dirName
.length() == 3 && dirName
[1u] == wxT(':') )
278 wxString
dirNameLower(dirName
.Lower());
279 // VS: always return true for removable media, since Win95 doesn't
280 // like it when MS-DOS app accesses empty floppy drive
281 return (dirNameLower
[0u] == wxT('a') ||
282 dirNameLower
[0u] == wxT('b') ||
283 wxDirExists(dirNameLower
));
290 #elif defined(__WINDOWS__) || defined(__OS2__)
292 int setdrive(int WXUNUSED_IN_WINCE(drive
))
296 #elif defined(__GNUWIN32__) && \
297 (defined(__MINGW32_MAJOR_VERSION) && __MINGW32_MAJOR_VERSION >= 1)
298 return _chdrive(drive
);
302 if (drive
< 1 || drive
> 31)
304 newdrive
[0] = (wxChar
)(wxT('A') + drive
- 1);
305 newdrive
[1] = wxT(':');
307 newdrive
[2] = wxT('\\');
308 newdrive
[3] = wxT('\0');
310 newdrive
[2] = wxT('\0');
312 #if defined(__WXMSW__)
313 if (::SetCurrentDirectory(newdrive
))
315 // VA doesn't know what LPSTR is and has its own set
316 if (!DosSetCurrentDir((PSZ
)newdrive
))
324 bool wxIsDriveAvailable(const wxString
& WXUNUSED_IN_WINCE(dirName
))
330 UINT errorMode
= SetErrorMode(SEM_FAILCRITICALERRORS
| SEM_NOOPENFILEERRORBOX
);
334 // Check if this is a root directory and if so,
335 // whether the drive is available.
336 if (dirName
.length() == 3 && dirName
[(size_t)1] == wxT(':'))
338 wxString
dirNameLower(dirName
.Lower());
339 #if defined(__GNUWIN32__) && !(defined(__MINGW32_MAJOR_VERSION) && __MINGW32_MAJOR_VERSION >= 1)
340 success
= wxDirExists(dirNameLower
);
343 // Avoid changing to drive since no media may be inserted.
344 if (dirNameLower
[(size_t)0] == 'a' || dirNameLower
[(size_t)0] == 'b')
347 int currentDrive
= _getdrive();
348 int thisDrive
= (int) (dirNameLower
[(size_t)0] - 'a' + 1) ;
349 int err
= setdrive( thisDrive
) ;
350 setdrive( currentDrive
);
359 (void) SetErrorMode(errorMode
);
365 #endif // __WINDOWS__ || __OS2__
367 #endif // wxUSE_DIRDLG || wxUSE_FILEDLG
373 // Function which is called by quick sort. We want to override the default wxArrayString behaviour,
374 // and sort regardless of case.
375 static int wxCMPFUNC_CONV
wxDirCtrlStringCompareFunction(const wxString
& strFirst
, const wxString
& strSecond
)
377 return strFirst
.CmpNoCase(strSecond
);
380 //-----------------------------------------------------------------------------
382 //-----------------------------------------------------------------------------
384 wxDirItemData::wxDirItemData(const wxString
& path
, const wxString
& name
,
389 /* Insert logic to detect hidden files here
390 * In UnixLand we just check whether the first char is a dot
391 * For FileNameFromPath read LastDirNameInThisPath ;-) */
392 // m_isHidden = (bool)(wxFileNameFromPath(*m_path)[0] == '.');
394 m_isExpanded
= false;
398 void wxDirItemData::SetNewDirName(const wxString
& path
)
401 m_name
= wxFileNameFromPath(path
);
404 bool wxDirItemData::HasSubDirs() const
412 if ( !dir
.Open(m_path
) )
416 return dir
.HasSubDirs();
419 bool wxDirItemData::HasFiles(const wxString
& WXUNUSED(spec
)) const
427 if ( !dir
.Open(m_path
) )
431 return dir
.HasFiles();
434 //-----------------------------------------------------------------------------
436 //-----------------------------------------------------------------------------
439 #if wxUSE_EXTENDED_RTTI
440 WX_DEFINE_FLAGS( wxGenericDirCtrlStyle
)
442 wxBEGIN_FLAGS( wxGenericDirCtrlStyle
)
443 // new style border flags, we put them first to
444 // use them for streaming out
445 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
446 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
447 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
448 wxFLAGS_MEMBER(wxBORDER_RAISED
)
449 wxFLAGS_MEMBER(wxBORDER_STATIC
)
450 wxFLAGS_MEMBER(wxBORDER_NONE
)
452 // old style border flags
453 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
454 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
455 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
456 wxFLAGS_MEMBER(wxRAISED_BORDER
)
457 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
458 wxFLAGS_MEMBER(wxBORDER
)
460 // standard window styles
461 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
462 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
463 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
464 wxFLAGS_MEMBER(wxWANTS_CHARS
)
465 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
466 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
467 wxFLAGS_MEMBER(wxVSCROLL
)
468 wxFLAGS_MEMBER(wxHSCROLL
)
470 wxFLAGS_MEMBER(wxDIRCTRL_DIR_ONLY
)
471 wxFLAGS_MEMBER(wxDIRCTRL_3D_INTERNAL
)
472 wxFLAGS_MEMBER(wxDIRCTRL_SELECT_FIRST
)
473 wxFLAGS_MEMBER(wxDIRCTRL_SHOW_FILTERS
)
475 wxEND_FLAGS( wxGenericDirCtrlStyle
)
477 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGenericDirCtrl
, wxControl
,"wx/dirctrl.h")
479 wxBEGIN_PROPERTIES_TABLE(wxGenericDirCtrl
)
480 wxHIDE_PROPERTY( Children
)
481 wxPROPERTY( DefaultPath
, wxString
, SetDefaultPath
, GetDefaultPath
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
482 wxPROPERTY( Filter
, wxString
, SetFilter
, GetFilter
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
483 wxPROPERTY( DefaultFilter
, int , SetFilterIndex
, GetFilterIndex
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
484 wxPROPERTY_FLAGS( WindowStyle
, wxGenericDirCtrlStyle
, long, SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0, wxT("Helpstring"), wxT("group") )
485 wxEND_PROPERTIES_TABLE()
487 wxBEGIN_HANDLERS_TABLE(wxGenericDirCtrl
)
488 wxEND_HANDLERS_TABLE()
490 wxCONSTRUCTOR_8( wxGenericDirCtrl
, wxWindow
* , Parent
, wxWindowID
, Id
, wxString
, DefaultPath
,
491 wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
, wxString
, Filter
, int , DefaultFilter
)
493 IMPLEMENT_DYNAMIC_CLASS(wxGenericDirCtrl
, wxControl
)
496 BEGIN_EVENT_TABLE(wxGenericDirCtrl
, wxControl
)
497 EVT_TREE_ITEM_EXPANDING (wxID_TREECTRL
, wxGenericDirCtrl::OnExpandItem
)
498 EVT_TREE_ITEM_COLLAPSED (wxID_TREECTRL
, wxGenericDirCtrl::OnCollapseItem
)
499 EVT_TREE_BEGIN_LABEL_EDIT (wxID_TREECTRL
, wxGenericDirCtrl::OnBeginEditItem
)
500 EVT_TREE_END_LABEL_EDIT (wxID_TREECTRL
, wxGenericDirCtrl::OnEndEditItem
)
501 EVT_SIZE (wxGenericDirCtrl::OnSize
)
504 wxGenericDirCtrl::wxGenericDirCtrl(void)
509 void wxGenericDirCtrl::ExpandRoot()
511 ExpandDir(m_rootId
); // automatically expand first level
513 // Expand and select the default path
514 if (!m_defaultPath
.empty())
516 ExpandPath(m_defaultPath
);
521 // On Unix, there's only one node under the (hidden) root node. It
522 // represents the / path, so the user would always have to expand it;
523 // let's do it ourselves
524 ExpandPath( wxT("/") );
529 bool wxGenericDirCtrl::Create(wxWindow
*parent
,
535 const wxString
& filter
,
537 const wxString
& name
)
539 if (!wxControl::Create(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
542 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));
543 SetForegroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
547 long treeStyle
= wxTR_HAS_BUTTONS
;
549 // On Windows CE, if you hide the root, you get a crash when
550 // attempting to access data for children of the root item.
552 treeStyle
|= wxTR_HIDE_ROOT
;
556 treeStyle
|= wxTR_NO_LINES
;
559 if (style
& wxDIRCTRL_EDIT_LABELS
)
560 treeStyle
|= wxTR_EDIT_LABELS
;
562 if ((style
& wxDIRCTRL_3D_INTERNAL
) == 0)
563 treeStyle
|= wxNO_BORDER
;
565 treeStyle
|= wxBORDER_SUNKEN
;
567 long filterStyle
= 0;
568 if ((style
& wxDIRCTRL_3D_INTERNAL
) == 0)
569 filterStyle
|= wxNO_BORDER
;
571 filterStyle
|= wxBORDER_SUNKEN
;
573 m_treeCtrl
= CreateTreeCtrl(this, wxID_TREECTRL
,
574 wxPoint(0,0), GetClientSize(), treeStyle
);
576 if (!filter
.empty() && (style
& wxDIRCTRL_SHOW_FILTERS
))
577 m_filterListCtrl
= new wxDirFilterListCtrl(this, wxID_FILTERLISTCTRL
, wxDefaultPosition
, wxDefaultSize
, filterStyle
);
582 if (m_filter
.empty())
586 m_filter
= wxT("*.*");
589 SetFilterIndex(defaultFilter
);
591 if (m_filterListCtrl
)
592 m_filterListCtrl
->FillFilterList(filter
, defaultFilter
);
594 m_treeCtrl
->SetImageList(wxTheFileIconsTable
->GetSmallImageList());
596 m_showHidden
= false;
597 wxDirItemData
* rootData
= new wxDirItemData(wxEmptyString
, wxEmptyString
, true);
601 #if defined(__WINDOWS__) || defined(__OS2__) || defined(__DOS__)
602 rootName
= _("Computer");
604 rootName
= _("Sections");
607 m_rootId
= m_treeCtrl
->AddRoot( rootName
, 3, -1, rootData
);
608 m_treeCtrl
->SetItemHasChildren(m_rootId
);
612 SetInitialSize(size
);
618 wxGenericDirCtrl::~wxGenericDirCtrl()
622 void wxGenericDirCtrl::Init()
624 m_showHidden
= false;
626 m_currentFilterStr
= wxEmptyString
; // Default: any file
628 m_filterListCtrl
= NULL
;
631 wxTreeCtrl
* wxGenericDirCtrl::CreateTreeCtrl(wxWindow
*parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
, long treeStyle
)
633 return new wxTreeCtrl(parent
, id
, pos
, size
, treeStyle
);
636 void wxGenericDirCtrl::ShowHidden( bool show
)
638 if ( m_showHidden
== show
)
643 wxString path
= GetPath();
649 wxGenericDirCtrl::AddSection(const wxString
& path
, const wxString
& name
, int imageId
)
651 wxDirItemData
*dir_item
= new wxDirItemData(path
,name
,true);
653 wxTreeItemId id
= AppendItem( m_rootId
, name
, imageId
, -1, dir_item
);
655 m_treeCtrl
->SetItemHasChildren(id
);
660 void wxGenericDirCtrl::SetupSections()
662 wxArrayString paths
, names
;
665 size_t n
, count
= wxGetAvailableDrives(paths
, names
, icons
);
668 wxString home
= wxGetHomeDir();
669 AddSection( home
, _("Home directory"), 1);
670 home
+= wxT("/Desktop");
671 AddSection( home
, _("Desktop"), 1);
674 for (n
= 0; n
< count
; n
++)
675 AddSection(paths
[n
], names
[n
], icons
[n
]);
678 void wxGenericDirCtrl::OnBeginEditItem(wxTreeEvent
&event
)
680 // don't rename the main entry "Sections"
681 if (event
.GetItem() == m_rootId
)
687 // don't rename the individual sections
688 if (m_treeCtrl
->GetItemParent( event
.GetItem() ) == m_rootId
)
695 void wxGenericDirCtrl::OnEndEditItem(wxTreeEvent
&event
)
697 if (event
.IsEditCancelled())
700 if ((event
.GetLabel().empty()) ||
701 (event
.GetLabel() == wxT(".")) ||
702 (event
.GetLabel() == wxT("..")) ||
703 (event
.GetLabel().Find(wxT('/')) != wxNOT_FOUND
) ||
704 (event
.GetLabel().Find(wxT('\\')) != wxNOT_FOUND
) ||
705 (event
.GetLabel().Find(wxT('|')) != wxNOT_FOUND
))
707 wxMessageDialog
dialog(this, _("Illegal directory name."), _("Error"), wxOK
| wxICON_ERROR
);
713 wxTreeItemId id
= event
.GetItem();
714 wxDirItemData
*data
= (wxDirItemData
*)m_treeCtrl
->GetItemData( id
);
717 wxString
new_name( wxPathOnly( data
->m_path
) );
718 new_name
+= wxString(wxFILE_SEP_PATH
);
719 new_name
+= event
.GetLabel();
723 if (wxFileExists(new_name
))
725 wxMessageDialog
dialog(this, _("File name exists already."), _("Error"), wxOK
| wxICON_ERROR
);
730 if (wxRenameFile(data
->m_path
,new_name
))
732 data
->SetNewDirName( new_name
);
736 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
742 void wxGenericDirCtrl::OnExpandItem(wxTreeEvent
&event
)
744 wxTreeItemId parentId
= event
.GetItem();
746 // VS: this is needed because the event handler is called from wxTreeCtrl
747 // ctor when wxTR_HIDE_ROOT was specified
749 if (!m_rootId
.IsOk())
751 m_rootId
= m_treeCtrl
->GetRootItem();
756 void wxGenericDirCtrl::OnCollapseItem(wxTreeEvent
&event
)
758 CollapseDir(event
.GetItem());
761 void wxGenericDirCtrl::CollapseDir(wxTreeItemId parentId
)
765 wxDirItemData
*data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(parentId
);
766 if (!data
->m_isExpanded
)
769 data
->m_isExpanded
= false;
770 wxTreeItemIdValue cookie
;
771 /* Workaround because DeleteChildren has disapeared (why?) and
772 * CollapseAndReset doesn't work as advertised (deletes parent too) */
773 child
= m_treeCtrl
->GetFirstChild(parentId
, cookie
);
776 m_treeCtrl
->Delete(child
);
777 /* Not GetNextChild below, because the cookie mechanism can't
778 * handle disappearing children! */
779 child
= m_treeCtrl
->GetFirstChild(parentId
, cookie
);
781 if (parentId
!= m_treeCtrl
->GetRootItem())
782 m_treeCtrl
->Collapse(parentId
);
785 void wxGenericDirCtrl::ExpandDir(wxTreeItemId parentId
)
787 wxDirItemData
*data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(parentId
);
789 if (data
->m_isExpanded
)
792 data
->m_isExpanded
= true;
794 if (parentId
== m_treeCtrl
->GetRootItem())
802 wxString search
,path
,filename
;
804 wxString
dirName(data
->m_path
);
806 #if (defined(__WINDOWS__) && !defined(__WXWINCE__)) || defined(__DOS__) || defined(__OS2__)
807 // Check if this is a root directory and if so,
808 // whether the drive is avaiable.
809 if (!wxIsDriveAvailable(dirName
))
811 data
->m_isExpanded
= false;
812 //wxMessageBox(wxT("Sorry, this drive is not available."));
817 // This may take a longish time. Go to busy cursor
820 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__OS2__)
821 if (dirName
.Last() == ':')
822 dirName
+= wxString(wxFILE_SEP_PATH
);
826 wxArrayString filenames
;
829 wxString eachFilename
;
836 int style
= wxDIR_DIRS
;
837 if (m_showHidden
) style
|= wxDIR_HIDDEN
;
838 if (d
.GetFirst(& eachFilename
, wxEmptyString
, style
))
842 if ((eachFilename
!= wxT(".")) && (eachFilename
!= wxT("..")))
844 dirs
.Add(eachFilename
);
847 while (d
.GetNext(&eachFilename
));
850 dirs
.Sort(wxDirCtrlStringCompareFunction
);
852 // Now do the filenames -- but only if we're allowed to
853 if ((GetWindowStyle() & wxDIRCTRL_DIR_ONLY
) == 0)
859 int style
= wxDIR_FILES
;
860 if (m_showHidden
) style
|= wxDIR_HIDDEN
;
861 // Process each filter (ex: "JPEG Files (*.jpg;*.jpeg)|*.jpg;*.jpeg")
862 wxStringTokenizer strTok
;
864 strTok
.SetString(m_currentFilterStr
,wxT(";"));
865 while(strTok
.HasMoreTokens())
867 curFilter
= strTok
.GetNextToken();
868 if (d
.GetFirst(& eachFilename
, curFilter
, style
))
872 if ((eachFilename
!= wxT(".")) && (eachFilename
!= wxT("..")))
874 filenames
.Add(eachFilename
);
877 while (d
.GetNext(& eachFilename
));
881 filenames
.Sort(wxDirCtrlStringCompareFunction
);
884 // Add the sorted dirs
886 for (i
= 0; i
< dirs
.Count(); i
++)
888 eachFilename
= dirs
[i
];
890 if (!wxEndsWithPathSeparator(path
))
891 path
+= wxString(wxFILE_SEP_PATH
);
892 path
+= eachFilename
;
894 wxDirItemData
*dir_item
= new wxDirItemData(path
,eachFilename
,true);
895 wxTreeItemId id
= AppendItem( parentId
, eachFilename
,
896 wxFileIconsTable::folder
, -1, dir_item
);
897 m_treeCtrl
->SetItemImage( id
, wxFileIconsTable::folder_open
,
898 wxTreeItemIcon_Expanded
);
900 // Has this got any children? If so, make it expandable.
901 // (There are two situations when a dir has children: either it
902 // has subdirectories or it contains files that weren't filtered
903 // out. The latter only applies to dirctrl with files.)
904 if ( dir_item
->HasSubDirs() ||
905 (((GetWindowStyle() & wxDIRCTRL_DIR_ONLY
) == 0) &&
906 dir_item
->HasFiles(m_currentFilterStr
)) )
908 m_treeCtrl
->SetItemHasChildren(id
);
912 // Add the sorted filenames
913 if ((GetWindowStyle() & wxDIRCTRL_DIR_ONLY
) == 0)
915 for (i
= 0; i
< filenames
.Count(); i
++)
917 eachFilename
= filenames
[i
];
919 if (!wxEndsWithPathSeparator(path
))
920 path
+= wxString(wxFILE_SEP_PATH
);
921 path
+= eachFilename
;
922 //path = dirName + wxString(wxT("/")) + eachFilename;
923 wxDirItemData
*dir_item
= new wxDirItemData(path
,eachFilename
,false);
924 int image_id
= wxFileIconsTable::file
;
925 if (eachFilename
.Find(wxT('.')) != wxNOT_FOUND
)
926 image_id
= wxTheFileIconsTable
->GetIconID(eachFilename
.AfterLast(wxT('.')));
927 (void) AppendItem( parentId
, eachFilename
, image_id
, -1, dir_item
);
932 void wxGenericDirCtrl::ReCreateTree()
934 CollapseDir(m_treeCtrl
->GetRootItem());
938 void wxGenericDirCtrl::CollapseTree()
940 wxTreeItemIdValue cookie
;
941 wxTreeItemId child
= m_treeCtrl
->GetFirstChild(m_rootId
, cookie
);
945 child
= m_treeCtrl
->GetNextChild(m_rootId
, cookie
);
949 // Find the child that matches the first part of 'path'.
950 // E.g. if a child path is "/usr" and 'path' is "/usr/include"
951 // then the child for /usr is returned.
952 wxTreeItemId
wxGenericDirCtrl::FindChild(wxTreeItemId parentId
, const wxString
& path
, bool& done
)
954 wxString
path2(path
);
956 // Make sure all separators are as per the current platform
957 path2
.Replace(wxT("\\"), wxString(wxFILE_SEP_PATH
));
958 path2
.Replace(wxT("/"), wxString(wxFILE_SEP_PATH
));
960 // Append a separator to foil bogus substring matching
961 path2
+= wxString(wxFILE_SEP_PATH
);
963 // In MSW or PM, case is not significant
964 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__OS2__)
968 wxTreeItemIdValue cookie
;
969 wxTreeItemId childId
= m_treeCtrl
->GetFirstChild(parentId
, cookie
);
970 while (childId
.IsOk())
972 wxDirItemData
* data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(childId
);
974 if (data
&& !data
->m_path
.empty())
976 wxString
childPath(data
->m_path
);
977 if (!wxEndsWithPathSeparator(childPath
))
978 childPath
+= wxString(wxFILE_SEP_PATH
);
980 // In MSW and PM, case is not significant
981 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__OS2__)
982 childPath
.MakeLower();
985 if (childPath
.length() <= path2
.length())
987 wxString path3
= path2
.Mid(0, childPath
.length());
988 if (childPath
== path3
)
990 if (path3
.length() == path2
.length())
999 childId
= m_treeCtrl
->GetNextChild(parentId
, cookie
);
1001 wxTreeItemId invalid
;
1005 // Try to expand as much of the given path as possible,
1006 // and select the given tree item.
1007 bool wxGenericDirCtrl::ExpandPath(const wxString
& path
)
1010 wxTreeItemId id
= FindChild(m_rootId
, path
, done
);
1011 wxTreeItemId lastId
= id
; // The last non-zero id
1012 while (id
.IsOk() && !done
)
1016 id
= FindChild(id
, path
, done
);
1023 wxDirItemData
*data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(lastId
);
1026 m_treeCtrl
->Expand(lastId
);
1028 if ((GetWindowStyle() & wxDIRCTRL_SELECT_FIRST
) && data
->m_isDir
)
1030 // Find the first file in this directory
1031 wxTreeItemIdValue cookie
;
1032 wxTreeItemId childId
= m_treeCtrl
->GetFirstChild(lastId
, cookie
);
1033 bool selectedChild
= false;
1034 while (childId
.IsOk())
1036 data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(childId
);
1038 if (data
&& data
->m_path
!= wxEmptyString
&& !data
->m_isDir
)
1040 m_treeCtrl
->SelectItem(childId
);
1041 m_treeCtrl
->EnsureVisible(childId
);
1042 selectedChild
= true;
1045 childId
= m_treeCtrl
->GetNextChild(lastId
, cookie
);
1049 m_treeCtrl
->SelectItem(lastId
);
1050 m_treeCtrl
->EnsureVisible(lastId
);
1055 m_treeCtrl
->SelectItem(lastId
);
1056 m_treeCtrl
->EnsureVisible(lastId
);
1063 bool wxGenericDirCtrl::CollapsePath(const wxString
& path
)
1066 wxTreeItemId id
= FindChild(m_rootId
, path
, done
);
1067 wxTreeItemId lastId
= id
; // The last non-zero id
1069 while ( id
.IsOk() && !done
)
1073 id
= FindChild(id
, path
, done
);
1079 if ( !lastId
.IsOk() )
1082 m_treeCtrl
->SelectItem(lastId
);
1083 m_treeCtrl
->EnsureVisible(lastId
);
1089 wxString
wxGenericDirCtrl::GetPath() const
1091 wxTreeItemId id
= m_treeCtrl
->GetSelection();
1094 wxDirItemData
* data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(id
);
1095 return data
->m_path
;
1098 return wxEmptyString
;
1101 wxString
wxGenericDirCtrl::GetFilePath() const
1103 wxTreeItemId id
= m_treeCtrl
->GetSelection();
1106 wxDirItemData
* data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(id
);
1108 return wxEmptyString
;
1110 return data
->m_path
;
1113 return wxEmptyString
;
1116 void wxGenericDirCtrl::SetPath(const wxString
& path
)
1118 m_defaultPath
= path
;
1125 void wxGenericDirCtrl::FindChildFiles(wxTreeItemId id
, int dirFlags
, wxArrayString
& filenames
)
1127 wxDirItemData
*data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(id
);
1129 // This may take a longish time. Go to busy cursor
1134 wxString search
,path
,filename
;
1136 wxString
dirName(data
->m_path
);
1138 #if defined(__WXMSW__) || defined(__OS2__)
1139 if (dirName
.Last() == ':')
1140 dirName
+= wxString(wxFILE_SEP_PATH
);
1144 wxString eachFilename
;
1151 if (d
.GetFirst(& eachFilename
, m_currentFilterStr
, dirFlags
))
1155 if ((eachFilename
!= wxT(".")) && (eachFilename
!= wxT("..")))
1157 filenames
.Add(eachFilename
);
1160 while (d
.GetNext(& eachFilename
)) ;
1166 void wxGenericDirCtrl::SetFilterIndex(int n
)
1168 m_currentFilter
= n
;
1171 if (ExtractWildcard(m_filter
, n
, f
, d
))
1172 m_currentFilterStr
= f
;
1175 m_currentFilterStr
= wxT("*");
1177 m_currentFilterStr
= wxT("*.*");
1181 void wxGenericDirCtrl::SetFilter(const wxString
& filter
)
1186 if (ExtractWildcard(m_filter
, m_currentFilter
, f
, d
))
1187 m_currentFilterStr
= f
;
1190 m_currentFilterStr
= wxT("*");
1192 m_currentFilterStr
= wxT("*.*");
1196 // Extract description and actual filter from overall filter string
1197 bool wxGenericDirCtrl::ExtractWildcard(const wxString
& filterStr
, int n
, wxString
& filter
, wxString
& description
)
1199 wxArrayString filters
, descriptions
;
1200 int count
= wxParseCommonDialogsFilter(filterStr
, descriptions
, filters
);
1201 if (count
> 0 && n
< count
)
1203 filter
= filters
[n
];
1204 description
= descriptions
[n
];
1212 void wxGenericDirCtrl::DoResize()
1214 wxSize sz
= GetClientSize();
1215 int verticalSpacing
= 3;
1219 if (m_filterListCtrl
)
1221 filterSz
= m_filterListCtrl
->GetSize();
1222 sz
.y
-= (filterSz
.y
+ verticalSpacing
);
1224 m_treeCtrl
->SetSize(0, 0, sz
.x
, sz
.y
);
1225 if (m_filterListCtrl
)
1227 m_filterListCtrl
->SetSize(0, sz
.y
+ verticalSpacing
, sz
.x
, filterSz
.y
);
1228 // Don't know why, but this needs refreshing after a resize (wxMSW)
1229 m_filterListCtrl
->Refresh();
1235 void wxGenericDirCtrl::OnSize(wxSizeEvent
& WXUNUSED(event
))
1240 wxTreeItemId
wxGenericDirCtrl::AppendItem (const wxTreeItemId
& parent
,
1241 const wxString
& text
,
1242 int image
, int selectedImage
,
1243 wxTreeItemData
* data
)
1245 wxTreeCtrl
*treeCtrl
= GetTreeCtrl ();
1247 wxASSERT (treeCtrl
);
1251 return treeCtrl
->AppendItem (parent
, text
, image
, selectedImage
, data
);
1255 return wxTreeItemId();
1260 //-----------------------------------------------------------------------------
1261 // wxDirFilterListCtrl
1262 //-----------------------------------------------------------------------------
1264 IMPLEMENT_CLASS(wxDirFilterListCtrl
, wxChoice
)
1266 BEGIN_EVENT_TABLE(wxDirFilterListCtrl
, wxChoice
)
1267 EVT_CHOICE(wxID_ANY
, wxDirFilterListCtrl::OnSelFilter
)
1270 bool wxDirFilterListCtrl::Create(wxGenericDirCtrl
* parent
, const wxWindowID id
,
1276 return wxChoice::Create(parent
, id
, pos
, size
, 0, NULL
, style
);
1279 void wxDirFilterListCtrl::Init()
1284 void wxDirFilterListCtrl::OnSelFilter(wxCommandEvent
& WXUNUSED(event
))
1286 int sel
= GetSelection();
1288 wxString currentPath
= m_dirCtrl
->GetPath();
1290 m_dirCtrl
->SetFilterIndex(sel
);
1292 // If the filter has changed, the view is out of date, so
1293 // collapse the tree.
1294 m_dirCtrl
->ReCreateTree();
1296 // Try to restore the selection, or at least the directory
1297 m_dirCtrl
->ExpandPath(currentPath
);
1300 void wxDirFilterListCtrl::FillFilterList(const wxString
& filter
, int defaultFilter
)
1303 wxArrayString descriptions
, filters
;
1304 size_t n
= (size_t) wxParseCommonDialogsFilter(filter
, descriptions
, filters
);
1306 if (n
> 0 && defaultFilter
< (int) n
)
1308 for (size_t i
= 0; i
< n
; i
++)
1309 Append(descriptions
[i
]);
1310 SetSelection(defaultFilter
);
1313 #endif // wxUSE_DIRDLG
1315 #if wxUSE_DIRDLG || wxUSE_FILEDLG
1317 // ----------------------------------------------------------------------------
1318 // wxFileIconsTable icons
1319 // ----------------------------------------------------------------------------
1322 /* Computer (c) Julian Smart */
1323 static const char * file_icons_tbl_computer_xpm
[] = {
1324 /* columns rows colors chars-per-pixel */
1386 #endif // GTK+ < 2.4
1388 // ----------------------------------------------------------------------------
1389 // wxFileIconsTable & friends
1390 // ----------------------------------------------------------------------------
1392 // global instance of a wxFileIconsTable
1393 wxFileIconsTable
* wxTheFileIconsTable
= (wxFileIconsTable
*)NULL
;
1395 // A module to allow icons table cleanup
1397 class wxFileIconsTableModule
: public wxModule
1399 DECLARE_DYNAMIC_CLASS(wxFileIconsTableModule
)
1401 wxFileIconsTableModule() {}
1402 bool OnInit() { wxTheFileIconsTable
= new wxFileIconsTable
; return true; }
1405 if (wxTheFileIconsTable
)
1407 delete wxTheFileIconsTable
;
1408 wxTheFileIconsTable
= NULL
;
1413 IMPLEMENT_DYNAMIC_CLASS(wxFileIconsTableModule
, wxModule
)
1415 class wxFileIconEntry
: public wxObject
1418 wxFileIconEntry(int i
) { id
= i
; }
1423 wxFileIconsTable::wxFileIconsTable()
1426 m_smallImageList
= NULL
;
1429 wxFileIconsTable::~wxFileIconsTable()
1433 WX_CLEAR_HASH_TABLE(*m_HashTable
);
1436 if (m_smallImageList
) delete m_smallImageList
;
1439 // delayed initialization - wait until first use (wxArtProv not created yet)
1440 void wxFileIconsTable::Create()
1442 wxCHECK_RET(!m_smallImageList
&& !m_HashTable
, wxT("creating icons twice"));
1443 m_HashTable
= new wxHashTable(wxKEY_STRING
);
1444 m_smallImageList
= new wxImageList(16, 16);
1447 m_smallImageList
->Add(wxArtProvider::GetBitmap(wxART_FOLDER
,
1451 m_smallImageList
->Add(wxArtProvider::GetBitmap(wxART_FOLDER_OPEN
,
1456 // GTK24 uses this icon in the file open dialog
1457 m_smallImageList
->Add(wxArtProvider::GetBitmap(wxART_HARDDISK
,
1461 m_smallImageList
->Add(wxIcon(file_icons_tbl_computer_xpm
));
1464 m_smallImageList
->Add(wxArtProvider::GetBitmap(wxART_HARDDISK
,
1468 m_smallImageList
->Add(wxArtProvider::GetBitmap(wxART_CDROM
,
1472 m_smallImageList
->Add(wxArtProvider::GetBitmap(wxART_FLOPPY
,
1476 m_smallImageList
->Add(wxArtProvider::GetBitmap(wxART_REMOVABLE
,
1480 m_smallImageList
->Add(wxArtProvider::GetBitmap(wxART_NORMAL_FILE
,
1484 if (GetIconID(wxEmptyString
, _T("application/x-executable")) == file
)
1486 m_smallImageList
->Add(wxArtProvider::GetBitmap(wxART_EXECUTABLE_FILE
,
1489 delete m_HashTable
->Get(_T("exe"));
1490 m_HashTable
->Delete(_T("exe"));
1491 m_HashTable
->Put(_T("exe"), new wxFileIconEntry(executable
));
1493 /* else put into list by GetIconID
1494 (KDE defines application/x-executable for *.exe and has nice icon)
1498 wxImageList
*wxFileIconsTable::GetSmallImageList()
1500 if (!m_smallImageList
)
1503 return m_smallImageList
;
1506 #if wxUSE_MIMETYPE && wxUSE_IMAGE && (!defined(__WXMSW__) || wxUSE_WXDIB)
1507 // VS: we don't need this function w/o wxMimeTypesManager because we'll only have
1508 // one icon and we won't resize it
1510 static wxBitmap
CreateAntialiasedBitmap(const wxImage
& img
)
1512 const unsigned int size
= 16;
1514 wxImage
smallimg (size
, size
);
1515 unsigned char *p1
, *p2
, *ps
;
1516 unsigned char mr
= img
.GetMaskRed(),
1517 mg
= img
.GetMaskGreen(),
1518 mb
= img
.GetMaskBlue();
1521 unsigned sr
, sg
, sb
, smask
;
1523 p1
= img
.GetData(), p2
= img
.GetData() + 3 * size
*2, ps
= smallimg
.GetData();
1524 smallimg
.SetMaskColour(mr
, mr
, mr
);
1526 for (y
= 0; y
< size
; y
++)
1528 for (x
= 0; x
< size
; x
++)
1530 sr
= sg
= sb
= smask
= 0;
1531 if (p1
[0] != mr
|| p1
[1] != mg
|| p1
[2] != mb
)
1532 sr
+= p1
[0], sg
+= p1
[1], sb
+= p1
[2];
1535 if (p1
[0] != mr
|| p1
[1] != mg
|| p1
[2] != mb
)
1536 sr
+= p1
[0], sg
+= p1
[1], sb
+= p1
[2];
1539 if (p2
[0] != mr
|| p2
[1] != mg
|| p2
[2] != mb
)
1540 sr
+= p2
[0], sg
+= p2
[1], sb
+= p2
[2];
1543 if (p2
[0] != mr
|| p2
[1] != mg
|| p2
[2] != mb
)
1544 sr
+= p2
[0], sg
+= p2
[1], sb
+= p2
[2];
1549 ps
[0] = ps
[1] = ps
[2] = mr
;
1552 ps
[0] = (unsigned char)(sr
>> 2);
1553 ps
[1] = (unsigned char)(sg
>> 2);
1554 ps
[2] = (unsigned char)(sb
>> 2);
1558 p1
+= size
*2 * 3, p2
+= size
*2 * 3;
1561 return wxBitmap(smallimg
);
1564 // This function is currently not unused anymore
1566 // finds empty borders and return non-empty area of image:
1567 static wxImage
CutEmptyBorders(const wxImage
& img
)
1569 unsigned char mr
= img
.GetMaskRed(),
1570 mg
= img
.GetMaskGreen(),
1571 mb
= img
.GetMaskBlue();
1572 unsigned char *dt
= img
.GetData(), *dttmp
;
1573 unsigned w
= img
.GetWidth(), h
= img
.GetHeight();
1575 unsigned top
, bottom
, left
, right
, i
;
1578 #define MK_DTTMP(x,y) dttmp = dt + ((x + y * w) * 3)
1579 #define NOEMPTY_PIX(empt) if (dttmp[0] != mr || dttmp[1] != mg || dttmp[2] != mb) {empt = false; break;}
1581 for (empt
= true, top
= 0; empt
&& top
< h
; top
++)
1584 for (i
= 0; i
< w
; i
++, dttmp
+=3)
1587 for (empt
= true, bottom
= h
-1; empt
&& bottom
> top
; bottom
--)
1589 MK_DTTMP(0, bottom
);
1590 for (i
= 0; i
< w
; i
++, dttmp
+=3)
1593 for (empt
= true, left
= 0; empt
&& left
< w
; left
++)
1596 for (i
= 0; i
< h
; i
++, dttmp
+=3*w
)
1599 for (empt
= true, right
= w
-1; empt
&& right
> left
; right
--)
1602 for (i
= 0; i
< h
; i
++, dttmp
+=3*w
)
1605 top
--, left
--, bottom
++, right
++;
1607 return img
.GetSubImage(wxRect(left
, top
, right
- left
+ 1, bottom
- top
+ 1));
1611 #endif // wxUSE_MIMETYPE
1613 int wxFileIconsTable::GetIconID(const wxString
& extension
, const wxString
& mime
)
1615 if (!m_smallImageList
)
1619 if (!extension
.empty())
1621 wxFileIconEntry
*entry
= (wxFileIconEntry
*) m_HashTable
->Get(extension
);
1622 if (entry
) return (entry
-> id
);
1625 wxFileType
*ft
= (mime
.empty()) ?
1626 wxTheMimeTypesManager
-> GetFileTypeFromExtension(extension
) :
1627 wxTheMimeTypesManager
-> GetFileTypeFromMimeType(mime
);
1629 wxIconLocation iconLoc
;
1634 if ( ft
&& ft
->GetIcon(&iconLoc
) )
1636 ic
= wxIcon( iconLoc
);
1645 m_HashTable
->Put(extension
, new wxFileIconEntry(newid
));
1650 bmp
.CopyFromIcon(ic
);
1655 m_HashTable
->Put(extension
, new wxFileIconEntry(newid
));
1659 const unsigned int size
= 16;
1661 int id
= m_smallImageList
->GetImageCount();
1662 if ((bmp
.GetWidth() == (int) size
) && (bmp
.GetHeight() == (int) size
))
1664 m_smallImageList
->Add(bmp
);
1666 #if wxUSE_IMAGE && (!defined(__WXMSW__) || wxUSE_WXDIB)
1669 wxImage img
= bmp
.ConvertToImage();
1671 if ((img
.GetWidth() != size
*2) || (img
.GetHeight() != size
*2))
1672 // m_smallImageList->Add(CreateAntialiasedBitmap(CutEmptyBorders(img).Rescale(size*2, size*2)));
1673 m_smallImageList
->Add(CreateAntialiasedBitmap(img
.Rescale(size
*2, size
*2)));
1675 m_smallImageList
->Add(CreateAntialiasedBitmap(img
));
1677 #endif // wxUSE_IMAGE
1679 m_HashTable
->Put(extension
, new wxFileIconEntry(id
));
1682 #else // !wxUSE_MIMETYPE
1685 if (extension
== wxT("exe"))
1689 #endif // wxUSE_MIMETYPE/!wxUSE_MIMETYPE
1692 #endif // wxUSE_DIRDLG || wxUSE_FILEDLG