1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxGenericDirCtrl
4 // Author: Harm van der Heijden, Robert Roebling, Julian Smart
8 // Copyright: (c) Harm van der Heijden, Robert Roebling and Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "dirctrlg.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
26 #include "wx/dialog.h"
27 #include "wx/button.h"
28 #include "wx/layout.h"
29 #include "wx/msgdlg.h"
30 #include "wx/textctrl.h"
31 #include "wx/textdlg.h"
32 #include "wx/filefn.h"
33 #include "wx/cmndata.h"
34 #include "wx/gdicmn.h"
36 #include "wx/imaglist.h"
40 #include "wx/tokenzr.h"
42 #include "wx/settings.h"
45 #include "wx/statline.h"
48 #include "wx/generic/dirctrlg.h"
53 // FIXME - Mingw32 1.0 has both _getdrive() and _chdrive(). For now, let's assume
54 // older releases don't, but it should be verified and the checks modified
56 #if !defined(__GNUWIN32__) || \
57 (defined(__MINGW32_MAJOR_VERSION) && __MINGW32_MAJOR_VERSION >= 1)
75 #if defined(__WXMAC__)
76 # include "MoreFilesExtras.h"
83 // If compiled under Windows, this macro can cause problems
89 static char * icon1_xpm
[] = {
90 /* width height ncolors chars_per_pixel */
118 static char * icon2_xpm
[] = {
119 /* width height ncolors chars_per_pixel */
147 static char * icon3_xpm
[] = {
148 /* width height ncolors chars_per_pixel */
173 static char * icon4_xpm
[] = {
200 static char * icon5_xpm
[] = {
227 static char *icon6_xpm
[] = {
257 static char * icon7_xpm
[] = {
284 static char * icon8_xpm
[] = {
310 static const int ID_DIRCTRL
= 1000;
311 static const int ID_TEXTCTRL
= 1001;
312 static const int ID_OK
= 1002;
313 static const int ID_CANCEL
= 1003;
314 static const int ID_NEW
= 1004;
315 //static const int ID_CHECK = 1005;
317 #if defined(__WXMSW__) || defined(__WXPM__) || defined(__DOS__)
318 int setdrive(int drive
)
320 #if defined(__GNUWIN32__) && \
321 (defined(__MINGW32_MAJOR_VERSION) && __MINGW32_MAJOR_VERSION >= 1)
322 return _chdrive(drive
);
326 if (drive
< 1 || drive
> 31)
328 newdrive
[0] = (wxChar
)(wxT('A') + drive
- 1);
329 newdrive
[1] = wxT(':');
330 newdrive
[2] = wxT('\0');
331 #if defined(__WXMSW__)
333 if (wxSetWorkingDirectory(newdrive
))
335 if (::SetCurrentDirectory(newdrive
))
338 // VA doesn't know what LPSTR is and has its own set
339 if (DosSetCurrentDir((PSZ
)newdrive
))
347 static bool wxIsDriveAvailable(const wxString dirName
)
350 UINT errorMode
= SetErrorMode(SEM_FAILCRITICALERRORS
| SEM_NOOPENFILEERRORBOX
);
354 // Check if this is a root directory and if so,
355 // whether the drive is avaiable.
356 if (dirName
.Len() == 3 && dirName
[(size_t)1] == wxT(':'))
358 wxString
dirNameLower(dirName
.Lower());
359 #if defined(__GNUWIN32__) && \
360 !(defined(__MINGW32_MAJOR_VERSION) && __MINGW32_MAJOR_VERSION >= 1)
361 success
= wxPathExists(dirNameLower
);
363 int currentDrive
= _getdrive();
364 int thisDrive
= (int) (dirNameLower
[(size_t)0] - 'a' + 1) ;
365 int err
= setdrive( thisDrive
) ;
366 setdrive( currentDrive
);
375 (void) SetErrorMode(errorMode
);
382 // Function which is called by quick sort. We want to override the default wxArrayString behaviour,
383 // and sort regardless of case.
384 static int LINKAGEMODE
wxDirCtrlStringCompareFunction(const void *first
, const void *second
)
386 wxString
*strFirst
= (wxString
*)first
;
387 wxString
*strSecond
= (wxString
*)second
;
389 return strFirst
->CmpNoCase(*strSecond
);
392 //-----------------------------------------------------------------------------
394 //-----------------------------------------------------------------------------
396 wxDirItemDataEx::wxDirItemDataEx(const wxString
& path
, const wxString
& name
,
401 /* Insert logic to detect hidden files here
402 * In UnixLand we just check whether the first char is a dot
403 * For FileNameFromPath read LastDirNameInThisPath ;-) */
404 // m_isHidden = (bool)(wxFileNameFromPath(*m_path)[0] == '.');
406 // m_hasSubDirs is no longer needed
407 m_hasSubDirs
= TRUE
; // HasSubDirs();
408 m_isExpanded
= FALSE
;
412 wxDirItemDataEx::~wxDirItemDataEx()
416 void wxDirItemDataEx::SetNewDirName( wxString path
)
419 m_name
= wxFileNameFromPath( path
);
422 //-----------------------------------------------------------------------------
424 //-----------------------------------------------------------------------------
426 IMPLEMENT_DYNAMIC_CLASS(wxGenericDirCtrl
, wxControl
)
428 BEGIN_EVENT_TABLE(wxGenericDirCtrl
, wxControl
)
429 EVT_TREE_ITEM_EXPANDING (-1, wxGenericDirCtrl::OnExpandItem
)
430 EVT_TREE_ITEM_COLLAPSED (-1, wxGenericDirCtrl::OnCollapseItem
)
431 EVT_TREE_BEGIN_LABEL_EDIT (-1, wxGenericDirCtrl::OnBeginEditItem
)
432 EVT_TREE_END_LABEL_EDIT (-1, wxGenericDirCtrl::OnEndEditItem
)
433 EVT_SIZE (wxGenericDirCtrl::OnSize
)
436 wxGenericDirCtrl::wxGenericDirCtrl(void)
441 bool wxGenericDirCtrl::Create(wxWindow
*parent
,
447 const wxString
& filter
,
449 const wxString
& name
)
451 if (!wxControl::Create(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
454 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
458 long treeStyle
= wxTR_HAS_BUTTONS
; // | wxTR_EDIT_LABELS;
459 if ((style
& wxDIRCTRL_3D_INTERNAL
) == 0)
460 treeStyle
|= wxNO_BORDER
;
462 long filterStyle
= 0;
463 if ((style
& wxDIRCTRL_3D_INTERNAL
) == 0)
464 filterStyle
|= wxNO_BORDER
;
466 m_treeCtrl
= new wxTreeCtrl(this, wxID_TREECTRL
, pos
, size
, treeStyle
);
468 if (!filter
.IsEmpty() && (style
& wxDIRCTRL_SHOW_FILTERS
))
469 m_filterListCtrl
= new wxDirFilterListCtrl(this, wxID_FILTERLISTCTRL
, wxDefaultPosition
, wxDefaultSize
, filterStyle
);
474 SetFilterIndex(defaultFilter
);
476 if (m_filterListCtrl
)
477 m_filterListCtrl
->FillFilterList(filter
, defaultFilter
);
479 m_imageList
= new wxImageList(16, 16, TRUE
);
480 m_imageList
->Add(wxIcon(icon1_xpm
));
481 m_imageList
->Add(wxIcon(icon2_xpm
));
482 m_imageList
->Add(wxIcon(icon3_xpm
));
483 m_imageList
->Add(wxIcon(icon4_xpm
));
484 m_imageList
->Add(wxIcon(icon5_xpm
));
485 m_imageList
->Add(wxIcon(icon6_xpm
));
486 m_imageList
->Add(wxIcon(icon7_xpm
));
487 m_imageList
->Add(wxIcon(icon8_xpm
));
488 m_treeCtrl
->SetImageList(m_imageList
);
490 m_showHidden
= FALSE
;
491 wxDirItemDataEx
* rootData
= new wxDirItemDataEx(wxT(""), wxT(""), TRUE
);
495 #if defined(__WXMSW__) || defined(__WXPM__)
496 rootName
= _("Computer");
498 rootName
= _("Sections");
501 m_rootId
= m_treeCtrl
->AddRoot( rootName
, 3, -1, rootData
);
502 m_treeCtrl
->SetItemHasChildren(m_rootId
);
503 m_treeCtrl
->Expand(m_rootId
); // automatically expand first level
505 // Expand and select the default path
506 if (!m_defaultPath
.IsEmpty())
507 ExpandPath(m_defaultPath
);
514 wxGenericDirCtrl::~wxGenericDirCtrl()
516 m_treeCtrl
->SetImageList(NULL
);
520 void wxGenericDirCtrl::Init()
522 m_showHidden
= FALSE
;
525 m_currentFilterStr
= wxEmptyString
; // Default: any file
527 m_filterListCtrl
= NULL
;
530 void wxGenericDirCtrl::AddSection(const wxString
& path
, const wxString
& name
, int imageId
)
532 wxDirItemDataEx
*dir_item
= new wxDirItemDataEx(path
,name
,TRUE
);
534 #if defined(__WXMSW__) || defined(__WXPM__)
535 // Windows and OS/2: sections are displayed as drives
536 wxTreeItemId id
= m_treeCtrl
->AppendItem( m_rootId
, name
, imageId
, -1, dir_item
);
538 // Unix: sections are displayed as folders
539 wxTreeItemId id
= m_treeCtrl
->AppendItem( m_rootId
, name
, 0, -1, dir_item
);
540 m_treeCtrl
->SetItemImage( id
, 1, wxTreeItemIcon_Expanded
);
542 // TODO: other operating systems.
544 m_treeCtrl
->SetItemHasChildren(id
);
547 void wxGenericDirCtrl::SetupSections()
549 #if defined(__WXMSW__) || defined(__WXPM__)
552 wxChar driveBuffer
[256];
553 size_t n
= (size_t) GetLogicalDriveStrings(255, driveBuffer
);
558 path
.Printf(wxT("%c:\\"), driveBuffer
[i
]);
559 name
.Printf(wxT("(%c:)"), driveBuffer
[i
]);
562 int driveType
= ::GetDriveType(path
);
565 case DRIVE_REMOVABLE
:
566 if (path
== wxT("a:\\") || path
== wxT("b:\\"))
567 imageId
= 6; // Floppy
585 AddSection(path
, name
, imageId
);
587 while (driveBuffer
[i
] != wxT('\0'))
590 if (driveBuffer
[i
] == wxT('\0'))
597 /* If we can switch to the drive, it exists. */
598 for( drive
= 1; drive
<= 26; drive
++ )
601 path
.Printf(wxT("%c:\\"), (char) (drive
+ 'a' - 1));
602 name
.Printf(wxT("(%c:)"), (char) (drive
+ 'a' - 1));
604 if (wxIsDriveAvailable(path
))
607 AddSection(path
, name
);
611 #elif defined(__WXMAC__)
615 short actualCount
= 0 ;
616 if ( OnLine( &volume
, 1 , &actualCount
, &index
) != noErr
|| actualCount
== 0 )
619 wxString name
= wxMacFSSpec2MacFilename( &volume
) ;
620 AddSection(name
+ wxFILE_SEP_PATH
, name
, 0);
623 AddSection(wxT("/"), _("The Computer"), 0);
624 AddSection(wxGetHomeDir(), _("My Home"), 0 );
625 AddSection(wxT("/mnt"), _("Mounted Devices"), 0 );
626 AddSection(wxT("/usr/local"), _("User Local"), 0 );
627 AddSection(wxT("/usr"), _("User"), 0 );
628 AddSection(wxT("/var"), _("Variables"), 0 );
629 AddSection(wxT("/etc"), _("Etcetera"), 0 );
630 AddSection(wxT("/tmp"), _("Temporary"), 0 );
634 void wxGenericDirCtrl::OnBeginEditItem(wxTreeEvent
&event
)
636 // don't rename the main entry "Sections"
637 if (event
.GetItem() == m_rootId
)
643 // don't rename the individual sections
644 if (m_treeCtrl
->GetParent( event
.GetItem() ) == m_rootId
)
651 void wxGenericDirCtrl::OnEndEditItem(wxTreeEvent
&event
)
653 if ((event
.GetLabel().IsEmpty()) ||
654 (event
.GetLabel() == _(".")) ||
655 (event
.GetLabel() == _("..")) ||
656 (event
.GetLabel().First( wxT("/") ) != wxNOT_FOUND
))
658 wxMessageDialog
dialog(this, _("Illegal directory name."), _("Error"), wxOK
| wxICON_ERROR
);
664 wxTreeItemId id
= event
.GetItem();
665 wxDirItemDataEx
*data
= (wxDirItemDataEx
*)m_treeCtrl
->GetItemData( id
);
668 wxString
new_name( wxPathOnly( data
->m_path
) );
669 new_name
+= wxString(wxFILE_SEP_PATH
);
670 new_name
+= event
.GetLabel();
674 if (wxFileExists(new_name
))
676 wxMessageDialog
dialog(this, _("File name exists already."), _("Error"), wxOK
| wxICON_ERROR
);
681 if (wxRenameFile(data
->m_path
,new_name
))
683 data
->SetNewDirName( new_name
);
687 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
693 void wxGenericDirCtrl::OnExpandItem(wxTreeEvent
&event
)
695 wxTreeItemId parentId
= event
.GetItem();
700 void wxGenericDirCtrl::OnCollapseItem(wxTreeEvent
&event
)
702 wxTreeItemId child
, parent
= event
.GetItem();
704 wxDirItemDataEx
*data
= (wxDirItemDataEx
*) m_treeCtrl
->GetItemData(event
.GetItem());
705 if (!data
->m_isExpanded
)
708 data
->m_isExpanded
= FALSE
;
710 /* Workaround because DeleteChildren has disapeared (why?) and
711 * CollapseAndReset doesn't work as advertised (deletes parent too) */
712 child
= m_treeCtrl
->GetFirstChild(parent
, cookie
);
715 m_treeCtrl
->Delete(child
);
716 /* Not GetNextChild below, because the cookie mechanism can't
717 * handle disappearing children! */
718 child
= m_treeCtrl
->GetFirstChild(parent
, cookie
);
722 void wxGenericDirCtrl::ExpandDir(wxTreeItemId parentId
)
724 wxDirItemDataEx
*data
= (wxDirItemDataEx
*) m_treeCtrl
->GetItemData(parentId
);
726 if (data
->m_isExpanded
)
729 data
->m_isExpanded
= TRUE
;
731 if (parentId
== m_rootId
)
739 wxString search
,path
,filename
;
741 wxString
dirName(data
->m_path
);
743 #if defined(__WXMSW__) || defined(__WXPM__)
744 // Check if this is a root directory and if so,
745 // whether the drive is avaiable.
746 if (!wxIsDriveAvailable(dirName
))
748 data
->m_isExpanded
= FALSE
;
749 //wxMessageBox(wxT("Sorry, this drive is not available."));
754 // This may take a longish time. Go to busy cursor
757 #if defined(__WXMSW__) || defined(__WXPM__)
758 if (dirName
.Last() == ':')
759 dirName
+= wxString(wxFILE_SEP_PATH
);
763 wxArrayString filenames
;
766 wxString eachFilename
;
773 if (d
.GetFirst(& eachFilename
, wxEmptyString
, wxDIR_DIRS
))
777 if ((eachFilename
!= wxT(".")) && (eachFilename
!= wxT("..")))
779 dirs
.Add(eachFilename
);
782 while (d
.GetNext(& eachFilename
)) ;
785 dirs
.Sort((wxArrayString::CompareFunction
) wxDirCtrlStringCompareFunction
);
787 // Now do the filenames -- but only if we're allowed to
788 if ((GetWindowStyle() & wxDIRCTRL_DIR_ONLY
) == 0)
796 if (d
.GetFirst(& eachFilename
, m_currentFilterStr
, wxDIR_FILES
))
800 if ((eachFilename
!= wxT(".")) && (eachFilename
!= wxT("..")))
802 filenames
.Add(eachFilename
);
805 while (d
.GetNext(& eachFilename
)) ;
808 filenames
.Sort((wxArrayString::CompareFunction
) wxDirCtrlStringCompareFunction
);
811 // Add the sorted dirs
813 for (i
= 0; i
< dirs
.Count(); i
++)
815 wxString
eachFilename(dirs
[i
]);
817 if (path
.Last() != wxFILE_SEP_PATH
)
818 path
+= wxString(wxFILE_SEP_PATH
);
819 path
+= eachFilename
;
821 wxDirItemDataEx
*dir_item
= new wxDirItemDataEx(path
,eachFilename
,TRUE
);
822 wxTreeItemId id
= m_treeCtrl
->AppendItem( parentId
, eachFilename
, 0, -1, dir_item
);
823 m_treeCtrl
->SetItemImage( id
, 1, wxTreeItemIcon_Expanded
);
825 // Has this got any children? If so, make it expandable.
826 int options
= wxDIR_DEFAULT
;
827 if (GetWindowStyle() & wxDIRCTRL_DIR_ONLY
) // If only showing dirs, then we specify dirs only here
829 options
= wxDIR_DIRS
;
837 // Have to test for wxDIR_DIRS separately in case m_currentFilterStr is non-empty and
838 // and filters out any directories
839 if (dir2
.GetFirst(& str
, m_currentFilterStr
, options
) || dir2
.GetFirst(& str
, wxEmptyString
, wxDIR_DIRS
))
841 m_treeCtrl
->SetItemHasChildren(id
);
846 // Add the sorted filenames
847 if ((GetWindowStyle() & wxDIRCTRL_DIR_ONLY
) == 0)
849 for (i
= 0; i
< filenames
.Count(); i
++)
851 wxString
eachFilename(filenames
[i
]);
853 if (path
.Last() != wxFILE_SEP_PATH
)
854 path
+= wxString(wxFILE_SEP_PATH
);
855 path
+= eachFilename
;
856 //path = dirName + wxString(wxT("/")) + eachFilename;
857 wxDirItemDataEx
*dir_item
= new wxDirItemDataEx(path
,eachFilename
,FALSE
);
858 (void)m_treeCtrl
->AppendItem( parentId
, eachFilename
, 2, -1, dir_item
);
863 // Find the child that matches the first part of 'path'.
864 // E.g. if a child path is "/usr" and 'path' is "/usr/include"
865 // then the child for /usr is returned.
866 wxTreeItemId
wxGenericDirCtrl::FindChild(wxTreeItemId parentId
, const wxString
& path
, bool& done
)
868 wxString
path2(path
);
870 // Make sure all separators are as per the current platform
871 path2
.Replace(wxT("\\"), wxString(wxFILE_SEP_PATH
));
872 path2
.Replace(wxT("/"), wxString(wxFILE_SEP_PATH
));
874 // Append a separator to foil bogus substring matching
875 path2
+= wxString(wxFILE_SEP_PATH
);
877 // In MSW or PM, case is not significant
878 #if defined(__WXMSW__) || defined(__WXPM__)
883 wxTreeItemId childId
= m_treeCtrl
->GetFirstChild(parentId
, cookie
);
884 while (childId
.IsOk())
886 wxDirItemDataEx
* data
= (wxDirItemDataEx
*) m_treeCtrl
->GetItemData(childId
);
888 if (data
&& data
->m_path
!= "")
890 wxString
childPath(data
->m_path
);
891 if (childPath
.Last() != wxFILE_SEP_PATH
)
892 childPath
+= wxString(wxFILE_SEP_PATH
);
894 // In MSW and PM, case is not significant
895 #if defined(__WXMSW__) || defined(__WXPM__)
896 childPath
.MakeLower();
899 if (childPath
.Len() <= path2
.Len())
901 wxString path3
= path2
.Mid(0, childPath
.Len());
902 if (childPath
== path3
)
904 if (path3
.Len() == path2
.Len())
913 childId
= m_treeCtrl
->GetNextChild(childId
, cookie
);
915 wxTreeItemId invalid
;
919 // Try to expand as much of the given path as possible,
920 // and select the given tree item.
921 bool wxGenericDirCtrl::ExpandPath(const wxString
& path
)
924 wxTreeItemId id
= FindChild(m_rootId
, path
, done
);
925 wxTreeItemId lastId
= id
; // The last non-zero id
926 while (id
.IsOk() && !done
)
930 id
= FindChild(id
, path
, done
);
936 wxDirItemDataEx
*data
= (wxDirItemDataEx
*) m_treeCtrl
->GetItemData(lastId
);
939 m_treeCtrl
->Expand(lastId
);
941 if ((GetWindowStyle() & wxDIRCTRL_SELECT_FIRST
) && data
->m_isDir
)
943 // Find the first file in this directory
945 wxTreeItemId childId
= m_treeCtrl
->GetFirstChild(lastId
, cookie
);
946 bool selectedChild
= FALSE
;
947 while (childId
.IsOk())
949 wxDirItemDataEx
* data
= (wxDirItemDataEx
*) m_treeCtrl
->GetItemData(childId
);
951 if (data
&& data
->m_path
!= "" && !data
->m_isDir
)
953 m_treeCtrl
->SelectItem(childId
);
954 m_treeCtrl
->EnsureVisible(childId
);
955 selectedChild
= TRUE
;
958 childId
= m_treeCtrl
->GetNextChild(lastId
, cookie
);
962 m_treeCtrl
->SelectItem(lastId
);
963 m_treeCtrl
->EnsureVisible(lastId
);
968 m_treeCtrl
->SelectItem(lastId
);
969 m_treeCtrl
->EnsureVisible(lastId
);
978 wxString
wxGenericDirCtrl::GetPath() const
980 wxTreeItemId id
= m_treeCtrl
->GetSelection();
983 wxDirItemDataEx
* data
= (wxDirItemDataEx
*) m_treeCtrl
->GetItemData(id
);
987 return wxEmptyString
;
990 wxString
wxGenericDirCtrl::GetFilePath() const
992 wxTreeItemId id
= m_treeCtrl
->GetSelection();
995 wxDirItemDataEx
* data
= (wxDirItemDataEx
*) m_treeCtrl
->GetItemData(id
);
997 return wxEmptyString
;
1002 return wxEmptyString
;
1005 void wxGenericDirCtrl::SetPath(const wxString
& path
)
1007 m_defaultPath
= path
;
1014 void wxGenericDirCtrl::FindChildFiles(wxTreeItemId id
, int dirFlags
, wxArrayString
& filenames
)
1016 wxDirItemDataEx
*data
= (wxDirItemDataEx
*) m_treeCtrl
->GetItemData(id
);
1018 // This may take a longish time. Go to busy cursor
1023 wxString search
,path
,filename
;
1025 wxString
dirName(data
->m_path
);
1027 #if defined(__WXMSW__) || defined(__WXPM__)
1028 if (dirName
.Last() == ':')
1029 dirName
+= wxString(wxFILE_SEP_PATH
);
1033 wxString eachFilename
;
1040 if (d
.GetFirst(& eachFilename
, m_currentFilterStr
, dirFlags
))
1044 if ((eachFilename
!= wxT(".")) && (eachFilename
!= wxT("..")))
1046 filenames
.Add(eachFilename
);
1049 while (d
.GetNext(& eachFilename
)) ;
1055 void wxGenericDirCtrl::SetFilterIndex(int n
)
1057 m_currentFilter
= n
;
1060 if (ExtractWildcard(m_filter
, n
, f
, d
))
1061 m_currentFilterStr
= f
;
1063 m_currentFilterStr
= wxT("*.*");
1066 void wxGenericDirCtrl::SetFilter(const wxString
& filter
)
1071 if (ExtractWildcard(m_filter
, m_currentFilter
, f
, d
))
1072 m_currentFilterStr
= f
;
1074 m_currentFilterStr
= wxT("*.*");
1077 // Extract description and actual filter from overall filter string
1078 bool wxGenericDirCtrl::ExtractWildcard(const wxString
& filterStr
, int n
, wxString
& filter
, wxString
& description
)
1080 wxArrayString filters
, descriptions
;
1081 int count
= ParseFilter(filterStr
, filters
, descriptions
);
1082 if (count
> 0 && n
< count
)
1084 filter
= filters
[n
];
1085 description
= descriptions
[n
];
1092 // Parses the global filter, returning the number of filters.
1093 // Returns 0 if none or if there's a problem.
1094 // filterStr is in the form:
1096 // "All files (*.*)|*.*|JPEG Files (*.jpeg)|*.jpg"
1098 int wxGenericDirCtrl::ParseFilter(const wxString
& filterStr
, wxArrayString
& filters
, wxArrayString
& descriptions
)
1100 wxString
str(filterStr
);
1102 wxString description
, filter
;
1104 bool finished
= FALSE
;
1107 pos
= str
.Find(wxT('|'));
1109 return 0; // Problem
1110 description
= str
.Left(pos
);
1111 str
= str
.Mid(pos
+1);
1112 pos
= str
.Find(wxT('|'));
1120 filter
= str
.Left(pos
);
1121 str
= str
.Mid(pos
+1);
1123 descriptions
.Add(description
);
1124 filters
.Add(filter
);
1128 return filters
.Count();
1131 void wxGenericDirCtrl::DoResize()
1133 wxSize sz
= GetClientSize();
1134 int verticalSpacing
= 3;
1138 if (m_filterListCtrl
)
1140 filterSz
= m_filterListCtrl
->GetSize();
1141 sz
.y
-= (filterSz
.y
+ verticalSpacing
);
1143 m_treeCtrl
->SetSize(0, 0, sz
.x
, sz
.y
);
1144 if (m_filterListCtrl
)
1146 m_filterListCtrl
->SetSize(0, sz
.y
+ verticalSpacing
, sz
.x
, filterSz
.y
);
1147 // Don't know why, but this needs refreshing after a resize (wxMSW)
1148 m_filterListCtrl
->Refresh();
1154 void wxGenericDirCtrl::OnSize(wxSizeEvent
& WXUNUSED(event
))
1159 //-----------------------------------------------------------------------------
1160 // wxDirFilterListCtrl
1161 //-----------------------------------------------------------------------------
1163 IMPLEMENT_CLASS(wxDirFilterListCtrl
, wxChoice
)
1165 BEGIN_EVENT_TABLE(wxDirFilterListCtrl
, wxChoice
)
1166 EVT_CHOICE(-1, wxDirFilterListCtrl::OnSelFilter
)
1169 bool wxDirFilterListCtrl::Create(wxGenericDirCtrl
* parent
, const wxWindowID id
,
1175 return wxChoice::Create(parent
, id
, pos
, size
, 0, NULL
, style
);
1178 void wxDirFilterListCtrl::Init()
1183 void wxDirFilterListCtrl::OnSelFilter(wxCommandEvent
& WXUNUSED(event
))
1185 int sel
= GetSelection();
1187 wxString currentPath
= m_dirCtrl
->GetPath();
1189 m_dirCtrl
->SetFilterIndex(sel
);
1191 // If the filter has changed, the view is out of date, so
1192 // collapse the tree.
1193 m_dirCtrl
->GetTreeCtrl()->Collapse(m_dirCtrl
->GetRootId());
1194 m_dirCtrl
->GetTreeCtrl()->Expand(m_dirCtrl
->GetRootId());
1196 // Try to restore the selection, or at least the directory
1197 m_dirCtrl
->ExpandPath(currentPath
);
1200 void wxDirFilterListCtrl::FillFilterList(const wxString
& filter
, int defaultFilter
)
1203 wxArrayString descriptions
, filters
;
1204 size_t n
= (size_t) m_dirCtrl
->ParseFilter(filter
, filters
, descriptions
);
1206 if (n
> 0 && defaultFilter
< (int) n
)
1209 for (i
= 0; i
< n
; i
++)
1210 Append(descriptions
[i
]);
1211 SetSelection(defaultFilter
);
1215 // wxGenericDirDialog implementation
1216 // This should be moved into dirdlgg.cpp eventually
1218 BEGIN_EVENT_TABLE(wxGenericDirDialog
, wxDialog
)
1219 EVT_BUTTON(wxID_OK
, wxGenericDirDialog::OnOK
)
1220 EVT_BUTTON(wxID_NEW
, wxGenericDirDialog::OnNew
)
1221 EVT_BUTTON (wxID_NEW
, wxGenericDirDialog::OnNew
)
1222 EVT_CLOSE(wxGenericDirDialog::OnCloseWindow
)
1223 EVT_TREE_KEY_DOWN (-1, wxGenericDirDialog::OnTreeKeyDown
)
1224 EVT_TREE_SEL_CHANGED (-1, wxGenericDirDialog::OnTreeSelected
)
1225 EVT_TEXT_ENTER (ID_TEXTCTRL
, wxGenericDirDialog::OnOK
)
1228 wxGenericDirDialog::wxGenericDirDialog(wxWindow
* parent
, const wxString
& title
,
1229 const wxString
& defaultPath
, long style
, const wxPoint
& pos
, const wxSize
& sz
, const wxString
& name
):
1230 wxDialog(parent
, ID_DIRCTRL
, title
, pos
, sz
, style
, name
)
1233 m_path
= defaultPath
;
1235 wxBusyCursor cursor
;
1237 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
1240 m_dirCtrl
= new wxGenericDirCtrl(this, ID_DIRCTRL
,
1241 defaultPath
, wxPoint(5, 5),
1242 wxSize(300, 200), wxDIRCTRL_DIR_ONLY
|wxSUNKEN_BORDER
);
1244 topsizer
->Add( m_dirCtrl
, 1, wxTOP
|wxLEFT
|wxRIGHT
| wxEXPAND
, 10 );
1247 m_input
= new wxTextCtrl( this, ID_TEXTCTRL
, m_path
, wxDefaultPosition
);
1248 topsizer
->Add( m_input
, 0, wxTOP
|wxLEFT
|wxRIGHT
| wxEXPAND
, 10 );
1252 topsizer
->Add( new wxStaticLine( this, -1 ), 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
1256 wxSizer
* buttonsizer
= new wxBoxSizer( wxHORIZONTAL
);
1257 wxButton
* okButton
= new wxButton(this, wxID_OK
, _("OK"));
1258 buttonsizer
->Add( okButton
, 0, wxLEFT
|wxRIGHT
, 10 );
1259 wxButton
* cancelButton
= new wxButton(this, wxID_CANCEL
, _("Cancel"));
1260 buttonsizer
->Add( cancelButton
, 0, wxLEFT
|wxRIGHT
, 10 );
1262 // I'm not convinced we need a New button, and we tend to get annoying
1263 // accidental-editing with label editing enabled.
1265 wxButton
* newButton
= new wxButton( this, wxID_NEW
, _("New...") );
1266 buttonsizer
->Add( newButton
, 0, wxLEFT
|wxRIGHT
, 10 );
1269 topsizer
->Add( buttonsizer
, 0, wxALL
| wxCENTER
, 10 );
1271 okButton
->SetDefault();
1272 m_dirCtrl
->SetFocus();
1274 SetAutoLayout( TRUE
);
1275 SetSizer( topsizer
);
1277 topsizer
->SetSizeHints( this );
1278 topsizer
->Fit( this );
1283 void wxGenericDirDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
1285 EndModal(wxID_CANCEL
);
1288 void wxGenericDirDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
1290 m_path
= m_input
->GetValue();
1291 // Does the path exist? (User may have typed anything in m_input)
1292 if (wxPathExists(m_path
)) {
1293 // OK, path exists, we're done.
1297 // Interact with user, find out if the dir is a typo or to be created
1298 wxString
msg( _("The directory ") );
1300 msg
= msg
+ _("\ndoes not exist\nCreate it now?") ;
1301 wxMessageDialog
dialog(this, msg
, _("Directory does not exist"), wxYES_NO
| wxICON_WARNING
);
1302 if ( dialog
.ShowModal() == wxID_YES
) {
1303 // Okay, let's make it
1305 if (wxMkdir(m_path
)) {
1306 // The new dir was created okay.
1312 msg
= _("Failed to create directory ")+m_path
+
1313 _("\n(Do you have the required permissions?)");
1314 wxMessageDialog
errmsg(this, msg
, _("Error creating directory"), wxOK
| wxICON_ERROR
);
1316 // We still don't have a valid dir. Back to the main dialog.
1319 // User has answered NO to create dir.
1322 void wxGenericDirDialog::SetPath(const wxString
& path
)
1324 m_dirCtrl
->SetPath(path
);
1328 wxString
wxGenericDirDialog::GetPath(void) const
1333 int wxGenericDirDialog::ShowModal()
1335 //m_input->SetValue( m_path );
1336 return wxDialog::ShowModal();
1339 void wxGenericDirDialog::OnTreeSelected( wxTreeEvent
&event
)
1344 wxDirItemDataEx
*data
= (wxDirItemDataEx
*)m_dirCtrl
->GetTreeCtrl()->GetItemData(event
.GetItem());
1346 m_input
->SetValue( data
->m_path
);
1349 void wxGenericDirDialog::OnTreeKeyDown( wxTreeEvent
&WXUNUSED(event
) )
1354 wxDirItemDataEx
*data
= (wxDirItemDataEx
*)m_dirCtrl
->GetTreeCtrl()->GetItemData(m_dirCtrl
->GetTreeCtrl()->GetSelection());
1356 m_input
->SetValue( data
->m_path
);
1359 void wxGenericDirDialog::OnNew( wxCommandEvent
& WXUNUSED(event
) )
1361 wxTreeItemId id
= m_dirCtrl
->GetTreeCtrl()->GetSelection();
1362 if ((id
== m_dirCtrl
->GetTreeCtrl()->GetRootItem()) ||
1363 (m_dirCtrl
->GetTreeCtrl()->GetParent(id
) == m_dirCtrl
->GetTreeCtrl()->GetRootItem()))
1365 wxMessageDialog
msg(this, _("You cannot add a new directory to this section."),
1366 _("Create directory"), wxOK
| wxICON_INFORMATION
);
1371 wxTreeItemId parent
= id
; // m_dirCtrl->GetTreeCtrl()->GetParent( id );
1372 wxDirItemDataEx
*data
= (wxDirItemDataEx
*)m_dirCtrl
->GetTreeCtrl()->GetItemData( parent
);
1375 wxString
new_name( wxT("NewName") );
1376 wxString
path( data
->m_path
);
1377 if (path
.Last() != wxFILE_SEP_PATH
)
1378 path
+= wxFILE_SEP_PATH
;
1380 if (wxFileExists(path
))
1382 // try NewName0, NewName1 etc.
1385 new_name
= wxT("NewName");
1387 num
.Printf( wxT("%d"), i
);
1390 path
= data
->m_path
;
1391 if (path
.Last() != wxFILE_SEP_PATH
)
1392 path
+= wxFILE_SEP_PATH
;
1395 } while (wxFileExists(path
));
1401 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
1406 wxDirItemDataEx
*new_data
= new wxDirItemDataEx( path
, new_name
, TRUE
);
1408 // TODO: THIS CODE DOESN'T WORK YET. We need to avoid duplication of the first child
1410 wxTreeItemId new_id
= m_dirCtrl
->GetTreeCtrl()->AppendItem( parent
, new_name
, 0, 0, new_data
);
1411 m_dirCtrl
->GetTreeCtrl()->EnsureVisible( new_id
);
1412 m_dirCtrl
->GetTreeCtrl()->EditLabel( new_id
);
1415 #endif // wxUSE_DIRDLG