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
22 typedef wxWindow wxTreebookPage
;
24 class WXDLLEXPORT wxTreeEvent
;
26 // ----------------------------------------------------------------------------
28 // ----------------------------------------------------------------------------
30 // This is a set of synonyms of wxNB_XXX, which still could be used directly
31 // for styling the control. Defined for consistency with wxListbook and
33 #define wxTBK_LEFT wxNB_LEFT
34 #define wxTBK_RIGHT wxNB_RIGHT
36 // we don't support TOP/BOTTOM orientations but still define the flags (again,
37 // for consistency with others)
38 #define wxTBK_TOP wxTBK_LEFT
39 #define wxTBK_BOTTOM wxTBK_RIGHT
41 #define wxTBK_ALIGN_MASK (wxTBK_LEFT | wxTBK_RIGHT)
42 #define wxTBK_DEFAULT wxTBK_LEFT
44 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
48 class WXDLLEXPORT wxTreebook
: public wxBookCtrlBase
51 // Constructors and such
52 // ---------------------
54 // Default ctor doesn't create the control, use Create() afterwards
60 // This ctor creates the tree book control
61 wxTreebook(wxWindow
*parent
,
63 const wxPoint
& pos
= wxDefaultPosition
,
64 const wxSize
& size
= wxDefaultSize
,
65 long style
= wxTBK_DEFAULT
,
66 const wxString
& name
= wxEmptyString
)
70 (void)Create(parent
, id
, pos
, size
, style
, name
);
73 // Really creates the control
74 bool Create(wxWindow
*parent
,
76 const wxPoint
& pos
= wxDefaultPosition
,
77 const wxSize
& size
= wxDefaultSize
,
78 long style
= wxTBK_DEFAULT
,
79 const wxString
& name
= wxEmptyString
);
82 // Page insertion operations
83 // -------------------------
85 // Notice that page pointer may be NULL in which case the next non NULL
86 // page (usually the first child page of a node) is shown when this page is
89 // Inserts a new page just before the page indicated by page.
90 // The new page is placed on the same level as page.
91 virtual bool InsertPage(size_t pos
,
95 int imageId
= wxNOT_FOUND
);
97 // Inserts a new sub-page to the end of children of the page at given pos.
98 virtual bool AddSubPage(size_t pos
,
100 const wxString
& text
,
101 bool bSelect
= false,
102 int imageId
= wxNOT_FOUND
);
104 // Adds a new page at top level after all other pages.
105 virtual bool AddPage(wxWindow
*page
,
106 const wxString
& text
,
107 bool bSelect
= false,
108 int imageId
= wxNOT_FOUND
);
110 // Adds a new child-page to the last top-level page inserted.
111 // Useful when constructing 1 level tree structure.
112 virtual bool AddSubPage(wxWindow
*page
,
113 const wxString
& text
,
114 bool bSelect
= false,
115 int imageId
= wxNOT_FOUND
);
117 // Deletes the page and ALL its children. Could trigger page selection
118 // change in a case when selected page is removed. In that case its parent
119 // is selected (or the next page if no parent).
120 virtual bool DeletePage(size_t pos
);
126 // Gets the page node state -- node is expanded or collapsed
127 virtual bool IsNodeExpanded(size_t pos
) const;
129 // Expands or collapses the page node. Returns the previous state.
130 // May generate page changing events (if selected page
131 // is under the collapsed branch, then parent is autoselected).
132 virtual bool ExpandNode(size_t pos
, bool expand
= true);
134 // shortcut for ExpandNode(pos, false)
135 bool CollapseNode(size_t pos
) { return ExpandNode(pos
, false); }
137 // get the parent page or wxNOT_FOUND if this is a top level page
138 int GetPageParent(size_t pos
) const;
141 // Standard operations inherited from wxBookCtrlBase
142 // -------------------------------------------------
144 virtual int GetSelection() const;
145 virtual bool SetPageText(size_t n
, const wxString
& strText
);
146 virtual wxString
GetPageText(size_t n
) const;
147 virtual int GetPageImage(size_t n
) const;
148 virtual bool SetPageImage(size_t n
, int imageId
);
149 virtual wxSize
CalcSizeFromPage(const wxSize
& sizePage
) const;
150 virtual int SetSelection(size_t n
);
151 virtual void SetImageList(wxImageList
*imageList
);
152 virtual void AssignImageList(wxImageList
*imageList
);
153 virtual bool DeleteAllPages();
156 // This subclass of wxBookCtrlBase accepts NULL page pointers (empty pages)
157 virtual bool AllowNullPage() const { return true; }
159 // get the size which the tree control should have
160 wxSize
GetTreeSize() const;
163 wxRect
GetPageRect() const;
166 void OnSize(wxSizeEvent
& event
);
167 void OnTreeSelectionChange(wxTreeEvent
& event
);
168 void OnTreeNodeExpandedCollapsed(wxTreeEvent
& event
);
171 // the tree control we use for showing the pages index tree
174 // array of page ids and page windows
175 wxArrayTreeItemIds m_treeIds
;
177 // the currently selected page or wxNOT_FOUND if none
180 // in the situation when m_selection page is not wxNOT_FOUND but page is
181 // NULL this is the first (sub)child that has a non-NULL page
182 int m_actualSelection
;
185 // common part of all constructors
188 // The real implementations of page insertion functions
189 // ------------------------------------------------------
190 // All DoInsert/Add(Sub)Page functions add the page into :
192 // - the tree control
193 // - update the index/TreeItemId corespondance array
194 bool DoInsertPage(size_t pos
,
196 const wxString
& text
,
197 bool bSelect
= false,
198 int imageId
= wxNOT_FOUND
);
199 bool DoInsertSubPage(size_t pos
,
201 const wxString
& text
,
202 bool bSelect
= false,
203 int imageId
= wxNOT_FOUND
);
204 bool DoAddSubPage(wxWindow
*page
,
205 const wxString
& text
,
206 bool bSelect
= false,
207 int imageId
= wxNOT_FOUND
);
209 // Implementation of a page removal. See DeletPage for comments.
210 wxTreebookPage
*DoRemovePage(size_t pos
);
212 // Sets selection in the tree control and updates the page being shown.
213 int DoSetSelection(size_t pos
);
215 // Returns currently shown page. In a case when selected the node
216 // has empty (NULL) page finds first (sub)child with not-empty page.
217 wxTreebookPage
*DoGetCurrentPage() const;
219 // Does the selection update. Called from page insertion functions
220 // to update selection if the selected page was pushed by the newly inserted
221 void DoUpdateSelection(bool bSelect
, int page
);
224 // Operations on the internal private members of the class
225 // -------------------------------------------------------
226 // Returns the page TreeItemId for the page.
227 // Or, if the page index is incorrect, a fake one (fakePage.IsOk() == false)
228 wxTreeItemId
DoInternalGetPage(size_t pos
) const;
230 // Linear search for a page with the id specified. If no page
231 // found wxNOT_FOUND is returned. The function is used when we catch an event
232 // from m_tree (wxTreeCtrl) component.
233 int DoInternalFindPageById(wxTreeItemId page
) const;
235 // Updates page and wxTreeItemId correspondance.
236 void DoInternalAddPage(size_t newPos
, wxWindow
*page
, wxTreeItemId pageId
);
238 // Removes the page from internal structure.
239 void DoInternalRemovePage(size_t pos
)
240 { DoInternalRemovePageRange(pos
, 0); }
242 // Removes the page and all its children designated by subCount
243 // from internal structures of the control.
244 void DoInternalRemovePageRange(size_t pos
, size_t subCount
);
246 // Returns internal number of pages which can be different from
247 // GetPageCount() while performing a page insertion or removal.
248 size_t DoInternalGetPageCount() const { return m_treeIds
.Count(); }
251 DECLARE_EVENT_TABLE()
252 DECLARE_DYNAMIC_CLASS_NO_COPY(wxTreebook
)
256 // ----------------------------------------------------------------------------
257 // treebook event class and related stuff
258 // ----------------------------------------------------------------------------
260 class WXDLLEXPORT wxTreebookEvent
: public wxBookCtrlBaseEvent
263 wxTreebookEvent(wxEventType commandType
= wxEVT_NULL
, int id
= 0,
264 int nSel
= wxNOT_FOUND
, int nOldSel
= wxNOT_FOUND
)
265 : wxBookCtrlBaseEvent(commandType
, id
, nSel
, nOldSel
)
270 DECLARE_DYNAMIC_CLASS_NO_COPY(wxTreebookEvent
)
273 extern WXDLLIMPEXP_CORE
const wxEventType wxEVT_COMMAND_TREEBOOK_PAGE_CHANGED
;
274 extern WXDLLIMPEXP_CORE
const wxEventType wxEVT_COMMAND_TREEBOOK_PAGE_CHANGING
;
275 extern WXDLLIMPEXP_CORE
const wxEventType wxEVT_COMMAND_TREEBOOK_NODE_COLLAPSED
;
276 extern WXDLLIMPEXP_CORE
const wxEventType wxEVT_COMMAND_TREEBOOK_NODE_EXPANDED
;
278 typedef void (wxEvtHandler::*wxTreebookEventFunction
)(wxTreebookEvent
&);
280 #define wxTreebookEventHandler(func) \
281 (wxObjectEventFunction)(wxEventFunction)wxStaticCastEvent(wxTreebookEventFunction, &func)
283 #define EVT_TREEBOOK_PAGE_CHANGED(winid, fn) \
284 wx__DECLARE_EVT1(wxEVT_COMMAND_TREEBOOK_PAGE_CHANGED, winid, wxTreebookEventHandler(fn))
286 #define EVT_TREEBOOK_PAGE_CHANGING(winid, fn) \
287 wx__DECLARE_EVT1(wxEVT_COMMAND_TREEBOOK_PAGE_CHANGING, winid, wxTreebookEventHandler(fn))
289 #define EVT_TREEBOOK_NODE_COLLAPSED(winid, fn) \
290 wx__DECLARE_EVT1(wxEVT_COMMAND_TREEBOOK_NODE_COLLAPSED, winid, wxTreebookEventHandler(fn))
292 #define EVT_TREEBOOK_NODE_EXPANDED(winid, fn) \
293 wx__DECLARE_EVT1(wxEVT_COMMAND_TREEBOOK_NODE_EXPANDED, winid, wxTreebookEventHandler(fn))
296 #endif // wxUSE_TREEBOOK
298 #endif // _WX_TREEBOOK_H_