From a865ca522f56feb5573f26d668270ce3b721e382 Mon Sep 17 00:00:00 2001
From: David Robillard <d@drobilla.net>
Date: Wed, 31 Jul 2019 21:05:52 +0200
Subject: Windows: Support UTF8 in window titles

---
 pugl/detail/win.c | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)

diff --git a/pugl/detail/win.c b/pugl/detail/win.c
index 6b56c07..6e176b6 100644
--- a/pugl/detail/win.c
+++ b/pugl/detail/win.c
@@ -52,6 +52,19 @@ typedef BOOL (WINAPI *PFN_SetProcessDPIAware)(void);
 
 static const TCHAR* DEFAULT_CLASSNAME = "Pugl";
 
+static wchar_t*
+puglUtf8ToWideChar(const char* const utf8)
+{
+    const int len = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);
+    if (len > 0) {
+	    wchar_t* result = (wchar_t*)calloc(len, sizeof(wchar_t));
+	    MultiByteToWideChar(CP_UTF8, 0, utf8, -1, result, len);
+	    return result;
+    }
+
+    return NULL;
+}
+
 LRESULT CALLBACK
 wndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
 
@@ -117,7 +130,12 @@ puglCreateWindow(PuglView* view, const char* title)
 		return 3;
 	}
 
-	SetWindowText(impl->hwnd, title);
+	wchar_t* wtitle = puglUtf8ToWideChar(title);
+	if (wtitle) {
+		SetWindowTextW(impl->hwnd, wtitle);
+		free(wtitle);
+	}
+
 	SetWindowLongPtr(impl->hwnd, GWLP_USERDATA, (LONG_PTR)view);
 
 	return 0;
-- 
cgit v1.2.1