Maelstrom: Moved the concept of the label to the button base class.

https://github.com/libsdl-org/Maelstrom/commit/19387b988e4d7af6562db9a68a9243a97913a8e1

From 19387b988e4d7af6562db9a68a9243a97913a8e1 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Sun, 30 Oct 2011 06:27:50 -0400
Subject: [PATCH] Moved the concept of the label to the button base class. This
 makes sense, simplifies the MacDialog classes and will be useful for the
 editbox.

---
 MacDialogButton.cpp           |  8 +++++++-
 MacDialogButton.h             |  6 ++++--
 MacDialogCheckbox.cpp         | 26 +++++++------------------
 MacDialogCheckbox.h           |  5 +++--
 MacDialogRadioButton.cpp      | 28 +++++++--------------------
 MacDialogRadioButton.h        |  5 +++--
 screenlib/UIDialogButton.cpp  | 14 --------------
 screenlib/UIDialogButton.h    |  1 -
 screenlib/UIElementButton.cpp | 36 +++++++++++++++++++++++++++++++++++
 screenlib/UIElementButton.h   | 18 ++++++++++++------
 screenlib/UIElementRadio.cpp  | 14 ++++++++++++++
 screenlib/UIElementRadio.h    |  1 +
 12 files changed, 94 insertions(+), 68 deletions(-)

diff --git a/MacDialogButton.cpp b/MacDialogButton.cpp
index 687dd3f1..cdb5f5eb 100644
--- a/MacDialogButton.cpp
+++ b/MacDialogButton.cpp
@@ -1,7 +1,7 @@
 
 #include "screenlib/SDL_FrameBuf.h"
-#include "screenlib/UIElementLabel.h"
 #include "MacDialogButton.h"
+#include "MacDialogLabel.h"
 
 /* Default dialog button size */
 #define BUTTON_WIDTH	75
@@ -20,6 +20,12 @@ MacDialogButton::MacDialogButton(UIBaseElement *parent, const char *name) :
 	SetSize(BUTTON_WIDTH, BUTTON_HEIGHT);
 }
 
+UIElementLabel *
+MacDialogButton::CreateLabel()
+{
+	return new MacDialogLabel(this, "label");
+}
+
 void
 MacDialogButton::Draw()
 {
diff --git a/MacDialogButton.h b/MacDialogButton.h
index c2bc0cba..f6634550 100644
--- a/MacDialogButton.h
+++ b/MacDialogButton.h
@@ -16,10 +16,12 @@ DECLARE_TYPESAFE_CLASS(UIDialogButton)
 	override void OnMouseUp();
 
 protected:
-	Uint32 m_colors[2];
+	override UIElementLabel *CreateLabel();
 
-protected:
 	void SetElementColor(Uint32 color);
+
+protected:
+	Uint32 m_colors[2];
 };
 
 #endif // _MacDialogButton_h
diff --git a/MacDialogCheckbox.cpp b/MacDialogCheckbox.cpp
index e91b2a93..bb3a4468 100644
--- a/MacDialogCheckbox.cpp
+++ b/MacDialogCheckbox.cpp
@@ -18,27 +18,15 @@ MacDialogCheckbox::MacDialogCheckbox(UIBaseElement *parent, const char *name) :
 	SetSize(CHECKBOX_SIZE, CHECKBOX_SIZE);
 }
 
-bool
-MacDialogCheckbox::Load(rapidxml::xml_node<> *node, const UITemplates *templates)
+UIElementLabel *
+MacDialogCheckbox::CreateLabel()
 {
-	rapidxml::xml_attribute<> *attr;
+	MacDialogLabel *label;
 
-	if (!UIElementCheckbox::Load(node, templates)) {
-		return false;
-	}
-
-	attr = node->first_attribute("text", 0, false);
-	if (attr) {
-		MacDialogLabel *label;
-
-		label = new MacDialogLabel(this, "label");
-		label->SetText(attr->value());
-		label->SetTextColor(m_color);
-		label->SetAnchor(TOPLEFT, TOPRIGHT, this, 3, -2);
-		AddElement(label);
-	}
-
-	return true;
+	label = new MacDialogLabel(this, "label");
+	label->SetTextColor(m_color);
+	label->SetAnchor(TOPLEFT, TOPRIGHT, this, 3, -2);
+	return label;
 }
 
 void
diff --git a/MacDialogCheckbox.h b/MacDialogCheckbox.h
index a6f03d5c..9eb2a6d7 100644
--- a/MacDialogCheckbox.h
+++ b/MacDialogCheckbox.h
@@ -10,10 +10,11 @@ DECLARE_TYPESAFE_CLASS(UIElementCheckbox)
 public:
 	MacDialogCheckbox(UIBaseElement *parent, const char *name = "");
 
