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
, wxControl
)
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_SIZE(wxTreebook::OnSize
)
55 EVT_TREE_SEL_CHANGED (wxID_TREEBOOKTREEVIEW
, wxTreebook::OnTreeSelectionChange
)
56 EVT_TREE_ITEM_EXPANDED (wxID_TREEBOOKTREEVIEW
, wxTreebook::OnTreeNodeExpandedCollapsed
)
57 EVT_TREE_ITEM_COLLAPSED(wxID_TREEBOOKTREEVIEW
, wxTreebook::OnTreeNodeExpandedCollapsed
)
60 // ============================================================================
61 // wxTreebook implementation
62 // ============================================================================
64 // ----------------------------------------------------------------------------
65 // wxTreebook creation
66 // ----------------------------------------------------------------------------
68 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
& wxTBK_RIGHT
)
86 wxASSERT_MSG( !(style
& wxTBK_LEFT
),
87 _T("RIGHT and LEFT can't be used together") );
94 // no border for this control, it doesn't look nice together with the tree
95 style
&= ~wxBORDER_MASK
;
96 style
|= wxBORDER_NONE
;
98 if ( !wxControl::Create(parent
, id
, pos
, size
,
99 style
, wxDefaultValidator
, name
) )
102 m_tree
= new wxTreeCtrl
105 wxID_TREEBOOKTREEVIEW
,
114 m_tree
->AddRoot(wxEmptyString
); // label doesn't matter, it's hidden
117 // see listbook.h for origins of that
118 // On XP with themes enabled the GetViewRect used in GetListSize to
119 // determine the space needed for the list view will incorrectly return
120 // (0,0,0,0) the first time. So send a pending event so OnSize will be
121 // called again after the window is ready to go. Technically we don't
122 // need to do this on non-XP windows, but if things are already sized
123 // correctly then nothing changes and so there is no harm.
125 GetEventHandler()->AddPendingEvent(evt
);
132 // insert a new page just before the pagePos
133 bool wxTreebook::InsertPage(size_t pagePos
,
135 const wxString
& text
,
139 return DoInsertPage(pagePos
, page
, text
, bSelect
, imageId
);
142 bool wxTreebook::AddSubPage(size_t pagePos
,
144 const wxString
& text
,
148 return DoInsertSubPage(pagePos
, page
, text
, bSelect
, imageId
);
151 bool wxTreebook::AddPage(wxWindow
*page
, const wxString
& text
, bool bSelect
,
154 return DoInsertPage(m_treeIds
.GetCount(), page
, text
, bSelect
, imageId
);
157 // insertion time is linear to the number of top-pages
158 bool wxTreebook::AddSubPage(wxWindow
*page
, const wxString
& text
, bool bSelect
, int imageId
)
160 return DoAddSubPage(page
, text
, bSelect
, imageId
);
164 bool wxTreebook::DoInsertPage(size_t pagePos
,
166 const wxString
& text
,
170 wxCHECK_MSG( pagePos
<= DoInternalGetPageCount(), false,
171 wxT("Invalid treebook page position") );
173 if ( !wxBookCtrlBase::InsertPage(pagePos
, page
, text
, bSelect
, imageId
) )
177 if ( pagePos
== DoInternalGetPageCount() )
179 // append the page to the end
180 wxTreeItemId rootId
= m_tree
->GetRootItem();
182 newId
= m_tree
->AppendItem(rootId
, text
, imageId
);
184 else // insert the new page before the given one
186 wxTreeItemId nodeId
= m_treeIds
[pagePos
];
188 wxTreeItemId previousId
= m_tree
->GetPrevSibling(nodeId
);
189 wxTreeItemId parentId
= m_tree
->GetItemParent(nodeId
);
191 if ( previousId
.IsOk() )
193 // insert before the sibling - previousId
194 newId
= m_tree
->InsertItem(parentId
, previousId
, text
, imageId
);
196 else // no prev siblings -- insert as a first child
198 wxASSERT_MSG( parentId
.IsOk(), wxT( "Tree has no root node?" ) );
200 newId
= m_tree
->PrependItem(parentId
, text
, imageId
);
206 //something wrong -> cleaning and returning with false
207 (void)wxBookCtrlBase::DoRemovePage(pagePos
);
209 wxFAIL_MSG( wxT("Failed to insert treebook page") );
213 DoInternalAddPage(pagePos
, page
, newId
);
215 DoUpdateSelection(bSelect
, pagePos
);
220 bool wxTreebook::DoAddSubPage(wxWindow
*page
, const wxString
& text
, bool bSelect
, int imageId
)
222 wxTreeItemId rootId
= m_tree
->GetRootItem();
224 wxTreeItemId lastNodeId
= m_tree
->GetLastChild(rootId
);
226 wxCHECK_MSG( lastNodeId
.IsOk(), false,
227 _T("Can't insert sub page when there are no pages") );
229 // now calculate its position (should we save/update it too?)
230 size_t newPos
= m_tree
->GetCount() -
231 (m_tree
->GetChildrenCount(lastNodeId
, true) + 1);
233 return DoInsertSubPage(newPos
, page
, text
, bSelect
, imageId
);
236 bool wxTreebook::DoInsertSubPage(size_t pagePos
,
237 wxTreebookPage
*page
,
238 const wxString
& text
,
242 wxTreeItemId parentId
= DoInternalGetPage(pagePos
);
243 wxCHECK_MSG( parentId
.IsOk(), false, wxT("invalid tree item") );
245 size_t newPos
= pagePos
+ m_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
= m_tree
->AppendItem(parentId
, text
, imageId
);
256 (void)wxBookCtrlBase::DoRemovePage(newPos
);
258 wxFAIL_MSG( wxT("Failed to insert treebook page") );
262 DoInternalAddPage(newPos
, page
, newId
);
264 DoUpdateSelection(bSelect
, newPos
);
269 bool wxTreebook::DeletePage(size_t pagePos
)
271 wxCHECK_MSG( IS_VALID_PAGE(pagePos
), false, wxT("Invalid tree index") );
273 wxTreebookPage
*oldPage
= DoRemovePage(pagePos
);
282 wxTreebookPage
*wxTreebook::DoRemovePage(size_t pagePos
)
284 wxTreeItemId pageId
= DoInternalGetPage(pagePos
);
285 wxCHECK_MSG( pageId
.IsOk(), NULL
, wxT("Invalid tree index") );
287 wxTreebookPage
* oldPage
= GetPage(pagePos
);
289 size_t subCount
= m_tree
->GetChildrenCount(pageId
, true);
290 wxASSERT_MSG ( IS_VALID_PAGE(pagePos
+ subCount
),
291 wxT("Internal error in wxTreebook::DoRemovePage") );
293 // here we are going to delete ALL the pages in the range
294 // [pagePos, pagePos + subCount] -- the page and its children
296 // deleting all the pages from the base class
297 for ( size_t i
= 0; i
<= subCount
; ++i
)
299 wxTreebookPage
*page
= wxBookCtrlBase::DoRemovePage(pagePos
);
301 // don't delete the page itself though -- it will be deleted in
302 // DeletePage() when we return
309 DoInternalRemovePageRange(pagePos
, subCount
);
311 m_tree
->DeleteChildren( pageId
);
312 m_tree
->Delete( pageId
);
317 bool wxTreebook::DeleteAllPages()
319 wxBookCtrlBase::DeleteAllPages();
322 m_actualSelection
= wxNOT_FOUND
;
324 m_tree
->DeleteChildren(m_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 // 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
= m_tree
->GetNextSibling(pageId
);
392 m_selection
= wxNOT_FOUND
;
393 m_actualSelection
= wxNOT_FOUND
;
397 // selecting next siblings
398 m_tree
->SelectItem(nodeId
);
400 else // no next sibling, select the parent
402 wxTreeItemId parentId
= m_tree
->GetItemParent(pageId
);
404 if ( parentId
.IsOk() && parentId
!= m_tree
->GetRootItem() )
406 m_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 m_tree
->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 m_tree
->Expand( pageId
);
500 m_tree
->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
= m_tree
->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 m_tree
->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 m_tree
->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 m_tree
->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 m_tree
->SetItemImage(pageId
, imageId
);
558 wxSize
wxTreebook::CalcSizeFromPage(const wxSize
& sizePage
) const
560 const wxSize sizeTree
= GetTreeSize();
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
;
590 wxTreebookEvent
event(wxEVT_COMMAND_TREEBOOK_PAGE_CHANGING
, m_windowId
);
591 event
.SetEventObject(this);
592 event
.SetSelection(pagePos
);
593 event
.SetOldSelection(m_selection
);
595 // don't send the event if the old and new pages are the same; do send it
596 // otherwise and be prepared for it to be vetoed
597 if ( (int)pagePos
== m_selection
||
598 !GetEventHandler()->ProcessEvent(event
) ||
601 // hide the previously shown page
602 wxTreebookPage
* const oldPage
= DoGetCurrentPage();
606 // then show the new one
607 m_selection
= pagePos
;
608 wxTreebookPage
*page
= wxBookCtrlBase::GetPage(m_selection
);
611 // find the next page suitable to be shown: the first (grand)child
612 // of this one with a non-NULL associated page
613 wxTreeItemId childId
= m_treeIds
[pagePos
];
614 m_actualSelection
= pagePos
;
615 while ( !page
&& childId
.IsOk() )
617 wxTreeItemIdValue cookie
;
618 childId
= m_tree
->GetFirstChild( childId
, cookie
);
619 if ( childId
.IsOk() )
621 page
= wxBookCtrlBase::GetPage(++m_actualSelection
);
625 wxASSERT_MSG( page
, wxT("no page to show found!") );
630 page
->SetSize(GetPageRect());
634 m_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 m_tree
->SelectItem(DoInternalGetPage(oldSel
));
649 void wxTreebook::SetImageList(wxImageList
*imageList
)
651 wxBookCtrlBase::SetImageList(imageList
);
652 m_tree
->SetImageList(imageList
);
655 void wxTreebook::AssignImageList(wxImageList
*imageList
)
657 wxBookCtrlBase::AssignImageList(imageList
);
658 m_tree
->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
== m_tree
->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
== m_tree
->GetRootItem() )
689 int pagePos
= DoInternalFindPageById(nodeId
);
690 wxCHECK_RET( pagePos
!= wxNOT_FOUND
, wxT("Internal problem in wxTreebook!..") );
692 wxTreebookEvent
ev(m_tree
->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 wxSize
wxTreebook::GetTreeSize() const
710 const wxSize sizeClient
= GetClientSize(),
711 sizeBorder
= m_tree
->GetSize() - m_tree
->GetClientSize(),
712 sizeTree
= m_tree
->GetBestSize() + sizeBorder
;
717 size
.y
= sizeClient
.y
;
722 wxRect
wxTreebook::GetPageRect() const
724 const wxSize sizeTree
= m_tree
->GetSize();
727 wxRect
rectPage(pt
, GetClientSize());
728 switch ( GetWindowStyle() & wxTBK_ALIGN_MASK
)
731 wxFAIL_MSG( _T("unexpected wxTreebook alignment") );
735 rectPage
.x
= sizeTree
.x
; // + MARGIN;
739 rectPage
.width
-= sizeTree
.x
; // + MARGIN;
746 void wxTreebook::OnSize(wxSizeEvent
& event
)
752 // we're not fully created yet
756 // resize the list control and the page area to fit inside our new size
757 const wxSize sizeClient
= GetClientSize(),
758 sizeBorder
= m_tree
->GetSize() - m_tree
->GetClientSize(),
759 sizeTree
= GetTreeSize();
761 m_tree
->SetClientSize( sizeTree
.x
- sizeBorder
.x
, sizeTree
.y
- sizeBorder
.y
);
763 const wxSize sizeNew
= m_tree
->GetSize();
765 switch ( GetWindowStyle() & wxTBK_ALIGN_MASK
)
768 wxFAIL_MSG( _T("unexpected wxTreebook alignment") );
772 // posTree is already ok
776 posTree
.x
= sizeClient
.x
- sizeNew
.x
;
780 if ( m_tree
->GetPosition() != posTree
)
781 m_tree
->Move(posTree
);
783 // resize the currently shown page
784 wxTreebookPage
*page
= DoGetCurrentPage();
787 wxRect rectPage
= GetPageRect();
788 page
->SetSize(rectPage
);
792 wxTreebookPage
* wxTreebook::DoGetCurrentPage() const
794 if ( m_selection
== wxNOT_FOUND
)
797 wxTreebookPage
*page
= wxBookCtrlBase::GetPage(m_selection
);
798 if ( !page
&& m_actualSelection
!= wxNOT_FOUND
)
800 page
= wxBookCtrlBase::GetPage(m_actualSelection
);
806 #endif // wxUSE_TREEBOOK