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 int setdrive(int drive
)
315 if (drive
< 1 || drive
> 31)
317 newdrive
[0] = (char)('A' + (char)drive
- (char)1);
320 #if defined(__WXMSW__)
321 if (SetCurrentDirectory((LPSTR
)newdrive
))
323 // VA doesn't know what LPSTR is and has its own set
324 if (DosSetCurrentDir((PSZ
)newdrive
))
331 static bool wxIsDriveAvailable(const wxString dirName
)
334 UINT errorMode
= SetErrorMode(SEM_FAILCRITICALERRORS
| SEM_NOOPENFILEERRORBOX
);
338 // Check if this is a root directory and if so,
339 // whether the drive is avaiable.
340 if (dirName
.Len() == 3 && dirName
[(size_t)1] == wxT(':'))
342 wxString
dirNameLower(dirName
.Lower());
343 #if defined(__GNUWIN32__)
344 success
= wxPathExists(dirNameLower
);
346 int currentDrive
= _getdrive();
347 int thisDrive
= (int) (dirNameLower
[(size_t)0] - 'a' + 1) ;
348 int err
= setdrive( thisDrive
) ;
349 setdrive( currentDrive
);
358 (void) SetErrorMode(errorMode
);
365 //-----------------------------------------------------------------------------
367 //-----------------------------------------------------------------------------
369 wxDirItemDataEx::wxDirItemDataEx(const wxString
& path
, const wxString
& name
,
374 /* Insert logic to detect hidden files here
375 * In UnixLand we just check whether the first char is a dot
376 * For FileNameFromPath read LastDirNameInThisPath ;-) */
377 // m_isHidden = (bool)(wxFileNameFromPath(*m_path)[0] == '.');
379 // m_hasSubDirs is no longer needed
380 m_hasSubDirs
= TRUE
; // HasSubDirs();
381 m_isExpanded
= FALSE
;
385 wxDirItemDataEx::~wxDirItemDataEx()
389 void wxDirItemDataEx::SetNewDirName( wxString path
)
392 m_name
= wxFileNameFromPath( path
);
395 //-----------------------------------------------------------------------------
397 //-----------------------------------------------------------------------------
399 IMPLEMENT_DYNAMIC_CLASS(wxGenericDirCtrl
, wxControl
)
401 BEGIN_EVENT_TABLE(wxGenericDirCtrl
, wxControl
)
402 EVT_TREE_ITEM_EXPANDING (-1, wxGenericDirCtrl::OnExpandItem
)
403 EVT_TREE_ITEM_COLLAPSED (-1, wxGenericDirCtrl::OnCollapseItem
)
404 EVT_TREE_BEGIN_LABEL_EDIT (-1, wxGenericDirCtrl::OnBeginEditItem
)
405 EVT_TREE_END_LABEL_EDIT (-1, wxGenericDirCtrl::OnEndEditItem
)
406 EVT_SIZE (wxGenericDirCtrl::OnSize
)
409 wxGenericDirCtrl::wxGenericDirCtrl(void)
414 bool wxGenericDirCtrl::Create(wxWindow
*parent
,
420 const wxString
& filter
,
422 const wxString
& name
)
424 if (!wxControl::Create(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
427 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
431 long treeStyle
= wxTR_HAS_BUTTONS
; // | wxTR_EDIT_LABELS;
432 if ((style
& wxDIRCTRL_3D_INTERNAL
) == 0)
433 treeStyle
|= wxNO_BORDER
;
435 long filterStyle
= 0;
436 if ((style
& wxDIRCTRL_3D_INTERNAL
) == 0)
437 filterStyle
|= wxNO_BORDER
;
439 m_treeCtrl
= new wxTreeCtrl(this, wxID_TREECTRL
, pos
, size
, treeStyle
);
441 if (!filter
.IsEmpty() && (style
& wxDIRCTRL_SHOW_FILTERS
))
442 m_filterListCtrl
= new wxDirFilterListCtrl(this, wxID_FILTERLISTCTRL
, wxDefaultPosition
, wxDefaultSize
, filterStyle
);
447 SetFilterIndex(defaultFilter
);
449 if (m_filterListCtrl
)
450 m_filterListCtrl
->FillFilterList(filter
, defaultFilter
);
452 m_imageList
= new wxImageList(16, 16, TRUE
);
453 #if !defined(__WXMSW__) || wxUSE_XPM_IN_MSW
454 m_imageList
->Add(wxIcon(icon1_xpm
));
455 m_imageList
->Add(wxIcon(icon2_xpm
));
456 m_imageList
->Add(wxIcon(icon3_xpm
));
457 m_imageList
->Add(wxIcon(icon4_xpm
));
458 m_imageList
->Add(wxIcon(icon5_xpm
));
459 m_imageList
->Add(wxIcon(icon6_xpm
));
460 m_imageList
->Add(wxIcon(icon7_xpm
));
461 m_imageList
->Add(wxIcon(icon8_xpm
));
462 #elif defined(__WXMSW__)
463 m_imageList
->Add(wxIcon(wxT("wxICON_SMALL_CLOSED_FOLDER"), wxBITMAP_TYPE_ICO_RESOURCE
));
464 m_imageList
->Add(wxIcon(wxT("wxICON_SMALL_OPEN_FOLDER"), wxBITMAP_TYPE_ICO_RESOURCE
));
465 m_imageList
->Add(wxIcon(wxT("wxICON_SMALL_FILE"), wxBITMAP_TYPE_ICO_RESOURCE
));
466 m_imageList
->Add(wxIcon(wxT("wxICON_SMALL_COMPUTER"), wxBITMAP_TYPE_ICO_RESOURCE
));
467 m_imageList
->Add(wxIcon(wxT("wxICON_SMALL_DRIVE"), wxBITMAP_TYPE_ICO_RESOURCE
));
468 m_imageList
->Add(wxIcon(wxT("wxICON_SMALL_CDROM"), wxBITMAP_TYPE_ICO_RESOURCE
));
469 m_imageList
->Add(wxIcon(wxT("wxICON_SMALL_FLOPPY"), wxBITMAP_TYPE_ICO_RESOURCE
));
470 m_imageList
->Add(wxIcon(wxT("wxICON_SMALL_REMOVEABLE"), wxBITMAP_TYPE_ICO_RESOURCE
));
472 #error "Sorry, we don't have icons available for this platforms."
474 m_treeCtrl
->SetImageList(m_imageList
);
476 m_showHidden
= FALSE
;
477 wxDirItemDataEx
* rootData
= new wxDirItemDataEx(wxT(""), wxT(""), TRUE
);
481 #if defined(__WXMSW__) || defined(__WXPM__)
482 rootName
= _("Computer");
484 rootName
= _("Sections");
487 m_rootId
= m_treeCtrl
->AddRoot( rootName
, 3, -1, rootData
);
488 m_treeCtrl
->SetItemHasChildren(m_rootId
);
489 m_treeCtrl
->Expand(m_rootId
); // automatically expand first level
491 // Expand and select the default path
492 if (!m_defaultPath
.IsEmpty())
493 ExpandPath(m_defaultPath
);
500 wxGenericDirCtrl::~wxGenericDirCtrl()
502 m_treeCtrl
->SetImageList(NULL
);
506 void wxGenericDirCtrl::Init()
508 m_showHidden
= FALSE
;
511 m_currentFilterStr
= wxEmptyString
; // Default: any file
513 m_filterListCtrl
= NULL
;
516 void wxGenericDirCtrl::AddSection(const wxString
& path
, const wxString
& name
, int imageId
)
518 wxDirItemDataEx
*dir_item
= new wxDirItemDataEx(path
,name
,TRUE
);
520 #if defined(__WXMSW__) || defined(__WXPM__)
521 // Windows and OS/2: sections are displayed as drives
522 wxTreeItemId id
= m_treeCtrl
->AppendItem( m_rootId
, name
, imageId
, -1, dir_item
);
524 // Unix: sections are displayed as folders
525 wxTreeItemId id
= m_treeCtrl
->AppendItem( m_rootId
, name
, 0, -1, dir_item
);
526 m_treeCtrl
->SetItemImage( id
, 1, wxTreeItemIcon_Expanded
);
528 // TODO: other operating systems.
530 m_treeCtrl
->SetItemHasChildren(id
);
533 void wxGenericDirCtrl::SetupSections()
535 #if defined(__WXMSW__) || defined(__WXPM__)
538 wxChar driveBuffer
[256];
539 size_t n
= (size_t) GetLogicalDriveStrings(255, driveBuffer
);
544 path
.Printf(wxT("%c:\\"), driveBuffer
[i
]);
545 name
.Printf(wxT("(%c:)"), driveBuffer
[i
]);
548 int driveType
= ::GetDriveType(path
);
551 case DRIVE_REMOVABLE
:
552 if (path
== wxT("a:\\") || path
== wxT("b:\\"))
553 imageId
= 6; // Floppy
571 AddSection(path
, name
, imageId
);
573 while (driveBuffer
[i
] != wxT('\0'))
576 if (driveBuffer
[i
] == wxT('\0'))
583 /* If we can switch to the drive, it exists. */
584 for( drive
= 1; drive
<= 26; drive
++ )
587 path
.Printf(wxT("%c:\\"), (char) (drive
+ 'a' - 1));
588 name
.Printf(wxT("(%c:)"), (char) (drive
+ 'a' - 1));
590 if (wxIsDriveAvailable(path
))
593 AddSection(path
, name
);
599 AddSection(wxT("/"), _("The Computer"), 0);
600 AddSection(wxGetHomeDir(), _("My Home"), 0 );
601 AddSection(wxT("/mnt"), _("Mounted Devices"), 0 );
602 AddSection(wxT("/usr/local"), _("User Local"), 0 );
603 AddSection(wxT("/usr"), _("User"), 0 );
604 AddSection(wxT("/var"), _("Variables"), 0 );
605 AddSection(wxT("/etc"), _("Etcetera"), 0 );
606 AddSection(wxT("/tmp"), _("Temporary"), 0 );
610 void wxGenericDirCtrl::OnBeginEditItem(wxTreeEvent
&event
)
612 // don't rename the main entry "Sections"
613 if (event
.GetItem() == m_rootId
)
619 // don't rename the individual sections
620 if (m_treeCtrl
->GetParent( event
.GetItem() ) == m_rootId
)
627 void wxGenericDirCtrl::OnEndEditItem(wxTreeEvent
&event
)
629 if ((event
.GetLabel().IsEmpty()) ||
630 (event
.GetLabel() == _(".")) ||
631 (event
.GetLabel() == _("..")) ||
632 (event
.GetLabel().First( wxT("/") ) != wxNOT_FOUND
))
634 wxMessageDialog
dialog(this, _("Illegal directory name."), _("Error"), wxOK
| wxICON_ERROR
);
640 wxTreeItemId id
= event
.GetItem();
641 wxDirItemDataEx
*data
= (wxDirItemDataEx
*)m_treeCtrl
->GetItemData( id
);
644 wxString
new_name( wxPathOnly( data
->m_path
) );
645 new_name
+= wxString(wxFILE_SEP_PATH
);
646 new_name
+= event
.GetLabel();
650 if (wxFileExists(new_name
))
652 wxMessageDialog
dialog(this, _("File name exists already."), _("Error"), wxOK
| wxICON_ERROR
);
657 if (wxRenameFile(data
->m_path
,new_name
))
659 data
->SetNewDirName( new_name
);
663 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
669 void wxGenericDirCtrl::OnExpandItem(wxTreeEvent
&event
)
671 wxTreeItemId parentId
= event
.GetItem();
676 void wxGenericDirCtrl::OnCollapseItem(wxTreeEvent
&event
)
678 wxTreeItemId child
, parent
= event
.GetItem();
680 wxDirItemDataEx
*data
= (wxDirItemDataEx
*) m_treeCtrl
->GetItemData(event
.GetItem());
681 if (!data
->m_isExpanded
)
684 data
->m_isExpanded
= FALSE
;
686 /* Workaround because DeleteChildren has disapeared (why?) and
687 * CollapseAndReset doesn't work as advertised (deletes parent too) */
688 child
= m_treeCtrl
->GetFirstChild(parent
, cookie
);
691 m_treeCtrl
->Delete(child
);
692 /* Not GetNextChild below, because the cookie mechanism can't
693 * handle disappearing children! */
694 child
= m_treeCtrl
->GetFirstChild(parent
, cookie
);
698 void wxGenericDirCtrl::ExpandDir(wxTreeItemId parentId
)
700 wxDirItemDataEx
*data
= (wxDirItemDataEx
*) m_treeCtrl
->GetItemData(parentId
);
702 if (data
->m_isExpanded
)
705 data
->m_isExpanded
= TRUE
;
707 if (parentId
== m_rootId
)
715 wxString search
,path
,filename
;
717 wxString
dirName(data
->m_path
);
719 #if defined(__WXMSW__) || defined(__WXPM__)
720 // Check if this is a root directory and if so,
721 // whether the drive is avaiable.
722 if (!wxIsDriveAvailable(dirName
))
724 data
->m_isExpanded
= FALSE
;
725 //wxMessageBox(wxT("Sorry, this drive is not available."));
730 // This may take a longish time. Go to busy cursor
733 #if defined(__WXMSW__) || defined(__WXPM__)
734 if (dirName
.Last() == ':')
735 dirName
+= wxString(wxFILE_SEP_PATH
);
739 wxArrayString filenames
;
742 wxString eachFilename
;
749 if (d
.GetFirst(& eachFilename
, wxEmptyString
, wxDIR_DIRS
))
753 if ((eachFilename
!= wxT(".")) && (eachFilename
!= wxT("..")))
755 dirs
.Add(eachFilename
);
758 while (d
.GetNext(& eachFilename
)) ;
763 // Now do the filenames -- but only if we're allowed to
764 if ((GetWindowStyle() & wxDIRCTRL_DIR_ONLY
) == 0)
772 if (d
.GetFirst(& eachFilename
, m_currentFilterStr
, wxDIR_FILES
))
776 if ((eachFilename
!= wxT(".")) && (eachFilename
!= wxT("..")))
778 filenames
.Add(eachFilename
);
781 while (d
.GetNext(& eachFilename
)) ;
787 // Add the sorted dirs
789 for (i
= 0; i
< dirs
.Count(); i
++)
791 wxString
eachFilename(dirs
[i
]);
793 if (path
.Last() != wxFILE_SEP_PATH
)
794 path
+= wxString(wxFILE_SEP_PATH
);
795 path
+= eachFilename
;
797 wxDirItemDataEx
*dir_item
= new wxDirItemDataEx(path
,eachFilename
,TRUE
);
798 wxTreeItemId id
= m_treeCtrl
->AppendItem( parentId
, eachFilename
, 0, -1, dir_item
);
799 m_treeCtrl
->SetItemImage( id
, 1, wxTreeItemIcon_Expanded
);
801 // Has this got any children? If so, make it expandable.
802 int options
= wxDIR_DEFAULT
;
803 if (GetWindowStyle() & wxDIRCTRL_DIR_ONLY
) // If only showing dirs, then we specify dirs only here
805 options
= wxDIR_DIRS
;
813 // Have to test for wxDIR_DIRS separately in case m_currentFilterStr is non-empty and
814 // and filters out any directories
815 if (dir2
.GetFirst(& str
, m_currentFilterStr
, options
) || dir2
.GetFirst(& str
, wxEmptyString
, wxDIR_DIRS
))
817 m_treeCtrl
->SetItemHasChildren(id
);
822 // Add the sorted filenames
823 if ((GetWindowStyle() & wxDIRCTRL_DIR_ONLY
) == 0)
825 for (i
= 0; i
< filenames
.Count(); i
++)
827 wxString
eachFilename(filenames
[i
]);
829 if (path
.Last() != wxFILE_SEP_PATH
)
830 path
+= wxString(wxFILE_SEP_PATH
);
831 path
+= eachFilename
;
832 //path = dirName + wxString(wxT("/")) + eachFilename;
833 wxDirItemDataEx
*dir_item
= new wxDirItemDataEx(path
,eachFilename
,FALSE
);
834 (void)m_treeCtrl
->AppendItem( parentId
, eachFilename
, 2, -1, dir_item
);
839 // Find the child that matches the first part of 'path'.
840 // E.g. if a child path is "/usr" and 'path' is "/usr/include"
841 // then the child for /usr is returned.
842 wxTreeItemId
wxGenericDirCtrl::FindChild(wxTreeItemId parentId
, const wxString
& path
, bool& done
)
844 wxString
path2(path
);
846 // Make sure all separators are as per the current platform
847 path2
.Replace(wxT("\\"), wxString(wxFILE_SEP_PATH
));
848 path2
.Replace(wxT("/"), wxString(wxFILE_SEP_PATH
));
850 // Append a separator to foil bogus substring matching
851 path2
+= wxString(wxFILE_SEP_PATH
);
853 // In MSW or PM, case is not significant
854 #if defined(__WXMSW__) || defined(__WXPM__)
859 wxTreeItemId childId
= m_treeCtrl
->GetFirstChild(parentId
, cookie
);
860 while (childId
.IsOk())
862 wxDirItemDataEx
* data
= (wxDirItemDataEx
*) m_treeCtrl
->GetItemData(childId
);
864 if (data
&& data
->m_path
!= "")
866 wxString
childPath(data
->m_path
);
867 if (childPath
.Last() != wxFILE_SEP_PATH
)
868 childPath
+= wxString(wxFILE_SEP_PATH
);
870 // In MSW and PM, case is not significant
871 #if defined(__WXMSW__) || defined(__WXPM__)
872 childPath
.MakeLower();
875 if (childPath
.Len() <= path2
.Len())
877 wxString path3
= path2
.Mid(0, childPath
.Len());
878 if (childPath
== path3
)
880 if (path3
.Len() == path2
.Len())
889 childId
= m_treeCtrl
->GetNextChild(childId
, cookie
);
891 wxTreeItemId invalid
;
895 // Try to expand as much of the given path as possible,
896 // and select the given tree item.
897 bool wxGenericDirCtrl::ExpandPath(const wxString
& path
)
900 wxTreeItemId id
= FindChild(m_rootId
, path
, done
);
901 wxTreeItemId lastId
= id
; // The last non-zero id
902 while (id
.IsOk() && !done
)
906 id
= FindChild(id
, path
, done
);
912 wxDirItemDataEx
*data
= (wxDirItemDataEx
*) m_treeCtrl
->GetItemData(lastId
);
915 m_treeCtrl
->Expand(lastId
);
917 if ((GetWindowStyle() & wxDIRCTRL_SELECT_FIRST
) && data
->m_isDir
)
919 // Find the first file in this directory
921 wxTreeItemId childId
= m_treeCtrl
->GetFirstChild(lastId
, cookie
);
922 bool selectedChild
= FALSE
;
923 while (childId
.IsOk())
925 wxDirItemDataEx
* data
= (wxDirItemDataEx
*) m_treeCtrl
->GetItemData(childId
);
927 if (data
&& data
->m_path
!= "" && !data
->m_isDir
)
929 m_treeCtrl
->SelectItem(childId
);
930 m_treeCtrl
->EnsureVisible(childId
);
931 selectedChild
= TRUE
;
934 childId
= m_treeCtrl
->GetNextChild(lastId
, cookie
);
938 m_treeCtrl
->SelectItem(lastId
);
939 m_treeCtrl
->EnsureVisible(lastId
);
944 m_treeCtrl
->SelectItem(lastId
);
945 m_treeCtrl
->EnsureVisible(lastId
);
954 wxString
wxGenericDirCtrl::GetPath() const
956 wxTreeItemId id
= m_treeCtrl
->GetSelection();
959 wxDirItemDataEx
* data
= (wxDirItemDataEx
*) m_treeCtrl
->GetItemData(id
);
963 return wxEmptyString
;
966 wxString
wxGenericDirCtrl::GetFilePath() const
968 wxTreeItemId id
= m_treeCtrl
->GetSelection();
971 wxDirItemDataEx
* data
= (wxDirItemDataEx
*) m_treeCtrl
->GetItemData(id
);
973 return wxEmptyString
;
978 return wxEmptyString
;
981 void wxGenericDirCtrl::SetPath(const wxString
& path
)
983 m_defaultPath
= path
;
990 void wxGenericDirCtrl::FindChildFiles(wxTreeItemId id
, int dirFlags
, wxArrayString
& filenames
)
992 wxDirItemDataEx
*data
= (wxDirItemDataEx
*) m_treeCtrl
->GetItemData(id
);
994 // This may take a longish time. Go to busy cursor
999 wxString search
,path
,filename
;
1001 wxString
dirName(data
->m_path
);
1003 #if defined(__WXMSW__) || defined(__WXPM__)
1004 if (dirName
.Last() == ':')
1005 dirName
+= wxString(wxFILE_SEP_PATH
);
1009 wxString eachFilename
;
1016 if (d
.GetFirst(& eachFilename
, m_currentFilterStr
, dirFlags
))
1020 if ((eachFilename
!= wxT(".")) && (eachFilename
!= wxT("..")))
1022 filenames
.Add(eachFilename
);
1025 while (d
.GetNext(& eachFilename
)) ;
1031 void wxGenericDirCtrl::SetFilterIndex(int n
)
1033 m_currentFilter
= n
;
1036 if (ExtractWildcard(m_filter
, n
, f
, d
))
1037 m_currentFilterStr
= f
;
1039 m_currentFilterStr
= wxT("*.*");
1042 void wxGenericDirCtrl::SetFilter(const wxString
& filter
)
1047 if (ExtractWildcard(m_filter
, m_currentFilter
, f
, d
))
1048 m_currentFilterStr
= f
;
1050 m_currentFilterStr
= wxT("*.*");
1053 // Extract description and actual filter from overall filter string
1054 bool wxGenericDirCtrl::ExtractWildcard(const wxString
& filterStr
, int n
, wxString
& filter
, wxString
& description
)
1056 wxArrayString filters
, descriptions
;
1057 int count
= ParseFilter(filterStr
, filters
, descriptions
);
1058 if (count
> 0 && n
< count
)
1060 filter
= filters
[n
];
1061 description
= descriptions
[n
];
1068 // Parses the global filter, returning the number of filters.
1069 // Returns 0 if none or if there's a problem.
1070 // filterStr is in the form:
1072 // "All files (*.*)|*.*|JPEG Files (*.jpeg)|*.jpg"
1074 int wxGenericDirCtrl::ParseFilter(const wxString
& filterStr
, wxArrayString
& filters
, wxArrayString
& descriptions
)
1076 wxString
str(filterStr
);
1078 wxString description
, filter
;
1080 bool finished
= FALSE
;
1083 pos
= str
.Find(wxT('|'));
1085 return 0; // Problem
1086 description
= str
.Left(pos
);
1087 str
= str
.Mid(pos
+1);
1088 pos
= str
.Find(wxT('|'));
1096 filter
= str
.Left(pos
);
1097 str
= str
.Mid(pos
+1);
1099 descriptions
.Add(description
);
1100 filters
.Add(filter
);
1104 return filters
.Count();
1107 void wxGenericDirCtrl::DoResize()
1109 wxSize sz
= GetClientSize();
1110 int verticalSpacing
= 3;
1114 if (m_filterListCtrl
)
1116 filterSz
= m_filterListCtrl
->GetSize();
1117 sz
.y
-= (filterSz
.y
+ verticalSpacing
);
1119 m_treeCtrl
->SetSize(0, 0, sz
.x
, sz
.y
);
1120 if (m_filterListCtrl
)
1122 m_filterListCtrl
->SetSize(0, sz
.y
+ verticalSpacing
, sz
.x
, filterSz
.y
);
1123 // Don't know why, but this needs refreshing after a resize (wxMSW)
1124 m_filterListCtrl
->Refresh();
1130 void wxGenericDirCtrl::OnSize(wxSizeEvent
& WXUNUSED(event
))
1135 //-----------------------------------------------------------------------------
1136 // wxDirFilterListCtrl
1137 //-----------------------------------------------------------------------------
1139 IMPLEMENT_CLASS(wxDirFilterListCtrl
, wxChoice
)
1141 BEGIN_EVENT_TABLE(wxDirFilterListCtrl
, wxChoice
)
1142 EVT_CHOICE(-1, wxDirFilterListCtrl::OnSelFilter
)
1145 bool wxDirFilterListCtrl::Create(wxGenericDirCtrl
* parent
, const wxWindowID id
,
1151 return wxChoice::Create(parent
, id
, pos
, size
, 0, NULL
, style
);
1154 void wxDirFilterListCtrl::Init()
1159 void wxDirFilterListCtrl::OnSelFilter(wxCommandEvent
& WXUNUSED(event
))
1161 int sel
= GetSelection();
1163 wxString currentPath
= m_dirCtrl
->GetPath();
1165 m_dirCtrl
->SetFilterIndex(sel
);
1167 // If the filter has changed, the view is out of date, so
1168 // collapse the tree.
1169 m_dirCtrl
->GetTreeCtrl()->Collapse(m_dirCtrl
->GetRootId());
1170 m_dirCtrl
->GetTreeCtrl()->Expand(m_dirCtrl
->GetRootId());
1172 // Try to restore the selection, or at least the directory
1173 m_dirCtrl
->ExpandPath(currentPath
);
1176 void wxDirFilterListCtrl::FillFilterList(const wxString
& filter
, int defaultFilter
)
1179 wxArrayString descriptions
, filters
;
1180 size_t n
= (size_t) m_dirCtrl
->ParseFilter(filter
, filters
, descriptions
);
1182 if (n
> 0 && defaultFilter
< (int) n
)
1185 for (i
= 0; i
< n
; i
++)
1186 Append(descriptions
[i
]);
1187 SetSelection(defaultFilter
);
1191 // wxGenericDirDialog implementation
1192 // This should be moved into dirdlgg.cpp eventually
1194 BEGIN_EVENT_TABLE(wxGenericDirDialog
, wxDialog
)
1195 EVT_BUTTON(wxID_OK
, wxGenericDirDialog::OnOK
)
1196 EVT_BUTTON(wxID_NEW
, wxGenericDirDialog::OnNew
)
1197 EVT_BUTTON (wxID_NEW
, wxGenericDirDialog::OnNew
)
1198 EVT_CLOSE(wxGenericDirDialog::OnCloseWindow
)
1199 EVT_TREE_KEY_DOWN (-1, wxGenericDirDialog::OnTreeKeyDown
)
1200 EVT_TREE_SEL_CHANGED (-1, wxGenericDirDialog::OnTreeSelected
)
1201 EVT_TEXT_ENTER (ID_TEXTCTRL
, wxGenericDirDialog::OnOK
)
1204 wxGenericDirDialog::wxGenericDirDialog(wxWindow
* parent
, const wxString
& title
,
1205 const wxString
& defaultPath
, long style
, const wxPoint
& pos
, const wxSize
& sz
, const wxString
& name
):
1206 wxDialog(parent
, ID_DIRCTRL
, title
, pos
, sz
, style
, name
)
1209 m_path
= defaultPath
;
1211 wxBusyCursor cursor
;
1213 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
1216 m_dirCtrl
= new wxGenericDirCtrl(this, ID_DIRCTRL
,
1217 defaultPath
, wxPoint(5, 5),
1218 wxSize(300, 200), wxDIRCTRL_DIR_ONLY
|wxSUNKEN_BORDER
);
1220 topsizer
->Add( m_dirCtrl
, 1, wxTOP
|wxLEFT
|wxRIGHT
| wxEXPAND
, 10 );
1223 m_input
= new wxTextCtrl( this, ID_TEXTCTRL
, m_path
, wxDefaultPosition
);
1224 topsizer
->Add( m_input
, 0, wxTOP
|wxLEFT
|wxRIGHT
| wxEXPAND
, 10 );
1228 topsizer
->Add( new wxStaticLine( this, -1 ), 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
1232 wxSizer
* buttonsizer
= new wxBoxSizer( wxHORIZONTAL
);
1233 wxButton
* okButton
= new wxButton(this, wxID_OK
, _("OK"));
1234 buttonsizer
->Add( okButton
, 0, wxLEFT
|wxRIGHT
, 10 );
1235 wxButton
* cancelButton
= new wxButton(this, wxID_CANCEL
, _("Cancel"));
1236 buttonsizer
->Add( cancelButton
, 0, wxLEFT
|wxRIGHT
, 10 );
1238 // I'm not convinced we need a New button, and we tend to get annoying
1239 // accidental-editing with label editing enabled.
1241 wxButton
* newButton
= new wxButton( this, wxID_NEW
, _("New...") );
1242 buttonsizer
->Add( newButton
, 0, wxLEFT
|wxRIGHT
, 10 );
1245 topsizer
->Add( buttonsizer
, 0, wxALL
| wxCENTER
, 10 );
1247 okButton
->SetDefault();
1248 m_dirCtrl
->SetFocus();
1250 SetAutoLayout( TRUE
);
1251 SetSizer( topsizer
);
1253 topsizer
->SetSizeHints( this );
1254 topsizer
->Fit( this );
1259 void wxGenericDirDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
1261 EndModal(wxID_CANCEL
);
1264 void wxGenericDirDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
1266 m_path
= m_input
->GetValue();
1267 // Does the path exist? (User may have typed anything in m_input)
1268 if (wxPathExists(m_path
)) {
1269 // OK, path exists, we're done.
1273 // Interact with user, find out if the dir is a typo or to be created
1274 wxString
msg( _("The directory ") );
1276 msg
= msg
+ _("\ndoes not exist\nCreate it now?") ;
1277 wxMessageDialog
dialog(this, msg
, _("Directory does not exist"), wxYES_NO
| wxICON_WARNING
);
1278 if ( dialog
.ShowModal() == wxID_YES
) {
1279 // Okay, let's make it
1281 if (wxMkdir(m_path
)) {
1282 // The new dir was created okay.
1288 msg
= _("Failed to create directory ")+m_path
+
1289 _("\n(Do you have the required permissions?)");
1290 wxMessageDialog
errmsg(this, msg
, _("Error creating directory"), wxOK
| wxICON_ERROR
);
1292 // We still don't have a valid dir. Back to the main dialog.
1295 // User has answered NO to create dir.
1298 void wxGenericDirDialog::SetPath(const wxString
& path
)
1300 m_dirCtrl
->SetPath(path
);
1304 wxString
wxGenericDirDialog::GetPath(void) const
1309 int wxGenericDirDialog::ShowModal()
1311 //m_input->SetValue( m_path );
1312 return wxDialog::ShowModal();
1315 void wxGenericDirDialog::OnTreeSelected( wxTreeEvent
&event
)
1320 wxDirItemDataEx
*data
= (wxDirItemDataEx
*)m_dirCtrl
->GetTreeCtrl()->GetItemData(event
.GetItem());
1322 m_input
->SetValue( data
->m_path
);
1325 void wxGenericDirDialog::OnTreeKeyDown( wxTreeEvent
&WXUNUSED(event
) )
1330 wxDirItemDataEx
*data
= (wxDirItemDataEx
*)m_dirCtrl
->GetTreeCtrl()->GetItemData(m_dirCtrl
->GetTreeCtrl()->GetSelection());
1332 m_input
->SetValue( data
->m_path
);
1335 void wxGenericDirDialog::OnNew( wxCommandEvent
& WXUNUSED(event
) )
1337 wxTreeItemId id
= m_dirCtrl
->GetTreeCtrl()->GetSelection();
1338 if ((id
== m_dirCtrl
->GetTreeCtrl()->GetRootItem()) ||
1339 (m_dirCtrl
->GetTreeCtrl()->GetParent(id
) == m_dirCtrl
->GetTreeCtrl()->GetRootItem()))
1341 wxMessageDialog
msg(this, _("You cannot add a new directory to this section."),
1342 _("Create directory"), wxOK
| wxICON_INFORMATION
);
1347 wxTreeItemId parent
= id
; // m_dirCtrl->GetTreeCtrl()->GetParent( id );
1348 wxDirItemDataEx
*data
= (wxDirItemDataEx
*)m_dirCtrl
->GetTreeCtrl()->GetItemData( parent
);
1351 wxString
new_name( wxT("NewName") );
1352 wxString
path( data
->m_path
);
1353 if (path
.Last() != wxFILE_SEP_PATH
)
1354 path
+= wxFILE_SEP_PATH
;
1356 if (wxFileExists(path
))
1358 // try NewName0, NewName1 etc.
1361 new_name
= wxT("NewName");
1363 num
.Printf( wxT("%d"), i
);
1366 path
= data
->m_path
;
1367 if (path
.Last() != wxFILE_SEP_PATH
)
1368 path
+= wxFILE_SEP_PATH
;
1371 } while (wxFileExists(path
));
1377 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
1382 wxDirItemDataEx
*new_data
= new wxDirItemDataEx( path
, new_name
, TRUE
);
1384 // TODO: THIS CODE DOESN'T WORK YET. We need to avoid duplication of the first child
1386 wxTreeItemId new_id
= m_dirCtrl
->GetTreeCtrl()->AppendItem( parent
, new_name
, 0, 0, new_data
);
1387 m_dirCtrl
->GetTreeCtrl()->EnsureVisible( new_id
);
1388 m_dirCtrl
->GetTreeCtrl()->EditLabel( new_id
);