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;
39 if(h
<= 0) h
= mpi
->height
;
40 if(w
<= 0) w
= mpi
->width
;
41 dh
= h
- 2*mpriv
->minb
;
42 dw
= w
- 2*mpriv
->minb
;
43 ptr_l
= mpriv
->ptr
? menu_text_length(mpriv
->ptr
) : 0;
45 if(h
- vo_font
->height
<= 0 || w
- ptr_l
<= 0 || dw
<= 0 || dh
<= 0)
48 th
= menu_text_num_lines(mpriv
->title
,dw
) * (mpriv
->vspace
+ vo_font
->height
) + mpriv
->vspace
;
50 // the selected item is hidden, find a visible one
51 if(mpriv
->current
->hide
) {
53 for(m
= mpriv
->current
->next
; m
; m
= m
->next
)
55 if(!m
) // or the previous
56 for(m
= mpriv
->current
->prev
; m
; m
= m
->prev
)
58 if(m
) mpriv
->current
= m
;
62 for(i
= 0, m
= mpriv
->menu
; m
; m
= m
->next
, i
++) {
65 ll
= menu_text_length(m
->txt
);
66 if(ptr_l
+ ll
> need_w
) need_w
= ptr_l
+ ll
;
67 if(m
== mpriv
->current
) sidx
= i
;
70 if(need_w
> dw
) need_w
= dw
;
78 need_h
= count
* (mpriv
->vspace
+ vo_font
->height
) - mpriv
->vspace
;
79 if( need_h
+ th
> dh
) {
81 mpriv
->disp_lines
= (dh
+ mpriv
->vspace
- th
) / (mpriv
->vspace
+ vo_font
->height
);
82 if(mpriv
->disp_lines
< 4) {
84 mpriv
->disp_lines
= (dh
+ mpriv
->vspace
) / ( vo_font
->height
+ mpriv
->vspace
);
87 if(mpriv
->disp_lines
< 1) return;
88 need_h
= mpriv
->disp_lines
*(mpriv
->vspace
+ vo_font
->height
) - mpriv
->vspace
;
90 start
= sidx
- (mpriv
->disp_lines
/2);
91 if(start
< 0) start
= 0;
92 end
= start
+ mpriv
->disp_lines
;
95 if(end
- start
< mpriv
->disp_lines
)
96 start
= end
- mpriv
->disp_lines
< 0 ? 0 : end
- mpriv
->disp_lines
;
99 for(i
= 0 ; m
->next
&& i
< start
; ) {
105 mpriv
->disp_lines
= count
;
108 bg_w
= need_w
+2*mpriv
->minb
;
110 if(mpriv
->title_bg
>= 0) {
112 menu_text_size(mpriv
->title
,dw
,mpriv
->vspace
,1,&tw
,&th2
);
113 if(tw
+2*mpriv
->minb
> bg_w
) bg_w
= tw
+2*mpriv
->minb
;
114 menu_draw_box(mpi
,mpriv
->title_bg
,mpriv
->title_bg_alpha
,
115 x
< 0 ? (mpi
->w
-bg_w
)/2 : x
-mpriv
->minb
,dy
+y
-mpriv
->vspace
/2,bg_w
,th
);
117 menu_draw_text_full(mpi
,mpriv
->title
,
118 x
< 0 ? mpi
->w
/ 2 : x
,
121 MENU_TEXT_TOP
|MENU_TEXT_HCENTER
,
122 MENU_TEXT_TOP
|(x
< 0 ? MENU_TEXT_HCENTER
:MENU_TEXT_LEFT
));
126 for( ; m
!= NULL
&& dy
+ vo_font
->height
< dh
; m
= m
->next
) {
127 if(m
->hide
) continue;
128 if(m
== mpriv
->current
) {
129 if(mpriv
->ptr_bg
>= 0)
130 menu_draw_box(mpi
,mpriv
->ptr_bg
,mpriv
->ptr_bg_alpha
,
131 x
< 0 ? (mpi
->w
-bg_w
)/2 : x
-mpriv
->minb
,dy
+y
-mpriv
->vspace
/2,
132 bg_w
,vo_font
->height
+ mpriv
->vspace
);
134 menu_draw_text_full(mpi
,mpriv
->ptr
,
135 x
< 0 ? (mpi
->w
- need_w
) / 2 + ptr_l
: x
,
138 MENU_TEXT_TOP
|(x
< 0 ? MENU_TEXT_RIGHT
:MENU_TEXT_LEFT
) ,
139 MENU_TEXT_TOP
|(x
< 0 ? MENU_TEXT_RIGHT
:MENU_TEXT_LEFT
));
140 } else if(mpriv
->item_bg
>= 0)
141 menu_draw_box(mpi
,mpriv
->item_bg
,mpriv
->item_bg_alpha
,
142 x
< 0 ? (mpi
->w
-bg_w
)/2 : x
-mpriv
->minb
,dy
+y
-mpriv
->vspace
/2,
143 bg_w
,vo_font
->height
+ mpriv
->vspace
);
144 menu_draw_text_full(mpi
,m
->txt
,
145 x
< 0 ? (mpi
->w
- need_w
) / 2 + ptr_l
: x
+ ptr_l
,
146 dy
+y
,dw
-ptr_l
,dh
- dy
,
148 MENU_TEXT_TOP
|MENU_TEXT_LEFT
,
149 MENU_TEXT_TOP
|MENU_TEXT_LEFT
);
150 dy
+= vo_font
->height
+ mpriv
->vspace
;
155 void menu_list_read_cmd(menu_t
* menu
,int cmd
) {
160 while(mpriv
->current
->prev
) {
161 mpriv
->current
= mpriv
->current
->prev
;
162 if(!mpriv
->current
->hide
) return;
164 for( ; mpriv
->current
->next
!= NULL
; mpriv
->current
= mpriv
->current
->next
)
166 if(!mpriv
->current
->hide
) return;
167 while(mpriv
->current
->prev
) {
168 mpriv
->current
= mpriv
->current
->prev
;
169 if(!mpriv
->current
->hide
) return;
173 while(mpriv
->current
->next
) {
174 mpriv
->current
= mpriv
->current
->next
;
175 if(!mpriv
->current
->hide
) return;
177 mpriv
->current
= mpriv
->menu
;
178 if(!mpriv
->current
->hide
) return;
179 while(mpriv
->current
->next
) {
180 mpriv
->current
= mpriv
->current
->next
;
181 if(!mpriv
->current
->hide
) return;
185 mpriv
->current
= mpriv
->menu
;
188 for(m
= mpriv
->current
; m
&& m
->next
; m
= m
->next
)
193 case MENU_CMD_PAGE_UP
:
194 for(i
= 0, m
= mpriv
->current
; m
&& m
->prev
&& i
< mpriv
->disp_lines
; m
= m
->prev
, i
++)
199 case MENU_CMD_PAGE_DOWN
:
200 for(i
= 0, m
= mpriv
->current
; m
&& m
->next
&& i
< mpriv
->disp_lines
; m
= m
->next
, i
++)
206 case MENU_CMD_CANCEL
:
213 int menu_list_jump_to_key(menu_t
* menu
,int c
) {
214 if(c
< 256 && isalnum(c
)) {
215 list_entry_t
* e
= mpriv
->current
;
216 if(e
->txt
[0] == c
) e
= e
->next
;
217 for( ; e
; e
= e
->next
) {
223 for(e
= mpriv
->menu
; e
; e
= e
->next
) {
234 void menu_list_add_entry(menu_t
* menu
,list_entry_t
* entry
) {
238 if(mpriv
->menu
== NULL
) {
239 mpriv
->menu
= mpriv
->current
= entry
;
243 for(l
= mpriv
->menu
; l
->next
!= NULL
; l
= l
->next
)
249 void menu_list_init(menu_t
* menu
) {
251 mpriv
= calloc(1,sizeof(struct menu_priv_s
));
255 void menu_list_uninit(menu_t
* menu
,free_entry_t free_func
) {
258 if(!free_func
) free_func
= (free_entry_t
)free
;
260 for(i
= mpriv
->menu
; i
!= NULL
; ) {
266 mpriv
->menu
= mpriv
->current
= NULL
;