ElementTabSet.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #include "../../Include/Rocket/Controls/ElementTabSet.h"
  28. #include "../../Include/Rocket/Core/Math.h"
  29. #include "../../Include/Rocket/Core/Factory.h"
  30. namespace Rocket {
  31. namespace Controls {
  32. ElementTabSet::ElementTabSet(const Rocket::Core::String& tag) : Core::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 Rocket::Core::String& rml)
  41. {
  42. Core::Element* element = Core::Factory::InstanceElement(NULL, "*", "tab", Rocket::Core::XMLAttributes());
  43. Core::Factory::InstanceElementText(element, rml);
  44. SetTab(tab_index, element);
  45. element->RemoveReference();
  46. }
  47. // Sets the specifed tab index's tab panel RML.
  48. void ElementTabSet::SetPanel(int tab_index, const Rocket::Core::String& rml)
  49. {
  50. Core::Element* element = Core::Factory::InstanceElement(NULL, "*", "panel", Rocket::Core::XMLAttributes());
  51. Core::Factory::InstanceElementText(element, rml);
  52. SetPanel(tab_index, element);
  53. element->RemoveReference();
  54. }
  55. // Set the specifed tab index's title element.
  56. void ElementTabSet::SetTab(int tab_index, Core::Element* element)
  57. {
  58. Core::Element* tabs = GetChildByTag("tabs");
  59. if (tab_index >= 0 &&
  60. tab_index < tabs->GetNumChildren())
  61. tabs->ReplaceChild(GetChild(tab_index), element);
  62. else
  63. tabs->AppendChild(element);
  64. }
  65. // Set the specified tab index's body element.
  66. void ElementTabSet::SetPanel(int tab_index, Core::Element* element)
  67. {
  68. // append the window
  69. Core::Element* windows = GetChildByTag("panels");
  70. if (tab_index >= 0 &&
  71. tab_index < windows->GetNumChildren())
  72. windows->ReplaceChild(GetChild(tab_index), element);
  73. else
  74. windows->AppendChild(element);
  75. }
  76. // Remove one of the tab set's panels and its corresponding tab.
  77. void ElementTabSet::RemoveTab(int tab_index)
  78. {
  79. if (tab_index < 0)
  80. return;
  81. Core::Element* panels = GetChildByTag("panels");
  82. Core::Element* tabs = GetChildByTag("tabs");
  83. if (panels->GetNumChildren() > tab_index &&
  84. tabs->GetNumChildren() > tab_index)
  85. {
  86. panels->RemoveChild(panels->GetChild(tab_index));
  87. tabs->RemoveChild(tabs->GetChild(tab_index));
  88. }
  89. }
  90. // Retrieve the number of tabs in the tabset.
  91. int ElementTabSet::GetNumTabs()
  92. {
  93. return GetChildByTag("tabs")->GetNumChildren();
  94. }
  95. void ElementTabSet::SetActiveTab(int tab_index)
  96. {
  97. // Update display if the tab has changed
  98. if (tab_index != active_tab)
  99. {
  100. Core::Element* tabs = GetChildByTag("tabs");
  101. Core::Element* old_tab = tabs->GetChild(active_tab);
  102. Core::Element* new_tab = tabs->GetChild(tab_index);
  103. if (old_tab)
  104. old_tab->SetPseudoClass("selected", false);
  105. if (new_tab)
  106. new_tab->SetPseudoClass("selected", true);
  107. Core::Element* windows = GetChildByTag("panels");
  108. Core::Element* old_window = windows->GetChild(active_tab);
  109. Core::Element* new_window = windows->GetChild(tab_index);
  110. if (old_window)
  111. old_window->SetProperty("display", Core::Property(Core::Style::Display::None));
  112. if (new_window)
  113. new_window->SetProperty("display", Core::Property(Core::Style::Display::InlineBlock));
  114. active_tab = tab_index;
  115. Rocket::Core::Dictionary parameters;
  116. parameters["tab_index"] = active_tab;
  117. DispatchEvent(Core::EventId::Tabchange, parameters);
  118. }
  119. }
  120. int ElementTabSet::GetActiveTab() const
  121. {
  122. return active_tab;
  123. }
  124. // Process the incoming event.
  125. void ElementTabSet::ProcessEvent(Core::Event& event)
  126. {
  127. if (event.GetCurrentElement() == this && event == Core::EventId::Click)
  128. {
  129. // Find the tab that this click occured on
  130. Core::Element* tabs = GetChildByTag("tabs");
  131. Core::Element* tab = event.GetTargetElement();
  132. while (tab && tab != this && tab->GetParentNode() != tabs)
  133. tab = tab->GetParentNode();
  134. // Abort if we couldn't find the tab the click occured on
  135. if (!tab || tab == this)
  136. return;
  137. // Determine the new active tab index
  138. int new_active_tab = active_tab;
  139. for (int i = 0; i < tabs->GetNumChildren(); i++)
  140. {
  141. if (tabs->GetChild(i) == tab)
  142. {
  143. new_active_tab = i;
  144. break;
  145. }
  146. }
  147. SetActiveTab(new_active_tab);
  148. }
  149. }
  150. void ElementTabSet::OnChildAdd(Core::Element* child)
  151. {
  152. Core::Element::OnChildAdd(child);
  153. if (child->GetParentNode() == GetChildByTag("tabs"))
  154. {
  155. // Set up the new button and append it
  156. child->SetProperty("display", Core::Property(Core::Style::Display::InlineBlock));
  157. child->AddEventListener(Core::EventId::Click, this);
  158. if (child->GetParentNode()->GetChild(active_tab) == child)
  159. child->SetPseudoClass("selected", true);
  160. }
  161. if (child->GetParentNode() == GetChildByTag("panels"))
  162. {
  163. // Hide the new tab window
  164. child->SetProperty("display", Core::Property(Core::Style::Display::None));
  165. // Make the new element visible if its the active tab
  166. if (child->GetParentNode()->GetChild(active_tab) == child)
  167. child->SetProperty("display", Core::Property(Core::Style::Display::InlineBlock));
  168. }
  169. }
  170. void ElementTabSet::OnChildRemove(Core::Element* child)
  171. {
  172. Core::Element::OnChildRemove(child);
  173. // If its a tab, remove its event listener
  174. if (child->GetParentNode() == GetChildByTag("tabs"))
  175. {
  176. child->RemoveEventListener(Core::EventId::Click, this);
  177. }
  178. }
  179. Core::Element* ElementTabSet::GetChildByTag(const Rocket::Core::String& tag)
  180. {
  181. // Look for the existing child
  182. for (int i = 0; i < GetNumChildren(); i++)
  183. {
  184. if (GetChild(i)->GetTagName() == tag)
  185. return GetChild(i);
  186. }
  187. // If it doesn't exist, create it
  188. Core::Element* element = Core::Factory::InstanceElement(this, "*", tag, Rocket::Core::XMLAttributes());
  189. AppendChild(element);
  190. element->RemoveReference();
  191. return element;
  192. }
  193. void ElementTabSet::OnAttach(Core::Element * ROCKET_UNUSED_PARAMETER(element))
  194. {
  195. ROCKET_UNUSED(element);
  196. AddReference();
  197. }
  198. void ElementTabSet::OnDetach(Core::Element * ROCKET_UNUSED_PARAMETER(element))
  199. {
  200. ROCKET_UNUSED(element);
  201. RemoveReference();
  202. }
  203. }
  204. }