ElementTabSet.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 "../../Include/RmlUi/Controls/ElementTabSet.h"
  29. #include "../../Include/RmlUi/Core/Math.h"
  30. #include "../../Include/RmlUi/Core/Factory.h"
  31. namespace Rml {
  32. namespace Controls {
  33. ElementTabSet::ElementTabSet(const Rml::Core::String& tag) : Core::Element(tag)
  34. {
  35. active_tab = 0;
  36. }
  37. ElementTabSet::~ElementTabSet()
  38. {
  39. }
  40. // Sets the specifed tab index's tab title RML.
  41. void ElementTabSet::SetTab(int tab_index, const Rml::Core::String& rml)
  42. {
  43. Core::ElementPtr element = Core::Factory::InstanceElement(nullptr, "*", "tab", Rml::Core::XMLAttributes());
  44. Core::Factory::InstanceElementText(element.get(), rml);
  45. SetTab(tab_index, std::move(element));
  46. }
  47. // Sets the specifed tab index's tab panel RML.
  48. void ElementTabSet::SetPanel(int tab_index, const Rml::Core::String& rml)
  49. {
  50. Core::ElementPtr element = Core::Factory::InstanceElement(nullptr, "*", "panel", Rml::Core::XMLAttributes());
  51. Core::Factory::InstanceElementText(element.get(), rml);
  52. SetPanel(tab_index, std::move(element));
  53. }
  54. // Set the specifed tab index's title element.
  55. void ElementTabSet::SetTab(int tab_index, Core::ElementPtr element)
  56. {
  57. Core::Element* tabs = GetChildByTag("tabs");
  58. if (tab_index >= 0 &&
  59. tab_index < tabs->GetNumChildren())
  60. tabs->ReplaceChild(std::move(element), GetChild(tab_index));
  61. else
  62. tabs->AppendChild(std::move(element));
  63. }
  64. // Set the specified tab index's body element.
  65. void ElementTabSet::SetPanel(int tab_index, Core::ElementPtr element)
  66. {
  67. // append the window
  68. Core::Element* windows = GetChildByTag("panels");
  69. if (tab_index >= 0 &&
  70. tab_index < windows->GetNumChildren())
  71. windows->ReplaceChild(std::move(element), GetChild(tab_index));
  72. else
  73. windows->AppendChild(std::move(element));
  74. }
  75. // Remove one of the tab set's panels and its corresponding tab.
  76. void ElementTabSet::RemoveTab(int tab_index)
  77. {
  78. if (tab_index < 0)
  79. return;
  80. Core::Element* panels = GetChildByTag("panels");
  81. Core::Element* tabs = GetChildByTag("tabs");
  82. if (panels->GetNumChildren() > tab_index &&
  83. tabs->GetNumChildren() > tab_index)
  84. {
  85. panels->RemoveChild(panels->GetChild(tab_index));
  86. tabs->RemoveChild(tabs->GetChild(tab_index));
  87. }
  88. }
  89. // Retrieve the number of tabs in the tabset.
  90. int ElementTabSet::GetNumTabs()
  91. {
  92. return GetChildByTag("tabs")->GetNumChildren();
  93. }
  94. void ElementTabSet::SetActiveTab(int tab_index)
  95. {
  96. // Update display if the tab has changed
  97. if (tab_index != active_tab)
  98. {
  99. Core::Element* tabs = GetChildByTag("tabs");
  100. Core::Element* old_tab = tabs->GetChild(active_tab);
  101. Core::Element* new_tab = tabs->GetChild(tab_index);
  102. if (old_tab)
  103. old_tab->SetPseudoClass("selected", false);
  104. if (new_tab)
  105. new_tab->SetPseudoClass("selected", true);
  106. Core::Element* windows = GetChildByTag("panels");
  107. Core::Element* old_window = windows->GetChild(active_tab);
  108. Core::Element* new_window = windows->GetChild(tab_index);
  109. if (old_window)
  110. old_window->SetProperty(Core::PropertyId::Display, Core::Property(Core::Style::Display::None));
  111. if (new_window)
  112. new_window->SetProperty(Core::PropertyId::Display, Core::Property(Core::Style::Display::InlineBlock));
  113. active_tab = tab_index;
  114. Rml::Core::Dictionary parameters;
  115. parameters["tab_index"] = active_tab;
  116. DispatchEvent(Core::EventId::Tabchange, parameters);
  117. }
  118. }
  119. int ElementTabSet::GetActiveTab() const
  120. {
  121. return active_tab;
  122. }
  123. // Process the incoming event.
  124. void ElementTabSet::ProcessEvent(Core::Event& event)
  125. {
  126. if (event.GetCurrentElement() == this && event == Core::EventId::Click)
  127. {
  128. // Find the tab that this click occured on
  129. Core::Element* tabs = GetChildByTag("tabs");
  130. Core::Element* tab = event.GetTargetElement();
  131. while (tab && tab != this && tab->GetParentNode() != tabs)
  132. tab = tab->GetParentNode();
  133. // Abort if we couldn't find the tab the click occured on
  134. if (!tab || tab == this)
  135. return;
  136. // Determine the new active tab index
  137. int new_active_tab = active_tab;
  138. for (int i = 0; i < tabs->GetNumChildren(); i++)
  139. {
  140. if (tabs->GetChild(i) == tab)
  141. {
  142. new_active_tab = i;
  143. break;
  144. }
  145. }
  146. SetActiveTab(new_active_tab);
  147. }
  148. }
  149. void ElementTabSet::OnChildAdd(Core::Element* child)
  150. {
  151. Core::Element::OnChildAdd(child);
  152. if (child->GetParentNode() == GetChildByTag("tabs"))
  153. {
  154. // Set up the new button and append it
  155. child->SetProperty(Core::PropertyId::Display, Core::Property(Core::Style::Display::InlineBlock));
  156. child->AddEventListener(Core::EventId::Click, this);
  157. if (child->GetParentNode()->GetChild(active_tab) == child)
  158. child->SetPseudoClass("selected", true);
  159. }
  160. if (child->GetParentNode() == GetChildByTag("panels"))
  161. {
  162. // Hide the new tab window
  163. child->SetProperty(Core::PropertyId::Display, Core::Property(Core::Style::Display::None));
  164. // Make the new element visible if its the active tab
  165. if (child->GetParentNode()->GetChild(active_tab) == child)
  166. child->SetProperty(Core::PropertyId::Display, Core::Property(Core::Style::Display::InlineBlock));
  167. }
  168. }
  169. void ElementTabSet::OnChildRemove(Core::Element* child)
  170. {
  171. Core::Element::OnChildRemove(child);
  172. // If its a tab, remove its event listener
  173. if (child->GetParentNode() == GetChildByTag("tabs"))
  174. {
  175. child->RemoveEventListener(Core::EventId::Click, this);
  176. }
  177. }
  178. Core::Element* ElementTabSet::GetChildByTag(const Rml::Core::String& tag)
  179. {
  180. // Look for the existing child
  181. for (int i = 0; i < GetNumChildren(); i++)
  182. {
  183. if (GetChild(i)->GetTagName() == tag)
  184. return GetChild(i);
  185. }
  186. // If it doesn't exist, create it
  187. Core::ElementPtr element = Core::Factory::InstanceElement(this, "*", tag, Rml::Core::XMLAttributes());
  188. Core::Element* result = AppendChild(std::move(element));
  189. return result;
  190. }
  191. }
  192. }