llview.cpp 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115
  1. /**
  2. * @file llview.cpp
  3. * @author James Cook
  4. * @brief Container for other views, anything that draws.
  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 "linden_common.h"
  34. #include <utility>
  35. #include "boost/tokenizer.hpp"
  36. #include "llview.h"
  37. #include "llcolor3.h"
  38. #include "llfloater.h"
  39. #include "llrender.h"
  40. #include "llstl.h"
  41. #include "lluictrl.h"
  42. #include "lluictrlfactory.h"
  43. #include "llwindow.h"
  44. using namespace LLOldEvents;
  45. // *HACK: this allows you to instantiate LLView from xml with "<view/>" which
  46. // we do not want
  47. static const std::string LL_VIEW_TAG = "view";
  48. static LLRegisterWidget<LLView> r32(LL_VIEW_TAG);
  49. bool LLView::sDebugRects = false;
  50. bool LLView::sDebugKeys = false;
  51. S32 LLView::sDepth = 0;
  52. bool LLView::sDebugMouseHandling = false;
  53. std::string LLView::sMouseHandlerMessage;
  54. bool LLView::sEditingUI = false;
  55. bool LLView::sForceReshape = false;
  56. LLView* LLView::sEditingUIView = NULL;
  57. S32 LLView::sLastLeftXML = S32_MIN;
  58. S32 LLView::sLastBottomXML = S32_MIN;
  59. constexpr S32 FLOATER_H_MARGIN = 15;
  60. constexpr S32 MIN_WIDGET_HEIGHT = 10;
  61. LLView::LLView()
  62. : mParentView(NULL),
  63. mReshapeFlags(FOLLOWS_NONE),
  64. mDefaultTabGroup(0),
  65. mEnabled(true),
  66. mMouseOpaque(true),
  67. mSoundFlags(MOUSE_UP), // Default to only make sound on mouse up
  68. mSaveToXML(true),
  69. mIsFocusRoot(false),
  70. mVisible(true),
  71. mLastVisible(true),
  72. mUseBoundingRect(false),
  73. mNextInsertionOrdinal(0),
  74. mHoverCursor(UI_CURSOR_ARROW),
  75. mChildListSize(0),
  76. mToolTipMsgPtr(NULL)
  77. {
  78. }
  79. LLView::LLView(const std::string& name, bool mouse_opaque)
  80. : mParentView(NULL),
  81. mName(name),
  82. mReshapeFlags(FOLLOWS_NONE),
  83. mDefaultTabGroup(0),
  84. mEnabled(true),
  85. mMouseOpaque(mouse_opaque),
  86. mSoundFlags(MOUSE_UP), // Default to only make sound on mouse up
  87. mSaveToXML(true),
  88. mIsFocusRoot(false),
  89. mVisible(true),
  90. mLastVisible(true),
  91. mUseBoundingRect(false),
  92. mNextInsertionOrdinal(0),
  93. mHoverCursor(UI_CURSOR_ARROW),
  94. mChildListSize(0),
  95. mToolTipMsgPtr(NULL)
  96. {
  97. }
  98. LLView::LLView(const std::string& name, const LLRect& rect, bool mouse_opaque,
  99. U8 reshape)
  100. : mParentView(NULL),
  101. mName(name),
  102. mRect(rect),
  103. mBoundingRect(rect),
  104. mReshapeFlags(reshape),
  105. mDefaultTabGroup(0),
  106. mEnabled(true),
  107. mMouseOpaque(mouse_opaque),
  108. mSoundFlags(MOUSE_UP), // Default to only make sound on mouse up
  109. mSaveToXML(true),
  110. mIsFocusRoot(false),
  111. mVisible(true),
  112. mLastVisible(true),
  113. mUseBoundingRect(false),
  114. mNextInsertionOrdinal(0),
  115. mHoverCursor(UI_CURSOR_ARROW),
  116. mChildListSize(0),
  117. mToolTipMsgPtr(NULL)
  118. {
  119. }
  120. LLView::~LLView()
  121. {
  122. LL_DEBUGS("View") << "Deleting view " << mName << " : " << (void*)this
  123. << LL_ENDL;
  124. if (hasMouseCapture())
  125. {
  126. llwarns << "View holding mouse capture deleted: " << getName()
  127. << ". Mouse capture removed." << llendl;
  128. gFocusMgr.removeMouseCaptureWithoutCallback(this);
  129. }
  130. deleteAllChildren();
  131. if (mParentView != NULL)
  132. {
  133. mParentView->removeChild(this);
  134. }
  135. dispatch_list_t::iterator it;
  136. for (it = mDispatchList.begin(); it != mDispatchList.end(); ++it)
  137. {
  138. it->second->clearDispatchers();
  139. }
  140. if (mToolTipMsgPtr)
  141. {
  142. delete mToolTipMsgPtr;
  143. mToolTipMsgPtr = NULL;
  144. }
  145. for (auto it = mControls.begin(), end = mControls.end();
  146. it != end; ++it)
  147. {
  148. delete it->second;
  149. }
  150. mControls.clear();
  151. for (auto it = mDummyWidgets.begin(), end = mDummyWidgets.end();
  152. it != end; ++it)
  153. {
  154. delete it->second;
  155. }
  156. mDummyWidgets.clear();
  157. }
  158. //virtual
  159. const std::string& LLView::getTag() const
  160. {
  161. return LL_VIEW_TAG;
  162. }
  163. //virtual
  164. void LLView::setToolTip(const std::string& msg)
  165. {
  166. if (!mToolTipMsgPtr)
  167. {
  168. mToolTipMsgPtr = new LLUIString(msg);
  169. }
  170. else
  171. {
  172. mToolTipMsgPtr->assign(msg);
  173. }
  174. }
  175. bool LLView::setToolTipArg(const std::string& key, const std::string& text)
  176. {
  177. if (!mToolTipMsgPtr)
  178. {
  179. mToolTipMsgPtr = new LLUIString("");
  180. }
  181. mToolTipMsgPtr->setArg(key, text);
  182. return true;
  183. }
  184. void LLView::setToolTipArgs(const LLStringUtil::format_map_t& args)
  185. {
  186. if (!mToolTipMsgPtr)
  187. {
  188. mToolTipMsgPtr = new LLUIString("");
  189. }
  190. mToolTipMsgPtr->setArgList(args);
  191. }
  192. const std::string& LLView::getToolTip() const
  193. {
  194. return mToolTipMsgPtr ? mToolTipMsgPtr->getString() : LLStringUtil::null;
  195. }
  196. //virtual
  197. void LLView::setRect(const LLRect& rect)
  198. {
  199. mRect = rect;
  200. updateBoundingRect();
  201. }
  202. void LLView::setUseBoundingRect(bool use_bounding_rect)
  203. {
  204. if (mUseBoundingRect != use_bounding_rect)
  205. {
  206. mUseBoundingRect = use_bounding_rect;
  207. updateBoundingRect();
  208. }
  209. }
  210. //virtual
  211. std::string LLView::getName() const
  212. {
  213. return mName.empty() ? "(no name)" : mName;
  214. }
  215. void LLView::sendChildToFront(LLView* child)
  216. {
  217. if (child && child->getParent() == this)
  218. {
  219. mChildList.remove(child);
  220. mChildList.push_front(child);
  221. // Paranoia: in case child was not in mChildList or was listed several
  222. // times in it...
  223. mChildListSize = mChildList.size();
  224. }
  225. }
  226. void LLView::sendChildToBack(LLView* child)
  227. {
  228. if (child && child->getParent() == this)
  229. {
  230. mChildList.remove(child);
  231. mChildList.push_back(child);
  232. // Paranoia: in case child was not in mChildList or was listed several
  233. // times in it...
  234. mChildListSize = mChildList.size();
  235. }
  236. }
  237. void LLView::moveChildToFrontOfTabGroup(LLUICtrl* child)
  238. {
  239. if (child && mCtrlOrder.find(child) != mCtrlOrder.end())
  240. {
  241. mCtrlOrder[child].second = -1 * mNextInsertionOrdinal++;
  242. }
  243. }
  244. void LLView::moveChildToBackOfTabGroup(LLUICtrl* child)
  245. {
  246. if (child && mCtrlOrder.find(child) != mCtrlOrder.end())
  247. {
  248. mCtrlOrder[child].second = mNextInsertionOrdinal++;
  249. }
  250. }
  251. void LLView::addChild(LLView* child, S32 tab_group)
  252. {
  253. if (!child)
  254. {
  255. llwarns << "Trying to add a NULL child" << llendl;
  256. return;
  257. }
  258. if (mParentView == child)
  259. {
  260. llerrs << "Adding view " << child->getName() << " as child of itself"
  261. << llendl;
  262. }
  263. // Remove from current parent
  264. if (child->mParentView)
  265. {
  266. child->mParentView->removeChild(child);
  267. }
  268. // Add to front of child list, as normal
  269. mChildList.push_front(child);
  270. // Paranoia: use this instead of mChildListSize++ in case child was already
  271. // parented to this view...
  272. mChildListSize = mChildList.size();
  273. // Add to control list if it is LLUICtrl
  274. if (child->isCtrl())
  275. {
  276. // Controls are stored in reverse order from render order
  277. addCtrlAtEnd((LLUICtrl*)child, tab_group);
  278. }
  279. child->mParentView = this;
  280. // If child is not visible it would not affect bounding rect; if current
  281. // view is not visible it will be recalculated on visibility change.
  282. if (getVisible() && child->getVisible())
  283. {
  284. updateBoundingRect();
  285. }
  286. }
  287. void LLView::addChildAtEnd(LLView* child, S32 tab_group)
  288. {
  289. if (!child)
  290. {
  291. llwarns << "Trying to add a NULL child at end" << llendl;
  292. return;
  293. }
  294. if (mParentView == child)
  295. {
  296. llerrs << "Adding view " << child->getName() << " as child of itself"
  297. << llendl;
  298. }
  299. // remove from current parent
  300. if (child->mParentView)
  301. {
  302. child->mParentView->removeChild(child);
  303. }
  304. // Add to back of child list
  305. mChildList.push_back(child);
  306. // Paranoia: use this instead of mChildListSize++ in case child was already
  307. // parented to this view...
  308. mChildListSize = mChildList.size();
  309. // Add to control list if it is LLUICtrl
  310. if (child->isCtrl())
  311. {
  312. // Controls are stored in reverse order from render order
  313. addCtrl((LLUICtrl*) child, tab_group);
  314. }
  315. child->mParentView = this;
  316. updateBoundingRect();
  317. }
  318. // Remove the specified child from the view, and set its parent to NULL.
  319. void LLView::removeChild(LLView* child, bool delete_it)
  320. {
  321. if (!child)
  322. {
  323. llwarns << "Trying to remove a NULL child" << llendl;
  324. return;
  325. }
  326. if (child->mParentView == this)
  327. {
  328. mChildList.remove(child);
  329. // Paranoia: use this instead of mChildListSize-- in case child was
  330. // not in mChildList or was listed several times in it...
  331. mChildListSize = mChildList.size();
  332. child->mParentView = NULL;
  333. if (child->isCtrl())
  334. {
  335. removeCtrl((LLUICtrl*)child);
  336. }
  337. if (delete_it)
  338. {
  339. delete child;
  340. }
  341. }
  342. else
  343. {
  344. llwarns << "Call done with non-child. Ignored." << llendl;
  345. }
  346. updateBoundingRect();
  347. }
  348. void LLView::addCtrlAtEnd(LLUICtrl* ctrl, S32 tab_group)
  349. {
  350. if (!ctrl)
  351. {
  352. llwarns << "Trying to add a NULL control at end" << llendl;
  353. return;
  354. }
  355. mCtrlOrder.insert(tab_order_pair_t(ctrl,
  356. tab_order_t(tab_group,
  357. mNextInsertionOrdinal++)));
  358. }
  359. void LLView::addCtrl(LLUICtrl* ctrl, S32 tab_group)
  360. {
  361. if (!ctrl)
  362. {
  363. llwarns << "Trying to add a NULL control" << llendl;
  364. return;
  365. }
  366. // add to front of list by using negative ordinal, which monotonically
  367. // increases
  368. mCtrlOrder.insert(tab_order_pair_t(ctrl,
  369. tab_order_t(tab_group,
  370. -1 * mNextInsertionOrdinal++)));
  371. }
  372. void LLView::removeCtrl(LLUICtrl* ctrl)
  373. {
  374. if (!ctrl)
  375. {
  376. llwarns << "Trying to remove a NULL control" << llendl;
  377. return;
  378. }
  379. child_tab_order_t::iterator found = mCtrlOrder.find(ctrl);
  380. if (found != mCtrlOrder.end())
  381. {
  382. mCtrlOrder.erase(found);
  383. }
  384. }
  385. LLView::ctrl_list_t LLView::getCtrlList() const
  386. {
  387. ctrl_list_t controls;
  388. for (child_list_const_iter_t iter = mChildList.begin(),
  389. end = mChildList.end();
  390. iter != end; ++iter)
  391. {
  392. LLView* childp = *iter;
  393. if (childp && childp->isCtrl())
  394. {
  395. controls.push_back((LLUICtrl*)childp);
  396. }
  397. }
  398. return controls;
  399. }
  400. LLView::ctrl_list_t LLView::getCtrlListSorted() const
  401. {
  402. ctrl_list_t controls = getCtrlList();
  403. std::sort(controls.begin(), controls.end(),
  404. LLCompareByTabOrder(mCtrlOrder));
  405. return controls;
  406. }
  407. // This method compares two LLViews by the tab order specified in the
  408. // comparator object. The code for this is a little convoluted because each
  409. // argument can have four states:
  410. // 1) not a control, 2) a control but not in the tab order,
  411. // 3) a control in the tab order, 4) null
  412. bool LLCompareByTabOrder::operator() (const LLView* const a,
  413. const LLView* const b) const
  414. {
  415. if (!a) return false;
  416. if (!b) return true;
  417. S32 a_score = a->isCtrl() ? -1 : 0;
  418. S32 b_score = b->isCtrl() ? -1 : 0;
  419. if (a_score == -1 && b_score == -1)
  420. {
  421. LLView::child_tab_order_const_iter_t a_found =
  422. mTabOrder.find((const LLUICtrl*)a);
  423. LLView::child_tab_order_const_iter_t b_found =
  424. mTabOrder.find((const LLUICtrl*)b);
  425. if (a_found != mTabOrder.end()) --a_score;
  426. if (b_found != mTabOrder.end()) --b_score;
  427. if (a_score == -2 && b_score == -2)
  428. {
  429. // Once we are in here, they are both in the tab order, and we can
  430. // compare based on that
  431. return compareTabOrders(a_found->second, b_found->second);
  432. }
  433. }
  434. return a_score == b_score ? a < b : a_score < b_score;
  435. }
  436. bool LLView::isInVisibleChain() const
  437. {
  438. const LLView* cur_view = this;
  439. while (cur_view)
  440. {
  441. if (!cur_view->getVisible())
  442. {
  443. return false;
  444. }
  445. cur_view = cur_view->getParent();
  446. }
  447. return true;
  448. }
  449. bool LLView::isInEnabledChain() const
  450. {
  451. const LLView* cur_view = this;
  452. while (cur_view)
  453. {
  454. if (!cur_view->getEnabled())
  455. {
  456. return false;
  457. }
  458. cur_view = cur_view->getParent();
  459. }
  460. return true;
  461. }
  462. bool LLView::focusNextRoot()
  463. {
  464. LLView::child_list_t result = LLView::getFocusRootsQuery().run(this);
  465. return LLView::focusNext(result);
  466. }
  467. bool LLView::focusPrevRoot()
  468. {
  469. LLView::child_list_t result = LLView::getFocusRootsQuery().run(this);
  470. return LLView::focusPrev(result);
  471. }
  472. //static
  473. bool LLView::focusNext(LLView::child_list_t& result)
  474. {
  475. LLView::child_list_iter_t result_begin = result.begin();
  476. LLView::child_list_iter_t result_end = result.end();
  477. LLView::child_list_iter_t focused = result_end;
  478. for (LLView::child_list_iter_t iter = result_begin; iter != result_end;
  479. ++iter)
  480. {
  481. if (gFocusMgr.childHasKeyboardFocus(*iter))
  482. {
  483. focused = iter;
  484. break;
  485. }
  486. }
  487. LLView::child_list_iter_t next = focused;
  488. next = (next == result_end) ? result_begin : ++next;
  489. while (next != focused)
  490. {
  491. // wrap around to beginning if necessary
  492. if (next == result_end)
  493. {
  494. next = result_begin;
  495. }
  496. if ((*next)->isCtrl())
  497. {
  498. LLUICtrl* ctrl = (LLUICtrl*)*next;
  499. ctrl->setFocus(true);
  500. ctrl->onTabInto();
  501. gFocusMgr.triggerFocusFlash();
  502. return true;
  503. }
  504. ++next;
  505. }
  506. return false;
  507. }
  508. //static
  509. bool LLView::focusPrev(LLView::child_list_t& result)
  510. {
  511. LLView::child_list_reverse_iter_t result_rbegin = result.rbegin();
  512. LLView::child_list_reverse_iter_t result_rend = result.rend();
  513. LLView::child_list_reverse_iter_t focused = result_rend;
  514. for (LLView::child_list_reverse_iter_t iter = result_rbegin;
  515. iter != result_rend; ++iter)
  516. {
  517. if (gFocusMgr.childHasKeyboardFocus(*iter))
  518. {
  519. focused = iter;
  520. break;
  521. }
  522. }
  523. LLView::child_list_reverse_iter_t next = focused;
  524. next = next == result_rend ? result_rbegin : ++next;
  525. while (next != focused)
  526. {
  527. // wrap around to beginning if necessary
  528. if (next == result_rend)
  529. {
  530. next = result_rbegin;
  531. }
  532. LLView* childp = *next++;
  533. if (childp && childp->isCtrl())
  534. {
  535. LLUICtrl* ctrl = (LLUICtrl*)childp;
  536. if (!ctrl->hasFocus())
  537. {
  538. ctrl->setFocus(true);
  539. ctrl->onTabInto();
  540. gFocusMgr.triggerFocusFlash();
  541. }
  542. return true;
  543. }
  544. }
  545. return false;
  546. }
  547. // Delete all children. Override this function if you need to perform any extra
  548. // clean up such as cached pointers to selected children, etc.
  549. void LLView::deleteAllChildren()
  550. {
  551. // Clear out the control ordering
  552. mCtrlOrder.clear();
  553. while (!mChildList.empty())
  554. {
  555. LLView* viewp = mChildList.front();
  556. viewp->mParentView = NULL;
  557. delete viewp;
  558. mChildList.pop_front();
  559. }
  560. mChildListSize = 0;
  561. }
  562. void LLView::setAllChildrenEnabled(bool b)
  563. {
  564. for (child_list_iter_t child_it = mChildList.begin(),
  565. end = mChildList.end();
  566. child_it != end; ++child_it)
  567. {
  568. LLView* viewp = *child_it;
  569. if (viewp)
  570. {
  571. viewp->setEnabled(b);
  572. }
  573. }
  574. }
  575. //virtual
  576. void LLView::setVisible(bool visible)
  577. {
  578. if (mVisible != visible)
  579. {
  580. if (!visible && gFocusMgr.getTopCtrl() == this)
  581. {
  582. gFocusMgr.setTopCtrl(NULL);
  583. }
  584. mVisible = visible;
  585. // notify children of visibility change if root, or part of visible
  586. // hierarchy
  587. if (!getParent() || getParent()->isInVisibleChain())
  588. {
  589. // tell all children of this view that the visibility may have
  590. // changed
  591. onVisibilityChange(visible);
  592. }
  593. updateBoundingRect();
  594. }
  595. }
  596. //virtual
  597. void LLView::onVisibilityChange(bool new_visibility)
  598. {
  599. for (child_list_iter_t child_it = mChildList.begin(),
  600. end = mChildList.end();
  601. child_it != end; ++child_it)
  602. {
  603. LLView* viewp = *child_it;
  604. // only views that are themselves visible will have their overall
  605. // visibility affected by their ancestors
  606. if (viewp && viewp->getVisible())
  607. {
  608. viewp->onVisibilityChange(new_visibility);
  609. }
  610. }
  611. }
  612. //virtual
  613. void LLView::translate(S32 x, S32 y)
  614. {
  615. mRect.translate(x, y);
  616. updateBoundingRect();
  617. }
  618. //virtual
  619. bool LLView::canSnapTo(LLView* other_view)
  620. {
  621. return other_view != this && other_view->getVisible();
  622. }
  623. bool LLView::handleHover(S32 x, S32 y, MASK mask)
  624. {
  625. bool handled = childrenHandleHover(x, y, mask) != NULL;
  626. if (!handled && blockMouseEvent(x, y))
  627. {
  628. gWindowp->setCursor(mHoverCursor);
  629. LL_DEBUGS("UserInput") << "hover handled by " << getName() << LL_ENDL;
  630. handled = true;
  631. }
  632. return handled;
  633. }
  634. std::string LLView::getShowNamesToolTip()
  635. {
  636. LLView* view = getParent();
  637. std::string name;
  638. std::string tool_tip = mName;
  639. while (view)
  640. {
  641. name = view->getName();
  642. if (name == "root") break;
  643. if (view->getToolTip().find(".xml") != std::string::npos)
  644. {
  645. tool_tip = view->getToolTip() + "/" + tool_tip;
  646. break;
  647. }
  648. else
  649. {
  650. tool_tip = view->getName() + "/" + tool_tip;
  651. }
  652. view = view->getParent();
  653. }
  654. return "/" + tool_tip;
  655. }
  656. bool LLView::handleToolTip(S32 x, S32 y, std::string& msg,
  657. LLRect* sticky_rect_screen)
  658. {
  659. bool handled = false;
  660. for (child_list_iter_t it = mChildList.begin(), end = mChildList.end();
  661. it != end; ++it)
  662. {
  663. LLView* viewp = *it;
  664. if (!viewp) continue; // Paranoia
  665. S32 local_x = x - viewp->mRect.mLeft;
  666. S32 local_y = y - viewp->mRect.mBottom;
  667. // Allow tooltips for disabled views so we can explain to the user why
  668. // the view is disabled. JC
  669. if (viewp->getVisible() && //viewp->getEnabled() &&
  670. viewp->pointInView(local_x, local_y) &&
  671. viewp->handleToolTip(local_x, local_y, msg, sticky_rect_screen))
  672. {
  673. // Child provided a tooltip, just return
  674. if (!msg.empty()) return true;
  675. // Otherwise, one of our children ate the event so do not traverse
  676. // siblings however, our child did not actually provide a tooltip
  677. // so we might want to.
  678. handled = true;
  679. break;
  680. }
  681. }
  682. // Get our own tooltip
  683. std::string tool_tip = mToolTipMsgPtr ? mToolTipMsgPtr->getString() : "";
  684. if (LLUI::sShowXUINames && tool_tip.find(".xml", 0) == std::string::npos)
  685. {
  686. tool_tip = getShowNamesToolTip();
  687. }
  688. bool show_names_text_box = LLUI::sShowXUINames &&
  689. dynamic_cast<LLTextBox*>(this) != NULL;
  690. // Do not allow any siblings to handle this event even if we do not have a
  691. // tooltip
  692. if (blockMouseEvent(x, y) || show_names_text_box)
  693. {
  694. if (!tool_tip.empty())
  695. {
  696. msg = tool_tip;
  697. // Convert rect local to screen coordinates
  698. localPointToScreen(0, 0,
  699. &(sticky_rect_screen->mLeft),
  700. &(sticky_rect_screen->mBottom));
  701. localPointToScreen(mRect.getWidth(), mRect.getHeight(),
  702. &(sticky_rect_screen->mRight),
  703. &(sticky_rect_screen->mTop));
  704. }
  705. handled = true;
  706. }
  707. return handled;
  708. }
  709. bool LLView::handleKey(KEY key, MASK mask, bool called_from_parent)
  710. {
  711. bool handled = false;
  712. if (getVisible() && getEnabled())
  713. {
  714. if (called_from_parent)
  715. {
  716. // Downward traversal
  717. handled = childrenHandleKey(key, mask) != NULL;
  718. }
  719. if (!handled)
  720. {
  721. handled = handleKeyHere(key, mask);
  722. if (handled && sDebugKeys)
  723. {
  724. llinfos << "Key handled by " << getName() << llendl;
  725. }
  726. }
  727. }
  728. if (!handled && !called_from_parent && mParentView)
  729. {
  730. // Upward traversal
  731. handled = mParentView->handleKey(key, mask, false);
  732. }
  733. return handled;
  734. }
  735. bool LLView::handleKeyUp(KEY key, MASK mask, bool called_from_parent)
  736. {
  737. bool handled = false;
  738. if (getVisible() && getEnabled())
  739. {
  740. if (called_from_parent)
  741. {
  742. // Downward traversal
  743. handled = childrenHandleKeyUp(key, mask) != NULL;
  744. }
  745. if (!handled)
  746. {
  747. handled = handleKeyUpHere(key, mask);
  748. if (handled && sDebugKeys)
  749. {
  750. llinfos << "Key handled by " << getName() << llendl;
  751. }
  752. }
  753. }
  754. if (!handled && !called_from_parent && mParentView)
  755. {
  756. // Upward traversal
  757. handled = mParentView->handleKeyUp(key, mask, false);
  758. }
  759. return handled;
  760. }
  761. bool LLView::handleUnicodeChar(llwchar uni_char, bool called_from_parent)
  762. {
  763. bool handled = false;
  764. if (getVisible() && getEnabled())
  765. {
  766. if (called_from_parent)
  767. {
  768. // Downward traversal
  769. handled = childrenHandleUnicodeChar(uni_char) != NULL;
  770. }
  771. if (!handled)
  772. {
  773. handled = handleUnicodeCharHere(uni_char);
  774. if (handled && sDebugKeys)
  775. {
  776. llinfos << "Unicode key handled by " << getName() << llendl;
  777. }
  778. }
  779. }
  780. if (!handled && !called_from_parent && mParentView)
  781. {
  782. // Upward traversal
  783. handled = mParentView->handleUnicodeChar(uni_char, false);
  784. }
  785. return handled;
  786. }
  787. bool LLView::handleDragAndDrop(S32 x, S32 y, MASK mask, bool drop,
  788. EDragAndDropType cargo_type, void* cargo_data,
  789. EAcceptance* accept,
  790. std::string& tooltip_msg)
  791. {
  792. // CRO this is an experiment to allow drag and drop into object inventory
  793. // based on the DragAndDrop tool's permissions rather than the parent
  794. bool handled = childrenHandleDragAndDrop(x, y, mask, drop,
  795. cargo_type, cargo_data, accept,
  796. tooltip_msg) != NULL;
  797. if (!handled && blockMouseEvent(x, y))
  798. {
  799. *accept = ACCEPT_NO;
  800. handled = true;
  801. LL_DEBUGS("UserInput") << "dragAndDrop handled by: " << getName()
  802. << " - drop = " << (drop ? "true" : "false")
  803. << " - accepted = false" << LL_ENDL;
  804. }
  805. return handled;
  806. }
  807. LLView* LLView::childrenHandleDragAndDrop(S32 x, S32 y, MASK mask,
  808. bool drop,
  809. EDragAndDropType cargo_type,
  810. void* cargo_data,
  811. EAcceptance* accept,
  812. std::string& tooltip_msg)
  813. {
  814. LLView* handled_view = NULL;
  815. // CRO this is an experiment to allow drag and drop into object inventory
  816. // based on the DragAndDrop tool's permissions rather than the parent
  817. if (getVisible()) //&& getEnabled())
  818. {
  819. for (child_list_iter_t child_it = mChildList.begin(),
  820. end = mChildList.end();
  821. child_it != end; ++child_it)
  822. {
  823. LLView* viewp = *child_it;
  824. if (!viewp) continue; // Paranoia
  825. S32 local_x = x - viewp->getRect().mLeft;
  826. S32 local_y = y - viewp->getRect().mBottom;
  827. if (viewp->getVisible() && viewp->getEnabled() &&
  828. viewp->pointInView(local_x, local_y) &&
  829. viewp->handleDragAndDrop(local_x, local_y, mask, drop,
  830. cargo_type,
  831. cargo_data,
  832. accept,
  833. tooltip_msg))
  834. {
  835. handled_view = viewp;
  836. break;
  837. }
  838. }
  839. }
  840. return handled_view;
  841. }
  842. //virtual
  843. bool LLView::hasMouseCapture()
  844. {
  845. return gFocusMgr.getMouseCapture() == this;
  846. }
  847. bool LLView::handleMouseUp(S32 x, S32 y, MASK mask)
  848. {
  849. bool handled = childrenHandleMouseUp(x, y, mask) != NULL;
  850. if (!handled && blockMouseEvent(x, y))
  851. {
  852. handled = true;
  853. }
  854. return handled;
  855. }
  856. LLFloater* LLView::getParentFloater() const
  857. {
  858. LLView* viewp = const_cast<LLView*>(this);
  859. while (viewp)
  860. {
  861. LLFloater* floaterp = viewp->asFloater();
  862. if (floaterp)
  863. {
  864. return floaterp;
  865. }
  866. viewp = viewp->getParent();
  867. }
  868. return NULL;
  869. }
  870. bool LLView::handleMouseDown(S32 x, S32 y, MASK mask)
  871. {
  872. LLView* handled_viewp = childrenHandleMouseDown(x, y, mask);
  873. bool handled = handled_viewp != NULL;
  874. if (!handled && blockMouseEvent(x, y))
  875. {
  876. handled = true;
  877. handled_viewp = this;
  878. }
  879. if (handled_viewp && sEditingUI)
  880. {
  881. const std::string& type = handled_viewp->getTag();
  882. // We want to be able to edit controls which have a specific (non
  883. // default) type and can be used in XUI files, so we exclude anonymous
  884. // LLView's and LLUICtrl's. HB
  885. if (type != LL_VIEW_TAG && type != LL_UI_CRTL_TAG)
  886. {
  887. // Also test if the parent floater can have its UI edited
  888. // (obviously, the Edit UI floater cannot, else we would end up
  889. // selecting its own UI each time we try and change a size or
  890. // position parameter for another edited floater's element via
  891. // the Edit UI floater buttons !). HB
  892. LLFloater* floaterp = getParentFloater();
  893. if (floaterp && floaterp->canEditUI())
  894. {
  895. sEditingUIView = handled_viewp;
  896. }
  897. }
  898. }
  899. return handled;
  900. }
  901. bool LLView::handleDoubleClick(S32 x, S32 y, MASK mask)
  902. {
  903. bool handled = childrenHandleDoubleClick(x, y, mask) != NULL;
  904. if (!handled && blockMouseEvent(x, y))
  905. {
  906. handleMouseDown(x, y, mask);
  907. return true;
  908. }
  909. return handled;
  910. }
  911. bool LLView::handleScrollWheel(S32 x, S32 y, S32 clicks)
  912. {
  913. if (getVisible() && getEnabled())
  914. {
  915. bool handled = childrenHandleScrollWheel(x, y, clicks) != NULL;
  916. return handled || blockMouseEvent(x, y);
  917. }
  918. return false;
  919. }
  920. bool LLView::handleRightMouseDown(S32 x, S32 y, MASK mask)
  921. {
  922. bool handled = childrenHandleRightMouseDown(x, y, mask) != NULL;
  923. return handled || blockMouseEvent(x, y);
  924. }
  925. bool LLView::handleRightMouseUp(S32 x, S32 y, MASK mask)
  926. {
  927. bool handled = childrenHandleRightMouseUp(x, y, mask) != NULL;
  928. return handled || blockMouseEvent(x, y);
  929. }
  930. bool LLView::handleMiddleMouseDown(S32 x, S32 y, MASK mask)
  931. {
  932. bool handled = childrenHandleMiddleMouseDown(x, y, mask) != NULL;
  933. return handled || blockMouseEvent(x, y);
  934. }
  935. bool LLView::handleMiddleMouseUp(S32 x, S32 y, MASK mask)
  936. {
  937. bool handled = childrenHandleMiddleMouseUp(x, y, mask) != NULL;
  938. return handled || blockMouseEvent(x, y);
  939. }
  940. LLView* LLView::childrenHandleScrollWheel(S32 x, S32 y, S32 clicks)
  941. {
  942. LLView* handled_view = NULL;
  943. if (getVisible() && getEnabled())
  944. {
  945. for (child_list_iter_t child_it = mChildList.begin(),
  946. end = mChildList.end();
  947. child_it != end; ++child_it)
  948. {
  949. LLView* viewp = *child_it;
  950. if (!viewp) continue; // Paranoia
  951. S32 local_x = x - viewp->getRect().mLeft;
  952. S32 local_y = y - viewp->getRect().mBottom;
  953. if (viewp->getVisible() && viewp->getEnabled() &&
  954. viewp->pointInView(local_x, local_y) &&
  955. viewp->handleScrollWheel(local_x, local_y, clicks))
  956. {
  957. if (sDebugMouseHandling)
  958. {
  959. sMouseHandlerMessage = "->" + viewp->mName +
  960. sMouseHandlerMessage;
  961. }
  962. handled_view = viewp;
  963. break;
  964. }
  965. }
  966. }
  967. return handled_view;
  968. }
  969. LLView* LLView::childrenHandleHover(S32 x, S32 y, MASK mask)
  970. {
  971. LLView* handled_view = NULL;
  972. if (getVisible() && getEnabled())
  973. {
  974. for (child_list_iter_t child_it = mChildList.begin(),
  975. end = mChildList.end();
  976. child_it != end; ++child_it)
  977. {
  978. LLView* viewp = *child_it;
  979. if (!viewp) continue; // Paranoia
  980. S32 local_x = x - viewp->getRect().mLeft;
  981. S32 local_y = y - viewp->getRect().mBottom;
  982. if (viewp->getVisible() && viewp->getEnabled() &&
  983. viewp->pointInView(local_x, local_y) &&
  984. viewp->handleHover(local_x, local_y, mask))
  985. {
  986. if (sDebugMouseHandling)
  987. {
  988. sMouseHandlerMessage = "->" + viewp->mName +
  989. sMouseHandlerMessage;
  990. }
  991. handled_view = viewp;
  992. break;
  993. }
  994. }
  995. }
  996. return handled_view;
  997. }
  998. // Called during downward traversal
  999. LLView* LLView::childrenHandleKey(KEY key, MASK mask)
  1000. {
  1001. LLView* handled_view = NULL;
  1002. if (getVisible() && getEnabled())
  1003. {
  1004. for (child_list_iter_t child_it = mChildList.begin(),
  1005. end = mChildList.end();
  1006. child_it != end; ++child_it)
  1007. {
  1008. LLView* viewp = *child_it;
  1009. if (viewp && viewp->handleKey(key, mask, true))
  1010. {
  1011. if (sDebugKeys)
  1012. {
  1013. llinfos << "Key handled by " << viewp->getName() << llendl;
  1014. }
  1015. handled_view = viewp;
  1016. break;
  1017. }
  1018. }
  1019. }
  1020. return handled_view;
  1021. }
  1022. // Called during downward traversal
  1023. LLView* LLView::childrenHandleKeyUp(KEY key, MASK mask)
  1024. {
  1025. LLView* handled_view = NULL;
  1026. if (getVisible() && getEnabled())
  1027. {
  1028. for (child_list_iter_t child_it = mChildList.begin(),
  1029. end = mChildList.end();
  1030. child_it != end; ++child_it)
  1031. {
  1032. LLView* viewp = *child_it;
  1033. if (viewp && viewp->handleKeyUp(key, mask, true))
  1034. {
  1035. if (sDebugKeys)
  1036. {
  1037. llinfos << "Key Up handled by " << viewp->getName()
  1038. << llendl;
  1039. }
  1040. handled_view = viewp;
  1041. break;
  1042. }
  1043. }
  1044. }
  1045. return handled_view;
  1046. }
  1047. // Called during downward traversal
  1048. LLView* LLView::childrenHandleUnicodeChar(llwchar uni_char)
  1049. {
  1050. LLView* handled_view = NULL;
  1051. if (getVisible() && getEnabled())
  1052. {
  1053. for (child_list_iter_t child_it = mChildList.begin(),
  1054. end = mChildList.end();
  1055. child_it != end; ++child_it)
  1056. {
  1057. LLView* viewp = *child_it;
  1058. if (viewp && viewp->handleUnicodeChar(uni_char, true))
  1059. {
  1060. if (sDebugKeys)
  1061. {
  1062. llinfos << "Unicode character handled by "
  1063. << viewp->getName() << llendl;
  1064. }
  1065. handled_view = viewp;
  1066. break;
  1067. }
  1068. }
  1069. }
  1070. return handled_view;
  1071. }
  1072. LLView* LLView::childrenHandleMouseDown(S32 x, S32 y, MASK mask)
  1073. {
  1074. LLView* handled_view = NULL;
  1075. for (child_list_iter_t child_it = mChildList.begin(),
  1076. end = mChildList.end();
  1077. child_it != end; ++child_it)
  1078. {
  1079. LLView* viewp = *child_it;
  1080. if (!viewp) continue; // Paranoia
  1081. S32 local_x = x - viewp->getRect().mLeft;
  1082. S32 local_y = y - viewp->getRect().mBottom;
  1083. if (viewp->getVisible() && viewp->getEnabled() &&
  1084. viewp->pointInView(local_x, local_y) &&
  1085. viewp->handleMouseDown(local_x, local_y, mask))
  1086. {
  1087. if (sDebugMouseHandling)
  1088. {
  1089. sMouseHandlerMessage = "->" + viewp->mName +
  1090. sMouseHandlerMessage;
  1091. }
  1092. handled_view = viewp;
  1093. break;
  1094. }
  1095. }
  1096. return handled_view;
  1097. }
  1098. LLView* LLView::childrenHandleRightMouseDown(S32 x, S32 y, MASK mask)
  1099. {
  1100. LLView* handled_view = NULL;
  1101. if (getVisible() && getEnabled())
  1102. {
  1103. for (child_list_iter_t child_it = mChildList.begin(),
  1104. end = mChildList.end();
  1105. child_it != end; ++child_it)
  1106. {
  1107. LLView* viewp = *child_it;
  1108. if (!viewp) continue; // Paranoia
  1109. S32 local_x = x - viewp->getRect().mLeft;
  1110. S32 local_y = y - viewp->getRect().mBottom;
  1111. if (viewp->getVisible() && viewp->getEnabled() &&
  1112. viewp->pointInView(local_x, local_y) &&
  1113. viewp->handleRightMouseDown(local_x, local_y, mask))
  1114. {
  1115. if (sDebugMouseHandling)
  1116. {
  1117. sMouseHandlerMessage = "->" + viewp->mName +
  1118. sMouseHandlerMessage;
  1119. }
  1120. handled_view = viewp;
  1121. break;
  1122. }
  1123. }
  1124. }
  1125. return handled_view;
  1126. }
  1127. LLView* LLView::childrenHandleMiddleMouseDown(S32 x, S32 y, MASK mask)
  1128. {
  1129. LLView* handled_view = NULL;
  1130. if (getVisible() && getEnabled())
  1131. {
  1132. for (child_list_iter_t child_it = mChildList.begin(),
  1133. end = mChildList.end();
  1134. child_it != end; ++child_it)
  1135. {
  1136. LLView* viewp = *child_it;
  1137. if (!viewp) continue; // Paranoia
  1138. S32 local_x = x - viewp->getRect().mLeft;
  1139. S32 local_y = y - viewp->getRect().mBottom;
  1140. if (viewp->getVisible() && viewp->getEnabled() &&
  1141. viewp->pointInView(local_x, local_y) &&
  1142. viewp->handleMiddleMouseDown(local_x, local_y, mask))
  1143. {
  1144. if (sDebugMouseHandling)
  1145. {
  1146. sMouseHandlerMessage = "->" + viewp->mName +
  1147. sMouseHandlerMessage;
  1148. }
  1149. handled_view = viewp;
  1150. break;
  1151. }
  1152. }
  1153. }
  1154. return handled_view;
  1155. }
  1156. LLView* LLView::childrenHandleDoubleClick(S32 x, S32 y, MASK mask)
  1157. {
  1158. LLView* handled_view = NULL;
  1159. if (getVisible() && getEnabled())
  1160. {
  1161. for (child_list_iter_t child_it = mChildList.begin(),
  1162. end = mChildList.end();
  1163. child_it != end; ++child_it)
  1164. {
  1165. LLView* viewp = *child_it;
  1166. if (!viewp) continue; // Paranoia
  1167. S32 local_x = x - viewp->getRect().mLeft;
  1168. S32 local_y = y - viewp->getRect().mBottom;
  1169. if (viewp->getVisible() && viewp->getEnabled() &&
  1170. viewp->pointInView(local_x, local_y) &&
  1171. viewp->handleDoubleClick(local_x, local_y, mask))
  1172. {
  1173. if (sDebugMouseHandling)
  1174. {
  1175. sMouseHandlerMessage = "->" + viewp->mName +
  1176. sMouseHandlerMessage;
  1177. }
  1178. handled_view = viewp;
  1179. break;
  1180. }
  1181. }
  1182. }
  1183. return handled_view;
  1184. }
  1185. LLView* LLView::childrenHandleMouseUp(S32 x, S32 y, MASK mask)
  1186. {
  1187. LLView* handled_view = NULL;
  1188. if (getVisible() && getEnabled())
  1189. {
  1190. for (child_list_iter_t child_it = mChildList.begin(),
  1191. end = mChildList.end();
  1192. child_it != end; ++child_it)
  1193. {
  1194. LLView* viewp = *child_it;
  1195. if (!viewp) continue; // Paranoia
  1196. S32 local_x = x - viewp->getRect().mLeft;
  1197. S32 local_y = y - viewp->getRect().mBottom;
  1198. if (viewp->getVisible() && viewp->getEnabled() &&
  1199. viewp->pointInView(local_x, local_y) &&
  1200. viewp->handleMouseUp(local_x, local_y, mask))
  1201. {
  1202. if (sDebugMouseHandling)
  1203. {
  1204. sMouseHandlerMessage = "->" + viewp->mName +
  1205. sMouseHandlerMessage;
  1206. }
  1207. handled_view = viewp;
  1208. break;
  1209. }
  1210. }
  1211. }
  1212. return handled_view;
  1213. }
  1214. LLView* LLView::childrenHandleRightMouseUp(S32 x, S32 y, MASK mask)
  1215. {
  1216. LLView* handled_view = NULL;
  1217. if (getVisible() && getEnabled())
  1218. {
  1219. for (child_list_iter_t child_it = mChildList.begin(),
  1220. end = mChildList.end();
  1221. child_it != end; ++child_it)
  1222. {
  1223. LLView* viewp = *child_it;
  1224. if (!viewp) continue; // Paranoia
  1225. S32 local_x = x - viewp->getRect().mLeft;
  1226. S32 local_y = y - viewp->getRect().mBottom;
  1227. if (viewp->getVisible() && viewp->getEnabled() &&
  1228. viewp->pointInView(local_x, local_y) &&
  1229. viewp->handleRightMouseUp(local_x, local_y, mask))
  1230. {
  1231. if (sDebugMouseHandling)
  1232. {
  1233. sMouseHandlerMessage = "->" + viewp->mName +
  1234. sMouseHandlerMessage;
  1235. }
  1236. handled_view = viewp;
  1237. break;
  1238. }
  1239. }
  1240. }
  1241. return handled_view;
  1242. }
  1243. LLView* LLView::childrenHandleMiddleMouseUp(S32 x, S32 y, MASK mask)
  1244. {
  1245. LLView* handled_view = NULL;
  1246. if (getVisible() && getEnabled())
  1247. {
  1248. for (child_list_iter_t child_it = mChildList.begin(),
  1249. end = mChildList.end();
  1250. child_it != end; ++child_it)
  1251. {
  1252. LLView* viewp = *child_it;
  1253. if (!viewp) continue; // Paranoia
  1254. S32 local_x = x - viewp->getRect().mLeft;
  1255. S32 local_y = y - viewp->getRect().mBottom;
  1256. if (viewp->getVisible() && viewp->getEnabled() &&
  1257. viewp->pointInView(local_x, local_y) &&
  1258. viewp->handleMiddleMouseUp(local_x, local_y, mask))
  1259. {
  1260. if (sDebugMouseHandling)
  1261. {
  1262. sMouseHandlerMessage = "->" + viewp->mName +
  1263. sMouseHandlerMessage;
  1264. }
  1265. handled_view = viewp;
  1266. break;
  1267. }
  1268. }
  1269. }
  1270. return handled_view;
  1271. }
  1272. void LLView::draw()
  1273. {
  1274. if (sDebugRects)
  1275. {
  1276. drawDebugRect();
  1277. // Check for bogus rectangle
  1278. if (getRect().mRight <= getRect().mLeft ||
  1279. getRect().mTop <= getRect().mBottom)
  1280. {
  1281. llwarns << "Bogus rectangle for " << getName() << " with " << mRect
  1282. << llendl;
  1283. }
  1284. }
  1285. // Draw focused control on top of everything else
  1286. LLUICtrl* focus_view = gFocusMgr.getKeyboardFocusUICtrl();
  1287. if (focus_view && focus_view->getParent() != this)
  1288. {
  1289. focus_view = NULL;
  1290. }
  1291. LLRect root_rect = getRootView()->getRect();
  1292. LLRect screen_rect;
  1293. ++sDepth;
  1294. for (child_list_reverse_iter_t child_iter = mChildList.rbegin();
  1295. child_iter != mChildList.rend(); ++child_iter)
  1296. {
  1297. LLView* viewp = *child_iter;
  1298. if (viewp && viewp->getVisible() &&
  1299. viewp != focus_view && viewp->getRect().isValid())
  1300. {
  1301. // Only draw views that are within the root view
  1302. localRectToScreen(viewp->getRect(), &screen_rect);
  1303. if (root_rect.overlaps(screen_rect))
  1304. {
  1305. gGL.matrixMode(LLRender::MM_MODELVIEW);
  1306. LLUI::pushMatrix();
  1307. {
  1308. LLUI::translate((F32)viewp->getRect().mLeft,
  1309. (F32)viewp->getRect().mBottom, 0.f);
  1310. viewp->draw();
  1311. }
  1312. LLUI::popMatrix();
  1313. }
  1314. }
  1315. }
  1316. --sDepth;
  1317. if (focus_view && focus_view->getVisible())
  1318. {
  1319. drawChild(focus_view);
  1320. }
  1321. // HACK
  1322. if (sEditingUI && this == sEditingUIView)
  1323. {
  1324. drawDebugRect();
  1325. }
  1326. }
  1327. // Draw a box for debugging.
  1328. void LLView::drawDebugRect()
  1329. {
  1330. LLUI::pushMatrix();
  1331. {
  1332. // Drawing solids requires texturing be disabled
  1333. gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);
  1334. if (mUseBoundingRect)
  1335. {
  1336. LLUI::translate((F32)mBoundingRect.mLeft - (F32)mRect.mLeft,
  1337. (F32)mBoundingRect.mBottom - (F32)mRect.mBottom,
  1338. 0.f);
  1339. }
  1340. LLRect debug_rect = mUseBoundingRect ? mBoundingRect : mRect;
  1341. // draw red rectangle for the border
  1342. LLColor4 border_color(0.f, 0.f, 0.f, 1.f);
  1343. if (sEditingUI)
  1344. {
  1345. border_color.mV[0] = 1.f;
  1346. }
  1347. else
  1348. {
  1349. border_color.mV[sDepth%3] = 1.f;
  1350. }
  1351. gGL.color4fv(border_color.mV);
  1352. gGL.begin(LLRender::LINES);
  1353. gGL.vertex2i(0, debug_rect.getHeight() - 1);
  1354. gGL.vertex2i(0, 0);
  1355. gGL.vertex2i(0, 0);
  1356. gGL.vertex2i(debug_rect.getWidth() - 1, 0);
  1357. gGL.vertex2i(debug_rect.getWidth() - 1, 0);
  1358. gGL.vertex2i(debug_rect.getWidth() - 1,
  1359. debug_rect.getHeight() - 1);
  1360. gGL.vertex2i(debug_rect.getWidth() - 1,
  1361. debug_rect.getHeight() - 1);
  1362. gGL.vertex2i(0, debug_rect.getHeight() - 1);
  1363. gGL.end();
  1364. // Draw the name if it is not a leaf node
  1365. if (mChildListSize && !sEditingUI)
  1366. {
  1367. static LLFontGL* fontp = LLFontGL::getFontSansSerifSmall();
  1368. S32 x, y;
  1369. gGL.color4fv(border_color.mV);
  1370. x = debug_rect.getWidth()/2;
  1371. y = debug_rect.getHeight()/2;
  1372. std::string debug_text = llformat("%s (%d x %d)",
  1373. getName().c_str(),
  1374. debug_rect.getWidth(),
  1375. debug_rect.getHeight());
  1376. fontp->renderUTF8(debug_text, 0, (F32)x, (F32)y, border_color,
  1377. LLFontGL::HCENTER, LLFontGL::BASELINE,
  1378. LLFontGL::NORMAL, S32_MAX, S32_MAX, NULL, false);
  1379. }
  1380. }
  1381. LLUI::popMatrix();
  1382. }
  1383. void LLView::drawChild(LLView* childp, S32 x_offset, S32 y_offset,
  1384. bool force_draw)
  1385. {
  1386. if (childp && childp->getParent() == this)
  1387. {
  1388. ++sDepth;
  1389. if (force_draw ||
  1390. (childp->getVisible() && childp->getRect().isValid()))
  1391. {
  1392. gGL.matrixMode(LLRender::MM_MODELVIEW);
  1393. LLUI::pushMatrix();
  1394. {
  1395. LLUI::translate((F32)childp->getRect().mLeft + x_offset,
  1396. (F32)childp->getRect().mBottom + y_offset,
  1397. 0.f);
  1398. childp->draw();
  1399. }
  1400. LLUI::popMatrix();
  1401. }
  1402. --sDepth;
  1403. }
  1404. }
  1405. void LLView::reshape(S32 width, S32 height, bool called_from_parent)
  1406. {
  1407. // Compute how much things changed and apply reshape logic to children
  1408. S32 delta_width = width - getRect().getWidth();
  1409. S32 delta_height = height - getRect().getHeight();
  1410. if (delta_width || delta_height || sForceReshape)
  1411. {
  1412. // Adjust our rectangle
  1413. mRect.mRight = getRect().mLeft + width;
  1414. mRect.mTop = getRect().mBottom + height;
  1415. // Move child views according to reshape flags
  1416. for (child_list_iter_t child_it = mChildList.begin(),
  1417. end = mChildList.end();
  1418. child_it != end; ++child_it)
  1419. {
  1420. LLView* viewp = *child_it;
  1421. if (!viewp) continue; // Paranoia
  1422. LLRect child_rect(viewp->mRect);
  1423. if (viewp->followsRight() && viewp->followsLeft())
  1424. {
  1425. child_rect.mRight += delta_width;
  1426. }
  1427. else if (viewp->followsRight())
  1428. {
  1429. child_rect.mLeft += delta_width;
  1430. child_rect.mRight += delta_width;
  1431. }
  1432. #if 0
  1433. else if (viewp->followsLeft())
  1434. {
  1435. // Left is 0, do not need to adjust coords
  1436. }
  1437. else
  1438. {
  1439. // *FIXME: what to do when we do not follow anyone ?
  1440. // For now, same as followsLeft
  1441. }
  1442. #endif
  1443. if (viewp->followsTop() && viewp->followsBottom())
  1444. {
  1445. child_rect.mTop += delta_height;
  1446. }
  1447. else if (viewp->followsTop())
  1448. {
  1449. child_rect.mTop += delta_height;
  1450. child_rect.mBottom += delta_height;
  1451. }
  1452. #if 0
  1453. else if (viewp->followsBottom())
  1454. {
  1455. // Bottom is 0, so do not need to adjust coords
  1456. }
  1457. else
  1458. {
  1459. // *FIXME what to do when we do not follow ?
  1460. // For now, same as bottom
  1461. }
  1462. #endif
  1463. S32 delta_x = child_rect.mLeft - viewp->getRect().mLeft;
  1464. S32 delta_y = child_rect.mBottom - viewp->getRect().mBottom;
  1465. viewp->translate(delta_x, delta_y);
  1466. viewp->reshape(child_rect.getWidth(), child_rect.getHeight());
  1467. }
  1468. }
  1469. if (!called_from_parent && mParentView)
  1470. {
  1471. mParentView->reshape(mParentView->getRect().getWidth(),
  1472. mParentView->getRect().getHeight(), false);
  1473. }
  1474. updateBoundingRect();
  1475. }
  1476. void LLView::updateBoundingRect()
  1477. {
  1478. if (isDead()) return;
  1479. if (mUseBoundingRect)
  1480. {
  1481. LLRect local_bounding_rect = LLRect::null;
  1482. child_list_const_iter_t child_it;
  1483. for (child_list_iter_t child_it = mChildList.begin(),
  1484. end = mChildList.end();
  1485. child_it != end; ++child_it)
  1486. {
  1487. LLView* childp = *child_it;
  1488. // Ignore invisible and "top" children when calculating bounding
  1489. // rect such as combobox popups
  1490. if (!childp || !childp->getVisible() ||
  1491. childp == gFocusMgr.getTopCtrl())
  1492. {
  1493. continue;
  1494. }
  1495. LLRect child_bounding_rect = childp->getBoundingRect();
  1496. if (local_bounding_rect.isEmpty())
  1497. {
  1498. // Start out with bounding rect equal to first visible child's
  1499. // bounding rect
  1500. local_bounding_rect = child_bounding_rect;
  1501. }
  1502. else
  1503. {
  1504. // Accumulate non-null children rectangles
  1505. if (!child_bounding_rect.isEmpty())
  1506. {
  1507. local_bounding_rect.unionWith(child_bounding_rect);
  1508. }
  1509. }
  1510. }
  1511. mBoundingRect = local_bounding_rect;
  1512. // Translate into parent-relative coordinates
  1513. mBoundingRect.translate(mRect.mLeft, mRect.mBottom);
  1514. }
  1515. else
  1516. {
  1517. mBoundingRect = mRect;
  1518. }
  1519. // Give parent view a chance to resize, in case we just moved, for example
  1520. if (getParent() && getParent()->mUseBoundingRect)
  1521. {
  1522. getParent()->updateBoundingRect();
  1523. }
  1524. }
  1525. LLRect LLView::getScreenRect() const
  1526. {
  1527. // *FIX: check for one-off error
  1528. LLRect screen_rect;
  1529. localPointToScreen(0, 0, &screen_rect.mLeft, &screen_rect.mBottom);
  1530. localPointToScreen(getRect().getWidth(), getRect().getHeight(),
  1531. &screen_rect.mRight, &screen_rect.mTop);
  1532. return screen_rect;
  1533. }
  1534. LLRect LLView::getLocalBoundingRect() const
  1535. {
  1536. LLRect local_bounding_rect = getBoundingRect();
  1537. local_bounding_rect.translate(-mRect.mLeft, -mRect.mBottom);
  1538. return local_bounding_rect;
  1539. }
  1540. LLRect LLView::getLocalRect() const
  1541. {
  1542. return LLRect(0, getRect().getHeight(), getRect().getWidth(), 0);
  1543. }
  1544. LLRect LLView::getLocalSnapRect() const
  1545. {
  1546. LLRect local_snap_rect = getSnapRect();
  1547. local_snap_rect.translate(-getRect().mLeft, -getRect().mBottom);
  1548. return local_snap_rect;
  1549. }
  1550. bool LLView::hasAncestor(const LLView* parentp) const
  1551. {
  1552. if (!parentp)
  1553. {
  1554. return false;
  1555. }
  1556. LLView* viewp = getParent();
  1557. while (viewp)
  1558. {
  1559. if (viewp == parentp)
  1560. {
  1561. return true;
  1562. }
  1563. viewp = viewp->getParent();
  1564. }
  1565. return false;
  1566. }
  1567. bool LLView::childHasKeyboardFocus(const char* childname) const
  1568. {
  1569. LLView* childp = getChildView(childname, true, false);
  1570. return childp && gFocusMgr.childHasKeyboardFocus(childp);
  1571. }
  1572. LLView* LLView::getChildView(const char* name, bool recurse,
  1573. bool create_if_missing) const
  1574. {
  1575. #if 0 // Should we forbid empty names ?
  1576. if (name.empty()) return NULL;
  1577. #endif
  1578. LL_DEBUGS("GetChildCalls") << "Requested child name: " << name << LL_ENDL;
  1579. // Look for direct children *first*
  1580. for (child_list_const_iter_t child_it = mChildList.begin(),
  1581. end = mChildList.end();
  1582. child_it != end; ++child_it)
  1583. {
  1584. LLView* childp = *child_it;
  1585. if (childp && strcmp(childp->getName().c_str(), name) == 0)
  1586. {
  1587. return childp;
  1588. }
  1589. }
  1590. if (recurse)
  1591. {
  1592. // Look inside each child as well.
  1593. for (child_list_const_iter_t child_it = mChildList.begin(),
  1594. end = mChildList.end();
  1595. child_it != end; ++child_it)
  1596. {
  1597. LLView* childp = *child_it;
  1598. if (!childp) continue; // Paranoia
  1599. LLView* viewp = childp->getChildView(name, recurse, false);
  1600. if (viewp)
  1601. {
  1602. return viewp;
  1603. }
  1604. }
  1605. }
  1606. if (create_if_missing)
  1607. {
  1608. return createDummyWidget<LLView>(name);
  1609. }
  1610. return NULL;
  1611. }
  1612. bool LLView::parentPointInView(S32 x, S32 y, EHitTestType type) const
  1613. {
  1614. return (mUseBoundingRect && type == HIT_TEST_USE_BOUNDING_RECT
  1615. ? mBoundingRect.pointInRect(x, y)
  1616. : mRect.pointInRect(x, y));
  1617. }
  1618. bool LLView::pointInView(S32 x, S32 y, EHitTestType type) const
  1619. {
  1620. return (mUseBoundingRect && type == HIT_TEST_USE_BOUNDING_RECT
  1621. ? mBoundingRect.pointInRect(x + mRect.mLeft, y + mRect.mBottom)
  1622. : mRect.localPointInRect(x, y));
  1623. }
  1624. bool LLView::blockMouseEvent(S32 x, S32 y) const
  1625. {
  1626. return mMouseOpaque && pointInView(x, y, HIT_TEST_IGNORE_BOUNDING_RECT);
  1627. }
  1628. //virtual
  1629. void LLView::screenPointToLocal(S32 screen_x, S32 screen_y, S32* local_x,
  1630. S32* local_y) const
  1631. {
  1632. *local_x = screen_x;
  1633. *local_y = screen_y;
  1634. const LLView* cur = this;
  1635. LLRect cur_rect;
  1636. do
  1637. {
  1638. cur_rect = cur->getRect();
  1639. *local_x -= cur_rect.mLeft;
  1640. *local_y -= cur_rect.mBottom;
  1641. cur = cur->mParentView;
  1642. }
  1643. while (cur);
  1644. }
  1645. //virtual
  1646. void LLView::localPointToScreen(S32 local_x, S32 local_y, S32* screen_x,
  1647. S32* screen_y) const
  1648. {
  1649. *screen_x = local_x;
  1650. *screen_y = local_y;
  1651. const LLView* cur = this;
  1652. LLRect cur_rect;
  1653. do
  1654. {
  1655. cur_rect = cur->getRect();
  1656. *screen_x += cur_rect.mLeft;
  1657. *screen_y += cur_rect.mBottom;
  1658. cur = cur->mParentView;
  1659. }
  1660. while (cur);
  1661. }
  1662. void LLView::screenRectToLocal(const LLRect& screen, LLRect* local) const
  1663. {
  1664. *local = screen;
  1665. local->translate(-getRect().mLeft, -getRect().mBottom);
  1666. const LLView* cur = this;
  1667. while (cur->mParentView)
  1668. {
  1669. cur = cur->mParentView;
  1670. local->translate(-cur->getRect().mLeft, -cur->getRect().mBottom);
  1671. }
  1672. }
  1673. void LLView::localRectToScreen(const LLRect& local, LLRect* screen) const
  1674. {
  1675. *screen = local;
  1676. screen->translate(getRect().mLeft, getRect().mBottom);
  1677. const LLView* cur = this;
  1678. while (cur->mParentView)
  1679. {
  1680. cur = cur->mParentView;
  1681. screen->translate(cur->getRect().mLeft, cur->getRect().mBottom);
  1682. }
  1683. }
  1684. LLView* LLView::getRootView()
  1685. {
  1686. static LLView* sRootView = NULL;
  1687. if (!sRootView)
  1688. {
  1689. sRootView = this;
  1690. while (sRootView->mParentView)
  1691. {
  1692. sRootView = sRootView->mParentView;
  1693. }
  1694. }
  1695. return sRootView;
  1696. }
  1697. LLView* LLView::findPrevSibling(LLView* child)
  1698. {
  1699. child_list_t::iterator prev_it = std::find(mChildList.begin(),
  1700. mChildList.end(), child);
  1701. if (prev_it != mChildList.end() && prev_it != mChildList.begin())
  1702. {
  1703. return *(--prev_it);
  1704. }
  1705. return NULL;
  1706. }
  1707. LLView* LLView::findNextSibling(LLView* child)
  1708. {
  1709. child_list_t::iterator next_it = std::find(mChildList.begin(),
  1710. mChildList.end(), child);
  1711. if (next_it != mChildList.end())
  1712. {
  1713. ++next_it;
  1714. }
  1715. return next_it != mChildList.end() ? *next_it : NULL;
  1716. }
  1717. bool LLView::deleteViewByHandle(LLHandle<LLView> handle)
  1718. {
  1719. LLView* viewp = handle.get();
  1720. if (viewp)
  1721. {
  1722. delete viewp;
  1723. return true;
  1724. }
  1725. return false;
  1726. }
  1727. // Moves the view so that it is entirely inside of constraint.
  1728. // If the view will not fit because it is too big, aligns with the top and
  1729. // left.
  1730. bool LLView::translateIntoRect(const LLRect& constraint,
  1731. bool allow_partial_outside)
  1732. {
  1733. S32 delta_x = 0;
  1734. S32 delta_y = 0;
  1735. if (allow_partial_outside)
  1736. {
  1737. constexpr S32 KEEP_ONSCREEN_PIXELS = 16;
  1738. if (getRect().mRight - KEEP_ONSCREEN_PIXELS < constraint.mLeft)
  1739. {
  1740. delta_x = constraint.mLeft - (getRect().mRight - KEEP_ONSCREEN_PIXELS);
  1741. }
  1742. else if (getRect().mLeft + KEEP_ONSCREEN_PIXELS > constraint.mRight)
  1743. {
  1744. delta_x = constraint.mRight - (getRect().mLeft + KEEP_ONSCREEN_PIXELS);
  1745. }
  1746. if (getRect().mTop > constraint.mTop)
  1747. {
  1748. delta_y = constraint.mTop - getRect().mTop;
  1749. }
  1750. else if (getRect().mTop - KEEP_ONSCREEN_PIXELS < constraint.mBottom)
  1751. {
  1752. delta_y = constraint.mBottom - (getRect().mTop - KEEP_ONSCREEN_PIXELS);
  1753. }
  1754. }
  1755. else
  1756. {
  1757. if (getRect().mLeft < constraint.mLeft)
  1758. {
  1759. delta_x = constraint.mLeft - getRect().mLeft;
  1760. }
  1761. else if (getRect().mRight > constraint.mRight)
  1762. {
  1763. delta_x = constraint.mRight - getRect().mRight;
  1764. // compensate for left edge possible going off screen
  1765. delta_x += llmax(0, getRect().getWidth() - constraint.getWidth());
  1766. }
  1767. if (getRect().mTop > constraint.mTop)
  1768. {
  1769. delta_y = constraint.mTop - getRect().mTop;
  1770. }
  1771. else if (getRect().mBottom < constraint.mBottom)
  1772. {
  1773. delta_y = constraint.mBottom - getRect().mBottom;
  1774. // compensate for top edge possible going off screen
  1775. delta_y -= llmax(0, getRect().getHeight() - constraint.getHeight());
  1776. }
  1777. }
  1778. if (delta_x != 0 || delta_y != 0)
  1779. {
  1780. translate(delta_x, delta_y);
  1781. return true;
  1782. }
  1783. return false;
  1784. }
  1785. void LLView::centerWithin(const LLRect& bounds)
  1786. {
  1787. S32 left = bounds.mLeft + (bounds.getWidth() - getRect().getWidth()) / 2;
  1788. S32 bottom = bounds.mBottom + (bounds.getHeight() - getRect().getHeight()) / 2;
  1789. translate(left - getRect().mLeft, bottom - getRect().mBottom);
  1790. }
  1791. bool LLView::localPointToOtherView(S32 x, S32 y, S32* other_x, S32* other_y,
  1792. LLView* other_view) const
  1793. {
  1794. const LLView* cur_view = this;
  1795. const LLView* root_view = NULL;
  1796. while (cur_view)
  1797. {
  1798. if (cur_view == other_view)
  1799. {
  1800. *other_x = x;
  1801. *other_y = y;
  1802. return true;
  1803. }
  1804. x += cur_view->getRect().mLeft;
  1805. y += cur_view->getRect().mBottom;
  1806. cur_view = cur_view->getParent();
  1807. root_view = cur_view;
  1808. }
  1809. // assuming common root between two views, chase other_view's parents up to
  1810. // root
  1811. cur_view = other_view;
  1812. while (cur_view)
  1813. {
  1814. x -= cur_view->getRect().mLeft;
  1815. y -= cur_view->getRect().mBottom;
  1816. cur_view = cur_view->getParent();
  1817. if (cur_view == root_view)
  1818. {
  1819. *other_x = x;
  1820. *other_y = y;
  1821. return true;
  1822. }
  1823. }
  1824. *other_x = x;
  1825. *other_y = y;
  1826. return false;
  1827. }
  1828. bool LLView::localRectToOtherView(const LLRect& local, LLRect* other,
  1829. LLView* other_view) const
  1830. {
  1831. LLRect cur_rect = local;
  1832. const LLView* cur_view = this;
  1833. const LLView* root_view = NULL;
  1834. while (cur_view)
  1835. {
  1836. if (cur_view == other_view)
  1837. {
  1838. *other = cur_rect;
  1839. return true;
  1840. }
  1841. cur_rect.translate(cur_view->getRect().mLeft,
  1842. cur_view->getRect().mBottom);
  1843. cur_view = cur_view->getParent();
  1844. root_view = cur_view;
  1845. }
  1846. // assuming common root between two views, chase other_view's parents up to
  1847. // root
  1848. cur_view = other_view;
  1849. while (cur_view)
  1850. {
  1851. cur_rect.translate(-cur_view->getRect().mLeft,
  1852. -cur_view->getRect().mBottom);
  1853. cur_view = cur_view->getParent();
  1854. if (cur_view == root_view)
  1855. {
  1856. *other = cur_rect;
  1857. return true;
  1858. }
  1859. }
  1860. *other = cur_rect;
  1861. return false;
  1862. }
  1863. //virtual
  1864. LLXMLNodePtr LLView::getXML(bool save_children) const
  1865. {
  1866. // If called from a derived class, the derived class will override the node
  1867. // name
  1868. LLXMLNodePtr node = new LLXMLNode("view", false);
  1869. node->createChild("name", true)->setStringValue(getName());
  1870. node->createChild("width", true)->setIntValue(getRect().getWidth());
  1871. node->createChild("height", true)->setIntValue(getRect().getHeight());
  1872. LLView* parent = getParent();
  1873. S32 left = getRect().mLeft;
  1874. S32 bottom = getRect().mBottom;
  1875. if (parent) bottom -= parent->getRect().getHeight();
  1876. node->createChild("left", true)->setIntValue(left);
  1877. node->createChild("bottom", true)->setIntValue(bottom);
  1878. U8 follows_flags = getFollows();
  1879. if (follows_flags)
  1880. {
  1881. std::stringstream buffer;
  1882. bool pipe = false;
  1883. if (followsLeft())
  1884. {
  1885. buffer << "left";
  1886. pipe = true;
  1887. }
  1888. if (followsTop())
  1889. {
  1890. if (pipe) buffer << "|";
  1891. buffer << "top";
  1892. pipe = true;
  1893. }
  1894. if (followsRight())
  1895. {
  1896. if (pipe) buffer << "|";
  1897. buffer << "right";
  1898. pipe = true;
  1899. }
  1900. if (followsBottom())
  1901. {
  1902. if (pipe) buffer << "|";
  1903. buffer << "bottom";
  1904. }
  1905. node->createChild("follows", true)->setStringValue(buffer.str());
  1906. }
  1907. // Export all widgets as enabled and visible - code must disable.
  1908. node->createChild("mouse_opaque", true)->setBoolValue(mMouseOpaque);
  1909. if (mToolTipMsgPtr && !mToolTipMsgPtr->getString().empty())
  1910. {
  1911. node->createChild("tool_tip", true)->setStringValue(mToolTipMsgPtr->getString());
  1912. }
  1913. if (mSoundFlags != MOUSE_UP)
  1914. {
  1915. node->createChild("sound_flags", true)->setIntValue((S32)mSoundFlags);
  1916. }
  1917. node->createChild("enabled", true)->setBoolValue(getEnabled());
  1918. if (!mControlName.empty())
  1919. {
  1920. node->createChild("control_name", true)->setStringValue(mControlName);
  1921. }
  1922. return node;
  1923. }
  1924. //static
  1925. LLView* LLView::fromXML(LLXMLNodePtr node, LLView* parent,
  1926. LLUICtrlFactory* factory)
  1927. {
  1928. LLView* viewp = new LLView();
  1929. viewp->initFromXML(node, parent);
  1930. return viewp;
  1931. }
  1932. //static
  1933. void LLView::addColorXML(LLXMLNodePtr node, const LLColor4& color,
  1934. const char* xml_name, const char* control_name)
  1935. {
  1936. if (color != LLUI::sColorsGroup->getColor(ll_safe_string(control_name).c_str()))
  1937. {
  1938. node->createChild(xml_name, true)->setFloatValue(4, color.mV);
  1939. }
  1940. }
  1941. //static
  1942. std::string LLView::escapeXML(const std::string& xml, std::string& indent)
  1943. {
  1944. std::string ret = indent + "\"" + LLXMLNode::escapeXML(xml);
  1945. // Replace every newline with a close quote, new line, indent, open quote
  1946. size_t index = ret.size()-1;
  1947. size_t fnd;
  1948. while ((fnd = ret.rfind("\n", index)) != std::string::npos)
  1949. {
  1950. ret.replace(fnd, 1, "\"\n" + indent + "\"");
  1951. index = fnd-1;
  1952. }
  1953. // Append close quote
  1954. ret.append("\"");
  1955. return ret;
  1956. }
  1957. //static
  1958. LLWString LLView::escapeXML(const LLWString& xml)
  1959. {
  1960. LLWString out;
  1961. for (LLWString::size_type i = 0; i < xml.size(); ++i)
  1962. {
  1963. llwchar c = xml[i];
  1964. switch (c)
  1965. {
  1966. case '"': out.append(utf8str_to_wstring("&quot;")); break;
  1967. case '\'': out.append(utf8str_to_wstring("&apos;")); break;
  1968. case '&': out.append(utf8str_to_wstring("&amp;")); break;
  1969. case '<': out.append(utf8str_to_wstring("&lt;")); break;
  1970. case '>': out.append(utf8str_to_wstring("&gt;")); break;
  1971. default: out.push_back(c); break;
  1972. }
  1973. }
  1974. return out;
  1975. }
  1976. //static
  1977. const LLCtrlQuery& LLView::getTabOrderQuery()
  1978. {
  1979. static LLCtrlQuery query;
  1980. if (query.getPreFilters().size() == 0)
  1981. {
  1982. query.addPreFilter(LLVisibleFilter::getInstance());
  1983. query.addPreFilter(LLEnabledFilter::getInstance());
  1984. query.addPreFilter(LLTabStopFilter::getInstance());
  1985. query.addPostFilter(LLLeavesFilter::getInstance());
  1986. }
  1987. return query;
  1988. }
  1989. // This class is only used internally by getFocusRootsQuery below.
  1990. class LLFocusRootsFilter : public LLQueryFilter, public LLSingleton<LLFocusRootsFilter>
  1991. {
  1992. friend class LLSingleton<LLFocusRootsFilter>;
  1993. filter_result_t operator()(const LLView* const view,
  1994. const view_list_t& children) const override
  1995. {
  1996. return filter_result_t(view->isCtrl() && view->isFocusRoot(),
  1997. !view->isFocusRoot());
  1998. }
  1999. };
  2000. //static
  2001. const LLCtrlQuery& LLView::getFocusRootsQuery()
  2002. {
  2003. static LLCtrlQuery query;
  2004. if (query.getPreFilters().size() == 0)
  2005. {
  2006. query.addPreFilter(LLVisibleFilter::getInstance());
  2007. query.addPreFilter(LLEnabledFilter::getInstance());
  2008. query.addPreFilter(LLFocusRootsFilter::getInstance());
  2009. query.addPostFilter(LLRootsFilter::getInstance());
  2010. }
  2011. return query;
  2012. }
  2013. void LLView::userSetShape(const LLRect& new_rect)
  2014. {
  2015. reshape(new_rect.getWidth(), new_rect.getHeight());
  2016. translate(new_rect.mLeft - getRect().mLeft,
  2017. new_rect.mBottom - getRect().mBottom);
  2018. }
  2019. LLView* LLView::findSnapRect(LLRect& new_rect, const LLCoordGL& mouse_dir,
  2020. LLView::ESnapType snap_type, S32 threshold,
  2021. S32 padding)
  2022. {
  2023. new_rect = mRect;
  2024. LLView* snap_view = NULL;
  2025. if (!mParentView)
  2026. {
  2027. return NULL;
  2028. }
  2029. S32 delta_x = 0;
  2030. S32 delta_y = 0;
  2031. if (mouse_dir.mX >= 0)
  2032. {
  2033. S32 new_right = mRect.mRight;
  2034. LLView* view = findSnapEdge(new_right, mouse_dir, SNAP_RIGHT,
  2035. snap_type, threshold, padding);
  2036. delta_x = new_right - mRect.mRight;
  2037. snap_view = view ? view : snap_view;
  2038. }
  2039. if (mouse_dir.mX <= 0)
  2040. {
  2041. S32 new_left = mRect.mLeft;
  2042. LLView* view = findSnapEdge(new_left, mouse_dir, SNAP_LEFT,
  2043. snap_type, threshold, padding);
  2044. delta_x = new_left - mRect.mLeft;
  2045. snap_view = view ? view : snap_view;
  2046. }
  2047. if (mouse_dir.mY >= 0)
  2048. {
  2049. S32 new_top = mRect.mTop;
  2050. LLView* view = findSnapEdge(new_top, mouse_dir, SNAP_TOP,
  2051. snap_type, threshold, padding);
  2052. delta_y = new_top - mRect.mTop;
  2053. snap_view = view ? view : snap_view;
  2054. }
  2055. if (mouse_dir.mY <= 0)
  2056. {
  2057. S32 new_bottom = mRect.mBottom;
  2058. LLView* view = findSnapEdge(new_bottom, mouse_dir, SNAP_BOTTOM,
  2059. snap_type, threshold, padding);
  2060. delta_y = new_bottom - mRect.mBottom;
  2061. snap_view = view ? view : snap_view;
  2062. }
  2063. new_rect.translate(delta_x, delta_y);
  2064. return snap_view;
  2065. }
  2066. LLView* LLView::findSnapEdge(S32& new_edge_val, const LLCoordGL& mouse_dir,
  2067. ESnapEdge snap_edge, ESnapType snap_type,
  2068. S32 threshold, S32 padding)
  2069. {
  2070. LLRect snap_rect = getSnapRect();
  2071. S32 snap_pos = 0;
  2072. switch (snap_edge)
  2073. {
  2074. case SNAP_LEFT:
  2075. snap_pos = snap_rect.mLeft;
  2076. break;
  2077. case SNAP_RIGHT:
  2078. snap_pos = snap_rect.mRight;
  2079. break;
  2080. case SNAP_TOP:
  2081. snap_pos = snap_rect.mTop;
  2082. break;
  2083. case SNAP_BOTTOM:
  2084. snap_pos = snap_rect.mBottom;
  2085. }
  2086. if (!mParentView)
  2087. {
  2088. new_edge_val = snap_pos;
  2089. return NULL;
  2090. }
  2091. LLView* snap_view = NULL;
  2092. // If the view is near the edge of its parent, snap it to the edge.
  2093. LLRect test_rect = snap_rect;
  2094. test_rect.stretch(padding);
  2095. S32 x_threshold = threshold;
  2096. S32 y_threshold = threshold;
  2097. LLRect parent_local_snap_rect = mParentView->getLocalSnapRect();
  2098. if (snap_type == SNAP_PARENT || snap_type == SNAP_PARENT_AND_SIBLINGS)
  2099. {
  2100. switch (snap_edge)
  2101. {
  2102. case SNAP_RIGHT:
  2103. {
  2104. if ((parent_local_snap_rect.mRight - test_rect.mRight) *
  2105. mouse_dir.mX >= 0)
  2106. {
  2107. S32 delta = abs(parent_local_snap_rect.mRight -
  2108. test_rect.mRight);
  2109. if (delta <= x_threshold)
  2110. {
  2111. snap_pos = parent_local_snap_rect.mRight - padding;
  2112. snap_view = mParentView;
  2113. x_threshold = delta;
  2114. }
  2115. }
  2116. break;
  2117. }
  2118. case SNAP_LEFT:
  2119. {
  2120. if (test_rect.mLeft * mouse_dir.mX <= 0)
  2121. {
  2122. S32 delta = abs(test_rect.mLeft -
  2123. parent_local_snap_rect.mLeft);
  2124. if (delta <= x_threshold)
  2125. {
  2126. snap_pos = parent_local_snap_rect.mLeft + padding;
  2127. snap_view = mParentView;
  2128. x_threshold = delta;
  2129. }
  2130. }
  2131. break;
  2132. }
  2133. case SNAP_BOTTOM:
  2134. {
  2135. if (test_rect.mBottom * mouse_dir.mY <= 0)
  2136. {
  2137. S32 delta = abs(test_rect.mBottom -
  2138. parent_local_snap_rect.mBottom);
  2139. if (delta <= y_threshold)
  2140. {
  2141. snap_pos = parent_local_snap_rect.mBottom + padding;
  2142. snap_view = mParentView;
  2143. y_threshold = delta;
  2144. }
  2145. }
  2146. break;
  2147. }
  2148. case SNAP_TOP:
  2149. {
  2150. if ((parent_local_snap_rect.mTop - test_rect.mTop) *
  2151. mouse_dir.mY >= 0)
  2152. {
  2153. S32 delta = abs(parent_local_snap_rect.mTop -
  2154. test_rect.mTop);
  2155. if (delta <= y_threshold)
  2156. {
  2157. snap_pos = parent_local_snap_rect.mTop - padding;
  2158. snap_view = mParentView;
  2159. y_threshold = delta;
  2160. }
  2161. }
  2162. break;
  2163. }
  2164. default:
  2165. {
  2166. llerrs << "Invalid snap edge" << llendl;
  2167. }
  2168. }
  2169. }
  2170. if (snap_type == SNAP_SIBLINGS || snap_type == SNAP_PARENT_AND_SIBLINGS)
  2171. {
  2172. for (child_list_const_iter_t it = mParentView->getChildList()->begin(),
  2173. end = mParentView->getChildList()->end();
  2174. it != end; ++it)
  2175. {
  2176. LLView* siblingp = *it;
  2177. if (!siblingp || !canSnapTo(siblingp)) continue;
  2178. LLRect sibling_rect = siblingp->getSnapRect();
  2179. switch (snap_edge)
  2180. {
  2181. case SNAP_RIGHT:
  2182. {
  2183. S32 delta = abs(test_rect.mRight - sibling_rect.mLeft);
  2184. if (delta <= x_threshold &&
  2185. (test_rect.mRight - sibling_rect.mLeft) *
  2186. mouse_dir.mX <= 0)
  2187. {
  2188. snap_pos = sibling_rect.mLeft - padding;
  2189. snap_view = siblingp;
  2190. x_threshold = delta;
  2191. }
  2192. // If snapped with sibling along other axis, check for
  2193. // shared edge
  2194. else if (abs(sibling_rect.mTop - test_rect.mBottom +
  2195. padding) <= y_threshold ||
  2196. abs(sibling_rect.mBottom - test_rect.mTop -
  2197. padding) <= x_threshold)
  2198. {
  2199. delta = abs(test_rect.mRight - sibling_rect.mRight);
  2200. if (delta <= x_threshold &&
  2201. (test_rect.mRight - sibling_rect.mRight) *
  2202. mouse_dir.mX <= 0)
  2203. {
  2204. snap_pos = sibling_rect.mRight;
  2205. snap_view = siblingp;
  2206. x_threshold = delta;
  2207. }
  2208. }
  2209. break;
  2210. }
  2211. case SNAP_LEFT:
  2212. {
  2213. S32 delta = abs(test_rect.mLeft - sibling_rect.mRight);
  2214. if (delta <= x_threshold &&
  2215. (test_rect.mLeft - sibling_rect.mRight) *
  2216. mouse_dir.mX <= 0)
  2217. {
  2218. snap_pos = sibling_rect.mRight + padding;
  2219. snap_view = siblingp;
  2220. x_threshold = delta;
  2221. }
  2222. // If snapped with sibling along other axis, check for
  2223. // shared edge
  2224. else if (abs(sibling_rect.mTop - test_rect.mBottom +
  2225. padding) <= y_threshold ||
  2226. abs(sibling_rect.mBottom - test_rect.mTop -
  2227. padding) <= y_threshold)
  2228. {
  2229. delta = abs(test_rect.mLeft - sibling_rect.mLeft);
  2230. if (delta <= x_threshold &&
  2231. (test_rect.mLeft - sibling_rect.mLeft) *
  2232. mouse_dir.mX <= 0)
  2233. {
  2234. snap_pos = sibling_rect.mLeft;
  2235. snap_view = siblingp;
  2236. x_threshold = delta;
  2237. }
  2238. }
  2239. break;
  2240. }
  2241. case SNAP_BOTTOM:
  2242. {
  2243. S32 delta = abs(test_rect.mBottom - sibling_rect.mTop);
  2244. if (delta <= y_threshold &&
  2245. (test_rect.mBottom - sibling_rect.mTop) *
  2246. mouse_dir.mY <= 0)
  2247. {
  2248. snap_pos = sibling_rect.mTop + padding;
  2249. snap_view = siblingp;
  2250. y_threshold = delta;
  2251. }
  2252. // If snapped with sibling along other axis, check for
  2253. // shared edge
  2254. else if (abs(sibling_rect.mRight - test_rect.mLeft +
  2255. padding) <= x_threshold ||
  2256. abs(sibling_rect.mLeft - test_rect.mRight -
  2257. padding) <= x_threshold)
  2258. {
  2259. delta = abs(test_rect.mBottom - sibling_rect.mBottom);
  2260. if (delta <= y_threshold &&
  2261. (test_rect.mBottom - sibling_rect.mBottom) *
  2262. mouse_dir.mY <= 0)
  2263. {
  2264. snap_pos = sibling_rect.mBottom;
  2265. snap_view = siblingp;
  2266. y_threshold = delta;
  2267. }
  2268. }
  2269. break;
  2270. }
  2271. case SNAP_TOP:
  2272. {
  2273. S32 delta = abs(test_rect.mTop - sibling_rect.mBottom);
  2274. if (delta <= y_threshold &&
  2275. (test_rect.mTop - sibling_rect.mBottom) *
  2276. mouse_dir.mY <= 0)
  2277. {
  2278. snap_pos = sibling_rect.mBottom - padding;
  2279. snap_view = siblingp;
  2280. y_threshold = delta;
  2281. }
  2282. // If snapped with sibling along other axis, check for
  2283. // shared edge
  2284. else if (abs(sibling_rect.mRight - test_rect.mLeft +
  2285. padding) <= x_threshold ||
  2286. abs(sibling_rect.mLeft - test_rect.mRight -
  2287. padding) <= x_threshold)
  2288. {
  2289. delta = abs(test_rect.mTop - sibling_rect.mTop);
  2290. if (delta <= y_threshold &&
  2291. (test_rect.mTop - sibling_rect.mTop) *
  2292. mouse_dir.mY <= 0)
  2293. {
  2294. snap_pos = sibling_rect.mTop;
  2295. snap_view = siblingp;
  2296. y_threshold = delta;
  2297. }
  2298. }
  2299. break;
  2300. }
  2301. default:
  2302. {
  2303. llerrs << "Invalid snap edge" << llendl;
  2304. }
  2305. }
  2306. }
  2307. }
  2308. new_edge_val = snap_pos;
  2309. return snap_view;
  2310. }
  2311. void LLView::registerEventListener(const std::string& name,
  2312. LLSimpleListener* function)
  2313. {
  2314. mDispatchList.emplace(name, function);
  2315. LL_DEBUGS("View") << getName() << " registered " << name << LL_ENDL;
  2316. }
  2317. void LLView::deregisterEventListener(const std::string& name)
  2318. {
  2319. dispatch_list_t::iterator it = mDispatchList.find(name);
  2320. if (it != mDispatchList.end())
  2321. {
  2322. mDispatchList.hmap_erase(it);
  2323. }
  2324. }
  2325. std::string LLView::findEventListener(LLSimpleListener* listener) const
  2326. {
  2327. for (dispatch_list_t::const_iterator it = mDispatchList.begin(),
  2328. end = mDispatchList.end();
  2329. it != end; ++it)
  2330. {
  2331. if (it->second == listener)
  2332. {
  2333. return it->first;
  2334. }
  2335. }
  2336. if (mParentView)
  2337. {
  2338. return mParentView->findEventListener(listener);
  2339. }
  2340. return LLStringUtil::null;
  2341. }
  2342. LLSimpleListener* LLView::getListenerByName(const std::string& callback_name)
  2343. {
  2344. LLSimpleListener* callback = NULL;
  2345. dispatch_list_t::iterator it = mDispatchList.find(callback_name);
  2346. if (it != mDispatchList.end())
  2347. {
  2348. callback = it->second;
  2349. }
  2350. else if (mParentView)
  2351. {
  2352. callback = mParentView->getListenerByName(callback_name);
  2353. }
  2354. return callback;
  2355. }
  2356. LLControlVariable* LLView::findControl(const std::string& name)
  2357. {
  2358. control_map_t::iterator it = mControls.find(name);
  2359. if (it != mControls.end())
  2360. {
  2361. return it->second;
  2362. }
  2363. if (mParentView)
  2364. {
  2365. return mParentView->findControl(name);
  2366. }
  2367. return LLUI::sConfigGroup->getControl(name.c_str());
  2368. }
  2369. //static
  2370. U32 LLView::createRect(LLXMLNodePtr node, LLRect& rect, LLView* parent_view,
  2371. const LLRect& required_rect)
  2372. {
  2373. U32 follows = 0;
  2374. S32 x = rect.mLeft;
  2375. S32 y = rect.mBottom;
  2376. S32 w = rect.getWidth();
  2377. S32 h = rect.getHeight();
  2378. U32 last_x = 0;
  2379. U32 last_y = 0;
  2380. if (parent_view)
  2381. {
  2382. last_y = parent_view->getRect().getHeight();
  2383. child_list_t::const_iterator it = parent_view->getChildList()->begin();
  2384. if (it != parent_view->getChildList()->end())
  2385. {
  2386. LLView* last_view = *it;
  2387. if (last_view->getSaveToXML())
  2388. {
  2389. last_x = last_view->getRect().mLeft;
  2390. last_y = last_view->getRect().mBottom;
  2391. }
  2392. }
  2393. }
  2394. std::string rect_control;
  2395. node->getAttributeString("rect_control", rect_control);
  2396. if (!rect_control.empty())
  2397. {
  2398. LLRect rect = LLUI::sConfigGroup->getRect(rect_control.c_str());
  2399. x = rect.mLeft;
  2400. y = rect.mBottom;
  2401. w = rect.getWidth();
  2402. h = rect.getHeight();
  2403. }
  2404. if (node->hasAttribute("left"))
  2405. {
  2406. node->getAttributeS32("left", x);
  2407. }
  2408. if (node->hasAttribute("bottom"))
  2409. {
  2410. node->getAttributeS32("bottom", y);
  2411. }
  2412. // Make your width the width of the containing view if you do not specify a
  2413. // width.
  2414. if (parent_view)
  2415. {
  2416. if (w == 0)
  2417. {
  2418. w = llmax(required_rect.getWidth(),
  2419. parent_view->getRect().getWidth() - FLOATER_H_MARGIN - x);
  2420. }
  2421. if (h == 0)
  2422. {
  2423. h = llmax(MIN_WIDGET_HEIGHT, required_rect.getHeight());
  2424. }
  2425. }
  2426. if (node->hasAttribute("width"))
  2427. {
  2428. node->getAttributeS32("width", w);
  2429. }
  2430. if (node->hasAttribute("height"))
  2431. {
  2432. node->getAttributeS32("height", h);
  2433. }
  2434. if (parent_view)
  2435. {
  2436. if (node->hasAttribute("left_delta"))
  2437. {
  2438. S32 left_delta = 0;
  2439. node->getAttributeS32("left_delta", left_delta);
  2440. x = last_x + left_delta;
  2441. }
  2442. else if (node->hasAttribute("left") && node->hasAttribute("right"))
  2443. {
  2444. // Compute width based on left and right
  2445. S32 right = 0;
  2446. node->getAttributeS32("right", right);
  2447. if (right < 0)
  2448. {
  2449. right = parent_view->getRect().getWidth() + right;
  2450. }
  2451. w = right - x;
  2452. }
  2453. else if (node->hasAttribute("left"))
  2454. {
  2455. if (x < 0)
  2456. {
  2457. x = parent_view->getRect().getWidth() + x;
  2458. follows |= FOLLOWS_RIGHT;
  2459. }
  2460. else
  2461. {
  2462. follows |= FOLLOWS_LEFT;
  2463. }
  2464. }
  2465. else if (node->hasAttribute("width") && node->hasAttribute("right"))
  2466. {
  2467. S32 right = 0;
  2468. node->getAttributeS32("right", right);
  2469. if (right < 0)
  2470. {
  2471. right = parent_view->getRect().getWidth() + right;
  2472. }
  2473. x = right - w;
  2474. }
  2475. else
  2476. {
  2477. // Left not specified, same as last
  2478. x = last_x;
  2479. }
  2480. if (node->hasAttribute("bottom_delta"))
  2481. {
  2482. S32 bottom_delta = 0;
  2483. node->getAttributeS32("bottom_delta", bottom_delta);
  2484. y = last_y + bottom_delta;
  2485. }
  2486. else if (node->hasAttribute("top"))
  2487. {
  2488. // compute height based on top
  2489. S32 top = 0;
  2490. node->getAttributeS32("top", top);
  2491. if (top < 0)
  2492. {
  2493. top = parent_view->getRect().getHeight() + top;
  2494. }
  2495. h = top - y;
  2496. }
  2497. else if (node->hasAttribute("bottom"))
  2498. {
  2499. if (y < 0)
  2500. {
  2501. y = parent_view->getRect().getHeight() + y;
  2502. follows |= FOLLOWS_TOP;
  2503. }
  2504. else
  2505. {
  2506. follows |= FOLLOWS_BOTTOM;
  2507. }
  2508. }
  2509. else
  2510. {
  2511. // If bottom not specified, generate automatically
  2512. if (last_y == 0)
  2513. {
  2514. // Treat first child as "bottom"
  2515. y = parent_view->getRect().getHeight() - (h + VPAD);
  2516. follows |= FOLLOWS_TOP;
  2517. }
  2518. else
  2519. {
  2520. // Treat subsequent children as "bottom_delta"
  2521. y = last_y - (h + VPAD);
  2522. }
  2523. }
  2524. }
  2525. else
  2526. {
  2527. x = llmax(x, 0);
  2528. y = llmax(y, 0);
  2529. follows = FOLLOWS_LEFT | FOLLOWS_TOP;
  2530. }
  2531. rect.setOriginAndSize(x, y, w, h);
  2532. return follows;
  2533. }
  2534. void LLView::initFromXML(LLXMLNodePtr node, LLView* parent)
  2535. {
  2536. // create rect first, as this will supply initial follows flags
  2537. LLRect view_rect;
  2538. U32 follows_flags = createRect(node, view_rect, parent, getRequiredRect());
  2539. // call reshape in case there are any child elements that need to be layed
  2540. // out
  2541. reshape(view_rect.getWidth(), view_rect.getHeight());
  2542. setRect(view_rect);
  2543. setFollows(follows_flags);
  2544. parseFollowsFlags(node);
  2545. if (node->hasAttribute("control_name"))
  2546. {
  2547. std::string control_name;
  2548. node->getAttributeString("control_name", control_name);
  2549. setControlName(control_name.c_str(), NULL);
  2550. }
  2551. if (node->hasAttribute("tool_tip"))
  2552. {
  2553. std::string tool_tip_msg;
  2554. node->getAttributeString("tool_tip", tool_tip_msg);
  2555. setToolTip(tool_tip_msg);
  2556. }
  2557. if (node->hasAttribute("enabled"))
  2558. {
  2559. bool enabled;
  2560. node->getAttributeBool("enabled", enabled);
  2561. setEnabled(enabled);
  2562. }
  2563. if (node->hasAttribute("visible"))
  2564. {
  2565. bool visible;
  2566. node->getAttributeBool("visible", visible);
  2567. setVisible(visible);
  2568. }
  2569. if (node->hasAttribute("hover_cursor"))
  2570. {
  2571. std::string cursor_string;
  2572. node->getAttributeString("hover_cursor", cursor_string);
  2573. mHoverCursor = getCursorFromString(cursor_string);
  2574. }
  2575. node->getAttributeBool("use_bounding_rect", mUseBoundingRect);
  2576. node->getAttributeBool("mouse_opaque", mMouseOpaque);
  2577. node->getAttributeS32("default_tab_group", mDefaultTabGroup);
  2578. reshape(view_rect.getWidth(), view_rect.getHeight());
  2579. }
  2580. void LLView::parseFollowsFlags(LLXMLNodePtr node)
  2581. {
  2582. if (node->hasAttribute("follows"))
  2583. {
  2584. setFollowsNone();
  2585. std::string follows;
  2586. node->getAttributeString("follows", follows);
  2587. typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
  2588. boost::char_separator<char> sep("|");
  2589. tokenizer tokens(follows, sep);
  2590. tokenizer::iterator token_iter = tokens.begin();
  2591. while (token_iter != tokens.end())
  2592. {
  2593. const std::string& token_str = *token_iter;
  2594. if (token_str == "left")
  2595. {
  2596. setFollowsLeft();
  2597. }
  2598. else if (token_str == "right")
  2599. {
  2600. setFollowsRight();
  2601. }
  2602. else if (token_str == "top")
  2603. {
  2604. setFollowsTop();
  2605. }
  2606. else if (token_str == "bottom")
  2607. {
  2608. setFollowsBottom();
  2609. }
  2610. else if (token_str == "all")
  2611. {
  2612. setFollowsAll();
  2613. }
  2614. ++token_iter;
  2615. }
  2616. }
  2617. }
  2618. //static
  2619. LLFontGL* LLView::selectFont(LLXMLNodePtr node)
  2620. {
  2621. std::string font_name;
  2622. if (node->hasAttribute("font"))
  2623. {
  2624. node->getAttributeString("font", font_name);
  2625. }
  2626. if (font_name.empty())
  2627. {
  2628. return NULL;
  2629. }
  2630. std::string font_size;
  2631. if (node->hasAttribute("font_size"))
  2632. {
  2633. node->getAttributeString("font_size", font_size);
  2634. }
  2635. U8 style = 0;
  2636. std::string font_style;
  2637. if (node->hasAttribute("font_style")) // Used in XUI XML files
  2638. {
  2639. node->getAttributeString("font_style", font_style);
  2640. style = LLFontGL::getStyleFromString(font_style);
  2641. }
  2642. if (node->hasAttribute("font-style")) // Used in scroll list items
  2643. {
  2644. node->getAttributeString("font-style", font_style);
  2645. style = LLFontGL::getStyleFromString(font_style);
  2646. }
  2647. LLFontDescriptor desc(font_name, font_size, style);
  2648. LLFontGL* gl_font = LLFontGL::getFont(desc);
  2649. return gl_font;
  2650. }
  2651. //static
  2652. LLFontGL::HAlign LLView::selectFontHAlign(LLXMLNodePtr node)
  2653. {
  2654. if (node->hasAttribute("halign"))
  2655. {
  2656. std::string horizontal_align_name;
  2657. node->getAttributeString("halign", horizontal_align_name);
  2658. return LLFontGL::hAlignFromName(horizontal_align_name);
  2659. }
  2660. return LLFontGL::LEFT;
  2661. }
  2662. //static
  2663. LLFontGL::VAlign LLView::selectFontVAlign(LLXMLNodePtr node)
  2664. {
  2665. if (node->hasAttribute("valign"))
  2666. {
  2667. std::string vert_align_name;
  2668. node->getAttributeString("valign", vert_align_name);
  2669. return LLFontGL::vAlignFromName(vert_align_name);
  2670. }
  2671. return LLFontGL::BASELINE;
  2672. }
  2673. //static
  2674. LLFontGL::StyleFlags LLView::selectFontStyle(LLXMLNodePtr node)
  2675. {
  2676. if (node->hasAttribute("style"))
  2677. {
  2678. std::string style_flags_name;
  2679. node->getAttributeString("style", style_flags_name);
  2680. if (style_flags_name == "normal")
  2681. {
  2682. return LLFontGL::NORMAL;
  2683. }
  2684. if (style_flags_name == "bold")
  2685. {
  2686. return LLFontGL::BOLD;
  2687. }
  2688. if (style_flags_name == "italic")
  2689. {
  2690. return LLFontGL::ITALIC;
  2691. }
  2692. if (style_flags_name == "underline")
  2693. {
  2694. return LLFontGL::UNDERLINE;
  2695. }
  2696. }
  2697. return LLFontGL::NORMAL;
  2698. }
  2699. bool LLView::setControlValue(const LLSD& value)
  2700. {
  2701. const std::string& ctrlname = getControlName();
  2702. if (!ctrlname.empty())
  2703. {
  2704. LLUI::sConfigGroup->setUntypedValue(ctrlname.c_str(), value);
  2705. return true;
  2706. }
  2707. return false;
  2708. }
  2709. //virtual
  2710. void LLView::setControlName(const char* control_name, LLView* context)
  2711. {
  2712. if (!context)
  2713. {
  2714. context = this;
  2715. }
  2716. if (!mControlName.empty())
  2717. {
  2718. if (control_name && *control_name)
  2719. {
  2720. llwarns << "Overwriting control '" << mControlName << "' with '"
  2721. << control_name << llendl;
  2722. }
  2723. mControlConnection.disconnect(); // Disconnect current signal
  2724. mControlName.clear();
  2725. }
  2726. // Register new listener
  2727. if (control_name && *control_name)
  2728. {
  2729. std::string ctrl_name(control_name);
  2730. LLControlVariable* control = context->findControl(ctrl_name);
  2731. if (control)
  2732. {
  2733. mControlName = std::move(ctrl_name);
  2734. mControlConnection =
  2735. control->getSignal()->connect(boost::bind(&controlListener,
  2736. _2, getHandle(),
  2737. "value"));
  2738. setValue(control->getValue());
  2739. }
  2740. }
  2741. }
  2742. //static
  2743. bool LLView::controlListener(const LLSD& newvalue, LLHandle<LLView> handle,
  2744. const std::string& type)
  2745. {
  2746. LLView* view = handle.get();
  2747. if (view)
  2748. {
  2749. if (type == "value")
  2750. {
  2751. view->setValue(newvalue);
  2752. return true;
  2753. }
  2754. if (type == "enabled")
  2755. {
  2756. view->setEnabled(newvalue.asBoolean());
  2757. return true;
  2758. }
  2759. if (type == "visible")
  2760. {
  2761. view->setVisible(newvalue.asBoolean());
  2762. return true;
  2763. }
  2764. }
  2765. return false;
  2766. }
  2767. void LLView::addBoolControl(const std::string& name, bool initial_value)
  2768. {
  2769. mControls[name] = new LLControlVariable(name.c_str(), TYPE_BOOLEAN,
  2770. initial_value, "UI");
  2771. }
  2772. LLControlVariable* LLView::getControl(const std::string& name)
  2773. {
  2774. control_map_t::iterator it = mControls.find(name);
  2775. return it != mControls.end() ? it->second : NULL;
  2776. }
  2777. LLView* LLView::createWidget(LLXMLNodePtr xml_node) const
  2778. {
  2779. // forward requests to ui ctrl factory
  2780. return LLUICtrlFactory::getInstance()->createCtrlWidget(NULL, xml_node);
  2781. }