DropDownList.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Oorni
  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. 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>();
  48. placeholder_->SetInternal(true);
  49. SubscribeToEvent(listView_, E_ITEMSELECTED, HANDLER(DropDownList, HandleItemSelected));
  50. }
  51. DropDownList::~DropDownList()
  52. {
  53. }
  54. void DropDownList::RegisterObject(Context* context)
  55. {
  56. context->RegisterFactory<DropDownList>();
  57. COPY_BASE_ATTRIBUTES(DropDownList, Menu);
  58. ACCESSOR_ATTRIBUTE(DropDownList, VAR_INT, "Selection", GetSelection, SetSelectionAttr, unsigned, 0, AM_FILE);
  59. ACCESSOR_ATTRIBUTE(DropDownList, VAR_BOOL, "Resize Popup", GetResizePopup, SetResizePopup, bool, false, AM_FILE);
  60. }
  61. void DropDownList::ApplyAttributes()
  62. {
  63. // Reapply selection after possible items have been loaded
  64. SetSelection(selectionAttr_);
  65. }
  66. void DropDownList::GetBatches(PODVector<UIBatch>& batches, PODVector<UIQuad>& quads, const IntRect& currentScissor)
  67. {
  68. Button::GetBatches(batches, quads, currentScissor);
  69. if (!placeholder_->IsVisible())
  70. return;
  71. UIElement* selectedItem = GetSelectedItem();
  72. if (selectedItem)
  73. {
  74. // Can not easily copy the selected item. However, it can be re-rendered on the placeholder's position
  75. const IntVector2& targetPos = placeholder_->GetScreenPosition();
  76. const IntVector2& originalPos = selectedItem->GetScreenPosition();
  77. IntVector2 offset = targetPos - originalPos;
  78. // GetBatches() usually resets the hover flag. Therefore get its value and then reset it for the real rendering
  79. bool hover = selectedItem->IsHovering();
  80. selectedItem->SetHovering(false);
  81. selectedItem->GetBatchesWithOffset(offset, batches, quads, currentScissor);
  82. selectedItem->SetHovering(hover);
  83. }
  84. }
  85. void DropDownList::OnShowPopup()
  86. {
  87. // Resize the popup to match the size of the list content, and optionally match the button width
  88. UIElement* content = listView_->GetContentElement();
  89. content->UpdateLayout();
  90. const IntVector2& contentSize = content->GetSize();
  91. const IntRect& border = popup_->GetLayoutBorder();
  92. popup_->SetSize(resizePopup_ ? GetWidth() : contentSize.x_ + border.left_ + border.right_, contentSize.y_ + border.top_ +
  93. border.bottom_);
  94. // Check if popup fits below the button. If not, show above instead
  95. bool showAbove = false;
  96. UIElement* root = GetRoot();
  97. if (root)
  98. {
  99. const IntVector2& screenPos = GetScreenPosition();
  100. if (screenPos.y_ + GetHeight() + popup_->GetHeight() > root->GetHeight() && screenPos.y_ - popup_->GetHeight() >= 0)
  101. showAbove = true;
  102. }
  103. if (!showAbove)
  104. SetPopupOffset(0, GetHeight());
  105. else
  106. SetPopupOffset(0, -popup_->GetHeight());
  107. }
  108. void DropDownList::AddItem(UIElement* item)
  109. {
  110. InsertItem(listView_->GetNumItems(), item);
  111. }
  112. void DropDownList::InsertItem(unsigned index, UIElement* item)
  113. {
  114. listView_->InsertItem(index, item);
  115. // If there was no selection, set to the first
  116. if (listView_->GetSelection() == M_MAX_UNSIGNED)
  117. listView_->SetSelection(0);
  118. }
  119. void DropDownList::RemoveItem(UIElement* item)
  120. {
  121. listView_->RemoveItem(item);
  122. }
  123. void DropDownList::RemoveItem(unsigned index)
  124. {
  125. listView_->RemoveItem(index);
  126. }
  127. void DropDownList::RemoveAllItems()
  128. {
  129. listView_->RemoveAllItems();
  130. }
  131. void DropDownList::SetSelection(unsigned index)
  132. {
  133. listView_->SetSelection(index);
  134. }
  135. void DropDownList::SetResizePopup(bool enable)
  136. {
  137. resizePopup_ = enable;
  138. }
  139. unsigned DropDownList::GetNumItems() const
  140. {
  141. return listView_->GetNumItems();
  142. }
  143. UIElement* DropDownList::GetItem(unsigned index) const
  144. {
  145. return listView_->GetItem(index);
  146. }
  147. PODVector<UIElement*> DropDownList::GetItems() const
  148. {
  149. PODVector<UIElement*> items;
  150. listView_->GetContentElement()->GetChildren(items);
  151. return items;
  152. }
  153. unsigned DropDownList::GetSelection() const
  154. {
  155. return listView_->GetSelection();
  156. }
  157. UIElement* DropDownList::GetSelectedItem() const
  158. {
  159. return listView_->GetSelectedItem();
  160. }
  161. void DropDownList::SetSelectionAttr(unsigned index)
  162. {
  163. selectionAttr_ = index;
  164. // We may not have the list items yet. Apply the index again in ApplyAttributes().
  165. SetSelection(index);
  166. }
  167. void DropDownList::HandleItemSelected(StringHash eventType, VariantMap& eventData)
  168. {
  169. // Resize the selection placeholder to match the selected item
  170. UIElement* selectedItem = GetSelectedItem();
  171. if (selectedItem)
  172. placeholder_->SetSize(selectedItem->GetSize());
  173. // Close the popup as the selection was made
  174. if (GetShowPopup())
  175. ShowPopup(false);
  176. // Send the event forward
  177. using namespace ItemSelected;
  178. VariantMap newEventData;
  179. newEventData[P_ELEMENT] = (void*)this;
  180. newEventData[P_SELECTION] = GetSelection();
  181. SendEvent(E_ITEMSELECTED, newEventData);
  182. }
  183. }