lluictrl.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. /**
  2. * @file lluictrl.cpp
  3. * @author James Cook, Richard Nelson, Tom Yedwab
  4. * @brief Abstract base class for UI controls
  5. *
  6. * $LicenseInfo:firstyear=2001&license=viewergpl$
  7. *
  8. * Copyright (c) 2001-2009, Linden Research, Inc.
  9. *
  10. * Second Life Viewer Source Code
  11. * The source code in this file ("Source Code") is provided by Linden Lab
  12. * to you under the terms of the GNU General Public License, version 2.0
  13. * ("GPL"), unless you have obtained a separate licensing agreement
  14. * ("Other License"), formally executed by you and Linden Lab. Terms of
  15. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  16. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  17. *
  18. * There are special exceptions to the terms and conditions of the GPL as
  19. * it is applied to this Source Code. View the full text of the exception
  20. * in the file doc/FLOSS-exception.txt in this software distribution, or
  21. * online at
  22. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  23. *
  24. * By copying, modifying or distributing this software, you acknowledge
  25. * that you have read and understood your obligations described above,
  26. * and agree to abide by those obligations.
  27. *
  28. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  29. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  30. * COMPLETENESS OR PERFORMANCE.
  31. * $/LicenseInfo$
  32. */
  33. //#include "llviewerprecompiledheaders.h"
  34. #include "linden_common.h"
  35. #include "lluictrl.h"
  36. #include "llpanel.h"
  37. const std::string LL_UI_CRTL_TAG = "ui_ctrl";
  38. static LLRegisterWidget<LLUICtrl> r28(LL_UI_CRTL_TAG);
  39. //static
  40. LLUICtrl::lua_reg_cmd_fn_t LLUICtrl::sRegisterLuaCommand = NULL;
  41. // NOTE: the LLFocusableElement implementation has been moved to
  42. // llfocusmgr.cpp, to mirror the header where the class is defined.
  43. LLUICtrl::LLUICtrl()
  44. : mCommitCallback(NULL),
  45. mLostTopCallback(NULL),
  46. mValidateCallback(NULL),
  47. mCallbackUserData(NULL),
  48. mTentative(false),
  49. mTabStop(true),
  50. mIsChrome(false)
  51. {
  52. }
  53. LLUICtrl::LLUICtrl(const std::string& name, const LLRect& rect,
  54. bool mouse_opaque,
  55. void (*on_commit_callback)(LLUICtrl*, void*),
  56. void* callback_userdata, U32 reshape)
  57. : LLView(name, rect, mouse_opaque, reshape),
  58. mCommitCallback(on_commit_callback),
  59. mLostTopCallback(NULL),
  60. mValidateCallback(NULL),
  61. mCallbackUserData(callback_userdata),
  62. mTentative(false),
  63. mTabStop(true),
  64. mIsChrome(false)
  65. {
  66. }
  67. //virtual
  68. LLUICtrl::~LLUICtrl()
  69. {
  70. gFocusMgr.releaseFocusIfNeeded(this); // calls onCommit()
  71. if (gFocusMgr.getTopCtrl() == this)
  72. {
  73. llwarns << "UI Control holding top ctrl deleted: " << getName()
  74. << ". Top view removed." << llendl;
  75. gFocusMgr.removeTopCtrlWithoutCallback(this);
  76. }
  77. }
  78. //virtual
  79. const std::string& LLUICtrl::getTag() const
  80. {
  81. return LL_UI_CRTL_TAG;
  82. }
  83. //virtual
  84. void LLUICtrl::onCommit()
  85. {
  86. if (mCommitCallback)
  87. {
  88. (*mCommitCallback)(this, mCallbackUserData);
  89. }
  90. }
  91. //virtual
  92. bool LLUICtrl::hasFocus() const
  93. {
  94. return gFocusMgr.childHasKeyboardFocus(this);
  95. }
  96. //virtual
  97. void LLUICtrl::setFocus(bool b)
  98. {
  99. // Focus NEVER goes to UI ctrls that are disabled !
  100. if (!getEnabled())
  101. {
  102. return;
  103. }
  104. if (b)
  105. {
  106. if (!hasFocus())
  107. {
  108. gFocusMgr.setKeyboardFocus(this);
  109. }
  110. }
  111. else if (gFocusMgr.childHasKeyboardFocus(this))
  112. {
  113. gFocusMgr.setKeyboardFocus(NULL);
  114. }
  115. }
  116. //virtual
  117. void LLUICtrl::onFocusReceived()
  118. {
  119. // Trigger callbacks
  120. LLFocusableElement::onFocusReceived();
  121. // Find first view in hierarchy above new focus that is a LLUICtrl
  122. LLUICtrl* last_focusp = gFocusMgr.getLastKeyboardFocusUICtrl();
  123. LLView* viewp = getParent();
  124. while (viewp && !viewp->isCtrl())
  125. {
  126. viewp = viewp->getParent();
  127. }
  128. // And if it has newly gained focus, call onFocusReceived()
  129. LLUICtrl* ctrlp = (LLUICtrl*)viewp;
  130. if (ctrlp && (!last_focusp || !last_focusp->hasAncestor(ctrlp)))
  131. {
  132. ctrlp->onFocusReceived();
  133. }
  134. }
  135. //virtual
  136. void LLUICtrl::onFocusLost()
  137. {
  138. // Trigger callbacks
  139. LLFocusableElement::onFocusLost();
  140. // Find first view in hierarchy above old focus that is a LLUICtrl
  141. LLView* viewp = getParent();
  142. while (viewp && !viewp->isCtrl())
  143. {
  144. viewp = viewp->getParent();
  145. }
  146. // And if it has just lost focus, call onFocusReceived()
  147. LLUICtrl* ctrlp = (LLUICtrl*)viewp;
  148. // hasFocus() includes any descendants
  149. if (ctrlp && !ctrlp->hasFocus())
  150. {
  151. ctrlp->onFocusLost();
  152. }
  153. }
  154. //virtual
  155. void LLUICtrl::onLostTop()
  156. {
  157. if (mLostTopCallback)
  158. {
  159. mLostTopCallback(this, mCallbackUserData);
  160. }
  161. }
  162. bool LLUICtrl::getIsChrome() const
  163. {
  164. if (mIsChrome) return true;
  165. LLView* parent_ctrlp = getParent();
  166. while (parent_ctrlp)
  167. {
  168. if (parent_ctrlp->getIsChrome())
  169. {
  170. return true;
  171. }
  172. parent_ctrlp = parent_ctrlp->getParent();
  173. }
  174. return false;
  175. }
  176. // This comparator uses the crazy disambiguating logic of LLCompareByTabOrder,
  177. // but to switch up the order so that children that have the default tab group
  178. // come first and those that are prior to the default tab group come last.
  179. class CompareByDefaultTabGroup : public LLCompareByTabOrder
  180. {
  181. public:
  182. LL_INLINE CompareByDefaultTabGroup(LLView::child_tab_order_t order,
  183. S32 default_tab_group)
  184. : LLCompareByTabOrder(order),
  185. mDefaultTabGroup(default_tab_group)
  186. {
  187. }
  188. private:
  189. LL_INLINE bool compareTabOrders(const LLView::tab_order_t& a,
  190. const LLView::tab_order_t& b) const override
  191. {
  192. S32 ag = a.first; // tab group for a
  193. S32 bg = b.first; // tab group for b
  194. // These two ifs have the effect of moving elements prior to the
  195. // default tab group to the end of the list (still sorted relative to
  196. // each other, though)
  197. if (ag < mDefaultTabGroup && bg >= mDefaultTabGroup)
  198. {
  199. return false;
  200. }
  201. if (bg < mDefaultTabGroup && ag >= mDefaultTabGroup)
  202. {
  203. return true;
  204. }
  205. // Sort correctly if they're both on the same side of the default tab
  206. // group
  207. return a < b;
  208. }
  209. private:
  210. S32 mDefaultTabGroup;
  211. };
  212. // Sorter for plugging into the query.
  213. class LLUICtrl::DefaultTabGroupFirstSorter
  214. : public LLQuerySorter, public LLSingleton<DefaultTabGroupFirstSorter>
  215. {
  216. friend class LLSingleton<DefaultTabGroupFirstSorter>;
  217. public:
  218. LL_INLINE void operator()(LLView* parent,
  219. view_list_t& children) const override
  220. {
  221. children.sort(CompareByDefaultTabGroup(parent->getCtrlOrder(),
  222. parent->getDefaultTabGroup()));
  223. }
  224. };
  225. bool LLUICtrl::focusFirstItem(bool prefer_text_fields, bool focus_flash)
  226. {
  227. // Try to select default tab group child
  228. LLCtrlQuery query = getTabOrderQuery();
  229. // Sort things such that the default tab group is at the front
  230. query.setSorter(DefaultTabGroupFirstSorter::getInstance());
  231. child_list_t result = query(this);
  232. if (result.size() > 0)
  233. {
  234. LLUICtrl* ctrlp = (LLUICtrl*)result.front();
  235. if (!ctrlp->hasFocus())
  236. {
  237. ctrlp->setFocus(true);
  238. ctrlp->onTabInto();
  239. if (focus_flash)
  240. {
  241. gFocusMgr.triggerFocusFlash();
  242. }
  243. }
  244. return true;
  245. }
  246. // Search for text field first
  247. if (prefer_text_fields)
  248. {
  249. LLCtrlQuery query = getTabOrderQuery();
  250. query.addPreFilter(LLUICtrl::LLTextInputFilter::getInstance());
  251. child_list_t result = query(this);
  252. if (result.size() > 0)
  253. {
  254. LLUICtrl* ctrlp = (LLUICtrl*)result.front();
  255. if (!ctrlp->hasFocus())
  256. {
  257. ctrlp->setFocus(true);
  258. ctrlp->onTabInto();
  259. gFocusMgr.triggerFocusFlash();
  260. }
  261. return true;
  262. }
  263. }
  264. // No text field found, or we do not care about text fields
  265. result = getTabOrderQuery().run(this);
  266. if (result.size() > 0)
  267. {
  268. LLUICtrl* ctrlp = (LLUICtrl*)result.front();
  269. if (!ctrlp->hasFocus())
  270. {
  271. ctrlp->setFocus(true);
  272. ctrlp->onTabInto();
  273. gFocusMgr.triggerFocusFlash();
  274. }
  275. return true;
  276. }
  277. return false;
  278. }
  279. bool LLUICtrl::focusLastItem(bool prefer_text_fields)
  280. {
  281. // Search for text field first
  282. if (prefer_text_fields)
  283. {
  284. LLCtrlQuery query = getTabOrderQuery();
  285. query.addPreFilter(LLUICtrl::LLTextInputFilter::getInstance());
  286. child_list_t result = query(this);
  287. if (result.size() > 0)
  288. {
  289. LLUICtrl* ctrlp = (LLUICtrl*)result.back();
  290. if (!ctrlp->hasFocus())
  291. {
  292. ctrlp->setFocus(true);
  293. ctrlp->onTabInto();
  294. gFocusMgr.triggerFocusFlash();
  295. }
  296. return true;
  297. }
  298. }
  299. // No text field found, or we do not care about text fields
  300. child_list_t result = getTabOrderQuery().run(this);
  301. if (result.size() > 0)
  302. {
  303. LLUICtrl* ctrlp = (LLUICtrl*)result.back();
  304. if (!ctrlp->hasFocus())
  305. {
  306. ctrlp->setFocus(true);
  307. ctrlp->onTabInto();
  308. gFocusMgr.triggerFocusFlash();
  309. }
  310. return true;
  311. }
  312. return false;
  313. }
  314. bool LLUICtrl::focusNextItem(bool text_fields_only)
  315. {
  316. // This assumes that this method is called on the focus root.
  317. LLCtrlQuery query = getTabOrderQuery();
  318. if (text_fields_only || LLUI::sTabToTextFieldsOnly)
  319. {
  320. query.addPreFilter(LLUICtrl::LLTextInputFilter::getInstance());
  321. }
  322. child_list_t result = query(this);
  323. return focusNext(result);
  324. }
  325. bool LLUICtrl::focusPrevItem(bool text_fields_only)
  326. {
  327. // This assumes that this method is called on the focus root.
  328. LLCtrlQuery query = getTabOrderQuery();
  329. if (text_fields_only || LLUI::sTabToTextFieldsOnly)
  330. {
  331. query.addPreFilter(LLUICtrl::LLTextInputFilter::getInstance());
  332. }
  333. child_list_t result = query(this);
  334. return focusPrev(result);
  335. }
  336. LLUICtrl* LLUICtrl::findRootMostFocusRoot()
  337. {
  338. LLUICtrl* focus_rootp = NULL;
  339. LLUICtrl* next_viewp = this;
  340. while (next_viewp)
  341. {
  342. if (next_viewp->isFocusRoot())
  343. {
  344. focus_rootp = next_viewp;
  345. }
  346. next_viewp = next_viewp->getParentUICtrl();
  347. }
  348. return focus_rootp;
  349. }
  350. void LLUICtrl::initFromXML(LLXMLNodePtr nodep, LLView* parentp)
  351. {
  352. nodep->getAttributeBool("tab_stop", mTabStop);
  353. // See if we got a lua_command attribute, and if any, try and register it
  354. // on behalf of our floater (only successful when it is a Lua floater). HB
  355. if (sRegisterLuaCommand)
  356. {
  357. std::string lua_command;
  358. nodep->getAttributeString("lua_command", lua_command);
  359. if (!lua_command.empty())
  360. {
  361. sRegisterLuaCommand(this, parentp, lua_command);
  362. }
  363. }
  364. LLView::initFromXML(nodep, parentp);
  365. }
  366. LLXMLNodePtr LLUICtrl::getXML(bool save_children) const
  367. {
  368. LLXMLNodePtr nodep = LLView::getXML(save_children);
  369. nodep->createChild("tab_stop", true)->setBoolValue(mTabStop);
  370. return nodep;
  371. }
  372. //static
  373. LLView* LLUICtrl::fromXML(LLXMLNodePtr nodep, LLView* parentp,
  374. class LLUICtrlFactory* factoryp)
  375. {
  376. LLUICtrl* ctrlp = new LLUICtrl();
  377. ctrlp->initFromXML(nodep, parentp);
  378. return ctrlp;
  379. }
  380. // Skip over any parents that are not LLUICtrl's. Used in focus logic since
  381. // only LLUICtrl elements can have focus.
  382. LLUICtrl* LLUICtrl::getParentUICtrl() const
  383. {
  384. LLView* parentp = getParent();
  385. while (parentp)
  386. {
  387. if (parentp->isCtrl())
  388. {
  389. return (LLUICtrl*)parentp;
  390. }
  391. parentp = parentp->getParent();
  392. }
  393. return NULL;
  394. }