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, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 static void back_lines(int n
);
25 static void print_page(WINDOW
*win
, int height
, int width
, update_text_fn
26 update_text
, void *data
);
27 static void print_line(WINDOW
*win
, int row
, int width
);
28 static char *get_line(void);
29 static void print_position(WINDOW
* win
);
32 static int begin_reached
, end_reached
, page_length
;
37 * refresh window content
39 static void refresh_text_box(WINDOW
*dialog
, WINDOW
*box
, int boxh
, int boxw
,
40 int cur_y
, int cur_x
, update_text_fn update_text
,
43 print_page(box
, boxh
, boxw
, update_text
, data
);
44 print_position(dialog
);
45 wmove(dialog
, cur_y
, cur_x
); /* Restore cursor position */
51 * Display text from a file in a dialog box.
53 * keys is a null-terminated array
54 * update_text() may not add or remove any '\n' or '\0' in tbuf
56 int dialog_textbox(const char *title
, char *tbuf
, int initial_height
,
57 int initial_width
, int *keys
, int *_vscroll
, int *_hscroll
,
58 update_text_fn update_text
, void *data
)
60 int i
, x
, y
, cur_x
, cur_y
, key
= 0;
61 int height
, width
, boxh
, boxw
;
70 page
= buf
; /* page is pointer to start of page to be displayed */
72 if (_vscroll
&& *_vscroll
) {
75 for (i
= 0; i
< *_vscroll
; i
++)
82 getmaxyx(stdscr
, height
, width
);
83 if (height
< TEXTBOX_HEIGTH_MIN
|| width
< TEXTBOX_WIDTH_MIN
)
84 return -ERRDISPLAYTOOSMALL
;
85 if (initial_height
!= 0)
86 height
= initial_height
;
92 if (initial_width
!= 0)
93 width
= initial_width
;
100 /* center dialog box on screen */
101 x
= (getmaxx(stdscr
) - width
) / 2;
102 y
= (getmaxy(stdscr
) - height
) / 2;
104 draw_shadow(stdscr
, y
, x
, height
, width
);
106 dialog
= newwin(height
, width
, y
, x
);
107 keypad(dialog
, TRUE
);
109 /* Create window for box region, used for scrolling text */
112 box
= subwin(dialog
, boxh
, boxw
, y
+ 1, x
+ 1);
113 wattrset(box
, dlg
.dialog
.atr
);
114 wbkgdset(box
, dlg
.dialog
.atr
& A_COLOR
);
118 /* register the new window, along with its borders */
119 draw_box(dialog
, 0, 0, height
, width
,
120 dlg
.dialog
.atr
, dlg
.border
.atr
);
122 wattrset(dialog
, dlg
.border
.atr
);
123 mvwaddch(dialog
, height
- 3, 0, ACS_LTEE
);
124 for (i
= 0; i
< width
- 2; i
++)
125 waddch(dialog
, ACS_HLINE
);
126 wattrset(dialog
, dlg
.dialog
.atr
);
127 wbkgdset(dialog
, dlg
.dialog
.atr
& A_COLOR
);
128 waddch(dialog
, ACS_RTEE
);
130 print_title(dialog
, title
, width
);
132 print_button(dialog
, gettext(" Exit "), height
- 2, width
/ 2 - 4, TRUE
);
133 wnoutrefresh(dialog
);
134 getyx(dialog
, cur_y
, cur_x
); /* Save cursor position */
136 /* Print first page of text */
137 attr_clear(box
, boxh
, boxw
, dlg
.dialog
.atr
);
138 refresh_text_box(dialog
, box
, boxh
, boxw
, cur_y
, cur_x
, update_text
,
142 key
= wgetch(dialog
);
152 case 'g': /* First page */
154 if (!begin_reached
) {
157 refresh_text_box(dialog
, box
, boxh
, boxw
,
158 cur_y
, cur_x
, update_text
,
162 case 'G': /* Last page */
166 /* point to last char in buf */
167 page
= buf
+ strlen(buf
);
169 refresh_text_box(dialog
, box
, boxh
, boxw
, cur_y
,
170 cur_x
, update_text
, data
);
172 case 'K': /* Previous line */
178 back_lines(page_length
+ 1);
179 refresh_text_box(dialog
, box
, boxh
, boxw
, cur_y
,
180 cur_x
, update_text
, data
);
182 case 'B': /* Previous page */
188 back_lines(page_length
+ boxh
);
189 refresh_text_box(dialog
, box
, boxh
, boxw
, cur_y
,
190 cur_x
, update_text
, data
);
192 case 'J': /* Next line */
198 back_lines(page_length
- 1);
199 refresh_text_box(dialog
, box
, boxh
, boxw
, cur_y
,
200 cur_x
, update_text
, data
);
202 case KEY_NPAGE
: /* Next page */
209 refresh_text_box(dialog
, box
, boxh
, boxw
, cur_y
,
210 cur_x
, update_text
, data
);
212 case '0': /* Beginning of line */
213 case 'H': /* Scroll left */
223 /* Reprint current page to scroll horizontally */
224 back_lines(page_length
);
225 refresh_text_box(dialog
, box
, boxh
, boxw
, cur_y
,
226 cur_x
, update_text
, data
);
228 case 'L': /* Scroll right */
231 if (hscroll
>= MAX_LEN
)
234 /* Reprint current page to scroll horizontally */
235 back_lines(page_length
);
236 refresh_text_box(dialog
, box
, boxh
, boxw
, cur_y
,
237 cur_x
, update_text
, data
);
240 if (on_key_esc(dialog
) == KEY_ESC
)
250 for (i
= 0; keys
[i
]; i
++) {
251 if (key
== keys
[i
]) {
265 back_lines(page_length
);
266 while (s
< page
&& (s
= strchr(s
, '\n'))) {
277 * Go back 'n' lines in text. Called by dialog_textbox().
278 * 'page' will be updated to point to the desired line in 'buf'.
280 static void back_lines(int n
)
285 /* Go back 'n' lines */
286 for (i
= 0; i
< n
; i
++) {
304 } while (*page
!= '\n');
310 * Print a new page of text.
312 static void print_page(WINDOW
*win
, int height
, int width
, update_text_fn
313 update_text
, void *data
)
315 int i
, passed_end
= 0;
320 for (i
= 0; i
< height
; i
++)
324 update_text(buf
, page
- buf
, end
- buf
, data
);
328 for (i
= 0; i
< height
; i
++) {
329 print_line(win
, i
, width
);
332 if (end_reached
&& !passed_end
)
339 * Print a new line of text.
341 static void print_line(WINDOW
* win
, int row
, int width
)
346 line
+= MIN(strlen(line
), hscroll
); /* Scroll horizontally */
347 wmove(win
, row
, 0); /* move cursor to correct line */
349 waddnstr(win
, line
, MIN(strlen(line
), width
- 2));
351 /* Clear 'residue' of previous line */
354 int x
= getcurx(win
);
356 for (i
= 0; i
< width
- x
; i
++)
365 * Return current line of text. Called by dialog_textbox() and print_line().
366 * 'page' should point to start of current line before calling, and will be
367 * updated to point to start of next line.
369 static char *get_line(void)
372 static char line
[MAX_LEN
+ 1];
375 while (*page
!= '\n') {
379 } else if (i
< MAX_LEN
)
380 line
[i
++] = *(page
++);
382 /* Truncate lines longer than MAX_LEN characters */
391 page
++; /* move past '\n' */
397 * Print current position
399 static void print_position(WINDOW
* win
)
403 wattrset(win
, dlg
.position_indicator
.atr
);
404 wbkgdset(win
, dlg
.position_indicator
.atr
& A_COLOR
);
405 percent
= (page
- buf
) * 100 / strlen(buf
);
406 wmove(win
, getmaxy(win
) - 3, getmaxx(win
) - 9);
407 wprintw(win
, "(%3d%%)", percent
);