1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
3 Copyright (C) 2002-2006 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
44 void set_menu_vars(int c
, int rows
, int items
, int *item
, int *top
)
51 selected
= toppos
= 0;
55 toppos
= items
- rows
+ 1;
58 if (selected
- 1 < 0) {
61 toppos
= selected
- rows
+ 1;
66 if (toppos
&& selected
<= toppos
)
71 if (selected
+ 1 > items
)
72 selected
= toppos
= 0;
76 if (selected
- toppos
>= rows
)
86 toppos
= selected
- rows
+ 1;
97 toppos
= selected
- rows
+ 1;
103 if (selected
== MAX_MENU_HEIGHT
- 4)
105 else if (selected
<= rows
)
108 toppos
= selected
- rows
+ 1;
115 if (selected
> items
) {
117 toppos
= selected
- rows
+ 1;
127 static void fix_menu_vals(WIN
*win
)
129 struct menu_input_s
*m
= win
->data
;
137 for (i
= 0; m
->items
[i
]; i
++) {
138 n
= strlen(m
->items
[i
]->name
);
143 if (m
->items
[i
]->value
)
144 n
= strlen(m
->items
[i
]->value
) + nlen
;
146 n
= (!m
->name_only
) ? strlen(UNKNOWN
) + nlen
: nlen
;
156 for (i
= 0; m
->items
[i
]; i
++);
159 if (!m
->rstatic
&& m
->title
)
163 win
->cols
+= (!m
->name_only
) ? 5 : 2; // 2 box, 3 separator
166 win
->rows
+= 3; // 2 box, 1 prompt
168 if (!m
->rstatic
&& win
->rows
> MAX_MENU_HEIGHT
)
169 win
->rows
= MAX_MENU_HEIGHT
;
171 snprintf(buf
, sizeof(buf
), "Item %i %s %i %s", m
->selected
+ 1,
172 N_OF_N_STR
, m
->total
, HELP_PROMPT
);
174 if (win
->cols
< strlen(buf
))
175 win
->cols
= strlen(buf
) + 2; // 2 box
177 if (win
->cols
> MAX_MENU_WIDTH
)
178 win
->cols
= MAX_MENU_WIDTH
;
180 wresize(win
->w
, win
->rows
, win
->cols
);
181 replace_panel(win
->p
, win
->w
);
182 move_panel(win
->p
, (m
->ystatic
== -1) ? CALCPOSY(win
->rows
) : m
->ystatic
,
183 (m
->xstatic
== -1) ? CALCPOSX(win
->cols
) : m
->xstatic
);
184 keypad(win
->w
, TRUE
);
187 window_draw_title(win
->w
, m
->title
, win
->cols
, CP_INPUT_TITLE
,
189 window_draw_prompt(win
->w
, win
->rows
- 2, win
->cols
, buf
, CP_INPUT_PROMPT
);
192 static void draw_menu(WIN
*win
)
196 struct menu_input_s
*m
= win
->data
;
201 for (i
= m
->top
, y
= 2; m
->items
[i
] && y
< win
->rows
- 2; i
++, y
++) {
202 if (i
== m
->selected
)
203 wattron(win
->w
, CP_MENU_SELECTED
);
204 else if (m
->items
[i
]->selected
)
205 wattron(win
->w
, CP_MENU_HIGHLIGHT
);
209 m
->item
= m
->items
[i
];
210 (*m
->print_func
)(win
);
213 if (i
== m
->selected
)
214 wattroff(win
->w
, CP_MENU_SELECTED
);
215 else if (m
->items
[i
]->selected
)
216 wattroff(win
->w
, CP_MENU_HIGHLIGHT
);
220 int display_menu(WIN
*win
)
222 struct menu_input_s
*m
= win
->data
;
229 keypad(win
->w
, TRUE
);
233 for (i
= 0; m
->keys
[i
]; i
++) {
234 if (win
->c
== m
->keys
[i
]->c
) {
235 (*m
->keys
[i
]->func
)(m
);
236 m
->items
= (*m
->func
)(win
);
246 m
->items
= (*m
->func
)(win
);
264 if (strlen(m
->search
) + 1 > sizeof(m
->search
) - 1)
277 for (i
= 0; m
->items
[i
]; i
++) {
278 if (strncasecmp(m
->search
, m
->items
[i
]->name
,
279 strlen(m
->search
)) == 0) {
286 if (n
== m
->selected
)
291 set_menu_vars(win
->c
, win
->rows
- 4, m
->total
- 1, &m
->selected
, &m
->top
);
295 if (m
->draw_exit_func
)
296 (*m
->draw_exit_func
)(m
);
304 for (i
= 0; m
->items
[i
]; i
++) {
306 free(m
->items
[i
]->name
);
308 if (!m
->nofree
&& m
->items
[i
]->value
)
309 free(m
->items
[i
]->value
);
318 for (i
= 0; m
->keys
[i
]; i
++)
331 WIN
*construct_menu(int rows
, int cols
, int y
, int x
, const char *title
,
332 int name_only
, menu_items
*func
, struct menu_key_s
**keys
, void *data
,
333 menu_print_func
*pfunc
, window_exit_func
*efunc
)
337 struct menu_input_s
*m
;
339 m
= Calloc(1, sizeof(struct menu_input_s
));
340 win
= window_create(rows
, cols
, (y
>= 0) ? y
: CALCPOSY(h
),
341 (x
>= 0) ? x
: CALCPOSX(w
), display_menu
, m
, efunc
);
352 m
->print_func
= pfunc
;
356 m
->name_only
= name_only
;
359 m
->title
= strdup(title
);
361 wbkgd(win
->w
, CP_MENU
);
364 keypad(win
->w
, TRUE
);
366 m
->items
= (*m
->func
)(win
);
372 void add_menu_key(struct menu_key_s
***dst
, int c
, menu_key func
)
375 struct menu_key_s
**keys
= *dst
;
378 for (; keys
[n
]; n
++);
380 keys
= Realloc(keys
, (n
+ 2) * sizeof(struct menu_key_s
*));
381 keys
[n
] = Malloc(sizeof(struct menu_key_s
));
383 keys
[n
++]->func
= func
;