wxNB_HITTEST_* flags renamed to wxBK_HITTEST_* to serve all book controls.
[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 // RCS-ID: $Id$
8 // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwidgets.org>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_BOOKCTRL_H_
13 #define _WX_BOOKCTRL_H_
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 #include "wx/defs.h"
20
21 #if wxUSE_BOOKCTRL
22
23 #include "wx/control.h"
24 #include "wx/dynarray.h"
25
26 WX_DEFINE_EXPORTED_ARRAY_PTR(wxWindow *, wxArrayPages);
27
28 class WXDLLEXPORT wxImageList;
29
30 // ----------------------------------------------------------------------------
31 // constants
32 // ----------------------------------------------------------------------------
33
34 // wxBookCtrl hit results
35 enum
36 {
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
42 };
43
44 // ----------------------------------------------------------------------------
45 // wxBookCtrlBase
46 // ----------------------------------------------------------------------------
47
48 class WXDLLEXPORT wxBookCtrlBase : public wxControl
49 {
50 public:
51 // construction
52 // ------------
53
54 wxBookCtrlBase()
55 {
56 Init();
57 }
58
59 wxBookCtrlBase(wxWindow *parent,
60 wxWindowID winid,
61 const wxPoint& pos = wxDefaultPosition,
62 const wxSize& size = wxDefaultSize,
63 long style = 0,
64 const wxString& name = wxEmptyString)
65 {
66 Init();
67
68 (void)Create(parent, winid, pos, size, style, name);
69 }
70
71 // quasi ctor
72 bool Create(wxWindow *parent,
73 wxWindowID winid,
74 const wxPoint& pos = wxDefaultPosition,
75 const wxSize& size = wxDefaultSize,
76 long style = 0,
77 const wxString& name = wxEmptyString);
78
79 // dtor
80 virtual ~wxBookCtrlBase();
81
82
83 // accessors
84 // ---------
85
86 // get number of pages in the dialog
87 virtual size_t GetPageCount() const { return m_pages.size(); }
88
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]; }
92
93 // get the current page or NULL if none
94 wxWindow *GetCurrentPage() const
95 {
96 const int n = GetSelection();
97 return n == wxNOT_FOUND ? NULL : GetPage(n);
98 }
99
100 // get the currently selected page or wxNOT_FOUND if none
101 virtual int GetSelection() const = 0;
102
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;
106
107
108 // image list stuff: each page may have an image associated with it (all
109 // images belong to the same image list)
110 // ---------------------------------------------------------------------
111
112 // sets the image list to use, it is *not* deleted by the control
113 virtual void SetImageList(wxImageList *imageList);
114
115 // as SetImageList() but we will delete the image list ourselves
116 void AssignImageList(wxImageList *imageList);
117
118 // get pointer (may be NULL) to the associated image list
119 wxImageList* GetImageList() const { return m_imageList; }
120
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;
124
125
126 // geometry
127 // --------
128
129 // resize the notebook so that all pages will have the specified size
130 virtual void SetPageSize(const wxSize& size);
131
132 // calculate the size of the control from the size of its page
133 virtual wxSize CalcSizeFromPage(const wxSize& sizePage) const = 0;
134
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; }
138
139 // Sets/gets the margin around the controller
140 void SetControlMargin(int margin) { m_controlMargin = margin; }
141 int GetControlMargin() const { return m_controlMargin; }
142
143 // returns true if we have wxBK_TOP or wxBK_BOTTOM style
144 bool IsVertical() const { return HasFlag(wxBK_BOTTOM | wxBK_TOP); }
145
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; }
149
150 // returns the sizer containing the control, if any
151 wxSizer* GetControlSizer() const { return m_controlSizer; }
152
153 // operations
154 // ----------
155
156 // remove one page from the control and delete it
157 virtual bool DeletePage(size_t n);
158
159 // remove one page from the notebook, without deleting it
160 virtual bool RemovePage(size_t n)
161 {
162 InvalidateBestSize();
163 return DoRemovePage(n) != NULL;
164 }
165
166 // remove all pages and delete them
167 virtual bool DeleteAllPages()
168 {
169 InvalidateBestSize();
170 WX_CLEAR_ARRAY(m_pages);
171 return true;
172 }
173
174 // adds a new page to the control
175 virtual bool AddPage(wxWindow *page,
176 const wxString& text,
177 bool bSelect = false,
178 int imageId = -1)
179 {
180 InvalidateBestSize();
181 return InsertPage(GetPageCount(), page, text, bSelect, imageId);
182 }
183
184 // the same as AddPage(), but adds the page at the specified position
185 virtual bool InsertPage(size_t n,
186 wxWindow *page,
187 const wxString& text,
188 bool bSelect = false,
189 int imageId = -1) = 0;
190
191 // set the currently selected page, return the index of the previously
192 // selected one (or -1 on error)
193 //
194 // NB: this function will generate PAGE_CHANGING/ED events
195 virtual int SetSelection(size_t n) = 0;
196
197
198 // cycle thru the pages
199 void AdvanceSelection(bool forward = true)
200 {
201 int nPage = GetNextPage(forward);
202 if ( nPage != -1 )
203 {
204 // cast is safe because of the check above
205 SetSelection((size_t)nPage);
206 }
207 }
208
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
212 {
213 return wxNOT_FOUND;
214 }
215
216 protected:
217 // Should we accept NULL page pointers in Add/InsertPage()?
218 //
219 // Default is no but derived classes may override it if they can treat NULL
220 // pages in some sensible way (e.g. wxTreebook overrides this to allow
221 // having nodes without any associated page)
222 virtual bool AllowNullPage() const { return false; }
223
224 // remove the page and return a pointer to it
225 virtual wxWindow *DoRemovePage(size_t page) = 0;
226
227 // our best size is the size which fits all our pages
228 virtual wxSize DoGetBestSize() const;
229
230 // helper: get the next page wrapping if we reached the end
231 int GetNextPage(bool forward) const;
232
233 // Always rely on GetBestSize, which will look at all the pages
234 virtual void SetInitialBestSize(const wxSize& WXUNUSED(size)) { }
235
236 // Lay out controls
237 void DoSize();
238
239 #if wxUSE_HELP
240 // Show the help for the corresponding page
241 void OnHelp(wxHelpEvent& event);
242 #endif // wxUSE_HELP
243
244
245 // the array of all pages of this control
246 wxArrayPages m_pages;
247
248 // the associated image list or NULL
249 wxImageList *m_imageList;
250
251 // true if we must delete m_imageList
252 bool m_ownsImageList;
253
254 // get the page area
255 wxRect GetPageRect() const;
256
257 // event handlers
258 virtual wxSize GetControllerSize() const;
259 void OnSize(wxSizeEvent& event);
260
261 // controller buddy if available, NULL otherwise (usually for native book controls like wxNotebook)
262 wxControl *m_bookctrl;
263
264 // Whether to shrink to fit current page
265 bool m_fitToCurrentPage;
266
267 // the sizer containing the choice control
268 wxSizer* m_controlSizer;
269
270 // the margin around the choice control
271 int m_controlMargin;
272
273 private:
274
275 // common part of all ctors
276 void Init();
277
278 // internal border
279 unsigned int m_internalBorder;
280
281 DECLARE_ABSTRACT_CLASS(wxBookCtrlBase)
282 DECLARE_NO_COPY_CLASS(wxBookCtrlBase)
283 DECLARE_EVENT_TABLE()
284 };
285
286 // ----------------------------------------------------------------------------
287 // wxBookCtrlBaseEvent: page changing events generated by derived classes
288 // ----------------------------------------------------------------------------
289
290 class WXDLLEXPORT wxBookCtrlBaseEvent : public wxNotifyEvent
291 {
292 public:
293 wxBookCtrlBaseEvent(wxEventType commandType = wxEVT_NULL, int winid = 0,
294 int nSel = -1, int nOldSel = -1)
295 : wxNotifyEvent(commandType, winid)
296 {
297 m_nSel = nSel;
298 m_nOldSel = nOldSel;
299 }
300
301 wxBookCtrlBaseEvent(const wxBookCtrlBaseEvent& event)
302 : wxNotifyEvent(event)
303 {
304 m_nSel = event.m_nSel;
305 m_nOldSel = event.m_nOldSel;
306 }
307
308 // accessors
309 // the currently selected page (-1 if none)
310 int GetSelection() const { return m_nSel; }
311 void SetSelection(int nSel) { m_nSel = nSel; }
312 // the page that was selected before the change (-1 if none)
313 int GetOldSelection() const { return m_nOldSel; }
314 void SetOldSelection(int nOldSel) { m_nOldSel = nOldSel; }
315
316 private:
317 int m_nSel, // currently selected page
318 m_nOldSel; // previously selected page
319 };
320
321 // make a default book control for given platform
322 #if wxUSE_NOTEBOOK
323 // dedicated to majority of desktops
324 #include "wx/notebook.h"
325 #define wxBookCtrl wxNotebook
326 #define wxBookCtrlEvent wxNotebookEvent
327 #define wxEVT_COMMAND_BOOKCTRL_PAGE_CHANGED wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
328 #define wxEVT_COMMAND_BOOKCTRL_PAGE_CHANGING wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
329 #define EVT_BOOKCTRL_PAGE_CHANGED(id, fn) EVT_NOTEBOOK_PAGE_CHANGED(id, fn)
330 #define EVT_BOOKCTRL_PAGE_CHANGING(id, fn) EVT_NOTEBOOK_PAGE_CHANGING(id, fn)
331 #else
332 // dedicated to Smartphones
333 #include "wx/choicebk.h"
334 #define wxBookCtrl wxChoicebook
335 #define wxBookCtrlEvent wxChoicebookEvent
336 #define wxEVT_COMMAND_BOOKCTRL_PAGE_CHANGED wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGED
337 #define wxEVT_COMMAND_BOOKCTRL_PAGE_CHANGING wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGING
338 #define EVT_BOOKCTRL_PAGE_CHANGED(id, fn) EVT_CHOICEBOOK_PAGE_CHANGED(id, fn)
339 #define EVT_BOOKCTRL_PAGE_CHANGING(id, fn) EVT_CHOICEBOOK_PAGE_CHANGING(id, fn)
340 #endif
341
342 #if WXWIN_COMPATIBILITY_2_6
343 #define wxBC_TOP wxBK_TOP
344 #define wxBC_BOTTOM wxBK_BOTTOM
345 #define wxBC_LEFT wxBK_LEFT
346 #define wxBC_RIGHT wxBK_RIGHT
347 #define wxBC_DEFAULT wxBK_DEFAULT
348 #endif
349
350 #endif // wxUSE_BOOKCTRL
351
352 #endif // _WX_BOOKCTRL_H_