Removed the OLD radio buttons and spinbox code.
This commit is contained in:
parent
f130360be6
commit
1bb98b2c61
|
@ -1,222 +0,0 @@
|
|||
// 20 may 2015
|
||||
#include "uipriv_windows.h"
|
||||
|
||||
// desired behavior:
|
||||
// - tab moves between /entire groups/
|
||||
// - arrow keys navigate between radio buttons
|
||||
|
||||
struct radiobuttons {
|
||||
uiRadioButtons r;
|
||||
struct ptrArray *hwnds;
|
||||
uiControl *parent;
|
||||
uintptr_t insertAfter; // safe to be 0 initially (either not in a container or trully the first in the z-order)
|
||||
};
|
||||
|
||||
uiDefineControlType(uiRadioButtons, uiTypeRadioButtons, struct radiobuttons)
|
||||
|
||||
// TODO arrow keys don't work for changing items
|
||||
|
||||
// TODO this wrecks the z-order
|
||||
static BOOL onWM_COMMAND(uiControl *c, HWND clicked, WORD code, LRESULT *lResult)
|
||||
{
|
||||
struct radiobuttons *r = (struct radiobuttons *) c;
|
||||
WPARAM check;
|
||||
uintmax_t i;
|
||||
HWND hwnd;
|
||||
|
||||
if (code != BN_CLICKED)
|
||||
return FALSE;
|
||||
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);
|
||||
}
|
||||
*lResult = 0;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static void radiobuttonsCommitDestroy(uiControl *c)
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
// radio buttons have more than one handle
|
||||
// if we allow deletion, the handles are not permanent
|
||||
static uintptr_t radiobuttonsHandle(uiControl *c)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void radiobuttonsCommitSetParent(uiControl *c, uiControl *parent)
|
||||
{
|
||||
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);
|
||||
uiWindowsUtilSetParent(hwnd, r->parent);
|
||||
}
|
||||
}
|
||||
|
||||
// from http://msdn.microsoft.com/en-us/library/windows/desktop/dn742486.aspx#sizingandspacing
|
||||
#define radiobuttonHeight 10
|
||||
// from http://msdn.microsoft.com/en-us/library/windows/desktop/bb226818%28v=vs.85%29.aspx
|
||||
#define radiobuttonXFromLeftOfBoxToLeftOfLabel 12
|
||||
|
||||
static void radiobuttonsPreferredSize(uiControl *c, uiWindowsSizing *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;
|
||||
}
|
||||
*width = uiWindowsDlgUnitsToX(radiobuttonXFromLeftOfBoxToLeftOfLabel, d->Sys->BaseX) + maxwid;
|
||||
*height = uiWindowsDlgUnitsToY(radiobuttonHeight, d->Sys->BaseY) * r->hwnds->len;
|
||||
}
|
||||
|
||||
static void radiobuttonsResize(uiControl *c, intmax_t x, intmax_t y, intmax_t width, intmax_t height, uiWindowsSizing *d)
|
||||
{
|
||||
struct radiobuttons *r = (struct radiobuttons *) c;
|
||||
intmax_t height1;
|
||||
intmax_t h;
|
||||
uintmax_t i;
|
||||
HWND hwnd;
|
||||
|
||||
height1 = uiWindowsDlgUnitsToY(radiobuttonHeight, d->Sys->BaseY);
|
||||
for (i = 0; i < r->hwnds->len; i++) {
|
||||
hwnd = ptrArrayIndex(r->hwnds, HWND, i);
|
||||
h = height1;
|
||||
if (h > height) // clip to height
|
||||
h = height;
|
||||
moveWindow(hwnd, x, y, width, h, d);
|
||||
y += height1;
|
||||
height -= height1;
|
||||
if (height <= 0) // clip to height
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static uiWindowsSizing *radiobuttonsSizing(uiControl *c)
|
||||
{
|
||||
complain("attempt to call uiControlSizing() on uiRadioButtons %p", c);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
#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)
|
||||
|
||||
static uintptr_t radiobuttonsStartZOrder(uiControl *c)
|
||||
{
|
||||
struct radiobuttons *r = (struct radiobuttons *) c;
|
||||
|
||||
return r->insertAfter;
|
||||
}
|
||||
|
||||
static uintptr_t radiobuttonsSetZOrder(uiControl *c, uintptr_t insertAfter)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
static int radiobuttonsHasTabStops(uiControl *c)
|
||||
{
|
||||
struct radiobuttons *r = (struct radiobuttons *) c;
|
||||
|
||||
return r->hwnds->len != 0;
|
||||
}
|
||||
|
||||
static void radiobuttonsAppend(uiRadioButtons *rr, const char *text)
|
||||
{
|
||||
struct radiobuttons *r = (struct radiobuttons *) rr;
|
||||
HWND hwnd;
|
||||
WCHAR *wtext;
|
||||
HWND after;
|
||||
|
||||
wtext = toUTF16(text);
|
||||
hwnd = uiWindowsEnsureCreateControlHWND(0,
|
||||
L"button", wtext,
|
||||
BS_RADIOBUTTON | WS_TABSTOP,
|
||||
hInstance, NULL,
|
||||
TRUE);
|
||||
uiFree(wtext);
|
||||
uiWindowsUtilSetParent(hwnd, r->parent);
|
||||
uiWindowsRegisterWM_COMMANDHandler(hwnd, onWM_COMMAND, uiControl(r));
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
||||
ptrArrayAppend(r->hwnds, hwnd);
|
||||
uiControlQueueResize(uiControl(r));
|
||||
}
|
||||
|
||||
uiRadioButtons *uiNewRadioButtons(void)
|
||||
{
|
||||
struct radiobuttons *r;
|
||||
|
||||
r = (struct radiobuttons *) uiNewControl(uiTypeRadioButtons());
|
||||
|
||||
r->hwnds = newPtrArray();
|
||||
|
||||
uiControl(r)->CommitDestroy = radiobuttonsCommitDestroy;
|
||||
uiControl(r)->Handle = radiobuttonsHandle;
|
||||
uiControl(r)->CommitSetParent = radiobuttonsCommitSetParent;
|
||||
uiControl(r)->PreferredSize = radiobuttonsPreferredSize;
|
||||
uiControl(r)->Resize = radiobuttonsResize;
|
||||
uiControl(r)->Sizing = radiobuttonsSizing;
|
||||
uiControl(r)->CommitShow = radiobuttonsCommitShow;
|
||||
uiControl(r)->CommitHide = radiobuttonsCommitHide;
|
||||
uiControl(r)->CommitEnable = radiobuttonsCommitEnable;
|
||||
uiControl(r)->CommitDisable = radiobuttonsCommitDisable;
|
||||
uiControl(r)->StartZOrder = radiobuttonsStartZOrder;
|
||||
uiControl(r)->SetZOrder = radiobuttonsSetZOrder;
|
||||
uiControl(r)->HasTabStops = radiobuttonsHasTabStops;
|
||||
|
||||
uiRadioButtons(r)->Append = radiobuttonsAppend;
|
||||
|
||||
return uiRadioButtons(r);
|
||||
}
|
|
@ -1,262 +0,0 @@
|
|||
// 8 april 2015
|
||||
#include "uipriv_windows.h"
|
||||
|
||||
struct spinbox {
|
||||
uiSpinbox s;
|
||||
HWND hwnd;
|
||||
HWND updown;
|
||||
void (*onChanged)(uiSpinbox *, void *);
|
||||
void *onChangedData;
|
||||
BOOL inhibitChanged;
|
||||
};
|
||||
|
||||
uiDefineControlType(uiSpinbox, uiTypeSpinbox, struct spinbox)
|
||||
|
||||
// utility functions
|
||||
|
||||
static intmax_t value(struct spinbox *s)
|
||||
{
|
||||
BOOL neededCap = FALSE;
|
||||
LRESULT val;
|
||||
|
||||
// This verifies the value put in, capping it automatically.
|
||||
// We don't need to worry about checking for an error; that flag should really be called "did we have to cap?".
|
||||
// We DO need to set the value in case of a cap though.
|
||||
val = SendMessageW(s->updown, UDM_GETPOS32, 0, (LPARAM) (&neededCap));
|
||||
if (neededCap) {
|
||||
s->inhibitChanged = TRUE;
|
||||
SendMessageW(s->updown, UDM_SETPOS32, 0, (LPARAM) val);
|
||||
s->inhibitChanged = FALSE;
|
||||
}
|
||||
return (intmax_t) val;
|
||||
}
|
||||
|
||||
// control implementation
|
||||
|
||||
static BOOL onWM_COMMAND(uiControl *c, HWND hwnd, WORD code, LRESULT *lResult)
|
||||
{
|
||||
struct spinbox *s = (struct spinbox *) c;
|
||||
WCHAR *wtext;
|
||||
|
||||
if (code != EN_CHANGE)
|
||||
return FALSE;
|
||||
if (s->inhibitChanged)
|
||||
return FALSE;
|
||||
// We want to allow typing negative numbers; the natural way to do so is to start with a -.
|
||||
// However, if we just have the code below, the up-down will catch the bare - and reject it.
|
||||
// Let's fix that.
|
||||
// This won't handle leading spaces, but spaces aren't allowed *anyway*.
|
||||
wtext = windowText(s->hwnd);
|
||||
if (wcscmp(wtext, L"-") == 0) {
|
||||
uiFree(wtext);
|
||||
return TRUE;
|
||||
}
|
||||
// value() does the work for us
|
||||
value(s);
|
||||
(*(s->onChanged))(uiSpinbox(s), s->onChangedData);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static void spinboxCommitDestroy(uiControl *c)
|
||||
{
|
||||
struct spinbox *s = (struct spinbox *) c;
|
||||
|
||||
uiWindowsUnregisterWM_COMMANDHandler(s->hwnd);
|
||||
if (DestroyWindow(s->updown) == 0)
|
||||
logLastError("error destroying updown in spinboxCommitDestroy()");
|
||||
uiWindowsUtilDestroy(s->hwnd);
|
||||
}
|
||||
|
||||
// the edit control is the one to return here
|
||||
// we can't return the updown because it gets recreated on resize
|
||||
static uintptr_t spinboxHandle(uiControl *c)
|
||||
{
|
||||
struct spinbox *s = (struct spinbox *) c;
|
||||
|
||||
return (uintptr_t) (s->hwnd);
|
||||
}
|
||||
|
||||
static void spinboxCommitSetParent(uiControl *c, uiControl *parent)
|
||||
{
|
||||
struct spinbox *s = (struct spinbox *) c;
|
||||
|
||||
uiWindowsUtilSetParent(s->hwnd, parent);
|
||||
uiWindowsUtilSetParent(s->updown, parent);
|
||||
}
|
||||
|
||||
// from http://msdn.microsoft.com/en-us/library/windows/desktop/dn742486.aspx#sizingandspacing
|
||||
#define entryWidth 107 /* this is actually the shorter progress bar width, but Microsoft only indicates as wide as necessary */
|
||||
#define entryHeight 14
|
||||
|
||||
static void spinboxPreferredSize(uiControl *c, uiWindowsSizing *d, intmax_t *width, intmax_t *height)
|
||||
{
|
||||
*width = uiWindowsDlgUnitsToX(entryWidth, d->Sys->BaseX);
|
||||
*height = uiWindowsDlgUnitsToY(entryHeight, d->Sys->BaseY);
|
||||
}
|
||||
|
||||
// an up-down control will only properly position itself the first time
|
||||
// stupidly, there are no messages to force a size calculation, nor can I seem to reset the buddy window to force a new position
|
||||
// alas, we have to make a new up/down control each time :(
|
||||
static void recreateUpDown(struct spinbox *s)
|
||||
{
|
||||
HWND parent;
|
||||
BOOL preserve = FALSE;
|
||||
intmax_t current;
|
||||
// Microsoft's commctrl.h says to use this type
|
||||
INT min, max;
|
||||
|
||||
parent = GetAncestor(s->hwnd, GA_PARENT);
|
||||
if (s->updown != NULL) {
|
||||
preserve = TRUE;
|
||||
current = value(s);
|
||||
SendMessageW(s->updown, UDM_GETRANGE32, (WPARAM) (&min), (LPARAM) (&max));
|
||||
if (DestroyWindow(s->updown) == 0)
|
||||
logLastError("error destroying old updown in recreateUpDown()");
|
||||
}
|
||||
s->inhibitChanged = TRUE;
|
||||
s->updown = CreateWindowExW(0,
|
||||
UPDOWN_CLASSW, L"",
|
||||
// no WS_VISIBLE; we set visibility ourselves
|
||||
// up-down control should not be a tab stop
|
||||
WS_CHILD | UDS_ALIGNRIGHT | UDS_ARROWKEYS | UDS_HOTTRACK | UDS_NOTHOUSANDS | UDS_SETBUDDYINT,
|
||||
// this is important; it's necessary for autosizing to work
|
||||
0, 0, 0, 0,
|
||||
parent, NULL, hInstance, NULL);
|
||||
if (s->updown == NULL)
|
||||
logLastError("error creating updown in recreateUpDown()");
|
||||
SendMessageW(s->updown, UDM_SETBUDDY, (WPARAM) (s->hwnd), 0);
|
||||
if (preserve) {
|
||||
SendMessageW(s->updown, UDM_SETRANGE32, (WPARAM) min, (LPARAM) max);
|
||||
SendMessageW(s->updown, UDM_SETPOS32, 0, (LPARAM) current);
|
||||
}
|
||||
// preserve the z-order
|
||||
uiWindowsUtilSetZOrder(s->updown, (uintptr_t) (s->hwnd));
|
||||
if (uiControlContainerVisible(uiControl(s)))
|
||||
uiWindowsUtilShow(s->updown);
|
||||
s->inhibitChanged = FALSE;
|
||||
}
|
||||
|
||||
static void spinboxResize(uiControl *c, intmax_t x, intmax_t y, intmax_t width, intmax_t height, uiWindowsSizing *d)
|
||||
{
|
||||
struct spinbox *s = (struct spinbox *) c;
|
||||
|
||||
moveWindow(s->hwnd, x, y, width, height, d);
|
||||
recreateUpDown(s);
|
||||
}
|
||||
|
||||
static uiWindowsSizing *spinboxSizing(uiControl *c)
|
||||
{
|
||||
complain("attempt to call uiControlSizing() on uiSpinbox %p", c);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
#define COMMIT(n, f) \
|
||||
static void spinboxCommit ## n(uiControl *c) \
|
||||
{ \
|
||||
struct spinbox *s = (struct spinbox *) c; \
|
||||
f(s->hwnd); \
|
||||
f(s->updown); \
|
||||
}
|
||||
COMMIT(Show, uiWindowsUtilShow)
|
||||
COMMIT(Hide, uiWindowsUtilHide)
|
||||
COMMIT(Enable, uiWindowsUtilEnable)
|
||||
COMMIT(Disable, uiWindowsUtilDisable)
|
||||
|
||||
static uintptr_t spinboxStartZOrder(uiControl *c)
|
||||
{
|
||||
struct spinbox *s = (struct spinbox *) c;
|
||||
|
||||
return uiWindowsUtilStartZOrder(s->hwnd);
|
||||
}
|
||||
|
||||
static uintptr_t spinboxSetZOrder(uiControl *c, uintptr_t insertAfter)
|
||||
{
|
||||
struct spinbox *s = (struct spinbox *) c;
|
||||
|
||||
uiWindowsUtilSetZOrder(s->hwnd, insertAfter);
|
||||
uiWindowsUtilSetZOrder(s->updown, (uintptr_t) (s->hwnd));
|
||||
return (uintptr_t) (s->updown);
|
||||
}
|
||||
|
||||
static int spinboxHasTabStops(uiControl *c)
|
||||
{
|
||||
struct spinbox *s = (struct spinbox *) c;
|
||||
|
||||
return uiWindowsUtilHasTabStops(s->hwnd);
|
||||
}
|
||||
|
||||
static void defaultOnChanged(uiSpinbox *s, void *data)
|
||||
{
|
||||
// do nothing
|
||||
}
|
||||
|
||||
static intmax_t spinboxValue(uiSpinbox *ss)
|
||||
{
|
||||
struct spinbox *s = (struct spinbox *) ss;
|
||||
|
||||
return value(s);
|
||||
}
|
||||
|
||||
static void spinboxSetValue(uiSpinbox *ss, intmax_t value)
|
||||
{
|
||||
struct spinbox *s = (struct spinbox *) ss;
|
||||
|
||||
s->inhibitChanged = TRUE;
|
||||
SendMessageW(s->updown, UDM_SETPOS32, 0, (LPARAM) value);
|
||||
s->inhibitChanged = FALSE;
|
||||
}
|
||||
|
||||
static void spinboxOnChanged(uiSpinbox *ss, void (*f)(uiSpinbox *, void *), void *data)
|
||||
{
|
||||
struct spinbox *s = (struct spinbox *) ss;
|
||||
|
||||
s->onChanged = f;
|
||||
s->onChangedData = data;
|
||||
}
|
||||
|
||||
uiSpinbox *uiNewSpinbox(intmax_t min, intmax_t max)
|
||||
{
|
||||
struct spinbox *s;
|
||||
|
||||
if (min >= max)
|
||||
complain("error: min >= max in uiNewSpinbox()");
|
||||
|
||||
s = (struct spinbox *) uiNewControl(uiTypeSpinbox());
|
||||
|
||||
s->hwnd = uiWindowsEnsureCreateControlHWND(WS_EX_CLIENTEDGE,
|
||||
L"edit", L"",
|
||||
// don't use ES_NUMBER; it doesn't allow typing in a leading -
|
||||
ES_AUTOHSCROLL | ES_LEFT | ES_NOHIDESEL | WS_TABSTOP,
|
||||
hInstance, NULL,
|
||||
TRUE);
|
||||
|
||||
uiWindowsRegisterWM_COMMANDHandler(s->hwnd, onWM_COMMAND, uiControl(s));
|
||||
|
||||
recreateUpDown(s);
|
||||
s->inhibitChanged = TRUE;
|
||||
SendMessageW(s->updown, UDM_SETRANGE32, (WPARAM) min, (LPARAM) max);
|
||||
SendMessageW(s->updown, UDM_SETPOS32, 0, (LPARAM) min);
|
||||
s->inhibitChanged = FALSE;
|
||||
|
||||
s->onChanged = defaultOnChanged;
|
||||
|
||||
uiControl(s)->Handle = spinboxHandle;
|
||||
uiControl(s)->PreferredSize = spinboxPreferredSize;
|
||||
uiControl(s)->Resize = spinboxResize;
|
||||
uiControl(s)->Sizing = spinboxSizing;
|
||||
uiControl(s)->CommitDestroy = spinboxCommitDestroy;
|
||||
uiControl(s)->CommitSetParent = spinboxCommitSetParent;
|
||||
uiControl(s)->CommitShow = spinboxCommitShow;
|
||||
uiControl(s)->CommitHide = spinboxCommitHide;
|
||||
uiControl(s)->CommitEnable = spinboxCommitEnable;
|
||||
uiControl(s)->CommitDisable = spinboxCommitDisable;
|
||||
uiControl(s)->StartZOrder = spinboxStartZOrder;
|
||||
uiControl(s)->SetZOrder = spinboxSetZOrder;
|
||||
uiControl(s)->HasTabStops = spinboxHasTabStops;
|
||||
|
||||
uiSpinbox(s)->Value = spinboxValue;
|
||||
uiSpinbox(s)->SetValue = spinboxSetValue;
|
||||
uiSpinbox(s)->OnChanged = spinboxOnChanged;
|
||||
|
||||
return uiSpinbox(s);
|
||||
}
|
Loading…
Reference in New Issue