WidgetDropDown.cpp 12 KB

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