2
0

Element.cpp 68 KB

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