slider.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/univ/slider.h
  3. // Purpose: wxSlider control for wxUniversal
  4. // Author: Vadim Zeitlin
  5. // Modified by:
  6. // Created: 09.02.01
  7. // Copyright: (c) 2001 SciTech Software, Inc. (www.scitechsoft.com)
  8. // Licence: wxWindows licence
  9. ///////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_UNIV_SLIDER_H_
  11. #define _WX_UNIV_SLIDER_H_
  12. #include "wx/univ/scrthumb.h"
  13. // ----------------------------------------------------------------------------
  14. // the actions supported by this control
  15. // ----------------------------------------------------------------------------
  16. // our actions are the same as scrollbars
  17. #define wxACTION_SLIDER_START wxT("start") // to the beginning
  18. #define wxACTION_SLIDER_END wxT("end") // to the end
  19. #define wxACTION_SLIDER_LINE_UP wxT("lineup") // one line up/left
  20. #define wxACTION_SLIDER_PAGE_UP wxT("pageup") // one page up/left
  21. #define wxACTION_SLIDER_LINE_DOWN wxT("linedown") // one line down/right
  22. #define wxACTION_SLIDER_PAGE_DOWN wxT("pagedown") // one page down/right
  23. #define wxACTION_SLIDER_PAGE_CHANGE wxT("pagechange")// change page by numArg
  24. #define wxACTION_SLIDER_THUMB_DRAG wxT("thumbdrag")
  25. #define wxACTION_SLIDER_THUMB_MOVE wxT("thumbmove")
  26. #define wxACTION_SLIDER_THUMB_RELEASE wxT("thumbrelease")
  27. // ----------------------------------------------------------------------------
  28. // wxSlider
  29. // ----------------------------------------------------------------------------
  30. class WXDLLIMPEXP_CORE wxSlider : public wxSliderBase,
  31. public wxControlWithThumb
  32. {
  33. public:
  34. // ctors and such
  35. wxSlider();
  36. wxSlider(wxWindow *parent,
  37. wxWindowID id,
  38. int value, int minValue, int maxValue,
  39. const wxPoint& pos = wxDefaultPosition,
  40. const wxSize& size = wxDefaultSize,
  41. long style = wxSL_HORIZONTAL,
  42. const wxValidator& validator = wxDefaultValidator,
  43. const wxString& name = wxSliderNameStr);
  44. bool Create(wxWindow *parent,
  45. wxWindowID id,
  46. int value, int minValue, int maxValue,
  47. const wxPoint& pos = wxDefaultPosition,
  48. const wxSize& size = wxDefaultSize,
  49. long style = wxSL_HORIZONTAL,
  50. const wxValidator& validator = wxDefaultValidator,
  51. const wxString& name = wxSliderNameStr);
  52. // implement base class pure virtuals
  53. virtual int GetValue() const;
  54. virtual void SetValue(int value);
  55. virtual void SetRange(int minValue, int maxValue);
  56. virtual int GetMin() const;
  57. virtual int GetMax() const;
  58. virtual void SetLineSize(int lineSize);
  59. virtual void SetPageSize(int pageSize);
  60. virtual int GetLineSize() const;
  61. virtual int GetPageSize() const;
  62. virtual void SetThumbLength(int lenPixels);
  63. virtual int GetThumbLength() const;
  64. virtual int GetTickFreq() const { return m_tickFreq; }
  65. // wxUniv-specific methods
  66. // -----------------------
  67. // is this a vertical slider?
  68. bool IsVert() const { return (GetWindowStyle() & wxSL_VERTICAL) != 0; }
  69. // get the slider orientation
  70. wxOrientation GetOrientation() const
  71. { return IsVert() ? wxVERTICAL : wxHORIZONTAL; }
  72. // do we have labels?
  73. bool HasLabels() const
  74. { return ((GetWindowStyle() & wxSL_LABELS) != 0) &&
  75. ((GetWindowStyle() & (wxSL_TOP|wxSL_BOTTOM|wxSL_LEFT|wxSL_RIGHT)) != 0); }
  76. // do we have ticks?
  77. bool HasTicks() const
  78. { return ((GetWindowStyle() & wxSL_TICKS) != 0) &&
  79. ((GetWindowStyle() & (wxSL_TOP|wxSL_BOTTOM|wxSL_LEFT|wxSL_RIGHT|wxSL_BOTH)) != 0); }
  80. // implement wxControlWithThumb interface
  81. virtual wxWindow *GetWindow() { return this; }
  82. virtual bool IsVertical() const { return IsVert(); }
  83. virtual wxScrollThumb::Shaft HitTest(const wxPoint& pt) const;
  84. virtual wxCoord ThumbPosToPixel() const;
  85. virtual int PixelToThumbPos(wxCoord x) const;
  86. virtual void SetShaftPartState(wxScrollThumb::Shaft shaftPart,
  87. int flag,
  88. bool set = true);
  89. virtual void OnThumbDragStart(int pos);
  90. virtual void OnThumbDrag(int pos);
  91. virtual void OnThumbDragEnd(int pos);
  92. virtual void OnPageScrollStart();
  93. virtual bool OnPageScroll(int pageInc);
  94. // for wxStdSliderInputHandler
  95. wxScrollThumb& GetThumb() { return m_thumb; }
  96. virtual bool PerformAction(const wxControlAction& action,
  97. long numArg = 0,
  98. const wxString& strArg = wxEmptyString);
  99. static wxInputHandler *GetStdInputHandler(wxInputHandler *handlerDef);
  100. virtual wxInputHandler *DoGetStdInputHandler(wxInputHandler *handlerDef)
  101. {
  102. return GetStdInputHandler(handlerDef);
  103. }
  104. protected:
  105. enum
  106. {
  107. INVALID_THUMB_VALUE = -0xffff
  108. };
  109. // Platform-specific implementation of SetTickFreq
  110. virtual void DoSetTickFreq(int freq);
  111. // overridden base class virtuals
  112. virtual wxSize DoGetBestClientSize() const;
  113. virtual void DoDraw(wxControlRenderer *renderer);
  114. virtual wxBorder GetDefaultBorder() const { return wxBORDER_NONE; }
  115. // event handlers
  116. void OnSize(wxSizeEvent& event);
  117. // common part of all ctors
  118. void Init();
  119. // normalize the value to fit in the range
  120. int NormalizeValue(int value) const;
  121. // change the value by the given increment, return true if really changed
  122. bool ChangeValueBy(int inc);
  123. // change the value to the given one
  124. bool ChangeValueTo(int value);
  125. // is the value inside the range?
  126. bool IsInRange(int value) { return (value >= m_min) && (value <= m_max); }
  127. // format the value for printing as label
  128. virtual wxString FormatValue(int value) const;
  129. // calculate max label size
  130. wxSize CalcLabelSize() const;
  131. // calculate m_rectLabel/Slider
  132. void CalcGeometry();
  133. // get the thumb size
  134. wxSize GetThumbSize() const;
  135. // get the shaft rect (uses m_rectSlider which is supposed to be calculated)
  136. wxRect GetShaftRect() const;
  137. // calc the current thumb position using the shaft rect (if the pointer is
  138. // NULL, we calculate it here too)
  139. void CalcThumbRect(const wxRect *rectShaft,
  140. wxRect *rectThumbOut,
  141. wxRect *rectLabelOut,
  142. int value = INVALID_THUMB_VALUE) const;
  143. // return the slider rect calculating it if needed
  144. const wxRect& GetSliderRect() const;
  145. // refresh the current thumb position
  146. void RefreshThumb();
  147. private:
  148. // get the default thumb size (without using m_thumbSize)
  149. wxSize GetDefaultThumbSize() const;
  150. // the object which manages our thumb
  151. wxScrollThumb m_thumb;
  152. // the slider range and value
  153. int m_min,
  154. m_max,
  155. m_value;
  156. // the tick frequence (default is 1)
  157. int m_tickFreq;
  158. // the line and page increments (logical units)
  159. int m_lineSize,
  160. m_pageSize;
  161. // the size of the thumb (in pixels)
  162. int m_thumbSize;
  163. // the part of the client area reserved for the label, the ticks and the
  164. // part for the slider itself
  165. wxRect m_rectLabel,
  166. m_rectTicks,
  167. m_rectSlider;
  168. // the state of the thumb (wxCONTROL_XXX constants sum)
  169. int m_thumbFlags;
  170. DECLARE_EVENT_TABLE()
  171. DECLARE_DYNAMIC_CLASS(wxSlider)
  172. };
  173. #endif // _WX_UNIV_SLIDER_H_