Maelstrom: Added a dropdown button type

https://github.com/libsdl-org/Maelstrom/commit/94bb6ed81e71667d18e7f472d78a1519978a5807

From 94bb6ed81e71667d18e7f472d78a1519978a5807 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Sun, 16 Dec 2012 20:42:34 -0800
Subject: [PATCH] Added a dropdown button type

---
 screenlib/Makefile.am           |   2 +
 screenlib/Makefile.in           |  10 +++-
 screenlib/UIElementDropdown.cpp | 102 ++++++++++++++++++++++++++++++++
 screenlib/UIElementDropdown.h   |  49 +++++++++++++++
 4 files changed, 160 insertions(+), 3 deletions(-)
 create mode 100644 screenlib/UIElementDropdown.cpp
 create mode 100644 screenlib/UIElementDropdown.h

diff --git a/screenlib/Makefile.am b/screenlib/Makefile.am
index e750fc25..f235bcc0 100644
--- a/screenlib/Makefile.am
+++ b/screenlib/Makefile.am
@@ -22,6 +22,8 @@ libSDLscreen_a_SOURCES =	\
 	UIElementButton.h	\
 	UIElementCheckbox.cpp	\
 	UIElementCheckbox.h	\
+	UIElementDropdown.cpp	\
+	UIElementDropdown.h	\
 	UIElementEditbox.cpp	\
 	UIElementEditbox.h	\
 	UIElementRadio.cpp	\
diff --git a/screenlib/Makefile.in b/screenlib/Makefile.in
index ddc01707..0d1996ea 100644
--- a/screenlib/Makefile.in
+++ b/screenlib/Makefile.in
@@ -75,9 +75,10 @@ am_libSDLscreen_a_OBJECTS = SDL_FrameBuf.$(OBJEXT) UIArea.$(OBJEXT) \
 	UIDialog.$(OBJEXT) UIDialogButton.$(OBJEXT) \
 	UIDrawEngine.$(OBJEXT) UIElement.$(OBJEXT) \
 	UIElementButton.$(OBJEXT) UIElementCheckbox.$(OBJEXT) \
-	UIElementEditbox.$(OBJEXT) UIElementRadio.$(OBJEXT) \
-	UIElementThumbstick.$(OBJEXT) UIManager.$(OBJEXT) \
-	UIPanel.$(OBJEXT) UITemplates.$(OBJEXT) UITexture.$(OBJEXT)
+	UIElementDropdown.$(OBJEXT) UIElementEditbox.$(OBJEXT) \
+	UIElementRadio.$(OBJEXT) UIElementThumbstick.$(OBJEXT) \
+	UIManager.$(OBJEXT) UIPanel.$(OBJEXT) UITemplates.$(OBJEXT) \
+	UITexture.$(OBJEXT)
 libSDLscreen_a_OBJECTS = $(am_libSDLscreen_a_OBJECTS)
 DEFAULT_INCLUDES = -I.@am__isrc@
 depcomp = $(SHELL) $(top_srcdir)/build-scripts/depcomp
@@ -234,6 +235,8 @@ libSDLscreen_a_SOURCES = \
 	UIElementButton.h	\
 	UIElementCheckbox.cpp	\
 	UIElementCheckbox.h	\
+	UIElementDropdown.cpp	\
+	UIElementDropdown.h	\
 	UIElementEditbox.cpp	\
 	UIElementEditbox.h	\
 	UIElementRadio.cpp	\
