2 * $Id: progressbox.c,v 1.8 2010/01/12 10:46:24 tom Exp $
4 * progressbox.c -- implements the progress box
6 * Copyright 2005 Valery Reznic
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as
10 * published by the Free Software Foundation; either version 2.1 of the
11 * License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this program; if not, write to
20 * Free Software Foundation, Inc.
21 * 51 Franklin St., Fifth Floor
22 * Boston, MA 02110, USA.
29 #define MIN_WIDE (10 + 2 * (2 + MARGIN))
34 char line[MAX_LEN + 1];
39 * Return current line of text.
42 get_line(MY_OBJ * obj)
44 FILE *fp = obj->obj.input;
49 if ((ch = getc(fp)) == EOF) {
61 if ((ch == TAB) && (dialog_vars.tab_correct)) {
62 tmpint = dialog_state.tab_len
63 - (col % dialog_state.tab_len);
64 for (j = 0; j < tmpint; j++) {
70 obj->line[col] = (char) ch;
77 obj->line[col] = '\0';
83 * Print a new line of text.
86 print_line(MY_OBJ * obj, WINDOW *win, int row, int width)
89 char *line = obj->line;
91 (void) wmove(win, row, 0); /* move cursor to correct line */
92 (void) waddch(win, ' ');
93 #ifdef NCURSES_VERSION
94 (void) waddnstr(win, line, MIN((int) strlen(line), width - 2));
96 line[MIN((int) strlen(line), width - 2)] = '\0';
101 /* Clear 'residue' of previous line */
102 for (i = 0; i < width - x; i++)
103 (void) waddch(win, ' ');
107 * Display text from a stdin in a scrolling window.
110 dialog_progressbox(const char *title, const char *cprompt, int height, int width)
114 WINDOW *dialog, *text;
116 FILE *fd = dialog_state.pipe_input;
117 char *prompt = dlg_strclone(cprompt);
119 dlg_tab_correct_str(prompt);
120 dlg_auto_size(title, prompt, &height, &width, MIN_HIGH, MIN_WIDE);
121 dlg_print_size(height, width);
122 dlg_ctl_size(height, width);
124 x = dlg_box_x_ordinate(width);
125 y = dlg_box_y_ordinate(height);
126 thigh = height - (2 * MARGIN);
128 dialog = dlg_new_window(height, width, y, x);
130 dlg_draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);
131 dlg_draw_title(dialog, title);
133 if (*prompt != '\0') {
136 wattrset(dialog, dialog_attr);
137 dlg_print_autowrap(dialog, prompt, height, width);
138 getyx(dialog, y2, x2);
140 wmove(dialog, y2, MARGIN);
141 for (i = 0; i < getmaxx(dialog) - 2 * MARGIN; i++)
142 (void) waddch(dialog, dlg_boxchar(ACS_HLINE));
147 /* Create window for text region, used for scrolling text */
148 text = dlg_sub_window(dialog,
150 width - (2 * MARGIN),
154 (void) wrefresh(dialog);
156 (void) wmove(dialog, thigh, (MARGIN + 1));
157 (void) wnoutrefresh(dialog);
159 obj = dlg_calloc(MY_OBJ, 1);
160 assert_ptr(obj, "dialog_progressbox");
163 obj->obj.win = dialog;
166 dlg_attr_clear(text, thigh, getmaxx(text), dialog_attr);
167 for (i = 0; get_line(obj); i++) {
169 print_line(obj, text, i, width - (2 * MARGIN));
171 scrollok(text, TRUE);
173 scrollok(text, FALSE);
174 print_line(obj, text, thigh - 1, width - (2 * MARGIN));
176 (void) wnoutrefresh(text);
177 (void) wrefresh(text);
181 dlg_unregister_window(text);
182 dlg_del_window(dialog);