DropDownList.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. window->SetInternal(true);
  39. SetPopup(window);
  40. listView_ = new ListView(context_);
  41. listView_->SetInternal(true);
  42. listView_->SetScrollBarsVisible(false, false);
  43. listView_->SetFocusMode(FM_NOTFOCUSABLE);
  44. popup_->SetLayout(LM_VERTICAL);
  45. popup_->AddChild(listView_);
  46. placeholder_ = CreateChild<UIElement>();
  47. placeholder_->SetInternal(true);
  48. SubscribeToEvent(listView_, E_ITEMSELECTED, HANDLER(DropDownList, HandleItemSelected));
  49. }
  50. DropDownList::~DropDownList()
  51. {
  52. }
  53. void DropDownList::RegisterObject(Context* context)
  54. {
  55. context->RegisterFactory<DropDownList>();
  56. }
  57. void DropDownList::SetStyle(const XMLElement& element)
  58. {
  59. Menu::SetStyle(element);
  60. XMLElement listElem = element.GetChild("listview");
  61. if (listElem)
  62. listView_->SetStyle(listElem);
  63. XMLElement popupElem = element.GetChild("popup");
  64. if (popupElem)
  65. popup_->SetStyle(popupElem);
  66. XMLElement placeholderElem = element.GetChild("placeholder");
  67. if (placeholderElem)
  68. placeholder_->SetStyle(placeholderElem);
  69. UIElement* root = GetRoot();
  70. XMLElement itemElem = element.GetChild("popupitem");
  71. if (root)
  72. {
  73. while (itemElem)
  74. {
  75. if (itemElem.HasAttribute("name"))
  76. AddItem(root->GetChild(itemElem.GetAttribute("name"), true));
  77. itemElem = itemElem.GetNext("popupitem");
  78. }
  79. }
  80. if (element.HasChild("selection"))
  81. SetSelection(element.GetChild("selection").GetInt("value"));
  82. if (element.HasChild("resizepopup"))
  83. SetResizePopup(element.GetChild("resizepopup").GetBool("enable"));
  84. }
  85. void DropDownList::GetBatches(PODVector<UIBatch>& batches, PODVector<UIQuad>& quads, const IntRect& currentScissor)
  86. {
  87. Button::GetBatches(batches, quads, currentScissor);
  88. if (!placeholder_->IsVisible())
  89. return;
  90. UIElement* selectedItem = GetSelectedItem();
  91. if (selectedItem)
  92. {
  93. // Can not easily copy the selected item. However, it can be re-rendered on the placeholder's position
  94. const IntVector2& targetPos = placeholder_->GetScreenPosition();
  95. const IntVector2& originalPos = selectedItem->GetScreenPosition();
  96. IntVector2 offset = targetPos - originalPos;
  97. // GetBatches() usually resets the hover flag. Therefore get its value and then reset it for the real rendering
  98. bool hover = selectedItem->IsHovering();
  99. selectedItem->SetHovering(false);
  100. selectedItem->GetBatchesWithOffset(offset, batches, quads, currentScissor);
  101. selectedItem->SetHovering(hover);
  102. }
  103. }
  104. void DropDownList::OnShowPopup()
  105. {
  106. // Resize the popup to match the size of the list content, and optionally match the button width
  107. UIElement* content = listView_->GetContentElement();
  108. content->UpdateLayout();
  109. const IntVector2& contentSize = content->GetSize();
  110. const IntRect& border = popup_->GetLayoutBorder();
  111. popup_->SetSize(resizePopup_ ? GetWidth() : contentSize.x_ + border.left_ + border.right_, contentSize.y_ + border.top_ +
  112. border.bottom_);
  113. // Check if popup fits below the button. If not, show above instead
  114. bool showAbove = false;
  115. UIElement* root = GetRoot();
  116. if (root)
  117. {
  118. const IntVector2& screenPos = GetScreenPosition();
  119. if (screenPos.y_ + GetHeight() + popup_->GetHeight() > root->GetHeight() && screenPos.y_ - popup_->GetHeight() >= 0)
  120. showAbove = true;
  121. }
  122. if (!showAbove)
  123. SetPopupOffset(0, GetHeight());
  124. else
  125. SetPopupOffset(0, -popup_->GetHeight());
  126. }
  127. void DropDownList::AddItem(UIElement* item)
  128. {
  129. InsertItem(listView_->GetNumItems(), item);
  130. }
  131. void DropDownList::InsertItem(unsigned index, UIElement* item)
  132. {
  133. listView_->InsertItem(index, item);
  134. // If there was no selection, set to the first
  135. if (listView_->GetSelection() == M_MAX_UNSIGNED)
  136. listView_->SetSelection(0);
  137. }
  138. void DropDownList::RemoveItem(UIElement* item)
  139. {
  140. listView_->RemoveItem(item);
  141. }
  142. void DropDownList::RemoveItem(unsigned index)
  143. {
  144. listView_->RemoveItem(index);
  145. }
  146. void DropDownList::RemoveAllItems()
  147. {
  148. listView_->RemoveAllItems();
  149. }
  150. void DropDownList::SetSelection(unsigned index)
  151. {
  152. listView_->SetSelection(index);
  153. }
  154. void DropDownList::SetResizePopup(bool enable)
  155. {
  156. resizePopup_ = enable;
  157. }
  158. unsigned DropDownList::GetNumItems() const
  159. {
  160. return listView_->GetNumItems();
  161. }
  162. UIElement* DropDownList::GetItem(unsigned index) const
  163. {
  164. return listView_->GetItem(index);
  165. }
  166. PODVector<UIElement*> DropDownList::GetItems() const
  167. {
  168. PODVector<UIElement*> items;
  169. listView_->GetContentElement()->GetChildren(items);
  170. return items;
  171. }
  172. unsigned DropDownList::GetSelection() const
  173. {
  174. return listView_->GetSelection();
  175. }
  176. UIElement* DropDownList::GetSelectedItem() const
  177. {
  178. return listView_->GetSelectedItem();
  179. }
  180. void DropDownList::HandleItemSelected(StringHash eventType, VariantMap& eventData)
  181. {
  182. // Resize the selection placeholder to match the selected item
  183. UIElement* selectedItem = GetSelectedItem();
  184. if (selectedItem)
  185. placeholder_->SetSize(selectedItem->GetSize());
  186. // Close the popup as the selection was made
  187. if (GetShowPopup())
  188. ShowPopup(false);
  189. // Send the event forward
  190. using namespace ItemSelected;
  191. VariantMap newEventData;
  192. newEventData[P_ELEMENT] = (void*)this;
  193. newEventData[P_SELECTION] = GetSelection();
  194. SendEvent(E_ITEMSELECTED, newEventData);
  195. }
  196. }