2 * --- T2-COPYRIGHT-NOTE-BEGIN ---
3 * This copyright note is auto-generated by ./scripts/Create-CopyPatch.
5 * T2 SDE: misc/confdialog/menubox.c
6 * Copyright (C) 2004 - 2005 The T2 SDE Project
7 * Copyright (C) 1998 - 2003 ROCK Linux Project
9 * More information can be found in the files COPYING and README.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; version 2 of the License. A copy of the
14 * GNU General Public License can be found in the file COPYING.
15 * --- T2-COPYRIGHT-NOTE-END ---
18 * menubox.c -- implements the menu box
20 * ORIGINAL AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
21 * MODIFIED FOR LINUX KERNEL CONFIG BY: William Roadcap (roadcapw@cfw.com)
22 * MODIFIED FOR ROCK LINUX CONFIG BY: Clifford Wolf (clifford@clifford.at)
24 * This program is free software; you can redistribute it and/or
25 * modify it under the terms of the GNU General Public License
26 * as published by the Free Software Foundation; either version 2
27 * of the License, or (at your option) any later version.
29 * This program is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU General Public License for more details.
34 * You should have received a copy of the GNU General Public License
35 * along with this program; if not, write to the Free Software
36 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
40 * Changes by Clifford Wolf (god@clifford.at)
44 * *) A bugfix for the Page-Down problem
46 * *) Formerly when I used Page Down and Page Up, the cursor would be set
47 * to the first position in the menu box. Now lxdialog is a bit
48 * smarter and works more like other menu systems (just have a look at
51 * *) Formerly if I selected something my scrolling would be broken because
52 * lxdialog is re-invoked by the Menuconfig shell script, can't
53 * remember the last scrolling position, and just sets it so that the
54 * cursor is at the bottom of the box. Now it writes the temporary file
55 * rockdialog.scrltmp which contains this information. The file is
56 * deleted by lxdialog if the user leaves a submenu or enters a new
57 * one, but it would be nice if Menuconfig could make another "rm -f"
58 * just to be sure. Just try it out - you will recognise a difference!
62 * *) Now lxdialog is crash-safe against broken "rockdialog.scrltmp" files
63 * and menus change their size on the fly.
65 * *) If for some reason the last scrolling position is not saved by
66 * lxdialog, it sets the scrolling so that the selected item is in the
67 * middle of the menu box, not at the bottom.
69 * 02 January 1999, Michael Elizabeth Chastain (mec@shout.net)
70 * Reset 'scroll' to 0 if the value from rockdialog.scrltmp is bogus.
71 * This fixes a bug in Menuconfig where using ' ' to descend into menus
72 * would leave mis-synchronized rockdialog.scrltmp files lying around,
73 * fscanf would read in 'scroll', and eventually that value would get used.
78 static int menu_width
, item_x
;
84 print_item (WINDOW
* win
, const char *item
, int choice
, int selected
, int hotkey
)
87 char menu_item
[menu_width
+1];
89 strncpy(menu_item
, item
, menu_width
);
90 menu_item
[menu_width
] = 0;
91 j
= first_alpha(menu_item
, "YyNnMm");
93 /* Clear 'residue' of last item */
94 wattrset (win
, menubox_attr
);
95 wmove (win
, choice
, 0);
99 for (i
= 0; i
< menu_width
; i
++)
105 wattrset (win
, selected
? item_selected_attr
: item_attr
);
106 mvwaddstr (win
, choice
, item_x
, menu_item
);
108 wattrset (win
, selected
? tag_key_selected_attr
: tag_key_attr
);
109 mvwaddch(win
, choice
, item_x
+j
, menu_item
[j
]);
112 wmove (win
, choice
, item_x
+1);
118 * Print the scroll indicators.
121 print_arrows (WINDOW
* win
, int item_no
, int scroll
,
122 int y
, int x
, int height
)
126 getyx(win
, cur_y
, cur_x
);
131 wattrset (win
, uarrow_attr
);
132 waddch (win
, ACS_UARROW
);
133 waddstr (win
, "(-)");
136 wattrset (win
, menubox_attr
);
137 waddch (win
, ACS_HLINE
);
138 waddch (win
, ACS_HLINE
);
139 waddch (win
, ACS_HLINE
);
140 waddch (win
, ACS_HLINE
);
146 if ((height
< item_no
) && (scroll
+ height
< item_no
)) {
147 wattrset (win
, darrow_attr
);
148 waddch (win
, ACS_DARROW
);
149 waddstr (win
, "(+)");
152 wattrset (win
, menubox_border_attr
);
153 waddch (win
, ACS_HLINE
);
154 waddch (win
, ACS_HLINE
);
155 waddch (win
, ACS_HLINE
);
156 waddch (win
, ACS_HLINE
);
159 wmove(win
, cur_y
, cur_x
);
163 * Display the termination buttons.
166 print_buttons (WINDOW
*win
, int height
, int width
, int selected
)
168 int x
= width
/ 2 - 16;
171 print_button (win
, "Select", y
, x
, selected
== 0);
172 print_button (win
, " Exit ", y
, x
+ 12, selected
== 1);
173 print_button (win
, " Help ", y
, x
+ 24, selected
== 2);
175 wmove(win
, y
, x
+1+12*selected
);
180 * Display a menu for choosing among a number of options
183 dialog_menu (const char *title
, const char *prompt
, int height
, int width
,
184 int menu_height
, const char *current
, int item_no
,
185 const char * const * items
)
188 int i
, j
, x
, y
, box_x
, box_y
;
189 int key
= 0, button
= 0, scroll
= 0, choice
= 0, first_item
= 0, max_choice
;
190 WINDOW
*dialog
, *menu
;
193 max_choice
= MIN (menu_height
, item_no
);
195 /* center dialog box on screen */
196 x
= (COLS
- width
) / 2;
197 y
= (LINES
- height
) / 2;
199 draw_shadow (stdscr
, y
, x
, height
, width
);
201 dialog
= newwin (height
, width
, y
, x
);
202 keypad (dialog
, TRUE
);
204 draw_box (dialog
, 0, 0, height
, width
, dialog_attr
, border_attr
);
205 wattrset (dialog
, border_attr
);
206 mvwaddch (dialog
, height
- 3, 0, ACS_LTEE
);
207 for (i
= 0; i
< width
- 2; i
++)
208 waddch (dialog
, ACS_HLINE
);
209 wattrset (dialog
, dialog_attr
);
210 wbkgdset (dialog
, dialog_attr
& A_COLOR
);
211 waddch (dialog
, ACS_RTEE
);
213 if (title
!= NULL
&& strlen(title
) >= width
-2 ) {
214 /* truncate long title -- mec */
215 char * title2
= malloc(width
-2+1);
216 memcpy( title2
, title
, width
-2 );
217 title2
[width
-2] = '\0';
222 wattrset (dialog
, title_attr
);
223 mvwaddch (dialog
, 0, (width
- strlen(title
))/2 - 1, ' ');
224 waddstr (dialog
, (char *)title
);
225 waddch (dialog
, ' ');
228 wattrset (dialog
, dialog_attr
);
229 print_autowrap (dialog
, prompt
, width
- 2, 1, 3);
231 menu_width
= width
- 6;
232 box_y
= height
- menu_height
- 5;
233 box_x
= (width
- menu_width
) / 2 - 1;
235 /* create new window for the menu */
236 menu
= subwin (dialog
, menu_height
, menu_width
,
237 y
+ box_y
+ 1, x
+ box_x
+ 1);
240 /* draw a box around the menu items */
241 draw_box (dialog
, box_y
, box_x
, menu_height
+ 2, menu_width
+ 2,
242 menubox_border_attr
, menubox_attr
);
245 * Find length of longest item in order to center menu.
246 * Set 'choice' to default item.
249 for (i
= 0; i
< item_no
; i
++) {
250 item_x
= MAX (item_x
, MIN(menu_width
, strlen (items
[i
* 2 + 1]) + 2));
251 if (strcmp(current
, items
[i
*2]) == 0) choice
= i
;
254 item_x
= (menu_width
- item_x
) / 2;
256 /* get the scroll info from the temp file */
257 if ( (f
=fopen("confdialog.scrltmp","r")) != NULL
) {
258 if ( (fscanf(f
,"%d\n",&scroll
) == 1) && (scroll
<= choice
) &&
259 (scroll
+max_choice
> choice
) && (scroll
>= 0) &&
260 (scroll
+max_choice
<= item_no
) ) {
262 choice
= choice
- scroll
;
266 remove("confdialog.scrltmp");
271 if ( (choice
>= max_choice
) || (f
==NULL
&& choice
>= max_choice
/2) ) {
272 if (choice
>= item_no
-max_choice
/2)
273 scroll
= first_item
= item_no
-max_choice
;
275 scroll
= first_item
= choice
- max_choice
/2;
276 choice
= choice
- scroll
;
280 for (i
=0; i
< max_choice
; i
++) {
281 print_item (menu
, items
[(first_item
+ i
) * 2 + 1], i
, i
== choice
,
282 (items
[(first_item
+ i
)*2][0] != ':'));
287 print_arrows(dialog
, item_no
, scroll
,
288 box_y
, box_x
+item_x
+1, menu_height
);
290 print_buttons (dialog
, height
, width
, 0);
291 wmove (menu
, choice
, item_x
+1);
297 if (key
< 256 && isalpha(key
)) key
= tolower(key
);
299 if (strchr("? \n", key
))
302 for (i
= choice
+1; i
< max_choice
; i
++) {
303 j
= first_alpha(items
[(scroll
+i
)*2+1], "YyNnMm");
304 if (key
== tolower(items
[(scroll
+i
)*2+1][j
]))
308 for (i
= 0; i
< max_choice
; i
++) {
309 j
= first_alpha(items
[(scroll
+i
)*2+1], "YyNnMm");
310 if (key
== tolower(items
[(scroll
+i
)*2+1][j
]))
315 if (i
< max_choice
||
316 key
== KEY_UP
|| key
== KEY_DOWN
||
317 key
== '-' || key
== '+' ||
318 key
== KEY_PPAGE
|| key
== KEY_NPAGE
) {
320 print_item (menu
, items
[(scroll
+choice
)*2+1], choice
, FALSE
,
321 (items
[(scroll
+choice
)*2][0] != ':'));
323 if (key
== KEY_UP
|| key
== '-') {
324 if (choice
< 2 && scroll
) {
325 /* Scroll menu down */
326 scrollok (menu
, TRUE
);
328 scrollok (menu
, FALSE
);
332 print_item (menu
, items
[scroll
* 2 + 1], 0, FALSE
,
333 (items
[scroll
*2][0] != ':'));
335 choice
= MAX(choice
- 1, 0);
337 } else if (key
== KEY_DOWN
|| key
== '+') {
339 print_item (menu
, items
[(scroll
+choice
)*2+1], choice
, FALSE
,
340 (items
[(scroll
+choice
)*2][0] != ':'));
342 if ((choice
> max_choice
-3) &&
343 (scroll
+ max_choice
< item_no
)
346 scrollok (menu
, TRUE
);
348 scrollok (menu
, FALSE
);
352 print_item (menu
, items
[(scroll
+max_choice
-1)*2+1],
354 (items
[(scroll
+max_choice
-1)*2][0] != ':'));
356 choice
= MIN(choice
+1, max_choice
-1);
358 } else if (key
== KEY_PPAGE
) {
359 scrollok (menu
, TRUE
);
360 for (i
=0; (i
< max_choice
); i
++) {
364 print_item (menu
, items
[scroll
* 2 + 1], 0, FALSE
,
365 (items
[scroll
*2][0] != ':'));
371 scrollok (menu
, FALSE
);
373 } else if (key
== KEY_NPAGE
) {
374 for (i
=0; (i
< max_choice
); i
++) {
375 if (scroll
+max_choice
< item_no
) {
376 scrollok (menu
, TRUE
);
378 scrollok (menu
, FALSE
);
380 print_item (menu
, items
[(scroll
+max_choice
-1)*2+1],
382 (items
[(scroll
+max_choice
-1)*2][0] != ':'));
384 if (choice
+1 < max_choice
)
392 print_item (menu
, items
[(scroll
+choice
)*2+1], choice
, TRUE
,
393 (items
[(scroll
+choice
)*2][0] != ':'));
395 print_arrows(dialog
, item_no
, scroll
,
396 box_y
, box_x
+item_x
+1, menu_height
);
398 wnoutrefresh (dialog
);
401 continue; /* wait for another key press */
408 button
= ((key
== KEY_LEFT
? --button
: ++button
) < 0)
409 ? 2 : (button
> 2 ? 0 : button
);
411 print_buttons(dialog
, height
, width
, button
);
418 if (key
== ' ') button
= 0;
420 /* save scroll info */
421 if ( (f
=fopen("confdialog.scrltmp","w")) != NULL
) {
422 fprintf(f
,"%d\n",scroll
);
427 fprintf(stderr
, "%s \"%s\"\n",
428 items
[(scroll
+ choice
) * 2],
429 items
[(scroll
+ choice
) * 2 + 1] +
430 first_alpha(items
[(scroll
+ choice
) * 2 + 1],""));
432 fprintf(stderr
, "%s\n", items
[(scroll
+ choice
) * 2]);
434 remove("confdialog.scrltmp");
443 remove("confdialog.scrltmp");
444 return -1; /* ESC pressed */