Update preset.
[bertos.git] / bertos / gui / levelbar.c
1 /**
2  * \file
3  * Copyright 2004, 2006 Develer S.r.l. (http://www.develer.com/)
4  *
5  *
6  * \brief Graphics user interface element to display a level bar.
7  *
8  * \author Stefano Fedrigo <aleph@develer.com>
9  */
10
11 #include "levelbar.h"
12
13
14 /**
15  * Initialize the LevelBar widget with the bitmap associated,
16  * the value range and the coordinates in the bitmap.
17  * \note The levelbar should be at least 5 pixels wide and high
18  *       for correct borders drawing. No check is done on this.
19  */
20 void lbar_init(struct LevelBar *lb, struct Bitmap *bmp, int type, int min, int max, int pos,
21                 coord_t x1, coord_t y1, coord_t x2, coord_t y2)
22 {
23         lb->bitmap = bmp;
24         lb->type = type;
25         lb->min = min;
26         lb->max = max;
27         lb->pos = pos;
28         lb->x1 = x1;
29         lb->y1 = y1;
30         lb->x2 = x2;
31         lb->y2 = y2;
32 }
33
34
35 /**
36  * Set the level.
37  */
38 void lbar_setLevel(struct LevelBar *lb, int level)
39 {
40         if (level < lb->min)
41                 level = lb->min;
42         if (level > lb->max)
43                 level = lb->max;
44
45         lb->pos = level;
46 }
47
48
49 /**
50  * Get current level.
51  */
52 int lbar_getLevel(struct LevelBar *lb)
53 {
54         return lb->pos;
55 }
56
57
58 /**
59  * Change level with respect to previous value
60  * (delta can be negative).
61  */
62 void lbar_changeLevel(struct LevelBar *lb, int delta)
63 {
64         lbar_setLevel(lb, lb->pos + delta);
65 }
66
67
68 /**
69  * Change the top limit.
70  */
71 void lbar_setMax(struct LevelBar *lb, int max)
72 {
73         lb->max = max;
74 }
75
76
77 /**
78  * Render the LevelBar on the bitmap.
79  */
80 void lbar_draw(struct LevelBar *lb)
81 {
82 #define BORDERW 1
83 #define BORDERH 1
84
85         /* Compute filled bar length in pixels */
86         int totlen = (lb->type & LBAR_HORIZONTAL) ? lb->x2 - lb->x1 - BORDERW*4 : lb->y2 - lb->y1 - BORDERH*4;
87         int range  = lb->max - lb->min;
88         int barlen = ((long)(lb->pos - lb->min) * (long)totlen + range - 1) / range;
89
90         // Draw border
91         gfx_rectDraw(lb->bitmap, lb->x1, lb->y1, lb->x2, lb->y2);
92
93         // Clear inside
94         gfx_rectClear(lb->bitmap, lb->x1 + BORDERW, lb->y1 + BORDERH, lb->x2 - BORDERW, lb->y2 - BORDERH);
95
96         // Draw bar
97         if (lb->type & LBAR_HORIZONTAL)
98                 gfx_rectFill(lb->bitmap,
99                                 lb->x1 + BORDERW*2, lb->y1 + BORDERH*2,
100                                 lb->x1 + BORDERW*2 + barlen, lb->y2 - BORDERH*2);
101         else
102                 gfx_rectFill(lb->bitmap,
103                                 lb->x1 + BORDERW*2, lb->y2 - BORDERH*2 - barlen,
104                                 lb->x2 - BORDERW*2, lb->y2 - BORDERH*2);
105 }