ElementTabSet.cpp 6.9 KB

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