ElementTabSet.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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("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. Core::Element::ProcessEvent(event);
  128. if (event.GetCurrentElement() == this && event == "click")
  129. {
  130. // Find the tab that this click occured on
  131. Core::Element* tabs = GetChildByTag("tabs");
  132. Core::Element* tab = event.GetTargetElement();
  133. while (tab && tab != this && tab->GetParentNode() != tabs)
  134. tab = tab->GetParentNode();
  135. // Abort if we couldn't find the tab the click occured on
  136. if (!tab || tab == this)
  137. return;
  138. // Determine the new active tab index
  139. int new_active_tab = active_tab;
  140. for (int i = 0; i < tabs->GetNumChildren(); i++)
  141. {
  142. if (tabs->GetChild(i) == tab)
  143. {
  144. new_active_tab = i;
  145. break;
  146. }
  147. }
  148. SetActiveTab(new_active_tab);
  149. }
  150. }
  151. void ElementTabSet::OnChildAdd(Core::Element* child)
  152. {
  153. Core::Element::OnChildAdd(child);
  154. if (child->GetParentNode() == GetChildByTag("tabs"))
  155. {
  156. // Set up the new button and append it
  157. child->SetProperty("display", Core::Property(Core::Style::Display::InlineBlock));
  158. child->AddEventListener("click", this);
  159. if (child->GetParentNode()->GetChild(active_tab) == child)
  160. child->SetPseudoClass("selected", true);
  161. }
  162. if (child->GetParentNode() == GetChildByTag("panels"))
  163. {
  164. // Hide the new tab window
  165. child->SetProperty("display", Core::Property(Core::Style::Display::None));
  166. // Make the new element visible if its the active tab
  167. if (child->GetParentNode()->GetChild(active_tab) == child)
  168. child->SetProperty("display", Core::Property(Core::Style::Display::InlineBlock));
  169. }
  170. }
  171. void ElementTabSet::OnChildRemove(Core::Element* child)
  172. {
  173. Core::Element::OnChildRemove(child);
  174. // If its a tab, remove its event listener
  175. if (child->GetParentNode() == GetChildByTag("tabs"))
  176. {
  177. child->RemoveEventListener("click", this);
  178. }
  179. }
  180. Core::Element* ElementTabSet::GetChildByTag(const Rocket::Core::String& tag)
  181. {
  182. // Look for the existing child
  183. for (int i = 0; i < GetNumChildren(); i++)
  184. {
  185. if (GetChild(i)->GetTagName() == tag)
  186. return GetChild(i);
  187. }
  188. // If it doesn't exist, create it
  189. Core::Element* element = Core::Factory::InstanceElement(this, "*", tag, Rocket::Core::XMLAttributes());
  190. AppendChild(element);
  191. element->RemoveReference();
  192. return element;
  193. }
  194. void ElementTabSet::OnAttach(Core::Element * ROCKET_UNUSED_PARAMETER(element))
  195. {
  196. ROCKET_UNUSED(element);
  197. AddReference();
  198. }
  199. void ElementTabSet::OnDetach(Core::Element * ROCKET_UNUSED_PARAMETER(element))
  200. {
  201. ROCKET_UNUSED(element);
  202. RemoveReference();
  203. }
  204. }
  205. }