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
79 #if !defined(__WXMSW__) || wxUSE_XPM_IN_MSW || wxUSE_XPM_IN_OS2
81 static char * icon1_xpm
[] = {
82 /* width height ncolors chars_per_pixel */
110 static char * icon2_xpm
[] = {
111 /* width height ncolors chars_per_pixel */
139 static char * icon3_xpm
[] = {
140 /* width height ncolors chars_per_pixel */
165 static char * icon4_xpm
[] = {
192 static char * icon5_xpm
[] = {
219 static char *icon6_xpm
[] = {
249 static char * icon7_xpm
[] = {
276 static char * icon8_xpm
[] = {
303 static const int ID_DIRCTRL
= 1000;
304 static const int ID_TEXTCTRL
= 1001;
305 static const int ID_OK
= 1002;
306 static const int ID_CANCEL
= 1003;
307 static const int ID_NEW
= 1004;
308 //static const int ID_CHECK = 1005;
310 #if defined(__WXMSW__) || defined(__WXPM__)
311 static bool wxIsDriveAvailable(const wxString dirName
)
314 UINT errorMode
= SetErrorMode(SEM_FAILCRITICALERRORS
| SEM_NOOPENFILEERRORBOX
);
318 // Check if this is a root directory and if so,
319 // whether the drive is avaiable.
320 if (dirName
.Len() == 3 && dirName
[(size_t)1] == wxT(':'))
322 wxString
dirNameLower(dirName
.Lower());
323 #if defined(__GNUWIN32__)
324 success
= wxPathExists(dirNameLower
);
326 int currentDrive
= _getdrive();
327 int thisDrive
= (int) (dirNameLower
[(size_t)0] - 'a' + 1) ;
328 int err
= _chdrive( thisDrive
) ;
329 _chdrive( currentDrive
);
338 (void) SetErrorMode(errorMode
);
345 //-----------------------------------------------------------------------------
347 //-----------------------------------------------------------------------------
349 wxDirItemDataEx::wxDirItemDataEx(const wxString
& path
, const wxString
& name
,
354 /* Insert logic to detect hidden files here
355 * In UnixLand we just check whether the first char is a dot
356 * For FileNameFromPath read LastDirNameInThisPath ;-) */
357 // m_isHidden = (bool)(wxFileNameFromPath(*m_path)[0] == '.');
359 // m_hasSubDirs is no longer needed
360 m_hasSubDirs
= TRUE
; // HasSubDirs();
361 m_isExpanded
= FALSE
;
365 wxDirItemDataEx::~wxDirItemDataEx()
369 void wxDirItemDataEx::SetNewDirName( wxString path
)
372 m_name
= wxFileNameFromPath( path
);
375 //-----------------------------------------------------------------------------
377 //-----------------------------------------------------------------------------
379 IMPLEMENT_DYNAMIC_CLASS(wxGenericDirCtrl
, wxControl
)
381 BEGIN_EVENT_TABLE(wxGenericDirCtrl
, wxControl
)
382 EVT_TREE_ITEM_EXPANDING (-1, wxGenericDirCtrl::OnExpandItem
)
383 EVT_TREE_ITEM_COLLAPSED (-1, wxGenericDirCtrl::OnCollapseItem
)
384 EVT_TREE_BEGIN_LABEL_EDIT (-1, wxGenericDirCtrl::OnBeginEditItem
)
385 EVT_TREE_END_LABEL_EDIT (-1, wxGenericDirCtrl::OnEndEditItem
)
386 EVT_SIZE (wxGenericDirCtrl::OnSize
)
389 wxGenericDirCtrl::wxGenericDirCtrl(void)
394 bool wxGenericDirCtrl::Create(wxWindow
*parent
,
400 const wxString
& filter
,
402 const wxString
& name
)
404 if (!wxControl::Create(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
407 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
411 long treeStyle
= wxTR_HAS_BUTTONS
; // | wxTR_EDIT_LABELS;
412 if ((style
& wxDIRCTRL_3D_INTERNAL
) == 0)
413 treeStyle
|= wxNO_BORDER
;
415 long filterStyle
= 0;
416 if ((style
& wxDIRCTRL_3D_INTERNAL
) == 0)
417 filterStyle
|= wxNO_BORDER
;
419 m_treeCtrl
= new wxTreeCtrl(this, wxID_TREECTRL
, pos
, size
, treeStyle
);
421 if (!filter
.IsEmpty() && (style
& wxDIRCTRL_SHOW_FILTERS
))
422 m_filterListCtrl
= new wxDirFilterListCtrl(this, wxID_FILTERLISTCTRL
, wxDefaultPosition
, wxDefaultSize
, filterStyle
);
427 SetFilterIndex(defaultFilter
);
429 if (m_filterListCtrl
)
430 m_filterListCtrl
->FillFilterList(filter
, defaultFilter
);
432 m_imageList
= new wxImageList(16, 16, TRUE
);
433 #if !defined(__WXMSW__) || wxUSE_XPM_IN_MSW
434 m_imageList
->Add(wxIcon(icon1_xpm
));
435 m_imageList
->Add(wxIcon(icon2_xpm
));
436 m_imageList
->Add(wxIcon(icon3_xpm
));
437 m_imageList
->Add(wxIcon(icon4_xpm
));
438 m_imageList
->Add(wxIcon(icon5_xpm
));
439 m_imageList
->Add(wxIcon(icon6_xpm
));
440 m_imageList
->Add(wxIcon(icon7_xpm
));
441 m_imageList
->Add(wxIcon(icon8_xpm
));
442 #elif defined(__WXMSW__)
443 m_imageList
->Add(wxIcon(wxT("wxICON_SMALL_CLOSED_FOLDER"), wxBITMAP_TYPE_ICO_RESOURCE
));
444 m_imageList
->Add(wxIcon(wxT("wxICON_SMALL_OPEN_FOLDER"), wxBITMAP_TYPE_ICO_RESOURCE
));
445 m_imageList
->Add(wxIcon(wxT("wxICON_SMALL_FILE"), wxBITMAP_TYPE_ICO_RESOURCE
));
446 m_imageList
->Add(wxIcon(wxT("wxICON_SMALL_COMPUTER"), wxBITMAP_TYPE_ICO_RESOURCE
));
447 m_imageList
->Add(wxIcon(wxT("wxICON_SMALL_DRIVE"), wxBITMAP_TYPE_ICO_RESOURCE
));
448 m_imageList
->Add(wxIcon(wxT("wxICON_SMALL_CDROM"), wxBITMAP_TYPE_ICO_RESOURCE
));
449 m_imageList
->Add(wxIcon(wxT("wxICON_SMALL_FLOPPY"), wxBITMAP_TYPE_ICO_RESOURCE
));
450 m_imageList
->Add(wxIcon(wxT("wxICON_SMALL_REMOVEABLE"), wxBITMAP_TYPE_ICO_RESOURCE
));
452 #error "Sorry, we don't have icons available for this platforms."
454 m_treeCtrl
->SetImageList(m_imageList
);
456 m_showHidden
= FALSE
;
457 wxDirItemDataEx
* rootData
= new wxDirItemDataEx(wxT(""), wxT(""), TRUE
);
461 #if defined(__WXMSW__) || defined(__WXPM__)
462 rootName
= _("Computer");
464 rootName
= _("Sections");
467 m_rootId
= m_treeCtrl
->AddRoot( rootName
, 3, -1, rootData
);
468 m_treeCtrl
->SetItemHasChildren(m_rootId
);
469 m_treeCtrl
->Expand(m_rootId
); // automatically expand first level
471 // Expand and select the default path
472 if (!m_defaultPath
.IsEmpty())
473 ExpandPath(m_defaultPath
);
480 wxGenericDirCtrl::~wxGenericDirCtrl()
482 m_treeCtrl
->SetImageList(NULL
);
486 void wxGenericDirCtrl::Init()
488 m_showHidden
= FALSE
;
491 m_currentFilterStr
= wxEmptyString
; // Default: any file
493 m_filterListCtrl
= NULL
;
496 void wxGenericDirCtrl::AddSection(const wxString
& path
, const wxString
& name
, int imageId
)
498 wxDirItemDataEx
*dir_item
= new wxDirItemDataEx(path
,name
,TRUE
);
500 #if defined(__WXMSW__) || defined(__WXPM__)
501 // Windows and OS/2: sections are displayed as drives
502 wxTreeItemId id
= m_treeCtrl
->AppendItem( m_rootId
, name
, imageId
, -1, dir_item
);
504 // Unix: sections are displayed as folders
505 wxTreeItemId id
= m_treeCtrl
->AppendItem( m_rootId
, name
, 0, -1, dir_item
);
506 m_treeCtrl
->SetItemImage( id
, 1, wxTreeItemIcon_Expanded
);
508 // TODO: other operating systems.
510 m_treeCtrl
->SetItemHasChildren(id
);
513 void wxGenericDirCtrl::SetupSections()
515 #if defined(__WXMSW__) || defined(__WXPM__)
518 wxChar driveBuffer
[256];
519 size_t n
= (size_t) GetLogicalDriveStrings(255, driveBuffer
);
524 path
.Printf(wxT("%c:\\"), driveBuffer
[i
]);
525 name
.Printf(wxT("(%c:)"), driveBuffer
[i
]);
528 int driveType
= ::GetDriveType(path
);
531 case DRIVE_REMOVABLE
:
532 if (path
== wxT("a:\\") || path
== wxT("b:\\"))
533 imageId
= 6; // Floppy
551 AddSection(path
, name
, imageId
);
553 while (driveBuffer
[i
] != wxT('\0'))
556 if (driveBuffer
[i
] == wxT('\0'))
563 /* If we can switch to the drive, it exists. */
564 for( drive
= 1; drive
<= 26; drive
++ )
567 path
.Printf(wxT("%c:\\"), (char) (drive
+ 'a' - 1));
568 name
.Printf(wxT("(%c:)"), (char) (drive
+ 'a' - 1));
570 if (wxIsDriveAvailable(path
))
573 AddSection(path
, name
);
579 AddSection(wxT("/"), _("The Computer"), 0);
580 AddSection(wxGetHomeDir(), _("My Home"), 0 );
581 AddSection(wxT("/mnt"), _("Mounted Devices"), 0 );
582 AddSection(wxT("/usr/local"), _("User Local"), 0 );
583 AddSection(wxT("/usr"), _("User"), 0 );
584 AddSection(wxT("/var"), _("Variables"), 0 );
585 AddSection(wxT("/etc"), _("Etcetera"), 0 );
586 AddSection(wxT("/tmp"), _("Temporary"), 0 );
590 void wxGenericDirCtrl::OnBeginEditItem(wxTreeEvent
&event
)
592 // don't rename the main entry "Sections"
593 if (event
.GetItem() == m_rootId
)
599 // don't rename the individual sections
600 if (m_treeCtrl
->GetParent( event
.GetItem() ) == m_rootId
)
607 void wxGenericDirCtrl::OnEndEditItem(wxTreeEvent
&event
)
609 if ((event
.GetLabel().IsEmpty()) ||
610 (event
.GetLabel() == _(".")) ||
611 (event
.GetLabel() == _("..")) ||
612 (event
.GetLabel().First( wxT("/") ) != wxNOT_FOUND
))
614 wxMessageDialog
dialog(this, _("Illegal directory name."), _("Error"), wxOK
| wxICON_ERROR
);
620 wxTreeItemId id
= event
.GetItem();
621 wxDirItemDataEx
*data
= (wxDirItemDataEx
*)m_treeCtrl
->GetItemData( id
);
624 wxString
new_name( wxPathOnly( data
->m_path
) );
625 new_name
+= wxString(wxFILE_SEP_PATH
);
626 new_name
+= event
.GetLabel();
630 if (wxFileExists(new_name
))
632 wxMessageDialog
dialog(this, _("File name exists already."), _("Error"), wxOK
| wxICON_ERROR
);
637 if (wxRenameFile(data
->m_path
,new_name
))
639 data
->SetNewDirName( new_name
);
643 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
649 void wxGenericDirCtrl::OnExpandItem(wxTreeEvent
&event
)
651 wxTreeItemId parentId
= event
.GetItem();
656 void wxGenericDirCtrl::OnCollapseItem(wxTreeEvent
&event
)
658 wxTreeItemId child
, parent
= event
.GetItem();
660 wxDirItemDataEx
*data
= (wxDirItemDataEx
*) m_treeCtrl
->GetItemData(event
.GetItem());
661 if (!data
->m_isExpanded
)
664 data
->m_isExpanded
= FALSE
;
666 /* Workaround because DeleteChildren has disapeared (why?) and
667 * CollapseAndReset doesn't work as advertised (deletes parent too) */
668 child
= m_treeCtrl
->GetFirstChild(parent
, cookie
);
671 m_treeCtrl
->Delete(child
);
672 /* Not GetNextChild below, because the cookie mechanism can't
673 * handle disappearing children! */
674 child
= m_treeCtrl
->GetFirstChild(parent
, cookie
);
678 void wxGenericDirCtrl::ExpandDir(wxTreeItemId parentId
)
680 wxDirItemDataEx
*data
= (wxDirItemDataEx
*) m_treeCtrl
->GetItemData(parentId
);
682 if (data
->m_isExpanded
)
685 data
->m_isExpanded
= TRUE
;
687 if (parentId
== m_rootId
)
695 wxString search
,path
,filename
;
697 wxString
dirName(data
->m_path
);
699 #if defined(__WXMSW__) || defined(__WXPM__)
700 // Check if this is a root directory and if so,
701 // whether the drive is avaiable.
702 if (!wxIsDriveAvailable(dirName
))
704 data
->m_isExpanded
= FALSE
;
705 //wxMessageBox(wxT("Sorry, this drive is not available."));
710 // This may take a longish time. Go to busy cursor
713 #if defined(__WXMSW__) || defined(__WXPM__)
714 if (dirName
.Last() == ':')
715 dirName
+= wxString(wxFILE_SEP_PATH
);
719 wxArrayString filenames
;
722 wxString eachFilename
;
729 if (d
.GetFirst(& eachFilename
, wxEmptyString
, wxDIR_DIRS
))
733 if ((eachFilename
!= wxT(".")) && (eachFilename
!= wxT("..")))
735 dirs
.Add(eachFilename
);
738 while (d
.GetNext(& eachFilename
)) ;
743 // Now do the filenames -- but only if we're allowed to
744 if ((GetWindowStyle() & wxDIRCTRL_DIR_ONLY
) == 0)
752 if (d
.GetFirst(& eachFilename
, m_currentFilterStr
, wxDIR_FILES
))
756 if ((eachFilename
!= wxT(".")) && (eachFilename
!= wxT("..")))
758 filenames
.Add(eachFilename
);
761 while (d
.GetNext(& eachFilename
)) ;
767 // Add the sorted dirs
769 for (i
= 0; i
< dirs
.Count(); i
++)
771 wxString
eachFilename(dirs
[i
]);
773 if (path
.Last() != wxFILE_SEP_PATH
)
774 path
+= wxString(wxFILE_SEP_PATH
);
775 path
+= eachFilename
;
777 wxDirItemDataEx
*dir_item
= new wxDirItemDataEx(path
,eachFilename
,TRUE
);
778 wxTreeItemId id
= m_treeCtrl
->AppendItem( parentId
, eachFilename
, 0, -1, dir_item
);
779 m_treeCtrl
->SetItemImage( id
, 1, wxTreeItemIcon_Expanded
);
781 // Has this got any children? If so, make it expandable.
782 int options
= wxDIR_DEFAULT
;
783 if (GetWindowStyle() & wxDIRCTRL_DIR_ONLY
) // If only showing dirs, then we specify dirs only here
785 options
= wxDIR_DIRS
;
793 // Have to test for wxDIR_DIRS separately in case m_currentFilterStr is non-empty and
794 // and filters out any directories
795 if (dir2
.GetFirst(& str
, m_currentFilterStr
, options
) || dir2
.GetFirst(& str
, wxEmptyString
, wxDIR_DIRS
))
797 m_treeCtrl
->SetItemHasChildren(id
);
802 // Add the sorted filenames
803 if ((GetWindowStyle() & wxDIRCTRL_DIR_ONLY
) == 0)
805 for (i
= 0; i
< filenames
.Count(); i
++)
807 wxString
eachFilename(filenames
[i
]);
809 if (path
.Last() != wxFILE_SEP_PATH
)
810 path
+= wxString(wxFILE_SEP_PATH
);
811 path
+= eachFilename
;
812 //path = dirName + wxString(wxT("/")) + eachFilename;
813 wxDirItemDataEx
*dir_item
= new wxDirItemDataEx(path
,eachFilename
,FALSE
);
814 (void)m_treeCtrl
->AppendItem( parentId
, eachFilename
, 2, -1, dir_item
);
819 // Find the child that matches the first part of 'path'.
820 // E.g. if a child path is "/usr" and 'path' is "/usr/include"
821 // then the child for /usr is returned.
822 wxTreeItemId
wxGenericDirCtrl::FindChild(wxTreeItemId parentId
, const wxString
& path
, bool& done
)
824 wxString
path2(path
);
826 // Make sure all separators are as per the current platform
827 path2
.Replace(wxT("\\"), wxString(wxFILE_SEP_PATH
));
828 path2
.Replace(wxT("/"), wxString(wxFILE_SEP_PATH
));
830 // Append a separator to foil bogus substring matching
831 path2
+= wxString(wxFILE_SEP_PATH
);
833 // In MSW or PM, case is not significant
834 #if defined(__WXMSW__) || defined(__WXPM__)
839 wxTreeItemId childId
= m_treeCtrl
->GetFirstChild(parentId
, cookie
);
840 while (childId
.IsOk())
842 wxDirItemDataEx
* data
= (wxDirItemDataEx
*) m_treeCtrl
->GetItemData(childId
);
844 if (data
&& data
->m_path
!= "")
846 wxString
childPath(data
->m_path
);
847 if (childPath
.Last() != wxFILE_SEP_PATH
)
848 childPath
+= wxString(wxFILE_SEP_PATH
);
850 // In MSW and PM, case is not significant
851 #if defined(__WXMSW__) || defined(__WXPM__)
852 childPath
.MakeLower();
855 if (childPath
.Len() <= path2
.Len())
857 wxString path3
= path2
.Mid(0, childPath
.Len());
858 if (childPath
== path3
)
860 if (path3
.Len() == path2
.Len())
869 childId
= m_treeCtrl
->GetNextChild(childId
, cookie
);
871 wxTreeItemId invalid
;
875 // Try to expand as much of the given path as possible,
876 // and select the given tree item.
877 bool wxGenericDirCtrl::ExpandPath(const wxString
& path
)
880 wxTreeItemId id
= FindChild(m_rootId
, path
, done
);
881 wxTreeItemId lastId
= id
; // The last non-zero id
882 while (id
.IsOk() && !done
)
886 id
= FindChild(id
, path
, done
);
892 wxDirItemDataEx
*data
= (wxDirItemDataEx
*) m_treeCtrl
->GetItemData(lastId
);
895 m_treeCtrl
->Expand(lastId
);
897 if ((GetWindowStyle() & wxDIRCTRL_SELECT_FIRST
) && data
->m_isDir
)
899 // Find the first file in this directory
901 wxTreeItemId childId
= m_treeCtrl
->GetFirstChild(lastId
, cookie
);
902 bool selectedChild
= FALSE
;
903 while (childId
.IsOk())
905 wxDirItemDataEx
* data
= (wxDirItemDataEx
*) m_treeCtrl
->GetItemData(childId
);
907 if (data
&& data
->m_path
!= "" && !data
->m_isDir
)
909 m_treeCtrl
->SelectItem(childId
);
910 m_treeCtrl
->EnsureVisible(childId
);
911 selectedChild
= TRUE
;
914 childId
= m_treeCtrl
->GetNextChild(lastId
, cookie
);
918 m_treeCtrl
->SelectItem(lastId
);
919 m_treeCtrl
->EnsureVisible(lastId
);
924 m_treeCtrl
->SelectItem(lastId
);
925 m_treeCtrl
->EnsureVisible(lastId
);
934 wxString
wxGenericDirCtrl::GetPath() const
936 wxTreeItemId id
= m_treeCtrl
->GetSelection();
939 wxDirItemDataEx
* data
= (wxDirItemDataEx
*) m_treeCtrl
->GetItemData(id
);
943 return wxEmptyString
;
946 wxString
wxGenericDirCtrl::GetFilePath() const
948 wxTreeItemId id
= m_treeCtrl
->GetSelection();
951 wxDirItemDataEx
* data
= (wxDirItemDataEx
*) m_treeCtrl
->GetItemData(id
);
953 return wxEmptyString
;
958 return wxEmptyString
;
961 void wxGenericDirCtrl::SetPath(const wxString
& path
)
963 m_defaultPath
= path
;
970 void wxGenericDirCtrl::FindChildFiles(wxTreeItemId id
, int dirFlags
, wxArrayString
& filenames
)
972 wxDirItemDataEx
*data
= (wxDirItemDataEx
*) m_treeCtrl
->GetItemData(id
);
974 // This may take a longish time. Go to busy cursor
979 wxString search
,path
,filename
;
981 wxString
dirName(data
->m_path
);
983 #if defined(__WXMSW__) || defined(__WXPM__)
984 if (dirName
.Last() == ':')
985 dirName
+= wxString(wxFILE_SEP_PATH
);
989 wxString eachFilename
;
996 if (d
.GetFirst(& eachFilename
, m_currentFilterStr
, dirFlags
))
1000 if ((eachFilename
!= wxT(".")) && (eachFilename
!= wxT("..")))
1002 filenames
.Add(eachFilename
);
1005 while (d
.GetNext(& eachFilename
)) ;
1011 void wxGenericDirCtrl::SetFilterIndex(int n
)
1013 m_currentFilter
= n
;
1016 if (ExtractWildcard(m_filter
, n
, f
, d
))
1017 m_currentFilterStr
= f
;
1019 m_currentFilterStr
= wxT("*.*");
1022 void wxGenericDirCtrl::SetFilter(const wxString
& filter
)
1027 if (ExtractWildcard(m_filter
, m_currentFilter
, f
, d
))
1028 m_currentFilterStr
= f
;
1030 m_currentFilterStr
= wxT("*.*");
1033 // Extract description and actual filter from overall filter string
1034 bool wxGenericDirCtrl::ExtractWildcard(const wxString
& filterStr
, int n
, wxString
& filter
, wxString
& description
)
1036 wxArrayString filters
, descriptions
;
1037 int count
= ParseFilter(filterStr
, filters
, descriptions
);
1038 if (count
> 0 && n
< count
)
1040 filter
= filters
[n
];
1041 description
= descriptions
[n
];
1048 // Parses the global filter, returning the number of filters.
1049 // Returns 0 if none or if there's a problem.
1050 // filterStr is in the form:
1052 // "All files (*.*)|*.*|JPEG Files (*.jpeg)|*.jpg"
1054 int wxGenericDirCtrl::ParseFilter(const wxString
& filterStr
, wxArrayString
& filters
, wxArrayString
& descriptions
)
1056 wxString
str(filterStr
);
1058 wxString description
, filter
;
1060 bool finished
= FALSE
;
1063 pos
= str
.Find(wxT('|'));
1065 return 0; // Problem
1066 description
= str
.Left(pos
);
1067 str
= str
.Mid(pos
+1);
1068 pos
= str
.Find(wxT('|'));
1076 filter
= str
.Left(pos
);
1077 str
= str
.Mid(pos
+1);
1079 descriptions
.Add(description
);
1080 filters
.Add(filter
);
1084 return filters
.Count();
1087 void wxGenericDirCtrl::DoResize()
1089 wxSize sz
= GetClientSize();
1090 int verticalSpacing
= 3;
1094 if (m_filterListCtrl
)
1096 filterSz
= m_filterListCtrl
->GetSize();
1097 sz
.y
-= (filterSz
.y
+ verticalSpacing
);
1099 m_treeCtrl
->SetSize(0, 0, sz
.x
, sz
.y
);
1100 if (m_filterListCtrl
)
1102 m_filterListCtrl
->SetSize(0, sz
.y
+ verticalSpacing
, sz
.x
, filterSz
.y
);
1103 // Don't know why, but this needs refreshing after a resize (wxMSW)
1104 m_filterListCtrl
->Refresh();
1110 void wxGenericDirCtrl::OnSize(wxSizeEvent
&event
)
1115 //-----------------------------------------------------------------------------
1116 // wxDirFilterListCtrl
1117 //-----------------------------------------------------------------------------
1119 IMPLEMENT_CLASS(wxDirFilterListCtrl
, wxChoice
)
1121 BEGIN_EVENT_TABLE(wxDirFilterListCtrl
, wxChoice
)
1122 EVT_CHOICE(-1, wxDirFilterListCtrl::OnSelFilter
)
1125 bool wxDirFilterListCtrl::Create(wxGenericDirCtrl
* parent
, const wxWindowID id
,
1131 return wxChoice::Create(parent
, id
, pos
, size
, 0, NULL
, style
);
1134 void wxDirFilterListCtrl::Init()
1139 void wxDirFilterListCtrl::OnSelFilter(wxCommandEvent
& event
)
1141 int sel
= GetSelection();
1143 wxString currentPath
= m_dirCtrl
->GetPath();
1145 m_dirCtrl
->SetFilterIndex(sel
);
1147 // If the filter has changed, the view is out of date, so
1148 // collapse the tree.
1149 m_dirCtrl
->GetTreeCtrl()->Collapse(m_dirCtrl
->GetRootId());
1150 m_dirCtrl
->GetTreeCtrl()->Expand(m_dirCtrl
->GetRootId());
1152 // Try to restore the selection, or at least the directory
1153 m_dirCtrl
->ExpandPath(currentPath
);
1156 void wxDirFilterListCtrl::FillFilterList(const wxString
& filter
, int defaultFilter
)
1159 wxArrayString descriptions
, filters
;
1160 size_t n
= (size_t) m_dirCtrl
->ParseFilter(filter
, filters
, descriptions
);
1162 if (n
> 0 && defaultFilter
< (int) n
)
1165 for (i
= 0; i
< n
; i
++)
1166 Append(descriptions
[i
]);
1167 SetSelection(defaultFilter
);
1171 // wxGenericDirDialog implementation
1172 // This should be moved into dirdlgg.cpp eventually
1174 BEGIN_EVENT_TABLE(wxGenericDirDialog
, wxDialog
)
1175 EVT_BUTTON(wxID_OK
, wxGenericDirDialog::OnOK
)
1176 EVT_BUTTON(wxID_NEW
, wxGenericDirDialog::OnNew
)
1177 EVT_BUTTON (wxID_NEW
, wxGenericDirDialog::OnNew
)
1178 EVT_CLOSE(wxGenericDirDialog::OnCloseWindow
)
1179 EVT_TREE_KEY_DOWN (-1, wxGenericDirDialog::OnTreeKeyDown
)
1180 EVT_TREE_SEL_CHANGED (-1, wxGenericDirDialog::OnTreeSelected
)
1181 EVT_TEXT_ENTER (ID_TEXTCTRL
, wxGenericDirDialog::OnOK
)
1184 wxGenericDirDialog::wxGenericDirDialog(wxWindow
* parent
, const wxString
& title
,
1185 const wxString
& defaultPath
, long style
, const wxPoint
& pos
, const wxSize
& sz
, const wxString
& name
):
1186 wxDialog(parent
, ID_DIRCTRL
, title
, pos
, sz
, style
, name
)
1189 m_path
= defaultPath
;
1191 wxBusyCursor cursor
;
1193 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
1196 m_dirCtrl
= new wxGenericDirCtrl(this, ID_DIRCTRL
,
1197 defaultPath
, wxPoint(5, 5),
1198 wxSize(300, 200), wxDIRCTRL_DIR_ONLY
|wxSUNKEN_BORDER
);
1200 topsizer
->Add( m_dirCtrl
, 1, wxTOP
|wxLEFT
|wxRIGHT
| wxEXPAND
, 10 );
1203 m_input
= new wxTextCtrl( this, ID_TEXTCTRL
, m_path
, wxDefaultPosition
);
1204 topsizer
->Add( m_input
, 0, wxTOP
|wxLEFT
|wxRIGHT
| wxEXPAND
, 10 );
1208 topsizer
->Add( new wxStaticLine( this, -1 ), 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
1212 wxSizer
* buttonsizer
= new wxBoxSizer( wxHORIZONTAL
);
1213 wxButton
* okButton
= new wxButton(this, wxID_OK
, _("OK"));
1214 buttonsizer
->Add( okButton
, 0, wxLEFT
|wxRIGHT
, 10 );
1215 wxButton
* cancelButton
= new wxButton(this, wxID_CANCEL
, _("Cancel"));
1216 buttonsizer
->Add( cancelButton
, 0, wxLEFT
|wxRIGHT
, 10 );
1218 // I'm not convinced we need a New button, and we tend to get annoying
1219 // accidental-editing with label editing enabled.
1221 wxButton
* newButton
= new wxButton( this, wxID_NEW
, _("New...") );
1222 buttonsizer
->Add( newButton
, 0, wxLEFT
|wxRIGHT
, 10 );
1225 topsizer
->Add( buttonsizer
, 0, wxALL
| wxCENTER
, 10 );
1227 okButton
->SetDefault();
1228 m_dirCtrl
->SetFocus();
1230 SetAutoLayout( TRUE
);
1231 SetSizer( topsizer
);
1233 topsizer
->SetSizeHints( this );
1234 topsizer
->Fit( this );
1239 void wxGenericDirDialog::OnCloseWindow(wxCloseEvent
& event
)
1241 EndModal(wxID_CANCEL
);
1244 void wxGenericDirDialog::OnOK(wxCommandEvent
& event
)
1246 m_path
= m_input
->GetValue();
1247 // Does the path exist? (User may have typed anything in m_input)
1248 if (wxPathExists(m_path
)) {
1249 // OK, path exists, we're done.
1253 // Interact with user, find out if the dir is a typo or to be created
1254 wxString
msg( _("The directory ") );
1256 msg
= msg
+ _("\ndoes not exist\nCreate it now?") ;
1257 wxMessageDialog
dialog(this, msg
, _("Directory does not exist"), wxYES_NO
| wxICON_WARNING
);
1258 if ( dialog
.ShowModal() == wxID_YES
) {
1259 // Okay, let's make it
1261 if (wxMkdir(m_path
)) {
1262 // The new dir was created okay.
1268 msg
= _("Failed to create directory ")+m_path
+
1269 _("\n(Do you have the required permissions?)");
1270 wxMessageDialog
errmsg(this, msg
, _("Error creating directory"), wxOK
| wxICON_ERROR
);
1272 // We still don't have a valid dir. Back to the main dialog.
1275 // User has answered NO to create dir.
1278 void wxGenericDirDialog::SetPath(const wxString
& path
)
1280 m_dirCtrl
->SetPath(path
);
1284 wxString
wxGenericDirDialog::GetPath(void) const
1289 int wxGenericDirDialog::ShowModal()
1291 //m_input->SetValue( m_path );
1292 return wxDialog::ShowModal();
1295 void wxGenericDirDialog::OnTreeSelected( wxTreeEvent
&event
)
1300 wxDirItemDataEx
*data
= (wxDirItemDataEx
*)m_dirCtrl
->GetTreeCtrl()->GetItemData(event
.GetItem());
1302 m_input
->SetValue( data
->m_path
);
1305 void wxGenericDirDialog::OnTreeKeyDown( wxTreeEvent
&WXUNUSED(event
) )
1310 wxDirItemDataEx
*data
= (wxDirItemDataEx
*)m_dirCtrl
->GetTreeCtrl()->GetItemData(m_dirCtrl
->GetTreeCtrl()->GetSelection());
1312 m_input
->SetValue( data
->m_path
);
1315 void wxGenericDirDialog::OnNew( wxCommandEvent
& WXUNUSED(event
) )
1317 wxTreeItemId id
= m_dirCtrl
->GetTreeCtrl()->GetSelection();
1318 if ((id
== m_dirCtrl
->GetTreeCtrl()->GetRootItem()) ||
1319 (m_dirCtrl
->GetTreeCtrl()->GetParent(id
) == m_dirCtrl
->GetTreeCtrl()->GetRootItem()))
1321 wxMessageDialog
msg(this, _("You cannot add a new directory to this section."),
1322 _("Create directory"), wxOK
| wxICON_INFORMATION
);
1327 wxTreeItemId parent
= id
; // m_dirCtrl->GetTreeCtrl()->GetParent( id );
1328 wxDirItemDataEx
*data
= (wxDirItemDataEx
*)m_dirCtrl
->GetTreeCtrl()->GetItemData( parent
);
1331 wxString
new_name( wxT("NewName") );
1332 wxString
path( data
->m_path
);
1333 if (path
.Last() != wxFILE_SEP_PATH
)
1334 path
+= wxFILE_SEP_PATH
;
1336 if (wxFileExists(path
))
1338 // try NewName0, NewName1 etc.
1341 new_name
= wxT("NewName");
1343 num
.Printf( wxT("%d"), i
);
1346 path
= data
->m_path
;
1347 if (path
.Last() != wxFILE_SEP_PATH
)
1348 path
+= wxFILE_SEP_PATH
;
1351 } while (wxFileExists(path
));
1357 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
1362 wxDirItemDataEx
*new_data
= new wxDirItemDataEx( path
, new_name
, TRUE
);
1364 // TODO: THIS CODE DOESN'T WORK YET. We need to avoid duplication of the first child
1366 wxTreeItemId new_id
= m_dirCtrl
->GetTreeCtrl()->AppendItem( parent
, new_name
, 0, 0, new_data
);
1367 m_dirCtrl
->GetTreeCtrl()->EnsureVisible( new_id
);
1368 m_dirCtrl
->GetTreeCtrl()->EditLabel( new_id
);