OSDN Git Service

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