From 99545e8775a7c53e957fa4f9e09e3fcfbcf40688 Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Thu, 16 Jun 2016 17:43:04 -0400 Subject: [PATCH] Fixed up the progressbar changes. --- README.md | 1 + darwin/progressbar.m | 8 +++----- test/page13.c | 14 +++++++------- unix/progressbar.c | 23 ++++++++++++----------- windows/progressbar.cpp | 24 ++++++++++-------------- 5 files changed, 33 insertions(+), 37 deletions(-) diff --git a/README.md b/README.md index e3bba8e1..8a71e539 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,7 @@ This README is being written.
* Added `uiWindowFullscreen()` and `uiWindowSetFullscreen()` to allow making fullscreen uiWindows, taking advantage of OS facilities for fullscreen and without changing the screen resolution (!). * Added `uiWindowBorderless()` and `uiWindowSetBorderless()` for allowing borderless uiWindows. * Added `uiMainSteps()`. You call this instead of `uiMain()` if you want to run the main loop yourself. You pass in a function that will be called; within that function, you call `uiMainStep()` repeatedly until it returns 0, doing whatever you need to do in the meantime. (This was needed because just having `uiMainStep()` by itself only worked on some systems.) + * Added `uiProgressBarValue()` and allowed passing -1 to `uiProgressBarSetValue()` to make an indeterminate progress bar. Thanks to @emersion. * **15 June 2016** * Added `uiFormDelete()`; thanks to @emersion. diff --git a/darwin/progressbar.m b/darwin/progressbar.m index 87a9c8d2..b5382281 100644 --- a/darwin/progressbar.m +++ b/darwin/progressbar.m @@ -29,11 +29,9 @@ uiDarwinControlAllDefaults(uiProgressBar, pi) int uiProgressBarValue(uiProgressBar *p) { - if ([p->pi getIndeterminate]) { + if ([p->pi isIndeterminate]) return -1; - } - - return (int) [p->pi getDoubleValue]; + return [p->pi doubleValue]; } void uiProgressBarSetValue(uiProgressBar *p, int value) @@ -44,7 +42,7 @@ void uiProgressBarSetValue(uiProgressBar *p, int value) return; } - if ([p->pi getIndeterminate]) { + if ([p->pi isIndeterminate]) { [p->pi setIndeterminate:NO]; [p->pi stopAnimation:p->pi]; } diff --git a/test/page13.c b/test/page13.c index 6eb19f29..cda91ce7 100644 --- a/test/page13.c +++ b/test/page13.c @@ -78,14 +78,13 @@ static void showHide(uiButton *b, void *data) static void setIndeterminate(uiButton *b, void *data) { uiProgressBar *p = uiProgressBar(data); + int value; - int value = uiProgressBarValue(p); - if (value == -1) { - value = 0; - } else { + value = uiProgressBarValue(p); + if (value == -1) + value = 50; + else value = -1; - } - uiProgressBarSetValue(p, value); } @@ -139,8 +138,9 @@ uiBox *makePage13(void) uiFormAppend(f, "MLE", uiControl(uiNewMultilineEntry()), 1); p = uiNewProgressBar(); + uiProgressBarSetValue(p, 50); uiBoxAppend(page13, uiControl(p), 0); - b = uiNewButton("Toggle indeterminate"); + b = uiNewButton("Toggle Indeterminate"); uiButtonOnClicked(b, setIndeterminate, p); uiBoxAppend(page13, uiControl(b), 0); diff --git a/unix/progressbar.c b/unix/progressbar.c index 9e3fd4a4..08b45f33 100644 --- a/unix/progressbar.c +++ b/unix/progressbar.c @@ -5,7 +5,8 @@ struct uiProgressBar { uiUnixControl c; GtkWidget *widget; GtkProgressBar *pbar; - int indeterminate; + gboolean indeterminate; + guint pulser; }; uiUnixControlAllDefaults(uiProgressBar) @@ -14,35 +15,35 @@ int uiProgressBarValue(uiProgressBar *p) { if (p->indeterminate) return -1; - return (int) (gtk_progress_bar_get_fraction(p->pbar) * 100); } -gboolean uiProgressBarPulse(void* data) +static gboolean pulse(void* data) { - uiProgressBar *p = (uiProgressBar*) data; - - if (!GTK_IS_WIDGET(p->pbar) || !p->indeterminate) - return 0; + uiProgressBar *p = uiProgressBar(data); gtk_progress_bar_pulse(p->pbar); - return 1; + return TRUE; } void uiProgressBarSetValue(uiProgressBar *p, int value) { if (value == -1) { if (!p->indeterminate) { - p->indeterminate = 1; - g_timeout_add(100, uiProgressBarPulse, p); + p->indeterminate = TRUE; + // TODO verify the timeout + p->pulser = g_timeout_add(100, pulse, p); } return; } + if (p->indeterminate) { + p->indeterminate = FALSE; + g_source_remove(p->pulser); + } if (value < 0 || value > 100) userbug("Value %d is out of range for a uiProgressBar.", value); - p->indeterminate = 0; gtk_progress_bar_set_fraction(p->pbar, ((gdouble) value) / 100); } diff --git a/windows/progressbar.cpp b/windows/progressbar.cpp index 2d155de1..3750eb6a 100644 --- a/windows/progressbar.cpp +++ b/windows/progressbar.cpp @@ -26,14 +26,13 @@ static void uiProgressBarMinimumSize(uiWindowsControl *c, int *width, int *heigh *height = y; } +#define indeterminate(p) ((getStyle(p->hwnd) & PBS_MARQUEE) != 0) + int uiProgressBarValue(uiProgressBar *p) { - LONG_PTR style = GetWindowLongPtr(p->hwnd, GWL_STYLE); - if ((style & PBS_MARQUEE) != 0) { + if (indeterminate(p)) return -1; - } - - return (int) SendMessage(p->hwnd, PBM_GETPOS, 0, 0); + return SendMessage(p->hwnd, PBM_GETPOS, 0, 0); } // unfortunately, as of Vista progress bars have a forced animation on increase @@ -42,19 +41,16 @@ int uiProgressBarValue(uiProgressBar *p) // it's not ideal/perfect, but it will have to do void uiProgressBarSetValue(uiProgressBar *p, int value) { - LONG_PTR style = GetWindowLongPtr(p->hwnd, GWL_STYLE); - if (value == -1) { - if ((style & PBS_MARQUEE) == 0) { - SetWindowLongPtr(p->hwnd, GWL_STYLE, style | PBS_MARQUEE); - SendMessage(p->hwnd, PBM_SETMARQUEE, 1, 0); + if (!indeterminate(p)) { + setStyle(p->hwnd, getStyle(p->hwnd) | PBS_MARQUEE); + SendMessageW(p->hwnd, PBM_SETMARQUEE, (WPARAM) TRUE, 0); } return; } - - if ((style & PBS_MARQUEE) != 0) { - SetWindowLongPtr(p->hwnd, GWL_STYLE, style ^ PBS_MARQUEE); - SendMessage(p->hwnd, PBM_SETMARQUEE, 0, 0); + if (indeterminate(p)) { + SendMessageW(p->hwnd, PBM_SETMARQUEE, (WPARAM) FALSE, 0); + setStyle(p->hwnd, getStyle(p->hwnd) & ~PBS_MARQUEE); } if (value < 0 || value > 100)