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_TREEBOOK_PAGE_CHANGING
, wxBookCtrlEvent
);
51 wxDEFINE_EVENT( wxEVT_TREEBOOK_PAGE_CHANGED
, wxBookCtrlEvent
);
52 wxDEFINE_EVENT( wxEVT_TREEBOOK_NODE_COLLAPSED
, wxBookCtrlEvent
);
53 wxDEFINE_EVENT( wxEVT_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 int wxTreebook::DoSetSelection(size_t pagePos
, int flags
)
561 wxCHECK_MSG( IS_VALID_PAGE(pagePos
), wxNOT_FOUND
,
562 wxT("invalid page index in wxListbook::DoSetSelection()") );
563 wxASSERT_MSG( GetPageCount() == DoInternalGetPageCount(),
564 wxT("wxTreebook logic error: m_treeIds and m_pages not in sync!"));
566 wxBookCtrlEvent
event(wxEVT_TREEBOOK_PAGE_CHANGING
, m_windowId
);
567 const int oldSel
= m_selection
;
568 wxTreeCtrl
*tree
= GetTreeCtrl();
569 bool allowed
= false;
571 if (flags
& SetSelection_SendEvent
)
573 event
.SetEventObject(this);
574 event
.SetSelection(pagePos
);
575 event
.SetOldSelection(m_selection
);
577 // don't send the event if the old and new pages are the same; do send it
578 // otherwise and be prepared for it to be vetoed
579 allowed
= (int)pagePos
== m_selection
||
580 !GetEventHandler()->ProcessEvent(event
) ||
584 if ( !(flags
& SetSelection_SendEvent
) || allowed
)
586 // hide the previously shown page
587 wxTreebookPage
* const oldPage
= DoGetCurrentPage();
591 // then show the new one
592 m_selection
= pagePos
;
593 wxTreebookPage
*page
= wxBookCtrlBase::GetPage(m_selection
);
596 // find the next page suitable to be shown: the first (grand)child
597 // of this one with a non-NULL associated page
598 wxTreeItemId childId
= m_treeIds
[pagePos
];
599 int actualPagePos
= pagePos
;
600 while ( !page
&& childId
.IsOk() )
602 wxTreeItemIdValue cookie
;
603 childId
= tree
->GetFirstChild( childId
, cookie
);
604 if ( childId
.IsOk() )
606 page
= wxBookCtrlBase::GetPage(++actualPagePos
);
610 m_actualSelection
= page
? actualPagePos
: m_selection
;
616 tree
->SelectItem(DoInternalGetPage(pagePos
));
618 if (flags
& SetSelection_SendEvent
)
620 // notify about the (now completed) page change
621 event
.SetEventType(wxEVT_TREEBOOK_PAGE_CHANGED
);
622 (void)GetEventHandler()->ProcessEvent(event
);
625 else if ( (flags
& SetSelection_SendEvent
) && !allowed
) // page change vetoed
627 // tree selection might have already had changed
628 if ( oldSel
!= wxNOT_FOUND
)
629 tree
->SelectItem(DoInternalGetPage(oldSel
));
635 wxTreebookPage
*wxTreebook::DoGetCurrentPage() const
637 if ( m_selection
== wxNOT_FOUND
)
640 wxTreebookPage
*page
= wxBookCtrlBase::GetPage(m_selection
);
641 if ( !page
&& m_actualSelection
!= wxNOT_FOUND
)
643 page
= wxBookCtrlBase::GetPage(m_actualSelection
);
649 void wxTreebook::SetImageList(wxImageList
*imageList
)
651 wxBookCtrlBase::SetImageList(imageList
);
652 GetTreeCtrl()->SetImageList(imageList
);
655 void wxTreebook::AssignImageList(wxImageList
*imageList
)
657 wxBookCtrlBase::AssignImageList(imageList
);
658 GetTreeCtrl()->SetImageList(imageList
);
661 // ----------------------------------------------------------------------------
663 // ----------------------------------------------------------------------------
665 void wxTreebook::OnTreeSelectionChange(wxTreeEvent
& event
)
667 if ( event
.GetEventObject() != m_bookctrl
)
673 wxTreeItemId newId
= event
.GetItem();
675 if ( (m_selection
== wxNOT_FOUND
&&
676 (!newId
.IsOk() || newId
== GetTreeCtrl()->GetRootItem())) ||
677 (m_selection
!= wxNOT_FOUND
&& newId
== m_treeIds
[m_selection
]) )
679 // this event can only come when we modify the tree selection ourselves
680 // so we should simply ignore it
684 int newPos
= DoInternalFindPageById(newId
);
686 if ( newPos
!= wxNOT_FOUND
)
687 SetSelection( newPos
);
690 void wxTreebook::OnTreeNodeExpandedCollapsed(wxTreeEvent
& event
)
692 if ( event
.GetEventObject() != m_bookctrl
)
698 wxTreeItemId nodeId
= event
.GetItem();
699 if ( !nodeId
.IsOk() || nodeId
== GetTreeCtrl()->GetRootItem() )
701 int pagePos
= DoInternalFindPageById(nodeId
);
702 wxCHECK_RET( pagePos
!= wxNOT_FOUND
, wxT("Internal problem in wxTreebook!..") );
704 wxBookCtrlEvent
ev(GetTreeCtrl()->IsExpanded(nodeId
)
705 ? wxEVT_TREEBOOK_NODE_EXPANDED
706 : wxEVT_TREEBOOK_NODE_COLLAPSED
,
709 ev
.SetSelection(pagePos
);
710 ev
.SetOldSelection(pagePos
);
711 ev
.SetEventObject(this);
713 GetEventHandler()->ProcessEvent(ev
);
716 // ----------------------------------------------------------------------------
717 // wxTreebook geometry management
718 // ----------------------------------------------------------------------------
720 int wxTreebook::HitTest(wxPoint
const & pt
, long * flags
) const
722 int pagePos
= wxNOT_FOUND
;
725 *flags
= wxBK_HITTEST_NOWHERE
;
727 // convert from wxTreebook coorindates to wxTreeCtrl ones
728 const wxTreeCtrl
* const tree
= GetTreeCtrl();
729 const wxPoint treePt
= tree
->ScreenToClient(ClientToScreen(pt
));
731 // is it over the tree?
732 if ( wxRect(tree
->GetSize()).Contains(treePt
) )
735 wxTreeItemId id
= tree
->HitTest(treePt
, flagsTree
);
737 if ( id
.IsOk() && (flagsTree
& wxTREE_HITTEST_ONITEM
) )
739 pagePos
= DoInternalFindPageById(id
);
744 if ( pagePos
!= wxNOT_FOUND
)
747 if ( flagsTree
& (wxTREE_HITTEST_ONITEMBUTTON
|
748 wxTREE_HITTEST_ONITEMICON
|
749 wxTREE_HITTEST_ONITEMSTATEICON
) )
750 *flags
|= wxBK_HITTEST_ONICON
;
752 if ( flagsTree
& wxTREE_HITTEST_ONITEMLABEL
)
753 *flags
|= wxBK_HITTEST_ONLABEL
;
756 else // not over the tree
758 if ( flags
&& GetPageRect().Contains( pt
) )
759 *flags
|= wxBK_HITTEST_ONPAGE
;
765 #endif // wxUSE_TREEBOOK