Colour targets: Revert an optimisation from almost 18 months ago that actually turned...
[Rockbox.git] / apps / plugins / lib / oldmenuapi.c
blobb83f926532f412201f90823d8ad790feb95af6e3
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 Robert E. Hak
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 2005 Kevin Ferrare :
23 - Multi screen support
24 - Rewrote/removed a lot of code now useless with the new gui API
26 #include <stdbool.h>
27 #include <stdlib.h>
29 #include "plugin.h"
30 #include "oldmenuapi.h"
32 const struct plugin_api *rb = NULL;
34 struct menu {
35 struct menu_item* items;
36 int (*callback)(int, int);
37 struct gui_synclist synclist;
40 #define MAX_MENUS 6
42 static struct menu menus[MAX_MENUS];
43 static bool inuse[MAX_MENUS] = { false };
45 static char * menu_get_itemname(int selected_item, void * data,
46 char *buffer, size_t buffer_len)
48 (void)buffer; (void)buffer_len;
49 struct menu *local_menus=(struct menu *)data;
50 return(local_menus->items[selected_item].desc);
53 static int menu_find_free(void)
55 int i;
56 /* Tries to find an unused slot to put the new menu */
57 for ( i=0; i<MAX_MENUS; i++ ) {
58 if ( !inuse[i] ) {
59 inuse[i] = true;
60 break;
63 if ( i == MAX_MENUS ) {
64 DEBUGF("Out of menus!\n");
65 return -1;
67 return(i);
70 int menu_init(const struct plugin_api *api, const struct menu_item* mitems,
71 int count, int (*callback)(int, int),
72 const char *button1, const char *button2, const char *button3)
74 int menu=menu_find_free();
75 rb = api;
76 if(menu==-1)/* Out of menus */
77 return -1;
78 menus[menu].items = (struct menu_item*)mitems; /* de-const */
79 rb->gui_synclist_init(&(menus[menu].synclist),
80 &menu_get_itemname, &menus[menu], false, 1, NULL);
81 rb->gui_synclist_set_icon_callback(&(menus[menu].synclist), NULL);
82 rb->gui_synclist_set_nb_items(&(menus[menu].synclist), count);
83 menus[menu].callback = callback;
84 (void)button1;
85 (void)button2;
86 (void)button3;
87 return menu;
90 void menu_exit(int m)
92 inuse[m] = false;
95 int menu_show(int m)
97 bool exit = false;
98 int key;
100 rb->gui_synclist_draw(&(menus[m].synclist));
101 rb->gui_syncstatusbar_draw(rb->statusbars, true);
102 while (!exit) {
103 key = rb->get_action(CONTEXT_MAINMENU,HZ/2);
105 * "short-circuit" the default keypresses by running the
106 * callback function
107 * The callback may return a new key value, often this will be
108 * BUTTON_NONE or the same key value, but it's perfectly legal
109 * to "simulate" key presses by returning another value.
111 if( menus[m].callback != NULL )
112 key = menus[m].callback(key, m);
113 rb->gui_synclist_do_button(&(menus[m].synclist), &key,LIST_WRAP_UNLESS_HELD);
114 switch( key ) {
115 case ACTION_STD_OK:
116 return rb->gui_synclist_get_sel_pos(&(menus[m].synclist));
118 case ACTION_STD_CANCEL:
119 case ACTION_STD_MENU:
120 case SYS_POWEROFF:
121 exit = true;
122 break;
124 default:
125 if(rb->default_event_handler(key) == SYS_USB_CONNECTED)
126 return MENU_ATTACHED_USB;
127 break;
129 rb->gui_syncstatusbar_draw(rb->statusbars, false);
131 return MENU_SELECTED_EXIT;
135 bool menu_run(int m)
137 int selected;
138 while (1) {
139 switch (selected=menu_show(m))
141 case MENU_SELECTED_EXIT:
142 return false;
144 case MENU_ATTACHED_USB:
145 return true;
147 default:
149 if (menus[m].items[selected].function &&
150 menus[m].items[selected].function())
151 return true;
152 rb->gui_syncstatusbar_draw(rb->statusbars, true);
156 return false;
160 * Property function - return the current cursor for "menu"
163 int menu_cursor(int menu)
165 return rb->gui_synclist_get_sel_pos(&(menus[menu].synclist));
169 * Property function - return the "menu" description at "position"
172 char* menu_description(int menu, int position)
174 return menus[menu].items[position].desc;
178 * Delete the element "position" from the menu items in "menu"
181 void menu_delete(int menu, int position)
183 int i;
184 int nb_items=rb->gui_synclist_get_nb_items(&(menus[menu].synclist));
185 /* copy the menu item from the one below */
186 for( i = position; i < nb_items - 1; i++)
187 menus[menu].items[i] = menus[menu].items[i + 1];
189 rb->gui_synclist_del_item(&(menus[menu].synclist));
192 void menu_insert(int menu, int position, char *desc, bool (*function) (void))
194 int i;
195 int nb_items=rb->gui_synclist_get_nb_items(&(menus[menu].synclist));
196 if(position < 0)
197 position = nb_items;
199 /* Move the items below one position forward */
200 for( i = nb_items; i > position; i--)
201 menus[menu].items[i] = menus[menu].items[i - 1];
203 /* Update the current item */
204 menus[menu].items[position].desc = (unsigned char *)desc;
205 menus[menu].items[position].function = function;
206 rb->gui_synclist_add_item(&(menus[menu].synclist));
210 * Property function - return the "count" of menu items in "menu"
213 int menu_count(int menu)
215 return rb->gui_synclist_get_nb_items(&(menus[menu].synclist));
219 * Allows to set the cursor position. Doesn't redraw by itself.
222 void menu_set_cursor(int menu, int position)
224 rb->gui_synclist_select_item(&(menus[menu].synclist), position);
226 #if 0
227 void menu_talk_selected(int m)
229 if(rb->global_settings->talk_menu)
231 int selected=rb->gui_synclist_get_sel_pos(&(menus[m].synclist));
232 int voice_id = P2ID(menus[m].items[selected].desc);
233 if (voice_id >= 0) /* valid ID given? */
234 talk_id(voice_id, false); /* say it */
237 #endif
238 void menu_draw(int m)
240 rb->gui_synclist_draw(&(menus[m].synclist));