ElementDecoration.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 "ElementDecoration.h"
  29. #include "ElementDefinition.h"
  30. #include "../../Include/Rocket/Core/Decorator.h"
  31. #include "../../Include/Rocket/Core/Element.h"
  32. namespace Rocket {
  33. namespace Core {
  34. ElementDecoration::ElementDecoration(Element* _element)
  35. {
  36. element = _element;
  37. decorators_dirty = false;
  38. }
  39. ElementDecoration::~ElementDecoration()
  40. {
  41. ReleaseDecorators();
  42. }
  43. // Releases existing decorators and loads all decorators required by the element's definition.
  44. bool ElementDecoration::ReloadDecorators()
  45. {
  46. ReleaseDecorators();
  47. StyleSheet* stylesheet = element->GetStyleSheet();
  48. if (!stylesheet)
  49. return true;
  50. const Property* property = element->GetLocalProperty(PropertyId::Decorator);
  51. if (!property)
  52. return true;
  53. String decorator_value = property->Get<String>();
  54. // @performance: Can optimize for the case of only one decorator
  55. StringList decorator_list;
  56. // We use custom quote characters as we don't want to split on any commas inside parenthesis, which may appear in decorator shorthands.
  57. StringUtilities::ExpandString(decorator_list, decorator_value, ',', '(', ')');
  58. for (const String& name : decorator_list)
  59. {
  60. Decorator* decorator = stylesheet->GetOrInstanceDecorator(name, property->source, property->source_line_number);
  61. if (decorator)
  62. LoadDecorator(decorator);
  63. }
  64. return true;
  65. }
  66. // Loads a single decorator and adds it to the list of loaded decorators for this element.
  67. int ElementDecoration::LoadDecorator(Decorator* decorator)
  68. {
  69. DecoratorHandle element_decorator;
  70. element_decorator.decorator = decorator;
  71. element_decorator.decorator->AddReference();
  72. element_decorator.decorator_data = decorator->GenerateElementData(element);
  73. decorators.push_back(element_decorator);
  74. return (int) (decorators.size() - 1);
  75. }
  76. // Releases all existing decorators and frees their data.
  77. void ElementDecoration::ReleaseDecorators()
  78. {
  79. for (size_t i = 0; i < decorators.size(); i++)
  80. {
  81. if (decorators[i].decorator_data)
  82. decorators[i].decorator->ReleaseElementData(decorators[i].decorator_data);
  83. decorators[i].decorator->RemoveReference();
  84. }
  85. decorators.clear();
  86. }
  87. void ElementDecoration::RenderDecorators()
  88. {
  89. // @performance: Ignore dirty flag if e.g. pseudo classes do not affect the decorators
  90. if (decorators_dirty)
  91. {
  92. decorators_dirty = false;
  93. ReloadDecorators();
  94. }
  95. // Render the decorators attached to this element in its current state.
  96. // Render from back to front for correct render order.
  97. for (int i = (int)decorators.size() - 1; i >= 0; i--)
  98. {
  99. DecoratorHandle& decorator = decorators[i];
  100. decorator.decorator->RenderElement(element, decorator.decorator_data);
  101. }
  102. }
  103. void ElementDecoration::DirtyDecorators()
  104. {
  105. decorators_dirty = true;
  106. }
  107. // Iterates over all active decorators attached to the decoration's element.
  108. bool ElementDecoration::IterateDecorators(int& index, PseudoClassList& pseudo_classes, String& name, Decorator*& decorator, DecoratorDataHandle& decorator_data) const
  109. {
  110. if (index < 0)
  111. return false;
  112. if (index < (int)decorators.size())
  113. {
  114. decorator = decorators[index].decorator;
  115. decorator_data = decorators[index].decorator_data;
  116. name = ":not implemented:";
  117. index += 1;
  118. return true;
  119. }
  120. return false;
  121. }
  122. }
  123. }