mixedgauge.c revision 241818
1/*
2 *  $Id: mixedgauge.c,v 1.29 2011/10/20 23:35:31 tom Exp $
3 *
4 *  mixedgauge.c -- implements the mixedgauge dialog
5 *
6 *  Copyright 2007-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 *  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
37typedef 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
49static const char *
50status_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 */
106static void
107myprint_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    if (win) {
125	bm -= (2 * MARGIN);
126    }
127    if (win != 0)
128	getyx(win, last_y, last_x);
129    for (item = 0; item < dlg->item_no; ++item) {
130	chtype attr = A_NORMAL;
131
132	y = item + MARGIN + 1;
133	if (y > bm)
134	    break;
135
136	status = status_string(dlg->list[item].text, &freeMe);
137	if (status == 0 || *status == 0)
138	    continue;
139
140	(void) wmove(win, y, 2 * MARGIN);
141	dlg_print_text(win, dlg->list[item].name, lm, &attr);
142
143	(void) wmove(win, y, lm);
144	(void) waddch(win, '[');
145	(void) wmove(win, y, lm + (cells - (int) strlen(status)) / 2);
146	if (freeMe) {
147	    (void) wmove(win, y, lm + 1);
148	    (void) wattrset(win, title_attr);
149	    for (j = 0; j < cells; j++)
150		(void) waddch(win, ' ');
151
152	    (void) wmove(win, y, lm + (cells - (int) strlen(status)) / 2);
153	    (void) waddstr(win, status);
154
155	    if ((title_attr & A_REVERSE) != 0) {
156		wattroff(win, A_REVERSE);
157	    } else {
158		(void) wattrset(win, A_REVERSE);
159	    }
160	    (void) wmove(win, y, lm + 1);
161
162	    if (sscanf(status, "%f%%", &percent) != 1)
163		percent = 0.0;
164	    xxx = (int) ((cells * (percent + 0.5)) / 100.0);
165	    for (j = 0; j < xxx; j++) {
166		chtype ch1 = winch(win);
167		if (title_attr & A_REVERSE) {
168		    ch1 &= ~A_REVERSE;
169		}
170		(void) waddch(win, ch1);
171	    }
172	    free(freeMe);
173
174	} else {
175	    (void) wmove(win, y, lm + (cells - (int) strlen(status)) / 2);
176	    (void) waddstr(win, status);
177	}
178	(void) wmove(win, y, limit_x - 3);
179	(void) waddch(win, ']');
180	(void) wnoutrefresh(win);
181    }
182    if (win != 0)
183	wmove(win, last_y, last_x);
184}
185
186static void
187mydraw_mixed_box(WINDOW *win, int y, int x, int height, int width,
188		 chtype boxchar, chtype borderchar)
189{
190    dlg_draw_box(win, y, x, height, width, boxchar, borderchar);
191    {
192	chtype attr = A_NORMAL;
193	const char *message = _("Overall Progress");
194	chtype save2 = dlg_get_attrs(win);
195	(void) wattrset(win, title_attr);
196	(void) wmove(win, y, x + 2);
197	dlg_print_text(win, message, width, &attr);
198	(void) wattrset(win, save2);
199    }
200}
201
202static char *
203clean_copy(const char *string)
204{
205    char *result = dlg_strclone(string);
206
207    dlg_trim_string(result);
208    dlg_tab_correct_str(result);
209    return result;
210}
211
212/*
213 * Update mixed-gauge dialog (may be from pipe, may be via direct calls).
214 */
215static void
216dlg_update_mixedgauge(DIALOG_MIXEDGAUGE * dlg, int percent)
217{
218    int i, x;
219
220    /*
221     * Clear the area for the progress bar by filling it with spaces
222     * in the title-attribute, and write the percentage with that
223     * attribute.
224     */
225    (void) wmove(dlg->dialog, dlg->height - 3, 4);
226    (void) wattrset(dlg->dialog, gauge_attr);
227
228    for (i = 0; i < (dlg->width - 2 * (3 + MARGIN)); i++)
229	(void) waddch(dlg->dialog, ' ');
230
231    (void) wmove(dlg->dialog, dlg->height - 3, (dlg->width / 2) - 2);
232    (void) wprintw(dlg->dialog, "%3d%%", percent);
233
234    /*
235     * Now draw a bar in reverse, relative to the background.
236     * The window attribute was useful for painting the background,
237     * but requires some tweaks to reverse it.
238     */
239    x = (percent * (dlg->width - 2 * (3 + MARGIN))) / 100;
240    if ((title_attr & A_REVERSE) != 0) {
241	wattroff(dlg->dialog, A_REVERSE);
242    } else {
243	(void) wattrset(dlg->dialog, A_REVERSE);
244    }
245    (void) wmove(dlg->dialog, dlg->height - 3, 4);
246    for (i = 0; i < x; i++) {
247	chtype ch = winch(dlg->dialog);
248	if (title_attr & A_REVERSE) {
249	    ch &= ~A_REVERSE;
250	}
251	(void) waddch(dlg->dialog, ch);
252    }
253    myprint_status(dlg);
254    dlg_trace_win(dlg->dialog);
255}
256
257/*
258 * Setup dialog.
259 */
260static void
261dlg_begin_mixedgauge(DIALOG_MIXEDGAUGE * dlg,
262		     int *began,
263		     const char *aTitle,
264		     const char *aPrompt,
265		     int aHeight,
266		     int aWidth,
267		     int aItemNo,
268		     char **items)
269{
270    int n, y, x;
271
272    if (!*began) {
273	curs_set(0);
274
275	memset(dlg, 0, sizeof(*dlg));
276	dlg->title = aTitle;
277	dlg->prompt = clean_copy(aPrompt);
278	dlg->height = dlg->old_height = aHeight;
279	dlg->width = dlg->old_width = aWidth;
280	dlg->item_no = aItemNo;
281
282	dlg->list = dlg_calloc(DIALOG_LISTITEM, (size_t) aItemNo);
283	assert_ptr(dlg->list, "dialog_mixedgauge");
284
285	dlg->len_name = 0;
286	dlg->len_text = 15;
287
288	for (n = 0; n < aItemNo; ++n) {
289	    int thisWidth = (int) strlen(ItemName(n));
290	    if (dlg->len_name < thisWidth)
291		dlg->len_name = thisWidth;
292	    dlg->list[n].name = ItemName(n);
293	    dlg->list[n].text = ItemText(n);
294	}
295
296	dlg->min_height = MIN_HIGH + aItemNo;
297	dlg->min_width = MIN_WIDE + dlg->len_name + GUTTER + dlg->len_text;
298
299	if (dlg->prompt != 0 && *(dlg->prompt) != 0)
300	    dlg->min_height += (2 * MARGIN);
301#ifdef KEY_RESIZE
302	nodelay(stdscr, TRUE);
303#endif
304    }
305#ifdef KEY_RESIZE
306    else {
307	dlg_del_window(dlg->dialog);
308	dlg->height = dlg->old_height;
309	dlg->width = dlg->old_width;
310    }
311#endif
312
313    dlg_auto_size(dlg->title, dlg->prompt,
314		  &(dlg->height),
315		  &(dlg->width),
316		  dlg->min_height,
317		  dlg->min_width);
318    dlg_print_size(dlg->height, dlg->width);
319    dlg_ctl_size(dlg->height, dlg->width);
320
321    /* center dialog box on screen */
322    x = dlg_box_x_ordinate(dlg->width);
323    y = dlg_box_y_ordinate(dlg->height);
324
325    dlg->dialog = dlg_new_window(dlg->height, dlg->width, y, x);
326
327    (void) werase(dlg->dialog);
328    dlg_draw_box2(dlg->dialog,
329		  0, 0,
330		  dlg->height,
331		  dlg->width,
332		  dialog_attr, border_attr, border2_attr);
333
334    dlg_draw_title(dlg->dialog, dlg->title);
335    dlg_draw_helpline(dlg->dialog, FALSE);
336
337    if ((dlg->prompt != 0 && *(dlg->prompt) != 0)
338	&& wmove(dlg->dialog, dlg->item_no, 0) != ERR) {
339	dlg->caption = dlg_sub_window(dlg->dialog,
340				      dlg->height - dlg->item_no - (2 * MARGIN),
341				      dlg->width,
342				      y + dlg->item_no + (2 * MARGIN),
343				      x);
344	(void) wattrset(dlg->caption, dialog_attr);
345	dlg_print_autowrap(dlg->caption, dlg->prompt, dlg->height, dlg->width);
346    }
347
348    mydraw_mixed_box(dlg->dialog,
349		     dlg->height - 4,
350		     2 + MARGIN,
351		     2 + MARGIN,
352		     dlg->width - 2 * (2 + MARGIN),
353		     dialog_attr,
354		     border_attr);
355
356    *began += 1;
357}
358
359/*
360 * Discard the mixed-gauge dialog.
361 */
362static int
363dlg_finish_mixedgauge(DIALOG_MIXEDGAUGE * dlg, int status)
364{
365    (void) wrefresh(dlg->dialog);
366#ifdef KEY_RESIZE
367    nodelay(stdscr, FALSE);
368#endif
369    curs_set(1);
370    dlg_del_window(dlg->dialog);
371    return status;
372}
373
374/*
375 * Setup dialog, read mixed-gauge data from pipe.
376 */
377int
378dialog_mixedgauge(const char *title,
379		  const char *cprompt,
380		  int height,
381		  int width,
382		  int percent,
383		  int item_no,
384		  char **items)
385{
386    DIALOG_MIXEDGAUGE dlg;
387    int began = 0;
388
389    dlg_begin_mixedgauge(&dlg, &began, title, cprompt, height,
390			 width, item_no, items);
391
392    dlg_update_mixedgauge(&dlg, percent);
393
394    return dlg_finish_mixedgauge(&dlg, DLG_EXIT_OK);
395}
396