1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxBookCtrlBase: common base class for wxList/Tree/Notebook
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwidgets.org>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_BOOKCTRL_H_
13 #define _WX_BOOKCTRL_H_
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
23 #include "wx/control.h"
24 #include "wx/dynarray.h"
25 #include "wx/withimages.h"
27 WX_DEFINE_EXPORTED_ARRAY_PTR(wxWindow
*, wxArrayPages
);
29 class WXDLLIMPEXP_FWD_CORE wxImageList
;
30 class WXDLLIMPEXP_FWD_CORE wxBookCtrlEvent
;
32 // ----------------------------------------------------------------------------
34 // ----------------------------------------------------------------------------
36 // wxBookCtrl hit results
39 wxBK_HITTEST_NOWHERE
= 1, // not on tab
40 wxBK_HITTEST_ONICON
= 2, // on icon
41 wxBK_HITTEST_ONLABEL
= 4, // on label
42 wxBK_HITTEST_ONITEM
= wxBK_HITTEST_ONICON
| wxBK_HITTEST_ONLABEL
,
43 wxBK_HITTEST_ONPAGE
= 8 // not on tab control, but over the selected page
46 // wxBookCtrl flags (common for wxNotebook, wxListbook, wxChoicebook, wxTreebook)
47 #define wxBK_DEFAULT 0x0000
48 #define wxBK_TOP 0x0010
49 #define wxBK_BOTTOM 0x0020
50 #define wxBK_LEFT 0x0040
51 #define wxBK_RIGHT 0x0080
52 #define wxBK_ALIGN_MASK (wxBK_TOP | wxBK_BOTTOM | wxBK_LEFT | wxBK_RIGHT)
54 // ----------------------------------------------------------------------------
56 // ----------------------------------------------------------------------------
58 class WXDLLIMPEXP_CORE wxBookCtrlBase
: public wxControl
,
70 wxBookCtrlBase(wxWindow
*parent
,
72 const wxPoint
& pos
= wxDefaultPosition
,
73 const wxSize
& size
= wxDefaultSize
,
75 const wxString
& name
= wxEmptyString
)
79 (void)Create(parent
, winid
, pos
, size
, style
, name
);
83 bool Create(wxWindow
*parent
,
85 const wxPoint
& pos
= wxDefaultPosition
,
86 const wxSize
& size
= wxDefaultSize
,
88 const wxString
& name
= wxEmptyString
);
94 // get number of pages in the dialog
95 virtual size_t GetPageCount() const { return m_pages
.size(); }
97 // get the panel which represents the given page
98 virtual wxWindow
*GetPage(size_t n
) const { return m_pages
[n
]; }
100 // get the current page or NULL if none
101 wxWindow
*GetCurrentPage() const
103 const int n
= GetSelection();
104 return n
== wxNOT_FOUND
? NULL
: GetPage(n
);
107 // get the currently selected page or wxNOT_FOUND if none
108 virtual int GetSelection() const { return m_selection
; }
110 // set/get the title of a page
111 virtual bool SetPageText(size_t n
, const wxString
& strText
) = 0;
112 virtual wxString
GetPageText(size_t n
) const = 0;
115 // image list stuff: each page may have an image associated with it (all
116 // images belong to the same image list)
117 // ---------------------------------------------------------------------
119 // sets/returns item's image index in the current image list
120 virtual int GetPageImage(size_t n
) const = 0;
121 virtual bool SetPageImage(size_t n
, int imageId
) = 0;
127 // resize the notebook so that all pages will have the specified size
128 virtual void SetPageSize(const wxSize
& size
);
130 // return the size of the area needed to accommodate the controller
131 wxSize
GetControllerSize() const;
133 // calculate the size of the control from the size of its page
135 // by default this simply returns size enough to fit both the page and the
137 virtual wxSize
CalcSizeFromPage(const wxSize
& sizePage
) const;
139 // get/set size of area between book control area and page area
140 unsigned int GetInternalBorder() const { return m_internalBorder
; }
141 void SetInternalBorder(unsigned int border
) { m_internalBorder
= border
; }
143 // Sets/gets the margin around the controller
144 void SetControlMargin(int margin
) { m_controlMargin
= margin
; }
145 int GetControlMargin() const { return m_controlMargin
; }
147 // returns true if we have wxBK_TOP or wxBK_BOTTOM style
148 bool IsVertical() const { return HasFlag(wxBK_BOTTOM
| wxBK_TOP
); }
150 // set/get option to shrink to fit current page
151 void SetFitToCurrentPage(bool fit
) { m_fitToCurrentPage
= fit
; }
152 bool GetFitToCurrentPage() const { return m_fitToCurrentPage
; }
154 // returns the sizer containing the control, if any
155 wxSizer
* GetControlSizer() const { return m_controlSizer
; }
161 // remove one page from the control and delete it
162 virtual bool DeletePage(size_t n
);
164 // remove one page from the notebook, without deleting it
165 virtual bool RemovePage(size_t n
)
167 DoInvalidateBestSize();
168 return DoRemovePage(n
) != NULL
;
171 // remove all pages and delete them
172 virtual bool DeleteAllPages()
174 m_selection
= wxNOT_FOUND
;
175 DoInvalidateBestSize();
176 WX_CLEAR_ARRAY(m_pages
);
180 // adds a new page to the control
181 virtual bool AddPage(wxWindow
*page
,
182 const wxString
& text
,
183 bool bSelect
= false,
184 int imageId
= NO_IMAGE
)
186 DoInvalidateBestSize();
187 return InsertPage(GetPageCount(), page
, text
, bSelect
, imageId
);
190 // the same as AddPage(), but adds the page at the specified position
191 virtual bool InsertPage(size_t n
,
193 const wxString
& text
,
194 bool bSelect
= false,
195 int imageId
= NO_IMAGE
) = 0;
197 // set the currently selected page, return the index of the previously
198 // selected one (or wxNOT_FOUND on error)
200 // NB: this function will generate PAGE_CHANGING/ED events
201 virtual int SetSelection(size_t n
) = 0;
203 // acts as SetSelection but does not generate events
204 virtual int ChangeSelection(size_t n
) = 0;
207 // cycle thru the pages
208 void AdvanceSelection(bool forward
= true)
210 int nPage
= GetNextPage(forward
);
211 if ( nPage
!= wxNOT_FOUND
)
213 // cast is safe because of the check above
214 SetSelection((size_t)nPage
);
218 // return the index of the given page or wxNOT_FOUND
219 int FindPage(const wxWindow
* page
) const;
221 // hit test: returns which page is hit and, optionally, where (icon, label)
222 virtual int HitTest(const wxPoint
& WXUNUSED(pt
),
223 long * WXUNUSED(flags
) = NULL
) const
229 // we do have multiple pages
230 virtual bool HasMultiplePages() const { return true; }
232 // we don't want focus for ourselves
233 virtual bool AcceptsFocus() const { return false; }
235 // returns true if the platform should explicitly apply a theme border
236 virtual bool CanApplyThemeBorder() const { return false; }
239 // flags for DoSetSelection()
242 SetSelection_SendEvent
= 1
245 // choose the default border for this window
246 virtual wxBorder
GetDefaultBorder() const { return wxBORDER_NONE
; }
248 // After the insertion of the page in the method InsertPage, calling this
249 // method sets the selection to the given page or the first one if there is
250 // still no selection. The "selection changed" event is sent only if
251 // bSelect is true, so when it is false, no event is sent even if the
252 // selection changed from wxNOT_FOUND to 0 when inserting the first page.
254 // Returns true if the selection was set to the specified page (explicitly
255 // because of bSelect == true or implicitly because it's the first page) or
257 bool DoSetSelectionAfterInsertion(size_t n
, bool bSelect
);
259 // Update the selection after removing the page at the given index,
260 // typically called from the derived class overridden DoRemovePage().
261 void DoSetSelectionAfterRemoval(size_t n
);
263 // set the selection to the given page, sending the events (which can
264 // possibly prevent the page change from taking place) if SendEvent flag is
266 virtual int DoSetSelection(size_t nPage
, int flags
= 0);
268 // if the derived class uses DoSetSelection() for implementing
269 // [Set|Change]Selection, it must override UpdateSelectedPage(),
270 // CreatePageChangingEvent() and MakeChangedEvent(), but as it might not
271 // use it, these functions are not pure virtual
273 // called to notify the control about a new current page
274 virtual void UpdateSelectedPage(size_t WXUNUSED(newsel
))
275 { wxFAIL_MSG(wxT("Override this function!")); }
277 // create a new "page changing" event
278 virtual wxBookCtrlEvent
* CreatePageChangingEvent() const
279 { wxFAIL_MSG(wxT("Override this function!")); return NULL
; }
281 // modify the event created by CreatePageChangingEvent() to "page changed"
282 // event, usually by just calling SetEventType() on it
283 virtual void MakeChangedEvent(wxBookCtrlEvent
& WXUNUSED(event
))
284 { wxFAIL_MSG(wxT("Override this function!")); }
287 // The derived class also may override the following method, also called
288 // from DoSetSelection(), to show/hide pages differently.
289 virtual void DoShowPage(wxWindow
* page
, bool show
) { page
->Show(show
); }
292 // Should we accept NULL page pointers in Add/InsertPage()?
294 // Default is no but derived classes may override it if they can treat NULL
295 // pages in some sensible way (e.g. wxTreebook overrides this to allow
296 // having nodes without any associated page)
297 virtual bool AllowNullPage() const { return false; }
299 // Remove the page and return a pointer to it.
301 // It also needs to update the current selection if necessary, i.e. if the
302 // page being removed comes before the selected one and the helper method
303 // DoSetSelectionAfterRemoval() can be used for this.
304 virtual wxWindow
*DoRemovePage(size_t page
) = 0;
306 // our best size is the size which fits all our pages
307 virtual wxSize
DoGetBestSize() const;
309 // helper: get the next page wrapping if we reached the end
310 int GetNextPage(bool forward
) const;
313 virtual void DoSize();
315 // This method also invalidates the size of the controller and should be
316 // called instead of just InvalidateBestSize() whenever pages are added or
317 // removed as this also affects the controller
318 void DoInvalidateBestSize();
321 // Show the help for the corresponding page
322 void OnHelp(wxHelpEvent
& event
);
326 // the array of all pages of this control
327 wxArrayPages m_pages
;
330 virtual wxRect
GetPageRect() const;
333 void OnSize(wxSizeEvent
& event
);
335 // controller buddy if available, NULL otherwise (usually for native book controls like wxNotebook)
336 wxControl
*m_bookctrl
;
338 // Whether to shrink to fit current page
339 bool m_fitToCurrentPage
;
341 // the sizer containing the choice control
342 wxSizer
*m_controlSizer
;
344 // the margin around the choice control
347 // The currently selected page (in range 0..m_pages.size()-1 inclusive) or
348 // wxNOT_FOUND if none (this can normally only be the case for an empty
349 // control without any pages).
354 // common part of all ctors
358 unsigned int m_internalBorder
;
360 DECLARE_ABSTRACT_CLASS(wxBookCtrlBase
)
361 wxDECLARE_NO_COPY_CLASS(wxBookCtrlBase
);
363 DECLARE_EVENT_TABLE()
366 // ----------------------------------------------------------------------------
367 // wxBookCtrlEvent: page changing events generated by book classes
368 // ----------------------------------------------------------------------------
370 class WXDLLIMPEXP_CORE wxBookCtrlEvent
: public wxNotifyEvent
373 wxBookCtrlEvent(wxEventType commandType
= wxEVT_NULL
, int winid
= 0,
374 int nSel
= wxNOT_FOUND
, int nOldSel
= wxNOT_FOUND
)
375 : wxNotifyEvent(commandType
, winid
)
381 wxBookCtrlEvent(const wxBookCtrlEvent
& event
)
382 : wxNotifyEvent(event
)
384 m_nSel
= event
.m_nSel
;
385 m_nOldSel
= event
.m_nOldSel
;
388 virtual wxEvent
*Clone() const { return new wxBookCtrlEvent(*this); }
391 // the currently selected page (wxNOT_FOUND if none)
392 int GetSelection() const { return m_nSel
; }
393 void SetSelection(int nSel
) { m_nSel
= nSel
; }
394 // the page that was selected before the change (wxNOT_FOUND if none)
395 int GetOldSelection() const { return m_nOldSel
; }
396 void SetOldSelection(int nOldSel
) { m_nOldSel
= nOldSel
; }
399 int m_nSel
, // currently selected page
400 m_nOldSel
; // previously selected page
402 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxBookCtrlEvent
)
405 typedef void (wxEvtHandler::*wxBookCtrlEventFunction
)(wxBookCtrlEvent
&);
407 #define wxBookCtrlEventHandler(func) \
408 wxEVENT_HANDLER_CAST(wxBookCtrlEventFunction, func)
410 // obsolete name, defined for compatibility only
411 #define wxBookCtrlBaseEvent wxBookCtrlEvent
413 // make a default book control for given platform
415 // dedicated to majority of desktops
416 #include "wx/notebook.h"
417 #define wxBookCtrl wxNotebook
418 #define wxEVT_BOOKCTRL_PAGE_CHANGED wxEVT_NOTEBOOK_PAGE_CHANGED
419 #define wxEVT_BOOKCTRL_PAGE_CHANGING wxEVT_NOTEBOOK_PAGE_CHANGING
420 #define EVT_BOOKCTRL_PAGE_CHANGED(id, fn) EVT_NOTEBOOK_PAGE_CHANGED(id, fn)
421 #define EVT_BOOKCTRL_PAGE_CHANGING(id, fn) EVT_NOTEBOOK_PAGE_CHANGING(id, fn)
423 // dedicated to Smartphones
424 #include "wx/choicebk.h"
425 #define wxBookCtrl wxChoicebook
426 #define wxEVT_BOOKCTRL_PAGE_CHANGED wxEVT_CHOICEBOOK_PAGE_CHANGED
427 #define wxEVT_BOOKCTRL_PAGE_CHANGING wxEVT_CHOICEBOOK_PAGE_CHANGING
428 #define EVT_BOOKCTRL_PAGE_CHANGED(id, fn) EVT_CHOICEBOOK_PAGE_CHANGED(id, fn)
429 #define EVT_BOOKCTRL_PAGE_CHANGING(id, fn) EVT_CHOICEBOOK_PAGE_CHANGING(id, fn)
432 // old wxEVT_COMMAND_* constants
433 #define wxEVT_COMMAND_BOOKCTRL_PAGE_CHANGED wxEVT_BOOKCTRL_PAGE_CHANGED
434 #define wxEVT_COMMAND_BOOKCTRL_PAGE_CHANGING wxEVT_BOOKCTRL_PAGE_CHANGING
436 #if WXWIN_COMPATIBILITY_2_6
437 #define wxBC_TOP wxBK_TOP
438 #define wxBC_BOTTOM wxBK_BOTTOM
439 #define wxBC_LEFT wxBK_LEFT
440 #define wxBC_RIGHT wxBK_RIGHT
441 #define wxBC_DEFAULT wxBK_DEFAULT
444 #endif // wxUSE_BOOKCTRL
446 #endif // _WX_BOOKCTRL_H_