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-05-29 13:56:11 -05:00
|
|
|
void (*baseCommitDestroy)(uiControl *);
|
2015-04-07 03:02:21 -05:00
|
|
|
};
|
|
|
|
|
2015-05-29 13:56:11 -05:00
|
|
|
uiDefineControlType(uiButton, uiTypeButton, struct button)
|
|
|
|
|
2015-05-21 13:52:21 -05:00
|
|
|
static BOOL onWM_COMMAND(uiControl *c, HWND hwnd, 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-05-29 13:56:11 -05:00
|
|
|
static void buttonCommitDestroy(uiControl *c)
|
2015-04-07 23:51:50 -05:00
|
|
|
{
|
2015-05-29 13:56:11 -05:00
|
|
|
struct button *b = (struct button *) c;
|
2015-04-07 23:51:50 -05:00
|
|
|
|
2015-05-21 11:07:11 -05:00
|
|
|
uiWindowsUnregisterWM_COMMANDHandler(b->hwnd);
|
2015-05-29 13:56:11 -05:00
|
|
|
(*(b->baseCommitDestroy))(uiControl(b));
|
2015-04-07 23:51:50 -05:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
WCHAR *wtext;
|
|
|
|
|
2015-05-29 13:56:11 -05:00
|
|
|
b = (struct button *) uiWindowsNewControl(uiTypeButton());
|
2015-04-16 00:52:31 -05:00
|
|
|
|
2015-04-08 19:01:33 -05:00
|
|
|
wtext = toUTF16(text);
|
2015-05-29 13:56:11 -05:00
|
|
|
b->hwnd = uiWindowsUtilCreateControlHWND(0,
|
|
|
|
L"button", wtext,
|
|
|
|
BS_PUSHBUTTON | WS_TABSTOP,
|
|
|
|
hInstance, NULL,
|
|
|
|
TRUE);
|
2015-04-08 19:01:33 -05:00
|
|
|
uiFree(wtext);
|
2015-04-07 03:02:21 -05:00
|
|
|
|
2015-05-21 11:07:11 -05:00
|
|
|
uiWindowsRegisterWM_COMMANDHandler(b->hwnd, onWM_COMMAND, 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-05-29 13:56:11 -05:00
|
|
|
b->baseCommitDestroy = uiControl(b)->CommitDestroy;
|
|
|
|
uiControl(b)->CommitDestroy = buttonCommitDestroy;
|
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
|
|
|
}
|