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.
29 extern AwesomeConf globalconf
;
33 /** Percent 0 to 100 */
35 /** Width of the bars */
39 /** Pixel between bars */
43 /** Height 0-1, where 1 is height of statusbar */
45 /** Foreground color */
47 /** Background color */
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
;
64 width
= d
->width
- d
->padding_left
;
66 if(!widget
->user_supplied_x
)
67 widget
->area
.x
= widget_calculate_offset(widget
->statusbar
->width
,
72 if(!widget
->user_supplied_y
)
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;
84 left_offset
, margin_top
,
86 False
, d
->bordercolor
[i
]);
90 left_offset
+ 1, margin_top
+ 1,
91 pwidth
, pb_height
- 2,
94 if(width
- 2 - pwidth
> 0) /* not filled area */
96 left_offset
+ 1 + pwidth
, margin_top
+ 1,
97 width
- 2 - pwidth
, pb_height
- 2,
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
;
109 progressbar_tell(Widget
*widget
, char *command
)
111 Data
*d
= widget
->data
;
115 if(!command
|| !d
->bars
)
118 for (tok
= strtok(command
, ","); tok
&& i
< d
->bars
; tok
= strtok(NULL
, ","), i
++)
121 d
->percent
[i
] = (percent
< 0 ? 0 : (percent
> 100 ? 100 : percent
));
126 progressbar_new(Statusbar
*statusbar
, cfg_t
*config
)
131 int i
, phys_screen
= get_phys_screen(statusbar
->screen
);
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");
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
);
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
);
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
);
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");
181 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80