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 * SPDX-License-Identifier: GPL-2.0+
12 static void back_lines(int n
);
13 static void print_page(WINDOW
*win
, int height
, int width
, update_text_fn
14 update_text
, void *data
);
15 static void print_line(WINDOW
*win
, int row
, int width
);
16 static char *get_line(void);
17 static void print_position(WINDOW
* win
);
20 static int begin_reached
, end_reached
, page_length
;
25 * refresh window content
27 static void refresh_text_box(WINDOW
*dialog
, WINDOW
*box
, int boxh
, int boxw
,
28 int cur_y
, int cur_x
, update_text_fn update_text
,
31 print_page(box
, boxh
, boxw
, update_text
, data
);
32 print_position(dialog
);
33 wmove(dialog
, cur_y
, cur_x
); /* Restore cursor position */
39 * Display text from a file in a dialog box.
41 * keys is a null-terminated array
42 * update_text() may not add or remove any '\n' or '\0' in tbuf
44 int dialog_textbox(const char *title
, char *tbuf
, int initial_height
,
45 int initial_width
, int *keys
, int *_vscroll
, int *_hscroll
,
46 update_text_fn update_text
, void *data
)
48 int i
, x
, y
, cur_x
, cur_y
, key
= 0;
49 int height
, width
, boxh
, boxw
;
58 page
= buf
; /* page is pointer to start of page to be displayed */
60 if (_vscroll
&& *_vscroll
) {
63 for (i
= 0; i
< *_vscroll
; i
++)
70 getmaxyx(stdscr
, height
, width
);
71 if (height
< TEXTBOX_HEIGTH_MIN
|| width
< TEXTBOX_WIDTH_MIN
)
72 return -ERRDISPLAYTOOSMALL
;
73 if (initial_height
!= 0)
74 height
= initial_height
;
80 if (initial_width
!= 0)
81 width
= initial_width
;
88 /* center dialog box on screen */
89 x
= (getmaxx(stdscr
) - width
) / 2;
90 y
= (getmaxy(stdscr
) - height
) / 2;
92 draw_shadow(stdscr
, y
, x
, height
, width
);
94 dialog
= newwin(height
, width
, y
, x
);
97 /* Create window for box region, used for scrolling text */
100 box
= subwin(dialog
, boxh
, boxw
, y
+ 1, x
+ 1);
101 wattrset(box
, dlg
.dialog
.atr
);
102 wbkgdset(box
, dlg
.dialog
.atr
& A_COLOR
);
106 /* register the new window, along with its borders */
107 draw_box(dialog
, 0, 0, height
, width
,
108 dlg
.dialog
.atr
, dlg
.border
.atr
);
110 wattrset(dialog
, dlg
.border
.atr
);
111 mvwaddch(dialog
, height
- 3, 0, ACS_LTEE
);
112 for (i
= 0; i
< width
- 2; i
++)
113 waddch(dialog
, ACS_HLINE
);
114 wattrset(dialog
, dlg
.dialog
.atr
);
115 wbkgdset(dialog
, dlg
.dialog
.atr
& A_COLOR
);
116 waddch(dialog
, ACS_RTEE
);
118 print_title(dialog
, title
, width
);
120 print_button(dialog
, gettext(" Exit "), height
- 2, width
/ 2 - 4, TRUE
);
121 wnoutrefresh(dialog
);
122 getyx(dialog
, cur_y
, cur_x
); /* Save cursor position */
124 /* Print first page of text */
125 attr_clear(box
, boxh
, boxw
, dlg
.dialog
.atr
);
126 refresh_text_box(dialog
, box
, boxh
, boxw
, cur_y
, cur_x
, update_text
,
130 key
= wgetch(dialog
);
140 case 'g': /* First page */
142 if (!begin_reached
) {
145 refresh_text_box(dialog
, box
, boxh
, boxw
,
146 cur_y
, cur_x
, update_text
,
150 case 'G': /* Last page */
154 /* point to last char in buf */
155 page
= buf
+ strlen(buf
);
157 refresh_text_box(dialog
, box
, boxh
, boxw
, cur_y
,
158 cur_x
, update_text
, data
);
160 case 'K': /* Previous line */
166 back_lines(page_length
+ 1);
167 refresh_text_box(dialog
, box
, boxh
, boxw
, cur_y
,
168 cur_x
, update_text
, data
);
170 case 'B': /* Previous page */
176 back_lines(page_length
+ boxh
);
177 refresh_text_box(dialog
, box
, boxh
, boxw
, cur_y
,
178 cur_x
, update_text
, data
);
180 case 'J': /* Next line */
186 back_lines(page_length
- 1);
187 refresh_text_box(dialog
, box
, boxh
, boxw
, cur_y
,
188 cur_x
, update_text
, data
);
190 case KEY_NPAGE
: /* Next page */
197 refresh_text_box(dialog
, box
, boxh
, boxw
, cur_y
,
198 cur_x
, update_text
, data
);
200 case '0': /* Beginning of line */
201 case 'H': /* Scroll left */
211 /* Reprint current page to scroll horizontally */
212 back_lines(page_length
);
213 refresh_text_box(dialog
, box
, boxh
, boxw
, cur_y
,
214 cur_x
, update_text
, data
);
216 case 'L': /* Scroll right */
219 if (hscroll
>= MAX_LEN
)
222 /* Reprint current page to scroll horizontally */
223 back_lines(page_length
);
224 refresh_text_box(dialog
, box
, boxh
, boxw
, cur_y
,
225 cur_x
, update_text
, data
);
228 if (on_key_esc(dialog
) == KEY_ESC
)
238 for (i
= 0; keys
[i
]; i
++) {
239 if (key
== keys
[i
]) {
253 back_lines(page_length
);
254 while (s
< page
&& (s
= strchr(s
, '\n'))) {
265 * Go back 'n' lines in text. Called by dialog_textbox().
266 * 'page' will be updated to point to the desired line in 'buf'.
268 static void back_lines(int n
)
273 /* Go back 'n' lines */
274 for (i
= 0; i
< n
; i
++) {
292 } while (*page
!= '\n');
298 * Print a new page of text.
300 static void print_page(WINDOW
*win
, int height
, int width
, update_text_fn
301 update_text
, void *data
)
303 int i
, passed_end
= 0;
308 for (i
= 0; i
< height
; i
++)
312 update_text(buf
, page
- buf
, end
- buf
, data
);
316 for (i
= 0; i
< height
; i
++) {
317 print_line(win
, i
, width
);
320 if (end_reached
&& !passed_end
)
327 * Print a new line of text.
329 static void print_line(WINDOW
* win
, int row
, int width
)
334 line
+= MIN(strlen(line
), hscroll
); /* Scroll horizontally */
335 wmove(win
, row
, 0); /* move cursor to correct line */
337 waddnstr(win
, line
, MIN(strlen(line
), width
- 2));
339 /* Clear 'residue' of previous line */
342 int x
= getcurx(win
);
344 for (i
= 0; i
< width
- x
; i
++)
353 * Return current line of text. Called by dialog_textbox() and print_line().
354 * 'page' should point to start of current line before calling, and will be
355 * updated to point to start of next line.
357 static char *get_line(void)
360 static char line
[MAX_LEN
+ 1];
363 while (*page
!= '\n') {
367 } else if (i
< MAX_LEN
)
368 line
[i
++] = *(page
++);
370 /* Truncate lines longer than MAX_LEN characters */
379 page
++; /* move past '\n' */
385 * Print current position
387 static void print_position(WINDOW
* win
)
391 wattrset(win
, dlg
.position_indicator
.atr
);
392 wbkgdset(win
, dlg
.position_indicator
.atr
& A_COLOR
);
393 percent
= (page
- buf
) * 100 / strlen(buf
);
394 wmove(win
, getmaxy(win
) - 3, getmaxx(win
) - 9);
395 wprintw(win
, "(%3d%%)", percent
);