Browse Source

Fix DropDownList placeholder text not hiding when an item is selected. Clarify function comments on use of placeholder text.

Lasse Öörni 10 years ago
parent
commit
733c2e2d5b
2 changed files with 13 additions and 7 deletions
  1. 9 5
      Source/Urho3D/UI/DropDownList.cpp
  2. 4 2
      Source/Urho3D/UI/DropDownList.h

+ 9 - 5
Source/Urho3D/UI/DropDownList.cpp

@@ -63,6 +63,7 @@ DropDownList::DropDownList(Context* context) :
 
     SubscribeToEvent(listView_, E_ITEMCLICKED, URHO3D_HANDLER(DropDownList, HandleItemClicked));
     SubscribeToEvent(listView_, E_UNHANDLEDKEY, URHO3D_HANDLER(DropDownList, HandleListViewKey));
+    SubscribeToEvent(listView_, E_SELECTIONCHANGED, URHO3D_HANDLER(DropDownList, HandleSelectionChanged));
 }
 
 DropDownList::~DropDownList()
@@ -162,8 +163,8 @@ void DropDownList::InsertItem(unsigned index, UIElement* item)
     listView_->InsertItem(index, item);
 
     // If there was no selection, set to the first
-    if (listView_->GetSelection() == M_MAX_UNSIGNED)
-        listView_->SetSelection(0);
+    if (GetSelection() == M_MAX_UNSIGNED)
+        SetSelection(0);
 }
 
 void DropDownList::RemoveItem(UIElement* item)
@@ -184,9 +185,6 @@ void DropDownList::RemoveAllItems()
 void DropDownList::SetSelection(unsigned index)
 {
     listView_->SetSelection(index);
-
-    // 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
-    placeholder_->GetChild(0)->SetVisible(index == M_MAX_UNSIGNED);
 }
 
 void DropDownList::SetPlaceholderText(const String& text)
@@ -340,4 +338,10 @@ void DropDownList::HandleListViewKey(StringHash eventType, VariantMap& eventData
         HandleItemClicked(eventType, eventData);
 }
 
+void DropDownList::HandleSelectionChanged(StringHash eventType, VariantMap& eventData)
+{
+    // 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
+    placeholder_->GetChild(0)->SetVisible(GetSelection() == M_MAX_UNSIGNED);
+}
+
 }

+ 4 - 2
Source/Urho3D/UI/DropDownList.h

@@ -65,7 +65,7 @@ public:
     void RemoveAllItems();
     /// Set selection.
     void SetSelection(unsigned index);
-    /// Set place holder text. This is the text shown when there is no selection in drop down list.
+    /// Set place holder text. This is the text shown when there is no selection (-1) in drop down list. Note that if the list has items, the default is to show the first item, so the "no selection" state has to be set explicitly.
     void SetPlaceholderText(const String& text);
     /// Set whether popup should be automatically resized to match the dropdown button width.
     void SetResizePopup(bool enable);
@@ -112,8 +112,10 @@ protected:
 private:
     /// Handle listview item click event.
     void HandleItemClicked(StringHash eventType, VariantMap& eventData);
-    /// Handle a key press from the listview
+    /// Handle a key press from the listview.
     void HandleListViewKey(StringHash eventType, VariantMap& eventData);
+    /// Handle the listview selection change. Set placeholder text hidden/visible as necessary.
+    void HandleSelectionChanged(StringHash eventType, VariantMap& eventData);
 
     /// Selected item index attribute.
     unsigned selectionAttr_;