Element.cpp 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #include "precompiled.h"
  28. #include "../../Include/Rocket/Core/Element.h"
  29. #include "../../Include/Rocket/Core/Dictionary.h"
  30. #include "../../Include/Rocket/Core/TransformPrimitive.h"
  31. #include <algorithm>
  32. #include <limits>
  33. #include "Clock.h"
  34. #include "ElementAnimation.h"
  35. #include "ElementBackground.h"
  36. #include "ElementBorder.h"
  37. #include "ElementDefinition.h"
  38. #include "ElementStyle.h"
  39. #include "EventDispatcher.h"
  40. #include "ElementDecoration.h"
  41. #include "FontFaceHandle.h"
  42. #include "LayoutEngine.h"
  43. #include "PluginRegistry.h"
  44. #include "StyleSheetParser.h"
  45. #include "XMLParseTools.h"
  46. #include "../../Include/Rocket/Core/Core.h"
  47. namespace Rocket {
  48. namespace Core {
  49. /**
  50. STL function object for sorting elements by z-type (ie, float-types before general types, etc).
  51. @author Peter Curry
  52. */
  53. class ElementSortZOrder
  54. {
  55. public:
  56. bool operator()(const std::pair< Element*, float >& lhs, const std::pair< Element*, float >& rhs) const
  57. {
  58. return lhs.second < rhs.second;
  59. }
  60. };
  61. /**
  62. STL function object for sorting elements by z-index property.
  63. @author Peter Curry
  64. */
  65. class ElementSortZIndex
  66. {
  67. public:
  68. bool operator()(const Element* lhs, const Element* rhs) const
  69. {
  70. // Check the z-index.
  71. return lhs->GetZIndex() < rhs->GetZIndex();
  72. }
  73. };
  74. /// Constructs a new libRocket element.
  75. Element::Element(const String& _tag) : relative_offset_base(0, 0), relative_offset_position(0, 0), absolute_offset(0, 0), scroll_offset(0, 0), boxes(1), content_offset(0, 0), content_box(0, 0),
  76. transform_state(), transform_state_perspective_dirty(true), transform_state_transform_dirty(true), transform_state_parent_transform_dirty(true), dirty_animation(false)
  77. {
  78. tag = _tag.ToLower();
  79. parent = NULL;
  80. focus = NULL;
  81. instancer = NULL;
  82. owner_document = NULL;
  83. offset_fixed = false;
  84. offset_parent = NULL;
  85. offset_dirty = true;
  86. client_area = Box::PADDING;
  87. num_non_dom_children = 0;
  88. visible = true;
  89. z_index = 0;
  90. local_stacking_context = false;
  91. local_stacking_context_forced = false;
  92. stacking_context_dirty = false;
  93. font_face_handle = NULL;
  94. clipping_ignore_depth = 0;
  95. clipping_enabled = false;
  96. clipping_state_dirty = true;
  97. event_dispatcher = new EventDispatcher(this);
  98. style = new ElementStyle(this);
  99. background = new ElementBackground(this);
  100. border = new ElementBorder(this);
  101. decoration = new ElementDecoration(this);
  102. scroll = new ElementScroll(this);
  103. }
  104. Element::~Element()
  105. {
  106. ROCKET_ASSERT(parent == NULL);
  107. PluginRegistry::NotifyElementDestroy(this);
  108. // Delete the scroll funtionality before we delete the children!
  109. delete scroll;
  110. while (!children.empty())
  111. {
  112. // A simplified version of RemoveChild() for destruction.
  113. Element* child = children.front();
  114. child->OnChildRemove(child);
  115. if (num_non_dom_children > 0)
  116. num_non_dom_children--;
  117. deleted_children.push_back(child);
  118. children.erase(children.begin());
  119. }
  120. // Release all deleted children.
  121. ReleaseElements(deleted_children);
  122. delete decoration;
  123. delete border;
  124. delete background;
  125. delete style;
  126. delete event_dispatcher;
  127. if (font_face_handle != NULL)
  128. font_face_handle->RemoveReference();
  129. if (instancer)
  130. instancer->RemoveReference();
  131. }
  132. void Element::Update()
  133. {
  134. ReleaseElements(deleted_children);
  135. active_children = children;
  136. for (size_t i = 0; i < active_children.size(); i++)
  137. active_children[i]->Update();
  138. // Force a definition reload, if necessary.
  139. style->GetDefinition();
  140. scroll->Update();
  141. // Update and advance animations, if necessary.
  142. UpdateAnimation();
  143. AdvanceAnimations();
  144. // Update the transform state, if necessary.
  145. UpdateTransformState();
  146. OnUpdate();
  147. }
  148. void Element::Render()
  149. {
  150. // Rebuild our stacking context if necessary.
  151. if (stacking_context_dirty)
  152. BuildLocalStackingContext();
  153. // Apply our transform
  154. ElementUtilities::ApplyTransform(*this);
  155. // Render all elements in our local stacking context that have a z-index beneath our local index of 0.
  156. size_t i = 0;
  157. for (; i < stacking_context.size() && stacking_context[i]->z_index < 0; ++i)
  158. stacking_context[i]->Render();
  159. // Set up the clipping region for this element.
  160. if (ElementUtilities::SetClippingRegion(this))
  161. {
  162. background->RenderBackground();
  163. border->RenderBorder();
  164. decoration->RenderDecorators();
  165. OnRender();
  166. }
  167. // Render the rest of the elements in the stacking context.
  168. for (; i < stacking_context.size(); ++i)
  169. stacking_context[i]->Render();
  170. // Unapply our transform
  171. ElementUtilities::UnapplyTransform(*this);
  172. }
  173. // Clones this element, returning a new, unparented element.
  174. Element* Element::Clone() const
  175. {
  176. Element* clone = NULL;
  177. if (instancer != NULL)
  178. {
  179. clone = instancer->InstanceElement(NULL, GetTagName(), attributes);
  180. if (clone != NULL)
  181. clone->SetInstancer(instancer);
  182. }
  183. else
  184. clone = Factory::InstanceElement(NULL, GetTagName(), GetTagName(), attributes);
  185. if (clone != NULL)
  186. {
  187. String inner_rml;
  188. GetInnerRML(inner_rml);
  189. clone->SetInnerRML(inner_rml);
  190. }
  191. return clone;
  192. }
  193. // Sets or removes a class on the element.
  194. void Element::SetClass(const String& class_name, bool activate)
  195. {
  196. style->SetClass(class_name, activate);
  197. }
  198. // Checks if a class is set on the element.
  199. bool Element::IsClassSet(const String& class_name) const
  200. {
  201. return style->IsClassSet(class_name);
  202. }
  203. // Specifies the entire list of classes for this element. This will replace any others specified.
  204. void Element::SetClassNames(const String& class_names)
  205. {
  206. SetAttribute("class", class_names);
  207. }
  208. /// Return the active class list
  209. String Element::GetClassNames() const
  210. {
  211. return style->GetClassNames();
  212. }
  213. // Returns the active style sheet for this element. This may be NULL.
  214. StyleSheet* Element::GetStyleSheet() const
  215. {
  216. return style->GetStyleSheet();
  217. }
  218. // Returns the element's definition, updating if necessary.
  219. const ElementDefinition* Element::GetDefinition()
  220. {
  221. return style->GetDefinition();
  222. }
  223. // Fills an String with the full address of this element.
  224. String Element::GetAddress(bool include_pseudo_classes) const
  225. {
  226. // Add the tag name onto the address.
  227. String address(tag);
  228. // Add the ID if we have one.
  229. if (!id.Empty())
  230. {
  231. address += "#";
  232. address += id;
  233. }
  234. String classes = style->GetClassNames();
  235. if (!classes.Empty())
  236. {
  237. classes = classes.Replace(".", " ");
  238. address += ".";
  239. address += classes;
  240. }
  241. if (include_pseudo_classes)
  242. {
  243. const PseudoClassList& pseudo_classes = style->GetActivePseudoClasses();
  244. for (PseudoClassList::const_iterator i = pseudo_classes.begin(); i != pseudo_classes.end(); ++i)
  245. {
  246. address += ":";
  247. address += (*i);
  248. }
  249. }
  250. if (parent)
  251. {
  252. address.Append(" < ");
  253. return address + parent->GetAddress(true);
  254. }
  255. else
  256. return address;
  257. }
  258. // Sets the position of this element, as a two-dimensional offset from another element.
  259. void Element::SetOffset(const Vector2f& offset, Element* _offset_parent, bool _offset_fixed)
  260. {
  261. _offset_fixed |= GetPosition() == POSITION_FIXED;
  262. // If our offset has definitely changed, or any of our parenting has, then these are set and
  263. // updated based on our left / right / top / bottom properties.
  264. if (relative_offset_base != offset ||
  265. offset_parent != _offset_parent ||
  266. offset_fixed != _offset_fixed)
  267. {
  268. relative_offset_base = offset;
  269. offset_fixed = _offset_fixed;
  270. offset_parent = _offset_parent;
  271. UpdateOffset();
  272. DirtyOffset();
  273. }
  274. // Otherwise, our offset is updated in case left / right / top / bottom will have an impact on
  275. // our final position, and our children are dirtied if they do.
  276. else
  277. {
  278. Vector2f& old_base = relative_offset_base;
  279. Vector2f& old_position = relative_offset_position;
  280. UpdateOffset();
  281. if (old_base != relative_offset_base ||
  282. old_position != relative_offset_position)
  283. DirtyOffset();
  284. }
  285. }
  286. // Returns the position of the top-left corner of one of the areas of this element's primary box.
  287. Vector2f Element::GetRelativeOffset(Box::Area area)
  288. {
  289. UpdateLayout();
  290. return relative_offset_base + relative_offset_position + GetBox().GetPosition(area);
  291. }
  292. // Returns the position of the top-left corner of one of the areas of this element's primary box.
  293. Vector2f Element::GetAbsoluteOffset(Box::Area area)
  294. {
  295. UpdateLayout();
  296. if (offset_dirty)
  297. {
  298. offset_dirty = false;
  299. if (offset_parent != NULL)
  300. absolute_offset = offset_parent->GetAbsoluteOffset(Box::BORDER) + relative_offset_base + relative_offset_position;
  301. else
  302. absolute_offset = relative_offset_base + relative_offset_position;
  303. // Add any parent scrolling onto our position as well. Could cache this if required.
  304. if (!offset_fixed)
  305. {
  306. Element* scroll_parent = parent;
  307. while (scroll_parent != NULL)
  308. {
  309. absolute_offset -= (scroll_parent->scroll_offset + scroll_parent->content_offset);
  310. if (scroll_parent == offset_parent)
  311. break;
  312. else
  313. scroll_parent = scroll_parent->parent;
  314. }
  315. }
  316. }
  317. return absolute_offset + GetBox().GetPosition(area);
  318. }
  319. // Sets an alternate area to use as the client area.
  320. void Element::SetClientArea(Box::Area _client_area)
  321. {
  322. client_area = _client_area;
  323. }
  324. // Returns the area the element uses as its client area.
  325. Box::Area Element::GetClientArea() const
  326. {
  327. return client_area;
  328. }
  329. // Sets the dimensions of the element's internal content.
  330. void Element::SetContentBox(const Vector2f& _content_offset, const Vector2f& _content_box)
  331. {
  332. if (content_offset != _content_offset ||
  333. content_box != _content_box)
  334. {
  335. // Seems to be jittering a wee bit; might need to be looked at.
  336. scroll_offset.x += (content_offset.x - _content_offset.x);
  337. scroll_offset.y += (content_offset.y - _content_offset.y);
  338. content_offset = _content_offset;
  339. content_box = _content_box;
  340. scroll_offset.x = Math::Min(scroll_offset.x, GetScrollWidth() - GetClientWidth());
  341. scroll_offset.y = Math::Min(scroll_offset.y, GetScrollHeight() - GetClientHeight());
  342. DirtyOffset();
  343. }
  344. }
  345. // Sets the box describing the size of the element.
  346. void Element::SetBox(const Box& box)
  347. {
  348. if (box != boxes[0] ||
  349. boxes.size() > 1)
  350. {
  351. boxes[0] = box;
  352. boxes.resize(1);
  353. background->DirtyBackground();
  354. border->DirtyBorder();
  355. decoration->ReloadDecorators();
  356. DispatchEvent(RESIZE, Dictionary());
  357. }
  358. }
  359. // Adds a box to the end of the list describing this element's geometry.
  360. void Element::AddBox(const Box& box)
  361. {
  362. boxes.push_back(box);
  363. DispatchEvent(RESIZE, Dictionary());
  364. background->DirtyBackground();
  365. border->DirtyBorder();
  366. decoration->ReloadDecorators();
  367. }
  368. // Returns one of the boxes describing the size of the element.
  369. const Box& Element::GetBox(int index)
  370. {
  371. UpdateLayout();
  372. if (index < 0)
  373. return boxes[0];
  374. else if (index >= GetNumBoxes())
  375. return boxes.back();
  376. return boxes[index];
  377. }
  378. // Returns the number of boxes making up this element's geometry.
  379. int Element::GetNumBoxes()
  380. {
  381. UpdateLayout();
  382. return (int) boxes.size();
  383. }
  384. // Returns the baseline of the element, in pixels offset from the bottom of the element's content area.
  385. float Element::GetBaseline() const
  386. {
  387. return 0;
  388. }
  389. // Gets the intrinsic dimensions of this element, if it is of a type that has an inherent size.
  390. bool Element::GetIntrinsicDimensions(Vector2f& ROCKET_UNUSED_PARAMETER(dimensions))
  391. {
  392. ROCKET_UNUSED(dimensions);
  393. return false;
  394. }
  395. // Checks if a given point in screen coordinates lies within the bordered area of this element.
  396. bool Element::IsPointWithinElement(const Vector2f& point)
  397. {
  398. Vector2f position = GetAbsoluteOffset(Box::BORDER);
  399. for (int i = 0; i < GetNumBoxes(); ++i)
  400. {
  401. const Box& box = GetBox(i);
  402. Vector2f box_position = position + box.GetOffset();
  403. Vector2f box_dimensions = box.GetSize(Box::BORDER);
  404. if (point.x >= box_position.x &&
  405. point.x <= (box_position.x + box_dimensions.x) &&
  406. point.y >= box_position.y &&
  407. point.y <= (box_position.y + box_dimensions.y))
  408. {
  409. return true;
  410. }
  411. }
  412. return false;
  413. }
  414. // Returns the visibility of the element.
  415. bool Element::IsVisible() const
  416. {
  417. return visible;
  418. }
  419. // Returns the z-index of the element.
  420. float Element::GetZIndex() const
  421. {
  422. return z_index;
  423. }
  424. // Returns the element's font face handle.
  425. FontFaceHandle* Element::GetFontFaceHandle() const
  426. {
  427. return font_face_handle;
  428. }
  429. // Sets a local property override on the element.
  430. bool Element::SetProperty(const String& name, const String& value)
  431. {
  432. return style->SetProperty(name, value);
  433. }
  434. // Removes a local property override on the element.
  435. void Element::RemoveProperty(const String& name)
  436. {
  437. style->RemoveProperty(name);
  438. }
  439. // Sets a local property override on the element to a pre-parsed value.
  440. bool Element::SetProperty(const String& name, const Property& property)
  441. {
  442. return style->SetProperty(name, property);
  443. }
  444. // Returns one of this element's properties.
  445. const Property* Element::GetProperty(const String& name)
  446. {
  447. return style->GetProperty(name);
  448. }
  449. // Returns one of this element's properties.
  450. const Property* Element::GetLocalProperty(const String& name)
  451. {
  452. return style->GetLocalProperty(name);
  453. }
  454. const PropertyMap * Element::GetLocalProperties()
  455. {
  456. return style->GetLocalProperties();
  457. }
  458. // Resolves one of this element's style.
  459. float Element::ResolveProperty(const String& name, float base_value)
  460. {
  461. return style->ResolveProperty(name, base_value);
  462. }
  463. // Resolves one of this element's style.
  464. float Element::ResolveProperty(const Property *property, float base_value)
  465. {
  466. return style->ResolveProperty(property, base_value);
  467. }
  468. void Element::GetOffsetProperties(const Property **top, const Property **bottom, const Property **left, const Property **right )
  469. {
  470. style->GetOffsetProperties(top, bottom, left, right);
  471. }
  472. void Element::GetBorderWidthProperties(const Property **border_top, const Property **border_bottom, const Property **border_left, const Property **bottom_right)
  473. {
  474. style->GetBorderWidthProperties(border_top, border_bottom, border_left, bottom_right);
  475. }
  476. void Element::GetMarginProperties(const Property **margin_top, const Property **margin_bottom, const Property **margin_left, const Property **margin_right)
  477. {
  478. style->GetMarginProperties(margin_top, margin_bottom, margin_left, margin_right);
  479. }
  480. void Element::GetPaddingProperties(const Property **padding_top, const Property **padding_bottom, const Property **padding_left, const Property **padding_right)
  481. {
  482. style->GetPaddingProperties(padding_top, padding_bottom, padding_left, padding_right);
  483. }
  484. void Element::GetDimensionProperties(const Property **width, const Property **height)
  485. {
  486. style->GetDimensionProperties(width, height);
  487. }
  488. void Element::GetLocalDimensionProperties(const Property **width, const Property **height)
  489. {
  490. style->GetLocalDimensionProperties(width, height);
  491. }
  492. Vector2f Element::GetContainingBlock()
  493. {
  494. Vector2f containing_block(0, 0);
  495. if (offset_parent != NULL)
  496. {
  497. int position_property = GetPosition();
  498. const Box& parent_box = offset_parent->GetBox();
  499. if (position_property == POSITION_STATIC || position_property == POSITION_RELATIVE)
  500. {
  501. containing_block = parent_box.GetSize();
  502. }
  503. else if(position_property == POSITION_ABSOLUTE || position_property == POSITION_FIXED)
  504. {
  505. containing_block = parent_box.GetSize(Box::PADDING);
  506. }
  507. }
  508. return containing_block;
  509. }
  510. void Element::GetOverflow(int *overflow_x, int *overflow_y)
  511. {
  512. style->GetOverflow(overflow_x, overflow_y);
  513. }
  514. int Element::GetPosition()
  515. {
  516. return style->GetPosition();
  517. }
  518. int Element::GetFloat()
  519. {
  520. return style->GetFloat();
  521. }
  522. int Element::GetDisplay()
  523. {
  524. return style->GetDisplay();
  525. }
  526. int Element::GetWhitespace()
  527. {
  528. return style->GetWhitespace();
  529. }
  530. int Element::GetPointerEvents()
  531. {
  532. return style->GetPointerEvents();
  533. }
  534. const Property *Element::GetLineHeightProperty()
  535. {
  536. return style->GetLineHeightProperty();
  537. }
  538. int Element::GetTextAlign()
  539. {
  540. return style->GetTextAlign();
  541. }
  542. int Element::GetTextTransform()
  543. {
  544. return style->GetTextTransform();
  545. }
  546. const Property *Element::GetVerticalAlignProperty()
  547. {
  548. return style->GetVerticalAlignProperty();
  549. }
  550. // Returns 'perspective' property value from element's style or local cache.
  551. const Property *Element::GetPerspective()
  552. {
  553. return style->GetPerspective();
  554. }
  555. // Returns 'perspective-origin-x' property value from element's style or local cache.
  556. const Property *Element::GetPerspectiveOriginX()
  557. {
  558. return style->GetPerspectiveOriginX();
  559. }
  560. // Returns 'perspective-origin-y' property value from element's style or local cache.
  561. const Property *Element::GetPerspectiveOriginY()
  562. {
  563. return style->GetPerspectiveOriginY();
  564. }
  565. // Returns 'transform' property value from element's style or local cache.
  566. const Property *Element::GetTransform()
  567. {
  568. return style->GetTransform();
  569. }
  570. // Returns 'transform-origin-x' property value from element's style or local cache.
  571. const Property *Element::GetTransformOriginX()
  572. {
  573. return style->GetTransformOriginX();
  574. }
  575. // Returns 'transform-origin-y' property value from element's style or local cache.
  576. const Property *Element::GetTransformOriginY()
  577. {
  578. return style->GetTransformOriginY();
  579. }
  580. // Returns 'transform-origin-z' property value from element's style or local cache.
  581. const Property *Element::GetTransformOriginZ()
  582. {
  583. return style->GetTransformOriginZ();
  584. }
  585. // Returns this element's TransformState
  586. const TransformState *Element::GetTransformState() const noexcept
  587. {
  588. return transform_state.get();
  589. }
  590. // Returns the TransformStates that are effective for this element.
  591. void Element::GetEffectiveTransformState(
  592. const TransformState **local_perspective,
  593. const TransformState **perspective,
  594. const TransformState **transform
  595. ) noexcept
  596. {
  597. UpdateTransformState();
  598. if (local_perspective)
  599. {
  600. *local_perspective = 0;
  601. }
  602. if (perspective)
  603. {
  604. *perspective = 0;
  605. }
  606. if (transform)
  607. {
  608. *transform = 0;
  609. }
  610. Element *perspective_node = 0, *transform_node = 0;
  611. // Find the TransformState to use for unprojecting.
  612. if (transform_state.get() && transform_state->GetLocalPerspective(0))
  613. {
  614. if (local_perspective)
  615. {
  616. *local_perspective = transform_state.get();
  617. }
  618. }
  619. else
  620. {
  621. Element *node = 0;
  622. for (node = parent; node; node = node->parent)
  623. {
  624. if (node->transform_state.get() && node->transform_state->GetPerspective(0))
  625. {
  626. if (perspective)
  627. {
  628. *perspective = node->transform_state.get();
  629. }
  630. perspective_node = node;
  631. break;
  632. }
  633. }
  634. }
  635. // Find the TransformState to use for transforming.
  636. Element *node = 0;
  637. for (node = this; node; node = node->parent)
  638. {
  639. if (node->transform_state.get() && node->transform_state->GetRecursiveTransform(0))
  640. {
  641. if (transform)
  642. {
  643. *transform = node->transform_state.get();
  644. }
  645. transform_node = node;
  646. break;
  647. }
  648. }
  649. }
  650. // Project a 2D point in pixel coordinates onto the element's plane.
  651. const Vector2f Element::Project(const Vector2f& point) noexcept
  652. {
  653. UpdateTransformState();
  654. Context *context = GetContext();
  655. if (!context)
  656. {
  657. return point;
  658. }
  659. const TransformState *local_perspective, *perspective, *transform;
  660. GetEffectiveTransformState(&local_perspective, &perspective, &transform);
  661. Vector2i view_pos(0, 0);
  662. Vector2i view_size = context->GetDimensions();
  663. // Compute the line segment for ray picking, one point on the near and one on the far plane.
  664. // These need to be in clip space coordinates ([-1; 1]³) so that we an unproject them.
  665. Vector3f line_segment[2] =
  666. {
  667. // When unprojected, the intersection point on the near plane
  668. Vector3f(
  669. (point.x - view_pos.x) / (0.5f * view_size.x) - 1.0f,
  670. (view_size.y - point.y - view_pos.y) / (0.5f * view_size.y) - 1.0f,
  671. -1
  672. ),
  673. // When unprojected, the intersection point on the far plane
  674. Vector3f(
  675. (point.x - view_pos.x) / (0.5f * view_size.x) - 1.0f,
  676. (view_size.y - point.y - view_pos.y) / (0.5f * view_size.y) - 1.0f,
  677. 1
  678. )
  679. };
  680. // Find the TransformState to use for unprojecting.
  681. if (local_perspective)
  682. {
  683. TransformState::LocalPerspective the_local_perspective;
  684. local_perspective->GetLocalPerspective(&the_local_perspective);
  685. line_segment[0] = the_local_perspective.Unproject(line_segment[0]);
  686. line_segment[1] = the_local_perspective.Unproject(line_segment[1]);
  687. }
  688. else if (perspective)
  689. {
  690. TransformState::Perspective the_perspective;
  691. perspective->GetPerspective(&the_perspective);
  692. line_segment[0] = the_perspective.Unproject(line_segment[0]);
  693. line_segment[1] = the_perspective.Unproject(line_segment[1]);
  694. }
  695. else
  696. {
  697. line_segment[0] = context->GetViewState().Unproject(line_segment[0]);
  698. line_segment[1] = context->GetViewState().Unproject(line_segment[1]);
  699. }
  700. // Compute three points on the context's corners to define the element's plane.
  701. // It may seem elegant to base this computation on the element's size, but
  702. // there are elements with zero length or height.
  703. Vector3f element_rect[3] =
  704. {
  705. // Top-left corner
  706. Vector3f(0, 0, 0),
  707. // Top-right corner
  708. Vector3f((float)view_size.x, 0, 0),
  709. // Bottom-left corner
  710. Vector3f(0, (float)view_size.y, 0)
  711. };
  712. // Transform by the correct matrix
  713. if (transform)
  714. {
  715. element_rect[0] = transform->Transform(element_rect[0]);
  716. element_rect[1] = transform->Transform(element_rect[1]);
  717. element_rect[2] = transform->Transform(element_rect[2]);
  718. }
  719. Vector3f u = line_segment[0] - line_segment[1];
  720. Vector3f v = element_rect[1] - element_rect[0];
  721. Vector3f w = element_rect[2] - element_rect[0];
  722. // Now compute the intersection point of the line segment and the element's rectangle.
  723. // This is based on the algorithm discussed at Wikipedia
  724. // (http://en.wikipedia.org/wiki/Line-plane_intersection).
  725. Matrix4f A = Matrix4f::FromColumns(
  726. Vector4f(u, 0),
  727. Vector4f(v, 0),
  728. Vector4f(w, 0),
  729. Vector4f(0, 0, 0, 1)
  730. );
  731. if (A.Invert())
  732. {
  733. Vector3f factors = A * (line_segment[0] - element_rect[0]);
  734. Vector3f intersection3d = element_rect[0] + v * factors[1] + w * factors[2];
  735. Vector3f projected;
  736. if (transform)
  737. {
  738. projected = transform->Untransform(intersection3d);
  739. //ROCKET_ASSERT(fabs(projected.z) < 0.0001);
  740. }
  741. else
  742. {
  743. // FIXME: Is this correct?
  744. projected = intersection3d;
  745. }
  746. return Vector2f(projected.x, projected.y);
  747. }
  748. else
  749. {
  750. // The line segment is parallel to the element's plane.
  751. // Although, mathematically, it could also lie within the plane
  752. // (yielding infinitely many intersection points), we still
  753. // return a value that's pretty sure to not match anything,
  754. // since this case has nothing to do with the user `picking'
  755. // anything.
  756. float inf = std::numeric_limits< float >::infinity();
  757. return Vector2f(-inf, -inf);
  758. }
  759. }
  760. // Iterates over the properties defined on this element.
  761. bool Element::IterateProperties(int& index, PseudoClassList& pseudo_classes, String& name, const Property*& property) const
  762. {
  763. return style->IterateProperties(index, pseudo_classes, name, property);
  764. }
  765. // Sets or removes a pseudo-class on the element.
  766. void Element::SetPseudoClass(const String& pseudo_class, bool activate)
  767. {
  768. style->SetPseudoClass(pseudo_class, activate);
  769. }
  770. // Checks if a specific pseudo-class has been set on the element.
  771. bool Element::IsPseudoClassSet(const String& pseudo_class) const
  772. {
  773. return style->IsPseudoClassSet(pseudo_class);
  774. }
  775. // Checks if a complete set of pseudo-classes are set on the element.
  776. bool Element::ArePseudoClassesSet(const PseudoClassList& pseudo_classes) const
  777. {
  778. for (PseudoClassList::const_iterator i = pseudo_classes.begin(); i != pseudo_classes.end(); ++i)
  779. {
  780. if (!IsPseudoClassSet(*i))
  781. return false;
  782. }
  783. return true;
  784. }
  785. // Gets a list of the current active pseudo classes
  786. const PseudoClassList& Element::GetActivePseudoClasses() const
  787. {
  788. return style->GetActivePseudoClasses();
  789. }
  790. /// Get the named attribute
  791. Variant* Element::GetAttribute(const String& name) const
  792. {
  793. return attributes.Get(name);
  794. }
  795. // Checks if the element has a certain attribute.
  796. bool Element::HasAttribute(const String& name)
  797. {
  798. return attributes.Get(name) != NULL;
  799. }
  800. // Removes an attribute from the element
  801. void Element::RemoveAttribute(const String& name)
  802. {
  803. if (attributes.Remove(name))
  804. {
  805. AttributeNameList changed_attributes;
  806. changed_attributes.insert(name);
  807. OnAttributeChange(changed_attributes);
  808. }
  809. }
  810. // Gets the outer most focus element down the tree from this node
  811. Element* Element::GetFocusLeafNode()
  812. {
  813. // If there isn't a focus, then we are the leaf.
  814. if (!focus)
  815. {
  816. return this;
  817. }
  818. // Recurse down the tree until we found the leaf focus element
  819. Element* focus_element = focus;
  820. while (focus_element->focus)
  821. focus_element = focus_element->focus;
  822. return focus_element;
  823. }
  824. // Returns the element's context.
  825. Context* Element::GetContext()
  826. {
  827. ElementDocument* document = GetOwnerDocument();
  828. if (document != NULL)
  829. return document->GetContext();
  830. return NULL;
  831. }
  832. // Set a group of attributes
  833. void Element::SetAttributes(const ElementAttributes* _attributes)
  834. {
  835. int index = 0;
  836. String key;
  837. Variant* value;
  838. AttributeNameList changed_attributes;
  839. while (_attributes->Iterate(index, key, value))
  840. {
  841. changed_attributes.insert(key);
  842. attributes.Set(key, *value);
  843. }
  844. OnAttributeChange(changed_attributes);
  845. }
  846. // Returns the number of attributes on the element.
  847. int Element::GetNumAttributes() const
  848. {
  849. return attributes.Size();
  850. }
  851. // Iterates over all decorators attached to the element.
  852. bool Element::IterateDecorators(int& index, PseudoClassList& pseudo_classes, String& name, Decorator*& decorator, DecoratorDataHandle& decorator_data)
  853. {
  854. return decoration->IterateDecorators(index, pseudo_classes, name, decorator, decorator_data);
  855. }
  856. // Gets the name of the element.
  857. const String& Element::GetTagName() const
  858. {
  859. return tag;
  860. }
  861. // Gets the ID of the element.
  862. const String& Element::GetId() const
  863. {
  864. return id;
  865. }
  866. // Sets the ID of the element.
  867. void Element::SetId(const String& _id)
  868. {
  869. SetAttribute("id", _id);
  870. }
  871. // Gets the horizontal offset from the context's left edge to element's left border edge.
  872. float Element::GetAbsoluteLeft()
  873. {
  874. return GetAbsoluteOffset(Box::BORDER).x;
  875. }
  876. // Gets the vertical offset from the context's top edge to element's top border edge.
  877. float Element::GetAbsoluteTop()
  878. {
  879. return GetAbsoluteOffset(Box::BORDER).y;
  880. }
  881. // Gets the width of the left border of an element.
  882. float Element::GetClientLeft()
  883. {
  884. UpdateLayout();
  885. return GetBox().GetPosition(client_area).x;
  886. }
  887. // Gets the height of the top border of an element.
  888. float Element::GetClientTop()
  889. {
  890. UpdateLayout();
  891. return GetBox().GetPosition(client_area).y;
  892. }
  893. // Gets the inner width of the element.
  894. float Element::GetClientWidth()
  895. {
  896. UpdateLayout();
  897. return GetBox().GetSize(client_area).x - scroll->GetScrollbarSize(ElementScroll::VERTICAL);
  898. }
  899. // Gets the inner height of the element.
  900. float Element::GetClientHeight()
  901. {
  902. UpdateLayout();
  903. return GetBox().GetSize(client_area).y - scroll->GetScrollbarSize(ElementScroll::HORIZONTAL);
  904. }
  905. // Returns the element from which all offset calculations are currently computed.
  906. Element* Element::GetOffsetParent()
  907. {
  908. return offset_parent;
  909. }
  910. // Gets the distance from this element's left border to its offset parent's left border.
  911. float Element::GetOffsetLeft()
  912. {
  913. UpdateLayout();
  914. return relative_offset_base.x + relative_offset_position.x;
  915. }
  916. // Gets the distance from this element's top border to its offset parent's top border.
  917. float Element::GetOffsetTop()
  918. {
  919. UpdateLayout();
  920. return relative_offset_base.y + relative_offset_position.y;
  921. }
  922. // Gets the width of the element, including the client area, padding, borders and scrollbars, but not margins.
  923. float Element::GetOffsetWidth()
  924. {
  925. UpdateLayout();
  926. return GetBox().GetSize(Box::BORDER).x;
  927. }
  928. // Gets the height of the element, including the client area, padding, borders and scrollbars, but not margins.
  929. float Element::GetOffsetHeight()
  930. {
  931. UpdateLayout();
  932. return GetBox().GetSize(Box::BORDER).y;
  933. }
  934. // Gets the left scroll offset of the element.
  935. float Element::GetScrollLeft()
  936. {
  937. UpdateLayout();
  938. return scroll_offset.x;
  939. }
  940. // Sets the left scroll offset of the element.
  941. void Element::SetScrollLeft(float scroll_left)
  942. {
  943. scroll_offset.x = Math::Clamp(scroll_left, 0.0f, GetScrollWidth() - GetClientWidth());
  944. scroll->UpdateScrollbar(ElementScroll::HORIZONTAL);
  945. DirtyOffset();
  946. DispatchEvent("scroll", Dictionary());
  947. }
  948. // Gets the top scroll offset of the element.
  949. float Element::GetScrollTop()
  950. {
  951. UpdateLayout();
  952. return scroll_offset.y;
  953. }
  954. // Sets the top scroll offset of the element.
  955. void Element::SetScrollTop(float scroll_top)
  956. {
  957. scroll_offset.y = Math::Clamp(scroll_top, 0.0f, GetScrollHeight() - GetClientHeight());
  958. scroll->UpdateScrollbar(ElementScroll::VERTICAL);
  959. DirtyOffset();
  960. DispatchEvent("scroll", Dictionary());
  961. }
  962. // Gets the width of the scrollable content of the element; it includes the element padding but not its margin.
  963. float Element::GetScrollWidth()
  964. {
  965. return Math::Max(content_box.x, GetClientWidth());
  966. }
  967. // Gets the height of the scrollable content of the element; it includes the element padding but not its margin.
  968. float Element::GetScrollHeight()
  969. {
  970. return Math::Max(content_box.y, GetClientHeight());
  971. }
  972. // Gets the object representing the declarations of an element's style attributes.
  973. ElementStyle* Element::GetStyle()
  974. {
  975. return style;
  976. }
  977. // Gets the document this element belongs to.
  978. ElementDocument* Element::GetOwnerDocument()
  979. {
  980. if (parent == NULL)
  981. return NULL;
  982. if (!owner_document)
  983. {
  984. owner_document = parent->GetOwnerDocument();
  985. }
  986. return owner_document;
  987. }
  988. // Gets this element's parent node.
  989. Element* Element::GetParentNode() const
  990. {
  991. return parent;
  992. }
  993. // Gets the element immediately following this one in the tree.
  994. Element* Element::GetNextSibling() const
  995. {
  996. if (parent == NULL)
  997. return NULL;
  998. for (size_t i = 0; i < parent->children.size() - (parent->num_non_dom_children + 1); i++)
  999. {
  1000. if (parent->children[i] == this)
  1001. return parent->children[i + 1];
  1002. }
  1003. return NULL;
  1004. }
  1005. // Gets the element immediately preceding this one in the tree.
  1006. Element* Element::GetPreviousSibling() const
  1007. {
  1008. if (parent == NULL)
  1009. return NULL;
  1010. for (size_t i = 1; i < parent->children.size() - parent->num_non_dom_children; i++)
  1011. {
  1012. if (parent->children[i] == this)
  1013. return parent->children[i - 1];
  1014. }
  1015. return NULL;
  1016. }
  1017. // Returns the first child of this element.
  1018. Element* Element::GetFirstChild() const
  1019. {
  1020. if (GetNumChildren() > 0)
  1021. return children[0];
  1022. return NULL;
  1023. }
  1024. // Gets the last child of this element.
  1025. Element* Element::GetLastChild() const
  1026. {
  1027. if (GetNumChildren() > 0)
  1028. return *(children.end() - (num_non_dom_children + 1));
  1029. return NULL;
  1030. }
  1031. Element* Element::GetChild(int index) const
  1032. {
  1033. if (index < 0 || index >= (int) children.size())
  1034. return NULL;
  1035. return children[index];
  1036. }
  1037. int Element::GetNumChildren(bool include_non_dom_elements) const
  1038. {
  1039. return (int) children.size() - (include_non_dom_elements ? 0 : num_non_dom_children);
  1040. }
  1041. // Gets the markup and content of the element.
  1042. void Element::GetInnerRML(String& content) const
  1043. {
  1044. for (int i = 0; i < GetNumChildren(); i++)
  1045. {
  1046. children[i]->GetRML(content);
  1047. }
  1048. }
  1049. // Gets the markup and content of the element.
  1050. String Element::GetInnerRML() const {
  1051. String result;
  1052. GetInnerRML(result);
  1053. return result;
  1054. }
  1055. // Sets the markup and content of the element. All existing children will be replaced.
  1056. void Element::SetInnerRML(const String& rml)
  1057. {
  1058. // Remove all DOM children.
  1059. while ((int) children.size() > num_non_dom_children)
  1060. RemoveChild(children.front());
  1061. Factory::InstanceElementText(this, rml);
  1062. }
  1063. // Sets the current element as the focus object.
  1064. bool Element::Focus()
  1065. {
  1066. // Are we allowed focus?
  1067. int focus_property = GetProperty< int >(FOCUS);
  1068. if (focus_property == FOCUS_NONE)
  1069. return false;
  1070. // Ask our context if we can switch focus.
  1071. Context* context = GetContext();
  1072. if (context == NULL)
  1073. return false;
  1074. if (!context->OnFocusChange(this))
  1075. return false;
  1076. // Set this as the end of the focus chain.
  1077. focus = NULL;
  1078. // Update the focus chain up the hierarchy.
  1079. Element* element = this;
  1080. while (element->GetParentNode())
  1081. {
  1082. element->GetParentNode()->focus = element;
  1083. element = element->GetParentNode();
  1084. }
  1085. return true;
  1086. }
  1087. // Removes focus from from this element.
  1088. void Element::Blur()
  1089. {
  1090. if (parent)
  1091. {
  1092. Context* context = GetContext();
  1093. if (context == NULL)
  1094. return;
  1095. if (context->GetFocusElement() == this)
  1096. {
  1097. parent->Focus();
  1098. }
  1099. else if (parent->focus == this)
  1100. {
  1101. parent->focus = NULL;
  1102. }
  1103. }
  1104. }
  1105. // Fakes a mouse click on this element.
  1106. void Element::Click()
  1107. {
  1108. Context* context = GetContext();
  1109. if (context == NULL)
  1110. return;
  1111. context->GenerateClickEvent(this);
  1112. }
  1113. // Adds an event listener
  1114. void Element::AddEventListener(const String& event, EventListener* listener, bool in_capture_phase)
  1115. {
  1116. event_dispatcher->AttachEvent(event, listener, in_capture_phase);
  1117. }
  1118. // Removes an event listener from this element.
  1119. void Element::RemoveEventListener(const String& event, EventListener* listener, bool in_capture_phase)
  1120. {
  1121. event_dispatcher->DetachEvent(event, listener, in_capture_phase);
  1122. }
  1123. // Dispatches the specified event
  1124. bool Element::DispatchEvent(const String& event, const Dictionary& parameters, bool interruptible)
  1125. {
  1126. return event_dispatcher->DispatchEvent(this, event, parameters, interruptible);
  1127. }
  1128. // Scrolls the parent element's contents so that this element is visible.
  1129. void Element::ScrollIntoView(bool align_with_top)
  1130. {
  1131. Vector2f size(0, 0);
  1132. if (!align_with_top &&
  1133. !boxes.empty())
  1134. {
  1135. size.y = boxes.back().GetOffset().y +
  1136. boxes.back().GetSize(Box::BORDER).y;
  1137. }
  1138. Element* scroll_parent = parent;
  1139. while (scroll_parent != NULL)
  1140. {
  1141. int overflow_x_property = scroll_parent->GetProperty< int >(OVERFLOW_X);
  1142. int overflow_y_property = scroll_parent->GetProperty< int >(OVERFLOW_Y);
  1143. if ((overflow_x_property != OVERFLOW_VISIBLE &&
  1144. scroll_parent->GetScrollWidth() > scroll_parent->GetClientWidth()) ||
  1145. (overflow_y_property != OVERFLOW_VISIBLE &&
  1146. scroll_parent->GetScrollHeight() > scroll_parent->GetClientHeight()))
  1147. {
  1148. Vector2f offset = scroll_parent->GetAbsoluteOffset(Box::BORDER) - GetAbsoluteOffset(Box::BORDER);
  1149. Vector2f scroll_offset(scroll_parent->GetScrollLeft(), scroll_parent->GetScrollTop());
  1150. scroll_offset -= offset;
  1151. scroll_offset.x += scroll_parent->GetClientLeft();
  1152. scroll_offset.y += scroll_parent->GetClientTop();
  1153. if (!align_with_top)
  1154. scroll_offset.y -= (scroll_parent->GetClientHeight() - size.y);
  1155. if (overflow_x_property != OVERFLOW_VISIBLE)
  1156. scroll_parent->SetScrollLeft(scroll_offset.x);
  1157. if (overflow_y_property != OVERFLOW_VISIBLE)
  1158. scroll_parent->SetScrollTop(scroll_offset.y);
  1159. }
  1160. scroll_parent = scroll_parent->GetParentNode();
  1161. }
  1162. }
  1163. // Appends a child to this element
  1164. void Element::AppendChild(Element* child, bool dom_element)
  1165. {
  1166. LockLayout(true);
  1167. child->AddReference();
  1168. child->SetParent(this);
  1169. if (dom_element)
  1170. children.insert(children.end() - num_non_dom_children, child);
  1171. else
  1172. {
  1173. children.push_back(child);
  1174. num_non_dom_children++;
  1175. }
  1176. child->GetStyle()->DirtyDefinition();
  1177. child->GetStyle()->DirtyProperties();
  1178. child->OnChildAdd(child);
  1179. DirtyStackingContext();
  1180. DirtyStructure();
  1181. if (dom_element)
  1182. DirtyLayout();
  1183. LockLayout(false);
  1184. }
  1185. // Adds a child to this element, directly after the adjacent element. Inherits
  1186. // the dom/non-dom status from the adjacent element.
  1187. void Element::InsertBefore(Element* child, Element* adjacent_element)
  1188. {
  1189. // Find the position in the list of children of the adjacent element. If
  1190. // it's NULL or we can't find it, then we insert it at the end of the dom
  1191. // children, as a dom element.
  1192. size_t child_index = 0;
  1193. bool found_child = false;
  1194. if (adjacent_element)
  1195. {
  1196. for (child_index = 0; child_index < children.size(); child_index++)
  1197. {
  1198. if (children[child_index] == adjacent_element)
  1199. {
  1200. found_child = true;
  1201. break;
  1202. }
  1203. }
  1204. }
  1205. if (found_child)
  1206. {
  1207. LockLayout(true);
  1208. child->AddReference();
  1209. child->SetParent(this);
  1210. if ((int) child_index >= GetNumChildren())
  1211. num_non_dom_children++;
  1212. else
  1213. DirtyLayout();
  1214. children.insert(children.begin() + child_index, child);
  1215. child->GetStyle()->DirtyDefinition();
  1216. child->GetStyle()->DirtyProperties();
  1217. child->OnChildAdd(child);
  1218. DirtyStackingContext();
  1219. DirtyStructure();
  1220. LockLayout(false);
  1221. }
  1222. else
  1223. {
  1224. AppendChild(child);
  1225. }
  1226. }
  1227. // Replaces the second node with the first node.
  1228. bool Element::ReplaceChild(Element* inserted_element, Element* replaced_element)
  1229. {
  1230. inserted_element->AddReference();
  1231. inserted_element->SetParent(this);
  1232. ElementList::iterator insertion_point = children.begin();
  1233. while (insertion_point != children.end() && *insertion_point != replaced_element)
  1234. {
  1235. ++insertion_point;
  1236. }
  1237. if (insertion_point == children.end())
  1238. {
  1239. AppendChild(inserted_element);
  1240. return false;
  1241. }
  1242. LockLayout(true);
  1243. children.insert(insertion_point, inserted_element);
  1244. RemoveChild(replaced_element);
  1245. inserted_element->GetStyle()->DirtyDefinition();
  1246. inserted_element->GetStyle()->DirtyProperties();
  1247. inserted_element->OnChildAdd(inserted_element);
  1248. LockLayout(false);
  1249. return true;
  1250. }
  1251. // Removes the specified child
  1252. bool Element::RemoveChild(Element* child)
  1253. {
  1254. size_t child_index = 0;
  1255. for (ElementList::iterator itr = children.begin(); itr != children.end(); ++itr)
  1256. {
  1257. // Add the element to the delete list
  1258. if ((*itr) == child)
  1259. {
  1260. LockLayout(true);
  1261. // Inform the context of the element's pending removal (if we have a valid context).
  1262. Context* context = GetContext();
  1263. if (context)
  1264. context->OnElementRemove(child);
  1265. child->OnChildRemove(child);
  1266. if (child_index >= children.size() - num_non_dom_children)
  1267. num_non_dom_children--;
  1268. deleted_children.push_back(child);
  1269. children.erase(itr);
  1270. // Remove the child element as the focussed child of this element.
  1271. if (child == focus)
  1272. {
  1273. focus = NULL;
  1274. // If this child (or a descendant of this child) is the context's currently
  1275. // focussed element, set the focus to us instead.
  1276. Context* context = GetContext();
  1277. if (context != NULL)
  1278. {
  1279. Element* focus_element = context->GetFocusElement();
  1280. while (focus_element != NULL)
  1281. {
  1282. if (focus_element == child)
  1283. {
  1284. Focus();
  1285. break;
  1286. }
  1287. focus_element = focus_element->GetParentNode();
  1288. }
  1289. }
  1290. }
  1291. DirtyLayout();
  1292. DirtyStackingContext();
  1293. DirtyStructure();
  1294. LockLayout(false);
  1295. return true;
  1296. }
  1297. child_index++;
  1298. }
  1299. return false;
  1300. }
  1301. bool Element::HasChildNodes() const
  1302. {
  1303. return (int) children.size() > num_non_dom_children;
  1304. }
  1305. Element* Element::GetElementById(const String& id)
  1306. {
  1307. // Check for special-case tokens.
  1308. if (id == "#self")
  1309. return this;
  1310. else if (id == "#document")
  1311. return GetOwnerDocument();
  1312. else if (id == "#parent")
  1313. return this->parent;
  1314. else
  1315. {
  1316. Element* search_root = GetOwnerDocument();
  1317. if (search_root == NULL)
  1318. search_root = this;
  1319. return ElementUtilities::GetElementById(search_root, id);
  1320. }
  1321. }
  1322. // Get all elements with the given tag.
  1323. void Element::GetElementsByTagName(ElementList& elements, const String& tag)
  1324. {
  1325. return ElementUtilities::GetElementsByTagName(elements, this, tag);
  1326. }
  1327. // Get all elements with the given class set on them.
  1328. void Element::GetElementsByClassName(ElementList& elements, const String& class_name)
  1329. {
  1330. return ElementUtilities::GetElementsByClassName(elements, this, class_name);
  1331. }
  1332. // Access the event dispatcher
  1333. EventDispatcher* Element::GetEventDispatcher() const
  1334. {
  1335. return event_dispatcher;
  1336. }
  1337. String Element::GetEventDispatcherSummary() const
  1338. {
  1339. return event_dispatcher->ToString();
  1340. }
  1341. // Access the element background.
  1342. ElementBackground* Element::GetElementBackground() const
  1343. {
  1344. return background;
  1345. }
  1346. // Access the element border.
  1347. ElementBorder* Element::GetElementBorder() const
  1348. {
  1349. return border;
  1350. }
  1351. // Access the element decorators
  1352. ElementDecoration* Element::GetElementDecoration() const
  1353. {
  1354. return decoration;
  1355. }
  1356. // Returns the element's scrollbar functionality.
  1357. ElementScroll* Element::GetElementScroll() const
  1358. {
  1359. return scroll;
  1360. }
  1361. int Element::GetClippingIgnoreDepth()
  1362. {
  1363. if (clipping_state_dirty)
  1364. {
  1365. IsClippingEnabled();
  1366. }
  1367. return clipping_ignore_depth;
  1368. }
  1369. bool Element::IsClippingEnabled()
  1370. {
  1371. if (clipping_state_dirty)
  1372. {
  1373. // Is clipping enabled for this element, yes unless both overlow properties are set to visible
  1374. clipping_enabled = style->GetProperty(OVERFLOW_X)->Get< int >() != OVERFLOW_VISIBLE
  1375. || style->GetProperty(OVERFLOW_Y)->Get< int >() != OVERFLOW_VISIBLE;
  1376. // Get the clipping ignore depth from the clip property
  1377. clipping_ignore_depth = 0;
  1378. const Property* clip_property = GetProperty(CLIP);
  1379. if (clip_property->unit == Property::NUMBER)
  1380. clipping_ignore_depth = clip_property->Get< int >();
  1381. else if (clip_property->Get< int >() == CLIP_NONE)
  1382. clipping_ignore_depth = -1;
  1383. clipping_state_dirty = false;
  1384. }
  1385. return clipping_enabled;
  1386. }
  1387. // Gets the render interface owned by this element's context.
  1388. RenderInterface* Element::GetRenderInterface()
  1389. {
  1390. Context* context = GetContext();
  1391. if (context != NULL)
  1392. return context->GetRenderInterface();
  1393. return Rocket::Core::GetRenderInterface();
  1394. }
  1395. void Element::SetInstancer(ElementInstancer* _instancer)
  1396. {
  1397. // Only record the first instancer being set as some instancers call other instancers to do their dirty work, in
  1398. // which case we don't want to update the lowest level instancer.
  1399. if (instancer == NULL)
  1400. {
  1401. instancer = _instancer;
  1402. instancer->AddReference();
  1403. }
  1404. }
  1405. // Forces the element to generate a local stacking context, regardless of the value of its z-index property.
  1406. void Element::ForceLocalStackingContext()
  1407. {
  1408. local_stacking_context_forced = true;
  1409. local_stacking_context = true;
  1410. DirtyStackingContext();
  1411. }
  1412. // Called during the update loop after children are rendered.
  1413. void Element::OnUpdate()
  1414. {
  1415. }
  1416. // Called during render after backgrounds, borders, decorators, but before children, are rendered.
  1417. void Element::OnRender()
  1418. {
  1419. }
  1420. // Called during a layout operation, when the element is being positioned and sized.
  1421. void Element::OnLayout()
  1422. {
  1423. }
  1424. // Called when attributes on the element are changed.
  1425. void Element::OnAttributeChange(const AttributeNameList& changed_attributes)
  1426. {
  1427. if (changed_attributes.find("id") != changed_attributes.end())
  1428. {
  1429. id = GetAttribute< String >("id", "");
  1430. style->DirtyDefinition();
  1431. }
  1432. if (changed_attributes.find("class") != changed_attributes.end())
  1433. {
  1434. style->SetClassNames(GetAttribute< String >("class", ""));
  1435. }
  1436. // Add any inline style declarations.
  1437. if (changed_attributes.find("style") != changed_attributes.end())
  1438. {
  1439. PropertyDictionary properties;
  1440. StyleSheetParser parser;
  1441. parser.ParseProperties(properties, GetAttribute< String >("style", ""));
  1442. Rocket::Core::PropertyMap property_map = properties.GetProperties();
  1443. for (Rocket::Core::PropertyMap::iterator i = property_map.begin(); i != property_map.end(); ++i)
  1444. {
  1445. SetProperty((*i).first, (*i).second);
  1446. }
  1447. }
  1448. }
  1449. // Called when properties on the element are changed.
  1450. void Element::OnPropertyChange(const PropertyNameList& changed_properties)
  1451. {
  1452. bool all_dirty = StyleSheetSpecification::GetRegisteredProperties() == changed_properties;
  1453. if (!IsLayoutDirty())
  1454. {
  1455. if (all_dirty)
  1456. {
  1457. DirtyLayout();
  1458. }
  1459. else
  1460. {
  1461. // Force a relayout if any of the changed properties require it.
  1462. for (PropertyNameList::const_iterator i = changed_properties.begin(); i != changed_properties.end(); ++i)
  1463. {
  1464. const PropertyDefinition* property_definition = StyleSheetSpecification::GetProperty(*i);
  1465. if (property_definition)
  1466. {
  1467. if (property_definition->IsLayoutForced())
  1468. {
  1469. DirtyLayout();
  1470. break;
  1471. }
  1472. }
  1473. }
  1474. }
  1475. }
  1476. // Update the visibility.
  1477. if (all_dirty || changed_properties.find(VISIBILITY) != changed_properties.end() ||
  1478. changed_properties.find(DISPLAY) != changed_properties.end())
  1479. {
  1480. bool new_visibility = GetDisplay() != DISPLAY_NONE &&
  1481. GetProperty< int >(VISIBILITY) == VISIBILITY_VISIBLE;
  1482. if (visible != new_visibility)
  1483. {
  1484. visible = new_visibility;
  1485. if (parent != NULL)
  1486. parent->DirtyStackingContext();
  1487. }
  1488. if (all_dirty ||
  1489. changed_properties.find(DISPLAY) != changed_properties.end())
  1490. {
  1491. if (parent != NULL)
  1492. parent->DirtyStructure();
  1493. }
  1494. }
  1495. // Update the position.
  1496. if (all_dirty ||
  1497. changed_properties.find(LEFT) != changed_properties.end() ||
  1498. changed_properties.find(RIGHT) != changed_properties.end() ||
  1499. changed_properties.find(TOP) != changed_properties.end() ||
  1500. changed_properties.find(BOTTOM) != changed_properties.end())
  1501. {
  1502. UpdateOffset();
  1503. DirtyOffset();
  1504. }
  1505. // Update the z-index.
  1506. if (all_dirty ||
  1507. changed_properties.find(Z_INDEX) != changed_properties.end())
  1508. {
  1509. const Property* z_index_property = GetProperty(Z_INDEX);
  1510. if (z_index_property->unit == Property::KEYWORD &&
  1511. z_index_property->value.Get< int >() == Z_INDEX_AUTO)
  1512. {
  1513. if (local_stacking_context &&
  1514. !local_stacking_context_forced)
  1515. {
  1516. // We're no longer acting as a stacking context.
  1517. local_stacking_context = false;
  1518. stacking_context_dirty = false;
  1519. stacking_context.clear();
  1520. }
  1521. // If our old z-index was not zero, then we must dirty our stacking context so we'll be re-indexed.
  1522. if (z_index != 0)
  1523. {
  1524. z_index = 0;
  1525. DirtyStackingContext();
  1526. }
  1527. }
  1528. else
  1529. {
  1530. float new_z_index;
  1531. if (z_index_property->unit == Property::KEYWORD)
  1532. {
  1533. if (z_index_property->value.Get< int >() == Z_INDEX_TOP)
  1534. new_z_index = FLT_MAX;
  1535. else
  1536. new_z_index = -FLT_MAX;
  1537. }
  1538. else
  1539. new_z_index = z_index_property->value.Get< float >();
  1540. if (new_z_index != z_index)
  1541. {
  1542. z_index = new_z_index;
  1543. if (parent != NULL)
  1544. parent->DirtyStackingContext();
  1545. }
  1546. if (!local_stacking_context)
  1547. {
  1548. local_stacking_context = true;
  1549. stacking_context_dirty = true;
  1550. }
  1551. }
  1552. }
  1553. // Dirty the background if it's changed.
  1554. if (all_dirty ||
  1555. changed_properties.find(BACKGROUND_COLOR) != changed_properties.end() ||
  1556. changed_properties.find(OPACITY) != changed_properties.end() ||
  1557. changed_properties.find(IMAGE_COLOR) != changed_properties.end()) {
  1558. background->DirtyBackground();
  1559. decoration->ReloadDecorators();
  1560. }
  1561. // Dirty the border if it's changed.
  1562. if (all_dirty ||
  1563. changed_properties.find(BORDER_TOP_WIDTH) != changed_properties.end() ||
  1564. changed_properties.find(BORDER_RIGHT_WIDTH) != changed_properties.end() ||
  1565. changed_properties.find(BORDER_BOTTOM_WIDTH) != changed_properties.end() ||
  1566. changed_properties.find(BORDER_LEFT_WIDTH) != changed_properties.end() ||
  1567. changed_properties.find(BORDER_TOP_COLOR) != changed_properties.end() ||
  1568. changed_properties.find(BORDER_RIGHT_COLOR) != changed_properties.end() ||
  1569. changed_properties.find(BORDER_BOTTOM_COLOR) != changed_properties.end() ||
  1570. changed_properties.find(BORDER_LEFT_COLOR) != changed_properties.end() ||
  1571. changed_properties.find(OPACITY) != changed_properties.end())
  1572. border->DirtyBorder();
  1573. // Fetch a new font face if it has been changed.
  1574. if (all_dirty ||
  1575. changed_properties.find(FONT_FAMILY) != changed_properties.end() ||
  1576. changed_properties.find(FONT_CHARSET) != changed_properties.end() ||
  1577. changed_properties.find(FONT_WEIGHT) != changed_properties.end() ||
  1578. changed_properties.find(FONT_STYLE) != changed_properties.end() ||
  1579. changed_properties.find(FONT_SIZE) != changed_properties.end())
  1580. {
  1581. // Store the old em; if it changes, then we need to dirty all em-relative properties.
  1582. int old_em = -1;
  1583. if (font_face_handle != NULL)
  1584. old_em = font_face_handle->GetLineHeight();
  1585. // Fetch the new font face.
  1586. FontFaceHandle* new_font_face_handle = ElementUtilities::GetFontFaceHandle(this);
  1587. // If this is different from our current font face, then we've got to nuke
  1588. // all our characters and tell our parent that we have to be re-laid out.
  1589. if (new_font_face_handle != font_face_handle)
  1590. {
  1591. if (font_face_handle)
  1592. font_face_handle->RemoveReference();
  1593. font_face_handle = new_font_face_handle;
  1594. // Our font face has changed; odds are, so has our em. All of our em-relative values
  1595. // have therefore probably changed as well, so we'll need to dirty them.
  1596. int new_em = -1;
  1597. if (font_face_handle != NULL)
  1598. new_em = font_face_handle->GetLineHeight();
  1599. if (old_em != new_em)
  1600. {
  1601. style->DirtyEmProperties();
  1602. }
  1603. }
  1604. else if (new_font_face_handle != NULL)
  1605. new_font_face_handle->RemoveReference();
  1606. }
  1607. // Check for clipping state changes
  1608. if (all_dirty ||
  1609. changed_properties.find(CLIP) != changed_properties.end() ||
  1610. changed_properties.find(OVERFLOW_X) != changed_properties.end() ||
  1611. changed_properties.find(OVERFLOW_Y) != changed_properties.end())
  1612. {
  1613. clipping_state_dirty = true;
  1614. }
  1615. // Check for `perspective' and `perspective-origin' changes
  1616. if (all_dirty ||
  1617. changed_properties.find(PERSPECTIVE) != changed_properties.end() ||
  1618. changed_properties.find(PERSPECTIVE_ORIGIN_X) != changed_properties.end() ||
  1619. changed_properties.find(PERSPECTIVE_ORIGIN_Y) != changed_properties.end())
  1620. {
  1621. DirtyTransformState(true, false, false);
  1622. }
  1623. // Check for `transform' and `transform-origin' changes
  1624. if (all_dirty ||
  1625. changed_properties.find(TRANSFORM) != changed_properties.end() ||
  1626. changed_properties.find(TRANSFORM_ORIGIN_X) != changed_properties.end() ||
  1627. changed_properties.find(TRANSFORM_ORIGIN_Y) != changed_properties.end() ||
  1628. changed_properties.find(TRANSFORM_ORIGIN_Z) != changed_properties.end())
  1629. {
  1630. DirtyTransformState(false, true, false);
  1631. }
  1632. // Check for `animation' changes
  1633. if (all_dirty || changed_properties.find(ANIMATION) != changed_properties.end())
  1634. {
  1635. DirtyAnimation();
  1636. }
  1637. }
  1638. // Called when a child node has been added somewhere in the hierarchy
  1639. void Element::OnChildAdd(Element* child)
  1640. {
  1641. if (parent)
  1642. parent->OnChildAdd(child);
  1643. }
  1644. // Called when a child node has been removed somewhere in the hierarchy
  1645. void Element::OnChildRemove(Element* child)
  1646. {
  1647. if (parent)
  1648. parent->OnChildRemove(child);
  1649. }
  1650. // Update the element's layout if required.
  1651. void Element::UpdateLayout()
  1652. {
  1653. ElementDocument* document = GetOwnerDocument();
  1654. if (document != NULL)
  1655. document->UpdateLayout();
  1656. }
  1657. // Forces a re-layout of this element, and any other children required.
  1658. void Element::DirtyLayout()
  1659. {
  1660. Element* document = GetOwnerDocument();
  1661. if (document != NULL)
  1662. document->DirtyLayout();
  1663. }
  1664. /// Increment/Decrement the layout lock
  1665. void Element::LockLayout(bool lock)
  1666. {
  1667. Element* document = GetOwnerDocument();
  1668. if (document != NULL)
  1669. document->LockLayout(lock);
  1670. }
  1671. // Forces a re-layout of this element, and any other children required.
  1672. bool Element::IsLayoutDirty()
  1673. {
  1674. Element* document = GetOwnerDocument();
  1675. if (document != NULL)
  1676. return document->IsLayoutDirty();
  1677. return false;
  1678. }
  1679. // Forces a reevaluation of applicable font effects.
  1680. void Element::DirtyFont()
  1681. {
  1682. for (size_t i = 0; i < children.size(); ++i)
  1683. children[i]->DirtyFont();
  1684. }
  1685. void Element::OnReferenceDeactivate()
  1686. {
  1687. if (instancer)
  1688. {
  1689. instancer->ReleaseElement(this);
  1690. }
  1691. else
  1692. {
  1693. // Hopefully we can just delete ourselves.
  1694. //delete this;
  1695. Log::Message(Log::LT_WARNING, "Leak detected: element %s not instanced via Rocket Factory. Unable to release.", GetAddress().CString());
  1696. }
  1697. }
  1698. void Element::ProcessEvent(Event& event)
  1699. {
  1700. if (event == MOUSEDOWN && IsPointWithinElement(Vector2f(event.GetParameter< float >("mouse_x", 0), event.GetParameter< float >("mouse_y", 0))) &&
  1701. event.GetParameter< int >("button", 0) == 0)
  1702. SetPseudoClass("active", true);
  1703. if (event == MOUSESCROLL)
  1704. {
  1705. if (GetScrollHeight() > GetClientHeight())
  1706. {
  1707. int overflow_property = GetProperty< int >(OVERFLOW_Y);
  1708. if (overflow_property == OVERFLOW_AUTO ||
  1709. overflow_property == OVERFLOW_SCROLL)
  1710. {
  1711. // Stop the propagation if the current element has scrollbars.
  1712. // This prevents scrolling in parent elements, which is often unintended. If instead desired behavior is
  1713. // to scroll in parent elements when reaching top/bottom, move StopPropagation inside the next if statement.
  1714. event.StopPropagation();
  1715. int wheel_delta = event.GetParameter< int >("wheel_delta", 0);
  1716. if ((wheel_delta < 0 && GetScrollTop() > 0) ||
  1717. (wheel_delta > 0 && GetScrollHeight() > GetScrollTop() + GetClientHeight()))
  1718. {
  1719. SetScrollTop(GetScrollTop() + wheel_delta * (GetFontFaceHandle() ? ElementUtilities::GetLineHeight(this) : (GetProperty(SCROLL_DEFAULT_STEP_SIZE) ? GetProperty< int >(SCROLL_DEFAULT_STEP_SIZE) : 0)));
  1720. }
  1721. }
  1722. }
  1723. return;
  1724. }
  1725. if (event.GetTargetElement() == this)
  1726. {
  1727. if (event == MOUSEOVER)
  1728. SetPseudoClass("hover", true);
  1729. else if (event == MOUSEOUT)
  1730. SetPseudoClass("hover", false);
  1731. else if (event == FOCUS)
  1732. SetPseudoClass(FOCUS, true);
  1733. else if (event == BLUR)
  1734. SetPseudoClass(FOCUS, false);
  1735. }
  1736. }
  1737. void Element::GetRML(String& content)
  1738. {
  1739. // First we start the open tag, add the attributes then close the open tag.
  1740. // Then comes the children in order, then we add our close tag.
  1741. content.Append("<");
  1742. content.Append(tag);
  1743. int index = 0;
  1744. String name;
  1745. String value;
  1746. while (IterateAttributes(index, name, value))
  1747. {
  1748. size_t length = name.Length() + value.Length() + 8;
  1749. String attribute(length, " %s=\"%s\"", name.CString(), value.CString());
  1750. content.Append(attribute);
  1751. }
  1752. if (HasChildNodes())
  1753. {
  1754. content.Append(">");
  1755. GetInnerRML(content);
  1756. content.Append("</");
  1757. content.Append(tag);
  1758. content.Append(">");
  1759. }
  1760. else
  1761. {
  1762. content.Append(" />");
  1763. }
  1764. }
  1765. void Element::SetParent(Element* _parent)
  1766. {
  1767. // If there's an old parent, detach from it first.
  1768. if (parent &&
  1769. parent != _parent)
  1770. parent->RemoveChild(this);
  1771. // Save our parent
  1772. parent = _parent;
  1773. }
  1774. void Element::ReleaseDeletedElements()
  1775. {
  1776. for (size_t i = 0; i < active_children.size(); i++)
  1777. {
  1778. active_children[i]->ReleaseDeletedElements();
  1779. }
  1780. ReleaseElements(deleted_children);
  1781. active_children = children;
  1782. }
  1783. void Element::ReleaseElements(ElementList& released_elements)
  1784. {
  1785. // Remove deleted children from this element.
  1786. while (!released_elements.empty())
  1787. {
  1788. Element* element = released_elements.back();
  1789. released_elements.pop_back();
  1790. // If this element has been added back into our list, then we remove our previous oustanding reference on it
  1791. // and continue.
  1792. if (std::find(children.begin(), children.end(), element) != children.end())
  1793. {
  1794. element->RemoveReference();
  1795. continue;
  1796. }
  1797. // Set the parent to NULL unless it's been reparented already.
  1798. if (element->GetParentNode() == this)
  1799. element->parent = NULL;
  1800. element->RemoveReference();
  1801. }
  1802. }
  1803. void Element::DirtyOffset()
  1804. {
  1805. offset_dirty = true;
  1806. if(transform_state)
  1807. DirtyTransformState(true, true, false);
  1808. // Not strictly true ... ?
  1809. for (size_t i = 0; i < children.size(); i++)
  1810. children[i]->DirtyOffset();
  1811. }
  1812. void Element::UpdateOffset()
  1813. {
  1814. int position_property = GetPosition();
  1815. if (position_property == POSITION_ABSOLUTE ||
  1816. position_property == POSITION_FIXED)
  1817. {
  1818. if (offset_parent != NULL)
  1819. {
  1820. const Box& parent_box = offset_parent->GetBox();
  1821. Vector2f containing_block = parent_box.GetSize(Box::PADDING);
  1822. const Property *left = GetLocalProperty(LEFT);
  1823. const Property *right = GetLocalProperty(RIGHT);
  1824. // If the element is anchored left, then the position is offset by that resolved value.
  1825. if (left != NULL && left->unit != Property::KEYWORD)
  1826. relative_offset_base.x = parent_box.GetEdge(Box::BORDER, Box::LEFT) + (ResolveProperty(LEFT, containing_block.x) + GetBox().GetEdge(Box::MARGIN, Box::LEFT));
  1827. // If the element is anchored right, then the position is set first so the element's right-most edge
  1828. // (including margins) will render up against the containing box's right-most content edge, and then
  1829. // offset by the resolved value.
  1830. else if (right != NULL && right->unit != Property::KEYWORD)
  1831. relative_offset_base.x = containing_block.x + parent_box.GetEdge(Box::BORDER, Box::LEFT) - (ResolveProperty(RIGHT, containing_block.x) + GetBox().GetSize(Box::BORDER).x + GetBox().GetEdge(Box::MARGIN, Box::RIGHT));
  1832. const Property *top = GetLocalProperty(TOP);
  1833. const Property *bottom = GetLocalProperty(BOTTOM);
  1834. // If the element is anchored top, then the position is offset by that resolved value.
  1835. if (top != NULL && top->unit != Property::KEYWORD)
  1836. relative_offset_base.y = parent_box.GetEdge(Box::BORDER, Box::TOP) + (ResolveProperty(TOP, containing_block.y) + GetBox().GetEdge(Box::MARGIN, Box::TOP));
  1837. // If the element is anchored bottom, then the position is set first so the element's right-most edge
  1838. // (including margins) will render up against the containing box's right-most content edge, and then
  1839. // offset by the resolved value.
  1840. else if (bottom != NULL && bottom->unit != Property::KEYWORD)
  1841. relative_offset_base.y = containing_block.y + parent_box.GetEdge(Box::BORDER, Box::TOP) - (ResolveProperty(BOTTOM, containing_block.y) + GetBox().GetSize(Box::BORDER).y + GetBox().GetEdge(Box::MARGIN, Box::BOTTOM));
  1842. }
  1843. }
  1844. else if (position_property == POSITION_RELATIVE)
  1845. {
  1846. if (offset_parent != NULL)
  1847. {
  1848. const Box& parent_box = offset_parent->GetBox();
  1849. Vector2f containing_block = parent_box.GetSize();
  1850. const Property *left = GetLocalProperty(LEFT);
  1851. const Property *right = GetLocalProperty(RIGHT);
  1852. if (left != NULL && left->unit != Property::KEYWORD)
  1853. relative_offset_position.x = ResolveProperty(LEFT, containing_block.x);
  1854. else if (right != NULL && right->unit != Property::KEYWORD)
  1855. relative_offset_position.x = -1 * ResolveProperty(RIGHT, containing_block.x);
  1856. else
  1857. relative_offset_position.x = 0;
  1858. const Property *top = GetLocalProperty(TOP);
  1859. const Property *bottom = GetLocalProperty(BOTTOM);
  1860. if (top != NULL && top->unit != Property::KEYWORD)
  1861. relative_offset_position.y = ResolveProperty(TOP, containing_block.y);
  1862. else if (bottom != NULL && bottom->unit != Property::KEYWORD)
  1863. relative_offset_position.y = -1 * ResolveProperty(BOTTOM, containing_block.y);
  1864. else
  1865. relative_offset_position.y = 0;
  1866. }
  1867. }
  1868. else
  1869. {
  1870. relative_offset_position.x = 0;
  1871. relative_offset_position.y = 0;
  1872. }
  1873. }
  1874. void Element::BuildLocalStackingContext()
  1875. {
  1876. stacking_context_dirty = false;
  1877. stacking_context.clear();
  1878. BuildStackingContext(&stacking_context);
  1879. std::stable_sort(stacking_context.begin(), stacking_context.end(), ElementSortZIndex());
  1880. }
  1881. void Element::BuildStackingContext(ElementList* new_stacking_context)
  1882. {
  1883. // Build the list of ordered children. Our child list is sorted within the stacking context so stacked elements
  1884. // will render in the right order; ie, positioned elements will render on top of inline elements, which will render
  1885. // on top of floated elements, which will render on top of block elements.
  1886. std::vector< std::pair< Element*, float > > ordered_children;
  1887. for (size_t i = 0; i < children.size(); ++i)
  1888. {
  1889. Element* child = children[i];
  1890. if (!child->IsVisible())
  1891. continue;
  1892. std::pair< Element*, float > ordered_child;
  1893. ordered_child.first = child;
  1894. if (child->GetPosition() != POSITION_STATIC)
  1895. ordered_child.second = 3;
  1896. else if (child->GetFloat() != FLOAT_NONE)
  1897. ordered_child.second = 1;
  1898. else if (child->GetDisplay() == DISPLAY_BLOCK)
  1899. ordered_child.second = 0;
  1900. else
  1901. ordered_child.second = 2;
  1902. ordered_children.push_back(ordered_child);
  1903. }
  1904. // Sort the list!
  1905. std::stable_sort(ordered_children.begin(), ordered_children.end(), ElementSortZOrder());
  1906. // Add the list of ordered children into the stacking context in order.
  1907. for (size_t i = 0; i < ordered_children.size(); ++i)
  1908. {
  1909. new_stacking_context->push_back(ordered_children[i].first);
  1910. if (!ordered_children[i].first->local_stacking_context)
  1911. ordered_children[i].first->BuildStackingContext(new_stacking_context);
  1912. }
  1913. }
  1914. void Element::DirtyStackingContext()
  1915. {
  1916. // The first ancestor of ours that doesn't have an automatic z-index is the ancestor that is establishing our local
  1917. // stacking context.
  1918. Element* stacking_context_parent = this;
  1919. while (stacking_context_parent != NULL &&
  1920. !stacking_context_parent->local_stacking_context)
  1921. stacking_context_parent = stacking_context_parent->GetParentNode();
  1922. if (stacking_context_parent != NULL)
  1923. stacking_context_parent->stacking_context_dirty = true;
  1924. }
  1925. void Element::DirtyStructure()
  1926. {
  1927. // Clear the cached owner document
  1928. owner_document = NULL;
  1929. // Inform all children that the structure is drity
  1930. for (size_t i = 0; i < children.size(); ++i)
  1931. {
  1932. const ElementDefinition* element_definition = children[i]->GetStyle()->GetDefinition();
  1933. if (element_definition != NULL &&
  1934. element_definition->IsStructurallyVolatile())
  1935. {
  1936. children[i]->GetStyle()->DirtyDefinition();
  1937. }
  1938. children[i]->DirtyStructure();
  1939. }
  1940. }
  1941. bool Element::Animate(const String & property_name, const Property & target_value, float duration, Tween tween, int num_iterations, bool alternate_direction, float delay, const Property* start_value)
  1942. {
  1943. bool result = false;
  1944. if (auto it_animation = StartAnimation(property_name, start_value, num_iterations, alternate_direction, delay); it_animation != animations.end())
  1945. {
  1946. result = it_animation->AddKey(duration, target_value, *this, tween, true);
  1947. if (!result)
  1948. animations.erase(it_animation);
  1949. }
  1950. return result;
  1951. }
  1952. bool Element::AddAnimationKey(const String & property_name, const Property & target_value, float duration, Tween tween)
  1953. {
  1954. ElementAnimation* animation = nullptr;
  1955. for (auto& existing_animation : animations) {
  1956. if (existing_animation.GetPropertyName() == property_name) {
  1957. animation = &existing_animation;
  1958. break;
  1959. }
  1960. }
  1961. if (!animation)
  1962. return false;
  1963. bool result = animation->AddKey(animation->GetDuration() + duration, target_value, *this, tween, true);
  1964. return result;
  1965. }
  1966. ElementAnimationList::iterator Element::StartAnimation(const String & property_name, const Property* start_value, int num_iterations, bool alternate_direction, float delay)
  1967. {
  1968. auto it = std::find_if(animations.begin(), animations.end(), [&](const ElementAnimation& el) { return el.GetPropertyName() == property_name; });
  1969. if (it == animations.end())
  1970. {
  1971. animations.emplace_back();
  1972. it = animations.end() - 1;
  1973. }
  1974. Property value;
  1975. if (start_value)
  1976. {
  1977. value = *start_value;
  1978. if (!value.definition)
  1979. if(auto default_value = GetProperty(property_name))
  1980. value.definition = default_value->definition;
  1981. }
  1982. else if (auto default_value = GetProperty(property_name))
  1983. {
  1984. value = *default_value;
  1985. }
  1986. if (value.definition)
  1987. {
  1988. double start_time = Clock::GetElapsedTime() + (double)delay;
  1989. *it = ElementAnimation{ property_name, value, start_time, 0.0f, num_iterations, alternate_direction, false };
  1990. }
  1991. else
  1992. {
  1993. animations.erase(it);
  1994. it = animations.end();
  1995. }
  1996. return it;
  1997. }
  1998. bool Element::AddAnimationKeyTime(const String & property_name, const Property* target_value, float time, Tween tween)
  1999. {
  2000. if (!target_value)
  2001. target_value = GetProperty(property_name);
  2002. if (!target_value)
  2003. return false;
  2004. ElementAnimation* animation = nullptr;
  2005. for (auto& existing_animation : animations) {
  2006. if (existing_animation.GetPropertyName() == property_name) {
  2007. animation = &existing_animation;
  2008. break;
  2009. }
  2010. }
  2011. if (!animation)
  2012. return false;
  2013. bool result = animation->AddKey(time, *target_value, *this, tween, true);
  2014. return result;
  2015. }
  2016. bool Element::StartTransition(const Transition & transition, const Property& start_value, const Property & target_value)
  2017. {
  2018. auto it = std::find_if(animations.begin(), animations.end(), [&](const ElementAnimation& el) { return el.GetPropertyName() == transition.name; });
  2019. if (it != animations.end() && !it->IsTransition())
  2020. return false;
  2021. float duration = transition.duration;
  2022. double start_time = Clock::GetElapsedTime() + (double)transition.delay;
  2023. if (it == animations.end())
  2024. {
  2025. // Add transition as new animation
  2026. animations.push_back(
  2027. ElementAnimation{ transition.name, start_value, start_time, 0.0f, 1, false, true }
  2028. );
  2029. it = (animations.end() - 1);
  2030. }
  2031. else
  2032. {
  2033. // Compress the duration based on the progress of the current animation
  2034. float f = it->GetInterpolationFactor();
  2035. f = 1.0f - (1.0f - f)*transition.reverse_adjustment_factor;
  2036. duration = duration * f;
  2037. // Replace old transition
  2038. *it = ElementAnimation{ transition.name, start_value, start_time, 0.0f, 1, false, true };
  2039. }
  2040. bool result = it->AddKey(duration, target_value, *this, transition.tween, true);
  2041. if (result)
  2042. SetProperty(transition.name, start_value);
  2043. else
  2044. animations.erase(it);
  2045. return result;
  2046. }
  2047. void Element::DirtyAnimation()
  2048. {
  2049. dirty_animation = true;
  2050. }
  2051. void Element::UpdateAnimation()
  2052. {
  2053. if (dirty_animation)
  2054. {
  2055. const Property* property = style->GetLocalProperty(ANIMATION);
  2056. StyleSheet* stylesheet = nullptr;
  2057. if (property && (stylesheet = GetStyleSheet()))
  2058. {
  2059. auto animation_list = property->Get<AnimationList>();
  2060. for (auto& animation : animation_list)
  2061. {
  2062. Keyframes* keyframes_ptr = stylesheet->GetKeyframes(animation.name);
  2063. if (keyframes_ptr && keyframes_ptr->blocks.size() >= 1 && !animation.paused)
  2064. {
  2065. auto& property_names = keyframes_ptr->property_names;
  2066. auto& blocks = keyframes_ptr->blocks;
  2067. bool has_from_key = (blocks[0].normalized_time == 0);
  2068. bool has_to_key = (blocks.back().normalized_time == 1);
  2069. // If the first key defines initial conditions for a given property, use those values, else, use this element's current values.
  2070. for (auto& property : property_names)
  2071. StartAnimation(property, (has_from_key ? blocks[0].properties.GetProperty(property) : nullptr), animation.num_iterations, animation.alternate, animation.delay);
  2072. // Need to skip the first and last keys if they set the initial and end conditions, respectively.
  2073. for (int i = (has_from_key ? 1 : 0); i < (int)blocks.size() + (has_to_key ? -1 : 0); i++)
  2074. {
  2075. // Add properties of current key to animation
  2076. float time = blocks[i].normalized_time * animation.duration;
  2077. for (auto& property : blocks[i].properties.GetProperties())
  2078. AddAnimationKeyTime(property.first, &property.second, time, animation.tween);
  2079. }
  2080. // If the last key defines end conditions for a given property, use those values, else, use this element's current values.
  2081. float time = animation.duration;
  2082. for (auto& property : property_names)
  2083. AddAnimationKeyTime(property, (has_to_key ? blocks.back().properties.GetProperty(property) : nullptr), time, animation.tween);
  2084. }
  2085. }
  2086. }
  2087. dirty_animation = false;
  2088. }
  2089. }
  2090. void Element::AdvanceAnimations()
  2091. {
  2092. if (!animations.empty())
  2093. {
  2094. double time = Clock::GetElapsedTime();
  2095. for (auto& animation : animations)
  2096. {
  2097. Property property = animation.UpdateAndGetProperty(time, *this);
  2098. if (property.unit != Property::UNKNOWN)
  2099. SetProperty(animation.GetPropertyName(), property);
  2100. }
  2101. auto it_completed = std::remove_if(animations.begin(), animations.end(), [](const ElementAnimation& animation) { return animation.IsComplete(); });
  2102. std::vector<Dictionary> dictionary_list;
  2103. std::vector<bool> is_transition;
  2104. dictionary_list.reserve(animations.end() - it_completed);
  2105. is_transition.reserve(animations.end() - it_completed);
  2106. for (auto it = it_completed; it != animations.end(); ++it)
  2107. {
  2108. dictionary_list.emplace_back().Set("property", it->GetPropertyName());
  2109. is_transition.push_back(it->IsTransition());
  2110. }
  2111. // Need to erase elements before submitting event, as iterators might be invalidated when calling external code.
  2112. animations.erase(it_completed, animations.end());
  2113. for (size_t i = 0; i < dictionary_list.size(); i++)
  2114. DispatchEvent(is_transition[i] ? TRANSITIONEND : ANIMATIONEND, dictionary_list[i]);
  2115. }
  2116. }
  2117. void Element::DirtyTransformState(bool perspective_changed, bool transform_changed, bool parent_transform_changed)
  2118. {
  2119. for (size_t i = 0; i < children.size(); ++i)
  2120. {
  2121. children[i]->DirtyTransformState(false, false, transform_changed || parent_transform_changed);
  2122. }
  2123. if (perspective_changed)
  2124. {
  2125. this->transform_state_perspective_dirty = true;
  2126. }
  2127. if (transform_changed)
  2128. {
  2129. this->transform_state_transform_dirty = true;
  2130. }
  2131. if (parent_transform_changed)
  2132. {
  2133. this->transform_state_parent_transform_dirty = true;
  2134. }
  2135. }
  2136. void Element::UpdateTransformState()
  2137. {
  2138. if (!(transform_state_perspective_dirty || transform_state_transform_dirty || transform_state_parent_transform_dirty))
  2139. {
  2140. return;
  2141. }
  2142. if (!transform_state.get())
  2143. {
  2144. transform_state.reset(new TransformState());
  2145. }
  2146. if(transform_state_perspective_dirty || transform_state_transform_dirty)
  2147. {
  2148. Context *context = GetContext();
  2149. Vector2f pos = GetAbsoluteOffset(Box::BORDER);
  2150. Vector2f size = GetBox().GetSize(Box::BORDER);
  2151. if (transform_state_perspective_dirty)
  2152. {
  2153. bool have_perspective = false;
  2154. TransformState::Perspective perspective_value;
  2155. perspective_value.vanish = Vector2f(pos.x + size.x * 0.5f, pos.y + size.y * 0.5f);
  2156. const Property *perspective = GetPerspective();
  2157. if (perspective && (perspective->unit != Property::KEYWORD || perspective->value.Get< int >() != PERSPECTIVE_NONE))
  2158. {
  2159. have_perspective = true;
  2160. // Compute the perspective value
  2161. perspective_value.distance = ResolveProperty(perspective, Math::Max(size.x, size.y));
  2162. // Compute the perspective origin, if necessary
  2163. if (perspective_value.distance > 0)
  2164. {
  2165. const Property *perspective_origin_x = GetPerspectiveOriginX();
  2166. if (perspective_origin_x)
  2167. {
  2168. if (perspective_origin_x->unit == Property::KEYWORD)
  2169. {
  2170. switch (perspective_origin_x->value.Get< int >())
  2171. {
  2172. case PERSPECTIVE_ORIGIN_X_LEFT:
  2173. perspective_value.vanish.x = pos.x;
  2174. break;
  2175. case PERSPECTIVE_ORIGIN_X_CENTER:
  2176. perspective_value.vanish.x = pos.x + size.x * 0.5f;
  2177. break;
  2178. case PERSPECTIVE_ORIGIN_X_RIGHT:
  2179. perspective_value.vanish.x = pos.x + size.x;
  2180. break;
  2181. }
  2182. }
  2183. else
  2184. {
  2185. perspective_value.vanish.x = pos.x + ResolveProperty(perspective_origin_x, size.x);
  2186. }
  2187. }
  2188. const Property *perspective_origin_y = GetPerspectiveOriginY();
  2189. if (perspective_origin_y)
  2190. {
  2191. if (perspective_origin_y->unit == Property::KEYWORD)
  2192. {
  2193. switch (perspective_origin_y->value.Get< int >())
  2194. {
  2195. case PERSPECTIVE_ORIGIN_Y_TOP:
  2196. perspective_value.vanish.y = pos.y;
  2197. break;
  2198. case PERSPECTIVE_ORIGIN_Y_CENTER:
  2199. perspective_value.vanish.y = pos.y + size.y * 0.5f;
  2200. break;
  2201. case PERSPECTIVE_ORIGIN_Y_BOTTOM:
  2202. perspective_value.vanish.y = pos.y + size.y;
  2203. break;
  2204. }
  2205. }
  2206. else
  2207. {
  2208. perspective_value.vanish.y = pos.y + ResolveProperty(perspective_origin_y, size.y);
  2209. }
  2210. }
  2211. }
  2212. }
  2213. if (have_perspective && context)
  2214. {
  2215. perspective_value.view_size = context->GetDimensions();
  2216. transform_state->SetPerspective(&perspective_value);
  2217. }
  2218. else
  2219. {
  2220. transform_state->SetPerspective(0);
  2221. }
  2222. transform_state_perspective_dirty = false;
  2223. }
  2224. if (transform_state_transform_dirty)
  2225. {
  2226. bool have_local_perspective = false;
  2227. TransformState::LocalPerspective local_perspective;
  2228. bool have_transform = false;
  2229. Matrix4f transform_value = Matrix4f::Identity();
  2230. Vector3f transform_origin(pos.x + size.x * 0.5f, pos.y + size.y * 0.5f, 0);
  2231. const Property *transform = GetTransform();
  2232. if (transform && (transform->unit != Property::KEYWORD || transform->value.Get< int >() != TRANSFORM_NONE))
  2233. {
  2234. TransformRef transforms = transform->value.Get< TransformRef >();
  2235. int n = transforms->GetNumPrimitives();
  2236. for (int i = 0; i < n; ++i)
  2237. {
  2238. const Transforms::Primitive &primitive = transforms->GetPrimitive(i);
  2239. if (primitive.ResolvePerspective(local_perspective.distance, *this))
  2240. {
  2241. have_local_perspective = true;
  2242. }
  2243. Matrix4f matrix;
  2244. if (primitive.ResolveTransform(matrix, *this))
  2245. {
  2246. transform_value *= matrix;
  2247. have_transform = true;
  2248. }
  2249. }
  2250. // Compute the transform origin
  2251. const Property *transform_origin_x = GetTransformOriginX();
  2252. if (transform_origin_x)
  2253. {
  2254. if (transform_origin_x->unit == Property::KEYWORD)
  2255. {
  2256. switch (transform_origin_x->value.Get< int >())
  2257. {
  2258. case TRANSFORM_ORIGIN_X_LEFT:
  2259. transform_origin.x = pos.x;
  2260. break;
  2261. case TRANSFORM_ORIGIN_X_CENTER:
  2262. transform_origin.x = pos.x + size.x * 0.5f;
  2263. break;
  2264. case TRANSFORM_ORIGIN_X_RIGHT:
  2265. transform_origin.x = pos.x + size.x;
  2266. break;
  2267. }
  2268. }
  2269. else
  2270. {
  2271. transform_origin.x = pos.x + ResolveProperty(transform_origin_x, size.x);
  2272. }
  2273. }
  2274. const Property *transform_origin_y = GetTransformOriginY();
  2275. if (transform_origin_y)
  2276. {
  2277. if (transform_origin_y->unit == Property::KEYWORD)
  2278. {
  2279. switch (transform_origin_y->value.Get< int >())
  2280. {
  2281. case TRANSFORM_ORIGIN_Y_TOP:
  2282. transform_origin.y = pos.y;
  2283. break;
  2284. case TRANSFORM_ORIGIN_Y_CENTER:
  2285. transform_origin.y = pos.y + size.y * 0.5f;
  2286. break;
  2287. case TRANSFORM_ORIGIN_Y_BOTTOM:
  2288. transform_origin.y = pos.y + size.y;
  2289. break;
  2290. }
  2291. }
  2292. else
  2293. {
  2294. transform_origin.y = pos.y + ResolveProperty(transform_origin_y, size.y);
  2295. }
  2296. }
  2297. const Property *transform_origin_z = GetTransformOriginZ();
  2298. if (transform_origin_z)
  2299. {
  2300. transform_origin.z = ResolveProperty(transform_origin_z, Math::Max(size.x, size.y));
  2301. }
  2302. }
  2303. if (have_local_perspective && context)
  2304. {
  2305. local_perspective.view_size = context->GetDimensions();
  2306. transform_state->SetLocalPerspective(&local_perspective);
  2307. }
  2308. else
  2309. {
  2310. transform_state->SetLocalPerspective(0);
  2311. }
  2312. if (have_transform)
  2313. {
  2314. // TODO: If we're using the global projection matrix
  2315. // (perspective < 0), then scale the coordinates from
  2316. // pixel space to 3D unit space.
  2317. // Transform the Rocket context so that the computed `transform_origin'
  2318. // lies at the coordinate system origin.
  2319. transform_value =
  2320. Matrix4f::Translate(transform_origin)
  2321. * transform_value
  2322. * Matrix4f::Translate(-transform_origin);
  2323. }
  2324. transform_state->SetTransform(have_transform ? &transform_value : 0);
  2325. transform_state_transform_dirty = false;
  2326. }
  2327. }
  2328. if (transform_state_parent_transform_dirty)
  2329. {
  2330. // We need to clean up from the top-most to the bottom-most dirt.
  2331. if (parent)
  2332. {
  2333. parent->UpdateTransformState();
  2334. }
  2335. if (transform_state.get())
  2336. {
  2337. // Store the parent's new full transform as our parent transform
  2338. Element *node = 0;
  2339. Matrix4f parent_transform;
  2340. for (node = parent; node; node = node->parent)
  2341. {
  2342. if (node->GetTransformState() && node->GetTransformState()->GetRecursiveTransform(&parent_transform))
  2343. {
  2344. transform_state->SetParentRecursiveTransform(&parent_transform);
  2345. break;
  2346. }
  2347. }
  2348. if (!node)
  2349. {
  2350. transform_state->SetParentRecursiveTransform(0);
  2351. }
  2352. }
  2353. transform_state_parent_transform_dirty = false;
  2354. }
  2355. // If we neither have a local perspective, nor a perspective nor a
  2356. // transform, we don't need to keep the large TransformState object
  2357. // around. GetEffectiveTransformState() will then recursively visit
  2358. // parents in order to find a non-trivial TransformState.
  2359. if (transform_state.get() && !transform_state->GetLocalPerspective(0) && !transform_state->GetPerspective(0) && !transform_state->GetTransform(0))
  2360. {
  2361. transform_state.reset(0);
  2362. }
  2363. }
  2364. }
  2365. }