1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/treebkg.cpp
3 // Purpose: generic implementation of wxTreebook
4 // Author: Evgeniy Tarassov, Vadim Zeitlin
8 // Copyright: (c) 2005 Vadim Zeitlin <vadim@wxwidgets.org>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
29 #include "wx/treebook.h"
32 #include "wx/settings.h"
35 #include "wx/imaglist.h"
37 // ----------------------------------------------------------------------------
38 // various wxWidgets macros
39 // ----------------------------------------------------------------------------
41 // check that the page index is valid
42 #define IS_VALID_PAGE(nPage) ((nPage) < DoInternalGetPageCount())
44 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
48 IMPLEMENT_DYNAMIC_CLASS(wxTreebook
, wxBookCtrlBase
)
50 wxDEFINE_EVENT( wxEVT_COMMAND_TREEBOOK_PAGE_CHANGING
, wxBookCtrlEvent
);
51 wxDEFINE_EVENT( wxEVT_COMMAND_TREEBOOK_PAGE_CHANGED
, wxBookCtrlEvent
);
52 wxDEFINE_EVENT( wxEVT_COMMAND_TREEBOOK_NODE_COLLAPSED
, wxBookCtrlEvent
);
53 wxDEFINE_EVENT( wxEVT_COMMAND_TREEBOOK_NODE_EXPANDED
, wxBookCtrlEvent
);
55 BEGIN_EVENT_TABLE(wxTreebook
, wxBookCtrlBase
)
56 EVT_TREE_SEL_CHANGED (wxID_ANY
, wxTreebook::OnTreeSelectionChange
)
57 EVT_TREE_ITEM_EXPANDED (wxID_ANY
, wxTreebook::OnTreeNodeExpandedCollapsed
)
58 EVT_TREE_ITEM_COLLAPSED(wxID_ANY
, wxTreebook::OnTreeNodeExpandedCollapsed
)
61 // ============================================================================
62 // wxTreebook implementation
63 // ============================================================================
65 // ----------------------------------------------------------------------------
66 // wxTreebook creation
67 // ----------------------------------------------------------------------------
69 void wxTreebook::Init()
72 m_actualSelection
= wxNOT_FOUND
;
76 wxTreebook::Create(wxWindow
*parent
,
83 // Check the style flag to have either wxTBK_RIGHT or wxTBK_LEFT
84 if ( (style
& wxBK_ALIGN_MASK
) == wxBK_DEFAULT
)
88 style
|= wxTAB_TRAVERSAL
;
90 // no border for this control, it doesn't look nice together with the tree
91 style
&= ~wxBORDER_MASK
;
92 style
|= wxBORDER_NONE
;
94 if ( !wxControl::Create(parent
, id
, pos
, size
,
95 style
, wxDefaultValidator
, name
) )
98 m_bookctrl
= new wxTreeCtrl
109 GetTreeCtrl()->SetQuickBestSize(false); // do full size calculation
110 GetTreeCtrl()->AddRoot(wxEmptyString
); // label doesn't matter, it's hidden
113 // We need to add dummy size event to force possible scrollbar hiding
115 GetEventHandler()->AddPendingEvent(evt
);
122 // insert a new page just before the pagePos
123 bool wxTreebook::InsertPage(size_t pagePos
,
125 const wxString
& text
,
129 return DoInsertPage(pagePos
, page
, text
, bSelect
, imageId
);
132 bool wxTreebook::InsertSubPage(size_t pagePos
,
134 const wxString
& text
,
138 return DoInsertSubPage(pagePos
, page
, text
, bSelect
, imageId
);
141 bool wxTreebook::AddPage(wxWindow
*page
, const wxString
& text
, bool bSelect
,
144 return DoInsertPage(m_treeIds
.GetCount(), page
, text
, bSelect
, imageId
);
147 // insertion time is linear to the number of top-pages
148 bool wxTreebook::AddSubPage(wxWindow
*page
, const wxString
& text
, bool bSelect
, int imageId
)
150 return DoAddSubPage(page
, text
, bSelect
, imageId
);
154 bool wxTreebook::DoInsertPage(size_t pagePos
,
156 const wxString
& text
,
160 wxCHECK_MSG( pagePos
<= DoInternalGetPageCount(), false,
161 wxT("Invalid treebook page position") );
163 if ( !wxBookCtrlBase::InsertPage(pagePos
, page
, text
, bSelect
, imageId
) )
166 wxTreeCtrl
*tree
= GetTreeCtrl();
168 if ( pagePos
== DoInternalGetPageCount() )
170 // append the page to the end
171 wxTreeItemId rootId
= tree
->GetRootItem();
173 newId
= tree
->AppendItem(rootId
, text
, imageId
);
175 else // insert the new page before the given one
177 wxTreeItemId nodeId
= m_treeIds
[pagePos
];
179 wxTreeItemId previousId
= tree
->GetPrevSibling(nodeId
);
180 wxTreeItemId parentId
= tree
->GetItemParent(nodeId
);
182 if ( previousId
.IsOk() )
184 // insert before the sibling - previousId
185 newId
= tree
->InsertItem(parentId
, previousId
, text
, imageId
);
187 else // no prev siblings -- insert as a first child
189 wxASSERT_MSG( parentId
.IsOk(), wxT( "Tree has no root node?" ) );
191 newId
= tree
->PrependItem(parentId
, text
, imageId
);
197 //something wrong -> cleaning and returning with false
198 (void)wxBookCtrlBase::DoRemovePage(pagePos
);
200 wxFAIL_MSG( wxT("Failed to insert treebook page") );
204 DoInternalAddPage(pagePos
, page
, newId
);
206 DoUpdateSelection(bSelect
, pagePos
);
211 bool wxTreebook::DoAddSubPage(wxWindow
*page
, const wxString
& text
, bool bSelect
, int imageId
)
213 wxTreeCtrl
*tree
= GetTreeCtrl();
215 wxTreeItemId rootId
= tree
->GetRootItem();
217 wxTreeItemId lastNodeId
= tree
->GetLastChild(rootId
);
219 wxCHECK_MSG( lastNodeId
.IsOk(), false,
220 wxT("Can't insert sub page when there are no pages") );
222 // now calculate its position (should we save/update it too?)
223 size_t newPos
= tree
->GetCount() -
224 (tree
->GetChildrenCount(lastNodeId
, true) + 1);
226 return DoInsertSubPage(newPos
, page
, text
, bSelect
, imageId
);
229 bool wxTreebook::DoInsertSubPage(size_t pagePos
,
230 wxTreebookPage
*page
,
231 const wxString
& text
,
235 wxTreeItemId parentId
= DoInternalGetPage(pagePos
);
236 wxCHECK_MSG( parentId
.IsOk(), false, wxT("invalid tree item") );
238 wxTreeCtrl
*tree
= GetTreeCtrl();
240 size_t newPos
= pagePos
+ tree
->GetChildrenCount(parentId
, true) + 1;
241 wxASSERT_MSG( newPos
<= DoInternalGetPageCount(),
242 wxT("Internal error in tree insert point calculation") );
244 if ( !wxBookCtrlBase::InsertPage(newPos
, page
, text
, bSelect
, imageId
) )
247 wxTreeItemId newId
= tree
->AppendItem(parentId
, text
, imageId
);
251 (void)wxBookCtrlBase::DoRemovePage(newPos
);
253 wxFAIL_MSG( wxT("Failed to insert treebook page") );
257 DoInternalAddPage(newPos
, page
, newId
);
259 DoUpdateSelection(bSelect
, newPos
);
264 bool wxTreebook::DeletePage(size_t pagePos
)
266 wxCHECK_MSG( IS_VALID_PAGE(pagePos
), false, wxT("Invalid tree index") );
268 wxTreebookPage
*oldPage
= DoRemovePage(pagePos
);
277 wxTreebookPage
*wxTreebook::DoRemovePage(size_t pagePos
)
279 wxTreeItemId pageId
= DoInternalGetPage(pagePos
);
280 wxCHECK_MSG( pageId
.IsOk(), NULL
, wxT("Invalid tree index") );
282 wxTreebookPage
* oldPage
= GetPage(pagePos
);
283 wxTreeCtrl
*tree
= GetTreeCtrl();
285 size_t subCount
= tree
->GetChildrenCount(pageId
, true);
286 wxASSERT_MSG ( IS_VALID_PAGE(pagePos
+ subCount
),
287 wxT("Internal error in wxTreebook::DoRemovePage") );
289 // here we are going to delete ALL the pages in the range
290 // [pagePos, pagePos + subCount] -- the page and its children
292 // deleting all the pages from the base class
293 for ( size_t i
= 0; i
<= subCount
; ++i
)
295 wxTreebookPage
*page
= wxBookCtrlBase::DoRemovePage(pagePos
);
297 // don't delete the page itself though -- it will be deleted in
298 // DeletePage() when we return
305 DoInternalRemovePageRange(pagePos
, subCount
);
307 tree
->DeleteChildren( pageId
);
308 tree
->Delete( pageId
);
313 bool wxTreebook::DeleteAllPages()
315 wxBookCtrlBase::DeleteAllPages();
318 m_actualSelection
= wxNOT_FOUND
;
320 wxTreeCtrl
*tree
= GetTreeCtrl();
321 tree
->DeleteChildren(tree
->GetRootItem());
326 void wxTreebook::DoInternalAddPage(size_t newPos
,
327 wxTreebookPage
*page
,
330 wxASSERT_MSG( newPos
<= m_treeIds
.GetCount(), wxT("Ivalid index passed to wxTreebook::DoInternalAddPage") );
332 // hide newly inserted page initially (it will be shown when selected)
336 if ( newPos
== m_treeIds
.GetCount() )
339 m_treeIds
.Add(pageId
);
343 m_treeIds
.Insert(pageId
, newPos
);
345 if ( m_selection
!= wxNOT_FOUND
&& newPos
<= (size_t)m_selection
)
347 // selection has been moved one unit toward the end
349 if ( m_actualSelection
!= wxNOT_FOUND
)
352 else if ( m_actualSelection
!= wxNOT_FOUND
&&
353 newPos
<= (size_t)m_actualSelection
)
355 DoSetSelection(m_selection
);
360 void wxTreebook::DoInternalRemovePageRange(size_t pagePos
, size_t subCount
)
362 // Attention: this function is only for a situation when we delete a node
363 // with all its children so pagePos is the node's index and subCount is the
364 // node children count
365 wxASSERT_MSG( pagePos
+ subCount
< m_treeIds
.GetCount(),
366 wxT("Ivalid page index") );
368 wxTreeItemId pageId
= m_treeIds
[pagePos
];
370 m_treeIds
.RemoveAt(pagePos
, subCount
+ 1);
372 if ( m_selection
!= wxNOT_FOUND
)
374 if ( (size_t)m_selection
> pagePos
+ subCount
)
376 // selection is far after the deleted page, so just update the index and move on
377 m_selection
-= 1 + subCount
;
378 if ( m_actualSelection
!= wxNOT_FOUND
)
380 m_actualSelection
-= subCount
+ 1;
383 else if ( (size_t)m_selection
>= pagePos
)
385 wxTreeCtrl
*tree
= GetTreeCtrl();
387 // as selected page is going to be deleted, try to select the next
388 // sibling if exists, if not then the parent
389 wxTreeItemId nodeId
= tree
->GetNextSibling(pageId
);
391 m_selection
= wxNOT_FOUND
;
392 m_actualSelection
= wxNOT_FOUND
;
396 // selecting next siblings
397 tree
->SelectItem(nodeId
);
399 else // no next sibling, select the parent
401 wxTreeItemId parentId
= tree
->GetItemParent(pageId
);
403 if ( parentId
.IsOk() && parentId
!= tree
->GetRootItem() )
405 tree
->SelectItem(parentId
);
407 else // parent is root
409 // we can't select it as it's hidden
410 DoUpdateSelection(false, wxNOT_FOUND
);
414 else if ( m_actualSelection
!= wxNOT_FOUND
&&
415 (size_t)m_actualSelection
>= pagePos
)
417 // nothing to do -- selection is before the deleted node, but
418 // actually shown page (the first (sub)child with page != NULL) is
420 m_actualSelection
= m_selection
;
422 // send event as documented
423 DoSetSelection(m_selection
, SetSelection_SendEvent
);
425 //else: nothing to do -- selection is before the deleted node
429 DoUpdateSelection(false, wxNOT_FOUND
);
434 void wxTreebook::DoUpdateSelection(bool bSelect
, int newPos
)
441 else if ( m_selection
== wxNOT_FOUND
&& DoInternalGetPageCount() > 0 )
447 newSelPos
= wxNOT_FOUND
;
450 if ( newSelPos
!= wxNOT_FOUND
)
452 SetSelection((size_t)newSelPos
);
456 wxTreeItemId
wxTreebook::DoInternalGetPage(size_t pagePos
) const
458 if ( pagePos
>= m_treeIds
.GetCount() )
460 // invalid position but ok here, in this internal function, don't assert
461 // (the caller will do it)
462 return wxTreeItemId();
465 return m_treeIds
[pagePos
];
468 int wxTreebook::DoInternalFindPageById(wxTreeItemId pageId
) const
470 const size_t count
= m_treeIds
.GetCount();
471 for ( size_t i
= 0; i
< count
; ++i
)
473 if ( m_treeIds
[i
] == pageId
)
480 bool wxTreebook::IsNodeExpanded(size_t pagePos
) const
482 wxTreeItemId pageId
= DoInternalGetPage(pagePos
);
484 wxCHECK_MSG( pageId
.IsOk(), false, wxT("invalid tree item") );
486 return GetTreeCtrl()->IsExpanded(pageId
);
489 bool wxTreebook::ExpandNode(size_t pagePos
, bool expand
)
491 wxTreeItemId pageId
= DoInternalGetPage(pagePos
);
493 wxCHECK_MSG( pageId
.IsOk(), false, wxT("invalid tree item") );
497 GetTreeCtrl()->Expand( pageId
);
501 GetTreeCtrl()->Collapse( pageId
);
503 // rely on the events generated by wxTreeCtrl to update selection
509 int wxTreebook::GetPageParent(size_t pagePos
) const
511 wxTreeItemId nodeId
= DoInternalGetPage( pagePos
);
512 wxCHECK_MSG( nodeId
.IsOk(), wxNOT_FOUND
, wxT("Invalid page index spacified!") );
514 const wxTreeItemId parent
= GetTreeCtrl()->GetItemParent( nodeId
);
516 return parent
.IsOk() ? DoInternalFindPageById(parent
) : wxNOT_FOUND
;
519 bool wxTreebook::SetPageText(size_t n
, const wxString
& strText
)
521 wxTreeItemId pageId
= DoInternalGetPage(n
);
523 wxCHECK_MSG( pageId
.IsOk(), false, wxT("invalid tree item") );
525 GetTreeCtrl()->SetItemText(pageId
, strText
);
530 wxString
wxTreebook::GetPageText(size_t n
) const
532 wxTreeItemId pageId
= DoInternalGetPage(n
);
534 wxCHECK_MSG( pageId
.IsOk(), wxString(), wxT("invalid tree item") );
536 return GetTreeCtrl()->GetItemText(pageId
);
539 int wxTreebook::GetPageImage(size_t n
) const
541 wxTreeItemId pageId
= DoInternalGetPage(n
);
543 wxCHECK_MSG( pageId
.IsOk(), wxNOT_FOUND
, wxT("invalid tree item") );
545 return GetTreeCtrl()->GetItemImage(pageId
);
548 bool wxTreebook::SetPageImage(size_t n
, int imageId
)
550 wxTreeItemId pageId
= DoInternalGetPage(n
);
552 wxCHECK_MSG( pageId
.IsOk(), false, wxT("invalid tree item") );
554 GetTreeCtrl()->SetItemImage(pageId
, imageId
);
559 wxSize
wxTreebook::CalcSizeFromPage(const wxSize
& sizePage
) const
561 const wxSize sizeTree
= GetControllerSize();
563 wxSize size
= sizePage
;
564 size
.x
+= sizeTree
.x
;
569 int wxTreebook::GetSelection() const
574 int wxTreebook::DoSetSelection(size_t pagePos
, int flags
)
576 wxCHECK_MSG( IS_VALID_PAGE(pagePos
), wxNOT_FOUND
,
577 wxT("invalid page index in wxListbook::DoSetSelection()") );
578 wxASSERT_MSG( GetPageCount() == DoInternalGetPageCount(),
579 wxT("wxTreebook logic error: m_treeIds and m_pages not in sync!"));
581 wxBookCtrlEvent
event(wxEVT_COMMAND_TREEBOOK_PAGE_CHANGING
, m_windowId
);
582 const int oldSel
= m_selection
;
583 wxTreeCtrl
*tree
= GetTreeCtrl();
584 bool allowed
= false;
586 if (flags
& SetSelection_SendEvent
)
588 event
.SetEventObject(this);
589 event
.SetSelection(pagePos
);
590 event
.SetOldSelection(m_selection
);
592 // don't send the event if the old and new pages are the same; do send it
593 // otherwise and be prepared for it to be vetoed
594 allowed
= (int)pagePos
== m_selection
||
595 !GetEventHandler()->ProcessEvent(event
) ||
599 if ( !(flags
& SetSelection_SendEvent
) || allowed
)
601 // hide the previously shown page
602 wxTreebookPage
* const oldPage
= DoGetCurrentPage();
606 // then show the new one
607 m_selection
= pagePos
;
608 wxTreebookPage
*page
= wxBookCtrlBase::GetPage(m_selection
);
611 // find the next page suitable to be shown: the first (grand)child
612 // of this one with a non-NULL associated page
613 wxTreeItemId childId
= m_treeIds
[pagePos
];
614 int actualPagePos
= pagePos
;
615 while ( !page
&& childId
.IsOk() )
617 wxTreeItemIdValue cookie
;
618 childId
= tree
->GetFirstChild( childId
, cookie
);
619 if ( childId
.IsOk() )
621 page
= wxBookCtrlBase::GetPage(++actualPagePos
);
625 m_actualSelection
= page
? actualPagePos
: m_selection
;
631 tree
->SelectItem(DoInternalGetPage(pagePos
));
633 if (flags
& SetSelection_SendEvent
)
635 // notify about the (now completed) page change
636 event
.SetEventType(wxEVT_COMMAND_TREEBOOK_PAGE_CHANGED
);
637 (void)GetEventHandler()->ProcessEvent(event
);
640 else if ( (flags
& SetSelection_SendEvent
) && !allowed
) // page change vetoed
642 // tree selection might have already had changed
643 if ( oldSel
!= wxNOT_FOUND
)
644 tree
->SelectItem(DoInternalGetPage(oldSel
));
650 wxTreebookPage
*wxTreebook::DoGetCurrentPage() const
652 if ( m_selection
== wxNOT_FOUND
)
655 wxTreebookPage
*page
= wxBookCtrlBase::GetPage(m_selection
);
656 if ( !page
&& m_actualSelection
!= wxNOT_FOUND
)
658 page
= wxBookCtrlBase::GetPage(m_actualSelection
);
664 void wxTreebook::SetImageList(wxImageList
*imageList
)
666 wxBookCtrlBase::SetImageList(imageList
);
667 GetTreeCtrl()->SetImageList(imageList
);
670 void wxTreebook::AssignImageList(wxImageList
*imageList
)
672 wxBookCtrlBase::AssignImageList(imageList
);
673 GetTreeCtrl()->SetImageList(imageList
);
676 // ----------------------------------------------------------------------------
678 // ----------------------------------------------------------------------------
680 void wxTreebook::OnTreeSelectionChange(wxTreeEvent
& event
)
682 if ( event
.GetEventObject() != m_bookctrl
)
688 wxTreeItemId newId
= event
.GetItem();
690 if ( (m_selection
== wxNOT_FOUND
&&
691 (!newId
.IsOk() || newId
== GetTreeCtrl()->GetRootItem())) ||
692 (m_selection
!= wxNOT_FOUND
&& newId
== m_treeIds
[m_selection
]) )
694 // this event can only come when we modify the tree selection ourselves
695 // so we should simply ignore it
699 int newPos
= DoInternalFindPageById(newId
);
701 if ( newPos
!= wxNOT_FOUND
)
702 SetSelection( newPos
);
705 void wxTreebook::OnTreeNodeExpandedCollapsed(wxTreeEvent
& event
)
707 if ( event
.GetEventObject() != m_bookctrl
)
713 wxTreeItemId nodeId
= event
.GetItem();
714 if ( !nodeId
.IsOk() || nodeId
== GetTreeCtrl()->GetRootItem() )
716 int pagePos
= DoInternalFindPageById(nodeId
);
717 wxCHECK_RET( pagePos
!= wxNOT_FOUND
, wxT("Internal problem in wxTreebook!..") );
719 wxBookCtrlEvent
ev(GetTreeCtrl()->IsExpanded(nodeId
)
720 ? wxEVT_COMMAND_TREEBOOK_NODE_EXPANDED
721 : wxEVT_COMMAND_TREEBOOK_NODE_COLLAPSED
,
724 ev
.SetSelection(pagePos
);
725 ev
.SetOldSelection(pagePos
);
726 ev
.SetEventObject(this);
728 GetEventHandler()->ProcessEvent(ev
);
731 // ----------------------------------------------------------------------------
732 // wxTreebook geometry management
733 // ----------------------------------------------------------------------------
735 int wxTreebook::HitTest(wxPoint
const & pt
, long * flags
) const
737 int pagePos
= wxNOT_FOUND
;
740 *flags
= wxBK_HITTEST_NOWHERE
;
742 // convert from wxTreebook coorindates to wxTreeCtrl ones
743 const wxTreeCtrl
* const tree
= GetTreeCtrl();
744 const wxPoint treePt
= tree
->ScreenToClient(ClientToScreen(pt
));
746 // is it over the tree?
747 if ( wxRect(tree
->GetSize()).Contains(treePt
) )
750 wxTreeItemId id
= tree
->HitTest(treePt
, flagsTree
);
752 if ( id
.IsOk() && (flagsTree
& wxTREE_HITTEST_ONITEM
) )
754 pagePos
= DoInternalFindPageById(id
);
759 if ( pagePos
!= wxNOT_FOUND
)
762 if ( flagsTree
& (wxTREE_HITTEST_ONITEMBUTTON
|
763 wxTREE_HITTEST_ONITEMICON
|
764 wxTREE_HITTEST_ONITEMSTATEICON
) )
765 *flags
|= wxBK_HITTEST_ONICON
;
767 if ( flagsTree
& wxTREE_HITTEST_ONITEMLABEL
)
768 *flags
|= wxBK_HITTEST_ONLABEL
;
771 else // not over the tree
773 if ( flags
&& GetPageRect().Contains( pt
) )
774 *flags
|= wxBK_HITTEST_ONPAGE
;
780 #endif // wxUSE_TREEBOOK