llfocusmgr.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /**
  2. * @file llfocusmgr.h
  3. * @brief LLFocusMgr base class
  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. // Singleton that manages keyboard and mouse focus
  33. #ifndef LL_LLFOCUSMGR_H
  34. #define LL_LLFOCUSMGR_H
  35. #include "llstring.h"
  36. #include "llframetimer.h"
  37. #include "llhandle.h"
  38. #include "llcolor4.h"
  39. class LLFrameTimer;
  40. class LLMouseHandler;
  41. class LLUICtrl;
  42. class LLView;
  43. class LLFocusableElement
  44. {
  45. friend class LLFocusMgr; // Allow access to focus change handlers
  46. public:
  47. LLFocusableElement();
  48. virtual ~LLFocusableElement() = default;
  49. // MUST be defined and return false when the class derived from
  50. // LLFocusableElement is not also derived from LLUICtrl, or true when it
  51. // is.
  52. // Note: this was renamed from isUICtrl(), which was ambiguous: we also got
  53. // isCtrl() in the Cool VL Viewer to distinguish LLView from LLUICtrl. HB
  54. virtual bool isFocusableCtrl() const = 0;
  55. LL_INLINE virtual void setFocus(bool b) {}
  56. virtual bool hasFocus() const;
  57. void setFocusLostCallback(void (*cb)(LLFocusableElement*, void*),
  58. void* user_data = NULL);
  59. void setFocusReceivedCallback(void (*cb)(LLFocusableElement*, void*),
  60. void* user_data = NULL);
  61. void setFocusChangedCallback(void (*cb)(LLFocusableElement*, void*),
  62. void* user_data = NULL);
  63. // These were brought up the hierarchy from LLView so that we do not have
  64. // to use dynamic casts when dealing with keyboard focus.
  65. LL_INLINE virtual bool handleKey(KEY key, MASK mask, bool from_parent)
  66. {
  67. return false;
  68. }
  69. LL_INLINE virtual bool handleKeyUp(KEY key, MASK mask, bool from_parent)
  70. {
  71. return false;
  72. }
  73. LL_INLINE virtual bool handleUnicodeChar(llwchar uchar, bool from_parent)
  74. {
  75. return false;
  76. }
  77. // If these methods return true, this LLFocusableElement wants to receive
  78. // KEYUP and KEYDOWN messages. Default implementation returns false.
  79. LL_INLINE virtual bool wantsKeyUpKeyDown() const { return false; }
  80. LL_INLINE virtual bool wantsReturnKey() const { return false; }
  81. protected:
  82. virtual void onFocusReceived();
  83. virtual void onFocusLost();
  84. protected:
  85. void (*mFocusLostCallback)(LLFocusableElement* callerp, void* userdata);
  86. void (*mFocusReceivedCallback)(LLFocusableElement* ctrlp, void* userdata);
  87. void (*mFocusChangedCallback)(LLFocusableElement* ctrlp, void* userdata);
  88. void* mFocusCallbackUserData;
  89. };
  90. class LLFocusMgr
  91. {
  92. public:
  93. LLFocusMgr();
  94. ~LLFocusMgr();
  95. LL_INLINE void setFocusColor(LLColor4 color) { mFocusColor = color; }
  96. // Mouse Captor
  97. // new_captor = NULL to release the mouse:
  98. void setMouseCapture(LLMouseHandler* new_captorp);
  99. LL_INLINE LLMouseHandler* getMouseCapture() const { return mMouseCaptor; }
  100. void removeMouseCaptureWithoutCallback(const LLMouseHandler* captorp);
  101. bool childHasMouseCapture(const LLView* parentp) const;
  102. // Keyboard Focus
  103. // new_focus = NULL to release the focus:
  104. void setKeyboardFocus(LLFocusableElement* new_focusp, bool lock = false,
  105. bool keystrokes_only = false);
  106. LL_INLINE LLFocusableElement* getKeyboardFocus() const
  107. {
  108. return mKeyboardFocus;
  109. }
  110. LL_INLINE LLFocusableElement* getLastKeyboardFocus() const
  111. {
  112. return mLastKeyboardFocus;
  113. }
  114. LLUICtrl* getKeyboardFocusUICtrl();
  115. LLUICtrl* getLastKeyboardFocusUICtrl();
  116. bool childHasKeyboardFocus(const LLView* parentp) const;
  117. void removeKeyboardFocusWithoutCallback(const LLFocusableElement* focusp);
  118. LL_INLINE bool getKeystrokesOnly() { return mKeystrokesOnly; }
  119. LL_INLINE void setKeystrokesOnly(bool b) { mKeystrokesOnly = b; }
  120. LL_INLINE F32 getFocusTime() const { return mFocusTimer.getElapsedTimeF32(); }
  121. F32 getFocusFlashAmt() const;
  122. LL_INLINE S32 getFocusFlashWidth() const { return ll_roundp(lerp(1.f, 3.f, getFocusFlashAmt())); }
  123. LLColor4 getFocusColor() const;
  124. void triggerFocusFlash();
  125. LL_INLINE bool getAppHasFocus() const { return mAppHasFocus; }
  126. void setAppHasFocus(bool focus);
  127. LLUICtrl* getLastFocusForGroup(LLView* viewp) const;
  128. void clearLastFocusForGroup(LLView* viewp);
  129. // If setKeyboardFocus(NULL) is called, and there is a non-NULL default
  130. // keyboard focus view, focus goes there. JC
  131. LL_INLINE void setDefaultKeyboardFocus(LLFocusableElement* default_focus)
  132. {
  133. mDefaultKeyboardFocus = default_focus;
  134. }
  135. LL_INLINE LLFocusableElement* getDefaultKeyboardFocus() const
  136. {
  137. return mDefaultKeyboardFocus;
  138. }
  139. // Top View
  140. void setTopCtrl(LLUICtrl* ctrlp);
  141. LL_INLINE LLUICtrl* getTopCtrl() const { return mTopCtrl; }
  142. void removeTopCtrlWithoutCallback(const LLUICtrl* ctrlp);
  143. bool childIsTopCtrl(const LLView* parentp) const;
  144. // All Three
  145. void releaseFocusIfNeeded(const LLView* top_viewp);
  146. void lockFocus();
  147. LL_INLINE void unlockFocus() { mLockedView = NULL; }
  148. LL_INLINE bool focusLocked() const { return mLockedView != NULL; }
  149. private:
  150. LLColor4 mFocusColor;
  151. LLUICtrl* mLockedView;
  152. // Top View
  153. LLUICtrl* mTopCtrl;
  154. LLFrameTimer mFocusTimer;
  155. F32 mFocusWeight;
  156. // Mouse events are premptively routed to this object
  157. LLMouseHandler* mMouseCaptor;
  158. // Keyboard events are preemptively routed to this object:
  159. LLFocusableElement* mKeyboardFocus;
  160. LLFocusableElement* mLastKeyboardFocus; // who last had focus
  161. LLFocusableElement* mDefaultKeyboardFocus;
  162. typedef std::map<LLHandle<LLView>, LLHandle<LLView> > focus_history_map_t;
  163. focus_history_map_t mFocusHistory;
  164. bool mKeystrokesOnly;
  165. bool mAppHasFocus;
  166. #if LL_DEBUG
  167. std::string mMouseCaptorName;
  168. std::string mKeyboardFocusName;
  169. std::string mTopCtrlName;
  170. #endif
  171. };
  172. extern LLFocusMgr gFocusMgr;
  173. #endif // LL_LLFOCUSMGR_H