Element.cpp 70 KB

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