scrolbar.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/univ/scrolbar.h
  3. // Purpose: wxScrollBar for wxUniversal
  4. // Author: Vadim Zeitlin
  5. // Modified by:
  6. // Created: 20.08.00
  7. // Copyright: (c) 2000 SciTech Software, Inc. (www.scitechsoft.com)
  8. // Licence: wxWindows licence
  9. ///////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_UNIV_SCROLBAR_H_
  11. #define _WX_UNIV_SCROLBAR_H_
  12. class WXDLLIMPEXP_FWD_CORE wxScrollTimer;
  13. #include "wx/univ/scrarrow.h"
  14. #include "wx/renderer.h"
  15. // ----------------------------------------------------------------------------
  16. // the actions supported by this control
  17. // ----------------------------------------------------------------------------
  18. // scroll the bar
  19. #define wxACTION_SCROLL_START wxT("start") // to the beginning
  20. #define wxACTION_SCROLL_END wxT("end") // to the end
  21. #define wxACTION_SCROLL_LINE_UP wxT("lineup") // one line up/left
  22. #define wxACTION_SCROLL_PAGE_UP wxT("pageup") // one page up/left
  23. #define wxACTION_SCROLL_LINE_DOWN wxT("linedown") // one line down/right
  24. #define wxACTION_SCROLL_PAGE_DOWN wxT("pagedown") // one page down/right
  25. // the scrollbar thumb may be dragged
  26. #define wxACTION_SCROLL_THUMB_DRAG wxT("thumbdrag")
  27. #define wxACTION_SCROLL_THUMB_MOVE wxT("thumbmove")
  28. #define wxACTION_SCROLL_THUMB_RELEASE wxT("thumbrelease")
  29. // ----------------------------------------------------------------------------
  30. // wxScrollBar
  31. // ----------------------------------------------------------------------------
  32. class WXDLLIMPEXP_CORE wxScrollBar : public wxScrollBarBase,
  33. public wxControlWithArrows
  34. {
  35. public:
  36. // scrollbar elements: they correspond to wxHT_SCROLLBAR_XXX constants but
  37. // start from 0 which allows to use them as array indices
  38. enum Element
  39. {
  40. Element_Arrow_Line_1,
  41. Element_Arrow_Line_2,
  42. Element_Arrow_Page_1,
  43. Element_Arrow_Page_2,
  44. Element_Thumb,
  45. Element_Bar_1,
  46. Element_Bar_2,
  47. Element_Max
  48. };
  49. wxScrollBar();
  50. wxScrollBar(wxWindow *parent,
  51. wxWindowID id,
  52. const wxPoint& pos = wxDefaultPosition,
  53. const wxSize& size = wxDefaultSize,
  54. long style = wxSB_HORIZONTAL,
  55. const wxValidator& validator = wxDefaultValidator,
  56. const wxString& name = wxScrollBarNameStr);
  57. bool Create(wxWindow *parent,
  58. wxWindowID id,
  59. const wxPoint& pos = wxDefaultPosition,
  60. const wxSize& size = wxDefaultSize,
  61. long style = wxSB_HORIZONTAL,
  62. const wxValidator& validator = wxDefaultValidator,
  63. const wxString& name = wxScrollBarNameStr);
  64. virtual ~wxScrollBar();
  65. // implement base class pure virtuals
  66. virtual int GetThumbPosition() const;
  67. virtual int GetThumbSize() const;
  68. virtual int GetPageSize() const;
  69. virtual int GetRange() const;
  70. virtual void SetThumbPosition(int thumbPos);
  71. virtual void SetScrollbar(int position, int thumbSize,
  72. int range, int pageSize,
  73. bool refresh = true);
  74. // wxScrollBar actions
  75. void ScrollToStart();
  76. void ScrollToEnd();
  77. bool ScrollLines(int nLines);
  78. bool ScrollPages(int nPages);
  79. virtual bool PerformAction(const wxControlAction& action,
  80. long numArg = 0,
  81. const wxString& strArg = wxEmptyString);
  82. static wxInputHandler *GetStdInputHandler(wxInputHandler *handlerDef);
  83. virtual wxInputHandler *DoGetStdInputHandler(wxInputHandler *handlerDef)
  84. {
  85. return GetStdInputHandler(handlerDef);
  86. }
  87. // scrollbars around a normal window should not receive the focus
  88. virtual bool AcceptsFocus() const;
  89. // wxScrollBar sub elements state (combination of wxCONTROL_XXX)
  90. void SetState(Element which, int flags);
  91. int GetState(Element which) const;
  92. // implement wxControlWithArrows methods
  93. virtual wxRenderer *GetRenderer() const { return m_renderer; }
  94. virtual wxWindow *GetWindow() { return this; }
  95. virtual bool IsVertical() const { return wxScrollBarBase::IsVertical(); }
  96. virtual int GetArrowState(wxScrollArrows::Arrow arrow) const;
  97. virtual void SetArrowFlag(wxScrollArrows::Arrow arrow, int flag, bool set);
  98. virtual bool OnArrow(wxScrollArrows::Arrow arrow);
  99. virtual wxScrollArrows::Arrow HitTestArrow(const wxPoint& pt) const;
  100. // for wxControlRenderer::DrawScrollbar() only
  101. const wxScrollArrows& GetArrows() const { return m_arrows; }
  102. // returns one of wxHT_SCROLLBAR_XXX constants
  103. wxHitTest HitTestBar(const wxPoint& pt) const;
  104. // idle processing
  105. virtual void OnInternalIdle();
  106. protected:
  107. virtual wxSize DoGetBestClientSize() const;
  108. virtual void DoDraw(wxControlRenderer *renderer);
  109. virtual wxBorder GetDefaultBorder() const { return wxBORDER_NONE; }
  110. // forces update of thumb's visual appearence (does nothing if m_dirty=false)
  111. void UpdateThumb();
  112. // SetThumbPosition() helper
  113. void DoSetThumb(int thumbPos);
  114. // common part of all ctors
  115. void Init();
  116. // is this scrollbar attached to a window or a standalone control?
  117. bool IsStandalone() const;
  118. // scrollbar geometry methods:
  119. // gets the bounding box for a scrollbar element for the given (by default
  120. // - current) thumb position
  121. wxRect GetScrollbarRect(wxScrollBar::Element elem, int thumbPos = -1) const;
  122. // returns the size of the scrollbar shaft excluding the arrows
  123. wxCoord GetScrollbarSize() const;
  124. // translate the scrollbar position (in logical units) into physical
  125. // coordinate (in pixels) and the other way round
  126. wxCoord ScrollbarToPixel(int thumbPos = -1);
  127. int PixelToScrollbar(wxCoord coord);
  128. // return the starting and ending positions, in pixels, of the thumb of a
  129. // scrollbar with the given logical position, thumb size and range and the
  130. // given physical length
  131. static void GetScrollBarThumbSize(wxCoord length,
  132. int thumbPos,
  133. int thumbSize,
  134. int range,
  135. wxCoord *thumbStart,
  136. wxCoord *thumbEnd);
  137. private:
  138. // total range of the scrollbar in logical units
  139. int m_range;
  140. // the current and previous (after last refresh - this is used for
  141. // repainting optimisation) size of the thumb in logical units (from 0 to
  142. // m_range) and its position (from 0 to m_range - m_thumbSize)
  143. int m_thumbSize,
  144. m_thumbPos,
  145. m_thumbPosOld;
  146. // the page size, i.e. the number of lines by which to scroll when page
  147. // up/down action is performed
  148. int m_pageSize;
  149. // the state of the sub elements
  150. int m_elementsState[Element_Max];
  151. // the dirty flag: if set, scrollbar must be updated
  152. bool m_dirty;
  153. // the object handling the arrows
  154. wxScrollArrows m_arrows;
  155. friend WXDLLIMPEXP_CORE class wxControlRenderer; // for geometry methods
  156. friend class wxStdScrollBarInputHandler; // for geometry methods
  157. DECLARE_EVENT_TABLE()
  158. DECLARE_DYNAMIC_CLASS(wxScrollBar)
  159. };
  160. // ----------------------------------------------------------------------------
  161. // Standard scrollbar input handler which can be used as a base class
  162. // ----------------------------------------------------------------------------
  163. class WXDLLIMPEXP_CORE wxStdScrollBarInputHandler : public wxStdInputHandler
  164. {
  165. public:
  166. // constructor takes a renderer (used for scrollbar hit testing) and the
  167. // base handler to which all unhandled events are forwarded
  168. wxStdScrollBarInputHandler(wxRenderer *renderer,
  169. wxInputHandler *inphand);
  170. virtual bool HandleKey(wxInputConsumer *consumer,
  171. const wxKeyEvent& event,
  172. bool pressed);
  173. virtual bool HandleMouse(wxInputConsumer *consumer,
  174. const wxMouseEvent& event);
  175. virtual bool HandleMouseMove(wxInputConsumer *consumer, const wxMouseEvent& event);
  176. virtual ~wxStdScrollBarInputHandler();
  177. // this method is called by wxScrollBarTimer only and may be overridden
  178. //
  179. // return true to continue scrolling, false to stop the timer
  180. virtual bool OnScrollTimer(wxScrollBar *scrollbar,
  181. const wxControlAction& action);
  182. protected:
  183. // return true if the mouse button can be used to activate scrollbar, false
  184. // if not (any button under GTK+ unlike left button only which is default)
  185. virtual bool IsAllowedButton(int button) const
  186. { return button == wxMOUSE_BTN_LEFT; }
  187. // set or clear the specified flag on the scrollbar element corresponding
  188. // to m_htLast
  189. void SetElementState(wxScrollBar *scrollbar, int flag, bool doIt);
  190. // [un]highlight the scrollbar element corresponding to m_htLast
  191. virtual void Highlight(wxScrollBar *scrollbar, bool doIt)
  192. { SetElementState(scrollbar, wxCONTROL_CURRENT, doIt); }
  193. // [un]press the scrollbar element corresponding to m_htLast
  194. virtual void Press(wxScrollBar *scrollbar, bool doIt)
  195. { SetElementState(scrollbar, wxCONTROL_PRESSED, doIt); }
  196. // stop scrolling because we reached the end point
  197. void StopScrolling(wxScrollBar *scrollbar);
  198. // get the mouse coordinates in the scrollbar direction from the event
  199. wxCoord GetMouseCoord(const wxScrollBar *scrollbar,
  200. const wxMouseEvent& event) const;
  201. // generate a "thumb move" action for this mouse event
  202. void HandleThumbMove(wxScrollBar *scrollbar, const wxMouseEvent& event);
  203. // the window (scrollbar) which has capture or NULL and the flag telling if
  204. // the mouse is inside the element which captured it or not
  205. wxWindow *m_winCapture;
  206. bool m_winHasMouse;
  207. int m_btnCapture; // the mouse button which has captured mouse
  208. // the position where we started scrolling by page
  209. wxPoint m_ptStartScrolling;
  210. // one of wxHT_SCROLLBAR_XXX value: where has the mouse been last time?
  211. wxHitTest m_htLast;
  212. // the renderer (we use it only for hit testing)
  213. wxRenderer *m_renderer;
  214. // the offset of the top/left of the scrollbar relative to the mouse to
  215. // keep during the thumb drag
  216. int m_ofsMouse;
  217. // the timer for generating scroll events when the mouse stays pressed on
  218. // a scrollbar
  219. wxScrollTimer *m_timerScroll;
  220. };
  221. #endif // _WX_UNIV_SCROLBAR_H_