aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/kconfig/lxdialog/textbox.c
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/kconfig/lxdialog/textbox.c')
-rw-r--r--scripts/kconfig/lxdialog/textbox.c171
1 files changed, 93 insertions, 78 deletions
diff --git a/scripts/kconfig/lxdialog/textbox.c b/scripts/kconfig/lxdialog/textbox.c
index 4e5de60a0c0d..a48bb93e0907 100644
--- a/scripts/kconfig/lxdialog/textbox.c
+++ b/scripts/kconfig/lxdialog/textbox.c
@@ -22,23 +22,25 @@
22#include "dialog.h" 22#include "dialog.h"
23 23
24static void back_lines(int n); 24static void back_lines(int n);
25static void print_page(WINDOW * win, int height, int width); 25static void print_page(WINDOW *win, int height, int width, update_text_fn
26static void print_line(WINDOW * win, int row, int width); 26 update_text, void *data);
27static void print_line(WINDOW *win, int row, int width);
27static char *get_line(void); 28static char *get_line(void);
28static void print_position(WINDOW * win); 29static void print_position(WINDOW * win);
29 30
30static int hscroll; 31static int hscroll;
31static int begin_reached, end_reached, page_length; 32static int begin_reached, end_reached, page_length;
32static const char *buf; 33static char *buf;
33static const char *page; 34static char *page;
34 35
35/* 36/*
36 * refresh window content 37 * refresh window content
37 */ 38 */
38static void refresh_text_box(WINDOW *dialog, WINDOW *box, int boxh, int boxw, 39static void refresh_text_box(WINDOW *dialog, WINDOW *box, int boxh, int boxw,
39 int cur_y, int cur_x) 40 int cur_y, int cur_x, update_text_fn update_text,
41 void *data)
40{ 42{
41 print_page(box, boxh, boxw); 43 print_page(box, boxh, boxw, update_text, data);
42 print_position(dialog); 44 print_position(dialog);
43 wmove(dialog, cur_y, cur_x); /* Restore cursor position */ 45 wmove(dialog, cur_y, cur_x); /* Restore cursor position */
44 wrefresh(dialog); 46 wrefresh(dialog);
@@ -47,14 +49,18 @@ static void refresh_text_box(WINDOW *dialog, WINDOW *box, int boxh, int boxw,
47 49
48/* 50/*
49 * Display text from a file in a dialog box. 51 * Display text from a file in a dialog box.
52 *
53 * keys is a null-terminated array
54 * update_text() may not add or remove any '\n' or '\0' in tbuf
50 */ 55 */
51int dialog_textbox(const char *title, const char *tbuf, 56int dialog_textbox(const char *title, char *tbuf, int initial_height,
52 int initial_height, int initial_width) 57 int initial_width, int *keys, int *_vscroll, int *_hscroll,
58 update_text_fn update_text, void *data)
53{ 59{
54 int i, x, y, cur_x, cur_y, key = 0; 60 int i, x, y, cur_x, cur_y, key = 0;
55 int height, width, boxh, boxw; 61 int height, width, boxh, boxw;
56 int passed_end;
57 WINDOW *dialog, *box; 62 WINDOW *dialog, *box;
63 bool done = false;
58 64
59 begin_reached = 1; 65 begin_reached = 1;
60 end_reached = 0; 66 end_reached = 0;
@@ -63,6 +69,15 @@ int dialog_textbox(const char *title, const char *tbuf,
63 buf = tbuf; 69 buf = tbuf;
64 page = buf; /* page is pointer to start of page to be displayed */ 70 page = buf; /* page is pointer to start of page to be displayed */
65 71
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
66do_resize: 81do_resize:
67 getmaxyx(stdscr, height, width); 82 getmaxyx(stdscr, height, width);
68 if (height < 8 || width < 8) 83 if (height < 8 || width < 8)
@@ -120,9 +135,10 @@ do_resize:
120 135
121 /* Print first page of text */ 136 /* Print first page of text */
122 attr_clear(box, boxh, boxw, dlg.dialog.atr); 137 attr_clear(box, boxh, boxw, dlg.dialog.atr);
123 refresh_text_box(dialog, box, boxh, boxw, cur_y, cur_x); 138 refresh_text_box(dialog, box, boxh, boxw, cur_y, cur_x, update_text,
139 data);
124 140
125 while ((key != KEY_ESC) && (key != '\n')) { 141 while (!done) {
126 key = wgetch(dialog); 142 key = wgetch(dialog);
127 switch (key) { 143 switch (key) {
128 case 'E': /* Exit */ 144 case 'E': /* Exit */
@@ -130,16 +146,17 @@ do_resize:
130 case 'X': 146 case 'X':
131 case 'x': 147 case 'x':
132 case 'q': 148 case 'q':
133 delwin(box); 149 case '\n':
134 delwin(dialog); 150 done = true;
135 return 0; 151 break;
136 case 'g': /* First page */ 152 case 'g': /* First page */
137 case KEY_HOME: 153 case KEY_HOME:
138 if (!begin_reached) { 154 if (!begin_reached) {
139 begin_reached = 1; 155 begin_reached = 1;
140 page = buf; 156 page = buf;
141 refresh_text_box(dialog, box, boxh, boxw, 157 refresh_text_box(dialog, box, boxh, boxw,
142 cur_y, cur_x); 158 cur_y, cur_x, update_text,
159 data);
143 } 160 }
144 break; 161 break;
145 case 'G': /* Last page */ 162 case 'G': /* Last page */
@@ -149,45 +166,18 @@ do_resize:
149 /* point to last char in buf */ 166 /* point to last char in buf */
150 page = buf + strlen(buf); 167 page = buf + strlen(buf);
151 back_lines(boxh); 168 back_lines(boxh);
152 refresh_text_box(dialog, box, boxh, boxw, 169 refresh_text_box(dialog, box, boxh, boxw, cur_y,
153 cur_y, cur_x); 170 cur_x, update_text, data);
154 break; 171 break;
155 case 'K': /* Previous line */ 172 case 'K': /* Previous line */
156 case 'k': 173 case 'k':
157 case KEY_UP: 174 case KEY_UP:
158 if (!begin_reached) { 175 if (begin_reached)
159 back_lines(page_length + 1); 176 break;
160
161 /* We don't call print_page() here but use
162 * scrolling to ensure faster screen update.
163 * However, 'end_reached' and 'page_length'
164 * should still be updated, and 'page' should
165 * point to start of next page. This is done
166 * by calling get_line() in the following
167 * 'for' loop. */
168 scrollok(box, TRUE);
169 wscrl(box, -1); /* Scroll box region down one line */
170 scrollok(box, FALSE);
171 page_length = 0;
172 passed_end = 0;
173 for (i = 0; i < boxh; i++) {
174 if (!i) {
175 /* print first line of page */
176 print_line(box, 0, boxw);
177 wnoutrefresh(box);
178 } else
179 /* Called to update 'end_reached' and 'page' */
180 get_line();
181 if (!passed_end)
182 page_length++;
183 if (end_reached && !passed_end)
184 passed_end = 1;
185 }
186 177
187 print_position(dialog); 178 back_lines(page_length + 1);
188 wmove(dialog, cur_y, cur_x); /* Restore cursor position */ 179 refresh_text_box(dialog, box, boxh, boxw, cur_y,
189 wrefresh(dialog); 180 cur_x, update_text, data);
190 }
191 break; 181 break;
192 case 'B': /* Previous page */ 182 case 'B': /* Previous page */
193 case 'b': 183 case 'b':
@@ -196,23 +186,18 @@ do_resize:
196 if (begin_reached) 186 if (begin_reached)
197 break; 187 break;
198 back_lines(page_length + boxh); 188 back_lines(page_length + boxh);
199 refresh_text_box(dialog, box, boxh, boxw, 189 refresh_text_box(dialog, box, boxh, boxw, cur_y,
200 cur_y, cur_x); 190 cur_x, update_text, data);
201 break; 191 break;
202 case 'J': /* Next line */ 192 case 'J': /* Next line */
203 case 'j': 193 case 'j':
204 case KEY_DOWN: 194 case KEY_DOWN:
205 if (!end_reached) { 195 if (end_reached)
206 begin_reached = 0; 196 break;
207 scrollok(box, TRUE); 197
208 scroll(box); /* Scroll box region up one line */ 198 back_lines(page_length - 1);
209 scrollok(box, FALSE); 199 refresh_text_box(dialog, box, boxh, boxw, cur_y,
210 print_line(box, boxh - 1, boxw); 200 cur_x, update_text, data);
211 wnoutrefresh(box);
212 print_position(dialog);
213 wmove(dialog, cur_y, cur_x); /* Restore cursor position */
214 wrefresh(dialog);
215 }
216 break; 201 break;
217 case KEY_NPAGE: /* Next page */ 202 case KEY_NPAGE: /* Next page */
218 case ' ': 203 case ' ':
@@ -221,8 +206,8 @@ do_resize:
221 break; 206 break;
222 207
223 begin_reached = 0; 208 begin_reached = 0;
224 refresh_text_box(dialog, box, boxh, boxw, 209 refresh_text_box(dialog, box, boxh, boxw, cur_y,
225 cur_y, cur_x); 210 cur_x, update_text, data);
226 break; 211 break;
227 case '0': /* Beginning of line */ 212 case '0': /* Beginning of line */
228 case 'H': /* Scroll left */ 213 case 'H': /* Scroll left */
@@ -237,8 +222,8 @@ do_resize:
237 hscroll--; 222 hscroll--;
238 /* Reprint current page to scroll horizontally */ 223 /* Reprint current page to scroll horizontally */
239 back_lines(page_length); 224 back_lines(page_length);
240 refresh_text_box(dialog, box, boxh, boxw, 225 refresh_text_box(dialog, box, boxh, boxw, cur_y,
241 cur_y, cur_x); 226 cur_x, update_text, data);
242 break; 227 break;
243 case 'L': /* Scroll right */ 228 case 'L': /* Scroll right */
244 case 'l': 229 case 'l':
@@ -248,11 +233,12 @@ do_resize:
248 hscroll++; 233 hscroll++;
249 /* Reprint current page to scroll horizontally */ 234 /* Reprint current page to scroll horizontally */
250 back_lines(page_length); 235 back_lines(page_length);
251 refresh_text_box(dialog, box, boxh, boxw, 236 refresh_text_box(dialog, box, boxh, boxw, cur_y,
252 cur_y, cur_x); 237 cur_x, update_text, data);
253 break; 238 break;
254 case KEY_ESC: 239 case KEY_ESC:
255 key = on_key_esc(dialog); 240 if (on_key_esc(dialog) == KEY_ESC)
241 done = true;
256 break; 242 break;
257 case KEY_RESIZE: 243 case KEY_RESIZE:
258 back_lines(height); 244 back_lines(height);
@@ -260,11 +246,31 @@ do_resize:
260 delwin(dialog); 246 delwin(dialog);
261 on_key_resize(); 247 on_key_resize();
262 goto do_resize; 248 goto do_resize;
249 default:
250 for (i = 0; keys[i]; i++) {
251 if (key == keys[i]) {
252 done = true;
253 break;
254 }
255 }
263 } 256 }
264 } 257 }
265 delwin(box); 258 delwin(box);
266 delwin(dialog); 259 delwin(dialog);
267 return key; /* ESC pressed */ 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;
273 return key;
268} 274}
269 275
270/* 276/*
@@ -301,12 +307,23 @@ static void back_lines(int n)
301} 307}
302 308
303/* 309/*
304 * Print a new page of text. Called by dialog_textbox(). 310 * Print a new page of text.
305 */ 311 */
306static void print_page(WINDOW * win, int height, int width) 312static void print_page(WINDOW *win, int height, int width, update_text_fn
313 update_text, void *data)
307{ 314{
308 int i, passed_end = 0; 315 int i, passed_end = 0;
309 316
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
310 page_length = 0; 327 page_length = 0;
311 for (i = 0; i < height; i++) { 328 for (i = 0; i < height; i++) {
312 print_line(win, i, width); 329 print_line(win, i, width);
@@ -319,7 +336,7 @@ static void print_page(WINDOW * win, int height, int width)
319} 336}
320 337
321/* 338/*
322 * Print a new line of text. Called by dialog_textbox() and print_page(). 339 * Print a new line of text.
323 */ 340 */
324static void print_line(WINDOW * win, int row, int width) 341static void print_line(WINDOW * win, int row, int width)
325{ 342{
@@ -357,10 +374,8 @@ static char *get_line(void)
357 end_reached = 0; 374 end_reached = 0;
358 while (*page != '\n') { 375 while (*page != '\n') {
359 if (*page == '\0') { 376 if (*page == '\0') {
360 if (!end_reached) { 377 end_reached = 1;
361 end_reached = 1; 378 break;
362 break;
363 }
364 } else if (i < MAX_LEN) 379 } else if (i < MAX_LEN)
365 line[i++] = *(page++); 380 line[i++] = *(page++);
366 else { 381 else {
@@ -373,7 +388,7 @@ static char *get_line(void)
373 if (i <= MAX_LEN) 388 if (i <= MAX_LEN)
374 line[i] = '\0'; 389 line[i] = '\0';
375 if (!end_reached) 390 if (!end_reached)
376 page++; /* move pass '\n' */ 391 page++; /* move past '\n' */
377 392
378 return line; 393 return line;
379} 394}