1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxTreebook: wxNotebook-like control presenting pages in a tree
4 // Author: Evgeniy Tarassov, Vadim Zeitlin
8 // Copyright: (c) 2005 Vadim Zeitlin <vadim@wxwidgets.org>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_TREEBOOK_H_
13 #define _WX_TREEBOOK_H_
19 #include "wx/bookctrl.h"
20 #include "wx/treectrl.h" // for wxArrayTreeItemIds
21 #include "wx/containr.h"
23 typedef wxWindow wxTreebookPage
;
25 class WXDLLEXPORT wxTreeEvent
;
27 // ----------------------------------------------------------------------------
29 // ----------------------------------------------------------------------------
31 class WXDLLEXPORT wxTreebook
: public wxBookCtrlBase
34 // Constructors and such
35 // ---------------------
37 // Default ctor doesn't create the control, use Create() afterwards
43 // This ctor creates the tree book control
44 wxTreebook(wxWindow
*parent
,
46 const wxPoint
& pos
= wxDefaultPosition
,
47 const wxSize
& size
= wxDefaultSize
,
48 long style
= wxBK_DEFAULT
,
49 const wxString
& name
= wxEmptyString
)
53 (void)Create(parent
, id
, pos
, size
, style
, name
);
56 // Really creates the control
57 bool Create(wxWindow
*parent
,
59 const wxPoint
& pos
= wxDefaultPosition
,
60 const wxSize
& size
= wxDefaultSize
,
61 long style
= wxBK_DEFAULT
,
62 const wxString
& name
= wxEmptyString
);
65 // Page insertion operations
66 // -------------------------
68 // Notice that page pointer may be NULL in which case the next non NULL
69 // page (usually the first child page of a node) is shown when this page is
72 // Inserts a new page just before the page indicated by page.
73 // The new page is placed on the same level as page.
74 virtual bool InsertPage(size_t pos
,
78 int imageId
= wxNOT_FOUND
);
80 // Inserts a new sub-page to the end of children of the page at given pos.
81 virtual bool InsertSubPage(size_t pos
,
85 int imageId
= wxNOT_FOUND
);
87 // Adds a new page at top level after all other pages.
88 virtual bool AddPage(wxWindow
*page
,
91 int imageId
= wxNOT_FOUND
);
93 // Adds a new child-page to the last top-level page inserted.
94 // Useful when constructing 1 level tree structure.
95 virtual bool AddSubPage(wxWindow
*page
,
98 int imageId
= wxNOT_FOUND
);
100 // Deletes the page and ALL its children. Could trigger page selection
101 // change in a case when selected page is removed. In that case its parent
102 // is selected (or the next page if no parent).
103 virtual bool DeletePage(size_t pos
);
109 // Gets the page node state -- node is expanded or collapsed
110 virtual bool IsNodeExpanded(size_t pos
) const;
112 // Expands or collapses the page node. Returns the previous state.
113 // May generate page changing events (if selected page
114 // is under the collapsed branch, then parent is autoselected).
115 virtual bool ExpandNode(size_t pos
, bool expand
= true);
117 // shortcut for ExpandNode(pos, false)
118 bool CollapseNode(size_t pos
) { return ExpandNode(pos
, false); }
120 // get the parent page or wxNOT_FOUND if this is a top level page
121 int GetPageParent(size_t pos
) const;
123 // the tree control we use for showing the pages index tree
124 wxTreeCtrl
* GetTreeCtrl() const { return (wxTreeCtrl
*)m_bookctrl
; }
127 // Standard operations inherited from wxBookCtrlBase
128 // -------------------------------------------------
130 virtual int GetSelection() const;
131 virtual bool SetPageText(size_t n
, const wxString
& strText
);
132 virtual wxString
GetPageText(size_t n
) const;
133 virtual int GetPageImage(size_t n
) const;
134 virtual bool SetPageImage(size_t n
, int imageId
);
135 virtual wxSize
CalcSizeFromPage(const wxSize
& sizePage
) const;
136 virtual int SetSelection(size_t n
);
137 virtual void SetImageList(wxImageList
*imageList
);
138 virtual void AssignImageList(wxImageList
*imageList
);
139 virtual bool DeleteAllPages();
142 // Implementation of a page removal. See DeletPage for comments.
143 wxTreebookPage
*DoRemovePage(size_t pos
);
145 // This subclass of wxBookCtrlBase accepts NULL page pointers (empty pages)
146 virtual bool AllowNullPage() const { return true; }
148 // return the page corresponding to the tab at the specified position
149 virtual int HitTest(const wxPoint
& pt
, long *flags
= NULL
) const;
152 void OnTreeSelectionChange(wxTreeEvent
& event
);
153 void OnTreeNodeExpandedCollapsed(wxTreeEvent
& event
);
155 // array of page ids and page windows
156 wxArrayTreeItemIds m_treeIds
;
158 // the currently selected page or wxNOT_FOUND if none
161 // in the situation when m_selection page is not wxNOT_FOUND but page is
162 // NULL this is the first (sub)child that has a non-NULL page
163 int m_actualSelection
;
166 // common part of all constructors
169 // The real implementations of page insertion functions
170 // ------------------------------------------------------
171 // All DoInsert/Add(Sub)Page functions add the page into :
173 // - the tree control
174 // - update the index/TreeItemId corespondance array
175 bool DoInsertPage(size_t pos
,
177 const wxString
& text
,
178 bool bSelect
= false,
179 int imageId
= wxNOT_FOUND
);
180 bool DoInsertSubPage(size_t pos
,
182 const wxString
& text
,
183 bool bSelect
= false,
184 int imageId
= wxNOT_FOUND
);
185 bool DoAddSubPage(wxWindow
*page
,
186 const wxString
& text
,
187 bool bSelect
= false,
188 int imageId
= wxNOT_FOUND
);
190 // Sets selection in the tree control and updates the page being shown.
191 int DoSetSelection(size_t pos
);
193 // Returns currently shown page. In a case when selected the node
194 // has empty (NULL) page finds first (sub)child with not-empty page.
195 wxTreebookPage
*DoGetCurrentPage() const;
197 // Does the selection update. Called from page insertion functions
198 // to update selection if the selected page was pushed by the newly inserted
199 void DoUpdateSelection(bool bSelect
, int page
);
202 // Operations on the internal private members of the class
203 // -------------------------------------------------------
204 // Returns the page TreeItemId for the page.
205 // Or, if the page index is incorrect, a fake one (fakePage.IsOk() == false)
206 wxTreeItemId
DoInternalGetPage(size_t pos
) const;
208 // Linear search for a page with the id specified. If no page
209 // found wxNOT_FOUND is returned. The function is used when we catch an event
210 // from m_tree (wxTreeCtrl) component.
211 int DoInternalFindPageById(wxTreeItemId page
) const;
213 // Updates page and wxTreeItemId correspondance.
214 void DoInternalAddPage(size_t newPos
, wxWindow
*page
, wxTreeItemId pageId
);
216 // Removes the page from internal structure.
217 void DoInternalRemovePage(size_t pos
)
218 { DoInternalRemovePageRange(pos
, 0); }
220 // Removes the page and all its children designated by subCount
221 // from internal structures of the control.
222 void DoInternalRemovePageRange(size_t pos
, size_t subCount
);
224 // Returns internal number of pages which can be different from
225 // GetPageCount() while performing a page insertion or removal.
226 size_t DoInternalGetPageCount() const { return m_treeIds
.Count(); }
229 DECLARE_EVENT_TABLE()
230 DECLARE_DYNAMIC_CLASS_NO_COPY(wxTreebook
)
231 WX_DECLARE_CONTROL_CONTAINER();
235 // ----------------------------------------------------------------------------
236 // treebook event class and related stuff
237 // ----------------------------------------------------------------------------
239 class WXDLLEXPORT wxTreebookEvent
: public wxBookCtrlBaseEvent
242 wxTreebookEvent(wxEventType commandType
= wxEVT_NULL
, int id
= 0,
243 int nSel
= wxNOT_FOUND
, int nOldSel
= wxNOT_FOUND
)
244 : wxBookCtrlBaseEvent(commandType
, id
, nSel
, nOldSel
)
248 wxTreebookEvent(const wxTreebookEvent
& event
)
249 : wxBookCtrlBaseEvent(event
)
253 virtual wxEvent
*Clone() const { return new wxTreebookEvent(*this); }
256 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxTreebookEvent
)
259 extern WXDLLIMPEXP_CORE
const wxEventType wxEVT_COMMAND_TREEBOOK_PAGE_CHANGED
;
260 extern WXDLLIMPEXP_CORE
const wxEventType wxEVT_COMMAND_TREEBOOK_PAGE_CHANGING
;
261 extern WXDLLIMPEXP_CORE
const wxEventType wxEVT_COMMAND_TREEBOOK_NODE_COLLAPSED
;
262 extern WXDLLIMPEXP_CORE
const wxEventType wxEVT_COMMAND_TREEBOOK_NODE_EXPANDED
;
264 typedef void (wxEvtHandler::*wxTreebookEventFunction
)(wxTreebookEvent
&);
266 #define wxTreebookEventHandler(func) \
267 (wxObjectEventFunction)(wxEventFunction)wxStaticCastEvent(wxTreebookEventFunction, &func)
269 #define EVT_TREEBOOK_PAGE_CHANGED(winid, fn) \
270 wx__DECLARE_EVT1(wxEVT_COMMAND_TREEBOOK_PAGE_CHANGED, winid, wxTreebookEventHandler(fn))
272 #define EVT_TREEBOOK_PAGE_CHANGING(winid, fn) \
273 wx__DECLARE_EVT1(wxEVT_COMMAND_TREEBOOK_PAGE_CHANGING, winid, wxTreebookEventHandler(fn))
275 #define EVT_TREEBOOK_NODE_COLLAPSED(winid, fn) \
276 wx__DECLARE_EVT1(wxEVT_COMMAND_TREEBOOK_NODE_COLLAPSED, winid, wxTreebookEventHandler(fn))
278 #define EVT_TREEBOOK_NODE_EXPANDED(winid, fn) \
279 wx__DECLARE_EVT1(wxEVT_COMMAND_TREEBOOK_NODE_EXPANDED, winid, wxTreebookEventHandler(fn))
282 #endif // wxUSE_TREEBOOK
284 #endif // _WX_TREEBOOK_H_