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"
70 #if defined(__WXMAC__) && !defined(__UNIX__)
78 // If compiled under Windows, this macro can cause problems
84 static char * icon1_xpm
[] = {
85 /* width height ncolors chars_per_pixel */
113 static char * icon2_xpm
[] = {
114 /* width height ncolors chars_per_pixel */
142 static char * icon3_xpm
[] = {
143 /* width height ncolors chars_per_pixel */
168 static char * icon4_xpm
[] = {
195 static char * icon5_xpm
[] = {
222 static char *icon6_xpm
[] = {
252 static char * icon7_xpm
[] = {
279 static char * icon8_xpm
[] = {
305 static const int ID_DIRCTRL
= 1000;
306 static const int ID_TEXTCTRL
= 1001;
307 static const int ID_OK
= 1002;
308 static const int ID_CANCEL
= 1003;
309 static const int ID_NEW
= 1004;
310 //static const int ID_CHECK = 1005;
312 #if defined(__WXMSW__) || defined(__WXPM__)
313 int setdrive(int drive
)
317 if (drive
< 1 || drive
> 31)
319 newdrive
[0] = (wxChar
)(wxT('A') + drive
- 1);
320 newdrive
[1] = wxT(':');
321 newdrive
[2] = wxT('\0');
322 #if defined(__WXMSW__)
324 if (wxSetWorkingDirectory(newdrive
))
326 if (::SetCurrentDirectory(newdrive
))
329 // VA doesn't know what LPSTR is and has its own set
330 if (DosSetCurrentDir((PSZ
)newdrive
))
337 static bool wxIsDriveAvailable(const wxString dirName
)
340 UINT errorMode
= SetErrorMode(SEM_FAILCRITICALERRORS
| SEM_NOOPENFILEERRORBOX
);
344 // Check if this is a root directory and if so,
345 // whether the drive is avaiable.
346 if (dirName
.Len() == 3 && dirName
[(size_t)1] == wxT(':'))
348 wxString
dirNameLower(dirName
.Lower());
349 #if defined(__GNUWIN32__)
350 success
= wxPathExists(dirNameLower
);
352 int currentDrive
= _getdrive();
353 int thisDrive
= (int) (dirNameLower
[(size_t)0] - 'a' + 1) ;
354 int err
= setdrive( thisDrive
) ;
355 setdrive( currentDrive
);
364 (void) SetErrorMode(errorMode
);
371 // Function which is called by quick sort. We want to override the default wxArrayString behaviour,
372 // and sort regardless of case.
373 static int LINKAGEMODE
wxDirCtrlStringCompareFunction(const void *first
, const void *second
)
375 wxString
*strFirst
= (wxString
*)first
;
376 wxString
*strSecond
= (wxString
*)second
;
378 return strFirst
->CmpNoCase(*strSecond
);
381 //-----------------------------------------------------------------------------
383 //-----------------------------------------------------------------------------
385 wxDirItemDataEx::wxDirItemDataEx(const wxString
& path
, const wxString
& name
,
390 /* Insert logic to detect hidden files here
391 * In UnixLand we just check whether the first char is a dot
392 * For FileNameFromPath read LastDirNameInThisPath ;-) */
393 // m_isHidden = (bool)(wxFileNameFromPath(*m_path)[0] == '.');
395 // m_hasSubDirs is no longer needed
396 m_hasSubDirs
= TRUE
; // HasSubDirs();
397 m_isExpanded
= FALSE
;
401 wxDirItemDataEx::~wxDirItemDataEx()
405 void wxDirItemDataEx::SetNewDirName( wxString path
)
408 m_name
= wxFileNameFromPath( path
);
411 //-----------------------------------------------------------------------------
413 //-----------------------------------------------------------------------------
415 IMPLEMENT_DYNAMIC_CLASS(wxGenericDirCtrl
, wxControl
)
417 BEGIN_EVENT_TABLE(wxGenericDirCtrl
, wxControl
)
418 EVT_TREE_ITEM_EXPANDING (-1, wxGenericDirCtrl::OnExpandItem
)
419 EVT_TREE_ITEM_COLLAPSED (-1, wxGenericDirCtrl::OnCollapseItem
)
420 EVT_TREE_BEGIN_LABEL_EDIT (-1, wxGenericDirCtrl::OnBeginEditItem
)
421 EVT_TREE_END_LABEL_EDIT (-1, wxGenericDirCtrl::OnEndEditItem
)
422 EVT_SIZE (wxGenericDirCtrl::OnSize
)
425 wxGenericDirCtrl::wxGenericDirCtrl(void)
430 bool wxGenericDirCtrl::Create(wxWindow
*parent
,
436 const wxString
& filter
,
438 const wxString
& name
)
440 if (!wxControl::Create(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
443 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
447 long treeStyle
= wxTR_HAS_BUTTONS
; // | wxTR_EDIT_LABELS;
448 if ((style
& wxDIRCTRL_3D_INTERNAL
) == 0)
449 treeStyle
|= wxNO_BORDER
;
451 long filterStyle
= 0;
452 if ((style
& wxDIRCTRL_3D_INTERNAL
) == 0)
453 filterStyle
|= wxNO_BORDER
;
455 m_treeCtrl
= new wxTreeCtrl(this, wxID_TREECTRL
, pos
, size
, treeStyle
);
457 if (!filter
.IsEmpty() && (style
& wxDIRCTRL_SHOW_FILTERS
))
458 m_filterListCtrl
= new wxDirFilterListCtrl(this, wxID_FILTERLISTCTRL
, wxDefaultPosition
, wxDefaultSize
, filterStyle
);
463 SetFilterIndex(defaultFilter
);
465 if (m_filterListCtrl
)
466 m_filterListCtrl
->FillFilterList(filter
, defaultFilter
);
468 m_imageList
= new wxImageList(16, 16, TRUE
);
469 m_imageList
->Add(wxIcon(icon1_xpm
));
470 m_imageList
->Add(wxIcon(icon2_xpm
));
471 m_imageList
->Add(wxIcon(icon3_xpm
));
472 m_imageList
->Add(wxIcon(icon4_xpm
));
473 m_imageList
->Add(wxIcon(icon5_xpm
));
474 m_imageList
->Add(wxIcon(icon6_xpm
));
475 m_imageList
->Add(wxIcon(icon7_xpm
));
476 m_imageList
->Add(wxIcon(icon8_xpm
));
477 m_treeCtrl
->SetImageList(m_imageList
);
479 m_showHidden
= FALSE
;
480 wxDirItemDataEx
* rootData
= new wxDirItemDataEx(wxT(""), wxT(""), TRUE
);
484 #if defined(__WXMSW__) || defined(__WXPM__)
485 rootName
= _("Computer");
487 rootName
= _("Sections");
490 m_rootId
= m_treeCtrl
->AddRoot( rootName
, 3, -1, rootData
);
491 m_treeCtrl
->SetItemHasChildren(m_rootId
);
492 m_treeCtrl
->Expand(m_rootId
); // automatically expand first level
494 // Expand and select the default path
495 if (!m_defaultPath
.IsEmpty())
496 ExpandPath(m_defaultPath
);
503 wxGenericDirCtrl::~wxGenericDirCtrl()
505 m_treeCtrl
->SetImageList(NULL
);
509 void wxGenericDirCtrl::Init()
511 m_showHidden
= FALSE
;
514 m_currentFilterStr
= wxEmptyString
; // Default: any file
516 m_filterListCtrl
= NULL
;
519 void wxGenericDirCtrl::AddSection(const wxString
& path
, const wxString
& name
, int imageId
)
521 wxDirItemDataEx
*dir_item
= new wxDirItemDataEx(path
,name
,TRUE
);
523 #if defined(__WXMSW__) || defined(__WXPM__)
524 // Windows and OS/2: sections are displayed as drives
525 wxTreeItemId id
= m_treeCtrl
->AppendItem( m_rootId
, name
, imageId
, -1, dir_item
);
527 // Unix: sections are displayed as folders
528 wxTreeItemId id
= m_treeCtrl
->AppendItem( m_rootId
, name
, 0, -1, dir_item
);
529 m_treeCtrl
->SetItemImage( id
, 1, wxTreeItemIcon_Expanded
);
531 // TODO: other operating systems.
533 m_treeCtrl
->SetItemHasChildren(id
);
536 void wxGenericDirCtrl::SetupSections()
538 #if defined(__WXMSW__) || defined(__WXPM__)
541 wxChar driveBuffer
[256];
542 size_t n
= (size_t) GetLogicalDriveStrings(255, driveBuffer
);
547 path
.Printf(wxT("%c:\\"), driveBuffer
[i
]);
548 name
.Printf(wxT("(%c:)"), driveBuffer
[i
]);
551 int driveType
= ::GetDriveType(path
);
554 case DRIVE_REMOVABLE
:
555 if (path
== wxT("a:\\") || path
== wxT("b:\\"))
556 imageId
= 6; // Floppy
574 AddSection(path
, name
, imageId
);
576 while (driveBuffer
[i
] != wxT('\0'))
579 if (driveBuffer
[i
] == wxT('\0'))
586 /* If we can switch to the drive, it exists. */
587 for( drive
= 1; drive
<= 26; drive
++ )
590 path
.Printf(wxT("%c:\\"), (char) (drive
+ 'a' - 1));
591 name
.Printf(wxT("(%c:)"), (char) (drive
+ 'a' - 1));
593 if (wxIsDriveAvailable(path
))
596 AddSection(path
, name
);
600 #elif defined(__WXMAC__) && !defined(__UNIX__)
604 short actualCount
= 0 ;
605 if ( OnLine( &volume
, 1 , &actualCount
, &index
) != noErr
|| actualCount
== 0 )
608 wxString name
= wxMacFSSpec2MacFilename( &volume
) ;
609 AddSection(name
+":", name
, 0);
612 AddSection(wxT("/"), _("The Computer"), 0);
613 AddSection(wxGetHomeDir(), _("My Home"), 0 );
614 AddSection(wxT("/mnt"), _("Mounted Devices"), 0 );
615 AddSection(wxT("/usr/local"), _("User Local"), 0 );
616 AddSection(wxT("/usr"), _("User"), 0 );
617 AddSection(wxT("/var"), _("Variables"), 0 );
618 AddSection(wxT("/etc"), _("Etcetera"), 0 );
619 AddSection(wxT("/tmp"), _("Temporary"), 0 );
623 void wxGenericDirCtrl::OnBeginEditItem(wxTreeEvent
&event
)
625 // don't rename the main entry "Sections"
626 if (event
.GetItem() == m_rootId
)
632 // don't rename the individual sections
633 if (m_treeCtrl
->GetParent( event
.GetItem() ) == m_rootId
)
640 void wxGenericDirCtrl::OnEndEditItem(wxTreeEvent
&event
)
642 if ((event
.GetLabel().IsEmpty()) ||
643 (event
.GetLabel() == _(".")) ||
644 (event
.GetLabel() == _("..")) ||
645 (event
.GetLabel().First( wxT("/") ) != wxNOT_FOUND
))
647 wxMessageDialog
dialog(this, _("Illegal directory name."), _("Error"), wxOK
| wxICON_ERROR
);
653 wxTreeItemId id
= event
.GetItem();
654 wxDirItemDataEx
*data
= (wxDirItemDataEx
*)m_treeCtrl
->GetItemData( id
);
657 wxString
new_name( wxPathOnly( data
->m_path
) );
658 new_name
+= wxString(wxFILE_SEP_PATH
);
659 new_name
+= event
.GetLabel();
663 if (wxFileExists(new_name
))
665 wxMessageDialog
dialog(this, _("File name exists already."), _("Error"), wxOK
| wxICON_ERROR
);
670 if (wxRenameFile(data
->m_path
,new_name
))
672 data
->SetNewDirName( new_name
);
676 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
682 void wxGenericDirCtrl::OnExpandItem(wxTreeEvent
&event
)
684 wxTreeItemId parentId
= event
.GetItem();
689 void wxGenericDirCtrl::OnCollapseItem(wxTreeEvent
&event
)
691 wxTreeItemId child
, parent
= event
.GetItem();
693 wxDirItemDataEx
*data
= (wxDirItemDataEx
*) m_treeCtrl
->GetItemData(event
.GetItem());
694 if (!data
->m_isExpanded
)
697 data
->m_isExpanded
= FALSE
;
699 /* Workaround because DeleteChildren has disapeared (why?) and
700 * CollapseAndReset doesn't work as advertised (deletes parent too) */
701 child
= m_treeCtrl
->GetFirstChild(parent
, cookie
);
704 m_treeCtrl
->Delete(child
);
705 /* Not GetNextChild below, because the cookie mechanism can't
706 * handle disappearing children! */
707 child
= m_treeCtrl
->GetFirstChild(parent
, cookie
);
711 void wxGenericDirCtrl::ExpandDir(wxTreeItemId parentId
)
713 wxDirItemDataEx
*data
= (wxDirItemDataEx
*) m_treeCtrl
->GetItemData(parentId
);
715 if (data
->m_isExpanded
)
718 data
->m_isExpanded
= TRUE
;
720 if (parentId
== m_rootId
)
728 wxString search
,path
,filename
;
730 wxString
dirName(data
->m_path
);
732 #if defined(__WXMSW__) || defined(__WXPM__)
733 // Check if this is a root directory and if so,
734 // whether the drive is avaiable.
735 if (!wxIsDriveAvailable(dirName
))
737 data
->m_isExpanded
= FALSE
;
738 //wxMessageBox(wxT("Sorry, this drive is not available."));
743 // This may take a longish time. Go to busy cursor
746 #if defined(__WXMSW__) || defined(__WXPM__)
747 if (dirName
.Last() == ':')
748 dirName
+= wxString(wxFILE_SEP_PATH
);
752 wxArrayString filenames
;
755 wxString eachFilename
;
762 if (d
.GetFirst(& eachFilename
, wxEmptyString
, wxDIR_DIRS
))
766 if ((eachFilename
!= wxT(".")) && (eachFilename
!= wxT("..")))
768 dirs
.Add(eachFilename
);
771 while (d
.GetNext(& eachFilename
)) ;
774 dirs
.Sort((wxArrayString::CompareFunction
) wxDirCtrlStringCompareFunction
);
776 // Now do the filenames -- but only if we're allowed to
777 if ((GetWindowStyle() & wxDIRCTRL_DIR_ONLY
) == 0)
785 if (d
.GetFirst(& eachFilename
, m_currentFilterStr
, wxDIR_FILES
))
789 if ((eachFilename
!= wxT(".")) && (eachFilename
!= wxT("..")))
791 filenames
.Add(eachFilename
);
794 while (d
.GetNext(& eachFilename
)) ;
797 filenames
.Sort((wxArrayString::CompareFunction
) wxDirCtrlStringCompareFunction
);
800 // Add the sorted dirs
802 for (i
= 0; i
< dirs
.Count(); i
++)
804 wxString
eachFilename(dirs
[i
]);
806 if (path
.Last() != wxFILE_SEP_PATH
)
807 path
+= wxString(wxFILE_SEP_PATH
);
808 path
+= eachFilename
;
810 wxDirItemDataEx
*dir_item
= new wxDirItemDataEx(path
,eachFilename
,TRUE
);
811 wxTreeItemId id
= m_treeCtrl
->AppendItem( parentId
, eachFilename
, 0, -1, dir_item
);
812 m_treeCtrl
->SetItemImage( id
, 1, wxTreeItemIcon_Expanded
);
814 // Has this got any children? If so, make it expandable.
815 int options
= wxDIR_DEFAULT
;
816 if (GetWindowStyle() & wxDIRCTRL_DIR_ONLY
) // If only showing dirs, then we specify dirs only here
818 options
= wxDIR_DIRS
;
826 // Have to test for wxDIR_DIRS separately in case m_currentFilterStr is non-empty and
827 // and filters out any directories
828 if (dir2
.GetFirst(& str
, m_currentFilterStr
, options
) || dir2
.GetFirst(& str
, wxEmptyString
, wxDIR_DIRS
))
830 m_treeCtrl
->SetItemHasChildren(id
);
835 // Add the sorted filenames
836 if ((GetWindowStyle() & wxDIRCTRL_DIR_ONLY
) == 0)
838 for (i
= 0; i
< filenames
.Count(); i
++)
840 wxString
eachFilename(filenames
[i
]);
842 if (path
.Last() != wxFILE_SEP_PATH
)
843 path
+= wxString(wxFILE_SEP_PATH
);
844 path
+= eachFilename
;
845 //path = dirName + wxString(wxT("/")) + eachFilename;
846 wxDirItemDataEx
*dir_item
= new wxDirItemDataEx(path
,eachFilename
,FALSE
);
847 (void)m_treeCtrl
->AppendItem( parentId
, eachFilename
, 2, -1, dir_item
);
852 // Find the child that matches the first part of 'path'.
853 // E.g. if a child path is "/usr" and 'path' is "/usr/include"
854 // then the child for /usr is returned.
855 wxTreeItemId
wxGenericDirCtrl::FindChild(wxTreeItemId parentId
, const wxString
& path
, bool& done
)
857 wxString
path2(path
);
859 // Make sure all separators are as per the current platform
860 path2
.Replace(wxT("\\"), wxString(wxFILE_SEP_PATH
));
861 path2
.Replace(wxT("/"), wxString(wxFILE_SEP_PATH
));
863 // Append a separator to foil bogus substring matching
864 path2
+= wxString(wxFILE_SEP_PATH
);
866 // In MSW or PM, case is not significant
867 #if defined(__WXMSW__) || defined(__WXPM__)
872 wxTreeItemId childId
= m_treeCtrl
->GetFirstChild(parentId
, cookie
);
873 while (childId
.IsOk())
875 wxDirItemDataEx
* data
= (wxDirItemDataEx
*) m_treeCtrl
->GetItemData(childId
);
877 if (data
&& data
->m_path
!= "")
879 wxString
childPath(data
->m_path
);
880 if (childPath
.Last() != wxFILE_SEP_PATH
)
881 childPath
+= wxString(wxFILE_SEP_PATH
);
883 // In MSW and PM, case is not significant
884 #if defined(__WXMSW__) || defined(__WXPM__)
885 childPath
.MakeLower();
888 if (childPath
.Len() <= path2
.Len())
890 wxString path3
= path2
.Mid(0, childPath
.Len());
891 if (childPath
== path3
)
893 if (path3
.Len() == path2
.Len())
902 childId
= m_treeCtrl
->GetNextChild(childId
, cookie
);
904 wxTreeItemId invalid
;
908 // Try to expand as much of the given path as possible,
909 // and select the given tree item.
910 bool wxGenericDirCtrl::ExpandPath(const wxString
& path
)
913 wxTreeItemId id
= FindChild(m_rootId
, path
, done
);
914 wxTreeItemId lastId
= id
; // The last non-zero id
915 while (id
.IsOk() && !done
)
919 id
= FindChild(id
, path
, done
);
925 wxDirItemDataEx
*data
= (wxDirItemDataEx
*) m_treeCtrl
->GetItemData(lastId
);
928 m_treeCtrl
->Expand(lastId
);
930 if ((GetWindowStyle() & wxDIRCTRL_SELECT_FIRST
) && data
->m_isDir
)
932 // Find the first file in this directory
934 wxTreeItemId childId
= m_treeCtrl
->GetFirstChild(lastId
, cookie
);
935 bool selectedChild
= FALSE
;
936 while (childId
.IsOk())
938 wxDirItemDataEx
* data
= (wxDirItemDataEx
*) m_treeCtrl
->GetItemData(childId
);
940 if (data
&& data
->m_path
!= "" && !data
->m_isDir
)
942 m_treeCtrl
->SelectItem(childId
);
943 m_treeCtrl
->EnsureVisible(childId
);
944 selectedChild
= TRUE
;
947 childId
= m_treeCtrl
->GetNextChild(lastId
, cookie
);
951 m_treeCtrl
->SelectItem(lastId
);
952 m_treeCtrl
->EnsureVisible(lastId
);
957 m_treeCtrl
->SelectItem(lastId
);
958 m_treeCtrl
->EnsureVisible(lastId
);
967 wxString
wxGenericDirCtrl::GetPath() const
969 wxTreeItemId id
= m_treeCtrl
->GetSelection();
972 wxDirItemDataEx
* data
= (wxDirItemDataEx
*) m_treeCtrl
->GetItemData(id
);
976 return wxEmptyString
;
979 wxString
wxGenericDirCtrl::GetFilePath() const
981 wxTreeItemId id
= m_treeCtrl
->GetSelection();
984 wxDirItemDataEx
* data
= (wxDirItemDataEx
*) m_treeCtrl
->GetItemData(id
);
986 return wxEmptyString
;
991 return wxEmptyString
;
994 void wxGenericDirCtrl::SetPath(const wxString
& path
)
996 m_defaultPath
= path
;
1003 void wxGenericDirCtrl::FindChildFiles(wxTreeItemId id
, int dirFlags
, wxArrayString
& filenames
)
1005 wxDirItemDataEx
*data
= (wxDirItemDataEx
*) m_treeCtrl
->GetItemData(id
);
1007 // This may take a longish time. Go to busy cursor
1012 wxString search
,path
,filename
;
1014 wxString
dirName(data
->m_path
);
1016 #if defined(__WXMSW__) || defined(__WXPM__)
1017 if (dirName
.Last() == ':')
1018 dirName
+= wxString(wxFILE_SEP_PATH
);
1022 wxString eachFilename
;
1029 if (d
.GetFirst(& eachFilename
, m_currentFilterStr
, dirFlags
))
1033 if ((eachFilename
!= wxT(".")) && (eachFilename
!= wxT("..")))
1035 filenames
.Add(eachFilename
);
1038 while (d
.GetNext(& eachFilename
)) ;
1044 void wxGenericDirCtrl::SetFilterIndex(int n
)
1046 m_currentFilter
= n
;
1049 if (ExtractWildcard(m_filter
, n
, f
, d
))
1050 m_currentFilterStr
= f
;
1052 m_currentFilterStr
= wxT("*.*");
1055 void wxGenericDirCtrl::SetFilter(const wxString
& filter
)
1060 if (ExtractWildcard(m_filter
, m_currentFilter
, f
, d
))
1061 m_currentFilterStr
= f
;
1063 m_currentFilterStr
= wxT("*.*");
1066 // Extract description and actual filter from overall filter string
1067 bool wxGenericDirCtrl::ExtractWildcard(const wxString
& filterStr
, int n
, wxString
& filter
, wxString
& description
)
1069 wxArrayString filters
, descriptions
;
1070 int count
= ParseFilter(filterStr
, filters
, descriptions
);
1071 if (count
> 0 && n
< count
)
1073 filter
= filters
[n
];
1074 description
= descriptions
[n
];
1081 // Parses the global filter, returning the number of filters.
1082 // Returns 0 if none or if there's a problem.
1083 // filterStr is in the form:
1085 // "All files (*.*)|*.*|JPEG Files (*.jpeg)|*.jpg"
1087 int wxGenericDirCtrl::ParseFilter(const wxString
& filterStr
, wxArrayString
& filters
, wxArrayString
& descriptions
)
1089 wxString
str(filterStr
);
1091 wxString description
, filter
;
1093 bool finished
= FALSE
;
1096 pos
= str
.Find(wxT('|'));
1098 return 0; // Problem
1099 description
= str
.Left(pos
);
1100 str
= str
.Mid(pos
+1);
1101 pos
= str
.Find(wxT('|'));
1109 filter
= str
.Left(pos
);
1110 str
= str
.Mid(pos
+1);
1112 descriptions
.Add(description
);
1113 filters
.Add(filter
);
1117 return filters
.Count();
1120 void wxGenericDirCtrl::DoResize()
1122 wxSize sz
= GetClientSize();
1123 int verticalSpacing
= 3;
1127 if (m_filterListCtrl
)
1129 filterSz
= m_filterListCtrl
->GetSize();
1130 sz
.y
-= (filterSz
.y
+ verticalSpacing
);
1132 m_treeCtrl
->SetSize(0, 0, sz
.x
, sz
.y
);
1133 if (m_filterListCtrl
)
1135 m_filterListCtrl
->SetSize(0, sz
.y
+ verticalSpacing
, sz
.x
, filterSz
.y
);
1136 // Don't know why, but this needs refreshing after a resize (wxMSW)
1137 m_filterListCtrl
->Refresh();
1143 void wxGenericDirCtrl::OnSize(wxSizeEvent
& WXUNUSED(event
))
1148 //-----------------------------------------------------------------------------
1149 // wxDirFilterListCtrl
1150 //-----------------------------------------------------------------------------
1152 IMPLEMENT_CLASS(wxDirFilterListCtrl
, wxChoice
)
1154 BEGIN_EVENT_TABLE(wxDirFilterListCtrl
, wxChoice
)
1155 EVT_CHOICE(-1, wxDirFilterListCtrl::OnSelFilter
)
1158 bool wxDirFilterListCtrl::Create(wxGenericDirCtrl
* parent
, const wxWindowID id
,
1164 return wxChoice::Create(parent
, id
, pos
, size
, 0, NULL
, style
);
1167 void wxDirFilterListCtrl::Init()
1172 void wxDirFilterListCtrl::OnSelFilter(wxCommandEvent
& WXUNUSED(event
))
1174 int sel
= GetSelection();
1176 wxString currentPath
= m_dirCtrl
->GetPath();
1178 m_dirCtrl
->SetFilterIndex(sel
);
1180 // If the filter has changed, the view is out of date, so
1181 // collapse the tree.
1182 m_dirCtrl
->GetTreeCtrl()->Collapse(m_dirCtrl
->GetRootId());
1183 m_dirCtrl
->GetTreeCtrl()->Expand(m_dirCtrl
->GetRootId());
1185 // Try to restore the selection, or at least the directory
1186 m_dirCtrl
->ExpandPath(currentPath
);
1189 void wxDirFilterListCtrl::FillFilterList(const wxString
& filter
, int defaultFilter
)
1192 wxArrayString descriptions
, filters
;
1193 size_t n
= (size_t) m_dirCtrl
->ParseFilter(filter
, filters
, descriptions
);
1195 if (n
> 0 && defaultFilter
< (int) n
)
1198 for (i
= 0; i
< n
; i
++)
1199 Append(descriptions
[i
]);
1200 SetSelection(defaultFilter
);
1204 // wxGenericDirDialog implementation
1205 // This should be moved into dirdlgg.cpp eventually
1207 BEGIN_EVENT_TABLE(wxGenericDirDialog
, wxDialog
)
1208 EVT_BUTTON(wxID_OK
, wxGenericDirDialog::OnOK
)
1209 EVT_BUTTON(wxID_NEW
, wxGenericDirDialog::OnNew
)
1210 EVT_BUTTON (wxID_NEW
, wxGenericDirDialog::OnNew
)
1211 EVT_CLOSE(wxGenericDirDialog::OnCloseWindow
)
1212 EVT_TREE_KEY_DOWN (-1, wxGenericDirDialog::OnTreeKeyDown
)
1213 EVT_TREE_SEL_CHANGED (-1, wxGenericDirDialog::OnTreeSelected
)
1214 EVT_TEXT_ENTER (ID_TEXTCTRL
, wxGenericDirDialog::OnOK
)
1217 wxGenericDirDialog::wxGenericDirDialog(wxWindow
* parent
, const wxString
& title
,
1218 const wxString
& defaultPath
, long style
, const wxPoint
& pos
, const wxSize
& sz
, const wxString
& name
):
1219 wxDialog(parent
, ID_DIRCTRL
, title
, pos
, sz
, style
, name
)
1222 m_path
= defaultPath
;
1224 wxBusyCursor cursor
;
1226 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
1229 m_dirCtrl
= new wxGenericDirCtrl(this, ID_DIRCTRL
,
1230 defaultPath
, wxPoint(5, 5),
1231 wxSize(300, 200), wxDIRCTRL_DIR_ONLY
|wxSUNKEN_BORDER
);
1233 topsizer
->Add( m_dirCtrl
, 1, wxTOP
|wxLEFT
|wxRIGHT
| wxEXPAND
, 10 );
1236 m_input
= new wxTextCtrl( this, ID_TEXTCTRL
, m_path
, wxDefaultPosition
);
1237 topsizer
->Add( m_input
, 0, wxTOP
|wxLEFT
|wxRIGHT
| wxEXPAND
, 10 );
1241 topsizer
->Add( new wxStaticLine( this, -1 ), 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
1245 wxSizer
* buttonsizer
= new wxBoxSizer( wxHORIZONTAL
);
1246 wxButton
* okButton
= new wxButton(this, wxID_OK
, _("OK"));
1247 buttonsizer
->Add( okButton
, 0, wxLEFT
|wxRIGHT
, 10 );
1248 wxButton
* cancelButton
= new wxButton(this, wxID_CANCEL
, _("Cancel"));
1249 buttonsizer
->Add( cancelButton
, 0, wxLEFT
|wxRIGHT
, 10 );
1251 // I'm not convinced we need a New button, and we tend to get annoying
1252 // accidental-editing with label editing enabled.
1254 wxButton
* newButton
= new wxButton( this, wxID_NEW
, _("New...") );
1255 buttonsizer
->Add( newButton
, 0, wxLEFT
|wxRIGHT
, 10 );
1258 topsizer
->Add( buttonsizer
, 0, wxALL
| wxCENTER
, 10 );
1260 okButton
->SetDefault();
1261 m_dirCtrl
->SetFocus();
1263 SetAutoLayout( TRUE
);
1264 SetSizer( topsizer
);
1266 topsizer
->SetSizeHints( this );
1267 topsizer
->Fit( this );
1272 void wxGenericDirDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
1274 EndModal(wxID_CANCEL
);
1277 void wxGenericDirDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
1279 m_path
= m_input
->GetValue();
1280 // Does the path exist? (User may have typed anything in m_input)
1281 if (wxPathExists(m_path
)) {
1282 // OK, path exists, we're done.
1286 // Interact with user, find out if the dir is a typo or to be created
1287 wxString
msg( _("The directory ") );
1289 msg
= msg
+ _("\ndoes not exist\nCreate it now?") ;
1290 wxMessageDialog
dialog(this, msg
, _("Directory does not exist"), wxYES_NO
| wxICON_WARNING
);
1291 if ( dialog
.ShowModal() == wxID_YES
) {
1292 // Okay, let's make it
1294 if (wxMkdir(m_path
)) {
1295 // The new dir was created okay.
1301 msg
= _("Failed to create directory ")+m_path
+
1302 _("\n(Do you have the required permissions?)");
1303 wxMessageDialog
errmsg(this, msg
, _("Error creating directory"), wxOK
| wxICON_ERROR
);
1305 // We still don't have a valid dir. Back to the main dialog.
1308 // User has answered NO to create dir.
1311 void wxGenericDirDialog::SetPath(const wxString
& path
)
1313 m_dirCtrl
->SetPath(path
);
1317 wxString
wxGenericDirDialog::GetPath(void) const
1322 int wxGenericDirDialog::ShowModal()
1324 //m_input->SetValue( m_path );
1325 return wxDialog::ShowModal();
1328 void wxGenericDirDialog::OnTreeSelected( wxTreeEvent
&event
)
1333 wxDirItemDataEx
*data
= (wxDirItemDataEx
*)m_dirCtrl
->GetTreeCtrl()->GetItemData(event
.GetItem());
1335 m_input
->SetValue( data
->m_path
);
1338 void wxGenericDirDialog::OnTreeKeyDown( wxTreeEvent
&WXUNUSED(event
) )
1343 wxDirItemDataEx
*data
= (wxDirItemDataEx
*)m_dirCtrl
->GetTreeCtrl()->GetItemData(m_dirCtrl
->GetTreeCtrl()->GetSelection());
1345 m_input
->SetValue( data
->m_path
);
1348 void wxGenericDirDialog::OnNew( wxCommandEvent
& WXUNUSED(event
) )
1350 wxTreeItemId id
= m_dirCtrl
->GetTreeCtrl()->GetSelection();
1351 if ((id
== m_dirCtrl
->GetTreeCtrl()->GetRootItem()) ||
1352 (m_dirCtrl
->GetTreeCtrl()->GetParent(id
) == m_dirCtrl
->GetTreeCtrl()->GetRootItem()))
1354 wxMessageDialog
msg(this, _("You cannot add a new directory to this section."),
1355 _("Create directory"), wxOK
| wxICON_INFORMATION
);
1360 wxTreeItemId parent
= id
; // m_dirCtrl->GetTreeCtrl()->GetParent( id );
1361 wxDirItemDataEx
*data
= (wxDirItemDataEx
*)m_dirCtrl
->GetTreeCtrl()->GetItemData( parent
);
1364 wxString
new_name( wxT("NewName") );
1365 wxString
path( data
->m_path
);
1366 if (path
.Last() != wxFILE_SEP_PATH
)
1367 path
+= wxFILE_SEP_PATH
;
1369 if (wxFileExists(path
))
1371 // try NewName0, NewName1 etc.
1374 new_name
= wxT("NewName");
1376 num
.Printf( wxT("%d"), i
);
1379 path
= data
->m_path
;
1380 if (path
.Last() != wxFILE_SEP_PATH
)
1381 path
+= wxFILE_SEP_PATH
;
1384 } while (wxFileExists(path
));
1390 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
1395 wxDirItemDataEx
*new_data
= new wxDirItemDataEx( path
, new_name
, TRUE
);
1397 // TODO: THIS CODE DOESN'T WORK YET. We need to avoid duplication of the first child
1399 wxTreeItemId new_id
= m_dirCtrl
->GetTreeCtrl()->AppendItem( parent
, new_name
, 0, 0, new_data
);
1400 m_dirCtrl
->GetTreeCtrl()->EnsureVisible( new_id
);
1401 m_dirCtrl
->GetTreeCtrl()->EditLabel( new_id
);
1404 #endif // wxUSE_DIRDLG