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 #if !WXWIN_COMPATIBILITY_EVENT_TYPES
48 const wxEventType wxEVT_COMMAND_TREEBOOK_PAGE_CHANGING
= wxNewEventType();
49 const wxEventType wxEVT_COMMAND_TREEBOOK_PAGE_CHANGED
= wxNewEventType();
50 const wxEventType wxEVT_COMMAND_TREEBOOK_NODE_COLLAPSED
= wxNewEventType();
51 const wxEventType wxEVT_COMMAND_TREEBOOK_NODE_EXPANDED
= wxNewEventType();
53 const int wxID_TREEBOOKTREEVIEW
= wxNewId();
55 BEGIN_EVENT_TABLE(wxTreebook
, wxBookCtrlBase
)
56 EVT_TREE_SEL_CHANGED (wxID_TREEBOOKTREEVIEW
, wxTreebook::OnTreeSelectionChange
)
57 EVT_TREE_ITEM_EXPANDED (wxID_TREEBOOKTREEVIEW
, wxTreebook::OnTreeNodeExpandedCollapsed
)
58 EVT_TREE_ITEM_COLLAPSED(wxID_TREEBOOKTREEVIEW
, 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
)
89 // no border for this control, it doesn't look nice together with the tree
90 style
&= ~wxBORDER_MASK
;
91 style
|= wxBORDER_NONE
;
93 if ( !wxControl::Create(parent
, id
, pos
, size
,
94 style
, wxDefaultValidator
, name
) )
97 m_bookctrl
= new wxTreeCtrl
100 wxID_TREEBOOKTREEVIEW
,
104 wxBORDER_SIMPLE
| // On wxMSW this produces a black border which is wrong
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
);
250 tree
->InvalidateBestSize();
254 (void)wxBookCtrlBase::DoRemovePage(newPos
);
256 wxFAIL_MSG( wxT("Failed to insert treebook page") );
260 DoInternalAddPage(newPos
, page
, newId
);
262 DoUpdateSelection(bSelect
, newPos
);
267 bool wxTreebook::DeletePage(size_t pagePos
)
269 wxCHECK_MSG( IS_VALID_PAGE(pagePos
), false, wxT("Invalid tree index") );
271 wxTreebookPage
*oldPage
= DoRemovePage(pagePos
);
280 wxTreebookPage
*wxTreebook::DoRemovePage(size_t pagePos
)
282 wxTreeItemId pageId
= DoInternalGetPage(pagePos
);
283 wxCHECK_MSG( pageId
.IsOk(), NULL
, wxT("Invalid tree index") );
285 wxTreebookPage
* oldPage
= GetPage(pagePos
);
286 wxTreeCtrl
*tree
= GetTreeCtrl();
288 size_t subCount
= tree
->GetChildrenCount(pageId
, true);
289 wxASSERT_MSG ( IS_VALID_PAGE(pagePos
+ subCount
),
290 wxT("Internal error in wxTreebook::DoRemovePage") );
292 // here we are going to delete ALL the pages in the range
293 // [pagePos, pagePos + subCount] -- the page and its children
295 // deleting all the pages from the base class
296 for ( size_t i
= 0; i
<= subCount
; ++i
)
298 wxTreebookPage
*page
= wxBookCtrlBase::DoRemovePage(pagePos
);
300 // don't delete the page itself though -- it will be deleted in
301 // DeletePage() when we return
308 DoInternalRemovePageRange(pagePos
, subCount
);
310 tree
->DeleteChildren( pageId
);
311 tree
->Delete( pageId
);
316 bool wxTreebook::DeleteAllPages()
318 wxBookCtrlBase::DeleteAllPages();
321 m_actualSelection
= wxNOT_FOUND
;
323 wxTreeCtrl
*tree
= GetTreeCtrl();
324 tree
->DeleteChildren(tree
->GetRootItem());
329 void wxTreebook::DoInternalAddPage(size_t newPos
,
330 wxTreebookPage
*page
,
333 wxASSERT_MSG( newPos
<= m_treeIds
.GetCount(), wxT("Ivalid index passed to wxTreebook::DoInternalAddPage") );
335 // hide newly inserted page initially (it will be shown when selected)
339 if ( newPos
== m_treeIds
.GetCount() )
342 m_treeIds
.Add(pageId
);
346 m_treeIds
.Insert(pageId
, newPos
);
348 if ( m_selection
!= wxNOT_FOUND
&& newPos
<= (size_t)m_selection
)
350 // selection has been moved one unit toward the end
352 if ( m_actualSelection
!= wxNOT_FOUND
)
355 else if ( m_actualSelection
!= wxNOT_FOUND
&&
356 newPos
<= (size_t)m_actualSelection
)
358 DoSetSelection(m_selection
);
363 void wxTreebook::DoInternalRemovePageRange(size_t pagePos
, size_t subCount
)
365 // Attention: this function is only for a situation when we delete a node
366 // with all its children so pagePos is the node's index and subCount is the
367 // node children count
368 wxASSERT_MSG( pagePos
+ subCount
< m_treeIds
.GetCount(),
369 wxT("Ivalid page index") );
371 wxTreeItemId pageId
= m_treeIds
[pagePos
];
373 m_treeIds
.RemoveAt(pagePos
, subCount
+ 1);
375 if ( m_selection
!= wxNOT_FOUND
)
377 if ( (size_t)m_selection
> pagePos
+ subCount
)
379 // selection is far after the deleted page, so just update the index and move on
380 m_selection
-= 1 + subCount
;
381 if ( m_actualSelection
!= wxNOT_FOUND
)
383 m_actualSelection
-= subCount
+ 1;
386 else if ( (size_t)m_selection
>= pagePos
)
388 wxTreeCtrl
*tree
= GetTreeCtrl();
390 // as selected page is going to be deleted, try to select the next
391 // sibling if exists, if not then the parent
392 wxTreeItemId nodeId
= tree
->GetNextSibling(pageId
);
394 m_selection
= wxNOT_FOUND
;
395 m_actualSelection
= wxNOT_FOUND
;
399 // selecting next siblings
400 tree
->SelectItem(nodeId
);
402 else // no next sibling, select the parent
404 wxTreeItemId parentId
= tree
->GetItemParent(pageId
);
406 if ( parentId
.IsOk() && parentId
!= tree
->GetRootItem() )
408 tree
->SelectItem(parentId
);
410 else // parent is root
412 // we can't select it as it's hidden
413 DoUpdateSelection(false, wxNOT_FOUND
);
417 else if ( m_actualSelection
!= wxNOT_FOUND
&&
418 (size_t)m_actualSelection
>= pagePos
)
420 // nothing to do -- selection is before the deleted node, but
421 // actually shown page (the first (sub)child with page != NULL) is
423 m_actualSelection
= m_selection
;
424 DoSetSelection(m_selection
);
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::SetSelection(size_t pagePos
)
577 if ( (size_t)m_selection
!= pagePos
)
578 return DoSetSelection(pagePos
);
583 int wxTreebook::DoSetSelection(size_t pagePos
)
585 wxCHECK_MSG( IS_VALID_PAGE(pagePos
), wxNOT_FOUND
,
586 wxT("invalid page index in wxListbook::SetSelection()") );
587 wxASSERT_MSG( GetPageCount() == DoInternalGetPageCount(),
588 wxT("wxTreebook logic error: m_treeIds and m_pages not in sync!"));
590 const int oldSel
= m_selection
;
591 wxTreeCtrl
*tree
= GetTreeCtrl();
593 wxTreebookEvent
event(wxEVT_COMMAND_TREEBOOK_PAGE_CHANGING
, m_windowId
);
594 event
.SetEventObject(this);
595 event
.SetSelection(pagePos
);
596 event
.SetOldSelection(m_selection
);
598 // don't send the event if the old and new pages are the same; do send it
599 // otherwise and be prepared for it to be vetoed
600 if ( (int)pagePos
== m_selection
||
601 !GetEventHandler()->ProcessEvent(event
) ||
604 // hide the previously shown page
605 wxTreebookPage
* const oldPage
= DoGetCurrentPage();
609 // then show the new one
610 m_selection
= pagePos
;
611 wxTreebookPage
*page
= wxBookCtrlBase::GetPage(m_selection
);
614 // find the next page suitable to be shown: the first (grand)child
615 // of this one with a non-NULL associated page
616 wxTreeItemId childId
= m_treeIds
[pagePos
];
617 int actualPagePos
= pagePos
;
618 while ( !page
&& childId
.IsOk() )
620 wxTreeItemIdValue cookie
;
621 childId
= tree
->GetFirstChild( childId
, cookie
);
622 if ( childId
.IsOk() )
624 page
= wxBookCtrlBase::GetPage(++actualPagePos
);
628 m_actualSelection
= page
? actualPagePos
: m_selection
;
634 tree
->SelectItem(DoInternalGetPage(pagePos
));
636 // notify about the (now completed) page change
637 event
.SetEventType(wxEVT_COMMAND_TREEBOOK_PAGE_CHANGED
);
638 (void)GetEventHandler()->ProcessEvent(event
);
640 else // page change vetoed
642 // tree selection might have already had changed
643 tree
->SelectItem(DoInternalGetPage(oldSel
));
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 wxTreeItemId newId
= event
.GetItem();
669 if ( (m_selection
== wxNOT_FOUND
&&
670 (!newId
.IsOk() || newId
== GetTreeCtrl()->GetRootItem())) ||
671 (m_selection
!= wxNOT_FOUND
&& newId
== m_treeIds
[m_selection
]) )
673 // this event can only come when we modify the tree selection ourselves
674 // so we should simply ignore it
678 int newPos
= DoInternalFindPageById(newId
);
680 if ( newPos
!= wxNOT_FOUND
)
681 SetSelection( newPos
);
684 void wxTreebook::OnTreeNodeExpandedCollapsed(wxTreeEvent
& event
)
686 wxTreeItemId nodeId
= event
.GetItem();
687 if ( !nodeId
.IsOk() || nodeId
== GetTreeCtrl()->GetRootItem() )
689 int pagePos
= DoInternalFindPageById(nodeId
);
690 wxCHECK_RET( pagePos
!= wxNOT_FOUND
, wxT("Internal problem in wxTreebook!..") );
692 wxTreebookEvent
ev(GetTreeCtrl()->IsExpanded(nodeId
)
693 ? wxEVT_COMMAND_TREEBOOK_NODE_EXPANDED
694 : wxEVT_COMMAND_TREEBOOK_NODE_COLLAPSED
,
697 ev
.SetSelection(pagePos
);
698 ev
.SetOldSelection(pagePos
);
699 ev
.SetEventObject(this);
701 GetEventHandler()->ProcessEvent(ev
);
704 // ----------------------------------------------------------------------------
705 // wxTreebook geometry management
706 // ----------------------------------------------------------------------------
708 wxTreebookPage
* wxTreebook::DoGetCurrentPage() const
710 if ( m_selection
== wxNOT_FOUND
)
713 wxTreebookPage
*page
= wxBookCtrlBase::GetPage(m_selection
);
714 if ( !page
&& m_actualSelection
!= wxNOT_FOUND
)
716 page
= wxBookCtrlBase::GetPage(m_actualSelection
);
722 #endif // wxUSE_TREEBOOK