1 /* 2 * $Id: guage.c,v 1.52 2011/01/17 10:39:28 tom Exp $ 3 * 4 * guage.c -- implements the gauge dialog 5 * 6 * Copyright 2000-2010,2011 Thomas E. Dickey 7 * 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. 11 * 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. 16 * 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. 22 * 23 * An earlier version of this program lists as authors 24 * Marc Ewing, Red Hat Software 25 */ 26 27 #include <dialog.h> 28 29 #include <errno.h> 30 31 #define MY_LEN (MAX_LEN)/2 32 33 #define MIN_HIGH (4) 34 #define MIN_WIDE (10 + 2 * (2 + MARGIN)) 35 36 #define isMarker(buf) !strncmp(buf, "XXX", (size_t) 3) 37 38 typedef struct { 39 DIALOG_CALLBACK obj; 40 WINDOW *text; 41 const char *title; 42 char *prompt; 43 char prompt_buf[MY_LEN]; 44 int percent; 45 int height; 46 int width; 47 char line[MAX_LEN + 1]; 48 } MY_OBJ; 49 50 static int 51 read_data(char *buffer, FILE *fp) 52 { 53 int result; 54 55 if (feof(fp)) { 56 result = 0; 57 } else if (fgets(buffer, MY_LEN, fp) != 0) { 58 dlg_trim_string(buffer); 59 result = 1; 60 } else { 61 result = -1; 62 } 63 return result; 64 } 65 66 static int 67 decode_percent(char *buffer) 68 { 69 char *tmp = 0; 70 long value = strtol(buffer, &tmp, 10); 71 72 if (tmp != 0 && (*tmp == 0 || isspace(UCH(*tmp))) && value >= 0) { 73 return TRUE; 74 } 75 return FALSE; 76 } 77 78 static void 79 repaint_text(MY_OBJ * obj) 80 { 81 WINDOW *dialog = obj->obj.win; 82 int i, x; 83 84 if (dialog != 0 && obj->obj.input != 0) { 85 (void) werase(dialog); 86 dlg_draw_box(dialog, 0, 0, obj->height, obj->width, dialog_attr, border_attr); 87 88 dlg_draw_title(dialog, obj->title); 89 90 wattrset(dialog, dialog_attr); 91 dlg_print_autowrap(dialog, obj->prompt, obj->height, obj->width); 92 93 dlg_draw_box(dialog, 94 obj->height - 4, 2 + MARGIN, 95 2 + MARGIN, obj->width - 2 * (2 + MARGIN), 96 dialog_attr, 97 border_attr); 98 99 /* 100 * Clear the area for the progress bar by filling it with spaces 101 * in the title-attribute, and write the percentage with that 102 * attribute. 103 */ 104 (void) wmove(dialog, obj->height - 3, 4); 105 wattrset(dialog, gauge_attr); 106 107 for (i = 0; i < (obj->width - 2 * (3 + MARGIN)); i++) 108 (void) waddch(dialog, ' '); 109 110 (void) wmove(dialog, obj->height - 3, (obj->width / 2) - 2); 111 (void) wprintw(dialog, "%3d%%", obj->percent); 112 113 /* 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. 117 */ 118 x = (obj->percent * (obj->width - 2 * (3 + MARGIN))) / 100; 119 if ((title_attr & A_REVERSE) != 0) { 120 wattroff(dialog, A_REVERSE); 121 } else { 122 wattrset(dialog, A_REVERSE); 123 } 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) { 128 ch2 &= ~A_REVERSE; 129 } 130 (void) waddch(dialog, ch2); 131 } 132 133 (void) wrefresh(dialog); 134 } 135 } 136 137 static bool 138 handle_input(DIALOG_CALLBACK * cb) 139 { 140 MY_OBJ *obj = (MY_OBJ *) cb; 141 bool result; 142 int status; 143 char buf[MY_LEN]; 144 145 if (dialog_state.pipe_input == 0) { 146 status = -1; 147 } else if ((status = read_data(buf, dialog_state.pipe_input)) > 0) { 148 149 if (isMarker(buf)) { 150 /* 151 * Historically, next line should be percentage, but one of the 152 * worse-written clones of 'dialog' assumes the number is missing. 153 * (Gresham's Law applied to software). 154 */ 155 if ((status = read_data(buf, dialog_state.pipe_input)) > 0) { 156 157 obj->prompt_buf[0] = '\0'; 158 if (decode_percent(buf)) 159 obj->percent = atoi(buf); 160 else 161 strcpy(obj->prompt_buf, buf); 162 163 /* Rest is message text */ 164 while ((status = read_data(buf, dialog_state.pipe_input)) > 0 165 && !isMarker(buf)) { 166 if (strlen(obj->prompt_buf) + strlen(buf) < 167 sizeof(obj->prompt_buf) - 1) { 168 strcat(obj->prompt_buf, buf); 169 } 170 } 171 172 if (obj->prompt != obj->prompt_buf) 173 free(obj->prompt); 174 obj->prompt = obj->prompt_buf; 175 } 176 } else if (decode_percent(buf)) { 177 obj->percent = atoi(buf); 178 } 179 } else { 180 if (feof(dialog_state.pipe_input) || 181 (ferror(dialog_state.pipe_input) && errno != EINTR)) { 182 dlg_remove_callback(cb); 183 } 184 } 185 186 if (status > 0) { 187 result = TRUE; 188 repaint_text(obj); 189 } else { 190 result = FALSE; 191 } 192 193 return result; 194 } 195 196 static bool 197 handle_my_getc(DIALOG_CALLBACK * cb, int ch, int fkey, int *result) 198 { 199 int status = TRUE; 200 201 *result = DLG_EXIT_OK; 202 if (cb != 0) { 203 if (!fkey && (ch == ERR)) { 204 (void) handle_input(cb); 205 status = (cb->input != 0); 206 } 207 } else { 208 status = FALSE; 209 } 210 return status; 211 } 212 213 static void 214 my_cleanup(DIALOG_CALLBACK * cb) 215 { 216 MY_OBJ *obj = (MY_OBJ *) cb; 217 218 if (obj != 0) { 219 if (obj->prompt != obj->prompt_buf) 220 free(obj->prompt); 221 } 222 } 223 224 /* 225 * Display a gauge, or progress meter. Starts at percent% and reads stdin. If 226 * stdin is not XXX, then it is interpreted as a percentage, and the display is 227 * updated accordingly. Otherwise the next line is the percentage, and 228 * subsequent lines up to another XXX are used for the new prompt. Note that 229 * the size of the window never changes, so the prompt can not get any larger 230 * than the height and width specified. 231 */ 232 int 233 dialog_gauge(const char *title, 234 const char *cprompt, 235 int height, 236 int width, 237 int percent) 238 { 239 #ifdef KEY_RESIZE 240 int old_height = height; 241 int old_width = width; 242 #endif 243 int fkey; 244 int ch, result; 245 int x, y; 246 char *prompt = dlg_strclone(cprompt); 247 WINDOW *dialog; 248 MY_OBJ *obj = 0; 249 250 curs_set(0); 251 252 dlg_tab_correct_str(prompt); 253 254 #ifdef KEY_RESIZE 255 retry: 256 #endif 257 258 dlg_auto_size(title, prompt, &height, &width, MIN_HIGH, MIN_WIDE); 259 dlg_print_size(height, width); 260 dlg_ctl_size(height, width); 261 262 /* center dialog box on screen */ 263 x = dlg_box_x_ordinate(width); 264 y = dlg_box_y_ordinate(height); 265 266 dialog = dlg_new_window(height, width, y, x); 267 268 if (obj == 0) { 269 MY_OBJ **objref; 270 271 obj = dlg_calloc(MY_OBJ, 1); 272 assert_ptr(obj, "dialog_gauge"); 273 274 objref = &obj; 275 obj->obj.input = dialog_state.pipe_input; 276 obj->obj.win = dialog; 277 obj->obj.keep_win = TRUE; 278 obj->obj.bg_task = TRUE; 279 obj->obj.handle_getc = handle_my_getc; 280 obj->obj.handle_input = handle_input; 281 obj->title = title; 282 obj->prompt = prompt; 283 obj->percent = percent; 284 obj->height = height; 285 obj->width = width; 286 dlg_add_callback_ref((DIALOG_CALLBACK **) objref, my_cleanup); 287 } else { 288 obj->obj.win = dialog; 289 } 290 291 repaint_text(obj); 292 293 do { 294 ch = dlg_getc(dialog, &fkey); 295 #ifdef KEY_RESIZE 296 if (fkey && ch == KEY_RESIZE) { 297 /* reset data */ 298 height = old_height; 299 width = old_width; 300 /* repaint */ 301 dlg_clear(); 302 dlg_del_window(dialog); 303 refresh(); 304 dlg_mouse_free_regions(); 305 goto retry; 306 } 307 #endif 308 } 309 while (handle_my_getc(&(obj->obj), ch, fkey, &result)); 310 311 curs_set(1); 312 dlg_del_window(dialog); 313 314 return (DLG_EXIT_OK); 315 } 316