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
)
49 IMPLEMENT_DYNAMIC_CLASS(wxTreebookEvent
, wxNotifyEvent
)
51 const wxEventType wxEVT_COMMAND_TREEBOOK_PAGE_CHANGING
= wxNewEventType();
52 const wxEventType wxEVT_COMMAND_TREEBOOK_PAGE_CHANGED
= wxNewEventType();
53 const wxEventType wxEVT_COMMAND_TREEBOOK_NODE_COLLAPSED
= wxNewEventType();
54 const wxEventType wxEVT_COMMAND_TREEBOOK_NODE_EXPANDED
= wxNewEventType();
56 BEGIN_EVENT_TABLE(wxTreebook
, wxBookCtrlBase
)
57 EVT_TREE_SEL_CHANGED (wxID_ANY
, wxTreebook::OnTreeSelectionChange
)
58 EVT_TREE_ITEM_EXPANDED (wxID_ANY
, wxTreebook::OnTreeNodeExpandedCollapsed
)
59 EVT_TREE_ITEM_COLLAPSED(wxID_ANY
, wxTreebook::OnTreeNodeExpandedCollapsed
)
62 // ============================================================================
63 // wxTreebook implementation
64 // ============================================================================
66 // ----------------------------------------------------------------------------
67 // wxTreebook creation
68 // ----------------------------------------------------------------------------
70 void wxTreebook::Init()
73 m_actualSelection
= wxNOT_FOUND
;
77 wxTreebook::Create(wxWindow
*parent
,
84 // Check the style flag to have either wxTBK_RIGHT or wxTBK_LEFT
85 if ( (style
& wxBK_ALIGN_MASK
) == wxBK_DEFAULT
)
89 style
|= wxTAB_TRAVERSAL
;
91 // no border for this control, it doesn't look nice together with the tree
92 style
&= ~wxBORDER_MASK
;
93 style
|= wxBORDER_NONE
;
95 if ( !wxControl::Create(parent
, id
, pos
, size
,
96 style
, wxDefaultValidator
, name
) )
99 m_bookctrl
= new wxTreeCtrl
110 GetTreeCtrl()->SetQuickBestSize(false); // do full size calculation
111 GetTreeCtrl()->AddRoot(wxEmptyString
); // label doesn't matter, it's hidden
114 // We need to add dummy size event to force possible scrollbar hiding
116 GetEventHandler()->AddPendingEvent(evt
);
123 // insert a new page just before the pagePos
124 bool wxTreebook::InsertPage(size_t pagePos
,
126 const wxString
& text
,
130 return DoInsertPage(pagePos
, page
, text
, bSelect
, imageId
);
133 bool wxTreebook::InsertSubPage(size_t pagePos
,
135 const wxString
& text
,
139 return DoInsertSubPage(pagePos
, page
, text
, bSelect
, imageId
);
142 bool wxTreebook::AddPage(wxWindow
*page
, const wxString
& text
, bool bSelect
,
145 return DoInsertPage(m_treeIds
.GetCount(), page
, text
, bSelect
, imageId
);
148 // insertion time is linear to the number of top-pages
149 bool wxTreebook::AddSubPage(wxWindow
*page
, const wxString
& text
, bool bSelect
, int imageId
)
151 return DoAddSubPage(page
, text
, bSelect
, imageId
);
155 bool wxTreebook::DoInsertPage(size_t pagePos
,
157 const wxString
& text
,
161 wxCHECK_MSG( pagePos
<= DoInternalGetPageCount(), false,
162 wxT("Invalid treebook page position") );
164 if ( !wxBookCtrlBase::InsertPage(pagePos
, page
, text
, bSelect
, imageId
) )
167 wxTreeCtrl
*tree
= GetTreeCtrl();
169 if ( pagePos
== DoInternalGetPageCount() )
171 // append the page to the end
172 wxTreeItemId rootId
= tree
->GetRootItem();
174 newId
= tree
->AppendItem(rootId
, text
, imageId
);
176 else // insert the new page before the given one
178 wxTreeItemId nodeId
= m_treeIds
[pagePos
];
180 wxTreeItemId previousId
= tree
->GetPrevSibling(nodeId
);
181 wxTreeItemId parentId
= tree
->GetItemParent(nodeId
);
183 if ( previousId
.IsOk() )
185 // insert before the sibling - previousId
186 newId
= tree
->InsertItem(parentId
, previousId
, text
, imageId
);
188 else // no prev siblings -- insert as a first child
190 wxASSERT_MSG( parentId
.IsOk(), wxT( "Tree has no root node?" ) );
192 newId
= tree
->PrependItem(parentId
, text
, imageId
);
198 //something wrong -> cleaning and returning with false
199 (void)wxBookCtrlBase::DoRemovePage(pagePos
);
201 wxFAIL_MSG( wxT("Failed to insert treebook page") );
205 DoInternalAddPage(pagePos
, page
, newId
);
207 DoUpdateSelection(bSelect
, pagePos
);
212 bool wxTreebook::DoAddSubPage(wxWindow
*page
, const wxString
& text
, bool bSelect
, int imageId
)
214 wxTreeCtrl
*tree
= GetTreeCtrl();
216 wxTreeItemId rootId
= tree
->GetRootItem();
218 wxTreeItemId lastNodeId
= tree
->GetLastChild(rootId
);
220 wxCHECK_MSG( lastNodeId
.IsOk(), false,
221 _T("Can't insert sub page when there are no pages") );
223 // now calculate its position (should we save/update it too?)
224 size_t newPos
= tree
->GetCount() -
225 (tree
->GetChildrenCount(lastNodeId
, true) + 1);
227 return DoInsertSubPage(newPos
, page
, text
, bSelect
, imageId
);
230 bool wxTreebook::DoInsertSubPage(size_t pagePos
,
231 wxTreebookPage
*page
,
232 const wxString
& text
,
236 wxTreeItemId parentId
= DoInternalGetPage(pagePos
);
237 wxCHECK_MSG( parentId
.IsOk(), false, wxT("invalid tree item") );
239 wxTreeCtrl
*tree
= GetTreeCtrl();
241 size_t newPos
= pagePos
+ tree
->GetChildrenCount(parentId
, true) + 1;
242 wxASSERT_MSG( newPos
<= DoInternalGetPageCount(),
243 wxT("Internal error in tree insert point calculation") );
245 if ( !wxBookCtrlBase::InsertPage(newPos
, page
, text
, bSelect
, imageId
) )
248 wxTreeItemId newId
= tree
->AppendItem(parentId
, text
, imageId
);
252 (void)wxBookCtrlBase::DoRemovePage(newPos
);
254 wxFAIL_MSG( wxT("Failed to insert treebook page") );
258 DoInternalAddPage(newPos
, page
, newId
);
260 DoUpdateSelection(bSelect
, newPos
);
265 bool wxTreebook::DeletePage(size_t pagePos
)
267 wxCHECK_MSG( IS_VALID_PAGE(pagePos
), false, wxT("Invalid tree index") );
269 wxTreebookPage
*oldPage
= DoRemovePage(pagePos
);
278 wxTreebookPage
*wxTreebook::DoRemovePage(size_t pagePos
)
280 wxTreeItemId pageId
= DoInternalGetPage(pagePos
);
281 wxCHECK_MSG( pageId
.IsOk(), NULL
, wxT("Invalid tree index") );
283 wxTreebookPage
* oldPage
= GetPage(pagePos
);
284 wxTreeCtrl
*tree
= GetTreeCtrl();
286 size_t subCount
= tree
->GetChildrenCount(pageId
, true);
287 wxASSERT_MSG ( IS_VALID_PAGE(pagePos
+ subCount
),
288 wxT("Internal error in wxTreebook::DoRemovePage") );
290 // here we are going to delete ALL the pages in the range
291 // [pagePos, pagePos + subCount] -- the page and its children
293 // deleting all the pages from the base class
294 for ( size_t i
= 0; i
<= subCount
; ++i
)
296 wxTreebookPage
*page
= wxBookCtrlBase::DoRemovePage(pagePos
);
298 // don't delete the page itself though -- it will be deleted in
299 // DeletePage() when we return
306 DoInternalRemovePageRange(pagePos
, subCount
);
308 tree
->DeleteChildren( pageId
);
309 tree
->Delete( pageId
);
314 bool wxTreebook::DeleteAllPages()
316 wxBookCtrlBase::DeleteAllPages();
319 m_actualSelection
= wxNOT_FOUND
;
321 wxTreeCtrl
*tree
= GetTreeCtrl();
322 tree
->DeleteChildren(tree
->GetRootItem());
327 void wxTreebook::DoInternalAddPage(size_t newPos
,
328 wxTreebookPage
*page
,
331 wxASSERT_MSG( newPos
<= m_treeIds
.GetCount(), wxT("Ivalid index passed to wxTreebook::DoInternalAddPage") );
333 // hide newly inserted page initially (it will be shown when selected)
337 if ( newPos
== m_treeIds
.GetCount() )
340 m_treeIds
.Add(pageId
);
344 m_treeIds
.Insert(pageId
, newPos
);
346 if ( m_selection
!= wxNOT_FOUND
&& newPos
<= (size_t)m_selection
)
348 // selection has been moved one unit toward the end
350 if ( m_actualSelection
!= wxNOT_FOUND
)
353 else if ( m_actualSelection
!= wxNOT_FOUND
&&
354 newPos
<= (size_t)m_actualSelection
)
356 DoSetSelection(m_selection
);
361 void wxTreebook::DoInternalRemovePageRange(size_t pagePos
, size_t subCount
)
363 // Attention: this function is only for a situation when we delete a node
364 // with all its children so pagePos is the node's index and subCount is the
365 // node children count
366 wxASSERT_MSG( pagePos
+ subCount
< m_treeIds
.GetCount(),
367 wxT("Ivalid page index") );
369 wxTreeItemId pageId
= m_treeIds
[pagePos
];
371 m_treeIds
.RemoveAt(pagePos
, subCount
+ 1);
373 if ( m_selection
!= wxNOT_FOUND
)
375 if ( (size_t)m_selection
> pagePos
+ subCount
)
377 // selection is far after the deleted page, so just update the index and move on
378 m_selection
-= 1 + subCount
;
379 if ( m_actualSelection
!= wxNOT_FOUND
)
381 m_actualSelection
-= subCount
+ 1;
384 else if ( (size_t)m_selection
>= pagePos
)
386 wxTreeCtrl
*tree
= GetTreeCtrl();
388 // as selected page is going to be deleted, try to select the next
389 // sibling if exists, if not then the parent
390 wxTreeItemId nodeId
= tree
->GetNextSibling(pageId
);
392 m_selection
= wxNOT_FOUND
;
393 m_actualSelection
= wxNOT_FOUND
;
397 // selecting next siblings
398 tree
->SelectItem(nodeId
);
400 else // no next sibling, select the parent
402 wxTreeItemId parentId
= tree
->GetItemParent(pageId
);
404 if ( parentId
.IsOk() && parentId
!= tree
->GetRootItem() )
406 tree
->SelectItem(parentId
);
408 else // parent is root
410 // we can't select it as it's hidden
411 DoUpdateSelection(false, wxNOT_FOUND
);
415 else if ( m_actualSelection
!= wxNOT_FOUND
&&
416 (size_t)m_actualSelection
>= pagePos
)
418 // nothing to do -- selection is before the deleted node, but
419 // actually shown page (the first (sub)child with page != NULL) is
421 m_actualSelection
= m_selection
;
423 // send event as documented
424 DoSetSelection(m_selection
, SetSelection_SendEvent
);
426 //else: nothing to do -- selection is before the deleted node
430 DoUpdateSelection(false, wxNOT_FOUND
);
435 void wxTreebook::DoUpdateSelection(bool bSelect
, int newPos
)
442 else if ( m_selection
== wxNOT_FOUND
&& DoInternalGetPageCount() > 0 )
448 newSelPos
= wxNOT_FOUND
;
451 if ( newSelPos
!= wxNOT_FOUND
)
453 SetSelection((size_t)newSelPos
);
457 wxTreeItemId
wxTreebook::DoInternalGetPage(size_t pagePos
) const
459 if ( pagePos
>= m_treeIds
.GetCount() )
461 // invalid position but ok here, in this internal function, don't assert
462 // (the caller will do it)
463 return wxTreeItemId();
466 return m_treeIds
[pagePos
];
469 int wxTreebook::DoInternalFindPageById(wxTreeItemId pageId
) const
471 const size_t count
= m_treeIds
.GetCount();
472 for ( size_t i
= 0; i
< count
; ++i
)
474 if ( m_treeIds
[i
] == pageId
)
481 bool wxTreebook::IsNodeExpanded(size_t pagePos
) const
483 wxTreeItemId pageId
= DoInternalGetPage(pagePos
);
485 wxCHECK_MSG( pageId
.IsOk(), false, wxT("invalid tree item") );
487 return GetTreeCtrl()->IsExpanded(pageId
);
490 bool wxTreebook::ExpandNode(size_t pagePos
, bool expand
)
492 wxTreeItemId pageId
= DoInternalGetPage(pagePos
);
494 wxCHECK_MSG( pageId
.IsOk(), false, wxT("invalid tree item") );
498 GetTreeCtrl()->Expand( pageId
);
502 GetTreeCtrl()->Collapse( pageId
);
504 // rely on the events generated by wxTreeCtrl to update selection
510 int wxTreebook::GetPageParent(size_t pagePos
) const
512 wxTreeItemId nodeId
= DoInternalGetPage( pagePos
);
513 wxCHECK_MSG( nodeId
.IsOk(), wxNOT_FOUND
, wxT("Invalid page index spacified!") );
515 const wxTreeItemId parent
= GetTreeCtrl()->GetItemParent( nodeId
);
517 return parent
.IsOk() ? DoInternalFindPageById(parent
) : wxNOT_FOUND
;
520 bool wxTreebook::SetPageText(size_t n
, const wxString
& strText
)
522 wxTreeItemId pageId
= DoInternalGetPage(n
);
524 wxCHECK_MSG( pageId
.IsOk(), false, wxT("invalid tree item") );
526 GetTreeCtrl()->SetItemText(pageId
, strText
);
531 wxString
wxTreebook::GetPageText(size_t n
) const
533 wxTreeItemId pageId
= DoInternalGetPage(n
);
535 wxCHECK_MSG( pageId
.IsOk(), wxString(), wxT("invalid tree item") );
537 return GetTreeCtrl()->GetItemText(pageId
);
540 int wxTreebook::GetPageImage(size_t n
) const
542 wxTreeItemId pageId
= DoInternalGetPage(n
);
544 wxCHECK_MSG( pageId
.IsOk(), wxNOT_FOUND
, wxT("invalid tree item") );
546 return GetTreeCtrl()->GetItemImage(pageId
);
549 bool wxTreebook::SetPageImage(size_t n
, int imageId
)
551 wxTreeItemId pageId
= DoInternalGetPage(n
);
553 wxCHECK_MSG( pageId
.IsOk(), false, wxT("invalid tree item") );
555 GetTreeCtrl()->SetItemImage(pageId
, imageId
);
560 wxSize
wxTreebook::CalcSizeFromPage(const wxSize
& sizePage
) const
562 const wxSize sizeTree
= GetControllerSize();
564 wxSize size
= sizePage
;
565 size
.x
+= sizeTree
.x
;
570 int wxTreebook::GetSelection() const
575 int wxTreebook::DoSetSelection(size_t pagePos
, int flags
)
577 wxCHECK_MSG( IS_VALID_PAGE(pagePos
), wxNOT_FOUND
,
578 wxT("invalid page index in wxListbook::DoSetSelection()") );
579 wxASSERT_MSG( GetPageCount() == DoInternalGetPageCount(),
580 wxT("wxTreebook logic error: m_treeIds and m_pages not in sync!"));
582 wxTreebookEvent
event(wxEVT_COMMAND_TREEBOOK_PAGE_CHANGING
, m_windowId
);
583 const int oldSel
= m_selection
;
584 wxTreeCtrl
*tree
= GetTreeCtrl();
585 bool allowed
= false;
587 if (flags
& SetSelection_SendEvent
)
589 event
.SetEventObject(this);
590 event
.SetSelection(pagePos
);
591 event
.SetOldSelection(m_selection
);
593 // don't send the event if the old and new pages are the same; do send it
594 // otherwise and be prepared for it to be vetoed
595 allowed
= (int)pagePos
== m_selection
||
596 !GetEventHandler()->ProcessEvent(event
) ||
600 if ( !(flags
& SetSelection_SendEvent
) || allowed
)
602 // hide the previously shown page
603 wxTreebookPage
* const oldPage
= DoGetCurrentPage();
607 // then show the new one
608 m_selection
= pagePos
;
609 wxTreebookPage
*page
= wxBookCtrlBase::GetPage(m_selection
);
612 // find the next page suitable to be shown: the first (grand)child
613 // of this one with a non-NULL associated page
614 wxTreeItemId childId
= m_treeIds
[pagePos
];
615 int actualPagePos
= pagePos
;
616 while ( !page
&& childId
.IsOk() )
618 wxTreeItemIdValue cookie
;
619 childId
= tree
->GetFirstChild( childId
, cookie
);
620 if ( childId
.IsOk() )
622 page
= wxBookCtrlBase::GetPage(++actualPagePos
);
626 m_actualSelection
= page
? actualPagePos
: m_selection
;
632 tree
->SelectItem(DoInternalGetPage(pagePos
));
634 if (flags
& SetSelection_SendEvent
)
636 // notify about the (now completed) page change
637 event
.SetEventType(wxEVT_COMMAND_TREEBOOK_PAGE_CHANGED
);
638 (void)GetEventHandler()->ProcessEvent(event
);
641 else if ( (flags
& SetSelection_SendEvent
) && !allowed
) // page change vetoed
643 // tree selection might have already had changed
644 if ( oldSel
!= wxNOT_FOUND
)
645 tree
->SelectItem(DoInternalGetPage(oldSel
));
651 wxTreebookPage
*wxTreebook::DoGetCurrentPage() const
653 if ( m_selection
== wxNOT_FOUND
)
656 wxTreebookPage
*page
= wxBookCtrlBase::GetPage(m_selection
);
657 if ( !page
&& m_actualSelection
!= wxNOT_FOUND
)
659 page
= wxBookCtrlBase::GetPage(m_actualSelection
);
665 void wxTreebook::SetImageList(wxImageList
*imageList
)
667 wxBookCtrlBase::SetImageList(imageList
);
668 GetTreeCtrl()->SetImageList(imageList
);
671 void wxTreebook::AssignImageList(wxImageList
*imageList
)
673 wxBookCtrlBase::AssignImageList(imageList
);
674 GetTreeCtrl()->SetImageList(imageList
);
677 // ----------------------------------------------------------------------------
679 // ----------------------------------------------------------------------------
681 void wxTreebook::OnTreeSelectionChange(wxTreeEvent
& event
)
683 if ( event
.GetEventObject() != m_bookctrl
)
689 wxTreeItemId newId
= event
.GetItem();
691 if ( (m_selection
== wxNOT_FOUND
&&
692 (!newId
.IsOk() || newId
== GetTreeCtrl()->GetRootItem())) ||
693 (m_selection
!= wxNOT_FOUND
&& newId
== m_treeIds
[m_selection
]) )
695 // this event can only come when we modify the tree selection ourselves
696 // so we should simply ignore it
700 int newPos
= DoInternalFindPageById(newId
);
702 if ( newPos
!= wxNOT_FOUND
)
703 SetSelection( newPos
);
706 void wxTreebook::OnTreeNodeExpandedCollapsed(wxTreeEvent
& event
)
708 if ( event
.GetEventObject() != m_bookctrl
)
714 wxTreeItemId nodeId
= event
.GetItem();
715 if ( !nodeId
.IsOk() || nodeId
== GetTreeCtrl()->GetRootItem() )
717 int pagePos
= DoInternalFindPageById(nodeId
);
718 wxCHECK_RET( pagePos
!= wxNOT_FOUND
, wxT("Internal problem in wxTreebook!..") );
720 wxTreebookEvent
ev(GetTreeCtrl()->IsExpanded(nodeId
)
721 ? wxEVT_COMMAND_TREEBOOK_NODE_EXPANDED
722 : wxEVT_COMMAND_TREEBOOK_NODE_COLLAPSED
,
725 ev
.SetSelection(pagePos
);
726 ev
.SetOldSelection(pagePos
);
727 ev
.SetEventObject(this);
729 GetEventHandler()->ProcessEvent(ev
);
732 // ----------------------------------------------------------------------------
733 // wxTreebook geometry management
734 // ----------------------------------------------------------------------------
736 int wxTreebook::HitTest(wxPoint
const & pt
, long * flags
) const
738 int pagePos
= wxNOT_FOUND
;
741 *flags
= wxBK_HITTEST_NOWHERE
;
743 // convert from wxTreebook coorindates to wxTreeCtrl ones
744 const wxTreeCtrl
* const tree
= GetTreeCtrl();
745 const wxPoint treePt
= tree
->ScreenToClient(ClientToScreen(pt
));
747 // is it over the tree?
748 if ( wxRect(tree
->GetSize()).Contains(treePt
) )
751 wxTreeItemId id
= tree
->HitTest(treePt
, flagsTree
);
753 if ( id
.IsOk() && (flagsTree
& wxTREE_HITTEST_ONITEM
) )
755 pagePos
= DoInternalFindPageById(id
);
760 if ( pagePos
!= wxNOT_FOUND
)
763 if ( flagsTree
& (wxTREE_HITTEST_ONITEMBUTTON
|
764 wxTREE_HITTEST_ONITEMICON
|
765 wxTREE_HITTEST_ONITEMSTATEICON
) )
766 *flags
|= wxBK_HITTEST_ONICON
;
768 if ( flagsTree
& wxTREE_HITTEST_ONITEMLABEL
)
769 *flags
|= wxBK_HITTEST_ONLABEL
;
772 else // not over the tree
774 if ( flags
&& GetPageRect().Contains( pt
) )
775 *flags
|= wxBK_HITTEST_ONPAGE
;
781 #endif // wxUSE_TREEBOOK