llsliderctrl.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /**
  2. * @file llsliderctrl.h
  3. * @brief Slider with label and text
  4. *
  5. * $LicenseInfo:firstyear=2002&license=viewergpl$
  6. *
  7. * Copyright (c) 2002-2009, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #ifndef LL_LLSLIDERCTRL_H
  33. #define LL_LLSLIDERCTRL_H
  34. #include "llcolor4.h"
  35. #include "lluictrl.h"
  36. class LLLineEditor;
  37. class LLTextBox;
  38. class LLUICtrlFactory;
  39. //
  40. // Constants
  41. //
  42. constexpr S32 SLIDERCTRL_SPACING = 4; // Space between label, slider, and text
  43. constexpr S32 SLIDERCTRL_HEIGHT = 16;
  44. // Used to be in its own llslider.h/cpp module, and to be declared as both a
  45. // "volume_slider" and "slider_bar" UI control, but I eradicated its (rare)
  46. // usage in the viewer to use "slider" (LLSliderCtrl) everywhere; this class is
  47. // now just a sub-element of the more generic LLSliderCtrl class/UI control. HB
  48. class LLSlider final : public LLUICtrl
  49. {
  50. // Only LLSliderCtrl is allowed to use this class ! HB
  51. friend class LLSliderCtrl;
  52. protected:
  53. LOG_CLASS(LLSlider);
  54. LLSlider(const std::string& name, const LLRect& rect,
  55. void (*on_commit_callback)(LLUICtrl*, void*), void* userdata,
  56. F32 initial_value, F32 min_value, F32 max_value, F32 increment,
  57. const char* ctrl_name = NULL);
  58. void draw() override;
  59. void setValue(F32 value, bool from_event = false);
  60. F32 getValueF32() const { return mValue; }
  61. LL_INLINE void setValue(const LLSD& v) override { setValue((F32)v.asReal(), true); }
  62. LL_INLINE LLSD getValue() const override { return LLSD(getValueF32()); }
  63. LL_INLINE void setMinValue(LLSD v) override { setMinValue((F32)v.asReal()); }
  64. LL_INLINE void setMaxValue(LLSD v) override { setMaxValue((F32)v.asReal()); }
  65. LL_INLINE F32 getInitialValue() const { return mInitialValue; }
  66. LL_INLINE F32 getMinValue() const { return mMinValue; }
  67. LL_INLINE F32 getMaxValue() const { return mMaxValue; }
  68. LL_INLINE F32 getIncrement() const { return mIncrement; }
  69. LL_INLINE void setMinValue(F32 v) { mMinValue = v; updateThumbRect(); }
  70. LL_INLINE void setMaxValue(F32 v) { mMaxValue = v; updateThumbRect(); }
  71. LL_INLINE void setIncrement(F32 increment) { mIncrement = increment; }
  72. LL_INLINE void setMouseDownCallback(void (*cb)(LLUICtrl*, void*))
  73. {
  74. mMouseDownCallback = cb;
  75. }
  76. LL_INLINE void setMouseUpCallback(void (*cb)(LLUICtrl*, void*))
  77. {
  78. mMouseUpCallback = cb;
  79. }
  80. LL_INLINE void setMouseHoverCallback(void (*cb)(LLUICtrl*, void*))
  81. {
  82. mMouseHoverCallback = cb;
  83. }
  84. bool handleHover(S32 x, S32 y, MASK mask) override;
  85. bool handleMouseUp(S32 x, S32 y, MASK mask) override;
  86. bool handleMouseDown(S32 x, S32 y, MASK mask) override;
  87. bool handleKeyHere(KEY key, MASK mask) override;
  88. private:
  89. void setValueAndCommit(F32 value);
  90. void updateThumbRect();
  91. private:
  92. LLUIImage* mThumbImage;
  93. LLUIImage* mTrackImage;
  94. LLUIImage* mTrackHighlightImage;
  95. LLRect mDragStartThumbRect;
  96. LLRect mThumbRect;
  97. void (*mMouseDownCallback)(LLUICtrl*, void*);
  98. void (*mMouseUpCallback)(LLUICtrl*, void*);
  99. void (*mMouseHoverCallback)(LLUICtrl*, void*);
  100. F32 mValue;
  101. F32 mInitialValue;
  102. F32 mMinValue;
  103. F32 mMaxValue;
  104. F32 mIncrement;
  105. S32 mMouseOffset;
  106. };
  107. // LLSliderCtrl proper
  108. class LLSliderCtrl : public LLUICtrl
  109. {
  110. protected:
  111. LOG_CLASS(LLSliderCtrl);
  112. public:
  113. LLSliderCtrl(const std::string& name, const LLRect& rect,
  114. const std::string& label, const LLFontGL* font,
  115. S32 slider_left, S32 text_left,
  116. bool show_text, bool can_edit_text,
  117. void (*commit_callback)(LLUICtrl*, void*), void* userdata,
  118. F32 initial_value, F32 min_value, F32 max_value,
  119. F32 incr, const char* ctrl_name = NULL);
  120. ~LLSliderCtrl() override;
  121. const std::string& getTag() const override;
  122. LLXMLNodePtr getXML(bool save_children = true) const override;
  123. static LLView* fromXML(LLXMLNodePtr node, LLView* parent,
  124. LLUICtrlFactory*);
  125. LL_INLINE F32 getValueF32() const { return mSlider->getValueF32(); }
  126. void setValue(F32 v, bool from_event = false);
  127. LL_INLINE void setValue(const LLSD& v) override { setValue((F32)v.asReal(), true); }
  128. LL_INLINE LLSD getValue() const override { return LLSD(getValueF32()); }
  129. void setLabel(const std::string& label);
  130. bool setLabelArg(const std::string& key, const std::string& text) override;
  131. LL_INLINE void setMinValue(LLSD v) override { setMinValue((F32)v.asReal()); }
  132. LL_INLINE void setMaxValue(LLSD v) override { setMaxValue((F32)v.asReal()); }
  133. LL_INLINE bool isMouseHeldDown() const { return mSlider->hasMouseCapture(); }
  134. void setEnabled(bool b) override;
  135. void setRect(const LLRect& rect) override;
  136. void reshape(S32 width, S32 height, bool from_parent = true) override;
  137. void clear() override;
  138. void setPrecision(S32 precision);
  139. LL_INLINE void setMinValue(F32 v) { mSlider->setMinValue(v); updateText(); }
  140. LL_INLINE void setMaxValue(F32 v) { mSlider->setMaxValue(v); updateText(); }
  141. LL_INLINE void setIncrement(F32 inc) { mSlider->setIncrement(inc);}
  142. LL_INLINE F32 getMinValue() { return mSlider->getMinValue(); }
  143. LL_INLINE F32 getMaxValue() { return mSlider->getMaxValue(); }
  144. LL_INLINE void setLabelColor(const LLColor4& c) { mTextEnabledColor = c; }
  145. LL_INLINE void setDisabledLabelColor(const LLColor4& c)
  146. {
  147. mTextDisabledColor = c;
  148. }
  149. void setSliderMouseDownCallback(void (*cb)(LLUICtrl*, void*));
  150. void setSliderMouseUpCallback(void (*cb)(LLUICtrl*, void*));
  151. LL_INLINE void setMouseHoverCallback(void (*cb)(LLUICtrl*, void*))
  152. {
  153. mSlider->setMouseHoverCallback(cb);
  154. }
  155. void setOffLimit(const std::string& off_text, F32 off_value = 0.f);
  156. void onTabInto() override;
  157. // Marks value as tentative
  158. void setTentative(bool b) override;
  159. // Marks not tentative, then commits
  160. void onCommit() override;
  161. void setControlName(const char* ctrl, LLView* context) override;
  162. LL_INLINE const std::string& getControlName() const override
  163. {
  164. return mSlider->getControlName();
  165. }
  166. private:
  167. void updateText();
  168. void updateSliderRect();
  169. void reportInvalidData();
  170. static void onSliderCommit(LLUICtrl* caller, void* userdata);
  171. static void onSliderMouseDown(LLUICtrl* caller, void* userdata);
  172. static void onSliderMouseUp(LLUICtrl* caller, void* userdata);
  173. static void onEditorCommit(LLUICtrl* caller, void* userdata);
  174. static void onEditorGainFocus(LLFocusableElement* caller, void* userdata);
  175. static void onEditorChangeFocus(LLUICtrl* caller, S32 direction,
  176. void* userdata);
  177. private:
  178. const LLFontGL* mFont;
  179. LLSlider* mSlider;
  180. LLLineEditor* mEditor;
  181. LLTextBox* mTextBox;
  182. LLTextBox* mLabelBox;
  183. void (*mSliderMouseUpCallback)(LLUICtrl*, void*);
  184. void (*mSliderMouseDownCallback)(LLUICtrl*, void*);
  185. S32 mPrecision;
  186. S32 mLabelWidth;
  187. F32 mValue;
  188. F32 mOffValue;
  189. std::string mOffText;
  190. LLColor4 mTextEnabledColor;
  191. LLColor4 mTextDisabledColor;
  192. bool mShowText;
  193. bool mCanEditText;
  194. bool mDisplayOff;
  195. };
  196. #endif // LL_LLSLIDERCTRL_H