From 397aa33163729a146b76d7dea76552924df4ecbd Mon Sep 17 00:00:00 2001 From: marco candrian Date: Thu, 6 Mar 2008 14:30:18 +0100 Subject: [PATCH] widget error infrastructure additions --- structs.h | 2 + widget.c | 10 +++- widgets/graph.c | 35 ++++++++------ widgets/progressbar.c | 109 +++++++++++++++++++++++++++--------------- widgets/textbox.c | 14 ++++-- 5 files changed, 112 insertions(+), 58 deletions(-) diff --git a/structs.h b/structs.h index 5b1ce7990..d736f9e3a 100644 --- a/structs.h +++ b/structs.h @@ -96,9 +96,11 @@ typedef enum { WIDGET_NOERROR = 0, WIDGET_ERROR, + WIDGET_ERROR_NOVALUE, WIDGET_ERROR_CUSTOM, WIDGET_ERROR_FORMAT_BOOL, WIDGET_ERROR_FORMAT_FONT, + WIDGET_ERROR_FORMAT_COLOR, WIDGET_ERROR_FORMAT_SECTION } widget_tell_status_t; diff --git a/widget.c b/widget.c index fec5e012b..2ebc0b273 100644 --- a/widget.c +++ b/widget.c @@ -230,6 +230,10 @@ uicb_widget_tell(int screen, char *arg) warn("error changing property %s of widget %s\n", property, widget->name); break; + case WIDGET_ERROR_NOVALUE: + warn("error changing property %s of widget %s, no value given\n", + property, widget->name); + break; case WIDGET_ERROR_FORMAT_BOOL: warn("error changing property %s of widget %s, must is boolean (0 or 1)\n", property, widget->name); @@ -238,8 +242,12 @@ uicb_widget_tell(int screen, char *arg) warn("error changing property %s of widget %s, must be a valid font\n", property, widget->name); break; + case WIDGET_ERROR_FORMAT_COLOR: + warn("error changing property %s of widget %s, must be a valid color\n", + property, widget->name); + break; case WIDGET_ERROR_FORMAT_SECTION: - warn("error changing property %s of widget %s, section not found\n", + warn("error changing property %s of widget %s, section/title not found\n", property, widget->name); break; case WIDGET_NOERROR: diff --git a/widgets/graph.c b/widgets/graph.c index 69f8b5f92..f7d74f0f7 100644 --- a/widgets/graph.c +++ b/widgets/graph.c @@ -85,7 +85,7 @@ graph_draw(Widget *widget, DrawCtx *ctx, int offset, Data *d = widget->data; Area rectangle; - if(d->width < 1 || !d->data_items) + if(!d->data_items) return 0; if(!widget->user_supplied_x) @@ -182,13 +182,17 @@ graph_tell(Widget *widget, char *property, char *command) float value; char *title, *setting; - if(!property || !command || d->width < 1 || !(d->data_items > 0)) - return WIDGET_ERROR; + if(!d->data_items) + return WIDGET_ERROR_CUSTOM; /* error already printed on _new */ + + if(!command) + return WIDGET_ERROR_NOVALUE; if(!a_strcmp(property, "data")) { title = strtok(command, " "); - setting = strtok(NULL, " "); + if(!(setting = strtok(NULL, " "))) + return WIDGET_ERROR_NOVALUE; for(i = 0; i < d->data_items; i++) { @@ -240,25 +244,26 @@ graph_tell(Widget *widget, char *property, char *command) return WIDGET_NOERROR; } } - warn("no such data-section title: %s\n", title); - return WIDGET_ERROR; + return WIDGET_ERROR_FORMAT_SECTION; } - else if(!a_strcmp(property, "width")) - d->width = atoi(command); else if(!a_strcmp(property, "height")) d->height = atof(command); - else if(!a_strcmp(property, "padding_left")) - d->padding_left = atoi(command); else if(!a_strcmp(property, "bg")) - draw_color_new(globalconf.display, get_phys_screen(widget->statusbar->screen), - command, &d->bg); + { + if(!draw_color_new(globalconf.display, get_phys_screen(widget->statusbar->screen), + command, &d->bg)) + return WIDGET_ERROR_FORMAT_COLOR; + } else if(!a_strcmp(property, "bordercolor")) - draw_color_new(globalconf.display, get_phys_screen(widget->statusbar->screen), - command, &d->bordercolor); + { + if(!draw_color_new(globalconf.display, get_phys_screen(widget->statusbar->screen), + command, &d->bordercolor)) + return WIDGET_ERROR_FORMAT_COLOR; + } else return WIDGET_ERROR; - return WIDGET_NOERROR;; + return WIDGET_NOERROR; } Widget * diff --git a/widgets/progressbar.c b/widgets/progressbar.c index 0f05e14ce..67f2be9e2 100644 --- a/widgets/progressbar.c +++ b/widgets/progressbar.c @@ -63,7 +63,7 @@ progressbar_draw(Widget *widget, DrawCtx *ctx, int offset, Data *d = widget->data; - if (!(d->data_items) || (d->width - d->padding < 3)) + if (!d->data_items) return 0; width = d->width - 2 * d->padding; @@ -123,17 +123,21 @@ static widget_tell_status_t progressbar_tell(Widget *widget, char *property, char *command) { Data *d = widget->data; - int i = 0, percent; + int i = 0, percent, tmp; Bool flag; char *title, *setting; - if(!property || !command || !d->data_items) - return WIDGET_ERROR; + if(!d->data_items) + return WIDGET_ERROR_CUSTOM; /* error already printed on _new */ + + if(!command) + return WIDGET_ERROR_NOVALUE; if(!a_strcmp(property, "data")) { title = strtok(command, " "); - setting = strtok(NULL, " "); + if(!(setting = strtok(NULL, " "))) + return WIDGET_ERROR_NOVALUE; for(i = 0; i < d->data_items; i++) if(!a_strcmp(title, d->data_title[i])) { @@ -146,49 +150,59 @@ progressbar_tell(Widget *widget, char *property, char *command) else if(!a_strcmp(property, "fg")) { title = strtok(command, " "); - setting = strtok(NULL, " "); + if(!(setting = strtok(NULL, " "))) + return WIDGET_ERROR_NOVALUE; for(i = 0; i < d->data_items; i++) if(!a_strcmp(title, d->data_title[i])) { - draw_color_new(globalconf.display, get_phys_screen(widget->statusbar->screen), - setting, &d->fg[i]); - return WIDGET_NOERROR; + if(draw_color_new(globalconf.display, get_phys_screen(widget->statusbar->screen), + setting, &d->fg[i])) + return WIDGET_NOERROR; + else + return WIDGET_ERROR_FORMAT_COLOR; } return WIDGET_ERROR_FORMAT_SECTION; } else if(!a_strcmp(property, "bg")) { title = strtok(command, " "); - setting = strtok(NULL, " "); + if(!(setting = strtok(NULL, " "))) + return WIDGET_ERROR_NOVALUE; for(i = 0; i < d->data_items; i++) if(!a_strcmp(title, d->data_title[i])) { - draw_color_new(globalconf.display, get_phys_screen(widget->statusbar->screen), - setting, &d->bg[i]); - return WIDGET_NOERROR; + if(draw_color_new(globalconf.display, get_phys_screen(widget->statusbar->screen), + setting, &d->bg[i])) + return WIDGET_NOERROR; + else + return WIDGET_ERROR_FORMAT_COLOR; } return WIDGET_ERROR_FORMAT_SECTION; } else if(!a_strcmp(property, "fg_center")) { title = strtok(command, " "); - setting = strtok(NULL, " "); + if(!(setting = strtok(NULL, " "))) + return WIDGET_ERROR_NOVALUE; for(i = 0; i < d->data_items; i++) if(!a_strcmp(title, d->data_title[i])) { flag = False; if(!d->pfg_center[i]) { - flag = True; /* restore to NULL & p_delete */ + flag = True; /* p_delete && restore to NULL, if draw_color_new unsuccessful */ d->pfg_center[i] = p_new(XColor, 1); } if(!(draw_color_new(globalconf.display, get_phys_screen(widget->statusbar->screen), - setting, d->pfg_center[i]))) + setting, d->pfg_center[i]))) + { if(flag) /* restore */ { p_delete(&d->pfg_center[i]); d->pfg_center[i] = NULL; } + return WIDGET_ERROR_FORMAT_COLOR; + } return WIDGET_NOERROR; } return WIDGET_ERROR_FORMAT_SECTION; @@ -196,36 +210,43 @@ progressbar_tell(Widget *widget, char *property, char *command) else if(!a_strcmp(property, "fg_end")) { title = strtok(command, " "); - setting = strtok(NULL, " "); + if(!(setting = strtok(NULL, " "))) + return WIDGET_ERROR_NOVALUE; for(i = 0; i < d->data_items; i++) if(!a_strcmp(title, d->data_title[i])) { flag = False; if(!d->pfg_end[i]) { - flag = True; /* restore to NULL & p_delete */ + flag = True; d->pfg_end[i] = p_new(XColor, 1); } if(!(draw_color_new(globalconf.display, get_phys_screen(widget->statusbar->screen), - setting, d->pfg_end[i]))) + setting, d->pfg_end[i]))) + { if(flag) /* restore */ { p_delete(&d->pfg_end[i]); d->pfg_end[i] = NULL; } + return WIDGET_ERROR_FORMAT_COLOR; + } return WIDGET_NOERROR; } } else if(!a_strcmp(property, "bordercolor")) { title = strtok(command, " "); - setting = strtok(NULL, " "); + if(!(setting = strtok(NULL, " "))) + return WIDGET_ERROR_NOVALUE; for(i = 0; i < d->data_items; i++) if(!a_strcmp(title, d->data_title[i])) { - draw_color_new(globalconf.display, get_phys_screen(widget->statusbar->screen), - setting, &d->bordercolor[i]); - return WIDGET_NOERROR; + if(draw_color_new(globalconf.display, get_phys_screen(widget->statusbar->screen), + setting, &d->bordercolor[i])) + return WIDGET_NOERROR; + else + return WIDGET_ERROR_FORMAT_COLOR; } return WIDGET_ERROR_FORMAT_SECTION; } @@ -233,15 +254,28 @@ progressbar_tell(Widget *widget, char *property, char *command) d->gap = atoi(command); else if(!a_strcmp(property, "width")) { - d->width = atoi(command); - if(d->width - d->padding < 3) - warn("progressbar widget needs: (width - padding) >= 3\n"); - return WIDGET_ERROR_CUSTOM; + tmp = atoi(command); + if(tmp - d->padding < 3) + { + warn("progressbar widget needs: (width - padding) >= 3. Ignoring command.\n"); + return WIDGET_ERROR_CUSTOM; + } + else + d->width = tmp; } else if(!a_strcmp(property, "height")) d->height = atof(command); else if(!a_strcmp(property, "padding")) - d->padding = atoi(command); + { + tmp = atoi(command); + if(d->width - tmp < 3) + { + warn("progressbar widget needs: (width - padding) >= 3. Ignoring command.\n"); + return WIDGET_ERROR_CUSTOM; + } + else + d->padding = tmp; + } else return WIDGET_ERROR; @@ -263,12 +297,18 @@ progressbar_new(Statusbar *statusbar, cfg_t *config) w->tell = progressbar_tell; d = w->data = p_new(Data, 1); d->width = cfg_getint(config, "width"); - d->height = cfg_getfloat(config, "height"); - d->gap = cfg_getint(config, "gap"); d->padding = cfg_getint(config, "padding"); - w->alignment = draw_get_align(cfg_getstr(config, "align")); + if(d->width - d->padding < 3) + { + warn("progressbar widget needs: (width - padding) >= 3\n"); + return w; + } + d->height = cfg_getfloat(config, "height"); + d->gap = cfg_getint(config, "gap"); + + w->alignment = draw_get_align(cfg_getstr(config, "align")); if(!(d->data_items = cfg_size(config, "data"))) { @@ -318,13 +358,6 @@ progressbar_new(Statusbar *statusbar, cfg_t *config) d->bordercolor[i] = d->fg[i]; } - /* complete setup, since someone might 'enable' it with increasing the width with widget_tell */ - if(d->width - d->padding < 3) - { - warn("progressbar widget needs: (width - padding) >= 3\n"); - return w; - } - return w; } // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80 diff --git a/widgets/textbox.c b/widgets/textbox.c index f310b716f..fa332ca2c 100644 --- a/widgets/textbox.c +++ b/widgets/textbox.c @@ -69,8 +69,8 @@ textbox_tell(Widget *widget, char *property, char *command) { Data *d = widget->data; - if(!property || !command) - return WIDGET_ERROR; + if(!command) + return WIDGET_ERROR_NOVALUE; if(!a_strcmp(property, "text")) { @@ -79,9 +79,15 @@ textbox_tell(Widget *widget, char *property, char *command) d->text = a_strdup(command); } else if(!a_strcmp(property, "fg")) - draw_color_new(globalconf.display, widget->statusbar->screen, command, &d->fg); + if(draw_color_new(globalconf.display, widget->statusbar->screen, command, &d->fg)) + return WIDGET_NOERROR; + else + return WIDGET_ERROR_FORMAT_COLOR; else if(!a_strcmp(property, "bg")) - draw_color_new(globalconf.display, widget->statusbar->screen, command, &d->bg); + if(draw_color_new(globalconf.display, widget->statusbar->screen, command, &d->bg)) + return WIDGET_NOERROR; + else + return WIDGET_ERROR_FORMAT_COLOR; else if(!a_strcmp(property, "font")) { widget->font = XftFontOpenName(globalconf.display,