2015-04-07 03:02:21 -05:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2015-04-07 23:51:50 -05:00
|
|
|
static void onWM_DESTROY(uiControl *c, void *data)
|
|
|
|
{
|
|
|
|
struct button *b = (struct button *) data;
|
|
|
|
|
|
|
|
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-07 03:02:21 -05:00
|
|
|
static void preferredSize(uiControl *c, int baseX, int baseY, LONG internalLeading, intmax_t *width, intmax_t *height)
|
|
|
|
{
|
2015-04-07 18:32:16 -05:00
|
|
|
HWND hwnd;
|
|
|
|
SIZE size;
|
|
|
|
|
|
|
|
hwnd = (HWND) uiControlHandle(c);
|
|
|
|
|
|
|
|
// try the comctl32 version 6 way
|
|
|
|
size.cx = 0; // explicitly ask for ideal size
|
|
|
|
size.cy = 0;
|
|
|
|
if (SendMessageW(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(hwnd) + (2 * GetSystemMetrics(SM_CXEDGE));
|
|
|
|
*height = uiDlgUnitToY(buttonHeight, baseY);
|
2015-04-07 03:02:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void defaultOnClicked(uiControl *c, void *data)
|
|
|
|
{
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
uiControl *uiNewButton(const char *text)
|
|
|
|
{
|
|
|
|
struct button *b;
|
|
|
|
uiWindowsNewControlParams p;
|
|
|
|
WCHAR *wtext;
|
2015-04-07 03:12:03 -05:00
|
|
|
HWND hwnd;
|
2015-04-07 03:02:21 -05:00
|
|
|
|
|
|
|
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;
|
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;
|
|
|
|
p.onWM_COMMAND = onWM_COMMAND;
|
|
|
|
p.onWM_NOTIFY = onWM_NOTIFY;
|
2015-04-07 23:51:50 -05:00
|
|
|
p.onWM_DESTROY = onWM_DESTROY;
|
|
|
|
p.onCommandNotifyDestroyData = b;
|
2015-04-07 03:02:21 -05:00
|
|
|
p.preferredSize = preferredSize;
|
|
|
|
p.data = b;
|
|
|
|
b->c = uiWindowsNewControl(&p);
|
2015-04-08 19:01:33 -05:00
|
|
|
uiFree(wtext);
|
2015-04-07 03:02:21 -05:00
|
|
|
|
2015-04-07 03:12:03 -05:00
|
|
|
hwnd = (HWND) uiControlHandle(b->c);
|
|
|
|
SendMessageW(hwnd, WM_SETFONT, (WPARAM) hMessageFont, (LPARAM) TRUE);
|
2015-04-07 03:02:21 -05:00
|
|
|
|
|
|
|
b->onClicked = defaultOnClicked;
|
|
|
|
|
|
|
|
return b->c;
|
|
|
|
}
|
|
|
|
|
2015-04-09 10:12:01 -05:00
|
|
|
char *uiButtonText(uiControl *c)
|
|
|
|
{
|
|
|
|
return uiWindowsControlText(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
void uiButtonSetText(uiControl *c, const char *text)
|
|
|
|
{
|
|
|
|
uiWindowsControlSetText(c, text);
|
|
|
|
}
|
2015-04-07 03:02:21 -05:00
|
|
|
|
|
|
|
void uiButtonOnClicked(uiControl *c, void (*f)(uiControl *, void *), void *data)
|
|
|
|
{
|
|
|
|
struct button *b;
|
|
|
|
|
|
|
|
b = (struct button *) uiWindowsControlData(c);
|
|
|
|
b->onClicked = f;
|
|
|
|
b->onClickedData = data;
|
|
|
|
}
|