llfocusmgr.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. /**
  2. * @file llfocusmgr.cpp
  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. #include "linden_common.h"
  33. #include "lluictrl.h" // Also includes llfocusmgr.h
  34. #include "llcolor4.h"
  35. constexpr F32 FOCUS_FADE_TIME = 0.3f;
  36. LLFocusMgr gFocusMgr;
  37. ///////////////////////////////////////////////////////////////////////////////
  38. // LLFocusableElement class
  39. ///////////////////////////////////////////////////////////////////////////////
  40. LLFocusableElement::LLFocusableElement()
  41. : mFocusLostCallback(NULL),
  42. mFocusReceivedCallback(NULL),
  43. mFocusChangedCallback(NULL),
  44. mFocusCallbackUserData(NULL)
  45. {
  46. }
  47. void LLFocusableElement::setFocusLostCallback(void (*cb)(LLFocusableElement*,
  48. void*),
  49. void* user_data)
  50. {
  51. mFocusLostCallback = cb;
  52. mFocusCallbackUserData = user_data;
  53. }
  54. void LLFocusableElement::setFocusReceivedCallback(void (*cb)(LLFocusableElement*,
  55. void*),
  56. void* user_data)
  57. {
  58. mFocusReceivedCallback = cb;
  59. mFocusCallbackUserData = user_data;
  60. }
  61. void LLFocusableElement::setFocusChangedCallback(void (*cb)(LLFocusableElement*,
  62. void*),
  63. void* user_data)
  64. {
  65. mFocusChangedCallback = cb;
  66. mFocusCallbackUserData = user_data;
  67. }
  68. void LLFocusableElement::onFocusReceived()
  69. {
  70. if (mFocusReceivedCallback)
  71. {
  72. mFocusReceivedCallback(this, mFocusCallbackUserData);
  73. }
  74. if (mFocusChangedCallback)
  75. {
  76. mFocusChangedCallback(this, mFocusCallbackUserData);
  77. }
  78. }
  79. void LLFocusableElement::onFocusLost()
  80. {
  81. if (mFocusLostCallback)
  82. {
  83. mFocusLostCallback(this, mFocusCallbackUserData);
  84. }
  85. if (mFocusChangedCallback)
  86. {
  87. mFocusChangedCallback(this, mFocusCallbackUserData);
  88. }
  89. }
  90. bool LLFocusableElement::hasFocus() const
  91. {
  92. return gFocusMgr.getKeyboardFocus() == this;
  93. }
  94. ///////////////////////////////////////////////////////////////////////////////
  95. // LLFocusMgr class
  96. ///////////////////////////////////////////////////////////////////////////////
  97. LLFocusMgr::LLFocusMgr()
  98. : mLockedView(NULL),
  99. mMouseCaptor(NULL),
  100. mKeyboardFocus(NULL),
  101. mLastKeyboardFocus(NULL),
  102. mDefaultKeyboardFocus(NULL),
  103. mKeystrokesOnly(false),
  104. mTopCtrl(NULL),
  105. mFocusWeight(0.f),
  106. mFocusColor(LLColor4::white),
  107. #if LL_DEBUG
  108. mMouseCaptorName("none"),
  109. mKeyboardFocusName("none"),
  110. mTopCtrlName("none"),
  111. #endif
  112. // macOS does not seem to notify us that we have gotten focus, so default
  113. // to true
  114. mAppHasFocus(true)
  115. {
  116. }
  117. LLFocusMgr::~LLFocusMgr()
  118. {
  119. mFocusHistory.clear();
  120. }
  121. void LLFocusMgr::releaseFocusIfNeeded(const LLView* viewp)
  122. {
  123. if (childHasMouseCapture(viewp))
  124. {
  125. setMouseCapture(NULL);
  126. }
  127. if (childHasKeyboardFocus(viewp))
  128. {
  129. if (viewp == mLockedView)
  130. {
  131. mLockedView = NULL;
  132. setKeyboardFocus(NULL);
  133. }
  134. else
  135. {
  136. setKeyboardFocus(mLockedView, false, mKeystrokesOnly);
  137. }
  138. }
  139. if (childIsTopCtrl(viewp))
  140. {
  141. setTopCtrl(NULL);
  142. }
  143. }
  144. LLUICtrl* LLFocusMgr::getKeyboardFocusUICtrl()
  145. {
  146. if (mKeyboardFocus && mKeyboardFocus->isFocusableCtrl())
  147. {
  148. return (LLUICtrl*)mKeyboardFocus;
  149. }
  150. return NULL;
  151. }
  152. LLUICtrl* LLFocusMgr::getLastKeyboardFocusUICtrl()
  153. {
  154. if (mLastKeyboardFocus && mLastKeyboardFocus->isFocusableCtrl())
  155. {
  156. return (LLUICtrl*)mLastKeyboardFocus;
  157. }
  158. return NULL;
  159. }
  160. void LLFocusMgr::setKeyboardFocus(LLFocusableElement* new_focusp,
  161. bool lock, bool keystrokes_only)
  162. {
  163. // When locked, do not allow focus to go to anything that is not the locked
  164. // focus or one of its descendants
  165. if (mLockedView)
  166. {
  167. if (!new_focusp)
  168. {
  169. return;
  170. }
  171. if (new_focusp != mLockedView)
  172. {
  173. LLView* viewp = dynamic_cast<LLView*>(new_focusp);
  174. if (!viewp || !viewp->hasAncestor(mLockedView))
  175. {
  176. return;
  177. }
  178. }
  179. }
  180. mKeystrokesOnly = keystrokes_only;
  181. if (LLView::sDebugKeys)
  182. {
  183. llinfos << "mKeystrokesOnly = " << mKeystrokesOnly << llendl;
  184. }
  185. if (new_focusp != mKeyboardFocus)
  186. {
  187. mLastKeyboardFocus = mKeyboardFocus;
  188. mKeyboardFocus = new_focusp;
  189. if (mLastKeyboardFocus)
  190. {
  191. mLastKeyboardFocus->onFocusLost();
  192. }
  193. // Clear out any existing flash
  194. if (new_focusp)
  195. {
  196. mFocusWeight = 0.f;
  197. new_focusp->onFocusReceived();
  198. }
  199. mFocusTimer.reset();
  200. #if LL_DEBUG
  201. LLUICtrl* ctrlp = NULL;
  202. if (new_focusp && new_focusp->isFocusableCtrl())
  203. {
  204. ctrlp = (LLUICtrl*)new_focusp;
  205. }
  206. mKeyboardFocusName = ctrlp ? ctrlp->getName() : std::string("none");
  207. #endif
  208. // If we have got a default keyboard focus, and the caller is releasing
  209. // keyboard focus, move to the default.
  210. if (mDefaultKeyboardFocus != NULL && mKeyboardFocus == NULL)
  211. {
  212. mDefaultKeyboardFocus->setFocus(true);
  213. }
  214. LLView* focused_viewp = dynamic_cast<LLView*>(mKeyboardFocus);
  215. LLView* focus_subtreep = focused_viewp;
  216. LLView* viewp = focus_subtreep;
  217. // Find root-most focus root
  218. while (viewp)
  219. {
  220. if (viewp->isFocusRoot())
  221. {
  222. focus_subtreep = viewp;
  223. }
  224. viewp = viewp->getParent();
  225. }
  226. if (focus_subtreep)
  227. {
  228. mFocusHistory[focus_subtreep->getHandle()] =
  229. focused_viewp ? focused_viewp->getHandle()
  230. : LLHandle<LLView>();
  231. }
  232. }
  233. if (lock)
  234. {
  235. lockFocus();
  236. }
  237. }
  238. // Returns true is parent or any descedent of parent has keyboard focus.
  239. bool LLFocusMgr::childHasKeyboardFocus(const LLView* parentp) const
  240. {
  241. LLView* focus_viewp = dynamic_cast<LLView*>(mKeyboardFocus);
  242. while (focus_viewp)
  243. {
  244. if (focus_viewp == parentp)
  245. {
  246. return true;
  247. }
  248. focus_viewp = focus_viewp->getParent();
  249. }
  250. return false;
  251. }
  252. // Returns true is parent or any descedent of parent is the mouse captor.
  253. bool LLFocusMgr::childHasMouseCapture(const LLView* parentp) const
  254. {
  255. if (mMouseCaptor && mMouseCaptor->isView())
  256. {
  257. LLView* captor_viewp = (LLView*)mMouseCaptor;
  258. while (captor_viewp)
  259. {
  260. if (captor_viewp == parentp)
  261. {
  262. return true;
  263. }
  264. captor_viewp = captor_viewp->getParent();
  265. }
  266. }
  267. return false;
  268. }
  269. void LLFocusMgr::removeKeyboardFocusWithoutCallback(const LLFocusableElement* focusp)
  270. {
  271. // should be ok to unlock here, as you have to know the locked view
  272. // in order to unlock it
  273. if (mLockedView == focusp)
  274. {
  275. mLockedView = NULL;
  276. }
  277. if (mKeyboardFocus == focusp)
  278. {
  279. mKeyboardFocus = NULL;
  280. #if LL_DEBUG
  281. mKeyboardFocusName = "none";
  282. #endif
  283. }
  284. }
  285. void LLFocusMgr::setMouseCapture(LLMouseHandler* new_captorp)
  286. {
  287. #if 0
  288. if (mFocusLocked)
  289. {
  290. return;
  291. }
  292. #endif
  293. if (new_captorp != mMouseCaptor)
  294. {
  295. LLMouseHandler* old_captorp = mMouseCaptor;
  296. mMouseCaptor = new_captorp;
  297. if (old_captorp)
  298. {
  299. old_captorp->onMouseCaptureLost();
  300. }
  301. #if LL_DEBUG
  302. mMouseCaptorName = new_captorp ? new_captorp->getName()
  303. : std::string("none");
  304. #endif
  305. }
  306. }
  307. void LLFocusMgr::removeMouseCaptureWithoutCallback(const LLMouseHandler* captorp)
  308. {
  309. #if 0
  310. if (mFocusLocked)
  311. {
  312. return;
  313. }
  314. #endif
  315. if (mMouseCaptor == captorp)
  316. {
  317. mMouseCaptor = NULL;
  318. #if LL_DEBUG
  319. mMouseCaptorName = "none";
  320. #endif
  321. }
  322. }
  323. bool LLFocusMgr::childIsTopCtrl(const LLView* parentp) const
  324. {
  325. LLView* top_viewp = (LLView*)mTopCtrl;
  326. while (top_viewp)
  327. {
  328. if (top_viewp == parentp)
  329. {
  330. return true;
  331. }
  332. top_viewp = top_viewp->getParent();
  333. }
  334. return false;
  335. }
  336. // Set ctrlp = NULL to release top_view.
  337. void LLFocusMgr::setTopCtrl(LLUICtrl* ctrlp)
  338. {
  339. LLUICtrl* old_topp = mTopCtrl;
  340. if (ctrlp != old_topp)
  341. {
  342. mTopCtrl = ctrlp;
  343. #if LL_DEBUG
  344. mTopCtrlName = ctrlp ? ctrlp->getName() : std::string("none");
  345. #endif
  346. if (old_topp)
  347. {
  348. old_topp->onLostTop();
  349. }
  350. }
  351. }
  352. void LLFocusMgr::removeTopCtrlWithoutCallback(const LLUICtrl* ctrlp)
  353. {
  354. if (mTopCtrl == ctrlp)
  355. {
  356. mTopCtrl = NULL;
  357. #if LL_DEBUG
  358. mTopCtrlName = "none";
  359. #endif
  360. }
  361. }
  362. void LLFocusMgr::lockFocus()
  363. {
  364. if (mKeyboardFocus && mKeyboardFocus->isFocusableCtrl())
  365. {
  366. mLockedView = (LLUICtrl*)mKeyboardFocus;
  367. }
  368. else
  369. {
  370. mLockedView = NULL;
  371. }
  372. }
  373. F32 LLFocusMgr::getFocusFlashAmt() const
  374. {
  375. return clamp_rescale(getFocusTime(), 0.f, FOCUS_FADE_TIME, mFocusWeight,
  376. 0.f);
  377. }
  378. LLColor4 LLFocusMgr::getFocusColor() const
  379. {
  380. LLColor4 focus_color = lerp(mFocusColor, LLColor4::white,
  381. getFocusFlashAmt());
  382. // De-emphasize keyboard focus when app has lost focus (to avoid typing
  383. // into wrong window problem)
  384. if (!mAppHasFocus)
  385. {
  386. focus_color.mV[VALPHA] *= 0.4f;
  387. }
  388. return focus_color;
  389. }
  390. void LLFocusMgr::triggerFocusFlash()
  391. {
  392. mFocusTimer.reset();
  393. mFocusWeight = 1.f;
  394. }
  395. void LLFocusMgr::setAppHasFocus(bool focus)
  396. {
  397. if (!mAppHasFocus && focus)
  398. {
  399. triggerFocusFlash();
  400. }
  401. // Release focus from "top ctrl"s, which generally hides them
  402. if (!focus && mTopCtrl)
  403. {
  404. setTopCtrl(NULL);
  405. }
  406. mAppHasFocus = focus;
  407. }
  408. LLUICtrl* LLFocusMgr::getLastFocusForGroup(LLView* viewp) const
  409. {
  410. if (viewp)
  411. {
  412. focus_history_map_t::const_iterator it =
  413. mFocusHistory.find(viewp->getHandle());
  414. if (it != mFocusHistory.end())
  415. {
  416. // Found last focus for this subtree
  417. return (LLUICtrl*)it->second.get();
  418. }
  419. }
  420. return NULL;
  421. }
  422. void LLFocusMgr::clearLastFocusForGroup(LLView* viewp)
  423. {
  424. if (viewp)
  425. {
  426. mFocusHistory.erase(viewp->getHandle());
  427. }
  428. }