]>
Commit | Line | Data |
---|---|---|
15aad3b9 VZ |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: wx/bookctrl.h | |
61c083e7 | 3 | // Purpose: wxBookCtrlBase: common base class for wxList/Tree/Notebook |
15aad3b9 VZ |
4 | // Author: Vadim Zeitlin |
5 | // Modified by: | |
6 | // Created: 19.08.03 | |
7 | // RCS-ID: $Id$ | |
77ffb593 | 8 | // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwidgets.org> |
65571936 | 9 | // Licence: wxWindows licence |
15aad3b9 VZ |
10 | /////////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | #ifndef _WX_BOOKCTRL_H_ | |
13 | #define _WX_BOOKCTRL_H_ | |
14 | ||
15aad3b9 VZ |
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" | |
abfdefed | 25 | #include "wx/withimages.h" |
15aad3b9 | 26 | |
d5d29b8a | 27 | WX_DEFINE_EXPORTED_ARRAY_PTR(wxWindow *, wxArrayPages); |
15aad3b9 | 28 | |
b5dbe15d | 29 | class WXDLLIMPEXP_FWD_CORE wxImageList; |
3e97a905 | 30 | class WXDLLIMPEXP_FWD_CORE wxBookCtrlEvent; |
9804d540 WS |
31 | |
32 | // ---------------------------------------------------------------------------- | |
33 | // constants | |
34 | // ---------------------------------------------------------------------------- | |
35 | ||
36 | // wxBookCtrl hit results | |
37 | enum | |
38 | { | |
39 | wxBK_HITTEST_NOWHERE = 1, // not on tab | |
40 | wxBK_HITTEST_ONICON = 2, // on icon | |
41 | wxBK_HITTEST_ONLABEL = 4, // on label | |
42 | wxBK_HITTEST_ONITEM = wxBK_HITTEST_ONICON | wxBK_HITTEST_ONLABEL, | |
43 | wxBK_HITTEST_ONPAGE = 8 // not on tab control, but over the selected page | |
44 | }; | |
15aad3b9 | 45 | |
73a0b98c VZ |
46 | // wxBookCtrl flags (common for wxNotebook, wxListbook, wxChoicebook, wxTreebook) |
47 | #define wxBK_DEFAULT 0x0000 | |
48 | #define wxBK_TOP 0x0010 | |
49 | #define wxBK_BOTTOM 0x0020 | |
50 | #define wxBK_LEFT 0x0040 | |
51 | #define wxBK_RIGHT 0x0080 | |
52 | #define wxBK_ALIGN_MASK (wxBK_TOP | wxBK_BOTTOM | wxBK_LEFT | wxBK_RIGHT) | |
53 | ||
15aad3b9 | 54 | // ---------------------------------------------------------------------------- |
61c083e7 | 55 | // wxBookCtrlBase |
15aad3b9 VZ |
56 | // ---------------------------------------------------------------------------- |
57 | ||
abfdefed VZ |
58 | class WXDLLIMPEXP_CORE wxBookCtrlBase : public wxControl, |
59 | public wxWithImages | |
15aad3b9 VZ |
60 | { |
61 | public: | |
62 | // construction | |
63 | // ------------ | |
64 | ||
61c083e7 | 65 | wxBookCtrlBase() |
6463b9f5 JS |
66 | { |
67 | Init(); | |
68 | } | |
15aad3b9 | 69 | |
61c083e7 WS |
70 | wxBookCtrlBase(wxWindow *parent, |
71 | wxWindowID winid, | |
72 | const wxPoint& pos = wxDefaultPosition, | |
73 | const wxSize& size = wxDefaultSize, | |
74 | long style = 0, | |
75 | const wxString& name = wxEmptyString) | |
6463b9f5 JS |
76 | { |
77 | Init(); | |
78 | ||
79 | (void)Create(parent, winid, pos, size, style, name); | |
80 | } | |
15aad3b9 VZ |
81 | |
82 | // quasi ctor | |
83 | bool Create(wxWindow *parent, | |
aa6f64c7 | 84 | wxWindowID winid, |
15aad3b9 VZ |
85 | const wxPoint& pos = wxDefaultPosition, |
86 | const wxSize& size = wxDefaultSize, | |
87 | long style = 0, | |
88 | const wxString& name = wxEmptyString); | |
89 | ||
15aad3b9 VZ |
90 | |
91 | // accessors | |
92 | // --------- | |
93 | ||
94 | // get number of pages in the dialog | |
95 | virtual size_t GetPageCount() const { return m_pages.size(); } | |
96 | ||
97 | // get the panel which represents the given page | |
97c58531 | 98 | wxWindow *GetPage(size_t n) const { return m_pages[n]; } |
15aad3b9 | 99 | |
21db32c1 VZ |
100 | // get the current page or NULL if none |
101 | wxWindow *GetCurrentPage() const | |
102 | { | |
97c58531 VZ |
103 | const int n = GetSelection(); |
104 | return n == wxNOT_FOUND ? NULL : GetPage(n); | |
21db32c1 VZ |
105 | } |
106 | ||
15aad3b9 | 107 | // get the currently selected page or wxNOT_FOUND if none |
c78da4eb | 108 | virtual int GetSelection() const { return m_selection; } |
15aad3b9 VZ |
109 | |
110 | // set/get the title of a page | |
111 | virtual bool SetPageText(size_t n, const wxString& strText) = 0; | |
112 | virtual wxString GetPageText(size_t n) const = 0; | |
113 | ||
114 | ||
115 | // image list stuff: each page may have an image associated with it (all | |
116 | // images belong to the same image list) | |
117 | // --------------------------------------------------------------------- | |
118 | ||
15aad3b9 VZ |
119 | // sets/returns item's image index in the current image list |
120 | virtual int GetPageImage(size_t n) const = 0; | |
121 | virtual bool SetPageImage(size_t n, int imageId) = 0; | |
122 | ||
123 | ||
124 | // geometry | |
125 | // -------- | |
126 | ||
127 | // resize the notebook so that all pages will have the specified size | |
128 | virtual void SetPageSize(const wxSize& size); | |
129 | ||
1d2b7f06 VZ |
130 | // return the size of the area needed to accommodate the controller |
131 | wxSize GetControllerSize() const; | |
132 | ||
15aad3b9 | 133 | // calculate the size of the control from the size of its page |
175363f6 VZ |
134 | // |
135 | // by default this simply returns size enough to fit both the page and the | |
136 | // controller | |
137 | virtual wxSize CalcSizeFromPage(const wxSize& sizePage) const; | |
15aad3b9 | 138 | |
159e6235 | 139 | // get/set size of area between book control area and page area |
851b88c3 VZ |
140 | unsigned int GetInternalBorder() const { return m_internalBorder; } |
141 | void SetInternalBorder(unsigned int border) { m_internalBorder = border; } | |
15aad3b9 | 142 | |
87cf52d8 JS |
143 | // Sets/gets the margin around the controller |
144 | void SetControlMargin(int margin) { m_controlMargin = margin; } | |
145 | int GetControlMargin() const { return m_controlMargin; } | |
146 | ||
e0d5d9af | 147 | // returns true if we have wxBK_TOP or wxBK_BOTTOM style |
90f9b8ef | 148 | bool IsVertical() const { return HasFlag(wxBK_BOTTOM | wxBK_TOP); } |
d8fd7acb | 149 | |
93bfe545 | 150 | // set/get option to shrink to fit current page |
da817fa6 JS |
151 | void SetFitToCurrentPage(bool fit) { m_fitToCurrentPage = fit; } |
152 | bool GetFitToCurrentPage() const { return m_fitToCurrentPage; } | |
93bfe545 | 153 | |
87cf52d8 JS |
154 | // returns the sizer containing the control, if any |
155 | wxSizer* GetControlSizer() const { return m_controlSizer; } | |
156 | ||
3368424a | 157 | |
15aad3b9 VZ |
158 | // operations |
159 | // ---------- | |
160 | ||
161 | // remove one page from the control and delete it | |
162 | virtual bool DeletePage(size_t n); | |
163 | ||
164 | // remove one page from the notebook, without deleting it | |
37144cf0 RD |
165 | virtual bool RemovePage(size_t n) |
166 | { | |
e8a147a6 | 167 | DoInvalidateBestSize(); |
37144cf0 RD |
168 | return DoRemovePage(n) != NULL; |
169 | } | |
15aad3b9 VZ |
170 | |
171 | // remove all pages and delete them | |
37144cf0 RD |
172 | virtual bool DeleteAllPages() |
173 | { | |
681be2ef | 174 | m_selection = wxNOT_FOUND; |
e8a147a6 | 175 | DoInvalidateBestSize(); |
37144cf0 RD |
176 | WX_CLEAR_ARRAY(m_pages); |
177 | return true; | |
178 | } | |
15aad3b9 VZ |
179 | |
180 | // adds a new page to the control | |
181 | virtual bool AddPage(wxWindow *page, | |
182 | const wxString& text, | |
183 | bool bSelect = false, | |
1871b9fa | 184 | int imageId = NO_IMAGE) |
15aad3b9 | 185 | { |
e8a147a6 | 186 | DoInvalidateBestSize(); |
15aad3b9 VZ |
187 | return InsertPage(GetPageCount(), page, text, bSelect, imageId); |
188 | } | |
189 | ||
190 | // the same as AddPage(), but adds the page at the specified position | |
191 | virtual bool InsertPage(size_t n, | |
192 | wxWindow *page, | |
193 | const wxString& text, | |
194 | bool bSelect = false, | |
1871b9fa | 195 | int imageId = NO_IMAGE) = 0; |
15aad3b9 VZ |
196 | |
197 | // set the currently selected page, return the index of the previously | |
7e837615 | 198 | // selected one (or wxNOT_FOUND on error) |
15aad3b9 | 199 | // |
1f30c176 | 200 | // NB: this function will generate PAGE_CHANGING/ED events |
15aad3b9 VZ |
201 | virtual int SetSelection(size_t n) = 0; |
202 | ||
1d6fcbcc VZ |
203 | // acts as SetSelection but does not generate events |
204 | virtual int ChangeSelection(size_t n) = 0; | |
205 | ||
15aad3b9 VZ |
206 | |
207 | // cycle thru the pages | |
208 | void AdvanceSelection(bool forward = true) | |
209 | { | |
210 | int nPage = GetNextPage(forward); | |
7e837615 | 211 | if ( nPage != wxNOT_FOUND ) |
15aad3b9 VZ |
212 | { |
213 | // cast is safe because of the check above | |
214 | SetSelection((size_t)nPage); | |
215 | } | |
216 | } | |
217 | ||
851b88c3 VZ |
218 | // hit test: returns which page is hit and, optionally, where (icon, label) |
219 | virtual int HitTest(const wxPoint& WXUNUSED(pt), | |
220 | long * WXUNUSED(flags) = NULL) const | |
221 | { | |
222 | return wxNOT_FOUND; | |
223 | } | |
224 | ||
e71c530e VZ |
225 | |
226 | // we do have multiple pages | |
227 | virtual bool HasMultiplePages() const { return true; } | |
228 | ||
3368424a VZ |
229 | // we don't want focus for ourselves |
230 | virtual bool AcceptsFocus() const { return false; } | |
e8a147a6 | 231 | |
a047aff2 JS |
232 | // returns true if the platform should explicitly apply a theme border |
233 | virtual bool CanApplyThemeBorder() const { return false; } | |
234 | ||
15aad3b9 | 235 | protected: |
deb325e3 | 236 | // flags for DoSetSelection() |
1d6fcbcc VZ |
237 | enum |
238 | { | |
239 | SetSelection_SendEvent = 1 | |
240 | }; | |
241 | ||
dc797d8e JS |
242 | // choose the default border for this window |
243 | virtual wxBorder GetDefaultBorder() const { return wxBORDER_NONE; } | |
244 | ||
60d5c563 VZ |
245 | // After the insertion of the page in the method InsertPage, calling this |
246 | // method sets the selection to the given page or the first one if there is | |
247 | // still no selection. The "selection changed" event is sent only if | |
248 | // bSelect is true, so when it is false, no event is sent even if the | |
249 | // selection changed from wxNOT_FOUND to 0 when inserting the first page. | |
250 | // | |
251 | // Returns true if the selection was set to the specified page (explicitly | |
252 | // because of bSelect == true or implicitly because it's the first page) or | |
253 | // false otherwise. | |
254 | bool DoSetSelectionAfterInsertion(size_t n, bool bSelect); | |
255 | ||
deb325e3 VZ |
256 | // set the selection to the given page, sending the events (which can |
257 | // possibly prevent the page change from taking place) if SendEvent flag is | |
258 | // included | |
259 | virtual int DoSetSelection(size_t nPage, int flags = 0); | |
260 | ||
261 | // if the derived class uses DoSetSelection() for implementing | |
262 | // [Set|Change]Selection, it must override UpdateSelectedPage(), | |
263 | // CreatePageChangingEvent() and MakeChangedEvent(), but as it might not | |
264 | // use it, these functions are not pure virtual | |
265 | ||
266 | // called to notify the control about a new current page | |
1d6fcbcc VZ |
267 | virtual void UpdateSelectedPage(size_t WXUNUSED(newsel)) |
268 | { wxFAIL_MSG(wxT("Override this function!")); } | |
deb325e3 VZ |
269 | |
270 | // create a new "page changing" event | |
3e97a905 | 271 | virtual wxBookCtrlEvent* CreatePageChangingEvent() const |
deb325e3 VZ |
272 | { wxFAIL_MSG(wxT("Override this function!")); return NULL; } |
273 | ||
274 | // modify the event created by CreatePageChangingEvent() to "page changed" | |
275 | // event, usually by just calling SetEventType() on it | |
3e97a905 | 276 | virtual void MakeChangedEvent(wxBookCtrlEvent& WXUNUSED(event)) |
1d6fcbcc VZ |
277 | { wxFAIL_MSG(wxT("Override this function!")); } |
278 | ||
deb325e3 | 279 | |
eca15c0d VZ |
280 | // Should we accept NULL page pointers in Add/InsertPage()? |
281 | // | |
282 | // Default is no but derived classes may override it if they can treat NULL | |
283 | // pages in some sensible way (e.g. wxTreebook overrides this to allow | |
284 | // having nodes without any associated page) | |
285 | virtual bool AllowNullPage() const { return false; } | |
286 | ||
15aad3b9 VZ |
287 | // remove the page and return a pointer to it |
288 | virtual wxWindow *DoRemovePage(size_t page) = 0; | |
289 | ||
290 | // our best size is the size which fits all our pages | |
291 | virtual wxSize DoGetBestSize() const; | |
292 | ||
293 | // helper: get the next page wrapping if we reached the end | |
294 | int GetNextPage(bool forward) const; | |
295 | ||
233387bd | 296 | // Lay out controls |
95835bc4 | 297 | virtual void DoSize(); |
233387bd | 298 | |
e8a147a6 VZ |
299 | // This method also invalidates the size of the controller and should be |
300 | // called instead of just InvalidateBestSize() whenever pages are added or | |
301 | // removed as this also affects the controller | |
302 | void DoInvalidateBestSize(); | |
303 | ||
a18c21f0 VZ |
304 | #if wxUSE_HELP |
305 | // Show the help for the corresponding page | |
306 | void OnHelp(wxHelpEvent& event); | |
307 | #endif // wxUSE_HELP | |
308 | ||
309 | ||
15aad3b9 VZ |
310 | // the array of all pages of this control |
311 | wxArrayPages m_pages; | |
312 | ||
d8fd7acb | 313 | // get the page area |
95835bc4 | 314 | virtual wxRect GetPageRect() const; |
d8fd7acb WS |
315 | |
316 | // event handlers | |
d8fd7acb WS |
317 | void OnSize(wxSizeEvent& event); |
318 | ||
319 | // controller buddy if available, NULL otherwise (usually for native book controls like wxNotebook) | |
320 | wxControl *m_bookctrl; | |
321 | ||
93bfe545 | 322 | // Whether to shrink to fit current page |
da817fa6 | 323 | bool m_fitToCurrentPage; |
93bfe545 | 324 | |
87cf52d8 | 325 | // the sizer containing the choice control |
e8a147a6 | 326 | wxSizer *m_controlSizer; |
87cf52d8 JS |
327 | |
328 | // the margin around the choice control | |
e8a147a6 | 329 | int m_controlMargin; |
87cf52d8 | 330 | |
681be2ef VZ |
331 | // The currently selected page (in range 0..m_pages.size()-1 inclusive) or |
332 | // wxNOT_FOUND if none (this can normally only be the case for an empty | |
333 | // control without any pages). | |
334 | int m_selection; | |
335 | ||
159e6235 WS |
336 | private: |
337 | ||
338 | // common part of all ctors | |
339 | void Init(); | |
340 | ||
341 | // internal border | |
a5325ad6 | 342 | unsigned int m_internalBorder; |
15aad3b9 | 343 | |
d8fd7acb | 344 | DECLARE_ABSTRACT_CLASS(wxBookCtrlBase) |
c0c133e1 | 345 | wxDECLARE_NO_COPY_CLASS(wxBookCtrlBase); |
681be2ef | 346 | |
d8fd7acb | 347 | DECLARE_EVENT_TABLE() |
15aad3b9 VZ |
348 | }; |
349 | ||
350 | // ---------------------------------------------------------------------------- | |
3e97a905 | 351 | // wxBookCtrlEvent: page changing events generated by book classes |
15aad3b9 VZ |
352 | // ---------------------------------------------------------------------------- |
353 | ||
3e97a905 | 354 | class WXDLLIMPEXP_CORE wxBookCtrlEvent : public wxNotifyEvent |
15aad3b9 VZ |
355 | { |
356 | public: | |
3e97a905 | 357 | wxBookCtrlEvent(wxEventType commandType = wxEVT_NULL, int winid = 0, |
7e837615 | 358 | int nSel = wxNOT_FOUND, int nOldSel = wxNOT_FOUND) |
aa6f64c7 | 359 | : wxNotifyEvent(commandType, winid) |
b2f8e75a VZ |
360 | { |
361 | m_nSel = nSel; | |
362 | m_nOldSel = nOldSel; | |
363 | } | |
364 | ||
3e97a905 | 365 | wxBookCtrlEvent(const wxBookCtrlEvent& event) |
b2f8e75a VZ |
366 | : wxNotifyEvent(event) |
367 | { | |
368 | m_nSel = event.m_nSel; | |
369 | m_nOldSel = event.m_nOldSel; | |
370 | } | |
15aad3b9 | 371 | |
3e97a905 VZ |
372 | virtual wxEvent *Clone() const { return new wxBookCtrlEvent(*this); } |
373 | ||
15aad3b9 | 374 | // accessors |
7e837615 | 375 | // the currently selected page (wxNOT_FOUND if none) |
15aad3b9 VZ |
376 | int GetSelection() const { return m_nSel; } |
377 | void SetSelection(int nSel) { m_nSel = nSel; } | |
7e837615 | 378 | // the page that was selected before the change (wxNOT_FOUND if none) |
15aad3b9 VZ |
379 | int GetOldSelection() const { return m_nOldSel; } |
380 | void SetOldSelection(int nOldSel) { m_nOldSel = nOldSel; } | |
381 | ||
382 | private: | |
383 | int m_nSel, // currently selected page | |
384 | m_nOldSel; // previously selected page | |
3e97a905 VZ |
385 | |
386 | DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxBookCtrlEvent) | |
15aad3b9 VZ |
387 | }; |
388 | ||
3e97a905 VZ |
389 | typedef void (wxEvtHandler::*wxBookCtrlEventFunction)(wxBookCtrlEvent&); |
390 | ||
391 | #define wxBookCtrlEventHandler(func) \ | |
3c778901 | 392 | wxEVENT_HANDLER_CAST(wxBookCtrlEventFunction, func) |
3e97a905 VZ |
393 | |
394 | // obsolete name, defined for compatibility only | |
395 | #define wxBookCtrlBaseEvent wxBookCtrlEvent | |
396 | ||
61c083e7 | 397 | // make a default book control for given platform |
311131d3 WS |
398 | #if wxUSE_NOTEBOOK |
399 | // dedicated to majority of desktops | |
36b79d44 | 400 | #include "wx/notebook.h" |
61c083e7 | 401 | #define wxBookCtrl wxNotebook |
61c083e7 WS |
402 | #define wxEVT_COMMAND_BOOKCTRL_PAGE_CHANGED wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED |
403 | #define wxEVT_COMMAND_BOOKCTRL_PAGE_CHANGING wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING | |
404 | #define EVT_BOOKCTRL_PAGE_CHANGED(id, fn) EVT_NOTEBOOK_PAGE_CHANGED(id, fn) | |
405 | #define EVT_BOOKCTRL_PAGE_CHANGING(id, fn) EVT_NOTEBOOK_PAGE_CHANGING(id, fn) | |
311131d3 WS |
406 | #else |
407 | // dedicated to Smartphones | |
408 | #include "wx/choicebk.h" | |
409 | #define wxBookCtrl wxChoicebook | |
311131d3 WS |
410 | #define wxEVT_COMMAND_BOOKCTRL_PAGE_CHANGED wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGED |
411 | #define wxEVT_COMMAND_BOOKCTRL_PAGE_CHANGING wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGING | |
412 | #define EVT_BOOKCTRL_PAGE_CHANGED(id, fn) EVT_CHOICEBOOK_PAGE_CHANGED(id, fn) | |
413 | #define EVT_BOOKCTRL_PAGE_CHANGING(id, fn) EVT_CHOICEBOOK_PAGE_CHANGING(id, fn) | |
d8fd7acb WS |
414 | #endif |
415 | ||
416 | #if WXWIN_COMPATIBILITY_2_6 | |
417 | #define wxBC_TOP wxBK_TOP | |
418 | #define wxBC_BOTTOM wxBK_BOTTOM | |
419 | #define wxBC_LEFT wxBK_LEFT | |
420 | #define wxBC_RIGHT wxBK_RIGHT | |
421 | #define wxBC_DEFAULT wxBK_DEFAULT | |
61c083e7 WS |
422 | #endif |
423 | ||
15aad3b9 VZ |
424 | #endif // wxUSE_BOOKCTRL |
425 | ||
426 | #endif // _WX_BOOKCTRL_H_ |