DropDownList.cpp 9.9 KB

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