annotate libmenu/menu_txt.c @ 26425:3357581cefa5

10l: Rename remaining instances of $i to $lang. patch by Andrew Savchenko, Bircoph list ru
author diego
date Fri, 18 Apr 2008 07:16:22 +0000
parents 96d0992c7920
children f8b6c7045cf8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8197
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
1
16862
931bdbc37ee0 Unify include paths, -I.. is in CFLAGS.
diego
parents: 9380
diff changeset
2 #include "config.h"
17994
6927fabaef92 Part1 of several printf2mp_msg changes in patch from Otvos Attila oattila AT chello DOT hu
reynaldo
parents: 16862
diff changeset
3 #include "mp_msg.h"
6927fabaef92 Part1 of several printf2mp_msg changes in patch from Otvos Attila oattila AT chello DOT hu
reynaldo
parents: 16862
diff changeset
4 #include "help_mp.h"
8197
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
5
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
6 #include <stdlib.h>
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
7 #include <stdio.h>
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
8 #include <string.h>
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
9
19431
ac69ba536915 Explicitly include libmpcodecs/img_format.h and libvo/fastmemcpy.h.
diego
parents: 17994
diff changeset
10 #include "libmpcodecs/img_format.h"
ac69ba536915 Explicitly include libmpcodecs/img_format.h and libvo/fastmemcpy.h.
diego
parents: 17994
diff changeset
11 #include "libmpcodecs/mp_image.h"
8197
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
12
16862
931bdbc37ee0 Unify include paths, -I.. is in CFLAGS.
diego
parents: 9380
diff changeset
13 #include "m_struct.h"
931bdbc37ee0 Unify include paths, -I.. is in CFLAGS.
diego
parents: 9380
diff changeset
14 #include "m_option.h"
8197
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
15 #include "menu.h"
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
16
16862
931bdbc37ee0 Unify include paths, -I.. is in CFLAGS.
diego
parents: 9380
diff changeset
17 #include "libvo/font_load.h"
931bdbc37ee0 Unify include paths, -I.. is in CFLAGS.
diego
parents: 9380
diff changeset
18 #include "osdep/keycodes.h"
8197
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
19
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
20 struct menu_priv_s {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
21 char** lines;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
22 int num_lines;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
23 int cur_line;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
24 int disp_lines;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
25 int minb;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
26 int hspace;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
27 char* file;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
28 };
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
29
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
30 static struct menu_priv_s cfg_dflt = {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
31 NULL,
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
32 0,
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
33 0,
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
34 0,
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
35 0,
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
36 3,
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
37 NULL
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
38 };
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
39
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
40 #define ST_OFF(m) M_ST_OFF(struct menu_priv_s,m)
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
41
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
42 static m_option_t cfg_fields[] = {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
43 { "minbor", ST_OFF(minb), CONF_TYPE_INT, M_OPT_MIN, 0, 0, NULL },
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
44 { "hspace", ST_OFF(hspace), CONF_TYPE_INT, M_OPT_MIN, 0, 0, NULL },
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
45 { "file", ST_OFF(file), CONF_TYPE_STRING, 0, 0, 0, NULL },
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
46 { NULL, NULL, NULL, 0,0,0,NULL }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
47 };
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
48
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
49 #define mpriv (menu->priv)
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
50
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
51 static void read_cmd(menu_t* menu,int cmd) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
52 switch(cmd) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
53 case MENU_CMD_UP:
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
54 mpriv->cur_line -= mpriv->disp_lines / 2;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
55 if(mpriv->cur_line < 0)
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
56 mpriv->cur_line = 0;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
57 break;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
58 case MENU_CMD_DOWN:
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
59 case MENU_CMD_OK:
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
60 mpriv->cur_line += mpriv->disp_lines / 2;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
61 if(mpriv->cur_line >= mpriv->num_lines)
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
62 mpriv->cur_line = mpriv->num_lines - 1;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
63 break;
23367
ee2c8684c925 allow left key command to close text menus too
ben
parents: 23366
diff changeset
64 case MENU_CMD_LEFT:
8197
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
65 case MENU_CMD_CANCEL:
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
66 menu->show = 0;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
67 menu->cl = 1;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
68 break;
25263
96d0992c7920 Remove hardcoded key->cmd bindings in libmenu, support custom key bindings
ulion
parents: 23367
diff changeset
69 case MENU_CMD_HOME:
8197
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
70 mpriv->cur_line = 0;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
71 break;
25263
96d0992c7920 Remove hardcoded key->cmd bindings in libmenu, support custom key bindings
ulion
parents: 23367
diff changeset
72 case MENU_CMD_END:
8197
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
73 mpriv->cur_line = mpriv->num_lines - 1;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
74 break;
25263
96d0992c7920 Remove hardcoded key->cmd bindings in libmenu, support custom key bindings
ulion
parents: 23367
diff changeset
75 case MENU_CMD_PAGE_UP:
8197
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
76 mpriv->cur_line = mpriv->cur_line > mpriv->disp_lines ?
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
77 mpriv->cur_line - mpriv->disp_lines : 0;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
78 break;
25263
96d0992c7920 Remove hardcoded key->cmd bindings in libmenu, support custom key bindings
ulion
parents: 23367
diff changeset
79 case MENU_CMD_PAGE_DOWN:
8197
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
80 mpriv->cur_line = mpriv->cur_line + mpriv->disp_lines > mpriv->num_lines - 1 ? mpriv->num_lines - 1 : mpriv->cur_line + mpriv->disp_lines;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
81 break;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
82 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
83 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
84
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
85
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
86 static void draw(menu_t* menu,mp_image_t* mpi) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
87 int x = mpriv->minb;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
88 int y = mpriv->minb;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
89 //int th = 2*mpriv->hspace + vo_font->height;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
90 int i,end;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
91
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
92 if(x < 0) x = 8;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
93 if(y < 0) y = 8;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
94
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
95 mpriv->disp_lines = (mpi->h + mpriv->hspace - 2*mpriv->minb) / ( vo_font->height + mpriv->hspace);
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
96 if(mpriv->num_lines - mpriv->cur_line < mpriv->disp_lines) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
97 i = mpriv->num_lines - 1 - mpriv->disp_lines;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
98 if(i < 0) i = 0;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
99 end = mpriv->num_lines - 1;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
100 } else {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
101 i = mpriv->cur_line;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
102 end = i + mpriv->disp_lines;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
103 if(end >= mpriv->num_lines) end = mpriv->num_lines - 1;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
104 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
105
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
106 for( ; i < end ; i++) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
107 menu_draw_text(mpi,mpriv->lines[i],x,y);
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
108 y += vo_font->height + mpriv->hspace;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
109 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
110
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
111 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
112
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
113 #define BUF_SIZE 1024
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
114
23366
b344b6520518 rename some menu open functions, to avoid confusion with libc native open()
ben
parents: 19431
diff changeset
115 static int open_txt(menu_t* menu, char* args) {
8197
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
116 FILE* fd;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
117 char buf[BUF_SIZE];
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
118 char *l;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
119 int s;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
120 int pos = 0, r = 0;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
121 args = NULL; // Warning kill
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
122
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
123 menu->draw = draw;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
124 menu->read_cmd = read_cmd;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
125
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
126 if(!mpriv->file) {
17994
6927fabaef92 Part1 of several printf2mp_msg changes in patch from Otvos Attila oattila AT chello DOT hu
reynaldo
parents: 16862
diff changeset
127 mp_msg(MSGT_GLOBAL,MSGL_WARN,MSGTR_LIBMENU_MenuTxtNeedATxtFileName);
8197
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
128 return 0;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
129 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
130
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
131 fd = fopen(mpriv->file,"r");
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
132 if(!fd) {
17994
6927fabaef92 Part1 of several printf2mp_msg changes in patch from Otvos Attila oattila AT chello DOT hu
reynaldo
parents: 16862
diff changeset
133 mp_msg(MSGT_GLOBAL,MSGL_WARN,MSGTR_LIBMENU_MenuTxtCantOpen,mpriv->file);
8197
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
134 return 0;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
135 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
136
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
137 while(1) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
138 r = fread(buf+pos,1,BUF_SIZE-pos-1,fd);
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
139 if(r <= 0) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
140 if(pos > 0) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
141 mpriv->lines = realloc(mpriv->lines,(mpriv->num_lines + 1)*sizeof(char*));
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
142 mpriv->lines[mpriv->num_lines] = strdup(buf);
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
143 mpriv->num_lines++;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
144 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
145 fclose(fd);
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
146 break;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
147 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
148 pos += r;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
149 buf[pos] = '\0';
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
150
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
151 while((l = strchr(buf,'\n')) != NULL) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
152 s = l-buf;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
153 mpriv->lines = realloc(mpriv->lines,(mpriv->num_lines + 1)*sizeof(char*));
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
154 mpriv->lines[mpriv->num_lines] = malloc(s+1);
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
155 memcpy(mpriv->lines[mpriv->num_lines],buf,s);
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
156 mpriv->lines[mpriv->num_lines][s] = '\0';
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
157 pos -= s + 1;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
158 if(pos > 0)
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
159 memmove(buf,l+1,pos);
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
160 buf[pos] = '\0';
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
161 mpriv->num_lines++;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
162 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
163 if(pos >= BUF_SIZE-1) {
17994
6927fabaef92 Part1 of several printf2mp_msg changes in patch from Otvos Attila oattila AT chello DOT hu
reynaldo
parents: 16862
diff changeset
164 mp_msg(MSGT_GLOBAL,MSGL_WARN,MSGTR_LIBMENU_WarningTooLongLineSplitting);
8197
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
165 mpriv->lines = realloc(mpriv->lines,(mpriv->num_lines + 1)*sizeof(char*));
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
166 mpriv->lines[mpriv->num_lines] = strdup(buf);
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
167 mpriv->num_lines++;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
168 pos = 0;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
169 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
170 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
171
17994
6927fabaef92 Part1 of several printf2mp_msg changes in patch from Otvos Attila oattila AT chello DOT hu
reynaldo
parents: 16862
diff changeset
172 mp_msg(MSGT_GLOBAL,MSGL_INFO,MSGTR_LIBMENU_ParsedLines,mpriv->num_lines);
8197
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
173
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
174 return 1;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
175 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
176
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
177 const menu_info_t menu_info_txt = {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
178 "Text file viewer",
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
179 "txt",
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
180 "Albeu",
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
181 "",
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
182 {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
183 "txt_cfg",
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
184 sizeof(struct menu_priv_s),
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
185 &cfg_dflt,
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
186 cfg_fields
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
187 },
23366
b344b6520518 rename some menu open functions, to avoid confusion with libc native open()
ben
parents: 19431
diff changeset
188 open_txt,
8197
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
189 };