ElementTabSet.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 <Rocket/Controls/ElementTabSet.h>
  28. #include <Rocket/Core/Math.h>
  29. #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. }
  46. // Sets the specifed tab index's tab panel RML.
  47. void ElementTabSet::SetPanel(int tab_index, const Rocket::Core::String& rml)
  48. {
  49. Core::Element* element = Core::Factory::InstanceElement(NULL, "*", "tab", Rocket::Core::XMLAttributes());
  50. Core::Factory::InstanceElementText(element, rml);
  51. SetPanel(tab_index, element);
  52. }
  53. // Set the specifed tab index's title element.
  54. void ElementTabSet::SetTab(int tab_index, Core::Element* element)
  55. {
  56. Core::Element* tabs = GetChildByTag("tabs");
  57. if (tab_index >= 0 &&
  58. tab_index < tabs->GetNumChildren())
  59. tabs->ReplaceChild(GetChild(tab_index), element);
  60. else
  61. tabs->AppendChild(element);
  62. }
  63. // Set the specified tab index's body element.
  64. void ElementTabSet::SetPanel(int tab_index, Core::Element* element)
  65. {
  66. // Append the window
  67. Core::Element* windows = GetChildByTag("panels");
  68. if (tab_index >= 0 &&
  69. tab_index < windows->GetNumChildren())
  70. windows->ReplaceChild(GetChild(tab_index), element);
  71. else
  72. windows->AppendChild(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. Core::Element* panels = GetChildByTag("panels");
  80. Core::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. Core::Element* tabs = GetChildByTag("tabs");
  99. Core::Element* old_tab = tabs->GetChild(active_tab);
  100. Core::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. Core::Element* windows = GetChildByTag("panels");
  106. Core::Element* old_window = windows->GetChild(active_tab);
  107. Core::Element* new_window = windows->GetChild(tab_index);
  108. if (old_window)
  109. old_window->SetProperty("display", "none");
  110. if (new_window)
  111. new_window->SetProperty("display", "inline-block");
  112. active_tab = tab_index;
  113. Rocket::Core::Dictionary parameters;
  114. parameters.Set("tab_index", active_tab);
  115. DispatchEvent("tabchange", parameters);
  116. }
  117. }
  118. int ElementTabSet::GetActiveTab() const
  119. {
  120. return active_tab;
  121. }
  122. // Process the incoming event.
  123. void ElementTabSet::ProcessEvent(Core::Event& event)
  124. {
  125. Core::Element::ProcessEvent(event);
  126. if (event.GetCurrentElement() == this && event == "click")
  127. {
  128. // Find the tab that this click occured on
  129. Core::Element* tabs = GetChildByTag("tabs");
  130. Core::Element* tab = event.GetTargetElement();
  131. while (tab && tab != this && tab->GetParentNode() != tabs)
  132. tab = tab->GetParentNode();
  133. // Abort if we couldn't find the tab the click occured on
  134. if (!tab || tab == this)
  135. return;
  136. // Determine the new active tab index
  137. int new_active_tab = active_tab;
  138. for (int i = 0; i < tabs->GetNumChildren(); i++)
  139. {
  140. if (tabs->GetChild(i) == tab)
  141. {
  142. new_active_tab = i;
  143. break;
  144. }
  145. }
  146. SetActiveTab(new_active_tab);
  147. }
  148. }
  149. void ElementTabSet::OnChildAdd(Core::Element* child)
  150. {
  151. Core::Element::OnChildAdd(child);
  152. if (child->GetParentNode() == GetChildByTag("tabs"))
  153. {
  154. // Set up the new button and append it
  155. child->SetProperty("display", "inline-block");
  156. child->AddEventListener("click", this);
  157. if (child->GetParentNode()->GetChild(active_tab) == child)
  158. child->SetPseudoClass("selected", true);
  159. }
  160. if (child->GetParentNode() == GetChildByTag("panels"))
  161. {
  162. // Hide the new tab window
  163. child->SetProperty("display", "none");
  164. // Make the new element visible if its the active tab
  165. if (child->GetParentNode()->GetChild(active_tab) == child)
  166. child->SetProperty("display", "inline-block");
  167. }
  168. }
  169. void ElementTabSet::OnChildRemove(Core::Element* child)
  170. {
  171. Core::Element::OnChildRemove(child);
  172. // If its a tab, remove its event listener
  173. if (child->GetParentNode() == GetChildByTag("tabs"))
  174. {
  175. child->RemoveEventListener("click", this);
  176. }
  177. }
  178. Core::Element* ElementTabSet::GetChildByTag(const Rocket::Core::String& tag)
  179. {
  180. // Look for the existing child
  181. for (int i = 0; i < GetNumChildren(); i++)
  182. {
  183. if (GetChild(i)->GetTagName() == tag)
  184. return GetChild(i);
  185. }
  186. // If it doesn't exist, create it
  187. Core::Element* element = Core::Factory::InstanceElement(this, "*", tag, Rocket::Core::XMLAttributes());
  188. AppendChild(element);
  189. element->RemoveReference();
  190. return element;
  191. }
  192. void ElementTabSet::OnAttach(Core::Element * ROCKET_UNUSED(element))
  193. {
  194. AddReference();
  195. }
  196. void ElementTabSet::OnDetach(Core::Element * ROCKET_UNUSED(element))
  197. {
  198. RemoveReference();
  199. }
  200. }
  201. }