2015-04-09 11:32:59 -05:00
|
|
|
// 7 april 2015
|
|
|
|
#include "uipriv_windows.h"
|
|
|
|
|
|
|
|
struct checkbox {
|
2015-04-16 08:08:38 -05:00
|
|
|
uiCheckbox c;
|
2015-04-17 09:49:14 -05:00
|
|
|
HWND hwnd;
|
2015-04-16 08:53:00 -05:00
|
|
|
void (*onToggled)(uiCheckbox *, void *);
|
2015-04-09 11:32:59 -05:00
|
|
|
void *onToggledData;
|
|
|
|
};
|
|
|
|
|
2015-04-16 08:08:38 -05:00
|
|
|
static BOOL onWM_COMMAND(uiControl *cc, WORD code, LRESULT *lResult)
|
2015-04-09 11:32:59 -05:00
|
|
|
{
|
2015-04-16 08:08:38 -05:00
|
|
|
struct checkbox *c = (struct checkbox *) cc;
|
2015-04-09 11:32:59 -05:00
|
|
|
WPARAM check;
|
|
|
|
|
2015-04-09 17:54:14 -05:00
|
|
|
if (code != BN_CLICKED)
|
2015-04-09 11:32:59 -05:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
// we didn't use BS_AUTOCHECKBOX (see controls_windows.go) so we have to manage the check state ourselves
|
|
|
|
check = BST_CHECKED;
|
2015-04-17 09:49:14 -05:00
|
|
|
if (SendMessage(c->hwnd, BM_GETCHECK, 0, 0) == BST_CHECKED)
|
2015-04-09 11:32:59 -05:00
|
|
|
check = BST_UNCHECKED;
|
2015-04-17 09:49:14 -05:00
|
|
|
SendMessage(c->hwnd, BM_SETCHECK, check, 0);
|
2015-04-09 11:32:59 -05:00
|
|
|
|
2015-04-16 08:08:38 -05:00
|
|
|
(*(c->onToggled))(uiCheckbox(c), c->onToggledData);
|
2015-04-09 11:32:59 -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-09 11:32:59 -05:00
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2015-04-16 08:08:38 -05:00
|
|
|
static void onWM_DESTROY(uiControl *cc)
|
2015-04-09 11:32:59 -05:00
|
|
|
{
|
2015-04-16 08:08:38 -05:00
|
|
|
struct checkbox *c = (struct checkbox *) cc;
|
2015-04-09 11:32:59 -05:00
|
|
|
|
2015-04-16 08:08:38 -05:00
|
|
|
uiFree(c);
|
2015-04-09 11:32:59 -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
|
|
|
|
|
2015-04-17 09:49:14 -05:00
|
|
|
static void preferredSize(uiControl *cc, uiSizing *d, intmax_t *width, intmax_t *height)
|
2015-04-09 11:32:59 -05:00
|
|
|
{
|
2015-04-17 09:49:14 -05:00
|
|
|
struct checkbox *c = (struct checkbox *) cc;
|
|
|
|
|
|
|
|
*width = uiDlgUnitsToX(checkboxXFromLeftOfBoxToLeftOfLabel, d->sys->baseX) + uiWindowsWindowTextWidth(c->hwnd);
|
2015-04-09 17:45:58 -05:00
|
|
|
*height = uiDlgUnitsToY(checkboxHeight, d->sys->baseY);
|
2015-04-09 11:32:59 -05:00
|
|
|
}
|
|
|
|
|
2015-04-16 08:08:38 -05:00
|
|
|
static void defaultOnToggled(uiCheckbox *c, void *data)
|
2015-04-09 11:32:59 -05:00
|
|
|
{
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
2015-04-17 09:49:14 -05:00
|
|
|
static char *checkboxText(uiCheckbox *c)
|
2015-04-09 11:32:59 -05:00
|
|
|
{
|
2015-04-16 08:08:38 -05:00
|
|
|
return uiWindowsControlText(uiControl(c));
|
2015-04-09 11:32:59 -05:00
|
|
|
}
|
|
|
|
|
2015-04-17 09:49:14 -05:00
|
|
|
static void checkboxSetText(uiCheckbox *c, const char *text)
|
2015-04-09 11:32:59 -05:00
|
|
|
{
|
2015-04-16 08:08:38 -05:00
|
|
|
uiWindowsControlSetText(uiControl(c), text);
|
2015-04-09 11:32:59 -05:00
|
|
|
}
|
|
|
|
|
2015-04-17 09:49:14 -05:00
|
|
|
static void checkboxOnToggled(uiCheckbox *cc, void (*f)(uiCheckbox *, void *), void *data)
|
2015-04-09 11:32:59 -05:00
|
|
|
{
|
2015-04-16 08:08:38 -05:00
|
|
|
struct checkbox *c = (struct checkbox *) cc;
|
2015-04-09 11:32:59 -05:00
|
|
|
|
2015-04-16 08:08:38 -05:00
|
|
|
c->onToggled = f;
|
|
|
|
c->onToggledData = data;
|
2015-04-09 11:32:59 -05:00
|
|
|
}
|
2015-04-09 12:10:32 -05:00
|
|
|
|
2015-04-17 09:49:14 -05:00
|
|
|
static int checkboxChecked(uiCheckbox *cc)
|
2015-04-09 12:10:32 -05:00
|
|
|
{
|
2015-04-17 09:49:14 -05:00
|
|
|
struct checkbox *c = (struct checkbox *) cc;
|
2015-04-09 12:10:32 -05:00
|
|
|
|
2015-04-17 09:49:14 -05:00
|
|
|
return SendMessage(c->hwnd, BM_GETCHECK, 0, 0) == BST_CHECKED;
|
2015-04-09 12:10:32 -05:00
|
|
|
}
|
|
|
|
|
2015-04-17 09:49:14 -05:00
|
|
|
static void checkboxSetChecked(uiCheckbox *cc, int checked)
|
2015-04-09 12:10:32 -05:00
|
|
|
{
|
2015-04-17 09:49:14 -05:00
|
|
|
struct checkbox *c = (struct checkbox *) cc;
|
2015-04-09 12:10:32 -05:00
|
|
|
WPARAM check;
|
|
|
|
|
|
|
|
check = BST_CHECKED;
|
|
|
|
if (!checked)
|
|
|
|
check = BST_UNCHECKED;
|
2015-04-17 09:49:14 -05:00
|
|
|
SendMessage(c->hwnd, BM_SETCHECK, check, 0);
|
2015-04-09 12:10:32 -05:00
|
|
|
}
|
2015-04-16 08:08:38 -05:00
|
|
|
|
2015-04-16 08:33:21 -05:00
|
|
|
uiCheckbox *uiNewCheckbox(const char *text)
|
2015-04-16 08:08:38 -05:00
|
|
|
{
|
|
|
|
struct checkbox *c;
|
|
|
|
uiWindowsNewControlParams p;
|
|
|
|
WCHAR *wtext;
|
|
|
|
|
2015-04-16 08:53:00 -05:00
|
|
|
c = uiNew(struct checkbox);
|
2015-04-16 08:08:38 -05:00
|
|
|
|
|
|
|
p.dwExStyle = 0;
|
|
|
|
p.lpClassName = L"button";
|
|
|
|
wtext = toUTF16(text);
|
|
|
|
p.lpWindowName = wtext;
|
|
|
|
p.dwStyle = BS_CHECKBOX | WS_TABSTOP;
|
|
|
|
p.hInstance = hInstance;
|
|
|
|
p.useStandardControlFont = TRUE;
|
|
|
|
p.onWM_COMMAND = onWM_COMMAND;
|
|
|
|
p.onWM_NOTIFY = onWM_NOTIFY;
|
|
|
|
p.onWM_DESTROY = onWM_DESTROY;
|
|
|
|
uiWindowsNewControl(uiControl(c), &p);
|
|
|
|
uiFree(wtext);
|
|
|
|
|
2015-04-17 09:49:14 -05:00
|
|
|
c->hwnd = HWND(c);
|
|
|
|
|
2015-04-16 08:08:38 -05:00
|
|
|
c->onToggled = defaultOnToggled;
|
|
|
|
|
|
|
|
uiControl(c)->PreferredSize = preferredSize;
|
|
|
|
|
2015-04-17 09:49:14 -05:00
|
|
|
uiCheckbox(c)->Text = checkboxText;
|
|
|
|
uiCheckbox(c)->SetText = checkboxSetText;
|
|
|
|
uiCheckbox(c)->OnToggled = checkboxOnToggled;
|
|
|
|
uiCheckbox(c)->Checked = checkboxChecked;
|
|
|
|
uiCheckbox(c)->SetChecked = checkboxSetChecked;
|
2015-04-16 08:08:38 -05:00
|
|
|
|
|
|
|
return uiCheckbox(c);
|
|
|
|
}
|