9bd0c58e0828a654311dd274f17cbd1eb9f6a7ef
[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  * \version $Id$
36  *
37  * \author Stefano Fedrigo <aleph@develer.com>
38  * \author Francesco Sacchi <batt@develer.com>
39  *
40  */
41
42 #include "menubar.h"
43
44 #include <gfx/gfx.h>
45 #include <gfx/text.h>
46 #include <gfx/font.h>
47 #include <cfg/compiler.h>
48
49 #warning FIXME:This module is obsolete, you must refactor it!
50
51 #if 0
52 #if CPU_AVR
53         #include <avr/pgmspace.h> /* strlen_P() */
54 #else
55         #define strlen_P(s)             strlen(s)
56         #define text_puts_P(s, b)       text_puts(s, b)
57         #define pgm_read_uint16_t(addr) (*(addr))
58 #endif
59
60 #include <string.h> /* strlen, memcpy */
61
62
63 /** Predefined labels */
64 static const pgm_char lab_1[]  = "";
65 static const pgm_char lab_2[]  = "mute";
66 static const pgm_char lab_3[]  = "menu";
67 static const pgm_char lab_4[]  = "back";
68 static const pgm_char lab_5[]  = " ok ";
69 static const pgm_char lab_6[]  = "Ch 1";
70 static const pgm_char lab_7[]  = "Ch 2";
71 static const pgm_char lab_8[]  = "C1+2";
72 static const pgm_char lab_9[]  = " "UP_ARROW" ";
73 static const pgm_char lab_10[] = " "DOWN_ARROW" ";
74 static const pgm_char lab_11[] = " - ";
75 static const pgm_char lab_12[] = " + ";
76 static const pgm_char lab_13[] = "sel ";
77 static const pgm_char lab_14[] = "lock";
78 static const pgm_char lab_15[] = "unlock";
79 static const pgm_char lab_16[] = "more";
80 static const pgm_char lab_17[] = "edit";
81 static const pgm_char lab_18[] = "fast";
82 static const pgm_char lab_19[] = LEFT_ARROW" ";
83 static const pgm_char lab_20[] = " "RIGHT_ARROW;
84 static const pgm_char lab_21[] = "slow";
85 static const pgm_char lab_22[] = "yes";
86 static const pgm_char lab_23[] = "no";
87
88
89 static const pgm_char * PROGMEM label_strings[LABEL_CNT] = {
90         lab_1, lab_2, lab_3, lab_4, lab_5, lab_6, lab_7, lab_8, lab_9,
91         lab_10, lab_11, lab_12, lab_13, lab_14, lab_15, lab_16, lab_17,
92         lab_18, lab_19, lab_20, lab_21, lab_22, lab_23
93 };
94
95 /**
96  * Macro to access a label iptr_t: if a char pointer get the string pointed to
97  * in program memory, otherwise return the corrispondent predefined string
98  * (see label_strings in menubar.c)
99  */
100 #define PTRLBL(x) ((unsigned int)(x) < 256 ? \
101         (const pgm_char *)pgm_read_uint16_t(label_strings + (unsigned int)(x)) \
102         : (const pgm_char *)(x))
103
104
105 /**
106  * Initialize the MenuBar widget with the bitmap associated,
107  * the label names and the number of labels.
108  */
109 void mbar_init(
110                 struct MenuBar *mb,
111                 struct Bitmap *bmp,
112                 const_iptr_t labels[],
113                 int num_labels)
114 {
115         mb->bitmap     = bmp;
116         mb->labels     = labels;
117         mb->num_labels = num_labels;
118 }
119
120
121 /**
122  * Render the MenuBar on the bitmap.
123  */
124 void mbar_draw(const struct MenuBar *mb)
125 {
126         uint8_t oldstyle;
127         int i;
128         size_t maxlen = 0;  /* Length of the longest label */
129         coord_t x1, x2, y1, y2, label_padding;
130
131         /* Maximum space available for a label */
132         coord_t slot_width = mb->bitmap->width / mb->num_labels;
133
134         /* Find longest label */
135         for (i = 0; i < mb->num_labels; i++)
136                 if (strlen_P(PTRLBL(mb->labels[i])) > maxlen)
137                         maxlen = strlen_P(PTRLBL(mb->labels[i]));
138
139         oldstyle = text_style(mb->bitmap, STYLEF_INVERT, STYLEF_MASK);
140
141         /* y coords for menubar: bottom of the bitmap */
142         y1 = mb->bitmap->height - FONT_HEIGHT;
143         y2 = mb->bitmap->height;
144
145         /* Clear menubar area */
146         gfx_rectClear(mb->bitmap, 0, y1, mb->bitmap->width, y2);
147
148         for (i = 0; i < mb->num_labels; i++)
149         {
150                 size_t lablen = strlen_P(PTRLBL(mb->labels[i]));
151
152                 /* Don't draw empty labels */
153                 if (mb->labels[i] == (const_iptr_t)LABEL_EMPTY)
154                         continue;
155
156                 /* x coords: magic formula for equal distribution of the
157                  * labels along bitmap
158                  */
159                 label_padding = slot_width - (FONT_WIDTH * lablen + 2);
160                 x1 = i * (slot_width + (label_padding / (mb->num_labels - 1)));
161                 x2 = x1 + lablen * FONT_WIDTH + 1;
162
163                 /* Draw vertical line before.
164                  * Uncomment +1 for "rounded" menubars */
165                 gfx_line(mb->bitmap, x1, y1 /* + 1 */, x1, y2);
166
167                 /* Draw text */
168                 text_setCoord(mb->bitmap, x1 + 1, y1);
169                 text_puts_P(PTRLBL(mb->labels[i]), mb->bitmap);
170
171                 /* Draw vertical line after
172                  * Uncomment +1 for "rounded" menubars */
173                 gfx_line(mb->bitmap, x2, y1 /* + 1 */, x2, y2);
174         }
175
176         text_style(mb->bitmap, oldstyle, STYLEF_MASK);
177 }
178 #endif
179