2015-05-19 11:32:14 -05:00
|
|
|
// 8 april 2015
|
|
|
|
#include "uipriv_windows.h"
|
|
|
|
|
|
|
|
struct spinbox {
|
|
|
|
uiSpinbox s;
|
|
|
|
HWND hwnd;
|
|
|
|
HWND updown;
|
|
|
|
void (*baseResize)(uiControl *, intmax_t, intmax_t, intmax_t, intmax_t, uiSizing *);
|
2015-05-19 17:17:30 -05:00
|
|
|
void (*onChanged)(uiSpinbox *, void *);
|
|
|
|
void *onChangedData;
|
|
|
|
BOOL inhibitChanged;
|
2015-05-29 17:03:24 -05:00
|
|
|
void (*baseCommitDestroy)(uiControl *);
|
2015-05-19 11:32:14 -05:00
|
|
|
};
|
|
|
|
|
2015-05-29 17:03:24 -05:00
|
|
|
uiDefineControlType(uiSpinbox, uiTypeSpinbox, struct spinbox)
|
|
|
|
|
2015-05-19 17:17:30 -05:00
|
|
|
// utility functions
|
|
|
|
|
|
|
|
static intmax_t value(struct spinbox *s)
|
2015-05-19 11:32:14 -05:00
|
|
|
{
|
2015-05-19 12:37:06 -05:00
|
|
|
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.
|
2015-05-19 12:39:08 -05:00
|
|
|
// TODO wine only?
|
2015-05-19 12:37:06 -05:00
|
|
|
val = SendMessageW(s->updown, UDM_GETPOS32, 0, (LPARAM) (&neededCap));
|
2015-05-19 17:17:30 -05:00
|
|
|
if (neededCap) {
|
|
|
|
s->inhibitChanged = TRUE;
|
2015-05-19 12:37:06 -05:00
|
|
|
SendMessageW(s->updown, UDM_SETPOS32, 0, (LPARAM) val);
|
2015-05-19 17:17:30 -05:00
|
|
|
s->inhibitChanged = FALSE;
|
|
|
|
}
|
|
|
|
return (intmax_t) val;
|
|
|
|
}
|
|
|
|
|
|
|
|
// control implementation
|
|
|
|
|
2015-05-21 13:52:21 -05:00
|
|
|
static BOOL onWM_COMMAND(uiControl *c, HWND hwnd, WORD code, LRESULT *lResult)
|
2015-05-19 17:17:30 -05:00
|
|
|
{
|
|
|
|
struct spinbox *s = (struct spinbox *) c;
|
|
|
|
|
|
|
|
if (code != EN_CHANGE)
|
|
|
|
return FALSE;
|
|
|
|
if (s->inhibitChanged)
|
|
|
|
return FALSE;
|
|
|
|
// value() does the work for us
|
|
|
|
value(s);
|
|
|
|
(*(s->onChanged))(uiSpinbox(s), s->onChangedData);
|
|
|
|
return TRUE;
|
2015-05-19 11:32:14 -05:00
|
|
|
}
|
|
|
|
|
2015-05-29 17:03:24 -05:00
|
|
|
static void spinboxCommitDestroy(uiControl *c)
|
2015-05-19 11:32:14 -05:00
|
|
|
{
|
2015-05-29 17:03:24 -05:00
|
|
|
struct spinbox *s = (struct spinbox *) c;
|
2015-05-19 11:32:14 -05:00
|
|
|
|
2015-05-21 11:07:11 -05:00
|
|
|
uiWindowsUnregisterWM_COMMANDHandler(s->hwnd);
|
2015-05-29 17:03:24 -05:00
|
|
|
// TODO destroy the updown
|
|
|
|
(*(s->baseCommitDestroy))(uiControl(s));
|
2015-05-19 11:32:14 -05:00
|
|
|
}
|
|
|
|
|
2015-05-29 18:48:27 -05:00
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
2015-05-19 11:32:14 -05:00
|
|
|
// 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, uiSizing *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 :(
|
|
|
|
// TODO will we need to store a copy of the current position and range for this?
|
|
|
|
static void recreateUpDown(struct spinbox *s)
|
|
|
|
{
|
|
|
|
HWND parent;
|
2015-05-20 11:24:06 -05:00
|
|
|
BOOL preserve = FALSE;
|
|
|
|
intmax_t current;
|
|
|
|
// wine uses this type
|
|
|
|
INT min, max;
|
2015-05-19 11:32:14 -05:00
|
|
|
|
|
|
|
parent = GetAncestor(s->hwnd, GA_PARENT);
|
2015-05-20 11:24:06 -05:00
|
|
|
if (s->updown != NULL) {
|
|
|
|
preserve = TRUE;
|
|
|
|
current = value(s);
|
|
|
|
SendMessageW(s->updown, UDM_GETRANGE32, (WPARAM) (&min), (LPARAM) (&max));
|
2015-05-19 11:32:14 -05:00
|
|
|
if (DestroyWindow(s->updown) == 0)
|
|
|
|
logLastError("error destroying old updown in recreateUpDown()");
|
2015-05-20 11:24:06 -05:00
|
|
|
}
|
|
|
|
s->inhibitChanged = TRUE;
|
2015-05-19 11:32:14 -05:00
|
|
|
s->updown = CreateWindowExW(0,
|
|
|
|
UPDOWN_CLASSW, L"",
|
|
|
|
// no WS_VISIBLE; we set visibility ourselves
|
2015-05-19 20:05:18 -05:00
|
|
|
// TODO tab stop?
|
|
|
|
// TODO maintain Z-order
|
2015-05-19 11:32:14 -05:00
|
|
|
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);
|
2015-05-20 11:24:06 -05:00
|
|
|
if (preserve) {
|
|
|
|
SendMessageW(s->updown, UDM_SETRANGE32, (WPARAM) min, (LPARAM) max);
|
|
|
|
SendMessageW(s->updown, UDM_SETPOS32, 0, (LPARAM) current);
|
|
|
|
}
|
2015-05-19 11:32:14 -05:00
|
|
|
if (uiControlContainerVisible(uiControl(s)))
|
|
|
|
ShowWindow(s->updown, SW_SHOW);
|
2015-05-20 11:24:06 -05:00
|
|
|
s->inhibitChanged = FALSE;
|
2015-05-19 11:32:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void spinboxResize(uiControl *c, intmax_t x, intmax_t y, intmax_t width, intmax_t height, uiSizing *d)
|
|
|
|
{
|
|
|
|
struct spinbox *s = (struct spinbox *) c;
|
|
|
|
|
|
|
|
(*(s->baseResize))(uiControl(s), x, y, width, height, d);
|
|
|
|
recreateUpDown(s);
|
|
|
|
}
|
|
|
|
|
2015-05-19 17:17:30 -05:00
|
|
|
// TODO does it go here relative of other things?
|
|
|
|
static void defaultOnChanged(uiSpinbox *s, void *data)
|
|
|
|
{
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
static intmax_t spinboxValue(uiSpinbox *ss)
|
|
|
|
{
|
|
|
|
struct spinbox *s = (struct spinbox *) ss;
|
|
|
|
|
|
|
|
return value(s);
|
|
|
|
}
|
|
|
|
|
2015-05-20 11:24:06 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-05-19 17:17:30 -05:00
|
|
|
static void spinboxOnChanged(uiSpinbox *ss, void (*f)(uiSpinbox *, void *), void *data)
|
|
|
|
{
|
|
|
|
struct spinbox *s = (struct spinbox *) ss;
|
|
|
|
|
|
|
|
s->onChanged = f;
|
|
|
|
s->onChangedData = data;
|
|
|
|
}
|
|
|
|
|
2015-05-20 12:25:45 -05:00
|
|
|
uiSpinbox *uiNewSpinbox(intmax_t min, intmax_t max)
|
2015-05-19 11:32:14 -05:00
|
|
|
{
|
|
|
|
struct spinbox *s;
|
2015-05-29 17:03:24 -05:00
|
|
|
|
|
|
|
s = (struct spinbox *) uiWindowsNewSingleHWNDControl(uiTypeSpinbox());
|
|
|
|
|
|
|
|
s->hwnd = uiWindowsNewSingleHWNDControl(WS_EX_CLIENTEDGE,
|
|
|
|
L"edit", L"",
|
2015-05-29 18:48:27 -05:00
|
|
|
// TODO ES_NUMBER doesn't allow typing in a leading -
|
2015-05-29 17:03:24 -05:00
|
|
|
ES_AUTOHSCROLL | ES_LEFT | ES_NOHIDESEL | ES_NUMBER | WS_TABSTOP,
|
|
|
|
hInstance, NULL,
|
|
|
|
TRUE);
|
|
|
|
|
2015-05-21 11:07:11 -05:00
|
|
|
uiWindowsRegisterWM_COMMANDHandler(s->hwnd, onWM_COMMAND, uiControl(s));
|
2015-05-19 11:32:14 -05:00
|
|
|
|
|
|
|
recreateUpDown(s);
|
2015-05-20 12:25:45 -05:00
|
|
|
s->inhibitChanged = TRUE;
|
|
|
|
SendMessageW(s->updown, UDM_SETRANGE32, (WPARAM) min, (LPARAM) max);
|
|
|
|
SendMessageW(s->updown, UDM_SETPOS32, 0, (LPARAM) min);
|
|
|
|
s->inhibitChanged = FALSE;
|
2015-05-19 11:32:14 -05:00
|
|
|
|
2015-05-19 17:17:30 -05:00
|
|
|
s->onChanged = defaultOnChanged;
|
|
|
|
|
2015-05-29 18:48:27 -05:00
|
|
|
uiControl(s)->Handle = spinboxHandle;
|
2015-05-19 11:32:14 -05:00
|
|
|
uiControl(s)->PreferredSize = spinboxPreferredSize;
|
|
|
|
s->baseResize = uiControl(s)->Resize;
|
|
|
|
uiControl(s)->Resize = spinboxResize;
|
2015-05-29 17:03:24 -05:00
|
|
|
s->baseCommitDestroy = uiControl(s)->CommitDestroy;
|
|
|
|
uiControl(s)->CommitDestroy = spinboxCommitDestroy;
|
2015-05-19 11:32:14 -05:00
|
|
|
|
2015-05-19 17:17:30 -05:00
|
|
|
uiSpinbox(s)->Value = spinboxValue;
|
2015-05-20 11:24:06 -05:00
|
|
|
uiSpinbox(s)->SetValue = spinboxSetValue;
|
2015-05-19 17:17:30 -05:00
|
|
|
uiSpinbox(s)->OnChanged = spinboxOnChanged;
|
|
|
|
|
2015-05-19 11:32:14 -05:00
|
|
|
return uiSpinbox(s);
|
|
|
|
}
|