Player: Save a bit of space by only using 7 bytes/char in the glyph table.
[Rockbox.git] / apps / menu.h
blob365af536e9872cbfd29fde9be0c7ee17e622a113
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 #ifndef __MENU_H__
21 #define __MENU_H__
23 #include <stdbool.h>
24 #include "icon.h"
25 #include "icons.h"
26 #include "root_menu.h" /* needed for MENU_* return codes */
29 enum menu_item_type {
30 MT_MENU = 0,
31 MT_SETTING,
32 MT_SETTING_W_TEXT, /* same as setting, but uses different
33 text for the setting title,
34 ID2P() or "literal" for the str param */
35 MT_FUNCTION_CALL, /* call a function from the menus */
36 MT_RETURN_ID, /* returns the position of the selected item (starting at 0)*/
37 MT_RETURN_VALUE, /* returns a value associated with an item */
38 MT_OLD_MENU, /* used so we can wrap the old menu api
39 around the new api. Noone else should use this */
42 typedef int (*menu_function)(void);
43 struct menu_func {
44 union {
45 int (*function_w_param)(void* param); /* intptr_t instead of void*
46 for 64bit systems */
47 int (*function)(void);
49 void *param; /* passed to function_w_param */
52 #define MENU_TYPE_MASK 0xF /* MT_* type */
53 /* these next two are mutually exclusive */
54 #define MENU_HAS_DESC 0x10
55 #define MENU_DYNAMIC_DESC 0x20
57 /* Flags for MT_FUNCTION_CALL */
58 #define MENU_FUNC_USEPARAM 0x40
59 #define MENU_FUNC_CHECK_RETVAL 0x80
61 #define MENU_COUNT_MASK 0xFFF
62 #define MENU_COUNT_SHIFT 8
63 #define MENU_ITEM_COUNT(c) ((c&MENU_COUNT_MASK)<<MENU_COUNT_SHIFT)
64 #define MENU_GET_COUNT(flags) ((flags>>MENU_COUNT_SHIFT)&MENU_COUNT_MASK)
66 struct menu_item_ex {
67 unsigned int flags; /* above defines */
68 union {
69 const struct menu_item_ex **submenus; /* used with MT_MENU */
70 void *variable; /* used with MT_SETTING,
71 must be in the settings_list.c list */
72 const struct menu_func *function; /* MT_FUNCTION_* */
73 const char **strings; /* used with MT_RETURN_ID */
74 int value; /* MT_RETURN_VALUE */
76 union {
77 /* For settings */
78 int (*menu_callback)(int action, const struct menu_item_ex *this_item);
79 /* For everything else, except if the text is dynamic */
80 const struct menu_callback_with_desc {
81 int (*menu_callback)(int action,
82 const struct menu_item_ex *this_item);
83 unsigned char *desc; /* string or ID */
84 int icon_id; /* from icons_6x8 in icons.h */
85 } *callback_and_desc;
86 /* For when the item text is dynamic */
87 const struct menu_get_name_and_icon {
88 int (*menu_callback)(int action,
89 const struct menu_item_ex *this_item);
90 char *(*list_get_name)(int selected_item, void * data, char *buffer);
91 void *list_get_name_data;
92 int icon_id;
93 } *menu_get_name_and_icon;
97 typedef int (*menu_callback_type)(int action,
98 const struct menu_item_ex *this_item);
99 int do_menu(const struct menu_item_ex *menu, int *start_selected);
100 bool do_setting_from_menu(const struct menu_item_ex *temp);
102 /* In all the following macros the argument names are as follows:
103 - name: The name for the variable (so it can be used in a MAKE_MENU()
104 - str: the string to display for this menu item. use ID2P() for LANG_* id's
105 - callback: The callback function to call for this menu item.
108 /* Use this to put a setting into a menu.
109 The setting must appear in settings_list.c.
110 If the setting is not configured properly, the menu will display "Not Done yet!"
111 When the user selects this item the setting select screen will load,
112 when that screen exits the user wll be back in the menu */
113 #define MENUITEM_SETTING(name,var,callback) \
114 static const struct menu_item_ex name = \
115 {MT_SETTING, {.variable = (void*)var},{callback}};
117 /* Use this for settings which have a differnt title in their
118 setting screen than in the menu (e.g scroll options */
119 #define MENUITEM_SETTING_W_TEXT(name, var, str, callback ) \
120 static const struct menu_callback_with_desc name##__ = {callback,str, Icon_NOICON};\
121 static const struct menu_item_ex name = \
122 {MT_SETTING_W_TEXT|MENU_HAS_DESC, {.variable = (void*)var }, \
123 {.callback_and_desc = & name##__}};
125 /* Use this To create a list of NON-XLATABLE (for the time being) Strings
126 When the user enters this list and selects one, the menu will exits
127 and its return value will be the index of the chosen item */
128 #define MENUITEM_STRINGLIST(name, str, callback, ... ) \
129 static const char *name##_[] = {__VA_ARGS__}; \
130 static const struct menu_callback_with_desc name##__ = {callback,str, Icon_NOICON};\
131 static const struct menu_item_ex name = \
132 {MT_RETURN_ID|MENU_HAS_DESC| \
133 MENU_ITEM_COUNT(sizeof( name##_)/sizeof(*name##_)), \
134 { .strings = name##_},{.callback_and_desc = & name##__}};
137 /* returns a value associated with the item */
138 #define MENUITEM_RETURNVALUE(name, str, val, cb, icon) \
139 static const struct menu_callback_with_desc name##_ = {cb,str,icon}; \
140 static const struct menu_item_ex name = \
141 { MT_RETURN_VALUE|MENU_HAS_DESC, { .value = val}, \
142 {.callback_and_desc = & name##_}};
144 /* same as above, except the item name is dynamic */
145 #define MENUITEM_RETURNVALUE_DYNTEXT(name, val, cb, text_callback, text_cb_data, icon) \
146 static const struct menu_get_name_and_icon name##_ \
147 = {cb,text_callback,text_cb_data,icon}; \
148 static const struct menu_item_ex name = \
149 { MT_RETURN_VALUE|MENU_DYNAMIC_DESC, { .value = val}, \
150 {.menu_get_name_and_icon = & name##_}};
152 /* Use this to put a function call into the menu.
153 When the user selects this item the function will be run,
154 unless MENU_FUNC_IGNORE_RETVAL is set, the return value
155 will be checked, returning 1 will exit do_menu(); */
156 #define MENUITEM_FUNCTION(name, flags, str, func, param, \
157 callback, icon) \
158 static const struct menu_callback_with_desc name##_ = {callback,str,icon}; \
159 static const struct menu_func name##__ = {{(void*)func}, param}; \
160 /* should be const, but recording_settings wont let us do that */ \
161 const struct menu_item_ex name = \
162 { MT_FUNCTION_CALL|MENU_HAS_DESC|flags, \
163 { .function = & name##__}, {.callback_and_desc = & name##_}};
165 /* As above, except the text is dynamic */
166 #define MENUITEM_FUNCTION_DYNTEXT(name, flags, func, param, \
167 text_callback, text_cb_data, callback, icon) \
168 static const struct menu_get_name_and_icon name##_ \
169 = {callback,text_callback,text_cb_data,icon}; \
170 static const struct menu_func name##__ = {{(void*)func}, param}; \
171 static const struct menu_item_ex name = \
172 { MT_FUNCTION_CALL|MENU_DYNAMIC_DESC|flags, \
173 { .function = & name##__}, {.menu_get_name_and_icon = & name##_}};
175 /* Use this to actually create a menu. the ... argument is a list of pointers
176 to any of the above macro'd variables. (It can also have other menus in the list. */
177 #define MAKE_MENU( name, str, callback, icon, ... ) \
178 static const struct menu_item_ex *name##_[] = {__VA_ARGS__}; \
179 static const struct menu_callback_with_desc name##__ = {callback,str,icon};\
180 const struct menu_item_ex name = \
181 {MT_MENU|MENU_HAS_DESC| \
182 MENU_ITEM_COUNT(sizeof( name##_)/sizeof(*name##_)), \
183 { (void*)name##_},{.callback_and_desc = & name##__}};
186 /* OLD API - only use if you really have to.. Ideally this will be dropped */
187 struct menu_item {
188 unsigned char *desc; /* string or ID */
189 bool (*function) (void); /* return true if USB was connected */
192 int menu_init(const struct menu_item* mitems, int count,
193 int (*callback)(int, int),
194 const char *button1, const char *button2, const char *button3);
195 void menu_exit(int menu);
197 /* Returns MENU_* define from root_menu.h, or number of selected menu item*/
198 int menu_show(int m);
200 bool menu_run(int menu);
201 int menu_count(int menu);
203 #endif /* End __MENU_H__ */