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"
30 #include "wx/imaglist.h"
31 #include "wx/settings.h"
33 // ----------------------------------------------------------------------------
34 // various wxWidgets macros
35 // ----------------------------------------------------------------------------
37 // check that the page index is valid
38 #define IS_VALID_PAGE(nPage) ((nPage) < DoInternalGetPageCount())
40 // ----------------------------------------------------------------------------
42 // ----------------------------------------------------------------------------
44 IMPLEMENT_DYNAMIC_CLASS(wxTreebook
, wxBookCtrlBase
)
45 IMPLEMENT_DYNAMIC_CLASS(wxTreebookEvent
, wxNotifyEvent
)
47 const wxEventType wxEVT_COMMAND_TREEBOOK_PAGE_CHANGING
= wxNewEventType();
48 const wxEventType wxEVT_COMMAND_TREEBOOK_PAGE_CHANGED
= wxNewEventType();
49 const wxEventType wxEVT_COMMAND_TREEBOOK_NODE_COLLAPSED
= wxNewEventType();
50 const wxEventType wxEVT_COMMAND_TREEBOOK_NODE_EXPANDED
= wxNewEventType();
51 const int wxID_TREEBOOKTREEVIEW
= wxNewId();
53 BEGIN_EVENT_TABLE(wxTreebook
, wxBookCtrlBase
)
54 EVT_TREE_SEL_CHANGED (wxID_TREEBOOKTREEVIEW
, wxTreebook::OnTreeSelectionChange
)
55 EVT_TREE_ITEM_EXPANDED (wxID_TREEBOOKTREEVIEW
, wxTreebook::OnTreeNodeExpandedCollapsed
)
56 EVT_TREE_ITEM_COLLAPSED(wxID_TREEBOOKTREEVIEW
, wxTreebook::OnTreeNodeExpandedCollapsed
)
59 // ============================================================================
60 // wxTreebook implementation
61 // ============================================================================
63 // ----------------------------------------------------------------------------
64 // wxTreebook creation
65 // ----------------------------------------------------------------------------
67 void wxTreebook::Init()
70 m_actualSelection
= wxNOT_FOUND
;
74 wxTreebook::Create(wxWindow
*parent
,
81 // Check the style flag to have either wxTBK_RIGHT or wxTBK_LEFT
82 if ( (style
& wxBK_ALIGN_MASK
) == wxBK_DEFAULT
)
87 // no border for this control, it doesn't look nice together with the tree
88 style
&= ~wxBORDER_MASK
;
89 style
|= wxBORDER_NONE
;
91 if ( !wxControl::Create(parent
, id
, pos
, size
,
92 style
, wxDefaultValidator
, name
) )
95 m_bookctrl
= new wxTreeCtrl
98 wxID_TREEBOOKTREEVIEW
,
106 GetTreeCtrl()->AddRoot(wxEmptyString
); // label doesn't matter, it's hidden
109 // We need to add dummy size event to force possible scrollbar hiding
111 GetEventHandler()->AddPendingEvent(evt
);
118 // insert a new page just before the pagePos
119 bool wxTreebook::InsertPage(size_t pagePos
,
121 const wxString
& text
,
125 return DoInsertPage(pagePos
, page
, text
, bSelect
, imageId
);
128 bool wxTreebook::AddSubPage(size_t pagePos
,
130 const wxString
& text
,
134 return DoInsertSubPage(pagePos
, page
, text
, bSelect
, imageId
);
137 bool wxTreebook::AddPage(wxWindow
*page
, const wxString
& text
, bool bSelect
,
140 return DoInsertPage(m_treeIds
.GetCount(), page
, text
, bSelect
, imageId
);
143 // insertion time is linear to the number of top-pages
144 bool wxTreebook::AddSubPage(wxWindow
*page
, const wxString
& text
, bool bSelect
, int imageId
)
146 return DoAddSubPage(page
, text
, bSelect
, imageId
);
150 bool wxTreebook::DoInsertPage(size_t pagePos
,
152 const wxString
& text
,
156 wxCHECK_MSG( pagePos
<= DoInternalGetPageCount(), false,
157 wxT("Invalid treebook page position") );
159 if ( !wxBookCtrlBase::InsertPage(pagePos
, page
, text
, bSelect
, imageId
) )
162 wxTreeCtrl
*tree
= GetTreeCtrl();
164 if ( pagePos
== DoInternalGetPageCount() )
166 // append the page to the end
167 wxTreeItemId rootId
= tree
->GetRootItem();
169 newId
= tree
->AppendItem(rootId
, text
, imageId
);
171 else // insert the new page before the given one
173 wxTreeItemId nodeId
= m_treeIds
[pagePos
];
175 wxTreeItemId previousId
= tree
->GetPrevSibling(nodeId
);
176 wxTreeItemId parentId
= tree
->GetItemParent(nodeId
);
178 if ( previousId
.IsOk() )
180 // insert before the sibling - previousId
181 newId
= tree
->InsertItem(parentId
, previousId
, text
, imageId
);
183 else // no prev siblings -- insert as a first child
185 wxASSERT_MSG( parentId
.IsOk(), wxT( "Tree has no root node?" ) );
187 newId
= tree
->PrependItem(parentId
, text
, imageId
);
193 //something wrong -> cleaning and returning with false
194 (void)wxBookCtrlBase::DoRemovePage(pagePos
);
196 wxFAIL_MSG( wxT("Failed to insert treebook page") );
200 DoInternalAddPage(pagePos
, page
, newId
);
202 DoUpdateSelection(bSelect
, pagePos
);
204 m_bookctrl
->InvalidateBestSize();
209 bool wxTreebook::DoAddSubPage(wxWindow
*page
, const wxString
& text
, bool bSelect
, int imageId
)
211 wxTreeCtrl
*tree
= GetTreeCtrl();
213 wxTreeItemId rootId
= tree
->GetRootItem();
215 wxTreeItemId lastNodeId
= tree
->GetLastChild(rootId
);
217 wxCHECK_MSG( lastNodeId
.IsOk(), false,
218 _T("Can't insert sub page when there are no pages") );
220 // now calculate its position (should we save/update it too?)
221 size_t newPos
= tree
->GetCount() -
222 (tree
->GetChildrenCount(lastNodeId
, true) + 1);
224 return DoInsertSubPage(newPos
, page
, text
, bSelect
, imageId
);
227 bool wxTreebook::DoInsertSubPage(size_t pagePos
,
228 wxTreebookPage
*page
,
229 const wxString
& text
,
233 wxTreeItemId parentId
= DoInternalGetPage(pagePos
);
234 wxCHECK_MSG( parentId
.IsOk(), false, wxT("invalid tree item") );
236 wxTreeCtrl
*tree
= GetTreeCtrl();
238 size_t newPos
= pagePos
+ tree
->GetChildrenCount(parentId
, true) + 1;
239 wxASSERT_MSG( newPos
<= DoInternalGetPageCount(),
240 wxT("Internal error in tree insert point calculation") );
242 if ( !wxBookCtrlBase::InsertPage(newPos
, page
, text
, bSelect
, imageId
) )
245 wxTreeItemId newId
= tree
->AppendItem(parentId
, text
, imageId
);
249 (void)wxBookCtrlBase::DoRemovePage(newPos
);
251 wxFAIL_MSG( wxT("Failed to insert treebook page") );
255 DoInternalAddPage(newPos
, page
, newId
);
257 DoUpdateSelection(bSelect
, newPos
);
259 m_bookctrl
->InvalidateBestSize();
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
);
309 tree
->InvalidateBestSize();
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
;
422 DoSetSelection(m_selection
);
424 //else: nothing to do -- selection is before the deleted node
428 DoUpdateSelection(false, wxNOT_FOUND
);
433 void wxTreebook::DoUpdateSelection(bool bSelect
, int newPos
)
440 else if ( m_selection
== wxNOT_FOUND
&& DoInternalGetPageCount() > 0 )
446 newSelPos
= wxNOT_FOUND
;
449 if ( newSelPos
!= wxNOT_FOUND
)
451 SetSelection((size_t)newSelPos
);
455 wxTreeItemId
wxTreebook::DoInternalGetPage(size_t pagePos
) const
457 if ( pagePos
>= m_treeIds
.GetCount() )
459 // invalid position but ok here, in this internal function, don't assert
460 // (the caller will do it)
461 return wxTreeItemId();
464 return m_treeIds
[pagePos
];
467 int wxTreebook::DoInternalFindPageById(wxTreeItemId pageId
) const
469 const size_t count
= m_treeIds
.GetCount();
470 for ( size_t i
= 0; i
< count
; ++i
)
472 if ( m_treeIds
[i
] == pageId
)
479 bool wxTreebook::IsNodeExpanded(size_t pagePos
) const
481 wxTreeItemId pageId
= DoInternalGetPage(pagePos
);
483 wxCHECK_MSG( pageId
.IsOk(), false, wxT("invalid tree item") );
485 return GetTreeCtrl()->IsExpanded(pageId
);
488 bool wxTreebook::ExpandNode(size_t pagePos
, bool expand
)
490 wxTreeItemId pageId
= DoInternalGetPage(pagePos
);
492 wxCHECK_MSG( pageId
.IsOk(), false, wxT("invalid tree item") );
496 GetTreeCtrl()->Expand( pageId
);
500 GetTreeCtrl()->Collapse( pageId
);
502 // rely on the events generated by wxTreeCtrl to update selection
508 int wxTreebook::GetPageParent(size_t pagePos
) const
510 wxTreeItemId nodeId
= DoInternalGetPage( pagePos
);
511 wxCHECK_MSG( nodeId
.IsOk(), wxNOT_FOUND
, wxT("Invalid page index spacified!") );
513 const wxTreeItemId parent
= GetTreeCtrl()->GetItemParent( nodeId
);
515 return parent
.IsOk() ? DoInternalFindPageById(parent
) : wxNOT_FOUND
;
518 bool wxTreebook::SetPageText(size_t n
, const wxString
& strText
)
520 wxTreeItemId pageId
= DoInternalGetPage(n
);
522 wxCHECK_MSG( pageId
.IsOk(), false, wxT("invalid tree item") );
524 GetTreeCtrl()->SetItemText(pageId
, strText
);
529 wxString
wxTreebook::GetPageText(size_t n
) const
531 wxTreeItemId pageId
= DoInternalGetPage(n
);
533 wxCHECK_MSG( pageId
.IsOk(), wxString(), wxT("invalid tree item") );
535 return GetTreeCtrl()->GetItemText(pageId
);
538 int wxTreebook::GetPageImage(size_t n
) const
540 wxTreeItemId pageId
= DoInternalGetPage(n
);
542 wxCHECK_MSG( pageId
.IsOk(), wxNOT_FOUND
, wxT("invalid tree item") );
544 return GetTreeCtrl()->GetItemImage(pageId
);
547 bool wxTreebook::SetPageImage(size_t n
, int imageId
)
549 wxTreeItemId pageId
= DoInternalGetPage(n
);
551 wxCHECK_MSG( pageId
.IsOk(), false, wxT("invalid tree item") );
553 GetTreeCtrl()->SetItemImage(pageId
, imageId
);
558 wxSize
wxTreebook::CalcSizeFromPage(const wxSize
& sizePage
) const
560 const wxSize sizeTree
= GetControllerSize();
562 wxSize size
= sizePage
;
563 size
.x
+= sizeTree
.x
;
568 int wxTreebook::GetSelection() const
573 int wxTreebook::SetSelection(size_t pagePos
)
575 if ( (size_t)m_selection
!= pagePos
)
576 return DoSetSelection(pagePos
);
581 int wxTreebook::DoSetSelection(size_t pagePos
)
583 wxCHECK_MSG( IS_VALID_PAGE(pagePos
), wxNOT_FOUND
,
584 wxT("invalid page index in wxListbook::SetSelection()") );
585 wxASSERT_MSG( GetPageCount() == DoInternalGetPageCount(),
586 wxT("wxTreebook logic error: m_treeIds and m_pages not in sync!"));
588 const int oldSel
= m_selection
;
589 wxTreeCtrl
*tree
= GetTreeCtrl();
591 wxTreebookEvent
event(wxEVT_COMMAND_TREEBOOK_PAGE_CHANGING
, m_windowId
);
592 event
.SetEventObject(this);
593 event
.SetSelection(pagePos
);
594 event
.SetOldSelection(m_selection
);
596 // don't send the event if the old and new pages are the same; do send it
597 // otherwise and be prepared for it to be vetoed
598 if ( (int)pagePos
== m_selection
||
599 !GetEventHandler()->ProcessEvent(event
) ||
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 m_actualSelection
= pagePos
;
616 while ( !page
&& childId
.IsOk() )
618 wxTreeItemIdValue cookie
;
619 childId
= tree
->GetFirstChild( childId
, cookie
);
620 if ( childId
.IsOk() )
622 page
= wxBookCtrlBase::GetPage(++m_actualSelection
);
626 wxASSERT_MSG( page
, wxT("no page to show found!") );
631 page
->SetSize(GetPageRect());
635 tree
->SelectItem(DoInternalGetPage(pagePos
));
637 // notify about the (now completed) page change
638 event
.SetEventType(wxEVT_COMMAND_TREEBOOK_PAGE_CHANGED
);
639 (void)GetEventHandler()->ProcessEvent(event
);
641 else // page change vetoed
643 // tree selection might have already had changed
644 tree
->SelectItem(DoInternalGetPage(oldSel
));
650 void wxTreebook::SetImageList(wxImageList
*imageList
)
652 wxBookCtrlBase::SetImageList(imageList
);
653 GetTreeCtrl()->SetImageList(imageList
);
656 void wxTreebook::AssignImageList(wxImageList
*imageList
)
658 wxBookCtrlBase::AssignImageList(imageList
);
659 GetTreeCtrl()->SetImageList(imageList
);
662 // ----------------------------------------------------------------------------
664 // ----------------------------------------------------------------------------
666 void wxTreebook::OnTreeSelectionChange(wxTreeEvent
& event
)
668 wxTreeItemId newId
= event
.GetItem();
670 if ( (m_selection
== wxNOT_FOUND
&&
671 (!newId
.IsOk() || newId
== GetTreeCtrl()->GetRootItem())) ||
672 (m_selection
!= wxNOT_FOUND
&& newId
== m_treeIds
[m_selection
]) )
674 // this event can only come when we modify the tree selection ourselves
675 // so we should simply ignore it
679 int newPos
= DoInternalFindPageById(newId
);
681 if ( newPos
!= wxNOT_FOUND
)
682 SetSelection( newPos
);
685 void wxTreebook::OnTreeNodeExpandedCollapsed(wxTreeEvent
& event
)
687 wxTreeItemId nodeId
= event
.GetItem();
688 if ( !nodeId
.IsOk() || nodeId
== GetTreeCtrl()->GetRootItem() )
690 int pagePos
= DoInternalFindPageById(nodeId
);
691 wxCHECK_RET( pagePos
!= wxNOT_FOUND
, wxT("Internal problem in wxTreebook!..") );
693 wxTreebookEvent
ev(GetTreeCtrl()->IsExpanded(nodeId
)
694 ? wxEVT_COMMAND_TREEBOOK_NODE_EXPANDED
695 : wxEVT_COMMAND_TREEBOOK_NODE_COLLAPSED
,
698 ev
.SetSelection(pagePos
);
699 ev
.SetOldSelection(pagePos
);
700 ev
.SetEventObject(this);
702 GetEventHandler()->ProcessEvent(ev
);
705 // ----------------------------------------------------------------------------
706 // wxTreebook geometry management
707 // ----------------------------------------------------------------------------
709 wxTreebookPage
* wxTreebook::DoGetCurrentPage() const
711 if ( m_selection
== wxNOT_FOUND
)
714 wxTreebookPage
*page
= wxBookCtrlBase::GetPage(m_selection
);
715 if ( !page
&& m_actualSelection
!= wxNOT_FOUND
)
717 page
= wxBookCtrlBase::GetPage(m_actualSelection
);
723 #endif // wxUSE_TREEBOOK