2 * $Id: guage.c,v 1.69 2015/02/26 02:07:12 tom Exp $
4 * guage.c -- implements the gauge dialog
6 * Copyright 2000-2013,2015 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", (size_t) 3)
38 typedef struct _my_obj
{
39 DIALOG_CALLBACK obj
; /* has to be first in struct */
44 char prompt_buf
[MY_LEN
];
48 char line
[MAX_LEN
+ 1];
51 static MY_OBJ
*all_objects
;
56 MY_OBJ
*list
= all_objects
;
72 MY_OBJ
*p
= all_objects
;
79 all_objects
= p
->next
;
89 read_data(char *buffer
, FILE *fp
)
95 } else if (fgets(buffer
, MY_LEN
, fp
) != 0) {
96 DLG_TRACE(("read_data:%s", buffer
));
97 buffer
[MY_LEN
] = '\0';
98 dlg_trim_string(buffer
);
107 decode_percent(char *buffer
)
110 long value
= strtol(buffer
, &tmp
, 10);
112 if (tmp
!= 0 && (*tmp
== 0 || isspace(UCH(*tmp
))) && value
>= 0) {
119 repaint_text(MY_OBJ
* obj
)
121 WINDOW
*dialog
= obj
->obj
.win
;
125 (void) werase(dialog
);
126 dlg_draw_box2(dialog
, 0, 0, obj
->height
, obj
->width
, dialog_attr
,
127 border_attr
, border2_attr
);
129 dlg_draw_title(dialog
, obj
->title
);
131 (void) wattrset(dialog
, dialog_attr
);
132 dlg_draw_helpline(dialog
, FALSE
);
133 dlg_print_autowrap(dialog
, obj
->prompt
, obj
->height
, obj
->width
);
135 dlg_draw_box2(dialog
,
136 obj
->height
- 4, 2 + MARGIN
,
137 2 + MARGIN
, obj
->width
- 2 * (2 + MARGIN
),
143 * Clear the area for the progress bar by filling it with spaces
144 * in the gauge-attribute, and write the percentage with that
147 (void) wmove(dialog
, obj
->height
- 3, 4);
148 (void) wattrset(dialog
, gauge_attr
);
150 for (i
= 0; i
< (obj
->width
- 2 * (3 + MARGIN
)); i
++)
151 (void) waddch(dialog
, ' ');
153 (void) wmove(dialog
, obj
->height
- 3, (obj
->width
/ 2) - 2);
154 (void) wprintw(dialog
, "%3d%%", obj
->percent
);
157 * Now draw a bar in reverse, relative to the background.
158 * The window attribute was useful for painting the background,
159 * but requires some tweaks to reverse it.
161 x
= (obj
->percent
* (obj
->width
- 2 * (3 + MARGIN
))) / 100;
162 if ((gauge_attr
& A_REVERSE
) != 0) {
163 wattroff(dialog
, A_REVERSE
);
165 (void) wattrset(dialog
, A_REVERSE
);
167 (void) wmove(dialog
, obj
->height
- 3, 4);
168 for (i
= 0; i
< x
; i
++) {
169 chtype ch2
= winch(dialog
);
170 if (gauge_attr
& A_REVERSE
) {
173 (void) waddch(dialog
, ch2
);
176 (void) wrefresh(dialog
);
181 handle_input(DIALOG_CALLBACK
* cb
)
183 MY_OBJ
*obj
= (MY_OBJ
*) cb
;
186 char buf
[MY_LEN
+ 1];
188 if (dialog_state
.pipe_input
== 0) {
191 dlg_remove_callback(cb
);
192 } else if ((status
= read_data(buf
, dialog_state
.pipe_input
)) > 0) {
196 * Historically, next line should be percentage, but one of the
197 * worse-written clones of 'dialog' assumes the number is missing.
198 * (Gresham's Law applied to software).
200 if ((status
= read_data(buf
, dialog_state
.pipe_input
)) > 0) {
202 obj
->prompt_buf
[0] = '\0';
203 if (decode_percent(buf
))
204 obj
->percent
= atoi(buf
);
206 strcpy(obj
->prompt_buf
, buf
);
208 /* Rest is message text */
209 while ((status
= read_data(buf
, dialog_state
.pipe_input
)) > 0
211 if (strlen(obj
->prompt_buf
) + strlen(buf
) <
212 sizeof(obj
->prompt_buf
) - 1) {
213 strcat(obj
->prompt_buf
, buf
);
217 if (obj
->prompt
!= obj
->prompt_buf
)
219 obj
->prompt
= obj
->prompt_buf
;
221 } else if (decode_percent(buf
)) {
222 obj
->percent
= atoi(buf
);
225 if (feof(dialog_state
.pipe_input
) ||
226 (ferror(dialog_state
.pipe_input
) && errno
!= EINTR
)) {
228 dlg_remove_callback(cb
);
244 handle_my_getc(DIALOG_CALLBACK
* cb
, int ch
, int fkey
, int *result
)
248 *result
= DLG_EXIT_OK
;
250 if (!fkey
&& (ch
== ERR
)) {
251 (void) handle_input(cb
);
252 /* cb might be freed in handle_input */
253 status
= (valid((MY_OBJ
*) cb
) && (cb
->input
!= 0));
262 my_cleanup(DIALOG_CALLBACK
* cb
)
264 MY_OBJ
*obj
= (MY_OBJ
*) cb
;
267 if (obj
->prompt
!= obj
->prompt_buf
) {
269 obj
->prompt
= obj
->prompt_buf
;
276 dlg_update_gauge(void *objptr
, int percent
)
278 MY_OBJ
*obj
= (MY_OBJ
*) objptr
;
279 bool save_finish_string
= dialog_state
.finish_string
;
281 dialog_state
.finish_string
= TRUE
;
283 obj
->percent
= percent
;
285 dialog_state
.finish_string
= save_finish_string
;
289 * (Re)Allocates an object and fills it as per the arguments
292 dlg_reallocate_gauge(void *objptr
,
299 char *prompt
= dlg_strclone(cprompt
);
300 MY_OBJ
*obj
= objptr
;
301 bool save_finish_string
= dialog_state
.finish_string
;
303 dialog_state
.finish_string
= TRUE
;
304 dlg_tab_correct_str(prompt
);
307 /* create a new object */
308 obj
= dlg_calloc(MY_OBJ
, 1);
309 assert_ptr(obj
, "dialog_gauge");
311 dlg_auto_size(title
, prompt
, &height
, &width
, MIN_HIGH
, MIN_WIDE
);
312 dlg_print_size(height
, width
);
313 dlg_ctl_size(height
, width
);
316 /* reuse an existing object */
318 height
= obj
->height
;
322 if (obj
->obj
.win
== 0) {
323 /* center dialog box on screen */
324 int x
= dlg_box_x_ordinate(width
);
325 int y
= dlg_box_y_ordinate(height
);
326 WINDOW
*dialog
= dlg_new_window(height
, width
, y
, x
);
327 obj
->obj
.win
= dialog
;
330 obj
->obj
.input
= dialog_state
.pipe_input
;
331 obj
->obj
.keep_win
= TRUE
;
332 obj
->obj
.bg_task
= TRUE
;
333 obj
->obj
.handle_getc
= handle_my_getc
;
334 obj
->obj
.handle_input
= handle_input
;
336 if (obj
->title
== 0 || strcmp(obj
->title
, title
)) {
337 dlg_finish_string(obj
->title
);
339 obj
->title
= dlg_strclone(title
);
342 dlg_finish_string(obj
->prompt
);
345 obj
->prompt
= prompt
;
346 obj
->percent
= percent
;
347 obj
->height
= height
;
350 /* if this was a new object, link it into the list */
352 obj
->next
= all_objects
;
356 dialog_state
.finish_string
= save_finish_string
;
361 dlg_allocate_gauge(const char *title
,
367 return dlg_reallocate_gauge(NULL
, title
, cprompt
, height
, width
, percent
);
371 dlg_free_gauge(void *objptr
)
373 MY_OBJ
*obj
= (MY_OBJ
*) objptr
;
378 obj
->obj
.keep_win
= FALSE
;
379 dlg_remove_callback(&(obj
->obj
));
384 * Display a gauge, or progress meter. Starts at percent% and reads stdin. If
385 * stdin is not XXX, then it is interpreted as a percentage, and the display is
386 * updated accordingly. Otherwise the next line is the percentage, and
387 * subsequent lines up to another XXX are used for the new prompt. Note that
388 * the size of the window never changes, so the prompt can not get any larger
389 * than the height and width specified.
392 dialog_gauge(const char *title
,
400 void *objptr
= dlg_allocate_gauge(title
, cprompt
, height
, width
, percent
);
401 MY_OBJ
*obj
= (MY_OBJ
*) objptr
;
403 dlg_add_callback_ref((DIALOG_CALLBACK
**) & obj
, my_cleanup
);
404 dlg_update_gauge(obj
, percent
);
406 dlg_trace_win(obj
->obj
.win
);
408 ch
= dlg_getc(obj
->obj
.win
, &fkey
);
410 if (fkey
&& ch
== KEY_RESIZE
) {
411 MY_OBJ
*oldobj
= obj
;
413 dlg_mouse_free_regions();
415 obj
= dlg_allocate_gauge(title
,
421 /* avoid breaking new window in dlg_remove_callback */
422 oldobj
->obj
.caller
= 0;
423 oldobj
->obj
.input
= 0;
424 oldobj
->obj
.keep_win
= FALSE
;
426 /* remove the old version of the gauge */
428 dlg_remove_callback(&(oldobj
->obj
));
431 dlg_add_callback_ref((DIALOG_CALLBACK
**) & obj
, my_cleanup
);
432 dlg_update_gauge(obj
, obj
->percent
);
436 while (valid(obj
) && handle_my_getc(&(obj
->obj
), ch
, fkey
, &result
));
440 return (DLG_EXIT_OK
);