WidgetDropDown.cpp 11 KB

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