Element.cpp 48 KB

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