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