2
0

ElementStyle.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #include "precompiled.h"
  28. #include "ElementStyle.h"
  29. #include <algorithm>
  30. #include <Rocket/Core/ElementDocument.h>
  31. #include <Rocket/Core/ElementUtilities.h>
  32. #include <Rocket/Core/Log.h>
  33. #include <Rocket/Core/Property.h>
  34. #include <Rocket/Core/PropertyDefinition.h>
  35. #include <Rocket/Core/PropertyDictionary.h>
  36. #include <Rocket/Core/StyleSheetSpecification.h>
  37. #include "ElementBackground.h"
  38. #include "ElementBorder.h"
  39. #include "ElementDecoration.h"
  40. #include "ElementDefinition.h"
  41. #include "FontFaceHandle.h"
  42. namespace Rocket {
  43. namespace Core {
  44. ElementStyle::ElementStyle(Element* _element)
  45. {
  46. local_properties = NULL;
  47. definition = NULL;
  48. element = _element;
  49. definition_dirty = true;
  50. }
  51. ElementStyle::~ElementStyle()
  52. {
  53. if (local_properties != NULL)
  54. delete local_properties;
  55. if (definition != NULL)
  56. definition->RemoveReference();
  57. }
  58. // Returns the element's definition, updating if necessary.
  59. const ElementDefinition* ElementStyle::GetDefinition()
  60. {
  61. if (definition_dirty)
  62. {
  63. definition_dirty = false;
  64. ElementDefinition* new_definition = NULL;
  65. const StyleSheet* style_sheet = GetStyleSheet();
  66. if (style_sheet != NULL)
  67. {
  68. new_definition = style_sheet->GetElementDefinition(element);
  69. }
  70. // Switch the property definitions if the definition has changed.
  71. if (new_definition != definition || new_definition == NULL)
  72. {
  73. PropertyNameList properties;
  74. if (definition != NULL)
  75. {
  76. definition->GetDefinedProperties(properties, pseudo_classes);
  77. definition->RemoveReference();
  78. }
  79. definition = new_definition;
  80. if (definition != NULL)
  81. definition->GetDefinedProperties(properties, pseudo_classes);
  82. DirtyProperties(properties);
  83. element->GetElementDecoration()->ReloadDecorators();
  84. }
  85. else if (new_definition != NULL)
  86. new_definition->RemoveReference();
  87. }
  88. return definition;
  89. }
  90. // Sets or removes a pseudo-class on the element.
  91. void ElementStyle::SetPseudoClass(const String& pseudo_class, bool activate)
  92. {
  93. size_t num_pseudo_classes = pseudo_classes.size();
  94. if (activate)
  95. pseudo_classes.insert(pseudo_class);
  96. else
  97. pseudo_classes.erase(pseudo_class);
  98. if (pseudo_classes.size() != num_pseudo_classes)
  99. {
  100. element->GetElementDecoration()->DirtyDecorators();
  101. const ElementDefinition* definition = element->GetDefinition();
  102. if (definition != NULL)
  103. {
  104. PropertyNameList properties;
  105. definition->GetDefinedProperties(properties, pseudo_classes, pseudo_class);
  106. DirtyProperties(properties);
  107. switch (definition->GetPseudoClassVolatility(pseudo_class))
  108. {
  109. case ElementDefinition::FONT_VOLATILE:
  110. element->DirtyFont();
  111. break;
  112. case ElementDefinition::STRUCTURE_VOLATILE:
  113. DirtyChildDefinitions();
  114. break;
  115. default:
  116. break;
  117. }
  118. }
  119. }
  120. }
  121. // Checks if a specific pseudo-class has been set on the element.
  122. bool ElementStyle::IsPseudoClassSet(const String& pseudo_class) const
  123. {
  124. return (pseudo_classes.find(pseudo_class) != pseudo_classes.end());
  125. }
  126. const PseudoClassList& ElementStyle::GetActivePseudoClasses() const
  127. {
  128. return pseudo_classes;
  129. }
  130. // Sets or removes a class on the element.
  131. void ElementStyle::SetClass(const String& class_name, bool activate)
  132. {
  133. StringList::iterator class_location = std::find(classes.begin(), classes.end(), class_name);
  134. if (activate)
  135. {
  136. if (class_location == classes.end())
  137. {
  138. classes.push_back(class_name);
  139. DirtyDefinition();
  140. }
  141. }
  142. else
  143. {
  144. if (class_location != classes.end())
  145. {
  146. classes.erase(class_location);
  147. DirtyDefinition();
  148. }
  149. }
  150. }
  151. // Checks if a class is set on the element.
  152. bool ElementStyle::IsClassSet(const String& class_name) const
  153. {
  154. return std::find(classes.begin(), classes.end(), class_name) != classes.end();
  155. }
  156. // Specifies the entire list of classes for this element. This will replace any others specified.
  157. void ElementStyle::SetClassNames(const String& class_names)
  158. {
  159. classes.clear();
  160. StringUtilities::ExpandString(classes, class_names, ' ');
  161. DirtyDefinition();
  162. }
  163. // Returns the list of classes specified for this element.
  164. String ElementStyle::GetClassNames() const
  165. {
  166. String class_names;
  167. for (size_t i = 0; i < classes.size(); i++)
  168. {
  169. if (i != 0)
  170. {
  171. class_names.Append(" ");
  172. }
  173. class_names.Append(classes[i]);
  174. }
  175. return class_names;
  176. }
  177. // Sets a local property override on the element.
  178. bool ElementStyle::SetProperty(const String& name, const String& value)
  179. {
  180. if (local_properties == NULL)
  181. local_properties = new PropertyDictionary();
  182. if (StyleSheetSpecification::ParsePropertyDeclaration(*local_properties, name, value))
  183. {
  184. DirtyProperty(name);
  185. return true;
  186. }
  187. else
  188. {
  189. Log::Message(Log::LT_WARNING, "Syntax error parsing inline property declaration '%s: %s;'.", name.CString(), value.CString());
  190. return false;
  191. }
  192. }
  193. // Sets a local property override on the element to a pre-parsed value.
  194. bool ElementStyle::SetProperty(const String& name, const Property& property)
  195. {
  196. Property new_property = property;
  197. new_property.definition = StyleSheetSpecification::GetProperty(name);
  198. if (new_property.definition == NULL)
  199. return false;
  200. if (local_properties == NULL)
  201. local_properties = new PropertyDictionary();
  202. local_properties->SetProperty(name, new_property);
  203. DirtyProperty(name);
  204. return true;
  205. }
  206. // Removes a local property override on the element.
  207. void ElementStyle::RemoveProperty(const String& name)
  208. {
  209. if (local_properties == NULL)
  210. return;
  211. if (local_properties->GetProperty(name) != NULL)
  212. {
  213. local_properties->RemoveProperty(name);
  214. DirtyProperty(name);
  215. }
  216. }
  217. // Returns one of this element's properties.
  218. const Property* ElementStyle::GetProperty(const String& name)
  219. {
  220. const Property* local_property = GetLocalProperty(name);
  221. if (local_property != NULL)
  222. return local_property;
  223. // Fetch the property specification.
  224. const PropertyDefinition* property = StyleSheetSpecification::GetProperty(name);
  225. if (property == NULL)
  226. return NULL;
  227. // If we can inherit this property, return our parent's property.
  228. if (property->IsInherited())
  229. {
  230. Element* parent = element->GetParentNode();
  231. if (parent != NULL)
  232. {
  233. return parent->GetProperty(name);
  234. }
  235. }
  236. // No property available! Return the default value.
  237. return property->GetDefaultValue();
  238. }
  239. // Returns one of this element's properties.
  240. const Property* ElementStyle::GetLocalProperty(const String& name)
  241. {
  242. // Check for overriding local properties.
  243. if (local_properties != NULL)
  244. {
  245. const Property* property = local_properties->GetProperty(name);
  246. if (property != NULL)
  247. return property;
  248. }
  249. // Check for a property defined in an RCSS rule.
  250. if (definition != NULL)
  251. return definition->GetProperty(name, pseudo_classes);
  252. return NULL;
  253. }
  254. // Resolves one of this element's properties.
  255. float ElementStyle::ResolveProperty(const String& name, float base_value)
  256. {
  257. const Property* property = GetProperty(name);
  258. if (!property)
  259. {
  260. ROCKET_ERROR;
  261. return 0.0f;
  262. }
  263. if (property->unit & Property::RELATIVE_UNIT)
  264. {
  265. // The calculated value of the font-size property is inherited, so we need to check if this
  266. // is an inherited property. If so, then we return our parent's font size instead.
  267. if (name == FONT_SIZE)
  268. {
  269. Rocket::Core::Element* parent = element->GetParentNode();
  270. if (parent == NULL)
  271. return 0;
  272. if (GetLocalProperty(FONT_SIZE) == NULL)
  273. return parent->ResolveProperty(FONT_SIZE, 0);
  274. // The base value for font size is always the height of *this* element's parent's font.
  275. base_value = parent->ResolveProperty(FONT_SIZE, 0);
  276. }
  277. if (property->unit & Property::PERCENT)
  278. return base_value * property->value.Get< float >() * 0.01f;
  279. else if (property->unit & Property::EM)
  280. {
  281. // If an em-relative font size is specified, it is expressed relative to the parent's
  282. // font height.
  283. if (name == FONT_SIZE)
  284. return property->value.Get< float >() * base_value;
  285. else
  286. return property->value.Get< float >() * ElementUtilities::GetFontSize(element);
  287. }
  288. }
  289. if (property->unit & Property::NUMBER || property->unit & Property::PX)
  290. {
  291. return property->value.Get< float >();
  292. }
  293. // We're not a numeric property; return 0.
  294. return 0.0f;
  295. }
  296. // Iterates over the properties defined on the element.
  297. bool ElementStyle::IterateProperties(int& index, PseudoClassList& property_pseudo_classes, String& name, const Property*& property)
  298. {
  299. // First check for locally defined properties.
  300. if (local_properties != NULL)
  301. {
  302. if (index < local_properties->GetNumProperties())
  303. {
  304. PropertyMap::const_iterator i = local_properties->GetProperties().begin();
  305. for (int count = 0; count < index; ++count)
  306. ++i;
  307. name = (*i).first;
  308. property = &((*i).second);
  309. property_pseudo_classes.clear();
  310. ++index;
  311. return true;
  312. }
  313. }
  314. const ElementDefinition* definition = GetDefinition();
  315. if (definition != NULL)
  316. {
  317. int index_offset = 0;
  318. if (local_properties != NULL)
  319. index_offset = local_properties->GetNumProperties();
  320. // Offset the index to be relative to the definition before we start indexing. When we do get a property back,
  321. // check that it hasn't been overridden by the element's local properties; if so, continue on to the next one.
  322. index -= index_offset;
  323. while (definition->IterateProperties(index, pseudo_classes, property_pseudo_classes, name, property))
  324. {
  325. if (local_properties == NULL ||
  326. local_properties->GetProperty(name) == NULL)
  327. {
  328. index += index_offset;
  329. return true;
  330. }
  331. }
  332. return false;
  333. }
  334. return false;
  335. }
  336. // Returns the active style sheet for this element. This may be NULL.
  337. StyleSheet* ElementStyle::GetStyleSheet() const
  338. {
  339. ElementDocument* document = element->GetOwnerDocument();
  340. if (document != NULL)
  341. return document->GetStyleSheet();
  342. return NULL;
  343. }
  344. void ElementStyle::DirtyDefinition()
  345. {
  346. definition_dirty = true;
  347. DirtyChildDefinitions();
  348. }
  349. void ElementStyle::DirtyChildDefinitions()
  350. {
  351. for (int i = 0; i < element->GetNumChildren(true); i++)
  352. element->GetChild(i)->GetStyle()->DirtyDefinition();
  353. }
  354. // Dirties every property.
  355. void ElementStyle::DirtyProperties()
  356. {
  357. PropertyNameList properties;
  358. StyleSheetSpecification::GetRegisteredProperties(properties);
  359. DirtyProperties(properties);
  360. }
  361. // Dirties em-relative properties.
  362. void ElementStyle::DirtyEmProperties()
  363. {
  364. PropertyNameList properties;
  365. StyleSheetSpecification::GetRegisteredProperties(properties);
  366. // Check if any of these are currently em-relative. If so, dirty them.
  367. PropertyNameList em_properties;
  368. for (PropertyNameList::iterator list_iterator = properties.begin(); list_iterator != properties.end(); ++list_iterator)
  369. {
  370. // Skip font-size; this is relative to our parent's em, not ours.
  371. if (*list_iterator == FONT_SIZE)
  372. continue;
  373. // Get this element from this element. If this is em-relative, then add it to the list to
  374. // dirty.
  375. if (element->GetProperty(*list_iterator)->unit == Property::EM)
  376. em_properties.insert(*list_iterator);
  377. }
  378. if (!em_properties.empty())
  379. DirtyProperties(em_properties);
  380. // Now dirty all of our descendant's font-size properties that are relative to ems.
  381. int num_children = element->GetNumChildren(true);
  382. for (int i = 0; i < num_children; ++i)
  383. element->GetChild(i)->GetStyle()->DirtyInheritedEmProperties();
  384. }
  385. // Dirties font-size on child elements if appropriate.
  386. void ElementStyle::DirtyInheritedEmProperties()
  387. {
  388. const Property* font_size = element->GetLocalProperty(FONT_SIZE);
  389. if (font_size == NULL)
  390. {
  391. int num_children = element->GetNumChildren(true);
  392. for (int i = 0; i < num_children; ++i)
  393. element->GetChild(i)->GetStyle()->DirtyInheritedEmProperties();
  394. }
  395. else
  396. {
  397. if (font_size->unit & Property::RELATIVE_UNIT)
  398. DirtyProperty(FONT_SIZE);
  399. }
  400. }
  401. // Sets a single property as dirty.
  402. void ElementStyle::DirtyProperty(const String& property)
  403. {
  404. PropertyNameList properties;
  405. properties.insert(String(property));
  406. DirtyProperties(properties);
  407. }
  408. // Sets a list of properties as dirty.
  409. void ElementStyle::DirtyProperties(const PropertyNameList& properties)
  410. {
  411. if (properties.empty())
  412. return;
  413. PropertyNameList inherited_properties;
  414. for (PropertyNameList::const_iterator i = properties.begin(); i != properties.end(); ++i)
  415. {
  416. // If this property is an inherited property, then push it into the list to be passed onto our children.
  417. const PropertyDefinition* property = StyleSheetSpecification::GetProperty(*i);
  418. if (property != NULL &&
  419. property->IsInherited())
  420. inherited_properties.insert(*i);
  421. }
  422. // Pass the list of those properties that are inherited onto our children.
  423. if (!inherited_properties.empty())
  424. {
  425. for (int i = 0; i < element->GetNumChildren(true); i++)
  426. element->GetChild(i)->GetStyle()->DirtyInheritedProperties(inherited_properties);
  427. }
  428. // And send the event.
  429. element->OnPropertyChange(properties);
  430. }
  431. // Sets a list of our potentially inherited properties as dirtied by an ancestor.
  432. void ElementStyle::DirtyInheritedProperties(const PropertyNameList& properties)
  433. {
  434. PropertyNameList inherited_properties;
  435. for (PropertyNameList::const_iterator i = properties.begin(); i != properties.end(); ++i)
  436. {
  437. if (GetLocalProperty((*i)) == NULL)
  438. inherited_properties.insert(*i);
  439. }
  440. if (inherited_properties.empty())
  441. return;
  442. // Pass the list of those properties that this element doesn't override onto our children.
  443. for (int i = 0; i < element->GetNumChildren(true); i++)
  444. element->GetChild(i)->GetStyle()->DirtyInheritedProperties(inherited_properties);
  445. element->OnPropertyChange(properties);
  446. }
  447. }
  448. }