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