eb4ee92ad2b3170c503de3330898fe33e6800b04
[linux-3.10.git] / scripts / kconfig / lxdialog / textbox.c
1 /*
2  *  textbox.c -- implements the text box
3  *
4  *  ORIGINAL AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
5  *  MODIFIED FOR LINUX KERNEL CONFIG BY: William Roadcap (roadcap@cfw.com)
6  *
7  *  This program is free software; you can redistribute it and/or
8  *  modify it under the terms of the GNU General Public License
9  *  as published by the Free Software Foundation; either version 2
10  *  of the License, or (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22 #include "dialog.h"
23
24 static void back_lines(int n);
25 static void print_page(WINDOW * win, int height, int width);
26 static void print_line(WINDOW * win, int row, int width);
27 static char *get_line(void);
28 static void print_position(WINDOW * win);
29
30 static int hscroll;
31 static int begin_reached, end_reached, page_length;
32 static const char *buf;
33 static const char *page;
34
35 /*
36  * refresh window content
37  */
38 static void refresh_text_box(WINDOW *dialog, WINDOW *box, int boxh, int boxw,
39                                                           int cur_y, int cur_x)
40 {
41         print_page(box, boxh, boxw);
42         print_position(dialog);
43         wmove(dialog, cur_y, cur_x);    /* Restore cursor position */
44         wrefresh(dialog);
45 }
46
47
48 /*
49  * Display text from a file in a dialog box.
50  *
51  * keys is a null-terminated array
52  */
53 int dialog_textbox(const char *title, const char *tbuf, int initial_height,
54                    int initial_width, int *keys)
55 {
56         int i, x, y, cur_x, cur_y, key = 0;
57         int height, width, boxh, boxw;
58         WINDOW *dialog, *box;
59         bool done = false;
60
61         begin_reached = 1;
62         end_reached = 0;
63         page_length = 0;
64         hscroll = 0;
65         buf = tbuf;
66         page = buf;     /* page is pointer to start of page to be displayed */
67
68 do_resize:
69         getmaxyx(stdscr, height, width);
70         if (height < 8 || width < 8)
71                 return -ERRDISPLAYTOOSMALL;
72         if (initial_height != 0)
73                 height = initial_height;
74         else
75                 if (height > 4)
76                         height -= 4;
77                 else
78                         height = 0;
79         if (initial_width != 0)
80                 width = initial_width;
81         else
82                 if (width > 5)
83                         width -= 5;
84                 else
85                         width = 0;
86
87         /* center dialog box on screen */
88         x = (COLS - width) / 2;
89         y = (LINES - height) / 2;
90
91         draw_shadow(stdscr, y, x, height, width);
92
93         dialog = newwin(height, width, y, x);
94         keypad(dialog, TRUE);
95
96         /* Create window for box region, used for scrolling text */
97         boxh = height - 4;
98         boxw = width - 2;
99         box = subwin(dialog, boxh, boxw, y + 1, x + 1);
100         wattrset(box, dlg.dialog.atr);
101         wbkgdset(box, dlg.dialog.atr & A_COLOR);
102
103         keypad(box, TRUE);
104
105         /* register the new window, along with its borders */
106         draw_box(dialog, 0, 0, height, width,
107                  dlg.dialog.atr, dlg.border.atr);
108
109         wattrset(dialog, dlg.border.atr);
110         mvwaddch(dialog, height - 3, 0, ACS_LTEE);
111         for (i = 0; i < width - 2; i++)
112                 waddch(dialog, ACS_HLINE);
113         wattrset(dialog, dlg.dialog.atr);
114         wbkgdset(dialog, dlg.dialog.atr & A_COLOR);
115         waddch(dialog, ACS_RTEE);
116
117         print_title(dialog, title, width);
118
119         print_button(dialog, gettext(" Exit "), height - 2, width / 2 - 4, TRUE);
120         wnoutrefresh(dialog);
121         getyx(dialog, cur_y, cur_x);    /* Save cursor position */
122
123         /* Print first page of text */
124         attr_clear(box, boxh, boxw, dlg.dialog.atr);
125         refresh_text_box(dialog, box, boxh, boxw, cur_y, cur_x);
126
127         while (!done) {
128                 key = wgetch(dialog);
129                 switch (key) {
130                 case 'E':       /* Exit */
131                 case 'e':
132                 case 'X':
133                 case 'x':
134                 case 'q':
135                 case '\n':
136                         done = true;
137                         break;
138                 case 'g':       /* First page */
139                 case KEY_HOME:
140                         if (!begin_reached) {
141                                 begin_reached = 1;
142                                 page = buf;
143                                 refresh_text_box(dialog, box, boxh, boxw,
144                                                  cur_y, cur_x);
145                         }
146                         break;
147                 case 'G':       /* Last page */
148                 case KEY_END:
149
150                         end_reached = 1;
151                         /* point to last char in buf */
152                         page = buf + strlen(buf);
153                         back_lines(boxh);
154                         refresh_text_box(dialog, box, boxh, boxw,
155                                          cur_y, cur_x);
156                         break;
157                 case 'K':       /* Previous line */
158                 case 'k':
159                 case KEY_UP:
160                         if (!begin_reached) {
161                                 int passed_end = 0;
162
163                                 back_lines(page_length + 1);
164
165                                 /* We don't call print_page() here but use
166                                  * scrolling to ensure faster screen update.
167                                  * However, 'end_reached' and 'page_length'
168                                  * should still be updated, and 'page' should
169                                  * point to start of next page. This is done
170                                  * by calling get_line() in the following
171                                  * 'for' loop. */
172                                 scrollok(box, TRUE);
173                                 wscrl(box, -1); /* Scroll box region down one line */
174                                 scrollok(box, FALSE);
175                                 page_length = 0;
176                                 for (i = 0; i < boxh; i++) {
177                                         if (!i) {
178                                                 /* print first line of page */
179                                                 print_line(box, 0, boxw);
180                                                 wnoutrefresh(box);
181                                         } else
182                                                 /* Called to update 'end_reached' and 'page' */
183                                                 get_line();
184                                         if (!passed_end)
185                                                 page_length++;
186                                         if (end_reached && !passed_end)
187                                                 passed_end = 1;
188                                 }
189
190                                 print_position(dialog);
191                                 wmove(dialog, cur_y, cur_x);    /* Restore cursor position */
192                                 wrefresh(dialog);
193                         }
194                         break;
195                 case 'B':       /* Previous page */
196                 case 'b':
197                 case 'u':
198                 case KEY_PPAGE:
199                         if (begin_reached)
200                                 break;
201                         back_lines(page_length + boxh);
202                         refresh_text_box(dialog, box, boxh, boxw,
203                                          cur_y, cur_x);
204                         break;
205                 case 'J':       /* Next line */
206                 case 'j':
207                 case KEY_DOWN:
208                         if (!end_reached) {
209                                 begin_reached = 0;
210                                 scrollok(box, TRUE);
211                                 scroll(box);    /* Scroll box region up one line */
212                                 scrollok(box, FALSE);
213                                 print_line(box, boxh - 1, boxw);
214                                 wnoutrefresh(box);
215                                 print_position(dialog);
216                                 wmove(dialog, cur_y, cur_x);    /* Restore cursor position */
217                                 wrefresh(dialog);
218                         }
219                         break;
220                 case KEY_NPAGE: /* Next page */
221                 case ' ':
222                 case 'd':
223                         if (end_reached)
224                                 break;
225
226                         begin_reached = 0;
227                         refresh_text_box(dialog, box, boxh, boxw,
228                                          cur_y, cur_x);
229                         break;
230                 case '0':       /* Beginning of line */
231                 case 'H':       /* Scroll left */
232                 case 'h':
233                 case KEY_LEFT:
234                         if (hscroll <= 0)
235                                 break;
236
237                         if (key == '0')
238                                 hscroll = 0;
239                         else
240                                 hscroll--;
241                         /* Reprint current page to scroll horizontally */
242                         back_lines(page_length);
243                         refresh_text_box(dialog, box, boxh, boxw,
244                                          cur_y, cur_x);
245                         break;
246                 case 'L':       /* Scroll right */
247                 case 'l':
248                 case KEY_RIGHT:
249                         if (hscroll >= MAX_LEN)
250                                 break;
251                         hscroll++;
252                         /* Reprint current page to scroll horizontally */
253                         back_lines(page_length);
254                         refresh_text_box(dialog, box, boxh, boxw,
255                                          cur_y, cur_x);
256                         break;
257                 case KEY_ESC:
258                         if (on_key_esc(dialog) == KEY_ESC)
259                                 done = true;
260                         break;
261                 case KEY_RESIZE:
262                         back_lines(height);
263                         delwin(box);
264                         delwin(dialog);
265                         on_key_resize();
266                         goto do_resize;
267                 default:
268                         for (i = 0; keys[i]; i++) {
269                                 if (key == keys[i]) {
270                                         done = true;
271                                         break;
272                                 }
273                         }
274                 }
275         }
276         delwin(box);
277         delwin(dialog);
278         return key;
279 }
280
281 /*
282  * Go back 'n' lines in text. Called by dialog_textbox().
283  * 'page' will be updated to point to the desired line in 'buf'.
284  */
285 static void back_lines(int n)
286 {
287         int i;
288
289         begin_reached = 0;
290         /* Go back 'n' lines */
291         for (i = 0; i < n; i++) {
292                 if (*page == '\0') {
293                         if (end_reached) {
294                                 end_reached = 0;
295                                 continue;
296                         }
297                 }
298                 if (page == buf) {
299                         begin_reached = 1;
300                         return;
301                 }
302                 page--;
303                 do {
304                         if (page == buf) {
305                                 begin_reached = 1;
306                                 return;
307                         }
308                         page--;
309                 } while (*page != '\n');
310                 page++;
311         }
312 }
313
314 /*
315  * Print a new page of text. Called by dialog_textbox().
316  */
317 static void print_page(WINDOW * win, int height, int width)
318 {
319         int i, passed_end = 0;
320
321         page_length = 0;
322         for (i = 0; i < height; i++) {
323                 print_line(win, i, width);
324                 if (!passed_end)
325                         page_length++;
326                 if (end_reached && !passed_end)
327                         passed_end = 1;
328         }
329         wnoutrefresh(win);
330 }
331
332 /*
333  * Print a new line of text. Called by dialog_textbox() and print_page().
334  */
335 static void print_line(WINDOW * win, int row, int width)
336 {
337         char *line;
338
339         line = get_line();
340         line += MIN(strlen(line), hscroll);     /* Scroll horizontally */
341         wmove(win, row, 0);     /* move cursor to correct line */
342         waddch(win, ' ');
343         waddnstr(win, line, MIN(strlen(line), width - 2));
344
345         /* Clear 'residue' of previous line */
346 #if OLD_NCURSES
347         {
348                 int x = getcurx(win);
349                 int i;
350                 for (i = 0; i < width - x; i++)
351                         waddch(win, ' ');
352         }
353 #else
354         wclrtoeol(win);
355 #endif
356 }
357
358 /*
359  * Return current line of text. Called by dialog_textbox() and print_line().
360  * 'page' should point to start of current line before calling, and will be
361  * updated to point to start of next line.
362  */
363 static char *get_line(void)
364 {
365         int i = 0;
366         static char line[MAX_LEN + 1];
367
368         end_reached = 0;
369         while (*page != '\n') {
370                 if (*page == '\0') {
371                         end_reached = 1;
372                         break;
373                 } else if (i < MAX_LEN)
374                         line[i++] = *(page++);
375                 else {
376                         /* Truncate lines longer than MAX_LEN characters */
377                         if (i == MAX_LEN)
378                                 line[i++] = '\0';
379                         page++;
380                 }
381         }
382         if (i <= MAX_LEN)
383                 line[i] = '\0';
384         if (!end_reached)
385                 page++;         /* move past '\n' */
386
387         return line;
388 }
389
390 /*
391  * Print current position
392  */
393 static void print_position(WINDOW * win)
394 {
395         int percent;
396
397         wattrset(win, dlg.position_indicator.atr);
398         wbkgdset(win, dlg.position_indicator.atr & A_COLOR);
399         percent = (page - buf) * 100 / strlen(buf);
400         wmove(win, getmaxy(win) - 3, getmaxx(win) - 9);
401         wprintw(win, "(%3d%%)", percent);
402 }