From: Vadim Zeitlin Date: Sun, 2 May 2010 12:01:30 +0000 (+0000) Subject: Add wxTreeCtrl::{Clear,Set}FocusedItem(). X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/5708ae18f2c9b164713918405a6a738ca5538550 Add wxTreeCtrl::{Clear,Set}FocusedItem(). Allow changing just the currently focused (not selected) item and also removing the focus completely. Closes #11599. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@64196 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/docs/changes.txt b/docs/changes.txt index c7ba0b1c8d..8aecf879ba 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -515,6 +515,7 @@ All (GUI): - Added wxTransparentColour. - Added wxToolBar::GetToolByPos(). - Added wxProgressDialog::Was{Cancelled,Skipped}() (Julien Weinzorn). +- Added wxTreeCtrl::{Clear,Set}FocusedItem() (Nikolay Tiushkov). GTK: diff --git a/include/wx/generic/treectlg.h b/include/wx/generic/treectlg.h index 6a1a838e18..7c068dd7f5 100644 --- a/include/wx/generic/treectlg.h +++ b/include/wx/generic/treectlg.h @@ -122,6 +122,9 @@ public: virtual size_t GetSelections(wxArrayTreeItemIds&) const; virtual wxTreeItemId GetFocusedItem() const { return m_current; } + virtual void ClearFocusedItem(); + virtual void SetFocusedItem(const wxTreeItemId& item); + virtual wxTreeItemId GetItemParent(const wxTreeItemId& item) const; virtual wxTreeItemId GetFirstChild(const wxTreeItemId& item, wxTreeItemIdValue& cookie) const; diff --git a/include/wx/msw/treectrl.h b/include/wx/msw/treectrl.h index 25461a87de..b1e8e78492 100644 --- a/include/wx/msw/treectrl.h +++ b/include/wx/msw/treectrl.h @@ -129,6 +129,10 @@ public: virtual size_t GetSelections(wxArrayTreeItemIds& selections) const; virtual wxTreeItemId GetFocusedItem() const; + virtual void ClearFocusedItem(); + virtual void SetFocusedItem(const wxTreeItemId& item); + + virtual wxTreeItemId GetItemParent(const wxTreeItemId& item) const; virtual wxTreeItemId GetFirstChild(const wxTreeItemId& item, wxTreeItemIdValue& cookie) const; @@ -285,9 +289,6 @@ private: // and the tree has wxTR_HIDE_ROOT style) bool IsHiddenRoot(const wxTreeItemId& item) const; - // clears/sets the currently focused item - void ClearFocusedItem(); - void SetFocusedItem(const wxTreeItemId& item); // check if the given flags (taken from TV_HITTESTINFO structure) // indicate a position "on item": this is less trivial than just checking diff --git a/include/wx/treectrl.h b/include/wx/treectrl.h index fe8b7f1fb6..7b2d71c6e2 100644 --- a/include/wx/treectrl.h +++ b/include/wx/treectrl.h @@ -214,6 +214,13 @@ public: // equivalent to GetSelection() if not wxTR_MULTIPLE virtual wxTreeItemId GetFocusedItem() const = 0; + + // Clears the currently focused item + virtual void ClearFocusedItem() = 0; + // Sets the currently focused item. Item should be valid + virtual void SetFocusedItem(const wxTreeItemId& item) = 0; + + // get the parent of this item (may return NULL if root) virtual wxTreeItemId GetItemParent(const wxTreeItemId& item) const = 0; diff --git a/interface/wx/treectrl.h b/interface/wx/treectrl.h index 9b9fb7c141..f11959f743 100644 --- a/interface/wx/treectrl.h +++ b/interface/wx/treectrl.h @@ -437,6 +437,24 @@ public: */ virtual wxTreeItemId GetFocusedItem() const; + + /** + Clears the currently focused item + + @since 2.9.1 + */ + virtual void ClearFocusedItem(); + + /** + Sets the currently focused item. + + @param item + The item to make the current one. It must be valid. + @since 2.9.1 + */ + virtual void SetFocusedItem(const wxTreeItemId& item); + + /** Returns the normal image list. */ diff --git a/samples/treectrl/treetest.cpp b/samples/treectrl/treetest.cpp index 0da06d9ea1..5ac4e9c5cb 100644 --- a/samples/treectrl/treetest.cpp +++ b/samples/treectrl/treetest.cpp @@ -129,6 +129,8 @@ BEGIN_EVENT_TABLE(MyFrame, wxFrame) MENU_LINK(ToggleIcon) MENU_LINK(ToggleState) MENU_LINK(SelectRoot) + MENU_LINK(SetFocusedRoot) + MENU_LINK(ClearFocused) MENU_LINK(ShowFirstVisible) #ifdef wxHAS_LAST_VISIBLE @@ -263,6 +265,9 @@ 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_SetFocusedRoot, wxT("Set focus to root item")); + tree_menu->Append(TreeTest_ClearFocused, wxT("Reset focus")); tree_menu->AppendSeparator(); tree_menu->Append(TreeTest_Count, wxT("Count children of current item")); @@ -592,6 +597,17 @@ void MyFrame::OnSelectRoot(wxCommandEvent& WXUNUSED(event)) m_treeCtrl->SelectItem(m_treeCtrl->GetRootItem()); } +void MyFrame::OnSetFocusedRoot(wxCommandEvent& WXUNUSED(event)) +{ + if ( !m_treeCtrl->HasFlag(wxTR_HIDE_ROOT) ) + m_treeCtrl->SetFocusedItem(m_treeCtrl->GetRootItem()); +} + +void MyFrame::OnClearFocused(wxCommandEvent& WXUNUSED(event)) +{ + m_treeCtrl->ClearFocusedItem(); +} + void MyFrame::OnUnselect(wxCommandEvent& WXUNUSED(event)) { m_treeCtrl->UnselectAll(); diff --git a/samples/treectrl/treetest.h b/samples/treectrl/treetest.h index 50db3d92e3..645e0d0668 100644 --- a/samples/treectrl/treetest.h +++ b/samples/treectrl/treetest.h @@ -212,6 +212,8 @@ public: void OnSelectChildren(wxCommandEvent& event); #endif // NO_MULTIPLE_SELECTION void OnSelectRoot(wxCommandEvent& event); + void OnSetFocusedRoot(wxCommandEvent& event); + void OnClearFocused(wxCommandEvent& event); void OnDelete(wxCommandEvent& event); void OnDeleteChildren(wxCommandEvent& event); void OnDeleteAll(wxCommandEvent& event); @@ -355,6 +357,8 @@ enum TreeTest_Select, TreeTest_Unselect, TreeTest_SelectRoot, + TreeTest_ClearFocused, + TreeTest_SetFocusedRoot, TreeTest_SelectChildren, TreeTest_ShowFirstVisible, TreeTest_ShowLastVisible, diff --git a/src/generic/treectlg.cpp b/src/generic/treectlg.cpp index a393dc0fbb..b88ba3851a 100644 --- a/src/generic/treectlg.cpp +++ b/src/generic/treectlg.cpp @@ -1953,6 +1953,20 @@ void wxGenericTreeCtrl::Unselect() } } +void wxGenericTreeCtrl::ClearFocusedItem() +{ + wxTreeItemId item = GetFocusedItem(); + if ( item.IsOk() ) + SelectItem(item, false); +} + +void wxGenericTreeCtrl::SetFocusedItem(const wxTreeItemId& item) +{ + wxCHECK_RET( item.IsOk(), wxT("invalid tree item") ); + + SelectItem(item, true); +} + void wxGenericTreeCtrl::UnselectAllChildren(wxGenericTreeItem *item) { if (item->IsSelected()) diff --git a/src/msw/treectrl.cpp b/src/msw/treectrl.cpp index 9464bec90b..9e8c7e31d1 100644 --- a/src/msw/treectrl.cpp +++ b/src/msw/treectrl.cpp @@ -2137,6 +2137,8 @@ void wxTreeCtrl::ClearFocusedItem() void wxTreeCtrl::SetFocusedItem(const wxTreeItemId& item) { + wxCHECK_RET( item.IsOk(), wxT("invalid tree item") ); + TempSetter set(m_changingSelection); ::SetFocus(GetHwnd(), HITEM(item));