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 #if !WXWIN_COMPATIBILITY_EVENT_TYPES
52 const wxEventType wxEVT_COMMAND_TREEBOOK_PAGE_CHANGING
= wxNewEventType();
53 const wxEventType wxEVT_COMMAND_TREEBOOK_PAGE_CHANGED
= wxNewEventType();
54 const wxEventType wxEVT_COMMAND_TREEBOOK_NODE_COLLAPSED
= wxNewEventType();
55 const wxEventType wxEVT_COMMAND_TREEBOOK_NODE_EXPANDED
= wxNewEventType();
57 const int wxID_TREEBOOKTREEVIEW
= wxNewId();
59 BEGIN_EVENT_TABLE(wxTreebook
, wxBookCtrlBase
)
60 EVT_TREE_SEL_CHANGED (wxID_TREEBOOKTREEVIEW
, wxTreebook::OnTreeSelectionChange
)
61 EVT_TREE_ITEM_EXPANDED (wxID_TREEBOOKTREEVIEW
, wxTreebook::OnTreeNodeExpandedCollapsed
)
62 EVT_TREE_ITEM_COLLAPSED(wxID_TREEBOOKTREEVIEW
, wxTreebook::OnTreeNodeExpandedCollapsed
)
65 // ============================================================================
66 // wxTreebook implementation
67 // ============================================================================
69 // ----------------------------------------------------------------------------
70 // wxTreebook creation
71 // ----------------------------------------------------------------------------
73 void wxTreebook::Init()
76 m_actualSelection
= wxNOT_FOUND
;
80 wxTreebook::Create(wxWindow
*parent
,
87 // Check the style flag to have either wxTBK_RIGHT or wxTBK_LEFT
88 if ( (style
& wxBK_ALIGN_MASK
) == wxBK_DEFAULT
)
93 // no border for this control, it doesn't look nice together with the tree
94 style
&= ~wxBORDER_MASK
;
95 style
|= wxBORDER_NONE
;
97 if ( !wxControl::Create(parent
, id
, pos
, size
,
98 style
, wxDefaultValidator
, name
) )
101 m_bookctrl
= new wxTreeCtrl
104 wxID_TREEBOOKTREEVIEW
,
108 wxBORDER_SIMPLE
| // On wxMSW this produces a black border which is wrong
114 GetTreeCtrl()->SetQuickBestSize(false); // do full size calculation
115 GetTreeCtrl()->AddRoot(wxEmptyString
); // label doesn't matter, it's hidden
118 // We need to add dummy size event to force possible scrollbar hiding
120 GetEventHandler()->AddPendingEvent(evt
);
127 // insert a new page just before the pagePos
128 bool wxTreebook::InsertPage(size_t pagePos
,
130 const wxString
& text
,
134 return DoInsertPage(pagePos
, page
, text
, bSelect
, imageId
);
137 bool wxTreebook::InsertSubPage(size_t pagePos
,
139 const wxString
& text
,
143 return DoInsertSubPage(pagePos
, page
, text
, bSelect
, imageId
);
146 bool wxTreebook::AddPage(wxWindow
*page
, const wxString
& text
, bool bSelect
,
149 return DoInsertPage(m_treeIds
.GetCount(), page
, text
, bSelect
, imageId
);
152 // insertion time is linear to the number of top-pages
153 bool wxTreebook::AddSubPage(wxWindow
*page
, const wxString
& text
, bool bSelect
, int imageId
)
155 return DoAddSubPage(page
, text
, bSelect
, imageId
);
159 bool wxTreebook::DoInsertPage(size_t pagePos
,
161 const wxString
& text
,
165 wxCHECK_MSG( pagePos
<= DoInternalGetPageCount(), false,
166 wxT("Invalid treebook page position") );
168 if ( !wxBookCtrlBase::InsertPage(pagePos
, page
, text
, bSelect
, imageId
) )
171 wxTreeCtrl
*tree
= GetTreeCtrl();
173 if ( pagePos
== DoInternalGetPageCount() )
175 // append the page to the end
176 wxTreeItemId rootId
= tree
->GetRootItem();
178 newId
= tree
->AppendItem(rootId
, text
, imageId
);
180 else // insert the new page before the given one
182 wxTreeItemId nodeId
= m_treeIds
[pagePos
];
184 wxTreeItemId previousId
= tree
->GetPrevSibling(nodeId
);
185 wxTreeItemId parentId
= tree
->GetItemParent(nodeId
);
187 if ( previousId
.IsOk() )
189 // insert before the sibling - previousId
190 newId
= tree
->InsertItem(parentId
, previousId
, text
, imageId
);
192 else // no prev siblings -- insert as a first child
194 wxASSERT_MSG( parentId
.IsOk(), wxT( "Tree has no root node?" ) );
196 newId
= tree
->PrependItem(parentId
, text
, imageId
);
202 //something wrong -> cleaning and returning with false
203 (void)wxBookCtrlBase::DoRemovePage(pagePos
);
205 wxFAIL_MSG( wxT("Failed to insert treebook page") );
209 DoInternalAddPage(pagePos
, page
, newId
);
211 DoUpdateSelection(bSelect
, pagePos
);
216 bool wxTreebook::DoAddSubPage(wxWindow
*page
, const wxString
& text
, bool bSelect
, int imageId
)
218 wxTreeCtrl
*tree
= GetTreeCtrl();
220 wxTreeItemId rootId
= tree
->GetRootItem();
222 wxTreeItemId lastNodeId
= tree
->GetLastChild(rootId
);
224 wxCHECK_MSG( lastNodeId
.IsOk(), false,
225 _T("Can't insert sub page when there are no pages") );
227 // now calculate its position (should we save/update it too?)
228 size_t newPos
= tree
->GetCount() -
229 (tree
->GetChildrenCount(lastNodeId
, true) + 1);
231 return DoInsertSubPage(newPos
, page
, text
, bSelect
, imageId
);
234 bool wxTreebook::DoInsertSubPage(size_t pagePos
,
235 wxTreebookPage
*page
,
236 const wxString
& text
,
240 wxTreeItemId parentId
= DoInternalGetPage(pagePos
);
241 wxCHECK_MSG( parentId
.IsOk(), false, wxT("invalid tree item") );
243 wxTreeCtrl
*tree
= GetTreeCtrl();
245 size_t newPos
= pagePos
+ tree
->GetChildrenCount(parentId
, true) + 1;
246 wxASSERT_MSG( newPos
<= DoInternalGetPageCount(),
247 wxT("Internal error in tree insert point calculation") );
249 if ( !wxBookCtrlBase::InsertPage(newPos
, page
, text
, bSelect
, imageId
) )
252 wxTreeItemId newId
= tree
->AppendItem(parentId
, text
, imageId
);
254 tree
->InvalidateBestSize();
258 (void)wxBookCtrlBase::DoRemovePage(newPos
);
260 wxFAIL_MSG( wxT("Failed to insert treebook page") );
264 DoInternalAddPage(newPos
, page
, newId
);
266 DoUpdateSelection(bSelect
, newPos
);
271 bool wxTreebook::DeletePage(size_t pagePos
)
273 wxCHECK_MSG( IS_VALID_PAGE(pagePos
), false, wxT("Invalid tree index") );
275 wxTreebookPage
*oldPage
= DoRemovePage(pagePos
);
284 wxTreebookPage
*wxTreebook::DoRemovePage(size_t pagePos
)
286 wxTreeItemId pageId
= DoInternalGetPage(pagePos
);
287 wxCHECK_MSG( pageId
.IsOk(), NULL
, wxT("Invalid tree index") );
289 wxTreebookPage
* oldPage
= GetPage(pagePos
);
290 wxTreeCtrl
*tree
= GetTreeCtrl();
292 size_t subCount
= tree
->GetChildrenCount(pageId
, true);
293 wxASSERT_MSG ( IS_VALID_PAGE(pagePos
+ subCount
),
294 wxT("Internal error in wxTreebook::DoRemovePage") );
296 // here we are going to delete ALL the pages in the range
297 // [pagePos, pagePos + subCount] -- the page and its children
299 // deleting all the pages from the base class
300 for ( size_t i
= 0; i
<= subCount
; ++i
)
302 wxTreebookPage
*page
= wxBookCtrlBase::DoRemovePage(pagePos
);
304 // don't delete the page itself though -- it will be deleted in
305 // DeletePage() when we return
312 DoInternalRemovePageRange(pagePos
, subCount
);
314 tree
->DeleteChildren( pageId
);
315 tree
->Delete( pageId
);
320 bool wxTreebook::DeleteAllPages()
322 wxBookCtrlBase::DeleteAllPages();
325 m_actualSelection
= wxNOT_FOUND
;
327 wxTreeCtrl
*tree
= GetTreeCtrl();
328 tree
->DeleteChildren(tree
->GetRootItem());
333 void wxTreebook::DoInternalAddPage(size_t newPos
,
334 wxTreebookPage
*page
,
337 wxASSERT_MSG( newPos
<= m_treeIds
.GetCount(), wxT("Ivalid index passed to wxTreebook::DoInternalAddPage") );
339 // hide newly inserted page initially (it will be shown when selected)
343 if ( newPos
== m_treeIds
.GetCount() )
346 m_treeIds
.Add(pageId
);
350 m_treeIds
.Insert(pageId
, newPos
);
352 if ( m_selection
!= wxNOT_FOUND
&& newPos
<= (size_t)m_selection
)
354 // selection has been moved one unit toward the end
356 if ( m_actualSelection
!= wxNOT_FOUND
)
359 else if ( m_actualSelection
!= wxNOT_FOUND
&&
360 newPos
<= (size_t)m_actualSelection
)
362 DoSetSelection(m_selection
);
367 void wxTreebook::DoInternalRemovePageRange(size_t pagePos
, size_t subCount
)
369 // Attention: this function is only for a situation when we delete a node
370 // with all its children so pagePos is the node's index and subCount is the
371 // node children count
372 wxASSERT_MSG( pagePos
+ subCount
< m_treeIds
.GetCount(),
373 wxT("Ivalid page index") );
375 wxTreeItemId pageId
= m_treeIds
[pagePos
];
377 m_treeIds
.RemoveAt(pagePos
, subCount
+ 1);
379 if ( m_selection
!= wxNOT_FOUND
)
381 if ( (size_t)m_selection
> pagePos
+ subCount
)
383 // selection is far after the deleted page, so just update the index and move on
384 m_selection
-= 1 + subCount
;
385 if ( m_actualSelection
!= wxNOT_FOUND
)
387 m_actualSelection
-= subCount
+ 1;
390 else if ( (size_t)m_selection
>= pagePos
)
392 wxTreeCtrl
*tree
= GetTreeCtrl();
394 // as selected page is going to be deleted, try to select the next
395 // sibling if exists, if not then the parent
396 wxTreeItemId nodeId
= tree
->GetNextSibling(pageId
);
398 m_selection
= wxNOT_FOUND
;
399 m_actualSelection
= wxNOT_FOUND
;
403 // selecting next siblings
404 tree
->SelectItem(nodeId
);
406 else // no next sibling, select the parent
408 wxTreeItemId parentId
= tree
->GetItemParent(pageId
);
410 if ( parentId
.IsOk() && parentId
!= tree
->GetRootItem() )
412 tree
->SelectItem(parentId
);
414 else // parent is root
416 // we can't select it as it's hidden
417 DoUpdateSelection(false, wxNOT_FOUND
);
421 else if ( m_actualSelection
!= wxNOT_FOUND
&&
422 (size_t)m_actualSelection
>= pagePos
)
424 // nothing to do -- selection is before the deleted node, but
425 // actually shown page (the first (sub)child with page != NULL) is
427 m_actualSelection
= m_selection
;
428 DoSetSelection(m_selection
);
430 //else: nothing to do -- selection is before the deleted node
434 DoUpdateSelection(false, wxNOT_FOUND
);
439 void wxTreebook::DoUpdateSelection(bool bSelect
, int newPos
)
446 else if ( m_selection
== wxNOT_FOUND
&& DoInternalGetPageCount() > 0 )
452 newSelPos
= wxNOT_FOUND
;
455 if ( newSelPos
!= wxNOT_FOUND
)
457 SetSelection((size_t)newSelPos
);
461 wxTreeItemId
wxTreebook::DoInternalGetPage(size_t pagePos
) const
463 if ( pagePos
>= m_treeIds
.GetCount() )
465 // invalid position but ok here, in this internal function, don't assert
466 // (the caller will do it)
467 return wxTreeItemId();
470 return m_treeIds
[pagePos
];
473 int wxTreebook::DoInternalFindPageById(wxTreeItemId pageId
) const
475 const size_t count
= m_treeIds
.GetCount();
476 for ( size_t i
= 0; i
< count
; ++i
)
478 if ( m_treeIds
[i
] == pageId
)
485 bool wxTreebook::IsNodeExpanded(size_t pagePos
) const
487 wxTreeItemId pageId
= DoInternalGetPage(pagePos
);
489 wxCHECK_MSG( pageId
.IsOk(), false, wxT("invalid tree item") );
491 return GetTreeCtrl()->IsExpanded(pageId
);
494 bool wxTreebook::ExpandNode(size_t pagePos
, bool expand
)
496 wxTreeItemId pageId
= DoInternalGetPage(pagePos
);
498 wxCHECK_MSG( pageId
.IsOk(), false, wxT("invalid tree item") );
502 GetTreeCtrl()->Expand( pageId
);
506 GetTreeCtrl()->Collapse( pageId
);
508 // rely on the events generated by wxTreeCtrl to update selection
514 int wxTreebook::GetPageParent(size_t pagePos
) const
516 wxTreeItemId nodeId
= DoInternalGetPage( pagePos
);
517 wxCHECK_MSG( nodeId
.IsOk(), wxNOT_FOUND
, wxT("Invalid page index spacified!") );
519 const wxTreeItemId parent
= GetTreeCtrl()->GetItemParent( nodeId
);
521 return parent
.IsOk() ? DoInternalFindPageById(parent
) : wxNOT_FOUND
;
524 bool wxTreebook::SetPageText(size_t n
, const wxString
& strText
)
526 wxTreeItemId pageId
= DoInternalGetPage(n
);
528 wxCHECK_MSG( pageId
.IsOk(), false, wxT("invalid tree item") );
530 GetTreeCtrl()->SetItemText(pageId
, strText
);
535 wxString
wxTreebook::GetPageText(size_t n
) const
537 wxTreeItemId pageId
= DoInternalGetPage(n
);
539 wxCHECK_MSG( pageId
.IsOk(), wxString(), wxT("invalid tree item") );
541 return GetTreeCtrl()->GetItemText(pageId
);
544 int wxTreebook::GetPageImage(size_t n
) const
546 wxTreeItemId pageId
= DoInternalGetPage(n
);
548 wxCHECK_MSG( pageId
.IsOk(), wxNOT_FOUND
, wxT("invalid tree item") );
550 return GetTreeCtrl()->GetItemImage(pageId
);
553 bool wxTreebook::SetPageImage(size_t n
, int imageId
)
555 wxTreeItemId pageId
= DoInternalGetPage(n
);
557 wxCHECK_MSG( pageId
.IsOk(), false, wxT("invalid tree item") );
559 GetTreeCtrl()->SetItemImage(pageId
, imageId
);
564 wxSize
wxTreebook::CalcSizeFromPage(const wxSize
& sizePage
) const
566 const wxSize sizeTree
= GetControllerSize();
568 wxSize size
= sizePage
;
569 size
.x
+= sizeTree
.x
;
574 int wxTreebook::GetSelection() const
579 int wxTreebook::SetSelection(size_t pagePos
)
581 if ( (size_t)m_selection
!= pagePos
)
582 return DoSetSelection(pagePos
);
587 int wxTreebook::DoSetSelection(size_t pagePos
)
589 wxCHECK_MSG( IS_VALID_PAGE(pagePos
), wxNOT_FOUND
,
590 wxT("invalid page index in wxListbook::SetSelection()") );
591 wxASSERT_MSG( GetPageCount() == DoInternalGetPageCount(),
592 wxT("wxTreebook logic error: m_treeIds and m_pages not in sync!"));
594 const int oldSel
= m_selection
;
595 wxTreeCtrl
*tree
= GetTreeCtrl();
597 wxTreebookEvent
event(wxEVT_COMMAND_TREEBOOK_PAGE_CHANGING
, m_windowId
);
598 event
.SetEventObject(this);
599 event
.SetSelection(pagePos
);
600 event
.SetOldSelection(m_selection
);
602 // don't send the event if the old and new pages are the same; do send it
603 // otherwise and be prepared for it to be vetoed
604 if ( (int)pagePos
== m_selection
||
605 !GetEventHandler()->ProcessEvent(event
) ||
608 // hide the previously shown page
609 wxTreebookPage
* const oldPage
= DoGetCurrentPage();
613 // then show the new one
614 m_selection
= pagePos
;
615 wxTreebookPage
*page
= wxBookCtrlBase::GetPage(m_selection
);
618 // find the next page suitable to be shown: the first (grand)child
619 // of this one with a non-NULL associated page
620 wxTreeItemId childId
= m_treeIds
[pagePos
];
621 int actualPagePos
= pagePos
;
622 while ( !page
&& childId
.IsOk() )
624 wxTreeItemIdValue cookie
;
625 childId
= tree
->GetFirstChild( childId
, cookie
);
626 if ( childId
.IsOk() )
628 page
= wxBookCtrlBase::GetPage(++actualPagePos
);
632 m_actualSelection
= page
? actualPagePos
: m_selection
;
638 tree
->SelectItem(DoInternalGetPage(pagePos
));
640 // notify about the (now completed) page change
641 event
.SetEventType(wxEVT_COMMAND_TREEBOOK_PAGE_CHANGED
);
642 (void)GetEventHandler()->ProcessEvent(event
);
644 else // page change vetoed
646 // tree selection might have already had changed
647 tree
->SelectItem(DoInternalGetPage(oldSel
));
653 wxTreebookPage
*wxTreebook::DoGetCurrentPage() const
655 if ( m_selection
== wxNOT_FOUND
)
658 wxTreebookPage
*page
= wxBookCtrlBase::GetPage(m_selection
);
659 if ( !page
&& m_actualSelection
!= wxNOT_FOUND
)
661 page
= wxBookCtrlBase::GetPage(m_actualSelection
);
667 void wxTreebook::SetImageList(wxImageList
*imageList
)
669 wxBookCtrlBase::SetImageList(imageList
);
670 GetTreeCtrl()->SetImageList(imageList
);
673 void wxTreebook::AssignImageList(wxImageList
*imageList
)
675 wxBookCtrlBase::AssignImageList(imageList
);
676 GetTreeCtrl()->SetImageList(imageList
);
679 // ----------------------------------------------------------------------------
681 // ----------------------------------------------------------------------------
683 void wxTreebook::OnTreeSelectionChange(wxTreeEvent
& event
)
685 wxTreeItemId newId
= event
.GetItem();
687 if ( (m_selection
== wxNOT_FOUND
&&
688 (!newId
.IsOk() || newId
== GetTreeCtrl()->GetRootItem())) ||
689 (m_selection
!= wxNOT_FOUND
&& newId
== m_treeIds
[m_selection
]) )
691 // this event can only come when we modify the tree selection ourselves
692 // so we should simply ignore it
696 int newPos
= DoInternalFindPageById(newId
);
698 if ( newPos
!= wxNOT_FOUND
)
699 SetSelection( newPos
);
702 void wxTreebook::OnTreeNodeExpandedCollapsed(wxTreeEvent
& event
)
704 wxTreeItemId nodeId
= event
.GetItem();
705 if ( !nodeId
.IsOk() || nodeId
== GetTreeCtrl()->GetRootItem() )
707 int pagePos
= DoInternalFindPageById(nodeId
);
708 wxCHECK_RET( pagePos
!= wxNOT_FOUND
, wxT("Internal problem in wxTreebook!..") );
710 wxTreebookEvent
ev(GetTreeCtrl()->IsExpanded(nodeId
)
711 ? wxEVT_COMMAND_TREEBOOK_NODE_EXPANDED
712 : wxEVT_COMMAND_TREEBOOK_NODE_COLLAPSED
,
715 ev
.SetSelection(pagePos
);
716 ev
.SetOldSelection(pagePos
);
717 ev
.SetEventObject(this);
719 GetEventHandler()->ProcessEvent(ev
);
722 // ----------------------------------------------------------------------------
723 // wxTreebook geometry management
724 // ----------------------------------------------------------------------------
726 int wxTreebook::HitTest(wxPoint
const & pt
, long * WXUNUSED(flags
)) const
728 int pagePos
= wxNOT_FOUND
;
730 wxTreeCtrl
* const tree
= GetTreeCtrl();
731 const wxPoint treePt
= ClientToScreen(tree
->ScreenToClient(pt
));
733 if ( wxRect(tree
->GetSize()).Inside(treePt
) )
736 wxTreeItemId id
= tree
->HitTest(treePt
, flagsTree
);
738 if ( id
.IsOk() && (flagsTree
& wxTREE_HITTEST_ONITEM
) )
740 pagePos
= DoInternalFindPageById(id
);
747 #endif // wxUSE_TREEBOOK