Wrote up the initial uiButton implementation. It (mostly; just needs text and for that I need to add a uiControlHandle()) works!

This commit is contained in:
Pietro Gagliardi 2015-04-07 04:02:21 -04:00
parent fecb7caa14
commit 5b36956ecb
6 changed files with 107 additions and 1 deletions

73
button_windows.c Normal file
View File

@ -0,0 +1,73 @@
// 7 april 2015
#include "uipriv_windows.h"
struct button {
uiControl *c;
void (*onClicked)(uiControl *, void *);
void *onClickedData;
};
#define B(x) ((struct button *) (x))
static BOOL onWM_COMMAND(uiControl *c, WPARAM wParam, LPARAM lParam, void *data, LRESULT *lResult)
{
if (HIWORD(wParam) != BN_CLICKED)
return FALSE;
(*(B(data)->onClicked))(c, B(data)->onClickedData);
*lResult = 0;
return TRUE;
}
static BOOL onWM_NOTIFY(uiControl *c, WPARAM wParam, LPARAM lParam, void *data, LRESULT *lResult)
{
return FALSE;
}
static void preferredSize(uiControl *c, int baseX, int baseY, LONG internalLeading, intmax_t *width, intmax_t *height)
{
// TODO
}
static void defaultOnClicked(uiControl *c, void *data)
{
// do nothing
}
uiControl *uiNewButton(const char *text)
{
struct button *b;
uiWindowsNewControlParams p;
WCHAR *wtext;
b = uiNew(struct button);
p.dwExStyle = 0;
p.lpClassName = L"button";
p.dwStyle = BS_PUSHBUTTON;
p.hInstance = hInstance;
p.onWM_COMMAND = onWM_COMMAND;
p.onWM_NOTIFY = onWM_NOTIFY;
p.onCommandNotifyData = b;
p.preferredSize = preferredSize;
p.data = b;
b->c = uiWindowsNewControl(&p);
wtext = toUTF16(text);
// TODO set text
uiFree(wtext);
b->onClicked = defaultOnClicked;
return b->c;
}
// TODO text
void uiButtonOnClicked(uiControl *c, void (*f)(uiControl *, void *), void *data)
{
struct button *b;
b = (struct button *) uiWindowsControlData(c);
b->onClicked = f;
b->onClickedData = data;
}

View File

@ -10,6 +10,7 @@ struct uiSingleHWNDControl {
BOOL (*onWM_NOTIFY)(uiControl *, WPARAM, LPARAM, void *, LRESULT *);
void *onCommandNotifyData;
void (*preferredSize)(uiControl *, int, int, LONG, intmax_t *, intmax_t *);
void *data;
};
#define S(c) ((uiSingleHWNDControl *) (c))
@ -100,8 +101,15 @@ uiControl *uiWindowsNewControl(uiWindowsNewControlParams *p)
c->onCommandNotifyData = p->onCommandNotifyData;
c->preferredSize = p->preferredSize;
c->data = p->data;
if ((*fv_SetWindowSubclass)(c->hwnd, singleSubclassProc, 0, (DWORD_PTR) c) == FALSE)
logLastError("error subclassing Windows control in uiWindowsNewControl()");
return (uiControl *) c;
}
void *uiWindowsControlData(uiControl *c)
{
return S(c)->data;
}

10
test.c
View File

@ -9,10 +9,16 @@ int onClosing(uiWindow *w, void *data)
return 1;
}
void onClicked(uiControl *b, void *data)
{
printf("button clicked!\n");
}
int main(void)
{
uiInitError *err;
uiWindow *w;
uiControl *button;
err = uiInit(NULL);
if (err != NULL) {
@ -25,6 +31,10 @@ int main(void)
uiWindowOnClosing(w, onClosing, NULL);
uiWindowShow(w);
button = uiNewButton("Click Me");
uiButtonOnClicked(button, onClicked, NULL);
uiWindowSetChild(w, button);
uiMain();
printf("after uiMain()\n");
return 0;

7
ui.h
View File

@ -15,6 +15,8 @@ void uiInitErrorFree(uiInitError *);
void uiMain(void);
void uiQuit(void);
typedef struct uiControl uiControl;
typedef struct uiWindow uiWindow;
uiWindow *uiNewWindow(char *, int, int);
void uiWindowDestroy(uiWindow *);
@ -23,7 +25,10 @@ uintptr_t uiWindowHandle(uiWindow *);
void uiWindowShow(uiWindow *);
void uiWindowHide(uiWindow *);
void uiWindowOnClosing(uiWindow *, int (*)(uiWindow *, void *), void *);
void uiWindowSetChild(uiWindow *, uiControl *);
typedef struct uiControl uiControl;
uiControl *uiNewButton(const char *);
// TODO text
void uiButtonOnClicked(uiControl *, void (*)(uiControl *, void *), void *);
#endif

View File

@ -30,8 +30,12 @@ struct uiWindowsNewControlParams {
// baseX and baseY are the base units used to convert between dialog units and pixels.
// internalLeading is the internal leading of the control font.
void (*preferredSize)(uiControl *c, int baseX, int baseY, LONG internalLeading, intmax_t *width, intmax_t *height);
// Data you can get with uiWindowsControlData()
void *data;
};
uiControl *uiWindowsNewControl(uiWindowsNewControlParams *);
void *uiWindowsControlData(uiControl *);
// use these in your preferredSize() implementation with baseX and baseY
#define uiDlgUnitToX(dlg, baseX) MulDiv((dlg), baseX, 4)

View File

@ -133,3 +133,9 @@ void uiWindowOnClosing(uiWindow *w, int (*f)(uiWindow *, void *), void *data)
w->onClosing = f;
w->onClosingData = data;
}
void uiWindowSetChild(uiWindow *w, uiControl *c)
{
w->child = c;
(*(w->child->setParent))(w->child, (uintptr_t) (w->hwnd));
}