1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
3 Copyright (C) 2002-2018 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
41 set_menu_vars (int c
, int rows
, int items
, int *item
, int *top
)
49 selected
= toppos
= 0;
53 toppos
= items
- rows
+ 1;
60 toppos
= selected
- rows
+ 1;
66 if (toppos
&& selected
<= toppos
)
71 if (selected
+ 1 > items
)
72 selected
= toppos
= 0;
77 if (selected
- toppos
>= rows
)
87 toppos
= selected
- rows
+ 1;
98 toppos
= selected
- rows
+ 1;
104 if (selected
== MAX_MENU_HEIGHT
- 4)
106 else if (selected
<= rows
)
110 if (selected
- toppos
> rows
)
111 toppos
= selected
- rows
+ 1;
119 if (selected
>= items
)
122 toppos
= selected
- rows
+ 1;
133 fix_menu_vals (WIN
* win
)
135 struct menu_input_s
*m
= win
->data
;
140 int n
, nlen
= 0, vlen
= 0;
142 for (i
= 0; m
->items
[i
]; i
++);
144 snprintf (buf
, sizeof (buf
), _("Item %i %s %i %s"), m
->selected
+ 1,
145 _("of"), m
->total
, _("Type F1 for help"));
151 for (i
= 0; m
->items
[i
]; i
++)
153 wc
= str_to_wchar (m
->items
[i
]->name
);
159 if (m
->items
[i
]->value
)
161 wc
= str_to_wchar (m
->items
[i
]->value
);
171 char *s
= _("empty value");
173 n
= (!m
->name_only
) ? mblen (s
, strlen (s
)) + nlen
: nlen
;
184 if (!m
->rstatic
&& win
->title
)
188 win
->cols
+= (!m
->name_only
) ? 5 : 2; // 2 box, 3 separator
191 win
->rows
+= 3; // 2 box, 1 prompt
193 if (!m
->rstatic
&& win
->rows
> MAX_MENU_HEIGHT
)
194 win
->rows
= MAX_MENU_HEIGHT
;
196 wc
= str_to_wchar (buf
);
201 win
->cols
= len
+ 2; // 2 box
203 if (win
->cols
> MAX_MENU_WIDTH
)
204 win
->cols
= MAX_MENU_WIDTH
;
206 wresize (win
->w
, win
->rows
, win
->cols
);
207 replace_panel (win
->p
, win
->w
);
208 move_panel (win
->p
, (m
->ystatic
== -1) ? CALCPOSY (win
->rows
) : m
->ystatic
,
209 (m
->xstatic
== -1) ? CALCPOSX (win
->cols
) : m
->xstatic
);
210 keypad (win
->w
, TRUE
);
211 wmove (win
->w
, 0, 0);
213 window_draw_title (win
->w
, win
->title
, win
->cols
, CP_INPUT_TITLE
,
215 window_draw_prompt (win
->w
, win
->rows
- 2, win
->cols
, buf
, CP_INPUT_PROMPT
);
219 redraw_menu (WIN
* win
)
223 struct menu_input_s
*m
= win
->data
;
230 for (i
= m
->top
, y
= 2; m
->items
[i
] && y
< win
->rows
- 2; i
++, y
++)
232 if (i
== m
->selected
)
233 wattron (win
->w
, CP_MENU_SELECTED
);
234 else if (m
->items
[i
]->selected
)
235 wattron (win
->w
, CP_MENU_HIGHLIGHT
);
240 m
->item
= m
->items
[i
];
241 (*m
->print_func
) (win
);
244 if (i
== m
->selected
)
245 wattroff (win
->w
, CP_MENU_SELECTED
);
246 else if (m
->items
[i
]->selected
)
247 wattroff (win
->w
, CP_MENU_HIGHLIGHT
);
252 display_menu (WIN
* win
)
254 struct menu_input_s
*m
= win
->data
;
260 keypad (win
->w
, TRUE
);
265 for (i
= 0; m
->keys
[i
]; i
++)
267 if (win
->c
== m
->keys
[i
]->c
)
269 (*m
->keys
[i
]->func
) (m
);
270 m
->items
= (*m
->func
) (win
);
280 m
->items
= (*m
->func
) (win
);
298 if (strlen (m
->search
) + 1 > sizeof (m
->search
) - 1)
312 for (i
= 0; m
->items
[i
]; i
++)
314 if (strncasecmp (m
->search
, m
->items
[i
]->name
,
315 strlen (m
->search
)) == 0)
323 if (n
== m
->selected
)
328 set_menu_vars (win
->c
, win
->rows
- 4, m
->total
- 1, &m
->selected
, &m
->top
);
331 if (m
->draw_exit_func
)
332 (*m
->draw_exit_func
) (m
);
342 for (i
= 0; m
->items
[i
]; i
++)
345 free (m
->items
[i
]->name
);
347 if (!m
->nofree
&& m
->items
[i
]->value
)
348 free (m
->items
[i
]->value
);
358 for (i
= 0; m
->keys
[i
]; i
++)
370 menu_resize_func (WIN
*w
)
372 struct menu_input_s
*m
= w
->data
;
374 w
->rows
= m
->total
>= LINES
- 5 ? LINES
- 1 : m
->total
+ 5;
375 w
->cols
= w
->cols
> COLS
- 2 ? COLS
- 2 : w
->cols
;
376 wresize (w
->w
, w
->rows
, w
->cols
);
377 move_panel (w
->p
, CALCPOSY (w
->rows
), CALCPOSX (w
->cols
));
383 construct_menu (int rows
, int cols
, int y
, int x
, const char *title
,
384 int name_only
, menu_items_fn
* func
,
385 struct menu_key_s
** keys
, void *data
,
386 menu_print_func
* pfunc
, window_exit_func
* efunc
,
387 window_resize_func
*rfunc
)
390 struct menu_input_s
*m
;
392 m
= Calloc (1, sizeof (struct menu_input_s
));
393 win
= window_create (title
, (rows
<= 0) ? 1 : rows
, (cols
<= 0) ? 1 : cols
,
394 (y
>= 0) ? y
: 0, (x
>= 0) ? x
: 0, display_menu
, m
,
395 efunc
, rfunc
? rfunc
: menu_resize_func
);
406 m
->print_func
= pfunc
;
410 m
->name_only
= name_only
;
411 wbkgd (win
->w
, CP_MENU
);
414 keypad (win
->w
, TRUE
);
416 m
->items
= (*m
->func
) (win
);
423 add_menu_key (struct menu_key_s
***dst
, wint_t c
, menu_key func
)
426 struct menu_key_s
**keys
= *dst
;
429 for (; keys
[n
]; n
++);
431 keys
= Realloc (keys
, (n
+ 2) * sizeof (struct menu_key_s
*));
432 keys
[n
] = Malloc (sizeof (struct menu_key_s
));
434 keys
[n
++]->func
= func
;