Repository navigation
Expand file tree
/
Copy pathprogressbar.c
More file actions
670 lines (588 loc) · 21.6 KB
/
Copy pathprogressbar.c
File metadata and controls
670 lines (588 loc) · 21.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
/*
* progressbar.c - progressbar widget
*
* Copyright © 2007-2008 Julien Danjou <julien@danjou.info>
* Copyright © 2007-2008 Marco Candrian <mac@calmar.ws>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
#include "common/tokenize.h"
#include "widget.h"
extern awesome_t globalconf;
/** Progressbar bar data structure */
typedef struct
{
/** Title of the data/bar */
char *title;
/** These or lower values won't fill the bar at all*/
float min_value;
/** These or higher values fill the bar fully */
float max_value;
/** Pointer to value */
float value;
/** Reverse filling */
bool reverse;
/** Foreground color */
xcolor_t fg;
/** Foreground color of turned-off ticks */
xcolor_t fg_off;
/** Foreground color when bar is half-full */
xcolor_t fg_center;
/** Foreground color when bar is full */
xcolor_t fg_end;
/** Background color */
xcolor_t bg;
/** Border color */
xcolor_t border_color;
} bar_t;
/** Delete a bar.
* \param bar The bar to annihilate.
*/
static void
bar_delete(bar_t *bar)
{
p_delete(&bar->title);
}
DO_ARRAY(bar_t, bar, bar_delete)
/** Progressbar private data structure */
typedef struct
{
/** Width of the data_items */
int width;
/** Pixel between data items (bars) */
int gap;
/** Border width in pixels */
int border_width;
/** Padding between border and ticks/bar */
int border_padding;
/** Gap/distance between the individual ticks */
int ticks_gap;
/** Total number of ticks */
int ticks_count;
/** 90 Degree's turned */
bool vertical;
/** Height 0-1, where 1.0 is height of bar */
float height;
/** The bars */
bar_array_t bars;
} progressbar_data_t;
/** Add a new bar to the progressbar private data structure.
* \param bars The bar array.
* \param title The bar title.
* \return The new bar.
*/
static bar_t *
progressbar_bar_add(bar_array_t *bars, const char *title)
{
bar_t bar;
p_clear(&bar, 1);
bar.title = a_strdup(title);
bar.fg = globalconf.colors.fg;
bar.fg_off = globalconf.colors.bg;
bar.bg = globalconf.colors.bg;
bar.border_color = globalconf.colors.fg;
bar.max_value = 100.0;
/* append the bar in the list */
bar_array_append(bars, bar);
return &bars->tab[bars->len - 1];
}
/** Get the bar, and create one if it does not exist.
* \param bars The bar array.
* \param title The bar title.
* \return A maybe new bar.
*/
static bar_t *
progressbar_bar_get(bar_array_t *bars, const char *title)
{
bar_t *bar;
/* check if this section is defined already */
for(int j = 0; j < bars->len; j++)
{
bar = &bars->tab[j];
if(!a_strcmp(title, bar->title))
return bar;
}
/* no bar found -> create one */
return progressbar_bar_add(bars, title);
}
static area_t
progressbar_geometry(widget_t *widget, int screen, int height, int width)
{
area_t geometry;
progressbar_data_t *d = widget->data;
geometry.height = height;
if(d->vertical)
{
int pb_width = (int) ((d->width - 2 * (d->border_width + d->border_padding) * d->bars.len
- d->gap * (d->bars.len - 1)) / d->bars.len);
geometry.width = d->bars.len * (pb_width + 2 * (d->border_width + d->border_padding)
+ d->gap) - d->gap;
}
else
{
int pb_width = d->width - 2 * (d->border_width + d->border_padding);
if(d->ticks_count && d->ticks_gap)
{
int unit = (pb_width + d->ticks_gap) / d->ticks_count;
pb_width = unit * d->ticks_count - d->ticks_gap; /* rounded to match ticks... */
}
geometry.width = pb_width + 2 * (d->border_width + d->border_padding);
}
return geometry;
}
/** Draw a progressbar.
* \param ctx The draw context.
* \param screen The screen we're drawing for.
* \param w The widget node we're drawing for.
* \param offset Offset to draw at.
* \param used Space already used.
* \param object The object pointer we're drawing onto.
* \return The width used.
*/
static void
progressbar_draw(widget_t *widget, draw_context_t *ctx, area_t geometry,
int screen, wibox_t *p)
{
/* pb_.. values points to the widget inside a potential border */
int values_ticks, pb_x, pb_y, pb_height, pb_width, pb_progress, pb_offset;
int unit = 0; /* tick + gap */
area_t rectangle;
vector_t color_gradient;
progressbar_data_t *d = widget->data;
if(!d->bars.len)
return;
/* for a 'reversed' progressbar:
* basic progressbar:
* 1. the full space gets the size of the formerly empty one
* 2. the pattern must be mirrored
* 3. the formerly 'empty' side gets drawed with fg colors, the 'full' with bg-color
*
* ticks:
* 1. round the values to a full tick accordingly
* 2. finally draw the gaps
*/
pb_x = geometry.x + d->border_width + d->border_padding;
pb_offset = 0;
if(d->vertical)
{
pb_width = (int) ((d->width - 2 * (d->border_width + d->border_padding) * d->bars.len
- d->gap * (d->bars.len - 1)) / d->bars.len);
/** \todo maybe prevent to calculate that stuff below over and over again
* (->use static-values) */
pb_height = (int) (ctx->height * d->height + 0.5)
- 2 * (d->border_width + d->border_padding);
if(d->ticks_count && d->ticks_gap)
{
/* '+ d->ticks_gap' because a unit includes a ticks + ticks_gap */
unit = (pb_height + d->ticks_gap) / d->ticks_count;
pb_height = unit * d->ticks_count - d->ticks_gap;
}
pb_y = geometry.y + ((int) (ctx->height * (1 - d->height)) / 2)
+ d->border_width + d->border_padding;
for(int i = 0; i < d->bars.len; i++)
{
bar_t *bar = &d->bars.tab[i];
if(d->ticks_count && d->ticks_gap)
{
values_ticks = (int)(d->ticks_count * (bar->value - bar->min_value)
/ (bar->max_value - bar->min_value) + 0.5);
if(values_ticks)
pb_progress = values_ticks * unit - d->ticks_gap;
else
pb_progress = 0;
}
else
/* e.g.: min = 50; max = 56; 53 should show 50% graph
* (53(val) - 50(min) / (56(max) - 50(min) = 3 / 5 = 0.5 = 50%
* round that ( + 0.5 and (int)) and finally multiply with height
*/
pb_progress = (int) (pb_height * (bar->value - bar->min_value)
/ (bar->max_value - bar->min_value) + 0.5);
if(d->border_width)
{
/* border rectangle */
rectangle.x = pb_x + pb_offset - d->border_width - d->border_padding;
rectangle.y = pb_y - d->border_width - d->border_padding;
rectangle.width = pb_width + 2 * (d->border_padding + d->border_width);
rectangle.height = pb_height + 2 * (d->border_padding + d->border_width);
if(d->border_padding)
draw_rectangle(ctx, rectangle, 1.0, true, &bar->bg);
draw_rectangle(ctx, rectangle, d->border_width, false, &bar->border_color);
}
color_gradient.x = pb_x;
color_gradient.x_offset = 0;
color_gradient.y = pb_y;
/* new value/progress in px + pattern setup */
if(bar->reverse)
{
/* invert: top with bottom part */
pb_progress = pb_height - pb_progress;
color_gradient.y_offset = pb_height;
}
else
{
/* bottom to top */
color_gradient.y += pb_height;
color_gradient.y_offset = - pb_height;
}
/* bottom part */
if(pb_progress > 0)
{
rectangle.x = pb_x + pb_offset;
rectangle.y = pb_y + pb_height - pb_progress;
rectangle.width = pb_width;
rectangle.height = pb_progress;
/* fg color */
if(bar->reverse)
draw_rectangle(ctx, rectangle, 1.0, true, &bar->fg_off);
else
draw_rectangle_gradient(ctx, rectangle, 1.0, true, color_gradient,
&bar->fg, &bar->fg_center, &bar->fg_end);
}
/* top part */
if(pb_height - pb_progress > 0) /* not filled area */
{
rectangle.x = pb_x + pb_offset;
rectangle.y = pb_y;
rectangle.width = pb_width;
rectangle.height = pb_height - pb_progress;
/* bg color */
if(bar->reverse)
draw_rectangle_gradient(ctx, rectangle, 1.0, true, color_gradient,
&bar->fg, &bar->fg_center, &bar->fg_end);
else
draw_rectangle(ctx, rectangle, 1.0, true, &bar->fg_off);
}
/* draw gaps TODO: improve e.g all in one */
if(d->ticks_count && d->ticks_gap)
{
rectangle.width = pb_width;
rectangle.height = d->ticks_gap;
rectangle.x = pb_x + pb_offset;
for(rectangle.y = pb_y + (unit - d->ticks_gap);
pb_y + pb_height - d->ticks_gap >= rectangle.y;
rectangle.y += unit)
draw_rectangle(ctx, rectangle, 1.0, true, &bar->bg);
}
pb_offset += pb_width + d->gap + 2 * (d->border_width + d->border_padding);
}
}
else /* a horizontal progressbar */
{
pb_width = d->width - 2 * (d->border_width + d->border_padding);
if(d->ticks_count && d->ticks_gap)
{
unit = (pb_width + d->ticks_gap) / d->ticks_count;
pb_width = unit * d->ticks_count - d->ticks_gap; /* rounded to match ticks... */
}
pb_height = (int) ((ctx->height * d->height
- d->bars.len * 2 * (d->border_width + d->border_padding)
- (d->gap * (d->bars.len - 1))) / d->bars.len + 0.5);
pb_y = geometry.y + ((int) (ctx->height * (1 - d->height)) / 2)
+ d->border_width + d->border_padding;
for(int i = 0; i < d->bars.len; i++)
{
bar_t *bar = &d->bars.tab[i];
if(d->ticks_count && d->ticks_gap)
{
/* +0.5 rounds up ticks -> turn on a tick when half of it is reached */
values_ticks = (int)(d->ticks_count * (bar->value - bar->min_value)
/ (bar->max_value - bar->min_value) + 0.5);
if(values_ticks)
pb_progress = values_ticks * unit - d->ticks_gap;
else
pb_progress = 0;
}
else
pb_progress = (int) (pb_width * (bar->value - bar->min_value)
/ (bar->max_value - bar->min_value) + 0.5);
if(d->border_width)
{
/* border rectangle */
rectangle.x = pb_x - d->border_width - d->border_padding;
rectangle.y = pb_y + pb_offset - d->border_width - d->border_padding;
rectangle.width = pb_width + 2 * (d->border_padding + d->border_width);
rectangle.height = pb_height + 2 * (d->border_padding + d->border_width);
if(d->border_padding)
draw_rectangle(ctx, rectangle, 1.0, true, &bar->bg);
draw_rectangle(ctx, rectangle, d->border_width, false, &bar->border_color);
}
color_gradient.y = pb_y;
color_gradient.y_offset = 0;
color_gradient.x = pb_x;
/* new value/progress in px + pattern setup */
if(bar->reverse)
{
/* reverse: right to left */
pb_progress = pb_width - pb_progress;
color_gradient.x += pb_width;
color_gradient.x_offset = - pb_width;
}
else
/* left to right */
color_gradient.x_offset = pb_width;
/* left part */
if(pb_progress > 0)
{
rectangle.x = pb_x;
rectangle.y = pb_y + pb_offset;
rectangle.width = pb_progress;
rectangle.height = pb_height;
/* fg color */
if(bar->reverse)
draw_rectangle(ctx, rectangle, 1.0, true, &bar->fg_off);
else
draw_rectangle_gradient(ctx, rectangle, 1.0, true, color_gradient,
&bar->fg, &bar->fg_center, &bar->fg_end);
}
/* right part */
if(pb_width - pb_progress > 0)
{
rectangle.x = pb_x + pb_progress;
rectangle.y = pb_y + pb_offset;
rectangle.width = pb_width - pb_progress;
rectangle.height = pb_height;
/* bg color */
if(bar->reverse)
draw_rectangle_gradient(ctx, rectangle, 1.0, true, color_gradient,
&bar->fg, &bar->fg_center, &bar->fg_end);
else
draw_rectangle(ctx, rectangle, 1.0, true, &bar->fg_off);
}
/* draw gaps TODO: improve e.g all in one */
if(d->ticks_count && d->ticks_gap)
{
rectangle.width = d->ticks_gap;
rectangle.height = pb_height;
rectangle.y = pb_y + pb_offset;
for(rectangle.x = pb_x + (unit - d->ticks_gap);
pb_x + pb_width - d->ticks_gap >= rectangle.x;
rectangle.x += unit)
draw_rectangle(ctx, rectangle, 1.0, true, &bar->bg);
}
pb_offset += pb_height + d->gap + 2 * (d->border_width + d->border_padding);
}
}
}
/** Set various progressbar bars properties:
* \param L The Lua VM state.
* \return The number of elements pushed on the stack.
* \luastack
* \lvalue A widget.
* \lparam A bar name.
* \lparam A table with keys as properties names.
*/
static int
luaA_progressbar_bar_properties_set(lua_State *L)
{
size_t len;
widget_t **widget = luaA_checkudata(L, 1, "widget");
const char *buf, *title = luaL_checkstring(L, 2);
bar_t *bar;
progressbar_data_t *d = (*widget)->data;
xcolor_init_request_t reqs[6];
int i, reqs_nbr = -1;
luaA_checktable(L, 3);
bar = progressbar_bar_get(&d->bars, title);
if((buf = luaA_getopt_lstring(L, 3, "fg", NULL, &len)))
reqs[++reqs_nbr] = xcolor_init_unchecked(&bar->fg, buf, len);
if((buf = luaA_getopt_lstring(L, 3, "fg_off", NULL, &len)))
reqs[++reqs_nbr] = xcolor_init_unchecked(&bar->fg_off, buf, len);
if((buf = luaA_getopt_lstring(L, 3, "bg", NULL, &len)))
reqs[++reqs_nbr] = xcolor_init_unchecked(&bar->bg, buf, len);
if((buf = luaA_getopt_lstring(L, 3, "border_color", NULL, &len)))
reqs[++reqs_nbr] = xcolor_init_unchecked(&bar->border_color, buf, len);
if((buf = luaA_getopt_lstring(L, 3, "fg_center", NULL, &len)))
reqs[++reqs_nbr] = xcolor_init_unchecked(&bar->fg_center, buf, len);
if((buf = luaA_getopt_lstring(L, 3, "fg_end", NULL, &len)))
reqs[++reqs_nbr] = xcolor_init_unchecked(&bar->fg_end, buf, len);
bar->min_value = luaA_getopt_number(L, 3, "min_value", bar->min_value);
/* hack to prevent max_value beeing less than min_value
* and also preventing a division by zero when both are equal */
if(bar->max_value <= bar->min_value)
bar->max_value = bar->max_value + 0.0001;
/* force a actual value into the newly possible range */
if(bar->value < bar->min_value)
bar->value = bar->min_value;
bar->max_value = luaA_getopt_number(L, 3, "max_value", bar->max_value);
if(bar->min_value >= bar->max_value)
bar->min_value = bar->max_value - 0.0001;
if(bar->value > bar->max_value)
bar->value = bar->max_value;
bar->reverse = luaA_getopt_boolean(L, 3, "reverse", bar->reverse);
for(i = 0; i <= reqs_nbr; i++)
xcolor_init_reply(reqs[i]);
widget_invalidate_bywidget(*widget);
return 0;
}
/** Add a value to a progressbar bar.
* \param L The Lua VM state.
* \return The number of elements pushed on the stack.
* \luastack
* \lvalue A widget.
* \lparam A bar name.
* \lparam A data value.
*/
static int
luaA_progressbar_bar_data_add(lua_State *L)
{
widget_t **widget = luaA_checkudata(L, 1, "widget");
const char *title = luaL_checkstring(L, 2);
progressbar_data_t *d = (*widget)->data;
bar_t *bar;
bar = progressbar_bar_get(&d->bars, title);
bar->value = luaL_checknumber(L, 3);
bar->value = MAX(bar->min_value, MIN(bar->max_value, bar->value));
widget_invalidate_bywidget(*widget);
return 0;
}
/** Progressbar widget.
* \param L The Lua VM state.
* \param token The key token.
* \return The number of elements pushed on the stack.
* \luastack
* \lfield bar_properties_set Set the properties of a bar.
* \lfield bar_data_add Add data to a bar.
* \lfield gap Gap betweens bars.
* \lfield ticks_gap Gap between ticks.
* \lfield ticks_count Number of ticks.
* \lfield border_padding Border padding.
* \lfield border_width Border width.
* \lfield width Bars width.
* \lfield height Bars height.
* \lfield vertical True: draw bar vertically, false: horizontally.
*/
static int
luaA_progressbar_index(lua_State *L, awesome_token_t token)
{
widget_t **widget = luaA_checkudata(L, 1, "widget");
progressbar_data_t *d = (*widget)->data;
switch(token)
{
case A_TK_BAR_PROPERTIES_SET:
lua_pushcfunction(L, luaA_progressbar_bar_properties_set);
break;
case A_TK_BAR_DATA_ADD:
lua_pushcfunction(L, luaA_progressbar_bar_data_add);
break;
case A_TK_GAP:
lua_pushnumber(L, d->gap);
break;
case A_TK_TICKS_GAP:
lua_pushnumber(L, d->ticks_gap);
break;
case A_TK_TICKS_COUNT:
lua_pushnumber(L, d->ticks_count);
break;
case A_TK_BORDER_PADDING:
lua_pushnumber(L, d->border_padding);
break;
case A_TK_BORDER_WIDTH:
lua_pushnumber(L, d->border_width);
break;
case A_TK_WIDTH:
lua_pushnumber(L, d->width);
break;
case A_TK_HEIGHT:
lua_pushnumber(L, d->height);
break;
case A_TK_VERTICAL:
lua_pushboolean(L, d->vertical);
break;
default:
return 0;
}
return 1;
}
/** Newindex function for progressbar.
* \param L The Lua VM state.
* \param token The key token.
* \return The number of elements pushed on the stack.
*/
static int
luaA_progressbar_newindex(lua_State *L, awesome_token_t token)
{
widget_t **widget = luaA_checkudata(L, 1, "widget");
progressbar_data_t *d = (*widget)->data;
switch(token)
{
case A_TK_GAP:
d->gap = luaL_checknumber(L, 3);
break;
case A_TK_TICKS_COUNT:
d->ticks_count = luaL_checknumber(L, 3);
break;
case A_TK_TICKS_GAP:
d->ticks_gap = luaL_checknumber(L, 3);
break;
case A_TK_BORDER_PADDING:
d->border_padding = luaL_checknumber(L, 3);
break;
case A_TK_BORDER_WIDTH:
d->border_width = luaL_checknumber(L, 3);
break;
case A_TK_WIDTH:
d->width = luaL_checknumber(L, 3);
break;
case A_TK_HEIGHT:
d->height = luaL_checknumber(L, 3);
break;
case A_TK_VERTICAL:
d->vertical = luaA_checkboolean(L, 3);
break;
default:
return 0;
}
widget_invalidate_bywidget(*widget);
return 0;
}
/** Destroy a progressbar.
* \param widget The widget to kill.
*/
static void
progressbar_destructor(widget_t *widget)
{
progressbar_data_t *d = widget->data;
bar_array_wipe(&d->bars);
p_delete(&d);
}
/** Create a new progressbar.
* \param w The widget to initialize.
* \return A brand new progressbar.
*/
widget_t *
widget_progressbar(widget_t *w)
{
w->draw = progressbar_draw;
w->index = luaA_progressbar_index;
w->newindex = luaA_progressbar_newindex;
w->destructor = progressbar_destructor;
w->geometry = progressbar_geometry;
progressbar_data_t *d = w->data = p_new(progressbar_data_t, 1);
d->height = 0.80;
d->width = 80;
d->ticks_gap = 1;
d->border_width = 1;
d->gap = 2;
return w;
}
/* This is used for building documentation. */
static const struct luaL_reg awesome_progressbar_meta[] __attribute__ ((unused)) =
{
{ "bar_properties_set", luaA_progressbar_bar_properties_set },
{ "bar_data_add", luaA_progressbar_bar_data_add },
};
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80