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"
50 #if defined(__WXMAC__)
51 #include "wx/mac/private.h" // includes mac headers
57 // FIXME - Mingw32 1.0 has both _getdrive() and _chdrive(). For now, let's assume
58 // older releases don't, but it should be verified and the checks modified
60 #if !defined(__GNUWIN32__) || \
61 (defined(__MINGW32_MAJOR_VERSION) && __MINGW32_MAJOR_VERSION >= 1)
79 #if defined(__WXMAC__)
80 # include "MoreFilesExtras.h"
87 // If compiled under Windows, this macro can cause problems
93 static char * icon1_xpm
[] = {
94 /* width height ncolors chars_per_pixel */
122 static char * icon2_xpm
[] = {
123 /* width height ncolors chars_per_pixel */
151 static char * icon3_xpm
[] = {
152 /* width height ncolors chars_per_pixel */
177 static char * icon4_xpm
[] = {
204 static char * icon5_xpm
[] = {
231 static char *icon6_xpm
[] = {
261 static char * icon7_xpm
[] = {
288 static char * icon8_xpm
[] = {
314 static const int ID_DIRCTRL
= 1000;
315 static const int ID_TEXTCTRL
= 1001;
316 static const int ID_OK
= 1002;
317 static const int ID_CANCEL
= 1003;
318 static const int ID_NEW
= 1004;
319 //static const int ID_CHECK = 1005;
321 #if defined(__WXMSW__) || defined(__WXPM__) || defined(__DOS__)
322 int setdrive(int drive
)
324 #if defined(__GNUWIN32__) && \
325 (defined(__MINGW32_MAJOR_VERSION) && __MINGW32_MAJOR_VERSION >= 1)
326 return _chdrive(drive
);
330 if (drive
< 1 || drive
> 31)
332 newdrive
[0] = (wxChar
)(wxT('A') + drive
- 1);
333 newdrive
[1] = wxT(':');
334 newdrive
[2] = wxT('\0');
335 #if defined(__WXMSW__)
337 if (wxSetWorkingDirectory(newdrive
))
339 if (::SetCurrentDirectory(newdrive
))
342 // VA doesn't know what LPSTR is and has its own set
343 if (DosSetCurrentDir((PSZ
)newdrive
))
351 static bool wxIsDriveAvailable(const wxString dirName
)
354 UINT errorMode
= SetErrorMode(SEM_FAILCRITICALERRORS
| SEM_NOOPENFILEERRORBOX
);
358 // Check if this is a root directory and if so,
359 // whether the drive is avaiable.
360 if (dirName
.Len() == 3 && dirName
[(size_t)1] == wxT(':'))
362 wxString
dirNameLower(dirName
.Lower());
363 #if defined(__GNUWIN32__) && \
364 !(defined(__MINGW32_MAJOR_VERSION) && __MINGW32_MAJOR_VERSION >= 1)
365 success
= wxPathExists(dirNameLower
);
367 int currentDrive
= _getdrive();
368 int thisDrive
= (int) (dirNameLower
[(size_t)0] - 'a' + 1) ;
369 int err
= setdrive( thisDrive
) ;
370 setdrive( currentDrive
);
379 (void) SetErrorMode(errorMode
);
386 // Function which is called by quick sort. We want to override the default wxArrayString behaviour,
387 // and sort regardless of case.
388 static int LINKAGEMODE
wxDirCtrlStringCompareFunction(const void *first
, const void *second
)
390 wxString
*strFirst
= (wxString
*)first
;
391 wxString
*strSecond
= (wxString
*)second
;
393 return strFirst
->CmpNoCase(*strSecond
);
396 //-----------------------------------------------------------------------------
398 //-----------------------------------------------------------------------------
400 wxDirItemDataEx::wxDirItemDataEx(const wxString
& path
, const wxString
& name
,
405 /* Insert logic to detect hidden files here
406 * In UnixLand we just check whether the first char is a dot
407 * For FileNameFromPath read LastDirNameInThisPath ;-) */
408 // m_isHidden = (bool)(wxFileNameFromPath(*m_path)[0] == '.');
410 // m_hasSubDirs is no longer needed
411 m_hasSubDirs
= TRUE
; // HasSubDirs();
412 m_isExpanded
= FALSE
;
416 wxDirItemDataEx::~wxDirItemDataEx()
420 void wxDirItemDataEx::SetNewDirName( wxString path
)
423 m_name
= wxFileNameFromPath( path
);
426 //-----------------------------------------------------------------------------
428 //-----------------------------------------------------------------------------
430 IMPLEMENT_DYNAMIC_CLASS(wxGenericDirCtrl
, wxControl
)
432 BEGIN_EVENT_TABLE(wxGenericDirCtrl
, wxControl
)
433 EVT_TREE_ITEM_EXPANDING (-1, wxGenericDirCtrl::OnExpandItem
)
434 EVT_TREE_ITEM_COLLAPSED (-1, wxGenericDirCtrl::OnCollapseItem
)
435 EVT_TREE_BEGIN_LABEL_EDIT (-1, wxGenericDirCtrl::OnBeginEditItem
)
436 EVT_TREE_END_LABEL_EDIT (-1, wxGenericDirCtrl::OnEndEditItem
)
437 EVT_SIZE (wxGenericDirCtrl::OnSize
)
440 wxGenericDirCtrl::wxGenericDirCtrl(void)
445 bool wxGenericDirCtrl::Create(wxWindow
*parent
,
451 const wxString
& filter
,
453 const wxString
& name
)
455 if (!wxControl::Create(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
458 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
462 long treeStyle
= wxTR_HAS_BUTTONS
; // | wxTR_EDIT_LABELS;
463 if ((style
& wxDIRCTRL_3D_INTERNAL
) == 0)
464 treeStyle
|= wxNO_BORDER
;
466 long filterStyle
= 0;
467 if ((style
& wxDIRCTRL_3D_INTERNAL
) == 0)
468 filterStyle
|= wxNO_BORDER
;
470 m_treeCtrl
= new wxTreeCtrl(this, wxID_TREECTRL
, pos
, size
, treeStyle
);
472 if (!filter
.IsEmpty() && (style
& wxDIRCTRL_SHOW_FILTERS
))
473 m_filterListCtrl
= new wxDirFilterListCtrl(this, wxID_FILTERLISTCTRL
, wxDefaultPosition
, wxDefaultSize
, filterStyle
);
478 SetFilterIndex(defaultFilter
);
480 if (m_filterListCtrl
)
481 m_filterListCtrl
->FillFilterList(filter
, defaultFilter
);
483 m_imageList
= new wxImageList(16, 16, TRUE
);
484 m_imageList
->Add(wxIcon(icon1_xpm
));
485 m_imageList
->Add(wxIcon(icon2_xpm
));
486 m_imageList
->Add(wxIcon(icon3_xpm
));
487 m_imageList
->Add(wxIcon(icon4_xpm
));
488 m_imageList
->Add(wxIcon(icon5_xpm
));
489 m_imageList
->Add(wxIcon(icon6_xpm
));
490 m_imageList
->Add(wxIcon(icon7_xpm
));
491 m_imageList
->Add(wxIcon(icon8_xpm
));
492 m_treeCtrl
->SetImageList(m_imageList
);
494 m_showHidden
= FALSE
;
495 wxDirItemDataEx
* rootData
= new wxDirItemDataEx(wxT(""), wxT(""), TRUE
);
499 #if defined(__WXMSW__) || defined(__WXPM__)
500 rootName
= _("Computer");
502 rootName
= _("Sections");
505 m_rootId
= m_treeCtrl
->AddRoot( rootName
, 3, -1, rootData
);
506 m_treeCtrl
->SetItemHasChildren(m_rootId
);
507 m_treeCtrl
->Expand(m_rootId
); // automatically expand first level
509 // Expand and select the default path
510 if (!m_defaultPath
.IsEmpty())
511 ExpandPath(m_defaultPath
);
518 wxGenericDirCtrl::~wxGenericDirCtrl()
520 m_treeCtrl
->SetImageList(NULL
);
524 void wxGenericDirCtrl::Init()
526 m_showHidden
= FALSE
;
529 m_currentFilterStr
= wxEmptyString
; // Default: any file
531 m_filterListCtrl
= NULL
;
534 void wxGenericDirCtrl::AddSection(const wxString
& path
, const wxString
& name
, int imageId
)
536 wxDirItemDataEx
*dir_item
= new wxDirItemDataEx(path
,name
,TRUE
);
538 #if defined(__WXMSW__) || defined(__WXPM__)
539 // Windows and OS/2: sections are displayed as drives
540 wxTreeItemId id
= m_treeCtrl
->AppendItem( m_rootId
, name
, imageId
, -1, dir_item
);
542 // Unix: sections are displayed as folders
543 wxTreeItemId id
= m_treeCtrl
->AppendItem( m_rootId
, name
, 0, -1, dir_item
);
544 m_treeCtrl
->SetItemImage( id
, 1, wxTreeItemIcon_Expanded
);
546 // TODO: other operating systems.
548 m_treeCtrl
->SetItemHasChildren(id
);
551 void wxGenericDirCtrl::SetupSections()
553 #if defined(__WXMSW__) || defined(__WXPM__)
556 wxChar driveBuffer
[256];
557 size_t n
= (size_t) GetLogicalDriveStrings(255, driveBuffer
);
562 path
.Printf(wxT("%c:\\"), driveBuffer
[i
]);
563 name
.Printf(wxT("(%c:)"), driveBuffer
[i
]);
566 int driveType
= ::GetDriveType(path
);
569 case DRIVE_REMOVABLE
:
570 if (path
== wxT("a:\\") || path
== wxT("b:\\"))
571 imageId
= 6; // Floppy
589 AddSection(path
, name
, imageId
);
591 while (driveBuffer
[i
] != wxT('\0'))
594 if (driveBuffer
[i
] == wxT('\0'))
601 /* If we can switch to the drive, it exists. */
602 for( drive
= 1; drive
<= 26; drive
++ )
605 path
.Printf(wxT("%c:\\"), (char) (drive
+ 'a' - 1));
606 name
.Printf(wxT("(%c:)"), (char) (drive
+ 'a' - 1));
608 if (wxIsDriveAvailable(path
))
611 AddSection(path
, name
);
615 #elif defined(__WXMAC__)
619 short actualCount
= 0 ;
620 if ( OnLine( &volume
, 1 , &actualCount
, &index
) != noErr
|| actualCount
== 0 )
623 wxString name
= wxMacFSSpec2MacFilename( &volume
) ;
624 AddSection(name
+ wxFILE_SEP_PATH
, name
, 0);
627 AddSection(wxT("/"), _("The Computer"), 0);
628 AddSection(wxGetHomeDir(), _("My Home"), 0 );
629 AddSection(wxT("/mnt"), _("Mounted Devices"), 0 );
630 AddSection(wxT("/usr/local"), _("User Local"), 0 );
631 AddSection(wxT("/usr"), _("User"), 0 );
632 AddSection(wxT("/var"), _("Variables"), 0 );
633 AddSection(wxT("/etc"), _("Etcetera"), 0 );
634 AddSection(wxT("/tmp"), _("Temporary"), 0 );
638 void wxGenericDirCtrl::OnBeginEditItem(wxTreeEvent
&event
)
640 // don't rename the main entry "Sections"
641 if (event
.GetItem() == m_rootId
)
647 // don't rename the individual sections
648 if (m_treeCtrl
->GetParent( event
.GetItem() ) == m_rootId
)
655 void wxGenericDirCtrl::OnEndEditItem(wxTreeEvent
&event
)
657 if ((event
.GetLabel().IsEmpty()) ||
658 (event
.GetLabel() == _(".")) ||
659 (event
.GetLabel() == _("..")) ||
660 (event
.GetLabel().First( wxT("/") ) != wxNOT_FOUND
))
662 wxMessageDialog
dialog(this, _("Illegal directory name."), _("Error"), wxOK
| wxICON_ERROR
);
668 wxTreeItemId id
= event
.GetItem();
669 wxDirItemDataEx
*data
= (wxDirItemDataEx
*)m_treeCtrl
->GetItemData( id
);
672 wxString
new_name( wxPathOnly( data
->m_path
) );
673 new_name
+= wxString(wxFILE_SEP_PATH
);
674 new_name
+= event
.GetLabel();
678 if (wxFileExists(new_name
))
680 wxMessageDialog
dialog(this, _("File name exists already."), _("Error"), wxOK
| wxICON_ERROR
);
685 if (wxRenameFile(data
->m_path
,new_name
))
687 data
->SetNewDirName( new_name
);
691 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
697 void wxGenericDirCtrl::OnExpandItem(wxTreeEvent
&event
)
699 wxTreeItemId parentId
= event
.GetItem();
704 void wxGenericDirCtrl::OnCollapseItem(wxTreeEvent
&event
)
706 wxTreeItemId child
, parent
= event
.GetItem();
708 wxDirItemDataEx
*data
= (wxDirItemDataEx
*) m_treeCtrl
->GetItemData(event
.GetItem());
709 if (!data
->m_isExpanded
)
712 data
->m_isExpanded
= FALSE
;
714 /* Workaround because DeleteChildren has disapeared (why?) and
715 * CollapseAndReset doesn't work as advertised (deletes parent too) */
716 child
= m_treeCtrl
->GetFirstChild(parent
, cookie
);
719 m_treeCtrl
->Delete(child
);
720 /* Not GetNextChild below, because the cookie mechanism can't
721 * handle disappearing children! */
722 child
= m_treeCtrl
->GetFirstChild(parent
, cookie
);
726 void wxGenericDirCtrl::ExpandDir(wxTreeItemId parentId
)
728 wxDirItemDataEx
*data
= (wxDirItemDataEx
*) m_treeCtrl
->GetItemData(parentId
);
730 if (data
->m_isExpanded
)
733 data
->m_isExpanded
= TRUE
;
735 if (parentId
== m_rootId
)
743 wxString search
,path
,filename
;
745 wxString
dirName(data
->m_path
);
747 #if defined(__WXMSW__) || defined(__WXPM__)
748 // Check if this is a root directory and if so,
749 // whether the drive is avaiable.
750 if (!wxIsDriveAvailable(dirName
))
752 data
->m_isExpanded
= FALSE
;
753 //wxMessageBox(wxT("Sorry, this drive is not available."));
758 // This may take a longish time. Go to busy cursor
761 #if defined(__WXMSW__) || defined(__WXPM__)
762 if (dirName
.Last() == ':')
763 dirName
+= wxString(wxFILE_SEP_PATH
);
767 wxArrayString filenames
;
770 wxString eachFilename
;
777 if (d
.GetFirst(& eachFilename
, wxEmptyString
, wxDIR_DIRS
))
781 if ((eachFilename
!= wxT(".")) && (eachFilename
!= wxT("..")))
783 dirs
.Add(eachFilename
);
786 while (d
.GetNext(& eachFilename
)) ;
789 dirs
.Sort((wxArrayString::CompareFunction
) wxDirCtrlStringCompareFunction
);
791 // Now do the filenames -- but only if we're allowed to
792 if ((GetWindowStyle() & wxDIRCTRL_DIR_ONLY
) == 0)
800 if (d
.GetFirst(& eachFilename
, m_currentFilterStr
, wxDIR_FILES
))
804 if ((eachFilename
!= wxT(".")) && (eachFilename
!= wxT("..")))
806 filenames
.Add(eachFilename
);
809 while (d
.GetNext(& eachFilename
)) ;
812 filenames
.Sort((wxArrayString::CompareFunction
) wxDirCtrlStringCompareFunction
);
815 // Add the sorted dirs
817 for (i
= 0; i
< dirs
.Count(); i
++)
819 wxString
eachFilename(dirs
[i
]);
821 if (path
.Last() != wxFILE_SEP_PATH
)
822 path
+= wxString(wxFILE_SEP_PATH
);
823 path
+= eachFilename
;
825 wxDirItemDataEx
*dir_item
= new wxDirItemDataEx(path
,eachFilename
,TRUE
);
826 wxTreeItemId id
= m_treeCtrl
->AppendItem( parentId
, eachFilename
, 0, -1, dir_item
);
827 m_treeCtrl
->SetItemImage( id
, 1, wxTreeItemIcon_Expanded
);
829 // Has this got any children? If so, make it expandable.
830 int options
= wxDIR_DEFAULT
;
831 if (GetWindowStyle() & wxDIRCTRL_DIR_ONLY
) // If only showing dirs, then we specify dirs only here
833 options
= wxDIR_DIRS
;
841 // Have to test for wxDIR_DIRS separately in case m_currentFilterStr is non-empty and
842 // and filters out any directories
843 if (dir2
.GetFirst(& str
, m_currentFilterStr
, options
) || dir2
.GetFirst(& str
, wxEmptyString
, wxDIR_DIRS
))
845 m_treeCtrl
->SetItemHasChildren(id
);
850 // Add the sorted filenames
851 if ((GetWindowStyle() & wxDIRCTRL_DIR_ONLY
) == 0)
853 for (i
= 0; i
< filenames
.Count(); i
++)
855 wxString
eachFilename(filenames
[i
]);
857 if (path
.Last() != wxFILE_SEP_PATH
)
858 path
+= wxString(wxFILE_SEP_PATH
);
859 path
+= eachFilename
;
860 //path = dirName + wxString(wxT("/")) + eachFilename;
861 wxDirItemDataEx
*dir_item
= new wxDirItemDataEx(path
,eachFilename
,FALSE
);
862 (void)m_treeCtrl
->AppendItem( parentId
, eachFilename
, 2, -1, dir_item
);
867 // Find the child that matches the first part of 'path'.
868 // E.g. if a child path is "/usr" and 'path' is "/usr/include"
869 // then the child for /usr is returned.
870 wxTreeItemId
wxGenericDirCtrl::FindChild(wxTreeItemId parentId
, const wxString
& path
, bool& done
)
872 wxString
path2(path
);
874 // Make sure all separators are as per the current platform
875 path2
.Replace(wxT("\\"), wxString(wxFILE_SEP_PATH
));
876 path2
.Replace(wxT("/"), wxString(wxFILE_SEP_PATH
));
878 // Append a separator to foil bogus substring matching
879 path2
+= wxString(wxFILE_SEP_PATH
);
881 // In MSW or PM, case is not significant
882 #if defined(__WXMSW__) || defined(__WXPM__)
887 wxTreeItemId childId
= m_treeCtrl
->GetFirstChild(parentId
, cookie
);
888 while (childId
.IsOk())
890 wxDirItemDataEx
* data
= (wxDirItemDataEx
*) m_treeCtrl
->GetItemData(childId
);
892 if (data
&& data
->m_path
!= "")
894 wxString
childPath(data
->m_path
);
895 if (childPath
.Last() != wxFILE_SEP_PATH
)
896 childPath
+= wxString(wxFILE_SEP_PATH
);
898 // In MSW and PM, case is not significant
899 #if defined(__WXMSW__) || defined(__WXPM__)
900 childPath
.MakeLower();
903 if (childPath
.Len() <= path2
.Len())
905 wxString path3
= path2
.Mid(0, childPath
.Len());
906 if (childPath
== path3
)
908 if (path3
.Len() == path2
.Len())
917 childId
= m_treeCtrl
->GetNextChild(childId
, cookie
);
919 wxTreeItemId invalid
;
923 // Try to expand as much of the given path as possible,
924 // and select the given tree item.
925 bool wxGenericDirCtrl::ExpandPath(const wxString
& path
)
928 wxTreeItemId id
= FindChild(m_rootId
, path
, done
);
929 wxTreeItemId lastId
= id
; // The last non-zero id
930 while (id
.IsOk() && !done
)
934 id
= FindChild(id
, path
, done
);
940 wxDirItemDataEx
*data
= (wxDirItemDataEx
*) m_treeCtrl
->GetItemData(lastId
);
943 m_treeCtrl
->Expand(lastId
);
945 if ((GetWindowStyle() & wxDIRCTRL_SELECT_FIRST
) && data
->m_isDir
)
947 // Find the first file in this directory
949 wxTreeItemId childId
= m_treeCtrl
->GetFirstChild(lastId
, cookie
);
950 bool selectedChild
= FALSE
;
951 while (childId
.IsOk())
953 wxDirItemDataEx
* data
= (wxDirItemDataEx
*) m_treeCtrl
->GetItemData(childId
);
955 if (data
&& data
->m_path
!= "" && !data
->m_isDir
)
957 m_treeCtrl
->SelectItem(childId
);
958 m_treeCtrl
->EnsureVisible(childId
);
959 selectedChild
= TRUE
;
962 childId
= m_treeCtrl
->GetNextChild(lastId
, cookie
);
966 m_treeCtrl
->SelectItem(lastId
);
967 m_treeCtrl
->EnsureVisible(lastId
);
972 m_treeCtrl
->SelectItem(lastId
);
973 m_treeCtrl
->EnsureVisible(lastId
);
982 wxString
wxGenericDirCtrl::GetPath() const
984 wxTreeItemId id
= m_treeCtrl
->GetSelection();
987 wxDirItemDataEx
* data
= (wxDirItemDataEx
*) m_treeCtrl
->GetItemData(id
);
991 return wxEmptyString
;
994 wxString
wxGenericDirCtrl::GetFilePath() const
996 wxTreeItemId id
= m_treeCtrl
->GetSelection();
999 wxDirItemDataEx
* data
= (wxDirItemDataEx
*) m_treeCtrl
->GetItemData(id
);
1001 return wxEmptyString
;
1003 return data
->m_path
;
1006 return wxEmptyString
;
1009 void wxGenericDirCtrl::SetPath(const wxString
& path
)
1011 m_defaultPath
= path
;
1018 void wxGenericDirCtrl::FindChildFiles(wxTreeItemId id
, int dirFlags
, wxArrayString
& filenames
)
1020 wxDirItemDataEx
*data
= (wxDirItemDataEx
*) m_treeCtrl
->GetItemData(id
);
1022 // This may take a longish time. Go to busy cursor
1027 wxString search
,path
,filename
;
1029 wxString
dirName(data
->m_path
);
1031 #if defined(__WXMSW__) || defined(__WXPM__)
1032 if (dirName
.Last() == ':')
1033 dirName
+= wxString(wxFILE_SEP_PATH
);
1037 wxString eachFilename
;
1044 if (d
.GetFirst(& eachFilename
, m_currentFilterStr
, dirFlags
))
1048 if ((eachFilename
!= wxT(".")) && (eachFilename
!= wxT("..")))
1050 filenames
.Add(eachFilename
);
1053 while (d
.GetNext(& eachFilename
)) ;
1059 void wxGenericDirCtrl::SetFilterIndex(int n
)
1061 m_currentFilter
= n
;
1064 if (ExtractWildcard(m_filter
, n
, f
, d
))
1065 m_currentFilterStr
= f
;
1067 m_currentFilterStr
= wxT("*.*");
1070 void wxGenericDirCtrl::SetFilter(const wxString
& filter
)
1075 if (ExtractWildcard(m_filter
, m_currentFilter
, f
, d
))
1076 m_currentFilterStr
= f
;
1078 m_currentFilterStr
= wxT("*.*");
1081 // Extract description and actual filter from overall filter string
1082 bool wxGenericDirCtrl::ExtractWildcard(const wxString
& filterStr
, int n
, wxString
& filter
, wxString
& description
)
1084 wxArrayString filters
, descriptions
;
1085 int count
= ParseFilter(filterStr
, filters
, descriptions
);
1086 if (count
> 0 && n
< count
)
1088 filter
= filters
[n
];
1089 description
= descriptions
[n
];
1096 // Parses the global filter, returning the number of filters.
1097 // Returns 0 if none or if there's a problem.
1098 // filterStr is in the form:
1100 // "All files (*.*)|*.*|JPEG Files (*.jpeg)|*.jpg"
1102 int wxGenericDirCtrl::ParseFilter(const wxString
& filterStr
, wxArrayString
& filters
, wxArrayString
& descriptions
)
1104 wxString
str(filterStr
);
1106 wxString description
, filter
;
1108 bool finished
= FALSE
;
1111 pos
= str
.Find(wxT('|'));
1113 return 0; // Problem
1114 description
= str
.Left(pos
);
1115 str
= str
.Mid(pos
+1);
1116 pos
= str
.Find(wxT('|'));
1124 filter
= str
.Left(pos
);
1125 str
= str
.Mid(pos
+1);
1127 descriptions
.Add(description
);
1128 filters
.Add(filter
);
1132 return filters
.Count();
1135 void wxGenericDirCtrl::DoResize()
1137 wxSize sz
= GetClientSize();
1138 int verticalSpacing
= 3;
1142 if (m_filterListCtrl
)
1144 filterSz
= m_filterListCtrl
->GetSize();
1145 sz
.y
-= (filterSz
.y
+ verticalSpacing
);
1147 m_treeCtrl
->SetSize(0, 0, sz
.x
, sz
.y
);
1148 if (m_filterListCtrl
)
1150 m_filterListCtrl
->SetSize(0, sz
.y
+ verticalSpacing
, sz
.x
, filterSz
.y
);
1151 // Don't know why, but this needs refreshing after a resize (wxMSW)
1152 m_filterListCtrl
->Refresh();
1158 void wxGenericDirCtrl::OnSize(wxSizeEvent
& WXUNUSED(event
))
1163 //-----------------------------------------------------------------------------
1164 // wxDirFilterListCtrl
1165 //-----------------------------------------------------------------------------
1167 IMPLEMENT_CLASS(wxDirFilterListCtrl
, wxChoice
)
1169 BEGIN_EVENT_TABLE(wxDirFilterListCtrl
, wxChoice
)
1170 EVT_CHOICE(-1, wxDirFilterListCtrl::OnSelFilter
)
1173 bool wxDirFilterListCtrl::Create(wxGenericDirCtrl
* parent
, const wxWindowID id
,
1179 return wxChoice::Create(parent
, id
, pos
, size
, 0, NULL
, style
);
1182 void wxDirFilterListCtrl::Init()
1187 void wxDirFilterListCtrl::OnSelFilter(wxCommandEvent
& WXUNUSED(event
))
1189 int sel
= GetSelection();
1191 wxString currentPath
= m_dirCtrl
->GetPath();
1193 m_dirCtrl
->SetFilterIndex(sel
);
1195 // If the filter has changed, the view is out of date, so
1196 // collapse the tree.
1197 m_dirCtrl
->GetTreeCtrl()->Collapse(m_dirCtrl
->GetRootId());
1198 m_dirCtrl
->GetTreeCtrl()->Expand(m_dirCtrl
->GetRootId());
1200 // Try to restore the selection, or at least the directory
1201 m_dirCtrl
->ExpandPath(currentPath
);
1204 void wxDirFilterListCtrl::FillFilterList(const wxString
& filter
, int defaultFilter
)
1207 wxArrayString descriptions
, filters
;
1208 size_t n
= (size_t) m_dirCtrl
->ParseFilter(filter
, filters
, descriptions
);
1210 if (n
> 0 && defaultFilter
< (int) n
)
1213 for (i
= 0; i
< n
; i
++)
1214 Append(descriptions
[i
]);
1215 SetSelection(defaultFilter
);
1219 // wxGenericDirDialog implementation
1220 // This should be moved into dirdlgg.cpp eventually
1222 BEGIN_EVENT_TABLE(wxGenericDirDialog
, wxDialog
)
1223 EVT_BUTTON(wxID_OK
, wxGenericDirDialog::OnOK
)
1224 EVT_BUTTON(wxID_NEW
, wxGenericDirDialog::OnNew
)
1225 EVT_BUTTON (wxID_NEW
, wxGenericDirDialog::OnNew
)
1226 EVT_CLOSE(wxGenericDirDialog::OnCloseWindow
)
1227 EVT_TREE_KEY_DOWN (-1, wxGenericDirDialog::OnTreeKeyDown
)
1228 EVT_TREE_SEL_CHANGED (-1, wxGenericDirDialog::OnTreeSelected
)
1229 EVT_TEXT_ENTER (ID_TEXTCTRL
, wxGenericDirDialog::OnOK
)
1232 wxGenericDirDialog::wxGenericDirDialog(wxWindow
* parent
, const wxString
& title
,
1233 const wxString
& defaultPath
, long style
, const wxPoint
& pos
, const wxSize
& sz
, const wxString
& name
):
1234 wxDialog(parent
, ID_DIRCTRL
, title
, pos
, sz
, style
, name
)
1237 m_path
= defaultPath
;
1239 wxBusyCursor cursor
;
1241 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
1244 m_dirCtrl
= new wxGenericDirCtrl(this, ID_DIRCTRL
,
1245 defaultPath
, wxPoint(5, 5),
1246 wxSize(300, 200), wxDIRCTRL_DIR_ONLY
|wxSUNKEN_BORDER
);
1248 topsizer
->Add( m_dirCtrl
, 1, wxTOP
|wxLEFT
|wxRIGHT
| wxEXPAND
, 10 );
1251 m_input
= new wxTextCtrl( this, ID_TEXTCTRL
, m_path
, wxDefaultPosition
);
1252 topsizer
->Add( m_input
, 0, wxTOP
|wxLEFT
|wxRIGHT
| wxEXPAND
, 10 );
1256 topsizer
->Add( new wxStaticLine( this, -1 ), 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
1260 wxSizer
* buttonsizer
= new wxBoxSizer( wxHORIZONTAL
);
1261 wxButton
* okButton
= new wxButton(this, wxID_OK
, _("OK"));
1262 buttonsizer
->Add( okButton
, 0, wxLEFT
|wxRIGHT
, 10 );
1263 wxButton
* cancelButton
= new wxButton(this, wxID_CANCEL
, _("Cancel"));
1264 buttonsizer
->Add( cancelButton
, 0, wxLEFT
|wxRIGHT
, 10 );
1266 // I'm not convinced we need a New button, and we tend to get annoying
1267 // accidental-editing with label editing enabled.
1269 wxButton
* newButton
= new wxButton( this, wxID_NEW
, _("New...") );
1270 buttonsizer
->Add( newButton
, 0, wxLEFT
|wxRIGHT
, 10 );
1273 topsizer
->Add( buttonsizer
, 0, wxALL
| wxCENTER
, 10 );
1275 okButton
->SetDefault();
1276 m_dirCtrl
->SetFocus();
1278 SetAutoLayout( TRUE
);
1279 SetSizer( topsizer
);
1281 topsizer
->SetSizeHints( this );
1282 topsizer
->Fit( this );
1287 void wxGenericDirDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
1289 EndModal(wxID_CANCEL
);
1292 void wxGenericDirDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
1294 m_path
= m_input
->GetValue();
1295 // Does the path exist? (User may have typed anything in m_input)
1296 if (wxPathExists(m_path
)) {
1297 // OK, path exists, we're done.
1301 // Interact with user, find out if the dir is a typo or to be created
1302 wxString
msg( _("The directory ") );
1304 msg
= msg
+ _("\ndoes not exist\nCreate it now?") ;
1305 wxMessageDialog
dialog(this, msg
, _("Directory does not exist"), wxYES_NO
| wxICON_WARNING
);
1306 if ( dialog
.ShowModal() == wxID_YES
) {
1307 // Okay, let's make it
1309 if (wxMkdir(m_path
)) {
1310 // The new dir was created okay.
1316 msg
= _("Failed to create directory ")+m_path
+
1317 _("\n(Do you have the required permissions?)");
1318 wxMessageDialog
errmsg(this, msg
, _("Error creating directory"), wxOK
| wxICON_ERROR
);
1320 // We still don't have a valid dir. Back to the main dialog.
1323 // User has answered NO to create dir.
1326 void wxGenericDirDialog::SetPath(const wxString
& path
)
1328 m_dirCtrl
->SetPath(path
);
1332 wxString
wxGenericDirDialog::GetPath(void) const
1337 int wxGenericDirDialog::ShowModal()
1339 //m_input->SetValue( m_path );
1340 return wxDialog::ShowModal();
1343 void wxGenericDirDialog::OnTreeSelected( wxTreeEvent
&event
)
1348 wxDirItemDataEx
*data
= (wxDirItemDataEx
*)m_dirCtrl
->GetTreeCtrl()->GetItemData(event
.GetItem());
1350 m_input
->SetValue( data
->m_path
);
1353 void wxGenericDirDialog::OnTreeKeyDown( wxTreeEvent
&WXUNUSED(event
) )
1358 wxDirItemDataEx
*data
= (wxDirItemDataEx
*)m_dirCtrl
->GetTreeCtrl()->GetItemData(m_dirCtrl
->GetTreeCtrl()->GetSelection());
1360 m_input
->SetValue( data
->m_path
);
1363 void wxGenericDirDialog::OnNew( wxCommandEvent
& WXUNUSED(event
) )
1365 wxTreeItemId id
= m_dirCtrl
->GetTreeCtrl()->GetSelection();
1366 if ((id
== m_dirCtrl
->GetTreeCtrl()->GetRootItem()) ||
1367 (m_dirCtrl
->GetTreeCtrl()->GetParent(id
) == m_dirCtrl
->GetTreeCtrl()->GetRootItem()))
1369 wxMessageDialog
msg(this, _("You cannot add a new directory to this section."),
1370 _("Create directory"), wxOK
| wxICON_INFORMATION
);
1375 wxTreeItemId parent
= id
; // m_dirCtrl->GetTreeCtrl()->GetParent( id );
1376 wxDirItemDataEx
*data
= (wxDirItemDataEx
*)m_dirCtrl
->GetTreeCtrl()->GetItemData( parent
);
1379 wxString
new_name( wxT("NewName") );
1380 wxString
path( data
->m_path
);
1381 if (path
.Last() != wxFILE_SEP_PATH
)
1382 path
+= wxFILE_SEP_PATH
;
1384 if (wxFileExists(path
))
1386 // try NewName0, NewName1 etc.
1389 new_name
= wxT("NewName");
1391 num
.Printf( wxT("%d"), i
);
1394 path
= data
->m_path
;
1395 if (path
.Last() != wxFILE_SEP_PATH
)
1396 path
+= wxFILE_SEP_PATH
;
1399 } while (wxFileExists(path
));
1405 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
1410 wxDirItemDataEx
*new_data
= new wxDirItemDataEx( path
, new_name
, TRUE
);
1412 // TODO: THIS CODE DOESN'T WORK YET. We need to avoid duplication of the first child
1414 wxTreeItemId new_id
= m_dirCtrl
->GetTreeCtrl()->AppendItem( parent
, new_name
, 0, 0, new_data
);
1415 m_dirCtrl
->GetTreeCtrl()->EnsureVisible( new_id
);
1416 m_dirCtrl
->GetTreeCtrl()->EditLabel( new_id
);
1419 #endif // wxUSE_DIRDLG