Element.cpp 65 KB

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