libui/windows/button.c

115 lines
2.5 KiB
C
Raw Normal View History

// 7 april 2015
#include "uipriv_windows.h"
struct button {
uiButton b;
HWND hwnd;
void (*onClicked)(uiButton *, void *);
void *onClickedData;
};
static BOOL onWM_COMMAND(uiControl *c, WORD code, LRESULT *lResult)
{
struct button *b = (struct button *) c;
2015-04-09 16:46:26 -05:00
if (code != BN_CLICKED)
return FALSE;
(*(b->onClicked))(uiButton(b), b->onClickedData);
*lResult = 0;
return TRUE;
}
static BOOL onWM_NOTIFY(uiControl *c, NMHDR *nm, LRESULT *lResult)
{
return FALSE;
}
static void onDestroy(void *data)
{
struct button *b = (struct button *) data;
uiFree(b);
}
// from http://msdn.microsoft.com/en-us/library/windows/desktop/dn742486.aspx#sizingandspacing
#define buttonHeight 14
2015-04-09 16:46:26 -05:00
static void preferredSize(uiControl *c, uiSizing *d, intmax_t *width, intmax_t *height)
{
struct button *b = (struct button *) c;
SIZE size;
// try the comctl32 version 6 way
size.cx = 0; // explicitly ask for ideal size
size.cy = 0;
if (SendMessageW(b->hwnd, BCM_GETIDEALSIZE, 0, (LPARAM) (&size)) != FALSE) {
*width = size.cx;
*height = size.cy;
return;
}
// that didn't work; fall back to using Microsoft's metrics
// Microsoft says to use a fixed width for all buttons; this isn't good enough
// use the text width instead, with some edge padding
*width = uiWindowsWindowTextWidth(b->hwnd) + (2 * GetSystemMetrics(SM_CXEDGE));
2015-04-09 17:45:58 -05:00
*height = uiDlgUnitsToY(buttonHeight, d->sys->baseY);
}
static void defaultOnClicked(uiButton *b, void *data)
{
// do nothing
}
static char *buttonText(uiButton *b)
{
return uiWindowsControlText(uiControl(b));
}
static void buttonSetText(uiButton *b, const char *text)
{
uiWindowsControlSetText(uiControl(b), text);
}
static void buttonOnClicked(uiButton *bb, void (*f)(uiButton *, void *), void *data)
{
struct button *b = (struct button *) bb;
b->onClicked = f;
b->onClickedData = data;
}
uiButton *uiNewButton(const char *text)
{
struct button *b;
uiWindowsNewControlParams p;
WCHAR *wtext;
b = uiNew(struct button);
p.dwExStyle = 0;
p.lpClassName = L"button";
2015-04-08 19:01:33 -05:00
wtext = toUTF16(text);
p.lpWindowName = wtext;
p.dwStyle = BS_PUSHBUTTON | WS_TABSTOP;
p.hInstance = hInstance;
p.useStandardControlFont = TRUE;
p.onWM_COMMAND = onWM_COMMAND;
p.onWM_NOTIFY = onWM_NOTIFY;
p.onDestroy = onDestroy;
p.onDestroyData = b;
uiWindowsNewControl(uiControl(b), &p);
2015-04-08 19:01:33 -05:00
uiFree(wtext);
b->hwnd = HWND(b);
b->onClicked = defaultOnClicked;
uiControl(b)->PreferredSize = preferredSize;
uiButton(b)->Text = buttonText;
uiButton(b)->SetText = buttonSetText;
uiButton(b)->OnClicked = buttonOnClicked;
return uiButton(b);
}