1 /* 2 * $Id: guage.c,v 1.45 2010/01/19 09:15:20 tom Exp $ 3 * 4 * guage.c -- implements the gauge dialog 5 * 6 * Copyright 2000-2007,2010 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", 3) 37 38 typedef struct { 39 DIALOG_CALLBACK obj; 40 WINDOW *text; 41 bool done; 42 const char *title; 43 char *prompt; 44 char prompt_buf[MY_LEN]; 45 int percent; 46 int height; 47 int width; 48 char line[MAX_LEN + 1]; 49 } MY_OBJ; 50 51 static int 52 read_data(char *buffer, FILE *fp) 53 { 54 int result; 55 56 if (feof(fp)) { 57 result = 0; 58 } else if (fgets(buffer, MY_LEN, fp) != 0) { 59 dlg_trim_string(buffer); 60 result = 1; 61 } else { 62 result = -1; 63 } 64 return result; 65 } 66 67 static int 68 decode_percent(char *buffer) 69 { 70 char *tmp = 0; 71 long value = strtol(buffer, &tmp, 10); 72 73 if (tmp != 0 && (*tmp == 0 || isspace(UCH(*tmp))) && value >= 0) { 74 return TRUE; 75 } 76 return FALSE; 77 } 78 79 static void 80 repaint_text(MY_OBJ * obj) 81 { 82 WINDOW *dialog = obj->obj.win; 83 int i, x; 84 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, title_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 static int 137 handle_input(MY_OBJ * obj) 138 { 139 int status; 140 char buf[MY_LEN]; 141 142 if ((status = read_data(buf, dialog_state.pipe_input)) > 0) { 143 144 if (isMarker(buf)) { 145 /* 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). 149 */ 150 if ((status = read_data(buf, dialog_state.pipe_input)) > 0) { 151 152 obj->prompt_buf[0] = '\0'; 153 if (decode_percent(buf)) 154 obj->percent = atoi(buf); 155 else 156 strcpy(obj->prompt_buf, buf); 157 158 /* Rest is message text */ 159 while ((status = read_data(buf, dialog_state.pipe_input)) > 0 160 && !isMarker(buf)) { 161 if (strlen(obj->prompt_buf) + strlen(buf) < 162 sizeof(obj->prompt_buf) - 1) { 163 strcat(obj->prompt_buf, buf); 164 } 165 } 166 167 if (obj->prompt != obj->prompt_buf) 168 free(obj->prompt); 169 obj->prompt = obj->prompt_buf; 170 } 171 } else if (decode_percent(buf)) { 172 obj->percent = atoi(buf); 173 } 174 } else { 175 obj->done = TRUE; 176 } 177 178 return status; 179 } 180 181 static bool 182 handle_my_getc(DIALOG_CALLBACK * cb, int ch, int fkey, int *result) 183 { 184 MY_OBJ *obj = (MY_OBJ *) cb; 185 int status = TRUE; 186 187 *result = DLG_EXIT_OK; 188 if (obj != 0) { 189 if (!fkey && (ch == ERR)) { 190 if (handle_input(obj) > 0) 191 repaint_text(obj); 192 else 193 status = FALSE; 194 } 195 } else { 196 status = FALSE; 197 } 198 return status; 199 } 200 201 static void 202 my_cleanup(DIALOG_CALLBACK * cb) 203 { 204 MY_OBJ *obj = (MY_OBJ *) cb; 205 206 if (obj != 0) { 207 if (obj->prompt != obj->prompt_buf) 208 free(obj->prompt); 209 } 210 } 211 212 /* 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. 219 */ 220 int 221 dialog_gauge(const char *title, 222 const char *cprompt, 223 int height, 224 int width, 225 int percent) 226 { 227 #ifdef KEY_RESIZE 228 int old_height = height; 229 int old_width = width; 230 #endif 231 int fkey; 232 int ch, result; 233 int x, y; 234 char *prompt = dlg_strclone(cprompt); 235 WINDOW *dialog; 236 MY_OBJ *obj = 0; 237 238 curs_set(0); 239 240 dlg_tab_correct_str(prompt); 241 242 #ifdef KEY_RESIZE 243 retry: 244 #endif 245 246 dlg_auto_size(title, prompt, &height, &width, MIN_HIGH, MIN_WIDE); 247 dlg_print_size(height, width); 248 dlg_ctl_size(height, width); 249 250 /* center dialog box on screen */ 251 x = dlg_box_x_ordinate(width); 252 y = dlg_box_y_ordinate(height); 253 254 dialog = dlg_new_window(height, width, y, x); 255 256 if (obj == 0) { 257 MY_OBJ **objref; 258 259 obj = dlg_calloc(MY_OBJ, 1); 260 assert_ptr(obj, "dialog_gauge"); 261 262 objref = &obj; 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; 268 obj->title = title; 269 obj->prompt = prompt; 270 obj->percent = percent; 271 obj->height = height; 272 obj->width = width; 273 dlg_add_callback_ref((DIALOG_CALLBACK **) objref, my_cleanup); 274 } 275 276 repaint_text(obj); 277 278 do { 279 ch = dlg_getc(dialog, &fkey); 280 #ifdef KEY_RESIZE 281 if (fkey && ch == KEY_RESIZE) { 282 /* reset data */ 283 height = old_height; 284 width = old_width; 285 /* repaint */ 286 dlg_clear(); 287 dlg_del_window(dialog); 288 refresh(); 289 dlg_mouse_free_regions(); 290 goto retry; 291 } 292 #endif 293 } 294 while (handle_my_getc(&(obj->obj), ch, fkey, &result)); 295 296 curs_set(1); 297 dlg_del_window(dialog); 298 299 return (DLG_EXIT_OK); 300 } 301