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
This commit is contained in:
Vadim Zeitlin 2010-01-26 12:43:39 +00:00
parent 6b2f55531a
commit 5cb3a695e0
9 changed files with 100 additions and 1 deletions

View File

@ -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:

View File

@ -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);

View File

@ -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();

View File

@ -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;

View File

@ -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);
};

View File

@ -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)

View File

@ -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,

View File

@ -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<last_item
// Algorithm :

View File

@ -1883,6 +1883,39 @@ void wxTreeCtrl::UnselectAll()
}
}
void wxTreeCtrl::DoSelectChildren(const wxTreeItemId& parent)
{
DoUnselectAll();
wxTreeItemIdValue cookie;
wxTreeItemId child = GetFirstChild(parent, cookie);
while ( child.IsOk() )
{
DoSelectItem(child, true);
child = GetNextChild(child, cookie);
}
}
void wxTreeCtrl::SelectChildren(const wxTreeItemId& parent)
{
wxCHECK_RET( HasFlag(wxTR_MULTIPLE),
"this only works with multiple selection controls" );
HTREEITEM htFocus = (HTREEITEM)TreeView_GetSelection(GetHwnd());
wxTreeEvent changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING, this);
changingEvent.m_itemOld = htFocus;
if ( IsTreeEventAllowed(changingEvent) )
{
DoSelectChildren(parent);
wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED, this);
changedEvent.m_itemOld = htFocus;
(void)HandleTreeEvent(changedEvent);
}
}
void wxTreeCtrl::DoSelectItem(const wxTreeItemId& item, bool select)
{
TempSetter set(m_changingSelection);