Mercurial > mplayer.hg
annotate libmenu/menu_list.c @ 12596:dbc071ea8ae6
sync
author | paszczi |
---|---|
date | Wed, 16 Jun 2004 21:36:28 +0000 |
parents | ec8a1e6443d5 |
children | 931bdbc37ee0 |
rev | line source |
---|---|
8197 | 1 |
2 #include <stdlib.h> | |
3 #include <stdio.h> | |
4 #include <ctype.h> | |
8623
440301fef3fe
Added/reordered #includes to silence warnings about "implicit declaration".
rathann
parents:
8197
diff
changeset
|
5 #include <string.h> |
8197 | 6 |
7 #include "../config.h" | |
8 | |
9 #include "img_format.h" | |
10 #include "mp_image.h" | |
11 | |
12 #include "m_struct.h" | |
13 #include "menu.h" | |
14 | |
15 #include "../libvo/font_load.h" | |
9380 | 16 #include "../osdep/keycodes.h" |
8197 | 17 |
18 #define IMPL 1 | |
19 #include "menu_list.h" | |
20 | |
21 #define mpriv (menu->priv) | |
22 | |
23 void menu_list_draw(menu_t* menu,mp_image_t* mpi) { | |
24 int x = mpriv->x; | |
25 int y = mpriv->y; | |
26 int i; | |
27 int h = mpriv->h; | |
28 int w = mpriv->w; | |
29 int dh = 0,dw = 0; | |
30 int dy = 0; | |
31 int need_h = 0,need_w = 0,ptr_l = menu_text_length(mpriv->ptr) + 10,sidx = 0; | |
32 int th; | |
33 list_entry_t* m; | |
34 | |
35 if(mpriv->count < 1) | |
36 return; | |
37 | |
38 if(h <= 0) h = mpi->height; | |
39 if(w <= 0) w = mpi->width; | |
40 dh = h - 2*mpriv->minb; | |
41 dw = w - 2*mpriv->minb; | |
42 ptr_l = menu_text_length(mpriv->ptr); | |
43 // mpi is too small | |
44 if(h - vo_font->height <= 0 || w - ptr_l <= 0 || dw <= 0 || dh <= 0) | |
45 return; | |
46 | |
47 th = menu_text_num_lines(mpriv->title,dw) * (mpriv->vspace + vo_font->height) + mpriv->vspace; | |
48 | |
49 for(i = 0, m = mpriv->menu ; m ; m = m->next, i++) { | |
50 int ll = menu_text_length(m->txt); | |
51 if(ptr_l + ll > need_w) need_w = ptr_l + ll; | |
52 if(m == mpriv->current) sidx = i; | |
53 } | |
54 if(need_w > dw) need_w = dw; | |
55 if(x > 0) | |
56 x += mpriv->minb; | |
57 if(y > 0) | |
58 y += mpriv->minb; | |
59 else | |
60 y = mpriv->minb; | |
61 | |
62 need_h = mpriv->count * (mpriv->vspace + vo_font->height) - mpriv->vspace; | |
63 if( need_h + th > dh) { | |
64 int start,end; | |
65 int maxl = (dh + mpriv->vspace - th) / (mpriv->vspace + vo_font->height); | |
66 if(maxl < 4) { | |
67 th = 0; | |
68 maxl = (dh + mpriv->vspace) / ( vo_font->height + mpriv->vspace); | |
69 } | |
70 // Too smoll | |
71 if(maxl < 1) return; | |
72 need_h = maxl*(mpriv->vspace + vo_font->height) - mpriv->vspace; | |
73 | |
74 start = sidx - (maxl/2); | |
75 if(start < 0) start = 0; | |
76 end = start + maxl; | |
77 if(end > mpriv->count) { | |
78 end = mpriv->count; | |
79 if(end - start < maxl) | |
80 start = end - maxl < 0 ? 0 : end - maxl; | |
81 } | |
82 m = mpriv->menu; | |
83 for(i = 0 ; m->next && i < start ; i++) | |
84 m = m->next; | |
85 } else | |
86 m = mpriv->menu; | |
87 | |
88 if(th > 0) { | |
89 menu_draw_text_full(mpi,mpriv->title, | |
90 x < 0 ? mpi->w / 2 : x, | |
91 dy+y,dw,0, | |
92 mpriv->vspace,1, | |
93 MENU_TEXT_TOP|MENU_TEXT_HCENTER, | |
94 MENU_TEXT_TOP|(x < 0 ? MENU_TEXT_HCENTER :MENU_TEXT_LEFT)); | |
95 dy += th; | |
96 } | |
97 | |
98 for( ; m != NULL && dy + vo_font->height < dh ; m = m->next ) { | |
99 if(m == mpriv->current) | |
100 menu_draw_text_full(mpi,mpriv->ptr, | |
101 x < 0 ? (mpi->w - need_w) / 2 + ptr_l : x, | |
102 dy+y,dw,dh - dy, | |
103 mpriv->vspace,0, | |
104 MENU_TEXT_TOP|(x < 0 ? MENU_TEXT_RIGHT :MENU_TEXT_LEFT) , | |
105 MENU_TEXT_TOP|(x < 0 ? MENU_TEXT_RIGHT :MENU_TEXT_LEFT)); | |
106 menu_draw_text_full(mpi,m->txt, | |
107 x < 0 ? (mpi->w - need_w) / 2 + ptr_l : x + ptr_l, | |
108 dy+y,dw-ptr_l,dh - dy, | |
109 mpriv->vspace,0, | |
110 MENU_TEXT_TOP|MENU_TEXT_LEFT, | |
111 MENU_TEXT_TOP|MENU_TEXT_LEFT); | |
112 dy += vo_font->height + mpriv->vspace; | |
113 } | |
114 | |
115 } | |
116 | |
117 void menu_list_read_cmd(menu_t* menu,int cmd) { | |
118 switch(cmd) { | |
119 case MENU_CMD_UP: | |
120 if(mpriv->current->prev) { | |
121 mpriv->current = mpriv->current->prev; | |
122 } else { | |
123 for( ; mpriv->current->next != NULL ; mpriv->current = mpriv->current->next) | |
124 /* NOTHING */; | |
125 } break; | |
126 case MENU_CMD_DOWN: | |
127 if(mpriv->current->next) { | |
128 mpriv->current = mpriv->current->next; | |
129 } else { | |
130 mpriv->current = mpriv->menu; | |
131 } break; | |
132 case MENU_CMD_CANCEL: | |
133 menu->show = 0; | |
134 menu->cl = 1; | |
135 break; | |
136 } | |
137 } | |
138 | |
139 void menu_list_jump_to_key(menu_t* menu,int c) { | |
10935
ec8a1e6443d5
Fix long-known bug with handling 'down key' in lists.
lumag
parents:
9380
diff
changeset
|
140 if(c < 256 && isalnum(c)) { |
8197 | 141 list_entry_t* e = mpriv->current; |
142 if(e->txt[0] == c) e = e->next; | |
143 for( ; e ; e = e->next) { | |
144 if(e->txt[0] == c) { | |
145 mpriv->current = e; | |
146 return; | |
147 } | |
148 } | |
149 for(e = mpriv->menu ; e ; e = e->next) { | |
150 if(e->txt[0] == c) { | |
151 mpriv->current = e; | |
152 return; | |
153 } | |
154 } | |
155 } else | |
156 menu_dflt_read_key(menu,c); | |
157 } | |
158 | |
159 void menu_list_read_key(menu_t* menu,int c,int jump_to) { | |
160 list_entry_t* m; | |
161 int i; | |
162 switch(c) { | |
163 case KEY_HOME: | |
164 mpriv->current = mpriv->menu; | |
165 break; | |
166 case KEY_END: | |
167 for(m = mpriv->current ; m && m->next ; m = m->next) | |
168 /**/; | |
169 if(m) | |
170 mpriv->current = m; | |
171 break; | |
172 case KEY_PAGE_UP: | |
173 for(i = 0, m = mpriv->current ; m && m->prev && i < 10 ; m = m->prev, i++) | |
174 /**/; | |
175 if(m) | |
176 mpriv->current = m; | |
177 break; | |
178 case KEY_PAGE_DOWN: | |
179 for(i = 0, m = mpriv->current ; m && m->next && i < 10 ; m = m->next, i++) | |
180 /**/; | |
181 if(m) | |
182 mpriv->current = m; | |
183 break; | |
184 default: | |
185 if(jump_to) | |
186 menu_list_jump_to_key(menu,c); | |
187 else | |
188 menu_dflt_read_key(menu,c); | |
189 } | |
190 } | |
191 | |
192 void menu_list_add_entry(menu_t* menu,list_entry_t* entry) { | |
193 list_entry_t* l; | |
194 mpriv->count++; | |
195 | |
196 if(mpriv->menu == NULL) { | |
197 mpriv->menu = mpriv->current = entry; | |
198 return; | |
199 } | |
200 | |
201 for(l = mpriv->menu ; l->next != NULL ; l = l->next) | |
202 /* NOP */; | |
203 l->next = entry; | |
204 entry->prev = l; | |
205 } | |
206 | |
207 void menu_list_init(menu_t* menu) { | |
208 if(!mpriv) | |
209 mpriv = calloc(1,sizeof(struct menu_priv_s)); | |
210 | |
211 } | |
212 | |
213 void menu_list_uninit(menu_t* menu,free_entry_t free_func) { | |
214 list_entry_t *i,*j; | |
215 | |
216 if(!free_func) free_func = (free_entry_t)free; | |
217 | |
218 for(i = mpriv->menu ; i != NULL ; ) { | |
219 j = i->next; | |
220 free_func(i); | |
221 i = j; | |
222 } | |
223 | |
224 mpriv->menu = mpriv->current = NULL; | |
225 | |
226 } |