DropDownList.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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 "SystemUI.h"
  29. #include "SystemUIEvents.h"
  30. #include "Window.h"
  31. #include "../../DebugNew.h"
  32. namespace Atomic
  33. {
  34. namespace SystemUI
  35. {
  36. extern const char* UI_CATEGORY;
  37. DropDownList::DropDownList(Context* context) :
  38. Menu(context),
  39. resizePopup_(false),
  40. selectionAttr_(0)
  41. {
  42. focusMode_ = FM_FOCUSABLE_DEFOCUSABLE;
  43. Window* window = new Window(context_);
  44. window->SetInternal(true);
  45. SetPopup(window);
  46. listView_ = new ListView(context_);
  47. listView_->SetInternal(true);
  48. listView_->SetScrollBarsVisible(false, false);
  49. popup_->SetLayout(LM_VERTICAL);
  50. popup_->AddChild(listView_);
  51. placeholder_ = CreateChild<UIElement>("DDL_Placeholder");
  52. placeholder_->SetInternal(true);
  53. Text* text = placeholder_->CreateChild<Text>("DDL_Placeholder_Text");
  54. text->SetInternal(true);
  55. text->SetVisible(false);
  56. SubscribeToEvent(listView_, E_ITEMCLICKED, HANDLER(DropDownList, HandleItemClicked));
  57. SubscribeToEvent(listView_, E_UNHANDLEDKEY, HANDLER(DropDownList, HandleListViewKey));
  58. }
  59. DropDownList::~DropDownList()
  60. {
  61. }
  62. void DropDownList::RegisterObject(Context* context)
  63. {
  64. context->RegisterFactory<DropDownList>(UI_CATEGORY);
  65. COPY_BASE_ATTRIBUTES(Menu);
  66. UPDATE_ATTRIBUTE_DEFAULT_VALUE("Focus Mode", FM_FOCUSABLE_DEFOCUSABLE);
  67. ACCESSOR_ATTRIBUTE("Selection", GetSelection, SetSelectionAttr, unsigned, 0, AM_FILE);
  68. 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<SystemUIBatch>& 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<SystemUI>()->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 (listView_->GetSelection() == M_MAX_UNSIGNED)
  141. listView_->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. // 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
  159. placeholder_->GetChild(0)->SetVisible(index == M_MAX_UNSIGNED);
  160. }
  161. void DropDownList::SetPlaceholderText(const String& text)
  162. {
  163. static_cast<Text*>(placeholder_->GetChild(0))->SetText(text);
  164. }
  165. void DropDownList::SetResizePopup(bool enable)
  166. {
  167. resizePopup_ = enable;
  168. }
  169. unsigned DropDownList::GetNumItems() const
  170. {
  171. return listView_->GetNumItems();
  172. }
  173. UIElement* DropDownList::GetItem(unsigned index) const
  174. {
  175. return listView_->GetItem(index);
  176. }
  177. PODVector<UIElement*> DropDownList::GetItems() const
  178. {
  179. return listView_->GetItems();
  180. }
  181. unsigned DropDownList::GetSelection() const
  182. {
  183. return listView_->GetSelection();
  184. }
  185. UIElement* DropDownList::GetSelectedItem() const
  186. {
  187. return listView_->GetSelectedItem();
  188. }
  189. const String& DropDownList::GetPlaceholderText() const
  190. {
  191. return static_cast<Text*>(placeholder_->GetChild(0))->GetText();
  192. }
  193. void DropDownList::SetSelectionAttr(unsigned index)
  194. {
  195. selectionAttr_ = index;
  196. // We may not have the list items yet. Apply the index again in ApplyAttributes().
  197. SetSelection(index);
  198. }
  199. bool DropDownList::FilterImplicitAttributes(XMLElement& dest) const
  200. {
  201. if (!Menu::FilterImplicitAttributes(dest))
  202. return false;
  203. if (!RemoveChildXML(dest, "Popup Offset"))
  204. return false;
  205. XMLElement childElem = dest.GetChild("element");
  206. if (!childElem)
  207. return false;
  208. if (!RemoveChildXML(childElem, "Name", "DDL_Placeholder"))
  209. return false;
  210. if (!RemoveChildXML(childElem, "Size"))
  211. return false;
  212. childElem = childElem.GetChild("element");
  213. if (!childElem)
  214. return false;
  215. if (!RemoveChildXML(childElem, "Name", "DDL_Placeholder_Text"))
  216. return false;
  217. if (!RemoveChildXML(childElem, "Is Visible"))
  218. return false;
  219. return true;
  220. }
  221. bool DropDownList::FilterPopupImplicitAttributes(XMLElement& dest) const
  222. {
  223. if (!Menu::FilterPopupImplicitAttributes(dest))
  224. return false;
  225. // Window popup
  226. if (dest.GetAttribute("style").Empty() && !dest.SetAttribute("style", "none"))
  227. return false;
  228. if (!RemoveChildXML(dest, "Layout Mode", "Vertical"))
  229. return false;
  230. if (!RemoveChildXML(dest, "Size"))
  231. return false;
  232. // ListView
  233. XMLElement childElem = dest.GetChild("element");
  234. if (!childElem)
  235. return false;
  236. if (!listView_->FilterAttributes(childElem))
  237. return false;
  238. if (childElem.GetAttribute("style").Empty() && !childElem.SetAttribute("style", "none"))
  239. return false;
  240. if (!RemoveChildXML(childElem, "Focus Mode", "NotFocusable"))
  241. return false;
  242. if (!RemoveChildXML(childElem, "Auto Show/Hide Scrollbars", "false"))
  243. return false;
  244. // Horizontal scroll bar
  245. childElem = childElem.GetChild("element");
  246. if (childElem && !childElem.GetParent().RemoveChild(childElem))
  247. return false;
  248. // Vertical scroll bar
  249. childElem = childElem.GetNext("element");
  250. if (childElem && !childElem.GetParent().RemoveChild(childElem))
  251. return false;
  252. // Scroll panel
  253. childElem = childElem.GetNext("element");
  254. if (!childElem)
  255. return false;
  256. if (childElem.GetAttribute("style").Empty() && !childElem.SetAttribute("style", "none"))
  257. return false;
  258. // Item container
  259. childElem = childElem.GetChild("element");
  260. if (!childElem)
  261. return false;
  262. if (childElem.GetAttribute("style").Empty() && !childElem.SetAttribute("style", "none"))
  263. return false;
  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<SystemUI>()->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. }
  286. }