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(__WXWINE__) && (!defined(__GNUWIN32__) || \
61 (defined(__MINGW32_MAJOR_VERSION) && __MINGW32_MAJOR_VERSION >= 1))
81 #if defined(__WXMAC__)
83 # include "MoreFilesX.h"
85 # include "MoreFilesExtras.h"
93 // If compiled under Windows, this macro can cause problems
99 static const char * icon1_xpm
[] = {
100 /* width height ncolors chars_per_pixel */
128 static const char * icon2_xpm
[] = {
129 /* width height ncolors chars_per_pixel */
157 static const char * icon3_xpm
[] = {
158 /* width height ncolors chars_per_pixel */
183 static const char * icon4_xpm
[] = {
210 static const char * icon5_xpm
[] = {
237 static const char *icon6_xpm
[] = {
267 static const char * icon7_xpm
[] = {
294 static const char * icon8_xpm
[] = {
323 bool wxIsDriveAvailable(const wxString
& dirName
)
325 // FIXME_MGL - this method leads to hang up under Watcom for some reason
327 if ( dirName
.Len() == 3 && dirName
[1u] == wxT(':') )
329 wxString
dirNameLower(dirName
.Lower());
330 // VS: always return TRUE for removable media, since Win95 doesn't
331 // like it when MS-DOS app accesses empty floppy drive
332 return (dirNameLower
[0u] == wxT('a') ||
333 dirNameLower
[0u] == wxT('b') ||
334 wxPathExists(dirNameLower
));
341 #elif defined(__WINDOWS__) || defined(__WXPM__)
343 int setdrive(int drive
)
345 #if defined(__GNUWIN32__) && \
346 (defined(__MINGW32_MAJOR_VERSION) && __MINGW32_MAJOR_VERSION >= 1)
347 return _chdrive(drive
);
351 if (drive
< 1 || drive
> 31)
353 newdrive
[0] = (wxChar
)(wxT('A') + drive
- 1);
354 newdrive
[1] = wxT(':');
355 newdrive
[2] = wxT('\0');
356 #if defined(__WXMSW__)
358 if (wxSetWorkingDirectory(newdrive
))
360 if (::SetCurrentDirectory(newdrive
))
363 // VA doesn't know what LPSTR is and has its own set
364 if (DosSetCurrentDir((PSZ
)newdrive
))
372 bool wxIsDriveAvailable(const wxString
& dirName
)
375 UINT errorMode
= SetErrorMode(SEM_FAILCRITICALERRORS
| SEM_NOOPENFILEERRORBOX
);
379 // Check if this is a root directory and if so,
380 // whether the drive is available.
381 if (dirName
.Len() == 3 && dirName
[(size_t)1] == wxT(':'))
383 wxString
dirNameLower(dirName
.Lower());
384 #if defined(__WXWINE__) || (defined(__GNUWIN32__) && \
385 !(defined(__MINGW32_MAJOR_VERSION) && __MINGW32_MAJOR_VERSION >= 1))
386 success
= wxPathExists(dirNameLower
);
388 int currentDrive
= _getdrive();
389 int thisDrive
= (int) (dirNameLower
[(size_t)0] - 'a' + 1) ;
390 int err
= setdrive( thisDrive
) ;
391 setdrive( currentDrive
);
400 (void) SetErrorMode(errorMode
);
405 #endif // __WINDOWS__ || __WXPM__
407 // Function which is called by quick sort. We want to override the default wxArrayString behaviour,
408 // and sort regardless of case.
409 static int LINKAGEMODE
wxDirCtrlStringCompareFunction(const void *first
, const void *second
)
411 wxString
*strFirst
= (wxString
*)first
;
412 wxString
*strSecond
= (wxString
*)second
;
414 return strFirst
->CmpNoCase(*strSecond
);
417 //-----------------------------------------------------------------------------
419 //-----------------------------------------------------------------------------
421 wxDirItemData::wxDirItemData(const wxString
& path
, const wxString
& name
,
426 /* Insert logic to detect hidden files here
427 * In UnixLand we just check whether the first char is a dot
428 * For FileNameFromPath read LastDirNameInThisPath ;-) */
429 // m_isHidden = (bool)(wxFileNameFromPath(*m_path)[0] == '.');
431 m_isExpanded
= FALSE
;
435 wxDirItemData::~wxDirItemData()
439 void wxDirItemData::SetNewDirName(const wxString
& path
)
442 m_name
= wxFileNameFromPath(path
);
445 bool wxDirItemData::HasSubDirs() const
447 if (m_path
.IsEmpty())
453 if ( !dir
.Open(m_path
) )
457 return dir
.HasSubDirs();
460 bool wxDirItemData::HasFiles(const wxString
& WXUNUSED(spec
)) const
462 if (m_path
.IsEmpty())
468 if ( !dir
.Open(m_path
) )
472 return dir
.HasFiles();
475 //-----------------------------------------------------------------------------
477 //-----------------------------------------------------------------------------
479 IMPLEMENT_DYNAMIC_CLASS(wxGenericDirCtrl
, wxControl
)
481 BEGIN_EVENT_TABLE(wxGenericDirCtrl
, wxControl
)
482 EVT_TREE_ITEM_EXPANDING (-1, wxGenericDirCtrl::OnExpandItem
)
483 EVT_TREE_ITEM_COLLAPSED (-1, wxGenericDirCtrl::OnCollapseItem
)
484 EVT_TREE_BEGIN_LABEL_EDIT (-1, wxGenericDirCtrl::OnBeginEditItem
)
485 EVT_TREE_END_LABEL_EDIT (-1, wxGenericDirCtrl::OnEndEditItem
)
486 EVT_SIZE (wxGenericDirCtrl::OnSize
)
489 wxGenericDirCtrl::wxGenericDirCtrl(void)
494 bool wxGenericDirCtrl::Create(wxWindow
*parent
,
500 const wxString
& filter
,
502 const wxString
& name
)
504 if (!wxControl::Create(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
507 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));
511 long treeStyle
= wxTR_HAS_BUTTONS
| wxTR_HIDE_ROOT
;
513 if (style
& wxDIRCTRL_EDIT_LABELS
)
514 treeStyle
|= wxTR_EDIT_LABELS
;
516 if ((style
& wxDIRCTRL_3D_INTERNAL
) == 0)
517 treeStyle
|= wxNO_BORDER
;
519 long filterStyle
= 0;
520 if ((style
& wxDIRCTRL_3D_INTERNAL
) == 0)
521 filterStyle
|= wxNO_BORDER
;
523 m_treeCtrl
= new wxTreeCtrl(this, wxID_TREECTRL
, pos
, size
, treeStyle
);
525 if (!filter
.IsEmpty() && (style
& wxDIRCTRL_SHOW_FILTERS
))
526 m_filterListCtrl
= new wxDirFilterListCtrl(this, wxID_FILTERLISTCTRL
, wxDefaultPosition
, wxDefaultSize
, filterStyle
);
531 SetFilterIndex(defaultFilter
);
533 if (m_filterListCtrl
)
534 m_filterListCtrl
->FillFilterList(filter
, defaultFilter
);
536 m_imageList
= new wxImageList(16, 16, TRUE
);
537 m_imageList
->Add(wxIcon(icon1_xpm
));
538 m_imageList
->Add(wxIcon(icon2_xpm
));
539 m_imageList
->Add(wxIcon(icon3_xpm
));
540 m_imageList
->Add(wxIcon(icon4_xpm
));
541 m_imageList
->Add(wxIcon(icon5_xpm
));
542 m_imageList
->Add(wxIcon(icon6_xpm
));
543 m_imageList
->Add(wxIcon(icon7_xpm
));
544 m_imageList
->Add(wxIcon(icon8_xpm
));
545 m_treeCtrl
->AssignImageList(m_imageList
);
547 m_showHidden
= FALSE
;
548 wxDirItemData
* rootData
= new wxDirItemData(wxT(""), wxT(""), TRUE
);
552 #if defined(__WINDOWS__) || defined(__WXPM__) || defined(__DOS__)
553 rootName
= _("Computer");
555 rootName
= _("Sections");
558 m_rootId
= m_treeCtrl
->AddRoot( rootName
, 3, -1, rootData
);
559 m_treeCtrl
->SetItemHasChildren(m_rootId
);
560 ExpandDir(m_rootId
); // automatically expand first level
562 // Expand and select the default path
563 if (!m_defaultPath
.IsEmpty())
564 ExpandPath(m_defaultPath
);
571 wxGenericDirCtrl::~wxGenericDirCtrl()
575 void wxGenericDirCtrl::Init()
577 m_showHidden
= FALSE
;
580 m_currentFilterStr
= wxEmptyString
; // Default: any file
582 m_filterListCtrl
= NULL
;
585 void wxGenericDirCtrl::ShowHidden( bool show
)
589 wxString path
= GetPath();
594 void wxGenericDirCtrl::AddSection(const wxString
& path
, const wxString
& name
, int imageId
)
596 wxDirItemData
*dir_item
= new wxDirItemData(path
,name
,TRUE
);
598 wxTreeItemId id
= m_treeCtrl
->AppendItem( m_rootId
, name
, imageId
, -1, dir_item
);
600 m_treeCtrl
->SetItemHasChildren(id
);
603 void wxGenericDirCtrl::SetupSections()
605 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXPM__)
608 wxChar driveBuffer
[256];
609 size_t n
= (size_t) GetLogicalDriveStrings(255, driveBuffer
);
614 path
.Printf(wxT("%c:\\"), driveBuffer
[i
]);
615 name
.Printf(wxT("(%c:)"), driveBuffer
[i
]);
618 int driveType
= ::GetDriveType(path
);
621 case DRIVE_REMOVABLE
:
622 if (path
== wxT("a:\\") || path
== wxT("b:\\"))
623 imageId
= 6; // Floppy
641 AddSection(path
, name
, imageId
);
643 while (driveBuffer
[i
] != wxT('\0'))
646 if (driveBuffer
[i
] == wxT('\0'))
652 /* If we can switch to the drive, it exists. */
653 for( drive
= 1; drive
<= 26; drive
++ )
656 path
.Printf(wxT("%c:\\"), (char) (drive
+ 'a' - 1));
657 name
.Printf(wxT("(%c:)"), (char) (drive
+ 'A' - 1));
659 if (wxIsDriveAvailable(path
))
661 AddSection(path
, name
, (drive
<= 2) ? 6/*floppy*/ : 4/*disk*/);
664 #endif // __WIN32__/!__WIN32__
666 #elif defined(__WXMAC__)
669 ItemCount theVolCount
;
670 char thePath
[FILENAME_MAX
];
672 if (FSGetMountedVolumes(&theVolRefs
, &theVolCount
) == noErr
) {
674 ::HLock( (Handle
)theVolRefs
) ;
675 for (index
= 0; index
< theVolCount
; ++index
) {
676 // get the POSIX path associated with the FSRef
677 if ( FSRefMakePath(&((*theVolRefs
)[index
]),
678 (UInt8
*)thePath
, sizeof(thePath
)) != noErr
) {
681 // add path separator at end if necessary
682 wxString
path( thePath
) ;
683 if (path
.Last() != wxFILE_SEP_PATH
) {
684 path
+= wxFILE_SEP_PATH
;
686 // get Mac volume name for display
687 FSVolumeRefNum vRefNum
;
688 HFSUniStr255 volumeName
;
690 if ( FSGetVRefNum(&((*theVolRefs
)[index
]), &vRefNum
) != noErr
) {
693 if ( FSGetVInfo(vRefNum
, &volumeName
, NULL
, NULL
) != noErr
) {
696 // get C string from Unicode HFS name
697 // see: http://developer.apple.com/carbon/tipsandtricks.html
698 CFStringRef cfstr
= CFStringCreateWithCharacters( kCFAllocatorDefault
,
701 // Do something with str
702 char *cstr
= NewPtr(CFStringGetLength(cfstr
) + 1);
703 if (( cstr
== NULL
) ||
704 !CFStringGetCString(cfstr
, cstr
, CFStringGetLength(cfstr
) + 1,
705 kCFStringEncodingMacRoman
)) {
709 wxString
name( cstr
) ;
713 GetVolParmsInfoBuffer volParmsInfo
;
715 if ( FSGetVolParms(vRefNum
, sizeof(volParmsInfo
), &volParmsInfo
, &actualSize
) != noErr
) {
719 if ( VolIsEjectable(&volParmsInfo
) ) {
720 AddSection(path
, name
, 5/*cd-rom*/);
723 AddSection(path
, name
, 4/*disk*/);
726 ::HUnlock( (Handle
)theVolRefs
) ;
727 ::DisposeHandle( (Handle
)theVolRefs
) ;
733 short actualCount
= 0 ;
734 if ( OnLine( &volume
, 1 , &actualCount
, &index
) != noErr
|| actualCount
== 0 ) {
738 wxString name
= wxMacFSSpec2MacFilename( &volume
) ;
739 AddSection(name
+ wxFILE_SEP_PATH
, name
, 4/*disk*/);
741 # endif /* __DARWIN__ */
742 #elif defined(__UNIX__)
743 AddSection(wxT("/"), wxT("/"), 3/*computer icon*/);
745 #error "Unsupported platform in wxGenericDirCtrl!"
749 void wxGenericDirCtrl::OnBeginEditItem(wxTreeEvent
&event
)
751 // don't rename the main entry "Sections"
752 if (event
.GetItem() == m_rootId
)
758 // don't rename the individual sections
759 if (m_treeCtrl
->GetItemParent( event
.GetItem() ) == m_rootId
)
766 void wxGenericDirCtrl::OnEndEditItem(wxTreeEvent
&event
)
768 if ((event
.GetLabel().IsEmpty()) ||
769 (event
.GetLabel() == _(".")) ||
770 (event
.GetLabel() == _("..")) ||
771 (event
.GetLabel().First( wxT("/") ) != wxNOT_FOUND
))
773 wxMessageDialog
dialog(this, _("Illegal directory name."), _("Error"), wxOK
| wxICON_ERROR
);
779 wxTreeItemId id
= event
.GetItem();
780 wxDirItemData
*data
= (wxDirItemData
*)m_treeCtrl
->GetItemData( id
);
783 wxString
new_name( wxPathOnly( data
->m_path
) );
784 new_name
+= wxString(wxFILE_SEP_PATH
);
785 new_name
+= event
.GetLabel();
789 if (wxFileExists(new_name
))
791 wxMessageDialog
dialog(this, _("File name exists already."), _("Error"), wxOK
| wxICON_ERROR
);
796 if (wxRenameFile(data
->m_path
,new_name
))
798 data
->SetNewDirName( new_name
);
802 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
808 void wxGenericDirCtrl::OnExpandItem(wxTreeEvent
&event
)
810 wxTreeItemId parentId
= event
.GetItem();
812 // VS: this is needed because the event handler is called from wxTreeCtrl
813 // ctor when wxTR_HIDE_ROOT was specified
815 m_rootId
= m_treeCtrl
->GetRootItem();
820 void wxGenericDirCtrl::OnCollapseItem(wxTreeEvent
&event
)
822 CollapseDir(event
.GetItem());
825 void wxGenericDirCtrl::CollapseDir(wxTreeItemId parentId
)
829 wxDirItemData
*data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(parentId
);
830 if (!data
->m_isExpanded
)
833 data
->m_isExpanded
= FALSE
;
835 /* Workaround because DeleteChildren has disapeared (why?) and
836 * CollapseAndReset doesn't work as advertised (deletes parent too) */
837 child
= m_treeCtrl
->GetFirstChild(parentId
, cookie
);
840 m_treeCtrl
->Delete(child
);
841 /* Not GetNextChild below, because the cookie mechanism can't
842 * handle disappearing children! */
843 child
= m_treeCtrl
->GetFirstChild(parentId
, cookie
);
847 void wxGenericDirCtrl::ExpandDir(wxTreeItemId parentId
)
849 wxDirItemData
*data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(parentId
);
851 if (data
->m_isExpanded
)
854 data
->m_isExpanded
= TRUE
;
856 if (parentId
== m_treeCtrl
->GetRootItem())
864 wxString search
,path
,filename
;
866 wxString
dirName(data
->m_path
);
868 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXPM__)
869 // Check if this is a root directory and if so,
870 // whether the drive is avaiable.
871 if (!wxIsDriveAvailable(dirName
))
873 data
->m_isExpanded
= FALSE
;
874 //wxMessageBox(wxT("Sorry, this drive is not available."));
879 // This may take a longish time. Go to busy cursor
882 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXPM__)
883 if (dirName
.Last() == ':')
884 dirName
+= wxString(wxFILE_SEP_PATH
);
888 wxArrayString filenames
;
891 wxString eachFilename
;
898 int style
= wxDIR_DIRS
;
899 if (m_showHidden
) style
|= wxDIR_HIDDEN
;
900 if (d
.GetFirst(& eachFilename
, wxEmptyString
, style
))
904 if ((eachFilename
!= wxT(".")) && (eachFilename
!= wxT("..")))
906 dirs
.Add(eachFilename
);
909 while (d
.GetNext(& eachFilename
));
912 dirs
.Sort((wxArrayString::CompareFunction
) wxDirCtrlStringCompareFunction
);
914 // Now do the filenames -- but only if we're allowed to
915 if ((GetWindowStyle() & wxDIRCTRL_DIR_ONLY
) == 0)
923 if (d
.GetFirst(& eachFilename
, m_currentFilterStr
, wxDIR_FILES
))
927 if ((eachFilename
!= wxT(".")) && (eachFilename
!= wxT("..")))
929 filenames
.Add(eachFilename
);
932 while (d
.GetNext(& eachFilename
));
935 filenames
.Sort((wxArrayString::CompareFunction
) wxDirCtrlStringCompareFunction
);
938 // Add the sorted dirs
940 for (i
= 0; i
< dirs
.Count(); i
++)
942 wxString
eachFilename(dirs
[i
]);
944 if (path
.Last() != wxFILE_SEP_PATH
)
945 path
+= wxString(wxFILE_SEP_PATH
);
946 path
+= eachFilename
;
948 wxDirItemData
*dir_item
= new wxDirItemData(path
,eachFilename
,TRUE
);
949 wxTreeItemId id
= m_treeCtrl
->AppendItem( parentId
, eachFilename
, 0, -1, dir_item
);
950 m_treeCtrl
->SetItemImage( id
, 1, wxTreeItemIcon_Expanded
);
952 // Has this got any children? If so, make it expandable.
953 // (There are two situations when a dir has children: either it
954 // has subdirectories or it contains files that weren't filtered
955 // out. The latter only applies to dirctrl with files.)
956 if ( dir_item
->HasSubDirs() ||
957 (((GetWindowStyle() & wxDIRCTRL_DIR_ONLY
) == 0) &&
958 dir_item
->HasFiles(m_currentFilterStr
)) )
960 m_treeCtrl
->SetItemHasChildren(id
);
964 // Add the sorted filenames
965 if ((GetWindowStyle() & wxDIRCTRL_DIR_ONLY
) == 0)
967 for (i
= 0; i
< filenames
.Count(); i
++)
969 wxString
eachFilename(filenames
[i
]);
971 if (path
.Last() != wxFILE_SEP_PATH
)
972 path
+= wxString(wxFILE_SEP_PATH
);
973 path
+= eachFilename
;
974 //path = dirName + wxString(wxT("/")) + eachFilename;
975 wxDirItemData
*dir_item
= new wxDirItemData(path
,eachFilename
,FALSE
);
976 (void)m_treeCtrl
->AppendItem( parentId
, eachFilename
, 2, -1, dir_item
);
981 void wxGenericDirCtrl::ReCreateTree()
983 CollapseDir(m_treeCtrl
->GetRootItem());
984 ExpandDir(m_treeCtrl
->GetRootItem());
987 // Find the child that matches the first part of 'path'.
988 // E.g. if a child path is "/usr" and 'path' is "/usr/include"
989 // then the child for /usr is returned.
990 wxTreeItemId
wxGenericDirCtrl::FindChild(wxTreeItemId parentId
, const wxString
& path
, bool& done
)
992 wxString
path2(path
);
994 // Make sure all separators are as per the current platform
995 path2
.Replace(wxT("\\"), wxString(wxFILE_SEP_PATH
));
996 path2
.Replace(wxT("/"), wxString(wxFILE_SEP_PATH
));
998 // Append a separator to foil bogus substring matching
999 path2
+= wxString(wxFILE_SEP_PATH
);
1001 // In MSW or PM, case is not significant
1002 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXPM__)
1007 wxTreeItemId childId
= m_treeCtrl
->GetFirstChild(parentId
, cookie
);
1008 while (childId
.IsOk())
1010 wxDirItemData
* data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(childId
);
1012 if (data
&& !data
->m_path
.IsEmpty())
1014 wxString
childPath(data
->m_path
);
1015 if (childPath
.Last() != wxFILE_SEP_PATH
)
1016 childPath
+= wxString(wxFILE_SEP_PATH
);
1018 // In MSW and PM, case is not significant
1019 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXPM__)
1020 childPath
.MakeLower();
1023 if (childPath
.Len() <= path2
.Len())
1025 wxString path3
= path2
.Mid(0, childPath
.Len());
1026 if (childPath
== path3
)
1028 if (path3
.Len() == path2
.Len())
1037 childId
= m_treeCtrl
->GetNextChild(parentId
, cookie
);
1039 wxTreeItemId invalid
;
1043 // Try to expand as much of the given path as possible,
1044 // and select the given tree item.
1045 bool wxGenericDirCtrl::ExpandPath(const wxString
& path
)
1048 wxTreeItemId id
= FindChild(m_rootId
, path
, done
);
1049 wxTreeItemId lastId
= id
; // The last non-zero id
1050 while (id
.IsOk() && !done
)
1054 id
= FindChild(id
, path
, done
);
1060 wxDirItemData
*data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(lastId
);
1063 m_treeCtrl
->Expand(lastId
);
1065 if ((GetWindowStyle() & wxDIRCTRL_SELECT_FIRST
) && data
->m_isDir
)
1067 // Find the first file in this directory
1069 wxTreeItemId childId
= m_treeCtrl
->GetFirstChild(lastId
, cookie
);
1070 bool selectedChild
= FALSE
;
1071 while (childId
.IsOk())
1073 wxDirItemData
* data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(childId
);
1075 if (data
&& data
->m_path
!= wxT("") && !data
->m_isDir
)
1077 m_treeCtrl
->SelectItem(childId
);
1078 m_treeCtrl
->EnsureVisible(childId
);
1079 selectedChild
= TRUE
;
1082 childId
= m_treeCtrl
->GetNextChild(lastId
, cookie
);
1086 m_treeCtrl
->SelectItem(lastId
);
1087 m_treeCtrl
->EnsureVisible(lastId
);
1092 m_treeCtrl
->SelectItem(lastId
);
1093 m_treeCtrl
->EnsureVisible(lastId
);
1102 wxString
wxGenericDirCtrl::GetPath() const
1104 wxTreeItemId id
= m_treeCtrl
->GetSelection();
1107 wxDirItemData
* data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(id
);
1108 return data
->m_path
;
1111 return wxEmptyString
;
1114 wxString
wxGenericDirCtrl::GetFilePath() const
1116 wxTreeItemId id
= m_treeCtrl
->GetSelection();
1119 wxDirItemData
* data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(id
);
1121 return wxEmptyString
;
1123 return data
->m_path
;
1126 return wxEmptyString
;
1129 void wxGenericDirCtrl::SetPath(const wxString
& path
)
1131 m_defaultPath
= path
;
1138 void wxGenericDirCtrl::FindChildFiles(wxTreeItemId id
, int dirFlags
, wxArrayString
& filenames
)
1140 wxDirItemData
*data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(id
);
1142 // This may take a longish time. Go to busy cursor
1147 wxString search
,path
,filename
;
1149 wxString
dirName(data
->m_path
);
1151 #if defined(__WXMSW__) || defined(__WXPM__)
1152 if (dirName
.Last() == ':')
1153 dirName
+= wxString(wxFILE_SEP_PATH
);
1157 wxString eachFilename
;
1164 if (d
.GetFirst(& eachFilename
, m_currentFilterStr
, dirFlags
))
1168 if ((eachFilename
!= wxT(".")) && (eachFilename
!= wxT("..")))
1170 filenames
.Add(eachFilename
);
1173 while (d
.GetNext(& eachFilename
)) ;
1179 void wxGenericDirCtrl::SetFilterIndex(int n
)
1181 m_currentFilter
= n
;
1184 if (ExtractWildcard(m_filter
, n
, f
, d
))
1185 m_currentFilterStr
= f
;
1187 m_currentFilterStr
= wxT("*.*");
1190 void wxGenericDirCtrl::SetFilter(const wxString
& filter
)
1195 if (ExtractWildcard(m_filter
, m_currentFilter
, f
, d
))
1196 m_currentFilterStr
= f
;
1198 m_currentFilterStr
= wxT("*.*");
1201 // Extract description and actual filter from overall filter string
1202 bool wxGenericDirCtrl::ExtractWildcard(const wxString
& filterStr
, int n
, wxString
& filter
, wxString
& description
)
1204 wxArrayString filters
, descriptions
;
1205 int count
= ParseFilter(filterStr
, filters
, descriptions
);
1206 if (count
> 0 && n
< count
)
1208 filter
= filters
[n
];
1209 description
= descriptions
[n
];
1216 // Parses the global filter, returning the number of filters.
1217 // Returns 0 if none or if there's a problem.
1218 // filterStr is in the form:
1220 // "All files (*.*)|*.*|JPEG Files (*.jpeg)|*.jpg"
1222 int wxGenericDirCtrl::ParseFilter(const wxString
& filterStr
, wxArrayString
& filters
, wxArrayString
& descriptions
)
1224 wxString
str(filterStr
);
1226 wxString description
, filter
;
1228 bool finished
= FALSE
;
1231 pos
= str
.Find(wxT('|'));
1233 return 0; // Problem
1234 description
= str
.Left(pos
);
1235 str
= str
.Mid(pos
+1);
1236 pos
= str
.Find(wxT('|'));
1244 filter
= str
.Left(pos
);
1245 str
= str
.Mid(pos
+1);
1247 descriptions
.Add(description
);
1248 filters
.Add(filter
);
1252 return filters
.Count();
1255 void wxGenericDirCtrl::DoResize()
1257 wxSize sz
= GetClientSize();
1258 int verticalSpacing
= 3;
1262 if (m_filterListCtrl
)
1265 // For some reason, this is required in order for the
1266 // correct control height to always be returned, rather
1267 // than the drop-down list height which is sometimes returned.
1268 wxSize oldSize
= m_filterListCtrl
->GetSize();
1269 m_filterListCtrl
->SetSize(-1, -1, oldSize
.x
+10, -1, wxSIZE_USE_EXISTING
);
1270 m_filterListCtrl
->SetSize(-1, -1, oldSize
.x
, -1, wxSIZE_USE_EXISTING
);
1272 filterSz
= m_filterListCtrl
->GetSize();
1273 sz
.y
-= (filterSz
.y
+ verticalSpacing
);
1275 m_treeCtrl
->SetSize(0, 0, sz
.x
, sz
.y
);
1276 if (m_filterListCtrl
)
1278 m_filterListCtrl
->SetSize(0, sz
.y
+ verticalSpacing
, sz
.x
, filterSz
.y
);
1279 // Don't know why, but this needs refreshing after a resize (wxMSW)
1280 m_filterListCtrl
->Refresh();
1286 void wxGenericDirCtrl::OnSize(wxSizeEvent
& WXUNUSED(event
))
1291 //-----------------------------------------------------------------------------
1292 // wxDirFilterListCtrl
1293 //-----------------------------------------------------------------------------
1295 IMPLEMENT_CLASS(wxDirFilterListCtrl
, wxChoice
)
1297 BEGIN_EVENT_TABLE(wxDirFilterListCtrl
, wxChoice
)
1298 EVT_CHOICE(-1, wxDirFilterListCtrl::OnSelFilter
)
1301 bool wxDirFilterListCtrl::Create(wxGenericDirCtrl
* parent
, const wxWindowID id
,
1307 return wxChoice::Create(parent
, id
, pos
, size
, 0, NULL
, style
);
1310 void wxDirFilterListCtrl::Init()
1315 void wxDirFilterListCtrl::OnSelFilter(wxCommandEvent
& WXUNUSED(event
))
1317 int sel
= GetSelection();
1319 wxString currentPath
= m_dirCtrl
->GetPath();
1321 m_dirCtrl
->SetFilterIndex(sel
);
1323 // If the filter has changed, the view is out of date, so
1324 // collapse the tree.
1325 m_dirCtrl
->ReCreateTree();
1327 // Try to restore the selection, or at least the directory
1328 m_dirCtrl
->ExpandPath(currentPath
);
1331 void wxDirFilterListCtrl::FillFilterList(const wxString
& filter
, int defaultFilter
)
1334 wxArrayString descriptions
, filters
;
1335 size_t n
= (size_t) m_dirCtrl
->ParseFilter(filter
, filters
, descriptions
);
1337 if (n
> 0 && defaultFilter
< (int) n
)
1340 for (i
= 0; i
< n
; i
++)
1341 Append(descriptions
[i
]);
1342 SetSelection(defaultFilter
);
1346 #endif // wxUSE_DIRDLG