DropDownList.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. //
  2. // Copyright (c) 2008-2015 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "../../Core/Context.h"
  23. #include "../../Input/InputEvents.h"
  24. #include "../../IO/Log.h"
  25. #include "DropDownList.h"
  26. #include "ListView.h"
  27. #include "Text.h"
  28. #include "UI.h"
  29. #include "UIEvents.h"
  30. #include "Window.h"
  31. #include "../../DebugNew.h"
  32. namespace Atomic
  33. {
  34. extern const char* UI_CATEGORY;
  35. DropDownList::DropDownList(Context* context) :
  36. Menu(context),
  37. resizePopup_(false),
  38. selectionAttr_(0)
  39. {
  40. focusMode_ = FM_FOCUSABLE_DEFOCUSABLE;
  41. Window* window = new Window(context_);
  42. window->SetInternal(true);
  43. SetPopup(window);
  44. listView_ = new ListView(context_);
  45. listView_->SetInternal(true);
  46. listView_->SetScrollBarsVisible(false, false);
  47. popup_->SetLayout(LM_VERTICAL);
  48. popup_->AddChild(listView_);
  49. placeholder_ = CreateChild<UIElement>("DDL_Placeholder");
  50. placeholder_->SetInternal(true);
  51. Text* text = placeholder_->CreateChild<Text>("DDL_Placeholder_Text");
  52. text->SetInternal(true);
  53. text->SetVisible(false);
  54. SubscribeToEvent(listView_, E_ITEMCLICKED, HANDLER(DropDownList, HandleItemClicked));
  55. SubscribeToEvent(listView_, E_UNHANDLEDKEY, HANDLER(DropDownList, HandleListViewKey));
  56. }
  57. DropDownList::~DropDownList()
  58. {
  59. }
  60. void DropDownList::RegisterObject(Context* context)
  61. {
  62. context->RegisterFactory<DropDownList>(UI_CATEGORY);
  63. COPY_BASE_ATTRIBUTES(Menu);
  64. UPDATE_ATTRIBUTE_DEFAULT_VALUE("Focus Mode", FM_FOCUSABLE_DEFOCUSABLE);
  65. ACCESSOR_ATTRIBUTE("Selection", GetSelection, SetSelectionAttr, unsigned, 0, AM_FILE);
  66. ACCESSOR_ATTRIBUTE("Resize Popup", GetResizePopup, SetResizePopup, bool, false, AM_FILE);
  67. }
  68. void DropDownList::ApplyAttributes()
  69. {
  70. // Reapply selection after possible items have been loaded
  71. SetSelection(selectionAttr_);
  72. }
  73. void DropDownList::GetBatches(PODVector<UIBatch>& batches, PODVector<float>& vertexData, const IntRect& currentScissor)
  74. {
  75. Menu::GetBatches(batches, vertexData, currentScissor);
  76. if (!placeholder_->IsVisible())
  77. return;
  78. UIElement* selectedItem = GetSelectedItem();
  79. if (selectedItem)
  80. {
  81. // Can not easily copy the selected item. However, it can be re-rendered on the placeholder's position
  82. const IntVector2& targetPos = placeholder_->GetScreenPosition();
  83. const IntVector2& originalPos = selectedItem->GetScreenPosition();
  84. IntVector2 offset = targetPos - originalPos;
  85. // GetBatches() usually resets the hover flag. Therefore get its value and then reset it for the real rendering
  86. // Render the selected item without its selection color, so temporarily reset the item's selected attribute
  87. bool hover = selectedItem->IsHovering();
  88. selectedItem->SetSelected(false);
  89. selectedItem->SetHovering(false);
  90. selectedItem->GetBatchesWithOffset(offset, batches, vertexData, currentScissor);
  91. selectedItem->SetSelected(true);
  92. selectedItem->SetHovering(hover);
  93. }
  94. }
  95. void DropDownList::OnShowPopup()
  96. {
  97. // Resize the popup to match the size of the list content, and optionally match the button width
  98. UIElement* content = listView_->GetContentElement();
  99. content->UpdateLayout();
  100. const IntVector2& contentSize = content->GetSize();
  101. const IntRect& border = popup_->GetLayoutBorder();
  102. popup_->SetSize(resizePopup_ ? GetWidth() : contentSize.x_ + border.left_ + border.right_,
  103. contentSize.y_ + border.top_ + border.bottom_);
  104. // Check if popup fits below the button. If not, show above instead
  105. bool showAbove = false;
  106. UIElement* root = GetRoot();
  107. if (root)
  108. {
  109. const IntVector2& screenPos = GetScreenPosition();
  110. if (screenPos.y_ + GetHeight() + popup_->GetHeight() > root->GetHeight() && screenPos.y_ - popup_->GetHeight() >= 0)
  111. showAbove = true;
  112. }
  113. SetPopupOffset(0, showAbove ? -popup_->GetHeight() : GetHeight());
  114. // Focus the ListView to allow making the selection with keys
  115. GetSubsystem<UI>()->SetFocusElement(listView_);
  116. }
  117. void DropDownList::OnHidePopup()
  118. {
  119. // When the popup is hidden, propagate the selection
  120. using namespace ItemSelected;
  121. VariantMap& eventData = GetEventDataMap();
  122. eventData[P_ELEMENT] = this;
  123. eventData[P_SELECTION] = GetSelection();
  124. SendEvent(E_ITEMSELECTED, eventData);
  125. }
  126. void DropDownList::OnSetEditable()
  127. {
  128. listView_->SetEditable(editable_);
  129. }
  130. void DropDownList::AddItem(UIElement* item)
  131. {
  132. InsertItem(M_MAX_UNSIGNED, item);
  133. }
  134. void DropDownList::InsertItem(unsigned index, UIElement* item)
  135. {
  136. listView_->InsertItem(index, item);
  137. // If there was no selection, set to the first
  138. if (listView_->GetSelection() == M_MAX_UNSIGNED)
  139. listView_->SetSelection(0);
  140. }
  141. void DropDownList::RemoveItem(UIElement* item)
  142. {
  143. listView_->RemoveItem(item);
  144. }
  145. void DropDownList::RemoveItem(unsigned index)
  146. {
  147. listView_->RemoveItem(index);
  148. }
  149. void DropDownList::RemoveAllItems()
  150. {
  151. listView_->RemoveAllItems();
  152. }
  153. void DropDownList::SetSelection(unsigned index)
  154. {
  155. listView_->SetSelection(index);
  156. // Display the place holder text when there is no selection, however, the place holder text is only visible when the place holder itself is set to visible
  157. placeholder_->GetChild(0)->SetVisible(index == M_MAX_UNSIGNED);
  158. }
  159. void DropDownList::SetPlaceholderText(const String& text)
  160. {
  161. static_cast<Text*>(placeholder_->GetChild(0))->SetText(text);
  162. }
  163. void DropDownList::SetResizePopup(bool enable)
  164. {
  165. resizePopup_ = enable;
  166. }
  167. unsigned DropDownList::GetNumItems() const
  168. {
  169. return listView_->GetNumItems();
  170. }
  171. UIElement* DropDownList::GetItem(unsigned index) const
  172. {
  173. return listView_->GetItem(index);
  174. }
  175. PODVector<UIElement*> DropDownList::GetItems() const
  176. {
  177. return listView_->GetItems();
  178. }
  179. unsigned DropDownList::GetSelection() const
  180. {
  181. return listView_->GetSelection();
  182. }
  183. UIElement* DropDownList::GetSelectedItem() const
  184. {
  185. return listView_->GetSelectedItem();
  186. }
  187. const String& DropDownList::GetPlaceholderText() const
  188. {
  189. return static_cast<Text*>(placeholder_->GetChild(0))->GetText();
  190. }
  191. void DropDownList::SetSelectionAttr(unsigned index)
  192. {
  193. selectionAttr_ = index;
  194. // We may not have the list items yet. Apply the index again in ApplyAttributes().
  195. SetSelection(index);
  196. }
  197. bool DropDownList::FilterImplicitAttributes(XMLElement& dest) const
  198. {
  199. if (!Menu::FilterImplicitAttributes(dest))
  200. return false;
  201. if (!RemoveChildXML(dest, "Popup Offset"))
  202. return false;
  203. XMLElement childElem = dest.GetChild("element");
  204. if (!childElem)
  205. return false;
  206. if (!RemoveChildXML(childElem, "Name", "DDL_Placeholder"))
  207. return false;
  208. if (!RemoveChildXML(childElem, "Size"))
  209. return false;
  210. childElem = childElem.GetChild("element");
  211. if (!childElem)
  212. return false;
  213. if (!RemoveChildXML(childElem, "Name", "DDL_Placeholder_Text"))
  214. return false;
  215. if (!RemoveChildXML(childElem, "Is Visible"))
  216. return false;
  217. return true;
  218. }
  219. bool DropDownList::FilterPopupImplicitAttributes(XMLElement& dest) const
  220. {
  221. if (!Menu::FilterPopupImplicitAttributes(dest))
  222. return false;
  223. // Window popup
  224. if (dest.GetAttribute("style").Empty() && !dest.SetAttribute("style", "none"))
  225. return false;
  226. if (!RemoveChildXML(dest, "Layout Mode", "Vertical"))
  227. return false;
  228. if (!RemoveChildXML(dest, "Size"))
  229. return false;
  230. // ListView
  231. XMLElement childElem = dest.GetChild("element");
  232. if (!childElem)
  233. return false;
  234. if (!listView_->FilterAttributes(childElem))
  235. return false;
  236. if (childElem.GetAttribute("style").Empty() && !childElem.SetAttribute("style", "none"))
  237. return false;
  238. if (!RemoveChildXML(childElem, "Focus Mode", "NotFocusable"))
  239. return false;
  240. if (!RemoveChildXML(childElem, "Auto Show/Hide Scrollbars", "false"))
  241. return false;
  242. // Horizontal scroll bar
  243. childElem = childElem.GetChild("element");
  244. if (childElem && !childElem.GetParent().RemoveChild(childElem))
  245. return false;
  246. // Vertical scroll bar
  247. childElem = childElem.GetNext("element");
  248. if (childElem && !childElem.GetParent().RemoveChild(childElem))
  249. return false;
  250. // Scroll panel
  251. childElem = childElem.GetNext("element");
  252. if (!childElem)
  253. return false;
  254. if (childElem.GetAttribute("style").Empty() && !childElem.SetAttribute("style", "none"))
  255. return false;
  256. // Item container
  257. childElem = childElem.GetChild("element");
  258. if (!childElem)
  259. return false;
  260. if (childElem.GetAttribute("style").Empty() && !childElem.SetAttribute("style", "none"))
  261. return false;
  262. return true;
  263. }
  264. void DropDownList::HandleItemClicked(StringHash eventType, VariantMap& eventData)
  265. {
  266. // Resize the selection placeholder to match the selected item
  267. UIElement* selectedItem = GetSelectedItem();
  268. if (selectedItem)
  269. placeholder_->SetSize(selectedItem->GetSize());
  270. // Close and defocus the popup. This will actually send the selection forward
  271. if (listView_->HasFocus())
  272. GetSubsystem<UI>()->SetFocusElement(focusMode_ < FM_FOCUSABLE ? 0 : this);
  273. ShowPopup(false);
  274. }
  275. void DropDownList::HandleListViewKey(StringHash eventType, VariantMap& eventData)
  276. {
  277. using namespace UnhandledKey;
  278. // If enter pressed in the list view, close and propagate selection
  279. int key = eventData[P_KEY].GetInt();
  280. if (key == KEY_RETURN || key == KEY_RETURN2 || key == KEY_KP_ENTER)
  281. HandleItemClicked(eventType, eventData);
  282. }
  283. }