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