Element.cpp 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #include "precompiled.h"
  28. #include <Rocket/Core/Element.h>
  29. #include <Rocket/Core/Dictionary.h>
  30. #include <algorithm>
  31. #include "ElementBackground.h"
  32. #include "ElementBorder.h"
  33. #include "ElementDefinition.h"
  34. #include "ElementStyle.h"
  35. #include "EventDispatcher.h"
  36. #include "ElementDecoration.h"
  37. #include "FontFaceHandle.h"
  38. #include "LayoutEngine.h"
  39. #include "PluginRegistry.h"
  40. #include "StyleSheetParser.h"
  41. #include "XMLParseTools.h"
  42. #include <Rocket/Core/Core.h>
  43. namespace Rocket {
  44. namespace Core {
  45. /**
  46. STL function object for sorting elements by z-type (ie, float-types before general types, etc).
  47. @author Peter Curry
  48. */
  49. class ElementSortZOrder
  50. {
  51. public:
  52. bool operator()(const std::pair< Element*, float >& lhs, const std::pair< Element*, float >& rhs)
  53. {
  54. return lhs.second < rhs.second;
  55. }
  56. };
  57. /**
  58. STL function object for sorting elements by z-index property.
  59. @author Peter Curry
  60. */
  61. class ElementSortZIndex
  62. {
  63. public:
  64. bool operator()(const Element* lhs, const Element* rhs)
  65. {
  66. // Check the z-index.
  67. return lhs->GetZIndex() < rhs->GetZIndex();
  68. }
  69. };
  70. /// Constructs a new libRocket element.
  71. Element::Element(const String& _tag) : absolute_offset(0, 0), relative_offset_base(0, 0), relative_offset_position(0, 0), scroll_offset(0, 0), content_offset(0, 0), content_box(0, 0), boxes(1)
  72. {
  73. tag = _tag.ToLower();
  74. parent = NULL;
  75. focus = NULL;
  76. instancer = NULL;
  77. owner_document = NULL;
  78. offset_fixed = false;
  79. offset_parent = NULL;
  80. offset_dirty = true;
  81. client_area = Box::PADDING;
  82. num_non_dom_children = 0;
  83. visible = true;
  84. z_index = 0;
  85. local_stacking_context = false;
  86. local_stacking_context_forced = false;
  87. stacking_context_dirty = false;
  88. font_face_handle = NULL;
  89. clipping_ignore_depth = 0;
  90. clipping_enabled = false;
  91. clipping_state_dirty = true;
  92. event_dispatcher = new EventDispatcher(this);
  93. style = new ElementStyle(this);
  94. background = new ElementBackground(this);
  95. border = new ElementBorder(this);
  96. decoration = new ElementDecoration(this);
  97. scroll = new ElementScroll(this);
  98. }
  99. Element::~Element()
  100. {
  101. ROCKET_ASSERT(parent == NULL);
  102. PluginRegistry::NotifyElementDestroy(this);
  103. // Delete the scroll funtionality before we delete the children!
  104. delete scroll;
  105. while (!children.empty())
  106. {
  107. // A simplified version of RemoveChild() for destruction.
  108. Element* child = children.front();
  109. child->OnChildRemove(child);
  110. if (num_non_dom_children > 0)
  111. num_non_dom_children--;
  112. deleted_children.push_back(child);
  113. children.erase(children.begin());
  114. }
  115. // Release all deleted children.
  116. ReleaseElements(deleted_children);
  117. delete decoration;
  118. delete border;
  119. delete background;
  120. delete style;
  121. delete event_dispatcher;
  122. if (font_face_handle != NULL)
  123. font_face_handle->RemoveReference();
  124. if (instancer)
  125. instancer->RemoveReference();
  126. }
  127. void Element::Update()
  128. {
  129. ReleaseElements(deleted_children);
  130. active_children = children;
  131. for (size_t i = 0; i < active_children.size(); i++)
  132. active_children[i]->Update();
  133. // Force a definition reload, if necessary.
  134. style->GetDefinition();
  135. scroll->Update();
  136. OnUpdate();
  137. }
  138. void Element::Render()
  139. {
  140. // Rebuild our stacking context if necessary.
  141. if (stacking_context_dirty)
  142. BuildLocalStackingContext();
  143. // Render all elements in our local stacking context that have a z-index beneath our local index of 0.
  144. size_t i = 0;
  145. for (; i < stacking_context.size() && stacking_context[i]->z_index < 0; ++i)
  146. stacking_context[i]->Render();
  147. // Set up the clipping region for this element.
  148. if (ElementUtilities::SetClippingRegion(this))
  149. {
  150. background->RenderBackground();
  151. border->RenderBorder();
  152. decoration->RenderDecorators();
  153. OnRender();
  154. }
  155. // Render the rest of the elements in the stacking context.
  156. for (; i < stacking_context.size(); ++i)
  157. stacking_context[i]->Render();
  158. }
  159. // Clones this element, returning a new, unparented element.
  160. Element* Element::Clone() const
  161. {
  162. Element* clone = NULL;
  163. if (instancer != NULL)
  164. {
  165. clone = instancer->InstanceElement(NULL, GetTagName(), attributes);
  166. if (clone != NULL)
  167. clone->SetInstancer(instancer);
  168. }
  169. else
  170. clone = Factory::InstanceElement(NULL, GetTagName(), GetTagName(), attributes);
  171. if (clone != NULL)
  172. {
  173. String inner_rml;
  174. GetInnerRML(inner_rml);
  175. clone->SetInnerRML(inner_rml);
  176. }
  177. return clone;
  178. }
  179. // Sets or removes a class on the element.
  180. void Element::SetClass(const String& class_name, bool activate)
  181. {
  182. style->SetClass(class_name, activate);
  183. }
  184. // Checks if a class is set on the element.
  185. bool Element::IsClassSet(const String& class_name) const
  186. {
  187. return style->IsClassSet(class_name);
  188. }
  189. // Specifies the entire list of classes for this element. This will replace any others specified.
  190. void Element::SetClassNames(const String& class_names)
  191. {
  192. SetAttribute("class", class_names);
  193. }
  194. /// Return the active class list
  195. String Element::GetClassNames() const
  196. {
  197. return style->GetClassNames();
  198. }
  199. // Returns the active style sheet for this element. This may be NULL.
  200. StyleSheet* Element::GetStyleSheet() const
  201. {
  202. return style->GetStyleSheet();
  203. }
  204. // Returns the element's definition, updating if necessary.
  205. const ElementDefinition* Element::GetDefinition()
  206. {
  207. return style->GetDefinition();
  208. }
  209. // Fills an String with the full address of this element.
  210. String Element::GetAddress(bool include_pseudo_classes) const
  211. {
  212. // Add the tag name onto the address.
  213. String address(tag);
  214. // Add the ID if we have one.
  215. if (!id.Empty())
  216. {
  217. address += "#";
  218. address += id;
  219. }
  220. String classes = style->GetClassNames();
  221. if (!classes.Empty())
  222. {
  223. classes = classes.Replace(".", " ");
  224. address += ".";
  225. address += classes;
  226. }
  227. if (include_pseudo_classes)
  228. {
  229. const PseudoClassList& pseudo_classes = style->GetActivePseudoClasses();
  230. for (PseudoClassList::const_iterator i = pseudo_classes.begin(); i != pseudo_classes.end(); ++i)
  231. {
  232. address += ":";
  233. address += (*i);
  234. }
  235. }
  236. if (parent)
  237. {
  238. address.Append(" < ");
  239. return address + parent->GetAddress(true);
  240. }
  241. else
  242. return address;
  243. }
  244. // Sets the position of this element, as a two-dimensional offset from another element.
  245. void Element::SetOffset(const Vector2f& offset, Element* _offset_parent, bool _offset_fixed)
  246. {
  247. _offset_fixed |= GetPosition() == POSITION_FIXED;
  248. // If our offset has definitely changed, or any of our parenting has, then these are set and
  249. // updated based on our left / right / top / bottom properties.
  250. if (relative_offset_base != offset ||
  251. offset_parent != _offset_parent ||
  252. offset_fixed != _offset_fixed)
  253. {
  254. relative_offset_base = offset;
  255. offset_fixed = _offset_fixed;
  256. offset_parent = _offset_parent;
  257. UpdateOffset();
  258. DirtyOffset();
  259. }
  260. // Otherwise, our offset is updated in case left / right / top / bottom will have an impact on
  261. // our final position, and our children are dirtied if they do.
  262. else
  263. {
  264. Vector2f& old_base = relative_offset_base;
  265. Vector2f& old_position = relative_offset_position;
  266. UpdateOffset();
  267. if (old_base != relative_offset_base ||
  268. old_position != relative_offset_position)
  269. DirtyOffset();
  270. }
  271. }
  272. // Returns the position of the top-left corner of one of the areas of this element's primary box.
  273. Vector2f Element::GetRelativeOffset(Box::Area area)
  274. {
  275. UpdateLayout();
  276. return relative_offset_base + relative_offset_position + GetBox().GetPosition(area);
  277. }
  278. // Returns the position of the top-left corner of one of the areas of this element's primary box.
  279. Vector2f Element::GetAbsoluteOffset(Box::Area area)
  280. {
  281. UpdateLayout();
  282. if (offset_dirty)
  283. {
  284. offset_dirty = false;
  285. if (offset_parent != NULL)
  286. absolute_offset = offset_parent->GetAbsoluteOffset(Box::BORDER) + relative_offset_base + relative_offset_position;
  287. else
  288. absolute_offset = relative_offset_base + relative_offset_position;
  289. // Add any parent scrolling onto our position as well. Could cache this if required.
  290. if (!offset_fixed)
  291. {
  292. Element* scroll_parent = parent;
  293. while (scroll_parent != NULL)
  294. {
  295. absolute_offset -= (scroll_parent->scroll_offset + scroll_parent->content_offset);
  296. if (scroll_parent == offset_parent)
  297. break;
  298. else
  299. scroll_parent = scroll_parent->parent;
  300. }
  301. }
  302. }
  303. return absolute_offset + GetBox().GetPosition(area);
  304. }
  305. // Sets an alternate area to use as the client area.
  306. void Element::SetClientArea(Box::Area _client_area)
  307. {
  308. client_area = _client_area;
  309. }
  310. // Returns the area the element uses as its client area.
  311. Box::Area Element::GetClientArea() const
  312. {
  313. return client_area;
  314. }
  315. // Sets the dimensions of the element's internal content.
  316. void Element::SetContentBox(const Vector2f& _content_offset, const Vector2f& _content_box)
  317. {
  318. if (content_offset != _content_offset ||
  319. content_box != _content_box)
  320. {
  321. // Seems to be jittering a wee bit; might need to be looked at.
  322. scroll_offset.x += (content_offset.x - _content_offset.x);
  323. scroll_offset.y += (content_offset.y - _content_offset.y);
  324. content_offset = _content_offset;
  325. content_box = _content_box;
  326. scroll_offset.x = Math::Min(scroll_offset.x, GetScrollWidth() - GetClientWidth());
  327. scroll_offset.y = Math::Min(scroll_offset.y, GetScrollHeight() - GetClientHeight());
  328. DirtyOffset();
  329. }
  330. }
  331. // Sets the box describing the size of the element.
  332. void Element::SetBox(const Box& box)
  333. {
  334. if (box != boxes[0] ||
  335. boxes.size() > 1)
  336. {
  337. boxes[0] = box;
  338. boxes.resize(1);
  339. background->DirtyBackground();
  340. border->DirtyBorder();
  341. decoration->ReloadDecorators();
  342. DispatchEvent(RESIZE, Dictionary());
  343. }
  344. }
  345. // Adds a box to the end of the list describing this element's geometry.
  346. void Element::AddBox(const Box& box)
  347. {
  348. boxes.push_back(box);
  349. DispatchEvent(RESIZE, Dictionary());
  350. background->DirtyBackground();
  351. border->DirtyBorder();
  352. decoration->ReloadDecorators();
  353. }
  354. // Returns one of the boxes describing the size of the element.
  355. const Box& Element::GetBox(int index)
  356. {
  357. UpdateLayout();
  358. if (index < 0)
  359. return boxes[0];
  360. else if (index >= GetNumBoxes())
  361. return boxes.back();
  362. return boxes[index];
  363. }
  364. // Returns the number of boxes making up this element's geometry.
  365. int Element::GetNumBoxes()
  366. {
  367. UpdateLayout();
  368. return (int) boxes.size();
  369. }
  370. // Returns the baseline of the element, in pixels offset from the bottom of the element's content area.
  371. float Element::GetBaseline() const
  372. {
  373. return 0;
  374. }
  375. // Gets the intrinsic dimensions of this element, if it is of a type that has an inherent size.
  376. bool Element::GetIntrinsicDimensions(Vector2f& ROCKET_UNUSED(dimensions))
  377. {
  378. return false;
  379. }
  380. // Checks if a given point in screen coordinates lies within the bordered area of this element.
  381. bool Element::IsPointWithinElement(const Vector2f& point)
  382. {
  383. Vector2f position = GetAbsoluteOffset(Box::BORDER);
  384. for (int i = 0; i < GetNumBoxes(); ++i)
  385. {
  386. const Box& box = GetBox(i);
  387. Vector2f box_position = position + box.GetOffset();
  388. Vector2f box_dimensions = box.GetSize(Box::BORDER);
  389. if (point.x >= box_position.x &&
  390. point.x <= (box_position.x + box_dimensions.x) &&
  391. point.y >= box_position.y &&
  392. point.y <= (box_position.y + box_dimensions.y))
  393. {
  394. return true;
  395. }
  396. }
  397. return false;
  398. }
  399. // Returns the visibility of the element.
  400. bool Element::IsVisible() const
  401. {
  402. return visible;
  403. }
  404. // Returns the z-index of the element.
  405. float Element::GetZIndex() const
  406. {
  407. return z_index;
  408. }
  409. // Returns the element's font face handle.
  410. FontFaceHandle* Element::GetFontFaceHandle() const
  411. {
  412. return font_face_handle;
  413. }
  414. // Sets a local property override on the element.
  415. bool Element::SetProperty(const String& name, const String& value)
  416. {
  417. return style->SetProperty(name, value);
  418. }
  419. // Removes a local property override on the element.
  420. void Element::RemoveProperty(const String& name)
  421. {
  422. style->RemoveProperty(name);
  423. }
  424. // Sets a local property override on the element to a pre-parsed value.
  425. bool Element::SetProperty(const String& name, const Property& property)
  426. {
  427. return style->SetProperty(name, property);
  428. }
  429. // Returns one of this element's properties.
  430. const Property* Element::GetProperty(const String& name)
  431. {
  432. return style->GetProperty(name);
  433. }
  434. // Returns one of this element's properties.
  435. const Property* Element::GetLocalProperty(const String& name)
  436. {
  437. return style->GetLocalProperty(name);
  438. }
  439. // Resolves one of this element's style.
  440. float Element::ResolveProperty(const String& name, float base_value)
  441. {
  442. return style->ResolveProperty(name, base_value);
  443. }
  444. // Resolves one of this element's style.
  445. float Element::ResolveProperty(const Property *property, float base_value)
  446. {
  447. return style->ResolveProperty(property, base_value);
  448. }
  449. void Element::GetBorderWidthProperties(const Property **border_top, const Property **border_bottom, const Property **border_left, const Property **bottom_right)
  450. {
  451. style->GetBorderWidthProperties(border_top, border_bottom, border_left, bottom_right);
  452. }
  453. void Element::GetMarginProperties(const Property **margin_top, const Property **margin_bottom, const Property **margin_left, const Property **margin_right)
  454. {
  455. style->GetMarginProperties(margin_top, margin_bottom, margin_left, margin_right);
  456. }
  457. void Element::GetPaddingProperties(const Property **padding_top, const Property **padding_bottom, const Property **padding_left, const Property **padding_right)
  458. {
  459. style->GetPaddingProperties(padding_top, padding_bottom, padding_left, padding_right);
  460. }
  461. void Element::GetDimensionProperties(const Property **width, const Property **height)
  462. {
  463. style->GetDimensionProperties(width, height);
  464. }
  465. void Element::GetLocalDimensionProperties(const Property **width, const Property **height)
  466. {
  467. style->GetLocalDimensionProperties(width, height);
  468. }
  469. void Element::GetOverflow(int *overflow_x, int *overflow_y)
  470. {
  471. style->GetOverflow(overflow_x, overflow_y);
  472. }
  473. int Element::GetPosition()
  474. {
  475. return style->GetPosition();
  476. }
  477. int Element::GetFloat()
  478. {
  479. return style->GetFloat();
  480. }
  481. int Element::GetDisplay()
  482. {
  483. return style->GetDisplay();
  484. }
  485. int Element::GetWhitespace()
  486. {
  487. return style->GetWhitespace();
  488. }
  489. const Property *Element::GetLineHeightProperty()
  490. {
  491. return style->GetLineHeightProperty();
  492. }
  493. int Element::GetTextAlign()
  494. {
  495. return style->GetTextAlign();
  496. }
  497. int Element::GetTextTransform()
  498. {
  499. return style->GetTextTransform();
  500. }
  501. const Property *Element::GetVerticalAlignProperty()
  502. {
  503. return style->GetVerticalAlignProperty();
  504. }
  505. // Iterates over the properties defined on this element.
  506. bool Element::IterateProperties(int& index, PseudoClassList& pseudo_classes, String& name, const Property*& property) const
  507. {
  508. return style->IterateProperties(index, pseudo_classes, name, property);
  509. }
  510. // Sets or removes a pseudo-class on the element.
  511. void Element::SetPseudoClass(const String& pseudo_class, bool activate)
  512. {
  513. style->SetPseudoClass(pseudo_class, activate);
  514. }
  515. // Checks if a specific pseudo-class has been set on the element.
  516. bool Element::IsPseudoClassSet(const String& pseudo_class) const
  517. {
  518. return style->IsPseudoClassSet(pseudo_class);
  519. }
  520. // Checks if a complete set of pseudo-classes are set on the element.
  521. bool Element::ArePseudoClassesSet(const PseudoClassList& pseudo_classes) const
  522. {
  523. for (PseudoClassList::const_iterator i = pseudo_classes.begin(); i != pseudo_classes.end(); ++i)
  524. {
  525. if (!IsPseudoClassSet(*i))
  526. return false;
  527. }
  528. return true;
  529. }
  530. // Gets a list of the current active pseudo classes
  531. const PseudoClassList& Element::GetActivePseudoClasses() const
  532. {
  533. return style->GetActivePseudoClasses();
  534. }
  535. /// Get the named attribute
  536. Variant* Element::GetAttribute(const String& name) const
  537. {
  538. return attributes.Get(name);
  539. }
  540. // Checks if the element has a certain attribute.
  541. bool Element::HasAttribute(const String& name)
  542. {
  543. return attributes.Get(name) != NULL;
  544. }
  545. // Removes an attribute from the element
  546. void Element::RemoveAttribute(const String& name)
  547. {
  548. if (attributes.Remove(name))
  549. {
  550. AttributeNameList changed_attributes;
  551. changed_attributes.insert(name);
  552. OnAttributeChange(changed_attributes);
  553. }
  554. }
  555. // Gets the outer most focus element down the tree from this node
  556. Element* Element::GetFocusLeafNode()
  557. {
  558. // If there isn't a focus, then we are the leaf.
  559. if (!focus)
  560. {
  561. return this;
  562. }
  563. // Recurse down the tree until we found the leaf focus element
  564. Element* focus_element = focus;
  565. while (focus_element->focus)
  566. focus_element = focus_element->focus;
  567. return focus_element;
  568. }
  569. // Returns the element's context.
  570. Context* Element::GetContext()
  571. {
  572. ElementDocument* document = GetOwnerDocument();
  573. if (document != NULL)
  574. return document->GetContext();
  575. return NULL;
  576. }
  577. // Set a group of attributes
  578. void Element::SetAttributes(const ElementAttributes* _attributes)
  579. {
  580. int index = 0;
  581. String key;
  582. Variant* value;
  583. AttributeNameList changed_attributes;
  584. while (_attributes->Iterate(index, key, value))
  585. {
  586. changed_attributes.insert(key);
  587. attributes.Set(key, *value);
  588. }
  589. OnAttributeChange(changed_attributes);
  590. }
  591. // Returns the number of attributes on the element.
  592. int Element::GetNumAttributes() const
  593. {
  594. return attributes.Size();
  595. }
  596. // Iterates over all decorators attached to the element.
  597. bool Element::IterateDecorators(int& index, PseudoClassList& pseudo_classes, String& name, Decorator*& decorator, DecoratorDataHandle& decorator_data)
  598. {
  599. return decoration->IterateDecorators(index, pseudo_classes, name, decorator, decorator_data);
  600. }
  601. // Gets the name of the element.
  602. const String& Element::GetTagName() const
  603. {
  604. return tag;
  605. }
  606. // Gets the ID of the element.
  607. const String& Element::GetId() const
  608. {
  609. return id;
  610. }
  611. // Sets the ID of the element.
  612. void Element::SetId(const String& _id)
  613. {
  614. SetAttribute("id", _id);
  615. }
  616. // Gets the horizontal offset from the context's left edge to element's left border edge.
  617. float Element::GetAbsoluteLeft()
  618. {
  619. return GetAbsoluteOffset(Box::BORDER).x;
  620. }
  621. // Gets the vertical offset from the context's top edge to element's top border edge.
  622. float Element::GetAbsoluteTop()
  623. {
  624. return GetAbsoluteOffset(Box::BORDER).y;
  625. }
  626. // Gets the width of the left border of an element.
  627. float Element::GetClientLeft()
  628. {
  629. UpdateLayout();
  630. return GetBox().GetPosition(client_area).x;
  631. }
  632. // Gets the height of the top border of an element.
  633. float Element::GetClientTop()
  634. {
  635. UpdateLayout();
  636. return GetBox().GetPosition(client_area).y;
  637. }
  638. // Gets the inner width of the element.
  639. float Element::GetClientWidth()
  640. {
  641. UpdateLayout();
  642. return GetBox().GetSize(client_area).x - scroll->GetScrollbarSize(ElementScroll::VERTICAL);
  643. }
  644. // Gets the inner height of the element.
  645. float Element::GetClientHeight()
  646. {
  647. UpdateLayout();
  648. return GetBox().GetSize(client_area).y - scroll->GetScrollbarSize(ElementScroll::HORIZONTAL);
  649. }
  650. // Returns the element from which all offset calculations are currently computed.
  651. Element* Element::GetOffsetParent()
  652. {
  653. return parent;
  654. }
  655. // Gets the distance from this element's left border to its offset parent's left border.
  656. float Element::GetOffsetLeft()
  657. {
  658. UpdateLayout();
  659. return relative_offset_base.x + relative_offset_position.x;
  660. }
  661. // Gets the distance from this element's top border to its offset parent's top border.
  662. float Element::GetOffsetTop()
  663. {
  664. UpdateLayout();
  665. return relative_offset_base.y + relative_offset_position.y;
  666. }
  667. // Gets the width of the element, including the client area, padding, borders and scrollbars, but not margins.
  668. float Element::GetOffsetWidth()
  669. {
  670. UpdateLayout();
  671. return GetBox().GetSize(Box::BORDER).x;
  672. }
  673. // Gets the height of the element, including the client area, padding, borders and scrollbars, but not margins.
  674. float Element::GetOffsetHeight()
  675. {
  676. UpdateLayout();
  677. return GetBox().GetSize(Box::BORDER).y;
  678. }
  679. // Gets the left scroll offset of the element.
  680. float Element::GetScrollLeft()
  681. {
  682. UpdateLayout();
  683. return scroll_offset.x;
  684. }
  685. // Sets the left scroll offset of the element.
  686. void Element::SetScrollLeft(float scroll_left)
  687. {
  688. scroll_offset.x = LayoutEngine::Round(Math::Clamp(scroll_left, 0.0f, GetScrollWidth() - GetClientWidth()));
  689. scroll->UpdateScrollbar(ElementScroll::HORIZONTAL);
  690. DirtyOffset();
  691. DispatchEvent("scroll", Dictionary());
  692. }
  693. // Gets the top scroll offset of the element.
  694. float Element::GetScrollTop()
  695. {
  696. UpdateLayout();
  697. return scroll_offset.y;
  698. }
  699. // Sets the top scroll offset of the element.
  700. void Element::SetScrollTop(float scroll_top)
  701. {
  702. scroll_offset.y = LayoutEngine::Round(Math::Clamp(scroll_top, 0.0f, GetScrollHeight() - GetClientHeight()));
  703. scroll->UpdateScrollbar(ElementScroll::VERTICAL);
  704. DirtyOffset();
  705. DispatchEvent("scroll", Dictionary());
  706. }
  707. // Gets the width of the scrollable content of the element; it includes the element padding but not its margin.
  708. float Element::GetScrollWidth()
  709. {
  710. return Math::Max(content_box.x, GetClientWidth());
  711. }
  712. // Gets the height of the scrollable content of the element; it includes the element padding but not its margin.
  713. float Element::GetScrollHeight()
  714. {
  715. return Math::Max(content_box.y, GetClientHeight());
  716. }
  717. // Gets the object representing the declarations of an element's style attributes.
  718. ElementStyle* Element::GetStyle()
  719. {
  720. return style;
  721. }
  722. // Gets the document this element belongs to.
  723. ElementDocument* Element::GetOwnerDocument()
  724. {
  725. if (parent == NULL)
  726. return NULL;
  727. if (!owner_document)
  728. {
  729. owner_document = parent->GetOwnerDocument();
  730. }
  731. return owner_document;
  732. }
  733. // Gets this element's parent node.
  734. Element* Element::GetParentNode() const
  735. {
  736. return parent;
  737. }
  738. // Gets the element immediately following this one in the tree.
  739. Element* Element::GetNextSibling() const
  740. {
  741. if (parent == NULL)
  742. return NULL;
  743. for (size_t i = 0; i < parent->children.size() - (parent->num_non_dom_children + 1); i++)
  744. {
  745. if (parent->children[i] == this)
  746. return parent->children[i + 1];
  747. }
  748. return NULL;
  749. }
  750. // Gets the element immediately preceding this one in the tree.
  751. Element* Element::GetPreviousSibling() const
  752. {
  753. if (parent == NULL)
  754. return NULL;
  755. for (size_t i = 1; i < parent->children.size() - parent->num_non_dom_children; i++)
  756. {
  757. if (parent->children[i] == this)
  758. return parent->children[i - 1];
  759. }
  760. return NULL;
  761. }
  762. // Returns the first child of this element.
  763. Element* Element::GetFirstChild() const
  764. {
  765. if (GetNumChildren() > 0)
  766. return children[0];
  767. return NULL;
  768. }
  769. // Gets the last child of this element.
  770. Element* Element::GetLastChild() const
  771. {
  772. if (GetNumChildren() > 0)
  773. return *(children.end() - (num_non_dom_children + 1));
  774. return NULL;
  775. }
  776. Element* Element::GetChild(int index) const
  777. {
  778. if (index < 0 || index >= (int) children.size())
  779. return NULL;
  780. return children[index];
  781. }
  782. int Element::GetNumChildren(bool include_non_dom_elements) const
  783. {
  784. return (int) children.size() - (include_non_dom_elements ? 0 : num_non_dom_children);
  785. }
  786. // Gets the markup and content of the element.
  787. void Element::GetInnerRML(String& content) const
  788. {
  789. for (int i = 0; i < GetNumChildren(); i++)
  790. {
  791. children[i]->GetRML(content);
  792. }
  793. }
  794. // Gets the markup and content of the element.
  795. String Element::GetInnerRML() const {
  796. String result;
  797. GetInnerRML(result);
  798. return result;
  799. }
  800. // Sets the markup and content of the element. All existing children will be replaced.
  801. void Element::SetInnerRML(const String& rml)
  802. {
  803. // Remove all DOM children.
  804. while ((int) children.size() > num_non_dom_children)
  805. RemoveChild(children.front());
  806. Factory::InstanceElementText(this, rml);
  807. }
  808. // Sets the current element as the focus object.
  809. bool Element::Focus()
  810. {
  811. // Are we allowed focus?
  812. int focus_property = GetProperty< int >(FOCUS);
  813. if (focus_property == FOCUS_NONE)
  814. return false;
  815. // Ask our context if we can switch focus.
  816. Context* context = GetContext();
  817. if (context == NULL)
  818. return false;
  819. if (!context->OnFocusChange(this))
  820. return false;
  821. // Set this as the end of the focus chain.
  822. focus = NULL;
  823. // Update the focus chain up the hierarchy.
  824. Element* element = this;
  825. while (element->GetParentNode())
  826. {
  827. element->GetParentNode()->focus = element;
  828. element = element->GetParentNode();
  829. }
  830. return true;
  831. }
  832. // Removes focus from from this element.
  833. void Element::Blur()
  834. {
  835. if (parent)
  836. {
  837. Context* context = GetContext();
  838. if (context == NULL)
  839. return;
  840. if (context->GetFocusElement() == this)
  841. {
  842. parent->Focus();
  843. }
  844. else if (parent->focus == this)
  845. {
  846. parent->focus = NULL;
  847. }
  848. }
  849. }
  850. // Fakes a mouse click on this element.
  851. void Element::Click()
  852. {
  853. Context* context = GetContext();
  854. if (context == NULL)
  855. return;
  856. context->GenerateClickEvent(this);
  857. }
  858. // Adds an event listener
  859. void Element::AddEventListener(const String& event, EventListener* listener, bool in_capture_phase)
  860. {
  861. event_dispatcher->AttachEvent(event, listener, in_capture_phase);
  862. }
  863. // Removes an event listener from this element.
  864. void Element::RemoveEventListener(const String& event, EventListener* listener, bool in_capture_phase)
  865. {
  866. event_dispatcher->DetachEvent(event, listener, in_capture_phase);
  867. }
  868. // Dispatches the specified event
  869. bool Element::DispatchEvent(const String& event, const Dictionary& parameters, bool interruptible)
  870. {
  871. return event_dispatcher->DispatchEvent(this, event, parameters, interruptible);
  872. }
  873. // Scrolls the parent element's contents so that this element is visible.
  874. void Element::ScrollIntoView(bool align_with_top)
  875. {
  876. Vector2f size(0, 0);
  877. if (!align_with_top &&
  878. !boxes.empty())
  879. {
  880. size.y = boxes.back().GetOffset().y +
  881. boxes.back().GetSize(Box::BORDER).y;
  882. }
  883. Element* scroll_parent = parent;
  884. while (scroll_parent != NULL)
  885. {
  886. int overflow_x_property = scroll_parent->GetProperty< int >(OVERFLOW_X);
  887. int overflow_y_property = scroll_parent->GetProperty< int >(OVERFLOW_Y);
  888. if ((overflow_x_property != OVERFLOW_VISIBLE &&
  889. scroll_parent->GetScrollWidth() > scroll_parent->GetClientWidth()) ||
  890. (overflow_y_property != OVERFLOW_VISIBLE &&
  891. scroll_parent->GetScrollHeight() > scroll_parent->GetClientHeight()))
  892. {
  893. Vector2f offset = scroll_parent->GetAbsoluteOffset(Box::BORDER) - GetAbsoluteOffset(Box::BORDER);
  894. Vector2f scroll_offset(scroll_parent->GetScrollLeft(), scroll_parent->GetScrollTop());
  895. scroll_offset -= offset;
  896. scroll_offset.x += scroll_parent->GetClientLeft();
  897. scroll_offset.y += scroll_parent->GetClientTop();
  898. if (!align_with_top)
  899. scroll_offset.y -= (scroll_parent->GetClientHeight() - size.y);
  900. if (overflow_x_property != OVERFLOW_VISIBLE)
  901. scroll_parent->SetScrollLeft(scroll_offset.x);
  902. if (overflow_y_property != OVERFLOW_VISIBLE)
  903. scroll_parent->SetScrollTop(scroll_offset.y);
  904. }
  905. scroll_parent = scroll_parent->GetParentNode();
  906. }
  907. }
  908. // Appends a child to this element
  909. void Element::AppendChild(Element* child, bool dom_element)
  910. {
  911. LockLayout(true);
  912. child->AddReference();
  913. child->SetParent(this);
  914. if (dom_element)
  915. children.insert(children.end() - num_non_dom_children, child);
  916. else
  917. {
  918. children.push_back(child);
  919. num_non_dom_children++;
  920. }
  921. child->GetStyle()->DirtyDefinition();
  922. child->GetStyle()->DirtyProperties();
  923. child->OnChildAdd(child);
  924. DirtyStackingContext();
  925. DirtyStructure();
  926. if (dom_element)
  927. DirtyLayout();
  928. LockLayout(false);
  929. }
  930. // Adds a child to this element, directly after the adjacent element. Inherits
  931. // the dom/non-dom status from the adjacent element.
  932. void Element::InsertBefore(Element* child, Element* adjacent_element)
  933. {
  934. // Find the position in the list of children of the adjacent element. If
  935. // it's NULL or we can't find it, then we insert it at the end of the dom
  936. // children, as a dom element.
  937. size_t child_index = 0;
  938. bool found_child = false;
  939. if (adjacent_element)
  940. {
  941. for (child_index = 0; child_index < children.size(); child_index++)
  942. {
  943. if (children[child_index] == adjacent_element)
  944. {
  945. found_child = true;
  946. break;
  947. }
  948. }
  949. }
  950. if (found_child)
  951. {
  952. LockLayout(true);
  953. child->AddReference();
  954. child->SetParent(this);
  955. if ((int) child_index >= GetNumChildren())
  956. num_non_dom_children++;
  957. else
  958. DirtyLayout();
  959. children.insert(children.begin() + child_index, child);
  960. child->GetStyle()->DirtyDefinition();
  961. child->GetStyle()->DirtyProperties();
  962. child->OnChildAdd(child);
  963. DirtyStackingContext();
  964. DirtyStructure();
  965. LockLayout(false);
  966. }
  967. else
  968. {
  969. AppendChild(child);
  970. }
  971. }
  972. // Replaces the second node with the first node.
  973. bool Element::ReplaceChild(Element* inserted_element, Element* replaced_element)
  974. {
  975. inserted_element->AddReference();
  976. inserted_element->SetParent(this);
  977. ElementList::iterator insertion_point = children.begin();
  978. while (insertion_point != children.end() && *insertion_point != replaced_element)
  979. {
  980. ++insertion_point;
  981. }
  982. if (insertion_point == children.end())
  983. {
  984. AppendChild(inserted_element);
  985. return false;
  986. }
  987. LockLayout(true);
  988. children.insert(insertion_point, inserted_element);
  989. RemoveChild(replaced_element);
  990. inserted_element->GetStyle()->DirtyDefinition();
  991. inserted_element->GetStyle()->DirtyProperties();
  992. inserted_element->OnChildAdd(inserted_element);
  993. LockLayout(false);
  994. return true;
  995. }
  996. // Removes the specified child
  997. bool Element::RemoveChild(Element* child)
  998. {
  999. size_t child_index = 0;
  1000. for (ElementList::iterator itr = children.begin(); itr != children.end(); ++itr)
  1001. {
  1002. // Add the element to the delete list
  1003. if ((*itr) == child)
  1004. {
  1005. LockLayout(true);
  1006. // Inform the context of the element's pending removal (if we have a valid context).
  1007. Context* context = GetContext();
  1008. if (context)
  1009. context->OnElementRemove(child);
  1010. child->OnChildRemove(child);
  1011. if (child_index >= children.size() - num_non_dom_children)
  1012. num_non_dom_children--;
  1013. deleted_children.push_back(child);
  1014. children.erase(itr);
  1015. // Remove the child element as the focussed child of this element.
  1016. if (child == focus)
  1017. {
  1018. focus = NULL;
  1019. // If this child (or a descendant of this child) is the context's currently
  1020. // focussed element, set the focus to us instead.
  1021. Context* context = GetContext();
  1022. if (context != NULL)
  1023. {
  1024. Element* focus_element = context->GetFocusElement();
  1025. while (focus_element != NULL)
  1026. {
  1027. if (focus_element == child)
  1028. {
  1029. Focus();
  1030. break;
  1031. }
  1032. focus_element = focus_element->GetParentNode();
  1033. }
  1034. }
  1035. }
  1036. DirtyLayout();
  1037. DirtyStackingContext();
  1038. DirtyStructure();
  1039. LockLayout(false);
  1040. return true;
  1041. }
  1042. child_index++;
  1043. }
  1044. return false;
  1045. }
  1046. bool Element::HasChildNodes() const
  1047. {
  1048. return (int) children.size() > num_non_dom_children;
  1049. }
  1050. Element* Element::GetElementById(const String& id)
  1051. {
  1052. // Check for special-case tokens.
  1053. if (id == "#self")
  1054. return this;
  1055. else if (id == "#document")
  1056. return GetOwnerDocument();
  1057. else if (id == "#parent")
  1058. return this->parent;
  1059. else
  1060. {
  1061. Element* search_root = GetOwnerDocument();
  1062. if (search_root == NULL)
  1063. search_root = this;
  1064. return ElementUtilities::GetElementById(search_root, id);
  1065. }
  1066. }
  1067. // Get all elements with the given tag.
  1068. void Element::GetElementsByTagName(ElementList& elements, const String& tag)
  1069. {
  1070. return ElementUtilities::GetElementsByTagName(elements, this, tag);
  1071. }
  1072. // Get all elements with the given class set on them.
  1073. void Element::GetElementsByClassName(ElementList& elements, const String& class_name)
  1074. {
  1075. return ElementUtilities::GetElementsByClassName(elements, this, class_name);
  1076. }
  1077. // Access the event dispatcher
  1078. EventDispatcher* Element::GetEventDispatcher() const
  1079. {
  1080. return event_dispatcher;
  1081. }
  1082. // Access the element background.
  1083. ElementBackground* Element::GetElementBackground() const
  1084. {
  1085. return background;
  1086. }
  1087. // Access the element border.
  1088. ElementBorder* Element::GetElementBorder() const
  1089. {
  1090. return border;
  1091. }
  1092. // Access the element decorators
  1093. ElementDecoration* Element::GetElementDecoration() const
  1094. {
  1095. return decoration;
  1096. }
  1097. // Returns the element's scrollbar functionality.
  1098. ElementScroll* Element::GetElementScroll() const
  1099. {
  1100. return scroll;
  1101. }
  1102. int Element::GetClippingIgnoreDepth()
  1103. {
  1104. if (clipping_state_dirty)
  1105. {
  1106. IsClippingEnabled();
  1107. }
  1108. return clipping_ignore_depth;
  1109. }
  1110. bool Element::IsClippingEnabled()
  1111. {
  1112. if (clipping_state_dirty)
  1113. {
  1114. // Is clipping enabled for this element, yes unless both overlow properties are set to visible
  1115. clipping_enabled = style->GetProperty(OVERFLOW_X)->Get< int >() != OVERFLOW_VISIBLE
  1116. || style->GetProperty(OVERFLOW_Y)->Get< int >() != OVERFLOW_VISIBLE;
  1117. // Get the clipping ignore depth from the clip property
  1118. clipping_ignore_depth = 0;
  1119. const Property* clip_property = GetProperty(CLIP);
  1120. if (clip_property->unit == Property::NUMBER)
  1121. clipping_ignore_depth = clip_property->Get< int >();
  1122. else if (clip_property->Get< int >() == CLIP_NONE)
  1123. clipping_ignore_depth = -1;
  1124. clipping_state_dirty = false;
  1125. }
  1126. return clipping_enabled;
  1127. }
  1128. // Gets the render interface owned by this element's context.
  1129. RenderInterface* Element::GetRenderInterface()
  1130. {
  1131. Context* context = GetContext();
  1132. if (context != NULL)
  1133. return context->GetRenderInterface();
  1134. return Rocket::Core::GetRenderInterface();
  1135. }
  1136. void Element::SetInstancer(ElementInstancer* _instancer)
  1137. {
  1138. // Only record the first instancer being set as some instancers call other instancers to do their dirty work, in
  1139. // which case we don't want to update the lowest level instancer.
  1140. if (instancer == NULL)
  1141. {
  1142. instancer = _instancer;
  1143. instancer->AddReference();
  1144. }
  1145. }
  1146. // Forces the element to generate a local stacking context, regardless of the value of its z-index property.
  1147. void Element::ForceLocalStackingContext()
  1148. {
  1149. local_stacking_context_forced = true;
  1150. local_stacking_context = true;
  1151. DirtyStackingContext();
  1152. }
  1153. // Called during the update loop after children are rendered.
  1154. void Element::OnUpdate()
  1155. {
  1156. }
  1157. // Called during render after backgrounds, borders, decorators, but before children, are rendered.
  1158. void Element::OnRender()
  1159. {
  1160. }
  1161. // Called during a layout operation, when the element is being positioned and sized.
  1162. void Element::OnLayout()
  1163. {
  1164. }
  1165. // Called when attributes on the element are changed.
  1166. void Element::OnAttributeChange(const AttributeNameList& changed_attributes)
  1167. {
  1168. if (changed_attributes.find("id") != changed_attributes.end())
  1169. {
  1170. id = GetAttribute< String >("id", "");
  1171. style->DirtyDefinition();
  1172. }
  1173. if (changed_attributes.find("class") != changed_attributes.end())
  1174. {
  1175. style->SetClassNames(GetAttribute< String >("class", ""));
  1176. }
  1177. // Add any inline style declarations.
  1178. if (changed_attributes.find("style") != changed_attributes.end())
  1179. {
  1180. PropertyDictionary properties;
  1181. StyleSheetParser parser;
  1182. parser.ParseProperties(properties, GetAttribute< String >("style", ""));
  1183. Rocket::Core::PropertyMap property_map = properties.GetProperties();
  1184. for (Rocket::Core::PropertyMap::iterator i = property_map.begin(); i != property_map.end(); ++i)
  1185. {
  1186. SetProperty((*i).first, (*i).second);
  1187. }
  1188. }
  1189. }
  1190. // Called when properties on the element are changed.
  1191. void Element::OnPropertyChange(const PropertyNameList& changed_properties)
  1192. {
  1193. bool all_dirty = StyleSheetSpecification::GetRegisteredProperties() == changed_properties;
  1194. if (!IsLayoutDirty())
  1195. {
  1196. if (all_dirty)
  1197. {
  1198. DirtyLayout();
  1199. }
  1200. else
  1201. {
  1202. // Force a relayout if any of the changed properties require it.
  1203. for (PropertyNameList::const_iterator i = changed_properties.begin(); i != changed_properties.end(); ++i)
  1204. {
  1205. const PropertyDefinition* property_definition = StyleSheetSpecification::GetProperty(*i);
  1206. if (property_definition)
  1207. {
  1208. if (property_definition->IsLayoutForced())
  1209. {
  1210. DirtyLayout();
  1211. break;
  1212. }
  1213. }
  1214. }
  1215. }
  1216. }
  1217. // Update the visibility.
  1218. if (all_dirty || changed_properties.find(VISIBILITY) != changed_properties.end() ||
  1219. changed_properties.find(DISPLAY) != changed_properties.end())
  1220. {
  1221. bool new_visibility = GetDisplay() != DISPLAY_NONE &&
  1222. GetProperty< int >(VISIBILITY) == VISIBILITY_VISIBLE;
  1223. if (visible != new_visibility)
  1224. {
  1225. visible = new_visibility;
  1226. if (parent != NULL)
  1227. parent->DirtyStackingContext();
  1228. }
  1229. if (all_dirty ||
  1230. changed_properties.find(DISPLAY) != changed_properties.end())
  1231. {
  1232. if (parent != NULL)
  1233. parent->DirtyStructure();
  1234. }
  1235. }
  1236. // Update the position.
  1237. if (all_dirty ||
  1238. changed_properties.find(LEFT) != changed_properties.end() ||
  1239. changed_properties.find(RIGHT) != changed_properties.end() ||
  1240. changed_properties.find(TOP) != changed_properties.end() ||
  1241. changed_properties.find(BOTTOM) != changed_properties.end())
  1242. {
  1243. UpdateOffset();
  1244. DirtyOffset();
  1245. }
  1246. // Update the z-index.
  1247. if (all_dirty ||
  1248. changed_properties.find(Z_INDEX) != changed_properties.end())
  1249. {
  1250. const Property* z_index_property = GetProperty(Z_INDEX);
  1251. if (z_index_property->unit == Property::KEYWORD &&
  1252. z_index_property->value.Get< int >() == Z_INDEX_AUTO)
  1253. {
  1254. if (local_stacking_context &&
  1255. !local_stacking_context_forced)
  1256. {
  1257. // We're no longer acting as a stacking context.
  1258. local_stacking_context = false;
  1259. stacking_context_dirty = false;
  1260. stacking_context.clear();
  1261. }
  1262. // If our old z-index was not zero, then we must dirty our stacking context so we'll be re-indexed.
  1263. if (z_index != 0)
  1264. {
  1265. z_index = 0;
  1266. DirtyStackingContext();
  1267. }
  1268. }
  1269. else
  1270. {
  1271. float new_z_index;
  1272. if (z_index_property->unit == Property::KEYWORD)
  1273. {
  1274. if (z_index_property->value.Get< int >() == Z_INDEX_TOP)
  1275. new_z_index = FLT_MAX;
  1276. else
  1277. new_z_index = -FLT_MAX;
  1278. }
  1279. else
  1280. new_z_index = z_index_property->value.Get< float >();
  1281. if (new_z_index != z_index)
  1282. {
  1283. z_index = new_z_index;
  1284. if (parent != NULL)
  1285. parent->DirtyStackingContext();
  1286. }
  1287. if (!local_stacking_context)
  1288. {
  1289. local_stacking_context = true;
  1290. stacking_context_dirty = true;
  1291. }
  1292. }
  1293. }
  1294. // Dirty the background if it's changed.
  1295. if (all_dirty ||
  1296. changed_properties.find(BACKGROUND_COLOR) != changed_properties.end())
  1297. background->DirtyBackground();
  1298. // Dirty the border if it's changed.
  1299. if (all_dirty ||
  1300. changed_properties.find(BORDER_TOP_WIDTH) != changed_properties.end() ||
  1301. changed_properties.find(BORDER_RIGHT_WIDTH) != changed_properties.end() ||
  1302. changed_properties.find(BORDER_BOTTOM_WIDTH) != changed_properties.end() ||
  1303. changed_properties.find(BORDER_LEFT_WIDTH) != changed_properties.end() ||
  1304. changed_properties.find(BORDER_TOP_COLOR) != changed_properties.end() ||
  1305. changed_properties.find(BORDER_RIGHT_COLOR) != changed_properties.end() ||
  1306. changed_properties.find(BORDER_BOTTOM_COLOR) != changed_properties.end() ||
  1307. changed_properties.find(BORDER_LEFT_COLOR) != changed_properties.end())
  1308. border->DirtyBorder();
  1309. // Fetch a new font face if it has been changed.
  1310. if (all_dirty ||
  1311. changed_properties.find(FONT_FAMILY) != changed_properties.end() ||
  1312. changed_properties.find(FONT_CHARSET) != changed_properties.end() ||
  1313. changed_properties.find(FONT_WEIGHT) != changed_properties.end() ||
  1314. changed_properties.find(FONT_STYLE) != changed_properties.end() ||
  1315. changed_properties.find(FONT_SIZE) != changed_properties.end())
  1316. {
  1317. // Store the old em; if it changes, then we need to dirty all em-relative properties.
  1318. int old_em = -1;
  1319. if (font_face_handle != NULL)
  1320. old_em = font_face_handle->GetLineHeight();
  1321. // Fetch the new font face.
  1322. FontFaceHandle* new_font_face_handle = ElementUtilities::GetFontFaceHandle(this);
  1323. // If this is different from our current font face, then we've got to nuke
  1324. // all our characters and tell our parent that we have to be re-laid out.
  1325. if (new_font_face_handle != font_face_handle)
  1326. {
  1327. if (font_face_handle)
  1328. font_face_handle->RemoveReference();
  1329. font_face_handle = new_font_face_handle;
  1330. // Our font face has changed; odds are, so has our em. All of our em-relative values
  1331. // have therefore probably changed as well, so we'll need to dirty them.
  1332. int new_em = -1;
  1333. if (font_face_handle != NULL)
  1334. new_em = font_face_handle->GetLineHeight();
  1335. if (old_em != new_em)
  1336. {
  1337. style->DirtyEmProperties();
  1338. }
  1339. }
  1340. else if (new_font_face_handle != NULL)
  1341. new_font_face_handle->RemoveReference();
  1342. }
  1343. // Check for clipping state changes
  1344. if (all_dirty ||
  1345. changed_properties.find(CLIP) != changed_properties.end() ||
  1346. changed_properties.find(OVERFLOW_X) != changed_properties.end() ||
  1347. changed_properties.find(OVERFLOW_Y) != changed_properties.end())
  1348. {
  1349. clipping_state_dirty = true;
  1350. }
  1351. }
  1352. // Called when a child node has been added somewhere in the hierarchy
  1353. void Element::OnChildAdd(Element* child)
  1354. {
  1355. if (parent)
  1356. parent->OnChildAdd(child);
  1357. }
  1358. // Called when a child node has been removed somewhere in the hierarchy
  1359. void Element::OnChildRemove(Element* child)
  1360. {
  1361. if (parent)
  1362. parent->OnChildRemove(child);
  1363. }
  1364. // Update the element's layout if required.
  1365. void Element::UpdateLayout()
  1366. {
  1367. ElementDocument* document = GetOwnerDocument();
  1368. if (document != NULL)
  1369. document->UpdateLayout();
  1370. }
  1371. // Forces a re-layout of this element, and any other children required.
  1372. void Element::DirtyLayout()
  1373. {
  1374. Element* document = GetOwnerDocument();
  1375. if (document != NULL)
  1376. document->DirtyLayout();
  1377. }
  1378. /// Increment/Decrement the layout lock
  1379. void Element::LockLayout(bool lock)
  1380. {
  1381. Element* document = GetOwnerDocument();
  1382. if (document != NULL)
  1383. document->LockLayout(lock);
  1384. }
  1385. // Forces a re-layout of this element, and any other children required.
  1386. bool Element::IsLayoutDirty()
  1387. {
  1388. Element* document = GetOwnerDocument();
  1389. if (document != NULL)
  1390. return document->IsLayoutDirty();
  1391. return false;
  1392. }
  1393. // Forces a reevaluation of applicable font effects.
  1394. void Element::DirtyFont()
  1395. {
  1396. for (size_t i = 0; i < children.size(); ++i)
  1397. children[i]->DirtyFont();
  1398. }
  1399. void Element::OnReferenceDeactivate()
  1400. {
  1401. if (instancer)
  1402. {
  1403. instancer->ReleaseElement(this);
  1404. }
  1405. else
  1406. {
  1407. // Hopefully we can just delete ourselves.
  1408. //delete this;
  1409. Log::Message(Log::LT_WARNING, "Leak detected: element %s not instanced via Rocket Factory. Unable to release.", GetAddress().CString());
  1410. }
  1411. }
  1412. void Element::ProcessEvent(Event& event)
  1413. {
  1414. if (event == MOUSEDOWN && IsPointWithinElement(Vector2f(event.GetParameter< float >("mouse_x", 0), event.GetParameter< float >("mouse_y", 0))) &&
  1415. event.GetParameter< int >("button", 0) == 0)
  1416. SetPseudoClass("active", true);
  1417. if (event == MOUSESCROLL)
  1418. {
  1419. int wheel_delta = event.GetParameter< int >("wheel_delta", 0);
  1420. if ((wheel_delta < 0 && GetScrollTop() > 0) ||
  1421. (wheel_delta > 0 && GetScrollHeight() > GetScrollTop() + GetClientHeight()))
  1422. {
  1423. int overflow_property = GetProperty< int >(OVERFLOW_Y);
  1424. if (overflow_property == OVERFLOW_AUTO ||
  1425. overflow_property == OVERFLOW_SCROLL)
  1426. {
  1427. SetScrollTop(GetScrollTop() + wheel_delta * ElementUtilities::GetLineHeight(this));
  1428. event.StopPropagation();
  1429. }
  1430. }
  1431. return;
  1432. }
  1433. if (event.GetTargetElement() == this)
  1434. {
  1435. if (event == MOUSEOVER)
  1436. SetPseudoClass("hover", true);
  1437. else if (event == MOUSEOUT)
  1438. SetPseudoClass("hover", false);
  1439. else if (event == FOCUS)
  1440. SetPseudoClass(FOCUS, true);
  1441. else if (event == BLUR)
  1442. SetPseudoClass(FOCUS, false);
  1443. }
  1444. }
  1445. void Element::GetRML(String& content)
  1446. {
  1447. // First we start the open tag, add the attributes then close the open tag.
  1448. // Then comes the children in order, then we add our close tag.
  1449. content.Append("<");
  1450. content.Append(tag);
  1451. int index = 0;
  1452. String name;
  1453. String value;
  1454. while (IterateAttributes(index, name, value))
  1455. {
  1456. size_t length = name.Length() + value.Length() + 8;
  1457. String attribute(length, " %s=\"%s\"", name.CString(), value.CString());
  1458. content.Append(attribute);
  1459. }
  1460. if (HasChildNodes())
  1461. {
  1462. content.Append(">");
  1463. GetInnerRML(content);
  1464. content.Append("</");
  1465. content.Append(tag);
  1466. content.Append(">");
  1467. }
  1468. else
  1469. {
  1470. content.Append(" />");
  1471. }
  1472. }
  1473. void Element::SetParent(Element* _parent)
  1474. {
  1475. // If there's an old parent, detach from it first.
  1476. if (parent &&
  1477. parent != _parent)
  1478. parent->RemoveChild(this);
  1479. // Save our parent
  1480. parent = _parent;
  1481. }
  1482. void Element::ReleaseDeletedElements()
  1483. {
  1484. for (size_t i = 0; i < active_children.size(); i++)
  1485. {
  1486. active_children[i]->ReleaseDeletedElements();
  1487. }
  1488. ReleaseElements(deleted_children);
  1489. active_children = children;
  1490. }
  1491. void Element::ReleaseElements(ElementList& released_elements)
  1492. {
  1493. // Remove deleted children from this element.
  1494. while (!released_elements.empty())
  1495. {
  1496. Element* element = released_elements.back();
  1497. released_elements.pop_back();
  1498. // If this element has been added back into our list, then we remove our previous oustanding reference on it
  1499. // and continue.
  1500. if (std::find(children.begin(), children.end(), element) != children.end())
  1501. {
  1502. element->RemoveReference();
  1503. continue;
  1504. }
  1505. // Set the parent to NULL unless it's been reparented already.
  1506. if (element->GetParentNode() == this)
  1507. element->parent = NULL;
  1508. element->RemoveReference();
  1509. }
  1510. }
  1511. void Element::DirtyOffset()
  1512. {
  1513. offset_dirty = true;
  1514. // Not strictly true ... ?
  1515. for (size_t i = 0; i < children.size(); i++)
  1516. children[i]->DirtyOffset();
  1517. }
  1518. void Element::UpdateOffset()
  1519. {
  1520. int position_property = GetPosition();
  1521. if (position_property == POSITION_ABSOLUTE ||
  1522. position_property == POSITION_FIXED)
  1523. {
  1524. if (offset_parent != NULL)
  1525. {
  1526. const Box& parent_box = offset_parent->GetBox();
  1527. Vector2f containing_block = parent_box.GetSize(Box::PADDING);
  1528. const Property *left = GetLocalProperty(LEFT);
  1529. const Property *right = GetLocalProperty(RIGHT);
  1530. // If the element is anchored left, then the position is offset by that resolved value.
  1531. if (left != NULL && left->unit != Property::KEYWORD)
  1532. relative_offset_base.x = parent_box.GetEdge(Box::BORDER, Box::LEFT) + (ResolveProperty(LEFT, containing_block.x) + GetBox().GetEdge(Box::MARGIN, Box::LEFT));
  1533. // If the element is anchored right, then the position is set first so the element's right-most edge
  1534. // (including margins) will render up against the containing box's right-most content edge, and then
  1535. // offset by the resolved value.
  1536. if (right != NULL && right->unit != Property::KEYWORD)
  1537. 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));
  1538. const Property *top = GetLocalProperty(TOP);
  1539. const Property *bottom = GetLocalProperty(BOTTOM);
  1540. // If the element is anchored top, then the position is offset by that resolved value.
  1541. if (top != NULL && top->unit != Property::KEYWORD)
  1542. relative_offset_base.y = parent_box.GetEdge(Box::BORDER, Box::TOP) + (ResolveProperty(TOP, containing_block.y) + GetBox().GetEdge(Box::MARGIN, Box::TOP));
  1543. // If the element is anchored bottom, then the position is set first so the element's right-most edge
  1544. // (including margins) will render up against the containing box's right-most content edge, and then
  1545. // offset by the resolved value.
  1546. else if (bottom != NULL && bottom->unit != Property::KEYWORD)
  1547. 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));
  1548. }
  1549. }
  1550. else if (position_property == POSITION_RELATIVE)
  1551. {
  1552. if (offset_parent != NULL)
  1553. {
  1554. const Box& parent_box = offset_parent->GetBox();
  1555. Vector2f containing_block = parent_box.GetSize();
  1556. const Property *left = GetLocalProperty(LEFT);
  1557. const Property *right = GetLocalProperty(RIGHT);
  1558. if (left != NULL && left->unit != Property::KEYWORD)
  1559. relative_offset_position.x = ResolveProperty(LEFT, containing_block.x);
  1560. else if (right != NULL && right->unit != Property::KEYWORD)
  1561. relative_offset_position.x = -1 * ResolveProperty(RIGHT, containing_block.x);
  1562. else
  1563. relative_offset_position.x = 0;
  1564. const Property *top = GetLocalProperty(TOP);
  1565. const Property *bottom = GetLocalProperty(BOTTOM);
  1566. if (top != NULL && top->unit != Property::KEYWORD)
  1567. relative_offset_position.y = ResolveProperty(TOP, containing_block.y);
  1568. else if (bottom != NULL && bottom->unit != Property::KEYWORD)
  1569. relative_offset_position.y = -1 * ResolveProperty(BOTTOM, containing_block.y);
  1570. else
  1571. relative_offset_position.y = 0;
  1572. }
  1573. }
  1574. else
  1575. {
  1576. relative_offset_position.x = 0;
  1577. relative_offset_position.y = 0;
  1578. }
  1579. LayoutEngine::Round(relative_offset_base);
  1580. LayoutEngine::Round(relative_offset_position);
  1581. }
  1582. void Element::BuildLocalStackingContext()
  1583. {
  1584. stacking_context_dirty = false;
  1585. stacking_context.clear();
  1586. BuildStackingContext(&stacking_context);
  1587. std::stable_sort(stacking_context.begin(), stacking_context.end(), ElementSortZIndex());
  1588. }
  1589. void Element::BuildStackingContext(ElementList* new_stacking_context)
  1590. {
  1591. // Build the list of ordered children. Our child list is sorted within the stacking context so stacked elements
  1592. // will render in the right order; ie, positioned elements will render on top of inline elements, which will render
  1593. // on top of floated elements, which will render on top of block elements.
  1594. std::vector< std::pair< Element*, float > > ordered_children;
  1595. for (size_t i = 0; i < children.size(); ++i)
  1596. {
  1597. Element* child = children[i];
  1598. if (!child->IsVisible())
  1599. continue;
  1600. std::pair< Element*, float > ordered_child;
  1601. ordered_child.first = child;
  1602. if (child->GetPosition() != POSITION_STATIC)
  1603. ordered_child.second = 3;
  1604. else if (child->GetFloat() != FLOAT_NONE)
  1605. ordered_child.second = 1;
  1606. else if (child->GetDisplay() == DISPLAY_BLOCK)
  1607. ordered_child.second = 0;
  1608. else
  1609. ordered_child.second = 2;
  1610. ordered_children.push_back(ordered_child);
  1611. }
  1612. // Sort the list!
  1613. std::stable_sort(ordered_children.begin(), ordered_children.end(), ElementSortZOrder());
  1614. // Add the list of ordered children into the stacking context in order.
  1615. for (size_t i = 0; i < ordered_children.size(); ++i)
  1616. {
  1617. new_stacking_context->push_back(ordered_children[i].first);
  1618. if (!ordered_children[i].first->local_stacking_context)
  1619. ordered_children[i].first->BuildStackingContext(new_stacking_context);
  1620. }
  1621. }
  1622. void Element::DirtyStackingContext()
  1623. {
  1624. // The first ancestor of ours that doesn't have an automatic z-index is the ancestor that is establishing our local
  1625. // stacking context.
  1626. Element* stacking_context_parent = this;
  1627. while (stacking_context_parent != NULL &&
  1628. !stacking_context_parent->local_stacking_context)
  1629. stacking_context_parent = stacking_context_parent->GetParentNode();
  1630. if (stacking_context_parent != NULL)
  1631. stacking_context_parent->stacking_context_dirty = true;
  1632. }
  1633. void Element::DirtyStructure()
  1634. {
  1635. // Clear the cached owner document
  1636. owner_document = NULL;
  1637. // Inform all children that the structure is drity
  1638. for (size_t i = 0; i < children.size(); ++i)
  1639. {
  1640. const ElementDefinition* element_definition = children[i]->GetStyle()->GetDefinition();
  1641. if (element_definition != NULL &&
  1642. element_definition->IsStructurallyVolatile())
  1643. {
  1644. children[i]->GetStyle()->DirtyDefinition();
  1645. }
  1646. children[i]->DirtyStructure();
  1647. }
  1648. }
  1649. }
  1650. }