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();
58 BEGIN_EVENT_TABLE(wxTreebook
, wxBookCtrlBase
)
59 EVT_TREE_SEL_CHANGED (wxID_ANY
, wxTreebook::OnTreeSelectionChange
)
60 EVT_TREE_ITEM_EXPANDED (wxID_ANY
, wxTreebook::OnTreeNodeExpandedCollapsed
)
61 EVT_TREE_ITEM_COLLAPSED(wxID_ANY
, wxTreebook::OnTreeNodeExpandedCollapsed
)
64 // ============================================================================
65 // wxTreebook implementation
66 // ============================================================================
68 // ----------------------------------------------------------------------------
69 // wxTreebook creation
70 // ----------------------------------------------------------------------------
72 void wxTreebook::Init()
75 m_actualSelection
= wxNOT_FOUND
;
79 wxTreebook::Create(wxWindow
*parent
,
86 // Check the style flag to have either wxTBK_RIGHT or wxTBK_LEFT
87 if ( (style
& wxBK_ALIGN_MASK
) == wxBK_DEFAULT
)
91 style
|= wxTAB_TRAVERSAL
;
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
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
);
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
);
288 wxTreeCtrl
*tree
= GetTreeCtrl();
290 size_t subCount
= tree
->GetChildrenCount(pageId
, true);
291 wxASSERT_MSG ( IS_VALID_PAGE(pagePos
+ subCount
),
292 wxT("Internal error in wxTreebook::DoRemovePage") );
294 // here we are going to delete ALL the pages in the range
295 // [pagePos, pagePos + subCount] -- the page and its children
297 // deleting all the pages from the base class
298 for ( size_t i
= 0; i
<= subCount
; ++i
)
300 wxTreebookPage
*page
= wxBookCtrlBase::DoRemovePage(pagePos
);
302 // don't delete the page itself though -- it will be deleted in
303 // DeletePage() when we return
310 DoInternalRemovePageRange(pagePos
, subCount
);
312 tree
->DeleteChildren( pageId
);
313 tree
->Delete( pageId
);
318 bool wxTreebook::DeleteAllPages()
320 wxBookCtrlBase::DeleteAllPages();
323 m_actualSelection
= wxNOT_FOUND
;
325 wxTreeCtrl
*tree
= GetTreeCtrl();
326 tree
->DeleteChildren(tree
->GetRootItem());
331 void wxTreebook::DoInternalAddPage(size_t newPos
,
332 wxTreebookPage
*page
,
335 wxASSERT_MSG( newPos
<= m_treeIds
.GetCount(), wxT("Ivalid index passed to wxTreebook::DoInternalAddPage") );
337 // hide newly inserted page initially (it will be shown when selected)
341 if ( newPos
== m_treeIds
.GetCount() )
344 m_treeIds
.Add(pageId
);
348 m_treeIds
.Insert(pageId
, newPos
);
350 if ( m_selection
!= wxNOT_FOUND
&& newPos
<= (size_t)m_selection
)
352 // selection has been moved one unit toward the end
354 if ( m_actualSelection
!= wxNOT_FOUND
)
357 else if ( m_actualSelection
!= wxNOT_FOUND
&&
358 newPos
<= (size_t)m_actualSelection
)
360 DoSetSelection(m_selection
);
365 void wxTreebook::DoInternalRemovePageRange(size_t pagePos
, size_t subCount
)
367 // Attention: this function is only for a situation when we delete a node
368 // with all its children so pagePos is the node's index and subCount is the
369 // node children count
370 wxASSERT_MSG( pagePos
+ subCount
< m_treeIds
.GetCount(),
371 wxT("Ivalid page index") );
373 wxTreeItemId pageId
= m_treeIds
[pagePos
];
375 m_treeIds
.RemoveAt(pagePos
, subCount
+ 1);
377 if ( m_selection
!= wxNOT_FOUND
)
379 if ( (size_t)m_selection
> pagePos
+ subCount
)
381 // selection is far after the deleted page, so just update the index and move on
382 m_selection
-= 1 + subCount
;
383 if ( m_actualSelection
!= wxNOT_FOUND
)
385 m_actualSelection
-= subCount
+ 1;
388 else if ( (size_t)m_selection
>= pagePos
)
390 wxTreeCtrl
*tree
= GetTreeCtrl();
392 // as selected page is going to be deleted, try to select the next
393 // sibling if exists, if not then the parent
394 wxTreeItemId nodeId
= tree
->GetNextSibling(pageId
);
396 m_selection
= wxNOT_FOUND
;
397 m_actualSelection
= wxNOT_FOUND
;
401 // selecting next siblings
402 tree
->SelectItem(nodeId
);
404 else // no next sibling, select the parent
406 wxTreeItemId parentId
= tree
->GetItemParent(pageId
);
408 if ( parentId
.IsOk() && parentId
!= tree
->GetRootItem() )
410 tree
->SelectItem(parentId
);
412 else // parent is root
414 // we can't select it as it's hidden
415 DoUpdateSelection(false, wxNOT_FOUND
);
419 else if ( m_actualSelection
!= wxNOT_FOUND
&&
420 (size_t)m_actualSelection
>= pagePos
)
422 // nothing to do -- selection is before the deleted node, but
423 // actually shown page (the first (sub)child with page != NULL) is
425 m_actualSelection
= m_selection
;
427 // send event as documented
428 DoSetSelection(m_selection
, SetSelection_SendEvent
);
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::DoSetSelection(size_t pagePos
, int flags
)
581 wxCHECK_MSG( IS_VALID_PAGE(pagePos
), wxNOT_FOUND
,
582 wxT("invalid page index in wxListbook::DoSetSelection()") );
583 wxASSERT_MSG( GetPageCount() == DoInternalGetPageCount(),
584 wxT("wxTreebook logic error: m_treeIds and m_pages not in sync!"));
586 wxTreebookEvent
event(wxEVT_COMMAND_TREEBOOK_PAGE_CHANGING
, m_windowId
);
587 const int oldSel
= m_selection
;
588 wxTreeCtrl
*tree
= GetTreeCtrl();
589 bool allowed
= false;
591 if (flags
& SetSelection_SendEvent
)
593 event
.SetEventObject(this);
594 event
.SetSelection(pagePos
);
595 event
.SetOldSelection(m_selection
);
597 // don't send the event if the old and new pages are the same; do send it
598 // otherwise and be prepared for it to be vetoed
599 allowed
= (int)pagePos
== m_selection
||
600 !GetEventHandler()->ProcessEvent(event
) ||
604 if ( !(flags
& SetSelection_SendEvent
) || allowed
)
606 // hide the previously shown page
607 wxTreebookPage
* const oldPage
= DoGetCurrentPage();
611 // then show the new one
612 m_selection
= pagePos
;
613 wxTreebookPage
*page
= wxBookCtrlBase::GetPage(m_selection
);
616 // find the next page suitable to be shown: the first (grand)child
617 // of this one with a non-NULL associated page
618 wxTreeItemId childId
= m_treeIds
[pagePos
];
619 int actualPagePos
= pagePos
;
620 while ( !page
&& childId
.IsOk() )
622 wxTreeItemIdValue cookie
;
623 childId
= tree
->GetFirstChild( childId
, cookie
);
624 if ( childId
.IsOk() )
626 page
= wxBookCtrlBase::GetPage(++actualPagePos
);
630 m_actualSelection
= page
? actualPagePos
: m_selection
;
636 tree
->SelectItem(DoInternalGetPage(pagePos
));
638 if (flags
& SetSelection_SendEvent
)
640 // notify about the (now completed) page change
641 event
.SetEventType(wxEVT_COMMAND_TREEBOOK_PAGE_CHANGED
);
642 (void)GetEventHandler()->ProcessEvent(event
);
645 else if ( (flags
& SetSelection_SendEvent
) && !allowed
) // page change vetoed
647 // tree selection might have already had changed
648 if ( oldSel
!= wxNOT_FOUND
)
649 tree
->SelectItem(DoInternalGetPage(oldSel
));
655 wxTreebookPage
*wxTreebook::DoGetCurrentPage() const
657 if ( m_selection
== wxNOT_FOUND
)
660 wxTreebookPage
*page
= wxBookCtrlBase::GetPage(m_selection
);
661 if ( !page
&& m_actualSelection
!= wxNOT_FOUND
)
663 page
= wxBookCtrlBase::GetPage(m_actualSelection
);
669 void wxTreebook::SetImageList(wxImageList
*imageList
)
671 wxBookCtrlBase::SetImageList(imageList
);
672 GetTreeCtrl()->SetImageList(imageList
);
675 void wxTreebook::AssignImageList(wxImageList
*imageList
)
677 wxBookCtrlBase::AssignImageList(imageList
);
678 GetTreeCtrl()->SetImageList(imageList
);
681 // ----------------------------------------------------------------------------
683 // ----------------------------------------------------------------------------
685 void wxTreebook::OnTreeSelectionChange(wxTreeEvent
& event
)
687 if ( event
.GetEventObject() != m_bookctrl
)
693 wxTreeItemId newId
= event
.GetItem();
695 if ( (m_selection
== wxNOT_FOUND
&&
696 (!newId
.IsOk() || newId
== GetTreeCtrl()->GetRootItem())) ||
697 (m_selection
!= wxNOT_FOUND
&& newId
== m_treeIds
[m_selection
]) )
699 // this event can only come when we modify the tree selection ourselves
700 // so we should simply ignore it
704 int newPos
= DoInternalFindPageById(newId
);
706 if ( newPos
!= wxNOT_FOUND
)
707 SetSelection( newPos
);
710 void wxTreebook::OnTreeNodeExpandedCollapsed(wxTreeEvent
& event
)
712 if ( event
.GetEventObject() != m_bookctrl
)
718 wxTreeItemId nodeId
= event
.GetItem();
719 if ( !nodeId
.IsOk() || nodeId
== GetTreeCtrl()->GetRootItem() )
721 int pagePos
= DoInternalFindPageById(nodeId
);
722 wxCHECK_RET( pagePos
!= wxNOT_FOUND
, wxT("Internal problem in wxTreebook!..") );
724 wxTreebookEvent
ev(GetTreeCtrl()->IsExpanded(nodeId
)
725 ? wxEVT_COMMAND_TREEBOOK_NODE_EXPANDED
726 : wxEVT_COMMAND_TREEBOOK_NODE_COLLAPSED
,
729 ev
.SetSelection(pagePos
);
730 ev
.SetOldSelection(pagePos
);
731 ev
.SetEventObject(this);
733 GetEventHandler()->ProcessEvent(ev
);
736 // ----------------------------------------------------------------------------
737 // wxTreebook geometry management
738 // ----------------------------------------------------------------------------
740 int wxTreebook::HitTest(wxPoint
const & pt
, long * flags
) const
742 int pagePos
= wxNOT_FOUND
;
745 *flags
= wxBK_HITTEST_NOWHERE
;
747 // convert from wxTreebook coorindates to wxTreeCtrl ones
748 const wxTreeCtrl
* const tree
= GetTreeCtrl();
749 const wxPoint treePt
= tree
->ScreenToClient(ClientToScreen(pt
));
751 // is it over the tree?
752 if ( wxRect(tree
->GetSize()).Contains(treePt
) )
755 wxTreeItemId id
= tree
->HitTest(treePt
, flagsTree
);
757 if ( id
.IsOk() && (flagsTree
& wxTREE_HITTEST_ONITEM
) )
759 pagePos
= DoInternalFindPageById(id
);
764 if ( pagePos
!= wxNOT_FOUND
)
767 if ( flagsTree
& (wxTREE_HITTEST_ONITEMBUTTON
|
768 wxTREE_HITTEST_ONITEMICON
|
769 wxTREE_HITTEST_ONITEMSTATEICON
) )
770 *flags
|= wxBK_HITTEST_ONICON
;
772 if ( flagsTree
& wxTREE_HITTEST_ONITEMLABEL
)
773 *flags
|= wxBK_HITTEST_ONLABEL
;
776 else // not over the tree
778 if ( flags
&& GetPageRect().Contains( pt
) )
779 *flags
|= wxBK_HITTEST_ONPAGE
;
785 #endif // wxUSE_TREEBOOK