Element.cpp 51 KB

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