2015-08-29 20:02:38 -05:00
|
|
|
// 7 april 2015
|
2016-04-22 19:14:12 -05:00
|
|
|
#include "uipriv_windows.hpp"
|
2015-08-29 20:02:38 -05:00
|
|
|
|
|
|
|
struct uiButton {
|
|
|
|
uiWindowsControl c;
|
|
|
|
HWND hwnd;
|
|
|
|
void (*onClicked)(uiButton *, void *);
|
|
|
|
void *onClickedData;
|
|
|
|
};
|
|
|
|
|
|
|
|
static BOOL onWM_COMMAND(uiControl *c, HWND hwnd, WORD code, LRESULT *lResult)
|
|
|
|
{
|
|
|
|
uiButton *b = uiButton(c);
|
|
|
|
|
|
|
|
if (code != BN_CLICKED)
|
|
|
|
return FALSE;
|
|
|
|
(*(b->onClicked))(b, b->onClickedData);
|
|
|
|
*lResult = 0;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2016-04-29 11:18:46 -05:00
|
|
|
static void uiButtonDestroy(uiControl *c)
|
|
|
|
{
|
|
|
|
uiButton *b = uiButton(c);
|
|
|
|
|
|
|
|
uiWindowsUnregisterWM_COMMANDHandler(b->hwnd);
|
|
|
|
uiWindowsEnsureDestroyWindow(b->hwnd);
|
2016-04-29 16:08:31 -05:00
|
|
|
uiFreeControl(uiControl(b));
|
2016-04-29 11:18:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
uiWindowsControlAllDefaultsExceptDestroy(uiButton)
|
|
|
|
|
2015-08-29 20:02:38 -05:00
|
|
|
// from http://msdn.microsoft.com/en-us/library/windows/desktop/dn742486.aspx#sizingandspacing
|
|
|
|
#define buttonHeight 14
|
|
|
|
|
2016-04-29 11:18:46 -05:00
|
|
|
static void uiButtonMinimumSize(uiWindowsControl *c, intmax_t *width, intmax_t *height)
|
2015-08-29 20:02:38 -05:00
|
|
|
{
|
|
|
|
uiButton *b = uiButton(c);
|
|
|
|
SIZE size;
|
2016-04-29 11:18:46 -05:00
|
|
|
uiWindowsSizing sizing;
|
|
|
|
int y;
|
2015-08-29 20:02:38 -05:00
|
|
|
|
|
|
|
// 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));
|
2016-04-29 11:18:46 -05:00
|
|
|
y = buttonHeight;
|
|
|
|
uiWindowsGetSizing(b->hwnd, &sizing);
|
|
|
|
uiWindowsSizingDlgUnitsToPixels(&sizing, NULL, &y);
|
|
|
|
*height = y;
|
2015-08-29 20:02:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void defaultOnClicked(uiButton *b, void *data)
|
|
|
|
{
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
char *uiButtonText(uiButton *b)
|
|
|
|
{
|
2016-04-22 19:04:30 -05:00
|
|
|
return uiWindowsWindowText(b->hwnd);
|
2015-08-29 20:02:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void uiButtonSetText(uiButton *b, const char *text)
|
|
|
|
{
|
2016-04-22 19:04:30 -05:00
|
|
|
uiWindowsSetWindowText(b->hwnd, text);
|
2015-08-29 20:02:38 -05:00
|
|
|
// changing the text might necessitate a change in the button's size
|
2016-04-29 11:20:41 -05:00
|
|
|
uiWindowsControlMinimumSizeChanged(uiWindowsControl(b));
|
2015-08-29 20:02:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void uiButtonOnClicked(uiButton *b, void (*f)(uiButton *, void *), void *data)
|
|
|
|
{
|
|
|
|
b->onClicked = f;
|
|
|
|
b->onClickedData = data;
|
|
|
|
}
|
|
|
|
|
|
|
|
uiButton *uiNewButton(const char *text)
|
|
|
|
{
|
|
|
|
uiButton *b;
|
|
|
|
WCHAR *wtext;
|
|
|
|
|
2016-04-29 11:18:46 -05:00
|
|
|
uiWindowsNewControl(uiButton, b);
|
2015-08-29 20:02:38 -05:00
|
|
|
|
|
|
|
wtext = toUTF16(text);
|
|
|
|
b->hwnd = uiWindowsEnsureCreateControlHWND(0,
|
|
|
|
L"button", wtext,
|
|
|
|
BS_PUSHBUTTON | WS_TABSTOP,
|
|
|
|
hInstance, NULL,
|
|
|
|
TRUE);
|
|
|
|
uiFree(wtext);
|
|
|
|
|
|
|
|
uiWindowsRegisterWM_COMMANDHandler(b->hwnd, onWM_COMMAND, uiControl(b));
|
|
|
|
uiButtonOnClicked(b, defaultOnClicked, NULL);
|
|
|
|
|
|
|
|
return b;
|
|
|
|
}
|