123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531 |
- #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 char *get_line(void);
- static void print_position(WINDOW * win, int height, int width);
- static int hscroll, fd, file_size, bytes_read;
- static int begin_reached = 1, end_reached, page_length;
- static char *buf, *page;
- int dialog_textbox(const char *title, const char *file, int height, int width)
- {
- int i, x, y, cur_x, cur_y, fpos, key = 0;
- int passed_end;
- WINDOW *dialog, *text;
-
- if ((fd = open(file, O_RDONLY)) == -1) {
- endwin();
- fprintf(stderr, "\nCan't open input file in dialog_textbox().\n");
- exit(-1);
- }
-
- if ((file_size = lseek(fd, 0, SEEK_END)) == -1) {
- endwin();
- fprintf(stderr, "\nError getting file size in dialog_textbox().\n");
- exit(-1);
- }
-
- if (lseek(fd, 0, SEEK_SET) == -1) {
- endwin();
- fprintf(stderr, "\nError moving file pointer in dialog_textbox().\n");
- exit(-1);
- }
-
- 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);
- }
- buf[bytes_read] = '\0';
- page = buf;
-
- x = (COLS - width) / 2;
- y = (LINES - height) / 2;
- draw_shadow(stdscr, y, x, height, width);
- dialog = newwin(height, width, y, x);
- keypad(dialog, TRUE);
-
- text = subwin(dialog, height - 4, width - 2, y + 1, x + 1);
- wattrset(text, dialog_attr);
- wbkgdset(text, dialog_attr & A_COLOR);
- keypad(text, TRUE);
-
- draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);
- wattrset(dialog, border_attr);
- 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);
- waddch(dialog, ACS_RTEE);
- print_title(dialog, title, width);
- print_button(dialog, " Exit ", height - 2, width / 2 - 4, TRUE);
- wnoutrefresh(dialog);
- getyx(dialog, cur_y, cur_x);
-
- 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);
- wrefresh(dialog);
- while ((key != ESC) && (key != '\n')) {
- key = wgetch(dialog);
- switch (key) {
- case 'E':
- case 'e':
- case 'X':
- case 'x':
- delwin(dialog);
- free(buf);
- close(fd);
- return 0;
- case 'g':
- case KEY_HOME:
- if (!begin_reached) {
- begin_reached = 1;
-
- 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) {
- 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);
- wrefresh(dialog);
- }
- break;
- case 'G':
- case KEY_END:
- end_reached = 1;
-
- 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) {
- 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);
- wrefresh(dialog);
- break;
- case 'K':
- case 'k':
- case KEY_UP:
- if (!begin_reached) {
- back_lines(page_length + 1);
-
- scrollok(text, TRUE);
- wscrl(text, -1);
- scrollok(text, FALSE);
- page_length = 0;
- passed_end = 0;
- for (i = 0; i < height - 4; i++) {
- if (!i) {
-
- print_line(text, 0, width - 2);
- wnoutrefresh(text);
- } else
-
- get_line();
- if (!passed_end)
- page_length++;
- if (end_reached && !passed_end)
- passed_end = 1;
- }
- print_position(dialog, height, width);
- wmove(dialog, cur_y, cur_x);
- wrefresh(dialog);
- }
- break;
- case 'B':
- case 'b':
- 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);
- break;
- case 'J':
- case 'j':
- case KEY_DOWN:
- if (!end_reached) {
- begin_reached = 0;
- scrollok(text, TRUE);
- scroll(text);
- scrollok(text, FALSE);
- print_line(text, height - 5, width - 2);
- wnoutrefresh(text);
- print_position(dialog, height, width);
- wmove(dialog, cur_y, cur_x);
- wrefresh(dialog);
- }
- break;
- case KEY_NPAGE:
- case ' ':
- 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);
- break;
- case '0':
- case 'H':
- case 'h':
- case KEY_LEFT:
- if (hscroll <= 0)
- break;
- if (key == '0')
- hscroll = 0;
- else
- hscroll--;
-
- back_lines(page_length);
- print_page(text, height - 4, width - 2);
- wmove(dialog, cur_y, cur_x);
- wrefresh(dialog);
- break;
- case 'L':
- case 'l':
- case KEY_RIGHT:
- if (hscroll >= MAX_LEN)
- break;
- hscroll++;
-
- back_lines(page_length);
- print_page(text, height - 4, width - 2);
- wmove(dialog, cur_y, cur_x);
- wrefresh(dialog);
- break;
- case ESC:
- break;
- }
- }
- delwin(dialog);
- free(buf);
- close(fd);
- return -1;
- }
- static void back_lines(int n)
- {
- int i, fpos;
- begin_reached = 0;
-
- if (!end_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) {
-
-
- if (fpos < BUF_SIZE / 2 + bytes_read) {
-
- 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 {
- 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 {
- begin_reached = 1;
- return;
- }
- }
- if (*(--page) != '\n') {
-
- endwin();
- fprintf(stderr, "\nInternal error in back_lines().\n");
- exit(-1);
- }
- }
-
- for (i = 0; i < n; i++)
- 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) {
-
- if (fpos < BUF_SIZE / 2 + bytes_read) {
-
- 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 {
- 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 {
- begin_reached = 1;
- return;
- }
- }
- } while (*(--page) != '\n');
- page++;
- }
- static void print_page(WINDOW * win, int height, int width)
- {
- int i, passed_end = 0;
- page_length = 0;
- for (i = 0; i < height; i++) {
- print_line(win, i, width);
- if (!passed_end)
- page_length++;
- if (end_reached && !passed_end)
- passed_end = 1;
- }
- wnoutrefresh(win);
- }
- static void print_line(WINDOW * win, int row, int width)
- {
- char *line;
- line = get_line();
- line += MIN(strlen(line), hscroll);
- wmove(win, row, 0);
- waddch(win, ' ');
- waddnstr(win, line, MIN(strlen(line), width - 2));
-
- #if OLD_NCURSES
- {
- int i;
- int y, x;
- getyx(win, y, x);
- for (i = 0; i < width - x; i++)
- waddch(win, ' ');
- }
- #else
- wclrtoeol(win);
- #endif
- }
- static char *get_line(void)
- {
- int i = 0, fpos;
- static char line[MAX_LEN + 1];
- end_reached = 0;
- while (*page != '\n') {
- if (*page == '\0') {
-
- 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) {
-
- 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;
- }
- } else if (i < MAX_LEN)
- line[i++] = *(page++);
- else {
-
- if (i == MAX_LEN)
- line[i++] = '\0';
- page++;
- }
- }
- if (i <= MAX_LEN)
- line[i] = '\0';
- if (!end_reached)
- page++;
- return line;
- }
- static void print_position(WINDOW * win, int height, int width)
- {
- int fpos, 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);
- wprintw(win, "(%3d%%)", percent);
- }
|