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 extern double menu_mouse_x
;
22 extern double menu_mouse_y
;
23 extern int menu_mouse_pos_updated
;
26 static int selection_x
;
27 static int selection_y
;
28 static int selection_w
;
29 static int selection_h
;
31 #define mpriv (menu->priv)
33 void menu_list_draw(menu_t
* menu
,mp_image_t
* mpi
) {
41 int need_h
= 0,need_w
= 0,ptr_l
,sidx
= 0;
50 if(h
<= 0) h
= mpi
->height
;
51 if(w
<= 0) w
= mpi
->width
;
52 dh
= h
- 2*mpriv
->minb
;
53 dw
= w
- 2*mpriv
->minb
;
54 ptr_l
= mpriv
->ptr
? menu_text_length(mpriv
->ptr
) : 0;
56 if(h
- vo_font
->height
<= 0 || w
- ptr_l
<= 0 || dw
<= 0 || dh
<= 0)
59 line_h
= mpriv
->vspace
+ vo_font
->height
;
60 th
= menu_text_num_lines(mpriv
->title
,dw
) * line_h
+ mpriv
->vspace
;
62 // the selected item is hidden, find a visible one
63 if(mpriv
->current
->hide
) {
65 for(m
= mpriv
->current
->next
; m
; m
= m
->next
)
67 if(!m
) // or the previous
68 for(m
= mpriv
->current
->prev
; m
; m
= m
->prev
)
70 if(m
) mpriv
->current
= m
;
74 for(i
= 0, m
= mpriv
->menu
; m
; m
= m
->next
, i
++) {
77 ll
= menu_text_length(m
->txt
);
78 if(ptr_l
+ ll
> need_w
) need_w
= ptr_l
+ ll
;
79 if(m
== mpriv
->current
) sidx
= i
;
82 if(need_w
> dw
) need_w
= dw
;
90 need_h
= count
* line_h
- mpriv
->vspace
;
91 if( need_h
+ th
> dh
) {
93 mpriv
->disp_lines
= (dh
+ mpriv
->vspace
- th
) / line_h
;
94 if(mpriv
->disp_lines
< 4) {
96 mpriv
->disp_lines
= (dh
+ mpriv
->vspace
) / line_h
;
99 if(mpriv
->disp_lines
< 1) return;
100 need_h
= mpriv
->disp_lines
* line_h
- mpriv
->vspace
;
102 start
= sidx
- (mpriv
->disp_lines
/2);
103 if(start
< 0) start
= 0;
104 end
= start
+ mpriv
->disp_lines
;
107 if(end
- start
< mpriv
->disp_lines
)
108 start
= end
- mpriv
->disp_lines
< 0 ? 0 : end
- mpriv
->disp_lines
;
111 for(i
= 0 ; m
->next
&& i
< start
; ) {
117 mpriv
->disp_lines
= count
;
120 bg_w
= need_w
+2*mpriv
->minb
;
123 menu_text_size(mpriv
->title
,dw
,mpriv
->vspace
,1,&tw
,&th2
);
124 if(mpriv
->title_bg
>= 0) {
125 if(tw
+2*mpriv
->minb
> bg_w
) bg_w
= tw
+2*mpriv
->minb
;
126 menu_draw_box(mpi
,mpriv
->title_bg
,mpriv
->title_bg_alpha
,
127 x
< 0 ? (mpi
->w
-bg_w
)/2 : x
-mpriv
->minb
,dy
+y
-mpriv
->vspace
/2,bg_w
,th
);
129 menu_draw_text_full(mpi
,mpriv
->title
,
130 x
< 0 ? mpi
->w
/ 2 : x
,
131 dy
+y
, x
< 0 ? dw
: (tw
> need_w
? tw
: need_w
), 0,
133 MENU_TEXT_TOP
|MENU_TEXT_HCENTER
,
134 MENU_TEXT_TOP
|(x
< 0 ? MENU_TEXT_HCENTER
:MENU_TEXT_LEFT
));
138 dx
= x
< 0 ? (mpi
->w
- need_w
) / 2 : x
;
139 bx
= x
< 0 ? (mpi
->w
- bg_w
) / 2 : x
- mpriv
->minb
;
141 // If mouse moved, try to update selected menu item by the mouse position.
142 if (menu_mouse_pos_updated
) {
143 mouse_x
= menu_mouse_x
* mpi
->width
;
144 mouse_y
= menu_mouse_y
* mpi
->height
;
145 if (mouse_x
>= bx
&& mouse_x
< bx
+ bg_w
) {
146 int by
= dy
+ y
- mpriv
->vspace
/ 2;
147 int max_by
= dh
+ y
+ mpriv
->vspace
/ 2;
148 if (mouse_y
>= by
&& mouse_y
< max_by
) {
149 int cur_no
= (mouse_y
- by
) / line_h
;
151 for (i
= 0; e
!= NULL
; e
= e
->next
) {
152 if (e
->hide
) continue;
161 menu_mouse_pos_updated
= 0;
164 for( ; m
!= NULL
&& dy
+ vo_font
->height
< dh
; m
= m
->next
) {
165 if(m
->hide
) continue;
166 if(m
== mpriv
->current
) {
167 // Record rectangle of current selection box.
169 selection_y
= dy
+ y
- mpriv
->vspace
/ 2;
171 selection_h
= line_h
;
173 if(mpriv
->ptr_bg
>= 0)
174 menu_draw_box(mpi
,mpriv
->ptr_bg
,mpriv
->ptr_bg_alpha
,
175 bx
, dy
+ y
- mpriv
->vspace
/ 2,
178 menu_draw_text_full(mpi
,mpriv
->ptr
,
182 MENU_TEXT_TOP
|MENU_TEXT_LEFT
,
183 MENU_TEXT_TOP
|MENU_TEXT_LEFT
);
184 } else if(mpriv
->item_bg
>= 0)
185 menu_draw_box(mpi
,mpriv
->item_bg
,mpriv
->item_bg_alpha
,
186 bx
, dy
+ y
- mpriv
->vspace
/ 2,
188 menu_draw_text_full(mpi
,m
->txt
,
190 dy
+y
,dw
-ptr_l
,dh
- dy
,
192 MENU_TEXT_TOP
|MENU_TEXT_LEFT
,
193 MENU_TEXT_TOP
|MENU_TEXT_LEFT
);
199 void menu_list_read_cmd(menu_t
* menu
,int cmd
) {
204 while(mpriv
->current
->prev
) {
205 mpriv
->current
= mpriv
->current
->prev
;
206 if(!mpriv
->current
->hide
) return;
208 for( ; mpriv
->current
->next
!= NULL
; mpriv
->current
= mpriv
->current
->next
)
210 if(!mpriv
->current
->hide
) return;
211 while(mpriv
->current
->prev
) {
212 mpriv
->current
= mpriv
->current
->prev
;
213 if(!mpriv
->current
->hide
) return;
217 while(mpriv
->current
->next
) {
218 mpriv
->current
= mpriv
->current
->next
;
219 if(!mpriv
->current
->hide
) return;
221 mpriv
->current
= mpriv
->menu
;
222 if(!mpriv
->current
->hide
) return;
223 while(mpriv
->current
->next
) {
224 mpriv
->current
= mpriv
->current
->next
;
225 if(!mpriv
->current
->hide
) return;
229 mpriv
->current
= mpriv
->menu
;
232 for(m
= mpriv
->current
; m
&& m
->next
; m
= m
->next
)
237 case MENU_CMD_PAGE_UP
:
238 for(i
= 0, m
= mpriv
->current
; m
&& m
->prev
&& i
< mpriv
->disp_lines
; m
= m
->prev
, i
++)
243 case MENU_CMD_PAGE_DOWN
:
244 for(i
= 0, m
= mpriv
->current
; m
&& m
->next
&& i
< mpriv
->disp_lines
; m
= m
->next
, i
++)
250 case MENU_CMD_CANCEL
:
255 if (mouse_x
>= selection_x
&& mouse_x
< selection_x
+ selection_w
&&
256 mouse_y
>= selection_y
&& mouse_y
< selection_y
+ selection_h
)
257 menu_read_cmd(menu
, MENU_CMD_OK
);
262 int menu_list_jump_to_key(menu_t
* menu
,int c
) {
263 if(c
< 256 && isalnum(c
)) {
264 list_entry_t
* e
= mpriv
->current
;
265 if(e
->txt
[0] == c
) e
= e
->next
;
266 for( ; e
; e
= e
->next
) {
272 for(e
= mpriv
->menu
; e
; e
= e
->next
) {
283 void menu_list_add_entry(menu_t
* menu
,list_entry_t
* entry
) {
287 if(mpriv
->menu
== NULL
) {
288 mpriv
->menu
= mpriv
->current
= entry
;
292 for(l
= mpriv
->menu
; l
->next
!= NULL
; l
= l
->next
)
298 void menu_list_init(menu_t
* menu
) {
300 mpriv
= calloc(1,sizeof(struct menu_priv_s
));
304 void menu_list_uninit(menu_t
* menu
,free_entry_t free_func
) {
307 if(!free_func
) free_func
= (free_entry_t
)free
;
309 for(i
= mpriv
->menu
; i
!= NULL
; ) {
315 mpriv
->menu
= mpriv
->current
= NULL
;