1 // SPDX-License-Identifier: GPL-2.0+
3 * textbox.c -- implements the text box
5 * ORIGINAL AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
6 * MODIFIED FOR LINUX KERNEL CONFIG BY: William Roadcap (roadcap@cfw.com)
11 static void back_lines(int n
);
12 static void print_page(WINDOW
*win
, int height
, int width
, update_text_fn
13 update_text
, void *data
);
14 static void print_line(WINDOW
*win
, int row
, int width
);
15 static char *get_line(void);
16 static void print_position(WINDOW
* win
);
19 static int begin_reached
, end_reached
, page_length
;
24 * refresh window content
26 static void refresh_text_box(WINDOW
*dialog
, WINDOW
*box
, int boxh
, int boxw
,
27 int cur_y
, int cur_x
, update_text_fn update_text
,
30 print_page(box
, boxh
, boxw
, update_text
, data
);
31 print_position(dialog
);
32 wmove(dialog
, cur_y
, cur_x
); /* Restore cursor position */
38 * Display text from a file in a dialog box.
40 * keys is a null-terminated array
41 * update_text() may not add or remove any '\n' or '\0' in tbuf
43 int dialog_textbox(const char *title
, char *tbuf
, int initial_height
,
44 int initial_width
, int *keys
, int *_vscroll
, int *_hscroll
,
45 update_text_fn update_text
, void *data
)
47 int i
, x
, y
, cur_x
, cur_y
, key
= 0;
48 int height
, width
, boxh
, boxw
;
57 page
= buf
; /* page is pointer to start of page to be displayed */
59 if (_vscroll
&& *_vscroll
) {
62 for (i
= 0; i
< *_vscroll
; i
++)
69 getmaxyx(stdscr
, height
, width
);
70 if (height
< TEXTBOX_HEIGTH_MIN
|| width
< TEXTBOX_WIDTH_MIN
)
71 return -ERRDISPLAYTOOSMALL
;
72 if (initial_height
!= 0)
73 height
= initial_height
;
79 if (initial_width
!= 0)
80 width
= initial_width
;
87 /* center dialog box on screen */
88 x
= (getmaxx(stdscr
) - width
) / 2;
89 y
= (getmaxy(stdscr
) - height
) / 2;
91 draw_shadow(stdscr
, y
, x
, height
, width
);
93 dialog
= newwin(height
, width
, y
, x
);
96 /* Create window for box region, used for scrolling text */
99 box
= subwin(dialog
, boxh
, boxw
, y
+ 1, x
+ 1);
100 wattrset(box
, dlg
.dialog
.atr
);
101 wbkgdset(box
, dlg
.dialog
.atr
& A_COLOR
);
105 /* register the new window, along with its borders */
106 draw_box(dialog
, 0, 0, height
, width
,
107 dlg
.dialog
.atr
, dlg
.border
.atr
);
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
);
117 print_title(dialog
, title
, width
);
119 print_button(dialog
, " Exit ", height
- 2, width
/ 2 - 4, TRUE
);
120 wnoutrefresh(dialog
);
121 getyx(dialog
, cur_y
, cur_x
); /* Save cursor position */
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
, update_text
,
129 key
= wgetch(dialog
);
139 case 'g': /* First page */
141 if (!begin_reached
) {
144 refresh_text_box(dialog
, box
, boxh
, boxw
,
145 cur_y
, cur_x
, update_text
,
149 case 'G': /* Last page */
153 /* point to last char in buf */
154 page
= buf
+ strlen(buf
);
156 refresh_text_box(dialog
, box
, boxh
, boxw
, cur_y
,
157 cur_x
, update_text
, data
);
159 case 'K': /* Previous line */
165 back_lines(page_length
+ 1);
166 refresh_text_box(dialog
, box
, boxh
, boxw
, cur_y
,
167 cur_x
, update_text
, data
);
169 case 'B': /* Previous page */
175 back_lines(page_length
+ boxh
);
176 refresh_text_box(dialog
, box
, boxh
, boxw
, cur_y
,
177 cur_x
, update_text
, data
);
179 case 'J': /* Next line */
185 back_lines(page_length
- 1);
186 refresh_text_box(dialog
, box
, boxh
, boxw
, cur_y
,
187 cur_x
, update_text
, data
);
189 case KEY_NPAGE
: /* Next page */
196 refresh_text_box(dialog
, box
, boxh
, boxw
, cur_y
,
197 cur_x
, update_text
, data
);
199 case '0': /* Beginning of line */
200 case 'H': /* Scroll left */
210 /* Reprint current page to scroll horizontally */
211 back_lines(page_length
);
212 refresh_text_box(dialog
, box
, boxh
, boxw
, cur_y
,
213 cur_x
, update_text
, data
);
215 case 'L': /* Scroll right */
218 if (hscroll
>= MAX_LEN
)
221 /* Reprint current page to scroll horizontally */
222 back_lines(page_length
);
223 refresh_text_box(dialog
, box
, boxh
, boxw
, cur_y
,
224 cur_x
, update_text
, data
);
227 if (on_key_esc(dialog
) == KEY_ESC
)
237 for (i
= 0; keys
[i
]; i
++) {
238 if (key
== keys
[i
]) {
252 back_lines(page_length
);
253 while (s
< page
&& (s
= strchr(s
, '\n'))) {
264 * Go back 'n' lines in text. Called by dialog_textbox().
265 * 'page' will be updated to point to the desired line in 'buf'.
267 static void back_lines(int n
)
272 /* Go back 'n' lines */
273 for (i
= 0; i
< n
; i
++) {
291 } while (*page
!= '\n');
297 * Print a new page of text.
299 static void print_page(WINDOW
*win
, int height
, int width
, update_text_fn
300 update_text
, void *data
)
302 int i
, passed_end
= 0;
307 for (i
= 0; i
< height
; i
++)
311 update_text(buf
, page
- buf
, end
- buf
, data
);
315 for (i
= 0; i
< height
; i
++) {
316 print_line(win
, i
, width
);
319 if (end_reached
&& !passed_end
)
326 * Print a new line of text.
328 static void print_line(WINDOW
* win
, int row
, int width
)
333 line
+= MIN(strlen(line
), hscroll
); /* Scroll horizontally */
334 wmove(win
, row
, 0); /* move cursor to correct line */
336 waddnstr(win
, line
, MIN(strlen(line
), width
- 2));
338 /* Clear 'residue' of previous line */
341 int x
= getcurx(win
);
343 for (i
= 0; i
< width
- x
; i
++)
352 * Return current line of text. Called by dialog_textbox() and print_line().
353 * 'page' should point to start of current line before calling, and will be
354 * updated to point to start of next line.
356 static char *get_line(void)
359 static char line
[MAX_LEN
+ 1];
362 while (*page
!= '\n') {
366 } else if (i
< MAX_LEN
)
367 line
[i
++] = *(page
++);
369 /* Truncate lines longer than MAX_LEN characters */
378 page
++; /* move past '\n' */
384 * Print current position
386 static void print_position(WINDOW
* win
)
390 wattrset(win
, dlg
.position_indicator
.atr
);
391 wbkgdset(win
, dlg
.position_indicator
.atr
& A_COLOR
);
392 percent
= (page
- buf
) * 100 / strlen(buf
);
393 wmove(win
, getmaxy(win
) - 3, getmaxx(win
) - 9);
394 wprintw(win
, "(%3d%%)", percent
);