1 /////////////////////////////////////////////////////////////////////////////
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 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "dirctrlg.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
26 #include "wx/dialog.h"
27 #include "wx/button.h"
28 #include "wx/layout.h"
29 #include "wx/msgdlg.h"
30 #include "wx/textctrl.h"
31 #include "wx/textdlg.h"
32 #include "wx/filefn.h"
33 #include "wx/cmndata.h"
34 #include "wx/gdicmn.h"
36 #include "wx/imaglist.h"
40 #include "wx/tokenzr.h"
42 #include "wx/settings.h"
45 #include "wx/statline.h"
48 #include "wx/generic/dirctrlg.h"
50 #if defined(__WXMAC__)
51 #include "wx/mac/private.h" // includes mac headers
57 // FIXME - Mingw32 1.0 has both _getdrive() and _chdrive(). For now, let's assume
58 // older releases don't, but it should be verified and the checks modified
60 #if !defined(__GNUWIN32__) || \
61 (defined(__MINGW32_MAJOR_VERSION) && __MINGW32_MAJOR_VERSION >= 1)
79 #if defined(__WXMAC__)
80 # include "MoreFilesExtras.h"
87 // If compiled under Windows, this macro can cause problems
93 static const char * icon1_xpm
[] = {
94 /* width height ncolors chars_per_pixel */
122 static const char * icon2_xpm
[] = {
123 /* width height ncolors chars_per_pixel */
151 static const char * icon3_xpm
[] = {
152 /* width height ncolors chars_per_pixel */
177 static const char * icon4_xpm
[] = {
204 static const char * icon5_xpm
[] = {
231 static const char *icon6_xpm
[] = {
261 static const char * icon7_xpm
[] = {
288 static const char * icon8_xpm
[] = {
315 #define wxID_TREECTRL 7000
316 #define wxID_FILTERLISTCTRL 7001
320 bool wxIsDriveAvailable(const wxString
& dirName
)
322 // FIXME_MGL - this method leads to hang up under Watcom for some reason
324 if ( dirName
.Len() == 3 && dirName
[1u] == wxT(':') )
326 wxString
dirNameLower(dirName
.Lower());
327 // VS: always return TRUE for removable media, since Win95 doesn't
328 // like it when MS-DOS app accesses empty floppy drive
329 return (dirNameLower
[0u] == wxT('a') ||
330 dirNameLower
[0u] == wxT('b') ||
331 wxPathExists(dirNameLower
));
338 #elif defined(__WINDOWS__) || defined(__WXPM__)
340 int setdrive(int drive
)
342 #if defined(__GNUWIN32__) && \
343 (defined(__MINGW32_MAJOR_VERSION) && __MINGW32_MAJOR_VERSION >= 1)
344 return _chdrive(drive
);
348 if (drive
< 1 || drive
> 31)
350 newdrive
[0] = (wxChar
)(wxT('A') + drive
- 1);
351 newdrive
[1] = wxT(':');
352 newdrive
[2] = wxT('\0');
353 #if defined(__WXMSW__)
355 if (wxSetWorkingDirectory(newdrive
))
357 if (::SetCurrentDirectory(newdrive
))
360 // VA doesn't know what LPSTR is and has its own set
361 if (DosSetCurrentDir((PSZ
)newdrive
))
369 bool wxIsDriveAvailable(const wxString
& dirName
)
372 UINT errorMode
= SetErrorMode(SEM_FAILCRITICALERRORS
| SEM_NOOPENFILEERRORBOX
);
376 // Check if this is a root directory and if so,
377 // whether the drive is avaiable.
378 if (dirName
.Len() == 3 && dirName
[(size_t)1] == wxT(':'))
380 wxString
dirNameLower(dirName
.Lower());
381 #if defined(__GNUWIN32__) && \
382 !(defined(__MINGW32_MAJOR_VERSION) && __MINGW32_MAJOR_VERSION >= 1)
383 success
= wxPathExists(dirNameLower
);
385 int currentDrive
= _getdrive();
386 int thisDrive
= (int) (dirNameLower
[(size_t)0] - 'a' + 1) ;
387 int err
= setdrive( thisDrive
) ;
388 setdrive( currentDrive
);
397 (void) SetErrorMode(errorMode
);
402 #endif // __WINDOWS__ || __WXPM__
404 // Function which is called by quick sort. We want to override the default wxArrayString behaviour,
405 // and sort regardless of case.
406 static int LINKAGEMODE
wxDirCtrlStringCompareFunction(const void *first
, const void *second
)
408 wxString
*strFirst
= (wxString
*)first
;
409 wxString
*strSecond
= (wxString
*)second
;
411 return strFirst
->CmpNoCase(*strSecond
);
414 //-----------------------------------------------------------------------------
416 //-----------------------------------------------------------------------------
418 wxDirItemData::wxDirItemData(const wxString
& path
, const wxString
& name
,
423 /* Insert logic to detect hidden files here
424 * In UnixLand we just check whether the first char is a dot
425 * For FileNameFromPath read LastDirNameInThisPath ;-) */
426 // m_isHidden = (bool)(wxFileNameFromPath(*m_path)[0] == '.');
428 m_isExpanded
= FALSE
;
432 wxDirItemData::~wxDirItemData()
436 void wxDirItemData::SetNewDirName(const wxString
& path
)
439 m_name
= wxFileNameFromPath(path
);
442 bool wxDirItemData::HasSubDirs() const
444 if (m_path
.IsEmpty())
450 if ( !dir
.Open(m_path
) )
454 return dir
.HasSubDirs();
457 bool wxDirItemData::HasFiles(const wxString
& spec
) const
459 if (m_path
.IsEmpty())
465 if ( !dir
.Open(m_path
) )
469 return dir
.HasFiles();
472 //-----------------------------------------------------------------------------
474 //-----------------------------------------------------------------------------
476 IMPLEMENT_DYNAMIC_CLASS(wxGenericDirCtrl
, wxControl
)
478 BEGIN_EVENT_TABLE(wxGenericDirCtrl
, wxControl
)
479 EVT_TREE_ITEM_EXPANDING (-1, wxGenericDirCtrl::OnExpandItem
)
480 EVT_TREE_ITEM_COLLAPSED (-1, wxGenericDirCtrl::OnCollapseItem
)
481 EVT_TREE_BEGIN_LABEL_EDIT (-1, wxGenericDirCtrl::OnBeginEditItem
)
482 EVT_TREE_END_LABEL_EDIT (-1, wxGenericDirCtrl::OnEndEditItem
)
483 EVT_SIZE (wxGenericDirCtrl::OnSize
)
486 wxGenericDirCtrl::wxGenericDirCtrl(void)
491 bool wxGenericDirCtrl::Create(wxWindow
*parent
,
497 const wxString
& filter
,
499 const wxString
& name
)
501 if (!wxControl::Create(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
504 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));
508 long treeStyle
= wxTR_HAS_BUTTONS
| wxTR_EDIT_LABELS
| wxTR_HIDE_ROOT
;
509 if ((style
& wxDIRCTRL_3D_INTERNAL
) == 0)
510 treeStyle
|= wxNO_BORDER
;
512 long filterStyle
= 0;
513 if ((style
& wxDIRCTRL_3D_INTERNAL
) == 0)
514 filterStyle
|= wxNO_BORDER
;
516 m_treeCtrl
= new wxTreeCtrl(this, wxID_TREECTRL
, pos
, size
, treeStyle
);
518 if (!filter
.IsEmpty() && (style
& wxDIRCTRL_SHOW_FILTERS
))
519 m_filterListCtrl
= new wxDirFilterListCtrl(this, wxID_FILTERLISTCTRL
, wxDefaultPosition
, wxDefaultSize
, filterStyle
);
524 SetFilterIndex(defaultFilter
);
526 if (m_filterListCtrl
)
527 m_filterListCtrl
->FillFilterList(filter
, defaultFilter
);
529 m_imageList
= new wxImageList(16, 16, TRUE
);
530 m_imageList
->Add(wxIcon(icon1_xpm
));
531 m_imageList
->Add(wxIcon(icon2_xpm
));
532 m_imageList
->Add(wxIcon(icon3_xpm
));
533 m_imageList
->Add(wxIcon(icon4_xpm
));
534 m_imageList
->Add(wxIcon(icon5_xpm
));
535 m_imageList
->Add(wxIcon(icon6_xpm
));
536 m_imageList
->Add(wxIcon(icon7_xpm
));
537 m_imageList
->Add(wxIcon(icon8_xpm
));
538 m_treeCtrl
->AssignImageList(m_imageList
);
540 m_showHidden
= FALSE
;
541 wxDirItemData
* rootData
= new wxDirItemData(wxT(""), wxT(""), TRUE
);
545 #if defined(__WINDOWS__) || defined(__WXPM__) || defined(__DOS__)
546 rootName
= _("Computer");
548 rootName
= _("Sections");
551 m_rootId
= m_treeCtrl
->AddRoot( rootName
, 3, -1, rootData
);
552 m_treeCtrl
->SetItemHasChildren(m_rootId
);
553 m_treeCtrl
->Expand(m_rootId
); // automatically expand first level
555 // Expand and select the default path
556 if (!m_defaultPath
.IsEmpty())
557 ExpandPath(m_defaultPath
);
564 wxGenericDirCtrl::~wxGenericDirCtrl()
568 void wxGenericDirCtrl::Init()
570 m_showHidden
= FALSE
;
573 m_currentFilterStr
= wxEmptyString
; // Default: any file
575 m_filterListCtrl
= NULL
;
578 void wxGenericDirCtrl::ShowHidden( bool show
)
585 void wxGenericDirCtrl::AddSection(const wxString
& path
, const wxString
& name
, int imageId
)
587 wxDirItemData
*dir_item
= new wxDirItemData(path
,name
,TRUE
);
589 wxTreeItemId id
= m_treeCtrl
->AppendItem( m_rootId
, name
, imageId
, -1, dir_item
);
591 m_treeCtrl
->SetItemHasChildren(id
);
594 void wxGenericDirCtrl::SetupSections()
596 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXPM__)
599 wxChar driveBuffer
[256];
600 size_t n
= (size_t) GetLogicalDriveStrings(255, driveBuffer
);
605 path
.Printf(wxT("%c:\\"), driveBuffer
[i
]);
606 name
.Printf(wxT("(%c:)"), driveBuffer
[i
]);
609 int driveType
= ::GetDriveType(path
);
612 case DRIVE_REMOVABLE
:
613 if (path
== wxT("a:\\") || path
== wxT("b:\\"))
614 imageId
= 6; // Floppy
632 AddSection(path
, name
, imageId
);
634 while (driveBuffer
[i
] != wxT('\0'))
637 if (driveBuffer
[i
] == wxT('\0'))
643 /* If we can switch to the drive, it exists. */
644 for( drive
= 1; drive
<= 26; drive
++ )
647 path
.Printf(wxT("%c:\\"), (char) (drive
+ 'a' - 1));
648 name
.Printf(wxT("(%c:)"), (char) (drive
+ 'A' - 1));
650 if (wxIsDriveAvailable(path
))
652 AddSection(path
, name
, (drive
<= 2) ? 6/*floppy*/ : 4/*disk*/);
655 #endif // __WIN32__/!__WIN32__
657 #elif defined(__WXMAC__)
661 short actualCount
= 0 ;
662 if ( OnLine( &volume
, 1 , &actualCount
, &index
) != noErr
|| actualCount
== 0 )
665 wxString name
= wxMacFSSpec2MacFilename( &volume
) ;
666 AddSection(name
+ wxFILE_SEP_PATH
, name
, 0);
668 #elif defined(__UNIX__)
669 AddSection(wxT("/"), wxT("/"), 3/*computer icon*/);
671 #error "Unsupported platform in wxGenericDirCtrl!"
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
->GetParent( event
.GetItem() ) == m_rootId
)
692 void wxGenericDirCtrl::OnEndEditItem(wxTreeEvent
&event
)
694 if ((event
.GetLabel().IsEmpty()) ||
695 (event
.GetLabel() == _(".")) ||
696 (event
.GetLabel() == _("..")) ||
697 (event
.GetLabel().First( wxT("/") ) != wxNOT_FOUND
))
699 wxMessageDialog
dialog(this, _("Illegal directory name."), _("Error"), wxOK
| wxICON_ERROR
);
705 wxTreeItemId id
= event
.GetItem();
706 wxDirItemData
*data
= (wxDirItemData
*)m_treeCtrl
->GetItemData( id
);
709 wxString
new_name( wxPathOnly( data
->m_path
) );
710 new_name
+= wxString(wxFILE_SEP_PATH
);
711 new_name
+= event
.GetLabel();
715 if (wxFileExists(new_name
))
717 wxMessageDialog
dialog(this, _("File name exists already."), _("Error"), wxOK
| wxICON_ERROR
);
722 if (wxRenameFile(data
->m_path
,new_name
))
724 data
->SetNewDirName( new_name
);
728 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
734 void wxGenericDirCtrl::OnExpandItem(wxTreeEvent
&event
)
736 wxTreeItemId parentId
= event
.GetItem();
738 // VS: this is needed because the event handler is called from wxTreeCtrl
739 // ctor when wxTR_HIDE_ROOT was specified
741 m_rootId
= m_treeCtrl
->GetRootItem();
746 void wxGenericDirCtrl::OnCollapseItem(wxTreeEvent
&event
)
748 wxTreeItemId child
, parent
= event
.GetItem();
750 wxDirItemData
*data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(event
.GetItem());
751 if (!data
->m_isExpanded
)
754 data
->m_isExpanded
= FALSE
;
756 /* Workaround because DeleteChildren has disapeared (why?) and
757 * CollapseAndReset doesn't work as advertised (deletes parent too) */
758 child
= m_treeCtrl
->GetFirstChild(parent
, cookie
);
761 m_treeCtrl
->Delete(child
);
762 /* Not GetNextChild below, because the cookie mechanism can't
763 * handle disappearing children! */
764 child
= m_treeCtrl
->GetFirstChild(parent
, cookie
);
768 void wxGenericDirCtrl::ExpandDir(wxTreeItemId parentId
)
770 wxDirItemData
*data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(parentId
);
772 if (data
->m_isExpanded
)
775 data
->m_isExpanded
= TRUE
;
777 if (parentId
== m_treeCtrl
->GetRootItem())
785 wxString search
,path
,filename
;
787 wxString
dirName(data
->m_path
);
789 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXPM__)
790 // Check if this is a root directory and if so,
791 // whether the drive is avaiable.
792 if (!wxIsDriveAvailable(dirName
))
794 data
->m_isExpanded
= FALSE
;
795 //wxMessageBox(wxT("Sorry, this drive is not available."));
800 // This may take a longish time. Go to busy cursor
803 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXPM__)
804 if (dirName
.Last() == ':')
805 dirName
+= wxString(wxFILE_SEP_PATH
);
809 wxArrayString filenames
;
812 wxString eachFilename
;
819 int style
= wxDIR_DIRS
;
820 if (m_showHidden
) style
|= wxDIR_HIDDEN
;
821 if (d
.GetFirst(& eachFilename
, wxEmptyString
, style
))
825 if ((eachFilename
!= wxT(".")) && (eachFilename
!= wxT("..")))
827 dirs
.Add(eachFilename
);
830 while (d
.GetNext(& eachFilename
));
833 dirs
.Sort((wxArrayString::CompareFunction
) wxDirCtrlStringCompareFunction
);
835 // Now do the filenames -- but only if we're allowed to
836 if ((GetWindowStyle() & wxDIRCTRL_DIR_ONLY
) == 0)
844 if (d
.GetFirst(& eachFilename
, m_currentFilterStr
, wxDIR_FILES
))
848 if ((eachFilename
!= wxT(".")) && (eachFilename
!= wxT("..")))
850 filenames
.Add(eachFilename
);
853 while (d
.GetNext(& eachFilename
));
856 filenames
.Sort((wxArrayString::CompareFunction
) wxDirCtrlStringCompareFunction
);
859 // Add the sorted dirs
861 for (i
= 0; i
< dirs
.Count(); i
++)
863 wxString
eachFilename(dirs
[i
]);
865 if (path
.Last() != wxFILE_SEP_PATH
)
866 path
+= wxString(wxFILE_SEP_PATH
);
867 path
+= eachFilename
;
869 wxDirItemData
*dir_item
= new wxDirItemData(path
,eachFilename
,TRUE
);
870 wxTreeItemId id
= m_treeCtrl
->AppendItem( parentId
, eachFilename
, 0, -1, dir_item
);
871 m_treeCtrl
->SetItemImage( id
, 1, wxTreeItemIcon_Expanded
);
873 // Has this got any children? If so, make it expandable.
874 // (There are two situations when a dir has children: either it
875 // has subdirectories or it contains files that weren't filtered
876 // out. The latter only applies to dirctrl with files.)
877 if ( dir_item
->HasSubDirs() ||
878 (((GetWindowStyle() & wxDIRCTRL_DIR_ONLY
) == 0) &&
879 dir_item
->HasFiles(m_currentFilterStr
)) )
881 m_treeCtrl
->SetItemHasChildren(id
);
885 // Add the sorted filenames
886 if ((GetWindowStyle() & wxDIRCTRL_DIR_ONLY
) == 0)
888 for (i
= 0; i
< filenames
.Count(); i
++)
890 wxString
eachFilename(filenames
[i
]);
892 if (path
.Last() != wxFILE_SEP_PATH
)
893 path
+= wxString(wxFILE_SEP_PATH
);
894 path
+= eachFilename
;
895 //path = dirName + wxString(wxT("/")) + eachFilename;
896 wxDirItemData
*dir_item
= new wxDirItemData(path
,eachFilename
,FALSE
);
897 (void)m_treeCtrl
->AppendItem( parentId
, eachFilename
, 2, -1, dir_item
);
902 // Find the child that matches the first part of 'path'.
903 // E.g. if a child path is "/usr" and 'path' is "/usr/include"
904 // then the child for /usr is returned.
905 wxTreeItemId
wxGenericDirCtrl::FindChild(wxTreeItemId parentId
, const wxString
& path
, bool& done
)
907 wxString
path2(path
);
909 // Make sure all separators are as per the current platform
910 path2
.Replace(wxT("\\"), wxString(wxFILE_SEP_PATH
));
911 path2
.Replace(wxT("/"), wxString(wxFILE_SEP_PATH
));
913 // Append a separator to foil bogus substring matching
914 path2
+= wxString(wxFILE_SEP_PATH
);
916 // In MSW or PM, case is not significant
917 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXPM__)
922 wxTreeItemId childId
= m_treeCtrl
->GetFirstChild(parentId
, cookie
);
923 while (childId
.IsOk())
925 wxDirItemData
* data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(childId
);
927 if (data
&& !data
->m_path
.IsEmpty())
929 wxString
childPath(data
->m_path
);
930 if (childPath
.Last() != wxFILE_SEP_PATH
)
931 childPath
+= wxString(wxFILE_SEP_PATH
);
933 // In MSW and PM, case is not significant
934 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXPM__)
935 childPath
.MakeLower();
938 if (childPath
.Len() <= path2
.Len())
940 wxString path3
= path2
.Mid(0, childPath
.Len());
941 if (childPath
== path3
)
943 if (path3
.Len() == path2
.Len())
952 childId
= m_treeCtrl
->GetNextChild(parentId
, cookie
);
954 wxTreeItemId invalid
;
958 // Try to expand as much of the given path as possible,
959 // and select the given tree item.
960 bool wxGenericDirCtrl::ExpandPath(const wxString
& path
)
963 wxTreeItemId id
= FindChild(m_rootId
, path
, done
);
964 wxTreeItemId lastId
= id
; // The last non-zero id
965 while (id
.IsOk() && !done
)
969 id
= FindChild(id
, path
, done
);
975 wxDirItemData
*data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(lastId
);
978 m_treeCtrl
->Expand(lastId
);
980 if ((GetWindowStyle() & wxDIRCTRL_SELECT_FIRST
) && data
->m_isDir
)
982 // Find the first file in this directory
984 wxTreeItemId childId
= m_treeCtrl
->GetFirstChild(lastId
, cookie
);
985 bool selectedChild
= FALSE
;
986 while (childId
.IsOk())
988 wxDirItemData
* data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(childId
);
990 if (data
&& data
->m_path
!= "" && !data
->m_isDir
)
992 m_treeCtrl
->SelectItem(childId
);
993 m_treeCtrl
->EnsureVisible(childId
);
994 selectedChild
= TRUE
;
997 childId
= m_treeCtrl
->GetNextChild(lastId
, cookie
);
1001 m_treeCtrl
->SelectItem(lastId
);
1002 m_treeCtrl
->EnsureVisible(lastId
);
1007 m_treeCtrl
->SelectItem(lastId
);
1008 m_treeCtrl
->EnsureVisible(lastId
);
1017 wxString
wxGenericDirCtrl::GetPath() const
1019 wxTreeItemId id
= m_treeCtrl
->GetSelection();
1022 wxDirItemData
* data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(id
);
1023 return data
->m_path
;
1026 return wxEmptyString
;
1029 wxString
wxGenericDirCtrl::GetFilePath() const
1031 wxTreeItemId id
= m_treeCtrl
->GetSelection();
1034 wxDirItemData
* data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(id
);
1036 return wxEmptyString
;
1038 return data
->m_path
;
1041 return wxEmptyString
;
1044 void wxGenericDirCtrl::SetPath(const wxString
& path
)
1046 m_defaultPath
= path
;
1053 void wxGenericDirCtrl::FindChildFiles(wxTreeItemId id
, int dirFlags
, wxArrayString
& filenames
)
1055 wxDirItemData
*data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(id
);
1057 // This may take a longish time. Go to busy cursor
1062 wxString search
,path
,filename
;
1064 wxString
dirName(data
->m_path
);
1066 #if defined(__WXMSW__) || defined(__WXPM__)
1067 if (dirName
.Last() == ':')
1068 dirName
+= wxString(wxFILE_SEP_PATH
);
1072 wxString eachFilename
;
1079 if (d
.GetFirst(& eachFilename
, m_currentFilterStr
, dirFlags
))
1083 if ((eachFilename
!= wxT(".")) && (eachFilename
!= wxT("..")))
1085 filenames
.Add(eachFilename
);
1088 while (d
.GetNext(& eachFilename
)) ;
1094 void wxGenericDirCtrl::SetFilterIndex(int n
)
1096 m_currentFilter
= n
;
1099 if (ExtractWildcard(m_filter
, n
, f
, d
))
1100 m_currentFilterStr
= f
;
1102 m_currentFilterStr
= wxT("*.*");
1105 void wxGenericDirCtrl::SetFilter(const wxString
& filter
)
1110 if (ExtractWildcard(m_filter
, m_currentFilter
, f
, d
))
1111 m_currentFilterStr
= f
;
1113 m_currentFilterStr
= wxT("*.*");
1116 // Extract description and actual filter from overall filter string
1117 bool wxGenericDirCtrl::ExtractWildcard(const wxString
& filterStr
, int n
, wxString
& filter
, wxString
& description
)
1119 wxArrayString filters
, descriptions
;
1120 int count
= ParseFilter(filterStr
, filters
, descriptions
);
1121 if (count
> 0 && n
< count
)
1123 filter
= filters
[n
];
1124 description
= descriptions
[n
];
1131 // Parses the global filter, returning the number of filters.
1132 // Returns 0 if none or if there's a problem.
1133 // filterStr is in the form:
1135 // "All files (*.*)|*.*|JPEG Files (*.jpeg)|*.jpg"
1137 int wxGenericDirCtrl::ParseFilter(const wxString
& filterStr
, wxArrayString
& filters
, wxArrayString
& descriptions
)
1139 wxString
str(filterStr
);
1141 wxString description
, filter
;
1143 bool finished
= FALSE
;
1146 pos
= str
.Find(wxT('|'));
1148 return 0; // Problem
1149 description
= str
.Left(pos
);
1150 str
= str
.Mid(pos
+1);
1151 pos
= str
.Find(wxT('|'));
1159 filter
= str
.Left(pos
);
1160 str
= str
.Mid(pos
+1);
1162 descriptions
.Add(description
);
1163 filters
.Add(filter
);
1167 return filters
.Count();
1170 void wxGenericDirCtrl::DoResize()
1172 wxSize sz
= GetClientSize();
1173 int verticalSpacing
= 3;
1177 if (m_filterListCtrl
)
1179 filterSz
= m_filterListCtrl
->GetSize();
1180 sz
.y
-= (filterSz
.y
+ verticalSpacing
);
1182 m_treeCtrl
->SetSize(0, 0, sz
.x
, sz
.y
);
1183 if (m_filterListCtrl
)
1185 m_filterListCtrl
->SetSize(0, sz
.y
+ verticalSpacing
, sz
.x
, filterSz
.y
);
1186 // Don't know why, but this needs refreshing after a resize (wxMSW)
1187 m_filterListCtrl
->Refresh();
1193 void wxGenericDirCtrl::OnSize(wxSizeEvent
& WXUNUSED(event
))
1198 //-----------------------------------------------------------------------------
1199 // wxDirFilterListCtrl
1200 //-----------------------------------------------------------------------------
1202 IMPLEMENT_CLASS(wxDirFilterListCtrl
, wxChoice
)
1204 BEGIN_EVENT_TABLE(wxDirFilterListCtrl
, wxChoice
)
1205 EVT_CHOICE(-1, wxDirFilterListCtrl::OnSelFilter
)
1208 bool wxDirFilterListCtrl::Create(wxGenericDirCtrl
* parent
, const wxWindowID id
,
1214 return wxChoice::Create(parent
, id
, pos
, size
, 0, NULL
, style
);
1217 void wxDirFilterListCtrl::Init()
1222 void wxDirFilterListCtrl::OnSelFilter(wxCommandEvent
& WXUNUSED(event
))
1224 int sel
= GetSelection();
1226 wxString currentPath
= m_dirCtrl
->GetPath();
1228 m_dirCtrl
->SetFilterIndex(sel
);
1230 // If the filter has changed, the view is out of date, so
1231 // collapse the tree.
1232 m_dirCtrl
->GetTreeCtrl()->Collapse(m_dirCtrl
->GetRootId());
1233 m_dirCtrl
->GetTreeCtrl()->Expand(m_dirCtrl
->GetRootId());
1235 // Try to restore the selection, or at least the directory
1236 m_dirCtrl
->ExpandPath(currentPath
);
1239 void wxDirFilterListCtrl::FillFilterList(const wxString
& filter
, int defaultFilter
)
1242 wxArrayString descriptions
, filters
;
1243 size_t n
= (size_t) m_dirCtrl
->ParseFilter(filter
, filters
, descriptions
);
1245 if (n
> 0 && defaultFilter
< (int) n
)
1248 for (i
= 0; i
< n
; i
++)
1249 Append(descriptions
[i
]);
1250 SetSelection(defaultFilter
);
1254 #endif // wxUSE_DIRDLG