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 if ( dirName
.Len() == 3 && dirName
[1u] == wxT(':') )
324 wxString
dirNameLower(dirName
.Lower());
325 // VS: always return TRUE for removable media, since Win95 doesn't
326 // like it when MS-DOS app accesses empty floppy drive
327 return (dirNameLower
[0u] == wxT('a') ||
328 dirNameLower
[0u] == wxT('b') ||
329 wxPathExists(dirNameLower
));
335 #elif defined(__WINDOWS__) || defined(__WXPM__)
337 int setdrive(int drive
)
339 #if defined(__GNUWIN32__) && \
340 (defined(__MINGW32_MAJOR_VERSION) && __MINGW32_MAJOR_VERSION >= 1)
341 return _chdrive(drive
);
345 if (drive
< 1 || drive
> 31)
347 newdrive
[0] = (wxChar
)(wxT('A') + drive
- 1);
348 newdrive
[1] = wxT(':');
349 newdrive
[2] = wxT('\0');
350 #if defined(__WXMSW__)
352 if (wxSetWorkingDirectory(newdrive
))
354 if (::SetCurrentDirectory(newdrive
))
357 // VA doesn't know what LPSTR is and has its own set
358 if (DosSetCurrentDir((PSZ
)newdrive
))
366 bool wxIsDriveAvailable(const wxString
& dirName
)
369 UINT errorMode
= SetErrorMode(SEM_FAILCRITICALERRORS
| SEM_NOOPENFILEERRORBOX
);
373 // Check if this is a root directory and if so,
374 // whether the drive is avaiable.
375 if (dirName
.Len() == 3 && dirName
[(size_t)1] == wxT(':'))
377 wxString
dirNameLower(dirName
.Lower());
378 #if defined(__GNUWIN32__) && \
379 !(defined(__MINGW32_MAJOR_VERSION) && __MINGW32_MAJOR_VERSION >= 1)
380 success
= wxPathExists(dirNameLower
);
382 int currentDrive
= _getdrive();
383 int thisDrive
= (int) (dirNameLower
[(size_t)0] - 'a' + 1) ;
384 int err
= setdrive( thisDrive
) ;
385 setdrive( currentDrive
);
394 (void) SetErrorMode(errorMode
);
399 #endif // __WINDOWS__ || __WXPM__
401 // Function which is called by quick sort. We want to override the default wxArrayString behaviour,
402 // and sort regardless of case.
403 static int LINKAGEMODE
wxDirCtrlStringCompareFunction(const void *first
, const void *second
)
405 wxString
*strFirst
= (wxString
*)first
;
406 wxString
*strSecond
= (wxString
*)second
;
408 return strFirst
->CmpNoCase(*strSecond
);
411 //-----------------------------------------------------------------------------
413 //-----------------------------------------------------------------------------
415 wxDirItemData::wxDirItemData(const wxString
& path
, const wxString
& name
,
420 /* Insert logic to detect hidden files here
421 * In UnixLand we just check whether the first char is a dot
422 * For FileNameFromPath read LastDirNameInThisPath ;-) */
423 // m_isHidden = (bool)(wxFileNameFromPath(*m_path)[0] == '.');
425 m_isExpanded
= FALSE
;
429 wxDirItemData::~wxDirItemData()
433 void wxDirItemData::SetNewDirName(const wxString
& path
)
436 m_name
= wxFileNameFromPath(path
);
439 bool wxDirItemData::HasSubDirs() const
441 if (m_path
.IsEmpty())
447 if ( !dir
.Open(m_path
) )
451 return dir
.HasSubDirs();
454 bool wxDirItemData::HasFiles(const wxString
& spec
) const
456 if (m_path
.IsEmpty())
462 if ( !dir
.Open(m_path
) )
466 return dir
.HasFiles();
469 //-----------------------------------------------------------------------------
471 //-----------------------------------------------------------------------------
473 IMPLEMENT_DYNAMIC_CLASS(wxGenericDirCtrl
, wxControl
)
475 BEGIN_EVENT_TABLE(wxGenericDirCtrl
, wxControl
)
476 EVT_TREE_ITEM_EXPANDING (-1, wxGenericDirCtrl::OnExpandItem
)
477 EVT_TREE_ITEM_COLLAPSED (-1, wxGenericDirCtrl::OnCollapseItem
)
478 EVT_TREE_BEGIN_LABEL_EDIT (-1, wxGenericDirCtrl::OnBeginEditItem
)
479 EVT_TREE_END_LABEL_EDIT (-1, wxGenericDirCtrl::OnEndEditItem
)
480 EVT_SIZE (wxGenericDirCtrl::OnSize
)
483 wxGenericDirCtrl::wxGenericDirCtrl(void)
488 bool wxGenericDirCtrl::Create(wxWindow
*parent
,
494 const wxString
& filter
,
496 const wxString
& name
)
498 if (!wxControl::Create(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
501 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));
505 long treeStyle
= wxTR_HAS_BUTTONS
| wxTR_EDIT_LABELS
| wxTR_HIDE_ROOT
;
506 if ((style
& wxDIRCTRL_3D_INTERNAL
) == 0)
507 treeStyle
|= wxNO_BORDER
;
509 long filterStyle
= 0;
510 if ((style
& wxDIRCTRL_3D_INTERNAL
) == 0)
511 filterStyle
|= wxNO_BORDER
;
513 m_treeCtrl
= new wxTreeCtrl(this, wxID_TREECTRL
, pos
, size
, treeStyle
);
515 if (!filter
.IsEmpty() && (style
& wxDIRCTRL_SHOW_FILTERS
))
516 m_filterListCtrl
= new wxDirFilterListCtrl(this, wxID_FILTERLISTCTRL
, wxDefaultPosition
, wxDefaultSize
, filterStyle
);
521 SetFilterIndex(defaultFilter
);
523 if (m_filterListCtrl
)
524 m_filterListCtrl
->FillFilterList(filter
, defaultFilter
);
526 m_imageList
= new wxImageList(16, 16, TRUE
);
527 m_imageList
->Add(wxIcon(icon1_xpm
));
528 m_imageList
->Add(wxIcon(icon2_xpm
));
529 m_imageList
->Add(wxIcon(icon3_xpm
));
530 m_imageList
->Add(wxIcon(icon4_xpm
));
531 m_imageList
->Add(wxIcon(icon5_xpm
));
532 m_imageList
->Add(wxIcon(icon6_xpm
));
533 m_imageList
->Add(wxIcon(icon7_xpm
));
534 m_imageList
->Add(wxIcon(icon8_xpm
));
535 m_treeCtrl
->AssignImageList(m_imageList
);
537 m_showHidden
= FALSE
;
538 wxDirItemData
* rootData
= new wxDirItemData(wxT(""), wxT(""), TRUE
);
542 #if defined(__WINDOWS__) || defined(__WXPM__) || defined(__DOS__)
543 rootName
= _("Computer");
545 rootName
= _("Sections");
548 m_rootId
= m_treeCtrl
->AddRoot( rootName
, 3, -1, rootData
);
549 m_treeCtrl
->SetItemHasChildren(m_rootId
);
550 m_treeCtrl
->Expand(m_rootId
); // automatically expand first level
552 // Expand and select the default path
553 if (!m_defaultPath
.IsEmpty())
554 ExpandPath(m_defaultPath
);
561 wxGenericDirCtrl::~wxGenericDirCtrl()
565 void wxGenericDirCtrl::Init()
567 m_showHidden
= FALSE
;
570 m_currentFilterStr
= wxEmptyString
; // Default: any file
572 m_filterListCtrl
= NULL
;
575 void wxGenericDirCtrl::ShowHidden( bool show
)
582 void wxGenericDirCtrl::AddSection(const wxString
& path
, const wxString
& name
, int imageId
)
584 wxDirItemData
*dir_item
= new wxDirItemData(path
,name
,TRUE
);
586 wxTreeItemId id
= m_treeCtrl
->AppendItem( m_rootId
, name
, imageId
, -1, dir_item
);
588 m_treeCtrl
->SetItemHasChildren(id
);
591 void wxGenericDirCtrl::SetupSections()
593 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXPM__)
596 wxChar driveBuffer
[256];
597 size_t n
= (size_t) GetLogicalDriveStrings(255, driveBuffer
);
602 path
.Printf(wxT("%c:\\"), driveBuffer
[i
]);
603 name
.Printf(wxT("(%c:)"), driveBuffer
[i
]);
606 int driveType
= ::GetDriveType(path
);
609 case DRIVE_REMOVABLE
:
610 if (path
== wxT("a:\\") || path
== wxT("b:\\"))
611 imageId
= 6; // Floppy
629 AddSection(path
, name
, imageId
);
631 while (driveBuffer
[i
] != wxT('\0'))
634 if (driveBuffer
[i
] == wxT('\0'))
640 /* If we can switch to the drive, it exists. */
641 for( drive
= 1; drive
<= 26; drive
++ )
644 path
.Printf(wxT("%c:\\"), (char) (drive
+ 'a' - 1));
645 name
.Printf(wxT("(%c:)"), (char) (drive
+ 'A' - 1));
647 if (wxIsDriveAvailable(path
))
649 AddSection(path
, name
, (drive
<= 2) ? 6/*floppy*/ : 4/*disk*/);
652 #endif // __WIN32__/!__WIN32__
654 #elif defined(__WXMAC__)
658 short actualCount
= 0 ;
659 if ( OnLine( &volume
, 1 , &actualCount
, &index
) != noErr
|| actualCount
== 0 )
662 wxString name
= wxMacFSSpec2MacFilename( &volume
) ;
663 AddSection(name
+ wxFILE_SEP_PATH
, name
, 0);
665 #elif defined(__UNIX__)
666 AddSection(wxT("/"), wxT("/"), 3/*computer icon*/);
668 #error "Unsupported platform in wxGenericDirCtrl!"
672 void wxGenericDirCtrl::OnBeginEditItem(wxTreeEvent
&event
)
674 // don't rename the main entry "Sections"
675 if (event
.GetItem() == m_rootId
)
681 // don't rename the individual sections
682 if (m_treeCtrl
->GetParent( event
.GetItem() ) == m_rootId
)
689 void wxGenericDirCtrl::OnEndEditItem(wxTreeEvent
&event
)
691 if ((event
.GetLabel().IsEmpty()) ||
692 (event
.GetLabel() == _(".")) ||
693 (event
.GetLabel() == _("..")) ||
694 (event
.GetLabel().First( wxT("/") ) != wxNOT_FOUND
))
696 wxMessageDialog
dialog(this, _("Illegal directory name."), _("Error"), wxOK
| wxICON_ERROR
);
702 wxTreeItemId id
= event
.GetItem();
703 wxDirItemData
*data
= (wxDirItemData
*)m_treeCtrl
->GetItemData( id
);
706 wxString
new_name( wxPathOnly( data
->m_path
) );
707 new_name
+= wxString(wxFILE_SEP_PATH
);
708 new_name
+= event
.GetLabel();
712 if (wxFileExists(new_name
))
714 wxMessageDialog
dialog(this, _("File name exists already."), _("Error"), wxOK
| wxICON_ERROR
);
719 if (wxRenameFile(data
->m_path
,new_name
))
721 data
->SetNewDirName( new_name
);
725 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
731 void wxGenericDirCtrl::OnExpandItem(wxTreeEvent
&event
)
733 wxTreeItemId parentId
= event
.GetItem();
735 // VS: this is needed because the event handler is called from wxTreeCtrl
736 // ctor when wxTR_HIDE_ROOT was specified
738 m_rootId
= m_treeCtrl
->GetRootItem();
743 void wxGenericDirCtrl::OnCollapseItem(wxTreeEvent
&event
)
745 wxTreeItemId child
, parent
= event
.GetItem();
747 wxDirItemData
*data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(event
.GetItem());
748 if (!data
->m_isExpanded
)
751 data
->m_isExpanded
= FALSE
;
753 /* Workaround because DeleteChildren has disapeared (why?) and
754 * CollapseAndReset doesn't work as advertised (deletes parent too) */
755 child
= m_treeCtrl
->GetFirstChild(parent
, cookie
);
758 m_treeCtrl
->Delete(child
);
759 /* Not GetNextChild below, because the cookie mechanism can't
760 * handle disappearing children! */
761 child
= m_treeCtrl
->GetFirstChild(parent
, cookie
);
765 void wxGenericDirCtrl::ExpandDir(wxTreeItemId parentId
)
767 wxDirItemData
*data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(parentId
);
769 if (data
->m_isExpanded
)
772 data
->m_isExpanded
= TRUE
;
774 if (parentId
== m_treeCtrl
->GetRootItem())
782 wxString search
,path
,filename
;
784 wxString
dirName(data
->m_path
);
786 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXPM__)
787 // Check if this is a root directory and if so,
788 // whether the drive is avaiable.
789 if (!wxIsDriveAvailable(dirName
))
791 data
->m_isExpanded
= FALSE
;
792 //wxMessageBox(wxT("Sorry, this drive is not available."));
797 // This may take a longish time. Go to busy cursor
800 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXPM__)
801 if (dirName
.Last() == ':')
802 dirName
+= wxString(wxFILE_SEP_PATH
);
806 wxArrayString filenames
;
809 wxString eachFilename
;
816 int style
= wxDIR_DIRS
;
817 if (m_showHidden
) style
|= wxDIR_HIDDEN
;
818 if (d
.GetFirst(& eachFilename
, wxEmptyString
, style
))
822 if ((eachFilename
!= wxT(".")) && (eachFilename
!= wxT("..")))
824 dirs
.Add(eachFilename
);
827 while (d
.GetNext(& eachFilename
));
830 dirs
.Sort((wxArrayString::CompareFunction
) wxDirCtrlStringCompareFunction
);
832 // Now do the filenames -- but only if we're allowed to
833 if ((GetWindowStyle() & wxDIRCTRL_DIR_ONLY
) == 0)
841 if (d
.GetFirst(& eachFilename
, m_currentFilterStr
, wxDIR_FILES
))
845 if ((eachFilename
!= wxT(".")) && (eachFilename
!= wxT("..")))
847 filenames
.Add(eachFilename
);
850 while (d
.GetNext(& eachFilename
));
853 filenames
.Sort((wxArrayString::CompareFunction
) wxDirCtrlStringCompareFunction
);
856 // Add the sorted dirs
858 for (i
= 0; i
< dirs
.Count(); i
++)
860 wxString
eachFilename(dirs
[i
]);
862 if (path
.Last() != wxFILE_SEP_PATH
)
863 path
+= wxString(wxFILE_SEP_PATH
);
864 path
+= eachFilename
;
866 wxDirItemData
*dir_item
= new wxDirItemData(path
,eachFilename
,TRUE
);
867 wxTreeItemId id
= m_treeCtrl
->AppendItem( parentId
, eachFilename
, 0, -1, dir_item
);
868 m_treeCtrl
->SetItemImage( id
, 1, wxTreeItemIcon_Expanded
);
870 // Has this got any children? If so, make it expandable.
871 // (There are two situations when a dir has children: either it
872 // has subdirectories or it contains files that weren't filtered
873 // out. The latter only applies to dirctrl with files.)
874 if ( dir_item
->HasSubDirs() ||
875 (((GetWindowStyle() & wxDIRCTRL_DIR_ONLY
) == 0) &&
876 dir_item
->HasFiles(m_currentFilterStr
)) )
878 m_treeCtrl
->SetItemHasChildren(id
);
882 // Add the sorted filenames
883 if ((GetWindowStyle() & wxDIRCTRL_DIR_ONLY
) == 0)
885 for (i
= 0; i
< filenames
.Count(); i
++)
887 wxString
eachFilename(filenames
[i
]);
889 if (path
.Last() != wxFILE_SEP_PATH
)
890 path
+= wxString(wxFILE_SEP_PATH
);
891 path
+= eachFilename
;
892 //path = dirName + wxString(wxT("/")) + eachFilename;
893 wxDirItemData
*dir_item
= new wxDirItemData(path
,eachFilename
,FALSE
);
894 (void)m_treeCtrl
->AppendItem( parentId
, eachFilename
, 2, -1, dir_item
);
899 // Find the child that matches the first part of 'path'.
900 // E.g. if a child path is "/usr" and 'path' is "/usr/include"
901 // then the child for /usr is returned.
902 wxTreeItemId
wxGenericDirCtrl::FindChild(wxTreeItemId parentId
, const wxString
& path
, bool& done
)
904 wxString
path2(path
);
906 // Make sure all separators are as per the current platform
907 path2
.Replace(wxT("\\"), wxString(wxFILE_SEP_PATH
));
908 path2
.Replace(wxT("/"), wxString(wxFILE_SEP_PATH
));
910 // Append a separator to foil bogus substring matching
911 path2
+= wxString(wxFILE_SEP_PATH
);
913 // In MSW or PM, case is not significant
914 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXPM__)
919 wxTreeItemId childId
= m_treeCtrl
->GetFirstChild(parentId
, cookie
);
920 while (childId
.IsOk())
922 wxDirItemData
* data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(childId
);
924 if (data
&& !data
->m_path
.IsEmpty())
926 wxString
childPath(data
->m_path
);
927 if (childPath
.Last() != wxFILE_SEP_PATH
)
928 childPath
+= wxString(wxFILE_SEP_PATH
);
930 // In MSW and PM, case is not significant
931 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXPM__)
932 childPath
.MakeLower();
935 if (childPath
.Len() <= path2
.Len())
937 wxString path3
= path2
.Mid(0, childPath
.Len());
938 if (childPath
== path3
)
940 if (path3
.Len() == path2
.Len())
949 childId
= m_treeCtrl
->GetNextChild(parentId
, cookie
);
951 wxTreeItemId invalid
;
955 // Try to expand as much of the given path as possible,
956 // and select the given tree item.
957 bool wxGenericDirCtrl::ExpandPath(const wxString
& path
)
960 wxTreeItemId id
= FindChild(m_rootId
, path
, done
);
961 wxTreeItemId lastId
= id
; // The last non-zero id
962 while (id
.IsOk() && !done
)
966 id
= FindChild(id
, path
, done
);
972 wxDirItemData
*data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(lastId
);
975 m_treeCtrl
->Expand(lastId
);
977 if ((GetWindowStyle() & wxDIRCTRL_SELECT_FIRST
) && data
->m_isDir
)
979 // Find the first file in this directory
981 wxTreeItemId childId
= m_treeCtrl
->GetFirstChild(lastId
, cookie
);
982 bool selectedChild
= FALSE
;
983 while (childId
.IsOk())
985 wxDirItemData
* data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(childId
);
987 if (data
&& data
->m_path
!= "" && !data
->m_isDir
)
989 m_treeCtrl
->SelectItem(childId
);
990 m_treeCtrl
->EnsureVisible(childId
);
991 selectedChild
= TRUE
;
994 childId
= m_treeCtrl
->GetNextChild(lastId
, cookie
);
998 m_treeCtrl
->SelectItem(lastId
);
999 m_treeCtrl
->EnsureVisible(lastId
);
1004 m_treeCtrl
->SelectItem(lastId
);
1005 m_treeCtrl
->EnsureVisible(lastId
);
1014 wxString
wxGenericDirCtrl::GetPath() const
1016 wxTreeItemId id
= m_treeCtrl
->GetSelection();
1019 wxDirItemData
* data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(id
);
1020 return data
->m_path
;
1023 return wxEmptyString
;
1026 wxString
wxGenericDirCtrl::GetFilePath() const
1028 wxTreeItemId id
= m_treeCtrl
->GetSelection();
1031 wxDirItemData
* data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(id
);
1033 return wxEmptyString
;
1035 return data
->m_path
;
1038 return wxEmptyString
;
1041 void wxGenericDirCtrl::SetPath(const wxString
& path
)
1043 m_defaultPath
= path
;
1050 void wxGenericDirCtrl::FindChildFiles(wxTreeItemId id
, int dirFlags
, wxArrayString
& filenames
)
1052 wxDirItemData
*data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(id
);
1054 // This may take a longish time. Go to busy cursor
1059 wxString search
,path
,filename
;
1061 wxString
dirName(data
->m_path
);
1063 #if defined(__WXMSW__) || defined(__WXPM__)
1064 if (dirName
.Last() == ':')
1065 dirName
+= wxString(wxFILE_SEP_PATH
);
1069 wxString eachFilename
;
1076 if (d
.GetFirst(& eachFilename
, m_currentFilterStr
, dirFlags
))
1080 if ((eachFilename
!= wxT(".")) && (eachFilename
!= wxT("..")))
1082 filenames
.Add(eachFilename
);
1085 while (d
.GetNext(& eachFilename
)) ;
1091 void wxGenericDirCtrl::SetFilterIndex(int n
)
1093 m_currentFilter
= n
;
1096 if (ExtractWildcard(m_filter
, n
, f
, d
))
1097 m_currentFilterStr
= f
;
1099 m_currentFilterStr
= wxT("*.*");
1102 void wxGenericDirCtrl::SetFilter(const wxString
& filter
)
1107 if (ExtractWildcard(m_filter
, m_currentFilter
, f
, d
))
1108 m_currentFilterStr
= f
;
1110 m_currentFilterStr
= wxT("*.*");
1113 // Extract description and actual filter from overall filter string
1114 bool wxGenericDirCtrl::ExtractWildcard(const wxString
& filterStr
, int n
, wxString
& filter
, wxString
& description
)
1116 wxArrayString filters
, descriptions
;
1117 int count
= ParseFilter(filterStr
, filters
, descriptions
);
1118 if (count
> 0 && n
< count
)
1120 filter
= filters
[n
];
1121 description
= descriptions
[n
];
1128 // Parses the global filter, returning the number of filters.
1129 // Returns 0 if none or if there's a problem.
1130 // filterStr is in the form:
1132 // "All files (*.*)|*.*|JPEG Files (*.jpeg)|*.jpg"
1134 int wxGenericDirCtrl::ParseFilter(const wxString
& filterStr
, wxArrayString
& filters
, wxArrayString
& descriptions
)
1136 wxString
str(filterStr
);
1138 wxString description
, filter
;
1140 bool finished
= FALSE
;
1143 pos
= str
.Find(wxT('|'));
1145 return 0; // Problem
1146 description
= str
.Left(pos
);
1147 str
= str
.Mid(pos
+1);
1148 pos
= str
.Find(wxT('|'));
1156 filter
= str
.Left(pos
);
1157 str
= str
.Mid(pos
+1);
1159 descriptions
.Add(description
);
1160 filters
.Add(filter
);
1164 return filters
.Count();
1167 void wxGenericDirCtrl::DoResize()
1169 wxSize sz
= GetClientSize();
1170 int verticalSpacing
= 3;
1174 if (m_filterListCtrl
)
1176 filterSz
= m_filterListCtrl
->GetSize();
1177 sz
.y
-= (filterSz
.y
+ verticalSpacing
);
1179 m_treeCtrl
->SetSize(0, 0, sz
.x
, sz
.y
);
1180 if (m_filterListCtrl
)
1182 m_filterListCtrl
->SetSize(0, sz
.y
+ verticalSpacing
, sz
.x
, filterSz
.y
);
1183 // Don't know why, but this needs refreshing after a resize (wxMSW)
1184 m_filterListCtrl
->Refresh();
1190 void wxGenericDirCtrl::OnSize(wxSizeEvent
& WXUNUSED(event
))
1195 //-----------------------------------------------------------------------------
1196 // wxDirFilterListCtrl
1197 //-----------------------------------------------------------------------------
1199 IMPLEMENT_CLASS(wxDirFilterListCtrl
, wxChoice
)
1201 BEGIN_EVENT_TABLE(wxDirFilterListCtrl
, wxChoice
)
1202 EVT_CHOICE(-1, wxDirFilterListCtrl::OnSelFilter
)
1205 bool wxDirFilterListCtrl::Create(wxGenericDirCtrl
* parent
, const wxWindowID id
,
1211 return wxChoice::Create(parent
, id
, pos
, size
, 0, NULL
, style
);
1214 void wxDirFilterListCtrl::Init()
1219 void wxDirFilterListCtrl::OnSelFilter(wxCommandEvent
& WXUNUSED(event
))
1221 int sel
= GetSelection();
1223 wxString currentPath
= m_dirCtrl
->GetPath();
1225 m_dirCtrl
->SetFilterIndex(sel
);
1227 // If the filter has changed, the view is out of date, so
1228 // collapse the tree.
1229 m_dirCtrl
->GetTreeCtrl()->Collapse(m_dirCtrl
->GetRootId());
1230 m_dirCtrl
->GetTreeCtrl()->Expand(m_dirCtrl
->GetRootId());
1232 // Try to restore the selection, or at least the directory
1233 m_dirCtrl
->ExpandPath(currentPath
);
1236 void wxDirFilterListCtrl::FillFilterList(const wxString
& filter
, int defaultFilter
)
1239 wxArrayString descriptions
, filters
;
1240 size_t n
= (size_t) m_dirCtrl
->ParseFilter(filter
, filters
, descriptions
);
1242 if (n
> 0 && defaultFilter
< (int) n
)
1245 for (i
= 0; i
< n
; i
++)
1246 Append(descriptions
[i
]);
1247 SetSelection(defaultFilter
);
1251 #endif // wxUSE_DIRDLG