Czech language: Some minor terminology changes by Marek Salaba FS#11935
[kugel-rb.git] / apps / menu.c
blob8ea2f0883eaf61a1fbacf7e7323388b7362b701d
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>
28 #include "config.h"
29 #include "system.h"
31 #include "appevents.h"
32 #include "lcd.h"
33 #include "font.h"
34 #include "file.h"
35 #include "menu.h"
36 #include "button.h"
37 #include "kernel.h"
38 #include "debug.h"
39 #include "usb.h"
40 #include "panic.h"
41 #include "settings.h"
42 #include "settings_list.h"
43 #include "option_select.h"
44 #include "screens.h"
45 #include "talk.h"
46 #include "lang.h"
47 #include "misc.h"
48 #include "action.h"
49 #include "menus/exported_menus.h"
50 #include "string.h"
51 #include "root_menu.h"
52 #include "audio.h"
53 #include "viewport.h"
54 #include "quickscreen.h"
56 #ifdef HAVE_LCD_BITMAP
57 #include "icons.h"
58 #endif
60 /* gui api */
61 #include "list.h"
62 #include "buttonbar.h"
64 #define MAX_MENUS 8
65 /* used to allow for dynamic menus */
66 #define MAX_MENU_SUBITEMS 64
67 static struct menu_item_ex *current_submenus_menu;
68 static int current_subitems[MAX_MENU_SUBITEMS];
69 static int current_subitems_count = 0;
70 static int talk_menu_item(int selected_item, void *data);
72 static void get_menu_callback(const struct menu_item_ex *m,
73 menu_callback_type *menu_callback)
75 if (m->flags&(MENU_HAS_DESC|MENU_DYNAMIC_DESC))
76 *menu_callback= m->callback_and_desc->menu_callback;
77 else
78 *menu_callback = m->menu_callback;
81 static int get_menu_selection(int selected_item, const struct menu_item_ex *menu)
83 int type = (menu->flags&MENU_TYPE_MASK);
84 if ((type == MT_MENU || type == MT_RETURN_ID)
85 && (selected_item<current_subitems_count))
86 return current_subitems[selected_item];
87 return selected_item;
89 static int find_menu_selection(int selected)
91 int i;
92 for (i=0; i< current_subitems_count; i++)
93 if (current_subitems[i] == selected)
94 return i;
95 return 0;
97 static const char* get_menu_item_name(int selected_item,
98 void * data,
99 char *buffer,
100 size_t buffer_len)
102 const struct menu_item_ex *menu = (const struct menu_item_ex *)data;
103 int type = (menu->flags&MENU_TYPE_MASK);
104 selected_item = get_menu_selection(selected_item, menu);
106 (void)buffer_len;
107 /* only MT_MENU or MT_RETURN_ID is allowed in here */
108 if (type == MT_RETURN_ID)
110 if (menu->flags&MENU_DYNAMIC_DESC)
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);
113 return menu->strings[selected_item];
116 menu = menu->submenus[selected_item];
118 if ((menu->flags&MENU_DYNAMIC_DESC) && (type != MT_SETTING_W_TEXT))
119 return menu->menu_get_name_and_icon->list_get_name(selected_item,
120 menu->menu_get_name_and_icon->list_get_name_data, buffer);
122 type = (menu->flags&MENU_TYPE_MASK);
123 if ((type == MT_SETTING) || (type == MT_SETTING_W_TEXT))
125 const struct settings_list *v
126 = find_setting(menu->variable, NULL);
127 if (v)
128 return str(v->lang_id);
129 else return "Not Done yet!";
131 return P2STR(menu->callback_and_desc->desc);
133 #ifdef HAVE_LCD_BITMAP
134 static enum themable_icons menu_get_icon(int selected_item, void * data)
136 const struct menu_item_ex *menu = (const struct menu_item_ex *)data;
137 int menu_icon = Icon_NOICON;
138 selected_item = get_menu_selection(selected_item, menu);
140 if ((menu->flags&MENU_TYPE_MASK) == MT_RETURN_ID)
142 return Icon_Menu_functioncall;
144 menu = menu->submenus[selected_item];
145 if (menu->flags&MENU_HAS_DESC)
146 menu_icon = menu->callback_and_desc->icon_id;
147 else if (menu->flags&MENU_DYNAMIC_DESC)
148 menu_icon = menu->menu_get_name_and_icon->icon_id;
150 if (menu_icon == Icon_NOICON)
152 switch (menu->flags&MENU_TYPE_MASK)
154 case MT_SETTING:
155 case MT_SETTING_W_TEXT:
156 menu_icon = Icon_Menu_setting;
157 break;
158 case MT_MENU:
159 menu_icon = Icon_Submenu;
160 break;
161 case MT_FUNCTION_CALL:
162 case MT_RETURN_VALUE:
163 menu_icon = Icon_Menu_functioncall;
164 break;
167 return menu_icon;
169 #endif
171 static void init_menu_lists(const struct menu_item_ex *menu,
172 struct gui_synclist *lists, int selected, bool callback,
173 struct viewport parent[NB_SCREENS])
175 int i, count = MENU_GET_COUNT(menu->flags);
176 int type = (menu->flags&MENU_TYPE_MASK);
177 menu_callback_type menu_callback = NULL;
178 int icon;
179 current_subitems_count = 0;
181 if (type == MT_RETURN_ID)
182 get_menu_callback(menu, &menu_callback);
184 for (i=0; i<count; i++)
186 if (type != MT_RETURN_ID)
187 get_menu_callback(menu->submenus[i],&menu_callback);
188 if (menu_callback)
190 if (menu_callback(ACTION_REQUEST_MENUITEM,
191 type==MT_RETURN_ID ? (void*)(intptr_t)i: menu->submenus[i])
192 != ACTION_EXIT_MENUITEM)
194 current_subitems[current_subitems_count] = i;
195 current_subitems_count++;
198 else
200 current_subitems[current_subitems_count] = i;
201 current_subitems_count++;
204 current_submenus_menu = (struct menu_item_ex *)menu;
206 gui_synclist_init(lists,get_menu_item_name,(void*)menu,false,1, parent);
207 #ifdef HAVE_LCD_BITMAP
208 if (menu->callback_and_desc->icon_id == Icon_NOICON)
209 icon = Icon_Submenu_Entered;
210 else
211 icon = menu->callback_and_desc->icon_id;
212 gui_synclist_set_title(lists, P2STR(menu->callback_and_desc->desc), icon);
213 gui_synclist_set_icon_callback(lists, global_settings.show_icons?menu_get_icon:NULL);
214 #else
215 (void)icon;
216 gui_synclist_set_icon_callback(lists, NULL);
217 #endif
218 if(global_settings.talk_menu)
219 gui_synclist_set_voice_callback(lists, talk_menu_item);
220 gui_synclist_set_nb_items(lists,current_subitems_count);
221 gui_synclist_limit_scroll(lists,true);
222 gui_synclist_select_item(lists, find_menu_selection(selected));
224 get_menu_callback(menu,&menu_callback);
225 if (callback && menu_callback)
226 menu_callback(ACTION_ENTER_MENUITEM,menu);
229 static int talk_menu_item(int selected_item, void *data)
231 const struct menu_item_ex *menu = (const struct menu_item_ex *)data;
232 int id = -1;
233 int type;
234 unsigned char *str;
235 int sel = get_menu_selection(selected_item, menu);
237 if ((menu->flags&MENU_TYPE_MASK) == MT_MENU)
239 type = menu->submenus[sel]->flags&MENU_TYPE_MASK;
240 if ((type == MT_SETTING) || (type == MT_SETTING_W_TEXT))
241 talk_setting(menu->submenus[sel]->variable);
242 else
244 if (menu->submenus[sel]->flags&(MENU_DYNAMIC_DESC))
246 int (*list_speak_item)(int selected_item, void * data)
247 = menu->submenus[sel]->menu_get_name_and_icon->
248 list_speak_item;
249 if(list_speak_item)
250 list_speak_item(sel, menu->submenus[sel]->
251 menu_get_name_and_icon->
252 list_get_name_data);
253 else
255 char buffer[80];
256 str = menu->submenus[sel]->menu_get_name_and_icon->
257 list_get_name(sel, menu->submenus[sel]->
258 menu_get_name_and_icon->
259 list_get_name_data, buffer);
260 id = P2ID(str);
263 else
264 id = P2ID(menu->submenus[sel]->callback_and_desc->desc);
265 if (id != -1)
266 talk_id(id,false);
269 else if(((menu->flags&MENU_TYPE_MASK) == MT_RETURN_ID))
271 if ((menu->flags&MENU_DYNAMIC_DESC) == 0)
273 unsigned char *s = (unsigned char *)menu->strings[sel];
274 /* string list, try to talk it if ID2P was used */
275 id = P2ID(s);
276 if (id != -1)
277 talk_id(id,false);
280 return 0;
283 void do_setting_from_menu(const struct menu_item_ex *temp,
284 struct viewport parent[NB_SCREENS])
286 int setting_id;
287 const struct settings_list *setting =
288 find_setting(temp->variable, &setting_id);
289 char *title;
290 char padded_title[MAX_PATH];
291 if ((temp->flags&MENU_TYPE_MASK) == MT_SETTING_W_TEXT)
292 title = temp->callback_and_desc->desc;
293 else
294 title = ID2P(setting->lang_id);
296 /* Pad the title string by repeating it. This is needed
297 so the scroll settings title can actually be used to
298 test the setting */
299 if (setting->flags&F_PADTITLE)
301 int i = 0, len;
302 if (setting->lang_id == -1)
303 title = (char*)setting->cfg_vals;
304 else
305 title = P2STR((unsigned char*)title);
306 len = strlen(title);
307 while (i < MAX_PATH-1)
309 int padlen = MIN(len, MAX_PATH-1-i);
310 memcpy(&padded_title[i], title, padlen);
311 i += padlen;
312 if (i<MAX_PATH-1)
313 padded_title[i++] = ' ';
315 padded_title[i] = '\0';
316 title = padded_title;
319 option_screen((struct settings_list *)setting, parent,
320 setting->flags&F_TEMPVAR, title);
323 /* display a menu */
324 int do_menu(const struct menu_item_ex *start_menu, int *start_selected,
325 struct viewport parent[NB_SCREENS], bool hide_theme)
327 int selected = start_selected? *start_selected : 0;
328 int action;
329 struct gui_synclist lists;
330 const struct menu_item_ex *temp, *menu = start_menu;
331 int ret = 0, i;
332 bool redraw_lists;
333 int old_audio_status = audio_status();
334 FOR_NB_SCREENS(i)
335 viewportmanager_theme_enable(i, !hide_theme, NULL);
337 const struct menu_item_ex *menu_stack[MAX_MENUS];
338 int menu_stack_selected_item[MAX_MENUS];
339 int stack_top = 0;
340 bool in_stringlist, done = false;
341 struct viewport *vps = NULL;
342 #ifdef HAVE_BUTTONBAR
343 struct gui_buttonbar buttonbar;
344 gui_buttonbar_init(&buttonbar);
345 gui_buttonbar_set_display(&buttonbar, &(screens[SCREEN_MAIN]) );
346 gui_buttonbar_set(&buttonbar, "<<<", "", "");
347 #endif
349 menu_callback_type menu_callback = NULL;
351 /* if hide_theme is true, assume parent has been fixed before passed into
352 * this function, e.g. with viewport_set_defaults(parent, screen) */
353 init_menu_lists(menu, &lists, selected, true, parent);
354 vps = *(lists.parent);
355 in_stringlist = ((menu->flags&MENU_TYPE_MASK) == MT_RETURN_ID);
356 /* load the callback, and only reload it if menu changes */
357 get_menu_callback(menu, &menu_callback);
359 gui_synclist_draw(&lists);
360 gui_synclist_speak_item(&lists);
361 #ifdef HAVE_BUTTONBAR
362 if (!hide_theme)
364 gui_buttonbar_set(&buttonbar, "<<<", "", "");
365 gui_buttonbar_draw(&buttonbar);
367 #endif
368 while (!done)
370 int new_audio_status;
371 redraw_lists = false;
372 if (!hide_theme)
374 #ifdef HAVE_BUTTONBAR
375 gui_buttonbar_draw(&buttonbar);
376 #endif
378 action = get_action(CONTEXT_MAINMENU,
379 list_do_action_timeout(&lists, HZ));
381 /* query audio status to see if it changed */
382 new_audio_status = audio_status();
383 if (old_audio_status != new_audio_status)
384 { /* force a redraw if anything changed the audio status
385 * from outside */
386 redraw_lists = true;
387 old_audio_status = new_audio_status;
389 /* HZ so the status bar redraws corectly */
391 if (menu_callback)
393 int old_action = action;
394 action = menu_callback(action, menu);
395 if (action == ACTION_EXIT_AFTER_THIS_MENUITEM)
397 action = old_action;
398 ret = MENU_SELECTED_EXIT; /* will exit after returning
399 from selection */
401 else if (action == ACTION_REDRAW)
403 action = old_action;
404 redraw_lists = true;
408 if (gui_synclist_do_button(&lists, &action, LIST_WRAP_UNLESS_HELD))
409 continue;
410 #ifdef HAVE_QUICKSCREEN
411 else if (action == ACTION_STD_QUICKSCREEN)
413 quick_screen_quick(action);
414 redraw_lists = true;
416 #endif
417 #ifdef HAVE_RECORDING
418 else if (action == ACTION_STD_REC)
420 ret = GO_TO_RECSCREEN;
421 done = true;
423 #endif
424 else if (action == ACTION_TREE_WPS)
426 ret = GO_TO_PREVIOUS_MUSIC;
427 done = true;
429 else if (action == ACTION_TREE_STOP)
431 redraw_lists = list_stop_handler();
433 else if (action == ACTION_STD_CONTEXT)
435 if (menu == &root_menu_)
437 ret = GO_TO_ROOTITEM_CONTEXT;
438 done = true;
440 else if (!in_stringlist)
442 int type;
443 selected = get_menu_selection(gui_synclist_get_sel_pos(&lists),menu);
444 temp = menu->submenus[selected];
445 type = (temp->flags&MENU_TYPE_MASK);
446 if ((type == MT_SETTING_W_TEXT || type == MT_SETTING))
448 #ifdef HAVE_QUICKSCREEN
449 MENUITEM_STRINGLIST(quickscreen_able_option,
450 ID2P(LANG_ONPLAY_MENU_TITLE), NULL,
451 ID2P(LANG_RESET_SETTING),
452 ID2P(LANG_TOP_QS_ITEM),
453 ID2P(LANG_LEFT_QS_ITEM),
454 ID2P(LANG_BOTTOM_QS_ITEM),
455 ID2P(LANG_RIGHT_QS_ITEM));
456 #endif
457 MENUITEM_STRINGLIST(notquickscreen_able_option,
458 ID2P(LANG_ONPLAY_MENU_TITLE), NULL,
459 ID2P(LANG_RESET_SETTING));
460 const struct menu_item_ex *menu;
461 int menu_selection = 0;
462 const struct settings_list *setting =
463 find_setting(temp->variable, NULL);
464 #ifdef HAVE_QUICKSCREEN
465 if (is_setting_quickscreenable(setting))
466 menu = &quickscreen_able_option;
467 else
468 #endif
469 menu = &notquickscreen_able_option;
470 switch (do_menu(menu, &menu_selection, NULL, false))
472 case GO_TO_PREVIOUS:
473 break;
474 case 0: /* reset setting */
475 reset_setting(setting, setting->setting);
476 break;
477 #ifdef HAVE_QUICKSCREEN
478 case 1: /* set as top QS item */
479 set_as_qs_item(setting, QUICKSCREEN_TOP);
480 break;
481 case 2: /* set as left QS item */
482 set_as_qs_item(setting, QUICKSCREEN_LEFT);
483 break;
484 case 3: /* set as bottom QS item */
485 set_as_qs_item(setting, QUICKSCREEN_BOTTOM);
486 break;
487 case 4: /* set as right QS item */
488 set_as_qs_item(setting, QUICKSCREEN_RIGHT);
489 break;
490 #endif
491 } /* swicth(do_menu()) */
492 redraw_lists = true;
494 } /* else if (!in_stringlist) */
496 else if (action == ACTION_STD_MENU)
498 if (menu != &root_menu_)
499 ret = GO_TO_ROOT;
500 else
501 ret = GO_TO_PREVIOUS;
502 done = true;
504 else if (action == ACTION_STD_CANCEL)
506 bool exiting_menu = false;
507 in_stringlist = false;
508 /* might be leaving list, so stop scrolling */
509 gui_synclist_scroll_stop(&lists);
510 if (menu_callback)
511 menu_callback(ACTION_EXIT_MENUITEM, menu);
513 if (menu->flags&MENU_EXITAFTERTHISMENU)
514 done = true;
515 else if ((menu->flags&MENU_TYPE_MASK) == MT_MENU)
516 exiting_menu = true;
517 if (stack_top > 0)
519 stack_top--;
520 menu = menu_stack[stack_top];
521 if (!exiting_menu && (menu->flags&MENU_EXITAFTERTHISMENU))
522 done = true;
523 else
524 init_menu_lists(menu, &lists,
525 menu_stack_selected_item[stack_top], false, vps);
526 redraw_lists = true;
527 /* new menu, so reload the callback */
528 get_menu_callback(menu, &menu_callback);
530 else if (menu != &root_menu_)
532 ret = GO_TO_PREVIOUS;
533 done = true;
536 else if (action == ACTION_STD_OK)
538 int type;
539 /* entering an item that may not be a list, so stop scrolling */
540 gui_synclist_scroll_stop(&lists);
541 #ifdef HAVE_BUTTONBAR
542 if (!hide_theme)
544 gui_buttonbar_unset(&buttonbar);
545 gui_buttonbar_draw(&buttonbar);
547 #endif
548 selected = get_menu_selection(gui_synclist_get_sel_pos(&lists), menu);
549 temp = menu->submenus[selected];
550 redraw_lists = true;
551 if (in_stringlist)
552 type = (menu->flags&MENU_TYPE_MASK);
553 else
555 type = (temp->flags&MENU_TYPE_MASK);
556 get_menu_callback(temp, &menu_callback);
557 if (menu_callback)
559 action = menu_callback(ACTION_ENTER_MENUITEM,temp);
560 if (action == ACTION_EXIT_MENUITEM)
561 break;
564 switch (type)
566 case MT_MENU:
567 if (stack_top < MAX_MENUS)
569 menu_stack[stack_top] = menu;
570 menu_stack_selected_item[stack_top] = selected;
571 stack_top++;
572 menu = temp;
573 init_menu_lists(menu, &lists, 0, true, vps);
575 break;
576 case MT_FUNCTION_CALL:
578 int return_value;
579 if (temp->flags&MENU_FUNC_USEPARAM)
580 return_value = temp->function->function_w_param(
581 temp->function->param);
582 else
583 return_value = temp->function->function();
584 if (!(menu->flags&MENU_EXITAFTERTHISMENU) ||
585 (temp->flags&MENU_EXITAFTERTHISMENU))
587 init_menu_lists(menu, &lists, selected, true, vps);
589 if (temp->flags&MENU_FUNC_CHECK_RETVAL)
591 if (return_value != 0)
593 done = true;
594 ret = return_value;
597 break;
599 case MT_SETTING:
600 case MT_SETTING_W_TEXT:
602 do_setting_from_menu(temp, vps);
603 send_event(GUI_EVENT_ACTIONUPDATE, (void*)1); /* force a redraw */
604 break;
606 case MT_RETURN_ID:
607 if (in_stringlist)
609 done = true;
610 ret = selected;
612 else if (stack_top < MAX_MENUS)
614 menu_stack[stack_top] = menu;
615 menu_stack_selected_item[stack_top] = selected;
616 stack_top++;
617 menu = temp;
618 init_menu_lists(menu,&lists,0,false, vps);
619 in_stringlist = true;
621 break;
622 case MT_RETURN_VALUE:
623 ret = temp->value;
624 done = true;
625 break;
627 if (type != MT_MENU)
629 if (menu_callback)
630 menu_callback(ACTION_EXIT_MENUITEM,temp);
632 if (current_submenus_menu != menu)
633 init_menu_lists(menu,&lists,selected,true,vps);
634 /* callback was changed, so reload the menu's callback */
635 get_menu_callback(menu, &menu_callback);
636 if ((menu->flags&MENU_EXITAFTERTHISMENU) &&
637 !(temp->flags&MENU_EXITAFTERTHISMENU))
639 done = true;
640 break;
642 #ifdef HAVE_BUTTONBAR
643 if (!hide_theme)
645 gui_buttonbar_set(&buttonbar, "<<<", "", "");
646 gui_buttonbar_draw(&buttonbar);
648 #endif
650 else
652 switch(default_event_handler(action))
654 case SYS_USB_CONNECTED:
655 ret = MENU_ATTACHED_USB;
656 done = true;
657 break;
658 case SYS_CALL_HUNG_UP:
659 case BUTTON_MULTIMEDIA_PLAYPAUSE:
660 /* remove splash from playlist_resume() */
661 redraw_lists = true;
662 break;
666 if (redraw_lists && !done)
668 if (menu_callback)
669 menu_callback(ACTION_REDRAW, menu);
670 gui_synclist_draw(&lists);
671 gui_synclist_speak_item(&lists);
674 if (start_selected)
676 /* make sure the start_selected variable is set to
677 the selected item from the menu do_menu() was called from */
678 if (stack_top > 0)
680 menu = menu_stack[0];
681 init_menu_lists(menu,&lists,menu_stack_selected_item[0],true, vps);
683 *start_selected = get_menu_selection(
684 gui_synclist_get_sel_pos(&lists), menu);
686 FOR_NB_SCREENS(i)
687 viewportmanager_theme_undo(i, false);
688 return ret;