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/textdlg.h"
31 #include "wx/filefn.h"
32 #include "wx/cmndata.h"
33 #include "wx/gdicmn.h"
35 #include "wx/imaglist.h"
39 #include "wx/tokenzr.h"
41 #include "wx/settings.h"
44 #include "wx/statline.h"
47 #include "wx/generic/dirctrlg.h"
74 // If compiled under Windows, this macro can cause problems
80 static char * icon1_xpm
[] = {
81 /* width height ncolors chars_per_pixel */
109 static char * icon2_xpm
[] = {
110 /* width height ncolors chars_per_pixel */
138 static char * icon3_xpm
[] = {
139 /* width height ncolors chars_per_pixel */
164 static char * icon4_xpm
[] = {
191 static char * icon5_xpm
[] = {
218 static char *icon6_xpm
[] = {
248 static char * icon7_xpm
[] = {
275 static char * icon8_xpm
[] = {
301 static const int ID_DIRCTRL
= 1000;
302 static const int ID_TEXTCTRL
= 1001;
303 static const int ID_OK
= 1002;
304 static const int ID_CANCEL
= 1003;
305 static const int ID_NEW
= 1004;
306 //static const int ID_CHECK = 1005;
308 #if defined(__WXMSW__) || defined(__WXPM__)
309 int setdrive(int drive
)
313 if (drive
< 1 || drive
> 31)
315 newdrive
[0] = (wxChar
)(wxT('A') + drive
- 1);
316 newdrive
[1] = wxT(':');
317 newdrive
[2] = wxT('\0');
318 #if defined(__WXMSW__)
320 if (wxSetWorkingDirectory(newdrive
))
322 if (::SetCurrentDirectory(newdrive
))
325 // VA doesn't know what LPSTR is and has its own set
326 if (DosSetCurrentDir((PSZ
)newdrive
))
333 static bool wxIsDriveAvailable(const wxString dirName
)
336 UINT errorMode
= SetErrorMode(SEM_FAILCRITICALERRORS
| SEM_NOOPENFILEERRORBOX
);
340 // Check if this is a root directory and if so,
341 // whether the drive is avaiable.
342 if (dirName
.Len() == 3 && dirName
[(size_t)1] == wxT(':'))
344 wxString
dirNameLower(dirName
.Lower());
345 #if defined(__GNUWIN32__)
346 success
= wxPathExists(dirNameLower
);
348 int currentDrive
= _getdrive();
349 int thisDrive
= (int) (dirNameLower
[(size_t)0] - 'a' + 1) ;
350 int err
= setdrive( thisDrive
) ;
351 setdrive( currentDrive
);
360 (void) SetErrorMode(errorMode
);
367 // Function which is called by quick sort. We want to override the default wxArrayString behaviour,
368 // and sort regardless of case.
369 static int LINKAGEMODE
wxDirCtrlStringCompareFunction(const void *first
, const void *second
)
371 wxString
*strFirst
= (wxString
*)first
;
372 wxString
*strSecond
= (wxString
*)second
;
374 return strFirst
->CmpNoCase(*strSecond
);
377 //-----------------------------------------------------------------------------
379 //-----------------------------------------------------------------------------
381 wxDirItemDataEx::wxDirItemDataEx(const wxString
& path
, const wxString
& name
,
386 /* Insert logic to detect hidden files here
387 * In UnixLand we just check whether the first char is a dot
388 * For FileNameFromPath read LastDirNameInThisPath ;-) */
389 // m_isHidden = (bool)(wxFileNameFromPath(*m_path)[0] == '.');
391 // m_hasSubDirs is no longer needed
392 m_hasSubDirs
= TRUE
; // HasSubDirs();
393 m_isExpanded
= FALSE
;
397 wxDirItemDataEx::~wxDirItemDataEx()
401 void wxDirItemDataEx::SetNewDirName( wxString path
)
404 m_name
= wxFileNameFromPath( path
);
407 //-----------------------------------------------------------------------------
409 //-----------------------------------------------------------------------------
411 IMPLEMENT_DYNAMIC_CLASS(wxGenericDirCtrl
, wxControl
)
413 BEGIN_EVENT_TABLE(wxGenericDirCtrl
, wxControl
)
414 EVT_TREE_ITEM_EXPANDING (-1, wxGenericDirCtrl::OnExpandItem
)
415 EVT_TREE_ITEM_COLLAPSED (-1, wxGenericDirCtrl::OnCollapseItem
)
416 EVT_TREE_BEGIN_LABEL_EDIT (-1, wxGenericDirCtrl::OnBeginEditItem
)
417 EVT_TREE_END_LABEL_EDIT (-1, wxGenericDirCtrl::OnEndEditItem
)
418 EVT_SIZE (wxGenericDirCtrl::OnSize
)
421 wxGenericDirCtrl::wxGenericDirCtrl(void)
426 bool wxGenericDirCtrl::Create(wxWindow
*parent
,
432 const wxString
& filter
,
434 const wxString
& name
)
436 if (!wxControl::Create(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
439 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
443 long treeStyle
= wxTR_HAS_BUTTONS
; // | wxTR_EDIT_LABELS;
444 if ((style
& wxDIRCTRL_3D_INTERNAL
) == 0)
445 treeStyle
|= wxNO_BORDER
;
447 long filterStyle
= 0;
448 if ((style
& wxDIRCTRL_3D_INTERNAL
) == 0)
449 filterStyle
|= wxNO_BORDER
;
451 m_treeCtrl
= new wxTreeCtrl(this, wxID_TREECTRL
, pos
, size
, treeStyle
);
453 if (!filter
.IsEmpty() && (style
& wxDIRCTRL_SHOW_FILTERS
))
454 m_filterListCtrl
= new wxDirFilterListCtrl(this, wxID_FILTERLISTCTRL
, wxDefaultPosition
, wxDefaultSize
, filterStyle
);
459 SetFilterIndex(defaultFilter
);
461 if (m_filterListCtrl
)
462 m_filterListCtrl
->FillFilterList(filter
, defaultFilter
);
464 m_imageList
= new wxImageList(16, 16, TRUE
);
465 m_imageList
->Add(wxIcon(icon1_xpm
));
466 m_imageList
->Add(wxIcon(icon2_xpm
));
467 m_imageList
->Add(wxIcon(icon3_xpm
));
468 m_imageList
->Add(wxIcon(icon4_xpm
));
469 m_imageList
->Add(wxIcon(icon5_xpm
));
470 m_imageList
->Add(wxIcon(icon6_xpm
));
471 m_imageList
->Add(wxIcon(icon7_xpm
));
472 m_imageList
->Add(wxIcon(icon8_xpm
));
473 m_treeCtrl
->SetImageList(m_imageList
);
475 m_showHidden
= FALSE
;
476 wxDirItemDataEx
* rootData
= new wxDirItemDataEx(wxT(""), wxT(""), TRUE
);
480 #if defined(__WXMSW__) || defined(__WXPM__)
481 rootName
= _("Computer");
483 rootName
= _("Sections");
486 m_rootId
= m_treeCtrl
->AddRoot( rootName
, 3, -1, rootData
);
487 m_treeCtrl
->SetItemHasChildren(m_rootId
);
488 m_treeCtrl
->Expand(m_rootId
); // automatically expand first level
490 // Expand and select the default path
491 if (!m_defaultPath
.IsEmpty())
492 ExpandPath(m_defaultPath
);
499 wxGenericDirCtrl::~wxGenericDirCtrl()
501 m_treeCtrl
->SetImageList(NULL
);
505 void wxGenericDirCtrl::Init()
507 m_showHidden
= FALSE
;
510 m_currentFilterStr
= wxEmptyString
; // Default: any file
512 m_filterListCtrl
= NULL
;
515 void wxGenericDirCtrl::AddSection(const wxString
& path
, const wxString
& name
, int imageId
)
517 wxDirItemDataEx
*dir_item
= new wxDirItemDataEx(path
,name
,TRUE
);
519 #if defined(__WXMSW__) || defined(__WXPM__)
520 // Windows and OS/2: sections are displayed as drives
521 wxTreeItemId id
= m_treeCtrl
->AppendItem( m_rootId
, name
, imageId
, -1, dir_item
);
523 // Unix: sections are displayed as folders
524 wxTreeItemId id
= m_treeCtrl
->AppendItem( m_rootId
, name
, 0, -1, dir_item
);
525 m_treeCtrl
->SetItemImage( id
, 1, wxTreeItemIcon_Expanded
);
527 // TODO: other operating systems.
529 m_treeCtrl
->SetItemHasChildren(id
);
532 void wxGenericDirCtrl::SetupSections()
534 #if defined(__WXMSW__) || defined(__WXPM__)
537 wxChar driveBuffer
[256];
538 size_t n
= (size_t) GetLogicalDriveStrings(255, driveBuffer
);
543 path
.Printf(wxT("%c:\\"), driveBuffer
[i
]);
544 name
.Printf(wxT("(%c:)"), driveBuffer
[i
]);
547 int driveType
= ::GetDriveType(path
);
550 case DRIVE_REMOVABLE
:
551 if (path
== wxT("a:\\") || path
== wxT("b:\\"))
552 imageId
= 6; // Floppy
570 AddSection(path
, name
, imageId
);
572 while (driveBuffer
[i
] != wxT('\0'))
575 if (driveBuffer
[i
] == wxT('\0'))
582 /* If we can switch to the drive, it exists. */
583 for( drive
= 1; drive
<= 26; drive
++ )
586 path
.Printf(wxT("%c:\\"), (char) (drive
+ 'a' - 1));
587 name
.Printf(wxT("(%c:)"), (char) (drive
+ 'a' - 1));
589 if (wxIsDriveAvailable(path
))
592 AddSection(path
, name
);
598 AddSection(wxT("/"), _("The Computer"), 0);
599 AddSection(wxGetHomeDir(), _("My Home"), 0 );
600 AddSection(wxT("/mnt"), _("Mounted Devices"), 0 );
601 AddSection(wxT("/usr/local"), _("User Local"), 0 );
602 AddSection(wxT("/usr"), _("User"), 0 );
603 AddSection(wxT("/var"), _("Variables"), 0 );
604 AddSection(wxT("/etc"), _("Etcetera"), 0 );
605 AddSection(wxT("/tmp"), _("Temporary"), 0 );
609 void wxGenericDirCtrl::OnBeginEditItem(wxTreeEvent
&event
)
611 // don't rename the main entry "Sections"
612 if (event
.GetItem() == m_rootId
)
618 // don't rename the individual sections
619 if (m_treeCtrl
->GetParent( event
.GetItem() ) == m_rootId
)
626 void wxGenericDirCtrl::OnEndEditItem(wxTreeEvent
&event
)
628 if ((event
.GetLabel().IsEmpty()) ||
629 (event
.GetLabel() == _(".")) ||
630 (event
.GetLabel() == _("..")) ||
631 (event
.GetLabel().First( wxT("/") ) != wxNOT_FOUND
))
633 wxMessageDialog
dialog(this, _("Illegal directory name."), _("Error"), wxOK
| wxICON_ERROR
);
639 wxTreeItemId id
= event
.GetItem();
640 wxDirItemDataEx
*data
= (wxDirItemDataEx
*)m_treeCtrl
->GetItemData( id
);
643 wxString
new_name( wxPathOnly( data
->m_path
) );
644 new_name
+= wxString(wxFILE_SEP_PATH
);
645 new_name
+= event
.GetLabel();
649 if (wxFileExists(new_name
))
651 wxMessageDialog
dialog(this, _("File name exists already."), _("Error"), wxOK
| wxICON_ERROR
);
656 if (wxRenameFile(data
->m_path
,new_name
))
658 data
->SetNewDirName( new_name
);
662 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
668 void wxGenericDirCtrl::OnExpandItem(wxTreeEvent
&event
)
670 wxTreeItemId parentId
= event
.GetItem();
675 void wxGenericDirCtrl::OnCollapseItem(wxTreeEvent
&event
)
677 wxTreeItemId child
, parent
= event
.GetItem();
679 wxDirItemDataEx
*data
= (wxDirItemDataEx
*) m_treeCtrl
->GetItemData(event
.GetItem());
680 if (!data
->m_isExpanded
)
683 data
->m_isExpanded
= FALSE
;
685 /* Workaround because DeleteChildren has disapeared (why?) and
686 * CollapseAndReset doesn't work as advertised (deletes parent too) */
687 child
= m_treeCtrl
->GetFirstChild(parent
, cookie
);
690 m_treeCtrl
->Delete(child
);
691 /* Not GetNextChild below, because the cookie mechanism can't
692 * handle disappearing children! */
693 child
= m_treeCtrl
->GetFirstChild(parent
, cookie
);
697 void wxGenericDirCtrl::ExpandDir(wxTreeItemId parentId
)
699 wxDirItemDataEx
*data
= (wxDirItemDataEx
*) m_treeCtrl
->GetItemData(parentId
);
701 if (data
->m_isExpanded
)
704 data
->m_isExpanded
= TRUE
;
706 if (parentId
== m_rootId
)
714 wxString search
,path
,filename
;
716 wxString
dirName(data
->m_path
);
718 #if defined(__WXMSW__) || defined(__WXPM__)
719 // Check if this is a root directory and if so,
720 // whether the drive is avaiable.
721 if (!wxIsDriveAvailable(dirName
))
723 data
->m_isExpanded
= FALSE
;
724 //wxMessageBox(wxT("Sorry, this drive is not available."));
729 // This may take a longish time. Go to busy cursor
732 #if defined(__WXMSW__) || defined(__WXPM__)
733 if (dirName
.Last() == ':')
734 dirName
+= wxString(wxFILE_SEP_PATH
);
738 wxArrayString filenames
;
741 wxString eachFilename
;
748 if (d
.GetFirst(& eachFilename
, wxEmptyString
, wxDIR_DIRS
))
752 if ((eachFilename
!= wxT(".")) && (eachFilename
!= wxT("..")))
754 dirs
.Add(eachFilename
);
757 while (d
.GetNext(& eachFilename
)) ;
760 dirs
.Sort((wxArrayString::CompareFunction
) wxDirCtrlStringCompareFunction
);
762 // Now do the filenames -- but only if we're allowed to
763 if ((GetWindowStyle() & wxDIRCTRL_DIR_ONLY
) == 0)
771 if (d
.GetFirst(& eachFilename
, m_currentFilterStr
, wxDIR_FILES
))
775 if ((eachFilename
!= wxT(".")) && (eachFilename
!= wxT("..")))
777 filenames
.Add(eachFilename
);
780 while (d
.GetNext(& eachFilename
)) ;
783 filenames
.Sort((wxArrayString::CompareFunction
) wxDirCtrlStringCompareFunction
);
786 // Add the sorted dirs
788 for (i
= 0; i
< dirs
.Count(); i
++)
790 wxString
eachFilename(dirs
[i
]);
792 if (path
.Last() != wxFILE_SEP_PATH
)
793 path
+= wxString(wxFILE_SEP_PATH
);
794 path
+= eachFilename
;
796 wxDirItemDataEx
*dir_item
= new wxDirItemDataEx(path
,eachFilename
,TRUE
);
797 wxTreeItemId id
= m_treeCtrl
->AppendItem( parentId
, eachFilename
, 0, -1, dir_item
);
798 m_treeCtrl
->SetItemImage( id
, 1, wxTreeItemIcon_Expanded
);
800 // Has this got any children? If so, make it expandable.
801 int options
= wxDIR_DEFAULT
;
802 if (GetWindowStyle() & wxDIRCTRL_DIR_ONLY
) // If only showing dirs, then we specify dirs only here
804 options
= wxDIR_DIRS
;
812 // Have to test for wxDIR_DIRS separately in case m_currentFilterStr is non-empty and
813 // and filters out any directories
814 if (dir2
.GetFirst(& str
, m_currentFilterStr
, options
) || dir2
.GetFirst(& str
, wxEmptyString
, wxDIR_DIRS
))
816 m_treeCtrl
->SetItemHasChildren(id
);
821 // Add the sorted filenames
822 if ((GetWindowStyle() & wxDIRCTRL_DIR_ONLY
) == 0)
824 for (i
= 0; i
< filenames
.Count(); i
++)
826 wxString
eachFilename(filenames
[i
]);
828 if (path
.Last() != wxFILE_SEP_PATH
)
829 path
+= wxString(wxFILE_SEP_PATH
);
830 path
+= eachFilename
;
831 //path = dirName + wxString(wxT("/")) + eachFilename;
832 wxDirItemDataEx
*dir_item
= new wxDirItemDataEx(path
,eachFilename
,FALSE
);
833 (void)m_treeCtrl
->AppendItem( parentId
, eachFilename
, 2, -1, dir_item
);
838 // Find the child that matches the first part of 'path'.
839 // E.g. if a child path is "/usr" and 'path' is "/usr/include"
840 // then the child for /usr is returned.
841 wxTreeItemId
wxGenericDirCtrl::FindChild(wxTreeItemId parentId
, const wxString
& path
, bool& done
)
843 wxString
path2(path
);
845 // Make sure all separators are as per the current platform
846 path2
.Replace(wxT("\\"), wxString(wxFILE_SEP_PATH
));
847 path2
.Replace(wxT("/"), wxString(wxFILE_SEP_PATH
));
849 // Append a separator to foil bogus substring matching
850 path2
+= wxString(wxFILE_SEP_PATH
);
852 // In MSW or PM, case is not significant
853 #if defined(__WXMSW__) || defined(__WXPM__)
858 wxTreeItemId childId
= m_treeCtrl
->GetFirstChild(parentId
, cookie
);
859 while (childId
.IsOk())
861 wxDirItemDataEx
* data
= (wxDirItemDataEx
*) m_treeCtrl
->GetItemData(childId
);
863 if (data
&& data
->m_path
!= "")
865 wxString
childPath(data
->m_path
);
866 if (childPath
.Last() != wxFILE_SEP_PATH
)
867 childPath
+= wxString(wxFILE_SEP_PATH
);
869 // In MSW and PM, case is not significant
870 #if defined(__WXMSW__) || defined(__WXPM__)
871 childPath
.MakeLower();
874 if (childPath
.Len() <= path2
.Len())
876 wxString path3
= path2
.Mid(0, childPath
.Len());
877 if (childPath
== path3
)
879 if (path3
.Len() == path2
.Len())
888 childId
= m_treeCtrl
->GetNextChild(childId
, cookie
);
890 wxTreeItemId invalid
;
894 // Try to expand as much of the given path as possible,
895 // and select the given tree item.
896 bool wxGenericDirCtrl::ExpandPath(const wxString
& path
)
899 wxTreeItemId id
= FindChild(m_rootId
, path
, done
);
900 wxTreeItemId lastId
= id
; // The last non-zero id
901 while (id
.IsOk() && !done
)
905 id
= FindChild(id
, path
, done
);
911 wxDirItemDataEx
*data
= (wxDirItemDataEx
*) m_treeCtrl
->GetItemData(lastId
);
914 m_treeCtrl
->Expand(lastId
);
916 if ((GetWindowStyle() & wxDIRCTRL_SELECT_FIRST
) && data
->m_isDir
)
918 // Find the first file in this directory
920 wxTreeItemId childId
= m_treeCtrl
->GetFirstChild(lastId
, cookie
);
921 bool selectedChild
= FALSE
;
922 while (childId
.IsOk())
924 wxDirItemDataEx
* data
= (wxDirItemDataEx
*) m_treeCtrl
->GetItemData(childId
);
926 if (data
&& data
->m_path
!= "" && !data
->m_isDir
)
928 m_treeCtrl
->SelectItem(childId
);
929 m_treeCtrl
->EnsureVisible(childId
);
930 selectedChild
= TRUE
;
933 childId
= m_treeCtrl
->GetNextChild(lastId
, cookie
);
937 m_treeCtrl
->SelectItem(lastId
);
938 m_treeCtrl
->EnsureVisible(lastId
);
943 m_treeCtrl
->SelectItem(lastId
);
944 m_treeCtrl
->EnsureVisible(lastId
);
953 wxString
wxGenericDirCtrl::GetPath() const
955 wxTreeItemId id
= m_treeCtrl
->GetSelection();
958 wxDirItemDataEx
* data
= (wxDirItemDataEx
*) m_treeCtrl
->GetItemData(id
);
962 return wxEmptyString
;
965 wxString
wxGenericDirCtrl::GetFilePath() const
967 wxTreeItemId id
= m_treeCtrl
->GetSelection();
970 wxDirItemDataEx
* data
= (wxDirItemDataEx
*) m_treeCtrl
->GetItemData(id
);
972 return wxEmptyString
;
977 return wxEmptyString
;
980 void wxGenericDirCtrl::SetPath(const wxString
& path
)
982 m_defaultPath
= path
;
989 void wxGenericDirCtrl::FindChildFiles(wxTreeItemId id
, int dirFlags
, wxArrayString
& filenames
)
991 wxDirItemDataEx
*data
= (wxDirItemDataEx
*) m_treeCtrl
->GetItemData(id
);
993 // This may take a longish time. Go to busy cursor
998 wxString search
,path
,filename
;
1000 wxString
dirName(data
->m_path
);
1002 #if defined(__WXMSW__) || defined(__WXPM__)
1003 if (dirName
.Last() == ':')
1004 dirName
+= wxString(wxFILE_SEP_PATH
);
1008 wxString eachFilename
;
1015 if (d
.GetFirst(& eachFilename
, m_currentFilterStr
, dirFlags
))
1019 if ((eachFilename
!= wxT(".")) && (eachFilename
!= wxT("..")))
1021 filenames
.Add(eachFilename
);
1024 while (d
.GetNext(& eachFilename
)) ;
1030 void wxGenericDirCtrl::SetFilterIndex(int n
)
1032 m_currentFilter
= n
;
1035 if (ExtractWildcard(m_filter
, n
, f
, d
))
1036 m_currentFilterStr
= f
;
1038 m_currentFilterStr
= wxT("*.*");
1041 void wxGenericDirCtrl::SetFilter(const wxString
& filter
)
1046 if (ExtractWildcard(m_filter
, m_currentFilter
, f
, d
))
1047 m_currentFilterStr
= f
;
1049 m_currentFilterStr
= wxT("*.*");
1052 // Extract description and actual filter from overall filter string
1053 bool wxGenericDirCtrl::ExtractWildcard(const wxString
& filterStr
, int n
, wxString
& filter
, wxString
& description
)
1055 wxArrayString filters
, descriptions
;
1056 int count
= ParseFilter(filterStr
, filters
, descriptions
);
1057 if (count
> 0 && n
< count
)
1059 filter
= filters
[n
];
1060 description
= descriptions
[n
];
1067 // Parses the global filter, returning the number of filters.
1068 // Returns 0 if none or if there's a problem.
1069 // filterStr is in the form:
1071 // "All files (*.*)|*.*|JPEG Files (*.jpeg)|*.jpg"
1073 int wxGenericDirCtrl::ParseFilter(const wxString
& filterStr
, wxArrayString
& filters
, wxArrayString
& descriptions
)
1075 wxString
str(filterStr
);
1077 wxString description
, filter
;
1079 bool finished
= FALSE
;
1082 pos
= str
.Find(wxT('|'));
1084 return 0; // Problem
1085 description
= str
.Left(pos
);
1086 str
= str
.Mid(pos
+1);
1087 pos
= str
.Find(wxT('|'));
1095 filter
= str
.Left(pos
);
1096 str
= str
.Mid(pos
+1);
1098 descriptions
.Add(description
);
1099 filters
.Add(filter
);
1103 return filters
.Count();
1106 void wxGenericDirCtrl::DoResize()
1108 wxSize sz
= GetClientSize();
1109 int verticalSpacing
= 3;
1113 if (m_filterListCtrl
)
1115 filterSz
= m_filterListCtrl
->GetSize();
1116 sz
.y
-= (filterSz
.y
+ verticalSpacing
);
1118 m_treeCtrl
->SetSize(0, 0, sz
.x
, sz
.y
);
1119 if (m_filterListCtrl
)
1121 m_filterListCtrl
->SetSize(0, sz
.y
+ verticalSpacing
, sz
.x
, filterSz
.y
);
1122 // Don't know why, but this needs refreshing after a resize (wxMSW)
1123 m_filterListCtrl
->Refresh();
1129 void wxGenericDirCtrl::OnSize(wxSizeEvent
& WXUNUSED(event
))
1134 //-----------------------------------------------------------------------------
1135 // wxDirFilterListCtrl
1136 //-----------------------------------------------------------------------------
1138 IMPLEMENT_CLASS(wxDirFilterListCtrl
, wxChoice
)
1140 BEGIN_EVENT_TABLE(wxDirFilterListCtrl
, wxChoice
)
1141 EVT_CHOICE(-1, wxDirFilterListCtrl::OnSelFilter
)
1144 bool wxDirFilterListCtrl::Create(wxGenericDirCtrl
* parent
, const wxWindowID id
,
1150 return wxChoice::Create(parent
, id
, pos
, size
, 0, NULL
, style
);
1153 void wxDirFilterListCtrl::Init()
1158 void wxDirFilterListCtrl::OnSelFilter(wxCommandEvent
& WXUNUSED(event
))
1160 int sel
= GetSelection();
1162 wxString currentPath
= m_dirCtrl
->GetPath();
1164 m_dirCtrl
->SetFilterIndex(sel
);
1166 // If the filter has changed, the view is out of date, so
1167 // collapse the tree.
1168 m_dirCtrl
->GetTreeCtrl()->Collapse(m_dirCtrl
->GetRootId());
1169 m_dirCtrl
->GetTreeCtrl()->Expand(m_dirCtrl
->GetRootId());
1171 // Try to restore the selection, or at least the directory
1172 m_dirCtrl
->ExpandPath(currentPath
);
1175 void wxDirFilterListCtrl::FillFilterList(const wxString
& filter
, int defaultFilter
)
1178 wxArrayString descriptions
, filters
;
1179 size_t n
= (size_t) m_dirCtrl
->ParseFilter(filter
, filters
, descriptions
);
1181 if (n
> 0 && defaultFilter
< (int) n
)
1184 for (i
= 0; i
< n
; i
++)
1185 Append(descriptions
[i
]);
1186 SetSelection(defaultFilter
);
1190 // wxGenericDirDialog implementation
1191 // This should be moved into dirdlgg.cpp eventually
1193 BEGIN_EVENT_TABLE(wxGenericDirDialog
, wxDialog
)
1194 EVT_BUTTON(wxID_OK
, wxGenericDirDialog::OnOK
)
1195 EVT_BUTTON(wxID_NEW
, wxGenericDirDialog::OnNew
)
1196 EVT_BUTTON (wxID_NEW
, wxGenericDirDialog::OnNew
)
1197 EVT_CLOSE(wxGenericDirDialog::OnCloseWindow
)
1198 EVT_TREE_KEY_DOWN (-1, wxGenericDirDialog::OnTreeKeyDown
)
1199 EVT_TREE_SEL_CHANGED (-1, wxGenericDirDialog::OnTreeSelected
)
1200 EVT_TEXT_ENTER (ID_TEXTCTRL
, wxGenericDirDialog::OnOK
)
1203 wxGenericDirDialog::wxGenericDirDialog(wxWindow
* parent
, const wxString
& title
,
1204 const wxString
& defaultPath
, long style
, const wxPoint
& pos
, const wxSize
& sz
, const wxString
& name
):
1205 wxDialog(parent
, ID_DIRCTRL
, title
, pos
, sz
, style
, name
)
1208 m_path
= defaultPath
;
1210 wxBusyCursor cursor
;
1212 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
1215 m_dirCtrl
= new wxGenericDirCtrl(this, ID_DIRCTRL
,
1216 defaultPath
, wxPoint(5, 5),
1217 wxSize(300, 200), wxDIRCTRL_DIR_ONLY
|wxSUNKEN_BORDER
);
1219 topsizer
->Add( m_dirCtrl
, 1, wxTOP
|wxLEFT
|wxRIGHT
| wxEXPAND
, 10 );
1222 m_input
= new wxTextCtrl( this, ID_TEXTCTRL
, m_path
, wxDefaultPosition
);
1223 topsizer
->Add( m_input
, 0, wxTOP
|wxLEFT
|wxRIGHT
| wxEXPAND
, 10 );
1227 topsizer
->Add( new wxStaticLine( this, -1 ), 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
1231 wxSizer
* buttonsizer
= new wxBoxSizer( wxHORIZONTAL
);
1232 wxButton
* okButton
= new wxButton(this, wxID_OK
, _("OK"));
1233 buttonsizer
->Add( okButton
, 0, wxLEFT
|wxRIGHT
, 10 );
1234 wxButton
* cancelButton
= new wxButton(this, wxID_CANCEL
, _("Cancel"));
1235 buttonsizer
->Add( cancelButton
, 0, wxLEFT
|wxRIGHT
, 10 );
1237 // I'm not convinced we need a New button, and we tend to get annoying
1238 // accidental-editing with label editing enabled.
1240 wxButton
* newButton
= new wxButton( this, wxID_NEW
, _("New...") );
1241 buttonsizer
->Add( newButton
, 0, wxLEFT
|wxRIGHT
, 10 );
1244 topsizer
->Add( buttonsizer
, 0, wxALL
| wxCENTER
, 10 );
1246 okButton
->SetDefault();
1247 m_dirCtrl
->SetFocus();
1249 SetAutoLayout( TRUE
);
1250 SetSizer( topsizer
);
1252 topsizer
->SetSizeHints( this );
1253 topsizer
->Fit( this );
1258 void wxGenericDirDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
1260 EndModal(wxID_CANCEL
);
1263 void wxGenericDirDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
1265 m_path
= m_input
->GetValue();
1266 // Does the path exist? (User may have typed anything in m_input)
1267 if (wxPathExists(m_path
)) {
1268 // OK, path exists, we're done.
1272 // Interact with user, find out if the dir is a typo or to be created
1273 wxString
msg( _("The directory ") );
1275 msg
= msg
+ _("\ndoes not exist\nCreate it now?") ;
1276 wxMessageDialog
dialog(this, msg
, _("Directory does not exist"), wxYES_NO
| wxICON_WARNING
);
1277 if ( dialog
.ShowModal() == wxID_YES
) {
1278 // Okay, let's make it
1280 if (wxMkdir(m_path
)) {
1281 // The new dir was created okay.
1287 msg
= _("Failed to create directory ")+m_path
+
1288 _("\n(Do you have the required permissions?)");
1289 wxMessageDialog
errmsg(this, msg
, _("Error creating directory"), wxOK
| wxICON_ERROR
);
1291 // We still don't have a valid dir. Back to the main dialog.
1294 // User has answered NO to create dir.
1297 void wxGenericDirDialog::SetPath(const wxString
& path
)
1299 m_dirCtrl
->SetPath(path
);
1303 wxString
wxGenericDirDialog::GetPath(void) const
1308 int wxGenericDirDialog::ShowModal()
1310 //m_input->SetValue( m_path );
1311 return wxDialog::ShowModal();
1314 void wxGenericDirDialog::OnTreeSelected( wxTreeEvent
&event
)
1319 wxDirItemDataEx
*data
= (wxDirItemDataEx
*)m_dirCtrl
->GetTreeCtrl()->GetItemData(event
.GetItem());
1321 m_input
->SetValue( data
->m_path
);
1324 void wxGenericDirDialog::OnTreeKeyDown( wxTreeEvent
&WXUNUSED(event
) )
1329 wxDirItemDataEx
*data
= (wxDirItemDataEx
*)m_dirCtrl
->GetTreeCtrl()->GetItemData(m_dirCtrl
->GetTreeCtrl()->GetSelection());
1331 m_input
->SetValue( data
->m_path
);
1334 void wxGenericDirDialog::OnNew( wxCommandEvent
& WXUNUSED(event
) )
1336 wxTreeItemId id
= m_dirCtrl
->GetTreeCtrl()->GetSelection();
1337 if ((id
== m_dirCtrl
->GetTreeCtrl()->GetRootItem()) ||
1338 (m_dirCtrl
->GetTreeCtrl()->GetParent(id
) == m_dirCtrl
->GetTreeCtrl()->GetRootItem()))
1340 wxMessageDialog
msg(this, _("You cannot add a new directory to this section."),
1341 _("Create directory"), wxOK
| wxICON_INFORMATION
);
1346 wxTreeItemId parent
= id
; // m_dirCtrl->GetTreeCtrl()->GetParent( id );
1347 wxDirItemDataEx
*data
= (wxDirItemDataEx
*)m_dirCtrl
->GetTreeCtrl()->GetItemData( parent
);
1350 wxString
new_name( wxT("NewName") );
1351 wxString
path( data
->m_path
);
1352 if (path
.Last() != wxFILE_SEP_PATH
)
1353 path
+= wxFILE_SEP_PATH
;
1355 if (wxFileExists(path
))
1357 // try NewName0, NewName1 etc.
1360 new_name
= wxT("NewName");
1362 num
.Printf( wxT("%d"), i
);
1365 path
= data
->m_path
;
1366 if (path
.Last() != wxFILE_SEP_PATH
)
1367 path
+= wxFILE_SEP_PATH
;
1370 } while (wxFileExists(path
));
1376 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
1381 wxDirItemDataEx
*new_data
= new wxDirItemDataEx( path
, new_name
, TRUE
);
1383 // TODO: THIS CODE DOESN'T WORK YET. We need to avoid duplication of the first child
1385 wxTreeItemId new_id
= m_dirCtrl
->GetTreeCtrl()->AppendItem( parent
, new_name
, 0, 0, new_data
);
1386 m_dirCtrl
->GetTreeCtrl()->EnsureVisible( new_id
);
1387 m_dirCtrl
->GetTreeCtrl()->EditLabel( new_id
);