2015-08-30 11:25:53 -05:00
|
|
|
// 7 april 2015
|
2016-04-22 19:14:12 -05:00
|
|
|
#include "uipriv_windows.hpp"
|
2015-08-30 11:25:53 -05:00
|
|
|
|
|
|
|
struct uiCheckbox {
|
|
|
|
uiWindowsControl c;
|
|
|
|
HWND hwnd;
|
|
|
|
void (*onToggled)(uiCheckbox *, void *);
|
|
|
|
void *onToggledData;
|
|
|
|
};
|
|
|
|
|
|
|
|
static BOOL onWM_COMMAND(uiControl *cc, HWND hwnd, WORD code, LRESULT *lResult)
|
|
|
|
{
|
|
|
|
uiCheckbox *c = uiCheckbox(cc);
|
|
|
|
WPARAM check;
|
|
|
|
|
|
|
|
if (code != BN_CLICKED)
|
|
|
|
return FALSE;
|
|
|
|
|
2015-09-01 15:10:29 -05:00
|
|
|
// we didn't use BS_AUTOCHECKBOX (http://blogs.msdn.com/b/oldnewthing/archive/2014/05/22/10527522.aspx) so we have to manage the check state ourselves
|
2015-08-30 11:25:53 -05:00
|
|
|
check = BST_CHECKED;
|
|
|
|
if (SendMessage(c->hwnd, BM_GETCHECK, 0, 0) == BST_CHECKED)
|
|
|
|
check = BST_UNCHECKED;
|
|
|
|
SendMessage(c->hwnd, BM_SETCHECK, check, 0);
|
|
|
|
|
|
|
|
(*(c->onToggled))(c, c->onToggledData);
|
|
|
|
*lResult = 0;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2016-04-29 11:49:42 -05:00
|
|
|
static void uiCheckboxDestroy(uiControl *cc)
|
|
|
|
{
|
|
|
|
uiCheckbox *c = uiCheckbox(cc);
|
|
|
|
|
|
|
|
uiWindowsUnregisterWM_COMMANDHandler(c->hwnd);
|
2016-04-29 16:08:31 -05:00
|
|
|
uiWindowsEnsureDestroyWindow(c->hwnd);
|
2016-04-29 11:49:42 -05:00
|
|
|
uiFreeControl(uiControl(c));
|
|
|
|
}
|
|
|
|
|
|
|
|
uiWindowsControlAllDefaultsExceptDestroy(uiCheckbox)
|
|
|
|
|
2015-08-30 11:25:53 -05:00
|
|
|
// from http://msdn.microsoft.com/en-us/library/windows/desktop/dn742486.aspx#sizingandspacing
|
|
|
|
#define checkboxHeight 10
|
|
|
|
// from http://msdn.microsoft.com/en-us/library/windows/desktop/bb226818%28v=vs.85%29.aspx
|
|
|
|
#define checkboxXFromLeftOfBoxToLeftOfLabel 12
|
|
|
|
|
2016-06-13 20:54:15 -05:00
|
|
|
static void uiCheckboxMinimumSize(uiWindowsControl *cc, int *width, int *height)
|
2015-08-30 11:25:53 -05:00
|
|
|
{
|
|
|
|
uiCheckbox *c = uiCheckbox(cc);
|
2016-04-29 11:49:42 -05:00
|
|
|
uiWindowsSizing sizing;
|
|
|
|
int x, y;
|
|
|
|
|
|
|
|
x = checkboxXFromLeftOfBoxToLeftOfLabel;
|
|
|
|
y = checkboxHeight;
|
|
|
|
uiWindowsGetSizing(c->hwnd, &sizing);
|
|
|
|
uiWindowsSizingDlgUnitsToPixels(&sizing, &x, &y);
|
|
|
|
*width = x + uiWindowsWindowTextWidth(c->hwnd);
|
|
|
|
*height = y;
|
2015-08-30 11:25:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void defaultOnToggled(uiCheckbox *c, void *data)
|
|
|
|
{
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
char *uiCheckboxText(uiCheckbox *c)
|
|
|
|
{
|
2016-04-22 19:04:30 -05:00
|
|
|
return uiWindowsWindowText(c->hwnd);
|
2015-08-30 11:25:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void uiCheckboxSetText(uiCheckbox *c, const char *text)
|
|
|
|
{
|
2016-04-22 19:04:30 -05:00
|
|
|
uiWindowsSetWindowText(c->hwnd, text);
|
2015-08-30 11:25:53 -05:00
|
|
|
// changing the text might necessitate a change in the checkbox's size
|
2016-04-29 11:20:41 -05:00
|
|
|
uiWindowsControlMinimumSizeChanged(uiWindowsControl(c));
|
2015-08-30 11:25:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void uiCheckboxOnToggled(uiCheckbox *c, void (*f)(uiCheckbox *, void *), void *data)
|
|
|
|
{
|
|
|
|
c->onToggled = f;
|
|
|
|
c->onToggledData = data;
|
|
|
|
}
|
|
|
|
|
|
|
|
int uiCheckboxChecked(uiCheckbox *c)
|
|
|
|
{
|
|
|
|
return SendMessage(c->hwnd, BM_GETCHECK, 0, 0) == BST_CHECKED;
|
|
|
|
}
|
|
|
|
|
|
|
|
void uiCheckboxSetChecked(uiCheckbox *c, int checked)
|
|
|
|
{
|
|
|
|
WPARAM check;
|
|
|
|
|
|
|
|
check = BST_CHECKED;
|
|
|
|
if (!checked)
|
|
|
|
check = BST_UNCHECKED;
|
|
|
|
SendMessage(c->hwnd, BM_SETCHECK, check, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
uiCheckbox *uiNewCheckbox(const char *text)
|
|
|
|
{
|
|
|
|
uiCheckbox *c;
|
|
|
|
WCHAR *wtext;
|
|
|
|
|
2016-04-29 11:49:42 -05:00
|
|
|
uiWindowsNewControl(uiCheckbox, c);
|
2015-08-30 11:25:53 -05:00
|
|
|
|
|
|
|
wtext = toUTF16(text);
|
|
|
|
c->hwnd = uiWindowsEnsureCreateControlHWND(0,
|
|
|
|
L"button", wtext,
|
|
|
|
BS_CHECKBOX | WS_TABSTOP,
|
|
|
|
hInstance, NULL,
|
|
|
|
TRUE);
|
|
|
|
uiFree(wtext);
|
|
|
|
|
|
|
|
uiWindowsRegisterWM_COMMANDHandler(c->hwnd, onWM_COMMAND, uiControl(c));
|
|
|
|
uiCheckboxOnToggled(c, defaultOnToggled, NULL);
|
|
|
|
|
|
|
|
return c;
|
|
|
|
}
|