@@ -310,6 +313,7 @@ distclean-compile:
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/UIElement.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/UIElementButton.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/UIElementCheckbox.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/UIElementDropdown.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/UIElementEditbox.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/UIElementRadio.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/UIElementThumbstick.Po@am__quote@
diff --git a/screenlib/UIElementDropdown.cpp b/screenlib/UIElementDropdown.cpp
new file mode 100644
index 00000000..c2c088dd
--- /dev/null
+++ b/screenlib/UIElementDropdown.cpp
@@ -0,0 +1,102 @@
+/*
+  screenlib:  A simple window and UI library based on the SDL library
+  Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
+
+  This software is provided 'as-is', without any express or implied
+  warranty.  In no event will the authors be held liable for any damages
+  arising from the use of this software.
+
+  Permission is granted to anyone to use this software for any purpose,
+  including commercial applications, and to alter it and redistribute it
+  freely, subject to the following restrictions:
+
+  1. The origin of this software must not be misrepresented; you must not
+     claim that you wrote the original software. If you use this software
+     in a product, an acknowledgment in the product documentation would be
+     appreciated but is not required.
+  2. Altered source versions must be plainly marked as such, and must not be
+     misrepresented as being the original software.
+  3. This notice may not be removed or altered from any source distribution.
+*/
+
+#include "UIElementDropdown.h"
+
+UIElementType UIElementDropdown::s_elementType;
+
+
+UIElementDropdown::UIElementDropdown(UIBaseElement *parent, const char *name, UIDrawEngine *drawEngine) :
+	UIElementButton(parent, name, drawEngine)
+{
+	m_showingElements = true;
+}
+
+bool
+UIElementDropdown::FinishLoading()
+{
+	if (!UIElementButton::FinishLoading()) {
+		return false;
+	}
+
+	HideElements();
+
+	return true;
+}
+
+bool
+UIElementDropdown::HandleEvent(const SDL_Event &event)
+{
+	if (UIElementButton::HandleEvent(event)) {
+		return true;
+	}
+
+	// Hide the dropdown when we get a mouse down event anywhere else
+	if (event.type == SDL_MOUSEBUTTONDOWN) {
+		HideElements();
+	}
+	return false;
+}
+
+void
+UIElementDropdown::Action(UIBaseElement *sender, const char *action)
+{
+	// Hide the dropdown when we execute an action
+	HideElements();
+
+	UIElementButton::Action(sender, action);
+}
+
+void
+UIElementDropdown::OnClick()
+{
+	ToggleElements();
+
+	UIElementButton::OnClick();
+}
+
+void
+UIElementDropdown::ShowElements()
+{
+	for (unsigned int i = 0; i < m_elements.length(); ++i) {
+		m_elements[i]->Show();
+	}
+	m_showingElements = true;
+}
+
+void
+UIElementDropdown::HideElements()
+{
+	for (unsigned int i = 0; i < m_elements.length(); ++i) {
+		m_elements[i]->Hide();
+	}
+	m_showingElements = false;
+}
+
+void
+UIElementDropdown::ToggleElements()
+{
+	if (m_showingElements) {
+		HideElements();
+	} else {
+		ShowElements();
+	}
+}
diff --git a/screenlib/UIElementDropdown.h b/screenlib/UIElementDropdown.h
new file mode 100644
index 00000000..0e6707ea
--- /dev/null
+++ b/screenlib/UIElementDropdown.h
@@ -0,0 +1,49 @@
+/*
+  screenlib:  A simple window and UI library based on the SDL library
+  Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
+
+  This software is provided 'as-is', without any express or implied
+  warranty.  In no event will the authors be held liable for any damages
+  arising from the use of this software.
+
+  Permission is granted to anyone to use this software for any purpose,
+  including commercial applications, and to alter it and redistribute it
+  freely, subject to the following restrictions:
+
+  1. The origin of this software must not be misrepresented; you must not
+     claim that you wrote the original software. If you use this software
+     in a product, an acknowledgment in the product documentation would be
+     appreciated but is not required.
+  2. Altered source versions must be plainly marked as such, and must not be
+     misrepresented as being the original software.
+  3. This notice may not be removed or altered from any source distribution.
+*/
+
+#ifndef _UIElementDropdown_h
+#define _UIElementDropdown_h
+
+#include "UIElementButton.h"
+
+
+class UIElementDropdown : public UIElementButton
+{
+DECLARE_TYPESAFE_CLASS(UIElementButton)
+public:
+	UIElementDropdown(UIBaseElement *parent, const char *name, UIDrawEngine *drawEngine);
+	virtual ~UIElementDropdown() { }
+
+	override bool FinishLoading();
+
+	override bool HandleEvent(const SDL_Event &event);
+	override void Action(UIBaseElement *sender, const char *action);
+	override void OnClick();
+
+	void ShowElements();
+	void HideElements();
+	void ToggleElements();
+
+protected:
+	bool m_showingElements;
+};
+
+#endif // _UIElementDropdown_h