ElementDecoration.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. const StyleSheet* stylesheet = element->GetStyleSheet();
  48. if (!stylesheet)
  49. return true;
  50. const String& decorator_value = element->GetComputedValues().decorator;
  51. if (decorator_value.empty())
  52. return true;
  53. // @performance: Can optimize for the case of only one decorator
  54. StringList decorator_list;
  55. StringUtilities::ExpandString(decorator_list, decorator_value);
  56. for (const String& name : decorator_list)
  57. {
  58. Decorator* decorator = stylesheet->GetDecorator(name);
  59. if (decorator)
  60. LoadDecorator(decorator);
  61. }
  62. return true;
  63. }
  64. // Loads a single decorator and adds it to the list of loaded decorators for this element.
  65. int ElementDecoration::LoadDecorator(Decorator* decorator)
  66. {
  67. DecoratorHandle element_decorator;
  68. element_decorator.decorator = decorator;
  69. element_decorator.decorator->AddReference();
  70. element_decorator.decorator_data = decorator->GenerateElementData(element);
  71. decorators.push_back(element_decorator);
  72. return (int) (decorators.size() - 1);
  73. }
  74. // Releases all existing decorators and frees their data.
  75. void ElementDecoration::ReleaseDecorators()
  76. {
  77. for (size_t i = 0; i < decorators.size(); i++)
  78. {
  79. if (decorators[i].decorator_data)
  80. decorators[i].decorator->ReleaseElementData(decorators[i].decorator_data);
  81. decorators[i].decorator->RemoveReference();
  82. }
  83. decorators.clear();
  84. }
  85. void ElementDecoration::RenderDecorators()
  86. {
  87. // @performance: Ignore dirty flag if e.g. pseudo classes do not affect the decorators
  88. if (decorators_dirty)
  89. {
  90. decorators_dirty = false;
  91. ReloadDecorators();
  92. }
  93. // Render the decorators attached to this element in its current state.
  94. for (size_t i = 0; i < decorators.size(); i++)
  95. {
  96. DecoratorHandle& decorator = decorators[i];
  97. decorator.decorator->RenderElement(element, decorator.decorator_data);
  98. }
  99. }
  100. void ElementDecoration::DirtyDecorators()
  101. {
  102. decorators_dirty = true;
  103. }
  104. // Iterates over all active decorators attached to the decoration's element.
  105. bool ElementDecoration::IterateDecorators(int& index, PseudoClassList& pseudo_classes, String& name, Decorator*& decorator, DecoratorDataHandle& decorator_data) const
  106. {
  107. if (index < 0)
  108. return false;
  109. if (index < (int)decorators.size())
  110. {
  111. decorator = decorators[index].decorator;
  112. decorator_data = decorators[index].decorator_data;
  113. name = ":not implemented:";
  114. index += 1;
  115. return true;
  116. }
  117. return false;
  118. }
  119. }
  120. }