Mercurial > mplayer.hg
annotate libmenu/menu.c @ 23101:253a6da2be20
Add general variables for either static or shared FFmpeg libraries.
author | diego |
---|---|
date | Thu, 26 Apr 2007 11:22:22 +0000 |
parents | 83366c8e1928 |
children | a142b048c65e |
rev | line source |
---|---|
8197 | 1 |
16862 | 2 #include "config.h" |
17994
6927fabaef92
Part1 of several printf2mp_msg changes in patch from Otvos Attila oattila AT chello DOT hu
reynaldo
parents:
17993
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:
17993
diff
changeset
|
4 #include "help_mp.h" |
8197 | 5 |
6 #include <stdlib.h> | |
7 #include <stdio.h> | |
8 #include <string.h> | |
8604
41a1e5dbb552
This patch fixes the reading of the menu.conf, because stream_open()
arpi
parents:
8251
diff
changeset
|
9 #include <fcntl.h> |
41a1e5dbb552
This patch fixes the reading of the menu.conf, because stream_open()
arpi
parents:
8251
diff
changeset
|
10 #include <unistd.h> |
8197 | 11 |
16862 | 12 #include "libvo/osd.h" |
13 #include "libvo/font_load.h" | |
14 #include "osdep/keycodes.h" | |
15 #include "asxparser.h" | |
19271
64d82a45a05d
introduce new 'stream' directory for all stream layer related components and split them from libmpdemux
ben
parents:
18194
diff
changeset
|
16 #include "stream/stream.h" |
8197 | 17 |
19431
ac69ba536915
Explicitly include libmpcodecs/img_format.h and libvo/fastmemcpy.h.
diego
parents:
19271
diff
changeset
|
18 #include "libmpcodecs/img_format.h" |
ac69ba536915
Explicitly include libmpcodecs/img_format.h and libvo/fastmemcpy.h.
diego
parents:
19271
diff
changeset
|
19 #include "libmpcodecs/mp_image.h" |
16862 | 20 #include "m_option.h" |
21 #include "m_struct.h" | |
8197 | 22 #include "menu.h" |
23 | |
24 extern menu_info_t menu_info_cmdlist; | |
25 extern menu_info_t menu_info_pt; | |
26 extern menu_info_t menu_info_filesel; | |
27 extern menu_info_t menu_info_txt; | |
28 extern menu_info_t menu_info_console; | |
29 extern menu_info_t menu_info_pref; | |
10626
fd97f3727f15
Finnaly commit Nico's dvb menu. Sorry for committing this
albeu
parents:
10397
diff
changeset
|
30 #ifdef HAS_DVBIN_SUPPORT |
fd97f3727f15
Finnaly commit Nico's dvb menu. Sorry for committing this
albeu
parents:
10397
diff
changeset
|
31 extern menu_info_t menu_info_dvbsel; |
fd97f3727f15
Finnaly commit Nico's dvb menu. Sorry for committing this
albeu
parents:
10397
diff
changeset
|
32 #endif |
fd97f3727f15
Finnaly commit Nico's dvb menu. Sorry for committing this
albeu
parents:
10397
diff
changeset
|
33 |
8197 | 34 |
35 menu_info_t* menu_info_list[] = { | |
36 &menu_info_pt, | |
37 &menu_info_cmdlist, | |
38 &menu_info_filesel, | |
39 &menu_info_txt, | |
40 &menu_info_console, | |
10626
fd97f3727f15
Finnaly commit Nico's dvb menu. Sorry for committing this
albeu
parents:
10397
diff
changeset
|
41 #ifdef HAS_DVBIN_SUPPORT |
fd97f3727f15
Finnaly commit Nico's dvb menu. Sorry for committing this
albeu
parents:
10397
diff
changeset
|
42 &menu_info_dvbsel, |
fd97f3727f15
Finnaly commit Nico's dvb menu. Sorry for committing this
albeu
parents:
10397
diff
changeset
|
43 #endif |
8197 | 44 &menu_info_pref, |
45 NULL | |
46 }; | |
47 | |
48 typedef struct menu_def_st { | |
49 char* name; | |
50 menu_info_t* type; | |
51 void* cfg; | |
52 char* args; | |
53 } menu_def_t; | |
54 | |
22284 | 55 static struct MPContext *menu_ctx = NULL; |
8197 | 56 static menu_def_t* menu_list = NULL; |
21797
14061bc22cb3
fix gprof support (aka work around gprof's brain dead design)
gpoirier
parents:
20507
diff
changeset
|
57 static int menu_count = 0; |
8197 | 58 |
59 | |
60 static int menu_parse_config(char* buffer) { | |
61 char *element,*body, **attribs, *name; | |
62 menu_info_t* minfo = NULL; | |
63 int r,i; | |
64 ASX_Parser_t* parser = asx_parser_new(); | |
65 | |
66 while(1) { | |
67 r = asx_get_element(parser,&buffer,&element,&body,&attribs); | |
68 if(r < 0) { | |
17994
6927fabaef92
Part1 of several printf2mp_msg changes in patch from Otvos Attila oattila AT chello DOT hu
reynaldo
parents:
17993
diff
changeset
|
69 mp_msg(MSGT_GLOBAL,MSGL_WARN,MSGTR_LIBMENU_SyntaxErrorAtLine,parser->line); |
8197 | 70 asx_parser_free(parser); |
71 return 0; | |
72 } else if(r == 0) { | |
73 asx_parser_free(parser); | |
74 return 1; | |
75 } | |
76 // Has it a name ? | |
77 name = asx_get_attrib("name",attribs); | |
78 if(!name) { | |
17994
6927fabaef92
Part1 of several printf2mp_msg changes in patch from Otvos Attila oattila AT chello DOT hu
reynaldo
parents:
17993
diff
changeset
|
79 mp_msg(MSGT_GLOBAL,MSGL_WARN,MSGTR_LIBMENU_MenuDefinitionsNeedANameAttrib,parser->line); |
8197 | 80 free(element); |
81 if(body) free(body); | |
82 asx_free_attribs(attribs); | |
83 continue; | |
84 } | |
85 | |
86 // Try to find this menu type in our list | |
87 for(i = 0, minfo = NULL ; menu_info_list[i] ; i++) { | |
88 if(strcasecmp(element,menu_info_list[i]->name) == 0) { | |
89 minfo = menu_info_list[i]; | |
90 break; | |
91 } | |
92 } | |
93 // Got it : add this to our list | |
94 if(minfo) { | |
21797
14061bc22cb3
fix gprof support (aka work around gprof's brain dead design)
gpoirier
parents:
20507
diff
changeset
|
95 menu_list = realloc(menu_list,(menu_count+2)*sizeof(menu_def_t)); |
14061bc22cb3
fix gprof support (aka work around gprof's brain dead design)
gpoirier
parents:
20507
diff
changeset
|
96 menu_list[menu_count].name = name; |
14061bc22cb3
fix gprof support (aka work around gprof's brain dead design)
gpoirier
parents:
20507
diff
changeset
|
97 menu_list[menu_count].type = minfo; |
14061bc22cb3
fix gprof support (aka work around gprof's brain dead design)
gpoirier
parents:
20507
diff
changeset
|
98 menu_list[menu_count].cfg = m_struct_alloc(&minfo->priv_st); |
14061bc22cb3
fix gprof support (aka work around gprof's brain dead design)
gpoirier
parents:
20507
diff
changeset
|
99 menu_list[menu_count].args = body; |
8197 | 100 // Setup the attribs |
101 for(i = 0 ; attribs[2*i] ; i++) { | |
102 if(strcasecmp(attribs[2*i],"name") == 0) continue; | |
21797
14061bc22cb3
fix gprof support (aka work around gprof's brain dead design)
gpoirier
parents:
20507
diff
changeset
|
103 if(!m_struct_set(&minfo->priv_st,menu_list[menu_count].cfg,attribs[2*i], attribs[2*i+1])) |
17994
6927fabaef92
Part1 of several printf2mp_msg changes in patch from Otvos Attila oattila AT chello DOT hu
reynaldo
parents:
17993
diff
changeset
|
104 mp_msg(MSGT_GLOBAL,MSGL_WARN,MSGTR_LIBMENU_BadAttrib,attribs[2*i],attribs[2*i+1], |
8197 | 105 name,parser->line); |
106 } | |
21797
14061bc22cb3
fix gprof support (aka work around gprof's brain dead design)
gpoirier
parents:
20507
diff
changeset
|
107 menu_count++; |
14061bc22cb3
fix gprof support (aka work around gprof's brain dead design)
gpoirier
parents:
20507
diff
changeset
|
108 memset(&menu_list[menu_count],0,sizeof(menu_def_t)); |
8197 | 109 } else { |
17994
6927fabaef92
Part1 of several printf2mp_msg changes in patch from Otvos Attila oattila AT chello DOT hu
reynaldo
parents:
17993
diff
changeset
|
110 mp_msg(MSGT_GLOBAL,MSGL_WARN,MSGTR_LIBMENU_UnknownMenuType,element,parser->line); |
8197 | 111 free(name); |
112 if(body) free(body); | |
113 } | |
114 | |
115 free(element); | |
116 asx_free_attribs(attribs); | |
117 } | |
118 | |
119 } | |
120 | |
121 | |
122 /// This will build the menu_defs list from the cfg file | |
123 #define BUF_STEP 1024 | |
124 #define BUF_MIN 128 | |
125 #define BUF_MAX BUF_STEP*1024 | |
22284 | 126 int menu_init(struct MPContext *mpctx, char* cfg_file) { |
8197 | 127 char* buffer = NULL; |
128 int bl = BUF_STEP, br = 0; | |
9103
6c2c74adaebe
mplayer crashes if one tries to use osd menu without having a font
arpi
parents:
8604
diff
changeset
|
129 int f, fd; |
9212
24b102dbd0fe
Fixes a problem where the menu won't work, if you just use freetype fonts
arpi
parents:
9103
diff
changeset
|
130 #ifndef HAVE_FREETYPE |
9103
6c2c74adaebe
mplayer crashes if one tries to use osd menu without having a font
arpi
parents:
8604
diff
changeset
|
131 if(vo_font == NULL) |
6c2c74adaebe
mplayer crashes if one tries to use osd menu without having a font
arpi
parents:
8604
diff
changeset
|
132 return 0; |
9212
24b102dbd0fe
Fixes a problem where the menu won't work, if you just use freetype fonts
arpi
parents:
9103
diff
changeset
|
133 #endif |
9103
6c2c74adaebe
mplayer crashes if one tries to use osd menu without having a font
arpi
parents:
8604
diff
changeset
|
134 fd = open(cfg_file, O_RDONLY); |
8604
41a1e5dbb552
This patch fixes the reading of the menu.conf, because stream_open()
arpi
parents:
8251
diff
changeset
|
135 if(fd < 0) { |
17994
6927fabaef92
Part1 of several printf2mp_msg changes in patch from Otvos Attila oattila AT chello DOT hu
reynaldo
parents:
17993
diff
changeset
|
136 mp_msg(MSGT_GLOBAL,MSGL_WARN,MSGTR_LIBMENU_CantOpenConfigFile,cfg_file); |
8197 | 137 return 0; |
138 } | |
139 buffer = malloc(bl); | |
140 while(1) { | |
141 int r; | |
142 if(bl - br < BUF_MIN) { | |
143 if(bl >= BUF_MAX) { | |
17994
6927fabaef92
Part1 of several printf2mp_msg changes in patch from Otvos Attila oattila AT chello DOT hu
reynaldo
parents:
17993
diff
changeset
|
144 mp_msg(MSGT_GLOBAL,MSGL_WARN,MSGTR_LIBMENU_ConfigFileIsTooBig,BUF_MAX/1024); |
8604
41a1e5dbb552
This patch fixes the reading of the menu.conf, because stream_open()
arpi
parents:
8251
diff
changeset
|
145 close(fd); |
8197 | 146 free(buffer); |
147 return 0; | |
148 } | |
149 bl += BUF_STEP; | |
150 buffer = realloc(buffer,bl); | |
151 } | |
8604
41a1e5dbb552
This patch fixes the reading of the menu.conf, because stream_open()
arpi
parents:
8251
diff
changeset
|
152 r = read(fd,buffer+br,bl-br); |
8197 | 153 if(r == 0) break; |
154 br += r; | |
155 } | |
156 if(!br) { | |
17994
6927fabaef92
Part1 of several printf2mp_msg changes in patch from Otvos Attila oattila AT chello DOT hu
reynaldo
parents:
17993
diff
changeset
|
157 mp_msg(MSGT_GLOBAL,MSGL_WARN,MSGTR_LIBMENU_ConfigFileIsEmpty); |
8197 | 158 return 0; |
159 } | |
160 buffer[br-1] = '\0'; | |
161 | |
8604
41a1e5dbb552
This patch fixes the reading of the menu.conf, because stream_open()
arpi
parents:
8251
diff
changeset
|
162 close(fd); |
41a1e5dbb552
This patch fixes the reading of the menu.conf, because stream_open()
arpi
parents:
8251
diff
changeset
|
163 |
22284 | 164 menu_ctx = mpctx; |
8197 | 165 f = menu_parse_config(buffer); |
166 free(buffer); | |
167 return f; | |
168 } | |
169 | |
170 // Destroy all this stuff | |
171 void menu_unint(void) { | |
172 int i; | |
173 for(i = 0 ; menu_list && menu_list[i].name ; i++) { | |
174 free(menu_list[i].name); | |
175 m_struct_free(&menu_list[i].type->priv_st,menu_list[i].cfg); | |
176 if(menu_list[i].args) free(menu_list[i].args); | |
177 } | |
178 free(menu_list); | |
21797
14061bc22cb3
fix gprof support (aka work around gprof's brain dead design)
gpoirier
parents:
20507
diff
changeset
|
179 menu_count = 0; |
8197 | 180 } |
181 | |
182 /// Default read_key function | |
183 void menu_dflt_read_key(menu_t* menu,int cmd) { | |
184 switch(cmd) { | |
185 case KEY_UP: | |
186 menu->read_cmd(menu,MENU_CMD_UP); | |
187 break; | |
188 case KEY_DOWN: | |
189 menu->read_cmd(menu,MENU_CMD_DOWN); | |
190 break; | |
191 case KEY_LEFT: | |
17945
98f4e3704a76
Allow 6 ways (up/down/left/right/ok/cancel) navigation.
albeu
parents:
16862
diff
changeset
|
192 menu->read_cmd(menu,MENU_CMD_LEFT); |
98f4e3704a76
Allow 6 ways (up/down/left/right/ok/cancel) navigation.
albeu
parents:
16862
diff
changeset
|
193 break; |
8197 | 194 case KEY_ESC: |
195 menu->read_cmd(menu,MENU_CMD_CANCEL); | |
196 break; | |
197 case KEY_RIGHT: | |
17945
98f4e3704a76
Allow 6 ways (up/down/left/right/ok/cancel) navigation.
albeu
parents:
16862
diff
changeset
|
198 menu->read_cmd(menu,MENU_CMD_RIGHT); |
98f4e3704a76
Allow 6 ways (up/down/left/right/ok/cancel) navigation.
albeu
parents:
16862
diff
changeset
|
199 break; |
8197 | 200 case KEY_ENTER: |
201 menu->read_cmd(menu,MENU_CMD_OK); | |
202 break; | |
203 } | |
204 } | |
205 | |
206 menu_t* menu_open(char *name) { | |
207 menu_t* m; | |
208 int i; | |
209 | |
210 for(i = 0 ; menu_list[i].name != NULL ; i++) { | |
211 if(strcmp(name,menu_list[i].name) == 0) | |
212 break; | |
213 } | |
214 if(menu_list[i].name == NULL) { | |
17994
6927fabaef92
Part1 of several printf2mp_msg changes in patch from Otvos Attila oattila AT chello DOT hu
reynaldo
parents:
17993
diff
changeset
|
215 mp_msg(MSGT_GLOBAL,MSGL_WARN,MSGTR_LIBMENU_MenuNotFound,name); |
8197 | 216 return NULL; |
217 } | |
218 m = calloc(1,sizeof(menu_t)); | |
219 m->priv_st = &(menu_list[i].type->priv_st); | |
220 m->priv = m_struct_copy(m->priv_st,menu_list[i].cfg); | |
22284 | 221 m->ctx = menu_ctx; |
8197 | 222 if(menu_list[i].type->open(m,menu_list[i].args)) |
223 return m; | |
224 if(m->priv) | |
225 m_struct_free(m->priv_st,m->priv); | |
226 free(m); | |
17994
6927fabaef92
Part1 of several printf2mp_msg changes in patch from Otvos Attila oattila AT chello DOT hu
reynaldo
parents:
17993
diff
changeset
|
227 mp_msg(MSGT_GLOBAL,MSGL_WARN,MSGTR_LIBMENU_MenuInitFailed,name); |
8197 | 228 return NULL; |
229 } | |
230 | |
231 void menu_draw(menu_t* menu,mp_image_t* mpi) { | |
232 if(menu->show && menu->draw) | |
233 menu->draw(menu,mpi); | |
234 } | |
235 | |
236 void menu_read_cmd(menu_t* menu,int cmd) { | |
237 if(menu->read_cmd) | |
238 menu->read_cmd(menu,cmd); | |
239 } | |
240 | |
241 void menu_close(menu_t* menu) { | |
242 if(menu->close) | |
243 menu->close(menu); | |
244 if(menu->priv) | |
245 m_struct_free(menu->priv_st,menu->priv); | |
246 free(menu); | |
247 } | |
248 | |
249 void menu_read_key(menu_t* menu,int cmd) { | |
250 if(menu->read_key) | |
251 menu->read_key(menu,cmd); | |
252 else | |
253 menu_dflt_read_key(menu,cmd); | |
254 } | |
255 | |
256 ///////////////////////////// Helpers //////////////////////////////////// | |
257 | |
258 typedef void (*draw_alpha_f)(int w,int h, unsigned char* src, unsigned char *srca, int srcstride, unsigned char* dstbase,int dststride); | |
259 | |
260 inline static draw_alpha_f get_draw_alpha(uint32_t fmt) { | |
261 switch(fmt) { | |
262 case IMGFMT_BGR15: | |
263 case IMGFMT_RGB15: | |
264 return vo_draw_alpha_rgb15; | |
265 case IMGFMT_BGR16: | |
266 case IMGFMT_RGB16: | |
267 return vo_draw_alpha_rgb16; | |
268 case IMGFMT_BGR24: | |
269 case IMGFMT_RGB24: | |
270 return vo_draw_alpha_rgb24; | |
271 case IMGFMT_BGR32: | |
272 case IMGFMT_RGB32: | |
273 return vo_draw_alpha_rgb32; | |
274 case IMGFMT_YV12: | |
275 case IMGFMT_I420: | |
276 case IMGFMT_IYUV: | |
277 case IMGFMT_YVU9: | |
278 case IMGFMT_IF09: | |
279 case IMGFMT_Y800: | |
280 case IMGFMT_Y8: | |
281 return vo_draw_alpha_yv12; | |
282 case IMGFMT_YUY2: | |
283 return vo_draw_alpha_yuy2; | |
18194 | 284 case IMGFMT_UYVY: |
285 return vo_draw_alpha_uyvy; | |
8197 | 286 } |
287 | |
288 return NULL; | |
289 } | |
290 | |
8224
fefc56153615
Fix freetype. Freetype is highly recommended for a nice output ;)
albeu
parents:
8197
diff
changeset
|
291 // return the real height of a char: |
fefc56153615
Fix freetype. Freetype is highly recommended for a nice output ;)
albeu
parents:
8197
diff
changeset
|
292 static inline int get_height(int c,int h){ |
fefc56153615
Fix freetype. Freetype is highly recommended for a nice output ;)
albeu
parents:
8197
diff
changeset
|
293 int font; |
fefc56153615
Fix freetype. Freetype is highly recommended for a nice output ;)
albeu
parents:
8197
diff
changeset
|
294 if ((font=vo_font->font[c])>=0) |
fefc56153615
Fix freetype. Freetype is highly recommended for a nice output ;)
albeu
parents:
8197
diff
changeset
|
295 if(h<vo_font->pic_a[font]->h) h=vo_font->pic_a[font]->h; |
fefc56153615
Fix freetype. Freetype is highly recommended for a nice output ;)
albeu
parents:
8197
diff
changeset
|
296 return h; |
fefc56153615
Fix freetype. Freetype is highly recommended for a nice output ;)
albeu
parents:
8197
diff
changeset
|
297 } |
fefc56153615
Fix freetype. Freetype is highly recommended for a nice output ;)
albeu
parents:
8197
diff
changeset
|
298 |
fefc56153615
Fix freetype. Freetype is highly recommended for a nice output ;)
albeu
parents:
8197
diff
changeset
|
299 #ifdef HAVE_FREETYPE |
20507
d03b71ab8b65
Fix possible crash: negative values may no be passed to render_one_glyph
reimar
parents:
19431
diff
changeset
|
300 #define render_txt(t) { unsigned char* p = t; while(*p) render_one_glyph(vo_font,*p++); } |
8224
fefc56153615
Fix freetype. Freetype is highly recommended for a nice output ;)
albeu
parents:
8197
diff
changeset
|
301 #else |
fefc56153615
Fix freetype. Freetype is highly recommended for a nice output ;)
albeu
parents:
8197
diff
changeset
|
302 #define render_txt(t) |
fefc56153615
Fix freetype. Freetype is highly recommended for a nice output ;)
albeu
parents:
8197
diff
changeset
|
303 #endif |
fefc56153615
Fix freetype. Freetype is highly recommended for a nice output ;)
albeu
parents:
8197
diff
changeset
|
304 |
8197 | 305 |
306 void menu_draw_text(mp_image_t* mpi,char* txt, int x, int y) { | |
307 draw_alpha_f draw_alpha = get_draw_alpha(mpi->imgfmt); | |
308 int font; | |
309 | |
310 if(!draw_alpha) { | |
17994
6927fabaef92
Part1 of several printf2mp_msg changes in patch from Otvos Attila oattila AT chello DOT hu
reynaldo
parents:
17993
diff
changeset
|
311 mp_msg(MSGT_GLOBAL,MSGL_WARN,MSGTR_LIBMENU_UnsupportedOutformat); |
8197 | 312 return; |
313 } | |
314 | |
8224
fefc56153615
Fix freetype. Freetype is highly recommended for a nice output ;)
albeu
parents:
8197
diff
changeset
|
315 render_txt(txt); |
fefc56153615
Fix freetype. Freetype is highly recommended for a nice output ;)
albeu
parents:
8197
diff
changeset
|
316 |
8197 | 317 while (*txt) { |
318 unsigned char c=*txt++; | |
319 if ((font=vo_font->font[c])>=0 && (x + vo_font->width[c] <= mpi->w) && (y + vo_font->pic_a[font]->h <= mpi->h)) | |
320 draw_alpha(vo_font->width[c], vo_font->pic_a[font]->h, | |
321 vo_font->pic_b[font]->bmp+vo_font->start[c], | |
322 vo_font->pic_a[font]->bmp+vo_font->start[c], | |
323 vo_font->pic_a[font]->w, | |
324 mpi->planes[0] + y * mpi->stride[0] + x * (mpi->bpp>>3), | |
325 mpi->stride[0]); | |
326 x+=vo_font->width[c]+vo_font->charspace; | |
327 } | |
328 | |
329 } | |
330 | |
331 void menu_draw_text_full(mp_image_t* mpi,char* txt, | |
332 int x, int y,int w, int h, | |
333 int vspace, int warp, int align, int anchor) { | |
334 int need_w,need_h; | |
335 int sy, ymin, ymax; | |
336 int sx, xmin, xmax, xmid, xrmin; | |
337 int ll = 0; | |
338 int font; | |
339 draw_alpha_f draw_alpha = get_draw_alpha(mpi->imgfmt); | |
340 | |
341 if(!draw_alpha) { | |
17994
6927fabaef92
Part1 of several printf2mp_msg changes in patch from Otvos Attila oattila AT chello DOT hu
reynaldo
parents:
17993
diff
changeset
|
342 mp_msg(MSGT_GLOBAL,MSGL_WARN,MSGTR_LIBMENU_UnsupportedOutformat); |
8197 | 343 return; |
344 } | |
345 | |
8224
fefc56153615
Fix freetype. Freetype is highly recommended for a nice output ;)
albeu
parents:
8197
diff
changeset
|
346 render_txt(txt); |
fefc56153615
Fix freetype. Freetype is highly recommended for a nice output ;)
albeu
parents:
8197
diff
changeset
|
347 |
8197 | 348 if(x > mpi->w || y > mpi->h) |
349 return; | |
350 | |
351 if(anchor & MENU_TEXT_VCENTER) { | |
352 if(h <= 0) h = mpi->h; | |
353 ymin = y - h/2; | |
354 ymax = y + h/2; | |
355 } else if(anchor & MENU_TEXT_BOT) { | |
356 if(h <= 0) h = mpi->h - y; | |
357 ymin = y - h; | |
358 ymax = y; | |
359 } else { | |
360 if(h <= 0) h = mpi->h - y; | |
361 ymin = y; | |
362 ymax = y + h; | |
363 } | |
364 | |
365 if(anchor & MENU_TEXT_HCENTER) { | |
366 if(w <= 0) w = mpi->w; | |
367 xmin = x - w/2; | |
368 xmax = x + w/2; | |
369 } else if(anchor & MENU_TEXT_RIGHT) { | |
370 if(w <= 0) w = mpi->w -x; | |
371 xmin = x - w; | |
372 xmax = x; | |
373 } else { | |
374 if(w <= 0) w = mpi->w -x; | |
375 xmin = x; | |
376 xmax = x + w; | |
377 } | |
378 | |
379 // How many space do we need to draw this ? | |
380 menu_text_size(txt,w,vspace,warp,&need_w,&need_h); | |
381 | |
382 // Find the first line | |
383 if(align & MENU_TEXT_VCENTER) | |
384 sy = ymin + ((h - need_h)/2); | |
385 else if(align & MENU_TEXT_BOT) | |
8232 | 386 sy = ymax - need_h - 1; |
8197 | 387 else |
388 sy = y; | |
389 | |
390 #if 0 | |
391 // Find the first col | |
392 if(align & MENU_TEXT_HCENTER) | |
393 sx = xmin + ((w - need_w)/2); | |
394 else if(align & MENU_TEXT_RIGHT) | |
395 sx = xmax - need_w; | |
396 #endif | |
397 | |
398 xmid = xmin + (xmax - xmin) / 2; | |
399 xrmin = xmin; | |
400 // Clamp the bb to the mpi size | |
401 if(ymin < 0) ymin = 0; | |
402 if(xmin < 0) xmin = 0; | |
403 if(ymax > mpi->h) ymax = mpi->h; | |
404 if(xmax > mpi->w) xmax = mpi->w; | |
405 | |
406 // Jump some the beginnig text if needed | |
407 while(sy < ymin && *txt) { | |
408 unsigned char c=*txt++; | |
409 if(c == '\n' || (warp && ll + vo_font->width[c] > w)) { | |
410 ll = 0; | |
411 sy += vo_font->height + vspace; | |
412 if(c == '\n') continue; | |
413 } | |
414 ll += vo_font->width[c]+vo_font->charspace; | |
415 } | |
416 if(*txt == '\0') // Nothing left to draw | |
417 return; | |
418 | |
419 while(sy < ymax && *txt) { | |
420 char* line_end = NULL; | |
421 int n; | |
422 | |
423 if(txt[0] == '\n') { // New line | |
424 sy += vo_font->height + vspace; | |
425 txt++; | |
426 continue; | |
427 } | |
428 | |
429 // Get the length and end of this line | |
430 for(n = 0, ll = 0 ; txt[n] != '\0' && txt[n] != '\n' ; n++) { | |
431 unsigned char c = txt[n]; | |
432 if(warp && ll + vo_font->width[c] > w) break; | |
433 ll += vo_font->width[c]+vo_font->charspace; | |
434 } | |
435 line_end = &txt[n]; | |
436 ll -= vo_font->charspace; | |
437 | |
438 | |
439 if(align & (MENU_TEXT_HCENTER|MENU_TEXT_RIGHT)) { | |
440 // Too long line | |
441 if(ll > xmax-xmin) { | |
442 if(align & MENU_TEXT_HCENTER) { | |
443 int mid = ll/2; | |
444 // Find the middle point | |
445 for(n--, ll = 0 ; n <= 0 ; n--) { | |
446 ll += vo_font->width[(int)txt[n]]+vo_font->charspace; | |
447 if(ll - vo_font->charspace > mid) break; | |
448 } | |
449 ll -= vo_font->charspace; | |
450 sx = xmid + mid - ll; | |
451 } else// MENU_TEXT_RIGHT) | |
452 sx = xmax + vo_font->charspace; | |
453 | |
454 // We are after the start point -> go back | |
455 if(sx > xmin) { | |
456 for(n-- ; n <= 0 ; n--) { | |
457 unsigned char c = txt[n]; | |
458 if(sx - vo_font->width[c] - vo_font->charspace < xmin) break; | |
459 sx -= vo_font->width[c]+vo_font->charspace; | |
460 } | |
461 } else { // We are before the start point -> go forward | |
462 for( ; sx < xmin && (&txt[n]) != line_end ; n++) { | |
463 unsigned char c = txt[n]; | |
464 sx += vo_font->width[c]+vo_font->charspace; | |
465 } | |
466 } | |
467 txt = &txt[n]; // Jump to the new start char | |
468 } else { | |
469 if(align & MENU_TEXT_HCENTER) | |
470 sx = xmid - ll/2; | |
471 else | |
8232 | 472 sx = xmax - 1 - ll; |
8197 | 473 } |
474 } else { | |
475 for(sx = xrmin ; sx < xmin && txt != line_end ; txt++) { | |
476 unsigned char c = txt[n]; | |
477 sx += vo_font->width[c]+vo_font->charspace; | |
478 } | |
479 } | |
480 | |
481 while(sx < xmax && txt != line_end) { | |
482 unsigned char c = *txt++; | |
483 font = vo_font->font[c]; | |
8232 | 484 if(font >= 0) { |
485 int cs = (vo_font->pic_a[font]->h - vo_font->height) / 2; | |
486 if ((sx + vo_font->width[c] < xmax) && (sy + vo_font->height < ymax) ) | |
487 draw_alpha(vo_font->width[c], vo_font->height, | |
488 vo_font->pic_b[font]->bmp+vo_font->start[c] + | |
489 cs * vo_font->pic_a[font]->w, | |
490 vo_font->pic_a[font]->bmp+vo_font->start[c] + | |
491 cs * vo_font->pic_a[font]->w, | |
492 vo_font->pic_a[font]->w, | |
493 mpi->planes[0] + sy * mpi->stride[0] + sx * (mpi->bpp>>3), | |
494 mpi->stride[0]); | |
495 // else | |
496 //printf("Can't draw '%c'\n",c); | |
497 } | |
8197 | 498 sx+=vo_font->width[c]+vo_font->charspace; |
499 } | |
500 txt = line_end; | |
501 if(txt[0] == '\0') break; | |
502 sy += vo_font->height + vspace; | |
503 } | |
504 } | |
505 | |
506 int menu_text_length(char* txt) { | |
507 int l = 0; | |
8224
fefc56153615
Fix freetype. Freetype is highly recommended for a nice output ;)
albeu
parents:
8197
diff
changeset
|
508 render_txt(txt); |
8197 | 509 while (*txt) { |
510 unsigned char c=*txt++; | |
511 l += vo_font->width[c]+vo_font->charspace; | |
512 } | |
513 return l - vo_font->charspace; | |
514 } | |
515 | |
516 void menu_text_size(char* txt,int max_width, int vspace, int warp, int* _w, int* _h) { | |
517 int l = 1, i = 0; | |
518 int w = 0; | |
8224
fefc56153615
Fix freetype. Freetype is highly recommended for a nice output ;)
albeu
parents:
8197
diff
changeset
|
519 |
fefc56153615
Fix freetype. Freetype is highly recommended for a nice output ;)
albeu
parents:
8197
diff
changeset
|
520 render_txt(txt); |
8197 | 521 while (*txt) { |
522 unsigned char c=*txt++; | |
523 if(c == '\n' || (warp && i + vo_font->width[c] >= max_width)) { | |
524 if(*txt) | |
525 l++; | |
526 i = 0; | |
527 if(c == '\n') continue; | |
528 } | |
529 i += vo_font->width[c]+vo_font->charspace; | |
530 if(i > w) w = i; | |
531 } | |
532 | |
533 *_w = w; | |
534 *_h = (l-1) * (vo_font->height + vspace) + vo_font->height; | |
535 } | |
536 | |
537 | |
538 int menu_text_num_lines(char* txt, int max_width) { | |
539 int l = 1, i = 0; | |
8224
fefc56153615
Fix freetype. Freetype is highly recommended for a nice output ;)
albeu
parents:
8197
diff
changeset
|
540 render_txt(txt); |
8197 | 541 while (*txt) { |
542 unsigned char c=*txt++; | |
543 if(c == '\n' || i + vo_font->width[c] > max_width) { | |
544 l++; | |
545 i = 0; | |
546 if(c == '\n') continue; | |
547 } | |
548 i += vo_font->width[c]+vo_font->charspace; | |
549 } | |
550 return l; | |
551 } | |
552 | |
553 char* menu_text_get_next_line(char* txt, int max_width) { | |
554 int i = 0; | |
8224
fefc56153615
Fix freetype. Freetype is highly recommended for a nice output ;)
albeu
parents:
8197
diff
changeset
|
555 render_txt(txt); |
8197 | 556 while (*txt) { |
557 unsigned char c=*txt; | |
558 if(c == '\n') { | |
559 txt++; | |
560 break; | |
561 } | |
562 i += vo_font->width[c]; | |
563 if(i >= max_width) | |
564 break; | |
565 i += vo_font->charspace; | |
566 } | |
567 return txt; | |
568 } | |
17993
98eb966a4024
Add a function to draw flat boxes and use it to make the list
albeu
parents:
17945
diff
changeset
|
569 |
98eb966a4024
Add a function to draw flat boxes and use it to make the list
albeu
parents:
17945
diff
changeset
|
570 |
18193 | 571 void menu_draw_box(mp_image_t* mpi,unsigned char grey,unsigned char alpha, int x, int y, int w, int h) { |
17993
98eb966a4024
Add a function to draw flat boxes and use it to make the list
albeu
parents:
17945
diff
changeset
|
572 draw_alpha_f draw_alpha = get_draw_alpha(mpi->imgfmt); |
18193 | 573 int g; |
17993
98eb966a4024
Add a function to draw flat boxes and use it to make the list
albeu
parents:
17945
diff
changeset
|
574 |
98eb966a4024
Add a function to draw flat boxes and use it to make the list
albeu
parents:
17945
diff
changeset
|
575 if(!draw_alpha) { |
18193 | 576 mp_msg(MSGT_GLOBAL,MSGL_WARN,MSGTR_LIBMENU_UnsupportedOutformat); |
17993
98eb966a4024
Add a function to draw flat boxes and use it to make the list
albeu
parents:
17945
diff
changeset
|
577 return; |
98eb966a4024
Add a function to draw flat boxes and use it to make the list
albeu
parents:
17945
diff
changeset
|
578 } |
98eb966a4024
Add a function to draw flat boxes and use it to make the list
albeu
parents:
17945
diff
changeset
|
579 |
98eb966a4024
Add a function to draw flat boxes and use it to make the list
albeu
parents:
17945
diff
changeset
|
580 if(x > mpi->w || y > mpi->h) return; |
98eb966a4024
Add a function to draw flat boxes and use it to make the list
albeu
parents:
17945
diff
changeset
|
581 |
98eb966a4024
Add a function to draw flat boxes and use it to make the list
albeu
parents:
17945
diff
changeset
|
582 if(x < 0) w += x, x = 0; |
98eb966a4024
Add a function to draw flat boxes and use it to make the list
albeu
parents:
17945
diff
changeset
|
583 if(x+w > mpi->w) w = mpi->w-x; |
98eb966a4024
Add a function to draw flat boxes and use it to make the list
albeu
parents:
17945
diff
changeset
|
584 if(y < 0) h += y, y = 0; |
98eb966a4024
Add a function to draw flat boxes and use it to make the list
albeu
parents:
17945
diff
changeset
|
585 if(y+h > mpi->h) h = mpi->h-y; |
18193 | 586 |
587 g = ((256-alpha)*grey)>>8; | |
588 if(g < 1) g = 1; | |
589 | |
17993
98eb966a4024
Add a function to draw flat boxes and use it to make the list
albeu
parents:
17945
diff
changeset
|
590 { |
98eb966a4024
Add a function to draw flat boxes and use it to make the list
albeu
parents:
17945
diff
changeset
|
591 int stride = (w+7)&(~7); // round to 8 |
98eb966a4024
Add a function to draw flat boxes and use it to make the list
albeu
parents:
17945
diff
changeset
|
592 char pic[stride*h],pic_alpha[stride*h]; |
18193 | 593 memset(pic,g,stride*h); |
17993
98eb966a4024
Add a function to draw flat boxes and use it to make the list
albeu
parents:
17945
diff
changeset
|
594 memset(pic_alpha,alpha,stride*h); |
98eb966a4024
Add a function to draw flat boxes and use it to make the list
albeu
parents:
17945
diff
changeset
|
595 draw_alpha(w,h,pic,pic_alpha,stride, |
98eb966a4024
Add a function to draw flat boxes and use it to make the list
albeu
parents:
17945
diff
changeset
|
596 mpi->planes[0] + y * mpi->stride[0] + x * (mpi->bpp>>3), |
98eb966a4024
Add a function to draw flat boxes and use it to make the list
albeu
parents:
17945
diff
changeset
|
597 mpi->stride[0]); |
98eb966a4024
Add a function to draw flat boxes and use it to make the list
albeu
parents:
17945
diff
changeset
|
598 } |
98eb966a4024
Add a function to draw flat boxes and use it to make the list
albeu
parents:
17945
diff
changeset
|
599 |
98eb966a4024
Add a function to draw flat boxes and use it to make the list
albeu
parents:
17945
diff
changeset
|
600 } |