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