/indra/llui/llbutton.h

https://bitbucket.org/lindenlab/viewer-beta/ · C Header · 387 lines · 242 code · 89 blank · 56 comment · 0 complexity · f6601dd6c0344afc8e74e087f2d5f45c MD5 · raw file

  1. /**
  2. * @file llbutton.h
  3. * @brief Header for buttons
  4. *
  5. * $LicenseInfo:firstyear=2001&license=viewerlgpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2010, Linden Research, Inc.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License only.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  24. * $/LicenseInfo$
  25. */
  26. #ifndef LL_LLBUTTON_H
  27. #define LL_LLBUTTON_H
  28. #include "lluuid.h"
  29. #include "llbadgeowner.h"
  30. #include "llcontrol.h"
  31. #include "lluictrl.h"
  32. #include "v4color.h"
  33. #include "llframetimer.h"
  34. #include "llfontgl.h"
  35. #include "lluiimage.h"
  36. #include "lluistring.h"
  37. //
  38. // Constants
  39. //
  40. // PLEASE please use these "constants" when building your own buttons.
  41. // They are loaded from settings.xml at run time.
  42. extern S32 LLBUTTON_H_PAD;
  43. extern S32 BTN_HEIGHT_SMALL;
  44. extern S32 BTN_HEIGHT;
  45. //
  46. // Helpful functions
  47. //
  48. S32 round_up(S32 grid, S32 value);
  49. class LLUICtrlFactory;
  50. //
  51. // Classes
  52. //
  53. class LLButton
  54. : public LLUICtrl, public LLBadgeOwner
  55. {
  56. public:
  57. struct Params
  58. : public LLInitParam::Block<Params, LLUICtrl::Params>
  59. {
  60. // text label
  61. Optional<std::string> label_selected;
  62. Optional<bool> label_shadow;
  63. Optional<bool> auto_resize;
  64. Optional<bool> use_ellipses;
  65. // images
  66. Optional<LLUIImage*> image_unselected,
  67. image_selected,
  68. image_hover_selected,
  69. image_hover_unselected,
  70. image_disabled_selected,
  71. image_disabled,
  72. image_flash,
  73. image_pressed,
  74. image_pressed_selected,
  75. image_overlay;
  76. Optional<std::string> image_overlay_alignment;
  77. // colors
  78. Optional<LLUIColor> label_color,
  79. label_color_selected,
  80. label_color_disabled,
  81. label_color_disabled_selected,
  82. image_color,
  83. image_color_disabled,
  84. image_overlay_color,
  85. image_overlay_selected_color,
  86. image_overlay_disabled_color,
  87. flash_color;
  88. // layout
  89. Optional<S32> pad_right;
  90. Optional<S32> pad_left;
  91. Optional<S32> pad_bottom; // under text label
  92. //image overlay paddings
  93. Optional<S32> image_top_pad;
  94. Optional<S32> image_bottom_pad;
  95. /**
  96. * Space between image_overlay and label
  97. */
  98. Optional<S32> imgoverlay_label_space;
  99. // callbacks
  100. Optional<CommitCallbackParam> click_callback, // alias -> commit_callback
  101. mouse_down_callback,
  102. mouse_up_callback,
  103. mouse_held_callback;
  104. // misc
  105. Optional<bool> is_toggle,
  106. scale_image,
  107. commit_on_return,
  108. display_pressed_state;
  109. Optional<F32> hover_glow_amount;
  110. Optional<TimeIntervalParam> held_down_delay;
  111. Optional<bool> use_draw_context_alpha;
  112. Optional<LLBadge::Params> badge;
  113. Optional<bool> handle_right_mouse;
  114. Optional<S32> button_flash_count;
  115. Optional<F32> button_flash_rate;
  116. Params();
  117. };
  118. protected:
  119. friend class LLUICtrlFactory;
  120. LLButton(const Params&);
  121. public:
  122. ~LLButton();
  123. // For backward compatability only
  124. typedef boost::function<void(void*)> button_callback_t;
  125. void addImageAttributeToXML(LLXMLNodePtr node, const std::string& imageName,
  126. const LLUUID& imageID,const std::string& xmlTagName) const;
  127. virtual BOOL handleUnicodeCharHere(llwchar uni_char);
  128. virtual BOOL handleKeyHere(KEY key, MASK mask);
  129. virtual BOOL handleMouseDown(S32 x, S32 y, MASK mask);
  130. virtual BOOL handleMouseUp(S32 x, S32 y, MASK mask);
  131. virtual BOOL handleHover(S32 x, S32 y, MASK mask);
  132. virtual BOOL handleRightMouseDown(S32 x, S32 y, MASK mask);
  133. virtual BOOL handleRightMouseUp(S32 x, S32 y, MASK mask);
  134. virtual BOOL handleDoubleClick(S32 x, S32 y, MASK mask);
  135. virtual void draw();
  136. /*virtual*/ BOOL postBuild();
  137. virtual void onMouseLeave(S32 x, S32 y, MASK mask);
  138. virtual void onMouseCaptureLost();
  139. virtual void onCommit();
  140. void setUnselectedLabelColor( const LLColor4& c ) { mUnselectedLabelColor = c; }
  141. void setSelectedLabelColor( const LLColor4& c ) { mSelectedLabelColor = c; }
  142. void setUseEllipses( BOOL use_ellipses ) { mUseEllipses = use_ellipses; }
  143. boost::signals2::connection setClickedCallback(const CommitCallbackParam& cb);
  144. boost::signals2::connection setMouseDownCallback(const CommitCallbackParam& cb);
  145. boost::signals2::connection setMouseUpCallback(const CommitCallbackParam& cb);
  146. boost::signals2::connection setHeldDownCallback(const CommitCallbackParam& cb);
  147. boost::signals2::connection setClickedCallback( const commit_signal_t::slot_type& cb ); // mouse down and up within button
  148. boost::signals2::connection setMouseDownCallback( const commit_signal_t::slot_type& cb );
  149. boost::signals2::connection setMouseUpCallback( const commit_signal_t::slot_type& cb ); // mouse up, EVEN IF NOT IN BUTTON
  150. // Passes a 'count' parameter in the commit param payload, i.e. param["count"])
  151. boost::signals2::connection setHeldDownCallback( const commit_signal_t::slot_type& cb ); // Mouse button held down and in button
  152. // *TODO: Deprecate (for backwards compatability only)
  153. boost::signals2::connection setClickedCallback( button_callback_t cb, void* data );
  154. boost::signals2::connection setMouseDownCallback( button_callback_t cb, void* data );
  155. boost::signals2::connection setMouseUpCallback( button_callback_t cb, void* data );
  156. boost::signals2::connection setHeldDownCallback( button_callback_t cb, void* data );
  157. void setHeldDownDelay( F32 seconds, S32 frames = 0) { mHeldDownDelay = seconds; mHeldDownFrameDelay = frames; }
  158. F32 getHeldDownTime() const { return mMouseDownTimer.getElapsedTimeF32(); }
  159. BOOL toggleState();
  160. BOOL getToggleState() const;
  161. void setToggleState(BOOL b);
  162. void setHighlight(bool b);
  163. void setFlashing( BOOL b );
  164. BOOL getFlashing() const { return mFlashing; }
  165. void setHAlign( LLFontGL::HAlign align ) { mHAlign = align; }
  166. LLFontGL::HAlign getHAlign() const { return mHAlign; }
  167. void setLeftHPad( S32 pad ) { mLeftHPad = pad; }
  168. void setRightHPad( S32 pad ) { mRightHPad = pad; }
  169. void setImageOverlayTopPad( S32 pad ) { mImageOverlayTopPad = pad; }
  170. S32 getImageOverlayTopPad() const { return mImageOverlayTopPad; }
  171. void setImageOverlayBottomPad( S32 pad ) { mImageOverlayBottomPad = pad; }
  172. S32 getImageOverlayBottomPad() const { return mImageOverlayBottomPad; }
  173. const std::string getLabelUnselected() const { return wstring_to_utf8str(mUnselectedLabel); }
  174. const std::string getLabelSelected() const { return wstring_to_utf8str(mSelectedLabel); }
  175. void setImageColor(const std::string& color_control);
  176. void setImageColor(const LLColor4& c);
  177. /*virtual*/ void setColor(const LLColor4& c);
  178. void setImages(const std::string &image_name, const std::string &selected_name);
  179. void setDisabledImageColor(const LLColor4& c) { mDisabledImageColor = c; }
  180. void setDisabledSelectedLabelColor( const LLColor4& c ) { mDisabledSelectedLabelColor = c; }
  181. void setImageOverlay(const std::string& image_name, LLFontGL::HAlign alignment = LLFontGL::HCENTER, const LLColor4& color = LLColor4::white);
  182. void setImageOverlay(const LLUUID& image_id, LLFontGL::HAlign alignment = LLFontGL::HCENTER, const LLColor4& color = LLColor4::white);
  183. LLPointer<LLUIImage> getImageOverlay() { return mImageOverlay; }
  184. LLFontGL::HAlign getImageOverlayHAlign() const { return mImageOverlayAlignment; }
  185. void autoResize(); // resize with label of current btn state
  186. void resize(LLUIString label); // resize with label input
  187. void setLabel( const LLStringExplicit& label);
  188. virtual BOOL setLabelArg( const std::string& key, const LLStringExplicit& text );
  189. void setLabelUnselected(const LLStringExplicit& label);
  190. void setLabelSelected(const LLStringExplicit& label);
  191. void setDisabledLabelColor( const LLColor4& c ) { mDisabledLabelColor = c; }
  192. void setFont(const LLFontGL *font)
  193. { mGLFont = ( font ? font : LLFontGL::getFontSansSerif()); }
  194. const LLFontGL* getFont() const { return mGLFont; }
  195. S32 getLastDrawCharsCount() const { return mLastDrawCharsCount; }
  196. bool labelIsTruncated() const;
  197. const LLUIString& getCurrentLabel() const;
  198. void setScaleImage(BOOL scale) { mScaleImage = scale; }
  199. BOOL getScaleImage() const { return mScaleImage; }
  200. void setDropShadowedText(BOOL b) { mDropShadowedText = b; }
  201. void setBorderEnabled(BOOL b) { mBorderEnabled = b; }
  202. void setHoverGlowStrength(F32 strength) { mHoverGlowStrength = strength; }
  203. void setImageUnselected(LLPointer<LLUIImage> image);
  204. void setImageSelected(LLPointer<LLUIImage> image);
  205. void setImageHoverSelected(LLPointer<LLUIImage> image);
  206. void setImageHoverUnselected(LLPointer<LLUIImage> image);
  207. void setImageDisabled(LLPointer<LLUIImage> image);
  208. void setImageDisabledSelected(LLPointer<LLUIImage> image);
  209. void setImageFlash(LLPointer<LLUIImage> image);
  210. void setImagePressed(LLPointer<LLUIImage> image);
  211. void setCommitOnReturn(BOOL commit) { mCommitOnReturn = commit; }
  212. BOOL getCommitOnReturn() const { return mCommitOnReturn; }
  213. static void onHeldDown(void *userdata); // to be called by gIdleCallbacks
  214. static void toggleFloaterAndSetToggleState(LLUICtrl* ctrl, const LLSD& sdname);
  215. static void setFloaterToggle(LLUICtrl* ctrl, const LLSD& sdname);
  216. static void setDockableFloaterToggle(LLUICtrl* ctrl, const LLSD& sdname);
  217. static void showHelp(LLUICtrl* ctrl, const LLSD& sdname);
  218. void setForcePressedState(bool b) { mForcePressedState = b; }
  219. void setAutoResize(bool auto_resize) { mAutoResize = auto_resize; }
  220. protected:
  221. LLPointer<LLUIImage> getImageUnselected() const { return mImageUnselected; }
  222. LLPointer<LLUIImage> getImageSelected() const { return mImageSelected; }
  223. void getOverlayImageSize(S32& overlay_width, S32& overlay_height);
  224. LLFrameTimer mMouseDownTimer;
  225. bool mNeedsHighlight;
  226. S32 mButtonFlashCount;
  227. F32 mButtonFlashRate;
  228. void drawBorder(LLUIImage* imagep, const LLColor4& color, S32 size);
  229. void resetMouseDownTimer();
  230. commit_signal_t* mMouseDownSignal;
  231. commit_signal_t* mMouseUpSignal;
  232. commit_signal_t* mHeldDownSignal;
  233. const LLFontGL* mGLFont;
  234. S32 mMouseDownFrame;
  235. S32 mMouseHeldDownCount; // Counter for parameter passed to held-down callback
  236. F32 mHeldDownDelay; // seconds, after which held-down callbacks get called
  237. S32 mHeldDownFrameDelay; // frames, after which held-down callbacks get called
  238. S32 mLastDrawCharsCount;
  239. LLPointer<LLUIImage> mImageOverlay;
  240. LLFontGL::HAlign mImageOverlayAlignment;
  241. LLUIColor mImageOverlayColor;
  242. LLUIColor mImageOverlaySelectedColor;
  243. LLUIColor mImageOverlayDisabledColor;
  244. LLPointer<LLUIImage> mImageUnselected;
  245. LLUIString mUnselectedLabel;
  246. LLUIColor mUnselectedLabelColor;
  247. LLPointer<LLUIImage> mImageSelected;
  248. LLUIString mSelectedLabel;
  249. LLUIColor mSelectedLabelColor;
  250. LLPointer<LLUIImage> mImageHoverSelected;
  251. LLPointer<LLUIImage> mImageHoverUnselected;
  252. LLPointer<LLUIImage> mImageDisabled;
  253. LLUIColor mDisabledLabelColor;
  254. LLPointer<LLUIImage> mImageDisabledSelected;
  255. LLUIString mDisabledSelectedLabel;
  256. LLUIColor mDisabledSelectedLabelColor;
  257. LLPointer<LLUIImage> mImagePressed;
  258. LLPointer<LLUIImage> mImagePressedSelected;
  259. /* There are two ways an image can flash- by making changes in color according to flash_color attribute
  260. or by changing icon from current to the one specified in image_flash. Second way is used only if
  261. flash icon name is set in attributes(by default it isn't). First way is used otherwise. */
  262. LLPointer<LLUIImage> mImageFlash;
  263. LLUIColor mFlashBgColor;
  264. LLUIColor mImageColor;
  265. LLUIColor mDisabledImageColor;
  266. bool mIsToggle;
  267. bool mScaleImage;
  268. bool mDropShadowedText;
  269. bool mAutoResize;
  270. bool mUseEllipses;
  271. bool mBorderEnabled;
  272. bool mFlashing;
  273. LLFontGL::HAlign mHAlign;
  274. S32 mLeftHPad;
  275. S32 mRightHPad;
  276. S32 mBottomVPad; // under text label
  277. S32 mImageOverlayTopPad;
  278. S32 mImageOverlayBottomPad;
  279. bool mUseDrawContextAlpha;
  280. /*
  281. * Space between image_overlay and label
  282. */
  283. S32 mImgOverlayLabelSpace;
  284. F32 mHoverGlowStrength;
  285. F32 mCurGlowStrength;
  286. bool mCommitOnReturn;
  287. bool mFadeWhenDisabled;
  288. bool mForcePressedState;
  289. bool mDisplayPressedState;
  290. LLFrameTimer mFlashingTimer;
  291. bool mHandleRightMouse;
  292. };
  293. // Build time optimization, generate once in .cpp file
  294. #ifndef LLBUTTON_CPP
  295. extern template class LLButton* LLView::getChild<class LLButton>(
  296. const std::string& name, BOOL recurse) const;
  297. #endif
  298. #endif // LL_LLBUTTON_H