From 5cb3a695e0f65d71fe0c315766e0d6dafd4c312a Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Tue, 26 Jan 2010 12:43:39 +0000 Subject: [PATCH] Add wxTreeCtrl::SelectChildren() method. Add MSW and generic implementation, documentation and change to the sample showing it. Closes #11620. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@63277 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- docs/changes.txt | 1 + include/wx/generic/treectlg.h | 1 + include/wx/msw/treectrl.h | 2 ++ include/wx/treectrl.h | 5 ++++- interface/wx/treectrl.h | 9 +++++++++ samples/treectrl/treetest.cpp | 12 ++++++++++++ samples/treectrl/treetest.h | 2 ++ src/generic/treectlg.cpp | 36 +++++++++++++++++++++++++++++++++++ src/msw/treectrl.cpp | 33 ++++++++++++++++++++++++++++++++ 9 files changed, 100 insertions(+), 1 deletion(-) diff --git a/docs/changes.txt b/docs/changes.txt index 5803345543..c719399322 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -492,6 +492,7 @@ All (GUI): - wxGraphicsContext is now enabled by default if supported by the platform. - Fix building with using system libpng 1.4 (Volker Grabsch). - Add wxComboBox::Popup() and Dismiss() methods (Igor Korot). +- Added wxTreeCtrl::SelectChildren() (Nikolay Tjushkov). GTK: diff --git a/include/wx/generic/treectlg.h b/include/wx/generic/treectlg.h index 86f2a90519..6a1a838e18 100644 --- a/include/wx/generic/treectlg.h +++ b/include/wx/generic/treectlg.h @@ -155,6 +155,7 @@ public: virtual void Unselect(); virtual void UnselectAll(); virtual void SelectItem(const wxTreeItemId& item, bool select = true); + virtual void SelectChildren(const wxTreeItemId& parent); virtual void EnsureVisible(const wxTreeItemId& item); virtual void ScrollTo(const wxTreeItemId& item); diff --git a/include/wx/msw/treectrl.h b/include/wx/msw/treectrl.h index 4c621980ef..25461a87de 100644 --- a/include/wx/msw/treectrl.h +++ b/include/wx/msw/treectrl.h @@ -162,6 +162,7 @@ public: virtual void Unselect(); virtual void UnselectAll(); virtual void SelectItem(const wxTreeItemId& item, bool select = true); + virtual void SelectChildren(const wxTreeItemId& parent); virtual void EnsureVisible(const wxTreeItemId& item); virtual void ScrollTo(const wxTreeItemId& item); @@ -276,6 +277,7 @@ private: void DoToggleItemSelection(const wxTreeItemId& item); void DoUnselectAll(); + void DoSelectChildren(const wxTreeItemId& parent); void DeleteTextCtrl(); diff --git a/include/wx/treectrl.h b/include/wx/treectrl.h index ee13f4f4a4..fe8b7f1fb6 100644 --- a/include/wx/treectrl.h +++ b/include/wx/treectrl.h @@ -324,6 +324,9 @@ public: virtual void UnselectAll() = 0; // select this item virtual void SelectItem(const wxTreeItemId& item, bool select = true) = 0; + // selects all (direct) children for given parent (only for + // multiselection controls) + virtual void SelectChildren(const wxTreeItemId& parent) = 0; // unselect this item void UnselectItem(const wxTreeItemId& item) { SelectItem(item, false); } // toggle item selection @@ -400,7 +403,7 @@ public: protected: virtual wxSize DoGetBestSize() const; - // comon part of Get/SetItemState() + // common part of Get/SetItemState() virtual int DoGetItemState(const wxTreeItemId& item) const = 0; virtual void DoSetItemState(const wxTreeItemId& item, int state) = 0; diff --git a/interface/wx/treectrl.h b/interface/wx/treectrl.h index f1cdacd529..9b9fb7c141 100644 --- a/interface/wx/treectrl.h +++ b/interface/wx/treectrl.h @@ -954,6 +954,15 @@ public: Unselects the given item. This works in multiselection controls only. */ void UnselectItem(const wxTreeItemId& item); + + /** + Select all the immediate children of the given parent. + + This function can be used with multiselection controls only. + + @since 2.9.1 + */ + virtual void SelectChildren(const wxTreeItemId& parent); }; diff --git a/samples/treectrl/treetest.cpp b/samples/treectrl/treetest.cpp index 41a86c466c..0da06d9ea1 100644 --- a/samples/treectrl/treetest.cpp +++ b/samples/treectrl/treetest.cpp @@ -98,6 +98,7 @@ BEGIN_EVENT_TABLE(MyFrame, wxFrame) MENU_LINK(Select) MENU_LINK(Unselect) MENU_LINK(ToggleSel) + MENU_LINK(SelectChildren) #endif // NO_MULTIPLE_SELECTION MENU_LINK(Rename) MENU_LINK(Count) @@ -262,6 +263,7 @@ MyFrame::MyFrame(const wxString& title, int x, int y, int w, int h) tree_menu->Append(TreeTest_DeleteChildren, wxT("Delete &children")); tree_menu->Append(TreeTest_DeleteAll, wxT("Delete &all items")); tree_menu->Append(TreeTest_SelectRoot, wxT("Select root item")); + tree_menu->AppendSeparator(); tree_menu->Append(TreeTest_Count, wxT("Count children of current item")); tree_menu->Append(TreeTest_CountRec, wxT("Recursively count children of current item")); @@ -307,6 +309,7 @@ MyFrame::MyFrame(const wxString& title, int x, int y, int w, int h) item_menu->Append(TreeTest_DumpSelected, wxT("Dump selected items\tAlt-D")); item_menu->Append(TreeTest_Select, wxT("Select current item\tAlt-S")); item_menu->Append(TreeTest_Unselect, wxT("Unselect everything\tAlt-U")); + item_menu->Append(TreeTest_SelectChildren, wxT("Select all children\tCtrl-A")); #endif // NO_MULTIPLE_SELECTION wxMenuBar *menu_bar = new wxMenuBar; @@ -594,6 +597,15 @@ void MyFrame::OnUnselect(wxCommandEvent& WXUNUSED(event)) m_treeCtrl->UnselectAll(); } +void MyFrame::OnSelectChildren(wxCommandEvent& WXUNUSED(event)) +{ + wxTreeItemId item = m_treeCtrl->GetFocusedItem(); + if ( !item.IsOk() ) + item = m_treeCtrl->GetRootItem(); + + m_treeCtrl->SelectChildren(item); +} + #endif // NO_MULTIPLE_SELECTION void MyFrame::DoSetBold(bool bold) diff --git a/samples/treectrl/treetest.h b/samples/treectrl/treetest.h index 90bbebb0da..50db3d92e3 100644 --- a/samples/treectrl/treetest.h +++ b/samples/treectrl/treetest.h @@ -209,6 +209,7 @@ public: void OnSelect(wxCommandEvent& event); void OnUnselect(wxCommandEvent& event); void OnToggleSel(wxCommandEvent& event); + void OnSelectChildren(wxCommandEvent& event); #endif // NO_MULTIPLE_SELECTION void OnSelectRoot(wxCommandEvent& event); void OnDelete(wxCommandEvent& event); @@ -354,6 +355,7 @@ enum TreeTest_Select, TreeTest_Unselect, TreeTest_SelectRoot, + TreeTest_SelectChildren, TreeTest_ShowFirstVisible, TreeTest_ShowLastVisible, TreeTest_ShowNextVisible, diff --git a/src/generic/treectlg.cpp b/src/generic/treectlg.cpp index 7f0dc649c6..da93936fdf 100644 --- a/src/generic/treectlg.cpp +++ b/src/generic/treectlg.cpp @@ -1983,6 +1983,42 @@ void wxGenericTreeCtrl::UnselectAll() } } +void wxGenericTreeCtrl::SelectChildren(const wxTreeItemId& parent) +{ + wxCHECK_RET( HasFlag(wxTR_MULTIPLE), + "this only works with multiple selection controls" ); + + UnselectAll(); + + if ( !HasChildren(parent) ) + return; + + + wxArrayGenericTreeItems& + children = ((wxGenericTreeItem*) parent.m_pItem)->GetChildren(); + size_t count = children.GetCount(); + + wxGenericTreeItem * + item = (wxGenericTreeItem*) ((wxTreeItemId)children[0]).m_pItem; + wxTreeEvent event(wxEVT_COMMAND_TREE_SEL_CHANGING, this, item); + event.m_itemOld = m_current; + + if ( GetEventHandler()->ProcessEvent( event ) && !event.IsAllowed() ) + return; + + for ( size_t n = 0; n < count; ++n ) + { + m_current = m_key_current = children[n]; + m_current->SetHilight(true); + RefreshSelected(); + } + + + event.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED); + GetEventHandler()->ProcessEvent( event ); +} + + // Recursive function ! // To stop we must have crt_item