Migrated newcontrol.c and button.c on the Windows backend.

This commit is contained in:
Pietro Gagliardi 2015-04-16 01:52:31 -04:00
parent 7b73734854
commit 6674944c0a
3 changed files with 68 additions and 67 deletions

View File

@ -11,7 +11,7 @@ This file assumes that you have included <windows.h> and "ui.h" beforehand. It p
#define uiControlHWND(c) ((HWND) uiControlHandle(c)) #define uiControlHWND(c) ((HWND) uiControlHandle(c))
#define uiParentHWND(p) ((HWND) uiParentHandle(p)) #define uiParentHWND(p) ((HWND) uiParentHandle(p))
// uiWindowsNewControl() creates a new uiControl with the given Windows API control inside. // uiWindowsNewControl() initializes the given uiControl with the given Windows API control inside.
// You will need to provide the preferredSize() method yourself. // You will need to provide the preferredSize() method yourself.
typedef struct uiWindowsNewControlParams uiWindowsNewControlParams; typedef struct uiWindowsNewControlParams uiWindowsNewControlParams;
struct uiWindowsNewControlParams { struct uiWindowsNewControlParams {
@ -34,7 +34,7 @@ struct uiWindowsNewControlParams {
// This is called in WM_DESTROY. // This is called in WM_DESTROY.
void (*onWM_DESTROY)(uiControl *c); void (*onWM_DESTROY)(uiControl *c);
}; };
uiControl *uiWindowsNewControl(uiWindowsNewControlParams *); void uiWindowsNewControl(uiControl *c, uiWindowsNewControlParams *p);
// This contains the Windows-specific parts of the uiSizing structure. // This contains the Windows-specific parts of the uiSizing structure.
// baseX and baseY are the dialog base units. // baseX and baseY are the dialog base units.

View File

@ -2,17 +2,18 @@
#include "uipriv_windows.h" #include "uipriv_windows.h"
struct button { struct button {
void (*onClicked)(uiControl *, void *); uiButton b;
void (*onClicked)(uiButton *, void *);
void *onClickedData; void *onClickedData;
}; };
static BOOL onWM_COMMAND(uiControl *c, WORD code, LRESULT *lResult) static BOOL onWM_COMMAND(uiControl *c, WORD code, LRESULT *lResult)
{ {
struct button *b = (struct button *) (c->data); struct button *b = (struct button *) c;
if (code != BN_CLICKED) if (code != BN_CLICKED)
return FALSE; return FALSE;
(*(b->onClicked))(c, b->onClickedData); (*(b->onClicked))(uiButton(b), b->onClickedData);
*lResult = 0; *lResult = 0;
return TRUE; return TRUE;
} }
@ -24,7 +25,7 @@ static BOOL onWM_NOTIFY(uiControl *c, NMHDR *nm, LRESULT *lResult)
static void onWM_DESTROY(uiControl *c) static void onWM_DESTROY(uiControl *c)
{ {
struct button *b = (struct button *) (c->data); struct button *b = (struct button *) c;
uiFree(b); uiFree(b);
} }
@ -55,18 +56,37 @@ static void preferredSize(uiControl *c, uiSizing *d, intmax_t *width, intmax_t *
*height = uiDlgUnitsToY(buttonHeight, d->sys->baseY); *height = uiDlgUnitsToY(buttonHeight, d->sys->baseY);
} }
static void defaultOnClicked(uiControl *c, void *data) static void defaultOnClicked(uiButton *b, void *data)
{ {
// do nothing // do nothing
} }
uiControl *uiNewButton(const char *text) static char *getText(uiButton *b)
{
return uiWindowsControlText(uiControl(c));
}
static void setText(uiButton *b, const char *text)
{
uiWindowsControlSetText(uiControl(b), text);
}
static void setOnClicked(uiButton *b, void (*f)(uiButton *, void *), void *data)
{
struct button *b = (struct button *) b;
b->onClicked = f;
b->onClickedData = data;
}
uiButton *uiNewButton(const char *text)
{ {
uiControl *c;
struct button *b; struct button *b;
uiWindowsNewControlParams p; uiWindowsNewControlParams p;
WCHAR *wtext; WCHAR *wtext;
b = uiNew(struct button);
p.dwExStyle = 0; p.dwExStyle = 0;
p.lpClassName = L"button"; p.lpClassName = L"button";
wtext = toUTF16(text); wtext = toUTF16(text);
@ -77,32 +97,16 @@ uiControl *uiNewButton(const char *text)
p.onWM_COMMAND = onWM_COMMAND; p.onWM_COMMAND = onWM_COMMAND;
p.onWM_NOTIFY = onWM_NOTIFY; p.onWM_NOTIFY = onWM_NOTIFY;
p.onWM_DESTROY = onWM_DESTROY; p.onWM_DESTROY = onWM_DESTROY;
c = uiWindowsNewControl(&p); uiWindowsNewControl(uiControl(b), &p);
uiFree(wtext); uiFree(wtext);
c->preferredSize = preferredSize;
b = uiNew(struct button);
b->onClicked = defaultOnClicked; b->onClicked = defaultOnClicked;
c->data = b;
return c; uiControl(b)->preferredSize = preferredSize;
}
uiButton(b)->Text = getText;
char *uiButtonText(uiControl *c) uiButton(b)->SetText = setText;
{ uiButton(b)->OnClicked = setOnClicked;
return uiWindowsControlText(c);
} return uiButton(b);
void uiButtonSetText(uiControl *c, const char *text)
{
uiWindowsControlSetText(c, text);
}
void uiButtonOnClicked(uiControl *c, void (*f)(uiControl *, void *), void *data)
{
struct button *b = (struct button *) (c->data);
b->onClicked = f;
b->onClickedData = data;
} }

