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
[] = {
317 bool wxIsDriveAvailable(const wxString
& dirName
)
319 // FIXME_MGL - this method leads to hang up under Watcom for some reason
321 if ( dirName
.Len() == 3 && dirName
[1u] == wxT(':') )
323 wxString
dirNameLower(dirName
.Lower());
324 // VS: always return TRUE for removable media, since Win95 doesn't
325 // like it when MS-DOS app accesses empty floppy drive
326 return (dirNameLower
[0u] == wxT('a') ||
327 dirNameLower
[0u] == wxT('b') ||
328 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
& WXUNUSED(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_HIDE_ROOT
;
508 if (style
& wxDIRCTRL_EDITABLE
)
509 treeStyle
|= wxTR_EDIT_LABELS
;
512 if ((style
& wxDIRCTRL_3D_INTERNAL
) == 0)
513 treeStyle
|= wxNO_BORDER
;
515 long filterStyle
= 0;
516 if ((style
& wxDIRCTRL_3D_INTERNAL
) == 0)
517 filterStyle
|= wxNO_BORDER
;
519 m_treeCtrl
= new wxTreeCtrl(this, wxID_TREECTRL
, pos
, size
, treeStyle
);
521 if (!filter
.IsEmpty() && (style
& wxDIRCTRL_SHOW_FILTERS
))
522 m_filterListCtrl
= new wxDirFilterListCtrl(this, wxID_FILTERLISTCTRL
, wxDefaultPosition
, wxDefaultSize
, filterStyle
);
527 SetFilterIndex(defaultFilter
);
529 if (m_filterListCtrl
)
530 m_filterListCtrl
->FillFilterList(filter
, defaultFilter
);
532 m_imageList
= new wxImageList(16, 16, TRUE
);
533 m_imageList
->Add(wxIcon(icon1_xpm
));
534 m_imageList
->Add(wxIcon(icon2_xpm
));
535 m_imageList
->Add(wxIcon(icon3_xpm
));
536 m_imageList
->Add(wxIcon(icon4_xpm
));
537 m_imageList
->Add(wxIcon(icon5_xpm
));
538 m_imageList
->Add(wxIcon(icon6_xpm
));
539 m_imageList
->Add(wxIcon(icon7_xpm
));
540 m_imageList
->Add(wxIcon(icon8_xpm
));
541 m_treeCtrl
->AssignImageList(m_imageList
);
543 m_showHidden
= FALSE
;
544 wxDirItemData
* rootData
= new wxDirItemData(wxT(""), wxT(""), TRUE
);
548 #if defined(__WINDOWS__) || defined(__WXPM__) || defined(__DOS__)
549 rootName
= _("Computer");
551 rootName
= _("Sections");
554 m_rootId
= m_treeCtrl
->AddRoot( rootName
, 3, -1, rootData
);
555 m_treeCtrl
->SetItemHasChildren(m_rootId
);
556 ExpandDir(m_rootId
); // automatically expand first level
558 // Expand and select the default path
559 if (!m_defaultPath
.IsEmpty())
560 ExpandPath(m_defaultPath
);
567 wxGenericDirCtrl::~wxGenericDirCtrl()
571 void wxGenericDirCtrl::Init()
573 m_showHidden
= FALSE
;
576 m_currentFilterStr
= wxEmptyString
; // Default: any file
578 m_filterListCtrl
= NULL
;
581 void wxGenericDirCtrl::ShowHidden( bool show
)
585 wxString path
= GetPath();
590 void wxGenericDirCtrl::AddSection(const wxString
& path
, const wxString
& name
, int imageId
)
592 wxDirItemData
*dir_item
= new wxDirItemData(path
,name
,TRUE
);
594 wxTreeItemId id
= m_treeCtrl
->AppendItem( m_rootId
, name
, imageId
, -1, dir_item
);
596 m_treeCtrl
->SetItemHasChildren(id
);
599 void wxGenericDirCtrl::SetupSections()
601 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXPM__)
604 wxChar driveBuffer
[256];
605 size_t n
= (size_t) GetLogicalDriveStrings(255, driveBuffer
);
610 path
.Printf(wxT("%c:\\"), driveBuffer
[i
]);
611 name
.Printf(wxT("(%c:)"), driveBuffer
[i
]);
614 int driveType
= ::GetDriveType(path
);
617 case DRIVE_REMOVABLE
:
618 if (path
== wxT("a:\\") || path
== wxT("b:\\"))
619 imageId
= 6; // Floppy
637 AddSection(path
, name
, imageId
);
639 while (driveBuffer
[i
] != wxT('\0'))
642 if (driveBuffer
[i
] == wxT('\0'))
648 /* If we can switch to the drive, it exists. */
649 for( drive
= 1; drive
<= 26; drive
++ )
652 path
.Printf(wxT("%c:\\"), (char) (drive
+ 'a' - 1));
653 name
.Printf(wxT("(%c:)"), (char) (drive
+ 'A' - 1));
655 if (wxIsDriveAvailable(path
))
657 AddSection(path
, name
, (drive
<= 2) ? 6/*floppy*/ : 4/*disk*/);
660 #endif // __WIN32__/!__WIN32__
662 #elif defined(__WXMAC__)
666 short actualCount
= 0 ;
667 if ( OnLine( &volume
, 1 , &actualCount
, &index
) != noErr
|| actualCount
== 0 )
670 wxString name
= wxMacFSSpec2MacFilename( &volume
) ;
671 AddSection(name
+ wxFILE_SEP_PATH
, name
, 0);
673 #elif defined(__UNIX__)
674 AddSection(wxT("/"), wxT("/"), 3/*computer icon*/);
676 #error "Unsupported platform in wxGenericDirCtrl!"
680 void wxGenericDirCtrl::OnBeginEditItem(wxTreeEvent
&event
)
682 // don't rename the main entry "Sections"
683 if (event
.GetItem() == m_rootId
)
689 // don't rename the individual sections
690 if (m_treeCtrl
->GetParent( event
.GetItem() ) == m_rootId
)
697 void wxGenericDirCtrl::OnEndEditItem(wxTreeEvent
&event
)
699 if ((event
.GetLabel().IsEmpty()) ||
700 (event
.GetLabel() == _(".")) ||
701 (event
.GetLabel() == _("..")) ||
702 (event
.GetLabel().First( 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 m_rootId
= m_treeCtrl
->GetRootItem();
751 void wxGenericDirCtrl::OnCollapseItem(wxTreeEvent
&event
)
753 CollapseDir(event
.GetItem());
756 void wxGenericDirCtrl::CollapseDir(wxTreeItemId parentId
)
760 wxDirItemData
*data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(parentId
);
761 if (!data
->m_isExpanded
)
764 data
->m_isExpanded
= FALSE
;
766 /* Workaround because DeleteChildren has disapeared (why?) and
767 * CollapseAndReset doesn't work as advertised (deletes parent too) */
768 child
= m_treeCtrl
->GetFirstChild(parentId
, cookie
);
771 m_treeCtrl
->Delete(child
);
772 /* Not GetNextChild below, because the cookie mechanism can't
773 * handle disappearing children! */
774 child
= m_treeCtrl
->GetFirstChild(parentId
, cookie
);
778 void wxGenericDirCtrl::ExpandDir(wxTreeItemId parentId
)
780 wxDirItemData
*data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(parentId
);
782 if (data
->m_isExpanded
)
785 data
->m_isExpanded
= TRUE
;
787 if (parentId
== m_treeCtrl
->GetRootItem())
795 wxString search
,path
,filename
;
797 wxString
dirName(data
->m_path
);
799 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXPM__)
800 // Check if this is a root directory and if so,
801 // whether the drive is avaiable.
802 if (!wxIsDriveAvailable(dirName
))
804 data
->m_isExpanded
= FALSE
;
805 //wxMessageBox(wxT("Sorry, this drive is not available."));
810 // This may take a longish time. Go to busy cursor
813 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXPM__)
814 if (dirName
.Last() == ':')
815 dirName
+= wxString(wxFILE_SEP_PATH
);
819 wxArrayString filenames
;
822 wxString eachFilename
;
829 int style
= wxDIR_DIRS
;
830 if (m_showHidden
) style
|= wxDIR_HIDDEN
;
831 if (d
.GetFirst(& eachFilename
, wxEmptyString
, style
))
835 if ((eachFilename
!= wxT(".")) && (eachFilename
!= wxT("..")))
837 dirs
.Add(eachFilename
);
840 while (d
.GetNext(& eachFilename
));
843 dirs
.Sort((wxArrayString::CompareFunction
) wxDirCtrlStringCompareFunction
);
845 // Now do the filenames -- but only if we're allowed to
846 if ((GetWindowStyle() & wxDIRCTRL_DIR_ONLY
) == 0)
854 if (d
.GetFirst(& eachFilename
, m_currentFilterStr
, wxDIR_FILES
))
858 if ((eachFilename
!= wxT(".")) && (eachFilename
!= wxT("..")))
860 filenames
.Add(eachFilename
);
863 while (d
.GetNext(& eachFilename
));
866 filenames
.Sort((wxArrayString::CompareFunction
) wxDirCtrlStringCompareFunction
);
869 // Add the sorted dirs
871 for (i
= 0; i
< dirs
.Count(); i
++)
873 wxString
eachFilename(dirs
[i
]);
875 if (path
.Last() != wxFILE_SEP_PATH
)
876 path
+= wxString(wxFILE_SEP_PATH
);
877 path
+= eachFilename
;
879 wxDirItemData
*dir_item
= new wxDirItemData(path
,eachFilename
,TRUE
);
880 wxTreeItemId id
= m_treeCtrl
->AppendItem( parentId
, eachFilename
, 0, -1, dir_item
);
881 m_treeCtrl
->SetItemImage( id
, 1, wxTreeItemIcon_Expanded
);
883 // Has this got any children? If so, make it expandable.
884 // (There are two situations when a dir has children: either it
885 // has subdirectories or it contains files that weren't filtered
886 // out. The latter only applies to dirctrl with files.)
887 if ( dir_item
->HasSubDirs() ||
888 (((GetWindowStyle() & wxDIRCTRL_DIR_ONLY
) == 0) &&
889 dir_item
->HasFiles(m_currentFilterStr
)) )
891 m_treeCtrl
->SetItemHasChildren(id
);
895 // Add the sorted filenames
896 if ((GetWindowStyle() & wxDIRCTRL_DIR_ONLY
) == 0)
898 for (i
= 0; i
< filenames
.Count(); i
++)
900 wxString
eachFilename(filenames
[i
]);
902 if (path
.Last() != wxFILE_SEP_PATH
)
903 path
+= wxString(wxFILE_SEP_PATH
);
904 path
+= eachFilename
;
905 //path = dirName + wxString(wxT("/")) + eachFilename;
906 wxDirItemData
*dir_item
= new wxDirItemData(path
,eachFilename
,FALSE
);
907 (void)m_treeCtrl
->AppendItem( parentId
, eachFilename
, 2, -1, dir_item
);
912 void wxGenericDirCtrl::ReCreateTree()
914 CollapseDir(m_treeCtrl
->GetRootItem());
915 ExpandDir(m_treeCtrl
->GetRootItem());
918 // Find the child that matches the first part of 'path'.
919 // E.g. if a child path is "/usr" and 'path' is "/usr/include"
920 // then the child for /usr is returned.
921 wxTreeItemId
wxGenericDirCtrl::FindChild(wxTreeItemId parentId
, const wxString
& path
, bool& done
)
923 wxString
path2(path
);
925 // Make sure all separators are as per the current platform
926 path2
.Replace(wxT("\\"), wxString(wxFILE_SEP_PATH
));
927 path2
.Replace(wxT("/"), wxString(wxFILE_SEP_PATH
));
929 // Append a separator to foil bogus substring matching
930 path2
+= wxString(wxFILE_SEP_PATH
);
932 // In MSW or PM, case is not significant
933 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXPM__)
938 wxTreeItemId childId
= m_treeCtrl
->GetFirstChild(parentId
, cookie
);
939 while (childId
.IsOk())
941 wxDirItemData
* data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(childId
);
943 if (data
&& !data
->m_path
.IsEmpty())
945 wxString
childPath(data
->m_path
);
946 if (childPath
.Last() != wxFILE_SEP_PATH
)
947 childPath
+= wxString(wxFILE_SEP_PATH
);
949 // In MSW and PM, case is not significant
950 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXPM__)
951 childPath
.MakeLower();
954 if (childPath
.Len() <= path2
.Len())
956 wxString path3
= path2
.Mid(0, childPath
.Len());
957 if (childPath
== path3
)
959 if (path3
.Len() == path2
.Len())
968 childId
= m_treeCtrl
->GetNextChild(parentId
, cookie
);
970 wxTreeItemId invalid
;
974 // Try to expand as much of the given path as possible,
975 // and select the given tree item.
976 bool wxGenericDirCtrl::ExpandPath(const wxString
& path
)
979 wxTreeItemId id
= FindChild(m_rootId
, path
, done
);
980 wxTreeItemId lastId
= id
; // The last non-zero id
981 while (id
.IsOk() && !done
)
985 id
= FindChild(id
, path
, done
);
991 wxDirItemData
*data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(lastId
);
994 m_treeCtrl
->Expand(lastId
);
996 if ((GetWindowStyle() & wxDIRCTRL_SELECT_FIRST
) && data
->m_isDir
)
998 // Find the first file in this directory
1000 wxTreeItemId childId
= m_treeCtrl
->GetFirstChild(lastId
, cookie
);
1001 bool selectedChild
= FALSE
;
1002 while (childId
.IsOk())
1004 wxDirItemData
* data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(childId
);
1006 if (data
&& data
->m_path
!= "" && !data
->m_isDir
)
1008 m_treeCtrl
->SelectItem(childId
);
1009 m_treeCtrl
->EnsureVisible(childId
);
1010 selectedChild
= TRUE
;
1013 childId
= m_treeCtrl
->GetNextChild(lastId
, cookie
);
1017 m_treeCtrl
->SelectItem(lastId
);
1018 m_treeCtrl
->EnsureVisible(lastId
);
1023 m_treeCtrl
->SelectItem(lastId
);
1024 m_treeCtrl
->EnsureVisible(lastId
);
1033 wxString
wxGenericDirCtrl::GetPath() const
1035 wxTreeItemId id
= m_treeCtrl
->GetSelection();
1038 wxDirItemData
* data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(id
);
1039 return data
->m_path
;
1042 return wxEmptyString
;
1045 wxString
wxGenericDirCtrl::GetFilePath() const
1047 wxTreeItemId id
= m_treeCtrl
->GetSelection();
1050 wxDirItemData
* data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(id
);
1052 return wxEmptyString
;
1054 return data
->m_path
;
1057 return wxEmptyString
;
1060 void wxGenericDirCtrl::SetPath(const wxString
& path
)
1062 m_defaultPath
= path
;
1069 void wxGenericDirCtrl::FindChildFiles(wxTreeItemId id
, int dirFlags
, wxArrayString
& filenames
)
1071 wxDirItemData
*data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(id
);
1073 // This may take a longish time. Go to busy cursor
1078 wxString search
,path
,filename
;
1080 wxString
dirName(data
->m_path
);
1082 #if defined(__WXMSW__) || defined(__WXPM__)
1083 if (dirName
.Last() == ':')
1084 dirName
+= wxString(wxFILE_SEP_PATH
);
1088 wxString eachFilename
;
1095 if (d
.GetFirst(& eachFilename
, m_currentFilterStr
, dirFlags
))
1099 if ((eachFilename
!= wxT(".")) && (eachFilename
!= wxT("..")))
1101 filenames
.Add(eachFilename
);
1104 while (d
.GetNext(& eachFilename
)) ;
1110 void wxGenericDirCtrl::SetFilterIndex(int n
)
1112 m_currentFilter
= n
;
1115 if (ExtractWildcard(m_filter
, n
, f
, d
))
1116 m_currentFilterStr
= f
;
1118 m_currentFilterStr
= wxT("*.*");
1121 void wxGenericDirCtrl::SetFilter(const wxString
& filter
)
1126 if (ExtractWildcard(m_filter
, m_currentFilter
, f
, d
))
1127 m_currentFilterStr
= f
;
1129 m_currentFilterStr
= wxT("*.*");
1132 // Extract description and actual filter from overall filter string
1133 bool wxGenericDirCtrl::ExtractWildcard(const wxString
& filterStr
, int n
, wxString
& filter
, wxString
& description
)
1135 wxArrayString filters
, descriptions
;
1136 int count
= ParseFilter(filterStr
, filters
, descriptions
);
1137 if (count
> 0 && n
< count
)
1139 filter
= filters
[n
];
1140 description
= descriptions
[n
];
1147 // Parses the global filter, returning the number of filters.
1148 // Returns 0 if none or if there's a problem.
1149 // filterStr is in the form:
1151 // "All files (*.*)|*.*|JPEG Files (*.jpeg)|*.jpg"
1153 int wxGenericDirCtrl::ParseFilter(const wxString
& filterStr
, wxArrayString
& filters
, wxArrayString
& descriptions
)
1155 wxString
str(filterStr
);
1157 wxString description
, filter
;
1159 bool finished
= FALSE
;
1162 pos
= str
.Find(wxT('|'));
1164 return 0; // Problem
1165 description
= str
.Left(pos
);
1166 str
= str
.Mid(pos
+1);
1167 pos
= str
.Find(wxT('|'));
1175 filter
= str
.Left(pos
);
1176 str
= str
.Mid(pos
+1);
1178 descriptions
.Add(description
);
1179 filters
.Add(filter
);
1183 return filters
.Count();
1186 void wxGenericDirCtrl::DoResize()
1188 wxSize sz
= GetClientSize();
1189 int verticalSpacing
= 3;
1193 if (m_filterListCtrl
)
1196 // For some reason, this is required in order for the
1197 // correct control height to always be returned, rather
1198 // than the drop-down list height which is sometimes returned.
1199 wxSize oldSize
= m_filterListCtrl
->GetSize();
1200 m_filterListCtrl
->SetSize(-1, -1, oldSize
.x
+10, -1, wxSIZE_USE_EXISTING
);
1201 m_filterListCtrl
->SetSize(-1, -1, oldSize
.x
, -1, wxSIZE_USE_EXISTING
);
1203 filterSz
= m_filterListCtrl
->GetSize();
1204 sz
.y
-= (filterSz
.y
+ verticalSpacing
);
1206 m_treeCtrl
->SetSize(0, 0, sz
.x
, sz
.y
);
1207 if (m_filterListCtrl
)
1209 m_filterListCtrl
->SetSize(0, sz
.y
+ verticalSpacing
, sz
.x
, filterSz
.y
);
1210 // Don't know why, but this needs refreshing after a resize (wxMSW)
1211 m_filterListCtrl
->Refresh();
1217 void wxGenericDirCtrl::OnSize(wxSizeEvent
& WXUNUSED(event
))
1222 //-----------------------------------------------------------------------------
1223 // wxDirFilterListCtrl
1224 //-----------------------------------------------------------------------------
1226 IMPLEMENT_CLASS(wxDirFilterListCtrl
, wxChoice
)
1228 BEGIN_EVENT_TABLE(wxDirFilterListCtrl
, wxChoice
)
1229 EVT_CHOICE(-1, wxDirFilterListCtrl::OnSelFilter
)
1232 bool wxDirFilterListCtrl::Create(wxGenericDirCtrl
* parent
, const wxWindowID id
,
1238 return wxChoice::Create(parent
, id
, pos
, size
, 0, NULL
, style
);
1241 void wxDirFilterListCtrl::Init()
1246 void wxDirFilterListCtrl::OnSelFilter(wxCommandEvent
& WXUNUSED(event
))
1248 int sel
= GetSelection();
1250 wxString currentPath
= m_dirCtrl
->GetPath();
1252 m_dirCtrl
->SetFilterIndex(sel
);
1254 // If the filter has changed, the view is out of date, so
1255 // collapse the tree.
1256 m_dirCtrl
->ReCreateTree();
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