Element.cpp 51 KB

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