From 5923c90aaae9af00f5f44dd3617047a915c5cc0d Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Sun, 16 Dec 2007 19:34:33 +1100 Subject: [PATCH] Solidify widgets. Factor out common initialisation into a common_new function. Copy the section title into the Widget title attribute. --- config.c | 14 ++++++++------ widget.c | 14 ++++++++++++-- widget.h | 11 ++++++----- widgets/focustitle.c | 14 ++++++++------ widgets/layoutinfo.c | 17 ++++++++++------- widgets/taglist.c | 16 +++++++++------- widgets/textbox.c | 12 ++++++------ 7 files changed, 59 insertions(+), 39 deletions(-) diff --git a/config.c b/config.c index 5142cc968..2e3e341bd 100644 --- a/config.c +++ b/config.c @@ -268,7 +268,7 @@ create_widgets(cfg_t* cfg_statusbar, Statusbar *statusbar) cfg_t* widgets, *wptr; Widget *widget = NULL; unsigned int i, j, numnames, numwidgets = 0; - Widget *(*widget_new)(Statusbar *); + WidgetConstructor *widget_new; numnames = sizeof(widget_names)/sizeof(widget_names[0]); for (i = 0; i < numnames; i++) @@ -290,19 +290,21 @@ create_widgets(cfg_t* cfg_statusbar, Statusbar *statusbar) for (i = 0; i < numwidgets; i++){ widget_new = name_func_lookup(cfg_name(widgets + i), WidgetList); - if (widget_new) - if(!widget) - statusbar->widgets = widget = widget_new(statusbar); + if(!widget){ + widget = widget_new(statusbar, + cfg_title(widgets + i)); + statusbar->widgets = widget; + } else { - widget->next = widget_new(statusbar); + widget->next = widget_new(statusbar, + cfg_title(widgets + i)); widget = widget->next; } else warn("Ignoring unknown widget: %s.\n", cfg_name(widgets + i)); } - } diff --git a/widget.c b/widget.c index f4830774c..d2c651672 100644 --- a/widget.c +++ b/widget.c @@ -26,7 +26,8 @@ calculate_alignments(Widget *widget) if(widget) for(; widget; widget = widget->next){ if (widget->alignment == AlignFlex) - warn("Multiple flex widgets in panel - ignoring flex for all but the first."); + warn("Multiple flex widgets in panel -" + " ignoring flex for all but the first."); widget->alignment = AlignRight; } } @@ -36,8 +37,17 @@ calculate_offset(int barwidth, int widgetwidth, int offset, int alignment) { if (alignment == AlignLeft || alignment == AlignFlex) return offset; - return barwidth - offset - widgetwidth; } + +void +common_new(Widget *widget, Statusbar *statusbar, const char *name) +{ + widget->statusbar = statusbar; + widget->name = p_new(char, strlen(name)+1); + strncpy(widget->name, name, strlen(name)); +} + + // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 diff --git a/widget.h b/widget.h index 3402d1fa7..baadc2470 100644 --- a/widget.h +++ b/widget.h @@ -6,15 +6,16 @@ enum { AlignLeft, AlignRight, AlignFlex }; -typedef Widget *(WidgetConstructor)(Statusbar *); +typedef Widget *(WidgetConstructor)(Statusbar*, const char*); int calculate_offset(int, int, int, int); void calculate_alignments(Widget *widget); +void common_new(Widget *, Statusbar *, const char *); -Widget *layoutinfo_new(Statusbar*); -Widget *taglist_new(Statusbar*); -Widget *textbox_new(Statusbar*); -Widget *focustitle_new(Statusbar*); +WidgetConstructor layoutinfo_new; +WidgetConstructor taglist_new; +WidgetConstructor textbox_new; +WidgetConstructor focustitle_new; #endif diff --git a/widgets/focustitle.c b/widgets/focustitle.c index 7aad756ad..08dab7a4c 100644 --- a/widgets/focustitle.c +++ b/widgets/focustitle.c @@ -1,16 +1,19 @@ +#include #include "util.h" #include "widget.h" -extern awesome_config globalconf; -static char name[] = "focustitle"; +extern awesome_config globalconf; static int focustitle_draw(Widget *widget, DrawCtx *ctx, int offset, int used) { Client *sel = globalconf.focus->client; VirtScreen vscreen = globalconf.screens[widget->statusbar->screen]; - int location = calculate_offset(vscreen.statusbar.width, 0, offset, widget->alignment); + int location = calculate_offset(vscreen.statusbar.width, + 0, + offset, + widget->alignment); if(sel) { @@ -31,13 +34,12 @@ focustitle_draw(Widget *widget, DrawCtx *ctx, int offset, int used) } Widget * -focustitle_new(Statusbar *statusbar) +focustitle_new(Statusbar *statusbar, const char *name) { Widget *w; w = p_new(Widget, 1); w->draw = focustitle_draw; - w->statusbar = statusbar; - w->name = name; + common_new(w, statusbar, name); w->alignment = AlignFlex; return w; } diff --git a/widgets/layoutinfo.c b/widgets/layoutinfo.c index 497c51bd5..c201f7697 100644 --- a/widgets/layoutinfo.c +++ b/widgets/layoutinfo.c @@ -4,17 +4,21 @@ extern awesome_config globalconf; -static char name[] = "layoutinfo"; - static int -layoutinfo_draw(Widget *widget, DrawCtx *ctx, int offset, int used __attribute__ ((unused))) +layoutinfo_draw(Widget *widget, + DrawCtx *ctx, + int offset, + int used __attribute__ ((unused))) { int width = 0, location; VirtScreen vscreen = globalconf.screens[widget->statusbar->screen]; Layout *l; for(l = vscreen.layouts ; l; l = l->next) width = MAX(width, (textwidth(ctx, vscreen.font, l->symbol))); - location = calculate_offset(vscreen.statusbar.width, width, offset, widget->alignment); + location = calculate_offset(vscreen.statusbar.width, + width, + offset, + widget->alignment); drawtext(ctx, location, 0, width, vscreen.statusbar.height, @@ -26,13 +30,12 @@ layoutinfo_draw(Widget *widget, DrawCtx *ctx, int offset, int used __attribute__ Widget * -layoutinfo_new(Statusbar *statusbar) +layoutinfo_new(Statusbar *statusbar, const char* name) { Widget *w; w = p_new(Widget, 1); w->draw = (void*) layoutinfo_draw; - w->statusbar = statusbar; - w->name = name; + common_new(w, statusbar, name); return w; } diff --git a/widgets/taglist.c b/widgets/taglist.c index 5bce17e61..5a63603bd 100644 --- a/widgets/taglist.c +++ b/widgets/taglist.c @@ -5,8 +5,6 @@ extern awesome_config globalconf; -static char name[] = "taglist"; - /** Check if at least one client is tagged with tag number t and is on screen * screen * \param screen screen number @@ -43,7 +41,10 @@ taglist_draw(Widget *widget, { width += textwidth(ctx, vscreen.font, tag->name); } - location = calculate_offset(vscreen.statusbar.width, width, offset, widget->alignment); + location = calculate_offset(vscreen.statusbar.width, + width, + offset, + widget->alignment); width = 0; for(tag = vscreen.tags; tag; tag = tag->next) @@ -57,7 +58,9 @@ taglist_draw(Widget *widget, vscreen.statusbar.height, vscreen.font, tag->name, colors); if(isoccupied(widget->statusbar->screen, tag)) drawrectangle(ctx, location + width, 0, flagsize, flagsize, - sel && is_client_tagged(sel, tag, widget->statusbar->screen), + sel && is_client_tagged(sel, + tag, + widget->statusbar->screen), colors[ColFG]); width += w; } @@ -65,13 +68,12 @@ taglist_draw(Widget *widget, } Widget * -taglist_new(Statusbar *statusbar) +taglist_new(Statusbar *statusbar, const char *name) { Widget *w; w = p_new(Widget, 1); w->draw = taglist_draw; - w->statusbar = statusbar; - w->name = name; + common_new(w, statusbar, name); return w; } diff --git a/widgets/textbox.c b/widgets/textbox.c index 4233204b6..9831002a3 100644 --- a/widgets/textbox.c +++ b/widgets/textbox.c @@ -3,8 +3,6 @@ extern awesome_config globalconf; -static char name[] = "textbox"; - static int textbox_draw(Widget *widget, DrawCtx *ctx, @@ -13,20 +11,22 @@ textbox_draw(Widget *widget, { VirtScreen vscreen = globalconf.screens[widget->statusbar->screen]; int width = textwidth(ctx, vscreen.font, vscreen.statustext); - int location = calculate_offset(vscreen.statusbar.width, width, offset, widget->alignment); + int location = calculate_offset(vscreen.statusbar.width, + width, + offset, + widget->alignment); drawtext(ctx, location, 0, width, vscreen.statusbar.height, vscreen.font, vscreen.statustext, vscreen.colors_normal); return width; } Widget * -textbox_new(Statusbar *statusbar) +textbox_new(Statusbar *statusbar, const char *name) { Widget *w; w = p_new(Widget, 1); w->draw = textbox_draw; - w->statusbar = statusbar; - w->name = name; + common_new(w, statusbar, name); return w; }