1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxBookCtrlBase: common base class for wxList/Tree/Notebook
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwidgets.org>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 #ifndef _WX_BOOKCTRL_H_
12 #define _WX_BOOKCTRL_H_
14 // ----------------------------------------------------------------------------
16 // ----------------------------------------------------------------------------
22 #include "wx/control.h"
23 #include "wx/dynarray.h"
24 #include "wx/withimages.h"
26 WX_DEFINE_EXPORTED_ARRAY_PTR(wxWindow
*, wxArrayPages
);
28 class WXDLLIMPEXP_FWD_CORE wxImageList
;
29 class WXDLLIMPEXP_FWD_CORE wxBookCtrlEvent
;
31 // ----------------------------------------------------------------------------
33 // ----------------------------------------------------------------------------
35 // wxBookCtrl hit results
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
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)
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
57 class WXDLLIMPEXP_CORE wxBookCtrlBase
: public wxControl
,
69 wxBookCtrlBase(wxWindow
*parent
,
71 const wxPoint
& pos
= wxDefaultPosition
,
72 const wxSize
& size
= wxDefaultSize
,
74 const wxString
& name
= wxEmptyString
)
78 (void)Create(parent
, winid
, pos
, size
, style
, name
);
82 bool Create(wxWindow
*parent
,
84 const wxPoint
& pos
= wxDefaultPosition
,
85 const wxSize
& size
= wxDefaultSize
,
87 const wxString
& name
= wxEmptyString
);
93 // get number of pages in the dialog
94 virtual size_t GetPageCount() const { return m_pages
.size(); }
96 // get the panel which represents the given page
97 virtual wxWindow
*GetPage(size_t n
) const { return m_pages
[n
]; }
99 // get the current page or NULL if none
100 wxWindow
*GetCurrentPage() const
102 const int n
= GetSelection();
103 return n
== wxNOT_FOUND
? NULL
: GetPage(n
);
106 // get the currently selected page or wxNOT_FOUND if none
107 virtual int GetSelection() const { return m_selection
; }
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;
114 // image list stuff: each page may have an image associated with it (all
115 // images belong to the same image list)
116 // ---------------------------------------------------------------------
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;
126 // resize the notebook so that all pages will have the specified size
127 virtual void SetPageSize(const wxSize
& size
);
129 // return the size of the area needed to accommodate the controller
130 wxSize
GetControllerSize() const;
132 // calculate the size of the control from the size of its page
134 // by default this simply returns size enough to fit both the page and the
136 virtual wxSize
CalcSizeFromPage(const wxSize
& sizePage
) const;
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
; }
142 // Sets/gets the margin around the controller
143 void SetControlMargin(int margin
) { m_controlMargin
= margin
; }
144 int GetControlMargin() const { return m_controlMargin
; }
146 // returns true if we have wxBK_TOP or wxBK_BOTTOM style
147 bool IsVertical() const { return HasFlag(wxBK_BOTTOM
| wxBK_TOP
); }
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
; }
153 // returns the sizer containing the control, if any
154 wxSizer
* GetControlSizer() const { return m_controlSizer
; }
160 // remove one page from the control and delete it
161 virtual bool DeletePage(size_t n
);
163 // remove one page from the notebook, without deleting it
164 virtual bool RemovePage(size_t n
)
166 DoInvalidateBestSize();
167 return DoRemovePage(n
) != NULL
;
170 // remove all pages and delete them
171 virtual bool DeleteAllPages()
173 m_selection
= wxNOT_FOUND
;
174 DoInvalidateBestSize();
175 WX_CLEAR_ARRAY(m_pages
);
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
)
185 DoInvalidateBestSize();
186 return InsertPage(GetPageCount(), page
, text
, bSelect
, imageId
);
189 // the same as AddPage(), but adds the page at the specified position
190 virtual bool InsertPage(size_t n
,
192 const wxString
& text
,
193 bool bSelect
= false,
194 int imageId
= NO_IMAGE
) = 0;
196 // set the currently selected page, return the index of the previously
197 // selected one (or wxNOT_FOUND on error)
199 // NB: this function will generate PAGE_CHANGING/ED events
200 virtual int SetSelection(size_t n
) = 0;
202 // acts as SetSelection but does not generate events
203 virtual int ChangeSelection(size_t n
) = 0;
206 // cycle thru the pages
207 void AdvanceSelection(bool forward
= true)
209 int nPage
= GetNextPage(forward
);
210 if ( nPage
!= wxNOT_FOUND
)
212 // cast is safe because of the check above
213 SetSelection((size_t)nPage
);
217 // return the index of the given page or wxNOT_FOUND
218 int FindPage(const wxWindow
* page
) const;
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
228 // we do have multiple pages
229 virtual bool HasMultiplePages() const { return true; }
231 // we don't want focus for ourselves
232 virtual bool AcceptsFocus() const { return false; }
234 // returns true if the platform should explicitly apply a theme border
235 virtual bool CanApplyThemeBorder() const { return false; }
238 // flags for DoSetSelection()
241 SetSelection_SendEvent
= 1
244 // choose the default border for this window
245 virtual wxBorder
GetDefaultBorder() const { return wxBORDER_NONE
; }
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.
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
256 bool DoSetSelectionAfterInsertion(size_t n
, bool bSelect
);
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
);
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
265 virtual int DoSetSelection(size_t nPage
, int flags
= 0);
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
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!")); }
276 // create a new "page changing" event
277 virtual wxBookCtrlEvent
* CreatePageChangingEvent() const
278 { wxFAIL_MSG(wxT("Override this function!")); return NULL
; }
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!")); }
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
); }
291 // Should we accept NULL page pointers in Add/InsertPage()?
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; }
298 // Remove the page and return a pointer to it.
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;
305 // our best size is the size which fits all our pages
306 virtual wxSize
DoGetBestSize() const;
308 // helper: get the next page wrapping if we reached the end
309 int GetNextPage(bool forward
) const;
312 virtual void DoSize();
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();
320 // Show the help for the corresponding page
321 void OnHelp(wxHelpEvent
& event
);
325 // the array of all pages of this control
326 wxArrayPages m_pages
;
329 virtual wxRect
GetPageRect() const;
332 void OnSize(wxSizeEvent
& event
);
334 // controller buddy if available, NULL otherwise (usually for native book controls like wxNotebook)
335 wxControl
*m_bookctrl
;
337 // Whether to shrink to fit current page
338 bool m_fitToCurrentPage
;
340 // the sizer containing the choice control
341 wxSizer
*m_controlSizer
;
343 // the margin around the choice control
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).
353 // common part of all ctors
357 unsigned int m_internalBorder
;
359 DECLARE_ABSTRACT_CLASS(wxBookCtrlBase
)
360 wxDECLARE_NO_COPY_CLASS(wxBookCtrlBase
);
362 DECLARE_EVENT_TABLE()
365 // ----------------------------------------------------------------------------
366 // wxBookCtrlEvent: page changing events generated by book classes
367 // ----------------------------------------------------------------------------
369 class WXDLLIMPEXP_CORE wxBookCtrlEvent
: public wxNotifyEvent
372 wxBookCtrlEvent(wxEventType commandType
= wxEVT_NULL
, int winid
= 0,
373 int nSel
= wxNOT_FOUND
, int nOldSel
= wxNOT_FOUND
)
374 : wxNotifyEvent(commandType
, winid
)
380 wxBookCtrlEvent(const wxBookCtrlEvent
& event
)
381 : wxNotifyEvent(event
)
383 m_nSel
= event
.m_nSel
;
384 m_nOldSel
= event
.m_nOldSel
;
387 virtual wxEvent
*Clone() const { return new wxBookCtrlEvent(*this); }
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
; }
398 int m_nSel
, // currently selected page
399 m_nOldSel
; // previously selected page
401 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxBookCtrlEvent
)
404 typedef void (wxEvtHandler::*wxBookCtrlEventFunction
)(wxBookCtrlEvent
&);
406 #define wxBookCtrlEventHandler(func) \
407 wxEVENT_HANDLER_CAST(wxBookCtrlEventFunction, func)
409 // obsolete name, defined for compatibility only
410 #define wxBookCtrlBaseEvent wxBookCtrlEvent
412 // make a default book control for given platform
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)
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)
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
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
443 #endif // wxUSE_BOOKCTRL
445 #endif // _WX_BOOKCTRL_H_