2015-05-20 16:52:21 -05:00
|
|
|
// 20 may 2015
|
|
|
|
#include "uipriv_windows.h"
|
|
|
|
|
2015-06-05 09:23:31 -05:00
|
|
|
// desired behavior:
|
|
|
|
// - tab moves between /entire groups/
|
|
|
|
// - arrow keys navigate between radio buttons
|
|
|
|
|
2015-05-20 16:52:21 -05:00
|
|
|
struct radiobuttons {
|
|
|
|
uiRadioButtons r;
|
|
|
|
struct ptrArray *hwnds;
|
2015-05-21 14:48:16 -05:00
|
|
|
uiControl *parent;
|
2015-06-04 22:31:43 -05:00
|
|
|
uintptr_t insertAfter; // safe to be 0 initially (either not in a container or trully the first in the z-order)
|
2015-05-20 16:52:21 -05:00
|
|
|
};
|
2015-05-21 14:48:16 -05:00
|
|
|
|
2015-05-30 01:14:13 -05:00
|
|
|
uiDefineControlType(uiRadioButtons, uiTypeRadioButtons, struct radiobuttons)
|
|
|
|
|
2015-06-04 23:11:34 -05:00
|
|
|
// TODO arrow keys don't work for changing items
|
|
|
|
|
|
|
|
// TODO this wrecks the z-order
|
2015-05-21 14:52:59 -05:00
|
|
|
static BOOL onWM_COMMAND(uiControl *c, HWND clicked, WORD code, LRESULT *lResult)
|
2015-05-21 14:48:16 -05:00
|
|
|
{
|
|
|
|
struct radiobuttons *r = (struct radiobuttons *) c;
|
2015-05-21 14:52:59 -05:00
|
|
|
WPARAM check;
|
|
|
|
uintmax_t i;
|
|
|
|
HWND hwnd;
|
2015-05-21 14:48:16 -05:00
|
|
|
|
|
|
|
if (code != BN_CLICKED)
|
|
|
|
return FALSE;
|
2015-05-21 14:52:59 -05:00
|
|
|
for (i = 0; i < r->hwnds->len; i++) {
|
|
|
|
hwnd = ptrArrayIndex(r->hwnds, HWND, i);
|
|
|
|
check = BST_UNCHECKED;
|
|
|
|
if (clicked == hwnd)
|
|
|
|
check = BST_CHECKED;
|
|
|
|
SendMessage(hwnd, BM_SETCHECK, check, 0);
|
|
|
|
}
|
2015-05-21 14:48:16 -05:00
|
|
|
*lResult = 0;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2015-05-29 21:28:00 -05:00
|
|
|
static void radiobuttonsCommitDestroy(uiControl *c)
|
2015-05-21 14:48:16 -05:00
|
|
|
{
|
2015-06-01 17:06:15 -05:00
|
|
|
struct radiobuttons *r = (struct radiobuttons *) c;
|
|
|
|
HWND hwnd;
|
|
|
|
|
|
|
|
while (r->hwnds->len != 0) {
|
|
|
|
hwnd = ptrArrayIndex(r->hwnds, HWND, 0);
|
|
|
|
ptrArrayDelete(r->hwnds, 0);
|
|
|
|
uiWindowsUnregisterWM_COMMANDHandler(hwnd);
|
|
|
|
uiWindowsUtilDestroy(hwnd);
|
|
|
|
}
|
|
|
|
ptrArrayDestroy(r->hwnds);
|
2015-05-21 14:48:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// radio buttons have more than one handle
|
|
|
|
// if we allow deletion, the handles are not permanent
|
|
|
|
static uintptr_t radiobuttonsHandle(uiControl *c)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-05-29 21:28:00 -05:00
|
|
|
static void radiobuttonsCommitSetParent(uiControl *c, uiControl *parent)
|
2015-05-21 14:48:16 -05:00
|
|
|
{
|
|
|
|
struct radiobuttons *r = (struct radiobuttons *) c;
|
|
|
|
HWND hwnd;
|
|
|
|
uintmax_t i;
|
|
|
|
|
|
|
|
r->parent = parent;
|
|
|
|
for (i = 0; i < r->hwnds->len; i++) {
|
|
|
|
hwnd = ptrArrayIndex(r->hwnds, HWND, i);
|
2015-05-30 20:54:52 -05:00
|
|
|
uiWindowsUtilSetParent(hwnd, r->parent);
|
2015-05-21 14:48:16 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// from http://msdn.microsoft.com/en-us/library/windows/desktop/dn742486.aspx#sizingandspacing
|
2015-05-21 16:26:25 -05:00
|
|
|
#define radiobuttonHeight 10
|
2015-05-21 14:48:16 -05:00
|
|
|
// from http://msdn.microsoft.com/en-us/library/windows/desktop/bb226818%28v=vs.85%29.aspx
|
2015-05-21 16:26:25 -05:00
|
|
|
#define radiobuttonXFromLeftOfBoxToLeftOfLabel 12
|
|
|
|
|
2015-05-21 14:48:16 -05:00
|
|
|
static void radiobuttonsPreferredSize(uiControl *c, uiSizing *d, intmax_t *width, intmax_t *height)
|
|
|
|
{
|
|
|
|
struct radiobuttons *r = (struct radiobuttons *) c;
|
|
|
|
uintmax_t i;
|
|
|
|
intmax_t wid, maxwid;
|
|
|
|
|
|
|
|
maxwid = 0;
|
|
|
|
for (i = 0; i < r->hwnds->len; i++) {
|
|
|
|
wid = uiWindowsWindowTextWidth(ptrArrayIndex(r->hwnds, HWND, i));
|
|
|
|
if (maxwid < wid)
|
|
|
|
maxwid = wid;
|
|
|
|
}
|
2015-05-21 16:26:25 -05:00
|
|
|
*width = uiWindowsDlgUnitsToX(radiobuttonXFromLeftOfBoxToLeftOfLabel, d->Sys->BaseX) + maxwid;
|
|
|
|
*height = uiWindowsDlgUnitsToY(radiobuttonHeight, d->Sys->BaseY) * r->hwnds->len;
|
2015-05-21 14:48:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void radiobuttonsResize(uiControl *c, intmax_t x, intmax_t y, intmax_t width, intmax_t height, uiSizing *d)
|
|
|
|
{
|
|
|
|
struct radiobuttons *r = (struct radiobuttons *) c;
|
|
|
|
intmax_t height1;
|
2015-06-04 22:39:11 -05:00
|
|
|
intmax_t h;
|
2015-05-21 14:48:16 -05:00
|
|
|
uintmax_t i;
|
|
|
|
HWND hwnd;
|
|
|
|
|
2015-05-21 16:26:25 -05:00
|
|
|
height1 = uiWindowsDlgUnitsToY(radiobuttonHeight, d->Sys->BaseY);
|
2015-05-21 14:48:16 -05:00
|
|
|
for (i = 0; i < r->hwnds->len; i++) {
|
|
|
|
hwnd = ptrArrayIndex(r->hwnds, HWND, i);
|
2015-06-04 22:39:11 -05:00
|
|
|
h = height1;
|
|
|
|
if (h > height) // clip to height
|
|
|
|
h = height;
|
|
|
|
moveWindow(hwnd, x, y, width, h, d);
|
2015-05-21 14:48:16 -05:00
|
|
|
y += height1;
|
2015-06-04 22:39:11 -05:00
|
|
|
height -= height1;
|
|
|
|
if (height <= 0) // clip to height
|
|
|
|
break;
|
2015-05-21 14:48:16 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static uiSizing *radiobuttonsSizing(uiControl *c)
|
|
|
|
{
|
2015-06-01 12:51:53 -05:00
|
|
|
complain("attempt to call uiControlSizing() on uiRadioButtons %p", c);
|
2015-05-21 14:48:16 -05:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-06-01 12:51:53 -05:00
|
|
|
#define COMMIT(n, f) \
|
|
|
|
static void radiobuttonsCommit ## n(uiControl *c) \
|
|
|
|
{ \
|
|
|
|
struct radiobuttons *r = (struct radiobuttons *) c; \
|
|
|
|
uintmax_t i; \
|
|
|
|
HWND hwnd; \
|
|
|
|
for (i = 0; i < r->hwnds->len; i++) { \
|
|
|
|
hwnd = ptrArrayIndex(r->hwnds, HWND, i); \
|
|
|
|
f(hwnd); \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
COMMIT(Show, uiWindowsUtilShow)
|
|
|
|
COMMIT(Hide, uiWindowsUtilHide)
|
|
|
|
COMMIT(Enable, uiWindowsUtilEnable)
|
|
|
|
COMMIT(Disable, uiWindowsUtilDisable)
|
2015-05-21 14:48:16 -05:00
|
|
|
|
2015-05-30 18:23:05 -05:00
|
|
|
static uintptr_t radiobuttonsStartZOrder(uiControl *c)
|
2015-05-21 14:48:16 -05:00
|
|
|
{
|
2015-06-04 22:31:43 -05:00
|
|
|
struct radiobuttons *r = (struct radiobuttons *) c;
|
|
|
|
|
|
|
|
return r->insertAfter;
|
2015-05-21 14:48:16 -05:00
|
|
|
}
|
|
|
|
|
2015-05-30 18:23:05 -05:00
|
|
|
static uintptr_t radiobuttonsSetZOrder(uiControl *c, uintptr_t insertAfter)
|
2015-05-21 14:48:16 -05:00
|
|
|
{
|
2015-06-04 22:31:43 -05:00
|
|
|
struct radiobuttons *r = (struct radiobuttons *) c;
|
|
|
|
uintmax_t i;
|
|
|
|
HWND hwnd;
|
|
|
|
|
|
|
|
r->insertAfter = insertAfter;
|
|
|
|
for (i = 0; i < r->hwnds->len; i++) {
|
|
|
|
hwnd = ptrArrayIndex(r->hwnds, HWND, i);
|
|
|
|
uiWindowsUtilSetZOrder(hwnd, insertAfter);
|
|
|
|
insertAfter = (uintptr_t) hwnd;
|
|
|
|
}
|
|
|
|
return insertAfter;
|
2015-05-21 14:48:16 -05:00
|
|
|
}
|
|
|
|
|
2015-05-30 18:23:05 -05:00
|
|
|
static int radiobuttonsHasTabStops(uiControl *c)
|
|
|
|
{
|
2015-06-01 12:51:53 -05:00
|
|
|
struct radiobuttons *r = (struct radiobuttons *) c;
|
|
|
|
|
|
|
|
return r->hwnds->len != 0;
|
2015-05-30 18:23:05 -05:00
|
|
|
}
|
|
|
|
|
2015-05-21 14:48:16 -05:00
|
|
|
static void radiobuttonsAppend(uiRadioButtons *rr, const char *text)
|
|
|
|
{
|
|
|
|
struct radiobuttons *r = (struct radiobuttons *) rr;
|
2015-05-30 20:54:52 -05:00
|
|
|
HWND hwnd;
|
2015-05-21 14:48:16 -05:00
|
|
|
WCHAR *wtext;
|
2015-06-04 22:31:43 -05:00
|
|
|
HWND after;
|
2015-05-21 14:48:16 -05:00
|
|
|
|
|
|
|
wtext = toUTF16(text);
|
2015-05-30 20:54:52 -05:00
|
|
|
hwnd = uiWindowsUtilCreateControlHWND(0,
|
2015-05-21 14:48:16 -05:00
|
|
|
L"button", wtext,
|
2015-06-05 12:20:30 -05:00
|
|
|
BS_RADIOBUTTON | WS_TABSTOP,
|
2015-05-30 20:54:52 -05:00
|
|
|
hInstance, NULL,
|
|
|
|
TRUE);
|
2015-06-01 17:06:15 -05:00
|
|
|
uiFree(wtext);
|
2015-05-30 20:54:52 -05:00
|
|
|
uiWindowsUtilSetParent(hwnd, r->parent);
|
2015-05-21 14:52:59 -05:00
|
|
|
uiWindowsRegisterWM_COMMANDHandler(hwnd, onWM_COMMAND, uiControl(r));
|
2015-06-04 22:31:43 -05:00
|
|
|
|
|
|
|
// maintain z-order
|
|
|
|
if (r->hwnds->len == 0) // first item
|
|
|
|
uiWindowsUtilSetZOrder(hwnd, r->insertAfter);
|
|
|
|
else {
|
|
|
|
after = ptrArrayIndex(r->hwnds, HWND, r->hwnds->len - 1);
|
|
|
|
uiWindowsUtilSetZOrder(hwnd, (uintptr_t) after);
|
|
|
|
}
|
|
|
|
|
2015-05-21 14:48:16 -05:00
|
|
|
ptrArrayAppend(r->hwnds, hwnd);
|
|
|
|
uiControlQueueResize(uiControl(r));
|
|
|
|
}
|
|
|
|
|
|
|
|
uiRadioButtons *uiNewRadioButtons(void)
|
|
|
|
{
|
|
|
|
struct radiobuttons *r;
|
|
|
|
|
2015-05-30 10:44:37 -05:00
|
|
|
r = (struct radiobuttons *) uiNewControl(uiTypeRadioButtons());
|
2015-05-21 14:48:16 -05:00
|
|
|
|
|
|
|
r->hwnds = newPtrArray();
|
|
|
|
|
2015-05-29 21:28:00 -05:00
|
|
|
uiControl(r)->CommitDestroy = radiobuttonsCommitDestroy;
|
2015-05-21 14:48:16 -05:00
|
|
|
uiControl(r)->Handle = radiobuttonsHandle;
|
2015-05-29 21:28:00 -05:00
|
|
|
uiControl(r)->CommitSetParent = radiobuttonsCommitSetParent;
|
2015-05-21 14:48:16 -05:00
|
|
|
uiControl(r)->PreferredSize = radiobuttonsPreferredSize;
|
|
|
|
uiControl(r)->Resize = radiobuttonsResize;
|
|
|
|
uiControl(r)->Sizing = radiobuttonsSizing;
|
2015-06-01 12:51:53 -05:00
|
|
|
uiControl(r)->CommitShow = radiobuttonsCommitShow;
|
|
|
|
uiControl(r)->CommitHide = radiobuttonsCommitHide;
|
|
|
|
uiControl(r)->CommitEnable = radiobuttonsCommitEnable;
|
|
|
|
uiControl(r)->CommitDisable = radiobuttonsCommitDisable;
|
2015-05-21 14:48:16 -05:00
|
|
|
uiControl(r)->StartZOrder = radiobuttonsStartZOrder;
|
2015-05-30 18:23:05 -05:00
|
|
|
uiControl(r)->SetZOrder = radiobuttonsSetZOrder;
|
|
|
|
uiControl(r)->HasTabStops = radiobuttonsHasTabStops;
|
2015-05-21 14:48:16 -05:00
|
|
|
|
|
|
|
uiRadioButtons(r)->Append = radiobuttonsAppend;
|
|
|
|
|
|
|
|
return uiRadioButtons(r);
|
|
|
|
}
|