From 178e9756d7abb6c4f2b416b0075d877cea8c91d8 Mon Sep 17 00:00:00 2001 From: "Ryan C. Gordon" Date: Fri, 16 Feb 2007 04:56:17 +0000 Subject: [PATCH] SDL_SetWindowCaption() on Windows: try to use the unicode SetWindowText() if it exists in user32.dll, and fallback to converting to the current codepage (instead of latin1) if not. Fixes Bugzilla #365. --- src/video/wincommon/SDL_syswm.c | 34 +++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/src/video/wincommon/SDL_syswm.c b/src/video/wincommon/SDL_syswm.c index e1106ac84..8bd2c1880 100644 --- a/src/video/wincommon/SDL_syswm.c +++ b/src/video/wincommon/SDL_syswm.c @@ -207,6 +207,8 @@ void WIN_SetWMIcon(_THIS, SDL_Surface *icon, Uint8 *mask) #endif /* DISABLE_ICON_SUPPORT */ } +typedef BOOL (WINAPI *PtrSetWindowTextW)(HWND hWnd, LPCWSTR lpString); + void WIN_SetWMCaption(_THIS, const char *title, const char *icon) { #ifdef _WIN32_WCE @@ -215,8 +217,36 @@ void WIN_SetWMCaption(_THIS, const char *title, const char *icon) SetWindowText(SDL_Window, lpszW); SDL_free(lpszW); #else - char *lpsz = SDL_iconv_utf8_latin1((char *)title); - SetWindowText(SDL_Window, lpsz); + /* + * Try loading SetWindowTextW from kernel32.dll first, and if it exists, + * pass the UCS-2 string to it. If it doesn't, use + * WideCharToMultiByte(CP_ACP) and hope that the codepage can support the + * string data in question. This lets us keep binary compatibility with + * Win95/98/ME but still use saner Unicode on NT-based Windows. + */ + static int tried_loading = 0; + static PtrSetWindowTextW swtw = NULL; + Uint16 *lpsz = SDL_iconv_utf8_ucs2(title); + if (!tried_loading) { + HMODULE dll = LoadLibrary("user32.dll"); + if (dll != NULL) { + swtw = (PtrSetWindowTextW) GetProcAddress(dll, "SetWindowTextW"); + if (swtw == NULL) { + FreeLibrary(dll); + } + } + tried_loading = 1; + } + + if (swtw != NULL) { + swtw(SDL_Window, lpsz); + } else { + size_t len = WideCharToMultiByte(CP_ACP, 0, lpsz, -1, NULL, 0, NULL, NULL); + char *cvt = SDL_malloc(len + 1); + WideCharToMultiByte(CP_ACP, 0, lpsz, -1, cvt, len, NULL, NULL); + SetWindowText(SDL_Window, cvt); + SDL_free(cvt); + } SDL_free(lpsz); #endif }