From 0178a75c7ded044a4586e5ceeb59b4cfb5555511 Mon Sep 17 00:00:00 2001 From: Julien Danjou Date: Wed, 22 Oct 2008 10:25:07 +0200 Subject: [PATCH] graph, progressbar: fix bar/plot additions Signed-off-by: Julien Danjou --- widgets/graph.c | 47 +++++++++++++++++++++------------------ widgets/progressbar.c | 61 +++++++++++++++++++++++++++------------------------ 2 files changed, 58 insertions(+), 50 deletions(-) diff --git a/widgets/graph.c b/widgets/graph.c index ec107dcf..1300a405 100644 --- a/widgets/graph.c +++ b/widgets/graph.c @@ -122,9 +122,32 @@ graph_plot_add(graph_data_t *d, const char *title) plot.vertical_gradient = true; plot_array_append(&d->plots, plot); + return &d->plots.tab[d->plots.len - 1]; } +/** Get the plot, and create one if it does not exist. + * \param d The graph private data. + * \param title The plot title. + * \return A maybe new plot. + */ +static plot_t * +graph_plot_get(graph_data_t *d, const char *title) +{ + plot_t *plot; + + /* check if this section is defined already */ + for(int j = 0; j < d->plots.len; j++) + { + plot = &d->plots.tab[j]; + if(!a_strcmp(title, plot->title)) + return plot; + } + + /* no plot found -> create one */ + return graph_plot_add(d, title); +} + /** Draw a graph widget. * \param ctx The draw context. * \param screen The screen number. @@ -290,16 +313,7 @@ luaA_graph_plot_properties_set(lua_State *L) title = luaL_checkstring(L, 2); luaA_checktable(L, 3); - for(int j = 0; j < d->plots.len; j++) - { - plot = &d->plots.tab[j]; - if(!a_strcmp(title, plot->title)) - break; - } - - /* no plot found -> create one */ - if(!plot) - plot = graph_plot_add(d, title); + plot = graph_plot_get(d, title); if((buf = luaA_getopt_lstring(L, 3, "fg", NULL, &len))) reqs[++reqs_nbr] = xcolor_init_unchecked(&plot->color_start, buf, len); @@ -359,16 +373,7 @@ luaA_graph_plot_data_add(lua_State *L) float value; int i; - for(int j = 0; j < d->plots.len; j++) - { - plot = &d->plots.tab[j]; - if(!a_strcmp(title, plot->title)) - break; - } - - /* no plot found -> create one */ - if(!plot) - plot = graph_plot_add(d, title); + plot = graph_plot_get(d, title); /* assign incoming value */ value = MAX(luaL_checknumber(L, 3), 0); @@ -426,7 +431,7 @@ luaA_graph_plot_data_add(lua_State *L) * \return The number of elements pushed on stack. * \luastack * \lfield plot_properties_set A function to set plot properties. - * \lfield plot_add_add A function to add data to a plot. + * \lfield plot_data_add A function to add data to a plot. * \lfield height Graph height. * \lfield widget Graph width. * \lfield bg Background color. diff --git a/widgets/progressbar.c b/widgets/progressbar.c index c56a648b..42a5436a 100644 --- a/widgets/progressbar.c +++ b/widgets/progressbar.c @@ -87,11 +87,12 @@ typedef struct } progressbar_data_t; /** Add a new bar to the progressbar private data structure. - * \param d The private data structure. - * \param title The graph title. + * \param bars The bar array. + * \param title The bar title. + * \return The new bar. */ static bar_t * -progressbar_bar_add(progressbar_data_t *d, const char *title) +progressbar_bar_add(bar_array_t *bars, const char *title) { bar_t bar; @@ -105,9 +106,31 @@ progressbar_bar_add(progressbar_data_t *d, const char *title) bar.max_value = 100.0; /* append the bar in the list */ - bar_array_append(&d->bars, bar); + bar_array_append(bars, bar); - return &d->bars.tab[d->bars.len - 1]; + return &bars->tab[bars->len - 1]; +} + +/** Get the bar, and create one if it does not exist. + * \param bars The bar array. + * \param title The bar title. + * \return A maybe new bar. + */ +static bar_t * +progressbar_bar_get(bar_array_t *bars, const char *title) +{ + bar_t *bar; + + /* check if this section is defined already */ + for(int j = 0; j < bars->len; j++) + { + bar = &bars->tab[j]; + if(!a_strcmp(title, bar->title)) + return bar; + } + + /* no bar found -> create one */ + return progressbar_bar_add(bars, title); } /** Draw a progressbar. @@ -406,24 +429,14 @@ luaA_progressbar_bar_properties_set(lua_State *L) size_t len; widget_t **widget = luaA_checkudata(L, 1, "widget"); const char *buf, *title = luaL_checkstring(L, 2); - bar_t *bar = NULL; + bar_t *bar; progressbar_data_t *d = (*widget)->data; xcolor_init_request_t reqs[6]; int8_t i, reqs_nbr = -1; luaA_checktable(L, 3); - /* check if this section is defined already */ - for(int j = 0; j < d->bars.len; j++) - { - bar = &d->bars.tab[j]; - if(!a_strcmp(title, bar->title)) - break; - } - - /* no bar found -> create one */ - if(!bar) - bar = progressbar_bar_add(d, title); + bar = progressbar_bar_get(&d->bars, title); if((buf = luaA_getopt_lstring(L, 3, "fg", NULL, &len))) reqs[++reqs_nbr] = xcolor_init_unchecked(&bar->fg, buf, len); @@ -482,19 +495,9 @@ luaA_progressbar_bar_data_add(lua_State *L) widget_t **widget = luaA_checkudata(L, 1, "widget"); const char *title = luaL_checkstring(L, 2); progressbar_data_t *d = (*widget)->data; - bar_t *bar = NULL; + bar_t *bar; - /* check if this section is defined already */ - for(int j = 0; j < d->bars.len; j++) - { - bar = &d->bars.tab[j]; - if(!a_strcmp(title, bar->title)) - break; - } - - /* no bar found -> create one */ - if(!bar) - bar = progressbar_bar_add(d, title); + bar = progressbar_bar_get(&d->bars, title); bar->value = luaL_checknumber(L, 3); bar->value = MAX(bar->min_value, MIN(bar->max_value, bar->value)); -- 2.11.4.GIT