From: Vadim Zeitlin Date: Sat, 6 Jun 2009 14:00:51 +0000 (+0000) Subject: added multiple selections support to wxDirCtrl (closes #10830) X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/80f624ec0c99878e327bfd070d6a92adf48eac14 added multiple selections support to wxDirCtrl (closes #10830) git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@60909 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/include/wx/generic/dirctrlg.h b/include/wx/generic/dirctrlg.h index 7c0640be7d..b2ff47f63c 100644 --- a/include/wx/generic/dirctrlg.h +++ b/include/wx/generic/dirctrlg.h @@ -51,7 +51,9 @@ enum // Use 3D borders on internal controls wxDIRCTRL_3D_INTERNAL = 0x0080, // Editable labels - wxDIRCTRL_EDIT_LABELS = 0x0100 + wxDIRCTRL_EDIT_LABELS = 0x0100, + // Allow multiple selection + wxDIRCTRL_MULTIPLE = 0x0200 }; //----------------------------------------------------------------------------- @@ -128,12 +130,17 @@ public: // Get dir or filename virtual wxString GetPath() const; + virtual void GetPaths(wxArrayString& paths) const; // Get selected filename path only (else empty string). // I.e. don't count a directory as a selection virtual wxString GetFilePath() const; + virtual void GetFilePaths(wxArrayString& paths) const; virtual void SetPath(const wxString& path); + virtual void SelectPath(const wxString& path, bool select = true); + virtual void SelectPaths(const wxArrayString& paths); + virtual void ShowHidden( bool show ); virtual bool GetShowHidden() { return m_showHidden; } @@ -148,6 +155,8 @@ public: virtual wxTreeCtrl* GetTreeCtrl() const { return m_treeCtrl; } virtual wxDirFilterListCtrl* GetFilterListCtrl() const { return m_filterListCtrl; } + virtual void UnselectAll(); + // Helper virtual void SetupSections(); @@ -166,7 +175,6 @@ public: // Collapse the entire tree virtual void CollapseTree(); - // overridden base class methods virtual void SetFocus(); diff --git a/interface/wx/dirctrl.h b/interface/wx/dirctrl.h index 4769c92772..fa10290f49 100644 --- a/interface/wx/dirctrl.h +++ b/interface/wx/dirctrl.h @@ -25,6 +25,8 @@ directory. @style{wxDIRCTRL_EDIT_LABELS} Allow the folder and file labels to be editable. + @style{wxDIRCTRL_MULTIPLE} + Allows multiple files and folders to be selected. @endStyleTable @library{wxbase} @@ -118,6 +120,13 @@ public: */ virtual wxString GetFilePath() const; + /** + Fills the array @a paths with the currently selected filepaths. + + This function doesn't count a directory as a selection. + */ + virtual void GetFilePaths(wxArrayString& paths) const; + /** Returns the filter string. */ @@ -138,6 +147,11 @@ public: */ virtual wxString GetPath() const; + /** + Fills the array @a paths with the selected directories and filenames. + */ + virtual void GetPaths(wxArrayString& paths) const; + /** Returns the root id for the tree control. */ @@ -185,5 +199,27 @@ public: control. If @false, they will not be displayed. */ virtual void ShowHidden(bool show); + + /** + Selects the given item. + + In multiple selection controls, can be also used to deselect a + currently selected item if the value of @a select is false. + Existing selections are not changed. Only visible items can be + (de)selected, otherwise use ExpandPath(). + */ + virtual void SelectPath(const wxString& path, bool select = true); + + /** + Selects only the specified paths, clearing any previous selection. + + Only supported when wxDIRCTRL_MULTIPLE is set. + */ + virtual void SelectPaths(const wxArrayString& paths); + + /** + Removes the selection from all currently selected items. + */ + virtual void UnselectAll(); }; diff --git a/samples/widgets/dirctrl.cpp b/samples/widgets/dirctrl.cpp index 654e256197..1d8ad59cbd 100644 --- a/samples/widgets/dirctrl.cpp +++ b/samples/widgets/dirctrl.cpp @@ -133,7 +133,9 @@ protected: wxCheckBox *m_chkDirOnly, *m_chk3D, *m_chkFirst, - *m_chkLabels; + *m_chkLabels, + *m_chkMulti; + // filters wxCheckBox *m_fltr[3]; @@ -185,6 +187,7 @@ void DirCtrlWidgetsPage::CreateContent() m_chk3D = CreateCheckBoxAndAddToSizer(sizerUseFlags, _T("wxDIRCTRL_3D_INTERNAL")); m_chkFirst = CreateCheckBoxAndAddToSizer(sizerUseFlags, _T("wxDIRCTRL_SELECT_FIRST")); m_chkLabels = CreateCheckBoxAndAddToSizer(sizerUseFlags, _T("wxDIRCTRL_EDIT_LABELS")); + m_chkMulti = CreateCheckBoxAndAddToSizer(sizerUseFlags, _T("wxDIRCTRL_MULTIPLE")); sizerLeft->Add(sizerUseFlags, wxSizerFlags().Expand().Border()); wxSizer *sizerFilters = @@ -245,13 +248,14 @@ void DirCtrlWidgetsPage::CreateDirCtrl() ( m_chkDirOnly->IsChecked() ? wxDIRCTRL_DIR_ONLY : 0 ) | ( m_chk3D->IsChecked() ? wxDIRCTRL_3D_INTERNAL : 0 ) | ( m_chkFirst->IsChecked() ? wxDIRCTRL_SELECT_FIRST : 0 ) | - ( m_chkLabels->IsChecked() ? wxDIRCTRL_EDIT_LABELS : 0 ) + ( m_chkLabels->IsChecked() ? wxDIRCTRL_EDIT_LABELS : 0 ) | + ( m_chkMulti->IsChecked() ? wxDIRCTRL_MULTIPLE : 0) ); wxString filter; - for (int i = 0; i < 3; ++i) + for (int i = 0; i < 3; ++i) { - if (m_fltr[i]->IsChecked()) + if (m_fltr[i]->IsChecked()) { if (!filter.IsEmpty()) filter += wxT("|"); diff --git a/src/generic/dirctrlg.cpp b/src/generic/dirctrlg.cpp index b50262b473..9bb119ebea 100644 --- a/src/generic/dirctrlg.cpp +++ b/src/generic/dirctrlg.cpp @@ -472,6 +472,7 @@ wxBEGIN_FLAGS( wxGenericDirCtrlStyle ) wxFLAGS_MEMBER(wxDIRCTRL_DIR_ONLY) wxFLAGS_MEMBER(wxDIRCTRL_3D_INTERNAL) wxFLAGS_MEMBER(wxDIRCTRL_SELECT_FIRST) + wxFLAGS_MEMBER(wxDIRCTRL_MULTIPLE) wxEND_FLAGS( wxGenericDirCtrlStyle ) @@ -560,6 +561,9 @@ bool wxGenericDirCtrl::Create(wxWindow *parent, if (style & wxDIRCTRL_EDIT_LABELS) treeStyle |= wxTR_EDIT_LABELS; + if (style & wxDIRCTRL_MULTIPLE) + treeStyle |= wxTR_MULTIPLE; + if ((style & wxDIRCTRL_3D_INTERNAL) == 0) treeStyle |= wxNO_BORDER; @@ -629,9 +633,22 @@ void wxGenericDirCtrl::ShowHidden( bool show ) m_showHidden = show; - wxString path = GetPath(); - ReCreateTree(); - SetPath(path); + if ( HasFlag(wxDIRCTRL_MULTIPLE) ) + { + wxArrayString paths; + GetPaths(paths); + ReCreateTree(); + for ( unsigned n = 0; n < paths.size(); n++ ) + { + ExpandPath(paths[n]); + } + } + else + { + wxString path = GetPath(); + ReCreateTree(); + SetPath(path); + } } const wxTreeItemId @@ -1095,6 +1112,20 @@ wxString wxGenericDirCtrl::GetPath() const return wxEmptyString; } +void wxGenericDirCtrl::GetPaths(wxArrayString& paths) const +{ + paths.clear(); + + wxArrayTreeItemIds items; + m_treeCtrl->GetSelections(items); + for ( unsigned n = 0; n < items.size(); n++ ) + { + wxTreeItemId id = items[n]; + wxDirItemData* data = (wxDirItemData*) m_treeCtrl->GetItemData(id); + paths.Add(data->m_path); + } +} + wxString wxGenericDirCtrl::GetFilePath() const { wxTreeItemId id = m_treeCtrl->GetSelection(); @@ -1110,6 +1141,21 @@ wxString wxGenericDirCtrl::GetFilePath() const return wxEmptyString; } +void wxGenericDirCtrl::GetFilePaths(wxArrayString& paths) const +{ + paths.clear(); + + wxArrayTreeItemIds items; + m_treeCtrl->GetSelections(items); + for ( unsigned n = 0; n < items.size(); n++ ) + { + wxTreeItemId id = items[n]; + wxDirItemData* data = (wxDirItemData*) m_treeCtrl->GetItemData(id); + if ( !data->m_isDir ) + paths.Add(data->m_path); + } +} + void wxGenericDirCtrl::SetPath(const wxString& path) { m_defaultPath = path; @@ -1117,6 +1163,48 @@ void wxGenericDirCtrl::SetPath(const wxString& path) ExpandPath(path); } +void wxGenericDirCtrl::SelectPath(const wxString& path, bool select) +{ + bool done = false; + wxTreeItemId id = FindChild(m_rootId, path, done); + wxTreeItemId lastId = id; // The last non-zero id + while ( id.IsOk() && !done ) + { + id = FindChild(id, path, done); + if ( id.IsOk() ) + lastId = id; + } + if ( !lastId.IsOk() ) + return; + + if ( done ) + { + if(select && m_treeCtrl->IsSelected(id)) + return; + else + { + m_treeCtrl->SelectItem(id, select); + } + } +} + +void wxGenericDirCtrl::SelectPaths(const wxArrayString& paths) +{ + if ( HasFlag(wxDIRCTRL_MULTIPLE) ) + { + UnselectAll(); + for ( unsigned n = 0; n < paths.size(); n++ ) + { + SelectPath(paths[n]); + } + } +} + +void wxGenericDirCtrl::UnselectAll() +{ + m_treeCtrl->UnselectAll(); +} + // Not used #if 0 void wxGenericDirCtrl::FindChildFiles(wxTreeItemId id, int dirFlags, wxArrayString& filenames) diff --git a/src/xrc/xh_gdctl.cpp b/src/xrc/xh_gdctl.cpp index bbd0a0df59..367ab60489 100644 --- a/src/xrc/xh_gdctl.cpp +++ b/src/xrc/xh_gdctl.cpp @@ -34,6 +34,7 @@ wxGenericDirCtrlXmlHandler::wxGenericDirCtrlXmlHandler() XRC_ADD_STYLE(wxDIRCTRL_3D_INTERNAL); XRC_ADD_STYLE(wxDIRCTRL_SELECT_FIRST); XRC_ADD_STYLE(wxDIRCTRL_EDIT_LABELS); + XRC_ADD_STYLE(wxDIRCTRL_MULTIPLE); AddWindowStyles(); }