2 * textbox.c -- implements the text box
4 * ORIGINAL AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
5 * MODIFIED FOR LINUX KERNEL CONFIG BY: William Roadcap (roadcap@cfw.com)
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.
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.
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/>.
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);
30 static int begin_reached, end_reached, page_length;
31 static const char *buf;
32 static const char *page;
35 * refresh window content
37 static void refresh_text_box(WINDOW *dialog, WINDOW *box, int boxh, int boxw,
40 print_page(box, boxh, boxw);
41 print_position(dialog);
42 wmove(dialog, cur_y, cur_x); /* Restore cursor position */
48 * Display text from a file in a dialog box.
50 int dialog_textbox(const char *title, const char *tbuf,
51 int initial_height, int initial_width)
53 int i, x, y, cur_x, cur_y, key = 0;
54 int height, width, boxh, boxw;
63 page = buf; /* page is pointer to start of page to be displayed */
66 getmaxyx(stdscr, height, width);
67 if (height < 8 || width < 8)
68 return -ERRDISPLAYTOOSMALL;
69 if (initial_height != 0)
70 height = initial_height;
76 if (initial_width != 0)
77 width = initial_width;
84 /* center dialog box on screen */
85 x = (COLS - width) / 2;
86 y = (LINES - height) / 2;
88 draw_shadow(stdscr, y, x, height, width);
90 dialog = newwin(height, width, y, x);
93 /* Create window for box region, used for scrolling text */
96 box = subwin(dialog, boxh, boxw, y + 1, x + 1);
97 wattrset(box, dlg.dialog.atr);
98 wbkgdset(box, dlg.dialog.atr & A_COLOR);
102 /* register the new window, along with its borders */
103 draw_box(dialog, 0, 0, height, width,
104 dlg.dialog.atr, dlg.border.atr);
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);
114 print_title(dialog, title, width);
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 */
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);
124 while ((key != KEY_ESC) && (key != '\n')) {
125 key = wgetch(dialog);
134 case 'g': /* First page */
136 if (!begin_reached) {
139 refresh_text_box(dialog, box, boxh, boxw,
143 case 'G': /* Last page */
147 /* point to last char in buf */
148 page = buf + strlen(buf);
150 refresh_text_box(dialog, box, boxh, boxw,
153 case 'K': /* Previous line */
156 if (!begin_reached) {
157 back_lines(page_length + 1);
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
167 wscrl(box, -1); /* Scroll box region down one line */
168 scrollok(box, FALSE);
171 for (i = 0; i < boxh; i++) {
173 /* print first line of page */
174 print_line(box, 0, boxw);
177 /* Called to update 'end_reached' and 'page' */
181 if (end_reached && !passed_end)
185 print_position(dialog);
186 wmove(dialog, cur_y, cur_x); /* Restore cursor position */
190 case 'B': /* Previous page */
195 back_lines(page_length + boxh);
196 refresh_text_box(dialog, box, boxh, boxw,
199 case 'J': /* Next line */
205 scroll(box); /* Scroll box region up one line */
206 scrollok(box, FALSE);
207 print_line(box, boxh - 1, boxw);
209 print_position(dialog);
210 wmove(dialog, cur_y, cur_x); /* Restore cursor position */
214 case KEY_NPAGE: /* Next page */
220 refresh_text_box(dialog, box, boxh, boxw,
223 case '0': /* Beginning of line */
224 case 'H': /* Scroll left */
234 /* Reprint current page to scroll horizontally */
235 back_lines(page_length);
236 refresh_text_box(dialog, box, boxh, boxw,
239 case 'L': /* Scroll right */
242 if (hscroll >= MAX_LEN)
245 /* Reprint current page to scroll horizontally */
246 back_lines(page_length);
247 refresh_text_box(dialog, box, boxh, boxw,
251 key = on_key_esc(dialog);
263 return key; /* ESC pressed */
267 * Go back 'n' lines in text. Called by dialog_textbox().
268 * 'page' will be updated to point to the desired line in 'buf'.
270 static void back_lines(int n)
275 /* Go back 'n' lines */
276 for (i = 0; i < n; i++) {
294 } while (*page != '\n');
300 * Print a new page of text. Called by dialog_textbox().
302 static void print_page(WINDOW * win, int height, int width)
304 int i, passed_end = 0;
307 for (i = 0; i < height; i++) {
308 print_line(win, i, width);
311 if (end_reached && !passed_end)
318 * Print a new line of text. Called by dialog_textbox() and print_page().
320 static void print_line(WINDOW * win, int row, int width)
326 line += MIN(strlen(line), hscroll); /* Scroll horizontally */
327 wmove(win, row, 0); /* move cursor to correct line */
329 waddnstr(win, line, MIN(strlen(line), width - 2));
332 /* Clear 'residue' of previous line */
336 for (i = 0; i < width - x; i++)
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.
349 static char *get_line(void)
352 static char line[MAX_LEN + 1];
355 while (*page != '\n') {
361 } else if (i < MAX_LEN)
362 line[i++] = *(page++);
364 /* Truncate lines longer than MAX_LEN characters */
373 page++; /* move pass '\n' */
379 * Print current position
381 static void print_position(WINDOW * win)
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);