28113
|
1 /*
|
|
2 * This file is part of MPlayer.
|
|
3 *
|
|
4 * MPlayer is free software; you can redistribute it and/or modify
|
|
5 * it under the terms of the GNU General Public License as published by
|
|
6 * the Free Software Foundation; either version 2 of the License, or
|
|
7 * (at your option) any later version.
|
|
8 *
|
|
9 * MPlayer is distributed in the hope that it will be useful,
|
|
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
12 * GNU General Public License for more details.
|
|
13 *
|
|
14 * You should have received a copy of the GNU General Public License along
|
|
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
|
|
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
17 */
|
8197
|
18
|
|
19 #include <stdlib.h>
|
|
20 #include <stdio.h>
|
|
21 #include <ctype.h>
|
8623
|
22 #include <string.h>
|
8197
|
23
|
16862
|
24 #include "config.h"
|
8197
|
25
|
19431
|
26 #include "libmpcodecs/img_format.h"
|
|
27 #include "libmpcodecs/mp_image.h"
|
8197
|
28
|
|
29 #include "m_struct.h"
|
|
30 #include "menu.h"
|
|
31
|
32466
|
32 #include "sub/font_load.h"
|
16862
|
33 #include "osdep/keycodes.h"
|
8197
|
34
|
|
35 #define IMPL 1
|
|
36 #include "menu_list.h"
|
|
37
|
25461
|
38 static int mouse_x;
|
|
39 static int mouse_y;
|
|
40 static int selection_x;
|
|
41 static int selection_y;
|
|
42 static int selection_w;
|
|
43 static int selection_h;
|
|
44
|
8197
|
45 #define mpriv (menu->priv)
|
|
46
|
|
47 void menu_list_draw(menu_t* menu,mp_image_t* mpi) {
|
|
48 int x = mpriv->x;
|
|
49 int y = mpriv->y;
|
|
50 int i;
|
|
51 int h = mpriv->h;
|
|
52 int w = mpriv->w;
|
|
53 int dh = 0,dw = 0;
|
25438
|
54 int bx, dx, dy = 0;
|
17946
|
55 int need_h = 0,need_w = 0,ptr_l,sidx = 0;
|
|
56 int th,count = 0;
|
17993
|
57 int bg_w;
|
25437
|
58 int line_h;
|
8197
|
59 list_entry_t* m;
|
|
60
|
|
61 if(mpriv->count < 1)
|
|
62 return;
|
|
63
|
|
64 if(h <= 0) h = mpi->height;
|
|
65 if(w <= 0) w = mpi->width;
|
|
66 dh = h - 2*mpriv->minb;
|
|
67 dw = w - 2*mpriv->minb;
|
17946
|
68 ptr_l = mpriv->ptr ? menu_text_length(mpriv->ptr) : 0;
|
8197
|
69 // mpi is too small
|
|
70 if(h - vo_font->height <= 0 || w - ptr_l <= 0 || dw <= 0 || dh <= 0)
|
|
71 return;
|
|
72
|
25437
|
73 line_h = mpriv->vspace + vo_font->height;
|
|
74 th = menu_text_num_lines(mpriv->title,dw) * line_h + mpriv->vspace;
|
8197
|
75
|
17946
|
76 // the selected item is hidden, find a visible one
|
|
77 if(mpriv->current->hide) {
|
|
78 // try the next
|
|
79 for(m = mpriv->current->next ; m ; m = m->next)
|
|
80 if(!m->hide) break;
|
|
81 if(!m) // or the previous
|
|
82 for(m = mpriv->current->prev ; m ; m = m->prev)
|
|
83 if(!m->hide) break;
|
|
84 if(m) mpriv->current = m;
|
|
85 else ptr_l = 0;
|
|
86 }
|
29263
|
87
|
8197
|
88 for(i = 0, m = mpriv->menu ; m ; m = m->next, i++) {
|
17946
|
89 int ll;
|
|
90 if(m->hide) continue;
|
|
91 ll = menu_text_length(m->txt);
|
8197
|
92 if(ptr_l + ll > need_w) need_w = ptr_l + ll;
|
|
93 if(m == mpriv->current) sidx = i;
|
17946
|
94 count++;
|
8197
|
95 }
|
|
96 if(need_w > dw) need_w = dw;
|
25421
|
97 if(x >= 0)
|
8197
|
98 x += mpriv->minb;
|
|
99 if(y > 0)
|
|
100 y += mpriv->minb;
|
29263
|
101 else
|
8197
|
102 y = mpriv->minb;
|
|
103
|
25437
|
104 need_h = count * line_h - mpriv->vspace;
|
8197
|
105 if( need_h + th > dh) {
|
|
106 int start,end;
|
25437
|
107 mpriv->disp_lines = (dh + mpriv->vspace - th) / line_h;
|
24979
|
108 if(mpriv->disp_lines < 4) {
|
8197
|
109 th = 0;
|
25437
|
110 mpriv->disp_lines = (dh + mpriv->vspace) / line_h;
|
8197
|
111 }
|
|
112 // Too smoll
|
24979
|
113 if(mpriv->disp_lines < 1) return;
|
25437
|
114 need_h = mpriv->disp_lines * line_h - mpriv->vspace;
|
8197
|
115
|
24979
|
116 start = sidx - (mpriv->disp_lines/2);
|
8197
|
117 if(start < 0) start = 0;
|
24979
|
118 end = start + mpriv->disp_lines;
|
17946
|
119 if(end > count) {
|
|
120 end = count;
|
24979
|
121 if(end - start < mpriv->disp_lines)
|
|
122 start = end - mpriv->disp_lines < 0 ? 0 : end - mpriv->disp_lines;
|
8197
|
123 }
|
|
124 m = mpriv->menu;
|
17946
|
125 for(i = 0 ; m->next && i < start ; ) {
|
|
126 if(!m->hide) i++;
|
8197
|
127 m = m->next;
|
17946
|
128 }
|
24979
|
129 } else {
|
8197
|
130 m = mpriv->menu;
|
24979
|
131 mpriv->disp_lines = count;
|
|
132 }
|
8197
|
133
|
17993
|
134 bg_w = need_w+2*mpriv->minb;
|
8197
|
135 if(th > 0) {
|
25435
|
136 int tw,th2;
|
|
137 menu_text_size(mpriv->title,dw,mpriv->vspace,1,&tw,&th2);
|
17993
|
138 if(mpriv->title_bg >= 0) {
|
|
139 if(tw+2*mpriv->minb > bg_w) bg_w = tw+2*mpriv->minb;
|
|
140 menu_draw_box(mpi,mpriv->title_bg,mpriv->title_bg_alpha,
|
18204
|
141 x < 0 ? (mpi->w-bg_w)/2 : x-mpriv->minb,dy+y-mpriv->vspace/2,bg_w,th);
|
17993
|
142 }
|
8197
|
143 menu_draw_text_full(mpi,mpriv->title,
|
|
144 x < 0 ? mpi->w / 2 : x,
|
25435
|
145 dy+y, x < 0 ? dw : (tw > need_w ? tw : need_w), 0,
|
8197
|
146 mpriv->vspace,1,
|
|
147 MENU_TEXT_TOP|MENU_TEXT_HCENTER,
|
|
148 MENU_TEXT_TOP|(x < 0 ? MENU_TEXT_HCENTER :MENU_TEXT_LEFT));
|
|
149 dy += th;
|
|
150 }
|
29263
|
151
|
25438
|
152 dx = x < 0 ? (mpi->w - need_w) / 2 : x;
|
|
153 bx = x < 0 ? (mpi->w - bg_w) / 2 : x - mpriv->minb;
|
25461
|
154
|
|
155 // If mouse moved, try to update selected menu item by the mouse position.
|
|
156 if (menu_mouse_pos_updated) {
|
|
157 mouse_x = menu_mouse_x * mpi->width;
|
|
158 mouse_y = menu_mouse_y * mpi->height;
|
|
159 if (mouse_x >= bx && mouse_x < bx + bg_w) {
|
|
160 int by = dy + y - mpriv->vspace / 2;
|
|
161 int max_by = dh + y + mpriv->vspace / 2;
|
|
162 if (mouse_y >= by && mouse_y < max_by) {
|
|
163 int cur_no = (mouse_y - by) / line_h;
|
|
164 list_entry_t* e = m;
|
|
165 for (i = 0; e != NULL; e = e->next) {
|
|
166 if (e->hide) continue;
|
|
167 if (i == cur_no) {
|
|
168 mpriv->current = e;
|
|
169 break;
|
|
170 }
|
|
171 ++i;
|
|
172 }
|
|
173 }
|
|
174 }
|
|
175 menu_mouse_pos_updated = 0;
|
|
176 }
|
|
177
|
8197
|
178 for( ; m != NULL && dy + vo_font->height < dh ; m = m->next ) {
|
17946
|
179 if(m->hide) continue;
|
17993
|
180 if(m == mpriv->current) {
|
25461
|
181 // Record rectangle of current selection box.
|
|
182 selection_x = bx;
|
|
183 selection_y = dy + y - mpriv->vspace / 2;
|
|
184 selection_w = bg_w;
|
|
185 selection_h = line_h;
|
|
186
|
17993
|
187 if(mpriv->ptr_bg >= 0)
|
|
188 menu_draw_box(mpi,mpriv->ptr_bg,mpriv->ptr_bg_alpha,
|
25438
|
189 bx, dy + y - mpriv->vspace / 2,
|
25437
|
190 bg_w, line_h);
|
17993
|
191 if(ptr_l > 0)
|
|
192 menu_draw_text_full(mpi,mpriv->ptr,
|
25438
|
193 dx,
|
17993
|
194 dy+y,dw,dh - dy,
|
|
195 mpriv->vspace,0,
|
25438
|
196 MENU_TEXT_TOP|MENU_TEXT_LEFT,
|
|
197 MENU_TEXT_TOP|MENU_TEXT_LEFT);
|
17993
|
198 } else if(mpriv->item_bg >= 0)
|
|
199 menu_draw_box(mpi,mpriv->item_bg,mpriv->item_bg_alpha,
|
25438
|
200 bx, dy + y - mpriv->vspace / 2,
|
25437
|
201 bg_w, line_h);
|
8197
|
202 menu_draw_text_full(mpi,m->txt,
|
25438
|
203 dx + ptr_l,
|
8197
|
204 dy+y,dw-ptr_l,dh - dy,
|
|
205 mpriv->vspace,0,
|
|
206 MENU_TEXT_TOP|MENU_TEXT_LEFT,
|
|
207 MENU_TEXT_TOP|MENU_TEXT_LEFT);
|
25437
|
208 dy += line_h;
|
8197
|
209 }
|
|
210
|
|
211 }
|
|
212
|
|
213 void menu_list_read_cmd(menu_t* menu,int cmd) {
|
25263
|
214 list_entry_t* m;
|
|
215 int i;
|
8197
|
216 switch(cmd) {
|
|
217 case MENU_CMD_UP:
|
17980
|
218 while(mpriv->current->prev) {
|
|
219 mpriv->current = mpriv->current->prev;
|
17946
|
220 if(!mpriv->current->hide) return;
|
|
221 }
|
17980
|
222 for( ; mpriv->current->next != NULL ; mpriv->current = mpriv->current->next)
|
|
223 /* NOTHING */;
|
|
224 if(!mpriv->current->hide) return;
|
17946
|
225 while(mpriv->current->prev) {
|
|
226 mpriv->current = mpriv->current->prev;
|
|
227 if(!mpriv->current->hide) return;
|
|
228 }
|
|
229 break;
|
8197
|
230 case MENU_CMD_DOWN:
|
17980
|
231 while(mpriv->current->next) {
|
|
232 mpriv->current = mpriv->current->next;
|
17946
|
233 if(!mpriv->current->hide) return;
|
|
234 }
|
17980
|
235 mpriv->current = mpriv->menu;
|
|
236 if(!mpriv->current->hide) return;
|
17946
|
237 while(mpriv->current->next) {
|
8197
|
238 mpriv->current = mpriv->current->next;
|
17946
|
239 if(!mpriv->current->hide) return;
|
|
240 }
|
|
241 break;
|
25263
|
242 case MENU_CMD_HOME:
|
|
243 mpriv->current = mpriv->menu;
|
|
244 break;
|
|
245 case MENU_CMD_END:
|
|
246 for(m = mpriv->current ; m && m->next ; m = m->next)
|
|
247 /**/;
|
|
248 if(m)
|
|
249 mpriv->current = m;
|
|
250 break;
|
|
251 case MENU_CMD_PAGE_UP:
|
|
252 for(i = 0, m = mpriv->current ; m && m->prev && i < mpriv->disp_lines ; m = m->prev, i++)
|
|
253 /**/;
|
|
254 if(m)
|
|
255 mpriv->current = m;
|
|
256 break;
|
|
257 case MENU_CMD_PAGE_DOWN:
|
|
258 for(i = 0, m = mpriv->current ; m && m->next && i < mpriv->disp_lines ; m = m->next, i++)
|
|
259 /**/;
|
|
260 if(m)
|
|
261 mpriv->current = m;
|
|
262 break;
|
17945
|
263 case MENU_CMD_LEFT:
|
8197
|
264 case MENU_CMD_CANCEL:
|
|
265 menu->show = 0;
|
|
266 menu->cl = 1;
|
|
267 break;
|
25461
|
268 case MENU_CMD_CLICK:
|
|
269 if (mouse_x >= selection_x && mouse_x < selection_x + selection_w &&
|
|
270 mouse_y >= selection_y && mouse_y < selection_y + selection_h)
|
|
271 menu_read_cmd(menu, MENU_CMD_OK);
|
|
272 break;
|
29263
|
273 }
|
8197
|
274 }
|
|
275
|
25263
|
276 int menu_list_jump_to_key(menu_t* menu,int c) {
|
10935
|
277 if(c < 256 && isalnum(c)) {
|
8197
|
278 list_entry_t* e = mpriv->current;
|
|
279 if(e->txt[0] == c) e = e->next;
|
|
280 for( ; e ; e = e->next) {
|
|
281 if(e->txt[0] == c) {
|
|
282 mpriv->current = e;
|
25263
|
283 return 1;
|
8197
|
284 }
|
|
285 }
|
|
286 for(e = mpriv->menu ; e ; e = e->next) {
|
|
287 if(e->txt[0] == c) {
|
|
288 mpriv->current = e;
|
25263
|
289 return 1;
|
8197
|
290 }
|
|
291 }
|
25263
|
292 return 1;
|
|
293 }
|
|
294 return 0;
|
8197
|
295 }
|
|
296
|
|
297 void menu_list_add_entry(menu_t* menu,list_entry_t* entry) {
|
|
298 list_entry_t* l;
|
|
299 mpriv->count++;
|
|
300
|
|
301 if(mpriv->menu == NULL) {
|
|
302 mpriv->menu = mpriv->current = entry;
|
|
303 return;
|
|
304 }
|
|
305
|
|
306 for(l = mpriv->menu ; l->next != NULL ; l = l->next)
|
|
307 /* NOP */;
|
|
308 l->next = entry;
|
|
309 entry->prev = l;
|
|
310 }
|
29263
|
311
|
8197
|
312 void menu_list_init(menu_t* menu) {
|
|
313 if(!mpriv)
|
|
314 mpriv = calloc(1,sizeof(struct menu_priv_s));
|
|
315
|
|
316 }
|
|
317
|
|
318 void menu_list_uninit(menu_t* menu,free_entry_t free_func) {
|
|
319 list_entry_t *i,*j;
|
|
320
|
|
321 if(!free_func) free_func = (free_entry_t)free;
|
|
322
|
|
323 for(i = mpriv->menu ; i != NULL ; ) {
|
|
324 j = i->next;
|
|
325 free_func(i);
|
|
326 i = j;
|
|
327 }
|
29263
|
328
|
8197
|
329 mpriv->menu = mpriv->current = NULL;
|
|
330
|
|
331 }
|