simplebook.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/simplebook.h
  3. // Purpose: wxBookCtrlBase-derived class without any controller.
  4. // Author: Vadim Zeitlin
  5. // Created: 2012-08-21
  6. // Copyright: (c) 2012 Vadim Zeitlin <vadim@wxwidgets.org>
  7. // Licence: wxWindows licence
  8. ///////////////////////////////////////////////////////////////////////////////
  9. #ifndef _WX_SIMPLEBOOK_H_
  10. #define _WX_SIMPLEBOOK_H_
  11. #include "wx/bookctrl.h"
  12. #if wxUSE_BOOKCTRL
  13. #include "wx/vector.h"
  14. // ----------------------------------------------------------------------------
  15. // wxSimplebook: a book control without any user-actionable controller.
  16. // ----------------------------------------------------------------------------
  17. // NB: This class doesn't use DLL export declaration as it's fully inline.
  18. class wxSimplebook : public wxBookCtrlBase
  19. {
  20. public:
  21. wxSimplebook()
  22. {
  23. Init();
  24. }
  25. wxSimplebook(wxWindow *parent,
  26. wxWindowID winid = wxID_ANY,
  27. const wxPoint& pos = wxDefaultPosition,
  28. const wxSize& size = wxDefaultSize,
  29. long style = 0,
  30. const wxString& name = wxEmptyString)
  31. : wxBookCtrlBase(parent, winid, pos, size, style | wxBK_TOP, name)
  32. {
  33. Init();
  34. }
  35. bool Create(wxWindow *parent,
  36. wxWindowID winid = wxID_ANY,
  37. const wxPoint& pos = wxDefaultPosition,
  38. const wxSize& size = wxDefaultSize,
  39. long style = 0,
  40. const wxString& name = wxEmptyString)
  41. {
  42. return wxBookCtrlBase::Create(parent, winid, pos, size, style | wxBK_TOP, name);
  43. }
  44. // Methods specific to this class.
  45. // A method allowing to add a new page without any label (which is unused
  46. // by this control) and show it immediately.
  47. bool ShowNewPage(wxWindow* page)
  48. {
  49. return AddPage(page, wxString(), true /* select it */);
  50. }
  51. // Set effect to use for showing/hiding pages.
  52. void SetEffects(wxShowEffect showEffect, wxShowEffect hideEffect)
  53. {
  54. m_showEffect = showEffect;
  55. m_hideEffect = hideEffect;
  56. }
  57. // Or the same effect for both of them.
  58. void SetEffect(wxShowEffect effect)
  59. {
  60. SetEffects(effect, effect);
  61. }
  62. // And the same for time outs.
  63. void SetEffectsTimeouts(unsigned showTimeout, unsigned hideTimeout)
  64. {
  65. m_showTimeout = showTimeout;
  66. m_hideTimeout = hideTimeout;
  67. }
  68. void SetEffectTimeout(unsigned timeout)
  69. {
  70. SetEffectsTimeouts(timeout, timeout);
  71. }
  72. // Implement base class pure virtual methods.
  73. // Page management
  74. virtual bool InsertPage(size_t n,
  75. wxWindow *page,
  76. const wxString& text,
  77. bool bSelect = false,
  78. int imageId = NO_IMAGE)
  79. {
  80. if ( !wxBookCtrlBase::InsertPage(n, page, text, bSelect, imageId) )
  81. return false;
  82. m_pageTexts.insert(m_pageTexts.begin() + n, text);
  83. if ( !DoSetSelectionAfterInsertion(n, bSelect) )
  84. page->Hide();
  85. return true;
  86. }
  87. virtual int SetSelection(size_t n)
  88. {
  89. return DoSetSelection(n, SetSelection_SendEvent);
  90. }
  91. virtual int ChangeSelection(size_t n)
  92. {
  93. return DoSetSelection(n);
  94. }
  95. // Neither labels nor images are supported but we still store the labels
  96. // just in case the user code attaches some importance to them.
  97. virtual bool SetPageText(size_t n, const wxString& strText)
  98. {
  99. wxCHECK_MSG( n < GetPageCount(), false, wxS("Invalid page") );
  100. m_pageTexts[n] = strText;
  101. return true;
  102. }
  103. virtual wxString GetPageText(size_t n) const
  104. {
  105. wxCHECK_MSG( n < GetPageCount(), wxString(), wxS("Invalid page") );
  106. return m_pageTexts[n];
  107. }
  108. virtual bool SetPageImage(size_t WXUNUSED(n), int WXUNUSED(imageId))
  109. {
  110. return false;
  111. }
  112. virtual int GetPageImage(size_t WXUNUSED(n)) const
  113. {
  114. return NO_IMAGE;
  115. }
  116. protected:
  117. virtual void UpdateSelectedPage(size_t newsel)
  118. {
  119. m_selection = (int)newsel;
  120. }
  121. virtual wxBookCtrlEvent* CreatePageChangingEvent() const
  122. {
  123. return new wxBookCtrlEvent(wxEVT_BOOKCTRL_PAGE_CHANGING,
  124. GetId());
  125. }
  126. virtual void MakeChangedEvent(wxBookCtrlEvent& event)
  127. {
  128. event.SetEventType(wxEVT_BOOKCTRL_PAGE_CHANGED);
  129. }
  130. virtual wxWindow *DoRemovePage(size_t page)
  131. {
  132. wxWindow* const win = wxBookCtrlBase::DoRemovePage(page);
  133. if ( win )
  134. {
  135. m_pageTexts.erase(m_pageTexts.begin() + page);
  136. DoSetSelectionAfterRemoval(page);
  137. }
  138. return win;
  139. }
  140. virtual void DoSize()
  141. {
  142. wxWindow* const page = GetCurrentPage();
  143. if ( page )
  144. page->SetSize(GetPageRect());
  145. }
  146. virtual void DoShowPage(wxWindow* page, bool show)
  147. {
  148. if ( show )
  149. page->ShowWithEffect(m_showEffect, m_showTimeout);
  150. else
  151. page->HideWithEffect(m_hideEffect, m_hideTimeout);
  152. }
  153. private:
  154. void Init()
  155. {
  156. // We don't need any border as we don't have anything to separate the
  157. // page contents from.
  158. SetInternalBorder(0);
  159. // No effects by default.
  160. m_showEffect =
  161. m_hideEffect = wxSHOW_EFFECT_NONE;
  162. m_showTimeout =
  163. m_hideTimeout = 0;
  164. }
  165. wxVector<wxString> m_pageTexts;
  166. wxShowEffect m_showEffect,
  167. m_hideEffect;
  168. unsigned m_showTimeout,
  169. m_hideTimeout;
  170. wxDECLARE_NO_COPY_CLASS(wxSimplebook);
  171. };
  172. #endif // wxUSE_BOOKCTRL
  173. #endif // _WX_SIMPLEBOOK_H_