Makefile: Rebuild FFmpeg libraries when .asm and .o files change
[mplayer/glamo.git] / libmenu / menu_list.c
blobc49bbd6e4d39e54f9fb801027ddf06eccc330fca
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <ctype.h>
5 #include <string.h>
7 #include "config.h"
9 #include "libmpcodecs/img_format.h"
10 #include "libmpcodecs/mp_image.h"
12 #include "m_struct.h"
13 #include "menu.h"
15 #include "libvo/font_load.h"
16 #include "osdep/keycodes.h"
18 #define IMPL 1
19 #include "menu_list.h"
21 extern double menu_mouse_x;
22 extern double menu_mouse_y;
23 extern int menu_mouse_pos_updated;
24 static int mouse_x;
25 static int mouse_y;
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) {
34 int x = mpriv->x;
35 int y = mpriv->y;
36 int i;
37 int h = mpriv->h;
38 int w = mpriv->w;
39 int dh = 0,dw = 0;
40 int bx, dx, dy = 0;
41 int need_h = 0,need_w = 0,ptr_l,sidx = 0;
42 int th,count = 0;
43 int bg_w;
44 int line_h;
45 list_entry_t* m;
47 if(mpriv->count < 1)
48 return;
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;
55 // mpi is too small
56 if(h - vo_font->height <= 0 || w - ptr_l <= 0 || dw <= 0 || dh <= 0)
57 return;
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) {
64 // try the next
65 for(m = mpriv->current->next ; m ; m = m->next)
66 if(!m->hide) break;
67 if(!m) // or the previous
68 for(m = mpriv->current->prev ; m ; m = m->prev)
69 if(!m->hide) break;
70 if(m) mpriv->current = m;
71 else ptr_l = 0;
74 for(i = 0, m = mpriv->menu ; m ; m = m->next, i++) {
75 int ll;
76 if(m->hide) continue;
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;
80 count++;
82 if(need_w > dw) need_w = dw;
83 if(x >= 0)
84 x += mpriv->minb;
85 if(y > 0)
86 y += mpriv->minb;
87 else
88 y = mpriv->minb;
90 need_h = count * line_h - mpriv->vspace;
91 if( need_h + th > dh) {
92 int start,end;
93 mpriv->disp_lines = (dh + mpriv->vspace - th) / line_h;
94 if(mpriv->disp_lines < 4) {
95 th = 0;
96 mpriv->disp_lines = (dh + mpriv->vspace) / line_h;
98 // Too smoll
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;
105 if(end > count) {
106 end = count;
107 if(end - start < mpriv->disp_lines)
108 start = end - mpriv->disp_lines < 0 ? 0 : end - mpriv->disp_lines;
110 m = mpriv->menu;
111 for(i = 0 ; m->next && i < start ; ) {
112 if(!m->hide) i++;
113 m = m->next;
115 } else {
116 m = mpriv->menu;
117 mpriv->disp_lines = count;
120 bg_w = need_w+2*mpriv->minb;
121 if(th > 0) {
122 int tw,th2;
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,
132 mpriv->vspace,1,
133 MENU_TEXT_TOP|MENU_TEXT_HCENTER,
134 MENU_TEXT_TOP|(x < 0 ? MENU_TEXT_HCENTER :MENU_TEXT_LEFT));
135 dy += th;
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;
150 list_entry_t* e = m;
151 for (i = 0; e != NULL; e = e->next) {
152 if (e->hide) continue;
153 if (i == cur_no) {
154 mpriv->current = e;
155 break;
157 ++i;
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.
168 selection_x = bx;
169 selection_y = dy + y - mpriv->vspace / 2;
170 selection_w = bg_w;
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,
176 bg_w, line_h);
177 if(ptr_l > 0)
178 menu_draw_text_full(mpi,mpriv->ptr,
180 dy+y,dw,dh - dy,
181 mpriv->vspace,0,
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,
187 bg_w, line_h);
188 menu_draw_text_full(mpi,m->txt,
189 dx + ptr_l,
190 dy+y,dw-ptr_l,dh - dy,
191 mpriv->vspace,0,
192 MENU_TEXT_TOP|MENU_TEXT_LEFT,
193 MENU_TEXT_TOP|MENU_TEXT_LEFT);
194 dy += line_h;
199 void menu_list_read_cmd(menu_t* menu,int cmd) {
200 list_entry_t* m;
201 int i;
202 switch(cmd) {
203 case MENU_CMD_UP:
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)
209 /* NOTHING */;
210 if(!mpriv->current->hide) return;
211 while(mpriv->current->prev) {
212 mpriv->current = mpriv->current->prev;
213 if(!mpriv->current->hide) return;
215 break;
216 case MENU_CMD_DOWN:
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;
227 break;
228 case MENU_CMD_HOME:
229 mpriv->current = mpriv->menu;
230 break;
231 case MENU_CMD_END:
232 for(m = mpriv->current ; m && m->next ; m = m->next)
233 /**/;
234 if(m)
235 mpriv->current = m;
236 break;
237 case MENU_CMD_PAGE_UP:
238 for(i = 0, m = mpriv->current ; m && m->prev && i < mpriv->disp_lines ; m = m->prev, i++)
239 /**/;
240 if(m)
241 mpriv->current = m;
242 break;
243 case MENU_CMD_PAGE_DOWN:
244 for(i = 0, m = mpriv->current ; m && m->next && i < mpriv->disp_lines ; m = m->next, i++)
245 /**/;
246 if(m)
247 mpriv->current = m;
248 break;
249 case MENU_CMD_LEFT:
250 case MENU_CMD_CANCEL:
251 menu->show = 0;
252 menu->cl = 1;
253 break;
254 case MENU_CMD_CLICK:
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);
258 break;
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) {
267 if(e->txt[0] == c) {
268 mpriv->current = e;
269 return 1;
272 for(e = mpriv->menu ; e ; e = e->next) {
273 if(e->txt[0] == c) {
274 mpriv->current = e;
275 return 1;
278 return 1;
280 return 0;
283 void menu_list_add_entry(menu_t* menu,list_entry_t* entry) {
284 list_entry_t* l;
285 mpriv->count++;
287 if(mpriv->menu == NULL) {
288 mpriv->menu = mpriv->current = entry;
289 return;
292 for(l = mpriv->menu ; l->next != NULL ; l = l->next)
293 /* NOP */;
294 l->next = entry;
295 entry->prev = l;
298 void menu_list_init(menu_t* menu) {
299 if(!mpriv)
300 mpriv = calloc(1,sizeof(struct menu_priv_s));
304 void menu_list_uninit(menu_t* menu,free_entry_t free_func) {
305 list_entry_t *i,*j;
307 if(!free_func) free_func = (free_entry_t)free;
309 for(i = mpriv->menu ; i != NULL ; ) {
310 j = i->next;
311 free_func(i);
312 i = j;
315 mpriv->menu = mpriv->current = NULL;