-	override bool Load(rapidxml::xml_node<> *node, const UITemplates *templates);
-
 	override void Draw();
 
+protected:
+	override UIElementLabel *CreateLabel();
+
 protected:
 	Uint32 m_color;
 };
diff --git a/MacDialogRadioButton.cpp b/MacDialogRadioButton.cpp
index 6cd30635..2a515e2d 100644
--- a/MacDialogRadioButton.cpp
+++ b/MacDialogRadioButton.cpp
@@ -17,29 +17,15 @@ MacDialogRadioButton::MacDialogRadioButton(UIBaseElement *parent, const char *na
 	SetSize(RADIOBUTTON_SIZE, RADIOBUTTON_SIZE);
 }
 
-bool
-MacDialogRadioButton::Load(rapidxml::xml_node<> *node, const UITemplates *templates)
-{
-	rapidxml::xml_attribute<> *attr;
-
-	if (!UIElementRadioButton::Load(node, templates)) {
-		return false;
-	}
-
-	attr = node->first_attribute("text", 0, false);
-	if (attr) {
-		MacDialogLabel *label;
 
-		label = new MacDialogLabel(this, "label");
-		label->SetText(attr->value());
-		label->SetAnchor(TOPLEFT, TOPLEFT, this, 21, 3);
-		AddElement(label);
-
-		/* Extend the sensitive area to encompass the label */
-		SetWidth((label->X()+label->Width()) - X());
-	}
+UIElementLabel *
+MacDialogRadioButton::CreateLabel()
+{
+	MacDialogLabel *label;
 
-	return true;
+	label = new MacDialogLabel(this, "label");
+	label->SetAnchor(TOPLEFT, TOPLEFT, this, 21, 3);
+	return label;
 }
 
 void
diff --git a/MacDialogRadioButton.h b/MacDialogRadioButton.h
index b673efd4..e4778764 100644
--- a/MacDialogRadioButton.h
+++ b/MacDialogRadioButton.h
@@ -10,10 +10,11 @@ DECLARE_TYPESAFE_CLASS(UIElementRadioButton)
 public:
 	MacDialogRadioButton(UIBaseElement *parent, const char *name = "");
 
-	override bool Load(rapidxml::xml_node<> *node, const UITemplates *templates);
-
 	override void Draw();
 
+protected:
+	override UIElementLabel *CreateLabel();
+
 protected:
 	Uint32 m_color;
 };
diff --git a/screenlib/UIDialogButton.cpp b/screenlib/UIDialogButton.cpp
index 67db7f65..00296e22 100644
--- a/screenlib/UIDialogButton.cpp
+++ b/screenlib/UIDialogButton.cpp
@@ -55,20 +55,6 @@ UIDialogButton::Load(rapidxml::xml_node<> *node, const UITemplates *templates)
 
 	LoadBool(node, "closeDialog", m_closeDialog);
 
-	attr = node->first_attribute("text", 0, false);
-	if (attr) {
-		UIElement *label;
-
-		label = GetUI()->CreateElement(this, "DialogLabel", "label");
-		if (label && label->IsA(UIElementLabel::GetType())) {
-			static_cast<UIElementLabel*>(label)->SetText(attr->value());
-			AddElement(label);
-		} else {
-			fprintf(stderr, "Warning: Couldn't create dialog button label");
-			delete label;
-		}
-	}
-
 	return true;
 }
 
diff --git a/screenlib/UIDialogButton.h b/screenlib/UIDialogButton.h
index 7b715e6c..1565e918 100644
--- a/screenlib/UIDialogButton.h
+++ b/screenlib/UIDialogButton.h
@@ -25,7 +25,6 @@
 
 #include "UIElementButton.h"
 
-
 class UIDialogButton : public UIElementButton
 {
 DECLARE_TYPESAFE_CLASS(UIElementButton)
diff --git a/screenlib/UIElementButton.cpp b/screenlib/UIElementButton.cpp
index db14df8f..4ebe3b9c 100644
--- a/screenlib/UIElementButton.cpp
+++ b/screenlib/UIElementButton.cpp
@@ -23,6 +23,7 @@
 #include "UIManager.h"
 #include "UIPanel.h"
 #include "UIElementButton.h"
+#include "UIElementLabel.h"
 
 
 class SimpleButtonDelegate : public UIButtonDelegate
@@ -52,6 +53,7 @@ UIElementButton::UIElementButton(UIBaseElement *parent, const char *name) :
 	m_mousePressed = false;
 	m_clickSound = 0;
 	m_clickPanel = NULL;
+	m_label = NULL;
 	m_delegate = NULL;
 	m_deleteDelegate = false;
 }
