ElementTabSet.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 = Factory::InstanceElement(nullptr, "*", "tab", XMLAttributes());
  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 = Factory::InstanceElement(nullptr, "*", "panel", XMLAttributes());
  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. old_window->SetProperty(PropertyId::Display, Property(Style::Display::None));
  102. if (new_window)
  103. new_window->RemoveProperty(PropertyId::Display);
  104. active_tab = tab_index;
  105. Dictionary parameters;
  106. parameters["tab_index"] = active_tab;
  107. DispatchEvent(EventId::Tabchange, parameters);
  108. }
  109. }
  110. int ElementTabSet::GetActiveTab() const
  111. {
  112. return active_tab;
  113. }
  114. void ElementTabSet::ProcessDefaultAction(Event& event)
  115. {
  116. Element::ProcessDefaultAction(event);
  117. if (event == EventId::Click)
  118. {
  119. // Find the tab that this click occured on
  120. Element* tabs = GetChildByTag("tabs");
  121. Element* tab = event.GetTargetElement();
  122. while (tab && tab != this && tab->GetParentNode() != tabs)
  123. tab = tab->GetParentNode();
  124. // Abort if we couldn't find the tab the click occured on
  125. if (!tab || tab == this)
  126. return;
  127. // Determine the new active tab index
  128. int new_active_tab = active_tab;
  129. for (int i = 0; i < tabs->GetNumChildren(); i++)
  130. {
  131. if (tabs->GetChild(i) == tab)
  132. {
  133. new_active_tab = i;
  134. break;
  135. }
  136. }
  137. SetActiveTab(new_active_tab);
  138. }
  139. }
  140. void ElementTabSet::OnChildAdd(Element* child)
  141. {
  142. Element::OnChildAdd(child);
  143. if (child->GetParentNode() == GetChildByTag("tabs"))
  144. {
  145. // Set up the new button and append it
  146. child->RemoveProperty(PropertyId::Display);
  147. if (child->GetParentNode()->GetChild(active_tab) == child)
  148. child->SetPseudoClass("selected", true);
  149. }
  150. if (child->GetParentNode() == GetChildByTag("panels"))
  151. {
  152. // Hide the new tab window
  153. child->SetProperty(PropertyId::Display, Property(Style::Display::None));
  154. // Make the new element visible if its the active tab
  155. if (child->GetParentNode()->GetChild(active_tab) == child)
  156. child->RemoveProperty(PropertyId::Display);
  157. }
  158. }
  159. Element* ElementTabSet::GetChildByTag(const String& tag)
  160. {
  161. // Look for the existing child
  162. for (int i = 0; i < GetNumChildren(); i++)
  163. {
  164. if (GetChild(i)->GetTagName() == tag)
  165. return GetChild(i);
  166. }
  167. // If it doesn't exist, create it
  168. ElementPtr element = Factory::InstanceElement(this, "*", tag, XMLAttributes());
  169. Element* result = AppendChild(std::move(element));
  170. return result;
  171. }
  172. } // namespace Rml