Element.cpp 70 KB

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