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