From 79a7e18b8da55b4bcec72cc66f9b4e07878e7eee Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Thu, 9 Apr 2015 11:12:01 -0400 Subject: Implemented the text functions on Windows. --- new/text_windows.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'new/text_windows.c') diff --git a/new/text_windows.c b/new/text_windows.c index e483a95..4f5b741 100644 --- a/new/text_windows.c +++ b/new/text_windows.c @@ -16,3 +16,46 @@ WCHAR *toUTF16(const char *str) logLastError("error converting from UTF-8 to UTF-16 in toUTF16()"); return wstr; } + +#define WCTMB(wstr, str, bufsiz) WideCharToMultiByte(CP_UTF8, WC_NO_BEST_FIT_CHARS, wstr, -1, str, bufsiz, NULL, FALSE) + +char *toUTF8(const WCHAR *wstr) +{ + char *str; + int n; + + n = WCTMB(wstr, NULL, 0); + if (n == 0) + logLastError("error figuring out number of characters to convert to in toUTF8()"); + // TODO does n include the null terminator? + str = (char *) uiAlloc((n + 1) * sizeof (char), "char[]"); + if (WCTMB(wstr, str, n + 1) != n) + logLastError("error converting from UTF-16 to UTF-8 in toUTFF8()"); + return str; +} + +WCHAR *windowText(HWND hwnd) +{ + int n; + WCHAR *text; + DWORD le; + + SetLastError(0); + n = GetWindowTextLengthW(hwnd); + if (n == 0) { + le = GetLastError(); + SetLastError(le); // just in case + if (le != 0) + logLastError("error getting window text length in windowText()"); + } + // TODO null terminator? + text = (WCHAR *) uiAlloc((n + 1) * sizeof (WCHAR), "WCHAR[]"); + if (GetWindowTextW(hwnd, text, n + 1) != n) + logLastError("error getting window text in windowText()"); + return text; +} + +void uiFreeText(char *text) +{ + uiFree(text); +} -- cgit v1.2.3