remove the need for action_signalscreenchange().
[Rockbox.git] / apps / plugins / lib / oldmenuapi.c
blobdbe151daf95e58084ea847418971c70708b8b71f
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 "plugin.h"
28 #include "oldmenuapi.h"
30 struct plugin_api *rb = NULL;
32 struct menu {
33 struct menu_item* items;
34 int (*callback)(int, int);
35 struct gui_synclist synclist;
38 #define MAX_MENUS 6
40 static struct menu menus[MAX_MENUS];
41 static bool inuse[MAX_MENUS] = { false };
43 static char * menu_get_itemname(int selected_item, void * data, char *buffer)
45 struct menu *local_menus=(struct menu *)data;
46 (void)buffer;
47 return(local_menus->items[selected_item].desc);
50 static int menu_find_free(void)
52 int i;
53 /* Tries to find an unused slot to put the new menu */
54 for ( i=0; i<MAX_MENUS; i++ ) {
55 if ( !inuse[i] ) {
56 inuse[i] = true;
57 break;
60 if ( i == MAX_MENUS ) {
61 DEBUGF("Out of menus!\n");
62 return -1;
64 return(i);
67 int menu_init(struct plugin_api *api, const struct menu_item* mitems,
68 int count, int (*callback)(int, int),
69 const char *button1, const char *button2, const char *button3)
71 int menu=menu_find_free();
72 rb = api;
73 if(menu==-1)/* Out of menus */
74 return -1;
75 menus[menu].items = (struct menu_item*)mitems; /* de-const */
76 rb->gui_synclist_init(&(menus[menu].synclist),
77 &menu_get_itemname, &menus[menu], false, 1);
78 rb->gui_synclist_set_icon_callback(&(menus[menu].synclist), NULL);
79 rb->gui_synclist_set_nb_items(&(menus[menu].synclist), count);
80 menus[menu].callback = callback;
81 (void)button1;
82 (void)button2;
83 (void)button3;
84 return menu;
87 void menu_exit(int m)
89 inuse[m] = false;
92 int menu_show(int m)
94 bool exit = false;
95 int key;
97 rb->gui_synclist_draw(&(menus[m].synclist));
98 rb->gui_syncstatusbar_draw(rb->statusbars, true);
99 while (!exit) {
100 key = rb->get_action(CONTEXT_MAINMENU,HZ/2);
102 * "short-circuit" the default keypresses by running the
103 * callback function
104 * The callback may return a new key value, often this will be
105 * BUTTON_NONE or the same key value, but it's perfectly legal
106 * to "simulate" key presses by returning another value.
108 if( menus[m].callback != NULL )
109 key = menus[m].callback(key, m);
110 rb->gui_synclist_do_button(&(menus[m].synclist), key,LIST_WRAP_UNLESS_HELD);
111 switch( key ) {
112 case ACTION_STD_OK:
113 return rb->gui_synclist_get_sel_pos(&(menus[m].synclist));
116 case ACTION_STD_CANCEL:
117 case ACTION_STD_MENU:
118 exit = true;
119 break;
121 default:
122 if(rb->default_event_handler(key) == SYS_USB_CONNECTED)
123 return MENU_ATTACHED_USB;
124 break;
126 rb->gui_syncstatusbar_draw(rb->statusbars, false);
128 return MENU_SELECTED_EXIT;
132 bool menu_run(int m)
134 int selected;
135 while (1) {
136 switch (selected=menu_show(m))
138 case MENU_SELECTED_EXIT:
139 return false;
141 case MENU_ATTACHED_USB:
142 return true;
144 default:
146 if (menus[m].items[selected].function &&
147 menus[m].items[selected].function())
148 return true;
149 rb->gui_syncstatusbar_draw(rb->statusbars, true);
153 return false;
157 * Property function - return the current cursor for "menu"
160 int menu_cursor(int menu)
162 return rb->gui_synclist_get_sel_pos(&(menus[menu].synclist));
166 * Property function - return the "menu" description at "position"
169 char* menu_description(int menu, int position)
171 return menus[menu].items[position].desc;
175 * Delete the element "position" from the menu items in "menu"
178 void menu_delete(int menu, int position)
180 int i;
181 int nb_items=rb->gui_synclist_get_nb_items(&(menus[menu].synclist));
182 /* copy the menu item from the one below */
183 for( i = position; i < nb_items - 1; i++)
184 menus[menu].items[i] = menus[menu].items[i + 1];
186 rb->gui_synclist_del_item(&(menus[menu].synclist));
189 void menu_insert(int menu, int position, char *desc, bool (*function) (void))
191 int i;
192 int nb_items=rb->gui_synclist_get_nb_items(&(menus[menu].synclist));
193 if(position < 0)
194 position = nb_items;
196 /* Move the items below one position forward */
197 for( i = nb_items; i > position; i--)
198 menus[menu].items[i] = menus[menu].items[i - 1];
200 /* Update the current item */
201 menus[menu].items[position].desc = (unsigned char *)desc;
202 menus[menu].items[position].function = function;
203 rb->gui_synclist_add_item(&(menus[menu].synclist));
207 * Property function - return the "count" of menu items in "menu"
210 int menu_count(int menu)
212 return rb->gui_synclist_get_nb_items(&(menus[menu].synclist));
216 * Allows to set the cursor position. Doesn't redraw by itself.
219 void menu_set_cursor(int menu, int position)
221 rb->gui_synclist_select_item(&(menus[menu].synclist), position);
223 #if 0
224 void menu_talk_selected(int m)
226 if(rb->global_settings->talk_menu)
228 int selected=rb->gui_synclist_get_sel_pos(&(menus[m].synclist));
229 int voice_id = P2ID(menus[m].items[selected].desc);
230 if (voice_id >= 0) /* valid ID given? */
231 talk_id(voice_id, false); /* say it */
234 #endif
235 void menu_draw(int m)
237 rb->gui_synclist_draw(&(menus[m].synclist));