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 WXDLLEXPORT wxImageList
;
30 // ----------------------------------------------------------------------------
32 // ----------------------------------------------------------------------------
34 // wxBookCtrl hit results
37 wxBK_HITTEST_NOWHERE
= 1, // not on tab
38 wxBK_HITTEST_ONICON
= 2, // on icon
39 wxBK_HITTEST_ONLABEL
= 4, // on label
40 wxBK_HITTEST_ONITEM
= wxBK_HITTEST_ONICON
| wxBK_HITTEST_ONLABEL
,
41 wxBK_HITTEST_ONPAGE
= 8 // not on tab control, but over the selected page
44 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
48 class WXDLLEXPORT wxBookCtrlBase
: public wxControl
59 wxBookCtrlBase(wxWindow
*parent
,
61 const wxPoint
& pos
= wxDefaultPosition
,
62 const wxSize
& size
= wxDefaultSize
,
64 const wxString
& name
= wxEmptyString
)
68 (void)Create(parent
, winid
, pos
, size
, style
, name
);
72 bool Create(wxWindow
*parent
,
74 const wxPoint
& pos
= wxDefaultPosition
,
75 const wxSize
& size
= wxDefaultSize
,
77 const wxString
& name
= wxEmptyString
);
80 virtual ~wxBookCtrlBase();
86 // get number of pages in the dialog
87 virtual size_t GetPageCount() const { return m_pages
.size(); }
89 // get the panel which represents the given page
90 wxWindow
*GetPage(size_t n
) { return m_pages
[n
]; }
91 wxWindow
*GetPage(size_t n
) const { return m_pages
[n
]; }
93 // get the current page or NULL if none
94 wxWindow
*GetCurrentPage() const
96 const int n
= GetSelection();
97 return n
== wxNOT_FOUND
? NULL
: GetPage(n
);
100 // get the currently selected page or wxNOT_FOUND if none
101 virtual int GetSelection() const = 0;
103 // set/get the title of a page
104 virtual bool SetPageText(size_t n
, const wxString
& strText
) = 0;
105 virtual wxString
GetPageText(size_t n
) const = 0;
108 // image list stuff: each page may have an image associated with it (all
109 // images belong to the same image list)
110 // ---------------------------------------------------------------------
112 // sets the image list to use, it is *not* deleted by the control
113 virtual void SetImageList(wxImageList
*imageList
);
115 // as SetImageList() but we will delete the image list ourselves
116 void AssignImageList(wxImageList
*imageList
);
118 // get pointer (may be NULL) to the associated image list
119 wxImageList
* GetImageList() const { return m_imageList
; }
121 // sets/returns item's image index in the current image list
122 virtual int GetPageImage(size_t n
) const = 0;
123 virtual bool SetPageImage(size_t n
, int imageId
) = 0;
129 // resize the notebook so that all pages will have the specified size
130 virtual void SetPageSize(const wxSize
& size
);
132 // calculate the size of the control from the size of its page
133 virtual wxSize
CalcSizeFromPage(const wxSize
& sizePage
) const = 0;
135 // get/set size of area between book control area and page area
136 unsigned int GetInternalBorder() const { return m_internalBorder
; }
137 void SetInternalBorder(unsigned int border
) { m_internalBorder
= border
; }
139 // Sets/gets the margin around the controller
140 void SetControlMargin(int margin
) { m_controlMargin
= margin
; }
141 int GetControlMargin() const { return m_controlMargin
; }
143 // returns true if we have wxBK_TOP or wxBK_BOTTOM style
144 bool IsVertical() const { return HasFlag(wxBK_BOTTOM
| wxBK_TOP
); }
146 // set/get option to shrink to fit current page
147 void SetFitToCurrentPage(bool fit
) { m_fitToCurrentPage
= fit
; }
148 bool GetFitToCurrentPage() const { return m_fitToCurrentPage
; }
150 // returns the sizer containing the control, if any
151 wxSizer
* GetControlSizer() const { return m_controlSizer
; }
156 // remove one page from the control and delete it
157 virtual bool DeletePage(size_t n
);
159 // remove one page from the notebook, without deleting it
160 virtual bool RemovePage(size_t n
)
162 InvalidateBestSize();
163 return DoRemovePage(n
) != NULL
;
166 // remove all pages and delete them
167 virtual bool DeleteAllPages()
169 InvalidateBestSize();
170 WX_CLEAR_ARRAY(m_pages
);
174 // adds a new page to the control
175 virtual bool AddPage(wxWindow
*page
,
176 const wxString
& text
,
177 bool bSelect
= false,
180 InvalidateBestSize();
181 return InsertPage(GetPageCount(), page
, text
, bSelect
, imageId
);
184 // the same as AddPage(), but adds the page at the specified position
185 virtual bool InsertPage(size_t n
,
187 const wxString
& text
,
188 bool bSelect
= false,
189 int imageId
= -1) = 0;
191 // set the currently selected page, return the index of the previously
192 // selected one (or -1 on error)
194 // NB: this function will generate PAGE_CHANGING/ED events
195 virtual int SetSelection(size_t n
) = 0;
198 // cycle thru the pages
199 void AdvanceSelection(bool forward
= true)
201 int nPage
= GetNextPage(forward
);
204 // cast is safe because of the check above
205 SetSelection((size_t)nPage
);
209 // hit test: returns which page is hit and, optionally, where (icon, label)
210 virtual int HitTest(const wxPoint
& WXUNUSED(pt
),
211 long * WXUNUSED(flags
) = NULL
) const
217 // we do have multiple pages
218 virtual bool HasMultiplePages() const { return true; }
221 // Should we accept NULL page pointers in Add/InsertPage()?
223 // Default is no but derived classes may override it if they can treat NULL
224 // pages in some sensible way (e.g. wxTreebook overrides this to allow
225 // having nodes without any associated page)
226 virtual bool AllowNullPage() const { return false; }
228 // remove the page and return a pointer to it
229 virtual wxWindow
*DoRemovePage(size_t page
) = 0;
231 // our best size is the size which fits all our pages
232 virtual wxSize
DoGetBestSize() const;
234 // helper: get the next page wrapping if we reached the end
235 int GetNextPage(bool forward
) const;
237 // Always rely on GetBestSize, which will look at all the pages
238 virtual void SetInitialBestSize(const wxSize
& WXUNUSED(size
)) { }
244 // Show the help for the corresponding page
245 void OnHelp(wxHelpEvent
& event
);
249 // the array of all pages of this control
250 wxArrayPages m_pages
;
252 // the associated image list or NULL
253 wxImageList
*m_imageList
;
255 // true if we must delete m_imageList
256 bool m_ownsImageList
;
259 wxRect
GetPageRect() const;
262 virtual wxSize
GetControllerSize() const;
263 void OnSize(wxSizeEvent
& event
);
265 // controller buddy if available, NULL otherwise (usually for native book controls like wxNotebook)
266 wxControl
*m_bookctrl
;
268 // Whether to shrink to fit current page
269 bool m_fitToCurrentPage
;
271 // the sizer containing the choice control
272 wxSizer
* m_controlSizer
;
274 // the margin around the choice control
279 // common part of all ctors
283 unsigned int m_internalBorder
;
285 DECLARE_ABSTRACT_CLASS(wxBookCtrlBase
)
286 DECLARE_NO_COPY_CLASS(wxBookCtrlBase
)
287 DECLARE_EVENT_TABLE()
290 // ----------------------------------------------------------------------------
291 // wxBookCtrlBaseEvent: page changing events generated by derived classes
292 // ----------------------------------------------------------------------------
294 class WXDLLEXPORT wxBookCtrlBaseEvent
: public wxNotifyEvent
297 wxBookCtrlBaseEvent(wxEventType commandType
= wxEVT_NULL
, int winid
= 0,
298 int nSel
= -1, int nOldSel
= -1)
299 : wxNotifyEvent(commandType
, winid
)
305 wxBookCtrlBaseEvent(const wxBookCtrlBaseEvent
& event
)
306 : wxNotifyEvent(event
)
308 m_nSel
= event
.m_nSel
;
309 m_nOldSel
= event
.m_nOldSel
;
313 // the currently selected page (-1 if none)
314 int GetSelection() const { return m_nSel
; }
315 void SetSelection(int nSel
) { m_nSel
= nSel
; }
316 // the page that was selected before the change (-1 if none)
317 int GetOldSelection() const { return m_nOldSel
; }
318 void SetOldSelection(int nOldSel
) { m_nOldSel
= nOldSel
; }
321 int m_nSel
, // currently selected page
322 m_nOldSel
; // previously selected page
325 // make a default book control for given platform
327 // dedicated to majority of desktops
328 #include "wx/notebook.h"
329 #define wxBookCtrl wxNotebook
330 #define wxBookCtrlEvent wxNotebookEvent
331 #define wxEVT_COMMAND_BOOKCTRL_PAGE_CHANGED wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
332 #define wxEVT_COMMAND_BOOKCTRL_PAGE_CHANGING wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
333 #define EVT_BOOKCTRL_PAGE_CHANGED(id, fn) EVT_NOTEBOOK_PAGE_CHANGED(id, fn)
334 #define EVT_BOOKCTRL_PAGE_CHANGING(id, fn) EVT_NOTEBOOK_PAGE_CHANGING(id, fn)
336 // dedicated to Smartphones
337 #include "wx/choicebk.h"
338 #define wxBookCtrl wxChoicebook
339 #define wxBookCtrlEvent wxChoicebookEvent
340 #define wxEVT_COMMAND_BOOKCTRL_PAGE_CHANGED wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGED
341 #define wxEVT_COMMAND_BOOKCTRL_PAGE_CHANGING wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGING
342 #define EVT_BOOKCTRL_PAGE_CHANGED(id, fn) EVT_CHOICEBOOK_PAGE_CHANGED(id, fn)
343 #define EVT_BOOKCTRL_PAGE_CHANGING(id, fn) EVT_CHOICEBOOK_PAGE_CHANGING(id, fn)
346 #if WXWIN_COMPATIBILITY_2_6
347 #define wxBC_TOP wxBK_TOP
348 #define wxBC_BOTTOM wxBK_BOTTOM
349 #define wxBC_LEFT wxBK_LEFT
350 #define wxBC_RIGHT wxBK_RIGHT
351 #define wxBC_DEFAULT wxBK_DEFAULT
354 #endif // wxUSE_BOOKCTRL
356 #endif // _WX_BOOKCTRL_H_