Implemented uiCheckbox on Windows. Fixed build issues in the test program.

This commit is contained in:
Pietro Gagliardi 2015-04-09 12:32:59 -04:00
parent c2d98a8b0e
commit 69b794c0b0
3 changed files with 112 additions and 3 deletions

109
checkbox_windows.c Normal file
View File

@ -0,0 +1,109 @@
// 7 april 2015
#include "uipriv_windows.h"
struct checkbox {
uiControl *c;
void (*onToggled)(uiControl *, void *);
void *onToggledData;
};
#define C(x) ((struct checkbox *) (x))
static BOOL onWM_COMMAND(uiControl *c, WPARAM wParam, LPARAM lParam, void *data, LRESULT *lResult)
{
HWND hwnd;
WPARAM check;
if (HIWORD(wParam) != BN_CLICKED)
return FALSE;
// we didn't use BS_AUTOCHECKBOX (see controls_windows.go) so we have to manage the check state ourselves
hwnd = (HWND) uiControlHandle(c);
check = BST_CHECKED;
if (SendMessage(hwnd, BM_GETCHECK, 0, 0) == BST_CHECKED)
check = BST_UNCHECKED;
SendMessage(hwnd, BM_SETCHECK, check, 0);
(*(C(data)->onToggled))(c, C(data)->onToggledData);
*lResult = 0;
return TRUE;
}
static BOOL onWM_NOTIFY(uiControl *c, WPARAM wParam, LPARAM lParam, void *data, LRESULT *lResult)
{
return FALSE;
}
static void onWM_DESTROY(uiControl *c, void *data)
{
struct checkbox *cc = (struct checkbox *) data;
uiFree(cc);
}
// 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
static void preferredSize(uiControl *c, int baseX, int baseY, LONG internalLeading, intmax_t *width, intmax_t *height)
{
*width = uiDlgUnitToX(checkboxXFromLeftOfBoxToLeftOfLabel, baseX) + uiWindowsWindowTextWidth((HWND) uiControlHandle(c));
*height = uiDlgUnitToY(checkboxHeight, baseY);
}
static void defaultOnToggled(uiControl *c, void *data)
{
// do nothing
}
uiControl *uiNewCheckbox(const char *text)
{
struct checkbox *c;
uiWindowsNewControlParams p;
WCHAR *wtext;
HWND hwnd;
c = uiNew(struct checkbox);
p.dwExStyle = 0;
p.lpClassName = L"button";
wtext = toUTF16(text);
p.lpWindowName = wtext;
p.dwStyle = BS_CHECKBOX | WS_TABSTOP;
p.hInstance = hInstance;
p.onWM_COMMAND = onWM_COMMAND;
p.onWM_NOTIFY = onWM_NOTIFY;
p.onWM_DESTROY = onWM_DESTROY;
p.onCommandNotifyDestroyData = c;
p.preferredSize = preferredSize;
p.data = c;
c->c = uiWindowsNewControl(&p);
uiFree(wtext);
hwnd = (HWND) uiControlHandle(c->c);
SendMessageW(hwnd, WM_SETFONT, (WPARAM) hMessageFont, (LPARAM) TRUE);
c->onToggled = defaultOnToggled;
return c->c;
}
char *uiCheckboxText(uiControl *c)
{
return uiWindowsControlText(c);
}
void uiCheckboxSetText(uiControl *c, const char *text)
{
uiWindowsControlSetText(c, text);
}
void uiCheckboxOnToggled(uiControl *c, void (*f)(uiControl *, void *), void *data)
{
struct checkbox *cc;
cc = (struct checkbox *) uiWindowsControlData(c);
cc->onToggled = f;
cc->onToggledData = data;
}

4
test.c
View File

@ -91,7 +91,7 @@ int main(int argc, char *argv[])
uiWindowOnClosing(w, onClosing, NULL);
stacks[0] = uiNewVerticalStack();
uiWindowSetChild(w, staacks[0]);
uiWindowSetChild(w, stacks[0]);
e = uiNewEntry();
uiStackAdd(stacks[0], e, 0);
@ -116,7 +116,7 @@ int main(int argc, char *argv[])
// this will also be used to make sure tab stops work properly when inserted out of creation order, especially on Windows
spaced = uiNewCheckbox("Spaced");
uiCheckboxOnClicked(spaced, setSpaced, NULL);
uiCheckboxOnToggled(spaced, setSpaced, NULL);
stacks[3] = uiNewHorizontalStack();
getButton = uiNewButton("Get Checkbox Text");

2
ui.h
View File

@ -46,7 +46,7 @@ char *uiEntryText(uiControl *);
void uiEntrySetText(uiControl *, const char *);
uiControl *uiNewCheckbox(const char *);
char *uiCheckboxText(void);
char *uiCheckboxText(uiControl *);
void uiCheckboxSetText(uiControl *, const char *);
void uiCheckboxOnToggled(uiControl *, void (*)(uiControl *, void *), void *);