ElementTabSet.cpp 5.9 KB

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