diff options
Diffstat (limited to 'scripts/config/lxdialog/textbox.c')
-rw-r--r-- | scripts/config/lxdialog/textbox.c | 507 |
1 files changed, 191 insertions, 316 deletions
diff --git a/scripts/config/lxdialog/textbox.c b/scripts/config/lxdialog/textbox.c index 77848bb8e0..a48bb93e09 100644 --- a/scripts/config/lxdialog/textbox.c +++ b/scripts/config/lxdialog/textbox.c @@ -22,59 +22,80 @@ #include "dialog.h" static void back_lines(int n); -static void print_page(WINDOW * win, int height, int width); -static void print_line(WINDOW * win, int row, int width); +static void print_page(WINDOW *win, int height, int width, update_text_fn + update_text, void *data); +static void print_line(WINDOW *win, int row, int width); static char *get_line(void); -static void print_position(WINDOW * win, int height, int width); +static void print_position(WINDOW * win); + +static int hscroll; +static int begin_reached, end_reached, page_length; +static char *buf; +static char *page; + +/* + * refresh window content + */ +static void refresh_text_box(WINDOW *dialog, WINDOW *box, int boxh, int boxw, + int cur_y, int cur_x, update_text_fn update_text, + void *data) +{ + print_page(box, boxh, boxw, update_text, data); + print_position(dialog); + wmove(dialog, cur_y, cur_x); /* Restore cursor position */ + wrefresh(dialog); +} -static int hscroll, fd, file_size, bytes_read; -static int begin_reached = 1, end_reached, page_length; -static char *buf, *page; /* * Display text from a file in a dialog box. + * + * keys is a null-terminated array + * update_text() may not add or remove any '\n' or '\0' in tbuf */ -int dialog_textbox(const char *title, const char *file, int height, int width) +int dialog_textbox(const char *title, char *tbuf, int initial_height, + int initial_width, int *keys, int *_vscroll, int *_hscroll, + update_text_fn update_text, void *data) { - int i, x, y, cur_x, cur_y, fpos, key = 0; - int passed_end; - char search_term[MAX_LEN + 1]; - WINDOW *dialog, *text; - - search_term[0] = '\0'; /* no search term entered yet */ - - /* Open input file for reading */ - if ((fd = open(file, O_RDONLY)) == -1) { - endwin(); - fprintf(stderr, "\nCan't open input file in dialog_textbox().\n"); - exit(-1); - } - /* Get file size. Actually, 'file_size' is the real file size - 1, - since it's only the last byte offset from the beginning */ - if ((file_size = lseek(fd, 0, SEEK_END)) == -1) { - endwin(); - fprintf(stderr, "\nError getting file size in dialog_textbox().\n"); - exit(-1); - } - /* Restore file pointer to beginning of file after getting file size */ - if (lseek(fd, 0, SEEK_SET) == -1) { - endwin(); - fprintf(stderr, "\nError moving file pointer in dialog_textbox().\n"); - exit(-1); - } - /* Allocate space for read buffer */ - if ((buf = malloc(BUF_SIZE + 1)) == NULL) { - endwin(); - fprintf(stderr, "\nCan't allocate memory in dialog_textbox().\n"); - exit(-1); - } - if ((bytes_read = read(fd, buf, BUF_SIZE)) == -1) { - endwin(); - fprintf(stderr, "\nError reading file in dialog_textbox().\n"); - exit(-1); + int i, x, y, cur_x, cur_y, key = 0; + int height, width, boxh, boxw; + WINDOW *dialog, *box; + bool done = false; + + begin_reached = 1; + end_reached = 0; + page_length = 0; + hscroll = 0; + buf = tbuf; + page = buf; /* page is pointer to start of page to be displayed */ + + if (_vscroll && *_vscroll) { + begin_reached = 0; + + for (i = 0; i < *_vscroll; i++) + get_line(); } - buf[bytes_read] = '\0'; /* mark end of valid data */ - page = buf; /* page is pointer to start of page to be displayed */ + if (_hscroll) + hscroll = *_hscroll; + +do_resize: + getmaxyx(stdscr, height, width); + if (height < 8 || width < 8) + return -ERRDISPLAYTOOSMALL; + if (initial_height != 0) + height = initial_height; + else + if (height > 4) + height -= 4; + else + height = 0; + if (initial_width != 0) + width = initial_width; + else + if (width > 5) + width -= 5; + else + width = 0; /* center dialog box on screen */ x = (COLS - width) / 2; @@ -85,182 +106,108 @@ int dialog_textbox(const char *title, const char *file, int height, int width) dialog = newwin(height, width, y, x); keypad(dialog, TRUE); - /* Create window for text region, used for scrolling text */ - text = subwin(dialog, height - 4, width - 2, y + 1, x + 1); - wattrset(text, dialog_attr); - wbkgdset(text, dialog_attr & A_COLOR); + /* Create window for box region, used for scrolling text */ + boxh = height - 4; + boxw = width - 2; + box = subwin(dialog, boxh, boxw, y + 1, x + 1); + wattrset(box, dlg.dialog.atr); + wbkgdset(box, dlg.dialog.atr & A_COLOR); - keypad(text, TRUE); + keypad(box, TRUE); /* register the new window, along with its borders */ - draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr); + draw_box(dialog, 0, 0, height, width, + dlg.dialog.atr, dlg.border.atr); - wattrset(dialog, border_attr); + wattrset(dialog, dlg.border.atr); mvwaddch(dialog, height - 3, 0, ACS_LTEE); for (i = 0; i < width - 2; i++) waddch(dialog, ACS_HLINE); - wattrset(dialog, dialog_attr); - wbkgdset(dialog, dialog_attr & A_COLOR); + wattrset(dialog, dlg.dialog.atr); + wbkgdset(dialog, dlg.dialog.atr & A_COLOR); waddch(dialog, ACS_RTEE); print_title(dialog, title, width); - print_button(dialog, " Exit ", height - 2, width / 2 - 4, TRUE); + print_button(dialog, gettext(" Exit "), height - 2, width / 2 - 4, TRUE); wnoutrefresh(dialog); getyx(dialog, cur_y, cur_x); /* Save cursor position */ /* Print first page of text */ - attr_clear(text, height - 4, width - 2, dialog_attr); - print_page(text, height - 4, width - 2); - print_position(dialog, height, width); - wmove(dialog, cur_y, cur_x); /* Restore cursor position */ - wrefresh(dialog); + attr_clear(box, boxh, boxw, dlg.dialog.atr); + refresh_text_box(dialog, box, boxh, boxw, cur_y, cur_x, update_text, + data); - while ((key != ESC) && (key != '\n')) { + while (!done) { key = wgetch(dialog); switch (key) { case 'E': /* Exit */ case 'e': case 'X': case 'x': - delwin(dialog); - free(buf); - close(fd); - return 0; + case 'q': + case '\n': + done = true; + break; case 'g': /* First page */ case KEY_HOME: if (!begin_reached) { begin_reached = 1; - /* First page not in buffer? */ - if ((fpos = lseek(fd, 0, SEEK_CUR)) == -1) { - endwin(); - fprintf(stderr, "\nError moving file pointer in dialog_textbox().\n"); - exit(-1); - } - if (fpos > bytes_read) { /* Yes, we have to read it in */ - if (lseek(fd, 0, SEEK_SET) == -1) { - endwin(); - fprintf(stderr, "\nError moving file pointer in " - "dialog_textbox().\n"); - exit(-1); - } - if ((bytes_read = - read(fd, buf, BUF_SIZE)) == -1) { - endwin(); - fprintf(stderr, "\nError reading file in dialog_textbox().\n"); - exit(-1); - } - buf[bytes_read] = '\0'; - } page = buf; - print_page(text, height - 4, width - 2); - print_position(dialog, height, width); - wmove(dialog, cur_y, cur_x); /* Restore cursor position */ - wrefresh(dialog); + refresh_text_box(dialog, box, boxh, boxw, + cur_y, cur_x, update_text, + data); } break; case 'G': /* Last page */ case KEY_END: end_reached = 1; - /* Last page not in buffer? */ - if ((fpos = lseek(fd, 0, SEEK_CUR)) == -1) { - endwin(); - fprintf(stderr, "\nError moving file pointer in dialog_textbox().\n"); - exit(-1); - } - if (fpos < file_size) { /* Yes, we have to read it in */ - if (lseek(fd, -BUF_SIZE, SEEK_END) == -1) { - endwin(); - fprintf(stderr, "\nError moving file pointer in dialog_textbox().\n"); - exit(-1); - } - if ((bytes_read = - read(fd, buf, BUF_SIZE)) == -1) { - endwin(); - fprintf(stderr, "\nError reading file in dialog_textbox().\n"); - exit(-1); - } - buf[bytes_read] = '\0'; - } - page = buf + bytes_read; - back_lines(height - 4); - print_page(text, height - 4, width - 2); - print_position(dialog, height, width); - wmove(dialog, cur_y, cur_x); /* Restore cursor position */ - wrefresh(dialog); + /* point to last char in buf */ + page = buf + strlen(buf); + back_lines(boxh); + refresh_text_box(dialog, box, boxh, boxw, cur_y, + cur_x, update_text, data); break; case 'K': /* Previous line */ case 'k': case KEY_UP: - if (!begin_reached) { - back_lines(page_length + 1); - - /* We don't call print_page() here but use scrolling to ensure - faster screen update. However, 'end_reached' and - 'page_length' should still be updated, and 'page' should - point to start of next page. This is done by calling - get_line() in the following 'for' loop. */ - scrollok(text, TRUE); - wscrl(text, -1); /* Scroll text region down one line */ - scrollok(text, FALSE); - page_length = 0; - passed_end = 0; - for (i = 0; i < height - 4; i++) { - if (!i) { - /* print first line of page */ - print_line(text, 0, width - 2); - wnoutrefresh(text); - } else - /* Called to update 'end_reached' and 'page' */ - get_line(); - if (!passed_end) - page_length++; - if (end_reached && !passed_end) - passed_end = 1; - } + if (begin_reached) + break; - print_position(dialog, height, width); - wmove(dialog, cur_y, cur_x); /* Restore cursor position */ - wrefresh(dialog); - } + back_lines(page_length + 1); + refresh_text_box(dialog, box, boxh, boxw, cur_y, + cur_x, update_text, data); break; case 'B': /* Previous page */ case 'b': + case 'u': case KEY_PPAGE: if (begin_reached) break; - back_lines(page_length + height - 4); - print_page(text, height - 4, width - 2); - print_position(dialog, height, width); - wmove(dialog, cur_y, cur_x); - wrefresh(dialog); + back_lines(page_length + boxh); + refresh_text_box(dialog, box, boxh, boxw, cur_y, + cur_x, update_text, data); break; case 'J': /* Next line */ case 'j': case KEY_DOWN: - if (!end_reached) { - begin_reached = 0; - scrollok(text, TRUE); - scroll(text); /* Scroll text region up one line */ - scrollok(text, FALSE); - print_line(text, height - 5, width - 2); - wnoutrefresh(text); - print_position(dialog, height, width); - wmove(dialog, cur_y, cur_x); /* Restore cursor position */ - wrefresh(dialog); - } + if (end_reached) + break; + + back_lines(page_length - 1); + refresh_text_box(dialog, box, boxh, boxw, cur_y, + cur_x, update_text, data); break; case KEY_NPAGE: /* Next page */ case ' ': + case 'd': if (end_reached) break; begin_reached = 0; - print_page(text, height - 4, width - 2); - print_position(dialog, height, width); - wmove(dialog, cur_y, cur_x); - wrefresh(dialog); + refresh_text_box(dialog, box, boxh, boxw, cur_y, + cur_x, update_text, data); break; case '0': /* Beginning of line */ case 'H': /* Scroll left */ @@ -275,9 +222,8 @@ int dialog_textbox(const char *title, const char *file, int height, int width) hscroll--; /* Reprint current page to scroll horizontally */ back_lines(page_length); - print_page(text, height - 4, width - 2); - wmove(dialog, cur_y, cur_x); - wrefresh(dialog); + refresh_text_box(dialog, box, boxh, boxw, cur_y, + cur_x, update_text, data); break; case 'L': /* Scroll right */ case 'l': @@ -287,140 +233,97 @@ int dialog_textbox(const char *title, const char *file, int height, int width) hscroll++; /* Reprint current page to scroll horizontally */ back_lines(page_length); - print_page(text, height - 4, width - 2); - wmove(dialog, cur_y, cur_x); - wrefresh(dialog); + refresh_text_box(dialog, box, boxh, boxw, cur_y, + cur_x, update_text, data); break; - case ESC: + case KEY_ESC: + if (on_key_esc(dialog) == KEY_ESC) + done = true; break; + case KEY_RESIZE: + back_lines(height); + delwin(box); + delwin(dialog); + on_key_resize(); + goto do_resize; + default: + for (i = 0; keys[i]; i++) { + if (key == keys[i]) { + done = true; + break; + } + } } } - + delwin(box); delwin(dialog); - free(buf); - close(fd); - return -1; /* ESC pressed */ + if (_vscroll) { + const char *s; + + s = buf; + *_vscroll = 0; + back_lines(page_length); + while (s < page && (s = strchr(s, '\n'))) { + (*_vscroll)++; + s++; + } + } + if (_hscroll) + *_hscroll = hscroll; + return key; } /* - * Go back 'n' lines in text file. Called by dialog_textbox(). + * Go back 'n' lines in text. Called by dialog_textbox(). * 'page' will be updated to point to the desired line in 'buf'. */ static void back_lines(int n) { - int i, fpos; + int i; begin_reached = 0; - /* We have to distinguish between end_reached and !end_reached - since at end of file, the line is not ended by a '\n'. - The code inside 'if' basically does a '--page' to move one - character backward so as to skip '\n' of the previous line */ - if (!end_reached) { - /* Either beginning of buffer or beginning of file reached? */ - if (page == buf) { - if ((fpos = lseek(fd, 0, SEEK_CUR)) == -1) { - endwin(); - fprintf(stderr, "\nError moving file pointer in " - "back_lines().\n"); - exit(-1); - } - if (fpos > bytes_read) { /* Not beginning of file yet */ - /* We've reached beginning of buffer, but not beginning of - file yet, so read previous part of file into buffer. - Note that we only move backward for BUF_SIZE/2 bytes, - but not BUF_SIZE bytes to avoid re-reading again in - print_page() later */ - /* Really possible to move backward BUF_SIZE/2 bytes? */ - if (fpos < BUF_SIZE / 2 + bytes_read) { - /* No, move less then */ - if (lseek(fd, 0, SEEK_SET) == -1) { - endwin(); - fprintf(stderr, "\nError moving file pointer in " - "back_lines().\n"); - exit(-1); - } - page = buf + fpos - bytes_read; - } else { /* Move backward BUF_SIZE/2 bytes */ - if (lseek (fd, -(BUF_SIZE / 2 + bytes_read), SEEK_CUR) == -1) { - endwin(); - fprintf(stderr, "\nError moving file pointer " - "in back_lines().\n"); - exit(-1); - } - page = buf + BUF_SIZE / 2; - } - if ((bytes_read = - read(fd, buf, BUF_SIZE)) == -1) { - endwin(); - fprintf(stderr, "\nError reading file in back_lines().\n"); - exit(-1); - } - buf[bytes_read] = '\0'; - } else { /* Beginning of file reached */ - begin_reached = 1; - return; + /* Go back 'n' lines */ + for (i = 0; i < n; i++) { + if (*page == '\0') { + if (end_reached) { + end_reached = 0; + continue; } } - if (*(--page) != '\n') { /* '--page' here */ - /* Something's wrong... */ - endwin(); - fprintf(stderr, "\nInternal error in back_lines().\n"); - exit(-1); + if (page == buf) { + begin_reached = 1; + return; } - } - /* Go back 'n' lines */ - for (i = 0; i < n; i++) + page--; do { if (page == buf) { - if ((fpos = lseek(fd, 0, SEEK_CUR)) == -1) { - endwin(); - fprintf(stderr, "\nError moving file pointer in back_lines().\n"); - exit(-1); - } - if (fpos > bytes_read) { - /* Really possible to move backward BUF_SIZE/2 bytes? */ - if (fpos < BUF_SIZE / 2 + bytes_read) { - /* No, move less then */ - if (lseek(fd, 0, SEEK_SET) == -1) { - endwin(); - fprintf(stderr, "\nError moving file pointer " - "in back_lines().\n"); - exit(-1); - } - page = buf + fpos - bytes_read; - } else { /* Move backward BUF_SIZE/2 bytes */ - if (lseek (fd, -(BUF_SIZE / 2 + bytes_read), SEEK_CUR) == -1) { - endwin(); - fprintf(stderr, "\nError moving file pointer" - " in back_lines().\n"); - exit(-1); - } - page = buf + BUF_SIZE / 2; - } - if ((bytes_read = - read(fd, buf, BUF_SIZE)) == -1) { - endwin(); - fprintf(stderr, "\nError reading file in " - "back_lines().\n"); - exit(-1); - } - buf[bytes_read] = '\0'; - } else { /* Beginning of file reached */ - begin_reached = 1; - return; - } + begin_reached = 1; + return; } - } while (*(--page) != '\n'); - page++; + page--; + } while (*page != '\n'); + page++; + } } /* - * Print a new page of text. Called by dialog_textbox(). + * Print a new page of text. */ -static void print_page(WINDOW * win, int height, int width) +static void print_page(WINDOW *win, int height, int width, update_text_fn + update_text, void *data) { int i, passed_end = 0; + if (update_text) { + char *end; + + for (i = 0; i < height; i++) + get_line(); + end = page; + back_lines(height); + update_text(buf, page - buf, end - buf, data); + } + page_length = 0; for (i = 0; i < height; i++) { print_line(win, i, width); @@ -433,11 +336,10 @@ static void print_page(WINDOW * win, int height, int width) } /* - * Print a new line of text. Called by dialog_textbox() and print_page(). + * Print a new line of text. */ static void print_line(WINDOW * win, int row, int width) { - int y, x; char *line; line = get_line(); @@ -446,10 +348,10 @@ static void print_line(WINDOW * win, int row, int width) waddch(win, ' '); waddnstr(win, line, MIN(strlen(line), width - 2)); - getyx(win, y, x); /* Clear 'residue' of previous line */ #if OLD_NCURSES { + int x = getcurx(win); int i; for (i = 0; i < width - x; i++) waddch(win, ' '); @@ -466,35 +368,14 @@ static void print_line(WINDOW * win, int row, int width) */ static char *get_line(void) { - int i = 0, fpos; + int i = 0; static char line[MAX_LEN + 1]; end_reached = 0; while (*page != '\n') { if (*page == '\0') { - /* Either end of file or end of buffer reached */ - if ((fpos = lseek(fd, 0, SEEK_CUR)) == -1) { - endwin(); - fprintf(stderr, "\nError moving file pointer in " - "get_line().\n"); - exit(-1); - } - if (fpos < file_size) { /* Not end of file yet */ - /* We've reached end of buffer, but not end of file yet, - so read next part of file into buffer */ - if ((bytes_read = - read(fd, buf, BUF_SIZE)) == -1) { - endwin(); - fprintf(stderr, "\nError reading file in get_line().\n"); - exit(-1); - } - buf[bytes_read] = '\0'; - page = buf; - } else { - if (!end_reached) - end_reached = 1; - break; - } + end_reached = 1; + break; } else if (i < MAX_LEN) line[i++] = *(page++); else { @@ -507,7 +388,7 @@ static char *get_line(void) if (i <= MAX_LEN) line[i] = '\0'; if (!end_reached) - page++; /* move pass '\n' */ + page++; /* move past '\n' */ return line; } @@ -515,19 +396,13 @@ static char *get_line(void) /* * Print current position */ -static void print_position(WINDOW * win, int height, int width) +static void print_position(WINDOW * win) { - int fpos, percent; + int percent; - if ((fpos = lseek(fd, 0, SEEK_CUR)) == -1) { - endwin(); - fprintf(stderr, "\nError moving file pointer in print_position().\n"); - exit(-1); - } - wattrset(win, position_indicator_attr); - wbkgdset(win, position_indicator_attr & A_COLOR); - percent = !file_size ? - 100 : ((fpos - bytes_read + page - buf) * 100) / file_size; - wmove(win, height - 3, width - 9); + wattrset(win, dlg.position_indicator.atr); + wbkgdset(win, dlg.position_indicator.atr & A_COLOR); + percent = (page - buf) * 100 / strlen(buf); + wmove(win, getmaxy(win) - 3, getmaxx(win) - 9); wprintw(win, "(%3d%%)", percent); } |