Maelstrom: Added autosizing based on the parent's size

https://github.com/libsdl-org/Maelstrom/commit/621d9a9cc14fa751df8fed77bdb8ae211fd1ea05

From 621d9a9cc14fa751df8fed77bdb8ae211fd1ea05 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Fri, 14 Sep 2012 01:48:47 -0700
Subject: [PATCH] Added autosizing based on the parent's size

---
 screenlib/UIArea.cpp      | 27 ++++++++++++++++++++-------
 screenlib/UIArea.h        |  9 +++++----
 screenlib/UIBaseElement.h |  4 ++++
 screenlib/UIManager.h     |  2 +-
 4 files changed, 30 insertions(+), 12 deletions(-)

diff --git a/screenlib/UIArea.cpp b/screenlib/UIArea.cpp
index 59cff532..d310a2ed 100644
--- a/screenlib/UIArea.cpp
+++ b/screenlib/UIArea.cpp
@@ -25,6 +25,7 @@
 
 UIArea::UIArea(UIArea *anchor, int w, int h) : ErrorBase()
 {
+	m_autosizeParent = true;
 	m_autosizeWidth = true;
 	m_autosizeHeight = true;
 	m_rect.x = 0;
@@ -122,7 +123,7 @@ UIArea::SetPosition(int x, int y) {
 }
 
 void
-UIArea::SetSize(int w, int h, bool autosize)
+UIArea::SetSize(int w, int h, bool autosize, bool parent)
 {
 	if (w != m_rect.w || h != m_rect.h) {
 		m_rect.w = w;
@@ -134,10 +135,13 @@ UIArea::SetSize(int w, int h, bool autosize)
 		m_autosizeWidth = false;
 		m_autosizeHeight = false;
 	}
+	if (!parent) {
+		m_autosizeParent = false;
+	}
 }
 
 void
-UIArea::SetWidth(int w, bool autosize)
+UIArea::SetWidth(int w, bool autosize, bool parent)
 {
 	if (w != m_rect.w) {
 		m_rect.w = w;
@@ -147,10 +151,13 @@ UIArea::SetWidth(int w, bool autosize)
 	if (!autosize) {
 		m_autosizeWidth = false;
 	}
+	if (!parent) {
+		m_autosizeParent = false;
+	}
 }
 
 void
-UIArea::SetHeight(int h, bool autosize)
+UIArea::SetHeight(int h, bool autosize, bool parent)
 {
 	if (h != m_rect.h) {
 		m_rect.h = h;
@@ -160,17 +167,23 @@ UIArea::SetHeight(int h, bool autosize)
 	if (!autosize) {
 		m_autosizeHeight = false;
 	}
+	if (!parent) {
+		m_autosizeParent = false;
+	}
 }
 
 void
-UIArea::AutoSize(int w, int h)
+UIArea::AutoSize(int w, int h, bool parent)
 {
+	if (parent && !m_autosizeParent) {
+		return;
+	}
 	if (m_autosizeWidth && m_autosizeHeight) {
-		SetSize(w, h, true);
+		SetSize(w, h, true, parent);
 	} else if (m_autosizeWidth) {
-		SetWidth(w, true);
+		SetWidth(w, true, parent);
 	} else if (m_autosizeHeight)  {
-		SetHeight(h, true);
+		SetHeight(h, true, parent);
 	}
 }
 
diff --git a/screenlib/UIArea.h b/screenlib/UIArea.h
index 9040fde3..c3958661 100644
--- a/screenlib/UIArea.h
+++ b/screenlib/UIArea.h
@@ -62,10 +62,10 @@ class UIArea : public ErrorBase
 	virtual UIArea *GetAnchorElement(const char *name);
 
 	void SetPosition(int x, int y);
-	void SetSize(int w, int h, bool autosize = false);
-	void SetWidth(int w, bool autosize = false);
-	void SetHeight(int h, bool autosize = false);
-	void AutoSize(int w, int h);
+	void SetSize(int w, int h, bool autosize = false, bool parent = false);
+	void SetWidth(int w, bool autosize = false, bool parent = false);
+	void SetHeight(int h, bool autosize = false, bool parent = false);
+	void AutoSize(int w, int h, bool parent = false);
 	void SetAnchor(AnchorLocation from, AnchorLocation to, UIArea *anchor,
 					int offsetX = 0, int offsetY = 0);
 
@@ -113,6 +113,7 @@ class UIArea : public ErrorBase
 
 private:
 	/* This is private so updates can trigger OnRectChanged() */
+	bool m_autosizeParent;
 	bool m_autosizeWidth;
 	bool m_autosizeHeight;
 	SDL_Rect m_rect;
diff --git a/screenlib/UIBaseElement.h b/screenlib/UIBaseElement.h
index 2be78f7b..cf650f91 100644
--- a/screenlib/UIBaseElement.h
+++ b/screenlib/UIBaseElement.h
@@ -171,6 +171,10 @@ class UIBaseElement : public UIArea
 	virtual void OnRectChanged() {
 		UIArea::OnRectChanged();
 
+		for (int i = 0; i < m_elements.length(); ++i) {
+			UIBaseElement *element = m_elements[i];
+			element->AutoSize(Width(), Height(), true);
+		}
 		if (m_parent) {
 			m_parent->OnChildRectChanged(this);
 		}
diff --git a/screenlib/UIManager.h b/screenlib/UIManager.h
index f126993c..0903a562 100644
--- a/screenlib/UIManager.h
+++ b/screenlib/UIManager.h
@@ -98,7 +98,7 @@ class UIManager : public UIArea, public UIFontInterface, public UISoundInterface
 
 		for (int i = 0; i < m_panels.length(); ++i) {
 			UIPanel *panel = m_panels[i];
-			panel->AutoSize(Width(), Height());
+			panel->AutoSize(Width(), Height(), true);
 		}
 	}