Element.cpp 70 KB

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