vo_gl: fix crash with backends lacking control() features
[mplayer.git] / libmenu / menu.c
blob3e003900ce66180e5926f4f8dce2c3272c75a7fe
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"
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <fcntl.h>
26 #include <unistd.h>
28 #include "libvo/osd.h"
29 #include "sub/font_load.h"
30 #include "sub/sub.h"
31 #include "input/keycodes.h"
32 #include "asxparser.h"
33 #include "stream/stream.h"
34 #include "input/input.h"
36 #include "libmpcodecs/img_format.h"
37 #include "libmpcodecs/mp_image.h"
38 #include "m_option.h"
39 #include "m_struct.h"
40 #include "menu.h"
42 extern menu_info_t menu_info_cmdlist;
43 extern menu_info_t menu_info_chapsel;
44 extern menu_info_t menu_info_pt;
45 extern menu_info_t menu_info_filesel;
46 extern menu_info_t menu_info_txt;
47 extern menu_info_t menu_info_console;
48 extern menu_info_t menu_info_pref;
49 extern menu_info_t menu_info_dvbsel;
52 menu_info_t* menu_info_list[] = {
53 &menu_info_pt,
54 &menu_info_cmdlist,
55 &menu_info_chapsel,
56 &menu_info_filesel,
57 &menu_info_txt,
58 &menu_info_console,
59 #ifdef CONFIG_DVBIN
60 &menu_info_dvbsel,
61 #endif
62 &menu_info_pref,
63 NULL
66 typedef struct key_cmd_s {
67 int key;
68 char *cmd;
69 } key_cmd_t;
71 typedef struct menu_cmd_bindings_s {
72 char *name;
73 key_cmd_t *bindings;
74 int binding_num;
75 struct menu_cmd_bindings_s *parent;
76 } menu_cmd_bindings_t;
78 struct menu_def_st {
79 char* name;
80 menu_info_t* type;
81 void* cfg;
82 char* args;
85 double menu_mouse_x = -1.0;
86 double menu_mouse_y = -1.0;
87 int menu_mouse_pos_updated = 0;
89 static struct MPContext *menu_ctx = NULL;
90 static struct m_config *menu_mconfig = NULL;
91 static struct input_ctx *menu_input = NULL;
92 static menu_def_t* menu_list = NULL;
93 static int menu_count = 0;
94 static menu_cmd_bindings_t *cmd_bindings = NULL;
95 static int cmd_bindings_num = 0;
98 static menu_cmd_bindings_t *get_cmd_bindings(const char *name)
100 int i;
101 for (i = 0; i < cmd_bindings_num; ++i)
102 if (!strcasecmp(cmd_bindings[i].name, name))
103 return &cmd_bindings[i];
104 return NULL;
107 static int menu_parse_config(char* buffer, struct m_config *mconfig)
109 char *element,*body, **attribs, *name;
110 menu_info_t* minfo = NULL;
111 int r,i;
112 ASX_Parser_t* parser = asx_parser_new(mconfig);
114 while(1) {
115 r = asx_get_element(parser,&buffer,&element,&body,&attribs);
116 if(r < 0) {
117 mp_tmsg(MSGT_GLOBAL,MSGL_WARN,"[MENU] syntax error at line: %d\n",parser->line);
118 asx_parser_free(parser);
119 return 0;
120 } else if(r == 0) {
121 asx_parser_free(parser);
122 return 1;
124 // Has it a name ?
125 name = asx_get_attrib("name",attribs);
126 if(!name) {
127 mp_tmsg(MSGT_GLOBAL,MSGL_WARN,"[MENU] Menu definitions need a name attribute (line %d).\n",parser->line);
128 free(element);
129 free(body);
130 asx_free_attribs(attribs);
131 continue;
134 if (!strcasecmp(element, "keybindings")) {
135 menu_cmd_bindings_t *bindings = cmd_bindings;
136 char *parent_bindings;
137 cmd_bindings = realloc(cmd_bindings,
138 (cmd_bindings_num+1)*sizeof(menu_cmd_bindings_t));
139 for (i = 0; i < cmd_bindings_num; ++i)
140 if (cmd_bindings[i].parent)
141 cmd_bindings[i].parent = cmd_bindings[i].parent-bindings+cmd_bindings;
142 bindings = &cmd_bindings[cmd_bindings_num];
143 memset(bindings, 0, sizeof(menu_cmd_bindings_t));
144 bindings->name = name;
145 parent_bindings = asx_get_attrib("parent",attribs);
146 if (parent_bindings) {
147 bindings->parent = get_cmd_bindings(parent_bindings);
148 free(parent_bindings);
150 free(element);
151 asx_free_attribs(attribs);
152 if (body) {
153 char *bd = body;
154 char *b, *key, *cmd;
155 int keycode;
156 for(;;) {
157 r = asx_get_element(parser,&bd,&element,&b,&attribs);
158 if(r < 0) {
159 mp_tmsg(MSGT_GLOBAL,MSGL_WARN,"[MENU] syntax error at line: %d\n",
160 parser->line);
161 free(body);
162 asx_parser_free(parser);
163 return 0;
165 if(r == 0)
166 break;
167 if (!strcasecmp(element, "binding")) {
168 key = asx_get_attrib("key",attribs);
169 cmd = asx_get_attrib("cmd",attribs);
170 if (key && (keycode = mp_input_get_key_from_name(key)) >= 0) {
171 keycode &= ~MP_NO_REPEAT_KEY;
172 mp_msg(MSGT_GLOBAL,MSGL_V,
173 "[libmenu] got keybinding element %d %s=>[%s].\n",
174 keycode, key, cmd ? cmd : "");
175 bindings->bindings = realloc(bindings->bindings,
176 (bindings->binding_num+1)*sizeof(key_cmd_t));
177 bindings->bindings[bindings->binding_num].key = keycode;
178 bindings->bindings[bindings->binding_num].cmd = cmd;
179 ++bindings->binding_num;
181 else
182 free(cmd);
183 free(key);
185 free(element);
186 asx_free_attribs(attribs);
187 free(b);
189 free(body);
191 ++cmd_bindings_num;
192 continue;
194 // Try to find this menu type in our list
195 for(i = 0, minfo = NULL ; menu_info_list[i] ; i++) {
196 if(strcasecmp(element,menu_info_list[i]->name) == 0) {
197 minfo = menu_info_list[i];
198 break;
201 // Got it : add this to our list
202 if(minfo) {
203 menu_list = realloc(menu_list,(menu_count+2)*sizeof(menu_def_t));
204 menu_list[menu_count].name = name;
205 menu_list[menu_count].type = minfo;
206 menu_list[menu_count].cfg = m_struct_alloc(&minfo->priv_st);
207 menu_list[menu_count].args = body;
208 // Setup the attribs
209 for(i = 0 ; attribs[2*i] ; i++) {
210 if(strcasecmp(attribs[2*i],"name") == 0) continue;
211 if (!m_struct_set(&minfo->priv_st, menu_list[menu_count].cfg,
212 attribs[2*i], bstr(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 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 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_BGR12:
390 case IMGFMT_RGB12:
391 return vo_draw_alpha_rgb12;
392 case IMGFMT_BGR15:
393 case IMGFMT_RGB15:
394 return vo_draw_alpha_rgb15;
395 case IMGFMT_BGR16:
396 case IMGFMT_RGB16:
397 return vo_draw_alpha_rgb16;
398 case IMGFMT_BGR24:
399 case IMGFMT_RGB24:
400 return vo_draw_alpha_rgb24;
401 case IMGFMT_BGR32:
402 case IMGFMT_RGB32:
403 return vo_draw_alpha_rgb32;
404 case IMGFMT_YV12:
405 case IMGFMT_I420:
406 case IMGFMT_IYUV:
407 case IMGFMT_YVU9:
408 case IMGFMT_IF09:
409 case IMGFMT_Y800:
410 case IMGFMT_Y8:
411 return vo_draw_alpha_yv12;
412 case IMGFMT_YUY2:
413 return vo_draw_alpha_yuy2;
414 case IMGFMT_UYVY:
415 return vo_draw_alpha_uyvy;
418 return NULL;
421 // return the real height of a char:
422 static inline int get_height(int c,int h){
423 int font;
424 if ((font=vo_font->font[c])>=0)
425 if(h<vo_font->pic_a[font]->h) h=vo_font->pic_a[font]->h;
426 return h;
429 static void render_txt(char *txt)
431 while (*txt) {
432 int c = utf8_get_char((const char**)&txt);
433 render_one_glyph(vo_font, c);
437 #ifdef CONFIG_FRIBIDI
438 #include <fribidi/fribidi.h>
439 #include "libavutil/common.h"
440 char *menu_fribidi_charset = NULL;
441 int menu_flip_hebrew = 0;
442 int menu_fribidi_flip_commas = 0;
444 static char *menu_fribidi(char *txt)
446 static int char_set_num = -1;
447 static FriBidiChar *logical, *visual;
448 static size_t buffer_size = 1024;
449 static char *outputstr;
451 #if FRIBIDI_INTERFACE_VERSION < 3
452 FriBidiCharType base;
453 #else
454 FriBidiParType base;
455 #endif
456 fribidi_boolean log2vis;
457 size_t len;
459 if (menu_flip_hebrew) {
460 len = strlen(txt);
461 if (char_set_num == -1) {
462 fribidi_set_mirroring (1);
463 fribidi_set_reorder_nsm (0);
464 char_set_num = fribidi_parse_charset("UTF-8");
465 buffer_size = FFMAX(1024,len+1);
466 logical = malloc(buffer_size);
467 visual = malloc(buffer_size);
468 outputstr = malloc(buffer_size);
469 } else if (len+1 > buffer_size) {
470 buffer_size = len+1;
471 logical = realloc(logical, buffer_size);
472 visual = realloc(visual, buffer_size);
473 outputstr = realloc(outputstr, buffer_size);
475 len = fribidi_charset_to_unicode (char_set_num, txt, len, logical);
476 base = menu_fribidi_flip_commas?FRIBIDI_TYPE_ON:FRIBIDI_TYPE_L;
477 log2vis = fribidi_log2vis (logical, len, &base, visual, NULL, NULL, NULL);
478 if (log2vis) {
479 len = fribidi_remove_bidi_marks (visual, len, NULL, NULL, NULL);
480 fribidi_unicode_to_charset (char_set_num, visual, len, outputstr);
481 return outputstr;
484 return txt;
486 #endif
488 void menu_draw_text(mp_image_t* mpi,char* txt, int x, int y) {
489 draw_alpha_f draw_alpha = get_draw_alpha(mpi->imgfmt);
490 int font;
492 if(!draw_alpha) {
493 mp_tmsg(MSGT_GLOBAL,MSGL_WARN,"[MENU] Unsupported output format!!!!\n");
494 return;
497 #ifdef CONFIG_FRIBIDI
498 txt = menu_fribidi(txt);
499 #endif
500 render_txt(txt);
502 while (*txt) {
503 int c=utf8_get_char((const char**)&txt);
504 if ((font=vo_font->font[c])>=0 && (x + vo_font->width[c] <= mpi->w) && (y + vo_font->pic_a[font]->h <= mpi->h))
505 draw_alpha(vo_font->width[c], vo_font->pic_a[font]->h,
506 vo_font->pic_b[font]->bmp+vo_font->start[c],
507 vo_font->pic_a[font]->bmp+vo_font->start[c],
508 vo_font->pic_a[font]->w,
509 mpi->planes[0] + y * mpi->stride[0] + x * (mpi->bpp>>3),
510 mpi->stride[0]);
511 x+=vo_font->width[c]+vo_font->charspace;
516 void menu_draw_text_full(mp_image_t* mpi,char* txt,
517 int x, int y,int w, int h,
518 int vspace, int warp, int align, int anchor) {
519 int need_w,need_h;
520 int sy, ymin, ymax;
521 int sx, xmin, xmax, xmid, xrmin;
522 int ll = 0;
523 int font;
524 draw_alpha_f draw_alpha = get_draw_alpha(mpi->imgfmt);
526 if(!draw_alpha) {
527 mp_tmsg(MSGT_GLOBAL,MSGL_WARN,"[MENU] Unsupported output format!!!!\n");
528 return;
531 #ifdef CONFIG_FRIBIDI
532 txt = menu_fribidi(txt);
533 #endif
534 render_txt(txt);
536 if(x > mpi->w || y > mpi->h)
537 return;
539 if(anchor & MENU_TEXT_VCENTER) {
540 if(h <= 0) h = mpi->h;
541 ymin = y - h/2;
542 ymax = y + h/2;
543 } else if(anchor & MENU_TEXT_BOT) {
544 if(h <= 0) h = mpi->h - y;
545 ymin = y - h;
546 ymax = y;
547 } else {
548 if(h <= 0) h = mpi->h - y;
549 ymin = y;
550 ymax = y + h;
553 if(anchor & MENU_TEXT_HCENTER) {
554 if(w <= 0) w = mpi->w;
555 xmin = x - w/2;
556 xmax = x + w/2;
557 } else if(anchor & MENU_TEXT_RIGHT) {
558 if(w <= 0) w = mpi->w -x;
559 xmin = x - w;
560 xmax = x;
561 } else {
562 if(w <= 0) w = mpi->w -x;
563 xmin = x;
564 xmax = x + w;
567 // How many space do we need to draw this ?
568 menu_text_size(txt,w,vspace,warp,&need_w,&need_h);
570 // Find the first line
571 if(align & MENU_TEXT_VCENTER)
572 sy = ymin + ((h - need_h)/2);
573 else if(align & MENU_TEXT_BOT)
574 sy = ymax - need_h - 1;
575 else
576 sy = y;
578 #if 0
579 // Find the first col
580 if(align & MENU_TEXT_HCENTER)
581 sx = xmin + ((w - need_w)/2);
582 else if(align & MENU_TEXT_RIGHT)
583 sx = xmax - need_w;
584 #endif
586 xmid = xmin + (xmax - xmin) / 2;
587 xrmin = xmin;
588 // Clamp the bb to the mpi size
589 if(ymin < 0) ymin = 0;
590 if(xmin < 0) xmin = 0;
591 if(ymax > mpi->h) ymax = mpi->h;
592 if(xmax > mpi->w) xmax = mpi->w;
594 // Jump some the beginnig text if needed
595 while(sy < ymin && *txt) {
596 int c=utf8_get_char((const char**)&txt);
597 if(c == '\n' || (warp && ll + vo_font->width[c] > w)) {
598 ll = 0;
599 sy += vo_font->height + vspace;
600 if(c == '\n') continue;
602 ll += vo_font->width[c]+vo_font->charspace;
604 if(*txt == '\0') // Nothing left to draw
605 return;
607 while(sy < ymax && *txt) {
608 char* line_end = NULL;
609 int n;
611 if(txt[0] == '\n') { // New line
612 sy += vo_font->height + vspace;
613 txt++;
614 continue;
617 // Get the length and end of this line
618 for(n = 0, ll = 0 ; txt[n] != '\0' && txt[n] != '\n' ; n++) {
619 unsigned char c = txt[n];
620 if(warp && ll + vo_font->width[c] > w) break;
621 ll += vo_font->width[c]+vo_font->charspace;
623 line_end = &txt[n];
624 ll -= vo_font->charspace;
627 if(align & (MENU_TEXT_HCENTER|MENU_TEXT_RIGHT)) {
628 // Too long line
629 if(ll > xmax-xmin) {
630 if(align & MENU_TEXT_HCENTER) {
631 int mid = ll/2;
632 // Find the middle point
633 for(n--, ll = 0 ; n <= 0 ; n--) {
634 ll += vo_font->width[(int)txt[n]]+vo_font->charspace;
635 if(ll - vo_font->charspace > mid) break;
637 ll -= vo_font->charspace;
638 sx = xmid + mid - ll;
639 } else// MENU_TEXT_RIGHT)
640 sx = xmax + vo_font->charspace;
642 // We are after the start point -> go back
643 if(sx > xmin) {
644 for(n-- ; n <= 0 ; n--) {
645 unsigned char c = txt[n];
646 if(sx - vo_font->width[c] - vo_font->charspace < xmin) break;
647 sx -= vo_font->width[c]+vo_font->charspace;
649 } else { // We are before the start point -> go forward
650 for( ; sx < xmin && (&txt[n]) != line_end ; n++) {
651 unsigned char c = txt[n];
652 sx += vo_font->width[c]+vo_font->charspace;
655 txt = &txt[n]; // Jump to the new start char
656 } else {
657 if(align & MENU_TEXT_HCENTER)
658 sx = xmid - ll/2;
659 else
660 sx = xmax - 1 - ll;
662 } else {
663 for(sx = xrmin ; sx < xmin && txt != line_end ; txt++) {
664 unsigned char c = txt[n];
665 sx += vo_font->width[c]+vo_font->charspace;
669 while(sx < xmax && txt != line_end) {
670 int c=utf8_get_char((const char**)&txt);
671 font = vo_font->font[c];
672 if(font >= 0) {
673 int cs = (vo_font->pic_a[font]->h - vo_font->height) / 2;
674 if ((sx + vo_font->width[c] <= xmax) && (sy + vo_font->height <= ymax) )
675 draw_alpha(vo_font->width[c], vo_font->height,
676 vo_font->pic_b[font]->bmp+vo_font->start[c] +
677 cs * vo_font->pic_a[font]->w,
678 vo_font->pic_a[font]->bmp+vo_font->start[c] +
679 cs * vo_font->pic_a[font]->w,
680 vo_font->pic_a[font]->w,
681 mpi->planes[0] + sy * mpi->stride[0] + sx * (mpi->bpp>>3),
682 mpi->stride[0]);
683 // else
684 //printf("Can't draw '%c'\n",c);
686 sx+=vo_font->width[c]+vo_font->charspace;
688 txt = line_end;
689 if(txt[0] == '\0') break;
690 sy += vo_font->height + vspace;
694 int menu_text_length(char* txt) {
695 int l = 0;
696 render_txt(txt);
697 while (*txt) {
698 int c=utf8_get_char((const char**)&txt);
699 l += vo_font->width[c]+vo_font->charspace;
701 return l - vo_font->charspace;
704 void menu_text_size(char* txt,int max_width, int vspace, int warp, int* _w, int* _h) {
705 int l = 1, i = 0;
706 int w = 0;
708 render_txt(txt);
709 while (*txt) {
710 int c=utf8_get_char((const char**)&txt);
711 if(c == '\n' || (warp && i + vo_font->width[c] >= max_width)) {
712 i -= vo_font->charspace;
713 if (i > w) w = i;
714 if(*txt)
715 l++;
716 i = 0;
717 if(c == '\n') continue;
719 i += vo_font->width[c]+vo_font->charspace;
721 if (i > 0) {
722 i -= vo_font->charspace;
723 if (i > w) w = i;
726 *_w = w;
727 *_h = (l-1) * (vo_font->height + vspace) + vo_font->height;
731 int menu_text_num_lines(char* txt, int max_width) {
732 int l = 1, i = 0;
733 render_txt(txt);
734 while (*txt) {
735 int c=utf8_get_char((const char**)&txt);
736 if(c == '\n' || i + vo_font->width[c] > max_width) {
737 l++;
738 i = 0;
739 if(c == '\n') continue;
741 i += vo_font->width[c]+vo_font->charspace;
743 return l;
747 void menu_draw_box(mp_image_t* mpi,unsigned char grey,unsigned char alpha, int x, int y, int w, int h) {
748 draw_alpha_f draw_alpha = get_draw_alpha(mpi->imgfmt);
749 int g;
751 if(!draw_alpha) {
752 mp_tmsg(MSGT_GLOBAL,MSGL_WARN,"[MENU] Unsupported output format!!!!\n");
753 return;
756 if(x > mpi->w || y > mpi->h) return;
758 if(x < 0) w += x, x = 0;
759 if(x+w > mpi->w) w = mpi->w-x;
760 if(y < 0) h += y, y = 0;
761 if(y+h > mpi->h) h = mpi->h-y;
763 g = ((256-alpha)*grey)>>8;
764 if(g < 1) g = 1;
767 int stride = (w+7)&(~7); // round to 8
768 char pic[stride*h],pic_alpha[stride*h];
769 memset(pic,g,stride*h);
770 memset(pic_alpha,alpha,stride*h);
771 draw_alpha(w,h,pic,pic_alpha,stride,
772 mpi->planes[0] + y * mpi->stride[0] + x * (mpi->bpp>>3),
773 mpi->stride[0]);