View File

@ -17,7 +17,7 @@ struct singleHWND {
static void singleDestroy(uiControl *c) static void singleDestroy(uiControl *c)
{ {
singleHWND *s = (singleHWND *) (c->internal); singleHWND *s = (singleHWND *) (c->Internal);
if (DestroyWindow(s->hwnd) == 0) if (DestroyWindow(s->hwnd) == 0)
logLastError("error destroying control in singleDestroy()"); logLastError("error destroying control in singleDestroy()");
@ -26,14 +26,14 @@ static void singleDestroy(uiControl *c)
static uintptr_t singleHandle(uiControl *c) static uintptr_t singleHandle(uiControl *c)
{ {
singleHWND *s = (singleHWND *) (c->internal); singleHWND *s = (singleHWND *) (c->Internal);
return (uintptr_t) (s->hwnd); return (uintptr_t) (s->hwnd);
} }
static void singleSetParent(uiControl *c, uiParent *parent) static void singleSetParent(uiControl *c, uiParent *parent)
{ {
singleHWND *s = (singleHWND *) (c->internal); singleHWND *s = (singleHWND *) (c->Internal);
uiParent *oldparent; uiParent *oldparent;
HWND newParentHWND; HWND newParentHWND;
@ -52,7 +52,7 @@ static void singleSetParent(uiControl *c, uiParent *parent)
static void singleResize(uiControl *c, intmax_t x, intmax_t y, intmax_t width, intmax_t height, uiSizing *d) static void singleResize(uiControl *c, intmax_t x, intmax_t y, intmax_t width, intmax_t height, uiSizing *d)
{ {
singleHWND *s = (singleHWND *) (c->internal); singleHWND *s = (singleHWND *) (c->Internal);
if (MoveWindow(s->hwnd, x, y, width, height, TRUE) == 0) if (MoveWindow(s->hwnd, x, y, width, height, TRUE) == 0)
logLastError("error moving control in singleResize()"); logLastError("error moving control in singleResize()");
@ -60,7 +60,7 @@ static void singleResize(uiControl *c, intmax_t x, intmax_t y, intmax_t width, i
static int singleVisible(uiControl *c) static int singleVisible(uiControl *c)
{ {
singleHWND *s = (singleHWND *) (c->internal); singleHWND *s = (singleHWND *) (c->Internal);
if (s->userHid) if (s->userHid)
return 0; return 0;
@ -69,7 +69,7 @@ static int singleVisible(uiControl *c)
static void singleShow(uiControl *c) static void singleShow(uiControl *c)
{ {
singleHWND *s = (singleHWND *) (c->internal); singleHWND *s = (singleHWND *) (c->Internal);
s->userHid = FALSE; s->userHid = FALSE;
if (!s->containerHid) { if (!s->containerHid) {
@ -81,7 +81,7 @@ static void singleShow(uiControl *c)
static void singleHide(uiControl *c) static void singleHide(uiControl *c)
{ {
singleHWND *s = (singleHWND *) (c->internal); singleHWND *s = (singleHWND *) (c->Internal);
s->userHid = TRUE; s->userHid = TRUE;
ShowWindow(s->hwnd, SW_HIDE); ShowWindow(s->hwnd, SW_HIDE);
@ -91,7 +91,7 @@ static void singleHide(uiControl *c)
static void singleContainerShow(uiControl *c) static void singleContainerShow(uiControl *c)
{ {
singleHWND *s = (singleHWND *) (c->internal); singleHWND *s = (singleHWND *) (c->Internal);
s->containerHid = FALSE; s->containerHid = FALSE;
if (!s->userHid) { if (!s->userHid) {
@ -103,7 +103,7 @@ static void singleContainerShow(uiControl *c)
static void singleContainerHide(uiControl *c) static void singleContainerHide(uiControl *c)
{ {
singleHWND *s = (singleHWND *) (c->internal); singleHWND *s = (singleHWND *) (c->Internal);
s->containerHid = TRUE; s->containerHid = TRUE;
ShowWindow(s->hwnd, SW_HIDE); ShowWindow(s->hwnd, SW_HIDE);
@ -113,7 +113,7 @@ static void singleContainerHide(uiControl *c)
static void singleEnable(uiControl *c) static void singleEnable(uiControl *c)
{ {
singleHWND *s = (singleHWND *) (c->internal); singleHWND *s = (singleHWND *) (c->Internal);
s->userDisabled = FALSE; s->userDisabled = FALSE;
if (!s->containerDisabled) if (!s->containerDisabled)
@ -122,7 +122,7 @@ static void singleEnable(uiControl *c)
static void singleDisable(uiControl *c) static void singleDisable(uiControl *c)
{ {
singleHWND *s = (singleHWND *) (c->internal); singleHWND *s = (singleHWND *) (c->Internal);
s->userDisabled = TRUE; s->userDisabled = TRUE;
EnableWindow(s->hwnd, FALSE); EnableWindow(s->hwnd, FALSE);
@ -130,7 +130,7 @@ static void singleDisable(uiControl *c)
static void singleContainerEnable(uiControl *c) static void singleContainerEnable(uiControl *c)
{ {
singleHWND *s = (singleHWND *) (c->internal); singleHWND *s = (singleHWND *) (c->Internal);
s->containerDisabled = FALSE; s->containerDisabled = FALSE;
if (!s->userDisabled) if (!s->userDisabled)
@ -139,7 +139,7 @@ static void singleContainerEnable(uiControl *c)
static void singleContainerDisable(uiControl *c) static void singleContainerDisable(uiControl *c)
{ {
singleHWND *s = (singleHWND *) (c->internal); singleHWND *s = (singleHWND *) (c->Internal);
s->containerDisabled = TRUE; s->containerDisabled = TRUE;
EnableWindow(s->hwnd, FALSE); EnableWindow(s->hwnd, FALSE);
@ -148,7 +148,7 @@ static void singleContainerDisable(uiControl *c)
static LRESULT CALLBACK singleSubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData) static LRESULT CALLBACK singleSubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData)
{ {
uiControl *c = (uiControl *) dwRefData; uiControl *c = (uiControl *) dwRefData;
singleHWND *s = (singleHWND *) (c->internal); singleHWND *s = (singleHWND *) (c->Internal);
LRESULT lResult; LRESULT lResult;
switch (uMsg) { switch (uMsg) {
@ -173,9 +173,8 @@ static LRESULT CALLBACK singleSubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam,
return (*fv_DefSubclassProc)(hwnd, uMsg, wParam, lParam); return (*fv_DefSubclassProc)(hwnd, uMsg, wParam, lParam);
} }
uiControl *uiWindowsNewControl(uiWindowsNewControlParams *p) void uiWindowsNewControl(uiControl *c, uiWindowsNewControlParams *p)
{ {
uiControl *c;
singleHWND *s; singleHWND *s;
s = uiNew(singleHWND); s = uiNew(singleHWND);
@ -192,20 +191,19 @@ uiControl *uiWindowsNewControl(uiWindowsNewControlParams *p)
s->onWM_NOTIFY = p->onWM_NOTIFY; s->onWM_NOTIFY = p->onWM_NOTIFY;
s->onWM_DESTROY = p->onWM_DESTROY; s->onWM_DESTROY = p->onWM_DESTROY;
c = uiNew(uiControl); c->Destroy = singleDestroy;
c->destroy = singleDestroy; c->Handle = singleHandle;
c->handle = singleHandle; c->SetParent = singleSetParent;
c->setParent = singleSetParent; c->Resize = singleResize;
c->resize = singleResize; c->Visible = singleVisible;
c->visible = singleVisible; c->Show = singleShow;
c->show = singleShow; c->Hide = singleHide;
c->hide = singleHide; c->ContainerShow = singleContainerShow;
c->containerShow = singleContainerShow; c->ContainerHide = singleContainerHide;
c->containerHide = singleContainerHide; c->Enable = singleEnable;
c->enable = singleEnable; c->Disable = singleDisable;
c->disable = singleDisable; c->ContainerEnable = singleContainerEnable;
c->containerEnable = singleContainerEnable; c->ContainerDisable = singleContainerDisable;
c->containerDisable = singleContainerDisable;
if (p->useStandardControlFont) if (p->useStandardControlFont)
SendMessageW(s->hwnd, WM_SETFONT, (WPARAM) hMessageFont, (LPARAM) TRUE); SendMessageW(s->hwnd, WM_SETFONT, (WPARAM) hMessageFont, (LPARAM) TRUE);
@ -213,13 +211,12 @@ uiControl *uiWindowsNewControl(uiWindowsNewControlParams *p)
if ((*fv_SetWindowSubclass)(s->hwnd, singleSubclassProc, 0, (DWORD_PTR) c) == FALSE) if ((*fv_SetWindowSubclass)(s->hwnd, singleSubclassProc, 0, (DWORD_PTR) c) == FALSE)
logLastError("error subclassing Windows control in uiWindowsNewControl()"); logLastError("error subclassing Windows control in uiWindowsNewControl()");
c->internal = s; c->Internal = s;
return c;
} }
char *uiWindowsControlText(uiControl *c) char *uiWindowsControlText(uiControl *c)
{ {
singleHWND *s = (singleHWND *) (c->internal); singleHWND *s = (singleHWND *) (c->Internal);
WCHAR *wtext; WCHAR *wtext;
char *text; char *text;
@ -231,7 +228,7 @@ char *uiWindowsControlText(uiControl *c)
void uiWindowsControlSetText(uiControl *c, const char *text) void uiWindowsControlSetText(uiControl *c, const char *text)
{ {
singleHWND *s = (singleHWND *) (c->internal); singleHWND *s = (singleHWND *) (c->Internal);
WCHAR *wtext; WCHAR *wtext;
wtext = toUTF16(text); wtext = toUTF16(text);