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
)
63 WX_EVENT_TABLE_CONTROL_CONTAINER(wxTreebook
)
66 // ============================================================================
67 // wxTreebook implementation
68 // ============================================================================
70 WX_DELEGATE_TO_CONTROL_CONTAINER(wxTreebook
, wxControl
)
72 // ----------------------------------------------------------------------------
73 // wxTreebook creation
74 // ----------------------------------------------------------------------------
76 void wxTreebook::Init()
78 m_container
.SetContainerWindow(this);
81 m_actualSelection
= wxNOT_FOUND
;
85 wxTreebook::Create(wxWindow
*parent
,
92 // Check the style flag to have either wxTBK_RIGHT or wxTBK_LEFT
93 if ( (style
& wxBK_ALIGN_MASK
) == wxBK_DEFAULT
)
97 style
|= wxTAB_TRAVERSAL
;
99 // no border for this control, it doesn't look nice together with the tree
100 style
&= ~wxBORDER_MASK
;
101 style
|= wxBORDER_NONE
;
103 if ( !wxControl::Create(parent
, id
, pos
, size
,
104 style
, wxDefaultValidator
, name
) )
107 m_bookctrl
= new wxTreeCtrl
114 wxBORDER_SIMPLE
| // On wxMSW this produces a black border which is wrong
120 GetTreeCtrl()->SetQuickBestSize(false); // do full size calculation
121 GetTreeCtrl()->AddRoot(wxEmptyString
); // label doesn't matter, it's hidden
124 // We need to add dummy size event to force possible scrollbar hiding
126 GetEventHandler()->AddPendingEvent(evt
);
133 // insert a new page just before the pagePos
134 bool wxTreebook::InsertPage(size_t pagePos
,
136 const wxString
& text
,
140 return DoInsertPage(pagePos
, page
, text
, bSelect
, imageId
);
143 bool wxTreebook::InsertSubPage(size_t pagePos
,
145 const wxString
& text
,
149 return DoInsertSubPage(pagePos
, page
, text
, bSelect
, imageId
);
152 bool wxTreebook::AddPage(wxWindow
*page
, const wxString
& text
, bool bSelect
,
155 return DoInsertPage(m_treeIds
.GetCount(), page
, text
, bSelect
, imageId
);
158 // insertion time is linear to the number of top-pages
159 bool wxTreebook::AddSubPage(wxWindow
*page
, const wxString
& text
, bool bSelect
, int imageId
)
161 return DoAddSubPage(page
, text
, bSelect
, imageId
);
165 bool wxTreebook::DoInsertPage(size_t pagePos
,
167 const wxString
& text
,
171 wxCHECK_MSG( pagePos
<= DoInternalGetPageCount(), false,
172 wxT("Invalid treebook page position") );
174 if ( !wxBookCtrlBase::InsertPage(pagePos
, page
, text
, bSelect
, imageId
) )
177 wxTreeCtrl
*tree
= GetTreeCtrl();
179 if ( pagePos
== DoInternalGetPageCount() )
181 // append the page to the end
182 wxTreeItemId rootId
= tree
->GetRootItem();
184 newId
= tree
->AppendItem(rootId
, text
, imageId
);
186 else // insert the new page before the given one
188 wxTreeItemId nodeId
= m_treeIds
[pagePos
];
190 wxTreeItemId previousId
= tree
->GetPrevSibling(nodeId
);
191 wxTreeItemId parentId
= tree
->GetItemParent(nodeId
);
193 if ( previousId
.IsOk() )
195 // insert before the sibling - previousId
196 newId
= tree
->InsertItem(parentId
, previousId
, text
, imageId
);
198 else // no prev siblings -- insert as a first child
200 wxASSERT_MSG( parentId
.IsOk(), wxT( "Tree has no root node?" ) );
202 newId
= tree
->PrependItem(parentId
, text
, imageId
);
208 //something wrong -> cleaning and returning with false
209 (void)wxBookCtrlBase::DoRemovePage(pagePos
);
211 wxFAIL_MSG( wxT("Failed to insert treebook page") );
215 DoInternalAddPage(pagePos
, page
, newId
);
217 DoUpdateSelection(bSelect
, pagePos
);
222 bool wxTreebook::DoAddSubPage(wxWindow
*page
, const wxString
& text
, bool bSelect
, int imageId
)
224 wxTreeCtrl
*tree
= GetTreeCtrl();
226 wxTreeItemId rootId
= tree
->GetRootItem();
228 wxTreeItemId lastNodeId
= tree
->GetLastChild(rootId
);
230 wxCHECK_MSG( lastNodeId
.IsOk(), false,
231 _T("Can't insert sub page when there are no pages") );
233 // now calculate its position (should we save/update it too?)
234 size_t newPos
= tree
->GetCount() -
235 (tree
->GetChildrenCount(lastNodeId
, true) + 1);
237 return DoInsertSubPage(newPos
, page
, text
, bSelect
, imageId
);
240 bool wxTreebook::DoInsertSubPage(size_t pagePos
,
241 wxTreebookPage
*page
,
242 const wxString
& text
,
246 wxTreeItemId parentId
= DoInternalGetPage(pagePos
);
247 wxCHECK_MSG( parentId
.IsOk(), false, wxT("invalid tree item") );
249 wxTreeCtrl
*tree
= GetTreeCtrl();
251 size_t newPos
= pagePos
+ tree
->GetChildrenCount(parentId
, true) + 1;
252 wxASSERT_MSG( newPos
<= DoInternalGetPageCount(),
253 wxT("Internal error in tree insert point calculation") );
255 if ( !wxBookCtrlBase::InsertPage(newPos
, page
, text
, bSelect
, imageId
) )
258 wxTreeItemId newId
= tree
->AppendItem(parentId
, text
, imageId
);
262 (void)wxBookCtrlBase::DoRemovePage(newPos
);
264 wxFAIL_MSG( wxT("Failed to insert treebook page") );
268 DoInternalAddPage(newPos
, page
, newId
);
270 DoUpdateSelection(bSelect
, newPos
);
275 bool wxTreebook::DeletePage(size_t pagePos
)
277 wxCHECK_MSG( IS_VALID_PAGE(pagePos
), false, wxT("Invalid tree index") );
279 wxTreebookPage
*oldPage
= DoRemovePage(pagePos
);
288 wxTreebookPage
*wxTreebook::DoRemovePage(size_t pagePos
)
290 wxTreeItemId pageId
= DoInternalGetPage(pagePos
);
291 wxCHECK_MSG( pageId
.IsOk(), NULL
, wxT("Invalid tree index") );
293 wxTreebookPage
* oldPage
= GetPage(pagePos
);
294 wxTreeCtrl
*tree
= GetTreeCtrl();
296 size_t subCount
= tree
->GetChildrenCount(pageId
, true);
297 wxASSERT_MSG ( IS_VALID_PAGE(pagePos
+ subCount
),
298 wxT("Internal error in wxTreebook::DoRemovePage") );
300 // here we are going to delete ALL the pages in the range
301 // [pagePos, pagePos + subCount] -- the page and its children
303 // deleting all the pages from the base class
304 for ( size_t i
= 0; i
<= subCount
; ++i
)
306 wxTreebookPage
*page
= wxBookCtrlBase::DoRemovePage(pagePos
);
308 // don't delete the page itself though -- it will be deleted in
309 // DeletePage() when we return
316 DoInternalRemovePageRange(pagePos
, subCount
);
318 tree
->DeleteChildren( pageId
);
319 tree
->Delete( pageId
);
324 bool wxTreebook::DeleteAllPages()
326 wxBookCtrlBase::DeleteAllPages();
329 m_actualSelection
= wxNOT_FOUND
;
331 wxTreeCtrl
*tree
= GetTreeCtrl();
332 tree
->DeleteChildren(tree
->GetRootItem());
337 void wxTreebook::DoInternalAddPage(size_t newPos
,
338 wxTreebookPage
*page
,
341 wxASSERT_MSG( newPos
<= m_treeIds
.GetCount(), wxT("Ivalid index passed to wxTreebook::DoInternalAddPage") );
343 // hide newly inserted page initially (it will be shown when selected)
347 if ( newPos
== m_treeIds
.GetCount() )
350 m_treeIds
.Add(pageId
);
354 m_treeIds
.Insert(pageId
, newPos
);
356 if ( m_selection
!= wxNOT_FOUND
&& newPos
<= (size_t)m_selection
)
358 // selection has been moved one unit toward the end
360 if ( m_actualSelection
!= wxNOT_FOUND
)
363 else if ( m_actualSelection
!= wxNOT_FOUND
&&
364 newPos
<= (size_t)m_actualSelection
)
366 DoSetSelection(m_selection
);
371 void wxTreebook::DoInternalRemovePageRange(size_t pagePos
, size_t subCount
)
373 // Attention: this function is only for a situation when we delete a node
374 // with all its children so pagePos is the node's index and subCount is the
375 // node children count
376 wxASSERT_MSG( pagePos
+ subCount
< m_treeIds
.GetCount(),
377 wxT("Ivalid page index") );
379 wxTreeItemId pageId
= m_treeIds
[pagePos
];
381 m_treeIds
.RemoveAt(pagePos
, subCount
+ 1);
383 if ( m_selection
!= wxNOT_FOUND
)
385 if ( (size_t)m_selection
> pagePos
+ subCount
)
387 // selection is far after the deleted page, so just update the index and move on
388 m_selection
-= 1 + subCount
;
389 if ( m_actualSelection
!= wxNOT_FOUND
)
391 m_actualSelection
-= subCount
+ 1;
394 else if ( (size_t)m_selection
>= pagePos
)
396 wxTreeCtrl
*tree
= GetTreeCtrl();
398 // as selected page is going to be deleted, try to select the next
399 // sibling if exists, if not then the parent
400 wxTreeItemId nodeId
= tree
->GetNextSibling(pageId
);
402 m_selection
= wxNOT_FOUND
;
403 m_actualSelection
= wxNOT_FOUND
;
407 // selecting next siblings
408 tree
->SelectItem(nodeId
);
410 else // no next sibling, select the parent
412 wxTreeItemId parentId
= tree
->GetItemParent(pageId
);
414 if ( parentId
.IsOk() && parentId
!= tree
->GetRootItem() )
416 tree
->SelectItem(parentId
);
418 else // parent is root
420 // we can't select it as it's hidden
421 DoUpdateSelection(false, wxNOT_FOUND
);
425 else if ( m_actualSelection
!= wxNOT_FOUND
&&
426 (size_t)m_actualSelection
>= pagePos
)
428 // nothing to do -- selection is before the deleted node, but
429 // actually shown page (the first (sub)child with page != NULL) is
431 m_actualSelection
= m_selection
;
433 // send event as documented
434 DoSetSelection(m_selection
, SetSelection_SendEvent
);
436 //else: nothing to do -- selection is before the deleted node
440 DoUpdateSelection(false, wxNOT_FOUND
);
445 void wxTreebook::DoUpdateSelection(bool bSelect
, int newPos
)
452 else if ( m_selection
== wxNOT_FOUND
&& DoInternalGetPageCount() > 0 )
458 newSelPos
= wxNOT_FOUND
;
461 if ( newSelPos
!= wxNOT_FOUND
)
463 SetSelection((size_t)newSelPos
);
467 wxTreeItemId
wxTreebook::DoInternalGetPage(size_t pagePos
) const
469 if ( pagePos
>= m_treeIds
.GetCount() )
471 // invalid position but ok here, in this internal function, don't assert
472 // (the caller will do it)
473 return wxTreeItemId();
476 return m_treeIds
[pagePos
];
479 int wxTreebook::DoInternalFindPageById(wxTreeItemId pageId
) const
481 const size_t count
= m_treeIds
.GetCount();
482 for ( size_t i
= 0; i
< count
; ++i
)
484 if ( m_treeIds
[i
] == pageId
)
491 bool wxTreebook::IsNodeExpanded(size_t pagePos
) const
493 wxTreeItemId pageId
= DoInternalGetPage(pagePos
);
495 wxCHECK_MSG( pageId
.IsOk(), false, wxT("invalid tree item") );
497 return GetTreeCtrl()->IsExpanded(pageId
);
500 bool wxTreebook::ExpandNode(size_t pagePos
, bool expand
)
502 wxTreeItemId pageId
= DoInternalGetPage(pagePos
);
504 wxCHECK_MSG( pageId
.IsOk(), false, wxT("invalid tree item") );
508 GetTreeCtrl()->Expand( pageId
);
512 GetTreeCtrl()->Collapse( pageId
);
514 // rely on the events generated by wxTreeCtrl to update selection
520 int wxTreebook::GetPageParent(size_t pagePos
) const
522 wxTreeItemId nodeId
= DoInternalGetPage( pagePos
);
523 wxCHECK_MSG( nodeId
.IsOk(), wxNOT_FOUND
, wxT("Invalid page index spacified!") );
525 const wxTreeItemId parent
= GetTreeCtrl()->GetItemParent( nodeId
);
527 return parent
.IsOk() ? DoInternalFindPageById(parent
) : wxNOT_FOUND
;
530 bool wxTreebook::SetPageText(size_t n
, const wxString
& strText
)
532 wxTreeItemId pageId
= DoInternalGetPage(n
);
534 wxCHECK_MSG( pageId
.IsOk(), false, wxT("invalid tree item") );
536 GetTreeCtrl()->SetItemText(pageId
, strText
);
541 wxString
wxTreebook::GetPageText(size_t n
) const
543 wxTreeItemId pageId
= DoInternalGetPage(n
);
545 wxCHECK_MSG( pageId
.IsOk(), wxString(), wxT("invalid tree item") );
547 return GetTreeCtrl()->GetItemText(pageId
);
550 int wxTreebook::GetPageImage(size_t n
) const
552 wxTreeItemId pageId
= DoInternalGetPage(n
);
554 wxCHECK_MSG( pageId
.IsOk(), wxNOT_FOUND
, wxT("invalid tree item") );
556 return GetTreeCtrl()->GetItemImage(pageId
);
559 bool wxTreebook::SetPageImage(size_t n
, int imageId
)
561 wxTreeItemId pageId
= DoInternalGetPage(n
);
563 wxCHECK_MSG( pageId
.IsOk(), false, wxT("invalid tree item") );
565 GetTreeCtrl()->SetItemImage(pageId
, imageId
);
570 wxSize
wxTreebook::CalcSizeFromPage(const wxSize
& sizePage
) const
572 const wxSize sizeTree
= GetControllerSize();
574 wxSize size
= sizePage
;
575 size
.x
+= sizeTree
.x
;
580 int wxTreebook::GetSelection() const
585 int wxTreebook::DoSetSelection(size_t pagePos
, int flags
)
587 wxCHECK_MSG( IS_VALID_PAGE(pagePos
), wxNOT_FOUND
,
588 wxT("invalid page index in wxListbook::DoSetSelection()") );
589 wxASSERT_MSG( GetPageCount() == DoInternalGetPageCount(),
590 wxT("wxTreebook logic error: m_treeIds and m_pages not in sync!"));
592 wxTreebookEvent
event(wxEVT_COMMAND_TREEBOOK_PAGE_CHANGING
, m_windowId
);
593 const int oldSel
= m_selection
;
594 wxTreeCtrl
*tree
= GetTreeCtrl();
595 bool allowed
= false;
597 if (flags
& SetSelection_SendEvent
)
599 event
.SetEventObject(this);
600 event
.SetSelection(pagePos
);
601 event
.SetOldSelection(m_selection
);
603 // don't send the event if the old and new pages are the same; do send it
604 // otherwise and be prepared for it to be vetoed
605 allowed
= (int)pagePos
== m_selection
||
606 !GetEventHandler()->ProcessEvent(event
) ||
610 if ( !(flags
& SetSelection_SendEvent
) || allowed
)
612 // hide the previously shown page
613 wxTreebookPage
* const oldPage
= DoGetCurrentPage();
617 // then show the new one
618 m_selection
= pagePos
;
619 wxTreebookPage
*page
= wxBookCtrlBase::GetPage(m_selection
);
622 // find the next page suitable to be shown: the first (grand)child
623 // of this one with a non-NULL associated page
624 wxTreeItemId childId
= m_treeIds
[pagePos
];
625 int actualPagePos
= pagePos
;
626 while ( !page
&& childId
.IsOk() )
628 wxTreeItemIdValue cookie
;
629 childId
= tree
->GetFirstChild( childId
, cookie
);
630 if ( childId
.IsOk() )
632 page
= wxBookCtrlBase::GetPage(++actualPagePos
);
636 m_actualSelection
= page
? actualPagePos
: m_selection
;
642 tree
->SelectItem(DoInternalGetPage(pagePos
));
644 if (flags
& SetSelection_SendEvent
)
646 // notify about the (now completed) page change
647 event
.SetEventType(wxEVT_COMMAND_TREEBOOK_PAGE_CHANGED
);
648 (void)GetEventHandler()->ProcessEvent(event
);
651 else if ( (flags
& SetSelection_SendEvent
) && !allowed
) // page change vetoed
653 // tree selection might have already had changed
654 if ( oldSel
!= wxNOT_FOUND
)
655 tree
->SelectItem(DoInternalGetPage(oldSel
));
661 wxTreebookPage
*wxTreebook::DoGetCurrentPage() const
663 if ( m_selection
== wxNOT_FOUND
)
666 wxTreebookPage
*page
= wxBookCtrlBase::GetPage(m_selection
);
667 if ( !page
&& m_actualSelection
!= wxNOT_FOUND
)
669 page
= wxBookCtrlBase::GetPage(m_actualSelection
);
675 void wxTreebook::SetImageList(wxImageList
*imageList
)
677 wxBookCtrlBase::SetImageList(imageList
);
678 GetTreeCtrl()->SetImageList(imageList
);
681 void wxTreebook::AssignImageList(wxImageList
*imageList
)
683 wxBookCtrlBase::AssignImageList(imageList
);
684 GetTreeCtrl()->SetImageList(imageList
);
687 // ----------------------------------------------------------------------------
689 // ----------------------------------------------------------------------------
691 void wxTreebook::OnTreeSelectionChange(wxTreeEvent
& event
)
693 if ( event
.GetEventObject() != m_bookctrl
)
699 wxTreeItemId newId
= event
.GetItem();
701 if ( (m_selection
== wxNOT_FOUND
&&
702 (!newId
.IsOk() || newId
== GetTreeCtrl()->GetRootItem())) ||
703 (m_selection
!= wxNOT_FOUND
&& newId
== m_treeIds
[m_selection
]) )
705 // this event can only come when we modify the tree selection ourselves
706 // so we should simply ignore it
710 int newPos
= DoInternalFindPageById(newId
);
712 if ( newPos
!= wxNOT_FOUND
)
713 SetSelection( newPos
);
716 void wxTreebook::OnTreeNodeExpandedCollapsed(wxTreeEvent
& event
)
718 if ( event
.GetEventObject() != m_bookctrl
)
724 wxTreeItemId nodeId
= event
.GetItem();
725 if ( !nodeId
.IsOk() || nodeId
== GetTreeCtrl()->GetRootItem() )
727 int pagePos
= DoInternalFindPageById(nodeId
);
728 wxCHECK_RET( pagePos
!= wxNOT_FOUND
, wxT("Internal problem in wxTreebook!..") );
730 wxTreebookEvent
ev(GetTreeCtrl()->IsExpanded(nodeId
)
731 ? wxEVT_COMMAND_TREEBOOK_NODE_EXPANDED
732 : wxEVT_COMMAND_TREEBOOK_NODE_COLLAPSED
,
735 ev
.SetSelection(pagePos
);
736 ev
.SetOldSelection(pagePos
);
737 ev
.SetEventObject(this);
739 GetEventHandler()->ProcessEvent(ev
);
742 // ----------------------------------------------------------------------------
743 // wxTreebook geometry management
744 // ----------------------------------------------------------------------------
746 int wxTreebook::HitTest(wxPoint
const & pt
, long * flags
) const
748 int pagePos
= wxNOT_FOUND
;
751 *flags
= wxBK_HITTEST_NOWHERE
;
753 // convert from wxTreebook coorindates to wxTreeCtrl ones
754 const wxTreeCtrl
* const tree
= GetTreeCtrl();
755 const wxPoint treePt
= tree
->ScreenToClient(ClientToScreen(pt
));
757 // is it over the tree?
758 if ( wxRect(tree
->GetSize()).Contains(treePt
) )
761 wxTreeItemId id
= tree
->HitTest(treePt
, flagsTree
);
763 if ( id
.IsOk() && (flagsTree
& wxTREE_HITTEST_ONITEM
) )
765 pagePos
= DoInternalFindPageById(id
);
770 if ( pagePos
!= wxNOT_FOUND
)
773 if ( flagsTree
& (wxTREE_HITTEST_ONITEMBUTTON
|
774 wxTREE_HITTEST_ONITEMICON
|
775 wxTREE_HITTEST_ONITEMSTATEICON
) )
776 *flags
|= wxBK_HITTEST_ONICON
;
778 if ( flagsTree
& wxTREE_HITTEST_ONITEMLABEL
)
779 *flags
|= wxBK_HITTEST_ONLABEL
;
782 else // not over the tree
784 if ( flags
&& GetPageRect().Contains( pt
) )
785 *flags
|= wxBK_HITTEST_ONPAGE
;
791 #endif // wxUSE_TREEBOOK