Patrick Georgi | 53ea1d4 | 2019-11-22 16:55:58 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 2 | /* |
| 3 | * textbox.c -- implements the text box |
| 4 | * |
| 5 | * ORIGINAL AUTHOR: Savio Lam (lam836@cs.cuhk.hk) |
| 6 | * MODIFIED FOR LINUX KERNEL CONFIG BY: William Roadcap (roadcap@cfw.com) |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include "dialog.h" |
| 10 | |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 11 | static int hscroll; |
| 12 | static int begin_reached, end_reached, page_length; |
Patrick Georgi | d520840 | 2014-04-11 20:24:06 +0200 | [diff] [blame] | 13 | static char *buf; |
| 14 | static char *page; |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 15 | |
| 16 | /* |
Patrick Georgi | 34b149b | 2023-11-20 18:09:23 +0100 | [diff] [blame^] | 17 | * Go back 'n' lines in text. Called by dialog_textbox(). |
| 18 | * 'page' will be updated to point to the desired line in 'buf'. |
| 19 | */ |
| 20 | static void back_lines(int n) |
| 21 | { |
| 22 | int i; |
| 23 | |
| 24 | begin_reached = 0; |
| 25 | /* Go back 'n' lines */ |
| 26 | for (i = 0; i < n; i++) { |
| 27 | if (*page == '\0') { |
| 28 | if (end_reached) { |
| 29 | end_reached = 0; |
| 30 | continue; |
| 31 | } |
| 32 | } |
| 33 | if (page == buf) { |
| 34 | begin_reached = 1; |
| 35 | return; |
| 36 | } |
| 37 | page--; |
| 38 | do { |
| 39 | if (page == buf) { |
| 40 | begin_reached = 1; |
| 41 | return; |
| 42 | } |
| 43 | page--; |
| 44 | } while (*page != '\n'); |
| 45 | page++; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | /* |
| 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. |
| 53 | */ |
| 54 | static char *get_line(void) |
| 55 | { |
| 56 | int i = 0; |
| 57 | static char line[MAX_LEN + 1]; |
| 58 | |
| 59 | end_reached = 0; |
| 60 | while (*page != '\n') { |
| 61 | if (*page == '\0') { |
| 62 | end_reached = 1; |
| 63 | break; |
| 64 | } else if (i < MAX_LEN) |
| 65 | line[i++] = *(page++); |
| 66 | else { |
| 67 | /* Truncate lines longer than MAX_LEN characters */ |
| 68 | if (i == MAX_LEN) |
| 69 | line[i++] = '\0'; |
| 70 | page++; |
| 71 | } |
| 72 | } |
| 73 | if (i <= MAX_LEN) |
| 74 | line[i] = '\0'; |
| 75 | if (!end_reached) |
| 76 | page++; /* move past '\n' */ |
| 77 | |
| 78 | return line; |
| 79 | } |
| 80 | |
| 81 | /* |
| 82 | * Print a new line of text. |
| 83 | */ |
| 84 | static void print_line(WINDOW *win, int row, int width) |
| 85 | { |
| 86 | char *line; |
| 87 | |
| 88 | line = get_line(); |
| 89 | line += MIN(strlen(line), hscroll); /* Scroll horizontally */ |
| 90 | wmove(win, row, 0); /* move cursor to correct line */ |
| 91 | waddch(win, ' '); |
| 92 | waddnstr(win, line, MIN(strlen(line), width - 2)); |
| 93 | |
| 94 | /* Clear 'residue' of previous line */ |
| 95 | wclrtoeol(win); |
| 96 | } |
| 97 | |
| 98 | /* |
| 99 | * Print a new page of text. |
| 100 | */ |
| 101 | static void print_page(WINDOW *win, int height, int width, update_text_fn |
| 102 | update_text, void *data) |
| 103 | { |
| 104 | int i, passed_end = 0; |
| 105 | |
| 106 | if (update_text) { |
| 107 | char *end; |
| 108 | |
| 109 | for (i = 0; i < height; i++) |
| 110 | get_line(); |
| 111 | end = page; |
| 112 | back_lines(height); |
| 113 | update_text(buf, page - buf, end - buf, data); |
| 114 | } |
| 115 | |
| 116 | page_length = 0; |
| 117 | for (i = 0; i < height; i++) { |
| 118 | print_line(win, i, width); |
| 119 | if (!passed_end) |
| 120 | page_length++; |
| 121 | if (end_reached && !passed_end) |
| 122 | passed_end = 1; |
| 123 | } |
| 124 | wnoutrefresh(win); |
| 125 | } |
| 126 | |
| 127 | /* |
| 128 | * Print current position |
| 129 | */ |
| 130 | static void print_position(WINDOW *win) |
| 131 | { |
| 132 | int percent; |
| 133 | |
| 134 | wattrset(win, dlg.position_indicator.atr); |
| 135 | wbkgdset(win, dlg.position_indicator.atr & A_COLOR); |
| 136 | percent = (page - buf) * 100 / strlen(buf); |
| 137 | wmove(win, getmaxy(win) - 3, getmaxx(win) - 9); |
| 138 | wprintw(win, "(%3d%%)", percent); |
| 139 | } |
| 140 | |
| 141 | /* |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 142 | * refresh window content |
| 143 | */ |
| 144 | static void refresh_text_box(WINDOW *dialog, WINDOW *box, int boxh, int boxw, |
Patrick Georgi | d520840 | 2014-04-11 20:24:06 +0200 | [diff] [blame] | 145 | int cur_y, int cur_x, update_text_fn update_text, |
| 146 | void *data) |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 147 | { |
Patrick Georgi | d520840 | 2014-04-11 20:24:06 +0200 | [diff] [blame] | 148 | print_page(box, boxh, boxw, update_text, data); |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 149 | print_position(dialog); |
| 150 | wmove(dialog, cur_y, cur_x); /* Restore cursor position */ |
| 151 | wrefresh(dialog); |
| 152 | } |
| 153 | |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 154 | /* |
| 155 | * Display text from a file in a dialog box. |
Patrick Georgi | d520840 | 2014-04-11 20:24:06 +0200 | [diff] [blame] | 156 | * |
| 157 | * keys is a null-terminated array |
| 158 | * update_text() may not add or remove any '\n' or '\0' in tbuf |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 159 | */ |
Patrick Georgi | d520840 | 2014-04-11 20:24:06 +0200 | [diff] [blame] | 160 | int dialog_textbox(const char *title, char *tbuf, int initial_height, |
| 161 | int initial_width, int *keys, int *_vscroll, int *_hscroll, |
| 162 | update_text_fn update_text, void *data) |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 163 | { |
| 164 | int i, x, y, cur_x, cur_y, key = 0; |
| 165 | int height, width, boxh, boxw; |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 166 | WINDOW *dialog, *box; |
Patrick Georgi | d520840 | 2014-04-11 20:24:06 +0200 | [diff] [blame] | 167 | bool done = false; |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 168 | |
| 169 | begin_reached = 1; |
| 170 | end_reached = 0; |
| 171 | page_length = 0; |
| 172 | hscroll = 0; |
| 173 | buf = tbuf; |
| 174 | page = buf; /* page is pointer to start of page to be displayed */ |
| 175 | |
Patrick Georgi | d520840 | 2014-04-11 20:24:06 +0200 | [diff] [blame] | 176 | if (_vscroll && *_vscroll) { |
| 177 | begin_reached = 0; |
| 178 | |
| 179 | for (i = 0; i < *_vscroll; i++) |
| 180 | get_line(); |
| 181 | } |
| 182 | if (_hscroll) |
| 183 | hscroll = *_hscroll; |
| 184 | |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 185 | do_resize: |
| 186 | getmaxyx(stdscr, height, width); |
Patrick Georgi | d520840 | 2014-04-11 20:24:06 +0200 | [diff] [blame] | 187 | if (height < TEXTBOX_HEIGTH_MIN || width < TEXTBOX_WIDTH_MIN) |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 188 | return -ERRDISPLAYTOOSMALL; |
| 189 | if (initial_height != 0) |
| 190 | height = initial_height; |
| 191 | else |
| 192 | if (height > 4) |
| 193 | height -= 4; |
| 194 | else |
| 195 | height = 0; |
| 196 | if (initial_width != 0) |
| 197 | width = initial_width; |
| 198 | else |
| 199 | if (width > 5) |
| 200 | width -= 5; |
| 201 | else |
| 202 | width = 0; |
| 203 | |
| 204 | /* center dialog box on screen */ |
Patrick Georgi | d520840 | 2014-04-11 20:24:06 +0200 | [diff] [blame] | 205 | x = (getmaxx(stdscr) - width) / 2; |
| 206 | y = (getmaxy(stdscr) - height) / 2; |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 207 | |
| 208 | draw_shadow(stdscr, y, x, height, width); |
| 209 | |
| 210 | dialog = newwin(height, width, y, x); |
| 211 | keypad(dialog, TRUE); |
| 212 | |
| 213 | /* Create window for box region, used for scrolling text */ |
| 214 | boxh = height - 4; |
| 215 | boxw = width - 2; |
| 216 | box = subwin(dialog, boxh, boxw, y + 1, x + 1); |
| 217 | wattrset(box, dlg.dialog.atr); |
| 218 | wbkgdset(box, dlg.dialog.atr & A_COLOR); |
| 219 | |
| 220 | keypad(box, TRUE); |
| 221 | |
| 222 | /* register the new window, along with its borders */ |
| 223 | draw_box(dialog, 0, 0, height, width, |
| 224 | dlg.dialog.atr, dlg.border.atr); |
| 225 | |
| 226 | wattrset(dialog, dlg.border.atr); |
| 227 | mvwaddch(dialog, height - 3, 0, ACS_LTEE); |
| 228 | for (i = 0; i < width - 2; i++) |
| 229 | waddch(dialog, ACS_HLINE); |
| 230 | wattrset(dialog, dlg.dialog.atr); |
| 231 | wbkgdset(dialog, dlg.dialog.atr & A_COLOR); |
| 232 | waddch(dialog, ACS_RTEE); |
| 233 | |
| 234 | print_title(dialog, title, width); |
| 235 | |
Patrick Georgi | 53ea1d4 | 2019-11-22 16:55:58 +0100 | [diff] [blame] | 236 | print_button(dialog, " Exit ", height - 2, width / 2 - 4, TRUE); |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 237 | wnoutrefresh(dialog); |
| 238 | getyx(dialog, cur_y, cur_x); /* Save cursor position */ |
| 239 | |
| 240 | /* Print first page of text */ |
| 241 | attr_clear(box, boxh, boxw, dlg.dialog.atr); |
Patrick Georgi | d520840 | 2014-04-11 20:24:06 +0200 | [diff] [blame] | 242 | refresh_text_box(dialog, box, boxh, boxw, cur_y, cur_x, update_text, |
| 243 | data); |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 244 | |
Patrick Georgi | d520840 | 2014-04-11 20:24:06 +0200 | [diff] [blame] | 245 | while (!done) { |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 246 | key = wgetch(dialog); |
| 247 | switch (key) { |
| 248 | case 'E': /* Exit */ |
| 249 | case 'e': |
| 250 | case 'X': |
| 251 | case 'x': |
Patrick Georgi | d520840 | 2014-04-11 20:24:06 +0200 | [diff] [blame] | 252 | case 'q': |
| 253 | case '\n': |
| 254 | done = true; |
| 255 | break; |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 256 | case 'g': /* First page */ |
| 257 | case KEY_HOME: |
| 258 | if (!begin_reached) { |
| 259 | begin_reached = 1; |
| 260 | page = buf; |
| 261 | refresh_text_box(dialog, box, boxh, boxw, |
Patrick Georgi | d520840 | 2014-04-11 20:24:06 +0200 | [diff] [blame] | 262 | cur_y, cur_x, update_text, |
| 263 | data); |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 264 | } |
| 265 | break; |
| 266 | case 'G': /* Last page */ |
| 267 | case KEY_END: |
| 268 | |
| 269 | end_reached = 1; |
| 270 | /* point to last char in buf */ |
| 271 | page = buf + strlen(buf); |
| 272 | back_lines(boxh); |
Patrick Georgi | d520840 | 2014-04-11 20:24:06 +0200 | [diff] [blame] | 273 | refresh_text_box(dialog, box, boxh, boxw, cur_y, |
| 274 | cur_x, update_text, data); |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 275 | break; |
| 276 | case 'K': /* Previous line */ |
| 277 | case 'k': |
| 278 | case KEY_UP: |
Patrick Georgi | d520840 | 2014-04-11 20:24:06 +0200 | [diff] [blame] | 279 | if (begin_reached) |
| 280 | break; |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 281 | |
Patrick Georgi | d520840 | 2014-04-11 20:24:06 +0200 | [diff] [blame] | 282 | back_lines(page_length + 1); |
| 283 | refresh_text_box(dialog, box, boxh, boxw, cur_y, |
| 284 | cur_x, update_text, data); |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 285 | break; |
| 286 | case 'B': /* Previous page */ |
| 287 | case 'b': |
Patrick Georgi | d520840 | 2014-04-11 20:24:06 +0200 | [diff] [blame] | 288 | case 'u': |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 289 | case KEY_PPAGE: |
| 290 | if (begin_reached) |
| 291 | break; |
| 292 | back_lines(page_length + boxh); |
Patrick Georgi | d520840 | 2014-04-11 20:24:06 +0200 | [diff] [blame] | 293 | refresh_text_box(dialog, box, boxh, boxw, cur_y, |
| 294 | cur_x, update_text, data); |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 295 | break; |
| 296 | case 'J': /* Next line */ |
| 297 | case 'j': |
| 298 | case KEY_DOWN: |
Patrick Georgi | d520840 | 2014-04-11 20:24:06 +0200 | [diff] [blame] | 299 | if (end_reached) |
| 300 | break; |
| 301 | |
| 302 | back_lines(page_length - 1); |
| 303 | refresh_text_box(dialog, box, boxh, boxw, cur_y, |
| 304 | cur_x, update_text, data); |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 305 | break; |
| 306 | case KEY_NPAGE: /* Next page */ |
| 307 | case ' ': |
Patrick Georgi | d520840 | 2014-04-11 20:24:06 +0200 | [diff] [blame] | 308 | case 'd': |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 309 | if (end_reached) |
| 310 | break; |
| 311 | |
| 312 | begin_reached = 0; |
Patrick Georgi | d520840 | 2014-04-11 20:24:06 +0200 | [diff] [blame] | 313 | refresh_text_box(dialog, box, boxh, boxw, cur_y, |
| 314 | cur_x, update_text, data); |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 315 | break; |
| 316 | case '0': /* Beginning of line */ |
| 317 | case 'H': /* Scroll left */ |
| 318 | case 'h': |
| 319 | case KEY_LEFT: |
| 320 | if (hscroll <= 0) |
| 321 | break; |
| 322 | |
| 323 | if (key == '0') |
| 324 | hscroll = 0; |
| 325 | else |
| 326 | hscroll--; |
| 327 | /* Reprint current page to scroll horizontally */ |
| 328 | back_lines(page_length); |
Patrick Georgi | d520840 | 2014-04-11 20:24:06 +0200 | [diff] [blame] | 329 | refresh_text_box(dialog, box, boxh, boxw, cur_y, |
| 330 | cur_x, update_text, data); |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 331 | break; |
| 332 | case 'L': /* Scroll right */ |
| 333 | case 'l': |
| 334 | case KEY_RIGHT: |
| 335 | if (hscroll >= MAX_LEN) |
| 336 | break; |
| 337 | hscroll++; |
| 338 | /* Reprint current page to scroll horizontally */ |
| 339 | back_lines(page_length); |
Patrick Georgi | d520840 | 2014-04-11 20:24:06 +0200 | [diff] [blame] | 340 | refresh_text_box(dialog, box, boxh, boxw, cur_y, |
| 341 | cur_x, update_text, data); |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 342 | break; |
| 343 | case KEY_ESC: |
Patrick Georgi | d520840 | 2014-04-11 20:24:06 +0200 | [diff] [blame] | 344 | if (on_key_esc(dialog) == KEY_ESC) |
| 345 | done = true; |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 346 | break; |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 347 | case KEY_RESIZE: |
| 348 | back_lines(height); |
| 349 | delwin(box); |
| 350 | delwin(dialog); |
| 351 | on_key_resize(); |
| 352 | goto do_resize; |
Patrick Georgi | d520840 | 2014-04-11 20:24:06 +0200 | [diff] [blame] | 353 | default: |
| 354 | for (i = 0; keys[i]; i++) { |
| 355 | if (key == keys[i]) { |
| 356 | done = true; |
| 357 | break; |
| 358 | } |
| 359 | } |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 360 | } |
| 361 | } |
| 362 | delwin(box); |
| 363 | delwin(dialog); |
Patrick Georgi | d520840 | 2014-04-11 20:24:06 +0200 | [diff] [blame] | 364 | if (_vscroll) { |
| 365 | const char *s; |
| 366 | |
| 367 | s = buf; |
| 368 | *_vscroll = 0; |
| 369 | back_lines(page_length); |
| 370 | while (s < page && (s = strchr(s, '\n'))) { |
| 371 | (*_vscroll)++; |
| 372 | s++; |
| 373 | } |
| 374 | } |
| 375 | if (_hscroll) |
| 376 | *_hscroll = hscroll; |
| 377 | return key; |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 378 | } |