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-05-29 17:03:24 -05:00
|
|
|
void (*baseCommitDestroy)(uiControl *);
|
2015-04-09 11:32:59 -05:00
|
|
|
};
|
|
|
|
|
2015-05-29 17:03:24 -05:00
|
|
|
uiDefineControlType(uiCheckbox, uiTypeCheckbox, struct checkbox)
|
|
|
|
|
2015-05-21 13:52:21 -05:00
|
|
|
static BOOL onWM_COMMAND(uiControl *cc, HWND hwnd, 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-05-29 17:03:24 -05:00
|
|
|
static void checkboxCommitDestroy(uiControl *cc)
|
2015-04-09 11:32:59 -05:00
|
|
|
{
|
2015-05-29 17:03:24 -05:00
|
|
|
struct checkbox *c = (struct checkbox *) cc;
|
2015-04-09 11:32:59 -05:00
|
|
|
|
2015-05-21 11:07:11 -05:00
|
|
|
uiWindowsUnregisterWM_COMMANDHandler(c->hwnd);
|
2015-05-29 17:03:24 -05:00
|
|
|
(*(c->baseCommitDestroy))(uiControl(c));
|
2015-04-09 11:32:59 -05:00
|
|
|
}
|
|
|
|
|
2015-05-29 18:48:27 -05:00
|
|
|
static uintptr_t checkboxHandle(uiControl *cc)
|
|
|
|
{
|
|
|
|
struct checkbox *c = (struct checkbox *) cc;
|
|
|
|
|
|
|
|
return (uintptr_t) (c->hwnd);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2015-05-07 13:33:46 -05:00
|
|
|
*width = uiWindowsDlgUnitsToX(checkboxXFromLeftOfBoxToLeftOfLabel, d->Sys->BaseX) + uiWindowsWindowTextWidth(c->hwnd);
|
|
|
|
*height = uiWindowsDlgUnitsToY(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-06-03 13:54:25 -05:00
|
|
|
return uiWindowsSingleHWNDControlText(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-06-03 13:54:25 -05:00
|
|
|
uiWindowsSingleHWNDControlSetText(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;
|
|
|
|
WCHAR *wtext;
|
|
|
|
|
2015-05-29 17:03:24 -05:00
|
|
|
c = (struct checkbox *) uiWindowsNewSingleHWNDControl(uiTypeCheckbox());
|
2015-04-16 08:08:38 -05:00
|
|
|
|
|
|
|
wtext = toUTF16(text);
|
2015-05-29 17:03:24 -05:00
|
|
|
c->hwnd = uiWindowsUtilCreateControlHWND(0,
|
|
|
|
L"button", wtext,
|
|
|
|
BS_CHECKBOX | WS_TABSTOP,
|
|
|
|
hInstance, NULL,
|
|
|
|
TRUE);
|
2015-04-16 08:08:38 -05:00
|
|
|
uiFree(wtext);
|
|
|
|
|
2015-05-21 11:07:11 -05:00
|
|
|
uiWindowsRegisterWM_COMMANDHandler(c->hwnd, onWM_COMMAND, uiControl(c));
|
2015-04-17 09:49:14 -05:00
|
|
|
|
2015-04-16 08:08:38 -05:00
|
|
|
c->onToggled = defaultOnToggled;
|
|
|
|
|
2015-05-29 18:48:27 -05:00
|
|
|
uiControl(c)->Handle = checkboxHandle;
|
2015-05-04 13:56:26 -05:00
|
|
|
uiControl(c)->PreferredSize = checkboxPreferredSize;
|
2015-05-29 17:03:24 -05:00
|
|
|
c->baseCommitDestroy = uiControl(c)->CommitDestroy;
|
|
|
|
uiControl(c)->CommitDestroy = checkboxCommitDestroy;
|
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);
|
|
|
|
}
|