llbutton.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /**
  2. * @file llbutton.h
  3. * @brief Header for buttons
  4. *
  5. * $LicenseInfo:firstyear=2001&license=viewergpl$
  6. *
  7. * Copyright (c) 2001-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_LLBUTTON_H
  33. #define LL_LLBUTTON_H
  34. #include "llcontrol.h"
  35. #include "llframetimer.h"
  36. #include "llimage.h"
  37. #include "lluictrl.h"
  38. #include "lluistring.h"
  39. #include "lluuid.h"
  40. #include "llcolor4.h"
  41. //
  42. // Constants
  43. //
  44. // PLEASE please use these "constants" when building your own buttons. They are
  45. // loaded from settings.xml at run time.
  46. extern S32 gButtonHPad;
  47. extern S32 gButtonVPad;
  48. extern S32 gBtnHeightSmall;
  49. extern S32 gBtnHeight;
  50. //
  51. // Helpful functions
  52. //
  53. S32 round_up(S32 grid, S32 value);
  54. class LLUICtrlFactory;
  55. //
  56. // Classes
  57. //
  58. class LLButton : public LLUICtrl
  59. {
  60. protected:
  61. LOG_CLASS(LLButton);
  62. public:
  63. // Simple button with text label
  64. LLButton(const std::string& name, const LLRect& rect,
  65. const char* control_name = NULL,
  66. void (*click_callback)(void*) = NULL,
  67. void* callback_data = NULL);
  68. LLButton(const std::string& name, const LLRect& rect,
  69. const std::string& unselected_image,
  70. const std::string& selected_image, const char* control_name,
  71. void (*click_callback)(void*), void* callback_data = NULL,
  72. const LLFontGL* mGLFont = NULL,
  73. const std::string& unselected_label = LLStringUtil::null,
  74. const std::string& selected_label = LLStringUtil::null);
  75. ~LLButton() override;
  76. void init(void (*click_callback)(void*), void* callback_data,
  77. const LLFontGL* font, const char* control_name);
  78. const std::string& getTag() const override;
  79. void addImageAttributeToXML(LLXMLNodePtr node,
  80. const std::string& image_name,
  81. const LLUUID& imageID,
  82. const std::string& xml_tag_name) const;
  83. LLXMLNodePtr getXML(bool save_children = true) const override;
  84. static LLView* fromXML(LLXMLNodePtr node, LLView* parent,
  85. LLUICtrlFactory* factory);
  86. bool handleUnicodeCharHere(llwchar uni_char) override;
  87. bool handleKeyHere(KEY key, MASK mask) override;
  88. bool handleMouseDown(S32 x, S32 y, MASK mask) override;
  89. bool handleMouseUp(S32 x, S32 y, MASK mask) override;
  90. bool handleHover(S32 x, S32 y, MASK mask) override;
  91. void draw() override;
  92. void onMouseCaptureLost() override;
  93. void onCommit() override;
  94. LL_INLINE void setUnselectedLabelColor(const LLColor4& c) { mUnselectedLabelColor = c; }
  95. LL_INLINE void setSelectedLabelColor(const LLColor4& c) { mSelectedLabelColor = c; }
  96. // Mouse down and up within button:
  97. void setClickedCallback(void (*cb)(void*), void* data = NULL);
  98. // Mouse hovering within button
  99. LL_INLINE void setMouseHoverCallback(void (*cb)(void*)) { mMouseHoverCallback = cb; }
  100. // Mouse down within button
  101. LL_INLINE void setMouseDownCallback(void (*cb)(void*)) { mMouseDownCallback = cb; }
  102. // Mouse up, EVEN IF NOT IN BUTTON
  103. LL_INLINE void setMouseUpCallback(void (*cb)(void*)) { mMouseUpCallback = cb; }
  104. // Mouse button held down and in button
  105. LL_INLINE void setHeldDownCallback(void (*cb)(void*)) { mHeldDownCallback = cb; }
  106. LL_INLINE void setHeldDownDelay(F32 seconds, S32 frames = 0)
  107. {
  108. mHeldDownDelay = seconds;
  109. mHeldDownFrameDelay = frames;
  110. }
  111. LL_INLINE F32 getHeldDownTime() const { return mMouseDownTimer.getElapsedTimeF32(); }
  112. LL_INLINE bool getIsToggle() const { return mIsToggle; }
  113. LL_INLINE void setIsToggle(bool is_toggle) { mIsToggle = is_toggle; }
  114. bool toggleState();
  115. LL_INLINE bool getToggleState() const { return mToggleState; }
  116. void setToggleState(bool b);
  117. void setHighlight(bool b) { mNeedsHighlight = b; }
  118. void setFlashing(bool b);
  119. LL_INLINE bool getFlashing() const { return mFlashing; }
  120. LL_INLINE void setHAlign(LLFontGL::HAlign align) { mHAlign = align; }
  121. LL_INLINE LLFontGL::HAlign getHAlign() const { return mHAlign; }
  122. LL_INLINE void setLeftHPad(S32 pad) { mLeftHPad = pad; }
  123. LL_INLINE void setRightHPad(S32 pad) { mRightHPad = pad; }
  124. LL_INLINE const std::string getLabelUnselected() const { return wstring_to_utf8str(mUnselectedLabel); }
  125. LL_INLINE const std::string getLabelSelected() const { return wstring_to_utf8str(mSelectedLabel); }
  126. LL_INLINE const std::string getCurrentLabel() const
  127. {
  128. return mToggleState ? getLabelSelected() : getLabelUnselected();
  129. }
  130. void setImageColor(const std::string& color_control);
  131. void setImageColor(const LLColor4& c);
  132. void setColor(const LLColor4& c) override;
  133. void setAlpha(F32 alpha) override;
  134. void setImages(const std::string& image_name,
  135. const std::string& selected_name);
  136. // Sets both selected and unselected images to image_name
  137. void setImages(const std::string& image_name);
  138. void setDisabledImages(const std::string& image_name,
  139. const std::string& selected_name);
  140. void setDisabledImages(const std::string& image_name,
  141. const std::string& selected_name,
  142. const LLColor4& c);
  143. void setHoverImages(const std::string& image_name,
  144. const std::string& selected_name);
  145. LL_INLINE void setDisabledImageColor(const LLColor4& c) { mDisabledImageColor = c; }
  146. LL_INLINE void setDisabledSelectedLabelColor(const LLColor4& c) { mDisabledSelectedLabelColor = c; }
  147. void setImageOverlay(const std::string& image_name,
  148. LLFontGL::HAlign alignment = LLFontGL::HCENTER,
  149. const LLColor4& color = LLColor4::white);
  150. void setImageOverlay(LLUIImagePtr image,
  151. LLFontGL::HAlign alignment = LLFontGL::HCENTER,
  152. const LLColor4& color = LLColor4::white);
  153. LL_INLINE LLUIImagePtr getImageOverlay() { return mImageOverlay; }
  154. LL_INLINE void setValue(const LLSD& value) override { mToggleState = value.asBoolean(); }
  155. LL_INLINE LLSD getValue() const override { return LLSD(mToggleState); }
  156. LL_INLINE bool setLabelArg(const std::string& key,
  157. const std::string& text) override
  158. {
  159. mUnselectedLabel.setArg(key, text);
  160. mSelectedLabel.setArg(key, text);
  161. return true;
  162. }
  163. LL_INLINE void setLabelUnselected(const std::string& label) { mUnselectedLabel = label; }
  164. LL_INLINE void setLabelSelected(const std::string& label) { mSelectedLabel = label; }
  165. LL_INLINE void setLabel(const std::string& label) { mUnselectedLabel = mSelectedLabel = label; }
  166. LL_INLINE void setDisabledLabel(const std::string& label) { mDisabledLabel = label; }
  167. LL_INLINE void setDisabledSelectedLabel(const std::string& label)
  168. {
  169. mDisabledSelectedLabel = label;
  170. }
  171. LL_INLINE void setDisabledLabelColor(const LLColor4& c) { mDisabledLabelColor = c; }
  172. LL_INLINE void setFont(const LLFontGL* font) { mGLFont = font ? font : LLFontGL::getFontSansSerif(); }
  173. LL_INLINE void setScaleImage(bool scale) { mScaleImage = scale; }
  174. LL_INLINE bool getScaleImage() const { return mScaleImage; }
  175. LL_INLINE void setDropShadowedText(bool b) { mDropShadowedText = b; }
  176. LL_INLINE void setBorderEnabled(bool b) { mBorderEnabled = b; }
  177. // to be called by gIdleCallbacks
  178. static void onHeldDown(void* userdata);
  179. LL_INLINE void setHoverGlowStrength(F32 strength) { mHoverGlowStrength = strength; }
  180. void setImageUnselected(const std::string& image_name);
  181. LL_INLINE const std::string& getImageUnselectedName() const { return mImageUnselectedName; }
  182. void setImageSelected(const std::string& image_name);
  183. LL_INLINE const std::string& getImageSelectedName() const { return mImageSelectedName; }
  184. void setImageHoverSelected(const std::string& image_name);
  185. void setImageHoverUnselected(const std::string& image_name);
  186. void setImageDisabled(const std::string& image_name);
  187. void setImageDisabledSelected(const std::string& image_name);
  188. void setImageUnselected(LLUIImagePtr image);
  189. void setImageSelected(LLUIImagePtr image);
  190. void setImageHoverSelected(LLUIImagePtr image);
  191. void setImageHoverUnselected(LLUIImagePtr image);
  192. void setImageDisabled(LLUIImagePtr image);
  193. void setImageDisabledSelected(LLUIImagePtr image);
  194. LL_INLINE void setCommitOnReturn(bool commit) { mCommitOnReturn = commit; }
  195. LL_INLINE bool getCommitOnReturn() const { return mCommitOnReturn; }
  196. void setHelpURLCallback(const std::string& help_url);
  197. LL_INLINE const std::string& getHelpURL() const { return mHelpURL; }
  198. protected:
  199. virtual void drawBorder(const LLColor4& color, S32 size);
  200. void setImageUnselectedID(const LLUUID& image_id);
  201. LL_INLINE const LLUUID& getImageUnselectedID() const { return mImageUnselectedID; }
  202. void setImageSelectedID(const LLUUID& image_id);
  203. LL_INLINE const LLUUID& getImageSelectedID() const { return mImageSelectedID; }
  204. void setImageHoverSelectedID(const LLUUID& image_id);
  205. void setImageHoverUnselectedID(const LLUUID& image_id);
  206. void setImageDisabledID(const LLUUID& image_id);
  207. void setImageDisabledSelectedID(const LLUUID& image_id);
  208. LL_INLINE const LLUIImagePtr& getImageUnselected() const { return mImageUnselected; }
  209. LL_INLINE const LLUIImagePtr& getImageSelected() const { return mImageSelected; }
  210. protected:
  211. LLFrameTimer mMouseDownTimer;
  212. private:
  213. void (*mClickedCallback)(void*);
  214. void (*mMouseHoverCallback)(void*);
  215. void (*mMouseDownCallback)(void*);
  216. void (*mMouseUpCallback)(void*);
  217. void (*mHeldDownCallback)(void*);
  218. const LLFontGL* mGLFont;
  219. S32 mMouseDownFrame;
  220. // seconds, after which held-down callbacks get called:
  221. F32 mHeldDownDelay;
  222. // frames, after which held-down callbacks get called:
  223. S32 mHeldDownFrameDelay;
  224. LLUIImagePtr mImageOverlay;
  225. LLFontGL::HAlign mImageOverlayAlignment;
  226. LLColor4 mImageOverlayColor;
  227. LLUIImagePtr mImageUnselected;
  228. LLUIString mUnselectedLabel;
  229. LLColor4 mUnselectedLabelColor;
  230. LLUIImagePtr mImageSelected;
  231. LLUIString mSelectedLabel;
  232. LLColor4 mSelectedLabelColor;
  233. LLUIImagePtr mImageHoverSelected;
  234. LLUIImagePtr mImageHoverUnselected;
  235. LLUIImagePtr mImageDisabled;
  236. LLUIString mDisabledLabel;
  237. LLColor4 mDisabledLabelColor;
  238. LLUIImagePtr mImageDisabledSelected;
  239. LLUIString mDisabledSelectedLabel;
  240. LLColor4 mDisabledSelectedLabelColor;
  241. LLUUID mImageUnselectedID;
  242. LLUUID mImageSelectedID;
  243. LLUUID mImageHoverSelectedID;
  244. LLUUID mImageHoverUnselectedID;
  245. LLUUID mImageDisabledID;
  246. LLUUID mImageDisabledSelectedID;
  247. LLColor4 mFlashBgColor;
  248. LLColor4 mImageColor;
  249. LLColor4 mDisabledImageColor;
  250. bool mIsToggle;
  251. bool mToggleState;
  252. bool mScaleImage;
  253. bool mDropShadowedText;
  254. bool mBorderEnabled;
  255. bool mNeedsHighlight;
  256. bool mCommitOnReturn;
  257. bool mFlashing;
  258. LLFrameTimer mFlashingTimer;
  259. LLFontGL::HAlign mHAlign;
  260. S32 mLeftHPad;
  261. S32 mRightHPad;
  262. F32 mHoverGlowStrength;
  263. F32 mCurGlowStrength;
  264. std::string mImageUnselectedName;
  265. std::string mImageSelectedName;
  266. std::string mImageHoverSelectedName;
  267. std::string mImageHoverUnselectedName;
  268. std::string mImageDisabledName;
  269. std::string mImageDisabledSelectedName;
  270. std::string mHelpURL;
  271. LLUIImagePtr mImagep;
  272. };
  273. #endif // LL_LLBUTTON_H