diff options
-rw-r--r-- | widget.c | 22 | ||||
-rw-r--r-- | widget.h | 6 | ||||
-rw-r--r-- | widget_icon.c | 136 |
3 files changed, 102 insertions, 62 deletions
@@ -1,4 +1,4 @@ -/* $Id: widget.c,v 1.19 2005/05/08 04:32:45 reinelt Exp $ +/* $Id: widget.c,v 1.20 2005/11/06 09:17:20 reinelt Exp $ * * generic widget handling * @@ -21,6 +21,9 @@ * * * $Log: widget.c,v $ + * Revision 1.20 2005/11/06 09:17:20 reinelt + * re-use icons (thanks to Jesus de Santos Garcia) + * * Revision 1.19 2005/05/08 04:32:45 reinelt * CodingStyle added and applied * @@ -165,6 +168,8 @@ void widget_unregister(void) int i; for (i = 0; i < nWidgets; i++) { Widgets[i].class->quit(&(Widgets[i])); + if (Widgets[i].name) + free (Widgets[i].name); } free(Widgets); @@ -182,6 +187,7 @@ int widget_add(const char *name, const int row, const int col) WIDGET_CLASS *Class; WIDGET *Widget; + WIDGET *Parent; /* prepare config section */ /* strlen("Widget:")=7 */ @@ -231,15 +237,25 @@ int widget_add(const char *name, const int row, const int col) return -1; } + /* look up parent widget (widget with the same name) */ + Parent = NULL; + for (i = 0; i < nWidgets; i++) { + if (strcmp(name, Widgets[i].name) == 0) { + Parent = &(Widgets[i]); + break; + } + } + Widget = &(Widgets[nWidgets]); nWidgets++; - Widget->name = (char *) name; + Widget->name = strdup(name); Widget->class = Class; + Widget->parent = Parent; Widget->row = row; Widget->col = col; - if (Class->init != 0) { + if (Class->init != NULL) { Class->init(Widget); } @@ -1,4 +1,4 @@ -/* $Id: widget.h,v 1.13 2005/05/08 04:32:45 reinelt Exp $ +/* $Id: widget.h,v 1.14 2005/11/06 09:17:20 reinelt Exp $ * * generic widget handling * @@ -23,6 +23,9 @@ * * * $Log: widget.h,v $ + * Revision 1.14 2005/11/06 09:17:20 reinelt + * re-use icons (thanks to Jesus de Santos Garcia) + * * Revision 1.13 2005/05/08 04:32:45 reinelt * CodingStyle added and applied * @@ -99,6 +102,7 @@ typedef struct WIDGET_CLASS { typedef struct WIDGET { char *name; WIDGET_CLASS *class; + struct WIDGET *parent; int row; int col; void *data; diff --git a/widget_icon.c b/widget_icon.c index 0b7a330..0341689 100644 --- a/widget_icon.c +++ b/widget_icon.c @@ -1,4 +1,4 @@ -/* $Id: widget_icon.c,v 1.17 2005/05/08 04:32:45 reinelt Exp $ +/* $Id: widget_icon.c,v 1.18 2005/11/06 09:17:20 reinelt Exp $ * * icon widget handling * @@ -21,6 +21,9 @@ * * * $Log: widget_icon.c,v $ + * Revision 1.18 2005/11/06 09:17:20 reinelt + * re-use icons (thanks to Jesus de Santos Garcia) + * * Revision 1.17 2005/05/08 04:32:45 reinelt * CodingStyle added and applied * @@ -170,29 +173,33 @@ void widget_icon_update(void *Self) WIDGET_ICON *Icon = W->data; RESULT result = { 0, 0, 0, NULL }; - /* evaluate expressions */ - Icon->speed = 100; - if (Icon->speed_tree != NULL) { - Eval(Icon->speed_tree, &result); - Icon->speed = R2N(&result); - if (Icon->speed < 10) - Icon->speed = 10; - DelResult(&result); - } + /* process the parent only */ + if (W->parent == NULL) { + + /* evaluate expressions */ + Icon->speed = 100; + if (Icon->speed_tree != NULL) { + Eval(Icon->speed_tree, &result); + Icon->speed = R2N(&result); + if (Icon->speed < 10) + Icon->speed = 10; + DelResult(&result); + } - Icon->visible = 1; - if (Icon->visible_tree != NULL) { - Eval(Icon->visible_tree, &result); - Icon->visible = R2N(&result); - if (Icon->visible < 1) - Icon->visible = 0; - DelResult(&result); - } + Icon->visible = 1; + if (Icon->visible_tree != NULL) { + Eval(Icon->visible_tree, &result); + Icon->visible = R2N(&result); + if (Icon->visible < 1) + Icon->visible = 0; + DelResult(&result); + } - /* rotate icon bitmap */ - Icon->curmap++; - if (Icon->curmap >= Icon->maxmap) - Icon->curmap = 0; + /* rotate icon bitmap */ + Icon->curmap++; + if (Icon->curmap >= Icon->maxmap) + Icon->curmap = 0; + } /* finally, draw it! */ if (W->class->draw) @@ -210,42 +217,52 @@ int widget_icon_init(WIDGET * Self) char *section; WIDGET_ICON *Icon; - /* prepare config section */ - /* strlen("Widget:")=7 */ - section = malloc(strlen(Self->name) + 8); - strcpy(section, "Widget:"); - strcat(section, Self->name); + /* re-use the parent if one exists */ + if (Self->parent == NULL) { - Icon = malloc(sizeof(WIDGET_ICON)); - memset(Icon, 0, sizeof(WIDGET_ICON)); + /* prepare config section */ + /* strlen("Widget:")=7 */ + section = malloc(strlen(Self->name) + 8); + strcpy(section, "Widget:"); + strcat(section, Self->name); - /* get raw expressions (we evaluate them ourselves) */ - Icon->speed_expr = cfg_get_raw(section, "speed", NULL); - Icon->visible_expr = cfg_get_raw(section, "visible", NULL); + Icon = malloc(sizeof(WIDGET_ICON)); + memset(Icon, 0, sizeof(WIDGET_ICON)); - /* compile'em */ - Compile(Icon->speed_expr, &Icon->speed_tree); - Compile(Icon->visible_expr, &Icon->visible_tree); + /* get raw expressions (we evaluate them ourselves) */ + Icon->speed_expr = cfg_get_raw(section, "speed", NULL); + Icon->visible_expr = cfg_get_raw(section, "visible", NULL); - /* sanity check */ - if (Icon->speed_expr == NULL || *Icon->speed_expr == '\0') { - error("Icon %s has no speed, using '100'", Self->name); - Icon->speed_expr = "100"; - } + /* compile'em */ + Compile(Icon->speed_expr, &Icon->speed_tree); + Compile(Icon->visible_expr, &Icon->visible_tree); + + /* sanity check */ + if (Icon->speed_expr == NULL || *Icon->speed_expr == '\0') { + error("Icon %s has no speed, using '100'", Self->name); + Icon->speed_expr = "100"; + } - /* read bitmap */ - widget_icon_read_bitmap(section, Icon); + /* read bitmap */ + widget_icon_read_bitmap(section, Icon); - free(section); - Self->data = Icon; + free(section); + Self->data = Icon; - /* as the speed is evaluatod on every call, we use 'one-shot'-timers. */ - /* The timer will be reactivated on every call to widget_icon_update(). */ - /* We do the initial call here... */ - Icon->prvmap = -1; + /* as the speed is evaluatod on every call, we use 'one-shot'-timers. */ + /* The timer will be reactivated on every call to widget_icon_update(). */ + /* We do the initial call here... */ + Icon->prvmap = -1; - /* reset ascii */ - Icon->ascii = -1; + /* reset ascii */ + Icon->ascii = -1; + + } else { + + /* re-use the parent */ + Self->data = Self->parent->data; + + } /* just do it! */ widget_icon_update(Self); @@ -257,14 +274,17 @@ int widget_icon_init(WIDGET * Self) int widget_icon_quit(WIDGET * Self) { if (Self) { - if (Self->data) { - WIDGET_ICON *Icon = Self->data; - DelTree(Icon->speed_tree); - DelTree(Icon->visible_tree); - if (Icon->bitmap) - free(Icon->bitmap); - free(Self->data); - Self->data = NULL; + /* do not deallocate child widget! */ + if (Self->parent == NULL) { + if (Self->data) { + WIDGET_ICON *Icon = Self->data; + DelTree(Icon->speed_tree); + DelTree(Icon->visible_tree); + if (Icon->bitmap) + free(Icon->bitmap); + free(Self->data); + Self->data = NULL; + } } } |