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"
26 WX_DEFINE_EXPORTED_ARRAY_PTR(wxWindow
*, wxArrayPages
);
28 class WXDLLIMPEXP_FWD_CORE wxImageList
;
29 class WXDLLIMPEXP_FWD_CORE wxBookCtrlBaseEvent
;
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 WXDLLEXPORT wxBookCtrlBase
: public wxControl
68 wxBookCtrlBase(wxWindow
*parent
,
70 const wxPoint
& pos
= wxDefaultPosition
,
71 const wxSize
& size
= wxDefaultSize
,
73 const wxString
& name
= wxEmptyString
)
77 (void)Create(parent
, winid
, pos
, size
, style
, name
);
81 bool Create(wxWindow
*parent
,
83 const wxPoint
& pos
= wxDefaultPosition
,
84 const wxSize
& size
= wxDefaultSize
,
86 const wxString
& name
= wxEmptyString
);
89 virtual ~wxBookCtrlBase();
95 // get number of pages in the dialog
96 virtual size_t GetPageCount() const { return m_pages
.size(); }
98 // get the panel which represents the given page
99 wxWindow
*GetPage(size_t n
) { return m_pages
[n
]; }
100 wxWindow
*GetPage(size_t n
) const { return m_pages
[n
]; }
102 // get the current page or NULL if none
103 wxWindow
*GetCurrentPage() const
105 const int n
= GetSelection();
106 return n
== wxNOT_FOUND
? NULL
: GetPage(n
);
109 // get the currently selected page or wxNOT_FOUND if none
110 virtual int GetSelection() const = 0;
112 // set/get the title of a page
113 virtual bool SetPageText(size_t n
, const wxString
& strText
) = 0;
114 virtual wxString
GetPageText(size_t n
) const = 0;
117 // image list stuff: each page may have an image associated with it (all
118 // images belong to the same image list)
119 // ---------------------------------------------------------------------
121 // sets the image list to use, it is *not* deleted by the control
122 virtual void SetImageList(wxImageList
*imageList
);
124 // as SetImageList() but we will delete the image list ourselves
125 void AssignImageList(wxImageList
*imageList
);
127 // get pointer (may be NULL) to the associated image list
128 wxImageList
* GetImageList() const { return m_imageList
; }
130 // sets/returns item's image index in the current image list
131 virtual int GetPageImage(size_t n
) const = 0;
132 virtual bool SetPageImage(size_t n
, int imageId
) = 0;
138 // resize the notebook so that all pages will have the specified size
139 virtual void SetPageSize(const wxSize
& size
);
141 // calculate the size of the control from the size of its page
142 virtual wxSize
CalcSizeFromPage(const wxSize
& sizePage
) const = 0;
144 // get/set size of area between book control area and page area
145 unsigned int GetInternalBorder() const { return m_internalBorder
; }
146 void SetInternalBorder(unsigned int border
) { m_internalBorder
= border
; }
148 // Sets/gets the margin around the controller
149 void SetControlMargin(int margin
) { m_controlMargin
= margin
; }
150 int GetControlMargin() const { return m_controlMargin
; }
152 // returns true if we have wxBK_TOP or wxBK_BOTTOM style
153 bool IsVertical() const { return HasFlag(wxBK_BOTTOM
| wxBK_TOP
); }
155 // set/get option to shrink to fit current page
156 void SetFitToCurrentPage(bool fit
) { m_fitToCurrentPage
= fit
; }
157 bool GetFitToCurrentPage() const { return m_fitToCurrentPage
; }
159 // returns the sizer containing the control, if any
160 wxSizer
* GetControlSizer() const { return m_controlSizer
; }
166 // remove one page from the control and delete it
167 virtual bool DeletePage(size_t n
);
169 // remove one page from the notebook, without deleting it
170 virtual bool RemovePage(size_t n
)
172 DoInvalidateBestSize();
173 return DoRemovePage(n
) != NULL
;
176 // remove all pages and delete them
177 virtual bool DeleteAllPages()
179 DoInvalidateBestSize();
180 WX_CLEAR_ARRAY(m_pages
);
184 // adds a new page to the control
185 virtual bool AddPage(wxWindow
*page
,
186 const wxString
& text
,
187 bool bSelect
= false,
190 DoInvalidateBestSize();
191 return InsertPage(GetPageCount(), page
, text
, bSelect
, imageId
);
194 // the same as AddPage(), but adds the page at the specified position
195 virtual bool InsertPage(size_t n
,
197 const wxString
& text
,
198 bool bSelect
= false,
199 int imageId
= -1) = 0;
201 // set the currently selected page, return the index of the previously
202 // selected one (or -1 on error)
204 // NB: this function will generate PAGE_CHANGING/ED events
205 virtual int SetSelection(size_t n
) = 0;
207 // acts as SetSelection but does not generate events
208 virtual int ChangeSelection(size_t n
) = 0;
211 // cycle thru the pages
212 void AdvanceSelection(bool forward
= true)
214 int nPage
= GetNextPage(forward
);
217 // cast is safe because of the check above
218 SetSelection((size_t)nPage
);
222 // hit test: returns which page is hit and, optionally, where (icon, label)
223 virtual int HitTest(const wxPoint
& WXUNUSED(pt
),
224 long * WXUNUSED(flags
) = NULL
) const
230 // we do have multiple pages
231 virtual bool HasMultiplePages() const { return true; }
233 // we don't want focus for ourselves
234 virtual bool AcceptsFocus() const { return false; }
236 // returns true if the platform should explicitly apply a theme border
237 virtual bool CanApplyThemeBorder() const { return false; }
240 // flags for DoSetSelection()
243 SetSelection_SendEvent
= 1
246 // set the selection to the given page, sending the events (which can
247 // possibly prevent the page change from taking place) if SendEvent flag is
249 virtual int DoSetSelection(size_t nPage
, int flags
= 0);
251 // if the derived class uses DoSetSelection() for implementing
252 // [Set|Change]Selection, it must override UpdateSelectedPage(),
253 // CreatePageChangingEvent() and MakeChangedEvent(), but as it might not
254 // use it, these functions are not pure virtual
256 // called to notify the control about a new current page
257 virtual void UpdateSelectedPage(size_t WXUNUSED(newsel
))
258 { wxFAIL_MSG(wxT("Override this function!")); }
260 // create a new "page changing" event
261 virtual wxBookCtrlBaseEvent
* CreatePageChangingEvent() const
262 { wxFAIL_MSG(wxT("Override this function!")); return NULL
; }
264 // modify the event created by CreatePageChangingEvent() to "page changed"
265 // event, usually by just calling SetEventType() on it
266 virtual void MakeChangedEvent(wxBookCtrlBaseEvent
& WXUNUSED(event
))
267 { wxFAIL_MSG(wxT("Override this function!")); }
270 // Should we accept NULL page pointers in Add/InsertPage()?
272 // Default is no but derived classes may override it if they can treat NULL
273 // pages in some sensible way (e.g. wxTreebook overrides this to allow
274 // having nodes without any associated page)
275 virtual bool AllowNullPage() const { return false; }
277 // remove the page and return a pointer to it
278 virtual wxWindow
*DoRemovePage(size_t page
) = 0;
280 // our best size is the size which fits all our pages
281 virtual wxSize
DoGetBestSize() const;
283 // helper: get the next page wrapping if we reached the end
284 int GetNextPage(bool forward
) const;
289 // This method also invalidates the size of the controller and should be
290 // called instead of just InvalidateBestSize() whenever pages are added or
291 // removed as this also affects the controller
292 void DoInvalidateBestSize();
295 // Show the help for the corresponding page
296 void OnHelp(wxHelpEvent
& event
);
300 // the array of all pages of this control
301 wxArrayPages m_pages
;
303 // the associated image list or NULL
304 wxImageList
*m_imageList
;
306 // true if we must delete m_imageList
307 bool m_ownsImageList
;
310 wxRect
GetPageRect() const;
313 virtual wxSize
GetControllerSize() const;
314 void OnSize(wxSizeEvent
& event
);
316 // controller buddy if available, NULL otherwise (usually for native book controls like wxNotebook)
317 wxControl
*m_bookctrl
;
319 // Whether to shrink to fit current page
320 bool m_fitToCurrentPage
;
322 // the sizer containing the choice control
323 wxSizer
*m_controlSizer
;
325 // the margin around the choice control
330 // common part of all ctors
334 unsigned int m_internalBorder
;
336 DECLARE_ABSTRACT_CLASS(wxBookCtrlBase
)
337 DECLARE_NO_COPY_CLASS(wxBookCtrlBase
)
338 DECLARE_EVENT_TABLE()
341 // ----------------------------------------------------------------------------
342 // wxBookCtrlBaseEvent: page changing events generated by derived classes
343 // ----------------------------------------------------------------------------
345 class WXDLLEXPORT wxBookCtrlBaseEvent
: public wxNotifyEvent
348 wxBookCtrlBaseEvent(wxEventType commandType
= wxEVT_NULL
, int winid
= 0,
349 int nSel
= -1, int nOldSel
= -1)
350 : wxNotifyEvent(commandType
, winid
)
356 wxBookCtrlBaseEvent(const wxBookCtrlBaseEvent
& event
)
357 : wxNotifyEvent(event
)
359 m_nSel
= event
.m_nSel
;
360 m_nOldSel
= event
.m_nOldSel
;
364 // the currently selected page (-1 if none)
365 int GetSelection() const { return m_nSel
; }
366 void SetSelection(int nSel
) { m_nSel
= nSel
; }
367 // the page that was selected before the change (-1 if none)
368 int GetOldSelection() const { return m_nOldSel
; }
369 void SetOldSelection(int nOldSel
) { m_nOldSel
= nOldSel
; }
372 int m_nSel
, // currently selected page
373 m_nOldSel
; // previously selected page
376 // make a default book control for given platform
378 // dedicated to majority of desktops
379 #include "wx/notebook.h"
380 #define wxBookCtrl wxNotebook
381 #define wxBookCtrlEvent wxNotebookEvent
382 #define wxEVT_COMMAND_BOOKCTRL_PAGE_CHANGED wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
383 #define wxEVT_COMMAND_BOOKCTRL_PAGE_CHANGING wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
384 #define EVT_BOOKCTRL_PAGE_CHANGED(id, fn) EVT_NOTEBOOK_PAGE_CHANGED(id, fn)
385 #define EVT_BOOKCTRL_PAGE_CHANGING(id, fn) EVT_NOTEBOOK_PAGE_CHANGING(id, fn)
386 #define wxBookctrlEventHandler(func) wxNotebookEventHandler(func)
388 // dedicated to Smartphones
389 #include "wx/choicebk.h"
390 #define wxBookCtrl wxChoicebook
391 #define wxBookCtrlEvent wxChoicebookEvent
392 #define wxEVT_COMMAND_BOOKCTRL_PAGE_CHANGED wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGED
393 #define wxEVT_COMMAND_BOOKCTRL_PAGE_CHANGING wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGING
394 #define EVT_BOOKCTRL_PAGE_CHANGED(id, fn) EVT_CHOICEBOOK_PAGE_CHANGED(id, fn)
395 #define EVT_BOOKCTRL_PAGE_CHANGING(id, fn) EVT_CHOICEBOOK_PAGE_CHANGING(id, fn)
396 #define wxBookctrlEventHandler(func) wxChoicebookEventHandler(func)
399 #if WXWIN_COMPATIBILITY_2_6
400 #define wxBC_TOP wxBK_TOP
401 #define wxBC_BOTTOM wxBK_BOTTOM
402 #define wxBC_LEFT wxBK_LEFT
403 #define wxBC_RIGHT wxBK_RIGHT
404 #define wxBC_DEFAULT wxBK_DEFAULT
407 #endif // wxUSE_BOOKCTRL
409 #endif // _WX_BOOKCTRL_H_