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); |
| 25 | static void print_page(WINDOW * win, int height, int width); |
| 26 | static void print_line(WINDOW * win, int row, int width); |
| 27 | static char *get_line(void); |
Sam Ravnborg | c8dc68a | 2006-07-29 22:48:57 +0200 | [diff] [blame] | 28 | static void print_position(WINDOW * win); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
Sam Ravnborg | 2982de6 | 2006-07-27 22:10:27 +0200 | [diff] [blame] | 30 | static int hscroll; |
| 31 | static int begin_reached, end_reached, page_length; |
| 32 | static const char *buf; |
| 33 | static const char *page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
| 35 | /* |
Sam Ravnborg | c8dc68a | 2006-07-29 22:48:57 +0200 | [diff] [blame] | 36 | * refresh window content |
| 37 | */ |
| 38 | static void refresh_text_box(WINDOW *dialog, WINDOW *box, int boxh, int boxw, |
| 39 | int cur_y, int cur_x) |
| 40 | { |
| 41 | print_page(box, boxh, boxw); |
| 42 | print_position(dialog); |
| 43 | wmove(dialog, cur_y, cur_x); /* Restore cursor position */ |
| 44 | wrefresh(dialog); |
| 45 | } |
| 46 | |
| 47 | |
| 48 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | * Display text from a file in a dialog box. |
Benjamin Poirier | 537ddae | 2012-08-23 14:55:04 -0400 | [diff] [blame^] | 50 | * |
| 51 | * keys is a null-terminated array |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | */ |
Benjamin Poirier | 537ddae | 2012-08-23 14:55:04 -0400 | [diff] [blame^] | 53 | int dialog_textbox(const char *title, const char *tbuf, int initial_height, |
| 54 | int initial_width, int *keys) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | { |
Sam Ravnborg | 2982de6 | 2006-07-27 22:10:27 +0200 | [diff] [blame] | 56 | int i, x, y, cur_x, cur_y, key = 0; |
Sam Ravnborg | c8dc68a | 2006-07-29 22:48:57 +0200 | [diff] [blame] | 57 | int height, width, boxh, boxw; |
Sam Ravnborg | c8dc68a | 2006-07-29 22:48:57 +0200 | [diff] [blame] | 58 | WINDOW *dialog, *box; |
Benjamin Poirier | 537ddae | 2012-08-23 14:55:04 -0400 | [diff] [blame^] | 59 | bool done = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
Sam Ravnborg | 2982de6 | 2006-07-27 22:10:27 +0200 | [diff] [blame] | 61 | begin_reached = 1; |
| 62 | end_reached = 0; |
| 63 | page_length = 0; |
| 64 | hscroll = 0; |
| 65 | buf = tbuf; |
| 66 | page = buf; /* page is pointer to start of page to be displayed */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
Sam Ravnborg | c8dc68a | 2006-07-29 22:48:57 +0200 | [diff] [blame] | 68 | do_resize: |
| 69 | getmaxyx(stdscr, height, width); |
| 70 | if (height < 8 || width < 8) |
| 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 */ |
| 88 | x = (COLS - width) / 2; |
| 89 | y = (LINES - height) / 2; |
| 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 | |
EGRY Gabor | 75c0a8a | 2008-01-11 23:42:54 +0100 | [diff] [blame] | 119 | print_button(dialog, gettext(" 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); |
| 125 | refresh_text_box(dialog, box, boxh, boxw, cur_y, cur_x); |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 126 | |
Benjamin Poirier | 537ddae | 2012-08-23 14:55:04 -0400 | [diff] [blame^] | 127 | while (!done) { |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 128 | key = wgetch(dialog); |
| 129 | switch (key) { |
| 130 | case 'E': /* Exit */ |
| 131 | case 'e': |
| 132 | case 'X': |
| 133 | case 'x': |
Benjamin Poirier | 9d4792c | 2012-07-24 16:12:02 -0400 | [diff] [blame] | 134 | case 'q': |
Benjamin Poirier | 537ddae | 2012-08-23 14:55:04 -0400 | [diff] [blame^] | 135 | case '\n': |
| 136 | done = true; |
| 137 | break; |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 138 | case 'g': /* First page */ |
| 139 | case KEY_HOME: |
| 140 | if (!begin_reached) { |
| 141 | begin_reached = 1; |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 142 | page = buf; |
Sam Ravnborg | c8dc68a | 2006-07-29 22:48:57 +0200 | [diff] [blame] | 143 | refresh_text_box(dialog, box, boxh, boxw, |
| 144 | cur_y, cur_x); |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 145 | } |
| 146 | break; |
| 147 | case 'G': /* Last page */ |
| 148 | case KEY_END: |
| 149 | |
| 150 | end_reached = 1; |
Sam Ravnborg | 2982de6 | 2006-07-27 22:10:27 +0200 | [diff] [blame] | 151 | /* point to last char in buf */ |
| 152 | page = buf + strlen(buf); |
Sam Ravnborg | c8dc68a | 2006-07-29 22:48:57 +0200 | [diff] [blame] | 153 | back_lines(boxh); |
| 154 | refresh_text_box(dialog, box, boxh, boxw, |
| 155 | cur_y, cur_x); |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 156 | break; |
| 157 | case 'K': /* Previous line */ |
| 158 | case 'k': |
| 159 | case KEY_UP: |
| 160 | if (!begin_reached) { |
Benjamin Poirier | 537ddae | 2012-08-23 14:55:04 -0400 | [diff] [blame^] | 161 | int passed_end = 0; |
| 162 | |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 163 | back_lines(page_length + 1); |
| 164 | |
Sam Ravnborg | 2982de6 | 2006-07-27 22:10:27 +0200 | [diff] [blame] | 165 | /* We don't call print_page() here but use |
| 166 | * scrolling to ensure faster screen update. |
| 167 | * However, 'end_reached' and 'page_length' |
| 168 | * should still be updated, and 'page' should |
| 169 | * point to start of next page. This is done |
| 170 | * by calling get_line() in the following |
| 171 | * 'for' loop. */ |
Sam Ravnborg | c8dc68a | 2006-07-29 22:48:57 +0200 | [diff] [blame] | 172 | scrollok(box, TRUE); |
| 173 | wscrl(box, -1); /* Scroll box region down one line */ |
| 174 | scrollok(box, FALSE); |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 175 | page_length = 0; |
Sam Ravnborg | c8dc68a | 2006-07-29 22:48:57 +0200 | [diff] [blame] | 176 | for (i = 0; i < boxh; i++) { |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 177 | if (!i) { |
| 178 | /* print first line of page */ |
Sam Ravnborg | c8dc68a | 2006-07-29 22:48:57 +0200 | [diff] [blame] | 179 | print_line(box, 0, boxw); |
| 180 | wnoutrefresh(box); |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 181 | } else |
| 182 | /* Called to update 'end_reached' and 'page' */ |
| 183 | get_line(); |
| 184 | if (!passed_end) |
| 185 | page_length++; |
| 186 | if (end_reached && !passed_end) |
| 187 | passed_end = 1; |
| 188 | } |
| 189 | |
Sam Ravnborg | c8dc68a | 2006-07-29 22:48:57 +0200 | [diff] [blame] | 190 | print_position(dialog); |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 191 | wmove(dialog, cur_y, cur_x); /* Restore cursor position */ |
| 192 | wrefresh(dialog); |
| 193 | } |
| 194 | break; |
| 195 | case 'B': /* Previous page */ |
| 196 | case 'b': |
Benjamin Poirier | 9d4792c | 2012-07-24 16:12:02 -0400 | [diff] [blame] | 197 | case 'u': |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 198 | case KEY_PPAGE: |
| 199 | if (begin_reached) |
| 200 | break; |
Sam Ravnborg | c8dc68a | 2006-07-29 22:48:57 +0200 | [diff] [blame] | 201 | back_lines(page_length + boxh); |
| 202 | refresh_text_box(dialog, box, boxh, boxw, |
| 203 | cur_y, cur_x); |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 204 | break; |
| 205 | case 'J': /* Next line */ |
| 206 | case 'j': |
| 207 | case KEY_DOWN: |
| 208 | if (!end_reached) { |
| 209 | begin_reached = 0; |
Sam Ravnborg | c8dc68a | 2006-07-29 22:48:57 +0200 | [diff] [blame] | 210 | scrollok(box, TRUE); |
| 211 | scroll(box); /* Scroll box region up one line */ |
| 212 | scrollok(box, FALSE); |
| 213 | print_line(box, boxh - 1, boxw); |
| 214 | wnoutrefresh(box); |
| 215 | print_position(dialog); |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 216 | wmove(dialog, cur_y, cur_x); /* Restore cursor position */ |
| 217 | wrefresh(dialog); |
| 218 | } |
| 219 | break; |
| 220 | case KEY_NPAGE: /* Next page */ |
| 221 | case ' ': |
Benjamin Poirier | 9d4792c | 2012-07-24 16:12:02 -0400 | [diff] [blame] | 222 | case 'd': |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 223 | if (end_reached) |
| 224 | break; |
| 225 | |
| 226 | begin_reached = 0; |
Sam Ravnborg | c8dc68a | 2006-07-29 22:48:57 +0200 | [diff] [blame] | 227 | refresh_text_box(dialog, box, boxh, boxw, |
| 228 | cur_y, cur_x); |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 229 | break; |
| 230 | case '0': /* Beginning of line */ |
| 231 | case 'H': /* Scroll left */ |
| 232 | case 'h': |
| 233 | case KEY_LEFT: |
| 234 | if (hscroll <= 0) |
| 235 | break; |
| 236 | |
| 237 | if (key == '0') |
| 238 | hscroll = 0; |
| 239 | else |
| 240 | hscroll--; |
| 241 | /* Reprint current page to scroll horizontally */ |
| 242 | back_lines(page_length); |
Sam Ravnborg | c8dc68a | 2006-07-29 22:48:57 +0200 | [diff] [blame] | 243 | refresh_text_box(dialog, box, boxh, boxw, |
| 244 | cur_y, cur_x); |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 245 | break; |
| 246 | case 'L': /* Scroll right */ |
| 247 | case 'l': |
| 248 | case KEY_RIGHT: |
| 249 | if (hscroll >= MAX_LEN) |
| 250 | break; |
| 251 | hscroll++; |
| 252 | /* Reprint current page to scroll horizontally */ |
| 253 | back_lines(page_length); |
Sam Ravnborg | c8dc68a | 2006-07-29 22:48:57 +0200 | [diff] [blame] | 254 | refresh_text_box(dialog, box, boxh, boxw, |
| 255 | cur_y, cur_x); |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 256 | break; |
Sam Ravnborg | f3cbcdc | 2006-07-28 23:57:48 +0200 | [diff] [blame] | 257 | case KEY_ESC: |
Benjamin Poirier | 537ddae | 2012-08-23 14:55:04 -0400 | [diff] [blame^] | 258 | if (on_key_esc(dialog) == KEY_ESC) |
| 259 | done = true; |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 260 | break; |
Sam Ravnborg | c8dc68a | 2006-07-29 22:48:57 +0200 | [diff] [blame] | 261 | case KEY_RESIZE: |
| 262 | back_lines(height); |
| 263 | delwin(box); |
| 264 | delwin(dialog); |
| 265 | on_key_resize(); |
| 266 | goto do_resize; |
Benjamin Poirier | 537ddae | 2012-08-23 14:55:04 -0400 | [diff] [blame^] | 267 | default: |
| 268 | for (i = 0; keys[i]; i++) { |
| 269 | if (key == keys[i]) { |
| 270 | done = true; |
| 271 | break; |
| 272 | } |
| 273 | } |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 274 | } |
| 275 | } |
Sam Ravnborg | c8dc68a | 2006-07-29 22:48:57 +0200 | [diff] [blame] | 276 | delwin(box); |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 277 | delwin(dialog); |
Benjamin Poirier | 537ddae | 2012-08-23 14:55:04 -0400 | [diff] [blame^] | 278 | return key; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | /* |
Sam Ravnborg | 2982de6 | 2006-07-27 22:10:27 +0200 | [diff] [blame] | 282 | * Go back 'n' lines in text. Called by dialog_textbox(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | * 'page' will be updated to point to the desired line in 'buf'. |
| 284 | */ |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 285 | static void back_lines(int n) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | { |
Sam Ravnborg | 2982de6 | 2006-07-27 22:10:27 +0200 | [diff] [blame] | 287 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 289 | begin_reached = 0; |
Sam Ravnborg | 2982de6 | 2006-07-27 22:10:27 +0200 | [diff] [blame] | 290 | /* Go back 'n' lines */ |
| 291 | for (i = 0; i < n; i++) { |
| 292 | if (*page == '\0') { |
| 293 | if (end_reached) { |
| 294 | end_reached = 0; |
| 295 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | } |
Sam Ravnborg | 2982de6 | 2006-07-27 22:10:27 +0200 | [diff] [blame] | 297 | } |
| 298 | if (page == buf) { |
| 299 | begin_reached = 1; |
| 300 | return; |
| 301 | } |
| 302 | page--; |
| 303 | do { |
| 304 | if (page == buf) { |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 305 | begin_reached = 1; |
| 306 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | } |
Sam Ravnborg | 2982de6 | 2006-07-27 22:10:27 +0200 | [diff] [blame] | 308 | page--; |
| 309 | } while (*page != '\n'); |
| 310 | page++; |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 311 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | /* |
| 315 | * Print a new page of text. Called by dialog_textbox(). |
| 316 | */ |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 317 | static void print_page(WINDOW * win, int height, int width) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | { |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 319 | int i, passed_end = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 321 | page_length = 0; |
| 322 | for (i = 0; i < height; i++) { |
| 323 | print_line(win, i, width); |
| 324 | if (!passed_end) |
| 325 | page_length++; |
| 326 | if (end_reached && !passed_end) |
| 327 | passed_end = 1; |
| 328 | } |
| 329 | wnoutrefresh(win); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | /* |
| 333 | * Print a new line of text. Called by dialog_textbox() and print_page(). |
| 334 | */ |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 335 | static void print_line(WINDOW * win, int row, int width) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | { |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 337 | char *line; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 339 | line = get_line(); |
| 340 | line += MIN(strlen(line), hscroll); /* Scroll horizontally */ |
| 341 | wmove(win, row, 0); /* move cursor to correct line */ |
| 342 | waddch(win, ' '); |
| 343 | waddnstr(win, line, MIN(strlen(line), width - 2)); |
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 | /* Clear 'residue' of previous line */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | #if OLD_NCURSES |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 347 | { |
Lucas De Marchi | 702a945 | 2011-08-20 02:28:53 -0300 | [diff] [blame] | 348 | int x = getcurx(win); |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 349 | int i; |
| 350 | for (i = 0; i < width - x; i++) |
| 351 | waddch(win, ' '); |
| 352 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | #else |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 354 | wclrtoeol(win); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | #endif |
| 356 | } |
| 357 | |
| 358 | /* |
| 359 | * Return current line of text. Called by dialog_textbox() and print_line(). |
| 360 | * 'page' should point to start of current line before calling, and will be |
| 361 | * updated to point to start of next line. |
| 362 | */ |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 363 | static char *get_line(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | { |
Sam Ravnborg | 2982de6 | 2006-07-27 22:10:27 +0200 | [diff] [blame] | 365 | int i = 0; |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 366 | static char line[MAX_LEN + 1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 368 | end_reached = 0; |
| 369 | while (*page != '\n') { |
| 370 | if (*page == '\0') { |
Benjamin Poirier | b9d29ab | 2012-08-23 14:55:03 -0400 | [diff] [blame] | 371 | end_reached = 1; |
| 372 | break; |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 373 | } else if (i < MAX_LEN) |
| 374 | line[i++] = *(page++); |
| 375 | else { |
| 376 | /* Truncate lines longer than MAX_LEN characters */ |
| 377 | if (i == MAX_LEN) |
| 378 | line[i++] = '\0'; |
| 379 | page++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | } |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 382 | if (i <= MAX_LEN) |
| 383 | line[i] = '\0'; |
| 384 | if (!end_reached) |
Benjamin Poirier | b9d29ab | 2012-08-23 14:55:03 -0400 | [diff] [blame] | 385 | page++; /* move past '\n' */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 387 | return line; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | /* |
| 391 | * Print current position |
| 392 | */ |
Sam Ravnborg | c8dc68a | 2006-07-29 22:48:57 +0200 | [diff] [blame] | 393 | static void print_position(WINDOW * win) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | { |
Sam Ravnborg | 2982de6 | 2006-07-27 22:10:27 +0200 | [diff] [blame] | 395 | int percent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | |
Sam Ravnborg | 98e5a15 | 2006-07-24 21:40:46 +0200 | [diff] [blame] | 397 | wattrset(win, dlg.position_indicator.atr); |
| 398 | wbkgdset(win, dlg.position_indicator.atr & A_COLOR); |
Sam Ravnborg | 2982de6 | 2006-07-27 22:10:27 +0200 | [diff] [blame] | 399 | percent = (page - buf) * 100 / strlen(buf); |
Sam Ravnborg | c8dc68a | 2006-07-29 22:48:57 +0200 | [diff] [blame] | 400 | wmove(win, getmaxy(win) - 3, getmaxx(win) - 9); |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 401 | wprintw(win, "(%3d%%)", percent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | } |