DropDownList.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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 "../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. }
  58. DropDownList::~DropDownList()
  59. {
  60. }
  61. void DropDownList::RegisterObject(Context* context)
  62. {
  63. context->RegisterFactory<DropDownList>(UI_CATEGORY);
  64. COPY_BASE_ATTRIBUTES(Menu);
  65. UPDATE_ATTRIBUTE_DEFAULT_VALUE("Focus Mode", FM_FOCUSABLE_DEFOCUSABLE);
  66. ACCESSOR_ATTRIBUTE("Selection", GetSelection, SetSelectionAttr, unsigned, 0, AM_FILE);
  67. ACCESSOR_ATTRIBUTE("Resize Popup", GetResizePopup, SetResizePopup, bool, false, AM_FILE);
  68. }
  69. void DropDownList::ApplyAttributes()
  70. {
  71. // Reapply selection after possible items have been loaded
  72. SetSelection(selectionAttr_);
  73. }
  74. void DropDownList::GetBatches(PODVector<UIBatch>& batches, PODVector<float>& vertexData, const IntRect& currentScissor)
  75. {
  76. Menu::GetBatches(batches, vertexData, currentScissor);
  77. if (!placeholder_->IsVisible())
  78. return;
  79. UIElement* selectedItem = GetSelectedItem();
  80. if (selectedItem)
  81. {
  82. // Can not easily copy the selected item. However, it can be re-rendered on the placeholder's position
  83. const IntVector2& targetPos = placeholder_->GetScreenPosition();
  84. const IntVector2& originalPos = selectedItem->GetScreenPosition();
  85. IntVector2 offset = targetPos - originalPos;
  86. // GetBatches() usually resets the hover flag. Therefore get its value and then reset it for the real rendering
  87. // Render the selected item without its selection color, so temporarily reset the item's selected attribute
  88. bool hover = selectedItem->IsHovering();
  89. selectedItem->SetSelected(false);
  90. selectedItem->SetHovering(false);
  91. selectedItem->GetBatchesWithOffset(offset, batches, vertexData, currentScissor);
  92. selectedItem->SetSelected(true);
  93. selectedItem->SetHovering(hover);
  94. }
  95. }
  96. void DropDownList::OnShowPopup()
  97. {
  98. // Resize the popup to match the size of the list content, and optionally match the button width
  99. UIElement* content = listView_->GetContentElement();
  100. content->UpdateLayout();
  101. const IntVector2& contentSize = content->GetSize();
  102. const IntRect& border = popup_->GetLayoutBorder();
  103. popup_->SetSize(resizePopup_ ? GetWidth() : contentSize.x_ + border.left_ + border.right_,
  104. contentSize.y_ + border.top_ + border.bottom_);
  105. // Check if popup fits below the button. If not, show above instead
  106. bool showAbove = false;
  107. UIElement* root = GetRoot();
  108. if (root)
  109. {
  110. const IntVector2& screenPos = GetScreenPosition();
  111. if (screenPos.y_ + GetHeight() + popup_->GetHeight() > root->GetHeight() && screenPos.y_ - popup_->GetHeight() >= 0)
  112. showAbove = true;
  113. }
  114. SetPopupOffset(0, showAbove ? -popup_->GetHeight() : GetHeight());
  115. // Focus the ListView to allow making the selection with keys
  116. GetSubsystem<UI>()->SetFocusElement(listView_);
  117. }
  118. void DropDownList::OnHidePopup()
  119. {
  120. // When the popup is hidden, propagate the selection
  121. using namespace ItemSelected;
  122. VariantMap& eventData = GetEventDataMap();
  123. eventData[P_ELEMENT] = this;
  124. eventData[P_SELECTION] = GetSelection();
  125. SendEvent(E_ITEMSELECTED, eventData);
  126. }
  127. void DropDownList::OnSetEditable()
  128. {
  129. listView_->SetEditable(editable_);
  130. }
  131. void DropDownList::AddItem(UIElement* item)
  132. {
  133. InsertItem(M_MAX_UNSIGNED, item);
  134. }
  135. void DropDownList::InsertItem(unsigned index, UIElement* item)
  136. {
  137. listView_->InsertItem(index, item);
  138. // If there was no selection, set to the first
  139. if (listView_->GetSelection() == M_MAX_UNSIGNED)
  140. listView_->SetSelection(0);
  141. }
  142. void DropDownList::RemoveItem(UIElement* item)
  143. {
  144. listView_->RemoveItem(item);
  145. }
  146. void DropDownList::RemoveItem(unsigned index)
  147. {
  148. listView_->RemoveItem(index);
  149. }
  150. void DropDownList::RemoveAllItems()
  151. {
  152. listView_->RemoveAllItems();
  153. }
  154. void DropDownList::SetSelection(unsigned index)
  155. {
  156. listView_->SetSelection(index);
  157. // 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
  158. placeholder_->GetChild(0)->SetVisible(index == M_MAX_UNSIGNED);
  159. }
  160. void DropDownList::SetPlaceholderText(const String& text)
  161. {
  162. static_cast<Text*>(placeholder_->GetChild(0))->SetText(text);
  163. }
  164. void DropDownList::SetResizePopup(bool enable)
  165. {
  166. resizePopup_ = enable;
  167. }
  168. unsigned DropDownList::GetNumItems() const
  169. {
  170. return listView_->GetNumItems();
  171. }
  172. UIElement* DropDownList::GetItem(unsigned index) const
  173. {
  174. return listView_->GetItem(index);
  175. }
  176. PODVector<UIElement*> DropDownList::GetItems() const
  177. {
  178. return listView_->GetItems();
  179. }
  180. unsigned DropDownList::GetSelection() const
  181. {
  182. return listView_->GetSelection();
  183. }
  184. UIElement* DropDownList::GetSelectedItem() const
  185. {
  186. return listView_->GetSelectedItem();
  187. }
  188. const String& DropDownList::GetPlaceholderText() const
  189. {
  190. return static_cast<Text*>(placeholder_->GetChild(0))->GetText();
  191. }
  192. void DropDownList::SetSelectionAttr(unsigned index)
  193. {
  194. selectionAttr_ = index;
  195. // We may not have the list items yet. Apply the index again in ApplyAttributes().
  196. SetSelection(index);
  197. }
  198. bool DropDownList::FilterImplicitAttributes(XMLElement& dest) const
  199. {
  200. if (!Menu::FilterImplicitAttributes(dest))
  201. return false;
  202. if (!RemoveChildXML(dest, "Popup Offset"))
  203. return false;
  204. XMLElement childElem = dest.GetChild("element");
  205. if (!childElem)
  206. return false;
  207. if (!RemoveChildXML(childElem, "Name", "DDL_Placeholder"))
  208. return false;
  209. if (!RemoveChildXML(childElem, "Size"))
  210. return false;
  211. childElem = childElem.GetChild("element");
  212. if (!childElem)
  213. return false;
  214. if (!RemoveChildXML(childElem, "Name", "DDL_Placeholder_Text"))
  215. return false;
  216. if (!RemoveChildXML(childElem, "Is Visible"))
  217. return false;
  218. return true;
  219. }
  220. bool DropDownList::FilterPopupImplicitAttributes(XMLElement& dest) const
  221. {
  222. if (!Menu::FilterPopupImplicitAttributes(dest))
  223. return false;
  224. // Window popup
  225. if (dest.GetAttribute("style").Empty() && !dest.SetAttribute("style", "none"))
  226. return false;
  227. if (!RemoveChildXML(dest, "Layout Mode", "Vertical"))
  228. return false;
  229. if (!RemoveChildXML(dest, "Size"))
  230. return false;
  231. // ListView
  232. XMLElement childElem = dest.GetChild("element");
  233. if (!childElem)
  234. return false;
  235. if (!listView_->FilterAttributes(childElem))
  236. return false;
  237. if (childElem.GetAttribute("style").Empty() && !childElem.SetAttribute("style", "none"))
  238. return false;
  239. if (!RemoveChildXML(childElem, "Focus Mode", "NotFocusable"))
  240. return false;
  241. if (!RemoveChildXML(childElem, "Auto Show/Hide Scrollbars", "false"))
  242. return false;
  243. // Horizontal scroll bar
  244. childElem = childElem.GetChild("element");
  245. if (childElem && !childElem.GetParent().RemoveChild(childElem))
  246. return false;
  247. // Vertical scroll bar
  248. childElem = childElem.GetNext("element");
  249. if (childElem && !childElem.GetParent().RemoveChild(childElem))
  250. return false;
  251. // Scroll panel
  252. childElem = childElem.GetNext("element");
  253. if (!childElem)
  254. return false;
  255. if (childElem.GetAttribute("style").Empty() && !childElem.SetAttribute("style", "none"))
  256. return false;
  257. // Item container
  258. childElem = childElem.GetChild("element");
  259. if (!childElem)
  260. return false;
  261. if (childElem.GetAttribute("style").Empty() && !childElem.SetAttribute("style", "none"))
  262. return false;
  263. return true;
  264. }
  265. void DropDownList::HandleItemClicked(StringHash eventType, VariantMap& eventData)
  266. {
  267. // Resize the selection placeholder to match the selected item
  268. UIElement* selectedItem = GetSelectedItem();
  269. if (selectedItem)
  270. placeholder_->SetSize(selectedItem->GetSize());
  271. // Close and defocus the popup. This will actually send the selection forward
  272. if (listView_->HasFocus())
  273. GetSubsystem<UI>()->SetFocusElement(focusMode_ < FM_FOCUSABLE ? 0 : this);
  274. ShowPopup(false);
  275. }
  276. void DropDownList::HandleListViewKey(StringHash eventType, VariantMap& eventData)
  277. {
  278. using namespace UnhandledKey;
  279. // If enter pressed in the list view, close and propagate selection
  280. int key = eventData[P_KEY].GetInt();
  281. if (key == KEY_RETURN || key == KEY_RETURN2 || key == KEY_KP_ENTER)
  282. HandleItemClicked(eventType, eventData);
  283. }
  284. }