drop EnterWindow event to keep focus
[awesome.git] / widgets / progressbar.c
blobf76370edefa8ea534c4cbce213189876bb4bf9db
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 "util.h"
24 #include "draw.h"
25 #include "widget.h"
26 #include "xutil.h"
27 #include "screen.h"
29 extern AwesomeConf globalconf;
31 typedef struct
33 /** Percent 0 to 100 */
34 int *percent;
35 /** Width of the bars */
36 int width;
37 /** Left padding */
38 int padding_left;
39 /** Pixel between bars */
40 int gap;
41 /** Number of bars */
42 int bars;
43 /** Height 0-1, where 1 is height of statusbar */
44 float height;
45 /** Foreground color */
46 XColor *fg;
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;
59 Data *d = widget->data;
61 if (!(d->bars))
62 return 0;
64 width = d->width - d->padding_left;
66 if(!widget->user_supplied_x)
67 widget->area.x = widget_calculate_offset(widget->statusbar->width,
68 d->width,
69 offset,
70 widget->alignment);
72 if(!widget->user_supplied_y)
73 widget->area.y = 0;
75 margin_top = (int) (widget->statusbar->height * (1 - d->height)) / 2 + 0.5 + widget->area.y;
76 pb_height = (int) ((widget->statusbar->height * d->height - (d->gap * (d->bars - 1))) / d->bars + 0.5);
77 left_offset = widget->area.x + d->padding_left;
79 for(i = 0; i < d->bars; i++)
81 pwidth = (int) d->percent[i] ? ((width - 2) * d->percent[i]) / 100 : 0;
83 draw_rectangle(ctx,
84 left_offset, margin_top,
85 width, pb_height,
86 False, d->bordercolor[i]);
88 if(pwidth > 0)
89 draw_rectangle(ctx,
90 left_offset + 1, margin_top + 1,
91 pwidth, pb_height - 2,
92 True, d->fg[i]);
94 if(width - 2 - pwidth > 0) /* not filled area */
95 draw_rectangle(ctx,
96 left_offset + 1 + pwidth, margin_top + 1,
97 width - 2 - pwidth, pb_height - 2,
98 True, d->bg[i]);
100 margin_top += (pb_height + d->gap);
103 widget->area.width = d->width;
104 widget->area.height = widget->statusbar->height;
105 return widget->area.width;
108 static void
109 progressbar_tell(Widget *widget, char *command)
111 Data *d = widget->data;
112 int i = 0, percent;
113 char * tok;
115 if(!command || !d->bars)
116 return;
118 for (tok = strtok(command, ","); tok && i < d->bars; tok = strtok(NULL, ","), i++)
120 percent = atoi(tok);
121 d->percent[i] = (percent < 0 ? 0 : (percent > 100 ? 100 : percent));
125 Widget *
126 progressbar_new(Statusbar *statusbar, cfg_t *config)
128 Widget *w;
129 Data *d;
130 char *color;
131 int i, phys_screen = get_phys_screen(statusbar->screen);
132 cfg_t *cfg;
135 w = p_new(Widget, 1);
136 widget_common_new(w, statusbar, config);
137 w->draw = progressbar_draw;
138 w->tell = progressbar_tell;
139 d = w->data = p_new(Data, 1);
140 d->width = cfg_getint(config, "width");
142 if(!(d->bars = cfg_size(config, "bar")))
144 warn("progressbar widget needs at least one bar section\n");
145 return w;
148 d->bg = p_new(XColor, d->bars);
149 d->fg = p_new(XColor, d->bars);
150 d->bordercolor = p_new(XColor, d->bars);
151 d->percent = p_new(int, d->bars);
153 for(i = 0; i < d->bars; i++)
155 cfg = cfg_getnsec(config, "bar", i);
157 if((color = cfg_getstr(cfg, "fg")))
158 d->fg[i] = initxcolor(phys_screen, color);
159 else
160 d->fg[i] = globalconf.screens[statusbar->screen].colors_normal[ColFG];
162 if((color = cfg_getstr(cfg, "bg")))
163 d->bg[i] = initxcolor(phys_screen, color);
164 else
165 d->bg[i] = globalconf.screens[statusbar->screen].colors_normal[ColBG];
167 if((color = cfg_getstr(cfg, "bordercolor")))
168 d->bordercolor[i] = initxcolor(phys_screen, color);
169 else
170 d->bordercolor[i] = d->fg[i];
175 d->height = cfg_getfloat(config, "height");
176 d->gap = cfg_getint(config, "gap");
177 d->padding_left = cfg_getint(config, "padding_left");
179 return w;
181 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80