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-18 17:02:16 -05:00
|
|
|
static void onDestroy(void *data)
|
2015-04-09 11:32:59 -05:00
|
|
|
{
|
2015-04-18 17:02:16 -05:00
|
|
|
struct checkbox *c = (struct checkbox *) data;
|
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-05-04 13:56:26 -05:00
|
|
|
static void checkboxPreferredSize(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;
|
2015-05-02 19:51:00 -05:00
|
|
|
uiWindowsMakeControlParams p;
|
2015-04-16 08:08:38 -05:00
|
|
|
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;
|
2015-04-18 17:02:16 -05:00
|
|
|
p.onDestroy = onDestroy;
|
|
|
|
p.onDestroyData = c;
|
2015-05-02 19:51:00 -05:00
|
|
|
uiWindowsMakeControl(uiControl(c), &p);
|
2015-04-16 08:08:38 -05:00
|
|
|
uiFree(wtext);
|
|
|
|
|
2015-04-29 09:46:09 -05:00
|
|
|
c->hwnd = (HWND) uiControlHandle(uiControl(c));
|
2015-04-17 09:49:14 -05:00
|
|
|
|
2015-04-16 08:08:38 -05:00
|
|
|
c->onToggled = defaultOnToggled;
|
|
|
|
|
2015-05-04 13:56:26 -05:00
|
|
|
uiControl(c)->PreferredSize = checkboxPreferredSize;
|
2015-04-16 08:08:38 -05:00
|
|
|
|
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);
|
|
|
|
}
|