2 * $Id: guage.c,v 1.45 2010/01/19 09:15:20 tom Exp $
4 * guage.c -- implements the gauge dialog
6 * Copyright 2000-2007,2010 Thomas E. Dickey
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, version 2.1
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this program; if not, write to
19 * Free Software Foundation, Inc.
20 * 51 Franklin St., Fifth Floor
21 * Boston, MA 02110, USA.
23 * An earlier version of this program lists as authors
24 * Marc Ewing, Red Hat Software
31 #define MY_LEN (MAX_LEN)/2
34 #define MIN_WIDE (10 + 2 * (2 + MARGIN))
36 #define isMarker(buf) !strncmp(buf, "XXX", 3)
44 char prompt_buf[MY_LEN];
48 char line[MAX_LEN + 1];
52 read_data(char *buffer, FILE *fp)
58 } else if (fgets(buffer, MY_LEN, fp) != 0) {
59 dlg_trim_string(buffer);
68 decode_percent(char *buffer)
71 long value = strtol(buffer, &tmp, 10);
73 if (tmp != 0 && (*tmp == 0 || isspace(UCH(*tmp))) && value >= 0) {
80 repaint_text(MY_OBJ * obj)
82 WINDOW *dialog = obj->obj.win;
85 (void) werase(dialog);
86 dlg_draw_box(dialog, 0, 0, obj->height, obj->width, dialog_attr, border_attr);
88 dlg_draw_title(dialog, obj->title);
90 wattrset(dialog, dialog_attr);
91 dlg_print_autowrap(dialog, obj->prompt, obj->height, obj->width);
94 obj->height - 4, 2 + MARGIN,
95 2 + MARGIN, obj->width - 2 * (2 + MARGIN),
100 * Clear the area for the progress bar by filling it with spaces
101 * in the title-attribute, and write the percentage with that
104 (void) wmove(dialog, obj->height - 3, 4);
105 wattrset(dialog, title_attr);
107 for (i = 0; i < (obj->width - 2 * (3 + MARGIN)); i++)
108 (void) waddch(dialog, ' ');
110 (void) wmove(dialog, obj->height - 3, (obj->width / 2) - 2);
111 (void) wprintw(dialog, "%3d%%", obj->percent);
114 * Now draw a bar in reverse, relative to the background.
115 * The window attribute was useful for painting the background,
116 * but requires some tweaks to reverse it.
118 x = (obj->percent * (obj->width - 2 * (3 + MARGIN))) / 100;
119 if ((title_attr & A_REVERSE) != 0) {
120 wattroff(dialog, A_REVERSE);
122 wattrset(dialog, A_REVERSE);
124 (void) wmove(dialog, obj->height - 3, 4);
125 for (i = 0; i < x; i++) {
126 chtype ch2 = winch(dialog);
127 if (title_attr & A_REVERSE) {
130 (void) waddch(dialog, ch2);
133 (void) wrefresh(dialog);
137 handle_input(MY_OBJ * obj)
142 if ((status = read_data(buf, dialog_state.pipe_input)) > 0) {
146 * Historically, next line should be percentage, but one of the
147 * worse-written clones of 'dialog' assumes the number is missing.
148 * (Gresham's Law applied to software).
150 if ((status = read_data(buf, dialog_state.pipe_input)) > 0) {
152 obj->prompt_buf[0] = '\0';
153 if (decode_percent(buf))
154 obj->percent = atoi(buf);
156 strcpy(obj->prompt_buf, buf);
158 /* Rest is message text */
159 while ((status = read_data(buf, dialog_state.pipe_input)) > 0
161 if (strlen(obj->prompt_buf) + strlen(buf) <
162 sizeof(obj->prompt_buf) - 1) {
163 strcat(obj->prompt_buf, buf);
167 if (obj->prompt != obj->prompt_buf)
169 obj->prompt = obj->prompt_buf;
171 } else if (decode_percent(buf)) {
172 obj->percent = atoi(buf);
182 handle_my_getc(DIALOG_CALLBACK * cb, int ch, int fkey, int *result)
184 MY_OBJ *obj = (MY_OBJ *) cb;
187 *result = DLG_EXIT_OK;
189 if (!fkey && (ch == ERR)) {
190 if (handle_input(obj) > 0)
202 my_cleanup(DIALOG_CALLBACK * cb)
204 MY_OBJ *obj = (MY_OBJ *) cb;
207 if (obj->prompt != obj->prompt_buf)
213 * Display a gauge, or progress meter. Starts at percent% and reads stdin. If
214 * stdin is not XXX, then it is interpreted as a percentage, and the display is
215 * updated accordingly. Otherwise the next line is the percentage, and
216 * subsequent lines up to another XXX are used for the new prompt. Note that
217 * the size of the window never changes, so the prompt can not get any larger
218 * than the height and width specified.
221 dialog_gauge(const char *title,
228 int old_height = height;
229 int old_width = width;
234 char *prompt = dlg_strclone(cprompt);
240 dlg_tab_correct_str(prompt);
246 dlg_auto_size(title, prompt, &height, &width, MIN_HIGH, MIN_WIDE);
247 dlg_print_size(height, width);
248 dlg_ctl_size(height, width);
250 /* center dialog box on screen */
251 x = dlg_box_x_ordinate(width);
252 y = dlg_box_y_ordinate(height);
254 dialog = dlg_new_window(height, width, y, x);
259 obj = dlg_calloc(MY_OBJ, 1);
260 assert_ptr(obj, "dialog_gauge");
263 obj->obj.input = dialog_state.pipe_input;
264 obj->obj.win = dialog;
265 obj->obj.keep_win = TRUE;
266 obj->obj.bg_task = TRUE;
267 obj->obj.handle_getc = handle_my_getc;
269 obj->prompt = prompt;
270 obj->percent = percent;
271 obj->height = height;
273 dlg_add_callback_ref((DIALOG_CALLBACK **) objref, my_cleanup);
279 ch = dlg_getc(dialog, &fkey);
281 if (fkey && ch == KEY_RESIZE) {
287 dlg_del_window(dialog);
289 dlg_mouse_free_regions();
294 while (handle_my_getc(&(obj->obj), ch, fkey, &result));
297 dlg_del_window(dialog);
299 return (DLG_EXIT_OK);