~ubuntu-branches/debian/sid/mplayer/sid

« back to all changes in this revision

Viewing changes to libmenu/menu_list.c

  • Committer: Bazaar Package Importer
  • Author(s): A Mennucc1
  • Date: 2009-03-23 10:05:45 UTC
  • mfrom: (4.1.4 sid)
  • Revision ID: james.westby@ubuntu.com-20090323100545-x8h79obawnnte7kk
Tags: 1.0~rc2+svn20090303-5
debian/control : move docbook-xml,docbook-xsl,xsltproc from 
Build-Depends-Indep to Build-Depends, since they are needed to run
configure

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 */
1
18
 
2
19
#include <stdlib.h>
3
20
#include <stdio.h>
18
35
#define IMPL 1
19
36
#include "menu_list.h"
20
37
 
 
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;
 
47
 
21
48
#define mpriv (menu->priv)
22
49
 
23
50
void menu_list_draw(menu_t* menu,mp_image_t* mpi) {
27
54
  int h = mpriv->h;
28
55
  int w = mpriv->w;
29
56
  int dh = 0,dw =  0;
30
 
  int dy = 0;
 
57
  int bx, dx, dy = 0;
31
58
  int need_h = 0,need_w = 0,ptr_l,sidx = 0;
32
59
  int th,count = 0;
33
60
  int bg_w;
 
61
  int line_h;
34
62
  list_entry_t* m;
35
63
 
36
64
  if(mpriv->count < 1)
45
73
  if(h - vo_font->height <= 0 || w - ptr_l <= 0 || dw <= 0 || dh <= 0)
46
74
    return;
47
75
 
48
 
  th = menu_text_num_lines(mpriv->title,dw) * (mpriv->vspace + vo_font->height) + mpriv->vspace;
 
76
  line_h = mpriv->vspace + vo_font->height;
 
77
  th = menu_text_num_lines(mpriv->title,dw) * line_h + mpriv->vspace;
49
78
 
50
79
  // the selected item is hidden, find a visible one
51
80
  if(mpriv->current->hide) {
68
97
    count++;
69
98
  }
70
99
  if(need_w > dw) need_w = dw;
71
 
  if(x > 0)
 
100
  if(x >= 0)
72
101
    x += mpriv->minb;
73
102
  if(y > 0)
74
103
    y += mpriv->minb;
75
104
  else 
76
105
    y = mpriv->minb;
77
106
 
78
 
  need_h = count * (mpriv->vspace + vo_font->height) - mpriv->vspace;
 
107
  need_h = count * line_h - mpriv->vspace;
79
108
  if( need_h + th > dh) {
80
109
    int start,end;
81
 
    int maxl = (dh + mpriv->vspace - th) / (mpriv->vspace + vo_font->height);
82
 
    if(maxl < 4) {
 
110
    mpriv->disp_lines = (dh + mpriv->vspace - th) / line_h;
 
111
    if(mpriv->disp_lines < 4) {
83
112
      th = 0;
84
 
      maxl = (dh + mpriv->vspace) / ( vo_font->height + mpriv->vspace);
 
113
      mpriv->disp_lines = (dh + mpriv->vspace) / line_h;
85
114
    }
86
115
    // Too smoll
87
 
    if(maxl < 1) return;
88
 
    need_h = maxl*(mpriv->vspace + vo_font->height) - mpriv->vspace;
 
116
    if(mpriv->disp_lines < 1) return;
 
117
    need_h = mpriv->disp_lines * line_h - mpriv->vspace;
89
118
 
90
 
    start = sidx - (maxl/2);
 
119
    start = sidx - (mpriv->disp_lines/2);
91
120
    if(start < 0) start = 0;
92
 
    end = start + maxl;
 
121
    end = start + mpriv->disp_lines;
93
122
    if(end > count) {
94
123
      end = count;
95
 
      if(end - start < maxl)
96
 
        start = end - maxl < 0 ? 0 : end - maxl;
 
124
      if(end - start < mpriv->disp_lines)
 
125
        start = end - mpriv->disp_lines < 0 ? 0 : end - mpriv->disp_lines;
97
126
    }
98
127
    m = mpriv->menu;
99
128
    for(i = 0 ; m->next && i < start ; ) {
100
129
      if(!m->hide) i++;
101
130
      m = m->next;
102
131
    }
103
 
  } else
 
132
  } else {
104
133
    m = mpriv->menu;
 
134
    mpriv->disp_lines = count;
 
135
  }
105
136
 
106
137
  bg_w = need_w+2*mpriv->minb;
107
138
  if(th > 0) {
 
139
    int tw,th2;
 
140
    menu_text_size(mpriv->title,dw,mpriv->vspace,1,&tw,&th2);
108
141
    if(mpriv->title_bg >= 0) {
109
 
      int tw,th2;
110
 
      menu_text_size(mpriv->title,dw,mpriv->vspace,1,&tw,&th2);
111
142
      if(tw+2*mpriv->minb > bg_w) bg_w = tw+2*mpriv->minb;
112
143
      menu_draw_box(mpi,mpriv->title_bg,mpriv->title_bg_alpha,
113
144
                    x < 0 ? (mpi->w-bg_w)/2 : x-mpriv->minb,dy+y-mpriv->vspace/2,bg_w,th);
114
145
    }
115
146
    menu_draw_text_full(mpi,mpriv->title,
116
147
                        x < 0 ? mpi->w / 2 : x,
117
 
                        dy+y,dw,0,
 
148
                        dy+y, x < 0 ? dw : (tw > need_w ? tw : need_w), 0,
118
149
                        mpriv->vspace,1,
119
150
                        MENU_TEXT_TOP|MENU_TEXT_HCENTER,
120
151
                        MENU_TEXT_TOP|(x < 0 ? MENU_TEXT_HCENTER :MENU_TEXT_LEFT));
121
152
    dy += th;
122
153
  }
123
154
  
 
155
  dx = x < 0 ? (mpi->w - need_w) / 2 : x;
 
156
  bx = x < 0 ? (mpi->w - bg_w) / 2 : x - mpriv->minb;
 
157
 
 
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;
 
173
          }
 
