Element.cpp 48 KB

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