1 is the correct value here. Not that it makes any difference... :)
[kugel-rb.git] / apps / menu.c
blobf861e0bddf1fd699cba193bd68b22f5dc9a96b0a
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 Robert E. Hak
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
20 2005 Kevin Ferrare :
21 - Multi screen support
22 - Rewrote/removed a lot of code now useless with the new gui API
24 #include <stdbool.h>
25 #include <stdlib.h>
27 #include "hwcompat.h"
28 #include "lcd.h"
29 #include "font.h"
30 #include "backlight.h"
31 #include "menu.h"
32 #include "button.h"
33 #include "kernel.h"
34 #include "debug.h"
35 #include "usb.h"
36 #include "panic.h"
37 #include "settings.h"
38 #include "status.h"
39 #include "screens.h"
40 #include "talk.h"
41 #include "lang.h"
42 #include "misc.h"
43 #include "action.h"
45 #ifdef HAVE_LCD_BITMAP
46 #include "icons.h"
47 #endif
49 /* gui api */
50 #include "list.h"
51 #include "statusbar.h"
52 #include "buttonbar.h"
54 struct menu {
55 struct menu_item* items;
56 int (*callback)(int, int);
57 #ifdef HAS_BUTTONBAR
58 struct gui_buttonbar buttonbar;
59 #endif
60 struct gui_synclist synclist;
63 #define MAX_MENUS 5
65 static struct menu menus[MAX_MENUS];
66 static bool inuse[MAX_MENUS] = { false };
68 char * menu_get_itemname(int selected_item, void * data, char *buffer)
70 struct menu *local_menus=(struct menu *)data;
71 (void)buffer;
72 return(P2STR(local_menus->items[selected_item].desc));
75 int menu_find_free(void)
77 int i;
78 /* Tries to find an unused slot to put the new menu */
79 for ( i=0; i<MAX_MENUS; i++ ) {
80 if ( !inuse[i] ) {
81 inuse[i] = true;
82 break;
85 if ( i == MAX_MENUS ) {
86 DEBUGF("Out of menus!\n");
87 return -1;
89 return(i);
92 int menu_init(const struct menu_item* mitems, int count, int (*callback)(int, int),
93 const char *button1, const char *button2, const char *button3)
95 int menu=menu_find_free();
96 if(menu==-1)/* Out of menus */
97 return -1;
98 menus[menu].items = (struct menu_item*)mitems; /* de-const */
99 gui_synclist_init(&(menus[menu].synclist),
100 &menu_get_itemname, &menus[menu], false, 1);
101 gui_synclist_set_icon_callback(&(menus[menu].synclist), NULL);
102 gui_synclist_set_nb_items(&(menus[menu].synclist), count);
103 menus[menu].callback = callback;
104 #ifdef HAS_BUTTONBAR
105 gui_buttonbar_init(&(menus[menu].buttonbar));
106 gui_buttonbar_set_display(&(menus[menu].buttonbar), &(screens[SCREEN_MAIN]) );
107 gui_buttonbar_set(&(menus[menu].buttonbar), button1, button2, button3);
108 #else
109 (void)button1;
110 (void)button2;
111 (void)button3;
112 #endif
113 return menu;
116 void menu_exit(int m)
118 inuse[m] = false;
121 int menu_show(int m)
123 #ifdef HAS_BUTTONBAR
124 gui_buttonbar_draw(&(menus[m].buttonbar));
125 #endif
126 bool exit = false;
127 int key;
129 gui_synclist_draw(&(menus[m].synclist));
130 gui_syncstatusbar_draw(&statusbars, true);
131 menu_talk_selected(m);
132 while (!exit) {
133 key = get_action(CONTEXT_MAINMENU,HZ/2);
135 * "short-circuit" the default keypresses by running the
136 * callback function
137 * The callback may return a new key value, often this will be
138 * BUTTON_NONE or the same key value, but it's perfectly legal
139 * to "simulate" key presses by returning another value.
141 if( menus[m].callback != NULL )
142 key = menus[m].callback(key, m);
143 /* If moved, "say" the entry under the cursor */
144 if(gui_synclist_do_button(&(menus[m].synclist), key))
145 menu_talk_selected(m);
146 switch( key ) {
147 case ACTION_STD_OK:
148 action_signalscreenchange();
149 return gui_synclist_get_sel_pos(&(menus[m].synclist));
152 case ACTION_STD_CANCEL:
153 case ACTION_STD_MENU:
154 exit = true;
155 break;
157 default:
158 if(default_event_handler(key) == SYS_USB_CONNECTED)
159 return MENU_ATTACHED_USB;
160 break;
162 gui_syncstatusbar_draw(&statusbars, false);
164 action_signalscreenchange();
165 return MENU_SELECTED_EXIT;
169 bool menu_run(int m)
171 int selected;
172 while (1) {
173 switch (selected=menu_show(m))
175 case MENU_SELECTED_EXIT:
176 return false;
178 case MENU_ATTACHED_USB:
179 return true;
181 default:
183 if (menus[m].items[selected].function &&
184 menus[m].items[selected].function())
185 return true;
186 gui_syncstatusbar_draw(&statusbars, true);
190 return false;
194 * Property function - return the current cursor for "menu"
197 int menu_cursor(int menu)
199 return gui_synclist_get_sel_pos(&(menus[menu].synclist));
203 * Property function - return the "menu" description at "position"
206 char* menu_description(int menu, int position)
208 return P2STR(menus[menu].items[position].desc);
212 * Delete the element "position" from the menu items in "menu"
215 void menu_delete(int menu, int position)
217 int i;
218 int nb_items=gui_synclist_get_nb_items(&(menus[menu].synclist));
219 /* copy the menu item from the one below */
220 for( i = position; i < nb_items - 1; i++)
221 menus[menu].items[i] = menus[menu].items[i + 1];
223 gui_synclist_del_item(&(menus[menu].synclist));
226 void menu_insert(int menu, int position, char *desc, bool (*function) (void))
228 int i;
229 int nb_items=gui_synclist_get_nb_items(&(menus[menu].synclist));
230 if(position < 0)
231 position = nb_items;
233 /* Move the items below one position forward */
234 for( i = nb_items; i > position; i--)
235 menus[menu].items[i] = menus[menu].items[i - 1];
237 /* Update the current item */
238 menus[menu].items[position].desc = (unsigned char *)desc;
239 menus[menu].items[position].function = function;
240 gui_synclist_add_item(&(menus[menu].synclist));
244 * Property function - return the "count" of menu items in "menu"
247 int menu_count(int menu)
249 return gui_synclist_get_nb_items(&(menus[menu].synclist));
253 * Allows a menu item at the current cursor position in "menu"
254 * to be moved up the list
257 bool menu_moveup(int menu)
259 struct menu_item swap;
260 int selected=menu_cursor(menu);
261 /* can't be the first item ! */
262 if( selected == 0)
263 return false;
265 /* use a temporary variable to do the swap */
266 swap = menus[menu].items[selected - 1];
267 menus[menu].items[selected - 1] = menus[menu].items[selected];
268 menus[menu].items[selected] = swap;
270 gui_synclist_select_previous(&(menus[menu].synclist));
271 return true;
275 * Allows a menu item at the current cursor position in "menu" to be moved down the list
278 bool menu_movedown(int menu)
280 struct menu_item swap;
281 int selected=menu_cursor(menu);
282 int nb_items=gui_synclist_get_nb_items(&(menus[menu].synclist));
284 /* can't be the last item ! */
285 if( selected == nb_items - 1)
286 return false;
288 /* use a temporary variable to do the swap */
289 swap = menus[menu].items[selected + 1];
290 menus[menu].items[selected + 1] = menus[menu].items[selected];
291 menus[menu].items[selected] = swap;
293 gui_synclist_select_next(&(menus[menu].synclist));
294 return true;
298 * Allows to set the cursor position. Doesn't redraw by itself.
301 void menu_set_cursor(int menu, int position)
303 gui_synclist_select_item(&(menus[menu].synclist), position);
306 void menu_talk_selected(int m)
308 if(global_settings.talk_menu)
310 int selected=gui_synclist_get_sel_pos(&(menus[m].synclist));
311 int voice_id = P2ID(menus[m].items[selected].desc);
312 if (voice_id >= 0) /* valid ID given? */
313 talk_id(voice_id, false); /* say it */
317 void menu_draw(int m)
319 gui_synclist_draw(&(menus[m].synclist));
322 /* count in letter positions, NOT pixels */
323 void put_cursorxy(int x, int y, bool on)
325 #ifdef HAVE_LCD_BITMAP
326 int fh, fw;
327 int xpos, ypos;
329 /* check here instead of at every call (ugly, but cheap) */
330 if (global_settings.invert_cursor)
331 return;
333 lcd_getstringsize((unsigned char *)"A", &fw, &fh);
334 xpos = x*6;
335 ypos = y*fh + lcd_getymargin();
336 if ( fh > 8 )
337 ypos += (fh - 8) / 2;
338 #endif
340 /* place the cursor */
341 if(on) {
342 #ifdef HAVE_LCD_BITMAP
343 lcd_mono_bitmap(bitmap_icons_6x8[Icon_Cursor], xpos, ypos, 4, 8);
344 #else
345 lcd_putc(x, y, CURSOR_CHAR);
346 #endif
348 else {
349 #if defined(HAVE_LCD_BITMAP)
350 /* I use xy here since it needs to disregard the margins */
351 lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
352 lcd_fillrect (xpos, ypos, 4, 8);
353 lcd_set_drawmode(DRMODE_SOLID);
354 #else
355 lcd_putc(x, y, ' ');
356 #endif