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