Ignore svn change r30673
[mplayer/glamo.git] / libmenu / menu.c
blobcae17a55c9a635e4f54f46cc231ae12ece2e72b4
1 /*
2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include "config.h"
20 #include "mp_msg.h"
21 #include "help_mp.h"
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <fcntl.h>
27 #include <unistd.h>
29 #include "libvo/osd.h"
30 #include "libvo/font_load.h"
31 #include "libvo/sub.h"
32 #include "osdep/keycodes.h"
33 #include "asxparser.h"
34 #include "stream/stream.h"
35 #include "input/input.h"
37 #include "libmpcodecs/img_format.h"
38 #include "libmpcodecs/mp_image.h"
39 #include "m_option.h"
40 #include "m_struct.h"
41 #include "menu.h"
43 extern menu_info_t menu_info_cmdlist;
44 extern menu_info_t menu_info_chapsel;
45 extern menu_info_t menu_info_pt;
46 extern menu_info_t menu_info_filesel;
47 extern menu_info_t menu_info_txt;
48 extern menu_info_t menu_info_console;
49 extern menu_info_t menu_info_pref;
50 extern menu_info_t menu_info_dvbsel;
53 menu_info_t* menu_info_list[] = {
54 &menu_info_pt,
55 &menu_info_cmdlist,
56 &menu_info_chapsel,
57 &menu_info_filesel,
58 &menu_info_txt,
59 &menu_info_console,
60 #ifdef CONFIG_DVBIN
61 &menu_info_dvbsel,
62 #endif
63 &menu_info_pref,
64 NULL
67 typedef struct key_cmd_s {
68 int key;
69 char *cmd;
70 } key_cmd_t;
72 typedef struct menu_cmd_bindings_s {
73 char *name;
74 key_cmd_t *bindings;
75 int binding_num;
76 struct menu_cmd_bindings_s *parent;
77 } menu_cmd_bindings_t;
79 struct menu_def_st {
80 char* name;
81 menu_info_t* type;
82 void* cfg;
83 char* args;
86 double menu_mouse_x = -1.0;
87 double menu_mouse_y = -1.0;
88 int menu_mouse_pos_updated = 0;
90 static struct MPContext *menu_ctx = NULL;
91 static struct m_config *menu_mconfig = NULL;
92 static struct input_ctx *menu_input = NULL;
93 static menu_def_t* menu_list = NULL;
94 static int menu_count = 0;
95 static menu_cmd_bindings_t *cmd_bindings = NULL;
96 static int cmd_bindings_num = 0;
99 static menu_cmd_bindings_t *get_cmd_bindings(const char *name)
101 int i;
102 for (i = 0; i < cmd_bindings_num; ++i)
103 if (!strcasecmp(cmd_bindings[i].name, name))
104 return &cmd_bindings[i];
105 return NULL;
108 static int menu_parse_config(char* buffer, struct m_config *mconfig)
110 char *element,*body, **attribs, *name;
111 menu_info_t* minfo = NULL;
112 int r,i;
113 ASX_Parser_t* parser = asx_parser_new(mconfig);
115 while(1) {
116 r = asx_get_element(parser,&buffer,&element,&body,&attribs);
117 if(r < 0) {
118 mp_tmsg(MSGT_GLOBAL,MSGL_WARN,"[MENU] syntax error at line: %d\n",parser->line);
119 asx_parser_free(parser);
120 return 0;
121 } else if(r == 0) {
122 asx_parser_free(parser);
123 return 1;
125 // Has it a name ?
126 name = asx_get_attrib("name",attribs);
127 if(!name) {
128 mp_tmsg(MSGT_GLOBAL,MSGL_WARN,"[MENU] Menu definitions need a name attribute (line %d).\n",parser->line);
129 free(element);
130 if(body) free(body);
131 asx_free_attribs(attribs);
132 continue;
135 if (!strcasecmp(element, "keybindings")) {
136 menu_cmd_bindings_t *bindings = cmd_bindings;
137 char *parent_bindings;
138 cmd_bindings = realloc(cmd_bindings,
139 (cmd_bindings_num+1)*sizeof(menu_cmd_bindings_t));
140 for (i = 0; i < cmd_bindings_num; ++i)
141 if (cmd_bindings[i].parent)
142 cmd_bindings[i].parent = cmd_bindings[i].parent-bindings+cmd_bindings;
143 bindings = &cmd_bindings[cmd_bindings_num];
144 memset(bindings, 0, sizeof(menu_cmd_bindings_t));
145 bindings->name = name;
146 parent_bindings = asx_get_attrib("parent",attribs);
147 if (parent_bindings) {
148 bindings->parent = get_cmd_bindings(parent_bindings);
149 free(parent_bindings);
151 free(element);
152 asx_free_attribs(attribs);
153 if (body) {
154 char *bd = body;
155 char *b, *key, *cmd;
156 int keycode;
157 for(;;) {
158 r = asx_get_element(parser,&bd,&element,&b,&attribs);
159 if(r < 0) {
160 mp_tmsg(MSGT_GLOBAL,MSGL_WARN,"[MENU] syntax error at line: %d\n",
161 parser->line);
162 free(body);
163 asx_parser_free(parser);
164 return 0;
166 if(r == 0)
167 break;
168 if (!strcasecmp(element, "binding")) {
169 key = asx_get_attrib("key",attribs);
170 cmd = asx_get_attrib("cmd",attribs);
171 if (key && (keycode = mp_input_get_key_from_name(key)) >= 0) {
172 keycode &= ~MP_NO_REPEAT_KEY;
173 mp_msg(MSGT_GLOBAL,MSGL_V,
174 "[libmenu] got keybinding element %d %s=>[%s].\n",
175 keycode, key, cmd ? cmd : "");
176 bindings->bindings = realloc(bindings->bindings,
177 (bindings->binding_num+1)*sizeof(key_cmd_t));
178 bindings->bindings[bindings->binding_num].key = keycode;
179 bindings->bindings[bindings->binding_num].cmd = cmd;
180 ++bindings->binding_num;
182 else
183 free(cmd);
184 free(key);
186 free(element);
187 asx_free_attribs(attribs);
188 free(b);
190 free(body);
192 ++cmd_bindings_num;
193 continue;
195 // Try to find this menu type in our list
196 for(i = 0, minfo = NULL ; menu_info_list[i] ; i++) {
197 if(strcasecmp(element,menu_info_list[i]->name) == 0) {
198 minfo = menu_info_list[i];
199 break;
202 // Got it : add this to our list
203 if(minfo) {
204 menu_list = realloc(menu_list,(menu_count+2)*sizeof(menu_def_t));
205 menu_list[menu_count].name = name;
206 menu_list[menu_count].type = minfo;
207 menu_list[menu_count].cfg = m_struct_alloc(&minfo->priv_st);
208 menu_list[menu_count].args = body;
209 // Setup the attribs
210 for(i = 0 ; attribs[2*i] ; i++) {
211 if(strcasecmp(attribs[2*i],"name") == 0) continue;
212 if(!m_struct_set(&minfo->priv_st,menu_list[menu_count].cfg,attribs[2*i], attribs[2*i+1]))
213 mp_tmsg(MSGT_GLOBAL,MSGL_WARN,"[MENU] bad attribute %s=%s in menu '%s' at line %d\n",attribs[2*i],attribs[2*i+1],
214 name,parser->line);
216 menu_count++;
217 memset(&menu_list[menu_count],0,sizeof(menu_def_t));
218 } else {
219 mp_tmsg(MSGT_GLOBAL,MSGL_WARN,"[MENU] unknown menu type '%s' at line %d\n",element,parser->line);
220 free(name);
221 if(body) free(body);
224 free(element);
225 asx_free_attribs(attribs);
231 /// This will build the menu_defs list from the cfg file
232 #define BUF_STEP 1024
233 #define BUF_MIN 128
234 #define BUF_MAX BUF_STEP*1024
235 int menu_init(struct MPContext *mpctx, struct m_config *mconfig,
236 struct input_ctx *input_ctx, char* cfg_file)
238 char* buffer = NULL;
239 int bl = BUF_STEP, br = 0;
240 int f, fd;
241 #ifndef CONFIG_FREETYPE
242 if(vo_font == NULL)
243 return 0;
244 #endif
245 fd = open(cfg_file, O_RDONLY);
246 if(fd < 0) {
247 mp_tmsg(MSGT_GLOBAL,MSGL_WARN,"[MENU] Can't open menu config file: %s\n",cfg_file);
248 return 0;
250 buffer = malloc(bl);
251 while(1) {
252 int r;
253 if(bl - br < BUF_MIN) {
254 if(bl >= BUF_MAX) {
255 mp_tmsg(MSGT_GLOBAL,MSGL_WARN,"[MENU] Config file is too big (> %d KB)\n",BUF_MAX/1024);
256 close(fd);
257 free(buffer);
258 return 0;
260 bl += BUF_STEP;
261 buffer = realloc(buffer,bl);
263 r = read(fd,buffer+br,bl-br);
264 if(r == 0) break;
265 br += r;
267 if(!br) {
268 mp_tmsg(MSGT_GLOBAL,MSGL_WARN,"[MENU] Config file is empty.\n");
269 return 0;
271 buffer[br-1] = '\0';
273 close(fd);
275 menu_ctx = mpctx;
276 menu_mconfig = mconfig;
277 menu_input = input_ctx;
278 f = menu_parse_config(buffer, mconfig);
279 free(buffer);
280 return f;
283 // Destroy all this stuff
284 void menu_uninit(void) {
285 int i;
286 for(i = 0 ; menu_list && menu_list[i].name ; i++) {
287 free(menu_list[i].name);
288 m_struct_free(&menu_list[i].type->priv_st,menu_list[i].cfg);
289 if(menu_list[i].args) free(menu_list[i].args);
291 free(menu_list);
292 menu_count = 0;
293 for (i = 0; i < cmd_bindings_num; ++i) {
294 free(cmd_bindings[i].name);
295 while(cmd_bindings[i].binding_num > 0)
296 free(cmd_bindings[i].bindings[--cmd_bindings[i].binding_num].cmd);
297 free(cmd_bindings[i].bindings);
299 free(cmd_bindings);
302 /// Default read_key function
303 int menu_dflt_read_key(menu_t* menu,int cmd) {
304 int i;
305 menu_cmd_bindings_t *bindings = get_cmd_bindings(menu->type->name);
306 if (!bindings)
307 bindings = get_cmd_bindings(menu->type->type->name);
308 if (!bindings)
309 bindings = get_cmd_bindings("default");
310 while (bindings) {
311 for (i = 0; i < bindings->binding_num; ++i) {
312 if (bindings->bindings[i].key == cmd) {
313 if (bindings->bindings[i].cmd)
314 mp_input_parse_and_queue_cmds(menu->input_ctx,
315 bindings->bindings[i].cmd);
316 return 1;
319 bindings = bindings->parent;
321 return 0;
324 menu_t* menu_open(char *name) {
325 menu_t* m;
326 int i;
328 for(i = 0 ; menu_list[i].name != NULL ; i++) {
329 if(strcmp(name,menu_list[i].name) == 0)
330 break;
332 if(menu_list[i].name == NULL) {
333 mp_tmsg(MSGT_GLOBAL,MSGL_WARN,"[MENU] Menu %s not found.\n",name);
334 return NULL;
336 m = calloc(1,sizeof(menu_t));
337 m->priv_st = &(menu_list[i].type->priv_st);
338 m->priv = m_struct_copy(m->priv_st,menu_list[i].cfg);
339 m->ctx = menu_ctx;
340 m->mconfig = menu_mconfig;
341 m->input_ctx = menu_input;
342 m->type = &menu_list[i];
343 if(menu_list[i].type->open(m,menu_list[i].args))
344 return m;
345 if(m->priv)
346 m_struct_free(m->priv_st,m->priv);
347 free(m);
348 mp_tmsg(MSGT_GLOBAL,MSGL_WARN,"[MENU] Menu '%s': Init failed.\n",name);
349 return NULL;
352 void menu_draw(menu_t* menu,mp_image_t* mpi) {
353 if(menu->show && menu->draw)
354 menu->draw(menu,mpi);
357 void menu_update_mouse_pos(double x, double y) {
358 menu_mouse_x = x;
359 menu_mouse_y = y;
360 menu_mouse_pos_updated = 1;
363 void menu_read_cmd(menu_t* menu,int cmd) {
364 if(menu->read_cmd)
365 menu->read_cmd(menu,cmd);
368 void menu_close(menu_t* menu) {
369 if(menu->close)
370 menu->close(menu);
371 if(menu->priv)
372 m_struct_free(menu->priv_st,menu->priv);
373 free(menu);
376 int menu_read_key(menu_t* menu,int cmd) {
377 if(menu->read_key)
378 return menu->read_key(menu,cmd);
379 else
380 return menu_dflt_read_key(menu,cmd);
383 ///////////////////////////// Helpers ////////////////////////////////////
385 typedef void (*draw_alpha_f)(int w,int h, unsigned char* src, unsigned char *srca, int srcstride, unsigned char* dstbase,int dststride);
387 inline static draw_alpha_f get_draw_alpha(uint32_t fmt) {
388 switch(fmt) {
389 case IMGFMT_BGR15:
390 case IMGFMT_RGB15:
391 return vo_draw_alpha_rgb15;
392 case IMGFMT_BGR16:
393 case IMGFMT_RGB16:
394 return vo_draw_alpha_rgb16;
395 case IMGFMT_BGR24:
396 case IMGFMT_RGB24:
397 return vo_draw_alpha_rgb24;
398 case IMGFMT_BGR32:
399 case IMGFMT_RGB32:
400 return vo_draw_alpha_rgb32;
401 case IMGFMT_YV12:
402 case IMGFMT_I420:
403 case IMGFMT_IYUV:
404 case IMGFMT_YVU9:
405 case IMGFMT_IF09:
406 case IMGFMT_Y800:
407 case IMGFMT_Y8:
408 return vo_draw_alpha_yv12;
409 case IMGFMT_YUY2:
410 return vo_draw_alpha_yuy2;
411 case IMGFMT_UYVY:
412 return vo_draw_alpha_uyvy;
415 return NULL;
418 // return the real height of a char:
419 static inline int get_height(int c,int h){
420 int font;
421 if ((font=vo_font->font[c])>=0)
422 if(h<vo_font->pic_a[font]->h) h=vo_font->pic_a[font]->h;
423 return h;
426 static void render_txt(char *txt)
428 while (*txt) {
429 int c = utf8_get_char((const char**)&txt);
430 render_one_glyph(vo_font, c);
434 #ifdef CONFIG_FRIBIDI
435 #include <fribidi/fribidi.h>
436 #include "libavutil/common.h"
437 char *menu_fribidi_charset = NULL;
438 int menu_flip_hebrew = 0;
439 int menu_fribidi_flip_commas = 0;
441 static char *menu_fribidi(char *txt)
443 static int char_set_num = -1;
444 static FriBidiChar *logical, *visual;
445 static size_t buffer_size = 1024;
446 static char *outputstr;
448 FriBidiCharType base;
449 fribidi_boolean log2vis;
450 size_t len;
452 if (menu_flip_hebrew) {
453 len = strlen(txt);
454 if (char_set_num == -1) {
455 fribidi_set_mirroring (1);
456 fribidi_set_reorder_nsm (0);
457 char_set_num = fribidi_parse_charset("UTF-8");
458 buffer_size = FFMAX(1024,len+1);
459 logical = malloc(buffer_size);
460 visual = malloc(buffer_size);
461 outputstr = malloc(buffer_size);
462 } else if (len+1 > buffer_size) {
463 buffer_size = len+1;
464 logical = realloc(logical, buffer_size);
465 visual = realloc(visual, buffer_size);
466 outputstr = realloc(outputstr, buffer_size);
468 len = fribidi_charset_to_unicode (char_set_num, txt, len, logical);
469 base = menu_fribidi_flip_commas?FRIBIDI_TYPE_ON:FRIBIDI_TYPE_L;
470 log2vis = fribidi_log2vis (logical, len, &base, visual, NULL, NULL, NULL);
471 if (log2vis) {
472 len = fribidi_remove_bidi_marks (visual, len, NULL, NULL, NULL);
473 fribidi_unicode_to_charset (char_set_num, visual, len, outputstr);
474 return outputstr;
477 return txt;
479 #endif
481 void menu_draw_text(mp_image_t* mpi,char* txt, int x, int y) {
482 draw_alpha_f draw_alpha = get_draw_alpha(mpi->imgfmt);
483 int font;
485 if(!draw_alpha) {
486 mp_tmsg(MSGT_GLOBAL,MSGL_WARN,"[MENU] Unsupported output format!!!!\n");
487 return;
490 #ifdef CONFIG_FRIBIDI
491 txt = menu_fribidi(txt);
492 #endif
493 render_txt(txt);
495 while (*txt) {
496 int c=utf8_get_char((const char**)&txt);
497 if ((font=vo_font->font[c])>=0 && (x + vo_font->width[c] <= mpi->w) && (y + vo_font->pic_a[font]->h <= mpi->h))
498 draw_alpha(vo_font->width[c], vo_font->pic_a[font]->h,
499 vo_font->pic_b[font]->bmp+vo_font->start[c],
500 vo_font->pic_a[font]->bmp+vo_font->start[c],
501 vo_font->pic_a[font]->w,
502 mpi->planes[0] + y * mpi->stride[0] + x * (mpi->bpp>>3),
503 mpi->stride[0]);
504 x+=vo_font->width[c]+vo_font->charspace;
509 void menu_draw_text_full(mp_image_t* mpi,char* txt,
510 int x, int y,int w, int h,
511 int vspace, int warp, int align, int anchor) {
512 int need_w,need_h;
513 int sy, ymin, ymax;
514 int sx, xmin, xmax, xmid, xrmin;
515 int ll = 0;
516 int font;
517 draw_alpha_f draw_alpha = get_draw_alpha(mpi->imgfmt);
519 if(!draw_alpha) {
520 mp_tmsg(MSGT_GLOBAL,MSGL_WARN,"[MENU] Unsupported output format!!!!\n");
521 return;
524 #ifdef CONFIG_FRIBIDI
525 txt = menu_fribidi(txt);
526 #endif
527 render_txt(txt);
529 if(x > mpi->w || y > mpi->h)
530 return;
532 if(anchor & MENU_TEXT_VCENTER) {
533 if(h <= 0) h = mpi->h;
534 ymin = y - h/2;
535 ymax = y + h/2;
536 } else if(anchor & MENU_TEXT_BOT) {
537 if(h <= 0) h = mpi->h - y;
538 ymin = y - h;
539 ymax = y;
540 } else {
541 if(h <= 0) h = mpi->h - y;
542 ymin = y;
543 ymax = y + h;
546 if(anchor & MENU_TEXT_HCENTER) {
547 if(w <= 0) w = mpi->w;
548 xmin = x - w/2;
549 xmax = x + w/2;
550 } else if(anchor & MENU_TEXT_RIGHT) {
551 if(w <= 0) w = mpi->w -x;
552 xmin = x - w;
553 xmax = x;
554 } else {
555 if(w <= 0) w = mpi->w -x;
556 xmin = x;
557 xmax = x + w;
560 // How many space do we need to draw this ?
561 menu_text_size(txt,w,vspace,warp,&need_w,&need_h);
563 // Find the first line
564 if(align & MENU_TEXT_VCENTER)
565 sy = ymin + ((h - need_h)/2);
566 else if(align & MENU_TEXT_BOT)
567 sy = ymax - need_h - 1;
568 else
569 sy = y;
571 #if 0
572 // Find the first col
573 if(align & MENU_TEXT_HCENTER)
574 sx = xmin + ((w - need_w)/2);
575 else if(align & MENU_TEXT_RIGHT)
576 sx = xmax - need_w;
577 #endif
579 xmid = xmin + (xmax - xmin) / 2;
580 xrmin = xmin;
581 // Clamp the bb to the mpi size
582 if(ymin < 0) ymin = 0;
583 if(xmin < 0) xmin = 0;
584 if(ymax > mpi->h) ymax = mpi->h;
585 if(xmax > mpi->w) xmax = mpi->w;
587 // Jump some the beginnig text if needed
588 while(sy < ymin && *txt) {
589 int c=utf8_get_char((const char**)&txt);
590 if(c == '\n' || (warp && ll + vo_font->width[c] > w)) {
591 ll = 0;
592 sy += vo_font->height + vspace;
593 if(c == '\n') continue;
595 ll += vo_font->width[c]+vo_font->charspace;
597 if(*txt == '\0') // Nothing left to draw
598 return;
600 while(sy < ymax && *txt) {
601 char* line_end = NULL;
602 int n;
604 if(txt[0] == '\n') { // New line
605 sy += vo_font->height + vspace;
606 txt++;
607 continue;
610 // Get the length and end of this line
611 for(n = 0, ll = 0 ; txt[n] != '\0' && txt[n] != '\n' ; n++) {
612 unsigned char c = txt[n];
613 if(warp && ll + vo_font->width[c] > w) break;
614 ll += vo_font->width[c]+vo_font->charspace;
616 line_end = &txt[n];
617 ll -= vo_font->charspace;
620 if(align & (MENU_TEXT_HCENTER|MENU_TEXT_RIGHT)) {
621 // Too long line
622 if(ll > xmax-xmin) {
623 if(align & MENU_TEXT_HCENTER) {
624 int mid = ll/2;
625 // Find the middle point
626 for(n--, ll = 0 ; n <= 0 ; n--) {
627 ll += vo_font->width[(int)txt[n]]+vo_font->charspace;
628 if(ll - vo_font->charspace > mid) break;
630 ll -= vo_font->charspace;
631 sx = xmid + mid - ll;
632 } else// MENU_TEXT_RIGHT)
633 sx = xmax + vo_font->charspace;
635 // We are after the start point -> go back
636 if(sx > xmin) {
637 for(n-- ; n <= 0 ; n--) {
638 unsigned char c = txt[n];
639 if(sx - vo_font->width[c] - vo_font->charspace < xmin) break;
640 sx -= vo_font->width[c]+vo_font->charspace;
642 } else { // We are before the start point -> go forward
643 for( ; sx < xmin && (&txt[n]) != line_end ; n++) {
644 unsigned char c = txt[n];
645 sx += vo_font->width[c]+vo_font->charspace;
648 txt = &txt[n]; // Jump to the new start char
649 } else {
650 if(align & MENU_TEXT_HCENTER)
651 sx = xmid - ll/2;
652 else
653 sx = xmax - 1 - ll;
655 } else {
656 for(sx = xrmin ; sx < xmin && txt != line_end ; txt++) {
657 unsigned char c = txt[n];
658 sx += vo_font->width[c]+vo_font->charspace;
662 while(sx < xmax && txt != line_end) {
663 int c=utf8_get_char((const char**)&txt);
664 font = vo_font->font[c];
665 if(font >= 0) {
666 int cs = (vo_font->pic_a[font]->h - vo_font->height) / 2;
667 if ((sx + vo_font->width[c] <= xmax) && (sy + vo_font->height <= ymax) )
668 draw_alpha(vo_font->width[c], vo_font->height,
669 vo_font->pic_b[font]->bmp+vo_font->start[c] +
670 cs * vo_font->pic_a[font]->w,
671 vo_font->pic_a[font]->bmp+vo_font->start[c] +
672 cs * vo_font->pic_a[font]->w,
673 vo_font->pic_a[font]->w,
674 mpi->planes[0] + sy * mpi->stride[0] + sx * (mpi->bpp>>3),
675 mpi->stride[0]);
676 // else
677 //printf("Can't draw '%c'\n",c);
679 sx+=vo_font->width[c]+vo_font->charspace;
681 txt = line_end;
682 if(txt[0] == '\0') break;
683 sy += vo_font->height + vspace;
687 int menu_text_length(char* txt) {
688 int l = 0;
689 render_txt(txt);
690 while (*txt) {
691 int c=utf8_get_char((const char**)&txt);
692 l += vo_font->width[c]+vo_font->charspace;
694 return l - vo_font->charspace;
697 void menu_text_size(char* txt,int max_width, int vspace, int warp, int* _w, int* _h) {
698 int l = 1, i = 0;
699 int w = 0;
701 render_txt(txt);
702 while (*txt) {
703 int c=utf8_get_char((const char**)&txt);
704 if(c == '\n' || (warp && i + vo_font->width[c] >= max_width)) {
705 i -= vo_font->charspace;
706 if (i > w) w = i;
707 if(*txt)
708 l++;
709 i = 0;
710 if(c == '\n') continue;
712 i += vo_font->width[c]+vo_font->charspace;
714 if (i > 0) {
715 i -= vo_font->charspace;
716 if (i > w) w = i;
719 *_w = w;
720 *_h = (l-1) * (vo_font->height + vspace) + vo_font->height;
724 int menu_text_num_lines(char* txt, int max_width) {
725 int l = 1, i = 0;
726 render_txt(txt);
727 while (*txt) {
728 int c=utf8_get_char((const char**)&txt);
729 if(c == '\n' || i + vo_font->width[c] > max_width) {
730 l++;
731 i = 0;
732 if(c == '\n') continue;
734 i += vo_font->width[c]+vo_font->charspace;
736 return l;
739 static char* menu_text_get_next_line(char* txt, int max_width)
741 int i = 0;
742 render_txt(txt);
743 while (*txt) {
744 int c=utf8_get_char((const char**)&txt);
745 if(c == '\n') {
746 txt++;
747 break;
749 i += vo_font->width[c];
750 if(i >= max_width)
751 break;
752 i += vo_font->charspace;
754 return txt;
758 void menu_draw_box(mp_image_t* mpi,unsigned char grey,unsigned char alpha, int x, int y, int w, int h) {
759 draw_alpha_f draw_alpha = get_draw_alpha(mpi->imgfmt);
760 int g;
762 if(!draw_alpha) {
763 mp_tmsg(MSGT_GLOBAL,MSGL_WARN,"[MENU] Unsupported output format!!!!\n");
764 return;
767 if(x > mpi->w || y > mpi->h) return;
769 if(x < 0) w += x, x = 0;
770 if(x+w > mpi->w) w = mpi->w-x;
771 if(y < 0) h += y, y = 0;
772 if(y+h > mpi->h) h = mpi->h-y;
774 g = ((256-alpha)*grey)>>8;
775 if(g < 1) g = 1;
778 int stride = (w+7)&(~7); // round to 8
779 char pic[stride*h],pic_alpha[stride*h];
780 memset(pic,g,stride*h);
781 memset(pic_alpha,alpha,stride*h);
782 draw_alpha(w,h,pic,pic_alpha,stride,
783 mpi->planes[0] + y * mpi->stride[0] + x * (mpi->bpp>>3),
784 mpi->stride[0]);