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 WXDLLIMPEXP_FWD_CORE wxTreeEvent
;
26 // ----------------------------------------------------------------------------
28 // ----------------------------------------------------------------------------
30 class WXDLLIMPEXP_CORE wxTreebook
: public wxBookCtrlBase
33 // Constructors and such
34 // ---------------------
36 // Default ctor doesn't create the control, use Create() afterwards
42 // This ctor creates the tree book control
43 wxTreebook(wxWindow
*parent
,
45 const wxPoint
& pos
= wxDefaultPosition
,
46 const wxSize
& size
= wxDefaultSize
,
47 long style
= wxBK_DEFAULT
,
48 const wxString
& name
= wxEmptyString
)
52 (void)Create(parent
, id
, pos
, size
, style
, name
);
55 // Really creates the control
56 bool Create(wxWindow
*parent
,
58 const wxPoint
& pos
= wxDefaultPosition
,
59 const wxSize
& size
= wxDefaultSize
,
60 long style
= wxBK_DEFAULT
,
61 const wxString
& name
= wxEmptyString
);
64 // Page insertion operations
65 // -------------------------
67 // Notice that page pointer may be NULL in which case the next non NULL
68 // page (usually the first child page of a node) is shown when this page is
71 // Inserts a new page just before the page indicated by page.
72 // The new page is placed on the same level as page.
73 virtual bool InsertPage(size_t pos
,
77 int imageId
= wxNOT_FOUND
);
79 // Inserts a new sub-page to the end of children of the page at given pos.
80 virtual bool InsertSubPage(size_t pos
,
84 int imageId
= wxNOT_FOUND
);
86 // Adds a new page at top level after all other pages.
87 virtual bool AddPage(wxWindow
*page
,
90 int imageId
= wxNOT_FOUND
);
92 // Adds a new child-page to the last top-level page inserted.
93 // Useful when constructing 1 level tree structure.
94 virtual bool AddSubPage(wxWindow
*page
,
97 int imageId
= wxNOT_FOUND
);
99 // Deletes the page and ALL its children. Could trigger page selection
100 // change in a case when selected page is removed. In that case its parent
101 // is selected (or the next page if no parent).
102 virtual bool DeletePage(size_t pos
);
108 // Gets the page node state -- node is expanded or collapsed
109 virtual bool IsNodeExpanded(size_t pos
) const;
111 // Expands or collapses the page node. Returns the previous state.
112 // May generate page changing events (if selected page
113 // is under the collapsed branch, then parent is autoselected).
114 virtual bool ExpandNode(size_t pos
, bool expand
= true);
116 // shortcut for ExpandNode(pos, false)
117 bool CollapseNode(size_t pos
) { return ExpandNode(pos
, false); }
119 // get the parent page or wxNOT_FOUND if this is a top level page
120 int GetPageParent(size_t pos
) const;
122 // the tree control we use for showing the pages index tree
123 wxTreeCtrl
* GetTreeCtrl() const { return (wxTreeCtrl
*)m_bookctrl
; }
126 // Standard operations inherited from wxBookCtrlBase
127 // -------------------------------------------------
129 virtual int GetSelection() const;
130 virtual bool SetPageText(size_t n
, const wxString
& strText
);
131 virtual wxString
GetPageText(size_t n
) const;
132 virtual int GetPageImage(size_t n
) const;
133 virtual bool SetPageImage(size_t n
, int imageId
);
134 virtual int SetSelection(size_t n
) { return DoSetSelection(n
, SetSelection_SendEvent
); }
135 virtual int ChangeSelection(size_t n
) { return DoSetSelection(n
); }
136 virtual int HitTest(const wxPoint
& pt
, long *flags
= NULL
) const;
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; }
149 void OnTreeSelectionChange(wxTreeEvent
& event
);
150 void OnTreeNodeExpandedCollapsed(wxTreeEvent
& event
);
152 // array of page ids and page windows
153 wxArrayTreeItemIds m_treeIds
;
155 // the currently selected page or wxNOT_FOUND if none
158 // in the situation when m_selection page is not wxNOT_FOUND but page is
159 // NULL this is the first (sub)child that has a non-NULL page
160 int m_actualSelection
;
163 // common part of all constructors
166 // The real implementations of page insertion functions
167 // ------------------------------------------------------
168 // All DoInsert/Add(Sub)Page functions add the page into :
170 // - the tree control
171 // - update the index/TreeItemId corespondance array
172 bool DoInsertPage(size_t pos
,
174 const wxString
& text
,
175 bool bSelect
= false,
176 int imageId
= wxNOT_FOUND
);
177 bool DoInsertSubPage(size_t pos
,
179 const wxString
& text
,
180 bool bSelect
= false,
181 int imageId
= wxNOT_FOUND
);
182 bool DoAddSubPage(wxWindow
*page
,
183 const wxString
& text
,
184 bool bSelect
= false,
185 int imageId
= wxNOT_FOUND
);
187 // Sets selection in the tree control and updates the page being shown.
188 int DoSetSelection(size_t pos
, int flags
= 0);
190 // Returns currently shown page. In a case when selected the node
191 // has empty (NULL) page finds first (sub)child with not-empty page.
192 wxTreebookPage
*DoGetCurrentPage() const;
194 // Does the selection update. Called from page insertion functions
195 // to update selection if the selected page was pushed by the newly inserted
196 void DoUpdateSelection(bool bSelect
, int page
);
199 // Operations on the internal private members of the class
200 // -------------------------------------------------------
201 // Returns the page TreeItemId for the page.
202 // Or, if the page index is incorrect, a fake one (fakePage.IsOk() == false)
203 wxTreeItemId
DoInternalGetPage(size_t pos
) const;
205 // Linear search for a page with the id specified. If no page
206 // found wxNOT_FOUND is returned. The function is used when we catch an event
207 // from m_tree (wxTreeCtrl) component.
208 int DoInternalFindPageById(wxTreeItemId page
) const;
210 // Updates page and wxTreeItemId correspondance.
211 void DoInternalAddPage(size_t newPos
, wxWindow
*page
, wxTreeItemId pageId
);
213 // Removes the page from internal structure.
214 void DoInternalRemovePage(size_t pos
)
215 { DoInternalRemovePageRange(pos
, 0); }
217 // Removes the page and all its children designated by subCount
218 // from internal structures of the control.
219 void DoInternalRemovePageRange(size_t pos
, size_t subCount
);
221 // Returns internal number of pages which can be different from
222 // GetPageCount() while performing a page insertion or removal.
223 size_t DoInternalGetPageCount() const { return m_treeIds
.GetCount(); }
226 DECLARE_EVENT_TABLE()
227 DECLARE_DYNAMIC_CLASS_NO_COPY(wxTreebook
)
231 // ----------------------------------------------------------------------------
232 // treebook event class and related stuff
233 // ----------------------------------------------------------------------------
235 // wxTreebookEvent is obsolete and defined for compatibility only
236 typedef wxBookCtrlEvent wxTreebookEvent
;
237 typedef wxBookCtrlEventFunction wxTreebookEventFunction
;
238 #define wxTreebookEventHandler(func) wxBookCtrlEventHandler(func)
241 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_CORE
, wxEVT_COMMAND_TREEBOOK_PAGE_CHANGED
, wxBookCtrlEvent
);
242 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_CORE
, wxEVT_COMMAND_TREEBOOK_PAGE_CHANGING
, wxBookCtrlEvent
);
243 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_CORE
, wxEVT_COMMAND_TREEBOOK_NODE_COLLAPSED
, wxBookCtrlEvent
);
244 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_CORE
, wxEVT_COMMAND_TREEBOOK_NODE_EXPANDED
, wxBookCtrlEvent
);
246 #define EVT_TREEBOOK_PAGE_CHANGED(winid, fn) \
247 wx__DECLARE_EVT1(wxEVT_COMMAND_TREEBOOK_PAGE_CHANGED, winid, wxBookCtrlEventHandler(fn))
249 #define EVT_TREEBOOK_PAGE_CHANGING(winid, fn) \
250 wx__DECLARE_EVT1(wxEVT_COMMAND_TREEBOOK_PAGE_CHANGING, winid, wxBookCtrlEventHandler(fn))
252 #define EVT_TREEBOOK_NODE_COLLAPSED(winid, fn) \
253 wx__DECLARE_EVT1(wxEVT_COMMAND_TREEBOOK_NODE_COLLAPSED, winid, wxBookCtrlEventHandler(fn))
255 #define EVT_TREEBOOK_NODE_EXPANDED(winid, fn) \
256 wx__DECLARE_EVT1(wxEVT_COMMAND_TREEBOOK_NODE_EXPANDED, winid, wxBookCtrlEventHandler(fn))
259 #endif // wxUSE_TREEBOOK
261 #endif // _WX_TREEBOOK_H_