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"
60 // If compiled under Windows, this macro can cause problems
65 #if !defined(__WXMSW__) || wxUSE_XPM_IN_MSW
67 static char * icon1_xpm
[] = {
68 /* width height ncolors chars_per_pixel */
96 static char * icon2_xpm
[] = {
97 /* width height ncolors chars_per_pixel */
125 static char * icon3_xpm
[] = {
126 /* width height ncolors chars_per_pixel */
151 static char * icon4_xpm
[] = {
178 static char * icon5_xpm
[] = {
205 static char *icon6_xpm
[] = {
235 static char * icon7_xpm
[] = {
262 static char * icon8_xpm
[] = {
289 static const int ID_DIRCTRL
= 1000;
290 static const int ID_TEXTCTRL
= 1001;
291 static const int ID_OK
= 1002;
292 static const int ID_CANCEL
= 1003;
293 static const int ID_NEW
= 1004;
294 //static const int ID_CHECK = 1005;
297 static bool wxIsDriveAvailable(const wxString dirName
)
300 UINT errorMode
= SetErrorMode(SEM_FAILCRITICALERRORS
| SEM_NOOPENFILEERRORBOX
);
304 // Check if this is a root directory and if so,
305 // whether the drive is avaiable.
306 if (dirName
.Len() == 3 && dirName
[(size_t)1] == wxT(':'))
308 wxString
dirNameLower(dirName
.Lower());
309 int currentDrive
= _getdrive();
310 int thisDrive
= (int) (dirNameLower
[(size_t)0] - 'a' + 1) ;
311 int err
= _chdrive( thisDrive
) ;
312 _chdrive( currentDrive
);
320 (void) SetErrorMode(errorMode
);
327 //-----------------------------------------------------------------------------
329 //-----------------------------------------------------------------------------
331 wxDirItemDataEx::wxDirItemDataEx(const wxString
& path
, const wxString
& name
,
336 /* Insert logic to detect hidden files here
337 * In UnixLand we just check whether the first char is a dot
338 * For FileNameFromPath read LastDirNameInThisPath ;-) */
339 // m_isHidden = (bool)(wxFileNameFromPath(*m_path)[0] == '.');
341 // m_hasSubDirs is no longer needed
342 m_hasSubDirs
= TRUE
; // HasSubDirs();
343 m_isExpanded
= FALSE
;
347 wxDirItemDataEx::~wxDirItemDataEx()
351 void wxDirItemDataEx::SetNewDirName( wxString path
)
354 m_name
= wxFileNameFromPath( path
);
357 // No longer used, and takes a very long time
358 bool wxDirItemDataEx::HasSubDirs()
360 if (m_path
.IsEmpty())
363 // On WIN32, must check if this volume is mounted or
364 // we get an error dialog for e.g. drive a:
366 if (!wxIsDriveAvailable(m_path
))
370 wxString search
= m_path
;
372 if (m_path
.Last() != wxFILE_SEP_PATH
)
374 search
+= wxString(wxFILE_SEP_PATH
);
379 wxString path
= wxFindFirstFile( search
, wxDIR
);
380 return (bool)(!path
.IsNull());
383 //-----------------------------------------------------------------------------
385 //-----------------------------------------------------------------------------
387 IMPLEMENT_DYNAMIC_CLASS(wxGenericDirCtrl
, wxControl
)
389 BEGIN_EVENT_TABLE(wxGenericDirCtrl
, wxControl
)
390 EVT_TREE_ITEM_EXPANDING (-1, wxGenericDirCtrl::OnExpandItem
)
391 EVT_TREE_ITEM_COLLAPSED (-1, wxGenericDirCtrl::OnCollapseItem
)
392 EVT_TREE_BEGIN_LABEL_EDIT (-1, wxGenericDirCtrl::OnBeginEditItem
)
393 EVT_TREE_END_LABEL_EDIT (-1, wxGenericDirCtrl::OnEndEditItem
)
394 EVT_SIZE (wxGenericDirCtrl::OnSize
)
397 wxGenericDirCtrl::wxGenericDirCtrl(void)
402 bool wxGenericDirCtrl::Create(wxWindow
*parent
,
408 const wxString
& filter
,
410 const wxString
& name
)
412 if (!wxControl::Create(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
415 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
419 long treeStyle
= wxTR_HAS_BUTTONS
; // | wxTR_EDIT_LABELS;
420 if ((style
& wxDIRCTRL_3D_INTERNAL
) == 0)
421 treeStyle
|= wxNO_BORDER
;
423 long filterStyle
= 0;
424 if ((style
& wxDIRCTRL_3D_INTERNAL
) == 0)
425 filterStyle
|= wxNO_BORDER
;
427 m_treeCtrl
= new wxTreeCtrl(this, wxID_TREECTRL
, pos
, size
, treeStyle
);
429 if (!filter
.IsEmpty() && (style
& wxDIRCTRL_SHOW_FILTERS
))
430 m_filterListCtrl
= new wxDirFilterListCtrl(this, wxID_FILTERLISTCTRL
, wxDefaultPosition
, wxDefaultSize
, filterStyle
);
435 SetFilterIndex(defaultFilter
);
437 if (m_filterListCtrl
)
438 m_filterListCtrl
->FillFilterList(filter
, defaultFilter
);
440 m_imageList
= new wxImageList(16, 16, TRUE
);
441 #if !defined(__WXMSW__) || wxUSE_XPM_IN_MSW
442 m_imageList
->Add(wxIcon(icon1_xpm
));
443 m_imageList
->Add(wxIcon(icon2_xpm
));
444 m_imageList
->Add(wxIcon(icon3_xpm
));
445 m_imageList
->Add(wxIcon(icon4_xpm
));
446 m_imageList
->Add(wxIcon(icon5_xpm
));
447 m_imageList
->Add(wxIcon(icon6_xpm
));
448 m_imageList
->Add(wxIcon(icon7_xpm
));
449 m_imageList
->Add(wxIcon(icon8_xpm
));
450 #elif defined(__WXMSW__)
451 m_imageList
->Add(wxIcon(wxT("wxICON_SMALL_CLOSED_FOLDER"), wxBITMAP_TYPE_ICO_RESOURCE
));
452 m_imageList
->Add(wxIcon(wxT("wxICON_SMALL_OPEN_FOLDER"), wxBITMAP_TYPE_ICO_RESOURCE
));
453 m_imageList
->Add(wxIcon(wxT("wxICON_SMALL_FILE"), wxBITMAP_TYPE_ICO_RESOURCE
));
454 m_imageList
->Add(wxIcon(wxT("wxICON_SMALL_COMPUTER"), wxBITMAP_TYPE_ICO_RESOURCE
));
455 m_imageList
->Add(wxIcon(wxT("wxICON_SMALL_DRIVE"), wxBITMAP_TYPE_ICO_RESOURCE
));
456 m_imageList
->Add(wxIcon(wxT("wxICON_SMALL_CDROM"), wxBITMAP_TYPE_ICO_RESOURCE
));
457 m_imageList
->Add(wxIcon(wxT("wxICON_SMALL_FLOPPY"), wxBITMAP_TYPE_ICO_RESOURCE
));
458 m_imageList
->Add(wxIcon(wxT("wxICON_SMALL_REMOVEABLE"), wxBITMAP_TYPE_ICO_RESOURCE
));
460 #error "Sorry, we don't have icons available for this platforms."
462 m_treeCtrl
->SetImageList(m_imageList
);
464 m_showHidden
= FALSE
;
465 wxDirItemDataEx
* rootData
= new wxDirItemDataEx(wxT(""), wxT(""), TRUE
);
470 rootName
= _("Computer");
472 rootName
= _("Sections");
475 m_rootId
= m_treeCtrl
->AddRoot( rootName
, 3, -1, rootData
);
476 m_treeCtrl
->SetItemHasChildren(m_rootId
);
477 m_treeCtrl
->Expand(m_rootId
); // automatically expand first level
479 // Expand and select the default path
480 if (!m_defaultPath
.IsEmpty())
481 ExpandPath(m_defaultPath
);
488 wxGenericDirCtrl::~wxGenericDirCtrl()
490 m_treeCtrl
->SetImageList(NULL
);
494 void wxGenericDirCtrl::Init()
496 m_showHidden
= FALSE
;
499 m_currentFilterStr
= wxEmptyString
; // Default: any file
501 m_filterListCtrl
= NULL
;
504 void wxGenericDirCtrl::AddSection(const wxString
& path
, const wxString
& name
, int imageId
)
506 wxDirItemDataEx
*dir_item
= new wxDirItemDataEx(path
,name
,TRUE
);
509 // Windows: sections are displayed as drives
510 wxTreeItemId id
= m_treeCtrl
->AppendItem( m_rootId
, name
, imageId
, -1, dir_item
);
512 // Unix: sections are displayed as folders
513 wxTreeItemId id
= m_treeCtrl
->AppendItem( m_rootId
, name
, 0, -1, dir_item
);
514 m_treeCtrl
->SetItemImage( id
, 1, wxTreeItemIcon_Expanded
);
516 // TODO: other operating systems.
518 m_treeCtrl
->SetItemHasChildren(id
);
521 void wxGenericDirCtrl::SetupSections()
526 wxChar driveBuffer
[256];
527 size_t n
= (size_t) GetLogicalDriveStrings(255, driveBuffer
);
532 path
.Printf(wxT("%c:\\"), driveBuffer
[i
]);
533 name
.Printf(wxT("(%c:)"), driveBuffer
[i
]);
536 int driveType
= ::GetDriveType(path
);
539 case DRIVE_REMOVABLE
:
540 if (path
== wxT("a:\\") || path
== wxT("b:\\"))
541 imageId
= 6; // Floppy
559 AddSection(path
, name
, imageId
);
561 while (driveBuffer
[i
] != wxT('\0'))
564 if (driveBuffer
[i
] == wxT('\0'))
571 /* If we can switch to the drive, it exists. */
572 for( drive
= 1; drive
<= 26; drive
++ )
575 path
.Printf(wxT("%c:\\"), (char) (drive
+ 'a' - 1));
576 name
.Printf(wxT("(%c:)"), (char) (drive
+ 'a' - 1));
578 if (wxIsDriveAvailable(path
))
581 AddSection(path
, name
);
587 AddSection(wxT("/"), _("The Computer"), 0);
588 AddSection(wxGetHomeDir(), _("My Home"), 0 );
589 AddSection(wxT("/mnt"), _("Mounted Devices"), 0 );
590 AddSection(wxT("/usr/local"), _("User Local"), 0 );
591 AddSection(wxT("/usr"), _("User"), 0 );
592 AddSection(wxT("/var"), _("Variables"), 0 );
593 AddSection(wxT("/etc"), _("Etcetera"), 0 );
594 AddSection(wxT("/tmp"), _("Temporary"), 0 );
598 void wxGenericDirCtrl::OnBeginEditItem(wxTreeEvent
&event
)
600 // don't rename the main entry "Sections"
601 if (event
.GetItem() == m_rootId
)
607 // don't rename the individual sections
608 if (m_treeCtrl
->GetParent( event
.GetItem() ) == m_rootId
)
615 void wxGenericDirCtrl::OnEndEditItem(wxTreeEvent
&event
)
617 if ((event
.GetLabel().IsEmpty()) ||
618 (event
.GetLabel() == _(".")) ||
619 (event
.GetLabel() == _("..")) ||
620 (event
.GetLabel().First( wxT("/") ) != wxNOT_FOUND
))
622 wxMessageDialog
dialog(this, _("Illegal directory name."), _("Error"), wxOK
| wxICON_ERROR
);
628 wxTreeItemId id
= event
.GetItem();
629 wxDirItemDataEx
*data
= (wxDirItemDataEx
*)m_treeCtrl
->GetItemData( id
);
632 wxString
new_name( wxPathOnly( data
->m_path
) );
633 new_name
+= wxString(wxFILE_SEP_PATH
);
634 new_name
+= event
.GetLabel();
638 if (wxFileExists(new_name
))
640 wxMessageDialog
dialog(this, _("File name exists already."), _("Error"), wxOK
| wxICON_ERROR
);
645 if (wxRenameFile(data
->m_path
,new_name
))
647 data
->SetNewDirName( new_name
);
651 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
657 void wxGenericDirCtrl::OnExpandItem(wxTreeEvent
&event
)
659 wxTreeItemId parentId
= event
.GetItem();
664 void wxGenericDirCtrl::OnCollapseItem(wxTreeEvent
&event
)
666 wxTreeItemId child
, parent
= event
.GetItem();
668 wxDirItemDataEx
*data
= (wxDirItemDataEx
*) m_treeCtrl
->GetItemData(event
.GetItem());
669 if (!data
->m_isExpanded
)
672 data
->m_isExpanded
= FALSE
;
674 /* Workaround because DeleteChildren has disapeared (why?) and
675 * CollapseAndReset doesn't work as advertised (deletes parent too) */
676 child
= m_treeCtrl
->GetFirstChild(parent
, cookie
);
679 m_treeCtrl
->Delete(child
);
680 /* Not GetNextChild below, because the cookie mechanism can't
681 * handle disappearing children! */
682 child
= m_treeCtrl
->GetFirstChild(parent
, cookie
);
686 void wxGenericDirCtrl::ExpandDir(wxTreeItemId parentId
)
688 wxDirItemDataEx
*data
= (wxDirItemDataEx
*) m_treeCtrl
->GetItemData(parentId
);
690 if (data
->m_isExpanded
)
693 data
->m_isExpanded
= TRUE
;
695 if (parentId
== m_rootId
)
703 wxString search
,path
,filename
;
705 wxString
dirName(data
->m_path
);
708 // Check if this is a root directory and if so,
709 // whether the drive is avaiable.
710 if (!wxIsDriveAvailable(dirName
))
712 data
->m_isExpanded
= FALSE
;
713 wxMessageBox(wxT("Sorry, this drive is not available."));
718 // This may take a longish time. Go to busy cursor
722 if (dirName
.Last() == ':')
723 dirName
+= wxString(wxFILE_SEP_PATH
);
727 wxArrayString filenames
;
730 wxString eachFilename
;
737 if (d
.GetFirst(& eachFilename
, wxEmptyString
, wxDIR_DIRS
))
741 if ((eachFilename
!= wxT(".")) && (eachFilename
!= wxT("..")))
743 dirs
.Add(eachFilename
);
746 while (d
.GetNext(& eachFilename
)) ;
751 // Now do the filenames -- but only if we're allowed to
752 if ((GetWindowStyle() & wxDIRCTRL_DIR_ONLY
) == 0)
760 if (d
.GetFirst(& eachFilename
, m_currentFilterStr
, wxDIR_FILES
))
764 if ((eachFilename
!= wxT(".")) && (eachFilename
!= wxT("..")))
766 filenames
.Add(eachFilename
);
769 while (d
.GetNext(& eachFilename
)) ;
775 // Add the sorted dirs
777 for (i
= 0; i
< dirs
.Count(); i
++)
779 wxString
eachFilename(dirs
[i
]);
781 if (path
.Last() != wxFILE_SEP_PATH
)
782 path
+= wxString(wxFILE_SEP_PATH
);
783 path
+= eachFilename
;
785 wxDirItemDataEx
*dir_item
= new wxDirItemDataEx(path
,eachFilename
,TRUE
);
786 wxTreeItemId id
= m_treeCtrl
->AppendItem( parentId
, eachFilename
, 0, -1, dir_item
);
787 m_treeCtrl
->SetItemImage( id
, 1, wxTreeItemIcon_Expanded
);
789 // Has this got any children? If so, make it expandable.
790 int options
= wxDIR_DEFAULT
;
791 if (GetWindowStyle() & wxDIRCTRL_DIR_ONLY
) // If only showing dirs, then we specify dirs only here
793 options
= wxDIR_DIRS
;
801 // Have to test for wxDIR_DIRS separately in case m_currentFilterStr is non-empty and
802 // and filters out any directories
803 if (dir2
.GetFirst(& str
, m_currentFilterStr
, options
) || dir2
.GetFirst(& str
, wxEmptyString
, wxDIR_DIRS
))
805 m_treeCtrl
->SetItemHasChildren(id
);
810 // Add the sorted filenames
811 if ((GetWindowStyle() & wxDIRCTRL_DIR_ONLY
) == 0)
813 for (i
= 0; i
< filenames
.Count(); i
++)
815 wxString
eachFilename(filenames
[i
]);
817 if (path
.Last() != wxFILE_SEP_PATH
)
818 path
+= wxString(wxFILE_SEP_PATH
);
819 path
+= eachFilename
;
820 //path = dirName + wxString(wxT("/")) + eachFilename;
821 wxDirItemDataEx
*dir_item
= new wxDirItemDataEx(path
,eachFilename
,FALSE
);
822 (void)m_treeCtrl
->AppendItem( parentId
, eachFilename
, 2, -1, dir_item
);
827 // Find the child that matches the first part of 'path'.
828 // E.g. if a child path is "/usr" and 'path' is "/usr/include"
829 // then the child for /usr is returned.
830 wxTreeItemId
wxGenericDirCtrl::FindChild(wxTreeItemId parentId
, const wxString
& path
, bool& done
)
832 wxString
path2(path
);
834 // Make sure all separators are as per the current platform
835 path2
.Replace(wxT("\\"), wxString(wxFILE_SEP_PATH
));
836 path2
.Replace(wxT("/"), wxString(wxFILE_SEP_PATH
));
838 // Append a separator to foil bogus substring matching
839 path2
+= wxString(wxFILE_SEP_PATH
);
841 // In MSW, case is not significant
847 wxTreeItemId childId
= m_treeCtrl
->GetFirstChild(parentId
, cookie
);
850 wxDirItemDataEx
* data
= (wxDirItemDataEx
*) m_treeCtrl
->GetItemData(childId
);
852 if (data
&& data
->m_path
!= "")
854 wxString
childPath(data
->m_path
);
855 if (childPath
.Last() != wxFILE_SEP_PATH
)
856 childPath
+= wxString(wxFILE_SEP_PATH
);
858 // In MSW, case is not significant
860 childPath
.MakeLower();
863 if (childPath
.Len() <= path2
.Len())
865 wxString path3
= path2
.Mid(0, childPath
.Len());
866 if (childPath
== path3
)
868 if (path3
.Len() == path2
.Len())
877 childId
= m_treeCtrl
->GetNextChild(childId
, cookie
);
879 wxTreeItemId invalid
;
883 // Try to expand as much of the given path as possible,
884 // and select the given tree item.
885 bool wxGenericDirCtrl::ExpandPath(const wxString
& path
)
888 wxTreeItemId id
= FindChild(m_rootId
, path
, done
);
889 wxTreeItemId lastId
= id
; // The last non-zero id
890 while ((id
> 0) && !done
)
894 id
= FindChild(id
, path
, done
);
900 wxDirItemDataEx
*data
= (wxDirItemDataEx
*) m_treeCtrl
->GetItemData(lastId
);
903 m_treeCtrl
->Expand(lastId
);
905 if ((GetWindowStyle() & wxDIRCTRL_SELECT_FIRST
) && data
->m_isDir
)
907 // Find the first file in this directory
909 wxTreeItemId childId
= m_treeCtrl
->GetFirstChild(lastId
, cookie
);
910 bool selectedChild
= FALSE
;
913 wxDirItemDataEx
* data
= (wxDirItemDataEx
*) m_treeCtrl
->GetItemData(childId
);
915 if (data
&& data
->m_path
!= "" && !data
->m_isDir
)
917 m_treeCtrl
->SelectItem(childId
);
918 m_treeCtrl
->EnsureVisible(childId
);
919 selectedChild
= TRUE
;
922 childId
= m_treeCtrl
->GetNextChild(lastId
, cookie
);
926 m_treeCtrl
->SelectItem(lastId
);
927 m_treeCtrl
->EnsureVisible(lastId
);
932 m_treeCtrl
->SelectItem(lastId
);
933 m_treeCtrl
->EnsureVisible(lastId
);
942 wxString
wxGenericDirCtrl::GetPath() const
944 wxTreeItemId id
= m_treeCtrl
->GetSelection();
947 wxDirItemDataEx
* data
= (wxDirItemDataEx
*) m_treeCtrl
->GetItemData(id
);
951 return wxEmptyString
;
954 wxString
wxGenericDirCtrl::GetFilePath() const
956 wxTreeItemId id
= m_treeCtrl
->GetSelection();
959 wxDirItemDataEx
* data
= (wxDirItemDataEx
*) m_treeCtrl
->GetItemData(id
);
961 return wxEmptyString
;
966 return wxEmptyString
;
969 void wxGenericDirCtrl::SetPath(const wxString
& path
)
971 m_defaultPath
= path
;
978 void wxGenericDirCtrl::FindChildFiles(wxTreeItemId id
, int dirFlags
, wxArrayString
& filenames
)
980 wxDirItemDataEx
*data
= (wxDirItemDataEx
*) m_treeCtrl
->GetItemData(id
);
982 // This may take a longish time. Go to busy cursor
987 wxString search
,path
,filename
;
989 wxString
dirName(data
->m_path
);
992 if (dirName
.Last() == ':')
993 dirName
+= wxString(wxFILE_SEP_PATH
);
997 wxString eachFilename
;
1004 if (d
.GetFirst(& eachFilename
, m_currentFilterStr
, dirFlags
))
1008 if ((eachFilename
!= wxT(".")) && (eachFilename
!= wxT("..")))
1010 filenames
.Add(eachFilename
);
1013 while (d
.GetNext(& eachFilename
)) ;
1019 void wxGenericDirCtrl::SetFilterIndex(int n
)
1021 m_currentFilter
= n
;
1024 if (ExtractWildcard(m_filter
, n
, f
, d
))
1025 m_currentFilterStr
= f
;
1027 m_currentFilterStr
= wxT("*.*");
1030 void wxGenericDirCtrl::SetFilter(const wxString
& filter
)
1035 if (ExtractWildcard(m_filter
, m_currentFilter
, f
, d
))
1036 m_currentFilterStr
= f
;
1038 m_currentFilterStr
= wxT("*.*");
1041 // Extract description and actual filter from overall filter string
1042 bool wxGenericDirCtrl::ExtractWildcard(const wxString
& filterStr
, int n
, wxString
& filter
, wxString
& description
)
1044 wxArrayString filters
, descriptions
;
1045 int count
= ParseFilter(filterStr
, filters
, descriptions
);
1046 if (count
> 0 && n
< count
)
1048 filter
= filters
[n
];
1049 description
= descriptions
[n
];
1056 // Parses the global filter, returning the number of filters.
1057 // Returns 0 if none or if there's a problem.
1058 // filterStr is in the form:
1060 // "All files (*.*)|*.*|JPEG Files (*.jpeg)|*.jpg"
1062 int wxGenericDirCtrl::ParseFilter(const wxString
& filterStr
, wxArrayString
& filters
, wxArrayString
& descriptions
)
1064 wxString
str(filterStr
);
1066 wxString description
, filter
;
1068 bool finished
= FALSE
;
1071 pos
= str
.Find(wxT('|'));
1073 return 0; // Problem
1074 description
= str
.Left(pos
);
1075 str
= str
.Mid(pos
+1);
1076 pos
= str
.Find(wxT('|'));
1084 filter
= str
.Left(pos
);
1085 str
= str
.Mid(pos
+1);
1087 descriptions
.Add(description
);
1088 filters
.Add(filter
);
1092 return filters
.Count();
1095 void wxGenericDirCtrl::DoResize()
1097 wxSize sz
= GetClientSize();
1098 int verticalSpacing
= 3;
1102 if (m_filterListCtrl
)
1104 filterSz
= m_filterListCtrl
->GetSize();
1105 sz
.y
-= (filterSz
.y
+ verticalSpacing
);
1107 m_treeCtrl
->SetSize(0, 0, sz
.x
, sz
.y
);
1108 if (m_filterListCtrl
)
1110 m_filterListCtrl
->SetSize(0, sz
.y
+ verticalSpacing
, sz
.x
, filterSz
.y
);
1111 // Don't know why, but this needs refreshing after a resize (wxMSW)
1112 m_filterListCtrl
->Refresh();
1118 void wxGenericDirCtrl::OnSize(wxSizeEvent
&event
)
1123 //-----------------------------------------------------------------------------
1124 // wxDirFilterListCtrl
1125 //-----------------------------------------------------------------------------
1127 IMPLEMENT_CLASS(wxDirFilterListCtrl
, wxChoice
)
1129 BEGIN_EVENT_TABLE(wxDirFilterListCtrl
, wxChoice
)
1130 EVT_CHOICE(-1, wxDirFilterListCtrl::OnSelFilter
)
1133 bool wxDirFilterListCtrl::Create(wxGenericDirCtrl
* parent
, const wxWindowID id
,
1139 return wxChoice::Create(parent
, id
, pos
, size
, 0, NULL
, style
);
1142 void wxDirFilterListCtrl::Init()
1147 void wxDirFilterListCtrl::OnSelFilter(wxCommandEvent
& event
)
1149 int sel
= GetSelection();
1151 wxString currentPath
= m_dirCtrl
->GetPath();
1153 m_dirCtrl
->SetFilterIndex(sel
);
1155 // If the filter has changed, the view is out of date, so
1156 // collapse the tree.
1157 m_dirCtrl
->GetTreeCtrl()->Collapse(m_dirCtrl
->GetRootId());
1158 m_dirCtrl
->GetTreeCtrl()->Expand(m_dirCtrl
->GetRootId());
1160 // Try to restore the selection, or at least the directory
1161 m_dirCtrl
->ExpandPath(currentPath
);
1164 void wxDirFilterListCtrl::FillFilterList(const wxString
& filter
, int defaultFilter
)
1167 wxArrayString descriptions
, filters
;
1168 size_t n
= (size_t) m_dirCtrl
->ParseFilter(filter
, filters
, descriptions
);
1170 if (n
> 0 && defaultFilter
< (int) n
)
1173 for (i
= 0; i
< n
; i
++)
1174 Append(descriptions
[i
]);
1175 SetSelection(defaultFilter
);
1179 // wxGenericDirDialog implementation
1180 // This should be moved into dirdlgg.cpp eventually
1182 BEGIN_EVENT_TABLE(wxGenericDirDialog
, wxDialog
)
1183 EVT_BUTTON(wxID_OK
, wxGenericDirDialog::OnOK
)
1184 EVT_BUTTON(wxID_NEW
, wxGenericDirDialog::OnNew
)
1185 EVT_BUTTON (wxID_NEW
, wxGenericDirDialog::OnNew
)
1186 EVT_CLOSE(wxGenericDirDialog::OnCloseWindow
)
1187 EVT_TREE_KEY_DOWN (-1, wxGenericDirDialog::OnTreeKeyDown
)
1188 EVT_TREE_SEL_CHANGED (-1, wxGenericDirDialog::OnTreeSelected
)
1189 EVT_TEXT_ENTER (ID_TEXTCTRL
, wxGenericDirDialog::OnOK
)
1192 wxGenericDirDialog::wxGenericDirDialog(wxWindow
* parent
, const wxString
& title
,
1193 const wxString
& defaultPath
, long style
, const wxPoint
& pos
, const wxSize
& sz
, const wxString
& name
):
1194 wxDialog(parent
, ID_DIRCTRL
, title
, pos
, sz
, style
, name
)
1197 m_path
= defaultPath
;
1199 wxBusyCursor cursor
;
1201 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
1204 m_dirCtrl
= new wxGenericDirCtrl(this, ID_DIRCTRL
,
1205 defaultPath
, wxPoint(5, 5),
1206 wxSize(300, 200), wxDIRCTRL_DIR_ONLY
|wxSUNKEN_BORDER
);
1208 topsizer
->Add( m_dirCtrl
, 1, wxTOP
|wxLEFT
|wxRIGHT
| wxEXPAND
, 10 );
1211 m_input
= new wxTextCtrl( this, ID_TEXTCTRL
, m_path
, wxDefaultPosition
);
1212 topsizer
->Add( m_input
, 0, wxTOP
|wxLEFT
|wxRIGHT
| wxEXPAND
, 10 );
1216 topsizer
->Add( new wxStaticLine( this, -1 ), 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
1220 wxSizer
* buttonsizer
= new wxBoxSizer( wxHORIZONTAL
);
1221 wxButton
* okButton
= new wxButton(this, wxID_OK
, _("OK"));
1222 buttonsizer
->Add( okButton
, 0, wxLEFT
|wxRIGHT
, 10 );
1223 wxButton
* cancelButton
= new wxButton(this, wxID_CANCEL
, _("Cancel"));
1224 buttonsizer
->Add( cancelButton
, 0, wxLEFT
|wxRIGHT
, 10 );
1226 // I'm not convinced we need a New button, and we tend to get annoying
1227 // accidental-editing with label editing enabled.
1229 wxButton
* newButton
= new wxButton( this, wxID_NEW
, _("New...") );
1230 buttonsizer
->Add( newButton
, 0, wxLEFT
|wxRIGHT
, 10 );
1233 topsizer
->Add( buttonsizer
, 0, wxALL
| wxCENTER
, 10 );
1235 okButton
->SetDefault();
1236 m_dirCtrl
->SetFocus();
1238 SetAutoLayout( TRUE
);
1239 SetSizer( topsizer
);
1241 topsizer
->SetSizeHints( this );
1242 topsizer
->Fit( this );
1247 void wxGenericDirDialog::OnCloseWindow(wxCloseEvent
& event
)
1249 EndModal(wxID_CANCEL
);
1252 void wxGenericDirDialog::OnOK(wxCommandEvent
& event
)
1254 m_path
= m_input
->GetValue();
1255 // Does the path exist? (User may have typed anything in m_input)
1256 if (wxPathExists(m_path
)) {
1257 // OK, path exists, we're done.
1261 // Interact with user, find out if the dir is a typo or to be created
1262 wxString
msg( _("The directory ") );
1264 msg
= msg
+ _("\ndoes not exist\nCreate it now?") ;
1265 wxMessageDialog
dialog(this, msg
, _("Directory does not exist"), wxYES_NO
| wxICON_WARNING
);
1266 if ( dialog
.ShowModal() == wxID_YES
) {
1267 // Okay, let's make it
1269 if (wxMkdir(m_path
)) {
1270 // The new dir was created okay.
1276 msg
= _("Failed to create directory ")+m_path
+
1277 _("\n(Do you have the required permissions?)");
1278 wxMessageDialog
errmsg(this, msg
, _("Error creating directory"), wxOK
| wxICON_ERROR
);
1280 // We still don't have a valid dir. Back to the main dialog.
1283 // User has answered NO to create dir.
1286 void wxGenericDirDialog::SetPath(const wxString
& path
)
1288 m_dirCtrl
->SetPath(path
);
1292 wxString
wxGenericDirDialog::GetPath(void) const
1297 int wxGenericDirDialog::ShowModal()
1299 //m_input->SetValue( m_path );
1300 return wxDialog::ShowModal();
1303 void wxGenericDirDialog::OnTreeSelected( wxTreeEvent
&event
)
1308 wxDirItemDataEx
*data
= (wxDirItemDataEx
*)m_dirCtrl
->GetTreeCtrl()->GetItemData(event
.GetItem());
1310 m_input
->SetValue( data
->m_path
);
1313 void wxGenericDirDialog::OnTreeKeyDown( wxTreeEvent
&WXUNUSED(event
) )
1318 wxDirItemDataEx
*data
= (wxDirItemDataEx
*)m_dirCtrl
->GetTreeCtrl()->GetItemData(m_dirCtrl
->GetTreeCtrl()->GetSelection());
1320 m_input
->SetValue( data
->m_path
);
1323 void wxGenericDirDialog::OnNew( wxCommandEvent
& WXUNUSED(event
) )
1325 wxTreeItemId id
= m_dirCtrl
->GetTreeCtrl()->GetSelection();
1326 if ((id
== m_dirCtrl
->GetTreeCtrl()->GetRootItem()) ||
1327 (m_dirCtrl
->GetTreeCtrl()->GetParent(id
) == m_dirCtrl
->GetTreeCtrl()->GetRootItem()))
1329 wxMessageDialog
msg(this, _("You cannot add a new directory to this section."),
1330 _("Create directory"), wxOK
| wxICON_INFORMATION
);
1335 wxTreeItemId parent
= id
; // m_dirCtrl->GetTreeCtrl()->GetParent( id );
1336 wxDirItemDataEx
*data
= (wxDirItemDataEx
*)m_dirCtrl
->GetTreeCtrl()->GetItemData( parent
);
1339 wxString
new_name( wxT("NewName") );
1340 wxString
path( data
->m_path
);
1341 if (path
.Last() != wxFILE_SEP_PATH
)
1342 path
+= wxFILE_SEP_PATH
;
1344 if (wxFileExists(path
))
1346 // try NewName0, NewName1 etc.
1349 new_name
= wxT("NewName");
1351 num
.Printf( wxT("%d"), i
);
1354 path
= data
->m_path
;
1355 if (path
.Last() != wxFILE_SEP_PATH
)
1356 path
+= wxFILE_SEP_PATH
;
1359 } while (wxFileExists(path
));
1365 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
1370 wxDirItemDataEx
*new_data
= new wxDirItemDataEx( path
, new_name
, TRUE
);
1372 // TODO: THIS CODE DOESN'T WORK YET. We need to avoid duplication of the first child
1374 wxTreeItemId new_id
= m_dirCtrl
->GetTreeCtrl()->AppendItem( parent
, new_name
, 0, 0, new_data
);
1375 m_dirCtrl
->GetTreeCtrl()->EnsureVisible( new_id
);
1376 m_dirCtrl
->GetTreeCtrl()->EditLabel( new_id
);