Maelstrom: Added missing Mac dialog editbox drawing

https://github.com/libsdl-org/Maelstrom/commit/55d709fcc5f724175a694a66ec618d0fb8566572

From 55d709fcc5f724175a694a66ec618d0fb8566572 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Wed, 2 Nov 2011 01:13:32 -0400
Subject: [PATCH] Added missing Mac dialog editbox drawing

---
 MacDialog.cpp                  | 49 +++++++++++++++++++++++++++++++++-
 MacDialog.h                    |  5 ++++
 screenlib/UIElementEditbox.cpp |  7 ++---
 screenlib/UIElementEditbox.h   |  9 ++++++-
 4 files changed, 65 insertions(+), 5 deletions(-)

diff --git a/MacDialog.cpp b/MacDialog.cpp
index 638162df..0e0a3458 100644
--- a/MacDialog.cpp
+++ b/MacDialog.cpp
@@ -23,6 +23,7 @@
 #include "screenlib/SDL_FrameBuf.h"
 #include "screenlib/UIDialogButton.h"
 #include "screenlib/UIElementCheckbox.h"
+#include "screenlib/UIElementEditbox.h"
 #include "screenlib/UIElementRadio.h"
 
 #include "MacDialog.h"
@@ -357,7 +358,53 @@ MacDialogEditbox::Init(UIElement *element)
 {
 	MacDialogDrawEngine::Init(element);
 
-	m_element->SetBorder(true);
+	UIArea *area = m_element->GetTextArea();
+	area->SetAnchor(LEFT, LEFT, m_element, 3, 0);
+}
+
+void
+MacDialogEditbox::OnLoad()
+{
+	MacDialogDrawEngine::OnLoad();
+
+	m_colors[0] = m_element->GetFillColor();
+	m_colors[1] = m_element->GetColor();
+}
+
+void
+MacDialogEditbox::OnDraw()
+{
+	bool highlight = false;
+	bool hasfocus = false;
+
+	if (m_element->IsA(UIElementEditbox::GetType())) {
+		UIElementEditbox *editbox;
+
+		editbox = static_cast<UIElementEditbox*>(m_element);
+		highlight = editbox->IsHighlighted();
+		hasfocus = editbox->HasFocus();
+	}
+
+	/* The colors are inverted when the editbox is highlighted */
+	m_element->SetFillColor(m_colors[highlight]);
+	m_element->SetColor(m_colors[!highlight]);
+
+	// Draw the outline, always in the real foreground color
+	m_screen->DrawRect(m_element->X(), m_element->Y(), m_element->Width(), m_element->Height(), m_colors[1]);
+
+	if (highlight) {
+		// Draw the highlight
+		m_screen->FillRect(m_element->X()+3, m_element->Y()+3, m_element->Width()-6, m_element->Height()-6, m_element->GetFillColor());
+	}
+
+	MacDialogDrawEngine::OnDraw();
+
+	if (hasfocus && !highlight) {
+		// Draw the cursor
+		int x = m_element->GetTextArea()->X() + m_element->GetTextArea()->Width();
+
+		m_screen->DrawLine(x, m_element->Y()+3, x, m_element->Y()+3+m_element->Height()-6-1, m_element->GetColor());
+	}
 }
 
 //////////////////////////////////////////////////////////////////////////////
diff --git a/MacDialog.h b/MacDialog.h
index 983d9bb8..ef3598b2 100644
--- a/MacDialog.h
+++ b/MacDialog.h
@@ -106,6 +106,11 @@ class MacDialogEditbox : public MacDialogDrawEngine
 	MacDialogEditbox() : MacDialogDrawEngine() { }
 
 	override void Init(UIElement *element);
+	override void OnLoad();
+	override void OnDraw();
+
+protected:
+	Uint32 m_colors[2];
 };
 
 //////////////////////////////////////////////////////////////////////////////
diff --git a/screenlib/UIElementEditbox.cpp b/screenlib/UIElementEditbox.cpp
index 04e55ac9..46a7a04f 100644
--- a/screenlib/UIElementEditbox.cpp
+++ b/screenlib/UIElementEditbox.cpp
@@ -69,9 +69,10 @@ UIElementEditbox::HandleEvent(const SDL_Event &event)
 
 	if (event.type == SDL_KEYUP) {
 		switch (event.key.keysym.sym) {
-			case SDLK_ESCAPE:
-				SetFocus(false);
-				return true;
+			// This is confusing in dialogs which are canceled by Escape.
+			//case SDLK_ESCAPE:
+			//	SetFocus(false);
+			//	return true;
 			case SDLK_TAB:
 				SetFocusNext();
 				return true;
diff --git a/screenlib/UIElementEditbox.h b/screenlib/UIElementEditbox.h
index 1281aa2b..cdfa0979 100644
--- a/screenlib/UIElementEditbox.h
+++ b/screenlib/UIElementEditbox.h
@@ -41,11 +41,18 @@ DECLARE_TYPESAFE_CLASS(UIElement)
 	override bool HandleEvent(const SDL_Event &event);
 
 	override void OnClick() {
-		SetFocus(true);
+		SetFocus(!HasFocus());
+	}
+
+	bool IsHighlighted() const {
+		return m_highlight;
 	}
 
 	void SetFocus(bool focus);
 	void SetFocusNext();
+	bool HasFocus() const {
+		return m_focus;
+	}
 
 	void SetTextMax(int maxLen);