add --check and --config, better usage printing
[awesome.git] / widgets / progressbar.c
blob4b98bfbc2e3d5ceca35e442f4dbea830787380b8
1 /*
2 * progressbar.c - progress bar widget
4 * Copyright © 2007-2008 Julien Danjou <julien@danjou.info>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include <string.h>
23 #include "widget.h"
24 #include "screen.h"
25 #include "common/util.h"
27 extern AwesomeConf globalconf;
29 typedef struct
31 /** Percent 0 to 100 */
32 int *percent;
33 /** Width of the bars */
34 int width;
35 /** Padding */
36 int padding;
37 /** Pixel between bars */
38 int gap;
39 /** Number of bars */
40 int bars;
41 /** Height 0-1, where 1 is height of statusbar */
42 float height;
43 /** Foreground color */
44 XColor *fg;
45 /** Foreground color when bar is full */
46 XColor *fg_full;
47 /** Background color */
48 XColor *bg;
49 /** Border color */
50 XColor *bordercolor;
51 } Data;
53 static int
54 progressbar_draw(Widget *widget, DrawCtx *ctx, int offset,
55 int used __attribute__ ((unused)))
57 int i, width, pwidth, margin_top, pb_height, left_offset;
58 Area rectangle;
60 Data *d = widget->data;
62 if (!(d->bars))
63 return 0;
65 width = d->width - 2 * d->padding;
67 if(!widget->user_supplied_x)
68 widget->area.x = widget_calculate_offset(widget->statusbar->width,
69 d->width,
70 offset,
71 widget->alignment);
73 if(!widget->user_supplied_y)
74 widget->area.y = 0;
76 margin_top = (int) (widget->statusbar->height * (1 - d->height)) / 2 + 0.5 + widget->area.y;
77 pb_height = (int) ((widget->statusbar->height * d->height - (d->gap * (d->bars - 1))) / d->bars + 0.5);
78 left_offset = widget->area.x + d->padding;
80 for(i = 0; i < d->bars; i++)
82 pwidth = (int) d->percent[i] ? ((width - 2) * d->percent[i]) / 100 : 0;
84 rectangle.x = left_offset;
85 rectangle.y = margin_top;
86 rectangle.width = width;
87 rectangle.height = pb_height;
89 draw_rectangle(ctx, rectangle, False, d->bordercolor[i]);
91 if(pwidth > 0)
93 rectangle.x = left_offset + 1;
94 rectangle.y = margin_top + 1;
95 rectangle.width = pwidth;
96 rectangle.height = pb_height - 2;
97 draw_rectangle_gradient(ctx, rectangle, width - 2, True, d->fg[i], d->fg_full[i]);
100 if(width - 2 - pwidth > 0) /* not filled area */
102 rectangle.x = left_offset + 1 + pwidth;
103 rectangle.y = margin_top + 1;
104 rectangle.width = width - 2 - pwidth;
105 rectangle.height = pb_height - 2;
106 draw_rectangle(ctx, rectangle, True, d->bg[i]);
109 margin_top += (pb_height + d->gap);
112 widget->area.width = d->width;
113 widget->area.height = widget->statusbar->height;
114 return widget->area.width;
117 static void
118 progressbar_tell(Widget *widget, char *command)
120 Data *d = widget->data;
121 int i = 0, percent;
122 char * tok;
124 if(!command || !d->bars)
125 return;
127 for (tok = strtok(command, ","); tok && i < d->bars; tok = strtok(NULL, ","), i++)
129 percent = atoi(tok);
130 d->percent[i] = (percent < 0 ? 0 : (percent > 100 ? 100 : percent));
134 Widget *
135 progressbar_new(Statusbar *statusbar, cfg_t *config)
137 Widget *w;
138 Data *d;
139 char *color;
140 int i, phys_screen = get_phys_screen(statusbar->screen);
141 cfg_t *cfg;
144 w = p_new(Widget, 1);
145 widget_common_new(w, statusbar, config);
146 w->draw = progressbar_draw;
147 w->tell = progressbar_tell;
148 d = w->data = p_new(Data, 1);
149 d->width = cfg_getint(config, "width");
151 if(!(d->bars = cfg_size(config, "bar")))
153 warn("progressbar widget needs at least one bar section\n");
154 return w;
157 d->fg = p_new(XColor, d->bars);
158 d->fg_full = p_new(XColor, d->bars);
159 d->bg = p_new(XColor, d->bars);
160 d->bordercolor = p_new(XColor, d->bars);
161 d->percent = p_new(int, d->bars);
163 for(i = 0; i < d->bars; i++)
165 cfg = cfg_getnsec(config, "bar", i);
167 if((color = cfg_getstr(cfg, "fg")))
168 d->fg[i] = draw_color_new(globalconf.display, phys_screen, color);
169 else
170 d->fg[i] = globalconf.screens[statusbar->screen].colors_normal[ColFG];
172 if((color = cfg_getstr(cfg, "fg_full")))
173 d->fg_full[i] = draw_color_new(globalconf.display, phys_screen, color);
174 else
175 d->fg_full[i] = d->fg[i];
177 if((color = cfg_getstr(cfg, "bg")))
178 d->bg[i] = draw_color_new(globalconf.display, phys_screen, color);
179 else
180 d->bg[i] = globalconf.screens[statusbar->screen].colors_normal[ColBG];
182 if((color = cfg_getstr(cfg, "bordercolor")))
183 d->bordercolor[i] = draw_color_new(globalconf.display, phys_screen, color);
184 else
185 d->bordercolor[i] = d->fg[i];
189 d->height = cfg_getfloat(config, "height");
190 d->gap = cfg_getint(config, "gap");
191 d->padding = cfg_getint(config, "padding");
193 return w;
195 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80