PluginRegistry.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "PluginRegistry.h"
  29. #include "../../Include/RmlUi/Core/Plugin.h"
  30. #include <algorithm>
  31. namespace Rml {
  32. typedef Vector< Plugin* > PluginList;
  33. static PluginList basic_plugins;
  34. static PluginList document_plugins;
  35. static PluginList element_plugins;
  36. PluginRegistry::PluginRegistry()
  37. {
  38. }
  39. void PluginRegistry::RegisterPlugin(Plugin* plugin)
  40. {
  41. int event_classes = plugin->GetEventClasses();
  42. if (event_classes & Plugin::EVT_BASIC)
  43. basic_plugins.push_back(plugin);
  44. if (event_classes & Plugin::EVT_DOCUMENT)
  45. document_plugins.push_back(plugin);
  46. if (event_classes & Plugin::EVT_ELEMENT)
  47. element_plugins.push_back(plugin);
  48. }
  49. void PluginRegistry::UnregisterPlugin(Plugin* plugin)
  50. {
  51. int event_classes = plugin->GetEventClasses();
  52. if(event_classes & Plugin::EVT_BASIC)
  53. basic_plugins.erase(std::remove(basic_plugins.begin(), basic_plugins.end(), plugin), basic_plugins.end());
  54. if(event_classes & Plugin::EVT_DOCUMENT)
  55. document_plugins.erase(std::remove(document_plugins.begin(), document_plugins.end(), plugin), document_plugins.end());
  56. if(event_classes & Plugin::EVT_ELEMENT)
  57. element_plugins.erase(std::remove(element_plugins.begin(), element_plugins.end(), plugin), element_plugins.end());
  58. }
  59. // Calls OnInitialise() on all plugins.
  60. void PluginRegistry::NotifyInitialise()
  61. {
  62. for (size_t i = 0; i < basic_plugins.size(); ++i)
  63. basic_plugins[i]->OnInitialise();
  64. }
  65. // Calls OnShutdown() on all plugins.
  66. void PluginRegistry::NotifyShutdown()
  67. {
  68. while (!basic_plugins.empty())
  69. {
  70. basic_plugins.back()->OnShutdown();
  71. basic_plugins.pop_back();
  72. }
  73. document_plugins.clear();
  74. element_plugins.clear();
  75. }
  76. // Calls OnContextCreate() on all plugins.
  77. void PluginRegistry::NotifyContextCreate(Context* context)
  78. {
  79. for (size_t i = 0; i < basic_plugins.size(); ++i)
  80. basic_plugins[i]->OnContextCreate(context);
  81. }
  82. // Calls OnContextDestroy() on all plugins.
  83. void PluginRegistry::NotifyContextDestroy(Context* context)
  84. {
  85. for (size_t i = 0; i < basic_plugins.size(); ++i)
  86. basic_plugins[i]->OnContextDestroy(context);
  87. }
  88. // Calls OnDocumentOpen() on all plugins.
  89. void PluginRegistry::NotifyDocumentOpen(Context* context, const String& document_path)
  90. {
  91. for (size_t i = 0; i < document_plugins.size(); ++i)
  92. document_plugins[i]->OnDocumentOpen(context, document_path);
  93. }
  94. // Calls OnDocumentLoad() on all plugins.
  95. void PluginRegistry::NotifyDocumentLoad(ElementDocument* document)
  96. {
  97. for (size_t i = 0; i < document_plugins.size(); ++i)
  98. document_plugins[i]->OnDocumentLoad(document);
  99. }
  100. // Calls OnDocumentUnload() on all plugins.
  101. void PluginRegistry::NotifyDocumentUnload(ElementDocument* document)
  102. {
  103. for (size_t i = 0; i < document_plugins.size(); ++i)
  104. document_plugins[i]->OnDocumentUnload(document);
  105. }
  106. // Calls OnElementCreate() on all plugins.
  107. void PluginRegistry::NotifyElementCreate(Element* element)
  108. {
  109. for (size_t i = 0; i < element_plugins.size(); ++i)
  110. element_plugins[i]->OnElementCreate(element);
  111. }
  112. // Calls OnElementDestroy() on all plugins.
  113. void PluginRegistry::NotifyElementDestroy(Element* element)
  114. {
  115. for (size_t i = 0; i < element_plugins.size(); ++i)
  116. element_plugins[i]->OnElementDestroy(element);
  117. }
  118. } // namespace Rml