Added wxTreebook:
[wxWidgets.git] / include / wx / treebook.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: wx/treebook.h
3 // Purpose: wxTreebook: wxNotebook-like control presenting pages in a tree
4 // Author: Evgeniy Tarassov, Vadim Zeitlin
5 // Modified by:
6 // Created: 2005-09-15
7 // RCS-ID: $Id$
8 // Copyright: (c) 2005 Vadim Zeitlin <vadim@wxwidgets.org>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_TREEBOOK_H_
13 #define _WX_TREEBOOK_H_
14
15 #include "wx/defs.h"
16
17 #if wxUSE_TREEBOOK
18
19 #include "wx/bookctrl.h"
20 #include "wx/treectrl.h" // for wxArrayTreeItemIds
21
22 typedef wxWindow wxTreebookPage;
23
24 class WXDLLEXPORT wxTreeEvent;
25
26 // ----------------------------------------------------------------------------
27 // style flags
28 // ----------------------------------------------------------------------------
29
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
32 // wxChoicebook only.
33 #define wxTBK_LEFT wxNB_LEFT
34 #define wxTBK_RIGHT wxNB_RIGHT
35
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
40
41 #define wxTBK_ALIGN_MASK (wxTBK_LEFT | wxTBK_RIGHT)
42 #define wxTBK_DEFAULT wxTBK_LEFT
43
44 // ----------------------------------------------------------------------------
45 // wxTreebook
46 // ----------------------------------------------------------------------------
47
48 class WXDLLEXPORT wxTreebook : public wxBookCtrlBase
49 {
50 public:
51 // Constructors and such
52 // ---------------------
53
54 // Default ctor doesn't create the control, use Create() afterwards
55 wxTreebook()
56 {
57 Init();
58 }
59
60 // This ctor creates the tree book control
61 wxTreebook(wxWindow *parent,
62 wxWindowID id,
63 const wxPoint& pos = wxDefaultPosition,
64 const wxSize& size = wxDefaultSize,
65 long style = wxTBK_DEFAULT,
66 const wxString& name = wxEmptyString)
67 {
68 Init();
69
70 (void)Create(parent, id, pos, size, style, name);
71 }
72
73 // Really creates the control
74 bool Create(wxWindow *parent,
75 wxWindowID id,
76 const wxPoint& pos = wxDefaultPosition,
77 const wxSize& size = wxDefaultSize,
78 long style = wxTBK_DEFAULT,
79 const wxString& name = wxEmptyString);
80
81
82 // Page insertion operations
83 // -------------------------
84
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
87 // selected
88
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,
92 wxWindow *page,
93 const wxString& text,
94 bool bSelect = false,
95 int imageId = wxNOT_FOUND);
96
97 // Inserts a new sub-page to the end of children of the page at given pos.
98 virtual bool AddSubPage(size_t pos,
99 wxWindow *page,
100 const wxString& text,
101 bool bSelect = false,
102 int imageId = wxNOT_FOUND);
103
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);
109
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);
116
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);
121
122
123 // Tree operations
124 // ---------------
125
126 // Gets the page node state -- node is expanded or collapsed
127 virtual bool IsNodeExpanded(size_t pos) const;
128
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);
133
134 // shortcut for ExpandNode(pos, false)
135 bool CollapseNode(size_t pos) { return ExpandNode(pos, false); }
136
137 // get the parent page or wxNOT_FOUND if this is a top level page
138 int GetPageParent(size_t pos) const;
139
140
141 // Standard operations inherited from wxBookCtrlBase
142 // -------------------------------------------------
143
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();
154
155 protected:
156 // This subclass of wxBookCtrlBase accepts NULL page pointers (empty pages)
157 virtual bool AllowNullPage() const { return true; }
158
159 // get the size which the tree control should have
160 wxSize GetTreeSize() const;
161
162 // get the page area
163 wxRect GetPageRect() const;
164
165 // event handlers
166 void OnSize(wxSizeEvent& event);
167 void OnTreeSelectionChange(wxTreeEvent& event);
168 void OnTreeNodeExpandedCollapsed(wxTreeEvent& event);
169
170
171 // the tree control we use for showing the pages index tree
172 wxTreeCtrl *m_tree;
173
174 // array of page ids and page windows
175 wxArrayTreeItemIds m_treeIds;
176
177 // the currently selected page or wxNOT_FOUND if none
178 int m_selection;
179
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;
183
184 private:
185 // common part of all constructors
186 void Init();
187
188 // The real implementations of page insertion functions
189 // ------------------------------------------------------
190 // All DoInsert/Add(Sub)Page functions add the page into :
191 // - the base class
192 // - the tree control
193 // - update the index/TreeItemId corespondance array
194 bool DoInsertPage(size_t pos,
195 wxWindow *page,
196 const wxString& text,
197 bool bSelect = false,
198 int imageId = wxNOT_FOUND);
199 bool DoInsertSubPage(size_t pos,
200 wxWindow *page,
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);
208
209 // Implementation of a page removal. See DeletPage for comments.
210 wxTreebookPage *DoRemovePage(size_t pos);
211
212 // Sets selection in the tree control and updates the page being shown.
213 int DoSetSelection(size_t pos);
214
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;
218
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);
222
223
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;
229
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;
234
235 // Updates page and wxTreeItemId correspondance.
236 void DoInternalAddPage(size_t newPos, wxWindow *page, wxTreeItemId pageId);
237
238 // Removes the page from internal structure.
239 void DoInternalRemovePage(size_t pos)
240 { DoInternalRemovePageRange(pos, 0); }
241
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);
245
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(); }
249
250
251 DECLARE_EVENT_TABLE()
252 DECLARE_DYNAMIC_CLASS_NO_COPY(wxTreebook)
253 };
254
255
256 // ----------------------------------------------------------------------------
257 // treebook event class and related stuff
258 // ----------------------------------------------------------------------------
259
260 class WXDLLEXPORT wxTreebookEvent : public wxBookCtrlBaseEvent
261 {
262 public:
263 wxTreebookEvent(wxEventType commandType = wxEVT_NULL, int id = 0,
264 int nSel = wxNOT_FOUND, int nOldSel = wxNOT_FOUND)
265 : wxBookCtrlBaseEvent(commandType, id, nSel, nOldSel)
266 {
267 }
268
269 private:
270 DECLARE_DYNAMIC_CLASS_NO_COPY(wxTreebookEvent)
271 };
272
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;
277
278 typedef void (wxEvtHandler::*wxTreebookEventFunction)(wxTreebookEvent&);
279
280 #define wxTreebookEventHandler(func) \
281 (wxObjectEventFunction)(wxEventFunction)wxStaticCastEvent(wxTreebookEventFunction, &func)
282
283 #define EVT_TREEBOOK_PAGE_CHANGED(winid, fn) \
284 wx__DECLARE_EVT1(wxEVT_COMMAND_TREEBOOK_PAGE_CHANGED, winid, wxTreebookEventHandler(fn))
285
286 #define EVT_TREEBOOK_PAGE_CHANGING(winid, fn) \
287 wx__DECLARE_EVT1(wxEVT_COMMAND_TREEBOOK_PAGE_CHANGING, winid, wxTreebookEventHandler(fn))
288
289 #define EVT_TREEBOOK_NODE_COLLAPSED(winid, fn) \
290 wx__DECLARE_EVT1(wxEVT_COMMAND_TREEBOOK_NODE_COLLAPSED, winid, wxTreebookEventHandler(fn))
291
292 #define EVT_TREEBOOK_NODE_EXPANDED(winid, fn) \
293 wx__DECLARE_EVT1(wxEVT_COMMAND_TREEBOOK_NODE_EXPANDED, winid, wxTreebookEventHandler(fn))
294
295
296 #endif // wxUSE_TREEBOOK
297
298 #endif // _WX_TREEBOOK_H_