libui/windows/spinbox.cpp

212 lines
5.7 KiB
C++
Raw Normal View History

// 8 april 2015
#include "uipriv_windows.hpp"
struct uiSpinbox {
uiWindowsControl c;
HWND hwnd;
HWND edit;
HWND updown;
2015-05-19 17:17:30 -05:00
void (*onChanged)(uiSpinbox *, void *);
void *onChangedData;
BOOL inhibitChanged;
};
2015-05-19 17:17:30 -05:00
// utility functions
static intmax_t value(uiSpinbox *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));
2015-05-19 17:17:30 -05:00
if (neededCap) {
s->inhibitChanged = TRUE;
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
static BOOL onWM_COMMAND(uiControl *c, HWND hwnd, WORD code, LRESULT *lResult)
2015-05-19 17:17:30 -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->edit);
2015-06-05 15:13:03 -05:00
if (wcscmp(wtext, L"-") == 0) {
uiFree(wtext);
return TRUE;
}
2015-09-02 18:46:10 -05:00
uiFree(wtext);
2015-05-19 17:17:30 -05:00
// value() does the work for us
value(s);
(*(s->onChanged))(s, s->onChangedData);
2015-05-19 17:17:30 -05:00
return TRUE;
}
static void uiSpinboxDestroy(uiControl *c)
{
uiSpinbox *s = uiSpinbox(c);
uiWindowsUnregisterWM_COMMANDHandler(s->hwnd);
uiWindowsEnsureDestroyWindow(s->updown);
uiWindowsEnsureDestroyWindow(s->edit);
uiWindowsEnsureDestroyWindow(s->hwnd);
uiFreeControl(uiControl(s));
}
// TODO SyncEnableState
uiWindowsControlAllDefaultsExceptDestroy(uiSpinbox)
// from http://msdn.microsoft.com/en-us/library/windows/desktop/dn742486.aspx#sizingandspacing
// TODO reduce this?
#define entryWidth 107 /* this is actually the shorter progress bar width, but Microsoft only indicates as wide as necessary */
#define entryHeight 14
static void uiSpinboxMinimumSize(uiWindowsControl *c, intmax_t *width, intmax_t *height)
{
uiSpinbox *s = uiSpinbox(c);
uiWindowsSizing sizing;
int x, y;
x = entryWidth;
y = entryHeight;
// note that we go by the edit here
uiWindowsGetSizing(s->edit, &sizing);
uiWindowsSizingDlgUnitsToPixels(&sizing, &x, &y);
*width = x;
*height = y;
}
static void spinboxArrangeChildren(uiSpinbox *s)
{
LONG_PTR controlID;
HWND insertAfter;
controlID = 100;
insertAfter = NULL;
uiWindowsEnsureAssignControlIDZOrder(s->edit, &controlID, &insertAfter);
uiWindowsEnsureAssignControlIDZOrder(s->updown, &controlID, &insertAfter);
}
// 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(uiSpinbox *s)
{
2015-05-20 11:24:06 -05:00
BOOL preserve = FALSE;
intmax_t current;
// Microsoft's commctrl.h says to use this type
2015-05-20 11:24:06 -05:00
INT min, max;
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));
uiWindowsEnsureDestroyWindow(s->updown);
2015-05-20 11:24:06 -05:00
}
s->inhibitChanged = TRUE;
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
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,
s->hwnd, NULL, hInstance, NULL);
if (s->updown == NULL)
logLastError(L"error creating updown");
SendMessageW(s->updown, UDM_SETBUDDY, (WPARAM) (s->edit), 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);
}
// preserve the Z-order
spinboxArrangeChildren(s);
// TODO properly show/enable
ShowWindow(s->updown, SW_SHOW);
2015-05-20 11:24:06 -05:00
s->inhibitChanged = FALSE;
}
static void spinboxRelayout(uiSpinbox *s)
{
RECT r;
// make the edit fill the container first; the new updown will resize it
uiWindowsEnsureGetClientRect(s->hwnd, &r);
uiWindowsEnsureMoveWindowDuringResize(s->edit, r.left, r.top, r.right - r.left, r.bottom - r.top);
recreateUpDown(s);
}
2015-05-19 17:17:30 -05:00
static void defaultOnChanged(uiSpinbox *s, void *data)
{
// do nothing
}
intmax_t uiSpinboxValue(uiSpinbox *s)
2015-05-19 17:17:30 -05:00
{
return value(s);
}
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;
}
void uiSpinboxOnChanged(uiSpinbox *s, void (*f)(uiSpinbox *, void *), void *data)
2015-05-19 17:17:30 -05:00
{
s->onChanged = f;
s->onChangedData = data;
}
static void onResize(uiWindowsControl *c)
{
spinboxRelayout(uiSpinbox(c));
}
2015-05-20 12:25:45 -05:00
uiSpinbox *uiNewSpinbox(intmax_t min, intmax_t max)
{
uiSpinbox *s;
2015-06-05 15:52:21 -05:00
if (min >= max)
complain("error: min >= max in uiNewSpinbox()");
uiWindowsNewControl(uiSpinbox, s);
s->hwnd = uiWindowsMakeContainer(uiWindowsControl(s), onResize);
s->edit = uiWindowsEnsureCreateControlHWND(WS_EX_CLIENTEDGE,
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,
hInstance, NULL,
TRUE);
uiWindowsEnsureSetParentHWND(s->edit, s->hwnd);
uiWindowsRegisterWM_COMMANDHandler(s->hwnd, onWM_COMMAND, uiControl(s));
uiSpinboxOnChanged(s, defaultOnChanged, NULL);
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;
return s;
}