Element.cpp 46 KB

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