xref: /freebsd/contrib/dialog/mixedgauge.c (revision f39bffc62c1395bde25d152c7f68fdf7cbaab414)
1 /*
2  *  $Id: mixedgauge.c,v 1.30 2012/11/18 16:30:20 tom Exp $
3  *
4  *  mixedgauge.c -- implements the mixedgauge dialog
5  *
6  *  Copyright 2007-2011,2012	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  *  This is inspired by a patch from Kiran Cherupally
24  *  (but different interface design).
25  */
26 
27 #include <dialog.h>
28 
29 #define LLEN(n) ((n) * MIXEDGAUGE_TAGS)
30 #define ItemData(i)    &items[LLEN(i)]
31 #define ItemName(i)    items[LLEN(i)]
32 #define ItemText(i)    items[LLEN(i) + 1]
33 
34 #define MIN_HIGH (4)
35 #define MIN_WIDE (10 + 2 * (2 + MARGIN))
36 
37 typedef struct {
38     WINDOW *dialog;
39     WINDOW *caption;
40     const char *title;
41     char *prompt;
42     int height, old_height, min_height;
43     int width, old_width, min_width;
44     int len_name, len_text;
45     int item_no;
46     DIALOG_LISTITEM *list;
47 } DIALOG_MIXEDGAUGE;
48 
49 static const char *
50 status_string(char *given, char **freeMe)
51 {
52     const char *result;
53 
54     *freeMe = 0;
55     if (isdigit(UCH(*given))) {
56 	switch (*given) {
57 	case '0':
58 	    result = _("Succeeded");
59 	    break;
60 	case '1':
61 	    result = _("Failed");
62 	    break;
63 	case '2':
64 	    result = _("Passed");
65 	    break;
66 	case '3':
67 	    result = _("Completed");
68 	    break;
69 	case '4':
70 	    result = _("Checked");
71 	    break;
72 	case '5':
73 	    result = _("Done");
74 	    break;
75 	case '6':
76 	    result = _("Skipped");
77 	    break;
78 	case '7':
79 	    result = _("In Progress");
80 	    break;
81 	case '8':
82 	    result = "";
83 	    break;
84 	case '9':
85 	    result = _("N/A");
86 	    break;
87 	default:
88 	    result = "?";
89 	    break;
90 	}
91     } else if (*given == '-') {
92 	size_t need = strlen(++given);
93 	char *temp = dlg_malloc(char, need);
94 	*freeMe = temp;
95 	sprintf(temp, "%3s%%", given);
96 	result = temp;
97     } else if (!isspace(UCH(*given))) {
98 	result = given;
99     } else {
100 	result = 0;
101     }
102     return result;
103 }
104 
105 /* This function displays status messages */
106 static void
107 myprint_status(DIALOG_MIXEDGAUGE * dlg)
108 {
109     WINDOW *win = dlg->dialog;
110     int limit_y = dlg->height;
111     int limit_x = dlg->width;
112 
113     int y = MARGIN;
114     int item;
115     int cells = dlg->len_text - 2;
116     int lm = limit_x - dlg->len_text - 1;
117     int bm = limit_y;		/* bottom margin */
118     int last_y = 0, last_x = 0;
119     int j, xxx;
120     float percent;
121     const char *status = "";
122     char *freeMe = 0;
123 
124     bm -= (2 * MARGIN);
125     getyx(win, last_y, last_x);
126     for (item = 0; item < dlg->item_no; ++item) {
127 	chtype attr = A_NORMAL;
128 
129 	y = item + MARGIN + 1;
130 	if (y > bm)
131 	    break;
132 
133 	status = status_string(dlg->list[item].text, &freeMe);
134 	if (status == 0 || *status == 0)
135 	    continue;
136 
137 	(void) wmove(win, y, 2 * MARGIN);
138 	dlg_print_text(win, dlg->list[item].name, lm, &attr);
139 
140 	(void) wmove(win, y, lm);
141 	(void) waddch(win, '[');
142 	(void) wmove(win, y, lm + (cells - (int) strlen(status)) / 2);
143 	if (freeMe) {
144 	    (void) wmove(win, y, lm + 1);
145 	    (void) wattrset(win, title_attr);
146 	    for (j = 0; j < cells; j++)
147 		(void) waddch(win, ' ');
148 
149 	    (void) wmove(win, y, lm + (cells - (int) strlen(status)) / 2);
150 	    (void) waddstr(win, status);
151 
152 	    if ((title_attr & A_REVERSE) != 0) {
153 		wattroff(win, A_REVERSE);
154 	    } else {
155 		(void) wattrset(win, A_REVERSE);
156 	    }
157 	    (void) wmove(win, y, lm + 1);
158 
159 	    if (sscanf(status, "%f%%", &percent) != 1)
160 		percent = 0.0;
161 	    xxx = (int) ((cells * (percent + 0.5)) / 100.0);
162 	    for (j = 0; j < xxx; j++) {
163 		chtype ch1 = winch(win);
164 		if (title_attr & A_REVERSE) {
165 		    ch1 &= ~A_REVERSE;
166 		}
167 		(void) waddch(win, ch1);
168 	    }
169 	    free(freeMe);
170 
171 	} else {
172 	    (void) wmove(win, y, lm + (cells - (int) strlen(status)) / 2);
173 	    (void) waddstr(win, status);
174 	}
175 	(void) wmove(win, y, limit_x - 3);
176 	(void) waddch(win, ']');
177 	(void) wnoutrefresh(win);
178     }
179     if (win != 0)
180 	wmove(win, last_y, last_x);
181 }
182 
183 static void
184 mydraw_mixed_box(WINDOW *win, int y, int x, int height, int width,
185 		 chtype boxchar, chtype borderchar)
186 {
187     dlg_draw_box(win, y, x, height, width, boxchar, borderchar);
188     {
189 	chtype attr = A_NORMAL;
190 	const char *message = _("Overall Progress");
191 	chtype save2 = dlg_get_attrs(win);
192 	(void) wattrset(win, title_attr);
193 	(void) wmove(win, y, x + 2);
194 	dlg_print_text(win, message, width, &attr);
195 	(void) wattrset(win, save2);
196     }
197 }
198 
199 static char *
200 clean_copy(const char *string)
201 {
202     char *result = dlg_strclone(string);
203 
204     dlg_trim_string(result);
205     dlg_tab_correct_str(result);
206     return result;
207 }
208 
209 /*
210  * Update mixed-gauge dialog (may be from pipe, may be via direct calls).
211  */
212 static void
213 dlg_update_mixedgauge(DIALOG_MIXEDGAUGE * dlg, int percent)
214 {
215     int i, x;
216 
217     /*
218      * Clear the area for the progress bar by filling it with spaces
219      * in the title-attribute, and write the percentage with that
220      * attribute.
221      */
222     (void) wmove(dlg->dialog, dlg->height - 3, 4);
223     (void) wattrset(dlg->dialog, gauge_attr);
224 
225     for (i = 0; i < (dlg->width - 2 * (3 + MARGIN)); i++)
226 	(void) waddch(dlg->dialog, ' ');
227 
228     (void) wmove(dlg->dialog, dlg->height - 3, (dlg->width / 2) - 2);
229     (void) wprintw(dlg->dialog, "%3d%%", percent);
230 
231     /*
232      * Now draw a bar in reverse, relative to the background.
233      * The window attribute was useful for painting the background,
234      * but requires some tweaks to reverse it.
235      */
236     x = (percent * (dlg->width - 2 * (3 + MARGIN))) / 100;
237     if ((title_attr & A_REVERSE) != 0) {
238 	wattroff(dlg->dialog, A_REVERSE);
239     } else {
240 	(void) wattrset(dlg->dialog, A_REVERSE);
241     }
242     (void) wmove(dlg->dialog, dlg->height - 3, 4);
243     for (i = 0; i < x; i++) {
244 	chtype ch = winch(dlg->dialog);
245 	if (title_attr & A_REVERSE) {
246 	    ch &= ~A_REVERSE;
247 	}
248 	(void) waddch(dlg->dialog, ch);
249     }
250     myprint_status(dlg);
251     dlg_trace_win(dlg->dialog);
252 }
253 
254 /*
255  * Setup dialog.
256  */
257 static void
258 dlg_begin_mixedgauge(DIALOG_MIXEDGAUGE * dlg,
259 		     int *began,
260 		     const char *aTitle,
261 		     const char *aPrompt,
262 		     int aHeight,
263 		     int aWidth,
264 		     int aItemNo,
265 		     char **items)
266 {
267     int n, y, x;
268 
269     if (!*began) {
270 	curs_set(0);
271 
272 	memset(dlg, 0, sizeof(*dlg));
273 	dlg->title = aTitle;
274 	dlg->prompt = clean_copy(aPrompt);
275 	dlg->height = dlg->old_height = aHeight;
276 	dlg->width = dlg->old_width = aWidth;
277 	dlg->item_no = aItemNo;
278 
279 	dlg->list = dlg_calloc(DIALOG_LISTITEM, (size_t) aItemNo);
280 	assert_ptr(dlg->list, "dialog_mixedgauge");
281 
282 	dlg->len_name = 0;
283 	dlg->len_text = 15;
284 
285 	for (n = 0; n < aItemNo; ++n) {
286 	    int thisWidth = (int) strlen(ItemName(n));
287 	    if (dlg->len_name < thisWidth)
288 		dlg->len_name = thisWidth;
289 	    dlg->list[n].name = ItemName(n);
290 	    dlg->list[n].text = ItemText(n);
291 	}
292 
293 	dlg->min_height = MIN_HIGH + aItemNo;
294 	dlg->min_width = MIN_WIDE + dlg->len_name + GUTTER + dlg->len_text;
295 
296 	if (dlg->prompt != 0 && *(dlg->prompt) != 0)
297 	    dlg->min_height += (2 * MARGIN);
298 #ifdef KEY_RESIZE
299 	nodelay(stdscr, TRUE);
300 #endif
301     }
302 #ifdef KEY_RESIZE
303     else {
304 	dlg_del_window(dlg->dialog);
305 	dlg->height = dlg->old_height;
306 	dlg->width = dlg->old_width;
307     }
308 #endif
309 
310     dlg_auto_size(dlg->title, dlg->prompt,
311 		  &(dlg->height),
312 		  &(dlg->width),
313 		  dlg->min_height,
314 		  dlg->min_width);
315     dlg_print_size(dlg->height, dlg->width);
316     dlg_ctl_size(dlg->height, dlg->width);
317 
318     /* center dialog box on screen */
319     x = dlg_box_x_ordinate(dlg->width);
320     y = dlg_box_y_ordinate(dlg->height);
321 
322     dlg->dialog = dlg_new_window(dlg->height, dlg->width, y, x);
323 
324     (void) werase(dlg->dialog);
325     dlg_draw_box2(dlg->dialog,
326 		  0, 0,
327 		  dlg->height,
328 		  dlg->width,
329 		  dialog_attr, border_attr, border2_attr);
330 
331     dlg_draw_title(dlg->dialog, dlg->title);
332     dlg_draw_helpline(dlg->dialog, FALSE);
333 
334     if ((dlg->prompt != 0 && *(dlg->prompt) != 0)
335 	&& wmove(dlg->dialog, dlg->item_no, 0) != ERR) {
336 	dlg->caption = dlg_sub_window(dlg->dialog,
337 				      dlg->height - dlg->item_no - (2 * MARGIN),
338 				      dlg->width,
339 				      y + dlg->item_no + (2 * MARGIN),
340 				      x);
341 	(void) wattrset(dlg->caption, dialog_attr);
342 	dlg_print_autowrap(dlg->caption, dlg->prompt, dlg->height, dlg->width);
343     }
344 
345     mydraw_mixed_box(dlg->dialog,
346 		     dlg->height - 4,
347 		     2 + MARGIN,
348 		     2 + MARGIN,
349 		     dlg->width - 2 * (2 + MARGIN),
350 		     dialog_attr,
351 		     border_attr);
352 
353     *began += 1;
354 }
355 
356 /*
357  * Discard the mixed-gauge dialog.
358  */
359 static int
360 dlg_finish_mixedgauge(DIALOG_MIXEDGAUGE * dlg, int status)
361 {
362     (void) wrefresh(dlg->dialog);
363 #ifdef KEY_RESIZE
364     nodelay(stdscr, FALSE);
365 #endif
366     curs_set(1);
367     dlg_del_window(dlg->dialog);
368     return status;
369 }
370 
371 /*
372  * Setup dialog, read mixed-gauge data from pipe.
373  */
374 int
375 dialog_mixedgauge(const char *title,
376 		  const char *cprompt,
377 		  int height,
378 		  int width,
379 		  int percent,
380 		  int item_no,
381 		  char **items)
382 {
383     DIALOG_MIXEDGAUGE dlg;
384     int began = 0;
385 
386     dlg_begin_mixedgauge(&dlg, &began, title, cprompt, height,
387 			 width, item_no, items);
388 
389     dlg_update_mixedgauge(&dlg, percent);
390 
391     return dlg_finish_mixedgauge(&dlg, DLG_EXIT_OK);
392 }
393