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"
71 #if defined(__WXMAC__)
72 # include "MoreFilesExtras.h"
79 // If compiled under Windows, this macro can cause problems
85 static char * icon1_xpm
[] = {
86 /* width height ncolors chars_per_pixel */
114 static char * icon2_xpm
[] = {
115 /* width height ncolors chars_per_pixel */
143 static char * icon3_xpm
[] = {
144 /* width height ncolors chars_per_pixel */
169 static char * icon4_xpm
[] = {
196 static char * icon5_xpm
[] = {
223 static char *icon6_xpm
[] = {
253 static char * icon7_xpm
[] = {
280 static char * icon8_xpm
[] = {
306 static const int ID_DIRCTRL
= 1000;
307 static const int ID_TEXTCTRL
= 1001;
308 static const int ID_OK
= 1002;
309 static const int ID_CANCEL
= 1003;
310 static const int ID_NEW
= 1004;
311 //static const int ID_CHECK = 1005;
313 #if defined(__WXMSW__) || defined(__WXPM__)
314 int setdrive(int drive
)
318 if (drive
< 1 || drive
> 31)
320 newdrive
[0] = (wxChar
)(wxT('A') + drive
- 1);
321 newdrive
[1] = wxT(':');
322 newdrive
[2] = wxT('\0');
323 #if defined(__WXMSW__)
325 if (wxSetWorkingDirectory(newdrive
))
327 if (::SetCurrentDirectory(newdrive
))
330 // VA doesn't know what LPSTR is and has its own set
331 if (DosSetCurrentDir((PSZ
)newdrive
))
338 static bool wxIsDriveAvailable(const wxString dirName
)
341 UINT errorMode
= SetErrorMode(SEM_FAILCRITICALERRORS
| SEM_NOOPENFILEERRORBOX
);
345 // Check if this is a root directory and if so,
346 // whether the drive is avaiable.
347 if (dirName
.Len() == 3 && dirName
[(size_t)1] == wxT(':'))
349 wxString
dirNameLower(dirName
.Lower());
350 #if defined(__GNUWIN32__)
351 success
= wxPathExists(dirNameLower
);
353 int currentDrive
= _getdrive();
354 int thisDrive
= (int) (dirNameLower
[(size_t)0] - 'a' + 1) ;
355 int err
= setdrive( thisDrive
) ;
356 setdrive( currentDrive
);
365 (void) SetErrorMode(errorMode
);
372 // Function which is called by quick sort. We want to override the default wxArrayString behaviour,
373 // and sort regardless of case.
374 static int LINKAGEMODE
wxDirCtrlStringCompareFunction(const void *first
, const void *second
)
376 wxString
*strFirst
= (wxString
*)first
;
377 wxString
*strSecond
= (wxString
*)second
;
379 return strFirst
->CmpNoCase(*strSecond
);
382 //-----------------------------------------------------------------------------
384 //-----------------------------------------------------------------------------
386 wxDirItemDataEx::wxDirItemDataEx(const wxString
& path
, const wxString
& name
,
391 /* Insert logic to detect hidden files here
392 * In UnixLand we just check whether the first char is a dot
393 * For FileNameFromPath read LastDirNameInThisPath ;-) */
394 // m_isHidden = (bool)(wxFileNameFromPath(*m_path)[0] == '.');
396 // m_hasSubDirs is no longer needed
397 m_hasSubDirs
= TRUE
; // HasSubDirs();
398 m_isExpanded
= FALSE
;
402 wxDirItemDataEx::~wxDirItemDataEx()
406 void wxDirItemDataEx::SetNewDirName( wxString path
)
409 m_name
= wxFileNameFromPath( path
);
412 //-----------------------------------------------------------------------------
414 //-----------------------------------------------------------------------------
416 IMPLEMENT_DYNAMIC_CLASS(wxGenericDirCtrl
, wxControl
)
418 BEGIN_EVENT_TABLE(wxGenericDirCtrl
, wxControl
)
419 EVT_TREE_ITEM_EXPANDING (-1, wxGenericDirCtrl::OnExpandItem
)
420 EVT_TREE_ITEM_COLLAPSED (-1, wxGenericDirCtrl::OnCollapseItem
)
421 EVT_TREE_BEGIN_LABEL_EDIT (-1, wxGenericDirCtrl::OnBeginEditItem
)
422 EVT_TREE_END_LABEL_EDIT (-1, wxGenericDirCtrl::OnEndEditItem
)
423 EVT_SIZE (wxGenericDirCtrl::OnSize
)
426 wxGenericDirCtrl::wxGenericDirCtrl(void)
431 bool wxGenericDirCtrl::Create(wxWindow
*parent
,
437 const wxString
& filter
,
439 const wxString
& name
)
441 if (!wxControl::Create(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
444 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
448 long treeStyle
= wxTR_HAS_BUTTONS
; // | wxTR_EDIT_LABELS;
449 if ((style
& wxDIRCTRL_3D_INTERNAL
) == 0)
450 treeStyle
|= wxNO_BORDER
;
452 long filterStyle
= 0;
453 if ((style
& wxDIRCTRL_3D_INTERNAL
) == 0)
454 filterStyle
|= wxNO_BORDER
;
456 m_treeCtrl
= new wxTreeCtrl(this, wxID_TREECTRL
, pos
, size
, treeStyle
);
458 if (!filter
.IsEmpty() && (style
& wxDIRCTRL_SHOW_FILTERS
))
459 m_filterListCtrl
= new wxDirFilterListCtrl(this, wxID_FILTERLISTCTRL
, wxDefaultPosition
, wxDefaultSize
, filterStyle
);
464 SetFilterIndex(defaultFilter
);
466 if (m_filterListCtrl
)
467 m_filterListCtrl
->FillFilterList(filter
, defaultFilter
);
469 m_imageList
= new wxImageList(16, 16, TRUE
);
470 m_imageList
->Add(wxIcon(icon1_xpm
));
471 m_imageList
->Add(wxIcon(icon2_xpm
));
472 m_imageList
->Add(wxIcon(icon3_xpm
));
473 m_imageList
->Add(wxIcon(icon4_xpm
));
474 m_imageList
->Add(wxIcon(icon5_xpm
));
475 m_imageList
->Add(wxIcon(icon6_xpm
));
476 m_imageList
->Add(wxIcon(icon7_xpm
));
477 m_imageList
->Add(wxIcon(icon8_xpm
));
478 m_treeCtrl
->SetImageList(m_imageList
);
480 m_showHidden
= FALSE
;
481 wxDirItemDataEx
* rootData
= new wxDirItemDataEx(wxT(""), wxT(""), TRUE
);
485 #if defined(__WXMSW__) || defined(__WXPM__)
486 rootName
= _("Computer");
488 rootName
= _("Sections");
491 m_rootId
= m_treeCtrl
->AddRoot( rootName
, 3, -1, rootData
);
492 m_treeCtrl
->SetItemHasChildren(m_rootId
);
493 m_treeCtrl
->Expand(m_rootId
); // automatically expand first level
495 // Expand and select the default path
496 if (!m_defaultPath
.IsEmpty())
497 ExpandPath(m_defaultPath
);
504 wxGenericDirCtrl::~wxGenericDirCtrl()
506 m_treeCtrl
->SetImageList(NULL
);
510 void wxGenericDirCtrl::Init()
512 m_showHidden
= FALSE
;
515 m_currentFilterStr
= wxEmptyString
; // Default: any file
517 m_filterListCtrl
= NULL
;
520 void wxGenericDirCtrl::AddSection(const wxString
& path
, const wxString
& name
, int imageId
)
522 wxDirItemDataEx
*dir_item
= new wxDirItemDataEx(path
,name
,TRUE
);
524 #if defined(__WXMSW__) || defined(__WXPM__)
525 // Windows and OS/2: sections are displayed as drives
526 wxTreeItemId id
= m_treeCtrl
->AppendItem( m_rootId
, name
, imageId
, -1, dir_item
);
528 // Unix: sections are displayed as folders
529 wxTreeItemId id
= m_treeCtrl
->AppendItem( m_rootId
, name
, 0, -1, dir_item
);
530 m_treeCtrl
->SetItemImage( id
, 1, wxTreeItemIcon_Expanded
);
532 // TODO: other operating systems.
534 m_treeCtrl
->SetItemHasChildren(id
);
537 void wxGenericDirCtrl::SetupSections()
539 #if defined(__WXMSW__) || defined(__WXPM__)
542 wxChar driveBuffer
[256];
543 size_t n
= (size_t) GetLogicalDriveStrings(255, driveBuffer
);
548 path
.Printf(wxT("%c:\\"), driveBuffer
[i
]);
549 name
.Printf(wxT("(%c:)"), driveBuffer
[i
]);
552 int driveType
= ::GetDriveType(path
);
555 case DRIVE_REMOVABLE
:
556 if (path
== wxT("a:\\") || path
== wxT("b:\\"))
557 imageId
= 6; // Floppy
575 AddSection(path
, name
, imageId
);
577 while (driveBuffer
[i
] != wxT('\0'))
580 if (driveBuffer
[i
] == wxT('\0'))
587 /* If we can switch to the drive, it exists. */
588 for( drive
= 1; drive
<= 26; drive
++ )
591 path
.Printf(wxT("%c:\\"), (char) (drive
+ 'a' - 1));
592 name
.Printf(wxT("(%c:)"), (char) (drive
+ 'a' - 1));
594 if (wxIsDriveAvailable(path
))
597 AddSection(path
, name
);
601 #elif defined(__WXMAC__)
605 short actualCount
= 0 ;
606 if ( OnLine( &volume
, 1 , &actualCount
, &index
) != noErr
|| actualCount
== 0 )
609 wxString name
= wxMacFSSpec2MacFilename( &volume
) ;
610 AddSection(name
+ wxFILE_SEP_PATH
, name
, 0);
613 AddSection(wxT("/"), _("The Computer"), 0);
614 AddSection(wxGetHomeDir(), _("My Home"), 0 );
615 AddSection(wxT("/mnt"), _("Mounted Devices"), 0 );
616 AddSection(wxT("/usr/local"), _("User Local"), 0 );
617 AddSection(wxT("/usr"), _("User"), 0 );
618 AddSection(wxT("/var"), _("Variables"), 0 );
619 AddSection(wxT("/etc"), _("Etcetera"), 0 );
620 AddSection(wxT("/tmp"), _("Temporary"), 0 );
624 void wxGenericDirCtrl::OnBeginEditItem(wxTreeEvent
&event
)
626 // don't rename the main entry "Sections"
627 if (event
.GetItem() == m_rootId
)
633 // don't rename the individual sections
634 if (m_treeCtrl
->GetParent( event
.GetItem() ) == m_rootId
)
641 void wxGenericDirCtrl::OnEndEditItem(wxTreeEvent
&event
)
643 if ((event
.GetLabel().IsEmpty()) ||
644 (event
.GetLabel() == _(".")) ||
645 (event
.GetLabel() == _("..")) ||
646 (event
.GetLabel().First( wxT("/") ) != wxNOT_FOUND
))
648 wxMessageDialog
dialog(this, _("Illegal directory name."), _("Error"), wxOK
| wxICON_ERROR
);
654 wxTreeItemId id
= event
.GetItem();
655 wxDirItemDataEx
*data
= (wxDirItemDataEx
*)m_treeCtrl
->GetItemData( id
);
658 wxString
new_name( wxPathOnly( data
->m_path
) );
659 new_name
+= wxString(wxFILE_SEP_PATH
);
660 new_name
+= event
.GetLabel();
664 if (wxFileExists(new_name
))
666 wxMessageDialog
dialog(this, _("File name exists already."), _("Error"), wxOK
| wxICON_ERROR
);
671 if (wxRenameFile(data
->m_path
,new_name
))
673 data
->SetNewDirName( new_name
);
677 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
683 void wxGenericDirCtrl::OnExpandItem(wxTreeEvent
&event
)
685 wxTreeItemId parentId
= event
.GetItem();
690 void wxGenericDirCtrl::OnCollapseItem(wxTreeEvent
&event
)
692 wxTreeItemId child
, parent
= event
.GetItem();
694 wxDirItemDataEx
*data
= (wxDirItemDataEx
*) m_treeCtrl
->GetItemData(event
.GetItem());
695 if (!data
->m_isExpanded
)
698 data
->m_isExpanded
= FALSE
;
700 /* Workaround because DeleteChildren has disapeared (why?) and
701 * CollapseAndReset doesn't work as advertised (deletes parent too) */
702 child
= m_treeCtrl
->GetFirstChild(parent
, cookie
);
705 m_treeCtrl
->Delete(child
);
706 /* Not GetNextChild below, because the cookie mechanism can't
707 * handle disappearing children! */
708 child
= m_treeCtrl
->GetFirstChild(parent
, cookie
);
712 void wxGenericDirCtrl::ExpandDir(wxTreeItemId parentId
)
714 wxDirItemDataEx
*data
= (wxDirItemDataEx
*) m_treeCtrl
->GetItemData(parentId
);
716 if (data
->m_isExpanded
)
719 data
->m_isExpanded
= TRUE
;
721 if (parentId
== m_rootId
)
729 wxString search
,path
,filename
;
731 wxString
dirName(data
->m_path
);
733 #if defined(__WXMSW__) || defined(__WXPM__)
734 // Check if this is a root directory and if so,
735 // whether the drive is avaiable.
736 if (!wxIsDriveAvailable(dirName
))
738 data
->m_isExpanded
= FALSE
;
739 //wxMessageBox(wxT("Sorry, this drive is not available."));
744 // This may take a longish time. Go to busy cursor
747 #if defined(__WXMSW__) || defined(__WXPM__)
748 if (dirName
.Last() == ':')
749 dirName
+= wxString(wxFILE_SEP_PATH
);
753 wxArrayString filenames
;
756 wxString eachFilename
;
763 if (d
.GetFirst(& eachFilename
, wxEmptyString
, wxDIR_DIRS
))
767 if ((eachFilename
!= wxT(".")) && (eachFilename
!= wxT("..")))
769 dirs
.Add(eachFilename
);
772 while (d
.GetNext(& eachFilename
)) ;
775 dirs
.Sort((wxArrayString::CompareFunction
) wxDirCtrlStringCompareFunction
);
777 // Now do the filenames -- but only if we're allowed to
778 if ((GetWindowStyle() & wxDIRCTRL_DIR_ONLY
) == 0)
786 if (d
.GetFirst(& eachFilename
, m_currentFilterStr
, wxDIR_FILES
))
790 if ((eachFilename
!= wxT(".")) && (eachFilename
!= wxT("..")))
792 filenames
.Add(eachFilename
);
795 while (d
.GetNext(& eachFilename
)) ;
798 filenames
.Sort((wxArrayString::CompareFunction
) wxDirCtrlStringCompareFunction
);
801 // Add the sorted dirs
803 for (i
= 0; i
< dirs
.Count(); i
++)
805 wxString
eachFilename(dirs
[i
]);
807 if (path
.Last() != wxFILE_SEP_PATH
)
808 path
+= wxString(wxFILE_SEP_PATH
);
809 path
+= eachFilename
;
811 wxDirItemDataEx
*dir_item
= new wxDirItemDataEx(path
,eachFilename
,TRUE
);
812 wxTreeItemId id
= m_treeCtrl
->AppendItem( parentId
, eachFilename
, 0, -1, dir_item
);
813 m_treeCtrl
->SetItemImage( id
, 1, wxTreeItemIcon_Expanded
);
815 // Has this got any children? If so, make it expandable.
816 int options
= wxDIR_DEFAULT
;
817 if (GetWindowStyle() & wxDIRCTRL_DIR_ONLY
) // If only showing dirs, then we specify dirs only here
819 options
= wxDIR_DIRS
;
827 // Have to test for wxDIR_DIRS separately in case m_currentFilterStr is non-empty and
828 // and filters out any directories
829 if (dir2
.GetFirst(& str
, m_currentFilterStr
, options
) || dir2
.GetFirst(& str
, wxEmptyString
, wxDIR_DIRS
))
831 m_treeCtrl
->SetItemHasChildren(id
);
836 // Add the sorted filenames
837 if ((GetWindowStyle() & wxDIRCTRL_DIR_ONLY
) == 0)
839 for (i
= 0; i
< filenames
.Count(); i
++)
841 wxString
eachFilename(filenames
[i
]);
843 if (path
.Last() != wxFILE_SEP_PATH
)
844 path
+= wxString(wxFILE_SEP_PATH
);
845 path
+= eachFilename
;
846 //path = dirName + wxString(wxT("/")) + eachFilename;
847 wxDirItemDataEx
*dir_item
= new wxDirItemDataEx(path
,eachFilename
,FALSE
);
848 (void)m_treeCtrl
->AppendItem( parentId
, eachFilename
, 2, -1, dir_item
);
853 // Find the child that matches the first part of 'path'.
854 // E.g. if a child path is "/usr" and 'path' is "/usr/include"
855 // then the child for /usr is returned.
856 wxTreeItemId
wxGenericDirCtrl::FindChild(wxTreeItemId parentId
, const wxString
& path
, bool& done
)
858 wxString
path2(path
);
860 // Make sure all separators are as per the current platform
861 path2
.Replace(wxT("\\"), wxString(wxFILE_SEP_PATH
));
862 path2
.Replace(wxT("/"), wxString(wxFILE_SEP_PATH
));
864 // Append a separator to foil bogus substring matching
865 path2
+= wxString(wxFILE_SEP_PATH
);
867 // In MSW or PM, case is not significant
868 #if defined(__WXMSW__) || defined(__WXPM__)
873 wxTreeItemId childId
= m_treeCtrl
->GetFirstChild(parentId
, cookie
);
874 while (childId
.IsOk())
876 wxDirItemDataEx
* data
= (wxDirItemDataEx
*) m_treeCtrl
->GetItemData(childId
);
878 if (data
&& data
->m_path
!= "")
880 wxString
childPath(data
->m_path
);
881 if (childPath
.Last() != wxFILE_SEP_PATH
)
882 childPath
+= wxString(wxFILE_SEP_PATH
);
884 // In MSW and PM, case is not significant
885 #if defined(__WXMSW__) || defined(__WXPM__)
886 childPath
.MakeLower();
889 if (childPath
.Len() <= path2
.Len())
891 wxString path3
= path2
.Mid(0, childPath
.Len());
892 if (childPath
== path3
)
894 if (path3
.Len() == path2
.Len())
903 childId
= m_treeCtrl
->GetNextChild(childId
, cookie
);
905 wxTreeItemId invalid
;
909 // Try to expand as much of the given path as possible,
910 // and select the given tree item.
911 bool wxGenericDirCtrl::ExpandPath(const wxString
& path
)
914 wxTreeItemId id
= FindChild(m_rootId
, path
, done
);
915 wxTreeItemId lastId
= id
; // The last non-zero id
916 while (id
.IsOk() && !done
)
920 id
= FindChild(id
, path
, done
);
926 wxDirItemDataEx
*data
= (wxDirItemDataEx
*) m_treeCtrl
->GetItemData(lastId
);
929 m_treeCtrl
->Expand(lastId
);
931 if ((GetWindowStyle() & wxDIRCTRL_SELECT_FIRST
) && data
->m_isDir
)
933 // Find the first file in this directory
935 wxTreeItemId childId
= m_treeCtrl
->GetFirstChild(lastId
, cookie
);
936 bool selectedChild
= FALSE
;
937 while (childId
.IsOk())
939 wxDirItemDataEx
* data
= (wxDirItemDataEx
*) m_treeCtrl
->GetItemData(childId
);
941 if (data
&& data
->m_path
!= "" && !data
->m_isDir
)
943 m_treeCtrl
->SelectItem(childId
);
944 m_treeCtrl
->EnsureVisible(childId
);
945 selectedChild
= TRUE
;
948 childId
= m_treeCtrl
->GetNextChild(lastId
, cookie
);
952 m_treeCtrl
->SelectItem(lastId
);
953 m_treeCtrl
->EnsureVisible(lastId
);
958 m_treeCtrl
->SelectItem(lastId
);
959 m_treeCtrl
->EnsureVisible(lastId
);
968 wxString
wxGenericDirCtrl::GetPath() const
970 wxTreeItemId id
= m_treeCtrl
->GetSelection();
973 wxDirItemDataEx
* data
= (wxDirItemDataEx
*) m_treeCtrl
->GetItemData(id
);
977 return wxEmptyString
;
980 wxString
wxGenericDirCtrl::GetFilePath() const
982 wxTreeItemId id
= m_treeCtrl
->GetSelection();
985 wxDirItemDataEx
* data
= (wxDirItemDataEx
*) m_treeCtrl
->GetItemData(id
);
987 return wxEmptyString
;
992 return wxEmptyString
;
995 void wxGenericDirCtrl::SetPath(const wxString
& path
)
997 m_defaultPath
= path
;
1004 void wxGenericDirCtrl::FindChildFiles(wxTreeItemId id
, int dirFlags
, wxArrayString
& filenames
)
1006 wxDirItemDataEx
*data
= (wxDirItemDataEx
*) m_treeCtrl
->GetItemData(id
);
1008 // This may take a longish time. Go to busy cursor
1013 wxString search
,path
,filename
;
1015 wxString
dirName(data
->m_path
);
1017 #if defined(__WXMSW__) || defined(__WXPM__)
1018 if (dirName
.Last() == ':')
1019 dirName
+= wxString(wxFILE_SEP_PATH
);
1023 wxString eachFilename
;
1030 if (d
.GetFirst(& eachFilename
, m_currentFilterStr
, dirFlags
))
1034 if ((eachFilename
!= wxT(".")) && (eachFilename
!= wxT("..")))
1036 filenames
.Add(eachFilename
);
1039 while (d
.GetNext(& eachFilename
)) ;
1045 void wxGenericDirCtrl::SetFilterIndex(int n
)
1047 m_currentFilter
= n
;
1050 if (ExtractWildcard(m_filter
, n
, f
, d
))
1051 m_currentFilterStr
= f
;
1053 m_currentFilterStr
= wxT("*.*");
1056 void wxGenericDirCtrl::SetFilter(const wxString
& filter
)
1061 if (ExtractWildcard(m_filter
, m_currentFilter
, f
, d
))
1062 m_currentFilterStr
= f
;
1064 m_currentFilterStr
= wxT("*.*");
1067 // Extract description and actual filter from overall filter string
1068 bool wxGenericDirCtrl::ExtractWildcard(const wxString
& filterStr
, int n
, wxString
& filter
, wxString
& description
)
1070 wxArrayString filters
, descriptions
;
1071 int count
= ParseFilter(filterStr
, filters
, descriptions
);
1072 if (count
> 0 && n
< count
)
1074 filter
= filters
[n
];
1075 description
= descriptions
[n
];
1082 // Parses the global filter, returning the number of filters.
1083 // Returns 0 if none or if there's a problem.
1084 // filterStr is in the form:
1086 // "All files (*.*)|*.*|JPEG Files (*.jpeg)|*.jpg"
1088 int wxGenericDirCtrl::ParseFilter(const wxString
& filterStr
, wxArrayString
& filters
, wxArrayString
& descriptions
)
1090 wxString
str(filterStr
);
1092 wxString description
, filter
;
1094 bool finished
= FALSE
;
1097 pos
= str
.Find(wxT('|'));
1099 return 0; // Problem
1100 description
= str
.Left(pos
);
1101 str
= str
.Mid(pos
+1);
1102 pos
= str
.Find(wxT('|'));
1110 filter
= str
.Left(pos
);
1111 str
= str
.Mid(pos
+1);
1113 descriptions
.Add(description
);
1114 filters
.Add(filter
);
1118 return filters
.Count();
1121 void wxGenericDirCtrl::DoResize()
1123 wxSize sz
= GetClientSize();
1124 int verticalSpacing
= 3;
1128 if (m_filterListCtrl
)
1130 filterSz
= m_filterListCtrl
->GetSize();
1131 sz
.y
-= (filterSz
.y
+ verticalSpacing
);
1133 m_treeCtrl
->SetSize(0, 0, sz
.x
, sz
.y
);
1134 if (m_filterListCtrl
)
1136 m_filterListCtrl
->SetSize(0, sz
.y
+ verticalSpacing
, sz
.x
, filterSz
.y
);
1137 // Don't know why, but this needs refreshing after a resize (wxMSW)
1138 m_filterListCtrl
->Refresh();
1144 void wxGenericDirCtrl::OnSize(wxSizeEvent
& WXUNUSED(event
))
1149 //-----------------------------------------------------------------------------
1150 // wxDirFilterListCtrl
1151 //-----------------------------------------------------------------------------
1153 IMPLEMENT_CLASS(wxDirFilterListCtrl
, wxChoice
)
1155 BEGIN_EVENT_TABLE(wxDirFilterListCtrl
, wxChoice
)
1156 EVT_CHOICE(-1, wxDirFilterListCtrl::OnSelFilter
)
1159 bool wxDirFilterListCtrl::Create(wxGenericDirCtrl
* parent
, const wxWindowID id
,
1165 return wxChoice::Create(parent
, id
, pos
, size
, 0, NULL
, style
);
1168 void wxDirFilterListCtrl::Init()
1173 void wxDirFilterListCtrl::OnSelFilter(wxCommandEvent
& WXUNUSED(event
))
1175 int sel
= GetSelection();
1177 wxString currentPath
= m_dirCtrl
->GetPath();
1179 m_dirCtrl
->SetFilterIndex(sel
);
1181 // If the filter has changed, the view is out of date, so
1182 // collapse the tree.
1183 m_dirCtrl
->GetTreeCtrl()->Collapse(m_dirCtrl
->GetRootId());
1184 m_dirCtrl
->GetTreeCtrl()->Expand(m_dirCtrl
->GetRootId());
1186 // Try to restore the selection, or at least the directory
1187 m_dirCtrl
->ExpandPath(currentPath
);
1190 void wxDirFilterListCtrl::FillFilterList(const wxString
& filter
, int defaultFilter
)
1193 wxArrayString descriptions
, filters
;
1194 size_t n
= (size_t) m_dirCtrl
->ParseFilter(filter
, filters
, descriptions
);
1196 if (n
> 0 && defaultFilter
< (int) n
)
1199 for (i
= 0; i
< n
; i
++)
1200 Append(descriptions
[i
]);
1201 SetSelection(defaultFilter
);
1205 // wxGenericDirDialog implementation
1206 // This should be moved into dirdlgg.cpp eventually
1208 BEGIN_EVENT_TABLE(wxGenericDirDialog
, wxDialog
)
1209 EVT_BUTTON(wxID_OK
, wxGenericDirDialog::OnOK
)
1210 EVT_BUTTON(wxID_NEW
, wxGenericDirDialog::OnNew
)
1211 EVT_BUTTON (wxID_NEW
, wxGenericDirDialog::OnNew
)
1212 EVT_CLOSE(wxGenericDirDialog::OnCloseWindow
)
1213 EVT_TREE_KEY_DOWN (-1, wxGenericDirDialog::OnTreeKeyDown
)
1214 EVT_TREE_SEL_CHANGED (-1, wxGenericDirDialog::OnTreeSelected
)
1215 EVT_TEXT_ENTER (ID_TEXTCTRL
, wxGenericDirDialog::OnOK
)
1218 wxGenericDirDialog::wxGenericDirDialog(wxWindow
* parent
, const wxString
& title
,
1219 const wxString
& defaultPath
, long style
, const wxPoint
& pos
, const wxSize
& sz
, const wxString
& name
):
1220 wxDialog(parent
, ID_DIRCTRL
, title
, pos
, sz
, style
, name
)
1223 m_path
= defaultPath
;
1225 wxBusyCursor cursor
;
1227 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
1230 m_dirCtrl
= new wxGenericDirCtrl(this, ID_DIRCTRL
,
1231 defaultPath
, wxPoint(5, 5),
1232 wxSize(300, 200), wxDIRCTRL_DIR_ONLY
|wxSUNKEN_BORDER
);
1234 topsizer
->Add( m_dirCtrl
, 1, wxTOP
|wxLEFT
|wxRIGHT
| wxEXPAND
, 10 );
1237 m_input
= new wxTextCtrl( this, ID_TEXTCTRL
, m_path
, wxDefaultPosition
);
1238 topsizer
->Add( m_input
, 0, wxTOP
|wxLEFT
|wxRIGHT
| wxEXPAND
, 10 );
1242 topsizer
->Add( new wxStaticLine( this, -1 ), 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
1246 wxSizer
* buttonsizer
= new wxBoxSizer( wxHORIZONTAL
);
1247 wxButton
* okButton
= new wxButton(this, wxID_OK
, _("OK"));
1248 buttonsizer
->Add( okButton
, 0, wxLEFT
|wxRIGHT
, 10 );
1249 wxButton
* cancelButton
= new wxButton(this, wxID_CANCEL
, _("Cancel"));
1250 buttonsizer
->Add( cancelButton
, 0, wxLEFT
|wxRIGHT
, 10 );
1252 // I'm not convinced we need a New button, and we tend to get annoying
1253 // accidental-editing with label editing enabled.
1255 wxButton
* newButton
= new wxButton( this, wxID_NEW
, _("New...") );
1256 buttonsizer
->Add( newButton
, 0, wxLEFT
|wxRIGHT
, 10 );
1259 topsizer
->Add( buttonsizer
, 0, wxALL
| wxCENTER
, 10 );
1261 okButton
->SetDefault();
1262 m_dirCtrl
->SetFocus();
1264 SetAutoLayout( TRUE
);
1265 SetSizer( topsizer
);
1267 topsizer
->SetSizeHints( this );
1268 topsizer
->Fit( this );
1273 void wxGenericDirDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
1275 EndModal(wxID_CANCEL
);
1278 void wxGenericDirDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
1280 m_path
= m_input
->GetValue();
1281 // Does the path exist? (User may have typed anything in m_input)
1282 if (wxPathExists(m_path
)) {
1283 // OK, path exists, we're done.
1287 // Interact with user, find out if the dir is a typo or to be created
1288 wxString
msg( _("The directory ") );
1290 msg
= msg
+ _("\ndoes not exist\nCreate it now?") ;
1291 wxMessageDialog
dialog(this, msg
, _("Directory does not exist"), wxYES_NO
| wxICON_WARNING
);
1292 if ( dialog
.ShowModal() == wxID_YES
) {
1293 // Okay, let's make it
1295 if (wxMkdir(m_path
)) {
1296 // The new dir was created okay.
1302 msg
= _("Failed to create directory ")+m_path
+
1303 _("\n(Do you have the required permissions?)");
1304 wxMessageDialog
errmsg(this, msg
, _("Error creating directory"), wxOK
| wxICON_ERROR
);
1306 // We still don't have a valid dir. Back to the main dialog.
1309 // User has answered NO to create dir.
1312 void wxGenericDirDialog::SetPath(const wxString
& path
)
1314 m_dirCtrl
->SetPath(path
);
1318 wxString
wxGenericDirDialog::GetPath(void) const
1323 int wxGenericDirDialog::ShowModal()
1325 //m_input->SetValue( m_path );
1326 return wxDialog::ShowModal();
1329 void wxGenericDirDialog::OnTreeSelected( wxTreeEvent
&event
)
1334 wxDirItemDataEx
*data
= (wxDirItemDataEx
*)m_dirCtrl
->GetTreeCtrl()->GetItemData(event
.GetItem());
1336 m_input
->SetValue( data
->m_path
);
1339 void wxGenericDirDialog::OnTreeKeyDown( wxTreeEvent
&WXUNUSED(event
) )
1344 wxDirItemDataEx
*data
= (wxDirItemDataEx
*)m_dirCtrl
->GetTreeCtrl()->GetItemData(m_dirCtrl
->GetTreeCtrl()->GetSelection());
1346 m_input
->SetValue( data
->m_path
);
1349 void wxGenericDirDialog::OnNew( wxCommandEvent
& WXUNUSED(event
) )
1351 wxTreeItemId id
= m_dirCtrl
->GetTreeCtrl()->GetSelection();
1352 if ((id
== m_dirCtrl
->GetTreeCtrl()->GetRootItem()) ||
1353 (m_dirCtrl
->GetTreeCtrl()->GetParent(id
) == m_dirCtrl
->GetTreeCtrl()->GetRootItem()))
1355 wxMessageDialog
msg(this, _("You cannot add a new directory to this section."),
1356 _("Create directory"), wxOK
| wxICON_INFORMATION
);
1361 wxTreeItemId parent
= id
; // m_dirCtrl->GetTreeCtrl()->GetParent( id );
1362 wxDirItemDataEx
*data
= (wxDirItemDataEx
*)m_dirCtrl
->GetTreeCtrl()->GetItemData( parent
);
1365 wxString
new_name( wxT("NewName") );
1366 wxString
path( data
->m_path
);
1367 if (path
.Last() != wxFILE_SEP_PATH
)
1368 path
+= wxFILE_SEP_PATH
;
1370 if (wxFileExists(path
))
1372 // try NewName0, NewName1 etc.
1375 new_name
= wxT("NewName");
1377 num
.Printf( wxT("%d"), i
);
1380 path
= data
->m_path
;
1381 if (path
.Last() != wxFILE_SEP_PATH
)
1382 path
+= wxFILE_SEP_PATH
;
1385 } while (wxFileExists(path
));
1391 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
1396 wxDirItemDataEx
*new_data
= new wxDirItemDataEx( path
, new_name
, TRUE
);
1398 // TODO: THIS CODE DOESN'T WORK YET. We need to avoid duplication of the first child
1400 wxTreeItemId new_id
= m_dirCtrl
->GetTreeCtrl()->AppendItem( parent
, new_name
, 0, 0, new_data
);
1401 m_dirCtrl
->GetTreeCtrl()->EnsureVisible( new_id
);
1402 m_dirCtrl
->GetTreeCtrl()->EditLabel( new_id
);
1405 #endif // wxUSE_DIRDLG