if a proxy is set, pre-fill the proxy setting dialog with the old value.
[Rockbox.git] / apps / menu.c
blob8720ea6062354dc5813e55e4e33c27356fed8eb3
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>
26 #include "config.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 "settings_list.h"
39 #include "status.h"
40 #include "screens.h"
41 #include "talk.h"
42 #include "lang.h"
43 #include "misc.h"
44 #include "action.h"
45 #include "menus/exported_menus.h"
46 #include "string.h"
47 #include "root_menu.h"
48 #include "bookmark.h"
49 #include "gwps-common.h" /* for fade() */
50 #include "audio.h"
52 #ifdef HAVE_LCD_BITMAP
53 #include "icons.h"
54 #endif
56 /* gui api */
57 #include "list.h"
58 #include "statusbar.h"
59 #include "buttonbar.h"
61 #define MAX_MENUS 8
62 /* used to allow for dynamic menus */
63 #define MAX_MENU_SUBITEMS 64
64 static struct menu_item_ex *current_submenus_menu;
65 static int current_subitems[MAX_MENU_SUBITEMS];
66 static int current_subitems_count = 0;
68 static void get_menu_callback(const struct menu_item_ex *m,
69 menu_callback_type *menu_callback)
71 if (m->flags&(MENU_HAS_DESC|MENU_DYNAMIC_DESC))
72 *menu_callback= m->callback_and_desc->menu_callback;
73 else
74 *menu_callback = m->menu_callback;
77 static int get_menu_selection(int selected_item, const struct menu_item_ex *menu)
79 int type = (menu->flags&MENU_TYPE_MASK);
80 if (type == MT_MENU && (selected_item<current_subitems_count))
81 return current_subitems[selected_item];
82 return selected_item;
84 static int find_menu_selection(int selected)
86 int i;
87 for (i=0; i< current_subitems_count; i++)
88 if (current_subitems[i] == selected)
89 return i;
90 return 0;
92 static char * get_menu_item_name(int selected_item,void * data, char *buffer)
94 const struct menu_item_ex *menu = (const struct menu_item_ex *)data;
95 int type = (menu->flags&MENU_TYPE_MASK);
96 selected_item = get_menu_selection(selected_item, menu);
98 (void)buffer;
99 /* only MT_MENU or MT_RETURN_ID is allowed in here */
100 if (type == MT_RETURN_ID)
102 if (menu->flags&MENU_DYNAMIC_DESC)
103 return menu->menu_get_name_and_icon->list_get_name(selected_item,
104 menu->menu_get_name_and_icon->list_get_name_data, buffer);
105 return (char*)menu->strings[selected_item];
108 menu = menu->submenus[selected_item];
110 if ((menu->flags&MENU_DYNAMIC_DESC) && (type != MT_SETTING_W_TEXT))
111 return menu->menu_get_name_and_icon->list_get_name(selected_item,
112 menu->menu_get_name_and_icon->list_get_name_data, buffer);
114 type = (menu->flags&MENU_TYPE_MASK);
115 if ((type == MT_SETTING) || (type == MT_SETTING_W_TEXT))
117 const struct settings_list *v
118 = find_setting(menu->variable, NULL);
119 if (v)
120 return str(v->lang_id);
121 else return "Not Done yet!";
123 return P2STR(menu->callback_and_desc->desc);
125 #ifdef HAVE_LCD_BITMAP
126 static int menu_get_icon(int selected_item, void * data)
128 const struct menu_item_ex *menu = (const struct menu_item_ex *)data;
129 int menu_icon = Icon_NOICON;
130 selected_item = get_menu_selection(selected_item, menu);
132 if ((menu->flags&MENU_TYPE_MASK) == MT_RETURN_ID)
134 return Icon_Menu_functioncall;
136 menu = menu->submenus[selected_item];
137 if (menu->flags&MENU_HAS_DESC)
138 menu_icon = menu->callback_and_desc->icon_id;
139 else if (menu->flags&MENU_DYNAMIC_DESC)
140 menu_icon = menu->menu_get_name_and_icon->icon_id;
142 if (menu_icon == Icon_NOICON)
144 switch (menu->flags&MENU_TYPE_MASK)
146 case MT_SETTING:
147 case MT_SETTING_W_TEXT:
148 menu_icon = Icon_Menu_setting;
149 break;
150 case MT_MENU:
151 menu_icon = Icon_Submenu;
152 break;
153 case MT_FUNCTION_CALL:
154 case MT_RETURN_VALUE:
155 menu_icon = Icon_Menu_functioncall;
156 break;
159 return menu_icon;
161 #endif
163 static void init_menu_lists(const struct menu_item_ex *menu,
164 struct gui_synclist *lists, int selected, bool callback)
166 int i, count = MENU_GET_COUNT(menu->flags);
167 int type = (menu->flags&MENU_TYPE_MASK);
168 menu_callback_type menu_callback = NULL;
169 int icon;
170 current_subitems_count = 0;
172 if (type == MT_RETURN_ID)
173 get_menu_callback(menu, &menu_callback);
175 for (i=0; i<count; i++)
177 if (type != MT_RETURN_ID)
178 get_menu_callback(menu->submenus[i],&menu_callback);
179 if (menu_callback)
181 if (menu_callback(ACTION_REQUEST_MENUITEM,
182 type==MT_RETURN_ID ? (void*)(intptr_t)i: menu->submenus[i])
183 != ACTION_EXIT_MENUITEM)
185 current_subitems[current_subitems_count] = i;
186 current_subitems_count++;
189 else
191 current_subitems[current_subitems_count] = i;
192 current_subitems_count++;
195 current_submenus_menu = (struct menu_item_ex *)menu;
197 gui_synclist_init(lists,get_menu_item_name,(void*)menu,false,1);
198 #ifdef HAVE_LCD_BITMAP
199 if (menu->callback_and_desc->icon_id == Icon_NOICON)
200 icon = Icon_Submenu_Entered;
201 else
202 icon = menu->callback_and_desc->icon_id;
203 gui_synclist_set_title(lists, P2STR(menu->callback_and_desc->desc), icon);
204 gui_synclist_set_icon_callback(lists, menu_get_icon);
205 #else
206 (void)icon;
207 gui_synclist_set_icon_callback(lists, NULL);
208 #endif
209 gui_synclist_set_nb_items(lists,current_subitems_count);
210 gui_synclist_limit_scroll(lists,true);
211 gui_synclist_select_item(lists, find_menu_selection(selected));
213 get_menu_callback(menu,&menu_callback);
214 if (callback && menu_callback)
215 menu_callback(ACTION_ENTER_MENUITEM,menu);
216 gui_synclist_draw(lists);
219 static void talk_menu_item(const struct menu_item_ex *menu,
220 struct gui_synclist *lists)
222 int id = -1;
223 int type;
224 unsigned char *str;
225 int sel;
227 if (talk_menus_enabled())
229 sel = get_menu_selection(gui_synclist_get_sel_pos(lists),menu);
230 if ((menu->flags&MENU_TYPE_MASK) == MT_MENU)
232 type = menu->submenus[sel]->flags&MENU_TYPE_MASK;
233 if ((type == MT_SETTING) || (type == MT_SETTING_W_TEXT))
234 talk_setting(menu->submenus[sel]->variable);
235 else
237 if (menu->submenus[sel]->flags&(MENU_DYNAMIC_DESC))
239 char buffer[80];
240 str = menu->submenus[sel]->menu_get_name_and_icon->
241 list_get_name(sel, menu->submenus[sel]->
242 menu_get_name_and_icon->
243 list_get_name_data, buffer);
244 id = P2ID(str);
246 else
247 id = P2ID(menu->submenus[sel]->callback_and_desc->desc);
248 if (id != -1)
249 talk_id(id,false);
254 #define MAX_OPTIONS 32
255 /* returns true if the menu needs to be redrwan */
256 bool do_setting_from_menu(const struct menu_item_ex *temp)
258 int setting_id;
259 const struct settings_list *setting = find_setting(
260 temp->variable,
261 &setting_id);
262 bool ret_val = false;
263 unsigned char *title;
264 if (setting)
266 if ((temp->flags&MENU_TYPE_MASK) == MT_SETTING_W_TEXT)
267 title = temp->callback_and_desc->desc;
268 else
269 title = ID2P(setting->lang_id);
271 if ((setting->flags&F_BOOL_SETTING) == F_BOOL_SETTING)
273 bool temp_var, *var;
274 bool show_icons = global_settings.show_icons;
275 if (setting->flags&F_TEMPVAR)
277 temp_var = *(bool*)setting->setting;
278 var = &temp_var;
280 else
282 var = (bool*)setting->setting;
284 set_bool_options(P2STR(title), var,
285 STR(setting->bool_setting->lang_yes),
286 STR(setting->bool_setting->lang_no),
287 setting->bool_setting->option_callback);
288 if (setting->flags&F_TEMPVAR)
289 *(bool*)setting->setting = temp_var;
290 if (show_icons != global_settings.show_icons)
291 ret_val = true;
293 else if (setting->flags&F_T_SOUND)
295 set_sound(P2STR(title), setting->setting,
296 setting->sound_setting->setting);
298 else /* other setting, must be an INT type */
300 int temp_var, *var;
301 if (setting->flags&F_TEMPVAR)
303 temp_var = *(int*)setting->setting;
304 var = &temp_var;
306 else
308 var = (int*)setting->setting;
310 if (setting->flags&F_INT_SETTING)
312 int min, max, step;
313 if (setting->flags&F_FLIPLIST)
315 min = setting->int_setting->max;
316 max = setting->int_setting->min;
317 step = -setting->int_setting->step;
319 else
321 max = setting->int_setting->max;
322 min = setting->int_setting->min;
323 step = setting->int_setting->step;
325 set_int_ex(P2STR(title), NULL,
326 setting->int_setting->unit,var,
327 setting->int_setting->option_callback,
328 step, min, max,
329 setting->int_setting->formatter,
330 setting->int_setting->get_talk_id);
332 else if (setting->flags&F_CHOICE_SETTING)
334 static struct opt_items options[MAX_OPTIONS];
335 char buffer[256];
336 char *buf_start = buffer;
337 int buf_free = 256;
338 int i,j, count = setting->choice_setting->count;
339 for (i=0, j=0; i<count && i<MAX_OPTIONS; i++)
341 if (setting->flags&F_CHOICETALKS)
343 if (cfg_int_to_string(setting_id, i,
344 buf_start, buf_free))
346 int len = strlen(buf_start) +1;
347 options[j].string = buf_start;
348 buf_start += len;
349 buf_free -= len;
350 options[j].voice_id =
351 setting->choice_setting->talks[i];
352 j++;
355 else
357 options[j].string =
358 P2STR(setting->
359 choice_setting->desc[i]);
360 options[j].voice_id =
361 P2ID(setting->
362 choice_setting->desc[i]);
363 j++;
366 set_option(P2STR(title), var, INT,
367 options,j,
368 setting->
369 choice_setting->option_callback);
371 if (setting->flags&F_TEMPVAR)
372 *(int*)setting->setting = temp_var;
375 return ret_val;
378 int do_menu(const struct menu_item_ex *start_menu, int *start_selected)
380 int selected = start_selected? *start_selected : 0;
381 int action;
382 struct gui_synclist lists;
383 const struct menu_item_ex *temp, *menu;
384 int ret = 0;
385 #ifdef HAS_BUTTONBAR
386 struct gui_buttonbar buttonbar;
387 #endif
389 const struct menu_item_ex *menu_stack[MAX_MENUS];
390 int menu_stack_selected_item[MAX_MENUS];
391 int stack_top = 0;
392 bool in_stringlist, done = false;
393 menu_callback_type menu_callback = NULL;
394 bool talk_item = false;
395 if (start_menu == NULL)
396 menu = &main_menu_;
397 else menu = start_menu;
398 #ifdef HAS_BUTTONBAR
399 gui_buttonbar_init(&buttonbar);
400 gui_buttonbar_set_display(&buttonbar, &(screens[SCREEN_MAIN]) );
401 gui_buttonbar_set(&buttonbar, "<<<", "", "");
402 gui_buttonbar_draw(&buttonbar);
403 #endif
404 init_menu_lists(menu,&lists,selected,true);
405 in_stringlist = ((menu->flags&MENU_TYPE_MASK) == MT_RETURN_ID);
407 talk_menu_item(menu, &lists);
409 action_signalscreenchange();
411 /* load the callback, and only reload it if menu changes */
412 get_menu_callback(menu, &menu_callback);
413 gui_synclist_draw(&lists);
415 while (!done)
417 talk_item = false;
418 gui_syncstatusbar_draw(&statusbars, true);
419 action = get_action(CONTEXT_MAINMENU,HZ);
420 /* HZ so the status bar redraws corectly */
421 if (action == ACTION_NONE)
423 continue;
425 gui_synclist_draw(&lists);
427 if (menu_callback)
429 int old_action = action;
430 action = menu_callback(action, menu);
431 if (action == ACTION_EXIT_AFTER_THIS_MENUITEM)
433 action = old_action;
434 ret = MENU_SELECTED_EXIT; /* will exit after returning
435 from selection */
439 if (gui_synclist_do_button(&lists,action,LIST_WRAP_UNLESS_HELD))
441 talk_item = true;
443 else if (action == ACTION_TREE_WPS)
445 ret = GO_TO_PREVIOUS_MUSIC;
446 done = true;
448 else if (action == ACTION_TREE_STOP)
450 list_stop_handler();
452 else if (action == ACTION_STD_CONTEXT &&
453 menu == &root_menu_)
455 ret = GO_TO_ROOTITEM_CONTEXT;
456 done = true;
458 else if (action == ACTION_STD_MENU)
460 if (menu != &root_menu_)
461 ret = GO_TO_ROOT;
462 else
463 ret = GO_TO_PREVIOUS;
464 done = true;
466 else if (action == ACTION_STD_CANCEL)
468 in_stringlist = false;
469 if (menu_callback)
470 menu_callback(ACTION_EXIT_MENUITEM, menu);
472 if (menu->flags&MENU_EXITAFTERTHISMENU)
473 done = true;
474 if (stack_top > 0)
476 stack_top--;
477 menu = menu_stack[stack_top];
478 if (menu->flags&MENU_EXITAFTERTHISMENU)
479 done = true;
480 init_menu_lists(menu, &lists,
481 menu_stack_selected_item[stack_top], false);
482 /* new menu, so reload the callback */
483 get_menu_callback(menu, &menu_callback);
484 talk_item = true;
486 else if (menu != &root_menu_)
488 ret = GO_TO_PREVIOUS;
489 done = true;
492 else if (action == ACTION_STD_OK)
494 int type;
495 #ifdef HAS_BUTTONBAR
496 gui_buttonbar_unset(&buttonbar);
497 gui_buttonbar_draw(&buttonbar);
498 #endif
499 selected = get_menu_selection(gui_synclist_get_sel_pos(&lists), menu);
500 temp = menu->submenus[selected];
501 if (in_stringlist)
502 type = (menu->flags&MENU_TYPE_MASK);
503 else
505 type = (temp->flags&MENU_TYPE_MASK);
506 get_menu_callback(temp, &menu_callback);
507 if (menu_callback)
509 action = menu_callback(ACTION_ENTER_MENUITEM,temp);
510 if (action == ACTION_EXIT_MENUITEM)
511 break;
514 switch (type)
516 case MT_MENU:
517 if (stack_top < MAX_MENUS)
519 menu_stack[stack_top] = menu;
520 menu_stack_selected_item[stack_top] = selected;
521 stack_top++;
522 init_menu_lists(temp, &lists, 0, true);
523 menu = temp;
524 talk_item = true;
526 break;
527 case MT_FUNCTION_CALL:
529 int return_value;
530 talk_item = true;
531 action_signalscreenchange();
532 if (temp->flags&MENU_FUNC_USEPARAM)
533 return_value = temp->function->function_w_param(
534 temp->function->param);
535 else
536 return_value = temp->function->function();
537 if (temp->flags&MENU_FUNC_CHECK_RETVAL)
539 if (return_value == 1)
541 done = true;
542 ret = return_value;
545 break;
547 case MT_SETTING:
548 case MT_SETTING_W_TEXT:
550 if (do_setting_from_menu(temp))
551 init_menu_lists(menu, &lists, 0, true);
552 talk_item = true;
553 break;
555 case MT_RETURN_ID:
556 if (in_stringlist)
558 action_signalscreenchange();
559 done = true;
560 ret = selected;
562 else if (stack_top < MAX_MENUS)
564 menu_stack[stack_top] = menu;
565 menu_stack_selected_item[stack_top] = selected;
566 stack_top++;
567 menu = temp;
568 init_menu_lists(menu,&lists,0,false);
569 talk_item = true;
570 in_stringlist = true;
572 break;
573 case MT_RETURN_VALUE:
574 ret = temp->value;
575 done = true;
576 break;
578 if (type != MT_MENU)
580 if (menu_callback)
581 menu_callback(ACTION_EXIT_MENUITEM,temp);
583 if (current_submenus_menu != menu)
584 init_menu_lists(menu,&lists,selected,true);
585 /* callback was changed, so reload the menu's callback */
586 get_menu_callback(menu, &menu_callback);
587 if ((menu->flags&MENU_EXITAFTERTHISMENU) &&
588 !(temp->flags&MENU_EXITAFTERTHISMENU))
590 done = true;
591 break;
593 #ifdef HAS_BUTTONBAR
594 gui_buttonbar_set(&buttonbar, "<<<", "", "");
595 gui_buttonbar_draw(&buttonbar);
596 #endif
597 gui_synclist_draw(&lists);
599 else if(default_event_handler(action) == SYS_USB_CONNECTED)
601 ret = MENU_ATTACHED_USB;
602 done = true;
604 if (talk_item && !done)
605 talk_menu_item(menu, &lists);
607 action_signalscreenchange();
608 if (start_selected)
610 /* make sure the start_selected variable is set to
611 the selected item from the menu do_menu() was called from */
612 if (stack_top > 0)
614 menu = menu_stack[0];
615 init_menu_lists(menu,&lists,menu_stack_selected_item[0],true);
617 *start_selected = get_menu_selection(
618 gui_synclist_get_sel_pos(&lists), menu);
620 return ret;