Fix vf_tcdump's compilation
[mplayer/kovensky.git] / libmenu / menu.c
blobc80f7a0adfafb28861ab195cadc789d64bbf5ca7
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 "libvo/font_load.h"
30 #include "libvo/sub.h"
31 #include "osdep/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 if(body) 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,attribs[2*i], attribs[2*i+1]))
212 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],
213 name,parser->line);
215 menu_count++;
216 memset(&menu_list[menu_count],0,sizeof(menu_def_t));
217 } else {
218 mp_tmsg(MSGT_GLOBAL,MSGL_WARN,"[MENU] unknown menu type '%s' at line %d\n",element,parser->line);
219 free(name);
220 if(body) free(body);
223 free(element);
224 asx_free_attribs(attribs);
230 /// This will build the menu_defs list from the cfg file
231 #define BUF_STEP 1024
232 #define BUF_MIN 128
233 #define BUF_MAX BUF_STEP*1024
234 int menu_init(struct MPContext *mpctx, struct m_config *mconfig,
235 struct input_ctx *input_ctx, char* cfg_file)
237 char* buffer = NULL;
238 int bl = BUF_STEP, br = 0;
239 int f, fd;
240 #ifndef CONFIG_FREETYPE
241 if(vo_font == NULL)
242 return 0;
243 #endif
244 fd = open(cfg_file, O_RDONLY);
245 if(fd < 0) {
246 mp_tmsg(MSGT_GLOBAL,MSGL_WARN,"[MENU] Can't open menu config file: %s\n",cfg_file);
247 return 0;
249 buffer = malloc(bl);
250 while(1) {
251 int r;
252 if(bl - br < BUF_MIN) {
253 if(bl >= BUF_MAX) {
254 mp_tmsg(MSGT_GLOBAL,MSGL_WARN,"[MENU] Config file is too big (> %d KB)\n",BUF_MAX/1024);
255 close(fd);
256 free(buffer);
257 return 0;
259 bl += BUF_STEP;
260 buffer = realloc(buffer,bl);
262 r = read(fd,buffer+br,bl-br);
263 if(r == 0) break;
264 br += r;
266 if(!br) {
267 mp_tmsg(MSGT_GLOBAL,MSGL_WARN,"[MENU] Config file is empty.\n");
268 return 0;
270 buffer[br-1] = '\0';
272 close(fd);
274 menu_ctx = mpctx;
275 menu_mconfig = mconfig;
276 menu_input = input_ctx;
277 f = menu_parse_config(buffer, mconfig);
278 free(buffer);
279 return f;
282 // Destroy all this stuff
283 void menu_uninit(void) {
284 int i;
285 for(i = 0 ; menu_list && menu_list[i].name ; i++) {
286 free(menu_list[i].name);
287 m_struct_free(&menu_list[i].type->priv_st,menu_list[i].cfg);
288 if(menu_list[i].args) free(menu_list[i].args);
290 free(menu_list);
291 menu_count = 0;
292 for (i = 0; i < cmd_bindings_num; ++i) {
293 free(cmd_bindings[i].name);
294 while(cmd_bindings[i].binding_num > 0)
295 free(cmd_bindings[i].bindings[--cmd_bindings[i].binding_num].cmd);
296 free(cmd_bindings[i].bindings);
298 free(cmd_bindings);
301 /// Default read_key function
302 int menu_dflt_read_key(menu_t* menu,int cmd) {
303 int i;
304 menu_cmd_bindings_t *bindings = get_cmd_bindings(menu->type->name);
305 if (!bindings)
306 bindings = get_cmd_bindings(menu->type->type->name);
307 if (!bindings)
308 bindings = get_cmd_bindings("default");
309 while (bindings) {
310 for (i = 0; i < bindings->binding_num; ++i) {
311 if (bindings->bindings[i].key == cmd) {
312 if (bindings->bindings[i].cmd)
313 mp_input_parse_and_queue_cmds(menu->input_ctx,
314 bindings->bindings[i].cmd);
315 return 1;
318 bindings = bindings->parent;
320 return 0;
323 menu_t* menu_open(char *name) {
324 menu_t* m;
325 int i;
327 for(i = 0 ; menu_list[i].name != NULL ; i++) {
328 if(strcmp(name,menu_list[i].name) == 0)
329 break;
331 if(menu_list[i].name == NULL) {
332 mp_tmsg(MSGT_GLOBAL,MSGL_WARN,"[MENU] Menu %s not found.\n",name);
333 return NULL;
335 m = calloc(1,sizeof(menu_t));
336 m->priv_st = &(menu_list[i].type->priv_st);
337 m->priv = m_struct_copy(m->priv_st,menu_list[i].cfg);
338 m->ctx = menu_ctx;
339 m->mconfig = menu_mconfig;
340 m->input_ctx = menu_input;
341 m->type = &menu_list[i];
342 if(menu_list[i].type->open(m,menu_list[i].args))
343 return m;
344 if(m->priv)
345 m_struct_free(m->priv_st,m->priv);
346 free(m);
347 mp_tmsg(MSGT_GLOBAL,MSGL_WARN,"[MENU] Menu '%s': Init failed.\n",name);
348 return NULL;
351 void menu_draw(menu_t* menu,mp_image_t* mpi) {
352 if(menu->show && menu->draw)
353 menu->draw(menu,mpi);
356 void menu_update_mouse_pos(double x, double y) {
357 menu_mouse_x = x;
358 menu_mouse_y = y;
359 menu_mouse_pos_updated = 1;
362 void menu_read_cmd(menu_t* menu,int cmd) {
363 if(menu->read_cmd)
364 menu->read_cmd(menu,cmd);
367 void menu_close(menu_t* menu) {
368 if(menu->close)
369 menu->close(menu);
370 if(menu->priv)
371 m_struct_free(menu->priv_st,menu->priv);
372 free(menu);
375 int menu_read_key(menu_t* menu,int cmd) {
376 if(menu->read_key)
377 return menu->read_key(menu,cmd);
378 else
379 return menu_dflt_read_key(menu,cmd);
382 ///////////////////////////// Helpers ////////////////////////////////////
384 typedef void (*draw_alpha_f)(int w,int h, unsigned char* src, unsigned char *srca, int srcstride, unsigned char* dstbase,int dststride);
386 inline static draw_alpha_f get_draw_alpha(uint32_t fmt) {
387 switch(fmt) {
388 case IMGFMT_BGR15:
389 case IMGFMT_RGB15:
390 return vo_draw_alpha_rgb15;
391 case IMGFMT_BGR16:
392 case IMGFMT_RGB16:
393 return vo_draw_alpha_rgb16;
394 case IMGFMT_BGR24:
395 case IMGFMT_RGB24:
396 return vo_draw_alpha_rgb24;
397 case IMGFMT_BGR32:
398 case IMGFMT_RGB32:
399 return vo_draw_alpha_rgb32;
400 case IMGFMT_YV12:
401 case IMGFMT_I420:
402 case IMGFMT_IYUV:
403 case IMGFMT_YVU9:
404 case IMGFMT_IF09:
405 case IMGFMT_Y800:
406 case IMGFMT_Y8:
407 return vo_draw_alpha_yv12;
408 case IMGFMT_YUY2:
409 return vo_draw_alpha_yuy2;
410 case IMGFMT_UYVY:
411 return vo_draw_alpha_uyvy;
414 return NULL;
417 // return the real height of a char:
418 static inline int get_height(int c,int h){
419 int font;
420 if ((font=vo_font->font[c])>=0)
421 if(h<vo_font->pic_a[font]->h) h=vo_font->pic_a[font]->h;
422 return h;
425 static void render_txt(char *txt)
427 while (*txt) {
428 int c = utf8_get_char((const char**)&txt);
429 render_one_glyph(vo_font, c);
433 #ifdef CONFIG_FRIBIDI
434 #include <fribidi/fribidi.h>
435 #include "libavutil/common.h"
436 char *menu_fribidi_charset = NULL;
437 int menu_flip_hebrew = 0;
438 int menu_fribidi_flip_commas = 0;
440 static char *menu_fribidi(char *txt)
442 static int char_set_num = -1;
443 static FriBidiChar *logical, *visual;
444 static size_t buffer_size = 1024;
445 static char *outputstr;
447 FriBidiCharType base;
448 fribidi_boolean log2vis;
449 size_t len;
451 if (menu_flip_hebrew) {
452 len = strlen(txt);
453 if (char_set_num == -1) {
454 fribidi_set_mirroring (1);
455 fribidi_set_reorder_nsm (0);
456 char_set_num = fribidi_parse_charset("UTF-8");
457 buffer_size = FFMAX(1024,len+1);
458 logical = malloc(buffer_size);
459 visual = malloc(buffer_size);
460 outputstr = malloc(buffer_size);
461 } else if (len+1 > buffer_size) {
462 buffer_size = len+1;
463 logical = realloc(logical, buffer_size);
464 visual = realloc(visual, buffer_size);
465 outputstr = realloc(outputstr, buffer_size);
467 len = fribidi_charset_to_unicode (char_set_num, txt, len, logical);
468 base = menu_fribidi_flip_commas?FRIBIDI_TYPE_ON:FRIBIDI_TYPE_L;
469 log2vis = fribidi_log2vis (logical, len, &base, visual, NULL, NULL, NULL);
470 if (log2vis) {
471 len = fribidi_remove_bidi_marks (visual, len, NULL, NULL, NULL);
472 fribidi_unicode_to_charset (char_set_num, visual, len, outputstr);
473 return outputstr;
476 return txt;
478 #endif
480 void menu_draw_text(mp_image_t* mpi,char* txt, int x, int y) {
481 draw_alpha_f draw_alpha = get_draw_alpha(mpi->imgfmt);
482 int font;
484 if(!draw_alpha) {
485 mp_tmsg(MSGT_GLOBAL,MSGL_WARN,"[MENU] Unsupported output format!!!!\n");
486 return;
489 #ifdef CONFIG_FRIBIDI
490 txt = menu_fribidi(txt);
491 #endif
492 render_txt(txt);
494 while (*txt) {
495 int c=utf8_get_char((const char**)&txt);
496 if ((font=vo_font->font[c])>=0 && (x + vo_font->width[c] <= mpi->w) && (y + vo_font->pic_a[font]->h <= mpi->h))
497 draw_alpha(vo_font->width[c], vo_font->pic_a[font]->h,
498 vo_font->pic_b[font]->bmp+vo_font->start[c],
499 vo_font->pic_a[font]->bmp+vo_font->start[c],
500 vo_font->pic_a[font]->w,
501 mpi->planes[0] + y * mpi->stride[0] + x * (mpi->bpp>>3),
502 mpi->stride[0]);
503 x+=vo_font->width[c]+vo_font->charspace;
508 void menu_draw_text_full(mp_image_t* mpi,char* txt,
509 int x, int y,int w, int h,
510 int vspace, int warp, int align, int anchor) {
511 int need_w,need_h;
512 int sy, ymin, ymax;
513 int sx, xmin, xmax, xmid, xrmin;
514 int ll = 0;
515 int font;
516 draw_alpha_f draw_alpha = get_draw_alpha(mpi->imgfmt);
518 if(!draw_alpha) {
519 mp_tmsg(MSGT_GLOBAL,MSGL_WARN,"[MENU] Unsupported output format!!!!\n");
520 return;
523 #ifdef CONFIG_FRIBIDI
524 txt = menu_fribidi(txt);
525 #endif
526 render_txt(txt);
528 if(x > mpi->w || y > mpi->h)
529 return;
531 if(anchor & MENU_TEXT_VCENTER) {
532 if(h <= 0) h = mpi->h;
533 ymin = y - h/2;
534 ymax = y + h/2;
535 } else if(anchor & MENU_TEXT_BOT) {
536 if(h <= 0) h = mpi->h - y;
537 ymin = y - h;
538 ymax = y;
539 } else {
540 if(h <= 0) h = mpi->h - y;
541 ymin = y;
542 ymax = y + h;
545 if(anchor & MENU_TEXT_HCENTER) {
546 if(w <= 0) w = mpi->w;
547 xmin = x - w/2;
548 xmax = x + w/2;
549 } else if(anchor & MENU_TEXT_RIGHT) {
550 if(w <= 0) w = mpi->w -x;
551 xmin = x - w;
552 xmax = x;
553 } else {
554 if(w <= 0) w = mpi->w -x;
555 xmin = x;
556 xmax = x + w;
559 // How many space do we need to draw this ?
560 menu_text_size(txt,w,vspace,warp,&need_w,&need_h);
562 // Find the first line
563 if(align & MENU_TEXT_VCENTER)
564 sy = ymin + ((h - need_h)/2);
565 else if(align & MENU_TEXT_BOT)
566 sy = ymax - need_h - 1;
567 else
568 sy = y;
570 #if 0
571 // Find the first col
572 if(align & MENU_TEXT_HCENTER)
573 sx = xmin + ((w - need_w)/2);
574 else if(align & MENU_TEXT_RIGHT)
575 sx = xmax - need_w;
576 #endif
578 xmid = xmin + (xmax - xmin) / 2;
579 xrmin = xmin;
580 // Clamp the bb to the mpi size
581 if(ymin < 0) ymin = 0;
582 if(xmin < 0) xmin = 0;
583 if(ymax > mpi->h) ymax = mpi->h;
584 if(xmax > mpi->w) xmax = mpi->w;
586 // Jump some the beginnig text if needed
587 while(sy < ymin && *txt) {
588 int c=utf8_get_char((const char**)&txt);
589 if(c == '\n' || (warp && ll + vo_font->width[c] > w)) {
590 ll = 0;
591 sy += vo_font->height + vspace;
592 if(c == '\n') continue;
594 ll += vo_font->width[c]+vo_font->charspace;
596 if(*txt == '\0') // Nothing left to draw
597 return;
599 while(sy < ymax && *txt) {
600 char* line_end = NULL;
601 int n;
603 if(txt[0] == '\n') { // New line
604 sy += vo_font->height + vspace;
605 txt++;
606 continue;
609 // Get the length and end of this line
610 for(n = 0, ll = 0 ; txt[n] != '\0' && txt[n] != '\n' ; n++) {
611 unsigned char c = txt[n];
612 if(warp && ll + vo_font->width[c] > w) break;
613 ll += vo_font->width[c]+vo_font->charspace;
615 line_end = &txt[n];
616 ll -= vo_font->charspace;
619 if(align & (MENU_TEXT_HCENTER|MENU_TEXT_RIGHT)) {
620 // Too long line
621 if(ll > xmax-xmin) {
622 if(align & MENU_TEXT_HCENTER) {
623 int mid = ll/2;
624 // Find the middle point
625 for(n--, ll = 0 ; n <= 0 ; n--) {
626 ll += vo_font->width[(int)txt[n]]+vo_font->charspace;
627 if(ll - vo_font->charspace > mid) break;
629 ll -= vo_font->charspace;
630 sx = xmid + mid - ll;
631 } else// MENU_TEXT_RIGHT)
632 sx = xmax + vo_font->charspace;
634 // We are after the start point -> go back
635 if(sx > xmin) {
636 for(n-- ; n <= 0 ; n--) {
637 unsigned char c = txt[n];
638 if(sx - vo_font->width[c] - vo_font->charspace < xmin) break;
639 sx -= vo_font->width[c]+vo_font->charspace;
641 } else { // We are before the start point -> go forward
642 for( ; sx < xmin && (&txt[n]) != line_end ; n++) {
643 unsigned char c = txt[n];
644 sx += vo_font->width[c]+vo_font->charspace;
647 txt = &txt[n]; // Jump to the new start char
648 } else {
649 if(align & MENU_TEXT_HCENTER)
650 sx = xmid - ll/2;
651 else
652 sx = xmax - 1 - ll;
654 } else {
655 for(sx = xrmin ; sx < xmin && txt != line_end ; txt++) {
656 unsigned char c = txt[n];
657 sx += vo_font->width[c]+vo_font->charspace;
661 while(sx < xmax && txt != line_end) {
662 int c=utf8_get_char((const char**)&txt);
663 font = vo_font->font[c];
664 if(font >= 0) {
665 int cs = (vo_font->pic_a[font]->h - vo_font->height) / 2;
666 if ((sx + vo_font->width[c] <= xmax) && (sy + vo_font->height <= ymax) )
667 draw_alpha(vo_font->width[c], vo_font->height,
668 vo_font->pic_b[font]->bmp+vo_font->start[c] +
669 cs * vo_font->pic_a[font]->w,
670 vo_font->pic_a[font]->bmp+vo_font->start[c] +
671 cs * vo_font->pic_a[font]->w,
672 vo_font->pic_a[font]->w,
673 mpi->planes[0] + sy * mpi->stride[0] + sx * (mpi->bpp>>3),
674 mpi->stride[0]);
675 // else
676 //printf("Can't draw '%c'\n",c);
678 sx+=vo_font->width[c]+vo_font->charspace;
680 txt = line_end;
681 if(txt[0] == '\0') break;
682 sy += vo_font->height + vspace;
686 int menu_text_length(char* txt) {
687 int l = 0;
688 render_txt(txt);
689 while (*txt) {
690 int c=utf8_get_char((const char**)&txt);
691 l += vo_font->width[c]+vo_font->charspace;
693 return l - vo_font->charspace;
696 void menu_text_size(char* txt,int max_width, int vspace, int warp, int* _w, int* _h) {
697 int l = 1, i = 0;
698 int w = 0;
700 render_txt(txt);
701 while (*txt) {
702 int c=utf8_get_char((const char**)&txt);
703 if(c == '\n' || (warp && i + vo_font->width[c] >= max_width)) {
704 i -= vo_font->charspace;
705 if (i > w) w = i;
706 if(*txt)
707 l++;
708 i = 0;
709 if(c == '\n') continue;
711 i += vo_font->width[c]+vo_font->charspace;
713 if (i > 0) {
714 i -= vo_font->charspace;
715 if (i > w) w = i;
718 *_w = w;
719 *_h = (l-1) * (vo_font->height + vspace) + vo_font->height;
723 int menu_text_num_lines(char* txt, int max_width) {
724 int l = 1, i = 0;
725 render_txt(txt);
726 while (*txt) {
727 int c=utf8_get_char((const char**)&txt);
728 if(c == '\n' || i + vo_font->width[c] > max_width) {
729 l++;
730 i = 0;
731 if(c == '\n') continue;
733 i += vo_font->width[c]+vo_font->charspace;
735 return l;
738 static char* menu_text_get_next_line(char* txt, int max_width)
740 int i = 0;
741 render_txt(txt);
742 while (*txt) {
743 int c=utf8_get_char((const char**)&txt);
744 if(c == '\n') {
745 txt++;
746 break;
748 i += vo_font->width[c];
749 if(i >= max_width)
750 break;
751 i += vo_font->charspace;
753 return txt;
757 void menu_draw_box(mp_image_t* mpi,unsigned char grey,unsigned char alpha, int x, int y, int w, int h) {
758 draw_alpha_f draw_alpha = get_draw_alpha(mpi->imgfmt);
759 int g;
761 if(!draw_alpha) {
762 mp_tmsg(MSGT_GLOBAL,MSGL_WARN,"[MENU] Unsupported output format!!!!\n");
763 return;
766 if(x > mpi->w || y > mpi->h) return;
768 if(x < 0) w += x, x = 0;
769 if(x+w > mpi->w) w = mpi->w-x;
770 if(y < 0) h += y, y = 0;
771 if(y+h > mpi->h) h = mpi->h-y;
773 g = ((256-alpha)*grey)>>8;
774 if(g < 1) g = 1;
777 int stride = (w+7)&(~7); // round to 8
778 char pic[stride*h],pic_alpha[stride*h];
779 memset(pic,g,stride*h);
780 memset(pic_alpha,alpha,stride*h);
781 draw_alpha(w,h,pic,pic_alpha,stride,
782 mpi->planes[0] + y * mpi->stride[0] + x * (mpi->bpp>>3),
783 mpi->stride[0]);