Maelstrom: Dispatch events to the proper panel if there is a topmost panel hanging out.

https://github.com/libsdl-org/Maelstrom/commit/612479a975603da19833e310635259372916a225

From 612479a975603da19833e310635259372916a225 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Fri, 23 Nov 2012 03:24:10 -0800
Subject: [PATCH] Dispatch events to the proper panel if there is a topmost
 panel hanging out.

---
 screenlib/UIDialogButton.cpp |  7 ++++++-
 screenlib/UIManager.cpp      | 30 ++++++++++++++++++++++++++++++
 screenlib/UIManager.h        |  2 ++
 screenlib/UIPanel.cpp        |  4 ++--
 4 files changed, 40 insertions(+), 3 deletions(-)

diff --git a/screenlib/UIDialogButton.cpp b/screenlib/UIDialogButton.cpp
index f9c8ddc1..9dec0301 100644
--- a/screenlib/UIDialogButton.cpp
+++ b/screenlib/UIDialogButton.cpp
@@ -57,7 +57,12 @@ UIDialogButton::Load(rapidxml::xml_node<> *node, const UITemplates *templates)
 void
 UIDialogButton::OnClick()
 {
-	UIPanel *panel = GetUI()->GetCurrentPanel();
+	UIBaseElement *parent;
+	UIPanel *panel = NULL;
+
+	while (!panel && (parent = GetParent()) != NULL) {
+		panel = parent->Cast<UIPanel>();
+	}
 
 	if (m_statusID && panel && panel->IsA(UIDialog::GetType())) {
 		static_cast<UIDialog*>(panel)->SetDialogStatus(m_statusID);
diff --git a/screenlib/UIManager.cpp b/screenlib/UIManager.cpp
index 17348929..a2a64c02 100644
--- a/screenlib/UIManager.cpp
+++ b/screenlib/UIManager.cpp
@@ -179,6 +179,36 @@ UIManager::GetFullscreenPanel()
 	return NULL;
 }
 
+UIPanel *
+UIManager::GetNextPanel(UIPanel *panel)
+{
+	for (int i = 0; i < m_visible.length(); ++i) {
+		if (m_visible[i] == panel) {
+			if (i+1 < m_visible.length()) {
+				return m_visible[i+1];
+			} else {
+				return NULL;
+			}
+		}
+	}
+	return NULL;
+}
+
+UIPanel *
+UIManager::GetPrevPanel(UIPanel *panel)
+{
+	for (int i = 0; i < m_visible.length(); ++i) {
+		if (m_visible[i] == panel) {
+			if (i > 0) {
+				return m_visible[i-1];
+			} else {
+				return NULL;
+			}
+		}
+	}
+	return NULL;
+}
+
 UIPanel *
 UIManager::GetCurrentPanel()
 {
diff --git a/screenlib/UIManager.h b/screenlib/UIManager.h
index 891ce86e..7d294ecc 100644
--- a/screenlib/UIManager.h
+++ b/screenlib/UIManager.h
@@ -74,6 +74,8 @@ class UIManager : public UIArea, public UIFontInterface, public UIImageInterface
 		return NULL;
 	}
 	UIPanel *GetFullscreenPanel();
+	UIPanel *GetNextPanel(UIPanel *panel);
+	UIPanel *GetPrevPanel(UIPanel *panel);
 	UIPanel *GetCurrentPanel();
 
 	/* These are called by the UIPanel class */
diff --git a/screenlib/UIPanel.cpp b/screenlib/UIPanel.cpp
index 8a255662..fda2a7e0 100644
--- a/screenlib/UIPanel.cpp
+++ b/screenlib/UIPanel.cpp
@@ -200,8 +200,8 @@ UIPanel::Action(UIBaseElement *sender, const char *action)
 	}
 
 	// Dialogs pass actions to their parents
-	UIPanel *panel = m_ui->GetFullscreenPanel();
-	if (panel && panel != this) {
+	UIPanel *panel = m_ui->GetPrevPanel(this);
+	if (panel) {
 		panel->Action(sender, action);
 	} else {
 		if (SDL_strncmp(action, "show_", 5) == 0) {