174
          ++i;
 
175
        }
 
176
      }
 
177
    }
 
178
    menu_mouse_pos_updated = 0;
 
179
  }
 
180
 
124
181
  for( ; m != NULL && dy + vo_font->height < dh ; m = m->next ) {
125
182
    if(m->hide) continue;
126
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;
 
189
 
127
190
      if(mpriv->ptr_bg >= 0)
128
191
        menu_draw_box(mpi,mpriv->ptr_bg,mpriv->ptr_bg_alpha,
129
 
                      x < 0 ? (mpi->w-bg_w)/2 : x-mpriv->minb,dy+y-mpriv->vspace/2,
130
 
                      bg_w,vo_font->height + mpriv->vspace);
 
192
                      bx, dy + y - mpriv->vspace / 2,
 
193
                      bg_w, line_h);
131
194
      if(ptr_l > 0)
132
195
        menu_draw_text_full(mpi,mpriv->ptr,
133
 
                            x < 0 ? (mpi->w - need_w) / 2 + ptr_l : x,
 
196
                            dx,
134
197
                            dy+y,dw,dh - dy,
135
198
                            mpriv->vspace,0,
136
 
                            MENU_TEXT_TOP|(x < 0 ? MENU_TEXT_RIGHT :MENU_TEXT_LEFT) ,
137
 
                            MENU_TEXT_TOP|(x < 0 ? MENU_TEXT_RIGHT :MENU_TEXT_LEFT));
 
199
                            MENU_TEXT_TOP|MENU_TEXT_LEFT,
 
200
                            MENU_TEXT_TOP|MENU_TEXT_LEFT);
138
201
    } else if(mpriv->item_bg >= 0)
