WidgetDropDown.cpp 11 KB

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