Element.cpp 47 KB

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