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-18 17:02:16 -05:00
|
|
|
static void onDestroy(void *data)
|
2015-04-07 23:51:50 -05:00
|
|
|
{
|
2015-04-18 17:02:16 -05:00
|
|
|
struct button *b = (struct button *) data;
|
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-05-04 13:56:26 -05:00
|
|
|
static void buttonPreferredSize(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-05-07 13:33:46 -05:00
|
|
|
*height = uiWindowsDlgUnitsToY(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;
|
2015-05-02 19:51:00 -05:00
|
|
|
uiWindowsMakeControlParams p;
|
2015-04-07 03:02:21 -05:00
|
|
|
WCHAR *wtext;
|
|
|
|
|
2015-04-16 00:52:31 -05:00
|
|
|
b = uiNew(struct button);
|
2015-05-17 18:07:07 -05:00
|
|
|
uiTyped(b)->Type = uiTypeButton();
|
2015-04-16 00:52:31 -05:00
|
|
|
|
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-05-15 15:14:22 -05:00
|
|
|
p.lpParam = NULL;
|
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-18 17:02:16 -05:00
|
|
|
p.onDestroy = onDestroy;
|
|
|
|
p.onDestroyData = b;
|
2015-05-02 19:51:00 -05:00
|
|
|
uiWindowsMakeControl(uiControl(b), &p);
|
2015-04-08 19:01:33 -05:00
|
|
|
uiFree(wtext);
|
2015-04-07 03:02:21 -05:00
|
|
|
|
2015-04-29 09:46:09 -05:00
|
|
|
b->hwnd = (HWND) uiControlHandle(uiControl(b));
|
2015-04-17 09:40:52 -05:00
|
|
|
|
2015-04-07 03:02:21 -05:00
|
|
|
b->onClicked = defaultOnClicked;
|
|
|
|
|
2015-05-04 13:56:26 -05:00
|
|
|
uiControl(b)->PreferredSize = buttonPreferredSize;
|
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
|
|
|
}
|