WidgetDropDown.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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 "WidgetDropDown.h"
  28. #include <Rocket/Core/Math.h>
  29. #include <Rocket/Core/Factory.h>
  30. #include <Rocket/Core/ElementUtilities.h>
  31. #include <Rocket/Core/Event.h>
  32. #include <Rocket/Core/Property.h>
  33. #include <Rocket/Core/StyleSheetKeywords.h>
  34. #include <Rocket/Controls/ElementFormControl.h>
  35. namespace Rocket {
  36. namespace Controls {
  37. WidgetDropDown::WidgetDropDown(ElementFormControl* element)
  38. {
  39. parent_element = element;
  40. box_layout_dirty = false;
  41. value_layout_dirty = false;
  42. selected_option = -1;
  43. // Create the button and selection elements.
  44. button_element = Core::Factory::InstanceElement(parent_element, "*", "selectarrow", Rocket::Core::XMLAttributes());
  45. value_element = Core::Factory::InstanceElement(element, "*", "selectvalue", Rocket::Core::XMLAttributes());
  46. selection_element = Core::Factory::InstanceElement(parent_element, "*", "selectbox", Rocket::Core::XMLAttributes());
  47. value_element->SetProperty("overflow", "hidden");
  48. selection_element->SetProperty("visibility", "hidden");
  49. selection_element->SetProperty("z-index", Core::Property(1.0f, Core::Property::NUMBER));
  50. selection_element->SetProperty("clip", "none");
  51. parent_element->AddEventListener("click", this, true);
  52. parent_element->AddEventListener("blur", this);
  53. // Add the elements to our parent element.
  54. parent_element->AppendChild(button_element, false);
  55. parent_element->AppendChild(selection_element, false);
  56. parent_element->AppendChild(value_element, false);
  57. }
  58. WidgetDropDown::~WidgetDropDown()
  59. {
  60. ClearOptions();
  61. parent_element->RemoveEventListener("click", this, true);
  62. parent_element->RemoveEventListener("blur", this);
  63. button_element->RemoveReference();
  64. selection_element->RemoveReference();
  65. value_element->RemoveReference();
  66. }
  67. // Updates the selection box layout if necessary.
  68. void WidgetDropDown::OnRender()
  69. {
  70. if (box_layout_dirty)
  71. {
  72. Core::Box box;
  73. Core::ElementUtilities::BuildBox(box, parent_element->GetBox().GetSize(), selection_element);
  74. // Layout the selection box.
  75. Core::ElementUtilities::FormatElement(selection_element, parent_element->GetBox().GetSize(Core::Box::BORDER));
  76. selection_element->SetOffset(Rocket::Core::Vector2f(box.GetEdge(Core::Box::MARGIN, Core::Box::LEFT), parent_element->GetBox().GetSize(Core::Box::BORDER).y + box.GetEdge(Core::Box::MARGIN, Core::Box::TOP)), parent_element);
  77. box_layout_dirty = false;
  78. }
  79. if (value_layout_dirty)
  80. {
  81. Core::ElementUtilities::FormatElement(value_element, parent_element->GetBox().GetSize(Core::Box::BORDER));
  82. value_element->SetOffset(parent_element->GetBox().GetPosition(Core::Box::CONTENT), parent_element);
  83. value_layout_dirty = false;
  84. }
  85. }
  86. void WidgetDropDown::OnLayout()
  87. {
  88. // Layout the button and selection boxes.
  89. Core::Box parent_box = parent_element->GetBox();
  90. Core::ElementUtilities::PositionElement(button_element, Rocket::Core::Vector2f(0, 0), Core::ElementUtilities::TOP_RIGHT);
  91. Core::ElementUtilities::PositionElement(selection_element, Rocket::Core::Vector2f(0, 0), Core::ElementUtilities::TOP_LEFT);
  92. // Calculate the value element position and size.
  93. Rocket::Core::Vector2f size;
  94. size.x = parent_element->GetBox().GetSize(Core::Box::CONTENT).x - button_element->GetBox().GetSize(Core::Box::MARGIN).x;
  95. size.y = parent_element->GetBox().GetSize(Core::Box::CONTENT).y;
  96. value_element->SetOffset(parent_element->GetBox().GetPosition(Core::Box::CONTENT), parent_element);
  97. value_element->SetBox(Core::Box(size));
  98. box_layout_dirty = true;
  99. value_layout_dirty = true;
  100. }
  101. // Sets the value of the widget.
  102. void WidgetDropDown::SetValue(const Rocket::Core::String& _value)
  103. {
  104. for (size_t i = 0; i < options.size(); ++i)
  105. {
  106. if (options[i].GetValue() == _value)
  107. {
  108. SetSelection((int) i);
  109. return;
  110. }
  111. }
  112. value = _value;
  113. value_element->SetInnerRML(value);
  114. value_layout_dirty = true;
  115. selected_option = -1;
  116. }
  117. // Returns the current value of the widget.
  118. const Rocket::Core::String& WidgetDropDown::GetValue() const
  119. {
  120. return value;
  121. }
  122. // Sets the index of the selection. If the new index lies outside of the bounds, it will be clamped.
  123. void WidgetDropDown::SetSelection(int selection, bool force)
  124. {
  125. Rocket::Core::String new_value;
  126. if (selection < 0 ||
  127. selection >= (int) options.size())
  128. {
  129. selection = -1;
  130. }
  131. else
  132. {
  133. new_value = options[selection].GetValue();
  134. }
  135. if (force ||
  136. selection != selected_option ||
  137. value != new_value)
  138. {
  139. selected_option = selection;
  140. value = new_value;
  141. Rocket::Core::String value_rml;
  142. if (selected_option >= 0)
  143. options[selected_option].GetElement()->GetInnerRML(value_rml);
  144. value_element->SetInnerRML(value_rml);
  145. value_layout_dirty = true;
  146. Rocket::Core::Dictionary parameters;
  147. parameters.Set("value", value);
  148. parent_element->DispatchEvent("change", parameters);
  149. }
  150. }
  151. // Returns the index of the currently selected item.
  152. int WidgetDropDown::GetSelection() const
  153. {
  154. return selected_option;
  155. }
  156. // Adds a new option to the select control.
  157. int WidgetDropDown::AddOption(const Rocket::Core::String& rml, const Rocket::Core::String& value, int before, bool select, bool selectable)
  158. {
  159. // Instance a new element for the option.
  160. Core::Element* element = Core::Factory::InstanceElement(selection_element, "*", "option", Rocket::Core::XMLAttributes());
  161. // Force to block display and inject the RML. Register a click handler so we can be notified of selection.
  162. element->SetProperty("display", "block");
  163. element->SetProperty("clip", "auto");
  164. element->SetInnerRML(rml);
  165. element->AddEventListener("click", this);
  166. int option_index;
  167. if (before < 0 ||
  168. before >= (int) options.size())
  169. {
  170. selection_element->AppendChild(element);
  171. options.push_back(SelectOption(element, value, selectable));
  172. option_index = (int) options.size() - 1;
  173. }
  174. else
  175. {
  176. selection_element->InsertBefore(element, selection_element->GetChild(before));
  177. options.insert(options.begin() + before, SelectOption(element, value, selectable));
  178. option_index = before;
  179. }
  180. element->RemoveReference();
  181. // Select the option if appropriate.
  182. if (select)
  183. SetSelection(option_index);
  184. box_layout_dirty = true;
  185. return option_index;
  186. }
  187. // Removes an option from the select control.
  188. void WidgetDropDown::RemoveOption(int index)
  189. {
  190. if (index < 0 ||
  191. index >= (int) options.size())
  192. return;
  193. // Remove the listener and delete the option element.
  194. options[index].GetElement()->RemoveEventListener("click", this);
  195. selection_element->RemoveChild(options[index].GetElement());
  196. options.erase(options.begin() + index);
  197. box_layout_dirty = true;
  198. }
  199. // Removes all options from the list.
  200. void WidgetDropDown::ClearOptions()
  201. {
  202. while (!options.empty())
  203. RemoveOption((int) options.size() - 1);
  204. }
  205. // Returns on of the widget's options.
  206. SelectOption* WidgetDropDown::GetOption(int index)
  207. {
  208. if (index < 0 ||
  209. index >= GetNumOptions())
  210. return NULL;
  211. return &options[index];
  212. }
  213. // Returns the number of options in the widget.
  214. int WidgetDropDown::GetNumOptions() const
  215. {
  216. return (int) options.size();
  217. }
  218. void WidgetDropDown::ProcessEvent(Core::Event& event)
  219. {
  220. // Process the button onclick
  221. if (event == "click" &&
  222. !parent_element->IsDisabled())
  223. {
  224. if (event.GetCurrentElement()->GetParentNode() == selection_element)
  225. {
  226. // Find the element in the options and fire the selection event
  227. for (size_t i = 0; i < options.size(); i++)
  228. {
  229. if (options[i].GetElement() == event.GetCurrentElement())
  230. {
  231. if (options[i].IsSelectable())
  232. {
  233. SetSelection(i);
  234. event.StopPropagation();
  235. ShowSelectBox(false);
  236. parent_element->Focus();
  237. }
  238. }
  239. }
  240. }
  241. else
  242. {
  243. // We have to check that this event isn't targeted to an element
  244. // inside the selection box as we'll get all events coming from our
  245. // root level select element as well as the ones coming from options (which
  246. // get caught in the above if)
  247. Core::Element* element = event.GetTargetElement();
  248. while (element && element != parent_element)
  249. {
  250. if (element == selection_element)
  251. return;
  252. element = element->GetParentNode();
  253. }
  254. if (selection_element->GetProperty< int >("visibility") == Core::VISIBILITY_HIDDEN)
  255. ShowSelectBox(true);
  256. else
  257. ShowSelectBox(false);
  258. }
  259. }
  260. else if (event == "blur" && event.GetTargetElement() == parent_element)
  261. ShowSelectBox(false);
  262. }
  263. // Shows or hides the selection box.
  264. void WidgetDropDown::ShowSelectBox(bool show)
  265. {
  266. if (show)
  267. {
  268. selection_element->SetProperty("visibility", "visible");
  269. value_element->SetPseudoClass("checked", true);
  270. button_element->SetPseudoClass("checked", true);
  271. }
  272. else
  273. {
  274. selection_element->SetProperty("visibility", "hidden");
  275. value_element->SetPseudoClass("checked", false);
  276. button_element->SetPseudoClass("checked", false);
  277. }
  278. }
  279. }
  280. }