Element.cpp 68 KB

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