diff options
| author | Pietro Gagliardi <[email protected]> | 2015-04-15 18:49:45 -0400 |
|---|---|---|
| committer | Pietro Gagliardi <[email protected]> | 2015-04-15 18:49:45 -0400 |
| commit | 518a5ddbf15d50a254c732a80d5907ef8878abe0 (patch) | |
| tree | 48cf259f98994e4570e65c389fcd9824272884ad /new/windows/text.c | |
| parent | 50ae3ca045e7f5f5744043df0a4506e2f6930bb1 (diff) | |
Split all OS backends into their own folders.
Diffstat (limited to 'new/windows/text.c')
| -rw-r--r-- | new/windows/text.c | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/new/windows/text.c b/new/windows/text.c new file mode 100644 index 0000000..8d6d0b0 --- /dev/null +++ b/new/windows/text.c @@ -0,0 +1,55 @@ +// 9 april 2015 +#include "uipriv_windows.h" + +// see http://stackoverflow.com/a/29556509/3408572 + +#define MBTWC(str, wstr, bufsiz) MultiByteToWideChar(CP_UTF8, 0, str, -1, wstr, bufsiz) + +WCHAR *toUTF16(const char *str) +{ + WCHAR *wstr; + int n; + + n = MBTWC(str, NULL, 0); + if (n == 0) + logLastError("error figuring out number of characters to convert to in toUTF16()"); + wstr = (WCHAR *) uiAlloc(n * sizeof (WCHAR), "WCHAR[]"); + if (MBTWC(str, wstr, n) != n) + logLastError("error converting from UTF-8 to UTF-16 in toUTF16()"); + return wstr; +} + +#define WCTMB(wstr, str, bufsiz) WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, bufsiz, NULL, NULL) + +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()"); + str = (char *) uiAlloc(n * sizeof (char), "char[]"); + if (WCTMB(wstr, str, n) != n) + logLastError("error converting from UTF-16 to UTF-8 in toUTFF8()"); + return str; +} + +WCHAR *windowText(HWND hwnd) +{ + LRESULT n; + WCHAR *text; + + n = SendMessageW(hwnd, WM_GETTEXTLENGTH, 0, 0); + // WM_GETTEXTLENGTH does not include the null terminator + text = (WCHAR *) uiAlloc((n + 1) * sizeof (WCHAR), "WCHAR[]"); + // note the comparison: the size includes the null terminator, but the return does not + if (GetWindowTextW(hwnd, text, n + 1) != n) + logLastError("error getting window text in windowText()"); + return text; +} + +void uiFreeText(char *text) +{ + uiFree(text); +} |
