simplebook.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. // Methods specific to this class.
  36. // A method allowing to add a new page without any label (which is unused
  37. // by this control) and show it immediately.
  38. bool ShowNewPage(wxWindow* page)
  39. {
  40. return AddPage(page, wxString(), true /* select it */);
  41. }
  42. // Set effect to use for showing/hiding pages.
  43. void SetEffects(wxShowEffect showEffect, wxShowEffect hideEffect)
  44. {
  45. m_showEffect = showEffect;
  46. m_hideEffect = hideEffect;
  47. }
  48. // Or the same effect for both of them.
  49. void SetEffect(wxShowEffect effect)
  50. {
  51. SetEffects(effect, effect);
  52. }
  53. // And the same for time outs.
  54. void SetEffectsTimeouts(unsigned showTimeout, unsigned hideTimeout)
  55. {
  56. m_showTimeout = showTimeout;
  57. m_hideTimeout = hideTimeout;
  58. }
  59. void SetEffectTimeout(unsigned timeout)
  60. {
  61. SetEffectsTimeouts(timeout, timeout);
  62. }
  63. // Implement base class pure virtual methods.
  64. // Page management
  65. virtual bool InsertPage(size_t n,
  66. wxWindow *page,
  67. const wxString& text,
  68. bool bSelect = false,
  69. int imageId = NO_IMAGE)
  70. {
  71. if ( !wxBookCtrlBase::InsertPage(n, page, text, bSelect, imageId) )
  72. return false;
  73. m_pageTexts.insert(m_pageTexts.begin() + n, text);
  74. if ( !DoSetSelectionAfterInsertion(n, bSelect) )
  75. page->Hide();
  76. return true;
  77. }
  78. virtual int SetSelection(size_t n)
  79. {
  80. return DoSetSelection(n, SetSelection_SendEvent);
  81. }
  82. virtual int ChangeSelection(size_t n)
  83. {
  84. return DoSetSelection(n);
  85. }
  86. // Neither labels nor images are supported but we still store the labels
  87. // just in case the user code attaches some importance to them.
  88. virtual bool SetPageText(size_t n, const wxString& strText)
  89. {
  90. wxCHECK_MSG( n < GetPageCount(), false, wxS("Invalid page") );
  91. m_pageTexts[n] = strText;
  92. return true;
  93. }
  94. virtual wxString GetPageText(size_t n) const
  95. {
  96. wxCHECK_MSG( n < GetPageCount(), wxString(), wxS("Invalid page") );
  97. return m_pageTexts[n];
  98. }
  99. virtual bool SetPageImage(size_t WXUNUSED(n), int WXUNUSED(imageId))
  100. {
  101. return false;
  102. }
  103. virtual int GetPageImage(size_t WXUNUSED(n)) const
  104. {
  105. return NO_IMAGE;
  106. }
  107. protected:
  108. virtual void UpdateSelectedPage(size_t newsel)
  109. {
  110. m_selection = (int)newsel;
  111. }
  112. virtual wxBookCtrlEvent* CreatePageChangingEvent() const
  113. {
  114. return new wxBookCtrlEvent(wxEVT_BOOKCTRL_PAGE_CHANGING,
  115. GetId());
  116. }
  117. virtual void MakeChangedEvent(wxBookCtrlEvent& event)
  118. {
  119. event.SetEventType(wxEVT_BOOKCTRL_PAGE_CHANGED);
  120. }
  121. virtual wxWindow *DoRemovePage(size_t page)
  122. {
  123. wxWindow* const win = wxBookCtrlBase::DoRemovePage(page);
  124. if ( win )
  125. {
  126. m_pageTexts.erase(m_pageTexts.begin() + page);
  127. DoSetSelectionAfterRemoval(page);
  128. }
  129. return win;
  130. }
  131. virtual void DoSize()
  132. {
  133. wxWindow* const page = GetCurrentPage();
  134. if ( page )
  135. page->SetSize(GetPageRect());
  136. }
  137. virtual void DoShowPage(wxWindow* page, bool show)
  138. {
  139. if ( show )
  140. page->ShowWithEffect(m_showEffect, m_showTimeout);
  141. else
  142. page->HideWithEffect(m_hideEffect, m_hideTimeout);
  143. }
  144. private:
  145. void Init()
  146. {
  147. // We don't need any border as we don't have anything to separate the
  148. // page contents from.
  149. SetInternalBorder(0);
  150. // No effects by default.
  151. m_showEffect =
  152. m_hideEffect = wxSHOW_EFFECT_NONE;
  153. m_showTimeout =
  154. m_hideTimeout = 0;
  155. }
  156. wxVector<wxString> m_pageTexts;
  157. wxShowEffect m_showEffect,
  158. m_hideEffect;
  159. unsigned m_showTimeout,
  160. m_hideTimeout;
  161. wxDECLARE_NO_COPY_CLASS(wxSimplebook);
  162. };
  163. #endif // wxUSE_BOOKCTRL
  164. #endif // _WX_SIMPLEBOOK_H_