Element.cpp 74 KB

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