Revert r24446 since it breaks mingw32 build: _WINGDI_H is defined in wingdi.h
[mplayer.git] / libmenu / menu_cmdlist.c
blobb3e3879de15b70ebb156cec86a187a035a45824d
2 #include "config.h"
3 #include "mp_msg.h"
4 #include "help_mp.h"
6 #include <stdlib.h>
7 #include <stdio.h>
8 #include <ctype.h>
9 #include <string.h>
11 #include "libmpcodecs/img_format.h"
12 #include "libmpcodecs/mp_image.h"
14 #include "m_option.h"
15 #include "m_struct.h"
16 #include "asxparser.h"
17 #include "menu.h"
18 #include "menu_list.h"
20 #include "libvo/font_load.h"
22 #include "input/input.h"
23 #include "version.h"
27 struct list_entry_s {
28 struct list_entry p;
30 char* ok;
31 char* cancel;
32 char* left;
33 char* right;
36 struct menu_priv_s {
37 menu_list_priv_t p;
38 int auto_close;
41 #define ST_OFF(m) M_ST_OFF(struct menu_priv_s, m)
43 static struct menu_priv_s cfg_dflt = {
44 MENU_LIST_PRIV_DFLT,
48 static m_option_t cfg_fields[] = {
49 MENU_LIST_PRIV_FIELDS,
50 { "title",M_ST_OFF(struct menu_priv_s,p.title), CONF_TYPE_STRING, 0, 0, 0, NULL },
51 { "auto-close", ST_OFF(auto_close), CONF_TYPE_FLAG, 0, 0, 1, NULL },
52 { NULL, NULL, NULL, 0,0,0,NULL }
55 #define mpriv (menu->priv)
57 static void read_cmd(menu_t* menu,int cmd) {
58 switch(cmd) {
59 case MENU_CMD_RIGHT:
60 if(mpriv->p.current->right) {
61 mp_cmd_t* c = mp_input_parse_cmd(mpriv->p.current->right);
62 if(c) mp_input_queue_cmd(c);
63 break;
64 } // fallback on ok if right is not defined
65 case MENU_CMD_OK: {
66 if(mpriv->p.current->ok) {
67 mp_cmd_t* c = mp_input_parse_cmd(mpriv->p.current->ok);
68 if(c)
70 if (mpriv->auto_close)
71 mp_input_queue_cmd (mp_input_parse_cmd ("menu hide"));
72 mp_input_queue_cmd(c);
75 } break;
76 case MENU_CMD_LEFT:
77 if(mpriv->p.current->left) {
78 mp_cmd_t* c = mp_input_parse_cmd(mpriv->p.current->left);
79 if(c) mp_input_queue_cmd(c);
80 break;
81 } // fallback on cancel if left is not defined
82 case MENU_CMD_CANCEL:
83 if(mpriv->p.current->cancel) {
84 mp_cmd_t* c = mp_input_parse_cmd(mpriv->p.current->cancel);
85 if(c)
86 mp_input_queue_cmd(c);
87 break;
89 default:
90 menu_list_read_cmd(menu,cmd);
94 static void read_key(menu_t* menu,int c){
95 menu_list_read_key(menu,c,0);
98 static void free_entry(list_entry_t* entry) {
99 if(entry->ok)
100 free(entry->ok);
101 if(entry->cancel)
102 free(entry->cancel);
103 free(entry->p.txt);
104 free(entry);
107 static void close_menu(menu_t* menu) {
108 menu_list_uninit(menu,free_entry);
111 static int parse_args(menu_t* menu,char* args) {
112 char *element,*body, **attribs, *name;
113 list_entry_t* m = NULL;
114 int r;
115 ASX_Parser_t* parser = asx_parser_new();
117 while(1) {
118 r = asx_get_element(parser,&args,&element,&body,&attribs);
119 if(r < 0) {
120 mp_msg(MSGT_GLOBAL,MSGL_WARN,MSGTR_LIBMENU_SyntaxErrorAtLine,parser->line);
121 asx_parser_free(parser);
122 return -1;
123 } else if(r == 0) {
124 asx_parser_free(parser);
125 if(!m)
126 mp_msg(MSGT_GLOBAL,MSGL_WARN,MSGTR_LIBMENU_NoEntryFoundInTheMenuDefinition);
127 return m ? 1 : 0;
129 // Has it a name ?
130 name = asx_get_attrib("name",attribs);
131 if(!name) {
132 mp_msg(MSGT_GLOBAL,MSGL_WARN,MSGTR_LIBMENU_ListMenuEntryDefinitionsNeedAName,parser->line);
133 free(element);
134 if(body) free(body);
135 asx_free_attribs(attribs);
136 continue;
138 m = calloc(1,sizeof(struct list_entry_s));
139 m->p.txt = name;
140 m->ok = asx_get_attrib("ok",attribs);
141 m->cancel = asx_get_attrib("cancel",attribs);
142 m->left = asx_get_attrib("left",attribs);
143 m->right = asx_get_attrib("right",attribs);
144 menu_list_add_entry(menu,m);
146 free(element);
147 if(body) free(body);
148 asx_free_attribs(attribs);
152 static int open_cmdlist(menu_t* menu, char* args) {
153 menu->draw = menu_list_draw;
154 menu->read_cmd = read_cmd;
155 menu->read_key = read_key;
156 menu->close = close_menu;
158 if(!args) {
159 mp_msg(MSGT_GLOBAL,MSGL_WARN,MSGTR_LIBMENU_ListMenuNeedsAnArgument);
160 return 0;
163 menu_list_init(menu);
164 if(!parse_args(menu,args))
165 return 0;
166 return 1;
169 const menu_info_t menu_info_cmdlist = {
170 "Command list menu",
171 "cmdlist",
172 "Albeu",
175 "cmdlist_cfg",
176 sizeof(struct menu_priv_s),
177 &cfg_dflt,
178 cfg_fields
180 open_cmdlist