From 0e69534a65e886210993f9b66cc766acb7785065 Mon Sep 17 00:00:00 2001 From: Julien Danjou Date: Tue, 4 Mar 2008 19:06:04 +0100 Subject: [PATCH] Add a common error infratructure for widget_tell Signed-off-by: Marco Candrian --- structs.h | 13 +++++++++++- widget.c | 27 ++++++++++++++++++++++-- widgets/graph.c | 24 +++++++--------------- widgets/iconbox.c | 27 +++++++++--------------- widgets/progressbar.c | 48 ++++++++++++++++++------------------------- widgets/textbox.c | 30 ++++++++------------------- 6 files changed, 83 insertions(+), 86 deletions(-) diff --git a/structs.h b/structs.h index f618ba01a..5b1ce7990 100644 --- a/structs.h +++ b/structs.h @@ -91,6 +91,17 @@ struct Button Button *next; }; +/** Widget tell status code */ +typedef enum +{ + WIDGET_NOERROR = 0, + WIDGET_ERROR, + WIDGET_ERROR_CUSTOM, + WIDGET_ERROR_FORMAT_BOOL, + WIDGET_ERROR_FORMAT_FONT, + WIDGET_ERROR_FORMAT_SECTION +} widget_tell_status_t; + /** Widget */ typedef struct Widget Widget; typedef struct Statusbar Statusbar; @@ -101,7 +112,7 @@ struct Widget /** Draw function */ int (*draw)(Widget *, DrawCtx *, int, int); /** Update function */ - void (*tell)(Widget *, char *, char *); + widget_tell_status_t (*tell)(Widget *, char *, char *); /** ButtonPressedEvent handler */ void (*button_press)(Widget *, XButtonPressedEvent *); /** Statusbar */ diff --git a/widget.c b/widget.c index 7b561d88a..fec5e012b 100644 --- a/widget.c +++ b/widget.c @@ -128,12 +128,14 @@ widget_common_button_press(Widget *widget, XButtonPressedEvent *ev) * cannot be told anything * \param widget the widget * \param command unused argument + * \return widget_tell_status_t enum (strucs.h) */ -static void +static widget_tell_status_t widget_common_tell(Widget *widget, char *property __attribute__ ((unused)), char *command __attribute__ ((unused))) { warn("%s widget does not accept commands.\n", widget->name); + return WIDGET_ERROR_CUSTOM; } /** Common function for creating a widget @@ -222,7 +224,28 @@ uicb_widget_tell(int screen, char *arg) len = a_strlen(p); command = p_new(char, len + 1); a_strncpy(command, len + 1, p, len); - widget->tell(widget, property, command); + switch(widget->tell(widget, property, command)) + { + case WIDGET_ERROR: + warn("error changing property %s of widget %s\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); + break; + case WIDGET_ERROR_FORMAT_FONT: + warn("error changing property %s of widget %s, must be a valid font\n", + property, widget->name); + break; + case WIDGET_ERROR_FORMAT_SECTION: + warn("error changing property %s of widget %s, section not found\n", + property, widget->name); + break; + case WIDGET_NOERROR: + case WIDGET_ERROR_CUSTOM: + break; + } p_delete(&command); } else diff --git a/widgets/graph.c b/widgets/graph.c index 5bbbb7c34..69f8b5f92 100644 --- a/widgets/graph.c +++ b/widgets/graph.c @@ -174,7 +174,7 @@ graph_draw(Widget *widget, DrawCtx *ctx, int offset, return widget->area.width; } -static void +static widget_tell_status_t graph_tell(Widget *widget, char *property, char *command) { Data *d = widget->data; @@ -183,7 +183,7 @@ graph_tell(Widget *widget, char *property, char *command) char *title, *setting; if(!property || !command || d->width < 1 || !(d->data_items > 0)) - return; + return WIDGET_ERROR; if(!a_strcmp(property, "data")) { @@ -237,38 +237,28 @@ graph_tell(Widget *widget, char *property, char *command) else d->lines[i][d->index[i]] = d->box_height; } - return; + return WIDGET_NOERROR; } } - warn("No such data-section title: %s\n", title); + warn("no such data-section title: %s\n", title); + return WIDGET_ERROR; } 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); - else if(!a_strcmp(property, "bordercolor")) draw_color_new(globalconf.display, get_phys_screen(widget->statusbar->screen), command, &d->bordercolor); - - else if(!a_strcmp(property, "fg") || !a_strcmp(property, "fg_center") || - !a_strcmp(property, "fg_end") || !a_strcmp(property, "scale") || - !a_strcmp(property, "max") || !a_strcmp(property, "style") || - !a_strcmp(property, "align") || !a_strcmp(property, "mouse") || - !a_strcmp(property, "x") || !a_strcmp(property, "y")) - warn("Property \"%s\" can't get changed.\n", property); else - warn("No such property: %s\n", property); + return WIDGET_ERROR; - return; + return WIDGET_NOERROR;; } Widget * diff --git a/widgets/iconbox.c b/widgets/iconbox.c index cb137dae7..e48b668d6 100644 --- a/widgets/iconbox.c +++ b/widgets/iconbox.c @@ -63,13 +63,14 @@ iconbox_draw(Widget *widget, DrawCtx *ctx, int offset, return widget->area.width; } -static void +static widget_tell_status_t iconbox_tell(Widget *widget, char *property, char *command) { + Bool b; Data *d = widget->data; if(!property || !command) - return; + return WIDGET_ERROR; if(!a_strcmp(property, "image")) { @@ -77,25 +78,17 @@ iconbox_tell(Widget *widget, char *property, char *command) p_delete(&d->image); d->image = a_strdup(command); } - else if(!a_strcmp(property, "resize")) /* XXX how to ignorecase compare? */ + else if(!a_strcmp(property, "resize")) { - if(!a_strcmp(command, "true") || !a_strcmp(command, "1")) - d->resize = True; - else if (!a_strcmp(command, "false") || !a_strcmp(command, "0")) - d->resize = False; + if((b = cfg_parse_boolean(command)) != -1) + d->resize = b; else - warn("Resize value must be (case-sensitive) \"true\", \"false\",\ - \"1\" or \"0\". But is: %s.\n", command); + return WIDGET_ERROR_FORMAT_BOOL; } - else if(!a_strcmp(property, "align") || !a_strcmp(property, "mouse") || - !a_strcmp(property, "x") || !a_strcmp(property, "y")) - warn("Property \"%s\" can't get changed.\n", property); - else - { - warn("No such property: %s\n", property); - return; - } + return WIDGET_ERROR; + + return WIDGET_NOERROR; } Widget * diff --git a/widgets/progressbar.c b/widgets/progressbar.c index 635c2d62b..0f05e14ce 100644 --- a/widgets/progressbar.c +++ b/widgets/progressbar.c @@ -119,7 +119,7 @@ progressbar_draw(Widget *widget, DrawCtx *ctx, int offset, return widget->area.width; } -static void +static widget_tell_status_t progressbar_tell(Widget *widget, char *property, char *command) { Data *d = widget->data; @@ -128,7 +128,7 @@ progressbar_tell(Widget *widget, char *property, char *command) char *title, *setting; if(!property || !command || !d->data_items) - return; + return WIDGET_ERROR; if(!a_strcmp(property, "data")) { @@ -139,9 +139,9 @@ progressbar_tell(Widget *widget, char *property, char *command) { percent = atoi(setting); d->percent[i] = (percent < 0 ? 0 : (percent > 100 ? 100 : percent)); - return; + return WIDGET_NOERROR; } - warn("No such data section title: %s\n", title); + return WIDGET_ERROR_FORMAT_SECTION; } else if(!a_strcmp(property, "fg")) { @@ -152,9 +152,9 @@ progressbar_tell(Widget *widget, char *property, char *command) { draw_color_new(globalconf.display, get_phys_screen(widget->statusbar->screen), setting, &d->fg[i]); - return; + return WIDGET_NOERROR; } - warn("No such data section title: %s\n", title); + return WIDGET_ERROR_FORMAT_SECTION; } else if(!a_strcmp(property, "bg")) { @@ -165,9 +165,9 @@ progressbar_tell(Widget *widget, char *property, char *command) { draw_color_new(globalconf.display, get_phys_screen(widget->statusbar->screen), setting, &d->bg[i]); - return; + return WIDGET_NOERROR; } - warn("No such data section title: %s\n", title); + return WIDGET_ERROR_FORMAT_SECTION; } else if(!a_strcmp(property, "fg_center")) { @@ -189,9 +189,9 @@ progressbar_tell(Widget *widget, char *property, char *command) p_delete(&d->pfg_center[i]); d->pfg_center[i] = NULL; } - return; + return WIDGET_NOERROR; } - warn("No such data section title: %s\n", title); + return WIDGET_ERROR_FORMAT_SECTION; } else if(!a_strcmp(property, "fg_end")) { @@ -213,9 +213,8 @@ progressbar_tell(Widget *widget, char *property, char *command) p_delete(&d->pfg_end[i]); d->pfg_end[i] = NULL; } - return; + return WIDGET_NOERROR; } - warn("No such data section title: %s\n", title); } else if(!a_strcmp(property, "bordercolor")) { @@ -226,34 +225,27 @@ progressbar_tell(Widget *widget, char *property, char *command) { draw_color_new(globalconf.display, get_phys_screen(widget->statusbar->screen), setting, &d->bordercolor[i]); - return; + return WIDGET_NOERROR; } - warn("No such data section title: %s\n", title); + return WIDGET_ERROR_FORMAT_SECTION; } - else if(!a_strcmp(property, "gap")) 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"); + warn("progressbar widget needs: (width - padding) >= 3\n"); + return WIDGET_ERROR_CUSTOM; } - else if(!a_strcmp(property, "height")) d->height = atof(command); - else if(!a_strcmp(property, "padding")) d->padding = atoi(command); - - else if(!a_strcmp(property, "align") || !a_strcmp(property, "mouse") || - !a_strcmp(property, "x") || !a_strcmp(property, "y")) - warn("Property \"%s\" can't get changed.\n", property); - else - warn("No such property: %s\n", property); - return; + return WIDGET_ERROR; + + return WIDGET_NOERROR; } Widget * @@ -280,7 +272,7 @@ progressbar_new(Statusbar *statusbar, cfg_t *config) if(!(d->data_items = cfg_size(config, "data"))) { - warn("Progressbar widget needs at least one bar section\n"); + warn("progressbar widget needs at least one bar section\n"); return w; } @@ -329,7 +321,7 @@ progressbar_new(Statusbar *statusbar, cfg_t *config) /* 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"); + warn("progressbar widget needs: (width - padding) >= 3\n"); return w; } diff --git a/widgets/textbox.c b/widgets/textbox.c index 5bd8a6a7a..f310b716f 100644 --- a/widgets/textbox.c +++ b/widgets/textbox.c @@ -64,13 +64,13 @@ textbox_draw(Widget *widget, DrawCtx *ctx, int offset, int used) return widget->area.width; } -static void +static widget_tell_status_t textbox_tell(Widget *widget, char *property, char *command) { Data *d = widget->data; if(!property || !command) - return; + return WIDGET_ERROR; if(!a_strcmp(property, "text")) { @@ -80,38 +80,26 @@ textbox_tell(Widget *widget, char *property, char *command) } else if(!a_strcmp(property, "fg")) draw_color_new(globalconf.display, widget->statusbar->screen, command, &d->fg); - else if(!a_strcmp(property, "bg")) draw_color_new(globalconf.display, widget->statusbar->screen, command, &d->bg); - else if(!a_strcmp(property, "font")) { widget->font = XftFontOpenName(globalconf.display, get_phys_screen(widget->statusbar->screen), command); if(!widget->font) + { widget->font = globalconf.screens[widget->statusbar->screen].font; + return WIDGET_ERROR_FORMAT_FONT; + } } - else if(!a_strcmp(property, "width")) d->width = atoi(command); - else if(!a_strcmp(property, "text_align")) - { - if(!a_strcmp(command, "center") || !a_strcmp(command, "left") || - !a_strcmp(command, "right")) - d->align = draw_get_align(command); - else - warn("text_align value must be (case-sensitive) \"center\", \"left\",\ - or \"right\". But is: %s.\n", command); - } - - else if(!a_strcmp(property, "align") || !a_strcmp(property, "mouse") || - !a_strcmp(property, "x") || !a_strcmp(property, "y")) - warn("Property \"%s\" can't get changed.\n", property); - + d->align = draw_get_align(command); else - warn("No such property: %s\n", property); - return; + return WIDGET_ERROR; + + return WIDGET_NOERROR; } Widget *