From 82bf34abe48b75809d07172fc19baa3b4273c67b Mon Sep 17 00:00:00 2001 From: Angelo Haller Date: Thu, 31 May 2018 09:56:52 -0500 Subject: [PATCH 1/2] Add uiSliderSetRange function to modify the uiSlider range after creation. Some applications need to modify the slider range at runtime. Instead of re-creating a slider with a new range, provide a function to modify the range of an existing uiSlider. --- darwin/slider.m | 12 ++++++++++++ ui.h | 1 + unix/slider.c | 14 ++++++++++++++ windows/slider.cpp | 12 ++++++++++++ 4 files changed, 39 insertions(+) diff --git a/darwin/slider.m b/darwin/slider.m index 6f5c5a76..945ec7b4 100644 --- a/darwin/slider.m +++ b/darwin/slider.m @@ -109,6 +109,18 @@ static void defaultOnChanged(uiSlider *s, void *data) // do nothing } +void uiSliderSetRange(uiSlider *s, int min, int max) +{ + if (min >= max) { + int temp = min; + min = max; + max = temp; + } + + [s->slider setMinValue:min]; + [s->slider setMaxValue:max]; +} + uiSlider *uiNewSlider(int min, int max) { uiSlider *s; diff --git a/ui.h b/ui.h index 40aea949..94193b04 100644 --- a/ui.h +++ b/ui.h @@ -210,6 +210,7 @@ typedef struct uiSlider uiSlider; _UI_EXTERN int uiSliderValue(uiSlider *s); _UI_EXTERN void uiSliderSetValue(uiSlider *s, int value); _UI_EXTERN void uiSliderOnChanged(uiSlider *s, void (*f)(uiSlider *s, void *data), void *data); +_UI_EXTERN void uiSliderSetRange(uiSlider *s, int min, int max); _UI_EXTERN uiSlider *uiNewSlider(int min, int max); typedef struct uiProgressBar uiProgressBar; diff --git a/unix/slider.c b/unix/slider.c index 7f0cc24a..f989a11f 100644 --- a/unix/slider.c +++ b/unix/slider.c @@ -44,6 +44,20 @@ void uiSliderOnChanged(uiSlider *s, void (*f)(uiSlider *, void *), void *data) s->onChangedData = data; } +void uiSliderSetRange(uiSlider *s, int min, int max) +{ + if (min >= max) { + int temp = min; + min = max; + max = temp; + } + + // we need to inhibit sending of ::value-changed because this WILL send a ::value-changed otherwise + g_signal_handler_block(s->range, s->onChangedSignal); + gtk_range_set_range(s->range, min, max); + g_signal_handler_unblock(s->range, s->onChangedSignal); +} + uiSlider *uiNewSlider(int min, int max) { uiSlider *s; diff --git a/windows/slider.cpp b/windows/slider.cpp index 5c671dda..ed6265e5 100644 --- a/windows/slider.cpp +++ b/windows/slider.cpp @@ -68,6 +68,18 @@ void uiSliderOnChanged(uiSlider *s, void (*f)(uiSlider *, void *), void *data) s->onChangedData = data; } +void uiSliderSetRange(uiSlider *s, int min, int max) +{ + if (min >= max) { + int temp = min; + min = max; + max = temp; + } + + SendMessageW(s->hwnd, TBM_SETRANGEMIN, (WPARAM) TRUE, (LPARAM) min); + SendMessageW(s->hwnd, TBM_SETRANGEMAX, (WPARAM) TRUE, (LPARAM) max); +} + uiSlider *uiNewSlider(int min, int max) { uiSlider *s; From ac7955452875dd166b5be7e3cdedb09431f8ac7c Mon Sep 17 00:00:00 2001 From: Angelo Haller Date: Thu, 31 May 2018 12:54:19 -0500 Subject: [PATCH 2/2] Add test case for uiSliderSetRange on page4. --- test/page4.c | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/test/page4.c b/test/page4.c index ce4a6afb..527460b2 100644 --- a/test/page4.c +++ b/test/page4.c @@ -4,6 +4,9 @@ static uiSpinbox *spinbox; static uiSlider *slider; static uiProgressBar *pbar; +static uiSpinbox *spinboxFrom; +static uiSpinbox *spinboxTo; + #define CHANGED(what) \ static void on ## what ## Changed(ui ## what *this, void *data) \ @@ -75,6 +78,24 @@ static void selectNone(uiButton *b, void *data) uiRadioButtonsSetSelected(rb, -1); } +static void setSliderRange(uiSpinbox *spinbox, void *data) +{ + uiSlider *s = data; + + uiSliderSetRange(s, uiSpinboxValue(spinboxFrom), uiSpinboxValue(spinboxTo)); +} + +static void onRangeSliderChanged(uiSlider *s, void *data) +{ + char str[128]; + uiLabel *lbl = data; + + // wonderful, vanilla snprintf() isn't in visual studio 2013 - http://blogs.msdn.com/b/vcblog/archive/2013/07/19/c99-library-support-in-visual-studio-2013.aspx + // we can't use _snprintf() in the test suite because that's msvc-only, so oops. sprintf() it is. + sprintf(str, "%d", uiSliderValue(s)); + uiLabelSetText(lbl, str); +} + uiBox *makePage4(void) { uiBox *page4; @@ -82,6 +103,7 @@ uiBox *makePage4(void) uiSpinbox *xsb; uiButton *b; uiSlider *xsl; + uiLabel *lbl; page4 = newVerticalBox(); @@ -122,6 +144,24 @@ uiBox *makePage4(void) uiBoxAppend(page4, uiControl(uiNewHorizontalSeparator()), 0); + lbl = uiNewLabel("100"); + uiBoxAppend(page4, uiControl(lbl), 0); + hbox = newHorizontalBox(); + spinboxFrom = uiNewSpinbox(0, 1000); + uiSpinboxSetValue(spinboxFrom, 100); + uiBoxAppend(hbox, uiControl(spinboxFrom), 1); + xsl = uiNewSlider(100, 200); + uiSpinboxOnChanged(spinboxFrom, setSliderRange, xsl); + uiBoxAppend(hbox, uiControl(xsl), 1); + uiSliderOnChanged(xsl, onRangeSliderChanged, lbl); + spinboxTo = uiNewSpinbox(0, 1000); + uiSpinboxSetValue(spinboxTo, 200); + uiSpinboxOnChanged(spinboxTo, setSliderRange, xsl); + uiBoxAppend(hbox, uiControl(spinboxTo), 1); + uiBoxAppend(page4, uiControl(hbox), 0); + + uiBoxAppend(page4, uiControl(uiNewHorizontalSeparator()), 0); + cbox = uiNewCombobox(); uiComboboxAppend(cbox, "Item 1"); uiComboboxAppend(cbox, "Item 2");