DropDownList.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. //
  2. // Copyright (c) 2008-2013 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 "Context.h"
  24. #include "DropDownList.h"
  25. #include "ListView.h"
  26. #include "Text.h"
  27. #include "UIEvents.h"
  28. #include "Window.h"
  29. #include "DebugNew.h"
  30. namespace Urho3D
  31. {
  32. extern const char* UI_CATEGORY;
  33. DropDownList::DropDownList(Context* context) :
  34. Menu(context),
  35. resizePopup_(false),
  36. selectionAttr_(0)
  37. {
  38. Window* window = new Window(context_);
  39. window->SetInternal(true);
  40. SetPopup(window);
  41. listView_ = new ListView(context_);
  42. listView_->SetInternal(true);
  43. listView_->SetScrollBarsVisible(false, false);
  44. listView_->SetFocusMode(FM_NOTFOCUSABLE);
  45. popup_->SetLayout(LM_VERTICAL);
  46. popup_->AddChild(listView_);
  47. placeholder_ = CreateChild<UIElement>("DDL_Placeholder");
  48. placeholder_->SetInternal(true);
  49. Text* text = placeholder_->CreateChild<Text>("DDL_Placeholder_Text");
  50. text->SetInternal(true);
  51. text->SetVisible(false);
  52. SubscribeToEvent(listView_, E_ITEMSELECTED, HANDLER(DropDownList, HandleItemSelected));
  53. }
  54. DropDownList::~DropDownList()
  55. {
  56. }
  57. void DropDownList::RegisterObject(Context* context)
  58. {
  59. context->RegisterFactory<DropDownList>(UI_CATEGORY);
  60. COPY_BASE_ATTRIBUTES(DropDownList, Menu);
  61. ACCESSOR_ATTRIBUTE(DropDownList, VAR_INT, "Selection", GetSelection, SetSelectionAttr, unsigned, 0, AM_FILE);
  62. ACCESSOR_ATTRIBUTE(DropDownList, VAR_BOOL, "Resize Popup", GetResizePopup, SetResizePopup, bool, false, AM_FILE);
  63. }
  64. void DropDownList::ApplyAttributes()
  65. {
  66. // Reapply selection after possible items have been loaded
  67. SetSelection(selectionAttr_);
  68. }
  69. void DropDownList::GetBatches(PODVector<UIBatch>& batches, PODVector<float>& vertexData, const IntRect& currentScissor)
  70. {
  71. Menu::GetBatches(batches, vertexData, currentScissor);
  72. if (!placeholder_->IsVisible())
  73. return;
  74. UIElement* selectedItem = GetSelectedItem();
  75. if (selectedItem)
  76. {
  77. // Can not easily copy the selected item. However, it can be re-rendered on the placeholder's position
  78. const IntVector2& targetPos = placeholder_->GetScreenPosition();
  79. const IntVector2& originalPos = selectedItem->GetScreenPosition();
  80. IntVector2 offset = targetPos - originalPos;
  81. // GetBatches() usually resets the hover flag. Therefore get its value and then reset it for the real rendering
  82. // Render the selected item without its selection color, so temporarily reset the item's selected attribute
  83. bool hover = selectedItem->IsHovering();
  84. selectedItem->SetSelected(false);
  85. selectedItem->SetHovering(false);
  86. selectedItem->GetBatchesWithOffset(offset, batches, vertexData, currentScissor);
  87. selectedItem->SetSelected(true);
  88. selectedItem->SetHovering(hover);
  89. }
  90. }
  91. void DropDownList::OnShowPopup()
  92. {
  93. // Resize the popup to match the size of the list content, and optionally match the button width
  94. UIElement* content = listView_->GetContentElement();
  95. content->UpdateLayout();
  96. const IntVector2& contentSize = content->GetSize();
  97. const IntRect& border = popup_->GetLayoutBorder();
  98. popup_->SetSize(resizePopup_ ? GetWidth() : contentSize.x_ + border.left_ + border.right_, contentSize.y_ + border.top_ +
  99. border.bottom_);
  100. // Check if popup fits below the button. If not, show above instead
  101. bool showAbove = false;
  102. UIElement* root = GetRoot();
  103. if (root)
  104. {
  105. const IntVector2& screenPos = GetScreenPosition();
  106. if (screenPos.y_ + GetHeight() + popup_->GetHeight() > root->GetHeight() && screenPos.y_ - popup_->GetHeight() >= 0)
  107. showAbove = true;
  108. }
  109. SetPopupOffset(0, showAbove ? -popup_->GetHeight() : GetHeight());
  110. }
  111. void DropDownList::OnSetEditable()
  112. {
  113. listView_->SetEditable(editable_);
  114. }
  115. void DropDownList::AddItem(UIElement* item)
  116. {
  117. InsertItem(M_MAX_UNSIGNED, item);
  118. }
  119. void DropDownList::InsertItem(unsigned index, UIElement* item)
  120. {
  121. listView_->InsertItem(index, item);
  122. // If there was no selection, set to the first
  123. if (listView_->GetSelection() == M_MAX_UNSIGNED)
  124. listView_->SetSelection(0);
  125. }
  126. void DropDownList::RemoveItem(UIElement* item)
  127. {
  128. listView_->RemoveItem(item);
  129. }
  130. void DropDownList::RemoveItem(unsigned index)
  131. {
  132. listView_->RemoveItem(index);
  133. }
  134. void DropDownList::RemoveAllItems()
  135. {
  136. listView_->RemoveAllItems();
  137. }
  138. void DropDownList::SetSelection(unsigned index)
  139. {
  140. listView_->SetSelection(index);
  141. // 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
  142. placeholder_->GetChild(0)->SetVisible(index == M_MAX_UNSIGNED);
  143. }
  144. void DropDownList::SetPlaceholderText(const String& text)
  145. {
  146. static_cast<Text*>(placeholder_->GetChild(0))->SetText(text);
  147. }
  148. void DropDownList::SetResizePopup(bool enable)
  149. {
  150. resizePopup_ = enable;
  151. }
  152. unsigned DropDownList::GetNumItems() const
  153. {
  154. return listView_->GetNumItems();
  155. }
  156. UIElement* DropDownList::GetItem(unsigned index) const
  157. {
  158. return listView_->GetItem(index);
  159. }
  160. PODVector<UIElement*> DropDownList::GetItems() const
  161. {
  162. return listView_->GetItems();
  163. }
  164. unsigned DropDownList::GetSelection() const
  165. {
  166. return listView_->GetSelection();
  167. }
  168. UIElement* DropDownList::GetSelectedItem() const
  169. {
  170. return listView_->GetSelectedItem();
  171. }
  172. const String& DropDownList::GetPlaceholderText() const
  173. {
  174. return static_cast<Text*>(placeholder_->GetChild(0))->GetText();
  175. }
  176. void DropDownList::SetSelectionAttr(unsigned index)
  177. {
  178. selectionAttr_ = index;
  179. // We may not have the list items yet. Apply the index again in ApplyAttributes().
  180. SetSelection(index);
  181. }
  182. bool DropDownList::FilterImplicitAttributes(XMLElement& dest) const
  183. {
  184. if (!Menu::FilterImplicitAttributes(dest))
  185. return false;
  186. if (!RemoveChildXML(dest, "Popup Offset"))
  187. return false;
  188. XMLElement childElem = dest.GetChild("element");
  189. if (!childElem)
  190. return false;
  191. if (!RemoveChildXML(childElem, "Name", "DDL_Placeholder"))
  192. return false;
  193. if (!RemoveChildXML(childElem, "Size"))
  194. return false;
  195. childElem = childElem.GetChild("element");
  196. if (!childElem)
  197. return false;
  198. if (!RemoveChildXML(childElem, "Name", "DDL_Placeholder_Text"))
  199. return false;
  200. if (!RemoveChildXML(childElem, "Is Visible"))
  201. return false;
  202. return true;
  203. }
  204. bool DropDownList::FilterPopupImplicitAttributes(XMLElement& dest) const
  205. {
  206. if (!Menu::FilterPopupImplicitAttributes(dest))
  207. return false;
  208. // Window popup
  209. if (dest.GetAttribute("style").Empty() && !dest.SetAttribute("style", "none"))
  210. return false;
  211. if (!RemoveChildXML(dest, "Layout Mode", "Vertical"))
  212. return false;
  213. if (!RemoveChildXML(dest, "Size"))
  214. return false;
  215. // ListView
  216. XMLElement childElem = dest.GetChild("element");
  217. if (!childElem)
  218. return false;
  219. if (!listView_->FilterAttributes(childElem))
  220. return false;
  221. if (childElem.GetAttribute("style").Empty() && !childElem.SetAttribute("style", "none"))
  222. return false;
  223. if (!RemoveChildXML(childElem, "Focus Mode", "NotFocusable"))
  224. return false;
  225. if (!RemoveChildXML(childElem, "Auto Show/Hide Scrollbars", "false"))
  226. return false;
  227. // Horizontal scroll bar
  228. childElem = childElem.GetChild("element");
  229. if (childElem && !childElem.GetParent().RemoveChild(childElem))
  230. return false;
  231. // Vertical scroll bar
  232. childElem = childElem.GetNext("element");
  233. if (childElem && !childElem.GetParent().RemoveChild(childElem))
  234. return false;
  235. // Scroll panel
  236. childElem = childElem.GetNext("element");
  237. if (!childElem)
  238. return false;
  239. if (childElem.GetAttribute("style").Empty() && !childElem.SetAttribute("style", "none"))
  240. return false;
  241. // Item container
  242. childElem = childElem.GetChild("element");
  243. if (!childElem)
  244. return false;
  245. if (childElem.GetAttribute("style").Empty() && !childElem.SetAttribute("style", "none"))
  246. return false;
  247. return true;
  248. }
  249. void DropDownList::HandleItemSelected(StringHash eventType, VariantMap& eventData)
  250. {
  251. // Resize the selection placeholder to match the selected item
  252. UIElement* selectedItem = GetSelectedItem();
  253. if (selectedItem)
  254. placeholder_->SetSize(selectedItem->GetSize());
  255. // Close the popup as the selection was made
  256. if (GetShowPopup())
  257. ShowPopup(false);
  258. // Send the event forward
  259. using namespace ItemSelected;
  260. VariantMap newEventData;
  261. newEventData[P_ELEMENT] = (void*)this;
  262. newEventData[P_SELECTION] = GetSelection();
  263. SendEvent(E_ITEMSELECTED, newEventData);
  264. }
  265. }