Element.cpp 51 KB

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