2015-04-07 03:02:21 -05:00
|
|
|
// 7 april 2015
|
|
|
|
#include "uipriv_windows.h"
|
|
|
|
|
|
|
|
struct button {
|
2015-04-16 00:52:31 -05:00
|
|
|
uiButton b;
|
2015-04-17 09:40:52 -05:00
|
|
|
HWND hwnd;
|
2015-04-16 00:52:31 -05:00
|
|
|
void (*onClicked)(uiButton *, void *);
|
2015-04-07 03:02:21 -05:00
|
|
|
void *onClickedData;
|
|
|
|
};
|
|
|
|
|
2015-04-09 17:54:14 -05:00
|
|
|
static BOOL onWM_COMMAND(uiControl *c, WORD code, LRESULT *lResult)
|
2015-04-07 03:02:21 -05:00
|
|
|
{
|
2015-04-16 00:52:31 -05:00
|
|
|
struct button *b = (struct button *) c;
|
2015-04-09 16:46:26 -05:00
|
|
|
|
2015-04-09 17:54:14 -05:00
|
|
|
if (code != BN_CLICKED)
|
2015-04-07 03:02:21 -05:00
|
|
|
return FALSE;
|
2015-04-16 00:52:31 -05:00
|
|
|
(*(b->onClicked))(uiButton(b), b->onClickedData);
|
2015-04-07 03:02:21 -05:00
|
|
|
*lResult = 0;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2015-04-09 18:07:41 -05:00
|
|
|
static BOOL onWM_NOTIFY(uiControl *c, NMHDR *nm, LRESULT *lResult)
|
2015-04-07 03:02:21 -05:00
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2015-04-09 16:46:26 -05:00
|
|
|
static void onWM_DESTROY(uiControl *c)
|
2015-04-07 23:51:50 -05:00
|
|
|
{
|
2015-04-16 00:52:31 -05:00
|
|
|
struct button *b = (struct button *) c;
|
2015-04-07 23:51:50 -05:00
|
|
|
|
|
|
|
uiFree(b);
|
|
|
|
}
|
|
|
|
|
2015-04-07 18:32:16 -05:00
|
|
|
// 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)
|
2015-04-07 03:02:21 -05:00
|
|
|
{
|
2015-04-17 09:40:52 -05:00
|
|
|
struct button *b = (struct button *) c;
|
2015-04-07 18:32:16 -05:00
|
|
|
SIZE size;
|
|
|
|
|
|
|
|
// try the comctl32 version 6 way
|
|
|
|
size.cx = 0; // explicitly ask for ideal size
|
|
|
|
size.cy = 0;
|
2015-04-17 09:40:52 -05:00
|
|
|
if (SendMessageW(b->hwnd, BCM_GETIDEALSIZE, 0, (LPARAM) (&size)) != FALSE) {
|
2015-04-07 18:32:16 -05:00
|
|
|
*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
|
2015-04-17 09:40:52 -05:00
|
|
|
*width = uiWindowsWindowTextWidth(b->hwnd) + (2 * GetSystemMetrics(SM_CXEDGE));
|
2015-04-09 17:45:58 -05:00
|
|
|
*height = uiDlgUnitsToY(buttonHeight, d->sys->baseY);
|
2015-04-07 03:02:21 -05:00
|
|
|
}
|
|
|
|
|
2015-04-16 00:52:31 -05:00
|
|
|
static void defaultOnClicked(uiButton *b, void *data)
|
2015-04-07 03:02:21 -05:00
|
|
|
{
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
2015-04-17 09:40:52 -05:00
|
|
|
static char *buttonText(uiButton *b)
|
2015-04-16 00:52:31 -05:00
|
|
|
{
|
2015-04-16 08:53:00 -05:00
|
|
|
return uiWindowsControlText(uiControl(b));
|
2015-04-16 00:52:31 -05:00
|
|
|
}
|
|
|
|
|
2015-04-17 09:40:52 -05:00
|
|
|
static void buttonSetText(uiButton *b, const char *text)
|
2015-04-16 00:52:31 -05:00
|
|
|
{
|
|
|
|
uiWindowsControlSetText(uiControl(b), text);
|
|
|
|
}
|
|
|
|
|
2015-04-17 09:40:52 -05:00
|
|
|
static void buttonOnClicked(uiButton *bb, void (*f)(uiButton *, void *), void *data)
|
2015-04-16 00:52:31 -05:00
|
|
|
{
|
2015-04-16 08:53:00 -05:00
|
|
|
struct button *b = (struct button *) bb;
|
2015-04-16 00:52:31 -05:00
|
|
|
|
|
|
|
b->onClicked = f;
|
|
|
|
b->onClickedData = data;
|
|
|
|
}
|
|
|
|
|
|
|
|
uiButton *uiNewButton(const char *text)
|
2015-04-07 03:02:21 -05:00
|
|
|
{
|
|
|
|
struct button *b;
|
|
|
|
uiWindowsNewControlParams p;
|
|
|
|
WCHAR *wtext;
|
|
|
|
|
2015-04-16 00:52:31 -05:00
|
|
|
b = uiNew(struct button);
|
|
|
|
|
2015-04-07 03:02:21 -05:00
|
|
|
p.dwExStyle = 0;
|
|
|
|
p.lpClassName = L"button";
|
2015-04-08 19:01:33 -05:00
|
|
|
wtext = toUTF16(text);
|
|
|
|
p.lpWindowName = wtext;
|
2015-04-07 21:23:25 -05:00
|
|
|
p.dwStyle = BS_PUSHBUTTON | WS_TABSTOP;
|
2015-04-07 03:02:21 -05:00
|
|
|
p.hInstance = hInstance;
|
2015-04-10 16:53:59 -05:00
|
|
|
p.useStandardControlFont = TRUE;
|
2015-04-07 03:02:21 -05:00
|
|
|
p.onWM_COMMAND = onWM_COMMAND;
|
|
|
|
p.onWM_NOTIFY = onWM_NOTIFY;
|
2015-04-07 23:51:50 -05:00
|
|
|
p.onWM_DESTROY = onWM_DESTROY;
|
2015-04-16 00:52:31 -05:00
|
|
|
uiWindowsNewControl(uiControl(b), &p);
|
2015-04-08 19:01:33 -05:00
|
|
|
uiFree(wtext);
|
2015-04-07 03:02:21 -05:00
|
|
|
|
2015-04-17 09:40:52 -05:00
|
|
|
b->hwnd = HWND(b);
|
|
|
|
|
2015-04-07 03:02:21 -05:00
|
|
|
b->onClicked = defaultOnClicked;
|
|
|
|
|
2015-04-16 08:08:38 -05:00
|
|
|
uiControl(b)->PreferredSize = preferredSize;
|
2015-04-07 03:02:21 -05:00
|
|
|
|
2015-04-17 09:40:52 -05:00
|
|
|
uiButton(b)->Text = buttonText;
|
|
|
|
uiButton(b)->SetText = buttonSetText;
|
|
|
|
uiButton(b)->OnClicked = buttonOnClicked;
|
2015-04-09 10:12:01 -05:00
|
|
|
|
2015-04-16 00:52:31 -05:00
|
|
|
return uiButton(b);
|
2015-04-07 03:02:21 -05:00
|
|
|
}
|