1 // SPDX-License-Identifier: GPL-2.0-or-later
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)
12 static int begin_reached
, end_reached
, page_length
;
13 static const char *buf
, *page
;
14 static size_t start
, end
;
17 * Go back 'n' lines in text. Called by dialog_textbox().
18 * 'page' will be updated to point to the desired line in 'buf'.
20 static void back_lines(int n
)
25 /* Go back 'n' lines */
26 for (i
= 0; i
< n
; i
++) {
44 } while (*page
!= '\n');
50 * Return current line of text. Called by dialog_textbox() and print_line().
51 * 'page' should point to start of current line before calling, and will be
52 * updated to point to start of next line.
54 static char *get_line(void)
57 static char line
[MAX_LEN
+ 1];
60 while (*page
!= '\n') {
64 } else if (i
< MAX_LEN
)
65 line
[i
++] = *(page
++);
67 /* Truncate lines longer than MAX_LEN characters */
76 page
++; /* move past '\n' */
82 * Print a new line of text.
84 static void print_line(WINDOW
*win
, int row
, int width
)
89 line
+= MIN(strlen(line
), hscroll
); /* Scroll horizontally */
90 wmove(win
, row
, 0); /* move cursor to correct line */
92 waddnstr(win
, line
, MIN(strlen(line
), width
- 2));
94 /* Clear 'residue' of previous line */
99 * Print a new page of text.
101 static void print_page(WINDOW
*win
, int height
, int width
)
103 int i
, passed_end
= 0;
106 for (i
= 0; i
< height
; i
++) {
107 print_line(win
, i
, width
);
110 if (end_reached
&& !passed_end
)
117 * Print current position
119 static void print_position(WINDOW
*win
)
123 wattrset(win
, dlg
.position_indicator
.atr
);
124 wbkgdset(win
, dlg
.position_indicator
.atr
& A_COLOR
);
125 percent
= (page
- buf
) * 100 / strlen(buf
);
126 wmove(win
, getmaxy(win
) - 3, getmaxx(win
) - 9);
127 wprintw(win
, "(%3d%%)", percent
);
131 * refresh window content
133 static void refresh_text_box(WINDOW
*dialog
, WINDOW
*box
, int boxh
, int boxw
,
134 int cur_y
, int cur_x
)
138 print_page(box
, boxh
, boxw
);
139 print_position(dialog
);
140 wmove(dialog
, cur_y
, cur_x
); /* Restore cursor position */
147 * Display text from a file in a dialog box.
149 * keys is a null-terminated array
151 int dialog_textbox(const char *title
, const char *tbuf
, int initial_height
,
152 int initial_width
, int *_vscroll
, int *_hscroll
,
153 int (*extra_key_cb
)(int, size_t, size_t, void *), void *data
)
155 int i
, x
, y
, cur_x
, cur_y
, key
= 0;
156 int height
, width
, boxh
, boxw
;
157 WINDOW
*dialog
, *box
;
165 page
= buf
; /* page is pointer to start of page to be displayed */
167 if (_vscroll
&& *_vscroll
) {
170 for (i
= 0; i
< *_vscroll
; i
++)
177 getmaxyx(stdscr
, height
, width
);
178 if (height
< TEXTBOX_HEIGTH_MIN
|| width
< TEXTBOX_WIDTH_MIN
)
179 return -ERRDISPLAYTOOSMALL
;
180 if (initial_height
!= 0)
181 height
= initial_height
;
187 if (initial_width
!= 0)
188 width
= initial_width
;
195 /* center dialog box on screen */
196 x
= (getmaxx(stdscr
) - width
) / 2;
197 y
= (getmaxy(stdscr
) - height
) / 2;
199 draw_shadow(stdscr
, y
, x
, height
, width
);
201 dialog
= newwin(height
, width
, y
, x
);
202 keypad(dialog
, TRUE
);
204 /* Create window for box region, used for scrolling text */
207 box
= subwin(dialog
, boxh
, boxw
, y
+ 1, x
+ 1);
208 wattrset(box
, dlg
.dialog
.atr
);
209 wbkgdset(box
, dlg
.dialog
.atr
& A_COLOR
);
213 /* register the new window, along with its borders */
214 draw_box(dialog
, 0, 0, height
, width
,
215 dlg
.dialog
.atr
, dlg
.border
.atr
);
217 wattrset(dialog
, dlg
.border
.atr
);
218 mvwaddch(dialog
, height
- 3, 0, ACS_LTEE
);
219 for (i
= 0; i
< width
- 2; i
++)
220 waddch(dialog
, ACS_HLINE
);
221 wattrset(dialog
, dlg
.dialog
.atr
);
222 wbkgdset(dialog
, dlg
.dialog
.atr
& A_COLOR
);
223 waddch(dialog
, ACS_RTEE
);
225 print_title(dialog
, title
, width
);
227 print_button(dialog
, " Exit ", height
- 2, width
/ 2 - 4, TRUE
);
228 wnoutrefresh(dialog
);
229 getyx(dialog
, cur_y
, cur_x
); /* Save cursor position */
231 /* Print first page of text */
232 attr_clear(box
, boxh
, boxw
, dlg
.dialog
.atr
);
233 refresh_text_box(dialog
, box
, boxh
, boxw
, cur_y
, cur_x
);
236 key
= wgetch(dialog
);
246 case 'g': /* First page */
248 if (!begin_reached
) {
251 refresh_text_box(dialog
, box
, boxh
, boxw
,
255 case 'G': /* Last page */
259 /* point to last char in buf */
260 page
= buf
+ strlen(buf
);
262 refresh_text_box(dialog
, box
, boxh
, boxw
, cur_y
, cur_x
);
264 case 'K': /* Previous line */
270 back_lines(page_length
+ 1);
271 refresh_text_box(dialog
, box
, boxh
, boxw
, cur_y
, cur_x
);
273 case 'B': /* Previous page */
279 back_lines(page_length
+ boxh
);
280 refresh_text_box(dialog
, box
, boxh
, boxw
, cur_y
, cur_x
);
282 case 'J': /* Next line */
288 back_lines(page_length
- 1);
289 refresh_text_box(dialog
, box
, boxh
, boxw
, cur_y
, cur_x
);
291 case KEY_NPAGE
: /* Next page */
298 refresh_text_box(dialog
, box
, boxh
, boxw
, cur_y
, cur_x
);
300 case '0': /* Beginning of line */
301 case 'H': /* Scroll left */
311 /* Reprint current page to scroll horizontally */
312 back_lines(page_length
);
313 refresh_text_box(dialog
, box
, boxh
, boxw
, cur_y
, cur_x
);
315 case 'L': /* Scroll right */
318 if (hscroll
>= MAX_LEN
)
321 /* Reprint current page to scroll horizontally */
322 back_lines(page_length
);
323 refresh_text_box(dialog
, box
, boxh
, boxw
, cur_y
, cur_x
);
326 if (on_key_esc(dialog
) == KEY_ESC
)
336 if (extra_key_cb
&& extra_key_cb(key
, start
, end
, data
)) {
349 back_lines(page_length
);
350 while (s
< page
&& (s
= strchr(s
, '\n'))) {