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