DropDownList.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. //
  2. // Copyright (c) 2008-2017 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 "../Precompiled.h"
  23. #include "../Core/Context.h"
  24. #include "../Input/InputEvents.h"
  25. #include "../IO/Log.h"
  26. #include "../UI/DropDownList.h"
  27. #include "../UI/ListView.h"
  28. #include "../UI/Text.h"
  29. #include "../UI/UI.h"
  30. #include "../UI/UIEvents.h"
  31. #include "../UI/Window.h"
  32. #include "../DebugNew.h"
  33. namespace Urho3D
  34. {
  35. extern const char* UI_CATEGORY;
  36. DropDownList::DropDownList(Context* context) :
  37. Menu(context),
  38. resizePopup_(false),
  39. selectionAttr_(0)
  40. {
  41. focusMode_ = FM_FOCUSABLE_DEFOCUSABLE;
  42. Window* window = new Window(context_);
  43. window->SetInternal(true);
  44. SetPopup(window);
  45. listView_ = new ListView(context_);
  46. listView_->SetInternal(true);
  47. listView_->SetScrollBarsVisible(false, false);
  48. popup_->SetLayout(LM_VERTICAL);
  49. popup_->AddChild(listView_);
  50. placeholder_ = CreateChild<UIElement>("DDL_Placeholder");
  51. placeholder_->SetInternal(true);
  52. Text* text = placeholder_->CreateChild<Text>("DDL_Placeholder_Text");
  53. text->SetInternal(true);
  54. text->SetVisible(false);
  55. SubscribeToEvent(listView_, E_ITEMCLICKED, URHO3D_HANDLER(DropDownList, HandleItemClicked));
  56. SubscribeToEvent(listView_, E_UNHANDLEDKEY, URHO3D_HANDLER(DropDownList, HandleListViewKey));
  57. SubscribeToEvent(listView_, E_SELECTIONCHANGED, URHO3D_HANDLER(DropDownList, HandleSelectionChanged));
  58. }
  59. DropDownList::~DropDownList()
  60. {
  61. }
  62. void DropDownList::RegisterObject(Context* context)
  63. {
  64. context->RegisterFactory<DropDownList>(UI_CATEGORY);
  65. URHO3D_COPY_BASE_ATTRIBUTES(Menu);
  66. URHO3D_UPDATE_ATTRIBUTE_DEFAULT_VALUE("Focus Mode", FM_FOCUSABLE_DEFOCUSABLE);
  67. URHO3D_ACCESSOR_ATTRIBUTE("Selection", GetSelection, SetSelectionAttr, unsigned, 0, AM_FILE);
  68. URHO3D_ACCESSOR_ATTRIBUTE("Resize Popup", GetResizePopup, SetResizePopup, bool, false, AM_FILE);
  69. }
  70. void DropDownList::ApplyAttributes()
  71. {
  72. // Reapply selection after possible items have been loaded
  73. SetSelection(selectionAttr_);
  74. }
  75. void DropDownList::GetBatches(PODVector<UIBatch>& batches, PODVector<float>& vertexData, const IntRect& currentScissor)
  76. {
  77. Menu::GetBatches(batches, vertexData, currentScissor);
  78. if (!placeholder_->IsVisible())
  79. return;
  80. UIElement* selectedItem = GetSelectedItem();
  81. if (selectedItem)
  82. {
  83. // Can not easily copy the selected item. However, it can be re-rendered on the placeholder's position
  84. const IntVector2& targetPos = placeholder_->GetScreenPosition();
  85. const IntVector2& originalPos = selectedItem->GetScreenPosition();
  86. IntVector2 offset = targetPos - originalPos;
  87. // GetBatches() usually resets the hover flag. Therefore get its value and then reset it for the real rendering
  88. // Render the selected item without its selection color, so temporarily reset the item's selected attribute
  89. bool hover = selectedItem->IsHovering();
  90. selectedItem->SetSelected(false);
  91. selectedItem->SetHovering(false);
  92. selectedItem->GetBatchesWithOffset(offset, batches, vertexData, currentScissor);
  93. selectedItem->SetSelected(true);
  94. selectedItem->SetHovering(hover);
  95. }
  96. }
  97. void DropDownList::OnShowPopup()
  98. {
  99. // Resize the popup to match the size of the list content, and optionally match the button width
  100. UIElement* content = listView_->GetContentElement();
  101. content->UpdateLayout();
  102. const IntVector2& contentSize = content->GetSize();
  103. const IntRect& border = popup_->GetLayoutBorder();
  104. popup_->SetSize(resizePopup_ ? GetWidth() : contentSize.x_ + border.left_ + border.right_,
  105. contentSize.y_ + border.top_ + border.bottom_);
  106. // Check if popup fits below the button. If not, show above instead
  107. bool showAbove = false;
  108. UIElement* root = GetRoot();
  109. if (root)
  110. {
  111. const IntVector2& screenPos = GetScreenPosition();
  112. if (screenPos.y_ + GetHeight() + popup_->GetHeight() > root->GetHeight() && screenPos.y_ - popup_->GetHeight() >= 0)
  113. showAbove = true;
  114. }
  115. SetPopupOffset(0, showAbove ? -popup_->GetHeight() : GetHeight());
  116. // Focus the ListView to allow making the selection with keys
  117. GetSubsystem<UI>()->SetFocusElement(listView_);
  118. }
  119. void DropDownList::OnHidePopup()
  120. {
  121. // When the popup is hidden, propagate the selection
  122. using namespace ItemSelected;
  123. VariantMap& eventData = GetEventDataMap();
  124. eventData[P_ELEMENT] = this;
  125. eventData[P_SELECTION] = GetSelection();
  126. SendEvent(E_ITEMSELECTED, eventData);
  127. }
  128. void DropDownList::OnSetEditable()
  129. {
  130. listView_->SetEditable(editable_);
  131. }
  132. void DropDownList::AddItem(UIElement* item)
  133. {
  134. InsertItem(M_MAX_UNSIGNED, item);
  135. }
  136. void DropDownList::InsertItem(unsigned index, UIElement* item)
  137. {
  138. listView_->InsertItem(index, item);
  139. // If there was no selection, set to the first
  140. if (GetSelection() == M_MAX_UNSIGNED)
  141. SetSelection(0);
  142. }
  143. void DropDownList::RemoveItem(UIElement* item)
  144. {
  145. listView_->RemoveItem(item);
  146. }
  147. void DropDownList::RemoveItem(unsigned index)
  148. {
  149. listView_->RemoveItem(index);
  150. }
  151. void DropDownList::RemoveAllItems()
  152. {
  153. listView_->RemoveAllItems();
  154. }
  155. void DropDownList::SetSelection(unsigned index)
  156. {
  157. listView_->SetSelection(index);
  158. }
  159. void DropDownList::SetPlaceholderText(const String& text)
  160. {
  161. placeholder_->GetChildStaticCast<Text>(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 placeholder_->GetChildStaticCast<Text>(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. XMLElement hScrollElem = childElem.GetChild("element");
  244. // Vertical scroll bar
  245. XMLElement vScrollElem = hScrollElem.GetNext("element");
  246. // Scroll panel
  247. XMLElement panelElem = vScrollElem.GetNext("element");
  248. if (hScrollElem && !hScrollElem.GetParent().RemoveChild(hScrollElem))
  249. return false;
  250. if (vScrollElem && !vScrollElem.GetParent().RemoveChild(vScrollElem))
  251. return false;
  252. if (panelElem)
  253. {
  254. if (panelElem.GetAttribute("style").Empty() && !panelElem.SetAttribute("style", "none"))
  255. return false;
  256. // Item container
  257. XMLElement containerElem = panelElem.GetChild("element");
  258. if (containerElem)
  259. {
  260. if (containerElem.GetAttribute("style").Empty() && !containerElem.SetAttribute("style", "none"))
  261. return false;
  262. }
  263. }
  264. return true;
  265. }
  266. void DropDownList::HandleItemClicked(StringHash eventType, VariantMap& eventData)
  267. {
  268. // Resize the selection placeholder to match the selected item
  269. UIElement* selectedItem = GetSelectedItem();
  270. if (selectedItem)
  271. placeholder_->SetSize(selectedItem->GetSize());
  272. // Close and defocus the popup. This will actually send the selection forward
  273. if (listView_->HasFocus())
  274. GetSubsystem<UI>()->SetFocusElement(focusMode_ < FM_FOCUSABLE ? 0 : this);
  275. ShowPopup(false);
  276. }
  277. void DropDownList::HandleListViewKey(StringHash eventType, VariantMap& eventData)
  278. {
  279. using namespace UnhandledKey;
  280. // If enter pressed in the list view, close and propagate selection
  281. int key = eventData[P_KEY].GetInt();
  282. if (key == KEY_RETURN || key == KEY_RETURN2 || key == KEY_KP_ENTER)
  283. HandleItemClicked(eventType, eventData);
  284. }
  285. void DropDownList::HandleSelectionChanged(StringHash eventType, VariantMap& eventData)
  286. {
  287. // 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
  288. placeholder_->GetChild(0)->SetVisible(GetSelection() == M_MAX_UNSIGNED);
  289. }
  290. }