Implemented the text functions on Windows.

This commit is contained in:
Pietro Gagliardi 2015-04-09 11:12:01 -04:00
parent 2b52563cd9
commit 880008b716
7 changed files with 110 additions and 2 deletions

View File

@ -93,7 +93,15 @@ uiControl *uiNewButton(const char *text)
return b->c; return b->c;
} }
// TODO text char *uiButtonText(uiControl *c)
{
return uiWindowsControlText(c);
}
void uiButtonSetText(uiControl *c, const char *text)
{
uiWindowsControlSetText(c, text);
}
void uiButtonOnClicked(uiControl *c, void (*f)(uiControl *, void *), void *data) void uiButtonOnClicked(uiControl *c, void (*f)(uiControl *, void *), void *data)
{ {

View File

@ -61,3 +61,13 @@ uiControl *uiNewEntry(void)
return e->c; return e->c;
} }
char *uiEntryText(uiControl *c)
{
return uiWindowsControlText(c);
}
void uiEntrySetText(uiControl *c, const char *text)
{
uiWindowsControlSetText(c, text);
}

View File

@ -123,3 +123,24 @@ void *uiWindowsControlData(uiControl *c)
{ {
return S(c)->data; return S(c)->data;
} }
char *uiWindowsControlText(uiControl *c)
{
WCHAR *wtext;
char *text;
wtext = windowText(S(c)->hwnd);
text = toUTF8(wtext);
uiFree(wtext);
return text;
}
void uiWindowsControlSetText(uiControl *c, const char *text)
{
WCHAR *wtext;
wtext = toUTF16(text);
if (SetWindowTextW(S(c)->hwnd, wtext) == 0)
logLastError("error setting control text in uiWindowsControlSetText()");
uiFree(wtext);
}

View File

@ -16,3 +16,46 @@ WCHAR *toUTF16(const char *str)
logLastError("error converting from UTF-8 to UTF-16 in toUTF16()"); logLastError("error converting from UTF-8 to UTF-16 in toUTF16()");
return wstr; 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);
}

View File

@ -47,4 +47,9 @@ void *uiWindowsControlData(uiControl *);
// TODO really export? // TODO really export?
extern intmax_t uiWindowsWindowTextWidth(HWND hwnd); extern intmax_t uiWindowsWindowTextWidth(HWND hwnd);
// these functions get and set the window text for such a uiControl
// the value returned should be freed with uiFreeText()
extern char *uiWindowsControlText(uiControl *);
extern void uiWindowsControlSetText(uiControl *, const char *);
#endif #endif

View File

@ -49,6 +49,8 @@ extern HWND initialParent;
// text_windows.c // text_windows.c
extern WCHAR *toUTF16(const char *); extern WCHAR *toUTF16(const char *);
extern char *toUTF8(const WCHAR *);
extern WCHAR *windowText(HWND);
// container_windows.c // container_windows.c
extern BOOL sharedWndProc(HWND, UINT, WPARAM, LPARAM, LRESULT *); extern BOOL sharedWndProc(HWND, UINT, WPARAM, LPARAM, LRESULT *);

View File

@ -116,7 +116,26 @@ uintptr_t uiWindowHandle(uiWindow *w)
return (uintptr_t) (w->hwnd); return (uintptr_t) (w->hwnd);
} }
// TODO titles char *uiWindowTitle(uiWindow *w)
{
WCHAR *wtext;
char *text;
wtext = windowText(w->hwnd);
text = toUTF8(wtext);
uiFree(wtext);
return text;
}
void uiWindowSetTitle(uiWindow *w, const char *text)
{
WCHAR *wtext;
wtext = toUTF16(text);
if (SetWindowTextW(w->hwnd, wtext) == 0)
logLastError("error setting window title in uiWindowSetTitle()");
uiFree(wtext);
}
void uiWindowShow(uiWindow *w) void uiWindowShow(uiWindow *w)
{ {