From 44153a32dfa8421da1675de94659bf4041540454 Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Tue, 19 May 2015 18:17:30 -0400 Subject: [PATCH] More uiSpinbox functionality. --- redo/test/page4.c | 8 +++++ redo/ui.idl | 3 ++ redo/windows/spinbox.c | 68 +++++++++++++++++++++++++++++++++++------- 3 files changed, 68 insertions(+), 11 deletions(-) diff --git a/redo/test/page4.c b/redo/test/page4.c index 7c60b73e..f5d655a0 100644 --- a/redo/test/page4.c +++ b/redo/test/page4.c @@ -3,6 +3,13 @@ static uiSpinbox *spinbox; +#define CHANGED(what) \ + static void on ## what ## Changed(ui ## what *this, void *data) \ + { \ + printf("on %s changed\n", #what); \ + } +CHANGED(Spinbox) + uiBox *makePage4(void) { uiBox *page4; @@ -10,6 +17,7 @@ uiBox *makePage4(void) page4 = newVerticalBox(); spinbox = uiNewSpinbox(); + uiSpinboxOnChanged(spinbox, onSpinboxChanged, NULL); uiBoxAppend(page4, uiControl(spinbox), 0); return page4; diff --git a/redo/ui.idl b/redo/ui.idl index 1fc94fec..1566159e 100644 --- a/redo/ui.idl +++ b/redo/ui.idl @@ -142,6 +142,9 @@ interface Group from Control { func NewGroup(text *const char) *Group; interface Spinbox from Control { + func Value(void) intmax_t; + // TODO SetValue() + func OnChanged(f *func(s *Spinbox, data *void), data *void); }; func NewSpinbox(void) *Spinbox; diff --git a/redo/windows/spinbox.c b/redo/windows/spinbox.c index 791adaeb..ef3677cb 100644 --- a/redo/windows/spinbox.c +++ b/redo/windows/spinbox.c @@ -6,25 +6,45 @@ struct spinbox { HWND hwnd; HWND updown; void (*baseResize)(uiControl *, intmax_t, intmax_t, intmax_t, intmax_t, uiSizing *); + void (*onChanged)(uiSpinbox *, void *); + void *onChangedData; + BOOL inhibitChanged; }; +// 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. + // TODO wine only? + 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, WORD code, LRESULT *lResult) { struct spinbox *s = (struct spinbox *) c; - BOOL neededCap = FALSE; - LRESULT val; if (code != EN_CHANGE) return FALSE; - // 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. - // TODO if we have change events, we need to make sure doing this doesn't trigger a double event - // TODO wine only? - val = SendMessageW(s->updown, UDM_GETPOS32, 0, (LPARAM) (&neededCap)); - if (neededCap) - SendMessageW(s->updown, UDM_SETPOS32, 0, (LPARAM) val); - return FALSE; + if (s->inhibitChanged) + return FALSE; + // value() does the work for us + value(s); + (*(s->onChanged))(uiSpinbox(s), s->onChangedData); + return TRUE; } static BOOL onWM_NOTIFY(uiControl *c, NMHDR *nm, LRESULT *lResult) @@ -86,6 +106,27 @@ static void spinboxResize(uiControl *c, intmax_t x, intmax_t y, intmax_t width, recreateUpDown(s); } +// 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); +} + +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(void) { struct spinbox *s; @@ -111,9 +152,14 @@ uiSpinbox *uiNewSpinbox(void) recreateUpDown(s); + s->onChanged = defaultOnChanged; + uiControl(s)->PreferredSize = spinboxPreferredSize; s->baseResize = uiControl(s)->Resize; uiControl(s)->Resize = spinboxResize; + uiSpinbox(s)->Value = spinboxValue; + uiSpinbox(s)->OnChanged = spinboxOnChanged; + return uiSpinbox(s); }