ElementTabSet.cpp 5.8 KB

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