Element.cpp 66 KB

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