9 #include "libmpcodecs/img_format.h"
10 #include "libmpcodecs/mp_image.h"
15 #include "libvo/font_load.h"
16 #include "osdep/keycodes.h"
19 #include "menu_list.h"
21 #define mpriv (menu->priv)
23 void menu_list_draw(menu_t
* menu
,mp_image_t
* mpi
) {
31 int need_h
= 0,need_w
= 0,ptr_l
,sidx
= 0;
40 if(h
<= 0) h
= mpi
->height
;
41 if(w
<= 0) w
= mpi
->width
;
42 dh
= h
- 2*mpriv
->minb
;
43 dw
= w
- 2*mpriv
->minb
;
44 ptr_l
= mpriv
->ptr
? menu_text_length(mpriv
->ptr
) : 0;
46 if(h
- vo_font
->height
<= 0 || w
- ptr_l
<= 0 || dw
<= 0 || dh
<= 0)
49 line_h
= mpriv
->vspace
+ vo_font
->height
;
50 th
= menu_text_num_lines(mpriv
->title
,dw
) * line_h
+ mpriv
->vspace
;
52 // the selected item is hidden, find a visible one
53 if(mpriv
->current
->hide
) {
55 for(m
= mpriv
->current
->next
; m
; m
= m
->next
)
57 if(!m
) // or the previous
58 for(m
= mpriv
->current
->prev
; m
; m
= m
->prev
)
60 if(m
) mpriv
->current
= m
;
64 for(i
= 0, m
= mpriv
->menu
; m
; m
= m
->next
, i
++) {
67 ll
= menu_text_length(m
->txt
);
68 if(ptr_l
+ ll
> need_w
) need_w
= ptr_l
+ ll
;
69 if(m
== mpriv
->current
) sidx
= i
;
72 if(need_w
> dw
) need_w
= dw
;
80 need_h
= count
* line_h
- mpriv
->vspace
;
81 if( need_h
+ th
> dh
) {
83 mpriv
->disp_lines
= (dh
+ mpriv
->vspace
- th
) / line_h
;
84 if(mpriv
->disp_lines
< 4) {
86 mpriv
->disp_lines
= (dh
+ mpriv
->vspace
) / line_h
;
89 if(mpriv
->disp_lines
< 1) return;
90 need_h
= mpriv
->disp_lines
* line_h
- mpriv
->vspace
;
92 start
= sidx
- (mpriv
->disp_lines
/2);
93 if(start
< 0) start
= 0;
94 end
= start
+ mpriv
->disp_lines
;
97 if(end
- start
< mpriv
->disp_lines
)
98 start
= end
- mpriv
->disp_lines
< 0 ? 0 : end
- mpriv
->disp_lines
;
101 for(i
= 0 ; m
->next
&& i
< start
; ) {
107 mpriv
->disp_lines
= count
;
110 bg_w
= need_w
+2*mpriv
->minb
;
113 menu_text_size(mpriv
->title
,dw
,mpriv
->vspace
,1,&tw
,&th2
);
114 if(mpriv
->title_bg
>= 0) {
115 if(tw
+2*mpriv
->minb
> bg_w
) bg_w
= tw
+2*mpriv
->minb
;
116 menu_draw_box(mpi
,mpriv
->title_bg
,mpriv
->title_bg_alpha
,
117 x
< 0 ? (mpi
->w
-bg_w
)/2 : x
-mpriv
->minb
,dy
+y
-mpriv
->vspace
/2,bg_w
,th
);
119 menu_draw_text_full(mpi
,mpriv
->title
,
120 x
< 0 ? mpi
->w
/ 2 : x
,
121 dy
+y
, x
< 0 ? dw
: (tw
> need_w
? tw
: need_w
), 0,
123 MENU_TEXT_TOP
|MENU_TEXT_HCENTER
,
124 MENU_TEXT_TOP
|(x
< 0 ? MENU_TEXT_HCENTER
:MENU_TEXT_LEFT
));
128 dx
= x
< 0 ? (mpi
->w
- need_w
) / 2 : x
;
129 bx
= x
< 0 ? (mpi
->w
- bg_w
) / 2 : x
- mpriv
->minb
;
130 for( ; m
!= NULL
&& dy
+ vo_font
->height
< dh
; m
= m
->next
) {
131 if(m
->hide
) continue;
132 if(m
== mpriv
->current
) {
133 if(mpriv
->ptr_bg
>= 0)
134 menu_draw_box(mpi
,mpriv
->ptr_bg
,mpriv
->ptr_bg_alpha
,
135 bx
, dy
+ y
- mpriv
->vspace
/ 2,
138 menu_draw_text_full(mpi
,mpriv
->ptr
,
142 MENU_TEXT_TOP
|MENU_TEXT_LEFT
,
143 MENU_TEXT_TOP
|MENU_TEXT_LEFT
);
144 } else if(mpriv
->item_bg
>= 0)
145 menu_draw_box(mpi
,mpriv
->item_bg
,mpriv
->item_bg_alpha
,
146 bx
, dy
+ y
- mpriv
->vspace
/ 2,
148 menu_draw_text_full(mpi
,m
->txt
,
150 dy
+y
,dw
-ptr_l
,dh
- dy
,
152 MENU_TEXT_TOP
|MENU_TEXT_LEFT
,
153 MENU_TEXT_TOP
|MENU_TEXT_LEFT
);
159 void menu_list_read_cmd(menu_t
* menu
,int cmd
) {
164 while(mpriv
->current
->prev
) {
165 mpriv
->current
= mpriv
->current
->prev
;
166 if(!mpriv
->current
->hide
) return;
168 for( ; mpriv
->current
->next
!= NULL
; mpriv
->current
= mpriv
->current
->next
)
170 if(!mpriv
->current
->hide
) return;
171 while(mpriv
->current
->prev
) {
172 mpriv
->current
= mpriv
->current
->prev
;
173 if(!mpriv
->current
->hide
) return;
177 while(mpriv
->current
->next
) {
178 mpriv
->current
= mpriv
->current
->next
;
179 if(!mpriv
->current
->hide
) return;
181 mpriv
->current
= mpriv
->menu
;
182 if(!mpriv
->current
->hide
) return;
183 while(mpriv
->current
->next
) {
184 mpriv
->current
= mpriv
->current
->next
;
185 if(!mpriv
->current
->hide
) return;
189 mpriv
->current
= mpriv
->menu
;
192 for(m
= mpriv
->current
; m
&& m
->next
; m
= m
->next
)
197 case MENU_CMD_PAGE_UP
:
198 for(i
= 0, m
= mpriv
->current
; m
&& m
->prev
&& i
< mpriv
->disp_lines
; m
= m
->prev
, i
++)
203 case MENU_CMD_PAGE_DOWN
:
204 for(i
= 0, m
= mpriv
->current
; m
&& m
->next
&& i
< mpriv
->disp_lines
; m
= m
->next
, i
++)
210 case MENU_CMD_CANCEL
:
217 int menu_list_jump_to_key(menu_t
* menu
,int c
) {
218 if(c
< 256 && isalnum(c
)) {
219 list_entry_t
* e
= mpriv
->current
;
220 if(e
->txt
[0] == c
) e
= e
->next
;
221 for( ; e
; e
= e
->next
) {
227 for(e
= mpriv
->menu
; e
; e
= e
->next
) {
238 void menu_list_add_entry(menu_t
* menu
,list_entry_t
* entry
) {
242 if(mpriv
->menu
== NULL
) {
243 mpriv
->menu
= mpriv
->current
= entry
;
247 for(l
= mpriv
->menu
; l
->next
!= NULL
; l
= l
->next
)
253 void menu_list_init(menu_t
* menu
) {
255 mpriv
= calloc(1,sizeof(struct menu_priv_s
));
259 void menu_list_uninit(menu_t
* menu
,free_entry_t free_func
) {
262 if(!free_func
) free_func
= (free_entry_t
)free
;
264 for(i
= mpriv
->menu
; i
!= NULL
; ) {
270 mpriv
->menu
= mpriv
->current
= NULL
;