@@ -111,9 +113,35 @@ UIElementButton::Load(rapidxml::xml_node<> *node, const UITemplates *templates)
 	LoadNumber(node, "clickSound", m_clickSound);
 	LoadString(node, "clickPanel", m_clickPanel);
 
+	attr = node->first_attribute("text", 0, false);
+	if (attr) {
+		m_label = CreateLabel();
+		if (m_label) {
+			AddElement(m_label);
+		}
+		SetText(attr->value());
+	}
+
 	return true;
 }
 
+UIElementLabel *
+UIElementButton::CreateLabel()
+{
+	UIElement *label;
+
+	label = GetUI()->CreateElement(this, "Label", "label");
+	if (!label) {
+		return NULL;
+	}
+	if (label->IsA(UIElementLabel::GetType())) {
+		return static_cast<UIElementLabel*>(label);
+	} else {
+		delete label;
+		return NULL;
+	}
+}
+
 bool
 UIElementButton::ShouldHandleKey(SDL_Keycode key)
 {
@@ -228,6 +256,14 @@ UIElementButton::OnClick()
 	}
 }
 
+void
+UIElementButton::SetText(const char *text)
+{
+	if (m_label) {
+		m_label->SetText(text);
+	}
+}
+
 void
 UIElementButton::SetClickCallback(void (*callback)(void))
 {
diff --git a/screenlib/UIElementButton.h b/screenlib/UIElementButton.h
index 1f33d54c..ae8aba56 100644
--- a/screenlib/UIElementButton.h
+++ b/screenlib/UIElementButton.h
@@ -25,6 +25,8 @@
 
 #include "UIElement.h"
 
+class UIElementLabel;
+
 class UIButtonDelegate
 {
 public:
@@ -42,16 +44,22 @@ DECLARE_TYPESAFE_CLASS(UIElement)
 
 	override bool HandleEvent(const SDL_Event &event);
 
+	void SetText(const char *text);
+
+	// Setting a click callback sets a simplified delegate
+	void SetClickCallback(void (*callback)(void));
+	void SetButtonDelegate(UIButtonDelegate *delegate, bool autodelete = true);
+
+protected:
 	// These can be overridden by inherited classes
 	virtual void OnMouseEnter() { }
 	virtual void OnMouseLeave() { }
 	virtual void OnMouseDown() { }
 	virtual void OnMouseUp() { }
 	virtual void OnClick();
+	virtual UIElementLabel *CreateLabel();
 
-	// Setting a click callback sets a simplified delegate
-	void SetClickCallback(void (*callback)(void));
-	void SetButtonDelegate(UIButtonDelegate *delegate, bool autodelete = true);
+	bool ShouldHandleKey(SDL_Keycode key);
 
 protected:
 	SDL_Keycode m_hotkey;
@@ -60,11 +68,9 @@ DECLARE_TYPESAFE_CLASS(UIElement)
 	bool m_mousePressed;
 	int m_clickSound;
 	char *m_clickPanel;
+	UIElementLabel *m_label;
 	UIButtonDelegate *m_delegate;
 	bool m_deleteDelegate;
-
-protected:
-	bool ShouldHandleKey(SDL_Keycode key);
 };
 
 #endif // _UIElementButton_h
diff --git a/screenlib/UIElementRadio.cpp b/screenlib/UIElementRadio.cpp
index 601c8ecc..e354b122 100644
--- a/screenlib/UIElementRadio.cpp
+++ b/screenlib/UIElementRadio.cpp
@@ -1,5 +1,6 @@
 
 #include "UIElementRadio.h"
+#include "UIElementLabel.h"
 
 UIElementType UIElementRadioGroup::s_elementType;
 
@@ -64,6 +65,19 @@ UIElementRadioButton::Load(rapidxml::xml_node<> *node, const UITemplates *templa
 	return true;
 }
 
+bool
+UIElementRadioButton::FinishLoading()
+{
+	if (m_label) {
+		// Extend the sensitive area to encompass the label
+		if (m_label->X() >= X()) {
+			SetWidth((m_label->X()+m_label->Width()) - X());
+		} else {
+			assert(!"Need code for labels on the left");
+		}
+	}
+}
+
 void
 UIElementRadioButton::OnChecked(bool checked)
 {
diff --git a/screenlib/UIElementRadio.h b/screenlib/UIElementRadio.h
index 63cb7fc8..bb4cd4f7 100644
--- a/screenlib/UIElementRadio.h
+++ b/screenlib/UIElementRadio.h
@@ -65,6 +65,7 @@ DECLARE_TYPESAFE_CLASS(UIElementCheckbox)
 	}
 
 	override bool Load(rapidxml::xml_node<> *node, const UITemplates *templates);
+	override bool FinishLoading();
 
 	override void OnChecked(bool checked);