Move input options to option struct
[mplayer.git] / libmenu / menu.c
blob0741f054489d937fd078e5f82ba844c96ffe841a
2 #include "config.h"
3 #include "mp_msg.h"
4 #include "help_mp.h"
6 #include <stdlib.h>
7 #include <stdio.h>
8 #include <string.h>
9 #include <fcntl.h>
10 #include <unistd.h>
12 #include "libvo/osd.h"
13 #include "libvo/font_load.h"
14 #include "libvo/sub.h"
15 #include "osdep/keycodes.h"
16 #include "asxparser.h"
17 #include "stream/stream.h"
18 #include "input/input.h"
20 #include "libmpcodecs/img_format.h"
21 #include "libmpcodecs/mp_image.h"
22 #include "m_option.h"
23 #include "m_struct.h"
24 #include "menu.h"
26 extern menu_info_t menu_info_cmdlist;
27 extern menu_info_t menu_info_chapsel;
28 extern menu_info_t menu_info_pt;
29 extern menu_info_t menu_info_filesel;
30 extern menu_info_t menu_info_txt;
31 extern menu_info_t menu_info_console;
32 extern menu_info_t menu_info_pref;
33 extern menu_info_t menu_info_dvbsel;
36 menu_info_t* menu_info_list[] = {
37 &menu_info_pt,
38 &menu_info_cmdlist,
39 &menu_info_chapsel,
40 &menu_info_filesel,
41 &menu_info_txt,
42 &menu_info_console,
43 #ifdef HAS_DVBIN_SUPPORT
44 &menu_info_dvbsel,
45 #endif
46 &menu_info_pref,
47 NULL
50 typedef struct key_cmd_s {
51 int key;
52 char *cmd;
53 } key_cmd_t;
55 typedef struct menu_cmd_bindings_s {
56 char *name;
57 key_cmd_t *bindings;
58 int binding_num;
59 struct menu_cmd_bindings_s *parent;
60 } menu_cmd_bindings_t;
62 struct menu_def_st {
63 char* name;
64 menu_info_t* type;
65 void* cfg;
66 char* args;
69 double menu_mouse_x = -1.0;
70 double menu_mouse_y = -1.0;
71 int menu_mouse_pos_updated = 0;
73 static struct MPContext *menu_ctx = NULL;
74 static struct m_config *menu_mconfig = NULL;
75 static struct input_ctx *menu_input = NULL;
76 static menu_def_t* menu_list = NULL;
77 static int menu_count = 0;
78 static menu_cmd_bindings_t *cmd_bindings = NULL;
79 static int cmd_bindings_num = 0;
82 menu_cmd_bindings_t *get_cmd_bindings(const char *name) {
83 int i;
84 for (i = 0; i < cmd_bindings_num; ++i)
85 if (!strcasecmp(cmd_bindings[i].name, name))
86 return &cmd_bindings[i];
87 return NULL;
90 static int menu_parse_config(char* buffer, struct m_config *mconfig)
92 char *element,*body, **attribs, *name;
93 menu_info_t* minfo = NULL;
94 int r,i;
95 ASX_Parser_t* parser = asx_parser_new(mconfig);
97 while(1) {
98 r = asx_get_element(parser,&buffer,&element,&body,&attribs);
99 if(r < 0) {
100 mp_msg(MSGT_GLOBAL,MSGL_WARN,MSGTR_LIBMENU_SyntaxErrorAtLine,parser->line);
101 asx_parser_free(parser);
102 return 0;
103 } else if(r == 0) {
104 asx_parser_free(parser);
105 return 1;
107 // Has it a name ?
108 name = asx_get_attrib("name",attribs);
109 if(!name) {
110 mp_msg(MSGT_GLOBAL,MSGL_WARN,MSGTR_LIBMENU_MenuDefinitionsNeedANameAttrib,parser->line);
111 free(element);
112 if(body) free(body);
113 asx_free_attribs(attribs);
114 continue;
117 if (!strcasecmp(element, "keybindings")) {
118 menu_cmd_bindings_t *bindings = cmd_bindings;
119 char *parent_bindings;
120 cmd_bindings = realloc(cmd_bindings,
121 (cmd_bindings_num+1)*sizeof(menu_cmd_bindings_t));
122 for (i = 0; i < cmd_bindings_num; ++i)
123 if (cmd_bindings[i].parent)
124 cmd_bindings[i].parent = cmd_bindings[i].parent-bindings+cmd_bindings;
125 bindings = &cmd_bindings[cmd_bindings_num];
126 memset(bindings, 0, sizeof(menu_cmd_bindings_t));
127 bindings->name = name;
128 parent_bindings = asx_get_attrib("parent",attribs);
129 if (parent_bindings) {
130 bindings->parent = get_cmd_bindings(parent_bindings);
131 free(parent_bindings);
133 free(element);
134 asx_free_attribs(attribs);
135 if (body) {
136 char *bd = body;
137 char *b, *key, *cmd;
138 int keycode;
139 for(;;) {
140 r = asx_get_element(parser,&bd,&element,&b,&attribs);
141 if(r < 0) {
142 mp_msg(MSGT_GLOBAL,MSGL_WARN,MSGTR_LIBMENU_SyntaxErrorAtLine,
143 parser->line);
144 free(body);
145 asx_parser_free(parser);
146 return 0;
148 if(r == 0)
149 break;
150 if (!strcasecmp(element, "binding")) {
151 key = asx_get_attrib("key",attribs);
152 cmd = asx_get_attrib("cmd",attribs);
153 if (key && (keycode = mp_input_get_key_from_name(key)) >= 0) {
154 keycode &= ~MP_NO_REPEAT_KEY;
155 mp_msg(MSGT_GLOBAL,MSGL_V,
156 "[libmenu] got keybinding element %d %s=>[%s].\n",
157 keycode, key, cmd ? cmd : "");
158 bindings->bindings = realloc(bindings->bindings,
159 (bindings->binding_num+1)*sizeof(key_cmd_t));
160 bindings->bindings[bindings->binding_num].key = keycode;
161 bindings->bindings[bindings->binding_num].cmd = cmd;
162 ++bindings->binding_num;
164 else
165 free(cmd);
166 free(key);
168 free(element);
169 asx_free_attribs(attribs);
170 free(b);
172 free(body);
174 ++cmd_bindings_num;
175 continue;
177 // Try to find this menu type in our list
178 for(i = 0, minfo = NULL ; menu_info_list[i] ; i++) {
179 if(strcasecmp(element,menu_info_list[i]->name) == 0) {
180 minfo = menu_info_list[i];
181 break;
184 // Got it : add this to our list
185 if(minfo) {
186 menu_list = realloc(menu_list,(menu_count+2)*sizeof(menu_def_t));
187 menu_list[menu_count].name = name;
188 menu_list[menu_count].type = minfo;
189 menu_list[menu_count].cfg = m_struct_alloc(&minfo->priv_st);
190 menu_list[menu_count].args = body;
191 // Setup the attribs
192 for(i = 0 ; attribs[2*i] ; i++) {
193 if(strcasecmp(attribs[2*i],"name") == 0) continue;
194 if(!m_struct_set(&minfo->priv_st,menu_list[menu_count].cfg,attribs[2*i], attribs[2*i+1]))
195 mp_msg(MSGT_GLOBAL,MSGL_WARN,MSGTR_LIBMENU_BadAttrib,attribs[2*i],attribs[2*i+1],
196 name,parser->line);
198 menu_count++;
199 memset(&menu_list[menu_count],0,sizeof(menu_def_t));
200 } else {
201 mp_msg(MSGT_GLOBAL,MSGL_WARN,MSGTR_LIBMENU_UnknownMenuType,element,parser->line);
202 free(name);
203 if(body) free(body);
206 free(element);
207 asx_free_attribs(attribs);
213 /// This will build the menu_defs list from the cfg file
214 #define BUF_STEP 1024
215 #define BUF_MIN 128
216 #define BUF_MAX BUF_STEP*1024
217 int menu_init(struct MPContext *mpctx, struct m_config *mconfig,
218 struct input_ctx *input_ctx, char* cfg_file)
220 char* buffer = NULL;
221 int bl = BUF_STEP, br = 0;
222 int f, fd;
223 #ifndef HAVE_FREETYPE
224 if(vo_font == NULL)
225 return 0;
226 #endif
227 fd = open(cfg_file, O_RDONLY);
228 if(fd < 0) {
229 mp_msg(MSGT_GLOBAL,MSGL_WARN,MSGTR_LIBMENU_CantOpenConfigFile,cfg_file);
230 return 0;
232 buffer = malloc(bl);
233 while(1) {
234 int r;
235 if(bl - br < BUF_MIN) {
236 if(bl >= BUF_MAX) {
237 mp_msg(MSGT_GLOBAL,MSGL_WARN,MSGTR_LIBMENU_ConfigFileIsTooBig,BUF_MAX/1024);
238 close(fd);
239 free(buffer);
240 return 0;
242 bl += BUF_STEP;
243 buffer = realloc(buffer,bl);
245 r = read(fd,buffer+br,bl-br);
246 if(r == 0) break;
247 br += r;
249 if(!br) {
250 mp_msg(MSGT_GLOBAL,MSGL_WARN,MSGTR_LIBMENU_ConfigFileIsEmpty);
251 return 0;
253 buffer[br-1] = '\0';
255 close(fd);
257 menu_ctx = mpctx;
258 menu_mconfig = mconfig;
259 menu_input = input_ctx;
260 f = menu_parse_config(buffer, mconfig);
261 free(buffer);
262 return f;
265 // Destroy all this stuff
266 void menu_uninit(void) {
267 int i;
268 for(i = 0 ; menu_list && menu_list[i].name ; i++) {
269 free(menu_list[i].name);
270 m_struct_free(&menu_list[i].type->priv_st,menu_list[i].cfg);
271 if(menu_list[i].args) free(menu_list[i].args);
273 free(menu_list);
274 menu_count = 0;
275 for (i = 0; i < cmd_bindings_num; ++i) {
276 free(cmd_bindings[i].name);
277 while(cmd_bindings[i].binding_num > 0)
278 free(cmd_bindings[i].bindings[--cmd_bindings[i].binding_num].cmd);
279 free(cmd_bindings[i].bindings);
281 free(cmd_bindings);
284 /// Default read_key function
285 int menu_dflt_read_key(menu_t* menu,int cmd) {
286 int i;
287 menu_cmd_bindings_t *bindings = get_cmd_bindings(menu->type->name);
288 if (!bindings)
289 bindings = get_cmd_bindings(menu->type->type->name);
290 if (!bindings)
291 bindings = get_cmd_bindings("default");
292 while (bindings) {
293 for (i = 0; i < bindings->binding_num; ++i) {
294 if (bindings->bindings[i].key == cmd) {
295 if (bindings->bindings[i].cmd)
296 mp_input_parse_and_queue_cmds(menu->input_ctx,
297 bindings->bindings[i].cmd);
298 return 1;
301 bindings = bindings->parent;
303 return 0;
306 menu_t* menu_open(char *name) {
307 menu_t* m;
308 int i;
310 for(i = 0 ; menu_list[i].name != NULL ; i++) {
311 if(strcmp(name,menu_list[i].name) == 0)
312 break;
314 if(menu_list[i].name == NULL) {
315 mp_msg(MSGT_GLOBAL,MSGL_WARN,MSGTR_LIBMENU_MenuNotFound,name);
316 return NULL;
318 m = calloc(1,sizeof(menu_t));
319 m->priv_st = &(menu_list[i].type->priv_st);
320 m->priv = m_struct_copy(m->priv_st,menu_list[i].cfg);
321 m->ctx = menu_ctx;
322 m->mconfig = menu_mconfig;
323 m->input_ctx = menu_input;
324 m->type = &menu_list[i];
325 if(menu_list[i].type->open(m,menu_list[i].args))
326 return m;
327 if(m->priv)
328 m_struct_free(m->priv_st,m->priv);
329 free(m);
330 mp_msg(MSGT_GLOBAL,MSGL_WARN,MSGTR_LIBMENU_MenuInitFailed,name);
331 return NULL;
334 void menu_draw(menu_t* menu,mp_image_t* mpi) {
335 if(menu->show && menu->draw)
336 menu->draw(menu,mpi);
339 void menu_update_mouse_pos(double x, double y) {
340 menu_mouse_x = x;
341 menu_mouse_y = y;
342 menu_mouse_pos_updated = 1;
345 void menu_read_cmd(menu_t* menu,int cmd) {
346 if(menu->read_cmd)
347 menu->read_cmd(menu,cmd);
350 void menu_close(menu_t* menu) {
351 if(menu->close)
352 menu->close(menu);
353 if(menu->priv)
354 m_struct_free(menu->priv_st,menu->priv);
355 free(menu);
358 int menu_read_key(menu_t* menu,int cmd) {
359 if(menu->read_key)
360 return menu->read_key(menu,cmd);
361 else
362 return menu_dflt_read_key(menu,cmd);
365 ///////////////////////////// Helpers ////////////////////////////////////
367 typedef void (*draw_alpha_f)(int w,int h, unsigned char* src, unsigned char *srca, int srcstride, unsigned char* dstbase,int dststride);
369 inline static draw_alpha_f get_draw_alpha(uint32_t fmt) {
370 switch(fmt) {
371 case IMGFMT_BGR15:
372 case IMGFMT_RGB15:
373 return vo_draw_alpha_rgb15;
374 case IMGFMT_BGR16:
375 case IMGFMT_RGB16:
376 return vo_draw_alpha_rgb16;
377 case IMGFMT_BGR24:
378 case IMGFMT_RGB24:
379 return vo_draw_alpha_rgb24;
380 case IMGFMT_BGR32:
381 case IMGFMT_RGB32:
382 return vo_draw_alpha_rgb32;
383 case IMGFMT_YV12:
384 case IMGFMT_I420:
385 case IMGFMT_IYUV:
386 case IMGFMT_YVU9:
387 case IMGFMT_IF09:
388 case IMGFMT_Y800:
389 case IMGFMT_Y8:
390 return vo_draw_alpha_yv12;
391 case IMGFMT_YUY2:
392 return vo_draw_alpha_yuy2;
393 case IMGFMT_UYVY:
394 return vo_draw_alpha_uyvy;
397 return NULL;
400 // return the real height of a char:
401 static inline int get_height(int c,int h){
402 int font;
403 if ((font=vo_font->font[c])>=0)
404 if(h<vo_font->pic_a[font]->h) h=vo_font->pic_a[font]->h;
405 return h;
408 static void render_txt(char *txt)
410 while (*txt) {
411 int c = utf8_get_char((const char**)&txt);
412 render_one_glyph(vo_font, c);
416 #ifdef USE_FRIBIDI
417 #include <fribidi/fribidi.h>
418 #include "libavutil/common.h"
419 char *menu_fribidi_charset = NULL;
420 int menu_flip_hebrew = 0;
421 int menu_fribidi_flip_commas = 0;
423 static char *menu_fribidi(char *txt)
425 static int char_set_num = -1;
426 static FriBidiChar *logical, *visual;
427 static size_t buffer_size = 1024;
428 static char *outputstr;
430 FriBidiCharType base;
431 fribidi_boolean log2vis;
432 size_t len;
434 if (menu_flip_hebrew) {
435 len = strlen(txt);
436 if (char_set_num == -1) {
437 fribidi_set_mirroring (1);
438 fribidi_set_reorder_nsm (0);
439 char_set_num = fribidi_parse_charset("UTF-8");
440 buffer_size = FFMAX(1024,len+1);
441 logical = malloc(buffer_size);
442 visual = malloc(buffer_size);
443 outputstr = malloc(buffer_size);
444 } else if (len+1 > buffer_size) {
445 buffer_size = len+1;
446 logical = realloc(logical, buffer_size);
447 visual = realloc(visual, buffer_size);
448 outputstr = realloc(outputstr, buffer_size);
450 len = fribidi_charset_to_unicode (char_set_num, txt, len, logical);
451 base = menu_fribidi_flip_commas?FRIBIDI_TYPE_ON:FRIBIDI_TYPE_L;
452 log2vis = fribidi_log2vis (logical, len, &base, visual, NULL, NULL, NULL);
453 if (log2vis) {
454 len = fribidi_remove_bidi_marks (visual, len, NULL, NULL, NULL);
455 fribidi_unicode_to_charset (char_set_num, visual, len, outputstr);
456 return outputstr;
459 return txt;
461 #endif
463 void menu_draw_text(mp_image_t* mpi,char* txt, int x, int y) {
464 draw_alpha_f draw_alpha = get_draw_alpha(mpi->imgfmt);
465 int font;
467 if(!draw_alpha) {
468 mp_msg(MSGT_GLOBAL,MSGL_WARN,MSGTR_LIBMENU_UnsupportedOutformat);
469 return;
472 #ifdef USE_FRIBIDI
473 txt = menu_fribidi(txt);
474 #endif
475 render_txt(txt);
477 while (*txt) {
478 int c=utf8_get_char((const char**)&txt);
479 if ((font=vo_font->font[c])>=0 && (x + vo_font->width[c] <= mpi->w) && (y + vo_font->pic_a[font]->h <= mpi->h))
480 draw_alpha(vo_font->width[c], vo_font->pic_a[font]->h,
481 vo_font->pic_b[font]->bmp+vo_font->start[c],
482 vo_font->pic_a[font]->bmp+vo_font->start[c],
483 vo_font->pic_a[font]->w,
484 mpi->planes[0] + y * mpi->stride[0] + x * (mpi->bpp>>3),
485 mpi->stride[0]);
486 x+=vo_font->width[c]+vo_font->charspace;
491 void menu_draw_text_full(mp_image_t* mpi,char* txt,
492 int x, int y,int w, int h,
493 int vspace, int warp, int align, int anchor) {
494 int need_w,need_h;
495 int sy, ymin, ymax;
496 int sx, xmin, xmax, xmid, xrmin;
497 int ll = 0;
498 int font;
499 draw_alpha_f draw_alpha = get_draw_alpha(mpi->imgfmt);
501 if(!draw_alpha) {
502 mp_msg(MSGT_GLOBAL,MSGL_WARN,MSGTR_LIBMENU_UnsupportedOutformat);
503 return;
506 #ifdef USE_FRIBIDI
507 txt = menu_fribidi(txt);
508 #endif
509 render_txt(txt);
511 if(x > mpi->w || y > mpi->h)
512 return;
514 if(anchor & MENU_TEXT_VCENTER) {
515 if(h <= 0) h = mpi->h;
516 ymin = y - h/2;
517 ymax = y + h/2;
518 } else if(anchor & MENU_TEXT_BOT) {
519 if(h <= 0) h = mpi->h - y;
520 ymin = y - h;
521 ymax = y;
522 } else {
523 if(h <= 0) h = mpi->h - y;
524 ymin = y;
525 ymax = y + h;
528 if(anchor & MENU_TEXT_HCENTER) {
529 if(w <= 0) w = mpi->w;
530 xmin = x - w/2;
531 xmax = x + w/2;
532 } else if(anchor & MENU_TEXT_RIGHT) {
533 if(w <= 0) w = mpi->w -x;
534 xmin = x - w;
535 xmax = x;
536 } else {
537 if(w <= 0) w = mpi->w -x;
538 xmin = x;
539 xmax = x + w;
542 // How many space do we need to draw this ?
543 menu_text_size(txt,w,vspace,warp,&need_w,&need_h);
545 // Find the first line
546 if(align & MENU_TEXT_VCENTER)
547 sy = ymin + ((h - need_h)/2);
548 else if(align & MENU_TEXT_BOT)
549 sy = ymax - need_h - 1;
550 else
551 sy = y;
553 #if 0
554 // Find the first col
555 if(align & MENU_TEXT_HCENTER)
556 sx = xmin + ((w - need_w)/2);
557 else if(align & MENU_TEXT_RIGHT)
558 sx = xmax - need_w;
559 #endif
561 xmid = xmin + (xmax - xmin) / 2;
562 xrmin = xmin;
563 // Clamp the bb to the mpi size
564 if(ymin < 0) ymin = 0;
565 if(xmin < 0) xmin = 0;
566 if(ymax > mpi->h) ymax = mpi->h;
567 if(xmax > mpi->w) xmax = mpi->w;
569 // Jump some the beginnig text if needed
570 while(sy < ymin && *txt) {
571 int c=utf8_get_char((const char**)&txt);
572 if(c == '\n' || (warp && ll + vo_font->width[c] > w)) {
573 ll = 0;
574 sy += vo_font->height + vspace;
575 if(c == '\n') continue;
577 ll += vo_font->width[c]+vo_font->charspace;
579 if(*txt == '\0') // Nothing left to draw
580 return;
582 while(sy < ymax && *txt) {
583 char* line_end = NULL;
584 int n;
586 if(txt[0] == '\n') { // New line
587 sy += vo_font->height + vspace;
588 txt++;
589 continue;
592 // Get the length and end of this line
593 for(n = 0, ll = 0 ; txt[n] != '\0' && txt[n] != '\n' ; n++) {
594 unsigned char c = txt[n];
595 if(warp && ll + vo_font->width[c] > w) break;
596 ll += vo_font->width[c]+vo_font->charspace;
598 line_end = &txt[n];
599 ll -= vo_font->charspace;
602 if(align & (MENU_TEXT_HCENTER|MENU_TEXT_RIGHT)) {
603 // Too long line
604 if(ll > xmax-xmin) {
605 if(align & MENU_TEXT_HCENTER) {
606 int mid = ll/2;
607 // Find the middle point
608 for(n--, ll = 0 ; n <= 0 ; n--) {
609 ll += vo_font->width[(int)txt[n]]+vo_font->charspace;
610 if(ll - vo_font->charspace > mid) break;
612 ll -= vo_font->charspace;
613 sx = xmid + mid - ll;
614 } else// MENU_TEXT_RIGHT)
615 sx = xmax + vo_font->charspace;
617 // We are after the start point -> go back
618 if(sx > xmin) {
619 for(n-- ; n <= 0 ; n--) {
620 unsigned char c = txt[n];
621 if(sx - vo_font->width[c] - vo_font->charspace < xmin) break;
622 sx -= vo_font->width[c]+vo_font->charspace;
624 } else { // We are before the start point -> go forward
625 for( ; sx < xmin && (&txt[n]) != line_end ; n++) {
626 unsigned char c = txt[n];
627 sx += vo_font->width[c]+vo_font->charspace;
630 txt = &txt[n]; // Jump to the new start char
631 } else {
632 if(align & MENU_TEXT_HCENTER)
633 sx = xmid - ll/2;
634 else
635 sx = xmax - 1 - ll;
637 } else {
638 for(sx = xrmin ; sx < xmin && txt != line_end ; txt++) {
639 unsigned char c = txt[n];
640 sx += vo_font->width[c]+vo_font->charspace;
644 while(sx < xmax && txt != line_end) {
645 int c=utf8_get_char((const char**)&txt);
646 font = vo_font->font[c];
647 if(font >= 0) {
648 int cs = (vo_font->pic_a[font]->h - vo_font->height) / 2;
649 if ((sx + vo_font->width[c] <= xmax) && (sy + vo_font->height <= ymax) )
650 draw_alpha(vo_font->width[c], vo_font->height,
651 vo_font->pic_b[font]->bmp+vo_font->start[c] +
652 cs * vo_font->pic_a[font]->w,
653 vo_font->pic_a[font]->bmp+vo_font->start[c] +
654 cs * vo_font->pic_a[font]->w,
655 vo_font->pic_a[font]->w,
656 mpi->planes[0] + sy * mpi->stride[0] + sx * (mpi->bpp>>3),
657 mpi->stride[0]);
658 // else
659 //printf("Can't draw '%c'\n",c);
661 sx+=vo_font->width[c]+vo_font->charspace;
663 txt = line_end;
664 if(txt[0] == '\0') break;
665 sy += vo_font->height + vspace;
669 int menu_text_length(char* txt) {
670 int l = 0;
671 render_txt(txt);
672 while (*txt) {
673 int c=utf8_get_char((const char**)&txt);
674 l += vo_font->width[c]+vo_font->charspace;
676 return l - vo_font->charspace;
679 void menu_text_size(char* txt,int max_width, int vspace, int warp, int* _w, int* _h) {
680 int l = 1, i = 0;
681 int w = 0;
683 render_txt(txt);
684 while (*txt) {
685 int c=utf8_get_char((const char**)&txt);
686 if(c == '\n' || (warp && i + vo_font->width[c] >= max_width)) {
687 i -= vo_font->charspace;
688 if (i > w) w = i;
689 if(*txt)
690 l++;
691 i = 0;
692 if(c == '\n') continue;
694 i += vo_font->width[c]+vo_font->charspace;
696 if (i > 0) {
697 i -= vo_font->charspace;
698 if (i > w) w = i;
701 *_w = w;
702 *_h = (l-1) * (vo_font->height + vspace) + vo_font->height;
706 int menu_text_num_lines(char* txt, int max_width) {
707 int l = 1, i = 0;
708 render_txt(txt);
709 while (*txt) {
710 int c=utf8_get_char((const char**)&txt);
711 if(c == '\n' || i + vo_font->width[c] > max_width) {
712 l++;
713 i = 0;
714 if(c == '\n') continue;
716 i += vo_font->width[c]+vo_font->charspace;
718 return l;
721 char* menu_text_get_next_line(char* txt, int max_width) {
722 int i = 0;
723 render_txt(txt);
724 while (*txt) {
725 int c=utf8_get_char((const char**)&txt);
726 if(c == '\n') {
727 txt++;
728 break;
730 i += vo_font->width[c];
731 if(i >= max_width)
732 break;
733 i += vo_font->charspace;
735 return txt;
739 void menu_draw_box(mp_image_t* mpi,unsigned char grey,unsigned char alpha, int x, int y, int w, int h) {
740 draw_alpha_f draw_alpha = get_draw_alpha(mpi->imgfmt);
741 int g;
743 if(!draw_alpha) {
744 mp_msg(MSGT_GLOBAL,MSGL_WARN,MSGTR_LIBMENU_UnsupportedOutformat);
745 return;
748 if(x > mpi->w || y > mpi->h) return;
750 if(x < 0) w += x, x = 0;
751 if(x+w > mpi->w) w = mpi->w-x;
752 if(y < 0) h += y, y = 0;
753 if(y+h > mpi->h) h = mpi->h-y;
755 g = ((256-alpha)*grey)>>8;
756 if(g < 1) g = 1;
759 int stride = (w+7)&(~7); // round to 8
760 char pic[stride*h],pic_alpha[stride*h];
761 memset(pic,g,stride*h);
762 memset(pic_alpha,alpha,stride*h);
763 draw_alpha(w,h,pic,pic_alpha,stride,
764 mpi->planes[0] + y * mpi->stride[0] + x * (mpi->bpp>>3),
765 mpi->stride[0]);