1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
3 Copyright (C) 2002-2013 Ben Kibbey <bjk@luxsci.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
40 static void set_menu_vars(int c
, int rows
, int items
, int *item
, int *top
)
47 selected
= toppos
= 0;
51 toppos
= items
- rows
+ 1;
54 if (selected
- 1 < 0) {
57 toppos
= selected
- rows
+ 1;
62 if (toppos
&& selected
<= toppos
)
67 if (selected
+ 1 > items
)
68 selected
= toppos
= 0;
72 if (selected
- toppos
>= rows
)
82 toppos
= selected
- rows
+ 1;
93 toppos
= selected
- rows
+ 1;
99 if (selected
== MAX_MENU_HEIGHT
- 4)
101 else if (selected
<= rows
)
104 if (selected
- toppos
> rows
)
105 toppos
= selected
- rows
+ 1;
113 if (selected
>= items
) {
115 toppos
= selected
- rows
+ 1;
125 static void fix_menu_vals(WIN
*win
)
127 struct menu_input_s
*m
= win
->data
;
132 int n
, nlen
= 0, vlen
= 0;
134 for (i
= 0; m
->items
[i
]; i
++);
136 snprintf(buf
, sizeof(buf
), _("Item %i %s %i %s"), m
->selected
+ 1,
137 _("of"), m
->total
, _("Type F1 for help"));
142 for (i
= 0; m
->items
[i
]; i
++) {
143 wc
= str_to_wchar (m
->items
[i
]->name
);
149 if (m
->items
[i
]->value
) {
150 wc
= str_to_wchar (m
->items
[i
]->value
);
159 char *s
= _("empty value");
161 n
= (!m
->name_only
) ? mblen(s
, strlen (s
)) + nlen
: nlen
;
172 if (!m
->rstatic
&& win
->title
)
176 win
->cols
+= (!m
->name_only
) ? 5 : 2; // 2 box, 3 separator
179 win
->rows
+= 3; // 2 box, 1 prompt
181 if (!m
->rstatic
&& win
->rows
> MAX_MENU_HEIGHT
)
182 win
->rows
= MAX_MENU_HEIGHT
;
184 wc
= str_to_wchar (buf
);
189 win
->cols
= len
+ 2; // 2 box
191 if (win
->cols
> MAX_MENU_WIDTH
)
192 win
->cols
= MAX_MENU_WIDTH
;
194 wresize(win
->w
, win
->rows
, win
->cols
);
195 replace_panel(win
->p
, win
->w
);
196 move_panel(win
->p
, (m
->ystatic
== -1) ? CALCPOSY(win
->rows
) : m
->ystatic
,
197 (m
->xstatic
== -1) ? CALCPOSX(win
->cols
) : m
->xstatic
);
198 keypad(win
->w
, TRUE
);
201 window_draw_title(win
->w
, win
->title
, win
->cols
, CP_INPUT_TITLE
,
203 window_draw_prompt(win
->w
, win
->rows
- 2, win
->cols
, buf
, CP_INPUT_PROMPT
);
206 static void draw_menu(WIN
*win
)
210 struct menu_input_s
*m
= win
->data
;
215 for (i
= m
->top
, y
= 2; m
->items
[i
] && y
< win
->rows
- 2; i
++, y
++) {
216 if (i
== m
->selected
)
217 wattron(win
->w
, CP_MENU_SELECTED
);
218 else if (m
->items
[i
]->selected
)
219 wattron(win
->w
, CP_MENU_HIGHLIGHT
);
223 m
->item
= m
->items
[i
];
224 (*m
->print_func
)(win
);
227 if (i
== m
->selected
)
228 wattroff(win
->w
, CP_MENU_SELECTED
);
229 else if (m
->items
[i
]->selected
)
230 wattroff(win
->w
, CP_MENU_HIGHLIGHT
);
234 static int display_menu(WIN
*win
)
236 struct menu_input_s
*m
= win
->data
;
242 keypad(win
->w
, TRUE
);
246 for (i
= 0; m
->keys
[i
]; i
++) {
247 if (win
->c
== m
->keys
[i
]->c
) {
248 (*m
->keys
[i
]->func
)(m
);
249 m
->items
= (*m
->func
)(win
);
258 m
->items
= (*m
->func
)(win
);
276 if (strlen(m
->search
) + 1 > sizeof(m
->search
) - 1)
289 for (i
= 0; m
->items
[i
]; i
++) {
290 if (strncasecmp(m
->search
, m
->items
[i
]->name
,
291 strlen(m
->search
)) == 0) {
298 if (n
== m
->selected
)
303 set_menu_vars(win
->c
, win
->rows
- 4, m
->total
- 1, &m
->selected
, &m
->top
);
307 if (m
->draw_exit_func
)
308 (*m
->draw_exit_func
)(m
);
317 for (i
= 0; m
->items
[i
]; i
++) {
319 free(m
->items
[i
]->name
);
321 if (!m
->nofree
&& m
->items
[i
]->value
)
322 free(m
->items
[i
]->value
);
331 for (i
= 0; m
->keys
[i
]; i
++)
342 WIN
*construct_menu(int rows
, int cols
, int y
, int x
, const char *title
,
343 int name_only
, menu_items_fn
*func
,
344 struct menu_key_s
**keys
, void *data
,
345 menu_print_func
*pfunc
, window_exit_func
*efunc
)
348 struct menu_input_s
*m
;
350 m
= Calloc(1, sizeof(struct menu_input_s
));
351 win
= window_create(title
, (rows
<= 0) ? 1 : rows
, (cols
<= 0) ? 1 : cols
,
352 (y
>= 0) ? y
: 0, (x
>= 0) ? x
: 0, display_menu
, m
, efunc
);
363 m
->print_func
= pfunc
;
367 m
->name_only
= name_only
;
368 wbkgd(win
->w
, CP_MENU
);
371 keypad(win
->w
, TRUE
);
373 m
->items
= (*m
->func
)(win
);
379 void add_menu_key(struct menu_key_s
***dst
, wint_t c
, menu_key func
)
382 struct menu_key_s
**keys
= *dst
;
385 for (; keys
[n
]; n
++);
387 keys
= Realloc(keys
, (n
+ 2) * sizeof(struct menu_key_s
*));
388 keys
[n
] = Malloc(sizeof(struct menu_key_s
));
390 keys
[n
++]->func
= func
;