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
& WXUNUSED(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
;
510 // VS: Do **NOT** remove this style, ever. MSW native wxTreeCtrl::EditLabel doesn't
511 // work without this style and we need it to be able to create new directories.
512 // Generic wxTreeCtrl can do it even w/o wxTR_EDIT_LABELS, so we only add it
513 // in case of wxMSW (as it is arguably better to not have the style enabled)
514 treeStyle
|= wxTR_EDIT_LABELS
;
517 // FIXME, doesn't work for some reason
518 treeStyle
|= wxTR_HIDE_ROOT
;
521 if ((style
& wxDIRCTRL_3D_INTERNAL
) == 0)
522 treeStyle
|= wxNO_BORDER
;
524 long filterStyle
= 0;
525 if ((style
& wxDIRCTRL_3D_INTERNAL
) == 0)
526 filterStyle
|= wxNO_BORDER
;
528 m_treeCtrl
= new wxTreeCtrl(this, wxID_TREECTRL
, pos
, size
, treeStyle
);
530 if (!filter
.IsEmpty() && (style
& wxDIRCTRL_SHOW_FILTERS
))
531 m_filterListCtrl
= new wxDirFilterListCtrl(this, wxID_FILTERLISTCTRL
, wxDefaultPosition
, wxDefaultSize
, filterStyle
);
536 SetFilterIndex(defaultFilter
);
538 if (m_filterListCtrl
)
539 m_filterListCtrl
->FillFilterList(filter
, defaultFilter
);
541 m_imageList
= new wxImageList(16, 16, TRUE
);
542 m_imageList
->Add(wxIcon(icon1_xpm
));
543 m_imageList
->Add(wxIcon(icon2_xpm
));
544 m_imageList
->Add(wxIcon(icon3_xpm
));
545 m_imageList
->Add(wxIcon(icon4_xpm
));
546 m_imageList
->Add(wxIcon(icon5_xpm
));
547 m_imageList
->Add(wxIcon(icon6_xpm
));
548 m_imageList
->Add(wxIcon(icon7_xpm
));
549 m_imageList
->Add(wxIcon(icon8_xpm
));
550 m_treeCtrl
->AssignImageList(m_imageList
);
552 m_showHidden
= FALSE
;
553 wxDirItemData
* rootData
= new wxDirItemData(wxT(""), wxT(""), TRUE
);
557 #if defined(__WINDOWS__) || defined(__WXPM__) || defined(__DOS__)
558 rootName
= _("Computer");
560 rootName
= _("Sections");
563 m_rootId
= m_treeCtrl
->AddRoot( rootName
, 3, -1, rootData
);
564 m_treeCtrl
->SetItemHasChildren(m_rootId
);
565 m_treeCtrl
->Expand(m_rootId
); // automatically expand first level
567 // Expand and select the default path
568 if (!m_defaultPath
.IsEmpty())
569 ExpandPath(m_defaultPath
);
576 wxGenericDirCtrl::~wxGenericDirCtrl()
580 void wxGenericDirCtrl::Init()
582 m_showHidden
= FALSE
;
585 m_currentFilterStr
= wxEmptyString
; // Default: any file
587 m_filterListCtrl
= NULL
;
590 void wxGenericDirCtrl::ShowHidden( bool show
)
594 wxString path
= GetPath();
595 m_treeCtrl
->Collapse(m_treeCtrl
->GetRootItem());
596 m_treeCtrl
->Expand(m_treeCtrl
->GetRootItem());
600 void wxGenericDirCtrl::AddSection(const wxString
& path
, const wxString
& name
, int imageId
)
602 wxDirItemData
*dir_item
= new wxDirItemData(path
,name
,TRUE
);
604 wxTreeItemId id
= m_treeCtrl
->AppendItem( m_rootId
, name
, imageId
, -1, dir_item
);
606 m_treeCtrl
->SetItemHasChildren(id
);
609 void wxGenericDirCtrl::SetupSections()
611 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXPM__)
614 wxChar driveBuffer
[256];
615 size_t n
= (size_t) GetLogicalDriveStrings(255, driveBuffer
);
620 path
.Printf(wxT("%c:\\"), driveBuffer
[i
]);
621 name
.Printf(wxT("(%c:)"), driveBuffer
[i
]);
624 int driveType
= ::GetDriveType(path
);
627 case DRIVE_REMOVABLE
:
628 if (path
== wxT("a:\\") || path
== wxT("b:\\"))
629 imageId
= 6; // Floppy
647 AddSection(path
, name
, imageId
);
649 while (driveBuffer
[i
] != wxT('\0'))
652 if (driveBuffer
[i
] == wxT('\0'))
658 /* If we can switch to the drive, it exists. */
659 for( drive
= 1; drive
<= 26; drive
++ )
662 path
.Printf(wxT("%c:\\"), (char) (drive
+ 'a' - 1));
663 name
.Printf(wxT("(%c:)"), (char) (drive
+ 'A' - 1));
665 if (wxIsDriveAvailable(path
))
667 AddSection(path
, name
, (drive
<= 2) ? 6/*floppy*/ : 4/*disk*/);
670 #endif // __WIN32__/!__WIN32__
672 #elif defined(__WXMAC__)
676 short actualCount
= 0 ;
677 if ( OnLine( &volume
, 1 , &actualCount
, &index
) != noErr
|| actualCount
== 0 )
680 wxString name
= wxMacFSSpec2MacFilename( &volume
) ;
681 AddSection(name
+ wxFILE_SEP_PATH
, name
, 0);
683 #elif defined(__UNIX__)
684 AddSection(wxT("/"), wxT("/"), 3/*computer icon*/);
686 #error "Unsupported platform in wxGenericDirCtrl!"
690 void wxGenericDirCtrl::OnBeginEditItem(wxTreeEvent
&event
)
692 // don't rename the main entry "Sections"
693 if (event
.GetItem() == m_rootId
)
699 // don't rename the individual sections
700 if (m_treeCtrl
->GetParent( event
.GetItem() ) == m_rootId
)
707 void wxGenericDirCtrl::OnEndEditItem(wxTreeEvent
&event
)
709 if ((event
.GetLabel().IsEmpty()) ||
710 (event
.GetLabel() == _(".")) ||
711 (event
.GetLabel() == _("..")) ||
712 (event
.GetLabel().First( wxT("/") ) != wxNOT_FOUND
))
714 wxMessageDialog
dialog(this, _("Illegal directory name."), _("Error"), wxOK
| wxICON_ERROR
);
720 wxTreeItemId id
= event
.GetItem();
721 wxDirItemData
*data
= (wxDirItemData
*)m_treeCtrl
->GetItemData( id
);
724 wxString
new_name( wxPathOnly( data
->m_path
) );
725 new_name
+= wxString(wxFILE_SEP_PATH
);
726 new_name
+= event
.GetLabel();
730 if (wxFileExists(new_name
))
732 wxMessageDialog
dialog(this, _("File name exists already."), _("Error"), wxOK
| wxICON_ERROR
);
737 if (wxRenameFile(data
->m_path
,new_name
))
739 data
->SetNewDirName( new_name
);
743 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
749 void wxGenericDirCtrl::OnExpandItem(wxTreeEvent
&event
)
751 wxTreeItemId parentId
= event
.GetItem();
753 // VS: this is needed because the event handler is called from wxTreeCtrl
754 // ctor when wxTR_HIDE_ROOT was specified
756 m_rootId
= m_treeCtrl
->GetRootItem();
761 void wxGenericDirCtrl::OnCollapseItem(wxTreeEvent
&event
)
763 wxTreeItemId child
, parent
= event
.GetItem();
765 wxDirItemData
*data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(event
.GetItem());
766 if (!data
->m_isExpanded
)
769 data
->m_isExpanded
= FALSE
;
771 /* Workaround because DeleteChildren has disapeared (why?) and
772 * CollapseAndReset doesn't work as advertised (deletes parent too) */
773 child
= m_treeCtrl
->GetFirstChild(parent
, 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(parent
, cookie
);
783 void wxGenericDirCtrl::ExpandDir(wxTreeItemId parentId
)
785 wxDirItemData
*data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(parentId
);
787 if (data
->m_isExpanded
)
790 data
->m_isExpanded
= TRUE
;
792 if (parentId
== m_treeCtrl
->GetRootItem())
800 wxString search
,path
,filename
;
802 wxString
dirName(data
->m_path
);
804 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXPM__)
805 // Check if this is a root directory and if so,
806 // whether the drive is avaiable.
807 if (!wxIsDriveAvailable(dirName
))
809 data
->m_isExpanded
= FALSE
;
810 //wxMessageBox(wxT("Sorry, this drive is not available."));
815 // This may take a longish time. Go to busy cursor
818 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXPM__)
819 if (dirName
.Last() == ':')
820 dirName
+= wxString(wxFILE_SEP_PATH
);
824 wxArrayString filenames
;
827 wxString eachFilename
;
834 int style
= wxDIR_DIRS
;
835 if (m_showHidden
) style
|= wxDIR_HIDDEN
;
836 if (d
.GetFirst(& eachFilename
, wxEmptyString
, style
))
840 if ((eachFilename
!= wxT(".")) && (eachFilename
!= wxT("..")))
842 dirs
.Add(eachFilename
);
845 while (d
.GetNext(& eachFilename
));
848 dirs
.Sort((wxArrayString::CompareFunction
) wxDirCtrlStringCompareFunction
);
850 // Now do the filenames -- but only if we're allowed to
851 if ((GetWindowStyle() & wxDIRCTRL_DIR_ONLY
) == 0)
859 if (d
.GetFirst(& eachFilename
, m_currentFilterStr
, wxDIR_FILES
))
863 if ((eachFilename
!= wxT(".")) && (eachFilename
!= wxT("..")))
865 filenames
.Add(eachFilename
);
868 while (d
.GetNext(& eachFilename
));
871 filenames
.Sort((wxArrayString::CompareFunction
) wxDirCtrlStringCompareFunction
);
874 // Add the sorted dirs
876 for (i
= 0; i
< dirs
.Count(); i
++)
878 wxString
eachFilename(dirs
[i
]);
880 if (path
.Last() != wxFILE_SEP_PATH
)
881 path
+= wxString(wxFILE_SEP_PATH
);
882 path
+= eachFilename
;
884 wxDirItemData
*dir_item
= new wxDirItemData(path
,eachFilename
,TRUE
);
885 wxTreeItemId id
= m_treeCtrl
->AppendItem( parentId
, eachFilename
, 0, -1, dir_item
);
886 m_treeCtrl
->SetItemImage( id
, 1, wxTreeItemIcon_Expanded
);
888 // Has this got any children? If so, make it expandable.
889 // (There are two situations when a dir has children: either it
890 // has subdirectories or it contains files that weren't filtered
891 // out. The latter only applies to dirctrl with files.)
892 if ( dir_item
->HasSubDirs() ||
893 (((GetWindowStyle() & wxDIRCTRL_DIR_ONLY
) == 0) &&
894 dir_item
->HasFiles(m_currentFilterStr
)) )
896 m_treeCtrl
->SetItemHasChildren(id
);
900 // Add the sorted filenames
901 if ((GetWindowStyle() & wxDIRCTRL_DIR_ONLY
) == 0)
903 for (i
= 0; i
< filenames
.Count(); i
++)
905 wxString
eachFilename(filenames
[i
]);
907 if (path
.Last() != wxFILE_SEP_PATH
)
908 path
+= wxString(wxFILE_SEP_PATH
);
909 path
+= eachFilename
;
910 //path = dirName + wxString(wxT("/")) + eachFilename;
911 wxDirItemData
*dir_item
= new wxDirItemData(path
,eachFilename
,FALSE
);
912 (void)m_treeCtrl
->AppendItem( parentId
, eachFilename
, 2, -1, dir_item
);
917 // Find the child that matches the first part of 'path'.
918 // E.g. if a child path is "/usr" and 'path' is "/usr/include"
919 // then the child for /usr is returned.
920 wxTreeItemId
wxGenericDirCtrl::FindChild(wxTreeItemId parentId
, const wxString
& path
, bool& done
)
922 wxString
path2(path
);
924 // Make sure all separators are as per the current platform
925 path2
.Replace(wxT("\\"), wxString(wxFILE_SEP_PATH
));
926 path2
.Replace(wxT("/"), wxString(wxFILE_SEP_PATH
));
928 // Append a separator to foil bogus substring matching
929 path2
+= wxString(wxFILE_SEP_PATH
);
931 // In MSW or PM, case is not significant
932 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXPM__)
937 wxTreeItemId childId
= m_treeCtrl
->GetFirstChild(parentId
, cookie
);
938 while (childId
.IsOk())
940 wxDirItemData
* data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(childId
);
942 if (data
&& !data
->m_path
.IsEmpty())
944 wxString
childPath(data
->m_path
);
945 if (childPath
.Last() != wxFILE_SEP_PATH
)
946 childPath
+= wxString(wxFILE_SEP_PATH
);
948 // In MSW and PM, case is not significant
949 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXPM__)
950 childPath
.MakeLower();
953 if (childPath
.Len() <= path2
.Len())
955 wxString path3
= path2
.Mid(0, childPath
.Len());
956 if (childPath
== path3
)
958 if (path3
.Len() == path2
.Len())
967 childId
= m_treeCtrl
->GetNextChild(parentId
, cookie
);
969 wxTreeItemId invalid
;
973 // Try to expand as much of the given path as possible,
974 // and select the given tree item.
975 bool wxGenericDirCtrl::ExpandPath(const wxString
& path
)
978 wxTreeItemId id
= FindChild(m_rootId
, path
, done
);
979 wxTreeItemId lastId
= id
; // The last non-zero id
980 while (id
.IsOk() && !done
)
984 id
= FindChild(id
, path
, done
);
990 wxDirItemData
*data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(lastId
);
993 m_treeCtrl
->Expand(lastId
);
995 if ((GetWindowStyle() & wxDIRCTRL_SELECT_FIRST
) && data
->m_isDir
)
997 // Find the first file in this directory
999 wxTreeItemId childId
= m_treeCtrl
->GetFirstChild(lastId
, cookie
);
1000 bool selectedChild
= FALSE
;
1001 while (childId
.IsOk())
1003 wxDirItemData
* data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(childId
);
1005 if (data
&& data
->m_path
!= "" && !data
->m_isDir
)
1007 m_treeCtrl
->SelectItem(childId
);
1008 m_treeCtrl
->EnsureVisible(childId
);
1009 selectedChild
= TRUE
;
1012 childId
= m_treeCtrl
->GetNextChild(lastId
, cookie
);
1016 m_treeCtrl
->SelectItem(lastId
);
1017 m_treeCtrl
->EnsureVisible(lastId
);
1022 m_treeCtrl
->SelectItem(lastId
);
1023 m_treeCtrl
->EnsureVisible(lastId
);
1032 wxString
wxGenericDirCtrl::GetPath() const
1034 wxTreeItemId id
= m_treeCtrl
->GetSelection();
1037 wxDirItemData
* data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(id
);
1038 return data
->m_path
;
1041 return wxEmptyString
;
1044 wxString
wxGenericDirCtrl::GetFilePath() const
1046 wxTreeItemId id
= m_treeCtrl
->GetSelection();
1049 wxDirItemData
* data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(id
);
1051 return wxEmptyString
;
1053 return data
->m_path
;
1056 return wxEmptyString
;
1059 void wxGenericDirCtrl::SetPath(const wxString
& path
)
1061 m_defaultPath
= path
;
1068 void wxGenericDirCtrl::FindChildFiles(wxTreeItemId id
, int dirFlags
, wxArrayString
& filenames
)
1070 wxDirItemData
*data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(id
);
1072 // This may take a longish time. Go to busy cursor
1077 wxString search
,path
,filename
;
1079 wxString
dirName(data
->m_path
);
1081 #if defined(__WXMSW__) || defined(__WXPM__)
1082 if (dirName
.Last() == ':')
1083 dirName
+= wxString(wxFILE_SEP_PATH
);
1087 wxString eachFilename
;
1094 if (d
.GetFirst(& eachFilename
, m_currentFilterStr
, dirFlags
))
1098 if ((eachFilename
!= wxT(".")) && (eachFilename
!= wxT("..")))
1100 filenames
.Add(eachFilename
);
1103 while (d
.GetNext(& eachFilename
)) ;
1109 void wxGenericDirCtrl::SetFilterIndex(int n
)
1111 m_currentFilter
= n
;
1114 if (ExtractWildcard(m_filter
, n
, f
, d
))
1115 m_currentFilterStr
= f
;
1117 m_currentFilterStr
= wxT("*.*");
1120 void wxGenericDirCtrl::SetFilter(const wxString
& filter
)
1125 if (ExtractWildcard(m_filter
, m_currentFilter
, f
, d
))
1126 m_currentFilterStr
= f
;
1128 m_currentFilterStr
= wxT("*.*");
1131 // Extract description and actual filter from overall filter string
1132 bool wxGenericDirCtrl::ExtractWildcard(const wxString
& filterStr
, int n
, wxString
& filter
, wxString
& description
)
1134 wxArrayString filters
, descriptions
;
1135 int count
= ParseFilter(filterStr
, filters
, descriptions
);
1136 if (count
> 0 && n
< count
)
1138 filter
= filters
[n
];
1139 description
= descriptions
[n
];
1146 // Parses the global filter, returning the number of filters.
1147 // Returns 0 if none or if there's a problem.
1148 // filterStr is in the form:
1150 // "All files (*.*)|*.*|JPEG Files (*.jpeg)|*.jpg"
1152 int wxGenericDirCtrl::ParseFilter(const wxString
& filterStr
, wxArrayString
& filters
, wxArrayString
& descriptions
)
1154 wxString
str(filterStr
);
1156 wxString description
, filter
;
1158 bool finished
= FALSE
;
1161 pos
= str
.Find(wxT('|'));
1163 return 0; // Problem
1164 description
= str
.Left(pos
);
1165 str
= str
.Mid(pos
+1);
1166 pos
= str
.Find(wxT('|'));
1174 filter
= str
.Left(pos
);
1175 str
= str
.Mid(pos
+1);
1177 descriptions
.Add(description
);
1178 filters
.Add(filter
);
1182 return filters
.Count();
1185 void wxGenericDirCtrl::DoResize()
1187 wxSize sz
= GetClientSize();
1188 int verticalSpacing
= 3;
1192 if (m_filterListCtrl
)
1195 // For some reason, this is required in order for the
1196 // correct control height to always be returned, rather
1197 // than the drop-down list height which is sometimes returned.
1198 wxSize oldSize
= m_filterListCtrl
->GetSize();
1199 m_filterListCtrl
->SetSize(-1, -1, oldSize
.x
+10, -1, wxSIZE_USE_EXISTING
);
1200 m_filterListCtrl
->SetSize(-1, -1, oldSize
.x
, -1, wxSIZE_USE_EXISTING
);
1202 filterSz
= m_filterListCtrl
->GetSize();
1203 sz
.y
-= (filterSz
.y
+ verticalSpacing
);
1205 m_treeCtrl
->SetSize(0, 0, sz
.x
, sz
.y
);
1206 if (m_filterListCtrl
)
1208 m_filterListCtrl
->SetSize(0, sz
.y
+ verticalSpacing
, sz
.x
, filterSz
.y
);
1209 // Don't know why, but this needs refreshing after a resize (wxMSW)
1210 m_filterListCtrl
->Refresh();
1216 void wxGenericDirCtrl::OnSize(wxSizeEvent
& WXUNUSED(event
))
1221 //-----------------------------------------------------------------------------
1222 // wxDirFilterListCtrl
1223 //-----------------------------------------------------------------------------
1225 IMPLEMENT_CLASS(wxDirFilterListCtrl
, wxChoice
)
1227 BEGIN_EVENT_TABLE(wxDirFilterListCtrl
, wxChoice
)
1228 EVT_CHOICE(-1, wxDirFilterListCtrl::OnSelFilter
)
1231 bool wxDirFilterListCtrl::Create(wxGenericDirCtrl
* parent
, const wxWindowID id
,
1237 return wxChoice::Create(parent
, id
, pos
, size
, 0, NULL
, style
);
1240 void wxDirFilterListCtrl::Init()
1245 void wxDirFilterListCtrl::OnSelFilter(wxCommandEvent
& WXUNUSED(event
))
1247 int sel
= GetSelection();
1249 wxString currentPath
= m_dirCtrl
->GetPath();
1251 m_dirCtrl
->SetFilterIndex(sel
);
1253 // If the filter has changed, the view is out of date, so
1254 // collapse the tree.
1255 m_dirCtrl
->GetTreeCtrl()->Collapse(m_dirCtrl
->GetRootId());
1256 m_dirCtrl
->GetTreeCtrl()->Expand(m_dirCtrl
->GetRootId());
1258 // Try to restore the selection, or at least the directory
1259 m_dirCtrl
->ExpandPath(currentPath
);
1262 void wxDirFilterListCtrl::FillFilterList(const wxString
& filter
, int defaultFilter
)
1265 wxArrayString descriptions
, filters
;
1266 size_t n
= (size_t) m_dirCtrl
->ParseFilter(filter
, filters
, descriptions
);
1268 if (n
> 0 && defaultFilter
< (int) n
)
1271 for (i
= 0; i
< n
; i
++)
1272 Append(descriptions
[i
]);
1273 SetSelection(defaultFilter
);
1277 #endif // wxUSE_DIRDLG