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
;
131 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 N_OF_N_STR
, m
->total
, HELP_PROMPT
);
143 for (i
= 0; m
->items
[i
]; i
++) {
144 n
= strlen(m
->items
[i
]->name
);
149 if (m
->items
[i
]->value
) {
150 n
= strlen(m
->items
[i
]->value
);
158 n
= (!m
->name_only
) ? strlen(UNKNOWN
) + nlen
: nlen
;
168 if (!m
->rstatic
&& win
->title
)
172 win
->cols
+= (!m
->name_only
) ? 5 : 2; // 2 box, 3 separator
175 win
->rows
+= 3; // 2 box, 1 prompt
177 if (!m
->rstatic
&& win
->rows
> MAX_MENU_HEIGHT
)
178 win
->rows
= MAX_MENU_HEIGHT
;
180 if (win
->cols
< strlen(buf
))
181 win
->cols
= strlen(buf
) + 2; // 2 box
183 if (win
->cols
> MAX_MENU_WIDTH
)
184 win
->cols
= MAX_MENU_WIDTH
;
186 wresize(win
->w
, win
->rows
, win
->cols
);
187 replace_panel(win
->p
, win
->w
);
189 move_panel(win
->p
, (m
->ystatic
== -1) ? CALCPOSY(win
->rows
) : m
->ystatic
,
190 (m
->xstatic
== -1) ? CALCPOSX(win
->cols
) : m
->xstatic
);
191 keypad(win
->w
, TRUE
);
194 window_draw_title(win
->w
, win
->title
, win
->cols
, CP_INPUT_TITLE
,
196 window_draw_prompt(win
->w
, win
->rows
- 2, win
->cols
, buf
, CP_INPUT_PROMPT
);
199 static void draw_menu(WIN
*win
)
203 struct menu_input_s
*m
= win
->data
;
208 for (i
= m
->top
, y
= 2; m
->items
[i
] && y
< win
->rows
- 2; i
++, y
++) {
209 if (i
== m
->selected
)
210 wattron(win
->w
, CP_MENU_SELECTED
);
211 else if (m
->items
[i
]->selected
)
212 wattron(win
->w
, CP_MENU_HIGHLIGHT
);
216 m
->item
= m
->items
[i
];
217 (*m
->print_func
)(win
);
220 if (i
== m
->selected
)
221 wattroff(win
->w
, CP_MENU_SELECTED
);
222 else if (m
->items
[i
]->selected
)
223 wattroff(win
->w
, CP_MENU_HIGHLIGHT
);
227 static int display_menu(WIN
*win
)
229 struct menu_input_s
*m
= win
->data
;
235 keypad(win
->w
, TRUE
);
239 for (i
= 0; m
->keys
[i
]; i
++) {
240 if (win
->c
== m
->keys
[i
]->c
) {
241 (*m
->keys
[i
]->func
)(m
);
242 m
->items
= (*m
->func
)(win
);
251 m
->items
= (*m
->func
)(win
);
269 if (strlen(m
->search
) + 1 > sizeof(m
->search
) - 1)
282 for (i
= 0; m
->items
[i
]; i
++) {
283 if (strncasecmp(m
->search
, m
->items
[i
]->name
,
284 strlen(m
->search
)) == 0) {
291 if (n
== m
->selected
)
296 set_menu_vars(win
->c
, win
->rows
- 4, m
->total
- 1, &m
->selected
, &m
->top
);
300 if (m
->draw_exit_func
)
301 (*m
->draw_exit_func
)(m
);
310 for (i
= 0; m
->items
[i
]; i
++) {
312 free(m
->items
[i
]->name
);
314 if (!m
->nofree
&& m
->items
[i
]->value
)
315 free(m
->items
[i
]->value
);
324 for (i
= 0; m
->keys
[i
]; i
++)
335 WIN
*construct_menu(int rows
, int cols
, int y
, int x
, const char *title
,
336 int name_only
, menu_items_fn
*func
,
337 struct menu_key_s
**keys
, void *data
,
338 menu_print_func
*pfunc
, window_exit_func
*efunc
)
341 struct menu_input_s
*m
;
345 rows
= MAX_MENU_HEIGHT
;
348 cols
= MAX_MENU_WIDTH
;
351 m
= Calloc(1, sizeof(struct menu_input_s
));
352 win
= window_create(title
, (rows
<= 0) ? 1 : rows
, (cols
<= 0) ? 1 : cols
,
353 (y
>= 0) ? y
: 0, (x
>= 0) ? x
: 0, display_menu
, m
, efunc
);
369 m
->print_func
= pfunc
;
373 m
->name_only
= name_only
;
374 wbkgd(win
->w
, CP_MENU
);
377 keypad(win
->w
, TRUE
);
379 m
->items
= (*m
->func
)(win
);
385 void add_menu_key(struct menu_key_s
***dst
, int c
, menu_key func
)
388 struct menu_key_s
**keys
= *dst
;
391 for (; keys
[n
]; n
++);
393 keys
= Realloc(keys
, (n
+ 2) * sizeof(struct menu_key_s
*));
394 keys
[n
] = Malloc(sizeof(struct menu_key_s
));
396 keys
[n
++]->func
= func
;