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.
25 #include "common/util.h"
27 extern AwesomeConf globalconf
;
31 /** Percent 0 to 100 */
33 /** Width of the bars */
37 /** Pixel between bars */
41 /** Height 0-1, where 1 is height of statusbar */
43 /** Foreground color */
45 /** Foreground color when bar is full */
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
;
60 Data
*d
= widget
->data
;
65 width
= d
->width
- 2 * d
->padding
;
67 if(!widget
->user_supplied_x
)
68 widget
->area
.x
= widget_calculate_offset(widget
->statusbar
->width
,
73 if(!widget
->user_supplied_y
)
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
]);
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
;
118 progressbar_tell(Widget
*widget
, char *command
)
120 Data
*d
= widget
->data
;
124 if(!command
|| !d
->bars
)
127 for (tok
= strtok(command
, ","); tok
&& i
< d
->bars
; tok
= strtok(NULL
, ","), i
++)
130 d
->percent
[i
] = (percent
< 0 ? 0 : (percent
> 100 ? 100 : percent
));
135 progressbar_new(Statusbar
*statusbar
, cfg_t
*config
)
140 int i
, phys_screen
= get_phys_screen(statusbar
->screen
);
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");
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
);
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
);
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
);
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
);
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");
195 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80