WidgetDropDown.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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", Core::Property(Core::Style::Overflow::Hidden));
  49. selection_element->SetProperty("visibility", Core::Property(Core::Style::Visibility::Hidden));
  50. selection_element->SetProperty("z-index", Core::Property(1.0f, Core::Property::NUMBER));
  51. selection_element->SetProperty("clip", Core::Property(Core::Style::Clip::None));
  52. parent_element->AddEventListener(Core::EventId::Click, this, true);
  53. parent_element->AddEventListener(Core::EventId::Blur, this);
  54. parent_element->AddEventListener(Core::EventId::Focus, this);
  55. parent_element->AddEventListener(Core::EventId::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(Core::EventId::Click, this);
  68. parent_element->RemoveEventListener(Core::EventId::Click, this, true);
  69. parent_element->RemoveEventListener(Core::EventId::Blur, this);
  70. parent_element->RemoveEventListener(Core::EventId::Focus, this);
  71. parent_element->RemoveEventListener(Core::EventId::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. if (selected_option >= 0 && selected_option < (int)options.size())
  128. options[selected_option].GetElement()->SetPseudoClass("checked", false);
  129. value = _value;
  130. value_element->SetInnerRML(value);
  131. value_layout_dirty = true;
  132. selected_option = -1;
  133. }
  134. // Returns the current value of the widget.
  135. const Rocket::Core::String& WidgetDropDown::GetValue() const
  136. {
  137. return value;
  138. }
  139. // Sets the index of the selection. If the new index lies outside of the bounds, it will be clamped.
  140. void WidgetDropDown::SetSelection(int selection, bool force)
  141. {
  142. Rocket::Core::String new_value;
  143. if (selection < 0 ||
  144. selection >= (int) options.size())
  145. {
  146. selection = -1;
  147. }
  148. else
  149. {
  150. new_value = options[selection].GetValue();
  151. }
  152. if (force ||
  153. selection != selected_option ||
  154. value != new_value)
  155. {
  156. if (selected_option >= 0 && selected_option < (int)options.size())
  157. options[selected_option].GetElement()->SetPseudoClass("checked", false);
  158. selected_option = selection;
  159. value = new_value;
  160. Rocket::Core::String value_rml;
  161. if (selected_option >= 0)
  162. {
  163. auto* el = options[selected_option].GetElement();
  164. el->GetInnerRML(value_rml);
  165. el->SetPseudoClass("checked", true);
  166. }
  167. value_element->SetInnerRML(value_rml);
  168. value_layout_dirty = true;
  169. Rocket::Core::Dictionary parameters;
  170. parameters["value"] = value;
  171. parent_element->DispatchEvent(Core::EventId::Change, parameters);
  172. }
  173. }
  174. // Returns the index of the currently selected item.
  175. int WidgetDropDown::GetSelection() const
  176. {
  177. return selected_option;
  178. }
  179. // Adds a new option to the select control.
  180. int WidgetDropDown::AddOption(const Rocket::Core::String& rml, const Rocket::Core::String& value, int before, bool select, bool selectable)
  181. {
  182. // Instance a new element for the option.
  183. Core::Element* element = Core::Factory::InstanceElement(selection_element, "*", "option", Rocket::Core::XMLAttributes());
  184. // Force to block display and inject the RML. Register a click handler so we can be notified of selection.
  185. element->SetProperty("display", Core::Property(Core::Style::Display::Block));
  186. element->SetProperty("clip", Core::Property(Core::Style::Clip::Auto));
  187. element->SetInnerRML(rml);
  188. element->AddEventListener(Core::EventId::Click, this);
  189. int option_index;
  190. if (before < 0 ||
  191. before >= (int) options.size())
  192. {
  193. selection_element->AppendChild(element);
  194. options.push_back(SelectOption(element, value, selectable));
  195. option_index = (int) options.size() - 1;
  196. }
  197. else
  198. {
  199. selection_element->InsertBefore(element, selection_element->GetChild(before));
  200. options.insert(options.begin() + before, SelectOption(element, value, selectable));
  201. option_index = before;
  202. }
  203. element->RemoveReference();
  204. // Select the option if appropriate.
  205. if (select)
  206. SetSelection(option_index);
  207. box_layout_dirty = true;
  208. return option_index;
  209. }
  210. // Removes an option from the select control.
  211. void WidgetDropDown::RemoveOption(int index)
  212. {
  213. if (index < 0 ||
  214. index >= (int) options.size())
  215. return;
  216. // Remove the listener and delete the option element.
  217. options[index].GetElement()->RemoveEventListener(Core::EventId::Click, this);
  218. selection_element->RemoveChild(options[index].GetElement());
  219. options.erase(options.begin() + index);
  220. box_layout_dirty = true;
  221. }
  222. // Removes all options from the list.
  223. void WidgetDropDown::ClearOptions()
  224. {
  225. while (!options.empty())
  226. RemoveOption((int) options.size() - 1);
  227. }
  228. // Returns on of the widget's options.
  229. SelectOption* WidgetDropDown::GetOption(int index)
  230. {
  231. if (index < 0 ||
  232. index >= GetNumOptions())
  233. return NULL;
  234. return &options[index];
  235. }
  236. // Returns the number of options in the widget.
  237. int WidgetDropDown::GetNumOptions() const
  238. {
  239. return (int) options.size();
  240. }
  241. void WidgetDropDown::ProcessEvent(Core::Event& event)
  242. {
  243. if (parent_element->IsDisabled())
  244. return;
  245. // Process the button onclick
  246. switch (event.GetId())
  247. {
  248. case Core::EventId::Click:
  249. {
  250. if (event.GetCurrentElement()->GetParentNode() == selection_element)
  251. {
  252. // Find the element in the options and fire the selection event
  253. for (size_t i = 0; i < options.size(); i++)
  254. {
  255. if (options[i].GetElement() == event.GetCurrentElement())
  256. {
  257. if (options[i].IsSelectable())
  258. {
  259. SetSelection((int)i);
  260. event.StopPropagation();
  261. ShowSelectBox(false);
  262. parent_element->Focus();
  263. }
  264. }
  265. }
  266. }
  267. else
  268. {
  269. // We have to check that this event isn't targeted to an element
  270. // inside the selection box as we'll get all events coming from our
  271. // root level select element as well as the ones coming from options (which
  272. // get caught in the above if)
  273. Core::Element* element = event.GetTargetElement();
  274. while (element && element != parent_element)
  275. {
  276. if (element == selection_element)
  277. return;
  278. element = element->GetParentNode();
  279. }
  280. if (selection_element->GetComputedValues().visibility == Core::Style::Visibility::Hidden)
  281. ShowSelectBox(true);
  282. else
  283. ShowSelectBox(false);
  284. }
  285. }
  286. break;
  287. case Core::EventId::Focus:
  288. {
  289. if (event.GetTargetElement() == parent_element)
  290. {
  291. value_element->SetPseudoClass("focus", true);
  292. button_element->SetPseudoClass("focus", true);
  293. }
  294. }
  295. case Core::EventId::Blur:
  296. {
  297. if (event.GetTargetElement() == parent_element)
  298. {
  299. ShowSelectBox(false);
  300. value_element->SetPseudoClass("focus", false);
  301. button_element->SetPseudoClass("focus", false);
  302. }
  303. }
  304. break;
  305. case Core::EventId::Keydown:
  306. {
  307. Core::Input::KeyIdentifier key_identifier = (Core::Input::KeyIdentifier) event.GetParameter< int >("key_identifier", 0);
  308. switch (key_identifier)
  309. {
  310. case Core::Input::KI_UP:
  311. SetSelection((selected_option - 1 + (int)options.size()) % (int)options.size());
  312. break;
  313. case Core::Input::KI_DOWN:
  314. SetSelection((selected_option + 1) % (int)options.size());
  315. break;
  316. default:
  317. break;
  318. }
  319. }
  320. break;
  321. default:
  322. break;
  323. }
  324. }
  325. // Shows or hides the selection box.
  326. void WidgetDropDown::ShowSelectBox(bool show)
  327. {
  328. if (show)
  329. {
  330. selection_element->SetProperty("visibility", Core::Property(Core::Style::Visibility::Visible));
  331. value_element->SetPseudoClass("checked", true);
  332. button_element->SetPseudoClass("checked", true);
  333. }
  334. else
  335. {
  336. selection_element->SetProperty("visibility", Core::Property(Core::Style::Visibility::Hidden));
  337. value_element->SetPseudoClass("checked", false);
  338. button_element->SetPseudoClass("checked", false);
  339. }
  340. }
  341. }
  342. }