Files should be opened in binary mode on OS/2.
[mplayer/glamo.git] / libmenu / menu_list.c
blob9264490b81caec7471e9475cf2a5f320ba0d31c0
1 /*
2 * This file is part of MPlayer.
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.
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.
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.
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <ctype.h>
22 #include <string.h>
24 #include "config.h"
26 #include "libmpcodecs/img_format.h"
27 #include "libmpcodecs/mp_image.h"
29 #include "m_struct.h"
30 #include "menu.h"
32 #include "libvo/font_load.h"
33 #include "osdep/keycodes.h"
35 #define IMPL 1
36 #include "menu_list.h"
38 extern double menu_mouse_x;
39 extern double menu_mouse_y;
40 extern int menu_mouse_pos_updated;
41 static int mouse_x;
42 static int mouse_y;
43 static int selection_x;
44 static int selection_y;
45 static int selection_w;
46 static int selection_h;
48 #define mpriv (menu->priv)
50 void menu_list_draw(menu_t* menu,mp_image_t* mpi) {
51 int x = mpriv->x;
52 int y = mpriv->y;
53 int i;
54 int h = mpriv->h;
55 int w = mpriv->w;
56 int dh = 0,dw = 0;
57 int bx, dx, dy = 0;
58 int need_h = 0,need_w = 0,ptr_l,sidx = 0;
59 int th,count = 0;
60 int bg_w;
61 int line_h;
62 list_entry_t* m;
64 if(mpriv->count < 1)
65 return;
67 if(h <= 0) h = mpi->height;
68 if(w <= 0) w = mpi->width;
69 dh = h - 2*mpriv->minb;
70 dw = w - 2*mpriv->minb;
71 ptr_l = mpriv->ptr ? menu_text_length(mpriv->ptr) : 0;
72 // mpi is too small
73 if(h - vo_font->height <= 0 || w - ptr_l <= 0 || dw <= 0 || dh <= 0)
74 return;
76 line_h = mpriv->vspace + vo_font->height;
77 th = menu_text_num_lines(mpriv->title,dw) * line_h + mpriv->vspace;
79 // the selected item is hidden, find a visible one
80 if(mpriv->current->hide) {
81 // try the next
82 for(m = mpriv->current->next ; m ; m = m->next)
83 if(!m->hide) break;
84 if(!m) // or the previous
85 for(m = mpriv->current->prev ; m ; m = m->prev)
86 if(!m->hide) break;
87 if(m) mpriv->current = m;
88 else ptr_l = 0;
91 for(i = 0, m = mpriv->menu ; m ; m = m->next, i++) {
92 int ll;
93 if(m->hide) continue;
94 ll = menu_text_length(m->txt);
95 if(ptr_l + ll > need_w) need_w = ptr_l + ll;
96 if(m == mpriv->current) sidx = i;
97 count++;
99 if(need_w > dw) need_w = dw;
100 if(x >= 0)
101 x += mpriv->minb;
102 if(y > 0)
103 y += mpriv->minb;
104 else
105 y = mpriv->minb;
107 need_h = count * line_h - mpriv->vspace;
108 if( need_h + th > dh) {
109 int start,end;
110 mpriv->disp_lines = (dh + mpriv->vspace - th) / line_h;
111 if(mpriv->disp_lines < 4) {
112 th = 0;
113 mpriv->disp_lines = (dh + mpriv->vspace) / line_h;
115 // Too smoll
116 if(mpriv->disp_lines < 1) return;
117 need_h = mpriv->disp_lines * line_h - mpriv->vspace;
119 start = sidx - (mpriv->disp_lines/2);
120 if(start < 0) start = 0;
121 end = start + mpriv->disp_lines;
122 if(end > count) {
123 end = count;
124 if(end - start < mpriv->disp_lines)
125 start = end - mpriv->disp_lines < 0 ? 0 : end - mpriv->disp_lines;
127 m = mpriv->menu;
128 for(i = 0 ; m->next && i < start ; ) {
129 if(!m->hide) i++;
130 m = m->next;
132 } else {
133 m = mpriv->menu;
134 mpriv->disp_lines = count;
137 bg_w = need_w+2*mpriv->minb;
138 if(th > 0) {
139 int tw,th2;
140 menu_text_size(mpriv->title,dw,mpriv->vspace,1,&tw,&th2);
141 if(mpriv->title_bg >= 0) {
142 if(tw+2*mpriv->minb > bg_w) bg_w = tw+2*mpriv->minb;
143 menu_draw_box(mpi,mpriv->title_bg,mpriv->title_bg_alpha,
144 x < 0 ? (mpi->w-bg_w)/2 : x-mpriv->minb,dy+y-mpriv->vspace/2,bg_w,th);
146 menu_draw_text_full(mpi,mpriv->title,
147 x < 0 ? mpi->w / 2 : x,
148 dy+y, x < 0 ? dw : (tw > need_w ? tw : need_w), 0,
149 mpriv->vspace,1,
150 MENU_TEXT_TOP|MENU_TEXT_HCENTER,
151 MENU_TEXT_TOP|(x < 0 ? MENU_TEXT_HCENTER :MENU_TEXT_LEFT));
152 dy += th;
155 dx = x < 0 ? (mpi->w - need_w) / 2 : x;
156 bx = x < 0 ? (mpi->w - bg_w) / 2 : x - mpriv->minb;
158 // If mouse moved, try to update selected menu item by the mouse position.
159 if (menu_mouse_pos_updated) {
160 mouse_x = menu_mouse_x * mpi->width;
161 mouse_y = menu_mouse_y * mpi->height;
162 if (mouse_x >= bx && mouse_x < bx + bg_w) {
163 int by = dy + y - mpriv->vspace / 2;
164 int max_by = dh + y + mpriv->vspace / 2;
165 if (mouse_y >= by && mouse_y < max_by) {
166 int cur_no = (mouse_y - by) / line_h;
167 list_entry_t* e = m;
168 for (i = 0; e != NULL; e = e->next) {
169 if (e->hide) continue;
170 if (i == cur_no) {
171 mpriv->current = e;
172 break;
174 ++i;
178 menu_mouse_pos_updated = 0;
181 for( ; m != NULL && dy + vo_font->height < dh ; m = m->next ) {
182 if(m->hide) continue;
183 if(m == mpriv->current) {
184 // Record rectangle of current selection box.
185 selection_x = bx;
186 selection_y = dy + y - mpriv->vspace / 2;
187 selection_w = bg_w;
188 selection_h = line_h;
190 if(mpriv->ptr_bg >= 0)
191 menu_draw_box(mpi,mpriv->ptr_bg,mpriv->ptr_bg_alpha,
192 bx, dy + y - mpriv->vspace / 2,
193 bg_w, line_h);
194 if(ptr_l > 0)
195 menu_draw_text_full(mpi,mpriv->ptr,
197 dy+y,dw,dh - dy,
198 mpriv->vspace,0,
199 MENU_TEXT_TOP|MENU_TEXT_LEFT,
200 MENU_TEXT_TOP|MENU_TEXT_LEFT);
201 } else if(mpriv->item_bg >= 0)
202 menu_draw_box(mpi,mpriv->item_bg,mpriv->item_bg_alpha,
203 bx, dy + y - mpriv->vspace / 2,
204 bg_w, line_h);
205 menu_draw_text_full(mpi,m->txt,
206 dx + ptr_l,
207 dy+y,dw-ptr_l,dh - dy,
208 mpriv->vspace,0,
209 MENU_TEXT_TOP|MENU_TEXT_LEFT,
210 MENU_TEXT_TOP|MENU_TEXT_LEFT);
211 dy += line_h;
216 void menu_list_read_cmd(menu_t* menu,int cmd) {
217 list_entry_t* m;
218 int i;
219 switch(cmd) {
220 case MENU_CMD_UP:
221 while(mpriv->current->prev) {
222 mpriv->current = mpriv->current->prev;
223 if(!mpriv->current->hide) return;
225 for( ; mpriv->current->next != NULL ; mpriv->current = mpriv->current->next)
226 /* NOTHING */;
227 if(!mpriv->current->hide) return;
228 while(mpriv->current->prev) {
229 mpriv->current = mpriv->current->prev;
230 if(!mpriv->current->hide) return;
232 break;
233 case MENU_CMD_DOWN:
234 while(mpriv->current->next) {
235 mpriv->current = mpriv->current->next;
236 if(!mpriv->current->hide) return;
238 mpriv->current = mpriv->menu;
239 if(!mpriv->current->hide) return;
240 while(mpriv->current->next) {
241 mpriv->current = mpriv->current->next;
242 if(!mpriv->current->hide) return;
244 break;
245 case MENU_CMD_HOME:
246 mpriv->current = mpriv->menu;
247 break;
248 case MENU_CMD_END:
249 for(m = mpriv->current ; m && m->next ; m = m->next)
250 /**/;
251 if(m)
252 mpriv->current = m;
253 break;
254 case MENU_CMD_PAGE_UP:
255 for(i = 0, m = mpriv->current ; m && m->prev && i < mpriv->disp_lines ; m = m->prev, i++)
256 /**/;
257 if(m)
258 mpriv->current = m;
259 break;
260 case MENU_CMD_PAGE_DOWN:
261 for(i = 0, m = mpriv->current ; m && m->next && i < mpriv->disp_lines ; m = m->next, i++)
262 /**/;
263 if(m)
264 mpriv->current = m;
265 break;
266 case MENU_CMD_LEFT:
267 case MENU_CMD_CANCEL:
268 menu->show = 0;
269 menu->cl = 1;
270 break;
271 case MENU_CMD_CLICK:
272 if (mouse_x >= selection_x && mouse_x < selection_x + selection_w &&
273 mouse_y >= selection_y && mouse_y < selection_y + selection_h)
274 menu_read_cmd(menu, MENU_CMD_OK);
275 break;
279 int menu_list_jump_to_key(menu_t* menu,int c) {
280 if(c < 256 && isalnum(c)) {
281 list_entry_t* e = mpriv->current;
282 if(e->txt[0] == c) e = e->next;
283 for( ; e ; e = e->next) {
284 if(e->txt[0] == c) {
285 mpriv->current = e;
286 return 1;
289 for(e = mpriv->menu ; e ; e = e->next) {
290 if(e->txt[0] == c) {
291 mpriv->current = e;
292 return 1;
295 return 1;
297 return 0;
300 void menu_list_add_entry(menu_t* menu,list_entry_t* entry) {
301 list_entry_t* l;
302 mpriv->count++;
304 if(mpriv->menu == NULL) {
305 mpriv->menu = mpriv->current = entry;
306 return;
309 for(l = mpriv->menu ; l->next != NULL ; l = l->next)
310 /* NOP */;
311 l->next = entry;
312 entry->prev = l;
315 void menu_list_init(menu_t* menu) {
316 if(!mpriv)
317 mpriv = calloc(1,sizeof(struct menu_priv_s));
321 void menu_list_uninit(menu_t* menu,free_entry_t free_func) {
322 list_entry_t *i,*j;
324 if(!free_func) free_func = (free_entry_t)free;
326 for(i = mpriv->menu ; i != NULL ; ) {
327 j = i->next;
328 free_func(i);
329 i = j;
332 mpriv->menu = mpriv->current = NULL;