]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/dialog/progressbox.c
Merge tnftp-20100108 from the vendor branch into head.
[FreeBSD/FreeBSD.git] / contrib / dialog / progressbox.c
1 /*
2  *  $Id: progressbox.c,v 1.11 2011/03/02 01:10:08 tom Exp $
3  *
4  *  progressbox.c -- implements the progress box
5  *
6  *  Copyright 2005      Valery Reznic
7  *  Copyright 2006-2011 Thomas E. Dickey
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU Lesser General Public License as
11  *  published by the Free Software Foundation; either version 2.1 of the
12  *  License, or (at your option) any later version.
13  *
14  *  This program is distributed in the hope that it will be useful, but
15  *  WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  *  Lesser General Public License for more details.
18  *
19  *  You should have received a copy of the GNU Lesser General Public
20  *  License along with this program; if not, write to
21  *      Free Software Foundation, Inc.
22  *      51 Franklin St., Fifth Floor
23  *      Boston, MA 02110, USA.
24  */
25
26 #include <dialog.h>
27 #include <dlg_keys.h>
28
29 #define MIN_HIGH (4)
30 #define MIN_WIDE (10 + 2 * (2 + MARGIN))
31
32 typedef struct {
33     DIALOG_CALLBACK obj;
34     WINDOW *text;
35     char line[MAX_LEN + 1];
36     int is_eof;
37 } MY_OBJ;
38
39 /*
40  * Return current line of text.
41  */
42 static char *
43 get_line(MY_OBJ * obj)
44 {
45     FILE *fp = obj->obj.input;
46     int col = 0;
47     int j, tmpint, ch;
48
49     while (1) {
50         if ((ch = getc(fp)) == EOF) {
51             obj->is_eof = 1;
52             if (col) {
53                 break;
54             } else {
55                 return NULL;
56             }
57         }
58         if (ch == '\n')
59             break;
60         if (ch == '\r')
61             break;
62         if ((ch == TAB) && (dialog_vars.tab_correct)) {
63             tmpint = dialog_state.tab_len
64                 - (col % dialog_state.tab_len);
65             for (j = 0; j < tmpint; j++) {
66                 if (col < MAX_LEN)
67                     obj->line[col] = ' ';
68                 ++col;
69             }
70         } else {
71             obj->line[col] = (char) ch;
72             ++col;
73         }
74         if (col >= MAX_LEN)
75             break;
76     }
77
78     obj->line[col] = '\0';
79
80     return obj->line;
81 }
82
83 /*
84  * Print a new line of text.
85  */
86 static void
87 print_line(MY_OBJ * obj, WINDOW *win, int row, int width)
88 {
89     int i, y, x;
90     char *line = obj->line;
91
92     (void) wmove(win, row, 0);  /* move cursor to correct line */
93     (void) waddch(win, ' ');
94 #ifdef NCURSES_VERSION
95     (void) waddnstr(win, line, MIN((int) strlen(line), width - 2));
96 #else
97     line[MIN((int) strlen(line), width - 2)] = '\0';
98     waddstr(win, line);
99 #endif
100
101     getyx(win, y, x);
102     /* Clear 'residue' of previous line */
103     for (i = 0; i < width - x; i++)
104         (void) waddch(win, ' ');
105 }
106
107 static int
108 pause_for_ok(WINDOW *dialog, int height, int width)
109 {
110     /* *INDENT-OFF* */
111     static DLG_KEYS_BINDING binding[] = {
112         ENTERKEY_BINDINGS,
113         DLG_KEYS_DATA( DLGK_ENTER,      ' ' ),
114         END_KEYS_BINDING
115     };
116     /* *INDENT-ON* */
117
118     int button = 0;
119     int key = 0, fkey;
120     int result = DLG_EXIT_UNKNOWN;
121     const char **buttons = dlg_ok_label();
122     int check;
123
124     dlg_register_window(dialog, "progressbox", binding);
125     dlg_register_buttons(dialog, "progressbox", buttons);
126
127     dlg_draw_bottom_box(dialog);
128     mouse_mkbutton(height - 2, width / 2 - 4, 6, '\n');
129     dlg_draw_buttons(dialog, height - 2, 0, buttons, button, FALSE, width);
130
131     while (result == DLG_EXIT_UNKNOWN) {
132         key = dlg_mouse_wgetch(dialog, &fkey);
133         if (dlg_result_key(key, fkey, &result))
134             break;
135
136         if (!fkey && (check = dlg_char_to_button(key, buttons)) >= 0) {
137             result = check ? DLG_EXIT_HELP : DLG_EXIT_OK;
138             break;
139         }
140
141         if (fkey) {
142             switch (key) {
143             case DLGK_ENTER:
144                 result = button ? DLG_EXIT_HELP : DLG_EXIT_OK;
145                 break;
146             case DLGK_MOUSE(0):
147                 result = DLG_EXIT_OK;
148                 break;
149             case DLGK_MOUSE(1):
150                 result = DLG_EXIT_HELP;
151                 break;
152             default:
153                 beep();
154                 break;
155             }
156         } else {
157             beep();
158         }
159     }
160     dlg_unregister_window(dialog);
161     return result;
162 }
163
164 int
165 dlg_progressbox(const char *title,
166                 const char *cprompt,
167                 int height,
168                 int width,
169                 int pauseopt,
170                 FILE *fp)
171 {
172     int i;
173     int x, y, thigh;
174     WINDOW *dialog, *text;
175     MY_OBJ *obj;
176     char *prompt = dlg_strclone(cprompt);
177     int result;
178
179     dlg_tab_correct_str(prompt);
180     dlg_auto_size(title, prompt, &height, &width, MIN_HIGH, MIN_WIDE);
181     dlg_print_size(height, width);
182     dlg_ctl_size(height, width);
183
184     x = dlg_box_x_ordinate(width);
185     y = dlg_box_y_ordinate(height);
186     thigh = height - (2 * MARGIN);
187
188     dialog = dlg_new_window(height, width, y, x);
189
190     dlg_draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);
191     dlg_draw_title(dialog, title);
192
193     if (*prompt != '\0') {
194         int y2, x2;
195
196         wattrset(dialog, dialog_attr);
197         dlg_print_autowrap(dialog, prompt, height, width);
198         getyx(dialog, y2, x2);
199         ++y2;
200         wmove(dialog, y2, MARGIN);
201         for (i = 0; i < getmaxx(dialog) - 2 * MARGIN; i++)
202             (void) waddch(dialog, dlg_boxchar(ACS_HLINE));
203         y += y2;
204         thigh -= y2;
205     }
206
207     /* Create window for text region, used for scrolling text */
208     text = dlg_sub_window(dialog,
209                           thigh,
210                           width - (2 * MARGIN),
211                           y + MARGIN,
212                           x + MARGIN);
213
214     (void) wrefresh(dialog);
215
216     (void) wmove(dialog, thigh, (MARGIN + 1));
217     (void) wnoutrefresh(dialog);
218
219     obj = dlg_calloc(MY_OBJ, 1);
220     assert_ptr(obj, "dlg_progressbox");
221
222     obj->obj.input = fp;
223     obj->obj.win = dialog;
224     obj->text = text;
225
226     dlg_attr_clear(text, thigh, getmaxx(text), dialog_attr);
227     for (i = 0; get_line(obj); i++) {
228         if (i < thigh) {
229             print_line(obj, text, i, width - (2 * MARGIN));
230         } else {
231             scrollok(text, TRUE);
232             scroll(text);
233             scrollok(text, FALSE);
234             print_line(obj, text, thigh - 1, width - (2 * MARGIN));
235         }
236         (void) wrefresh(text);
237         if (obj->is_eof)
238             break;
239     }
240
241     if (pauseopt) {
242         scrollok(text, TRUE);
243         wscrl(text, 1 + MARGIN);
244         (void) wrefresh(text);
245         result = pause_for_ok(dialog, height, width);
246     } else {
247         wrefresh(dialog);
248         result = DLG_EXIT_OK;
249     }
250
251     dlg_del_window(dialog);
252     free(prompt);
253     free(obj);
254
255     return DLG_EXIT_OK;
256 }
257
258 /*
259  * Display text from a stdin in a scrolling window.
260  */
261 int
262 dialog_progressbox(const char *title, const char *cprompt, int height, int width)
263 {
264     int result;
265     result = dlg_progressbox(title,
266                              cprompt,
267                              height,
268                              width,
269                              FALSE,
270                              dialog_state.pipe_input);
271     dialog_state.pipe_input = 0;
272     return result;
273 }