]> git.saurik.com Git - wxWidgets.git/blob - include/wx/bookctrl.h
using Run of base class
[wxWidgets.git] / include / wx / bookctrl.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: wx/bookctrl.h
3 // Purpose: wxBookCtrlBase: common base class for wxList/Tree/Notebook
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 19.08.03
7 // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwidgets.org>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
10
11 #ifndef _WX_BOOKCTRL_H_
12 #define _WX_BOOKCTRL_H_
13
14 // ----------------------------------------------------------------------------
15 // headers
16 // ----------------------------------------------------------------------------
17
18 #include "wx/defs.h"
19
20 #if wxUSE_BOOKCTRL
21
22 #include "wx/control.h"
23 #include "wx/dynarray.h"
24 #include "wx/withimages.h"
25
26 WX_DEFINE_EXPORTED_ARRAY_PTR(wxWindow *, wxArrayPages);
27
28 class WXDLLIMPEXP_FWD_CORE wxImageList;
29 class WXDLLIMPEXP_FWD_CORE wxBookCtrlEvent;
30
31 // ----------------------------------------------------------------------------
32 // constants
33 // ----------------------------------------------------------------------------
34
35 // wxBookCtrl hit results
36 enum
37 {
38 wxBK_HITTEST_NOWHERE = 1, // not on tab
39 wxBK_HITTEST_ONICON = 2, // on icon
40 wxBK_HITTEST_ONLABEL = 4, // on label
41 wxBK_HITTEST_ONITEM = wxBK_HITTEST_ONICON | wxBK_HITTEST_ONLABEL,
42 wxBK_HITTEST_ONPAGE = 8 // not on tab control, but over the selected page
43 };
44
45 // wxBookCtrl flags (common for wxNotebook, wxListbook, wxChoicebook, wxTreebook)
46 #define wxBK_DEFAULT 0x0000
47 #define wxBK_TOP 0x0010
48 #define wxBK_BOTTOM 0x0020
49 #define wxBK_LEFT 0x0040
50 #define wxBK_RIGHT 0x0080
51 #define wxBK_ALIGN_MASK (wxBK_TOP | wxBK_BOTTOM | wxBK_LEFT | wxBK_RIGHT)
52
53 // ----------------------------------------------------------------------------
54 // wxBookCtrlBase
55 // ----------------------------------------------------------------------------
56
57 class WXDLLIMPEXP_CORE wxBookCtrlBase : public wxControl,
58 public wxWithImages
59 {
60 public:
61 // construction
62 // ------------
63
64 wxBookCtrlBase()
65 {
66 Init();
67 }
68
69 wxBookCtrlBase(wxWindow *parent,
70 wxWindowID winid,
71 const wxPoint& pos = wxDefaultPosition,
72 const wxSize& size = wxDefaultSize,
73 long style = 0,
74 const wxString& name = wxEmptyString)
75 {
76 Init();
77
78 (void)Create(parent, winid, pos, size, style, name);
79 }
80
81 // quasi ctor
82 bool Create(wxWindow *parent,
83 wxWindowID winid,
84 const wxPoint& pos = wxDefaultPosition,
85 const wxSize& size = wxDefaultSize,
86 long style = 0,
87 const wxString& name = wxEmptyString);
88
89
90 // accessors
91 // ---------
92
93 // get number of pages in the dialog
94 virtual size_t GetPageCount() const { return m_pages.size(); }
95
96 // get the panel which represents the given page
97 virtual wxWindow *GetPage(size_t n) const { return m_pages[n]; }
98
99 // get the current page or NULL if none
100 wxWindow *GetCurrentPage() const
101 {
102 const int n = GetSelection();
103 return n == wxNOT_FOUND ? NULL : GetPage(n);
104 }
105
106 // get the currently selected page or wxNOT_FOUND if none
107 virtual int GetSelection() const { return m_selection; }
108
109 // set/get the title of a page
110 virtual bool SetPageText(size_t n, const wxString& strText) = 0;
111 virtual wxString GetPageText(size_t n) const = 0;
112
113
114 // image list stuff: each page may have an image associated with it (all
115 // images belong to the same image list)
116 // ---------------------------------------------------------------------
117
118 // sets/returns item's image index in the current image list
119 virtual int GetPageImage(size_t n) const = 0;
120 virtual bool SetPageImage(size_t n, int imageId) = 0;
121
122
123 // geometry
124 // --------
125
126 // resize the notebook so that all pages will have the specified size
127 virtual void SetPageSize(const wxSize& size);
128
129 // return the size of the area needed to accommodate the controller
130 wxSize GetControllerSize() const;
131
132 // calculate the size of the control from the size of its page
133 //
134 // by default this simply returns size enough to fit both the page and the
135 // controller
136 virtual wxSize CalcSizeFromPage(const wxSize& sizePage) const;
137
138 // get/set size of area between book control area and page area
139 unsigned int GetInternalBorder() const { return m_internalBorder; }
140 void SetInternalBorder(unsigned int border) { m_internalBorder = border; }
141
142 // Sets/gets the margin around the controller
143 void SetControlMargin(int margin) { m_controlMargin = margin; }
144 int GetControlMargin() const { return m_controlMargin; }
145
146 // returns true if we have wxBK_TOP or wxBK_BOTTOM style
147 bool IsVertical() const { return HasFlag(wxBK_BOTTOM | wxBK_TOP); }
148
149 // set/get option to shrink to fit current page
150 void SetFitToCurrentPage(bool fit) { m_fitToCurrentPage = fit; }
151 bool GetFitToCurrentPage() const { return m_fitToCurrentPage; }
152
153 // returns the sizer containing the control, if any
154 wxSizer* GetControlSizer() const { return m_controlSizer; }
155
156
157 // operations
158 // ----------
159
160 // remove one page from the control and delete it
161 virtual bool DeletePage(size_t n);
162
163 // remove one page from the notebook, without deleting it
164 virtual bool RemovePage(size_t n)
165 {
166 DoInvalidateBestSize();
167 return DoRemovePage(n) != NULL;
168 }
169
170 // remove all pages and delete them
171 virtual bool DeleteAllPages()
172 {
173 m_selection = wxNOT_FOUND;
174 DoInvalidateBestSize();
175 WX_CLEAR_ARRAY(m_pages);
176 return true;
177 }
178
179 // adds a new page to the control
180 virtual bool AddPage(wxWindow *page,
181 const wxString& text,
182 bool bSelect = false,
183 int imageId = NO_IMAGE)
184 {
185 DoInvalidateBestSize();
186 return InsertPage(GetPageCount(), page, text, bSelect, imageId);
187 }
188
189 // the same as AddPage(), but adds the page at the specified position
190 virtual bool InsertPage(size_t n,
191 wxWindow *page,
192 const wxString& text,
193 bool bSelect = false,
194 int imageId = NO_IMAGE) = 0;
195
196 // set the currently selected page, return the index of the previously
197 // selected one (or wxNOT_FOUND on error)
198 //
199 // NB: this function will generate PAGE_CHANGING/ED events
200 virtual int SetSelection(size_t n) = 0;
201
202 // acts as SetSelection but does not generate events
203 virtual int ChangeSelection(size_t n) = 0;
204
205
206 // cycle thru the pages
207 void AdvanceSelection(bool forward = true)
208 {
209 int nPage = GetNextPage(forward);
210 if ( nPage != wxNOT_FOUND )
211 {
212 // cast is safe because of the check above
213 SetSelection((size_t)nPage);
214 }
215 }
216
217 // return the index of the given page or wxNOT_FOUND
218 int FindPage(const wxWindow* page) const;
219
220 // hit test: returns which page is hit and, optionally, where (icon, label)
221 virtual int HitTest(const wxPoint& WXUNUSED(pt),
222 long * WXUNUSED(flags) = NULL) const
223 {
224 return wxNOT_FOUND;
225 }
226
227
228 // we do have multiple pages
229 virtual bool HasMultiplePages() const { return true; }
230
231 // we don't want focus for ourselves
232 virtual bool AcceptsFocus() const { return false; }
233
234 // returns true if the platform should explicitly apply a theme border
235 virtual bool CanApplyThemeBorder() const { return false; }
236
237 protected:
238 // flags for DoSetSelection()
239 enum
240 {
241 SetSelection_SendEvent = 1
242 };
243
244 // choose the default border for this window
245 virtual wxBorder GetDefaultBorder() const { return wxBORDER_NONE; }
246
247 // After the insertion of the page in the method InsertPage, calling this
248 // method sets the selection to the given page or the first one if there is
249 // still no selection. The "selection changed" event is sent only if
250 // bSelect is true, so when it is false, no event is sent even if the
251 // selection changed from wxNOT_FOUND to 0 when inserting the first page.
252 //
253 // Returns true if the selection was set to the specified page (explicitly
254 // because of bSelect == true or implicitly because it's the first page) or
255 // false otherwise.
256 bool DoSetSelectionAfterInsertion(size_t n, bool bSelect);
257
258 // Update the selection after removing the page at the given index,
259 // typically called from the derived class overridden DoRemovePage().
260 void DoSetSelectionAfterRemoval(size_t n);
261
262 // set the selection to the given page, sending the events (which can
263 // possibly prevent the page change from taking place) if SendEvent flag is
264 // included
265 virtual int DoSetSelection(size_t nPage, int flags = 0);
266
267 // if the derived class uses DoSetSelection() for implementing
268 // [Set|Change]Selection, it must override UpdateSelectedPage(),
269 // CreatePageChangingEvent() and MakeChangedEvent(), but as it might not
270 // use it, these functions are not pure virtual
271
272 // called to notify the control about a new current page
273 virtual void UpdateSelectedPage(size_t WXUNUSED(newsel))
274 { wxFAIL_MSG(wxT("Override this function!")); }
275
276 // create a new "page changing" event
277 virtual wxBookCtrlEvent* CreatePageChangingEvent() const
278 { wxFAIL_MSG(wxT("Override this function!")); return NULL; }
279
280 // modify the event created by CreatePageChangingEvent() to "page changed"
281 // event, usually by just calling SetEventType() on it
282 virtual void MakeChangedEvent(wxBookCtrlEvent& WXUNUSED(event))
283 { wxFAIL_MSG(wxT("Override this function!")); }
284
285
286 // The derived class also may override the following method, also called
287 // from DoSetSelection(), to show/hide pages differently.
288 virtual void DoShowPage(wxWindow* page, bool show) { page->Show(show); }
289
290
291 // Should we accept NULL page pointers in Add/InsertPage()?
292 //
293 // Default is no but derived classes may override it if they can treat NULL
294 // pages in some sensible way (e.g. wxTreebook overrides this to allow
295 // having nodes without any associated page)
296 virtual bool AllowNullPage() const { return false; }
297
298 // Remove the page and return a pointer to it.
299 //
300 // It also needs to update the current selection if necessary, i.e. if the
301 // page being removed comes before the selected one and the helper method
302 // DoSetSelectionAfterRemoval() can be used for this.
303 virtual wxWindow *DoRemovePage(size_t page) = 0;
304
305 // our best size is the size which fits all our pages
306 virtual wxSize DoGetBestSize() const;
307
308 // helper: get the next page wrapping if we reached the end
309 int GetNextPage(bool forward) const;
310
311 // Lay out controls
312 virtual void DoSize();
313
314 // This method also invalidates the size of the controller and should be
315 // called instead of just InvalidateBestSize() whenever pages are added or
316 // removed as this also affects the controller
317 void DoInvalidateBestSize();
318
319 #if wxUSE_HELP
320 // Show the help for the corresponding page
321 void OnHelp(wxHelpEvent& event);
322 #endif // wxUSE_HELP
323
324
325 // the array of all pages of this control
326 wxArrayPages m_pages;
327
328 // get the page area
329 virtual wxRect GetPageRect() const;
330
331 // event handlers
332 void OnSize(wxSizeEvent& event);
333
334 // controller buddy if available, NULL otherwise (usually for native book controls like wxNotebook)
335 wxControl *m_bookctrl;
336
337 // Whether to shrink to fit current page
338 bool m_fitToCurrentPage;
339
340 // the sizer containing the choice control
341 wxSizer *m_controlSizer;
342
343 // the margin around the choice control
344 int m_controlMargin;
345
346 // The currently selected page (in range 0..m_pages.size()-1 inclusive) or
347 // wxNOT_FOUND if none (this can normally only be the case for an empty
348 // control without any pages).
349 int m_selection;
350
351 private:
352
353 // common part of all ctors
354 void Init();
355
356 // internal border
357 unsigned int m_internalBorder;
358
359 DECLARE_ABSTRACT_CLASS(wxBookCtrlBase)
360 wxDECLARE_NO_COPY_CLASS(wxBookCtrlBase);
361
362 DECLARE_EVENT_TABLE()
363 };
364
365 // ----------------------------------------------------------------------------
366 // wxBookCtrlEvent: page changing events generated by book classes
367 // ----------------------------------------------------------------------------
368
369 class WXDLLIMPEXP_CORE wxBookCtrlEvent : public wxNotifyEvent
370 {
371 public:
372 wxBookCtrlEvent(wxEventType commandType = wxEVT_NULL, int winid = 0,
373 int nSel = wxNOT_FOUND, int nOldSel = wxNOT_FOUND)
374 : wxNotifyEvent(commandType, winid)
375 {
376 m_nSel = nSel;
377 m_nOldSel = nOldSel;
378 }
379
380 wxBookCtrlEvent(const wxBookCtrlEvent& event)
381 : wxNotifyEvent(event)
382 {
383 m_nSel = event.m_nSel;
384 m_nOldSel = event.m_nOldSel;
385 }
386
387 virtual wxEvent *Clone() const { return new wxBookCtrlEvent(*this); }
388
389 // accessors
390 // the currently selected page (wxNOT_FOUND if none)
391 int GetSelection() const { return m_nSel; }
392 void SetSelection(int nSel) { m_nSel = nSel; }
393 // the page that was selected before the change (wxNOT_FOUND if none)
394 int GetOldSelection() const { return m_nOldSel; }
395 void SetOldSelection(int nOldSel) { m_nOldSel = nOldSel; }
396
397 private:
398 int m_nSel, // currently selected page
399 m_nOldSel; // previously selected page
400
401 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxBookCtrlEvent)
402 };
403
404 typedef void (wxEvtHandler::*wxBookCtrlEventFunction)(wxBookCtrlEvent&);
405
406 #define wxBookCtrlEventHandler(func) \
407 wxEVENT_HANDLER_CAST(wxBookCtrlEventFunction, func)
408
409 // obsolete name, defined for compatibility only
410 #define wxBookCtrlBaseEvent wxBookCtrlEvent
411
412 // make a default book control for given platform
413 #if wxUSE_NOTEBOOK
414 // dedicated to majority of desktops
415 #include "wx/notebook.h"
416 #define wxBookCtrl wxNotebook
417 #define wxEVT_BOOKCTRL_PAGE_CHANGED wxEVT_NOTEBOOK_PAGE_CHANGED
418 #define wxEVT_BOOKCTRL_PAGE_CHANGING wxEVT_NOTEBOOK_PAGE_CHANGING
419 #define EVT_BOOKCTRL_PAGE_CHANGED(id, fn) EVT_NOTEBOOK_PAGE_CHANGED(id, fn)
420 #define EVT_BOOKCTRL_PAGE_CHANGING(id, fn) EVT_NOTEBOOK_PAGE_CHANGING(id, fn)
421 #else
422 // dedicated to Smartphones
423 #include "wx/choicebk.h"
424 #define wxBookCtrl wxChoicebook
425 #define wxEVT_BOOKCTRL_PAGE_CHANGED wxEVT_CHOICEBOOK_PAGE_CHANGED
426 #define wxEVT_BOOKCTRL_PAGE_CHANGING wxEVT_CHOICEBOOK_PAGE_CHANGING
427 #define EVT_BOOKCTRL_PAGE_CHANGED(id, fn) EVT_CHOICEBOOK_PAGE_CHANGED(id, fn)
428 #define EVT_BOOKCTRL_PAGE_CHANGING(id, fn) EVT_CHOICEBOOK_PAGE_CHANGING(id, fn)
429 #endif
430
431 // old wxEVT_COMMAND_* constants
432 #define wxEVT_COMMAND_BOOKCTRL_PAGE_CHANGED wxEVT_BOOKCTRL_PAGE_CHANGED
433 #define wxEVT_COMMAND_BOOKCTRL_PAGE_CHANGING wxEVT_BOOKCTRL_PAGE_CHANGING
434
435 #if WXWIN_COMPATIBILITY_2_6
436 #define wxBC_TOP wxBK_TOP
437 #define wxBC_BOTTOM wxBK_BOTTOM
438 #define wxBC_LEFT wxBK_LEFT
439 #define wxBC_RIGHT wxBK_RIGHT
440 #define wxBC_DEFAULT wxBK_DEFAULT
441 #endif
442
443 #endif // wxUSE_BOOKCTRL
444
445 #endif // _WX_BOOKCTRL_H_