Add a common error infratructure for widget_tell

Signed-off-by: Marco Candrian <mac@calmar.ws>
This commit is contained in:
Julien Danjou 2008-03-04 19:06:04 +01:00
parent 3efadded39
commit 0e69534a65
6 changed files with 83 additions and 86 deletions

View File

@ -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 */

View File

@ -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

View File

@ -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 *

View File

@ -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 *

View File

@ -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;
}

View File

@ -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 *