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__) || (defined(__MINGW32_MAJOR_VERSION) && __MINGW32_MAJOR_VERSION >= 1)
80 #if defined(__WXMAC__)
82 # include "MoreFilesX.h"
84 # include "MoreFilesExtras.h"
92 // If compiled under Windows, this macro can cause problems
98 static const char * icon1_xpm
[] = {
99 /* width height ncolors chars_per_pixel */
127 static const char * icon2_xpm
[] = {
128 /* width height ncolors chars_per_pixel */
156 static const char * icon3_xpm
[] = {
157 /* width height ncolors chars_per_pixel */
182 static const char * icon4_xpm
[] = {
209 static const char * icon5_xpm
[] = {
236 static const char *icon6_xpm
[] = {
266 static const char * icon7_xpm
[] = {
293 static const char * icon8_xpm
[] = {
322 bool wxIsDriveAvailable(const wxString
& dirName
)
324 // FIXME_MGL - this method leads to hang up under Watcom for some reason
326 if ( dirName
.Len() == 3 && dirName
[1u] == wxT(':') )
328 wxString
dirNameLower(dirName
.Lower());
329 // VS: always return TRUE for removable media, since Win95 doesn't
330 // like it when MS-DOS app accesses empty floppy drive
331 return (dirNameLower
[0u] == wxT('a') ||
332 dirNameLower
[0u] == wxT('b') ||
333 wxPathExists(dirNameLower
));
340 #elif defined(__WINDOWS__) || defined(__WXPM__)
342 int setdrive(int drive
)
344 #if defined(__GNUWIN32__) && \
345 (defined(__MINGW32_MAJOR_VERSION) && __MINGW32_MAJOR_VERSION >= 1)
346 return _chdrive(drive
);
350 if (drive
< 1 || drive
> 31)
352 newdrive
[0] = (wxChar
)(wxT('A') + drive
- 1);
353 newdrive
[1] = wxT(':');
354 newdrive
[2] = wxT('\0');
355 #if defined(__WXMSW__)
357 if (wxSetWorkingDirectory(newdrive
))
359 if (::SetCurrentDirectory(newdrive
))
362 // VA doesn't know what LPSTR is and has its own set
363 if (DosSetCurrentDir((PSZ
)newdrive
))
371 bool wxIsDriveAvailable(const wxString
& dirName
)
374 UINT errorMode
= SetErrorMode(SEM_FAILCRITICALERRORS
| SEM_NOOPENFILEERRORBOX
);
378 // Check if this is a root directory and if so,
379 // whether the drive is available.
380 if (dirName
.Len() == 3 && dirName
[(size_t)1] == wxT(':'))
382 wxString
dirNameLower(dirName
.Lower());
383 #if defined(__GNUWIN32__) && !(defined(__MINGW32_MAJOR_VERSION) && __MINGW32_MAJOR_VERSION >= 1)
384 success
= wxPathExists(dirNameLower
);
386 int currentDrive
= _getdrive();
387 int thisDrive
= (int) (dirNameLower
[(size_t)0] - 'a' + 1) ;
388 int err
= setdrive( thisDrive
) ;
389 setdrive( currentDrive
);
398 (void) SetErrorMode(errorMode
);
403 #endif // __WINDOWS__ || __WXPM__
405 // Function which is called by quick sort. We want to override the default wxArrayString behaviour,
406 // and sort regardless of case.
407 static int LINKAGEMODE
wxDirCtrlStringCompareFunction(const void *first
, const void *second
)
409 wxString
*strFirst
= (wxString
*)first
;
410 wxString
*strSecond
= (wxString
*)second
;
412 return strFirst
->CmpNoCase(*strSecond
);
415 //-----------------------------------------------------------------------------
417 //-----------------------------------------------------------------------------
419 wxDirItemData::wxDirItemData(const wxString
& path
, const wxString
& name
,
424 /* Insert logic to detect hidden files here
425 * In UnixLand we just check whether the first char is a dot
426 * For FileNameFromPath read LastDirNameInThisPath ;-) */
427 // m_isHidden = (bool)(wxFileNameFromPath(*m_path)[0] == '.');
429 m_isExpanded
= FALSE
;
433 wxDirItemData::~wxDirItemData()
437 void wxDirItemData::SetNewDirName(const wxString
& path
)
440 m_name
= wxFileNameFromPath(path
);
443 bool wxDirItemData::HasSubDirs() const
445 if (m_path
.IsEmpty())
451 if ( !dir
.Open(m_path
) )
455 return dir
.HasSubDirs();
458 bool wxDirItemData::HasFiles(const wxString
& WXUNUSED(spec
)) const
460 if (m_path
.IsEmpty())
466 if ( !dir
.Open(m_path
) )
470 return dir
.HasFiles();
473 //-----------------------------------------------------------------------------
475 //-----------------------------------------------------------------------------
477 IMPLEMENT_DYNAMIC_CLASS(wxGenericDirCtrl
, wxControl
)
479 BEGIN_EVENT_TABLE(wxGenericDirCtrl
, wxControl
)
480 EVT_TREE_ITEM_EXPANDING (-1, wxGenericDirCtrl::OnExpandItem
)
481 EVT_TREE_ITEM_COLLAPSED (-1, wxGenericDirCtrl::OnCollapseItem
)
482 EVT_TREE_BEGIN_LABEL_EDIT (-1, wxGenericDirCtrl::OnBeginEditItem
)
483 EVT_TREE_END_LABEL_EDIT (-1, wxGenericDirCtrl::OnEndEditItem
)
484 EVT_SIZE (wxGenericDirCtrl::OnSize
)
487 wxGenericDirCtrl::wxGenericDirCtrl(void)
492 bool wxGenericDirCtrl::Create(wxWindow
*parent
,
498 const wxString
& filter
,
500 const wxString
& name
)
502 if (!wxControl::Create(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
505 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));
509 long treeStyle
= wxTR_HAS_BUTTONS
| wxTR_HIDE_ROOT
;
511 if (style
& wxDIRCTRL_EDIT_LABELS
)
512 treeStyle
|= wxTR_EDIT_LABELS
;
514 if ((style
& wxDIRCTRL_3D_INTERNAL
) == 0)
515 treeStyle
|= wxNO_BORDER
;
517 long filterStyle
= 0;
518 if ((style
& wxDIRCTRL_3D_INTERNAL
) == 0)
519 filterStyle
|= wxNO_BORDER
;
521 m_treeCtrl
= new wxTreeCtrl(this, wxID_TREECTRL
, pos
, size
, treeStyle
);
523 if (!filter
.IsEmpty() && (style
& wxDIRCTRL_SHOW_FILTERS
))
524 m_filterListCtrl
= new wxDirFilterListCtrl(this, wxID_FILTERLISTCTRL
, wxDefaultPosition
, wxDefaultSize
, filterStyle
);
529 SetFilterIndex(defaultFilter
);
531 if (m_filterListCtrl
)
532 m_filterListCtrl
->FillFilterList(filter
, defaultFilter
);
534 m_imageList
= new wxImageList(16, 16, TRUE
);
535 m_imageList
->Add(wxIcon(icon1_xpm
));
536 m_imageList
->Add(wxIcon(icon2_xpm
));
537 m_imageList
->Add(wxIcon(icon3_xpm
));
538 m_imageList
->Add(wxIcon(icon4_xpm
));
539 m_imageList
->Add(wxIcon(icon5_xpm
));
540 m_imageList
->Add(wxIcon(icon6_xpm
));
541 m_imageList
->Add(wxIcon(icon7_xpm
));
542 m_imageList
->Add(wxIcon(icon8_xpm
));
543 m_treeCtrl
->AssignImageList(m_imageList
);
545 m_showHidden
= FALSE
;
546 wxDirItemData
* rootData
= new wxDirItemData(wxT(""), wxT(""), TRUE
);
550 #if defined(__WINDOWS__) || defined(__WXPM__) || defined(__DOS__)
551 rootName
= _("Computer");
553 rootName
= _("Sections");
556 m_rootId
= m_treeCtrl
->AddRoot( rootName
, 3, -1, rootData
);
557 m_treeCtrl
->SetItemHasChildren(m_rootId
);
558 ExpandDir(m_rootId
); // automatically expand first level
560 // Expand and select the default path
561 if (!m_defaultPath
.IsEmpty())
562 ExpandPath(m_defaultPath
);
569 wxGenericDirCtrl::~wxGenericDirCtrl()
573 void wxGenericDirCtrl::Init()
575 m_showHidden
= FALSE
;
578 m_currentFilterStr
= wxEmptyString
; // Default: any file
580 m_filterListCtrl
= NULL
;
583 void wxGenericDirCtrl::ShowHidden( bool show
)
587 wxString path
= GetPath();
592 void wxGenericDirCtrl::AddSection(const wxString
& path
, const wxString
& name
, int imageId
)
594 wxDirItemData
*dir_item
= new wxDirItemData(path
,name
,TRUE
);
596 wxTreeItemId id
= m_treeCtrl
->AppendItem( m_rootId
, name
, imageId
, -1, dir_item
);
598 m_treeCtrl
->SetItemHasChildren(id
);
601 void wxGenericDirCtrl::SetupSections()
603 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXPM__)
606 wxChar driveBuffer
[256];
607 size_t n
= (size_t) GetLogicalDriveStrings(255, driveBuffer
);
612 path
.Printf(wxT("%c:\\"), driveBuffer
[i
]);
613 name
.Printf(wxT("(%c:)"), driveBuffer
[i
]);
616 int driveType
= ::GetDriveType(path
);
619 case DRIVE_REMOVABLE
:
620 if (path
== wxT("a:\\") || path
== wxT("b:\\"))
621 imageId
= 6; // Floppy
639 AddSection(path
, name
, imageId
);
641 while (driveBuffer
[i
] != wxT('\0'))
644 if (driveBuffer
[i
] == wxT('\0'))
650 /* If we can switch to the drive, it exists. */
651 for( drive
= 1; drive
<= 26; drive
++ )
654 path
.Printf(wxT("%c:\\"), (char) (drive
+ 'a' - 1));
655 name
.Printf(wxT("(%c:)"), (char) (drive
+ 'A' - 1));
657 if (wxIsDriveAvailable(path
))
659 AddSection(path
, name
, (drive
<= 2) ? 6/*floppy*/ : 4/*disk*/);
662 #endif // __WIN32__/!__WIN32__
664 #elif defined(__WXMAC__)
667 ItemCount theVolCount
;
668 char thePath
[FILENAME_MAX
];
670 if (FSGetMountedVolumes(&theVolRefs
, &theVolCount
) == noErr
) {
672 ::HLock( (Handle
)theVolRefs
) ;
673 for (index
= 0; index
< theVolCount
; ++index
) {
674 // get the POSIX path associated with the FSRef
675 if ( FSRefMakePath(&((*theVolRefs
)[index
]),
676 (UInt8
*)thePath
, sizeof(thePath
)) != noErr
) {
679 // add path separator at end if necessary
680 wxString
path( thePath
) ;
681 if (path
.Last() != wxFILE_SEP_PATH
) {
682 path
+= wxFILE_SEP_PATH
;
684 // get Mac volume name for display
685 FSVolumeRefNum vRefNum
;
686 HFSUniStr255 volumeName
;
688 if ( FSGetVRefNum(&((*theVolRefs
)[index
]), &vRefNum
) != noErr
) {
691 if ( FSGetVInfo(vRefNum
, &volumeName
, NULL
, NULL
) != noErr
) {
694 // get C string from Unicode HFS name
695 // see: http://developer.apple.com/carbon/tipsandtricks.html
696 CFStringRef cfstr
= CFStringCreateWithCharacters( kCFAllocatorDefault
,
699 // Do something with str
700 char *cstr
= NewPtr(CFStringGetLength(cfstr
) + 1);
701 if (( cstr
== NULL
) ||
702 !CFStringGetCString(cfstr
, cstr
, CFStringGetLength(cfstr
) + 1,
703 kCFStringEncodingMacRoman
)) {
707 wxString
name( cstr
) ;
711 GetVolParmsInfoBuffer volParmsInfo
;
713 if ( FSGetVolParms(vRefNum
, sizeof(volParmsInfo
), &volParmsInfo
, &actualSize
) != noErr
) {
717 if ( VolIsEjectable(&volParmsInfo
) ) {
718 AddSection(path
, name
, 5/*cd-rom*/);
721 AddSection(path
, name
, 4/*disk*/);
724 ::HUnlock( (Handle
)theVolRefs
) ;
725 ::DisposeHandle( (Handle
)theVolRefs
) ;
731 short actualCount
= 0 ;
732 if ( OnLine( &volume
, 1 , &actualCount
, &index
) != noErr
|| actualCount
== 0 ) {
736 wxString name
= wxMacFSSpec2MacFilename( &volume
) ;
737 AddSection(name
+ wxFILE_SEP_PATH
, name
, 4/*disk*/);
739 # endif /* __DARWIN__ */
740 #elif defined(__UNIX__)
741 AddSection(wxT("/"), wxT("/"), 3/*computer icon*/);
743 #error "Unsupported platform in wxGenericDirCtrl!"
747 void wxGenericDirCtrl::OnBeginEditItem(wxTreeEvent
&event
)
749 // don't rename the main entry "Sections"
750 if (event
.GetItem() == m_rootId
)
756 // don't rename the individual sections
757 if (m_treeCtrl
->GetItemParent( event
.GetItem() ) == m_rootId
)
764 void wxGenericDirCtrl::OnEndEditItem(wxTreeEvent
&event
)
766 if ((event
.GetLabel().IsEmpty()) ||
767 (event
.GetLabel() == _(".")) ||
768 (event
.GetLabel() == _("..")) ||
769 (event
.GetLabel().First( wxT("/") ) != wxNOT_FOUND
))
771 wxMessageDialog
dialog(this, _("Illegal directory name."), _("Error"), wxOK
| wxICON_ERROR
);
777 wxTreeItemId id
= event
.GetItem();
778 wxDirItemData
*data
= (wxDirItemData
*)m_treeCtrl
->GetItemData( id
);
781 wxString
new_name( wxPathOnly( data
->m_path
) );
782 new_name
+= wxString(wxFILE_SEP_PATH
);
783 new_name
+= event
.GetLabel();
787 if (wxFileExists(new_name
))
789 wxMessageDialog
dialog(this, _("File name exists already."), _("Error"), wxOK
| wxICON_ERROR
);
794 if (wxRenameFile(data
->m_path
,new_name
))
796 data
->SetNewDirName( new_name
);
800 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
806 void wxGenericDirCtrl::OnExpandItem(wxTreeEvent
&event
)
808 wxTreeItemId parentId
= event
.GetItem();
810 // VS: this is needed because the event handler is called from wxTreeCtrl
811 // ctor when wxTR_HIDE_ROOT was specified
813 m_rootId
= m_treeCtrl
->GetRootItem();
818 void wxGenericDirCtrl::OnCollapseItem(wxTreeEvent
&event
)
820 CollapseDir(event
.GetItem());
823 void wxGenericDirCtrl::CollapseDir(wxTreeItemId parentId
)
827 wxDirItemData
*data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(parentId
);
828 if (!data
->m_isExpanded
)
831 data
->m_isExpanded
= FALSE
;
833 /* Workaround because DeleteChildren has disapeared (why?) and
834 * CollapseAndReset doesn't work as advertised (deletes parent too) */
835 child
= m_treeCtrl
->GetFirstChild(parentId
, cookie
);
838 m_treeCtrl
->Delete(child
);
839 /* Not GetNextChild below, because the cookie mechanism can't
840 * handle disappearing children! */
841 child
= m_treeCtrl
->GetFirstChild(parentId
, cookie
);
845 void wxGenericDirCtrl::ExpandDir(wxTreeItemId parentId
)
847 wxDirItemData
*data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(parentId
);
849 if (data
->m_isExpanded
)
852 data
->m_isExpanded
= TRUE
;
854 if (parentId
== m_treeCtrl
->GetRootItem())
862 wxString search
,path
,filename
;
864 wxString
dirName(data
->m_path
);
866 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXPM__)
867 // Check if this is a root directory and if so,
868 // whether the drive is avaiable.
869 if (!wxIsDriveAvailable(dirName
))
871 data
->m_isExpanded
= FALSE
;
872 //wxMessageBox(wxT("Sorry, this drive is not available."));
877 // This may take a longish time. Go to busy cursor
880 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXPM__)
881 if (dirName
.Last() == ':')
882 dirName
+= wxString(wxFILE_SEP_PATH
);
886 wxArrayString filenames
;
889 wxString eachFilename
;
896 int style
= wxDIR_DIRS
;
897 if (m_showHidden
) style
|= wxDIR_HIDDEN
;
898 if (d
.GetFirst(& eachFilename
, wxEmptyString
, style
))
902 if ((eachFilename
!= wxT(".")) && (eachFilename
!= wxT("..")))
904 dirs
.Add(eachFilename
);
907 while (d
.GetNext(& eachFilename
));
910 dirs
.Sort((wxArrayString::CompareFunction
) wxDirCtrlStringCompareFunction
);
912 // Now do the filenames -- but only if we're allowed to
913 if ((GetWindowStyle() & wxDIRCTRL_DIR_ONLY
) == 0)
921 if (d
.GetFirst(& eachFilename
, m_currentFilterStr
, wxDIR_FILES
))
925 if ((eachFilename
!= wxT(".")) && (eachFilename
!= wxT("..")))
927 filenames
.Add(eachFilename
);
930 while (d
.GetNext(& eachFilename
));
933 filenames
.Sort((wxArrayString::CompareFunction
) wxDirCtrlStringCompareFunction
);
936 // Add the sorted dirs
938 for (i
= 0; i
< dirs
.Count(); i
++)
940 wxString
eachFilename(dirs
[i
]);
942 if (path
.Last() != wxFILE_SEP_PATH
)
943 path
+= wxString(wxFILE_SEP_PATH
);
944 path
+= eachFilename
;
946 wxDirItemData
*dir_item
= new wxDirItemData(path
,eachFilename
,TRUE
);
947 wxTreeItemId id
= m_treeCtrl
->AppendItem( parentId
, eachFilename
, 0, -1, dir_item
);
948 m_treeCtrl
->SetItemImage( id
, 1, wxTreeItemIcon_Expanded
);
950 // Has this got any children? If so, make it expandable.
951 // (There are two situations when a dir has children: either it
952 // has subdirectories or it contains files that weren't filtered
953 // out. The latter only applies to dirctrl with files.)
954 if ( dir_item
->HasSubDirs() ||
955 (((GetWindowStyle() & wxDIRCTRL_DIR_ONLY
) == 0) &&
956 dir_item
->HasFiles(m_currentFilterStr
)) )
958 m_treeCtrl
->SetItemHasChildren(id
);
962 // Add the sorted filenames
963 if ((GetWindowStyle() & wxDIRCTRL_DIR_ONLY
) == 0)
965 for (i
= 0; i
< filenames
.Count(); i
++)
967 wxString
eachFilename(filenames
[i
]);
969 if (path
.Last() != wxFILE_SEP_PATH
)
970 path
+= wxString(wxFILE_SEP_PATH
);
971 path
+= eachFilename
;
972 //path = dirName + wxString(wxT("/")) + eachFilename;
973 wxDirItemData
*dir_item
= new wxDirItemData(path
,eachFilename
,FALSE
);
974 (void)m_treeCtrl
->AppendItem( parentId
, eachFilename
, 2, -1, dir_item
);
979 void wxGenericDirCtrl::ReCreateTree()
981 CollapseDir(m_treeCtrl
->GetRootItem());
982 ExpandDir(m_treeCtrl
->GetRootItem());
985 // Find the child that matches the first part of 'path'.
986 // E.g. if a child path is "/usr" and 'path' is "/usr/include"
987 // then the child for /usr is returned.
988 wxTreeItemId
wxGenericDirCtrl::FindChild(wxTreeItemId parentId
, const wxString
& path
, bool& done
)
990 wxString
path2(path
);
992 // Make sure all separators are as per the current platform
993 path2
.Replace(wxT("\\"), wxString(wxFILE_SEP_PATH
));
994 path2
.Replace(wxT("/"), wxString(wxFILE_SEP_PATH
));
996 // Append a separator to foil bogus substring matching
997 path2
+= wxString(wxFILE_SEP_PATH
);
999 // In MSW or PM, case is not significant
1000 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXPM__)
1005 wxTreeItemId childId
= m_treeCtrl
->GetFirstChild(parentId
, cookie
);
1006 while (childId
.IsOk())
1008 wxDirItemData
* data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(childId
);
1010 if (data
&& !data
->m_path
.IsEmpty())
1012 wxString
childPath(data
->m_path
);
1013 if (childPath
.Last() != wxFILE_SEP_PATH
)
1014 childPath
+= wxString(wxFILE_SEP_PATH
);
1016 // In MSW and PM, case is not significant
1017 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXPM__)
1018 childPath
.MakeLower();
1021 if (childPath
.Len() <= path2
.Len())
1023 wxString path3
= path2
.Mid(0, childPath
.Len());
1024 if (childPath
== path3
)
1026 if (path3
.Len() == path2
.Len())
1035 childId
= m_treeCtrl
->GetNextChild(parentId
, cookie
);
1037 wxTreeItemId invalid
;
1041 // Try to expand as much of the given path as possible,
1042 // and select the given tree item.
1043 bool wxGenericDirCtrl::ExpandPath(const wxString
& path
)
1046 wxTreeItemId id
= FindChild(m_rootId
, path
, done
);
1047 wxTreeItemId lastId
= id
; // The last non-zero id
1048 while (id
.IsOk() && !done
)
1052 id
= FindChild(id
, path
, done
);
1058 wxDirItemData
*data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(lastId
);
1061 m_treeCtrl
->Expand(lastId
);
1063 if ((GetWindowStyle() & wxDIRCTRL_SELECT_FIRST
) && data
->m_isDir
)
1065 // Find the first file in this directory
1067 wxTreeItemId childId
= m_treeCtrl
->GetFirstChild(lastId
, cookie
);
1068 bool selectedChild
= FALSE
;
1069 while (childId
.IsOk())
1071 wxDirItemData
* data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(childId
);
1073 if (data
&& data
->m_path
!= wxT("") && !data
->m_isDir
)
1075 m_treeCtrl
->SelectItem(childId
);
1076 m_treeCtrl
->EnsureVisible(childId
);
1077 selectedChild
= TRUE
;
1080 childId
= m_treeCtrl
->GetNextChild(lastId
, cookie
);
1084 m_treeCtrl
->SelectItem(lastId
);
1085 m_treeCtrl
->EnsureVisible(lastId
);
1090 m_treeCtrl
->SelectItem(lastId
);
1091 m_treeCtrl
->EnsureVisible(lastId
);
1100 wxString
wxGenericDirCtrl::GetPath() const
1102 wxTreeItemId id
= m_treeCtrl
->GetSelection();
1105 wxDirItemData
* data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(id
);
1106 return data
->m_path
;
1109 return wxEmptyString
;
1112 wxString
wxGenericDirCtrl::GetFilePath() const
1114 wxTreeItemId id
= m_treeCtrl
->GetSelection();
1117 wxDirItemData
* data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(id
);
1119 return wxEmptyString
;
1121 return data
->m_path
;
1124 return wxEmptyString
;
1127 void wxGenericDirCtrl::SetPath(const wxString
& path
)
1129 m_defaultPath
= path
;
1136 void wxGenericDirCtrl::FindChildFiles(wxTreeItemId id
, int dirFlags
, wxArrayString
& filenames
)
1138 wxDirItemData
*data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(id
);
1140 // This may take a longish time. Go to busy cursor
1145 wxString search
,path
,filename
;
1147 wxString
dirName(data
->m_path
);
1149 #if defined(__WXMSW__) || defined(__WXPM__)
1150 if (dirName
.Last() == ':')
1151 dirName
+= wxString(wxFILE_SEP_PATH
);
1155 wxString eachFilename
;
1162 if (d
.GetFirst(& eachFilename
, m_currentFilterStr
, dirFlags
))
1166 if ((eachFilename
!= wxT(".")) && (eachFilename
!= wxT("..")))
1168 filenames
.Add(eachFilename
);
1171 while (d
.GetNext(& eachFilename
)) ;
1177 void wxGenericDirCtrl::SetFilterIndex(int n
)
1179 m_currentFilter
= n
;
1182 if (ExtractWildcard(m_filter
, n
, f
, d
))
1183 m_currentFilterStr
= f
;
1185 m_currentFilterStr
= wxT("*.*");
1188 void wxGenericDirCtrl::SetFilter(const wxString
& filter
)
1193 if (ExtractWildcard(m_filter
, m_currentFilter
, f
, d
))
1194 m_currentFilterStr
= f
;
1196 m_currentFilterStr
= wxT("*.*");
1199 // Extract description and actual filter from overall filter string
1200 bool wxGenericDirCtrl::ExtractWildcard(const wxString
& filterStr
, int n
, wxString
& filter
, wxString
& description
)
1202 wxArrayString filters
, descriptions
;
1203 int count
= ParseFilter(filterStr
, filters
, descriptions
);
1204 if (count
> 0 && n
< count
)
1206 filter
= filters
[n
];
1207 description
= descriptions
[n
];
1214 // Parses the global filter, returning the number of filters.
1215 // Returns 0 if none or if there's a problem.
1216 // filterStr is in the form:
1218 // "All files (*.*)|*.*|JPEG Files (*.jpeg)|*.jpg"
1220 int wxGenericDirCtrl::ParseFilter(const wxString
& filterStr
, wxArrayString
& filters
, wxArrayString
& descriptions
)
1222 wxString
str(filterStr
);
1224 wxString description
, filter
;
1226 bool finished
= FALSE
;
1229 pos
= str
.Find(wxT('|'));
1231 return 0; // Problem
1232 description
= str
.Left(pos
);
1233 str
= str
.Mid(pos
+1);
1234 pos
= str
.Find(wxT('|'));
1242 filter
= str
.Left(pos
);
1243 str
= str
.Mid(pos
+1);
1245 descriptions
.Add(description
);
1246 filters
.Add(filter
);
1250 return filters
.Count();
1253 void wxGenericDirCtrl::DoResize()
1255 wxSize sz
= GetClientSize();
1256 int verticalSpacing
= 3;
1260 if (m_filterListCtrl
)
1263 // For some reason, this is required in order for the
1264 // correct control height to always be returned, rather
1265 // than the drop-down list height which is sometimes returned.
1266 wxSize oldSize
= m_filterListCtrl
->GetSize();
1267 m_filterListCtrl
->SetSize(-1, -1, oldSize
.x
+10, -1, wxSIZE_USE_EXISTING
);
1268 m_filterListCtrl
->SetSize(-1, -1, oldSize
.x
, -1, wxSIZE_USE_EXISTING
);
1270 filterSz
= m_filterListCtrl
->GetSize();
1271 sz
.y
-= (filterSz
.y
+ verticalSpacing
);
1273 m_treeCtrl
->SetSize(0, 0, sz
.x
, sz
.y
);
1274 if (m_filterListCtrl
)
1276 m_filterListCtrl
->SetSize(0, sz
.y
+ verticalSpacing
, sz
.x
, filterSz
.y
);
1277 // Don't know why, but this needs refreshing after a resize (wxMSW)
1278 m_filterListCtrl
->Refresh();
1284 void wxGenericDirCtrl::OnSize(wxSizeEvent
& WXUNUSED(event
))
1289 //-----------------------------------------------------------------------------
1290 // wxDirFilterListCtrl
1291 //-----------------------------------------------------------------------------
1293 IMPLEMENT_CLASS(wxDirFilterListCtrl
, wxChoice
)
1295 BEGIN_EVENT_TABLE(wxDirFilterListCtrl
, wxChoice
)
1296 EVT_CHOICE(-1, wxDirFilterListCtrl::OnSelFilter
)
1299 bool wxDirFilterListCtrl::Create(wxGenericDirCtrl
* parent
, const wxWindowID id
,
1305 return wxChoice::Create(parent
, id
, pos
, size
, 0, NULL
, style
);
1308 void wxDirFilterListCtrl::Init()
1313 void wxDirFilterListCtrl::OnSelFilter(wxCommandEvent
& WXUNUSED(event
))
1315 int sel
= GetSelection();
1317 wxString currentPath
= m_dirCtrl
->GetPath();
1319 m_dirCtrl
->SetFilterIndex(sel
);
1321 // If the filter has changed, the view is out of date, so
1322 // collapse the tree.
1323 m_dirCtrl
->ReCreateTree();
1325 // Try to restore the selection, or at least the directory
1326 m_dirCtrl
->ExpandPath(currentPath
);
1329 void wxDirFilterListCtrl::FillFilterList(const wxString
& filter
, int defaultFilter
)
1332 wxArrayString descriptions
, filters
;
1333 size_t n
= (size_t) m_dirCtrl
->ParseFilter(filter
, filters
, descriptions
);
1335 if (n
> 0 && defaultFilter
< (int) n
)
1338 for (i
= 0; i
< n
; i
++)
1339 Append(descriptions
[i
]);
1340 SetSelection(defaultFilter
);
1344 #endif // wxUSE_DIRDLG