DropDownList.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "Context.h"
  25. #include "DropDownList.h"
  26. #include "ListView.h"
  27. #include "UIEvents.h"
  28. #include "Window.h"
  29. #include "DebugNew.h"
  30. namespace Urho3D
  31. {
  32. OBJECTTYPESTATIC(DropDownList);
  33. DropDownList::DropDownList(Context* context) :
  34. Menu(context),
  35. resizePopup_(false)
  36. {
  37. Window* window = new Window(context_);
  38. SetPopup(window);
  39. listView_ = new ListView(context_);
  40. listView_->SetScrollBarsVisible(false, false);
  41. listView_->SetFocusMode(FM_NOTFOCUSABLE);
  42. popup_->SetLayout(LM_VERTICAL);
  43. popup_->AddChild(listView_);
  44. placeholder_ = new UIElement(context_);
  45. AddChild(placeholder_);
  46. SubscribeToEvent(listView_, E_ITEMSELECTED, HANDLER(DropDownList, HandleItemSelected));
  47. }
  48. DropDownList::~DropDownList()
  49. {
  50. }
  51. void DropDownList::RegisterObject(Context* context)
  52. {
  53. context->RegisterFactory<DropDownList>();
  54. }
  55. void DropDownList::SetStyle(const XMLElement& element)
  56. {
  57. Menu::SetStyle(element);
  58. XMLElement listElem = element.GetChild("listview");
  59. if (listElem)
  60. listView_->SetStyle(listElem);
  61. XMLElement popupElem = element.GetChild("popup");
  62. if (popupElem)
  63. popup_->SetStyle(popupElem);
  64. XMLElement placeholderElem = element.GetChild("placeholder");
  65. if (placeholderElem)
  66. placeholder_->SetStyle(placeholderElem);
  67. UIElement* root = GetRoot();
  68. XMLElement itemElem = element.GetChild("popupitem");
  69. if (root)
  70. {
  71. while (itemElem)
  72. {
  73. if (itemElem.HasAttribute("name"))
  74. AddItem(root->GetChild(itemElem.GetAttribute("name"), true));
  75. itemElem = itemElem.GetNext("popupitem");
  76. }
  77. }
  78. if (element.HasChild("selection"))
  79. SetSelection(element.GetChild("selection").GetInt("value"));
  80. if (element.HasChild("resizepopup"))
  81. SetResizePopup(element.GetChild("resizepopup").GetBool("enable"));
  82. }
  83. void DropDownList::GetBatches(PODVector<UIBatch>& batches, PODVector<UIQuad>& quads, const IntRect& currentScissor)
  84. {
  85. Button::GetBatches(batches, quads, currentScissor);
  86. if (!placeholder_->IsVisible())
  87. return;
  88. UIElement* selectedItem = GetSelectedItem();
  89. if (selectedItem)
  90. {
  91. // Can not easily copy the selected item. However, it can be re-rendered on the placeholder's position
  92. const IntVector2& targetPos = placeholder_->GetScreenPosition();
  93. const IntVector2& originalPos = selectedItem->GetScreenPosition();
  94. IntVector2 offset = targetPos - originalPos;
  95. // GetBatches() usually resets the hover flag. Therefore get its value and then reset it for the real rendering
  96. bool hover = selectedItem->IsHovering();
  97. selectedItem->SetHovering(false);
  98. selectedItem->GetBatchesWithOffset(offset, batches, quads, currentScissor);
  99. selectedItem->SetHovering(hover);
  100. }
  101. }
  102. void DropDownList::OnShowPopup()
  103. {
  104. // Resize the popup to match the size of the list content, and optionally match the button width
  105. UIElement* content = listView_->GetContentElement();
  106. content->UpdateLayout();
  107. const IntVector2& contentSize = content->GetSize();
  108. const IntRect& border = popup_->GetLayoutBorder();
  109. popup_->SetSize(resizePopup_ ? GetWidth() : contentSize.x_ + border.left_ + border.right_, contentSize.y_ + border.top_ +
  110. border.bottom_);
  111. // Check if popup fits below the button. If not, show above instead
  112. bool showAbove = false;
  113. UIElement* root = GetRoot();
  114. if (root)
  115. {
  116. const IntVector2& screenPos = GetScreenPosition();
  117. if (screenPos.y_ + GetHeight() + popup_->GetHeight() > root->GetHeight() && screenPos.y_ - popup_->GetHeight() >= 0)
  118. showAbove = true;
  119. }
  120. if (!showAbove)
  121. SetPopupOffset(0, GetHeight());
  122. else
  123. SetPopupOffset(0, -popup_->GetHeight());
  124. }
  125. void DropDownList::AddItem(UIElement* item)
  126. {
  127. InsertItem(listView_->GetNumItems(), item);
  128. }
  129. void DropDownList::InsertItem(unsigned index, UIElement* item)
  130. {
  131. listView_->InsertItem(index, item);
  132. // If there was no selection, set to the first
  133. if (listView_->GetSelection() == M_MAX_UNSIGNED)
  134. listView_->SetSelection(0);
  135. }
  136. void DropDownList::RemoveItem(UIElement* item)
  137. {
  138. listView_->RemoveItem(item);
  139. }
  140. void DropDownList::RemoveItem(unsigned index)
  141. {
  142. listView_->RemoveItem(index);
  143. }
  144. void DropDownList::RemoveAllItems()
  145. {
  146. listView_->RemoveAllItems();
  147. }
  148. void DropDownList::SetSelection(unsigned index)
  149. {
  150. listView_->SetSelection(index);
  151. }
  152. void DropDownList::SetResizePopup(bool enable)
  153. {
  154. resizePopup_ = enable;
  155. }
  156. unsigned DropDownList::GetNumItems() const
  157. {
  158. return listView_->GetNumItems();
  159. }
  160. UIElement* DropDownList::GetItem(unsigned index) const
  161. {
  162. return listView_->GetItem(index);
  163. }
  164. PODVector<UIElement*> DropDownList::GetItems() const
  165. {
  166. PODVector<UIElement*> items;
  167. listView_->GetContentElement()->GetChildren(items);
  168. return items;
  169. }
  170. unsigned DropDownList::GetSelection() const
  171. {
  172. return listView_->GetSelection();
  173. }
  174. UIElement* DropDownList::GetSelectedItem() const
  175. {
  176. return listView_->GetSelectedItem();
  177. }
  178. void DropDownList::HandleItemSelected(StringHash eventType, VariantMap& eventData)
  179. {
  180. // Resize the selection placeholder to match the selected item
  181. UIElement* selectedItem = GetSelectedItem();
  182. if (selectedItem)
  183. placeholder_->SetSize(selectedItem->GetSize());
  184. // Close the popup as the selection was made
  185. if (GetShowPopup())
  186. ShowPopup(false);
  187. // Send the event forward
  188. using namespace ItemSelected;
  189. VariantMap newEventData;
  190. newEventData[P_ELEMENT] = (void*)this;
  191. newEventData[P_SELECTION] = GetSelection();
  192. SendEvent(E_ITEMSELECTED, newEventData);
  193. }
  194. }