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
)
640 wxString path
= GetPath();
646 wxGenericDirCtrl::AddSection(const wxString
& path
, const wxString
& name
, int imageId
)
648 wxDirItemData
*dir_item
= new wxDirItemData(path
,name
,true);
650 wxTreeItemId id
= AppendItem( m_rootId
, name
, imageId
, -1, dir_item
);
652 m_treeCtrl
->SetItemHasChildren(id
);
657 void wxGenericDirCtrl::SetupSections()
659 wxArrayString paths
, names
;
662 size_t n
, count
= wxGetAvailableDrives(paths
, names
, icons
);
665 wxString home
= wxGetHomeDir();
666 AddSection( home
, _("Home directory"), 1);
667 home
+= wxT("/Desktop");
668 AddSection( home
, _("Desktop"), 1);
671 for (n
= 0; n
< count
; n
++)
672 AddSection(paths
[n
], names
[n
], icons
[n
]);
675 void wxGenericDirCtrl::OnBeginEditItem(wxTreeEvent
&event
)
677 // don't rename the main entry "Sections"
678 if (event
.GetItem() == m_rootId
)
684 // don't rename the individual sections
685 if (m_treeCtrl
->GetItemParent( event
.GetItem() ) == m_rootId
)
692 void wxGenericDirCtrl::OnEndEditItem(wxTreeEvent
&event
)
694 if (event
.IsEditCancelled())
697 if ((event
.GetLabel().empty()) ||
698 (event
.GetLabel() == wxT(".")) ||
699 (event
.GetLabel() == wxT("..")) ||
700 (event
.GetLabel().Find(wxT('/')) != wxNOT_FOUND
) ||
701 (event
.GetLabel().Find(wxT('\\')) != wxNOT_FOUND
) ||
702 (event
.GetLabel().Find(wxT('|')) != wxNOT_FOUND
))
704 wxMessageDialog
dialog(this, _("Illegal directory name."), _("Error"), wxOK
| wxICON_ERROR
);
710 wxTreeItemId id
= event
.GetItem();
711 wxDirItemData
*data
= (wxDirItemData
*)m_treeCtrl
->GetItemData( id
);
714 wxString
new_name( wxPathOnly( data
->m_path
) );
715 new_name
+= wxString(wxFILE_SEP_PATH
);
716 new_name
+= event
.GetLabel();
720 if (wxFileExists(new_name
))
722 wxMessageDialog
dialog(this, _("File name exists already."), _("Error"), wxOK
| wxICON_ERROR
);
727 if (wxRenameFile(data
->m_path
,new_name
))
729 data
->SetNewDirName( new_name
);
733 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
739 void wxGenericDirCtrl::OnExpandItem(wxTreeEvent
&event
)
741 wxTreeItemId parentId
= event
.GetItem();
743 // VS: this is needed because the event handler is called from wxTreeCtrl
744 // ctor when wxTR_HIDE_ROOT was specified
746 if (!m_rootId
.IsOk())
748 m_rootId
= m_treeCtrl
->GetRootItem();
753 void wxGenericDirCtrl::OnCollapseItem(wxTreeEvent
&event
)
755 CollapseDir(event
.GetItem());
758 void wxGenericDirCtrl::CollapseDir(wxTreeItemId parentId
)
762 wxDirItemData
*data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(parentId
);
763 if (!data
->m_isExpanded
)
766 data
->m_isExpanded
= false;
767 wxTreeItemIdValue cookie
;
768 /* Workaround because DeleteChildren has disapeared (why?) and
769 * CollapseAndReset doesn't work as advertised (deletes parent too) */
770 child
= m_treeCtrl
->GetFirstChild(parentId
, cookie
);
773 m_treeCtrl
->Delete(child
);
774 /* Not GetNextChild below, because the cookie mechanism can't
775 * handle disappearing children! */
776 child
= m_treeCtrl
->GetFirstChild(parentId
, cookie
);
778 if (parentId
!= m_treeCtrl
->GetRootItem())
779 m_treeCtrl
->Collapse(parentId
);
782 void wxGenericDirCtrl::ExpandDir(wxTreeItemId parentId
)
784 wxDirItemData
*data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(parentId
);
786 if (data
->m_isExpanded
)
789 data
->m_isExpanded
= true;
791 if (parentId
== m_treeCtrl
->GetRootItem())
799 wxString search
,path
,filename
;
801 wxString
dirName(data
->m_path
);
803 #if (defined(__WINDOWS__) && !defined(__WXWINCE__)) || defined(__DOS__) || defined(__OS2__)
804 // Check if this is a root directory and if so,
805 // whether the drive is avaiable.
806 if (!wxIsDriveAvailable(dirName
))
808 data
->m_isExpanded
= false;
809 //wxMessageBox(wxT("Sorry, this drive is not available."));
814 // This may take a longish time. Go to busy cursor
817 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__OS2__)
818 if (dirName
.Last() == ':')
819 dirName
+= wxString(wxFILE_SEP_PATH
);
823 wxArrayString filenames
;
826 wxString eachFilename
;
833 int style
= wxDIR_DIRS
;
834 if (m_showHidden
) style
|= wxDIR_HIDDEN
;
835 if (d
.GetFirst(& eachFilename
, wxEmptyString
, style
))
839 if ((eachFilename
!= wxT(".")) && (eachFilename
!= wxT("..")))
841 dirs
.Add(eachFilename
);
844 while (d
.GetNext(&eachFilename
));
847 dirs
.Sort(wxDirCtrlStringCompareFunction
);
849 // Now do the filenames -- but only if we're allowed to
850 if ((GetWindowStyle() & wxDIRCTRL_DIR_ONLY
) == 0)
856 int style
= wxDIR_FILES
;
857 if (m_showHidden
) style
|= wxDIR_HIDDEN
;
858 // Process each filter (ex: "JPEG Files (*.jpg;*.jpeg)|*.jpg;*.jpeg")
859 wxStringTokenizer strTok
;
861 strTok
.SetString(m_currentFilterStr
,wxT(";"));
862 while(strTok
.HasMoreTokens())
864 curFilter
= strTok
.GetNextToken();
865 if (d
.GetFirst(& eachFilename
, curFilter
, style
))
869 if ((eachFilename
!= wxT(".")) && (eachFilename
!= wxT("..")))
871 filenames
.Add(eachFilename
);
874 while (d
.GetNext(& eachFilename
));
878 filenames
.Sort(wxDirCtrlStringCompareFunction
);
881 // Add the sorted dirs
883 for (i
= 0; i
< dirs
.Count(); i
++)
885 eachFilename
= dirs
[i
];
887 if (!wxEndsWithPathSeparator(path
))
888 path
+= wxString(wxFILE_SEP_PATH
);
889 path
+= eachFilename
;
891 wxDirItemData
*dir_item
= new wxDirItemData(path
,eachFilename
,true);
892 wxTreeItemId id
= AppendItem( parentId
, eachFilename
,
893 wxFileIconsTable::folder
, -1, dir_item
);
894 m_treeCtrl
->SetItemImage( id
, wxFileIconsTable::folder_open
,
895 wxTreeItemIcon_Expanded
);
897 // Has this got any children? If so, make it expandable.
898 // (There are two situations when a dir has children: either it
899 // has subdirectories or it contains files that weren't filtered
900 // out. The latter only applies to dirctrl with files.)
901 if ( dir_item
->HasSubDirs() ||
902 (((GetWindowStyle() & wxDIRCTRL_DIR_ONLY
) == 0) &&
903 dir_item
->HasFiles(m_currentFilterStr
)) )
905 m_treeCtrl
->SetItemHasChildren(id
);
909 // Add the sorted filenames
910 if ((GetWindowStyle() & wxDIRCTRL_DIR_ONLY
) == 0)
912 for (i
= 0; i
< filenames
.Count(); i
++)
914 eachFilename
= filenames
[i
];
916 if (!wxEndsWithPathSeparator(path
))
917 path
+= wxString(wxFILE_SEP_PATH
);
918 path
+= eachFilename
;
919 //path = dirName + wxString(wxT("/")) + eachFilename;
920 wxDirItemData
*dir_item
= new wxDirItemData(path
,eachFilename
,false);
921 int image_id
= wxFileIconsTable::file
;
922 if (eachFilename
.Find(wxT('.')) != wxNOT_FOUND
)
923 image_id
= wxTheFileIconsTable
->GetIconID(eachFilename
.AfterLast(wxT('.')));
924 (void) AppendItem( parentId
, eachFilename
, image_id
, -1, dir_item
);
929 void wxGenericDirCtrl::ReCreateTree()
931 CollapseDir(m_treeCtrl
->GetRootItem());
935 void wxGenericDirCtrl::CollapseTree()
937 wxTreeItemIdValue cookie
;
938 wxTreeItemId child
= m_treeCtrl
->GetFirstChild(m_rootId
, cookie
);
942 child
= m_treeCtrl
->GetNextChild(m_rootId
, cookie
);
946 // Find the child that matches the first part of 'path'.
947 // E.g. if a child path is "/usr" and 'path' is "/usr/include"
948 // then the child for /usr is returned.
949 wxTreeItemId
wxGenericDirCtrl::FindChild(wxTreeItemId parentId
, const wxString
& path
, bool& done
)
951 wxString
path2(path
);
953 // Make sure all separators are as per the current platform
954 path2
.Replace(wxT("\\"), wxString(wxFILE_SEP_PATH
));
955 path2
.Replace(wxT("/"), wxString(wxFILE_SEP_PATH
));
957 // Append a separator to foil bogus substring matching
958 path2
+= wxString(wxFILE_SEP_PATH
);
960 // In MSW or PM, case is not significant
961 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__OS2__)
965 wxTreeItemIdValue cookie
;
966 wxTreeItemId childId
= m_treeCtrl
->GetFirstChild(parentId
, cookie
);
967 while (childId
.IsOk())
969 wxDirItemData
* data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(childId
);
971 if (data
&& !data
->m_path
.empty())
973 wxString
childPath(data
->m_path
);
974 if (!wxEndsWithPathSeparator(childPath
))
975 childPath
+= wxString(wxFILE_SEP_PATH
);
977 // In MSW and PM, case is not significant
978 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__OS2__)
979 childPath
.MakeLower();
982 if (childPath
.length() <= path2
.length())
984 wxString path3
= path2
.Mid(0, childPath
.length());
985 if (childPath
== path3
)
987 if (path3
.length() == path2
.length())
996 childId
= m_treeCtrl
->GetNextChild(parentId
, cookie
);
998 wxTreeItemId invalid
;
1002 // Try to expand as much of the given path as possible,
1003 // and select the given tree item.
1004 bool wxGenericDirCtrl::ExpandPath(const wxString
& path
)
1007 wxTreeItemId id
= FindChild(m_rootId
, path
, done
);
1008 wxTreeItemId lastId
= id
; // The last non-zero id
1009 while (id
.IsOk() && !done
)
1013 id
= FindChild(id
, path
, done
);
1020 wxDirItemData
*data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(lastId
);
1023 m_treeCtrl
->Expand(lastId
);
1025 if ((GetWindowStyle() & wxDIRCTRL_SELECT_FIRST
) && data
->m_isDir
)
1027 // Find the first file in this directory
1028 wxTreeItemIdValue cookie
;
1029 wxTreeItemId childId
= m_treeCtrl
->GetFirstChild(lastId
, cookie
);
1030 bool selectedChild
= false;
1031 while (childId
.IsOk())
1033 data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(childId
);
1035 if (data
&& data
->m_path
!= wxEmptyString
&& !data
->m_isDir
)
1037 m_treeCtrl
->SelectItem(childId
);
1038 m_treeCtrl
->EnsureVisible(childId
);
1039 selectedChild
= true;
1042 childId
= m_treeCtrl
->GetNextChild(lastId
, cookie
);
1046 m_treeCtrl
->SelectItem(lastId
);
1047 m_treeCtrl
->EnsureVisible(lastId
);
1052 m_treeCtrl
->SelectItem(lastId
);
1053 m_treeCtrl
->EnsureVisible(lastId
);
1060 bool wxGenericDirCtrl::CollapsePath(const wxString
& path
)
1063 wxTreeItemId id
= FindChild(m_rootId
, path
, done
);
1064 wxTreeItemId lastId
= id
; // The last non-zero id
1066 while ( id
.IsOk() && !done
)
1070 id
= FindChild(id
, path
, done
);
1076 if ( !lastId
.IsOk() )
1079 m_treeCtrl
->SelectItem(lastId
);
1080 m_treeCtrl
->EnsureVisible(lastId
);
1086 wxString
wxGenericDirCtrl::GetPath() const
1088 wxTreeItemId id
= m_treeCtrl
->GetSelection();
1091 wxDirItemData
* data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(id
);
1092 return data
->m_path
;
1095 return wxEmptyString
;
1098 wxString
wxGenericDirCtrl::GetFilePath() const
1100 wxTreeItemId id
= m_treeCtrl
->GetSelection();
1103 wxDirItemData
* data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(id
);
1105 return wxEmptyString
;
1107 return data
->m_path
;
1110 return wxEmptyString
;
1113 void wxGenericDirCtrl::SetPath(const wxString
& path
)
1115 m_defaultPath
= path
;
1122 void wxGenericDirCtrl::FindChildFiles(wxTreeItemId id
, int dirFlags
, wxArrayString
& filenames
)
1124 wxDirItemData
*data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(id
);
1126 // This may take a longish time. Go to busy cursor
1131 wxString search
,path
,filename
;
1133 wxString
dirName(data
->m_path
);
1135 #if defined(__WXMSW__) || defined(__OS2__)
1136 if (dirName
.Last() == ':')
1137 dirName
+= wxString(wxFILE_SEP_PATH
);
1141 wxString eachFilename
;
1148 if (d
.GetFirst(& eachFilename
, m_currentFilterStr
, dirFlags
))
1152 if ((eachFilename
!= wxT(".")) && (eachFilename
!= wxT("..")))
1154 filenames
.Add(eachFilename
);
1157 while (d
.GetNext(& eachFilename
)) ;
1163 void wxGenericDirCtrl::SetFilterIndex(int n
)
1165 m_currentFilter
= n
;
1168 if (ExtractWildcard(m_filter
, n
, f
, d
))
1169 m_currentFilterStr
= f
;
1172 m_currentFilterStr
= wxT("*");
1174 m_currentFilterStr
= wxT("*.*");
1178 void wxGenericDirCtrl::SetFilter(const wxString
& filter
)
1183 if (ExtractWildcard(m_filter
, m_currentFilter
, f
, d
))
1184 m_currentFilterStr
= f
;
1187 m_currentFilterStr
= wxT("*");
1189 m_currentFilterStr
= wxT("*.*");
1193 // Extract description and actual filter from overall filter string
1194 bool wxGenericDirCtrl::ExtractWildcard(const wxString
& filterStr
, int n
, wxString
& filter
, wxString
& description
)
1196 wxArrayString filters
, descriptions
;
1197 int count
= wxParseCommonDialogsFilter(filterStr
, descriptions
, filters
);
1198 if (count
> 0 && n
< count
)
1200 filter
= filters
[n
];
1201 description
= descriptions
[n
];
1209 void wxGenericDirCtrl::DoResize()
1211 wxSize sz
= GetClientSize();
1212 int verticalSpacing
= 3;
1216 if (m_filterListCtrl
)
1218 filterSz
= m_filterListCtrl
->GetSize();
1219 sz
.y
-= (filterSz
.y
+ verticalSpacing
);
1221 m_treeCtrl
->SetSize(0, 0, sz
.x
, sz
.y
);
1222 if (m_filterListCtrl
)
1224 m_filterListCtrl
->SetSize(0, sz
.y
+ verticalSpacing
, sz
.x
, filterSz
.y
);
1225 // Don't know why, but this needs refreshing after a resize (wxMSW)
1226 m_filterListCtrl
->Refresh();
1232 void wxGenericDirCtrl::OnSize(wxSizeEvent
& WXUNUSED(event
))
1237 wxTreeItemId
wxGenericDirCtrl::AppendItem (const wxTreeItemId
& parent
,
1238 const wxString
& text
,
1239 int image
, int selectedImage
,
1240 wxTreeItemData
* data
)
1242 wxTreeCtrl
*treeCtrl
= GetTreeCtrl ();
1244 wxASSERT (treeCtrl
);
1248 return treeCtrl
->AppendItem (parent
, text
, image
, selectedImage
, data
);
1252 return wxTreeItemId();
1257 //-----------------------------------------------------------------------------
1258 // wxDirFilterListCtrl
1259 //-----------------------------------------------------------------------------
1261 IMPLEMENT_CLASS(wxDirFilterListCtrl
, wxChoice
)
1263 BEGIN_EVENT_TABLE(wxDirFilterListCtrl
, wxChoice
)
1264 EVT_CHOICE(wxID_ANY
, wxDirFilterListCtrl::OnSelFilter
)
1267 bool wxDirFilterListCtrl::Create(wxGenericDirCtrl
* parent
, const wxWindowID id
,
1273 return wxChoice::Create(parent
, id
, pos
, size
, 0, NULL
, style
);
1276 void wxDirFilterListCtrl::Init()
1281 void wxDirFilterListCtrl::OnSelFilter(wxCommandEvent
& WXUNUSED(event
))
1283 int sel
= GetSelection();
1285 wxString currentPath
= m_dirCtrl
->GetPath();
1287 m_dirCtrl
->SetFilterIndex(sel
);
1289 // If the filter has changed, the view is out of date, so
1290 // collapse the tree.
1291 m_dirCtrl
->ReCreateTree();
1293 // Try to restore the selection, or at least the directory
1294 m_dirCtrl
->ExpandPath(currentPath
);
1297 void wxDirFilterListCtrl::FillFilterList(const wxString
& filter
, int defaultFilter
)
1300 wxArrayString descriptions
, filters
;
1301 size_t n
= (size_t) wxParseCommonDialogsFilter(filter
, descriptions
, filters
);
1303 if (n
> 0 && defaultFilter
< (int) n
)
1305 for (size_t i
= 0; i
< n
; i
++)
1306 Append(descriptions
[i
]);
1307 SetSelection(defaultFilter
);
1310 #endif // wxUSE_DIRDLG
1312 #if wxUSE_DIRDLG || wxUSE_FILEDLG
1314 // ----------------------------------------------------------------------------
1315 // wxFileIconsTable icons
1316 // ----------------------------------------------------------------------------
1319 /* Computer (c) Julian Smart */
1320 static const char * file_icons_tbl_computer_xpm
[] = {
1321 /* columns rows colors chars-per-pixel */
1383 #endif // GTK+ < 2.4
1385 // ----------------------------------------------------------------------------
1386 // wxFileIconsTable & friends
1387 // ----------------------------------------------------------------------------
1389 // global instance of a wxFileIconsTable
1390 wxFileIconsTable
* wxTheFileIconsTable
= (wxFileIconsTable
*)NULL
;
1392 // A module to allow icons table cleanup
1394 class wxFileIconsTableModule
: public wxModule
1396 DECLARE_DYNAMIC_CLASS(wxFileIconsTableModule
)
1398 wxFileIconsTableModule() {}
1399 bool OnInit() { wxTheFileIconsTable
= new wxFileIconsTable
; return true; }
1402 if (wxTheFileIconsTable
)
1404 delete wxTheFileIconsTable
;
1405 wxTheFileIconsTable
= NULL
;
1410 IMPLEMENT_DYNAMIC_CLASS(wxFileIconsTableModule
, wxModule
)
1412 class wxFileIconEntry
: public wxObject
1415 wxFileIconEntry(int i
) { id
= i
; }
1420 wxFileIconsTable::wxFileIconsTable()
1423 m_smallImageList
= NULL
;
1426 wxFileIconsTable::~wxFileIconsTable()
1430 WX_CLEAR_HASH_TABLE(*m_HashTable
);
1433 if (m_smallImageList
) delete m_smallImageList
;
1436 // delayed initialization - wait until first use (wxArtProv not created yet)
1437 void wxFileIconsTable::Create()
1439 wxCHECK_RET(!m_smallImageList
&& !m_HashTable
, wxT("creating icons twice"));
1440 m_HashTable
= new wxHashTable(wxKEY_STRING
);
1441 m_smallImageList
= new wxImageList(16, 16);
1444 m_smallImageList
->Add(wxArtProvider::GetBitmap(wxART_FOLDER
,
1448 m_smallImageList
->Add(wxArtProvider::GetBitmap(wxART_FOLDER_OPEN
,
1453 // GTK24 uses this icon in the file open dialog
1454 m_smallImageList
->Add(wxArtProvider::GetBitmap(wxART_HARDDISK
,
1458 m_smallImageList
->Add(wxIcon(file_icons_tbl_computer_xpm
));
1461 m_smallImageList
->Add(wxArtProvider::GetBitmap(wxART_HARDDISK
,
1465 m_smallImageList
->Add(wxArtProvider::GetBitmap(wxART_CDROM
,
1469 m_smallImageList
->Add(wxArtProvider::GetBitmap(wxART_FLOPPY
,
1473 m_smallImageList
->Add(wxArtProvider::GetBitmap(wxART_REMOVABLE
,
1477 m_smallImageList
->Add(wxArtProvider::GetBitmap(wxART_NORMAL_FILE
,
1481 if (GetIconID(wxEmptyString
, _T("application/x-executable")) == file
)
1483 m_smallImageList
->Add(wxArtProvider::GetBitmap(wxART_EXECUTABLE_FILE
,
1486 delete m_HashTable
->Get(_T("exe"));
1487 m_HashTable
->Delete(_T("exe"));
1488 m_HashTable
->Put(_T("exe"), new wxFileIconEntry(executable
));
1490 /* else put into list by GetIconID
1491 (KDE defines application/x-executable for *.exe and has nice icon)
1495 wxImageList
*wxFileIconsTable::GetSmallImageList()
1497 if (!m_smallImageList
)
1500 return m_smallImageList
;
1503 #if wxUSE_MIMETYPE && wxUSE_IMAGE && (!defined(__WXMSW__) || wxUSE_WXDIB)
1504 // VS: we don't need this function w/o wxMimeTypesManager because we'll only have
1505 // one icon and we won't resize it
1507 static wxBitmap
CreateAntialiasedBitmap(const wxImage
& img
)
1509 const unsigned int size
= 16;
1511 wxImage
smallimg (size
, size
);
1512 unsigned char *p1
, *p2
, *ps
;
1513 unsigned char mr
= img
.GetMaskRed(),
1514 mg
= img
.GetMaskGreen(),
1515 mb
= img
.GetMaskBlue();
1518 unsigned sr
, sg
, sb
, smask
;
1520 p1
= img
.GetData(), p2
= img
.GetData() + 3 * size
*2, ps
= smallimg
.GetData();
1521 smallimg
.SetMaskColour(mr
, mr
, mr
);
1523 for (y
= 0; y
< size
; y
++)
1525 for (x
= 0; x
< size
; x
++)
1527 sr
= sg
= sb
= smask
= 0;
1528 if (p1
[0] != mr
|| p1
[1] != mg
|| p1
[2] != mb
)
1529 sr
+= p1
[0], sg
+= p1
[1], sb
+= p1
[2];
1532 if (p1
[0] != mr
|| p1
[1] != mg
|| p1
[2] != mb
)
1533 sr
+= p1
[0], sg
+= p1
[1], sb
+= p1
[2];
1536 if (p2
[0] != mr
|| p2
[1] != mg
|| p2
[2] != mb
)
1537 sr
+= p2
[0], sg
+= p2
[1], sb
+= p2
[2];
1540 if (p2
[0] != mr
|| p2
[1] != mg
|| p2
[2] != mb
)
1541 sr
+= p2
[0], sg
+= p2
[1], sb
+= p2
[2];
1546 ps
[0] = ps
[1] = ps
[2] = mr
;
1549 ps
[0] = (unsigned char)(sr
>> 2);
1550 ps
[1] = (unsigned char)(sg
>> 2);
1551 ps
[2] = (unsigned char)(sb
>> 2);
1555 p1
+= size
*2 * 3, p2
+= size
*2 * 3;
1558 return wxBitmap(smallimg
);
1561 // This function is currently not unused anymore
1563 // finds empty borders and return non-empty area of image:
1564 static wxImage
CutEmptyBorders(const wxImage
& img
)
1566 unsigned char mr
= img
.GetMaskRed(),
1567 mg
= img
.GetMaskGreen(),
1568 mb
= img
.GetMaskBlue();
1569 unsigned char *dt
= img
.GetData(), *dttmp
;
1570 unsigned w
= img
.GetWidth(), h
= img
.GetHeight();
1572 unsigned top
, bottom
, left
, right
, i
;
1575 #define MK_DTTMP(x,y) dttmp = dt + ((x + y * w) * 3)
1576 #define NOEMPTY_PIX(empt) if (dttmp[0] != mr || dttmp[1] != mg || dttmp[2] != mb) {empt = false; break;}
1578 for (empt
= true, top
= 0; empt
&& top
< h
; top
++)
1581 for (i
= 0; i
< w
; i
++, dttmp
+=3)
1584 for (empt
= true, bottom
= h
-1; empt
&& bottom
> top
; bottom
--)
1586 MK_DTTMP(0, bottom
);
1587 for (i
= 0; i
< w
; i
++, dttmp
+=3)
1590 for (empt
= true, left
= 0; empt
&& left
< w
; left
++)
1593 for (i
= 0; i
< h
; i
++, dttmp
+=3*w
)
1596 for (empt
= true, right
= w
-1; empt
&& right
> left
; right
--)
1599 for (i
= 0; i
< h
; i
++, dttmp
+=3*w
)
1602 top
--, left
--, bottom
++, right
++;
1604 return img
.GetSubImage(wxRect(left
, top
, right
- left
+ 1, bottom
- top
+ 1));
1608 #endif // wxUSE_MIMETYPE
1610 int wxFileIconsTable::GetIconID(const wxString
& extension
, const wxString
& mime
)
1612 if (!m_smallImageList
)
1616 if (!extension
.empty())
1618 wxFileIconEntry
*entry
= (wxFileIconEntry
*) m_HashTable
->Get(extension
);
1619 if (entry
) return (entry
-> id
);
1622 wxFileType
*ft
= (mime
.empty()) ?
1623 wxTheMimeTypesManager
-> GetFileTypeFromExtension(extension
) :
1624 wxTheMimeTypesManager
-> GetFileTypeFromMimeType(mime
);
1626 wxIconLocation iconLoc
;
1631 if ( ft
&& ft
->GetIcon(&iconLoc
) )
1633 ic
= wxIcon( iconLoc
);
1642 m_HashTable
->Put(extension
, new wxFileIconEntry(newid
));
1647 bmp
.CopyFromIcon(ic
);
1652 m_HashTable
->Put(extension
, new wxFileIconEntry(newid
));
1656 const unsigned int size
= 16;
1658 int id
= m_smallImageList
->GetImageCount();
1659 if ((bmp
.GetWidth() == (int) size
) && (bmp
.GetHeight() == (int) size
))
1661 m_smallImageList
->Add(bmp
);
1663 #if wxUSE_IMAGE && (!defined(__WXMSW__) || wxUSE_WXDIB)
1666 wxImage img
= bmp
.ConvertToImage();
1668 if ((img
.GetWidth() != size
*2) || (img
.GetHeight() != size
*2))
1669 // m_smallImageList->Add(CreateAntialiasedBitmap(CutEmptyBorders(img).Rescale(size*2, size*2)));
1670 m_smallImageList
->Add(CreateAntialiasedBitmap(img
.Rescale(size
*2, size
*2)));
1672 m_smallImageList
->Add(CreateAntialiasedBitmap(img
));
1674 #endif // wxUSE_IMAGE
1676 m_HashTable
->Put(extension
, new wxFileIconEntry(id
));
1679 #else // !wxUSE_MIMETYPE
1682 if (extension
== wxT("exe"))
1686 #endif // wxUSE_MIMETYPE/!wxUSE_MIMETYPE
1689 #endif // wxUSE_DIRDLG || wxUSE_FILEDLG