SDL_SetWindowCaption() on Windows: try to use the unicode SDL-1.2
authorRyan C. Gordon <icculus@icculus.org>
Fri, 16 Feb 2007 04:56:17 +0000
branchSDL-1.2
changeset 3919d13618a935a2
parent 3918 f16c15f3bc2b
child 3920 e5a65c16913b
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
     1.1 --- a/src/video/wincommon/SDL_syswm.c	Fri Feb 16 03:50:42 2007 +0000
     1.2 +++ b/src/video/wincommon/SDL_syswm.c	Fri Feb 16 04:56:17 2007 +0000
     1.3 @@ -207,6 +207,8 @@
     1.4  #endif /* DISABLE_ICON_SUPPORT */
     1.5  }
     1.6  
     1.7 +typedef BOOL (WINAPI *PtrSetWindowTextW)(HWND hWnd, LPCWSTR lpString);
     1.8 +
     1.9  void WIN_SetWMCaption(_THIS, const char *title, const char *icon)
    1.10  {
    1.11  #ifdef _WIN32_WCE
    1.12 @@ -215,8 +217,36 @@
    1.13  	SetWindowText(SDL_Window, lpszW);
    1.14  	SDL_free(lpszW);
    1.15  #else
    1.16 -	char *lpsz = SDL_iconv_utf8_latin1((char *)title);
    1.17 -	SetWindowText(SDL_Window, lpsz);
    1.18 +	/*
    1.19 +	 * Try loading SetWindowTextW from kernel32.dll first, and if it exists,
    1.20 +	 *  pass the UCS-2 string to it. If it doesn't, use
    1.21 +	 *  WideCharToMultiByte(CP_ACP) and hope that the codepage can support the
    1.22 +	 *  string data in question. This lets us keep binary compatibility with
    1.23 +	 *  Win95/98/ME but still use saner Unicode on NT-based Windows.
    1.24 +	 */
    1.25 +	static int tried_loading = 0;
    1.26 +	static PtrSetWindowTextW swtw = NULL;
    1.27 +	Uint16 *lpsz = SDL_iconv_utf8_ucs2(title);
    1.28 +	if (!tried_loading) {
    1.29 +		HMODULE dll = LoadLibrary("user32.dll");
    1.30 +		if (dll != NULL) {
    1.31 +			swtw = (PtrSetWindowTextW) GetProcAddress(dll, "SetWindowTextW");
    1.32 +			if (swtw == NULL) {
    1.33 +				FreeLibrary(dll);
    1.34 +			}
    1.35 +		}
    1.36 +		tried_loading = 1;
    1.37 +	}
    1.38 +
    1.39 +	if (swtw != NULL) {
    1.40 +		swtw(SDL_Window, lpsz);
    1.41 +	} else {
    1.42 +		size_t len = WideCharToMultiByte(CP_ACP, 0, lpsz, -1, NULL, 0, NULL, NULL);
    1.43 +		char *cvt = SDL_malloc(len + 1);
    1.44 +		WideCharToMultiByte(CP_ACP, 0, lpsz, -1, cvt, len, NULL, NULL);
    1.45 +		SetWindowText(SDL_Window, cvt);
    1.46 +		SDL_free(cvt);
    1.47 +	}
    1.48  	SDL_free(lpsz);
    1.49  #endif
    1.50  }