Element.cpp 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989
  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 <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 "../../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) const // uniquejack add const
  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) const // uniquejack add const
  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) : 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)
  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_PARAMETER(dimensions))
  377. {
  378. ROCKET_UNUSED(dimensions);
  379. return false;
  380. }
  381. // Checks if a given point in screen coordinates lies within the bordered area of this element.
  382. bool Element::IsPointWithinElement(const Vector2f& point)
  383. {
  384. Vector2f position = GetAbsoluteOffset(Box::BORDER);
  385. for (int i = 0; i < GetNumBoxes(); ++i)
  386. {
  387. const Box& box = GetBox(i);
  388. Vector2f box_position = position + box.GetOffset();
  389. Vector2f box_dimensions = box.GetSize(Box::BORDER);
  390. if (point.x >= box_position.x &&
  391. point.x <= (box_position.x + box_dimensions.x) &&
  392. point.y >= box_position.y &&
  393. point.y <= (box_position.y + box_dimensions.y))
  394. {
  395. return true;
  396. }
  397. }
  398. return false;
  399. }
  400. // Returns the visibility of the element.
  401. bool Element::IsVisible() const
  402. {
  403. return visible;
  404. }
  405. // Returns the z-index of the element.
  406. float Element::GetZIndex() const
  407. {
  408. return z_index;
  409. }
  410. // Returns the element's font face handle.
  411. FontFaceHandle* Element::GetFontFaceHandle() const
  412. {
  413. return font_face_handle;
  414. }
  415. // Sets a local property override on the element.
  416. bool Element::SetProperty(const String& name, const String& value)
  417. {
  418. return style->SetProperty(name, value);
  419. }
  420. // Removes a local property override on the element.
  421. void Element::RemoveProperty(const String& name)
  422. {
  423. style->RemoveProperty(name);
  424. }
  425. // Sets a local property override on the element to a pre-parsed value.
  426. bool Element::SetProperty(const String& name, const Property& property)
  427. {
  428. return style->SetProperty(name, property);
  429. }
  430. // Returns one of this element's properties.
  431. const Property* Element::GetProperty(const String& name)
  432. {
  433. return style->GetProperty(name);
  434. }
  435. // Returns one of this element's properties.
  436. const Property* Element::GetLocalProperty(const String& name)
  437. {
  438. return style->GetLocalProperty(name);
  439. }
  440. // Resolves one of this element's style.
  441. float Element::ResolveProperty(const String& name, float base_value)
  442. {
  443. return style->ResolveProperty(name, base_value);
  444. }
  445. // Resolves one of this element's style.
  446. float Element::ResolveProperty(const Property *property, float base_value)
  447. {
  448. return style->ResolveProperty(property, base_value);
  449. }
  450. void Element::GetOffsetProperties(const Property **top, const Property **bottom, const Property **left, const Property **right )
  451. {
  452. style->GetOffsetProperties(top, bottom, left, right);
  453. }
  454. void Element::GetBorderWidthProperties(const Property **border_top, const Property **border_bottom, const Property **border_left, const Property **bottom_right)
  455. {
  456. style->GetBorderWidthProperties(border_top, border_bottom, border_left, bottom_right);
  457. }
  458. void Element::GetMarginProperties(const Property **margin_top, const Property **margin_bottom, const Property **margin_left, const Property **margin_right)
  459. {
  460. style->GetMarginProperties(margin_top, margin_bottom, margin_left, margin_right);
  461. }
  462. void Element::GetPaddingProperties(const Property **padding_top, const Property **padding_bottom, const Property **padding_left, const Property **padding_right)
  463. {
  464. style->GetPaddingProperties(padding_top, padding_bottom, padding_left, padding_right);
  465. }
  466. void Element::GetDimensionProperties(const Property **width, const Property **height)
  467. {
  468. style->GetDimensionProperties(width, height);
  469. }
  470. void Element::GetLocalDimensionProperties(const Property **width, const Property **height)
  471. {
  472. style->GetLocalDimensionProperties(width, height);
  473. }
  474. void Element::GetOverflow(int *overflow_x, int *overflow_y)
  475. {
  476. style->GetOverflow(overflow_x, overflow_y);
  477. }
  478. int Element::GetPosition()
  479. {
  480. return style->GetPosition();
  481. }
  482. int Element::GetFloat()
  483. {
  484. return style->GetFloat();
  485. }
  486. int Element::GetDisplay()
  487. {
  488. return style->GetDisplay();
  489. }
  490. int Element::GetWhitespace()
  491. {
  492. return style->GetWhitespace();
  493. }
  494. int Element::GetPointerEvents()
  495. {
  496. return style->GetPointerEvents();
  497. }
  498. const Property *Element::GetLineHeightProperty()
  499. {
  500. return style->GetLineHeightProperty();
  501. }
  502. int Element::GetTextAlign()
  503. {
  504. return style->GetTextAlign();
  505. }
  506. int Element::GetTextTransform()
  507. {
  508. return style->GetTextTransform();
  509. }
  510. const Property *Element::GetVerticalAlignProperty()
  511. {
  512. return style->GetVerticalAlignProperty();
  513. }
  514. // Iterates over the properties defined on this element.
  515. bool Element::IterateProperties(int& index, PseudoClassList& pseudo_classes, String& name, const Property*& property) const
  516. {
  517. return style->IterateProperties(index, pseudo_classes, name, property);
  518. }
  519. // Sets or removes a pseudo-class on the element.
  520. void Element::SetPseudoClass(const String& pseudo_class, bool activate)
  521. {
  522. style->SetPseudoClass(pseudo_class, activate);
  523. }
  524. // Checks if a specific pseudo-class has been set on the element.
  525. bool Element::IsPseudoClassSet(const String& pseudo_class) const
  526. {
  527. return style->IsPseudoClassSet(pseudo_class);
  528. }
  529. // Checks if a complete set of pseudo-classes are set on the element.
  530. bool Element::ArePseudoClassesSet(const PseudoClassList& pseudo_classes) const
  531. {
  532. for (PseudoClassList::const_iterator i = pseudo_classes.begin(); i != pseudo_classes.end(); ++i)
  533. {
  534. if (!IsPseudoClassSet(*i))
  535. return false;
  536. }
  537. return true;
  538. }
  539. // Gets a list of the current active pseudo classes
  540. const PseudoClassList& Element::GetActivePseudoClasses() const
  541. {
  542. return style->GetActivePseudoClasses();
  543. }
  544. /// Get the named attribute
  545. Variant* Element::GetAttribute(const String& name) const
  546. {
  547. return attributes.Get(name);
  548. }
  549. // Checks if the element has a certain attribute.
  550. bool Element::HasAttribute(const String& name)
  551. {
  552. return attributes.Get(name) != NULL;
  553. }
  554. // Removes an attribute from the element
  555. void Element::RemoveAttribute(const String& name)
  556. {
  557. if (attributes.Remove(name))
  558. {
  559. AttributeNameList changed_attributes;
  560. changed_attributes.insert(name);
  561. OnAttributeChange(changed_attributes);
  562. }
  563. }
  564. // Gets the outer most focus element down the tree from this node
  565. Element* Element::GetFocusLeafNode()
  566. {
  567. // If there isn't a focus, then we are the leaf.
  568. if (!focus)
  569. {
  570. return this;
  571. }
  572. // Recurse down the tree until we found the leaf focus element
  573. Element* focus_element = focus;
  574. while (focus_element->focus)
  575. focus_element = focus_element->focus;
  576. return focus_element;
  577. }
  578. // Returns the element's context.
  579. Context* Element::GetContext()
  580. {
  581. ElementDocument* document = GetOwnerDocument();
  582. if (document != NULL)
  583. return document->GetContext();
  584. return NULL;
  585. }
  586. // Set a group of attributes
  587. void Element::SetAttributes(const ElementAttributes* _attributes)
  588. {
  589. int index = 0;
  590. String key;
  591. Variant* value;
  592. AttributeNameList changed_attributes;
  593. while (_attributes->Iterate(index, key, value))
  594. {
  595. changed_attributes.insert(key);
  596. attributes.Set(key, *value);
  597. }
  598. OnAttributeChange(changed_attributes);
  599. }
  600. // Returns the number of attributes on the element.
  601. int Element::GetNumAttributes() const
  602. {
  603. return attributes.Size();
  604. }
  605. // Iterates over all decorators attached to the element.
  606. bool Element::IterateDecorators(int& index, PseudoClassList& pseudo_classes, String& name, Decorator*& decorator, DecoratorDataHandle& decorator_data)
  607. {
  608. return decoration->IterateDecorators(index, pseudo_classes, name, decorator, decorator_data);
  609. }
  610. // Gets the name of the element.
  611. const String& Element::GetTagName() const
  612. {
  613. return tag;
  614. }
  615. // Gets the ID of the element.
  616. const String& Element::GetId() const
  617. {
  618. return id;
  619. }
  620. // Sets the ID of the element.
  621. void Element::SetId(const String& _id)
  622. {
  623. SetAttribute("id", _id);
  624. }
  625. // Gets the horizontal offset from the context's left edge to element's left border edge.
  626. float Element::GetAbsoluteLeft()
  627. {
  628. return GetAbsoluteOffset(Box::BORDER).x;
  629. }
  630. // Gets the vertical offset from the context's top edge to element's top border edge.
  631. float Element::GetAbsoluteTop()
  632. {
  633. return GetAbsoluteOffset(Box::BORDER).y;
  634. }
  635. // Gets the width of the left border of an element.
  636. float Element::GetClientLeft()
  637. {
  638. UpdateLayout();
  639. return GetBox().GetPosition(client_area).x;
  640. }
  641. // Gets the height of the top border of an element.
  642. float Element::GetClientTop()
  643. {
  644. UpdateLayout();
  645. return GetBox().GetPosition(client_area).y;
  646. }
  647. // Gets the inner width of the element.
  648. float Element::GetClientWidth()
  649. {
  650. UpdateLayout();
  651. return GetBox().GetSize(client_area).x - scroll->GetScrollbarSize(ElementScroll::VERTICAL);
  652. }
  653. // Gets the inner height of the element.
  654. float Element::GetClientHeight()
  655. {
  656. UpdateLayout();
  657. return GetBox().GetSize(client_area).y - scroll->GetScrollbarSize(ElementScroll::HORIZONTAL);
  658. }
  659. // Returns the element from which all offset calculations are currently computed.
  660. Element* Element::GetOffsetParent()
  661. {
  662. return offset_parent;
  663. }
  664. // Gets the distance from this element's left border to its offset parent's left border.
  665. float Element::GetOffsetLeft()
  666. {
  667. UpdateLayout();
  668. return relative_offset_base.x + relative_offset_position.x;
  669. }
  670. // Gets the distance from this element's top border to its offset parent's top border.
  671. float Element::GetOffsetTop()
  672. {
  673. UpdateLayout();
  674. return relative_offset_base.y + relative_offset_position.y;
  675. }
  676. // Gets the width of the element, including the client area, padding, borders and scrollbars, but not margins.
  677. float Element::GetOffsetWidth()
  678. {
  679. UpdateLayout();
  680. return GetBox().GetSize(Box::BORDER).x;
  681. }
  682. // Gets the height of the element, including the client area, padding, borders and scrollbars, but not margins.
  683. float Element::GetOffsetHeight()
  684. {
  685. UpdateLayout();
  686. return GetBox().GetSize(Box::BORDER).y;
  687. }
  688. // Gets the left scroll offset of the element.
  689. float Element::GetScrollLeft()
  690. {
  691. UpdateLayout();
  692. return scroll_offset.x;
  693. }
  694. // Sets the left scroll offset of the element.
  695. void Element::SetScrollLeft(float scroll_left)
  696. {
  697. scroll_offset.x = LayoutEngine::Round(Math::Clamp(scroll_left, 0.0f, GetScrollWidth() - GetClientWidth()));
  698. scroll->UpdateScrollbar(ElementScroll::HORIZONTAL);
  699. DirtyOffset();
  700. DispatchEvent("scroll", Dictionary());
  701. }
  702. // Gets the top scroll offset of the element.
  703. float Element::GetScrollTop()
  704. {
  705. UpdateLayout();
  706. return scroll_offset.y;
  707. }
  708. // Sets the top scroll offset of the element.
  709. void Element::SetScrollTop(float scroll_top)
  710. {
  711. scroll_offset.y = LayoutEngine::Round(Math::Clamp(scroll_top, 0.0f, GetScrollHeight() - GetClientHeight()));
  712. scroll->UpdateScrollbar(ElementScroll::VERTICAL);
  713. DirtyOffset();
  714. DispatchEvent("scroll", Dictionary());
  715. }
  716. // Gets the width of the scrollable content of the element; it includes the element padding but not its margin.
  717. float Element::GetScrollWidth()
  718. {
  719. return Math::Max(content_box.x, GetClientWidth());
  720. }
  721. // Gets the height of the scrollable content of the element; it includes the element padding but not its margin.
  722. float Element::GetScrollHeight()
  723. {
  724. return Math::Max(content_box.y, GetClientHeight());
  725. }
  726. // Gets the object representing the declarations of an element's style attributes.
  727. ElementStyle* Element::GetStyle()
  728. {
  729. return style;
  730. }
  731. // Gets the document this element belongs to.
  732. ElementDocument* Element::GetOwnerDocument()
  733. {
  734. if (parent == NULL)
  735. return NULL;
  736. if (!owner_document)
  737. {
  738. owner_document = parent->GetOwnerDocument();
  739. }
  740. return owner_document;
  741. }
  742. // Gets this element's parent node.
  743. Element* Element::GetParentNode() const
  744. {
  745. return parent;
  746. }
  747. // Gets the element immediately following this one in the tree.
  748. Element* Element::GetNextSibling() const
  749. {
  750. if (parent == NULL)
  751. return NULL;
  752. for (size_t i = 0; i < parent->children.size() - (parent->num_non_dom_children + 1); i++)
  753. {
  754. if (parent->children[i] == this)
  755. return parent->children[i + 1];
  756. }
  757. return NULL;
  758. }
  759. // Gets the element immediately preceding this one in the tree.
  760. Element* Element::GetPreviousSibling() const
  761. {
  762. if (parent == NULL)
  763. return NULL;
  764. for (size_t i = 1; i < parent->children.size() - parent->num_non_dom_children; i++)
  765. {
  766. if (parent->children[i] == this)
  767. return parent->children[i - 1];
  768. }
  769. return NULL;
  770. }
  771. // Returns the first child of this element.
  772. Element* Element::GetFirstChild() const
  773. {
  774. if (GetNumChildren() > 0)
  775. return children[0];
  776. return NULL;
  777. }
  778. // Gets the last child of this element.
  779. Element* Element::GetLastChild() const
  780. {
  781. if (GetNumChildren() > 0)
  782. return *(children.end() - (num_non_dom_children + 1));
  783. return NULL;
  784. }
  785. Element* Element::GetChild(int index) const
  786. {
  787. if (index < 0 || index >= (int) children.size())
  788. return NULL;
  789. return children[index];
  790. }
  791. int Element::GetNumChildren(bool include_non_dom_elements) const
  792. {
  793. return (int) children.size() - (include_non_dom_elements ? 0 : num_non_dom_children);
  794. }
  795. // Gets the markup and content of the element.
  796. void Element::GetInnerRML(String& content) const
  797. {
  798. for (int i = 0; i < GetNumChildren(); i++)
  799. {
  800. children[i]->GetRML(content);
  801. }
  802. }
  803. // Gets the markup and content of the element.
  804. String Element::GetInnerRML() const {
  805. String result;
  806. GetInnerRML(result);
  807. return result;
  808. }
  809. // Sets the markup and content of the element. All existing children will be replaced.
  810. void Element::SetInnerRML(const String& rml)
  811. {
  812. // Remove all DOM children.
  813. while ((int) children.size() > num_non_dom_children)
  814. RemoveChild(children.front());
  815. Factory::InstanceElementText(this, rml);
  816. }
  817. // Sets the current element as the focus object.
  818. bool Element::Focus()
  819. {
  820. // Are we allowed focus?
  821. int focus_property = GetProperty< int >(FOCUS);
  822. if (focus_property == FOCUS_NONE)
  823. return false;
  824. // Ask our context if we can switch focus.
  825. Context* context = GetContext();
  826. if (context == NULL)
  827. return false;
  828. if (!context->OnFocusChange(this))
  829. return false;
  830. // Set this as the end of the focus chain.
  831. focus = NULL;
  832. // Update the focus chain up the hierarchy.
  833. Element* element = this;
  834. while (element->GetParentNode())
  835. {
  836. element->GetParentNode()->focus = element;
  837. element = element->GetParentNode();
  838. }
  839. return true;
  840. }
  841. // Removes focus from from this element.
  842. void Element::Blur()
  843. {
  844. if (parent)
  845. {
  846. Context* context = GetContext();
  847. if (context == NULL)
  848. return;
  849. if (context->GetFocusElement() == this)
  850. {
  851. parent->Focus();
  852. }
  853. else if (parent->focus == this)
  854. {
  855. parent->focus = NULL;
  856. }
  857. }
  858. }
  859. // Fakes a mouse click on this element.
  860. void Element::Click()
  861. {
  862. Context* context = GetContext();
  863. if (context == NULL)
  864. return;
  865. context->GenerateClickEvent(this);
  866. }
  867. // Adds an event listener
  868. void Element::AddEventListener(const String& event, EventListener* listener, bool in_capture_phase)
  869. {
  870. event_dispatcher->AttachEvent(event, listener, in_capture_phase);
  871. }
  872. // Removes an event listener from this element.
  873. void Element::RemoveEventListener(const String& event, EventListener* listener, bool in_capture_phase)
  874. {
  875. event_dispatcher->DetachEvent(event, listener, in_capture_phase);
  876. }
  877. // Dispatches the specified event
  878. bool Element::DispatchEvent(const String& event, const Dictionary& parameters, bool interruptible)
  879. {
  880. return event_dispatcher->DispatchEvent(this, event, parameters, interruptible);
  881. }
  882. // Scrolls the parent element's contents so that this element is visible.
  883. void Element::ScrollIntoView(bool align_with_top)
  884. {
  885. Vector2f size(0, 0);
  886. if (!align_with_top &&
  887. !boxes.empty())
  888. {
  889. size.y = boxes.back().GetOffset().y +
  890. boxes.back().GetSize(Box::BORDER).y;
  891. }
  892. Element* scroll_parent = parent;
  893. while (scroll_parent != NULL)
  894. {
  895. int overflow_x_property = scroll_parent->GetProperty< int >(OVERFLOW_X);
  896. int overflow_y_property = scroll_parent->GetProperty< int >(OVERFLOW_Y);
  897. if ((overflow_x_property != OVERFLOW_VISIBLE &&
  898. scroll_parent->GetScrollWidth() > scroll_parent->GetClientWidth()) ||
  899. (overflow_y_property != OVERFLOW_VISIBLE &&
  900. scroll_parent->GetScrollHeight() > scroll_parent->GetClientHeight()))
  901. {
  902. Vector2f offset = scroll_parent->GetAbsoluteOffset(Box::BORDER) - GetAbsoluteOffset(Box::BORDER);
  903. Vector2f scroll_offset(scroll_parent->GetScrollLeft(), scroll_parent->GetScrollTop());
  904. scroll_offset -= offset;
  905. scroll_offset.x += scroll_parent->GetClientLeft();
  906. scroll_offset.y += scroll_parent->GetClientTop();
  907. if (!align_with_top)
  908. scroll_offset.y -= (scroll_parent->GetClientHeight() - size.y);
  909. if (overflow_x_property != OVERFLOW_VISIBLE)
  910. scroll_parent->SetScrollLeft(scroll_offset.x);
  911. if (overflow_y_property != OVERFLOW_VISIBLE)
  912. scroll_parent->SetScrollTop(scroll_offset.y);
  913. }
  914. scroll_parent = scroll_parent->GetParentNode();
  915. }
  916. }
  917. // Appends a child to this element
  918. void Element::AppendChild(Element* child, bool dom_element)
  919. {
  920. LockLayout(true);
  921. child->AddReference();
  922. child->SetParent(this);
  923. if (dom_element)
  924. children.insert(children.end() - num_non_dom_children, child);
  925. else
  926. {
  927. children.push_back(child);
  928. num_non_dom_children++;
  929. }
  930. child->GetStyle()->DirtyDefinition();
  931. child->GetStyle()->DirtyProperties();
  932. child->OnChildAdd(child);
  933. DirtyStackingContext();
  934. DirtyStructure();
  935. if (dom_element)
  936. DirtyLayout();
  937. LockLayout(false);
  938. }
  939. // Adds a child to this element, directly after the adjacent element. Inherits
  940. // the dom/non-dom status from the adjacent element.
  941. void Element::InsertBefore(Element* child, Element* adjacent_element)
  942. {
  943. // Find the position in the list of children of the adjacent element. If
  944. // it's NULL or we can't find it, then we insert it at the end of the dom
  945. // children, as a dom element.
  946. size_t child_index = 0;
  947. bool found_child = false;
  948. if (adjacent_element)
  949. {
  950. for (child_index = 0; child_index < children.size(); child_index++)
  951. {
  952. if (children[child_index] == adjacent_element)
  953. {
  954. found_child = true;
  955. break;
  956. }
  957. }
  958. }
  959. if (found_child)
  960. {
  961. LockLayout(true);
  962. child->AddReference();
  963. child->SetParent(this);
  964. if ((int) child_index >= GetNumChildren())
  965. num_non_dom_children++;
  966. else
  967. DirtyLayout();
  968. children.insert(children.begin() + child_index, child);
  969. child->GetStyle()->DirtyDefinition();
  970. child->GetStyle()->DirtyProperties();
  971. child->OnChildAdd(child);
  972. DirtyStackingContext();
  973. DirtyStructure();
  974. LockLayout(false);
  975. }
  976. else
  977. {
  978. AppendChild(child);
  979. }
  980. }
  981. // Replaces the second node with the first node.
  982. bool Element::ReplaceChild(Element* inserted_element, Element* replaced_element)
  983. {
  984. inserted_element->AddReference();
  985. inserted_element->SetParent(this);
  986. ElementList::iterator insertion_point = children.begin();
  987. while (insertion_point != children.end() && *insertion_point != replaced_element)
  988. {
  989. ++insertion_point;
  990. }
  991. if (insertion_point == children.end())
  992. {
  993. AppendChild(inserted_element);
  994. return false;
  995. }
  996. LockLayout(true);
  997. children.insert(insertion_point, inserted_element);
  998. RemoveChild(replaced_element);
  999. inserted_element->GetStyle()->DirtyDefinition();
  1000. inserted_element->GetStyle()->DirtyProperties();
  1001. inserted_element->OnChildAdd(inserted_element);
  1002. LockLayout(false);
  1003. return true;
  1004. }
  1005. // Removes the specified child
  1006. bool Element::RemoveChild(Element* child)
  1007. {
  1008. size_t child_index = 0;
  1009. for (ElementList::iterator itr = children.begin(); itr != children.end(); ++itr)
  1010. {
  1011. // Add the element to the delete list
  1012. if ((*itr) == child)
  1013. {
  1014. LockLayout(true);
  1015. // Inform the context of the element's pending removal (if we have a valid context).
  1016. Context* context = GetContext();
  1017. if (context)
  1018. context->OnElementRemove(child);
  1019. child->OnChildRemove(child);
  1020. if (child_index >= children.size() - num_non_dom_children)
  1021. num_non_dom_children--;
  1022. deleted_children.push_back(child);
  1023. children.erase(itr);
  1024. // Remove the child element as the focussed child of this element.
  1025. if (child == focus)
  1026. {
  1027. focus = NULL;
  1028. // If this child (or a descendant of this child) is the context's currently
  1029. // focussed element, set the focus to us instead.
  1030. Context* context = GetContext();
  1031. if (context != NULL)
  1032. {
  1033. Element* focus_element = context->GetFocusElement();
  1034. while (focus_element != NULL)
  1035. {
  1036. if (focus_element == child)
  1037. {
  1038. Focus();
  1039. break;
  1040. }
  1041. focus_element = focus_element->GetParentNode();
  1042. }
  1043. }
  1044. }
  1045. DirtyLayout();
  1046. DirtyStackingContext();
  1047. DirtyStructure();
  1048. LockLayout(false);
  1049. return true;
  1050. }
  1051. child_index++;
  1052. }
  1053. return false;
  1054. }
  1055. bool Element::HasChildNodes() const
  1056. {
  1057. return (int) children.size() > num_non_dom_children;
  1058. }
  1059. Element* Element::GetElementById(const String& id)
  1060. {
  1061. // Check for special-case tokens.
  1062. if (id == "#self")
  1063. return this;
  1064. else if (id == "#document")
  1065. return GetOwnerDocument();
  1066. else if (id == "#parent")
  1067. return this->parent;
  1068. else
  1069. {
  1070. Element* search_root = GetOwnerDocument();
  1071. if (search_root == NULL)
  1072. search_root = this;
  1073. return ElementUtilities::GetElementById(search_root, id);
  1074. }
  1075. }
  1076. // Get all elements with the given tag.
  1077. void Element::GetElementsByTagName(ElementList& elements, const String& tag)
  1078. {
  1079. return ElementUtilities::GetElementsByTagName(elements, this, tag);
  1080. }
  1081. // Get all elements with the given class set on them.
  1082. void Element::GetElementsByClassName(ElementList& elements, const String& class_name)
  1083. {
  1084. return ElementUtilities::GetElementsByClassName(elements, this, class_name);
  1085. }
  1086. // Access the event dispatcher
  1087. EventDispatcher* Element::GetEventDispatcher() const
  1088. {
  1089. return event_dispatcher;
  1090. }
  1091. // Access the element background.
  1092. ElementBackground* Element::GetElementBackground() const
  1093. {
  1094. return background;
  1095. }
  1096. // Access the element border.
  1097. ElementBorder* Element::GetElementBorder() const
  1098. {
  1099. return border;
  1100. }
  1101. // Access the element decorators
  1102. ElementDecoration* Element::GetElementDecoration() const
  1103. {
  1104. return decoration;
  1105. }
  1106. // Returns the element's scrollbar functionality.
  1107. ElementScroll* Element::GetElementScroll() const
  1108. {
  1109. return scroll;
  1110. }
  1111. int Element::GetClippingIgnoreDepth()
  1112. {
  1113. if (clipping_state_dirty)
  1114. {
  1115. IsClippingEnabled();
  1116. }
  1117. return clipping_ignore_depth;
  1118. }
  1119. bool Element::IsClippingEnabled()
  1120. {
  1121. if (clipping_state_dirty)
  1122. {
  1123. // Is clipping enabled for this element, yes unless both overlow properties are set to visible
  1124. clipping_enabled = style->GetProperty(OVERFLOW_X)->Get< int >() != OVERFLOW_VISIBLE
  1125. || style->GetProperty(OVERFLOW_Y)->Get< int >() != OVERFLOW_VISIBLE;
  1126. // Get the clipping ignore depth from the clip property
  1127. clipping_ignore_depth = 0;
  1128. const Property* clip_property = GetProperty(CLIP);
  1129. if (clip_property->unit == Property::NUMBER)
  1130. clipping_ignore_depth = clip_property->Get< int >();
  1131. else if (clip_property->Get< int >() == CLIP_NONE)
  1132. clipping_ignore_depth = -1;
  1133. clipping_state_dirty = false;
  1134. }
  1135. return clipping_enabled;
  1136. }
  1137. // Gets the render interface owned by this element's context.
  1138. RenderInterface* Element::GetRenderInterface()
  1139. {
  1140. Context* context = GetContext();
  1141. if (context != NULL)
  1142. return context->GetRenderInterface();
  1143. return Rocket::Core::GetRenderInterface();
  1144. }
  1145. void Element::SetInstancer(ElementInstancer* _instancer)
  1146. {
  1147. // Only record the first instancer being set as some instancers call other instancers to do their dirty work, in
  1148. // which case we don't want to update the lowest level instancer.
  1149. if (instancer == NULL)
  1150. {
  1151. instancer = _instancer;
  1152. instancer->AddReference();
  1153. }
  1154. }
  1155. // Forces the element to generate a local stacking context, regardless of the value of its z-index property.
  1156. void Element::ForceLocalStackingContext()
  1157. {
  1158. local_stacking_context_forced = true;
  1159. local_stacking_context = true;
  1160. DirtyStackingContext();
  1161. }
  1162. // Called during the update loop after children are rendered.
  1163. void Element::OnUpdate()
  1164. {
  1165. }
  1166. // Called during render after backgrounds, borders, decorators, but before children, are rendered.
  1167. void Element::OnRender()
  1168. {
  1169. }
  1170. // Called during a layout operation, when the element is being positioned and sized.
  1171. void Element::OnLayout()
  1172. {
  1173. }
  1174. // Called when attributes on the element are changed.
  1175. void Element::OnAttributeChange(const AttributeNameList& changed_attributes)
  1176. {
  1177. if (changed_attributes.find("id") != changed_attributes.end())
  1178. {
  1179. id = GetAttribute< String >("id", "");
  1180. style->DirtyDefinition();
  1181. }
  1182. if (changed_attributes.find("class") != changed_attributes.end())
  1183. {
  1184. style->SetClassNames(GetAttribute< String >("class", ""));
  1185. }
  1186. // Add any inline style declarations.
  1187. if (changed_attributes.find("style") != changed_attributes.end())
  1188. {
  1189. PropertyDictionary properties;
  1190. StyleSheetParser parser;
  1191. parser.ParseProperties(properties, GetAttribute< String >("style", ""));
  1192. Rocket::Core::PropertyMap property_map = properties.GetProperties();
  1193. for (Rocket::Core::PropertyMap::iterator i = property_map.begin(); i != property_map.end(); ++i)
  1194. {
  1195. SetProperty((*i).first, (*i).second);
  1196. }
  1197. }
  1198. }
  1199. // Called when properties on the element are changed.
  1200. void Element::OnPropertyChange(const PropertyNameList& changed_properties)
  1201. {
  1202. bool all_dirty = StyleSheetSpecification::GetRegisteredProperties() == changed_properties;
  1203. if (!IsLayoutDirty())
  1204. {
  1205. if (all_dirty)
  1206. {
  1207. DirtyLayout();
  1208. }
  1209. else
  1210. {
  1211. // Force a relayout if any of the changed properties require it.
  1212. for (PropertyNameList::const_iterator i = changed_properties.begin(); i != changed_properties.end(); ++i)
  1213. {
  1214. const PropertyDefinition* property_definition = StyleSheetSpecification::GetProperty(*i);
  1215. if (property_definition)
  1216. {
  1217. if (property_definition->IsLayoutForced())
  1218. {
  1219. DirtyLayout();
  1220. break;
  1221. }
  1222. }
  1223. }
  1224. }
  1225. }
  1226. // Update the visibility.
  1227. if (all_dirty || changed_properties.find(VISIBILITY) != changed_properties.end() ||
  1228. changed_properties.find(DISPLAY) != changed_properties.end())
  1229. {
  1230. bool new_visibility = GetDisplay() != DISPLAY_NONE &&
  1231. GetProperty< int >(VISIBILITY) == VISIBILITY_VISIBLE;
  1232. if (visible != new_visibility)
  1233. {
  1234. visible = new_visibility;
  1235. if (parent != NULL)
  1236. parent->DirtyStackingContext();
  1237. }
  1238. if (all_dirty ||
  1239. changed_properties.find(DISPLAY) != changed_properties.end())
  1240. {
  1241. if (parent != NULL)
  1242. parent->DirtyStructure();
  1243. }
  1244. }
  1245. // Update the position.
  1246. if (all_dirty ||
  1247. changed_properties.find(LEFT) != changed_properties.end() ||
  1248. changed_properties.find(RIGHT) != changed_properties.end() ||
  1249. changed_properties.find(TOP) != changed_properties.end() ||
  1250. changed_properties.find(BOTTOM) != changed_properties.end())
  1251. {
  1252. UpdateOffset();
  1253. DirtyOffset();
  1254. }
  1255. // Update the z-index.
  1256. if (all_dirty ||
  1257. changed_properties.find(Z_INDEX) != changed_properties.end())
  1258. {
  1259. const Property* z_index_property = GetProperty(Z_INDEX);
  1260. if (z_index_property->unit == Property::KEYWORD &&
  1261. z_index_property->value.Get< int >() == Z_INDEX_AUTO)
  1262. {
  1263. if (local_stacking_context &&
  1264. !local_stacking_context_forced)
  1265. {
  1266. // We're no longer acting as a stacking context.
  1267. local_stacking_context = false;
  1268. stacking_context_dirty = false;
  1269. stacking_context.clear();
  1270. }
  1271. // If our old z-index was not zero, then we must dirty our stacking context so we'll be re-indexed.
  1272. if (z_index != 0)
  1273. {
  1274. z_index = 0;
  1275. DirtyStackingContext();
  1276. }
  1277. }
  1278. else
  1279. {
  1280. float new_z_index;
  1281. if (z_index_property->unit == Property::KEYWORD)
  1282. {
  1283. if (z_index_property->value.Get< int >() == Z_INDEX_TOP)
  1284. new_z_index = FLT_MAX;
  1285. else
  1286. new_z_index = -FLT_MAX;
  1287. }
  1288. else
  1289. new_z_index = z_index_property->value.Get< float >();
  1290. if (new_z_index != z_index)
  1291. {
  1292. z_index = new_z_index;
  1293. if (parent != NULL)
  1294. parent->DirtyStackingContext();
  1295. }
  1296. if (!local_stacking_context)
  1297. {
  1298. local_stacking_context = true;
  1299. stacking_context_dirty = true;
  1300. }
  1301. }
  1302. }
  1303. // Dirty the background if it's changed.
  1304. if (all_dirty ||
  1305. changed_properties.find(BACKGROUND_COLOR) != changed_properties.end() ||
  1306. changed_properties.find(OPACITY) != changed_properties.end() ||
  1307. changed_properties.find(IMAGE_COLOR) != changed_properties.end()) {
  1308. background->DirtyBackground();
  1309. decoration->ReloadDecorators();
  1310. }
  1311. // Dirty the border if it's changed.
  1312. if (all_dirty ||
  1313. changed_properties.find(BORDER_TOP_WIDTH) != changed_properties.end() ||
  1314. changed_properties.find(BORDER_RIGHT_WIDTH) != changed_properties.end() ||
  1315. changed_properties.find(BORDER_BOTTOM_WIDTH) != changed_properties.end() ||
  1316. changed_properties.find(BORDER_LEFT_WIDTH) != changed_properties.end() ||
  1317. changed_properties.find(BORDER_TOP_COLOR) != changed_properties.end() ||
  1318. changed_properties.find(BORDER_RIGHT_COLOR) != changed_properties.end() ||
  1319. changed_properties.find(BORDER_BOTTOM_COLOR) != changed_properties.end() ||
  1320. changed_properties.find(BORDER_LEFT_COLOR) != changed_properties.end() ||
  1321. changed_properties.find(OPACITY) != changed_properties.end())
  1322. border->DirtyBorder();
  1323. // Fetch a new font face if it has been changed.
  1324. if (all_dirty ||
  1325. changed_properties.find(FONT_FAMILY) != changed_properties.end() ||
  1326. changed_properties.find(FONT_CHARSET) != changed_properties.end() ||
  1327. changed_properties.find(FONT_WEIGHT) != changed_properties.end() ||
  1328. changed_properties.find(FONT_STYLE) != changed_properties.end() ||
  1329. changed_properties.find(FONT_SIZE) != changed_properties.end())
  1330. {
  1331. // Store the old em; if it changes, then we need to dirty all em-relative properties.
  1332. int old_em = -1;
  1333. if (font_face_handle != NULL)
  1334. old_em = font_face_handle->GetLineHeight();
  1335. // Fetch the new font face.
  1336. FontFaceHandle* new_font_face_handle = ElementUtilities::GetFontFaceHandle(this);
  1337. // If this is different from our current font face, then we've got to nuke
  1338. // all our characters and tell our parent that we have to be re-laid out.
  1339. if (new_font_face_handle != font_face_handle)
  1340. {
  1341. if (font_face_handle)
  1342. font_face_handle->RemoveReference();
  1343. font_face_handle = new_font_face_handle;
  1344. // Our font face has changed; odds are, so has our em. All of our em-relative values
  1345. // have therefore probably changed as well, so we'll need to dirty them.
  1346. int new_em = -1;
  1347. if (font_face_handle != NULL)
  1348. new_em = font_face_handle->GetLineHeight();
  1349. if (old_em != new_em)
  1350. {
  1351. style->DirtyEmProperties();
  1352. }
  1353. }
  1354. else if (new_font_face_handle != NULL)
  1355. new_font_face_handle->RemoveReference();
  1356. }
  1357. // Check for clipping state changes
  1358. if (all_dirty ||
  1359. changed_properties.find(CLIP) != changed_properties.end() ||
  1360. changed_properties.find(OVERFLOW_X) != changed_properties.end() ||
  1361. changed_properties.find(OVERFLOW_Y) != changed_properties.end())
  1362. {
  1363. clipping_state_dirty = true;
  1364. }
  1365. }
  1366. // Called when a child node has been added somewhere in the hierarchy
  1367. void Element::OnChildAdd(Element* child)
  1368. {
  1369. if (parent)
  1370. parent->OnChildAdd(child);
  1371. }
  1372. // Called when a child node has been removed somewhere in the hierarchy
  1373. void Element::OnChildRemove(Element* child)
  1374. {
  1375. if (parent)
  1376. parent->OnChildRemove(child);
  1377. }
  1378. // Update the element's layout if required.
  1379. void Element::UpdateLayout()
  1380. {
  1381. ElementDocument* document = GetOwnerDocument();
  1382. if (document != NULL)
  1383. document->UpdateLayout();
  1384. }
  1385. // Forces a re-layout of this element, and any other children required.
  1386. void Element::DirtyLayout()
  1387. {
  1388. Element* document = GetOwnerDocument();
  1389. if (document != NULL)
  1390. document->DirtyLayout();
  1391. }
  1392. /// Increment/Decrement the layout lock
  1393. void Element::LockLayout(bool lock)
  1394. {
  1395. Element* document = GetOwnerDocument();
  1396. if (document != NULL)
  1397. document->LockLayout(lock);
  1398. }
  1399. // Forces a re-layout of this element, and any other children required.
  1400. bool Element::IsLayoutDirty()
  1401. {
  1402. Element* document = GetOwnerDocument();
  1403. if (document != NULL)
  1404. return document->IsLayoutDirty();
  1405. return false;
  1406. }
  1407. // Forces a reevaluation of applicable font effects.
  1408. void Element::DirtyFont()
  1409. {
  1410. for (size_t i = 0; i < children.size(); ++i)
  1411. children[i]->DirtyFont();
  1412. }
  1413. void Element::OnReferenceDeactivate()
  1414. {
  1415. if (instancer)
  1416. {
  1417. instancer->ReleaseElement(this);
  1418. }
  1419. else
  1420. {
  1421. // Hopefully we can just delete ourselves.
  1422. //delete this;
  1423. Log::Message(Log::LT_WARNING, "Leak detected: element %s not instanced via Rocket Factory. Unable to release.", GetAddress().CString());
  1424. }
  1425. }
  1426. void Element::ProcessEvent(Event& event)
  1427. {
  1428. if (event == MOUSEDOWN && IsPointWithinElement(Vector2f(event.GetParameter< float >("mouse_x", 0), event.GetParameter< float >("mouse_y", 0))) &&
  1429. event.GetParameter< int >("button", 0) == 0)
  1430. SetPseudoClass("active", true);
  1431. if (event == MOUSESCROLL)
  1432. {
  1433. int wheel_delta = event.GetParameter< int >("wheel_delta", 0);
  1434. if ((wheel_delta < 0 && GetScrollTop() > 0) ||
  1435. (wheel_delta > 0 && GetScrollHeight() > GetScrollTop() + GetClientHeight()))
  1436. {
  1437. int overflow_property = GetProperty< int >(OVERFLOW_Y);
  1438. if (overflow_property == OVERFLOW_AUTO ||
  1439. overflow_property == OVERFLOW_SCROLL)
  1440. {
  1441. SetScrollTop(GetScrollTop() + wheel_delta * (GetFontFaceHandle() ? ElementUtilities::GetLineHeight(this) : (GetProperty(SCROLL_DEFAULT_STEP_SIZE) ? GetProperty< int >(SCROLL_DEFAULT_STEP_SIZE) : 0 )));
  1442. event.StopPropagation();
  1443. }
  1444. }
  1445. return;
  1446. }
  1447. if (event.GetTargetElement() == this)
  1448. {
  1449. if (event == MOUSEOVER)
  1450. SetPseudoClass("hover", true);
  1451. else if (event == MOUSEOUT)
  1452. SetPseudoClass("hover", false);
  1453. else if (event == FOCUS)
  1454. SetPseudoClass(FOCUS, true);
  1455. else if (event == BLUR)
  1456. SetPseudoClass(FOCUS, false);
  1457. }
  1458. }
  1459. void Element::GetRML(String& content)
  1460. {
  1461. // First we start the open tag, add the attributes then close the open tag.
  1462. // Then comes the children in order, then we add our close tag.
  1463. content.Append("<");
  1464. content.Append(tag);
  1465. int index = 0;
  1466. String name;
  1467. String value;
  1468. while (IterateAttributes(index, name, value))
  1469. {
  1470. size_t length = name.Length() + value.Length() + 8;
  1471. String attribute(length, " %s=\"%s\"", name.CString(), value.CString());
  1472. content.Append(attribute);
  1473. }
  1474. if (HasChildNodes())
  1475. {
  1476. content.Append(">");
  1477. GetInnerRML(content);
  1478. content.Append("</");
  1479. content.Append(tag);
  1480. content.Append(">");
  1481. }
  1482. else
  1483. {
  1484. content.Append(" />");
  1485. }
  1486. }
  1487. void Element::SetParent(Element* _parent)
  1488. {
  1489. // If there's an old parent, detach from it first.
  1490. if (parent &&
  1491. parent != _parent)
  1492. parent->RemoveChild(this);
  1493. // Save our parent
  1494. parent = _parent;
  1495. }
  1496. void Element::ReleaseDeletedElements()
  1497. {
  1498. for (size_t i = 0; i < active_children.size(); i++)
  1499. {
  1500. active_children[i]->ReleaseDeletedElements();
  1501. }
  1502. ReleaseElements(deleted_children);
  1503. active_children = children;
  1504. }
  1505. void Element::ReleaseElements(ElementList& released_elements)
  1506. {
  1507. // Remove deleted children from this element.
  1508. while (!released_elements.empty())
  1509. {
  1510. Element* element = released_elements.back();
  1511. released_elements.pop_back();
  1512. // If this element has been added back into our list, then we remove our previous oustanding reference on it
  1513. // and continue.
  1514. if (std::find(children.begin(), children.end(), element) != children.end())
  1515. {
  1516. element->RemoveReference();
  1517. continue;
  1518. }
  1519. // Set the parent to NULL unless it's been reparented already.
  1520. if (element->GetParentNode() == this)
  1521. element->parent = NULL;
  1522. element->RemoveReference();
  1523. }
  1524. }
  1525. void Element::DirtyOffset()
  1526. {
  1527. offset_dirty = true;
  1528. // Not strictly true ... ?
  1529. for (size_t i = 0; i < children.size(); i++)
  1530. children[i]->DirtyOffset();
  1531. }
  1532. void Element::UpdateOffset()
  1533. {
  1534. int position_property = GetPosition();
  1535. if (position_property == POSITION_ABSOLUTE ||
  1536. position_property == POSITION_FIXED)
  1537. {
  1538. if (offset_parent != NULL)
  1539. {
  1540. const Box& parent_box = offset_parent->GetBox();
  1541. Vector2f containing_block = parent_box.GetSize(Box::PADDING);
  1542. const Property *left = GetLocalProperty(LEFT);
  1543. const Property *right = GetLocalProperty(RIGHT);
  1544. // If the element is anchored left, then the position is offset by that resolved value.
  1545. if (left != NULL && left->unit != Property::KEYWORD)
  1546. relative_offset_base.x = parent_box.GetEdge(Box::BORDER, Box::LEFT) + (ResolveProperty(LEFT, containing_block.x) + GetBox().GetEdge(Box::MARGIN, Box::LEFT));
  1547. // If the element is anchored right, then the position is set first so the element's right-most edge
  1548. // (including margins) will render up against the containing box's right-most content edge, and then
  1549. // offset by the resolved value.
  1550. else if (right != NULL && right->unit != Property::KEYWORD)
  1551. 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));
  1552. const Property *top = GetLocalProperty(TOP);
  1553. const Property *bottom = GetLocalProperty(BOTTOM);
  1554. // If the element is anchored top, then the position is offset by that resolved value.
  1555. if (top != NULL && top->unit != Property::KEYWORD)
  1556. relative_offset_base.y = parent_box.GetEdge(Box::BORDER, Box::TOP) + (ResolveProperty(TOP, containing_block.y) + GetBox().GetEdge(Box::MARGIN, Box::TOP));
  1557. // If the element is anchored bottom, then the position is set first so the element's right-most edge
  1558. // (including margins) will render up against the containing box's right-most content edge, and then
  1559. // offset by the resolved value.
  1560. else if (bottom != NULL && bottom->unit != Property::KEYWORD)
  1561. 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));
  1562. }
  1563. }
  1564. else if (position_property == POSITION_RELATIVE)
  1565. {
  1566. if (offset_parent != NULL)
  1567. {
  1568. const Box& parent_box = offset_parent->GetBox();
  1569. Vector2f containing_block = parent_box.GetSize();
  1570. const Property *left = GetLocalProperty(LEFT);
  1571. const Property *right = GetLocalProperty(RIGHT);
  1572. if (left != NULL && left->unit != Property::KEYWORD)
  1573. relative_offset_position.x = ResolveProperty(LEFT, containing_block.x);
  1574. else if (right != NULL && right->unit != Property::KEYWORD)
  1575. relative_offset_position.x = -1 * ResolveProperty(RIGHT, containing_block.x);
  1576. else
  1577. relative_offset_position.x = 0;
  1578. const Property *top = GetLocalProperty(TOP);
  1579. const Property *bottom = GetLocalProperty(BOTTOM);
  1580. if (top != NULL && top->unit != Property::KEYWORD)
  1581. relative_offset_position.y = ResolveProperty(TOP, containing_block.y);
  1582. else if (bottom != NULL && bottom->unit != Property::KEYWORD)
  1583. relative_offset_position.y = -1 * ResolveProperty(BOTTOM, containing_block.y);
  1584. else
  1585. relative_offset_position.y = 0;
  1586. }
  1587. }
  1588. else
  1589. {
  1590. relative_offset_position.x = 0;
  1591. relative_offset_position.y = 0;
  1592. }
  1593. LayoutEngine::Round(relative_offset_base);
  1594. LayoutEngine::Round(relative_offset_position);
  1595. }
  1596. void Element::BuildLocalStackingContext()
  1597. {
  1598. stacking_context_dirty = false;
  1599. stacking_context.clear();
  1600. BuildStackingContext(&stacking_context);
  1601. std::stable_sort(stacking_context.begin(), stacking_context.end(), ElementSortZIndex());
  1602. }
  1603. void Element::BuildStackingContext(ElementList* new_stacking_context)
  1604. {
  1605. // Build the list of ordered children. Our child list is sorted within the stacking context so stacked elements
  1606. // will render in the right order; ie, positioned elements will render on top of inline elements, which will render
  1607. // on top of floated elements, which will render on top of block elements.
  1608. std::vector< std::pair< Element*, float > > ordered_children;
  1609. for (size_t i = 0; i < children.size(); ++i)
  1610. {
  1611. Element* child = children[i];
  1612. if (!child->IsVisible())
  1613. continue;
  1614. std::pair< Element*, float > ordered_child;
  1615. ordered_child.first = child;
  1616. if (child->GetPosition() != POSITION_STATIC)
  1617. ordered_child.second = 3;
  1618. else if (child->GetFloat() != FLOAT_NONE)
  1619. ordered_child.second = 1;
  1620. else if (child->GetDisplay() == DISPLAY_BLOCK)
  1621. ordered_child.second = 0;
  1622. else
  1623. ordered_child.second = 2;
  1624. ordered_children.push_back(ordered_child);
  1625. }
  1626. // Sort the list!
  1627. std::stable_sort(ordered_children.begin(), ordered_children.end(), ElementSortZOrder());
  1628. // Add the list of ordered children into the stacking context in order.
  1629. for (size_t i = 0; i < ordered_children.size(); ++i)
  1630. {
  1631. new_stacking_context->push_back(ordered_children[i].first);
  1632. if (!ordered_children[i].first->local_stacking_context)
  1633. ordered_children[i].first->BuildStackingContext(new_stacking_context);
  1634. }
  1635. }
  1636. void Element::DirtyStackingContext()
  1637. {
  1638. // The first ancestor of ours that doesn't have an automatic z-index is the ancestor that is establishing our local
  1639. // stacking context.
  1640. Element* stacking_context_parent = this;
  1641. while (stacking_context_parent != NULL &&
  1642. !stacking_context_parent->local_stacking_context)
  1643. stacking_context_parent = stacking_context_parent->GetParentNode();
  1644. if (stacking_context_parent != NULL)
  1645. stacking_context_parent->stacking_context_dirty = true;
  1646. }
  1647. void Element::DirtyStructure()
  1648. {
  1649. // Clear the cached owner document
  1650. owner_document = NULL;
  1651. // Inform all children that the structure is drity
  1652. for (size_t i = 0; i < children.size(); ++i)
  1653. {
  1654. const ElementDefinition* element_definition = children[i]->GetStyle()->GetDefinition();
  1655. if (element_definition != NULL &&
  1656. element_definition->IsStructurallyVolatile())
  1657. {
  1658. children[i]->GetStyle()->DirtyDefinition();
  1659. }
  1660. children[i]->DirtyStructure();
  1661. }
  1662. }
  1663. }
  1664. }