2015-05-19 11:32:14 -05:00
|
|
|
// 8 april 2015
|
|
|
|
#include "uipriv_windows.h"
|
|
|
|
|
2015-08-31 06:28:15 -05:00
|
|
|
struct uiSpinbox {
|
|
|
|
uiWindowsControl c;
|
2015-05-19 11:32:14 -05:00
|
|
|
HWND hwnd;
|
|
|
|
HWND updown;
|
2015-05-19 17:17:30 -05:00
|
|
|
void (*onChanged)(uiSpinbox *, void *);
|
|
|
|
void *onChangedData;
|
|
|
|
BOOL inhibitChanged;
|
2015-05-19 11:32:14 -05:00
|
|
|
};
|
|
|
|
|
2015-08-31 06:28:15 -05:00
|
|
|
static void onDestroy(uiSpinbox *);
|
|
|
|
|
2015-08-31 11:33:44 -05:00
|
|
|
uiWindowsDefineControlWithOnDestroy(
|
|
|
|
uiSpinbox, // type name
|
|
|
|
uiSpinboxType, // type function
|
|
|
|
onDestroy(this); // on destroy
|
|
|
|
)
|
2015-05-29 17:03:24 -05:00
|
|
|
|
2015-05-19 17:17:30 -05:00
|
|
|
// utility functions
|
|
|
|
|
2015-08-31 06:28:15 -05:00
|
|
|
static intmax_t value(uiSpinbox *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.
|
|
|
|
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
|
|
|
{
|
2015-08-31 06:28:15 -05:00
|
|
|
uiSpinbox *s = (uiSpinbox *) c;
|
2015-06-05 15:13:03 -05:00
|
|
|
WCHAR *wtext;
|
2015-05-19 17:17:30 -05:00
|
|
|
|
|
|
|
if (code != EN_CHANGE)
|
|
|
|
return FALSE;
|
|
|
|
if (s->inhibitChanged)
|
|
|
|
return FALSE;
|
2015-06-05 15:13:03 -05:00
|
|
|
// 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;
|
|
|
|
}
|
2015-05-19 17:17:30 -05:00
|
|
|
// value() does the work for us
|
|
|
|
value(s);
|
2015-08-31 06:28:15 -05:00
|
|
|
(*(s->onChanged))(s, s->onChangedData);
|
2015-05-19 17:17:30 -05:00
|
|
|
return TRUE;
|
2015-05-19 11:32:14 -05:00
|
|
|
}
|
|
|
|
|
2015-08-31 06:28:15 -05:00
|
|
|
static void onDestroy(uiSpinbox *s)
|
2015-05-19 11:32:14 -05:00
|
|
|
{
|
2015-05-21 11:07:11 -05:00
|
|
|
uiWindowsUnregisterWM_COMMANDHandler(s->hwnd);
|
2015-06-02 09:35:10 -05:00
|
|
|
if (DestroyWindow(s->updown) == 0)
|
|
|
|
logLastError("error destroying updown in spinboxCommitDestroy()");
|
2015-05-29 18:48:27 -05:00
|
|
|
}
|
|
|
|
|
2015-08-31 06:28:15 -05:00
|
|
|
// TODO set spinbox parent
|
2015-06-05 15:22:50 -05:00
|
|
|
|
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
|
|
|
|
|
2015-08-31 16:50:23 -05:00
|
|
|
static void minimumSize(uiWindowsControl *c, uiWindowsSizing *d, intmax_t *width, intmax_t *height)
|
2015-05-19 11:32:14 -05:00
|
|
|
{
|
2015-08-31 06:28:15 -05:00
|
|
|
*width = uiWindowsDlgUnitsToX(entryWidth, d->BaseX);
|
|
|
|
*height = uiWindowsDlgUnitsToY(entryHeight, d->BaseY);
|
2015-05-19 11:32:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 :(
|
2015-08-31 06:28:15 -05:00
|
|
|
static void recreateUpDown(uiSpinbox *s)
|
2015-05-19 11:32:14 -05:00
|
|
|
{
|
2015-08-31 06:28:15 -05:00
|
|
|
/* TODO
|
2015-05-19 11:32:14 -05:00
|
|
|
HWND parent;
|
2015-05-20 11:24:06 -05:00
|
|
|
BOOL preserve = FALSE;
|
|
|
|
intmax_t current;
|
2015-06-06 15:01:32 -05:00
|
|
|
// Microsoft's commctrl.h says to use this type
|
2015-05-20 11:24:06 -05:00
|
|
|
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-06-05 15:35:56 -05:00
|
|
|
// up-down control should not be a tab stop
|
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-06-03 14:58:47 -05:00
|
|
|
// preserve the z-order
|
|
|
|
uiWindowsUtilSetZOrder(s->updown, (uintptr_t) (s->hwnd));
|
2015-05-19 11:32:14 -05:00
|
|
|
if (uiControlContainerVisible(uiControl(s)))
|
2015-06-01 12:02:43 -05:00
|
|
|
uiWindowsUtilShow(s->updown);
|
2015-05-20 11:24:06 -05:00
|
|
|
s->inhibitChanged = FALSE;
|
2015-08-31 06:28:15 -05:00
|
|
|
*/
|
2015-05-19 11:32:14 -05:00
|
|
|
}
|
|
|
|
|
2015-09-01 06:21:18 -05:00
|
|
|
static void spinboxRelayout(uiWindowsControl *c, intmax_t x, intmax_t y, intmax_t width, intmax_t height)
|
2015-05-19 11:32:14 -05:00
|
|
|
{
|
2015-08-31 06:28:15 -05:00
|
|
|
/* TODO
|
|
|
|
uiSpinbox *s = uiSpinbox(c);
|
2015-05-19 11:32:14 -05:00
|
|
|
|
2015-06-05 15:22:50 -05:00
|
|
|
moveWindow(s->hwnd, x, y, width, height, d);
|
2015-05-19 11:32:14 -05:00
|
|
|
recreateUpDown(s);
|
2015-08-31 06:28:15 -05:00
|
|
|
*/
|
2015-05-19 11:32:14 -05:00
|
|
|
}
|
|
|
|
|
2015-08-31 06:28:15 -05:00
|
|
|
/* TODO
|
2015-06-05 15:22:50 -05:00
|
|
|
static uintptr_t spinboxStartZOrder(uiControl *c)
|
|
|
|
{
|
|
|
|
struct spinbox *s = (struct spinbox *) c;
|
|
|
|
|
|
|
|
return uiWindowsUtilStartZOrder(s->hwnd);
|
|
|
|
}
|
|
|
|
|
2015-06-03 14:58:47 -05:00
|
|
|
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);
|
|
|
|
}
|
2015-08-31 06:28:15 -05:00
|
|
|
*/
|
2015-06-05 15:22:50 -05:00
|
|
|
|
2015-05-19 17:17:30 -05:00
|
|
|
static void defaultOnChanged(uiSpinbox *s, void *data)
|
|
|
|
{
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
2015-08-31 06:28:15 -05:00
|
|
|
intmax_t uiSpinboxValue(uiSpinbox *s)
|
2015-05-19 17:17:30 -05:00
|
|
|
{
|
|
|
|
return value(s);
|
|
|
|
}
|
|
|
|
|
2015-08-31 06:28:15 -05:00
|
|
|
void uiSpinboxSetValue(uiSpinbox *s, intmax_t value)
|
2015-05-20 11:24:06 -05:00
|
|
|
{
|
|
|
|
s->inhibitChanged = TRUE;
|
|
|
|
SendMessageW(s->updown, UDM_SETPOS32, 0, (LPARAM) value);
|
|
|
|
s->inhibitChanged = FALSE;
|
|
|
|
}
|
|
|
|
|
2015-08-31 06:28:15 -05:00
|
|
|
void uiSpinboxOnChanged(uiSpinbox *s, void (*f)(uiSpinbox *, void *), void *data)
|
2015-05-19 17:17:30 -05:00
|
|
|
{
|
|
|
|
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
|
|
|
{
|
2015-08-31 06:28:15 -05:00
|
|
|
uiSpinbox *s;
|
2015-05-29 17:03:24 -05:00
|
|
|
|
2015-06-05 15:52:21 -05:00
|
|
|
if (min >= max)
|
|
|
|
complain("error: min >= max in uiNewSpinbox()");
|
|
|
|
|
2015-08-31 06:28:15 -05:00
|
|
|
s = (uiSpinbox *) uiNewControl(uiSpinboxType());
|
2015-05-29 17:03:24 -05:00
|
|
|
|
2015-08-31 11:33:44 -05:00
|
|
|
s->hwnd = uiWindowsEnsureCreateControlHWND(WS_EX_CLIENTEDGE,
|
2015-05-29 17:03:24 -05:00
|
|
|
L"edit", L"",
|
2015-06-05 15:13:03 -05:00
|
|
|
// don't use ES_NUMBER; it doesn't allow typing in a leading -
|
|
|
|
ES_AUTOHSCROLL | ES_LEFT | ES_NOHIDESEL | WS_TABSTOP,
|
2015-05-29 17:03:24 -05:00
|
|
|
hInstance, NULL,
|
|
|
|
TRUE);
|
|
|
|
|
2015-05-21 11:07:11 -05:00
|
|
|
uiWindowsRegisterWM_COMMANDHandler(s->hwnd, onWM_COMMAND, uiControl(s));
|
2015-08-31 06:28:15 -05:00
|
|
|
uiSpinboxOnChanged(s, defaultOnChanged, NULL);
|
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-08-31 11:33:44 -05:00
|
|
|
uiWindowsFinishNewControl(s, uiSpinbox);
|
2015-09-01 06:21:18 -05:00
|
|
|
uiWindowsControl(s)->Relayout = spinboxRelayout;
|
2015-08-31 06:28:15 -05:00
|
|
|
|
|
|
|
return s;
|
2015-05-19 11:32:14 -05:00
|
|
|
}
|