139
202
      menu_draw_box(mpi,mpriv->item_bg,mpriv->item_bg_alpha,
140
 
                    x < 0 ? (mpi->w-bg_w)/2 : x-mpriv->minb,dy+y-mpriv->vspace/2,
141
 
                    bg_w,vo_font->height + mpriv->vspace);
 
203
                    bx, dy + y - mpriv->vspace / 2,
 
204
                    bg_w, line_h);
142
205
    menu_draw_text_full(mpi,m->txt,
143
 
                        x < 0 ? (mpi->w - need_w) / 2  + ptr_l : x + ptr_l,
 
206
                        dx + ptr_l,
144
207
                        dy+y,dw-ptr_l,dh - dy,
145
208
                        mpriv->vspace,0,
146
209
                        MENU_TEXT_TOP|MENU_TEXT_LEFT,
147
210
                        MENU_TEXT_TOP|MENU_TEXT_LEFT);
148
 
    dy +=  vo_font->height + mpriv->vspace;
 
211
    dy += line_h;
149
212
  }
150
213
 
151
214
}
152
215
 
153
216
void menu_list_read_cmd(menu_t* menu,int cmd) {
 
217
  list_entry_t* m;
 
218
  int i;
154
219
  switch(cmd) {
155
220
  case MENU_CMD_UP:
156
221
    while(mpriv->current->prev) {
177
242
      if(!mpriv->current->hide) return;
178
243
    }
179
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;
180
266
  case MENU_CMD_LEFT:
181
267
  case MENU_CMD_CANCEL:
182
268
    menu->show = 0;
183
269
    menu->cl = 1;
184
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;
185
276
  }    
186
277
}
187
278
 
188
 
void menu_list_jump_to_key(menu_t* menu,int c) {
 
279
int menu_list_jump_to_key(menu_t* menu,int c) {
189
280
  if(c < 256 && isalnum(c)) {
190
281
    list_entry_t* e = mpriv->current;
191
282
    if(e->txt[0] == c) e = e->next;
192
283
    for(  ; e ; e = e->next) {
193
284
        if(e->txt[0] == c) {
194
285
          mpriv->current = e;
195
 
          return;
 
286
          return 1;
196
287
        }
197
288
    }
198
289
    for(e = mpriv->menu ; e ;  e = e->next) {
199
290
        if(e->txt[0] == c) {
200
291
          mpriv->current = e;
201
 
          return;
 
292
          return 1;
202
293
        }
203
294
    }
204
 
  } else
205
 
    menu_dflt_read_key(menu,c);
206
 
}
207
 
 
208
 
void menu_list_read_key(menu_t* menu,int c,int jump_to) {
209
 
  list_entry_t* m;
210
 
  int i;
211
 
  switch(c) {
212
 
  case KEY_HOME:
213
 
    mpriv->current = mpriv->menu;
214
 
    break;
215
 
  case KEY_END:
216
 
    for(m = mpriv->current ; m && m->next ; m = m->next)
217
 
      /**/;
218
 
    if(m)
219
 
      mpriv->current = m;
220
 
    break;
221
 
  case KEY_PAGE_UP:
222
 
    for(i = 0, m = mpriv->current ; m && m->prev && i < 10 ; m = m->prev, i++)
223
 
      /**/;
224
 
    if(m)
225
 
      mpriv->current = m;
226
 
    break;
227
 
  case KEY_PAGE_DOWN:
228
 
    for(i = 0, m = mpriv->current ; m && m->next && i < 10 ; m = m->next, i++)
229
 
      /**/;
230
 
    if(m)
231
 
      mpriv->current = m;
232
 
    break;
233
 
  default:
234
 
    if(jump_to)
235
 
      menu_list_jump_to_key(menu,c);
236
 
    else
237
 
      menu_dflt_read_key(menu,c);
238
 
  }    
 
295
    return 1;
 
296
  }
 
297
  return 0;
239
298
}
240
299
 
241
300
void menu_list_add_entry(menu_t* menu,list_entry_t* entry) {