PluginRegistry.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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-2023 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. void PluginRegistry::RegisterPlugin(Plugin* plugin)
  38. {
  39. int event_classes = plugin->GetEventClasses();
  40. if (event_classes & Plugin::EVT_BASIC)
  41. basic_plugins.push_back(plugin);
  42. if (event_classes & Plugin::EVT_DOCUMENT)
  43. document_plugins.push_back(plugin);
  44. if (event_classes & Plugin::EVT_ELEMENT)
  45. element_plugins.push_back(plugin);
  46. }
  47. void PluginRegistry::UnregisterPlugin(Plugin* plugin)
  48. {
  49. int event_classes = plugin->GetEventClasses();
  50. if (event_classes & Plugin::EVT_BASIC)
  51. basic_plugins.erase(std::remove(basic_plugins.begin(), basic_plugins.end(), plugin), basic_plugins.end());
  52. if (event_classes & Plugin::EVT_DOCUMENT)
  53. document_plugins.erase(std::remove(document_plugins.begin(), document_plugins.end(), plugin), document_plugins.end());
  54. if (event_classes & Plugin::EVT_ELEMENT)
  55. element_plugins.erase(std::remove(element_plugins.begin(), element_plugins.end(), plugin), element_plugins.end());
  56. }
  57. void PluginRegistry::NotifyInitialise()
  58. {
  59. for (size_t i = 0; i < basic_plugins.size(); ++i)
  60. basic_plugins[i]->OnInitialise();
  61. }
  62. void PluginRegistry::NotifyShutdown()
  63. {
  64. while (!basic_plugins.empty())
  65. {
  66. Plugin* plugin = basic_plugins.back();
  67. PluginRegistry::UnregisterPlugin(plugin);
  68. plugin->OnShutdown();
  69. }
  70. document_plugins.clear();
  71. element_plugins.clear();
  72. }
  73. void PluginRegistry::NotifyContextCreate(Context* context)
  74. {
  75. for (size_t i = 0; i < basic_plugins.size(); ++i)
  76. basic_plugins[i]->OnContextCreate(context);
  77. }
  78. void PluginRegistry::NotifyContextDestroy(Context* context)
  79. {
  80. for (size_t i = 0; i < basic_plugins.size(); ++i)
  81. basic_plugins[i]->OnContextDestroy(context);
  82. }
  83. void PluginRegistry::NotifyDocumentOpen(Context* context, const String& document_path)
  84. {
  85. for (size_t i = 0; i < document_plugins.size(); ++i)
  86. document_plugins[i]->OnDocumentOpen(context, document_path);
  87. }
  88. void PluginRegistry::NotifyDocumentLoad(ElementDocument* document)
  89. {
  90. for (size_t i = 0; i < document_plugins.size(); ++i)
  91. document_plugins[i]->OnDocumentLoad(document);
  92. }
  93. void PluginRegistry::NotifyDocumentUnload(ElementDocument* document)
  94. {
  95. for (size_t i = 0; i < document_plugins.size(); ++i)
  96. document_plugins[i]->OnDocumentUnload(document);
  97. }
  98. void PluginRegistry::NotifyElementCreate(Element* element)
  99. {
  100. for (size_t i = 0; i < element_plugins.size(); ++i)
  101. element_plugins[i]->OnElementCreate(element);
  102. }
  103. void PluginRegistry::NotifyElementDestroy(Element* element)
  104. {
  105. for (size_t i = 0; i < element_plugins.size(); ++i)
  106. element_plugins[i]->OnElementDestroy(element);
  107. }
  108. } // namespace Rml