bookctrl.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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. // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwidgets.org>
  8. // Licence: wxWindows licence
  9. ///////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_BOOKCTRL_H_
  11. #define _WX_BOOKCTRL_H_
  12. // ----------------------------------------------------------------------------
  13. // headers
  14. // ----------------------------------------------------------------------------
  15. #include "wx/defs.h"
  16. #if wxUSE_BOOKCTRL
  17. #include "wx/control.h"
  18. #include "wx/dynarray.h"
  19. #include "wx/withimages.h"
  20. WX_DEFINE_EXPORTED_ARRAY_PTR(wxWindow *, wxArrayPages);
  21. class WXDLLIMPEXP_FWD_CORE wxImageList;
  22. class WXDLLIMPEXP_FWD_CORE wxBookCtrlEvent;
  23. // ----------------------------------------------------------------------------
  24. // constants
  25. // ----------------------------------------------------------------------------
  26. // wxBookCtrl hit results
  27. enum
  28. {
  29. wxBK_HITTEST_NOWHERE = 1, // not on tab
  30. wxBK_HITTEST_ONICON = 2, // on icon
  31. wxBK_HITTEST_ONLABEL = 4, // on label
  32. wxBK_HITTEST_ONITEM = wxBK_HITTEST_ONICON | wxBK_HITTEST_ONLABEL,
  33. wxBK_HITTEST_ONPAGE = 8 // not on tab control, but over the selected page
  34. };
  35. // wxBookCtrl flags (common for wxNotebook, wxListbook, wxChoicebook, wxTreebook)
  36. #define wxBK_DEFAULT 0x0000
  37. #define wxBK_TOP 0x0010
  38. #define wxBK_BOTTOM 0x0020
  39. #define wxBK_LEFT 0x0040
  40. #define wxBK_RIGHT 0x0080
  41. #define wxBK_ALIGN_MASK (wxBK_TOP | wxBK_BOTTOM | wxBK_LEFT | wxBK_RIGHT)
  42. // ----------------------------------------------------------------------------
  43. // wxBookCtrlBase
  44. // ----------------------------------------------------------------------------
  45. class WXDLLIMPEXP_CORE wxBookCtrlBase : public wxControl,
  46. public wxWithImages
  47. {
  48. public:
  49. // construction
  50. // ------------
  51. wxBookCtrlBase()
  52. {
  53. Init();
  54. }
  55. wxBookCtrlBase(wxWindow *parent,
  56. wxWindowID winid,
  57. const wxPoint& pos = wxDefaultPosition,
  58. const wxSize& size = wxDefaultSize,
  59. long style = 0,
  60. const wxString& name = wxEmptyString)
  61. {
  62. Init();
  63. (void)Create(parent, winid, pos, size, style, name);
  64. }
  65. // quasi ctor
  66. bool Create(wxWindow *parent,
  67. wxWindowID winid,
  68. const wxPoint& pos = wxDefaultPosition,
  69. const wxSize& size = wxDefaultSize,
  70. long style = 0,
  71. const wxString& name = wxEmptyString);
  72. // accessors
  73. // ---------
  74. // get number of pages in the dialog
  75. virtual size_t GetPageCount() const { return m_pages.size(); }
  76. // get the panel which represents the given page
  77. virtual wxWindow *GetPage(size_t n) const { return m_pages[n]; }
  78. // get the current page or NULL if none
  79. wxWindow *GetCurrentPage() const
  80. {
  81. const int n = GetSelection();
  82. return n == wxNOT_FOUND ? NULL : GetPage(n);
  83. }
  84. // get the currently selected page or wxNOT_FOUND if none
  85. virtual int GetSelection() const { return m_selection; }
  86. // set/get the title of a page
  87. virtual bool SetPageText(size_t n, const wxString& strText) = 0;
  88. virtual wxString GetPageText(size_t n) const = 0;
  89. // image list stuff: each page may have an image associated with it (all
  90. // images belong to the same image list)
  91. // ---------------------------------------------------------------------
  92. // sets/returns item's image index in the current image list
  93. virtual int GetPageImage(size_t n) const = 0;
  94. virtual bool SetPageImage(size_t n, int imageId) = 0;
  95. // geometry
  96. // --------
  97. // resize the notebook so that all pages will have the specified size
  98. virtual void SetPageSize(const wxSize& size);
  99. // return the size of the area needed to accommodate the controller
  100. wxSize GetControllerSize() const;
  101. // calculate the size of the control from the size of its page
  102. //
  103. // by default this simply returns size enough to fit both the page and the
  104. // controller
  105. virtual wxSize CalcSizeFromPage(const wxSize& sizePage) const;
  106. // get/set size of area between book control area and page area
  107. unsigned int GetInternalBorder() const { return m_internalBorder; }
  108. void SetInternalBorder(unsigned int border) { m_internalBorder = border; }
  109. // Sets/gets the margin around the controller
  110. void SetControlMargin(int margin) { m_controlMargin = margin; }
  111. int GetControlMargin() const { return m_controlMargin; }
  112. // returns true if we have wxBK_TOP or wxBK_BOTTOM style
  113. bool IsVertical() const { return HasFlag(wxBK_BOTTOM | wxBK_TOP); }
  114. // set/get option to shrink to fit current page
  115. void SetFitToCurrentPage(bool fit) { m_fitToCurrentPage = fit; }
  116. bool GetFitToCurrentPage() const { return m_fitToCurrentPage; }
  117. // returns the sizer containing the control, if any
  118. wxSizer* GetControlSizer() const { return m_controlSizer; }
  119. // operations
  120. // ----------
  121. // remove one page from the control and delete it
  122. virtual bool DeletePage(size_t n);
  123. // remove one page from the notebook, without deleting it
  124. virtual bool RemovePage(size_t n)
  125. {
  126. DoInvalidateBestSize();
  127. return DoRemovePage(n) != NULL;
  128. }
  129. // remove all pages and delete them
  130. virtual bool DeleteAllPages()
  131. {
  132. m_selection = wxNOT_FOUND;
  133. DoInvalidateBestSize();
  134. WX_CLEAR_ARRAY(m_pages);
  135. return true;
  136. }
  137. // adds a new page to the control
  138. virtual bool AddPage(wxWindow *page,
  139. const wxString& text,
  140. bool bSelect = false,
  141. int imageId = NO_IMAGE)
  142. {
  143. DoInvalidateBestSize();
  144. return InsertPage(GetPageCount(), page, text, bSelect, imageId);
  145. }
  146. // the same as AddPage(), but adds the page at the specified position
  147. virtual bool InsertPage(size_t n,
  148. wxWindow *page,
  149. const wxString& text,
  150. bool bSelect = false,
  151. int imageId = NO_IMAGE) = 0;
  152. // set the currently selected page, return the index of the previously
  153. // selected one (or wxNOT_FOUND on error)
  154. //
  155. // NB: this function will generate PAGE_CHANGING/ED events
  156. virtual int SetSelection(size_t n) = 0;
  157. // acts as SetSelection but does not generate events
  158. virtual int ChangeSelection(size_t n) = 0;
  159. // cycle thru the pages
  160. void AdvanceSelection(bool forward = true)
  161. {
  162. int nPage = GetNextPage(forward);
  163. if ( nPage != wxNOT_FOUND )
  164. {
  165. // cast is safe because of the check above
  166. SetSelection((size_t)nPage);
  167. }
  168. }
  169. // return the index of the given page or wxNOT_FOUND
  170. int FindPage(const wxWindow* page) const;
  171. // hit test: returns which page is hit and, optionally, where (icon, label)
  172. virtual int HitTest(const wxPoint& WXUNUSED(pt),
  173. long * WXUNUSED(flags) = NULL) const
  174. {
  175. return wxNOT_FOUND;
  176. }
  177. // we do have multiple pages
  178. virtual bool HasMultiplePages() const { return true; }
  179. // we don't want focus for ourselves
  180. virtual bool AcceptsFocus() const { return false; }
  181. // returns true if the platform should explicitly apply a theme border
  182. virtual bool CanApplyThemeBorder() const { return false; }
  183. protected:
  184. // flags for DoSetSelection()
  185. enum
  186. {
  187. SetSelection_SendEvent = 1
  188. };
  189. // choose the default border for this window
  190. virtual wxBorder GetDefaultBorder() const { return wxBORDER_NONE; }
  191. // After the insertion of the page in the method InsertPage, calling this
  192. // method sets the selection to the given page or the first one if there is
  193. // still no selection. The "selection changed" event is sent only if
  194. // bSelect is true, so when it is false, no event is sent even if the
  195. // selection changed from wxNOT_FOUND to 0 when inserting the first page.
  196. //
  197. // Returns true if the selection was set to the specified page (explicitly
  198. // because of bSelect == true or implicitly because it's the first page) or
  199. // false otherwise.
  200. bool DoSetSelectionAfterInsertion(size_t n, bool bSelect);
  201. // Update the selection after removing the page at the given index,
  202. // typically called from the derived class overridden DoRemovePage().
  203. void DoSetSelectionAfterRemoval(size_t n);
  204. // set the selection to the given page, sending the events (which can
  205. // possibly prevent the page change from taking place) if SendEvent flag is
  206. // included
  207. virtual int DoSetSelection(size_t nPage, int flags = 0);
  208. // if the derived class uses DoSetSelection() for implementing
  209. // [Set|Change]Selection, it must override UpdateSelectedPage(),
  210. // CreatePageChangingEvent() and MakeChangedEvent(), but as it might not
  211. // use it, these functions are not pure virtual
  212. // called to notify the control about a new current page
  213. virtual void UpdateSelectedPage(size_t WXUNUSED(newsel))
  214. { wxFAIL_MSG(wxT("Override this function!")); }
  215. // create a new "page changing" event
  216. virtual wxBookCtrlEvent* CreatePageChangingEvent() const
  217. { wxFAIL_MSG(wxT("Override this function!")); return NULL; }
  218. // modify the event created by CreatePageChangingEvent() to "page changed"
  219. // event, usually by just calling SetEventType() on it
  220. virtual void MakeChangedEvent(wxBookCtrlEvent& WXUNUSED(event))
  221. { wxFAIL_MSG(wxT("Override this function!")); }
  222. // The derived class also may override the following method, also called
  223. // from DoSetSelection(), to show/hide pages differently.
  224. virtual void DoShowPage(wxWindow* page, bool show) { page->Show(show); }
  225. // Should we accept NULL page pointers in Add/InsertPage()?
  226. //
  227. // Default is no but derived classes may override it if they can treat NULL
  228. // pages in some sensible way (e.g. wxTreebook overrides this to allow
  229. // having nodes without any associated page)
  230. virtual bool AllowNullPage() const { return false; }
  231. // Remove the page and return a pointer to it.
  232. //
  233. // It also needs to update the current selection if necessary, i.e. if the
  234. // page being removed comes before the selected one and the helper method
  235. // DoSetSelectionAfterRemoval() can be used for this.
  236. virtual wxWindow *DoRemovePage(size_t page) = 0;
  237. // our best size is the size which fits all our pages
  238. virtual wxSize DoGetBestSize() const;
  239. // helper: get the next page wrapping if we reached the end
  240. int GetNextPage(bool forward) const;
  241. // Lay out controls
  242. virtual void DoSize();
  243. // This method also invalidates the size of the controller and should be
  244. // called instead of just InvalidateBestSize() whenever pages are added or
  245. // removed as this also affects the controller
  246. void DoInvalidateBestSize();
  247. #if wxUSE_HELP
  248. // Show the help for the corresponding page
  249. void OnHelp(wxHelpEvent& event);
  250. #endif // wxUSE_HELP
  251. // the array of all pages of this control
  252. wxArrayPages m_pages;
  253. // get the page area
  254. virtual wxRect GetPageRect() const;
  255. // event handlers
  256. void OnSize(wxSizeEvent& event);
  257. // controller buddy if available, NULL otherwise (usually for native book controls like wxNotebook)
  258. wxControl *m_bookctrl;
  259. // Whether to shrink to fit current page
  260. bool m_fitToCurrentPage;
  261. // the sizer containing the choice control
  262. wxSizer *m_controlSizer;
  263. // the margin around the choice control
  264. int m_controlMargin;
  265. // The currently selected page (in range 0..m_pages.size()-1 inclusive) or
  266. // wxNOT_FOUND if none (this can normally only be the case for an empty
  267. // control without any pages).
  268. int m_selection;
  269. private:
  270. // common part of all ctors
  271. void Init();
  272. // internal border
  273. unsigned int m_internalBorder;
  274. DECLARE_ABSTRACT_CLASS(wxBookCtrlBase)
  275. wxDECLARE_NO_COPY_CLASS(wxBookCtrlBase);
  276. DECLARE_EVENT_TABLE()
  277. };
  278. // ----------------------------------------------------------------------------
  279. // wxBookCtrlEvent: page changing events generated by book classes
  280. // ----------------------------------------------------------------------------
  281. class WXDLLIMPEXP_CORE wxBookCtrlEvent : public wxNotifyEvent
  282. {
  283. public:
  284. wxBookCtrlEvent(wxEventType commandType = wxEVT_NULL, int winid = 0,
  285. int nSel = wxNOT_FOUND, int nOldSel = wxNOT_FOUND)
  286. : wxNotifyEvent(commandType, winid)
  287. {
  288. m_nSel = nSel;
  289. m_nOldSel = nOldSel;
  290. }
  291. wxBookCtrlEvent(const wxBookCtrlEvent& event)
  292. : wxNotifyEvent(event)
  293. {
  294. m_nSel = event.m_nSel;
  295. m_nOldSel = event.m_nOldSel;
  296. }
  297. virtual wxEvent *Clone() const { return new wxBookCtrlEvent(*this); }
  298. // accessors
  299. // the currently selected page (wxNOT_FOUND if none)
  300. int GetSelection() const { return m_nSel; }
  301. void SetSelection(int nSel) { m_nSel = nSel; }
  302. // the page that was selected before the change (wxNOT_FOUND if none)
  303. int GetOldSelection() const { return m_nOldSel; }
  304. void SetOldSelection(int nOldSel) { m_nOldSel = nOldSel; }
  305. private:
  306. int m_nSel, // currently selected page
  307. m_nOldSel; // previously selected page
  308. DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxBookCtrlEvent)
  309. };
  310. typedef void (wxEvtHandler::*wxBookCtrlEventFunction)(wxBookCtrlEvent&);
  311. #define wxBookCtrlEventHandler(func) \
  312. wxEVENT_HANDLER_CAST(wxBookCtrlEventFunction, func)
  313. // obsolete name, defined for compatibility only
  314. #define wxBookCtrlBaseEvent wxBookCtrlEvent
  315. // make a default book control for given platform
  316. #if wxUSE_NOTEBOOK
  317. // dedicated to majority of desktops
  318. #include "wx/notebook.h"
  319. #define wxBookCtrl wxNotebook
  320. #define wxEVT_BOOKCTRL_PAGE_CHANGED wxEVT_NOTEBOOK_PAGE_CHANGED
  321. #define wxEVT_BOOKCTRL_PAGE_CHANGING wxEVT_NOTEBOOK_PAGE_CHANGING
  322. #define EVT_BOOKCTRL_PAGE_CHANGED(id, fn) EVT_NOTEBOOK_PAGE_CHANGED(id, fn)
  323. #define EVT_BOOKCTRL_PAGE_CHANGING(id, fn) EVT_NOTEBOOK_PAGE_CHANGING(id, fn)
  324. #else
  325. // dedicated to Smartphones
  326. #include "wx/choicebk.h"
  327. #define wxBookCtrl wxChoicebook
  328. #define wxEVT_BOOKCTRL_PAGE_CHANGED wxEVT_CHOICEBOOK_PAGE_CHANGED
  329. #define wxEVT_BOOKCTRL_PAGE_CHANGING wxEVT_CHOICEBOOK_PAGE_CHANGING
  330. #define EVT_BOOKCTRL_PAGE_CHANGED(id, fn) EVT_CHOICEBOOK_PAGE_CHANGED(id, fn)
  331. #define EVT_BOOKCTRL_PAGE_CHANGING(id, fn) EVT_CHOICEBOOK_PAGE_CHANGING(id, fn)
  332. #endif
  333. // old wxEVT_COMMAND_* constants
  334. #define wxEVT_COMMAND_BOOKCTRL_PAGE_CHANGED wxEVT_BOOKCTRL_PAGE_CHANGED
  335. #define wxEVT_COMMAND_BOOKCTRL_PAGE_CHANGING wxEVT_BOOKCTRL_PAGE_CHANGING
  336. #if WXWIN_COMPATIBILITY_2_6
  337. #define wxBC_TOP wxBK_TOP
  338. #define wxBC_BOTTOM wxBK_BOTTOM
  339. #define wxBC_LEFT wxBK_LEFT
  340. #define wxBC_RIGHT wxBK_RIGHT
  341. #define wxBC_DEFAULT wxBK_DEFAULT
  342. #endif
  343. #endif // wxUSE_BOOKCTRL
  344. #endif // _WX_BOOKCTRL_H_