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
|
/*
* $Id: progressbox.c,v 1.11 2011/03/02 01:10:08 tom Exp $
*
* progressbox.c -- implements the progress box
*
* Copyright 2005 Valery Reznic
* Copyright 2006-2011 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 as
* published by the Free Software Foundation; either version 2.1 of the
* License, or (at your option) any later version.
*
* 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.
*/
#include <dialog.h>
#include <dlg_keys.h>
#define MIN_HIGH (4)
#define MIN_WIDE (10 + 2 * (2 + MARGIN))
typedef struct {
DIALOG_CALLBACK obj;
WINDOW *text;
char line[MAX_LEN + 1];
int is_eof;
} MY_OBJ;
/*
* Return current line of text.
*/
static char *
get_line(MY_OBJ * obj)
{
FILE *fp = obj->obj.input;
int col = 0;
int j, tmpint, ch;
while (1) {
if ((ch = getc(fp)) == EOF) {
obj->is_eof = 1;
if (col) {
break;
} else {
return NULL;
}
}
if (ch == '\n')
break;
if (ch == '\r')
break;
if ((ch == TAB) && (dialog_vars.tab_correct)) {
tmpint = dialog_state.tab_len
- (col % dialog_state.tab_len);
for (j = 0; j < tmpint; j++) {
if (col < MAX_LEN)
obj->line[col] = ' ';
++col;
}
} else {
obj->line[col] = (char) ch;
++col;
}
if (col >= MAX_LEN)
break;
}
obj->line[col] = '\0';
return obj->line;
}
/*
* Print a new line of text.
*/
static void
print_line(MY_OBJ * obj, WINDOW *win, int row, int width)
{
int i, y, x;
char *line = obj->line;
(void) wmove(win, row, 0); /* move cursor to correct line */
(void) waddch(win, ' ');
#ifdef NCURSES_VERSION
(void) waddnstr(win, line, MIN((int) strlen(line), width - 2));
#else
line[MIN((int) strlen(line), width - 2)] = '\0';
waddstr(win, line);
#endif
getyx(win, y, x);
/* Clear 'residue' of previous line */
for (i = 0; i < width - x; i++)
(void) waddch(win, ' ');
}
static int
pause_for_ok(WINDOW *dialog, int height, int width)
{
/* *INDENT-OFF* */
static DLG_KEYS_BINDING binding[] = {
ENTERKEY_BINDINGS,
DLG_KEYS_DATA( DLGK_ENTER, ' ' ),
END_KEYS_BINDING
};
/* *INDENT-ON* */
int button = 0;
int key = 0, fkey;
int result = DLG_EXIT_UNKNOWN;
const char **buttons = dlg_ok_label();
int check;
dlg_register_window(dialog, "progressbox", binding);
dlg_register_buttons(dialog, "progressbox", buttons);
dlg_draw_bottom_box(dialog);
mouse_mkbutton(height - 2, width / 2 - 4, 6, '\n');
dlg_draw_buttons(dialog, height - 2, 0, buttons, button, FALSE, width);
while (result == DLG_EXIT_UNKNOWN) {
key = dlg_mouse_wgetch(dialog, &fkey);
if (dlg_result_key(key, fkey, &result))
break;
if (!fkey && (check = dlg_char_to_button(key, buttons)) >= 0) {
result = check ? DLG_EXIT_HELP : DLG_EXIT_OK;
break;
}
if (fkey) {
switch (key) {
case DLGK_ENTER:
result = button ? DLG_EXIT_HELP : DLG_EXIT_OK;
break;
case DLGK_MOUSE(0):
result = DLG_EXIT_OK;
break;
case DLGK_MOUSE(1):
result = DLG_EXIT_HELP;
break;
default:
beep();
break;
}
} else {
beep();
}
}
dlg_unregister_window(dialog);
return result;
}
int
dlg_progressbox(const char *title,
const char *cprompt,
int height,
int width,
int pauseopt,
FILE *fp)
{
int i;
int x, y, thigh;
WINDOW *dialog, *text;
MY_OBJ *obj;
char *prompt = dlg_strclone(cprompt);
int result;
dlg_tab_correct_str(prompt);
dlg_auto_size(title, prompt, &height, &width, MIN_HIGH, MIN_WIDE);
dlg_print_size(height, width);
dlg_ctl_size(height, width);
x = dlg_box_x_ordinate(width);
y = dlg_box_y_ordinate(height);
thigh = height - (2 * MARGIN);
dialog = dlg_new_window(height, width, y, x);
dlg_draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);
dlg_draw_title(dialog, title);
if (*prompt != '\0') {
int y2, x2;
wattrset(dialog, dialog_attr);
dlg_print_autowrap(dialog, prompt, height, width);
getyx(dialog, y2, x2);
++y2;
wmove(dialog, y2, MARGIN);
for (i = 0; i < getmaxx(dialog) - 2 * MARGIN; i++)
(void) waddch(dialog, dlg_boxchar(ACS_HLINE));
y += y2;
thigh -= y2;
}
/* Create window for text region, used for scrolling text */
text = dlg_sub_window(dialog,
thigh,
width - (2 * MARGIN),
y + MARGIN,
x + MARGIN);
(void) wrefresh(dialog);
(void) wmove(dialog, thigh, (MARGIN + 1));
(void) wnoutrefresh(dialog);
obj = dlg_calloc(MY_OBJ, 1);
assert_ptr(obj, "dlg_progressbox");
obj->obj.input = fp;
obj->obj.win = dialog;
obj->text = text;
dlg_attr_clear(text, thigh, getmaxx(text), dialog_attr);
for (i = 0; get_line(obj); i++) {
if (i < thigh) {
print_line(obj, text, i, width - (2 * MARGIN));
} else {
scrollok(text, TRUE);
scroll(text);
scrollok(text, FALSE);
print_line(obj, text, thigh - 1, width - (2 * MARGIN));
}
(void) wrefresh(text);
if (obj->is_eof)
break;
}
if (pauseopt) {
scrollok(text, TRUE);
wscrl(text, 1 + MARGIN);
(void) wrefresh(text);
result = pause_for_ok(dialog, height, width);
} else {
wrefresh(dialog);
result = DLG_EXIT_OK;
}
dlg_del_window(dialog);
free(prompt);
free(obj);
return DLG_EXIT_OK;
}
/*
* Display text from a stdin in a scrolling window.
*/
int
dialog_progressbox(const char *title, const char *cprompt, int height, int width)
{
int result;
result = dlg_progressbox(title,
cprompt,
height,
width,
FALSE,
dialog_state.pipe_input);
dialog_state.pipe_input = 0;
return result;
}
|