Element.cpp 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635
  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, *this);
  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. Vector2f Element::GetContainingBlock()
  504. {
  505. Vector2f containing_block(0, 0);
  506. if (offset_parent != NULL)
  507. {
  508. int position_property = GetPosition();
  509. const Box& parent_box = offset_parent->GetBox();
  510. if (position_property == POSITION_STATIC || position_property == POSITION_RELATIVE)
  511. {
  512. containing_block = parent_box.GetSize();
  513. }
  514. else if(position_property == POSITION_ABSOLUTE || position_property == POSITION_FIXED)
  515. {
  516. containing_block = parent_box.GetSize(Box::PADDING);
  517. }
  518. }
  519. return containing_block;
  520. }
  521. void Element::GetOverflow(int *overflow_x, int *overflow_y)
  522. {
  523. style->GetOverflow(overflow_x, overflow_y);
  524. }
  525. int Element::GetPosition()
  526. {
  527. return style->GetPosition();
  528. }
  529. int Element::GetFloat()
  530. {
  531. return style->GetFloat();
  532. }
  533. int Element::GetDisplay()
  534. {
  535. return style->GetDisplay();
  536. }
  537. int Element::GetWhitespace()
  538. {
  539. return style->GetWhitespace();
  540. }
  541. int Element::GetPointerEvents()
  542. {
  543. return style->GetPointerEvents();
  544. }
  545. const Property *Element::GetLineHeightProperty()
  546. {
  547. return style->GetLineHeightProperty();
  548. }
  549. int Element::GetTextAlign()
  550. {
  551. return style->GetTextAlign();
  552. }
  553. int Element::GetTextTransform()
  554. {
  555. return style->GetTextTransform();
  556. }
  557. const Property *Element::GetVerticalAlignProperty()
  558. {
  559. return style->GetVerticalAlignProperty();
  560. }
  561. // Returns 'perspective' property value from element's style or local cache.
  562. const Property *Element::GetPerspective()
  563. {
  564. return style->GetPerspective();
  565. }
  566. // Returns 'perspective-origin-x' property value from element's style or local cache.
  567. const Property *Element::GetPerspectiveOriginX()
  568. {
  569. return style->GetPerspectiveOriginX();
  570. }
  571. // Returns 'perspective-origin-y' property value from element's style or local cache.
  572. const Property *Element::GetPerspectiveOriginY()
  573. {
  574. return style->GetPerspectiveOriginY();
  575. }
  576. // Returns 'transform' property value from element's style or local cache.
  577. const Property *Element::GetTransform()
  578. {
  579. return style->GetTransform();
  580. }
  581. // Returns 'transform-origin-x' property value from element's style or local cache.
  582. const Property *Element::GetTransformOriginX()
  583. {
  584. return style->GetTransformOriginX();
  585. }
  586. // Returns 'transform-origin-y' property value from element's style or local cache.
  587. const Property *Element::GetTransformOriginY()
  588. {
  589. return style->GetTransformOriginY();
  590. }
  591. // Returns 'transform-origin-z' property value from element's style or local cache.
  592. const Property *Element::GetTransformOriginZ()
  593. {
  594. return style->GetTransformOriginZ();
  595. }
  596. // Returns this element's TransformState
  597. const TransformState *Element::GetTransformState() const noexcept
  598. {
  599. return transform_state.get();
  600. }
  601. // Returns the TransformStates that are effective for this element.
  602. void Element::GetEffectiveTransformState(
  603. const TransformState **local_perspective,
  604. const TransformState **perspective,
  605. const TransformState **transform
  606. ) noexcept
  607. {
  608. UpdateTransformState();
  609. if (local_perspective)
  610. {
  611. *local_perspective = 0;
  612. }
  613. if (perspective)
  614. {
  615. *perspective = 0;
  616. }
  617. if (transform)
  618. {
  619. *transform = 0;
  620. }
  621. Element *perspective_node = 0, *transform_node = 0;
  622. // Find the TransformState to use for unprojecting.
  623. if (transform_state.get() && transform_state->GetLocalPerspective(0))
  624. {
  625. if (local_perspective)
  626. {
  627. *local_perspective = transform_state.get();
  628. }
  629. }
  630. else
  631. {
  632. Element *node = 0;
  633. for (node = parent; node; node = node->parent)
  634. {
  635. if (node->transform_state.get() && node->transform_state->GetPerspective(0))
  636. {
  637. if (perspective)
  638. {
  639. *perspective = node->transform_state.get();
  640. }
  641. perspective_node = node;
  642. break;
  643. }
  644. }
  645. }
  646. // Find the TransformState to use for transforming.
  647. Element *node = 0;
  648. for (node = this; node; node = node->parent)
  649. {
  650. if (node->transform_state.get() && node->transform_state->GetRecursiveTransform(0))
  651. {
  652. if (transform)
  653. {
  654. *transform = node->transform_state.get();
  655. }
  656. transform_node = node;
  657. break;
  658. }
  659. }
  660. }
  661. // Project a 2D point in pixel coordinates onto the element's plane.
  662. const Vector2f Element::Project(const Vector2f& point) noexcept
  663. {
  664. UpdateTransformState();
  665. Context *context = GetContext();
  666. if (!context)
  667. {
  668. return point;
  669. }
  670. const TransformState *local_perspective, *perspective, *transform;
  671. GetEffectiveTransformState(&local_perspective, &perspective, &transform);
  672. Vector2i view_pos(0, 0);
  673. Vector2i view_size = context->GetDimensions();
  674. // Compute the line segment for ray picking, one point on the near and one on the far plane.
  675. // These need to be in clip space coordinates ([-1; 1]³) so that we an unproject them.
  676. Vector3f line_segment[2] =
  677. {
  678. // When unprojected, the intersection point on the near plane
  679. Vector3f(
  680. (point.x - view_pos.x) / (0.5f * view_size.x) - 1.0f,
  681. (view_size.y - point.y - view_pos.y) / (0.5f * view_size.y) - 1.0f,
  682. -1
  683. ),
  684. // When unprojected, the intersection point on the far plane
  685. Vector3f(
  686. (point.x - view_pos.x) / (0.5f * view_size.x) - 1.0f,
  687. (view_size.y - point.y - view_pos.y) / (0.5f * view_size.y) - 1.0f,
  688. 1
  689. )
  690. };
  691. // Find the TransformState to use for unprojecting.
  692. if (local_perspective)
  693. {
  694. TransformState::LocalPerspective the_local_perspective;
  695. local_perspective->GetLocalPerspective(&the_local_perspective);
  696. line_segment[0] = the_local_perspective.Unproject(line_segment[0]);
  697. line_segment[1] = the_local_perspective.Unproject(line_segment[1]);
  698. }
  699. else if (perspective)
  700. {
  701. TransformState::Perspective the_perspective;
  702. perspective->GetPerspective(&the_perspective);
  703. line_segment[0] = the_perspective.Unproject(line_segment[0]);
  704. line_segment[1] = the_perspective.Unproject(line_segment[1]);
  705. }
  706. else
  707. {
  708. line_segment[0] = context->GetViewState().Unproject(line_segment[0]);
  709. line_segment[1] = context->GetViewState().Unproject(line_segment[1]);
  710. }
  711. // Compute three points on the context's corners to define the element's plane.
  712. // It may seem elegant to base this computation on the element's size, but
  713. // there are elements with zero length or height.
  714. Vector3f element_rect[3] =
  715. {
  716. // Top-left corner
  717. Vector3f(0, 0, 0),
  718. // Top-right corner
  719. Vector3f((float)view_size.x, 0, 0),
  720. // Bottom-left corner
  721. Vector3f(0, (float)view_size.y, 0)
  722. };
  723. // Transform by the correct matrix
  724. if (transform)
  725. {
  726. element_rect[0] = transform->Transform(element_rect[0]);
  727. element_rect[1] = transform->Transform(element_rect[1]);
  728. element_rect[2] = transform->Transform(element_rect[2]);
  729. }
  730. Vector3f u = line_segment[0] - line_segment[1];
  731. Vector3f v = element_rect[1] - element_rect[0];
  732. Vector3f w = element_rect[2] - element_rect[0];
  733. // Now compute the intersection point of the line segment and the element's rectangle.
  734. // This is based on the algorithm discussed at Wikipedia
  735. // (http://en.wikipedia.org/wiki/Line-plane_intersection).
  736. Matrix4f A = Matrix4f::FromColumns(
  737. Vector4f(u, 0),
  738. Vector4f(v, 0),
  739. Vector4f(w, 0),
  740. Vector4f(0, 0, 0, 1)
  741. );
  742. if (A.Invert())
  743. {
  744. Vector3f factors = A * (line_segment[0] - element_rect[0]);
  745. Vector3f intersection3d = element_rect[0] + v * factors[1] + w * factors[2];
  746. Vector3f projected;
  747. if (transform)
  748. {
  749. projected = transform->Untransform(intersection3d);
  750. //ROCKET_ASSERT(fabs(projected.z) < 0.0001);
  751. }
  752. else
  753. {
  754. // FIXME: Is this correct?
  755. projected = intersection3d;
  756. }
  757. return Vector2f(projected.x, projected.y);
  758. }
  759. else
  760. {
  761. // The line segment is parallel to the element's plane.
  762. // Although, mathematically, it could also lie within the plane
  763. // (yielding infinitely many intersection points), we still
  764. // return a value that's pretty sure to not match anything,
  765. // since this case has nothing to do with the user `picking'
  766. // anything.
  767. float inf = std::numeric_limits< float >::infinity();
  768. return Vector2f(-inf, -inf);
  769. }
  770. }
  771. bool Element::Animate(const String & property_name, const Property & target_value, float duration, Tween tween, int num_iterations, bool alternate_direction, float delay)
  772. {
  773. if (delay < 0.0f)
  774. return false;
  775. ElementAnimation* animation = nullptr;
  776. for (auto& existing_animation : animations)
  777. {
  778. if (existing_animation.GetPropertyName() == property_name)
  779. {
  780. animation = &existing_animation;
  781. break;
  782. }
  783. }
  784. float target_time = duration;
  785. if (!animation)
  786. {
  787. float start_time = Clock::GetElapsedTime() + delay;
  788. const Property* property = GetProperty(property_name);
  789. if (!property || !property->definition)
  790. return false;
  791. animations.push_back(
  792. ElementAnimation{ property_name, *property, start_time, duration, num_iterations, alternate_direction }
  793. );
  794. animation = &animations.back();
  795. }
  796. else
  797. {
  798. target_time += animation->GetDuration();
  799. animation->SetDuration(target_time);
  800. }
  801. bool result = false;
  802. if(animation)
  803. {
  804. result = animation->AddKey(target_time, target_value, *this, tween);
  805. }
  806. return result;
  807. }
  808. // Iterates over the properties defined on this element.
  809. bool Element::IterateProperties(int& index, PseudoClassList& pseudo_classes, String& name, const Property*& property) const
  810. {
  811. return style->IterateProperties(index, pseudo_classes, name, property);
  812. }
  813. // Sets or removes a pseudo-class on the element.
  814. void Element::SetPseudoClass(const String& pseudo_class, bool activate)
  815. {
  816. style->SetPseudoClass(pseudo_class, activate);
  817. }
  818. // Checks if a specific pseudo-class has been set on the element.
  819. bool Element::IsPseudoClassSet(const String& pseudo_class) const
  820. {
  821. return style->IsPseudoClassSet(pseudo_class);
  822. }
  823. // Checks if a complete set of pseudo-classes are set on the element.
  824. bool Element::ArePseudoClassesSet(const PseudoClassList& pseudo_classes) const
  825. {
  826. for (PseudoClassList::const_iterator i = pseudo_classes.begin(); i != pseudo_classes.end(); ++i)
  827. {
  828. if (!IsPseudoClassSet(*i))
  829. return false;
  830. }
  831. return true;
  832. }
  833. // Gets a list of the current active pseudo classes
  834. const PseudoClassList& Element::GetActivePseudoClasses() const
  835. {
  836. return style->GetActivePseudoClasses();
  837. }
  838. /// Get the named attribute
  839. Variant* Element::GetAttribute(const String& name) const
  840. {
  841. return attributes.Get(name);
  842. }
  843. // Checks if the element has a certain attribute.
  844. bool Element::HasAttribute(const String& name)
  845. {
  846. return attributes.Get(name) != NULL;
  847. }
  848. // Removes an attribute from the element
  849. void Element::RemoveAttribute(const String& name)
  850. {
  851. if (attributes.Remove(name))
  852. {
  853. AttributeNameList changed_attributes;
  854. changed_attributes.insert(name);
  855. OnAttributeChange(changed_attributes);
  856. }
  857. }
  858. // Gets the outer most focus element down the tree from this node
  859. Element* Element::GetFocusLeafNode()
  860. {
  861. // If there isn't a focus, then we are the leaf.
  862. if (!focus)
  863. {
  864. return this;
  865. }
  866. // Recurse down the tree until we found the leaf focus element
  867. Element* focus_element = focus;
  868. while (focus_element->focus)
  869. focus_element = focus_element->focus;
  870. return focus_element;
  871. }
  872. // Returns the element's context.
  873. Context* Element::GetContext()
  874. {
  875. ElementDocument* document = GetOwnerDocument();
  876. if (document != NULL)
  877. return document->GetContext();
  878. return NULL;
  879. }
  880. // Set a group of attributes
  881. void Element::SetAttributes(const ElementAttributes* _attributes)
  882. {
  883. int index = 0;
  884. String key;
  885. Variant* value;
  886. AttributeNameList changed_attributes;
  887. while (_attributes->Iterate(index, key, value))
  888. {
  889. changed_attributes.insert(key);
  890. attributes.Set(key, *value);
  891. }
  892. OnAttributeChange(changed_attributes);
  893. }
  894. // Returns the number of attributes on the element.
  895. int Element::GetNumAttributes() const
  896. {
  897. return attributes.Size();
  898. }
  899. // Iterates over all decorators attached to the element.
  900. bool Element::IterateDecorators(int& index, PseudoClassList& pseudo_classes, String& name, Decorator*& decorator, DecoratorDataHandle& decorator_data)
  901. {
  902. return decoration->IterateDecorators(index, pseudo_classes, name, decorator, decorator_data);
  903. }
  904. // Gets the name of the element.
  905. const String& Element::GetTagName() const
  906. {
  907. return tag;
  908. }
  909. // Gets the ID of the element.
  910. const String& Element::GetId() const
  911. {
  912. return id;
  913. }
  914. // Sets the ID of the element.
  915. void Element::SetId(const String& _id)
  916. {
  917. SetAttribute("id", _id);
  918. }
  919. // Gets the horizontal offset from the context's left edge to element's left border edge.
  920. float Element::GetAbsoluteLeft()
  921. {
  922. return GetAbsoluteOffset(Box::BORDER).x;
  923. }
  924. // Gets the vertical offset from the context's top edge to element's top border edge.
  925. float Element::GetAbsoluteTop()
  926. {
  927. return GetAbsoluteOffset(Box::BORDER).y;
  928. }
  929. // Gets the width of the left border of an element.
  930. float Element::GetClientLeft()
  931. {
  932. UpdateLayout();
  933. return GetBox().GetPosition(client_area).x;
  934. }
  935. // Gets the height of the top border of an element.
  936. float Element::GetClientTop()
  937. {
  938. UpdateLayout();
  939. return GetBox().GetPosition(client_area).y;
  940. }
  941. // Gets the inner width of the element.
  942. float Element::GetClientWidth()
  943. {
  944. UpdateLayout();
  945. return GetBox().GetSize(client_area).x - scroll->GetScrollbarSize(ElementScroll::VERTICAL);
  946. }
  947. // Gets the inner height of the element.
  948. float Element::GetClientHeight()
  949. {
  950. UpdateLayout();
  951. return GetBox().GetSize(client_area).y - scroll->GetScrollbarSize(ElementScroll::HORIZONTAL);
  952. }
  953. // Returns the element from which all offset calculations are currently computed.
  954. Element* Element::GetOffsetParent()
  955. {
  956. return offset_parent;
  957. }
  958. // Gets the distance from this element's left border to its offset parent's left border.
  959. float Element::GetOffsetLeft()
  960. {
  961. UpdateLayout();
  962. return relative_offset_base.x + relative_offset_position.x;
  963. }
  964. // Gets the distance from this element's top border to its offset parent's top border.
  965. float Element::GetOffsetTop()
  966. {
  967. UpdateLayout();
  968. return relative_offset_base.y + relative_offset_position.y;
  969. }
  970. // Gets the width of the element, including the client area, padding, borders and scrollbars, but not margins.
  971. float Element::GetOffsetWidth()
  972. {
  973. UpdateLayout();
  974. return GetBox().GetSize(Box::BORDER).x;
  975. }
  976. // Gets the height of the element, including the client area, padding, borders and scrollbars, but not margins.
  977. float Element::GetOffsetHeight()
  978. {
  979. UpdateLayout();
  980. return GetBox().GetSize(Box::BORDER).y;
  981. }
  982. // Gets the left scroll offset of the element.
  983. float Element::GetScrollLeft()
  984. {
  985. UpdateLayout();
  986. return scroll_offset.x;
  987. }
  988. // Sets the left scroll offset of the element.
  989. void Element::SetScrollLeft(float scroll_left)
  990. {
  991. scroll_offset.x = Math::Clamp(scroll_left, 0.0f, GetScrollWidth() - GetClientWidth());
  992. scroll->UpdateScrollbar(ElementScroll::HORIZONTAL);
  993. DirtyOffset();
  994. DispatchEvent("scroll", Dictionary());
  995. }
  996. // Gets the top scroll offset of the element.
  997. float Element::GetScrollTop()
  998. {
  999. UpdateLayout();
  1000. return scroll_offset.y;
  1001. }
  1002. // Sets the top scroll offset of the element.
  1003. void Element::SetScrollTop(float scroll_top)
  1004. {
  1005. scroll_offset.y = Math::Clamp(scroll_top, 0.0f, GetScrollHeight() - GetClientHeight());
  1006. scroll->UpdateScrollbar(ElementScroll::VERTICAL);
  1007. DirtyOffset();
  1008. DispatchEvent("scroll", Dictionary());
  1009. }
  1010. // Gets the width of the scrollable content of the element; it includes the element padding but not its margin.
  1011. float Element::GetScrollWidth()
  1012. {
  1013. return Math::Max(content_box.x, GetClientWidth());
  1014. }
  1015. // Gets the height of the scrollable content of the element; it includes the element padding but not its margin.
  1016. float Element::GetScrollHeight()
  1017. {
  1018. return Math::Max(content_box.y, GetClientHeight());
  1019. }
  1020. // Gets the object representing the declarations of an element's style attributes.
  1021. ElementStyle* Element::GetStyle()
  1022. {
  1023. return style;
  1024. }
  1025. // Gets the document this element belongs to.
  1026. ElementDocument* Element::GetOwnerDocument()
  1027. {
  1028. if (parent == NULL)
  1029. return NULL;
  1030. if (!owner_document)
  1031. {
  1032. owner_document = parent->GetOwnerDocument();
  1033. }
  1034. return owner_document;
  1035. }
  1036. // Gets this element's parent node.
  1037. Element* Element::GetParentNode() const
  1038. {
  1039. return parent;
  1040. }
  1041. // Gets the element immediately following this one in the tree.
  1042. Element* Element::GetNextSibling() const
  1043. {
  1044. if (parent == NULL)
  1045. return NULL;
  1046. for (size_t i = 0; i < parent->children.size() - (parent->num_non_dom_children + 1); i++)
  1047. {
  1048. if (parent->children[i] == this)
  1049. return parent->children[i + 1];
  1050. }
  1051. return NULL;
  1052. }
  1053. // Gets the element immediately preceding this one in the tree.
  1054. Element* Element::GetPreviousSibling() const
  1055. {
  1056. if (parent == NULL)
  1057. return NULL;
  1058. for (size_t i = 1; i < parent->children.size() - parent->num_non_dom_children; i++)
  1059. {
  1060. if (parent->children[i] == this)
  1061. return parent->children[i - 1];
  1062. }
  1063. return NULL;
  1064. }
  1065. // Returns the first child of this element.
  1066. Element* Element::GetFirstChild() const
  1067. {
  1068. if (GetNumChildren() > 0)
  1069. return children[0];
  1070. return NULL;
  1071. }
  1072. // Gets the last child of this element.
  1073. Element* Element::GetLastChild() const
  1074. {
  1075. if (GetNumChildren() > 0)
  1076. return *(children.end() - (num_non_dom_children + 1));
  1077. return NULL;
  1078. }
  1079. Element* Element::GetChild(int index) const
  1080. {
  1081. if (index < 0 || index >= (int) children.size())
  1082. return NULL;
  1083. return children[index];
  1084. }
  1085. int Element::GetNumChildren(bool include_non_dom_elements) const
  1086. {
  1087. return (int) children.size() - (include_non_dom_elements ? 0 : num_non_dom_children);
  1088. }
  1089. // Gets the markup and content of the element.
  1090. void Element::GetInnerRML(String& content) const
  1091. {
  1092. for (int i = 0; i < GetNumChildren(); i++)
  1093. {
  1094. children[i]->GetRML(content);
  1095. }
  1096. }
  1097. // Gets the markup and content of the element.
  1098. String Element::GetInnerRML() const {
  1099. String result;
  1100. GetInnerRML(result);
  1101. return result;
  1102. }
  1103. // Sets the markup and content of the element. All existing children will be replaced.
  1104. void Element::SetInnerRML(const String& rml)
  1105. {
  1106. // Remove all DOM children.
  1107. while ((int) children.size() > num_non_dom_children)
  1108. RemoveChild(children.front());
  1109. Factory::InstanceElementText(this, rml);
  1110. }
  1111. // Sets the current element as the focus object.
  1112. bool Element::Focus()
  1113. {
  1114. // Are we allowed focus?
  1115. int focus_property = GetProperty< int >(FOCUS);
  1116. if (focus_property == FOCUS_NONE)
  1117. return false;
  1118. // Ask our context if we can switch focus.
  1119. Context* context = GetContext();
  1120. if (context == NULL)
  1121. return false;
  1122. if (!context->OnFocusChange(this))
  1123. return false;
  1124. // Set this as the end of the focus chain.
  1125. focus = NULL;
  1126. // Update the focus chain up the hierarchy.
  1127. Element* element = this;
  1128. while (element->GetParentNode())
  1129. {
  1130. element->GetParentNode()->focus = element;
  1131. element = element->GetParentNode();
  1132. }
  1133. return true;
  1134. }
  1135. // Removes focus from from this element.
  1136. void Element::Blur()
  1137. {
  1138. if (parent)
  1139. {
  1140. Context* context = GetContext();
  1141. if (context == NULL)
  1142. return;
  1143. if (context->GetFocusElement() == this)
  1144. {
  1145. parent->Focus();
  1146. }
  1147. else if (parent->focus == this)
  1148. {
  1149. parent->focus = NULL;
  1150. }
  1151. }
  1152. }
  1153. // Fakes a mouse click on this element.
  1154. void Element::Click()
  1155. {
  1156. Context* context = GetContext();
  1157. if (context == NULL)
  1158. return;
  1159. context->GenerateClickEvent(this);
  1160. }
  1161. // Adds an event listener
  1162. void Element::AddEventListener(const String& event, EventListener* listener, bool in_capture_phase)
  1163. {
  1164. event_dispatcher->AttachEvent(event, listener, in_capture_phase);
  1165. }
  1166. // Removes an event listener from this element.
  1167. void Element::RemoveEventListener(const String& event, EventListener* listener, bool in_capture_phase)
  1168. {
  1169. event_dispatcher->DetachEvent(event, listener, in_capture_phase);
  1170. }
  1171. // Dispatches the specified event
  1172. bool Element::DispatchEvent(const String& event, const Dictionary& parameters, bool interruptible)
  1173. {
  1174. return event_dispatcher->DispatchEvent(this, event, parameters, interruptible);
  1175. }
  1176. // Scrolls the parent element's contents so that this element is visible.
  1177. void Element::ScrollIntoView(bool align_with_top)
  1178. {
  1179. Vector2f size(0, 0);
  1180. if (!align_with_top &&
  1181. !boxes.empty())
  1182. {
  1183. size.y = boxes.back().GetOffset().y +
  1184. boxes.back().GetSize(Box::BORDER).y;
  1185. }
  1186. Element* scroll_parent = parent;
  1187. while (scroll_parent != NULL)
  1188. {
  1189. int overflow_x_property = scroll_parent->GetProperty< int >(OVERFLOW_X);
  1190. int overflow_y_property = scroll_parent->GetProperty< int >(OVERFLOW_Y);
  1191. if ((overflow_x_property != OVERFLOW_VISIBLE &&
  1192. scroll_parent->GetScrollWidth() > scroll_parent->GetClientWidth()) ||
  1193. (overflow_y_property != OVERFLOW_VISIBLE &&
  1194. scroll_parent->GetScrollHeight() > scroll_parent->GetClientHeight()))
  1195. {
  1196. Vector2f offset = scroll_parent->GetAbsoluteOffset(Box::BORDER) - GetAbsoluteOffset(Box::BORDER);
  1197. Vector2f scroll_offset(scroll_parent->GetScrollLeft(), scroll_parent->GetScrollTop());
  1198. scroll_offset -= offset;
  1199. scroll_offset.x += scroll_parent->GetClientLeft();
  1200. scroll_offset.y += scroll_parent->GetClientTop();
  1201. if (!align_with_top)
  1202. scroll_offset.y -= (scroll_parent->GetClientHeight() - size.y);
  1203. if (overflow_x_property != OVERFLOW_VISIBLE)
  1204. scroll_parent->SetScrollLeft(scroll_offset.x);
  1205. if (overflow_y_property != OVERFLOW_VISIBLE)
  1206. scroll_parent->SetScrollTop(scroll_offset.y);
  1207. }
  1208. scroll_parent = scroll_parent->GetParentNode();
  1209. }
  1210. }
  1211. // Appends a child to this element
  1212. void Element::AppendChild(Element* child, bool dom_element)
  1213. {
  1214. LockLayout(true);
  1215. child->AddReference();
  1216. child->SetParent(this);
  1217. if (dom_element)
  1218. children.insert(children.end() - num_non_dom_children, child);
  1219. else
  1220. {
  1221. children.push_back(child);
  1222. num_non_dom_children++;
  1223. }
  1224. child->GetStyle()->DirtyDefinition();
  1225. child->GetStyle()->DirtyProperties();
  1226. child->OnChildAdd(child);
  1227. DirtyStackingContext();
  1228. DirtyStructure();
  1229. if (dom_element)
  1230. DirtyLayout();
  1231. LockLayout(false);
  1232. }
  1233. // Adds a child to this element, directly after the adjacent element. Inherits
  1234. // the dom/non-dom status from the adjacent element.
  1235. void Element::InsertBefore(Element* child, Element* adjacent_element)
  1236. {
  1237. // Find the position in the list of children of the adjacent element. If
  1238. // it's NULL or we can't find it, then we insert it at the end of the dom
  1239. // children, as a dom element.
  1240. size_t child_index = 0;
  1241. bool found_child = false;
  1242. if (adjacent_element)
  1243. {
  1244. for (child_index = 0; child_index < children.size(); child_index++)
  1245. {
  1246. if (children[child_index] == adjacent_element)
  1247. {
  1248. found_child = true;
  1249. break;
  1250. }
  1251. }
  1252. }
  1253. if (found_child)
  1254. {
  1255. LockLayout(true);
  1256. child->AddReference();
  1257. child->SetParent(this);
  1258. if ((int) child_index >= GetNumChildren())
  1259. num_non_dom_children++;
  1260. else
  1261. DirtyLayout();
  1262. children.insert(children.begin() + child_index, child);
  1263. child->GetStyle()->DirtyDefinition();
  1264. child->GetStyle()->DirtyProperties();
  1265. child->OnChildAdd(child);
  1266. DirtyStackingContext();
  1267. DirtyStructure();
  1268. LockLayout(false);
  1269. }
  1270. else
  1271. {
  1272. AppendChild(child);
  1273. }
  1274. }
  1275. // Replaces the second node with the first node.
  1276. bool Element::ReplaceChild(Element* inserted_element, Element* replaced_element)
  1277. {
  1278. inserted_element->AddReference();
  1279. inserted_element->SetParent(this);
  1280. ElementList::iterator insertion_point = children.begin();
  1281. while (insertion_point != children.end() && *insertion_point != replaced_element)
  1282. {
  1283. ++insertion_point;
  1284. }
  1285. if (insertion_point == children.end())
  1286. {
  1287. AppendChild(inserted_element);
  1288. return false;
  1289. }
  1290. LockLayout(true);
  1291. children.insert(insertion_point, inserted_element);
  1292. RemoveChild(replaced_element);
  1293. inserted_element->GetStyle()->DirtyDefinition();
  1294. inserted_element->GetStyle()->DirtyProperties();
  1295. inserted_element->OnChildAdd(inserted_element);
  1296. LockLayout(false);
  1297. return true;
  1298. }
  1299. // Removes the specified child
  1300. bool Element::RemoveChild(Element* child)
  1301. {
  1302. size_t child_index = 0;
  1303. for (ElementList::iterator itr = children.begin(); itr != children.end(); ++itr)
  1304. {
  1305. // Add the element to the delete list
  1306. if ((*itr) == child)
  1307. {
  1308. LockLayout(true);
  1309. // Inform the context of the element's pending removal (if we have a valid context).
  1310. Context* context = GetContext();
  1311. if (context)
  1312. context->OnElementRemove(child);
  1313. child->OnChildRemove(child);
  1314. if (child_index >= children.size() - num_non_dom_children)
  1315. num_non_dom_children--;
  1316. deleted_children.push_back(child);
  1317. children.erase(itr);
  1318. // Remove the child element as the focussed child of this element.
  1319. if (child == focus)
  1320. {
  1321. focus = NULL;
  1322. // If this child (or a descendant of this child) is the context's currently
  1323. // focussed element, set the focus to us instead.
  1324. Context* context = GetContext();
  1325. if (context != NULL)
  1326. {
  1327. Element* focus_element = context->GetFocusElement();
  1328. while (focus_element != NULL)
  1329. {
  1330. if (focus_element == child)
  1331. {
  1332. Focus();
  1333. break;
  1334. }
  1335. focus_element = focus_element->GetParentNode();
  1336. }
  1337. }
  1338. }
  1339. DirtyLayout();
  1340. DirtyStackingContext();
  1341. DirtyStructure();
  1342. LockLayout(false);
  1343. return true;
  1344. }
  1345. child_index++;
  1346. }
  1347. return false;
  1348. }
  1349. bool Element::HasChildNodes() const
  1350. {
  1351. return (int) children.size() > num_non_dom_children;
  1352. }
  1353. Element* Element::GetElementById(const String& id)
  1354. {
  1355. // Check for special-case tokens.
  1356. if (id == "#self")
  1357. return this;
  1358. else if (id == "#document")
  1359. return GetOwnerDocument();
  1360. else if (id == "#parent")
  1361. return this->parent;
  1362. else
  1363. {
  1364. Element* search_root = GetOwnerDocument();
  1365. if (search_root == NULL)
  1366. search_root = this;
  1367. return ElementUtilities::GetElementById(search_root, id);
  1368. }
  1369. }
  1370. // Get all elements with the given tag.
  1371. void Element::GetElementsByTagName(ElementList& elements, const String& tag)
  1372. {
  1373. return ElementUtilities::GetElementsByTagName(elements, this, tag);
  1374. }
  1375. // Get all elements with the given class set on them.
  1376. void Element::GetElementsByClassName(ElementList& elements, const String& class_name)
  1377. {
  1378. return ElementUtilities::GetElementsByClassName(elements, this, class_name);
  1379. }
  1380. // Access the event dispatcher
  1381. EventDispatcher* Element::GetEventDispatcher() const
  1382. {
  1383. return event_dispatcher;
  1384. }
  1385. // Access the element background.
  1386. ElementBackground* Element::GetElementBackground() const
  1387. {
  1388. return background;
  1389. }
  1390. // Access the element border.
  1391. ElementBorder* Element::GetElementBorder() const
  1392. {
  1393. return border;
  1394. }
  1395. // Access the element decorators
  1396. ElementDecoration* Element::GetElementDecoration() const
  1397. {
  1398. return decoration;
  1399. }
  1400. // Returns the element's scrollbar functionality.
  1401. ElementScroll* Element::GetElementScroll() const
  1402. {
  1403. return scroll;
  1404. }
  1405. int Element::GetClippingIgnoreDepth()
  1406. {
  1407. if (clipping_state_dirty)
  1408. {
  1409. IsClippingEnabled();
  1410. }
  1411. return clipping_ignore_depth;
  1412. }
  1413. bool Element::IsClippingEnabled()
  1414. {
  1415. if (clipping_state_dirty)
  1416. {
  1417. // Is clipping enabled for this element, yes unless both overlow properties are set to visible
  1418. clipping_enabled = style->GetProperty(OVERFLOW_X)->Get< int >() != OVERFLOW_VISIBLE
  1419. || style->GetProperty(OVERFLOW_Y)->Get< int >() != OVERFLOW_VISIBLE;
  1420. // Get the clipping ignore depth from the clip property
  1421. clipping_ignore_depth = 0;
  1422. const Property* clip_property = GetProperty(CLIP);
  1423. if (clip_property->unit == Property::NUMBER)
  1424. clipping_ignore_depth = clip_property->Get< int >();
  1425. else if (clip_property->Get< int >() == CLIP_NONE)
  1426. clipping_ignore_depth = -1;
  1427. clipping_state_dirty = false;
  1428. }
  1429. return clipping_enabled;
  1430. }
  1431. // Gets the render interface owned by this element's context.
  1432. RenderInterface* Element::GetRenderInterface()
  1433. {
  1434. Context* context = GetContext();
  1435. if (context != NULL)
  1436. return context->GetRenderInterface();
  1437. return Rocket::Core::GetRenderInterface();
  1438. }
  1439. void Element::SetInstancer(ElementInstancer* _instancer)
  1440. {
  1441. // Only record the first instancer being set as some instancers call other instancers to do their dirty work, in
  1442. // which case we don't want to update the lowest level instancer.
  1443. if (instancer == NULL)
  1444. {
  1445. instancer = _instancer;
  1446. instancer->AddReference();
  1447. }
  1448. }
  1449. // Forces the element to generate a local stacking context, regardless of the value of its z-index property.
  1450. void Element::ForceLocalStackingContext()
  1451. {
  1452. local_stacking_context_forced = true;
  1453. local_stacking_context = true;
  1454. DirtyStackingContext();
  1455. }
  1456. // Called during the update loop after children are rendered.
  1457. void Element::OnUpdate()
  1458. {
  1459. }
  1460. // Called during render after backgrounds, borders, decorators, but before children, are rendered.
  1461. void Element::OnRender()
  1462. {
  1463. }
  1464. // Called during a layout operation, when the element is being positioned and sized.
  1465. void Element::OnLayout()
  1466. {
  1467. }
  1468. // Called when attributes on the element are changed.
  1469. void Element::OnAttributeChange(const AttributeNameList& changed_attributes)
  1470. {
  1471. if (changed_attributes.find("id") != changed_attributes.end())
  1472. {
  1473. id = GetAttribute< String >("id", "");
  1474. style->DirtyDefinition();
  1475. }
  1476. if (changed_attributes.find("class") != changed_attributes.end())
  1477. {
  1478. style->SetClassNames(GetAttribute< String >("class", ""));
  1479. }
  1480. // Add any inline style declarations.
  1481. if (changed_attributes.find("style") != changed_attributes.end())
  1482. {
  1483. PropertyDictionary properties;
  1484. StyleSheetParser parser;
  1485. parser.ParseProperties(properties, GetAttribute< String >("style", ""));
  1486. Rocket::Core::PropertyMap property_map = properties.GetProperties();
  1487. for (Rocket::Core::PropertyMap::iterator i = property_map.begin(); i != property_map.end(); ++i)
  1488. {
  1489. SetProperty((*i).first, (*i).second);
  1490. }
  1491. }
  1492. }
  1493. // Called when properties on the element are changed.
  1494. void Element::OnPropertyChange(const PropertyNameList& changed_properties)
  1495. {
  1496. bool all_dirty = StyleSheetSpecification::GetRegisteredProperties() == changed_properties;
  1497. if (!IsLayoutDirty())
  1498. {
  1499. if (all_dirty)
  1500. {
  1501. DirtyLayout();
  1502. }
  1503. else
  1504. {
  1505. // Force a relayout if any of the changed properties require it.
  1506. for (PropertyNameList::const_iterator i = changed_properties.begin(); i != changed_properties.end(); ++i)
  1507. {
  1508. const PropertyDefinition* property_definition = StyleSheetSpecification::GetProperty(*i);
  1509. if (property_definition)
  1510. {
  1511. if (property_definition->IsLayoutForced())
  1512. {
  1513. DirtyLayout();
  1514. break;
  1515. }
  1516. }
  1517. }
  1518. }
  1519. }
  1520. // Update the visibility.
  1521. if (all_dirty || changed_properties.find(VISIBILITY) != changed_properties.end() ||
  1522. changed_properties.find(DISPLAY) != changed_properties.end())
  1523. {
  1524. bool new_visibility = GetDisplay() != DISPLAY_NONE &&
  1525. GetProperty< int >(VISIBILITY) == VISIBILITY_VISIBLE;
  1526. if (visible != new_visibility)
  1527. {
  1528. visible = new_visibility;
  1529. if (parent != NULL)
  1530. parent->DirtyStackingContext();
  1531. }
  1532. if (all_dirty ||
  1533. changed_properties.find(DISPLAY) != changed_properties.end())
  1534. {
  1535. if (parent != NULL)
  1536. parent->DirtyStructure();
  1537. }
  1538. }
  1539. // Update the position.
  1540. if (all_dirty ||
  1541. changed_properties.find(LEFT) != changed_properties.end() ||
  1542. changed_properties.find(RIGHT) != changed_properties.end() ||
  1543. changed_properties.find(TOP) != changed_properties.end() ||
  1544. changed_properties.find(BOTTOM) != changed_properties.end())
  1545. {
  1546. UpdateOffset();
  1547. DirtyOffset();
  1548. }
  1549. // Update the z-index.
  1550. if (all_dirty ||
  1551. changed_properties.find(Z_INDEX) != changed_properties.end())
  1552. {
  1553. const Property* z_index_property = GetProperty(Z_INDEX);
  1554. if (z_index_property->unit == Property::KEYWORD &&
  1555. z_index_property->value.Get< int >() == Z_INDEX_AUTO)
  1556. {
  1557. if (local_stacking_context &&
  1558. !local_stacking_context_forced)
  1559. {
  1560. // We're no longer acting as a stacking context.
  1561. local_stacking_context = false;
  1562. stacking_context_dirty = false;
  1563. stacking_context.clear();
  1564. }
  1565. // If our old z-index was not zero, then we must dirty our stacking context so we'll be re-indexed.
  1566. if (z_index != 0)
  1567. {
  1568. z_index = 0;
  1569. DirtyStackingContext();
  1570. }
  1571. }
  1572. else
  1573. {
  1574. float new_z_index;
  1575. if (z_index_property->unit == Property::KEYWORD)
  1576. {
  1577. if (z_index_property->value.Get< int >() == Z_INDEX_TOP)
  1578. new_z_index = FLT_MAX;
  1579. else
  1580. new_z_index = -FLT_MAX;
  1581. }
  1582. else
  1583. new_z_index = z_index_property->value.Get< float >();
  1584. if (new_z_index != z_index)
  1585. {
  1586. z_index = new_z_index;
  1587. if (parent != NULL)
  1588. parent->DirtyStackingContext();
  1589. }
  1590. if (!local_stacking_context)
  1591. {
  1592. local_stacking_context = true;
  1593. stacking_context_dirty = true;
  1594. }
  1595. }
  1596. }
  1597. // Dirty the background if it's changed.
  1598. if (all_dirty ||
  1599. changed_properties.find(BACKGROUND_COLOR) != changed_properties.end() ||
  1600. changed_properties.find(OPACITY) != changed_properties.end() ||
  1601. changed_properties.find(IMAGE_COLOR) != changed_properties.end()) {
  1602. background->DirtyBackground();
  1603. decoration->ReloadDecorators();
  1604. }
  1605. // Dirty the border if it's changed.
  1606. if (all_dirty ||
  1607. changed_properties.find(BORDER_TOP_WIDTH) != changed_properties.end() ||
  1608. changed_properties.find(BORDER_RIGHT_WIDTH) != changed_properties.end() ||
  1609. changed_properties.find(BORDER_BOTTOM_WIDTH) != changed_properties.end() ||
  1610. changed_properties.find(BORDER_LEFT_WIDTH) != changed_properties.end() ||
  1611. changed_properties.find(BORDER_TOP_COLOR) != changed_properties.end() ||
  1612. changed_properties.find(BORDER_RIGHT_COLOR) != changed_properties.end() ||
  1613. changed_properties.find(BORDER_BOTTOM_COLOR) != changed_properties.end() ||
  1614. changed_properties.find(BORDER_LEFT_COLOR) != changed_properties.end() ||
  1615. changed_properties.find(OPACITY) != changed_properties.end())
  1616. border->DirtyBorder();
  1617. // Fetch a new font face if it has been changed.
  1618. if (all_dirty ||
  1619. changed_properties.find(FONT_FAMILY) != changed_properties.end() ||
  1620. changed_properties.find(FONT_CHARSET) != changed_properties.end() ||
  1621. changed_properties.find(FONT_WEIGHT) != changed_properties.end() ||
  1622. changed_properties.find(FONT_STYLE) != changed_properties.end() ||
  1623. changed_properties.find(FONT_SIZE) != changed_properties.end())
  1624. {
  1625. // Store the old em; if it changes, then we need to dirty all em-relative properties.
  1626. int old_em = -1;
  1627. if (font_face_handle != NULL)
  1628. old_em = font_face_handle->GetLineHeight();
  1629. // Fetch the new font face.
  1630. FontFaceHandle* new_font_face_handle = ElementUtilities::GetFontFaceHandle(this);
  1631. // If this is different from our current font face, then we've got to nuke
  1632. // all our characters and tell our parent that we have to be re-laid out.
  1633. if (new_font_face_handle != font_face_handle)
  1634. {
  1635. if (font_face_handle)
  1636. font_face_handle->RemoveReference();
  1637. font_face_handle = new_font_face_handle;
  1638. // Our font face has changed; odds are, so has our em. All of our em-relative values
  1639. // have therefore probably changed as well, so we'll need to dirty them.
  1640. int new_em = -1;
  1641. if (font_face_handle != NULL)
  1642. new_em = font_face_handle->GetLineHeight();
  1643. if (old_em != new_em)
  1644. {
  1645. style->DirtyEmProperties();
  1646. }
  1647. }
  1648. else if (new_font_face_handle != NULL)
  1649. new_font_face_handle->RemoveReference();
  1650. }
  1651. // Check for clipping state changes
  1652. if (all_dirty ||
  1653. changed_properties.find(CLIP) != changed_properties.end() ||
  1654. changed_properties.find(OVERFLOW_X) != changed_properties.end() ||
  1655. changed_properties.find(OVERFLOW_Y) != changed_properties.end())
  1656. {
  1657. clipping_state_dirty = true;
  1658. }
  1659. // Check for `perspective' and `perspective-origin' changes
  1660. if (all_dirty ||
  1661. changed_properties.find(PERSPECTIVE) != changed_properties.end() ||
  1662. changed_properties.find(PERSPECTIVE_ORIGIN_X) != changed_properties.end() ||
  1663. changed_properties.find(PERSPECTIVE_ORIGIN_Y) != changed_properties.end())
  1664. {
  1665. DirtyTransformState(true, false, false);
  1666. }
  1667. // Check for `transform' and `transform-origin' changes
  1668. if (all_dirty ||
  1669. changed_properties.find(TRANSFORM) != changed_properties.end() ||
  1670. changed_properties.find(TRANSFORM_ORIGIN_X) != changed_properties.end() ||
  1671. changed_properties.find(TRANSFORM_ORIGIN_Y) != changed_properties.end() ||
  1672. changed_properties.find(TRANSFORM_ORIGIN_Z) != changed_properties.end())
  1673. {
  1674. DirtyTransformState(false, true, false);
  1675. }
  1676. }
  1677. // Called when a child node has been added somewhere in the hierarchy
  1678. void Element::OnChildAdd(Element* child)
  1679. {
  1680. if (parent)
  1681. parent->OnChildAdd(child);
  1682. }
  1683. // Called when a child node has been removed somewhere in the hierarchy
  1684. void Element::OnChildRemove(Element* child)
  1685. {
  1686. if (parent)
  1687. parent->OnChildRemove(child);
  1688. }
  1689. // Update the element's layout if required.
  1690. void Element::UpdateLayout()
  1691. {
  1692. ElementDocument* document = GetOwnerDocument();
  1693. if (document != NULL)
  1694. document->UpdateLayout();
  1695. }
  1696. // Forces a re-layout of this element, and any other children required.
  1697. void Element::DirtyLayout()
  1698. {
  1699. Element* document = GetOwnerDocument();
  1700. if (document != NULL)
  1701. document->DirtyLayout();
  1702. }
  1703. /// Increment/Decrement the layout lock
  1704. void Element::LockLayout(bool lock)
  1705. {
  1706. Element* document = GetOwnerDocument();
  1707. if (document != NULL)
  1708. document->LockLayout(lock);
  1709. }
  1710. // Forces a re-layout of this element, and any other children required.
  1711. bool Element::IsLayoutDirty()
  1712. {
  1713. Element* document = GetOwnerDocument();
  1714. if (document != NULL)
  1715. return document->IsLayoutDirty();
  1716. return false;
  1717. }
  1718. // Forces a reevaluation of applicable font effects.
  1719. void Element::DirtyFont()
  1720. {
  1721. for (size_t i = 0; i < children.size(); ++i)
  1722. children[i]->DirtyFont();
  1723. }
  1724. void Element::OnReferenceDeactivate()
  1725. {
  1726. if (instancer)
  1727. {
  1728. instancer->ReleaseElement(this);
  1729. }
  1730. else
  1731. {
  1732. // Hopefully we can just delete ourselves.
  1733. //delete this;
  1734. Log::Message(Log::LT_WARNING, "Leak detected: element %s not instanced via Rocket Factory. Unable to release.", GetAddress().CString());
  1735. }
  1736. }
  1737. void Element::ProcessEvent(Event& event)
  1738. {
  1739. if (event == MOUSEDOWN && IsPointWithinElement(Vector2f(event.GetParameter< float >("mouse_x", 0), event.GetParameter< float >("mouse_y", 0))) &&
  1740. event.GetParameter< int >("button", 0) == 0)
  1741. SetPseudoClass("active", true);
  1742. if (event == MOUSESCROLL)
  1743. {
  1744. int wheel_delta = event.GetParameter< int >("wheel_delta", 0);
  1745. if ((wheel_delta < 0 && GetScrollTop() > 0) ||
  1746. (wheel_delta > 0 && GetScrollHeight() > GetScrollTop() + GetClientHeight()))
  1747. {
  1748. int overflow_property = GetProperty< int >(OVERFLOW_Y);
  1749. if (overflow_property == OVERFLOW_AUTO ||
  1750. overflow_property == OVERFLOW_SCROLL)
  1751. {
  1752. SetScrollTop(GetScrollTop() + wheel_delta * (GetFontFaceHandle() ? ElementUtilities::GetLineHeight(this) : (GetProperty(SCROLL_DEFAULT_STEP_SIZE) ? GetProperty< int >(SCROLL_DEFAULT_STEP_SIZE) : 0 )));
  1753. event.StopPropagation();
  1754. }
  1755. }
  1756. return;
  1757. }
  1758. if (event.GetTargetElement() == this)
  1759. {
  1760. if (event == MOUSEOVER)
  1761. SetPseudoClass("hover", true);
  1762. else if (event == MOUSEOUT)
  1763. SetPseudoClass("hover", false);
  1764. else if (event == FOCUS)
  1765. SetPseudoClass(FOCUS, true);
  1766. else if (event == BLUR)
  1767. SetPseudoClass(FOCUS, false);
  1768. }
  1769. }
  1770. void Element::GetRML(String& content)
  1771. {
  1772. // First we start the open tag, add the attributes then close the open tag.
  1773. // Then comes the children in order, then we add our close tag.
  1774. content.Append("<");
  1775. content.Append(tag);
  1776. int index = 0;
  1777. String name;
  1778. String value;
  1779. while (IterateAttributes(index, name, value))
  1780. {
  1781. size_t length = name.Length() + value.Length() + 8;
  1782. String attribute(length, " %s=\"%s\"", name.CString(), value.CString());
  1783. content.Append(attribute);
  1784. }
  1785. if (HasChildNodes())
  1786. {
  1787. content.Append(">");
  1788. GetInnerRML(content);
  1789. content.Append("</");
  1790. content.Append(tag);
  1791. content.Append(">");
  1792. }
  1793. else
  1794. {
  1795. content.Append(" />");
  1796. }
  1797. }
  1798. void Element::SetParent(Element* _parent)
  1799. {
  1800. // If there's an old parent, detach from it first.
  1801. if (parent &&
  1802. parent != _parent)
  1803. parent->RemoveChild(this);
  1804. // Save our parent
  1805. parent = _parent;
  1806. }
  1807. void Element::ReleaseDeletedElements()
  1808. {
  1809. for (size_t i = 0; i < active_children.size(); i++)
  1810. {
  1811. active_children[i]->ReleaseDeletedElements();
  1812. }
  1813. ReleaseElements(deleted_children);
  1814. active_children = children;
  1815. }
  1816. void Element::ReleaseElements(ElementList& released_elements)
  1817. {
  1818. // Remove deleted children from this element.
  1819. while (!released_elements.empty())
  1820. {
  1821. Element* element = released_elements.back();
  1822. released_elements.pop_back();
  1823. // If this element has been added back into our list, then we remove our previous oustanding reference on it
  1824. // and continue.
  1825. if (std::find(children.begin(), children.end(), element) != children.end())
  1826. {
  1827. element->RemoveReference();
  1828. continue;
  1829. }
  1830. // Set the parent to NULL unless it's been reparented already.
  1831. if (element->GetParentNode() == this)
  1832. element->parent = NULL;
  1833. element->RemoveReference();
  1834. }
  1835. }
  1836. void Element::DirtyOffset()
  1837. {
  1838. offset_dirty = true;
  1839. if(transform_state)
  1840. DirtyTransformState(true, true, false);
  1841. // Not strictly true ... ?
  1842. for (size_t i = 0; i < children.size(); i++)
  1843. children[i]->DirtyOffset();
  1844. }
  1845. void Element::UpdateOffset()
  1846. {
  1847. int position_property = GetPosition();
  1848. if (position_property == POSITION_ABSOLUTE ||
  1849. position_property == POSITION_FIXED)
  1850. {
  1851. if (offset_parent != NULL)
  1852. {
  1853. const Box& parent_box = offset_parent->GetBox();
  1854. Vector2f containing_block = parent_box.GetSize(Box::PADDING);
  1855. const Property *left = GetLocalProperty(LEFT);
  1856. const Property *right = GetLocalProperty(RIGHT);
  1857. // If the element is anchored left, then the position is offset by that resolved value.
  1858. if (left != NULL && left->unit != Property::KEYWORD)
  1859. relative_offset_base.x = parent_box.GetEdge(Box::BORDER, Box::LEFT) + (ResolveProperty(LEFT, containing_block.x) + GetBox().GetEdge(Box::MARGIN, Box::LEFT));
  1860. // If the element is anchored right, then the position is set first so the element's right-most edge
  1861. // (including margins) will render up against the containing box's right-most content edge, and then
  1862. // offset by the resolved value.
  1863. else if (right != NULL && right->unit != Property::KEYWORD)
  1864. 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));
  1865. const Property *top = GetLocalProperty(TOP);
  1866. const Property *bottom = GetLocalProperty(BOTTOM);
  1867. // If the element is anchored top, then the position is offset by that resolved value.
  1868. if (top != NULL && top->unit != Property::KEYWORD)
  1869. relative_offset_base.y = parent_box.GetEdge(Box::BORDER, Box::TOP) + (ResolveProperty(TOP, containing_block.y) + GetBox().GetEdge(Box::MARGIN, Box::TOP));
  1870. // If the element is anchored bottom, then the position is set first so the element's right-most edge
  1871. // (including margins) will render up against the containing box's right-most content edge, and then
  1872. // offset by the resolved value.
  1873. else if (bottom != NULL && bottom->unit != Property::KEYWORD)
  1874. 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));
  1875. }
  1876. }
  1877. else if (position_property == POSITION_RELATIVE)
  1878. {
  1879. if (offset_parent != NULL)
  1880. {
  1881. const Box& parent_box = offset_parent->GetBox();
  1882. Vector2f containing_block = parent_box.GetSize();
  1883. const Property *left = GetLocalProperty(LEFT);
  1884. const Property *right = GetLocalProperty(RIGHT);
  1885. if (left != NULL && left->unit != Property::KEYWORD)
  1886. relative_offset_position.x = ResolveProperty(LEFT, containing_block.x);
  1887. else if (right != NULL && right->unit != Property::KEYWORD)
  1888. relative_offset_position.x = -1 * ResolveProperty(RIGHT, containing_block.x);
  1889. else
  1890. relative_offset_position.x = 0;
  1891. const Property *top = GetLocalProperty(TOP);
  1892. const Property *bottom = GetLocalProperty(BOTTOM);
  1893. if (top != NULL && top->unit != Property::KEYWORD)
  1894. relative_offset_position.y = ResolveProperty(TOP, containing_block.y);
  1895. else if (bottom != NULL && bottom->unit != Property::KEYWORD)
  1896. relative_offset_position.y = -1 * ResolveProperty(BOTTOM, containing_block.y);
  1897. else
  1898. relative_offset_position.y = 0;
  1899. }
  1900. }
  1901. else
  1902. {
  1903. relative_offset_position.x = 0;
  1904. relative_offset_position.y = 0;
  1905. }
  1906. }
  1907. void Element::BuildLocalStackingContext()
  1908. {
  1909. stacking_context_dirty = false;
  1910. stacking_context.clear();
  1911. BuildStackingContext(&stacking_context);
  1912. std::stable_sort(stacking_context.begin(), stacking_context.end(), ElementSortZIndex());
  1913. }
  1914. void Element::BuildStackingContext(ElementList* new_stacking_context)
  1915. {
  1916. // Build the list of ordered children. Our child list is sorted within the stacking context so stacked elements
  1917. // will render in the right order; ie, positioned elements will render on top of inline elements, which will render
  1918. // on top of floated elements, which will render on top of block elements.
  1919. std::vector< std::pair< Element*, float > > ordered_children;
  1920. for (size_t i = 0; i < children.size(); ++i)
  1921. {
  1922. Element* child = children[i];
  1923. if (!child->IsVisible())
  1924. continue;
  1925. std::pair< Element*, float > ordered_child;
  1926. ordered_child.first = child;
  1927. if (child->GetPosition() != POSITION_STATIC)
  1928. ordered_child.second = 3;
  1929. else if (child->GetFloat() != FLOAT_NONE)
  1930. ordered_child.second = 1;
  1931. else if (child->GetDisplay() == DISPLAY_BLOCK)
  1932. ordered_child.second = 0;
  1933. else
  1934. ordered_child.second = 2;
  1935. ordered_children.push_back(ordered_child);
  1936. }
  1937. // Sort the list!
  1938. std::stable_sort(ordered_children.begin(), ordered_children.end(), ElementSortZOrder());
  1939. // Add the list of ordered children into the stacking context in order.
  1940. for (size_t i = 0; i < ordered_children.size(); ++i)
  1941. {
  1942. new_stacking_context->push_back(ordered_children[i].first);
  1943. if (!ordered_children[i].first->local_stacking_context)
  1944. ordered_children[i].first->BuildStackingContext(new_stacking_context);
  1945. }
  1946. }
  1947. void Element::DirtyStackingContext()
  1948. {
  1949. // The first ancestor of ours that doesn't have an automatic z-index is the ancestor that is establishing our local
  1950. // stacking context.
  1951. Element* stacking_context_parent = this;
  1952. while (stacking_context_parent != NULL &&
  1953. !stacking_context_parent->local_stacking_context)
  1954. stacking_context_parent = stacking_context_parent->GetParentNode();
  1955. if (stacking_context_parent != NULL)
  1956. stacking_context_parent->stacking_context_dirty = true;
  1957. }
  1958. void Element::DirtyStructure()
  1959. {
  1960. // Clear the cached owner document
  1961. owner_document = NULL;
  1962. // Inform all children that the structure is drity
  1963. for (size_t i = 0; i < children.size(); ++i)
  1964. {
  1965. const ElementDefinition* element_definition = children[i]->GetStyle()->GetDefinition();
  1966. if (element_definition != NULL &&
  1967. element_definition->IsStructurallyVolatile())
  1968. {
  1969. children[i]->GetStyle()->DirtyDefinition();
  1970. }
  1971. children[i]->DirtyStructure();
  1972. }
  1973. }
  1974. void Element::DirtyTransformState(bool perspective_changed, bool transform_changed, bool parent_transform_changed)
  1975. {
  1976. for (size_t i = 0; i < children.size(); ++i)
  1977. {
  1978. children[i]->DirtyTransformState(false, false, transform_changed || parent_transform_changed);
  1979. }
  1980. if (perspective_changed)
  1981. {
  1982. this->transform_state_perspective_dirty = true;
  1983. }
  1984. if (transform_changed)
  1985. {
  1986. this->transform_state_transform_dirty = true;
  1987. }
  1988. if (parent_transform_changed)
  1989. {
  1990. this->transform_state_parent_transform_dirty = true;
  1991. }
  1992. }
  1993. void Element::UpdateTransformState()
  1994. {
  1995. if (!(transform_state_perspective_dirty || transform_state_transform_dirty || transform_state_parent_transform_dirty))
  1996. {
  1997. return;
  1998. }
  1999. if (!transform_state.get())
  2000. {
  2001. transform_state.reset(new TransformState());
  2002. }
  2003. if(transform_state_perspective_dirty || transform_state_transform_dirty)
  2004. {
  2005. Context *context = GetContext();
  2006. Vector2f pos = GetAbsoluteOffset(Box::BORDER);
  2007. Vector2f size = GetBox().GetSize(Box::BORDER);
  2008. if (transform_state_perspective_dirty)
  2009. {
  2010. bool have_perspective = false;
  2011. TransformState::Perspective perspective_value;
  2012. perspective_value.vanish = Vector2f(pos.x + size.x * 0.5f, pos.y + size.y * 0.5f);
  2013. const Property *perspective = GetPerspective();
  2014. if (perspective && (perspective->unit != Property::KEYWORD || perspective->value.Get< int >() != PERSPECTIVE_NONE))
  2015. {
  2016. have_perspective = true;
  2017. // Compute the perspective value
  2018. perspective_value.distance = ResolveProperty(perspective, Math::Max(size.x, size.y));
  2019. // Compute the perspective origin, if necessary
  2020. if (perspective_value.distance > 0)
  2021. {
  2022. const Property *perspective_origin_x = GetPerspectiveOriginX();
  2023. if (perspective_origin_x)
  2024. {
  2025. if (perspective_origin_x->unit == Property::KEYWORD)
  2026. {
  2027. switch (perspective_origin_x->value.Get< int >())
  2028. {
  2029. case PERSPECTIVE_ORIGIN_X_LEFT:
  2030. perspective_value.vanish.x = pos.x;
  2031. break;
  2032. case PERSPECTIVE_ORIGIN_X_CENTER:
  2033. perspective_value.vanish.x = pos.x + size.x * 0.5f;
  2034. break;
  2035. case PERSPECTIVE_ORIGIN_X_RIGHT:
  2036. perspective_value.vanish.x = pos.x + size.x;
  2037. break;
  2038. }
  2039. }
  2040. else
  2041. {
  2042. perspective_value.vanish.x = pos.x + ResolveProperty(perspective_origin_x, size.x);
  2043. }
  2044. }
  2045. const Property *perspective_origin_y = GetPerspectiveOriginY();
  2046. if (perspective_origin_y)
  2047. {
  2048. if (perspective_origin_y->unit == Property::KEYWORD)
  2049. {
  2050. switch (perspective_origin_y->value.Get< int >())
  2051. {
  2052. case PERSPECTIVE_ORIGIN_Y_TOP:
  2053. perspective_value.vanish.y = pos.y;
  2054. break;
  2055. case PERSPECTIVE_ORIGIN_Y_CENTER:
  2056. perspective_value.vanish.y = pos.y + size.y * 0.5f;
  2057. break;
  2058. case PERSPECTIVE_ORIGIN_Y_BOTTOM:
  2059. perspective_value.vanish.y = pos.y + size.y;
  2060. break;
  2061. }
  2062. }
  2063. else
  2064. {
  2065. perspective_value.vanish.y = pos.y + ResolveProperty(perspective_origin_y, size.y);
  2066. }
  2067. }
  2068. }
  2069. }
  2070. if (have_perspective && context)
  2071. {
  2072. perspective_value.view_size = context->GetDimensions();
  2073. transform_state->SetPerspective(&perspective_value);
  2074. }
  2075. else
  2076. {
  2077. transform_state->SetPerspective(0);
  2078. }
  2079. transform_state_perspective_dirty = false;
  2080. }
  2081. if (transform_state_transform_dirty)
  2082. {
  2083. bool have_local_perspective = false;
  2084. TransformState::LocalPerspective local_perspective;
  2085. bool have_transform = false;
  2086. Matrix4f transform_value = Matrix4f::Identity();
  2087. Vector3f transform_origin(pos.x + size.x * 0.5f, pos.y + size.y * 0.5f, 0);
  2088. const Property *transform = GetTransform();
  2089. if (transform && (transform->unit != Property::KEYWORD || transform->value.Get< int >() != TRANSFORM_NONE))
  2090. {
  2091. TransformRef transforms = transform->value.Get< TransformRef >();
  2092. int n = transforms->GetNumPrimitives();
  2093. for (int i = 0; i < n; ++i)
  2094. {
  2095. const Transforms::Primitive &primitive = transforms->GetPrimitive(i);
  2096. if (primitive.ResolvePerspective(local_perspective.distance, *this))
  2097. {
  2098. have_local_perspective = true;
  2099. }
  2100. Matrix4f matrix;
  2101. if (primitive.ResolveTransform(matrix, *this))
  2102. {
  2103. transform_value *= matrix;
  2104. have_transform = true;
  2105. }
  2106. }
  2107. // Compute the transform origin
  2108. const Property *transform_origin_x = GetTransformOriginX();
  2109. if (transform_origin_x)
  2110. {
  2111. if (transform_origin_x->unit == Property::KEYWORD)
  2112. {
  2113. switch (transform_origin_x->value.Get< int >())
  2114. {
  2115. case TRANSFORM_ORIGIN_X_LEFT:
  2116. transform_origin.x = pos.x;
  2117. break;
  2118. case TRANSFORM_ORIGIN_X_CENTER:
  2119. transform_origin.x = pos.x + size.x * 0.5f;
  2120. break;
  2121. case TRANSFORM_ORIGIN_X_RIGHT:
  2122. transform_origin.x = pos.x + size.x;
  2123. break;
  2124. }
  2125. }
  2126. else
  2127. {
  2128. transform_origin.x = pos.x + ResolveProperty(transform_origin_x, size.x);
  2129. }
  2130. }
  2131. const Property *transform_origin_y = GetTransformOriginY();
  2132. if (transform_origin_y)
  2133. {
  2134. if (transform_origin_y->unit == Property::KEYWORD)
  2135. {
  2136. switch (transform_origin_y->value.Get< int >())
  2137. {
  2138. case TRANSFORM_ORIGIN_Y_TOP:
  2139. transform_origin.y = pos.y;
  2140. break;
  2141. case TRANSFORM_ORIGIN_Y_CENTER:
  2142. transform_origin.y = pos.y + size.y * 0.5f;
  2143. break;
  2144. case TRANSFORM_ORIGIN_Y_BOTTOM:
  2145. transform_origin.y = pos.y + size.y;
  2146. break;
  2147. }
  2148. }
  2149. else
  2150. {
  2151. transform_origin.y = pos.y + ResolveProperty(transform_origin_y, size.y);
  2152. }
  2153. }
  2154. const Property *transform_origin_z = GetTransformOriginZ();
  2155. if (transform_origin_z)
  2156. {
  2157. transform_origin.z = ResolveProperty(transform_origin_z, Math::Max(size.x, size.y));
  2158. }
  2159. }
  2160. if (have_local_perspective && context)
  2161. {
  2162. local_perspective.view_size = context->GetDimensions();
  2163. transform_state->SetLocalPerspective(&local_perspective);
  2164. }
  2165. else
  2166. {
  2167. transform_state->SetLocalPerspective(0);
  2168. }
  2169. if (have_transform)
  2170. {
  2171. // TODO: If we're using the global projection matrix
  2172. // (perspective < 0), then scale the coordinates from
  2173. // pixel space to 3D unit space.
  2174. // Transform the Rocket context so that the computed `transform_origin'
  2175. // lies at the coordinate system origin.
  2176. transform_value =
  2177. Matrix4f::Translate(transform_origin)
  2178. * transform_value
  2179. * Matrix4f::Translate(-transform_origin);
  2180. }
  2181. transform_state->SetTransform(have_transform ? &transform_value : 0);
  2182. transform_state_transform_dirty = false;
  2183. }
  2184. }
  2185. if (transform_state_parent_transform_dirty)
  2186. {
  2187. // We need to clean up from the top-most to the bottom-most dirt.
  2188. if (parent)
  2189. {
  2190. parent->UpdateTransformState();
  2191. }
  2192. if (transform_state.get())
  2193. {
  2194. // Store the parent's new full transform as our parent transform
  2195. Element *node = 0;
  2196. Matrix4f parent_transform;
  2197. for (node = parent; node; node = node->parent)
  2198. {
  2199. if (node->GetTransformState() && node->GetTransformState()->GetRecursiveTransform(&parent_transform))
  2200. {
  2201. transform_state->SetParentRecursiveTransform(&parent_transform);
  2202. break;
  2203. }
  2204. }
  2205. if (!node)
  2206. {
  2207. transform_state->SetParentRecursiveTransform(0);
  2208. }
  2209. }
  2210. transform_state_parent_transform_dirty = false;
  2211. }
  2212. // If we neither have a local perspective, nor a perspective nor a
  2213. // transform, we don't need to keep the large TransformState object
  2214. // around. GetEffectiveTransformState() will then recursively visit
  2215. // parents in order to find a non-trivial TransformState.
  2216. if (transform_state.get() && !transform_state->GetLocalPerspective(0) && !transform_state->GetPerspective(0) && !transform_state->GetTransform(0))
  2217. {
  2218. transform_state.reset(0);
  2219. }
  2220. }
  2221. }
  2222. }