/indra/llui/lluictrl.h

https://bitbucket.org/lindenlab/viewer-beta/ · C Header · 328 lines · 202 code · 74 blank · 52 comment · 1 complexity · 297e8a763a2471d345fc4c6473f66d16 MD5 · raw file

  1. /**
  2. * @file lluictrl.h
  3. * @author James Cook, Richard Nelson, Tom Yedwab
  4. * @brief Abstract base class for UI controls
  5. *
  6. * $LicenseInfo:firstyear=2001&license=viewerlgpl$
  7. * Second Life Viewer Source Code
  8. * Copyright (C) 2010, Linden Research, Inc.
  9. *
  10. * This library is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation;
  13. * version 2.1 of the License only.
  14. *
  15. * This library is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with this library; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. *
  24. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  25. * $/LicenseInfo$
  26. */
  27. #ifndef LL_LLUICTRL_H
  28. #define LL_LLUICTRL_H
  29. //#include "llboost.h"
  30. #include "llrect.h"
  31. #include "llsd.h"
  32. #include <boost/function.hpp>
  33. #include <boost/signals2.hpp>
  34. #include "llinitparam.h"
  35. #include "llview.h"
  36. #include "llviewmodel.h" // *TODO move dependency to .cpp file
  37. const BOOL TAKE_FOCUS_YES = TRUE;
  38. const BOOL TAKE_FOCUS_NO = FALSE;
  39. class LLUICtrl
  40. : public LLView, public boost::signals2::trackable
  41. {
  42. public:
  43. typedef boost::function<void (LLUICtrl* ctrl, const LLSD& param)> commit_callback_t;
  44. typedef boost::signals2::signal<void (LLUICtrl* ctrl, const LLSD& param)> commit_signal_t;
  45. // *TODO: add xml support for this type of signal in the future
  46. typedef boost::signals2::signal<void (LLUICtrl* ctrl, S32 x, S32 y, MASK mask)> mouse_signal_t;
  47. typedef boost::function<bool (LLUICtrl* ctrl, const LLSD& param)> enable_callback_t;
  48. typedef boost::signals2::signal<bool (LLUICtrl* ctrl, const LLSD& param), boost_boolean_combiner> enable_signal_t;
  49. struct CallbackParam : public LLInitParam::Block<CallbackParam>
  50. {
  51. Ignored name;
  52. Optional<std::string> function_name;
  53. Optional<LLSD> parameter;
  54. Optional<std::string> control_name;
  55. CallbackParam();
  56. };
  57. struct CommitCallbackParam : public LLInitParam::Block<CommitCallbackParam, CallbackParam >
  58. {
  59. Optional<commit_callback_t> function;
  60. };
  61. // also used for visible callbacks
  62. struct EnableCallbackParam : public LLInitParam::Block<EnableCallbackParam, CallbackParam >
  63. {
  64. Optional<enable_callback_t> function;
  65. };
  66. struct EnableControls : public LLInitParam::ChoiceBlock<EnableControls>
  67. {
  68. Alternative<std::string> enabled;
  69. Alternative<std::string> disabled;
  70. EnableControls();
  71. };
  72. struct ControlVisibility : public LLInitParam::ChoiceBlock<ControlVisibility>
  73. {
  74. Alternative<std::string> visible;
  75. Alternative<std::string> invisible;
  76. ControlVisibility();
  77. };
  78. struct Params : public LLInitParam::Block<Params, LLView::Params>
  79. {
  80. Optional<std::string> label;
  81. Optional<bool> tab_stop,
  82. chrome,
  83. requests_front;
  84. Optional<LLSD> initial_value;
  85. Optional<CommitCallbackParam> init_callback,
  86. commit_callback;
  87. Optional<EnableCallbackParam> validate_callback;
  88. Optional<CommitCallbackParam> mouseenter_callback,
  89. mouseleave_callback;
  90. Optional<std::string> control_name;
  91. Optional<EnableControls> enabled_controls;
  92. Optional<ControlVisibility> controls_visibility;
  93. // font params
  94. Optional<const LLFontGL*> font;
  95. Optional<LLFontGL::HAlign> font_halign;
  96. Optional<LLFontGL::VAlign> font_valign;
  97. // cruft from LLXMLNode implementation
  98. Ignored type,
  99. length;
  100. Params();
  101. };
  102. enum ETypeTransparency
  103. {
  104. TT_DEFAULT,
  105. TT_ACTIVE, // focused floater
  106. TT_INACTIVE, // other floaters
  107. TT_FADING, // fading toast
  108. };
  109. /*virtual*/ ~LLUICtrl();
  110. void initFromParams(const Params& p);
  111. protected:
  112. friend class LLUICtrlFactory;
  113. static const Params& getDefaultParams();
  114. LLUICtrl(const Params& p = getDefaultParams(),
  115. const LLViewModelPtr& viewmodel=LLViewModelPtr(new LLViewModel));
  116. commit_signal_t::slot_type initCommitCallback(const CommitCallbackParam& cb);
  117. enable_signal_t::slot_type initEnableCallback(const EnableCallbackParam& cb);
  118. // We need this virtual so we can override it with derived versions
  119. virtual LLViewModel* getViewModel() const;
  120. // We shouldn't ever need to set this directly
  121. //virtual void setViewModel(const LLViewModelPtr&);
  122. virtual BOOL postBuild();
  123. public:
  124. // LLView interface
  125. /*virtual*/ BOOL setLabelArg( const std::string& key, const LLStringExplicit& text );
  126. /*virtual*/ BOOL isCtrl() const;
  127. /*virtual*/ void onMouseEnter(S32 x, S32 y, MASK mask);
  128. /*virtual*/ void onMouseLeave(S32 x, S32 y, MASK mask);
  129. /*virtual*/ BOOL canFocusChildren() const;
  130. /*virtual*/ BOOL handleMouseDown(S32 x, S32 y, MASK mask);
  131. /*virtual*/ BOOL handleMouseUp(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. // From LLFocusableElement
  136. /*virtual*/ void setFocus( BOOL b );
  137. /*virtual*/ BOOL hasFocus() const;
  138. // New virtuals
  139. // Return NULL by default (overrride if the class has the appropriate interface)
  140. virtual class LLCtrlSelectionInterface* getSelectionInterface();
  141. virtual class LLCtrlListInterface* getListInterface();
  142. virtual class LLCtrlScrollInterface* getScrollInterface();
  143. bool setControlValue(const LLSD& value);
  144. void setControlVariable(LLControlVariable* control);
  145. virtual void setControlName(const std::string& control, LLView *context = NULL);
  146. LLControlVariable* getControlVariable() { return mControlVariable; }
  147. void setEnabledControlVariable(LLControlVariable* control);
  148. void setDisabledControlVariable(LLControlVariable* control);
  149. void setMakeVisibleControlVariable(LLControlVariable* control);
  150. void setMakeInvisibleControlVariable(LLControlVariable* control);
  151. virtual void setTentative(BOOL b);
  152. virtual BOOL getTentative() const;
  153. virtual void setValue(const LLSD& value);
  154. virtual LLSD getValue() const;
  155. /// When two widgets are displaying the same data (e.g. during a skin
  156. /// change), share their ViewModel.
  157. virtual void shareViewModelFrom(const LLUICtrl& other);
  158. virtual BOOL setTextArg( const std::string& key, const LLStringExplicit& text );
  159. virtual void setIsChrome(BOOL is_chrome);
  160. virtual BOOL acceptsTextInput() const; // Defaults to false
  161. // A control is dirty if the user has modified its value.
  162. // Editable controls should override this.
  163. virtual BOOL isDirty() const; // Defauls to false
  164. virtual void resetDirty(); //Defaults to no-op
  165. // Call appropriate callback
  166. virtual void onCommit();
  167. // Default to no-op:
  168. virtual void onTabInto();
  169. // Clear any user-provided input (text in a text editor, checked checkbox,
  170. // selected radio button, etc.). Defaults to no-op.
  171. virtual void clear();
  172. virtual void setColor(const LLColor4& color);
  173. F32 getCurrentTransparency();
  174. void setTransparencyType(ETypeTransparency type);
  175. ETypeTransparency getTransparencyType() const {return mTransparencyType;}
  176. BOOL focusNextItem(BOOL text_entry_only);
  177. BOOL focusPrevItem(BOOL text_entry_only);
  178. BOOL focusFirstItem(BOOL prefer_text_fields = FALSE, BOOL focus_flash = TRUE );
  179. BOOL focusLastItem(BOOL prefer_text_fields = FALSE);
  180. // Non Virtuals
  181. LLHandle<LLUICtrl> getHandle() const { return getDerivedHandle<LLUICtrl>(); }
  182. BOOL getIsChrome() const;
  183. void setTabStop( BOOL b );
  184. BOOL hasTabStop() const;
  185. LLUICtrl* getParentUICtrl() const;
  186. // return true if help topic found by crawling through parents -
  187. // topic then put in help_topic_out
  188. bool findHelpTopic(std::string& help_topic_out);
  189. boost::signals2::connection setCommitCallback(const CommitCallbackParam& cb);
  190. boost::signals2::connection setValidateCallback(const EnableCallbackParam& cb);
  191. boost::signals2::connection setCommitCallback( const commit_signal_t::slot_type& cb );
  192. boost::signals2::connection setValidateCallback( const enable_signal_t::slot_type& cb );
  193. boost::signals2::connection setMouseEnterCallback( const commit_signal_t::slot_type& cb );
  194. boost::signals2::connection setMouseLeaveCallback( const commit_signal_t::slot_type& cb );
  195. boost::signals2::connection setMouseDownCallback( const mouse_signal_t::slot_type& cb );
  196. boost::signals2::connection setMouseUpCallback( const mouse_signal_t::slot_type& cb );
  197. boost::signals2::connection setRightMouseDownCallback( const mouse_signal_t::slot_type& cb );
  198. boost::signals2::connection setRightMouseUpCallback( const mouse_signal_t::slot_type& cb );
  199. boost::signals2::connection setDoubleClickCallback( const mouse_signal_t::slot_type& cb );
  200. // *TODO: Deprecate; for backwards compatability only:
  201. boost::signals2::connection setCommitCallback( boost::function<void (LLUICtrl*,void*)> cb, void* data);
  202. boost::signals2::connection setValidateBeforeCommit( boost::function<bool (const LLSD& data)> cb );
  203. LLUICtrl* findRootMostFocusRoot();
  204. class LLTextInputFilter : public LLQueryFilter, public LLSingleton<LLTextInputFilter>
  205. {
  206. /*virtual*/ filterResult_t operator() (const LLView* const view, const viewList_t & children) const
  207. {
  208. return filterResult_t(view->isCtrl() && static_cast<const LLUICtrl *>(view)->acceptsTextInput(), TRUE);
  209. }
  210. };
  211. template <typename F, typename DERIVED> class CallbackRegistry : public LLRegistrySingleton<std::string, F, DERIVED >
  212. {};
  213. class CommitCallbackRegistry : public CallbackRegistry<commit_callback_t, CommitCallbackRegistry>{};
  214. // the enable callback registry is also used for visiblity callbacks
  215. class EnableCallbackRegistry : public CallbackRegistry<enable_callback_t, EnableCallbackRegistry>{};
  216. protected:
  217. static bool controlListener(const LLSD& newvalue, LLHandle<LLUICtrl> handle, std::string type);
  218. commit_signal_t* mCommitSignal;
  219. enable_signal_t* mValidateSignal;
  220. commit_signal_t* mMouseEnterSignal;
  221. commit_signal_t* mMouseLeaveSignal;
  222. mouse_signal_t* mMouseDownSignal;
  223. mouse_signal_t* mMouseUpSignal;
  224. mouse_signal_t* mRightMouseDownSignal;
  225. mouse_signal_t* mRightMouseUpSignal;
  226. mouse_signal_t* mDoubleClickSignal;
  227. LLViewModelPtr mViewModel;
  228. LLControlVariable* mControlVariable;
  229. boost::signals2::connection mControlConnection;
  230. LLControlVariable* mEnabledControlVariable;
  231. boost::signals2::connection mEnabledControlConnection;
  232. LLControlVariable* mDisabledControlVariable;
  233. boost::signals2::connection mDisabledControlConnection;
  234. LLControlVariable* mMakeVisibleControlVariable;
  235. boost::signals2::connection mMakeVisibleControlConnection;
  236. LLControlVariable* mMakeInvisibleControlVariable;
  237. boost::signals2::connection mMakeInvisibleControlConnection;
  238. static F32 sActiveControlTransparency;
  239. static F32 sInactiveControlTransparency;
  240. virtual void addInfo(LLSD & info);
  241. private:
  242. BOOL mIsChrome;
  243. BOOL mRequestsFront;
  244. BOOL mTabStop;
  245. BOOL mTentative;
  246. ETypeTransparency mTransparencyType;
  247. class DefaultTabGroupFirstSorter;
  248. };
  249. // Build time optimization, generate once in .cpp file
  250. #ifndef LLUICTRL_CPP
  251. extern template class LLUICtrl* LLView::getChild<class LLUICtrl>(
  252. const std::string& name, BOOL recurse) const;
  253. #endif
  254. #endif // LL_LLUICTRL_H