Solidify widgets.

Factor out common initialisation into a common_new function. Copy the section
title into the Widget title attribute.
This commit is contained in:
Aldo Cortesi 2007-12-16 19:34:33 +11:00 committed by Julien Danjou
parent 65fd8d48f0
commit 5923c90aaa
7 changed files with 59 additions and 39 deletions

View file

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

View file

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

View file

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

View file

@ -1,16 +1,19 @@
#include <stdio.h>
#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;
}

View file

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

View file

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

View file

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