Update preset.
[bertos.git] / bertos / gui / menubar.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos 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.
10  *
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.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2004, 2008 Develer S.r.l. (http://www.develer.com/)
30  * All Rights Reserved.
31  * -->
32  *
33  * \brief Graphics Menu bar widget
34  *
35  * \author Stefano Fedrigo <aleph@develer.com>
36  * \author Francesco Sacchi <batt@develer.com>
37  *
38  */
39
40 #include "menubar.h"
41
42 #include <gfx/gfx.h>
43 #include <gfx/text.h>
44 #include <gfx/font.h>
45 #include <cfg/compiler.h>
46
47 #warning FIXME:This module is obsolete, you must refactor it!
48
49 #if 0
50 #if CPU_AVR
51         #include <avr/pgmspace.h> /* strlen_P() */
52 #else
53         #define strlen_P(s)             strlen(s)
54         #define text_puts_P(s, b)       text_puts(s, b)
55         #define pgm_read_uint16_t(addr) (*(addr))
56 #endif
57
58 #include <string.h> /* strlen, memcpy */
59
60
61 /** Predefined labels */
62 static const pgm_char lab_1[]  = "";
63 static const pgm_char lab_2[]  = "mute";
64 static const pgm_char lab_3[]  = "menu";
65 static const pgm_char lab_4[]  = "back";
66 static const pgm_char lab_5[]  = " ok ";
67 static const pgm_char lab_6[]  = "Ch 1";
68 static const pgm_char lab_7[]  = "Ch 2";
69 static const pgm_char lab_8[]  = "C1+2";
70 static const pgm_char lab_9[]  = " "UP_ARROW" ";
71 static const pgm_char lab_10[] = " "DOWN_ARROW" ";
72 static const pgm_char lab_11[] = " - ";
73 static const pgm_char lab_12[] = " + ";
74 static const pgm_char lab_13[] = "sel ";
75 static const pgm_char lab_14[] = "lock";
76 static const pgm_char lab_15[] = "unlock";
77 static const pgm_char lab_16[] = "more";
78 static const pgm_char lab_17[] = "edit";
79 static const pgm_char lab_18[] = "fast";
80 static const pgm_char lab_19[] = LEFT_ARROW" ";
81 static const pgm_char lab_20[] = " "RIGHT_ARROW;
82 static const pgm_char lab_21[] = "slow";
83 static const pgm_char lab_22[] = "yes";
84 static const pgm_char lab_23[] = "no";
85
86
87 static const pgm_char * PROGMEM label_strings[LABEL_CNT] = {
88         lab_1, lab_2, lab_3, lab_4, lab_5, lab_6, lab_7, lab_8, lab_9,
89         lab_10, lab_11, lab_12, lab_13, lab_14, lab_15, lab_16, lab_17,
90         lab_18, lab_19, lab_20, lab_21, lab_22, lab_23
91 };
92
93 /**
94  * Macro to access a label iptr_t: if a char pointer get the string pointed to
95  * in program memory, otherwise return the corrispondent predefined string
96  * (see label_strings in menubar.c)
97  */
98 #define PTRLBL(x) ((unsigned int)(x) < 256 ? \
99         (const pgm_char *)pgm_read_uint16_t(label_strings + (unsigned int)(x)) \
100         : (const pgm_char *)(x))
101
102
103 /**
104  * Initialize the MenuBar widget with the bitmap associated,
105  * the label names and the number of labels.
106  */
107 void mbar_init(
108                 struct MenuBar *mb,
109                 struct Bitmap *bmp,
110                 const_iptr_t labels[],
111                 int num_labels)
112 {
113         mb->bitmap     = bmp;
114         mb->labels     = labels;
115         mb->num_labels = num_labels;
116 }
117
118
119 /**
120  * Render the MenuBar on the bitmap.
121  */
122 void mbar_draw(const struct MenuBar *mb)
123 {
124         uint8_t oldstyle;
125         int i;
126         size_t maxlen = 0;  /* Length of the longest label */
127         coord_t x1, x2, y1, y2, label_padding;
128
129         /* Maximum space available for a label */
130         coord_t slot_width = mb->bitmap->width / mb->num_labels;
131
132         /* Find longest label */
133         for (i = 0; i < mb->num_labels; i++)
134                 if (strlen_P(PTRLBL(mb->labels[i])) > maxlen)
135                         maxlen = strlen_P(PTRLBL(mb->labels[i]));
136
137         oldstyle = text_style(mb->bitmap, STYLEF_INVERT, STYLEF_MASK);
138
139         /* y coords for menubar: bottom of the bitmap */
140         y1 = mb->bitmap->height - FONT_HEIGHT;
141         y2 = mb->bitmap->height;
142
143         /* Clear menubar area */
144         gfx_rectClear(mb->bitmap, 0, y1, mb->bitmap->width, y2);
145
146         for (i = 0; i < mb->num_labels; i++)
147         {
148                 size_t lablen = strlen_P(PTRLBL(mb->labels[i]));
149
150                 /* Don't draw empty labels */
151                 if (mb->labels[i] == (const_iptr_t)LABEL_EMPTY)
152                         continue;
153
154                 /* x coords: magic formula for equal distribution of the
155                  * labels along bitmap
156                  */
157                 label_padding = slot_width - (FONT_WIDTH * lablen + 2);
158                 x1 = i * (slot_width + (label_padding / (mb->num_labels - 1)));
159                 x2 = x1 + lablen * FONT_WIDTH + 1;
160
161                 /* Draw vertical line before.
162                  * Uncomment +1 for "rounded" menubars */
163                 gfx_line(mb->bitmap, x1, y1 /* + 1 */, x1, y2);
164
165                 /* Draw text */
166                 text_setCoord(mb->bitmap, x1 + 1, y1);
167                 text_puts_P(PTRLBL(mb->labels[i]), mb->bitmap);
168
169                 /* Draw vertical line after
170                  * Uncomment +1 for "rounded" menubars */
171                 gfx_line(mb->bitmap, x2, y1 /* + 1 */, x2, y2);
172         }
173
174         text_style(mb->bitmap, oldstyle, STYLEF_MASK);
175 }
176 #endif
177