llspinctrl.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /**
  2. * @file llspinctrl.h
  3. * @brief Typical spinner with "up" and "down" arrow buttons.
  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_LLSPINCTRL_H
  33. #define LL_LLSPINCTRL_H
  34. #include "lllineeditor.h"
  35. class LLButton;
  36. class LLTextBox;
  37. //
  38. // Constants
  39. //
  40. constexpr S32 SPINCTRL_BTN_HEIGHT = 8;
  41. constexpr S32 SPINCTRL_BTN_WIDTH = 16;
  42. // Space between label right and button left:
  43. constexpr S32 SPINCTRL_SPACING = 2;
  44. constexpr S32 SPINCTRL_HEIGHT = 2 * SPINCTRL_BTN_HEIGHT;
  45. constexpr S32 SPINCTRL_DEFAULT_LABEL_WIDTH = 10;
  46. class LLSpinCtrl : public LLUICtrl
  47. {
  48. protected:
  49. LOG_CLASS(LLSpinCtrl);
  50. public:
  51. LLSpinCtrl(const std::string& name, const LLRect& rect,
  52. const std::string& label, const LLFontGL* font,
  53. void (*commit_callback)(LLUICtrl*, void*), void* userdata,
  54. F32 initial_value, F32 min_value, F32 max_value, F32 increment,
  55. const std::string& control_name = std::string(),
  56. S32 label_width = SPINCTRL_DEFAULT_LABEL_WIDTH);
  57. const std::string& getTag() const override;
  58. LLXMLNodePtr getXML(bool save_children = true) const override;
  59. static LLView* fromXML(LLXMLNodePtr node, LLView* parent,
  60. class LLUICtrlFactory* factory);
  61. void forceSetValue(const LLSD& value);
  62. void setValue(const LLSD& value) override;
  63. LL_INLINE LLSD getValue() const override { return mValue; }
  64. LL_INLINE F32 get() const { return (F32)getValue().asReal(); }
  65. LL_INLINE void set(F32 value) { setValue(value); mInitialValue = value; }
  66. void setMinValue(LLSD min_value) override { setMinValue((F32)min_value.asReal()); }
  67. void setMaxValue(LLSD max_value) override { setMaxValue((F32)max_value.asReal()); }
  68. bool isMouseHeldDown() const;
  69. void setEnabled(bool b) override;
  70. void setFocus(bool b) override;
  71. void clear() override;
  72. LL_INLINE bool isDirty() const override { return mValue != mInitialValue; }
  73. LL_INLINE void resetDirty() override { mInitialValue = mValue; }
  74. virtual void setPrecision(S32 precision);
  75. LL_INLINE virtual void setMinValue(F32 min) { mMinValue = min; }
  76. LL_INLINE virtual void setMaxValue(F32 max) { mMaxValue = max; }
  77. LL_INLINE virtual void setIncrement(F32 inc) { mIncrement = inc; }
  78. LL_INLINE virtual F32 getMinValue() { return mMinValue; }
  79. LL_INLINE virtual F32 getMaxValue() { return mMaxValue; }
  80. LL_INLINE virtual F32 getIncrement() { return mIncrement; }
  81. void setLabel(const std::string& label);
  82. LL_INLINE void setLabelColor(const LLColor4& c) { mTextEnabledColor = c; mDirty = true; }
  83. LL_INLINE void setDisabledLabelColor(const LLColor4& c)
  84. {
  85. mTextDisabledColor = c;
  86. mDirty = true;
  87. }
  88. void setAllowEdit(bool allow_edit);
  89. void onTabInto() override;
  90. void setTentative(bool b) override; // Marks value as tentative
  91. void onCommit() override; // Marks not tentative, then commits
  92. void forceEditorCommit(); // For commit on external button
  93. bool handleScrollWheel(S32 x, S32 y, S32 clicks) override;
  94. bool handleKeyHere(KEY key, MASK mask) override;
  95. void draw() override;
  96. LL_INLINE void setPrevalidate(LLLinePrevalidateFunc cb)
  97. {
  98. if (mEditor)
  99. {
  100. mEditor->setPrevalidate(cb);
  101. }
  102. }
  103. static void onEditorCommit(LLUICtrl* caller, void* userdata);
  104. static void onEditorGainFocus(LLFocusableElement* caller, void* userdata);
  105. static void onEditorLostFocus(LLFocusableElement* caller, void* userdata);
  106. static void onEditorChangeFocus(LLUICtrl* caller, S32 dir, void* userdata);
  107. static void onUpBtn(void* userdata);
  108. static void onDownBtn(void* userdata);
  109. typedef std::string (*preprocess_cb_t)(const std::string& text);
  110. LL_INLINE void setPreProcessCallback(preprocess_cb_t cb)
  111. {
  112. mPreProcessCallback = cb;
  113. }
  114. private:
  115. void updateEditor();
  116. void reportInvalidData();
  117. private:
  118. LLButton* mUpBtn;
  119. LLButton* mDownBtn;
  120. LLLineEditor* mEditor;
  121. LLTextBox* mLabelBox;
  122. preprocess_cb_t mPreProcessCallback;
  123. LLColor4 mTextEnabledColor;
  124. LLColor4 mTextDisabledColor;
  125. F32 mValue;
  126. F32 mInitialValue;
  127. F32 mMaxValue;
  128. F32 mMinValue;
  129. F32 mIncrement;
  130. S32 mPrecision;
  131. bool mHasBeenSet;
  132. bool mDirty;
  133. };
  134. #endif // LL_LLSPINCTRL_H