1318436Sngie/* 2318436Sngie * $Id: progressbox.c,v 1.13 2011/06/27 08:18:20 tom Exp $ 3318436Sngie * 4318436Sngie * progressbox.c -- implements the progress box 5318436Sngie * 6318436Sngie * Copyright 2005 Valery Reznic 7318436Sngie * Copyright 2006-2011 Thomas E. Dickey 8318436Sngie * 9318436Sngie * This program is free software; you can redistribute it and/or modify 10318436Sngie * it under the terms of the GNU Lesser General Public License as 11318436Sngie * published by the Free Software Foundation; either version 2.1 of the 12318436Sngie * License, or (at your option) any later version. 13318436Sngie * 14318436Sngie * This program is distributed in the hope that it will be useful, but 15318436Sngie * WITHOUT ANY WARRANTY; without even the implied warranty of 16318436Sngie * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17318436Sngie * Lesser General Public License for more details. 18318436Sngie * 19318436Sngie * You should have received a copy of the GNU Lesser General Public 20318436Sngie * License along with this program; if not, write to 21318436Sngie * Free Software Foundation, Inc. 22318436Sngie * 51 Franklin St., Fifth Floor 23318436Sngie * Boston, MA 02110, USA. 24318436Sngie */ 25318436Sngie 26318436Sngie#include <dialog.h> 27318436Sngie#include <dlg_keys.h> 28318436Sngie 29318436Sngie#define MIN_HIGH (4) 30318436Sngie#define MIN_WIDE (10 + 2 * (2 + MARGIN)) 31318436Sngie 32318436Sngietypedef struct { 33318436Sngie DIALOG_CALLBACK obj; 34318436Sngie WINDOW *text; 35318436Sngie char line[MAX_LEN + 1]; 36318436Sngie int is_eof; 37318436Sngie} MY_OBJ; 38318436Sngie 39318436Sngie/* 40318436Sngie * Return current line of text. 41318436Sngie */ 42318436Sngiestatic char * 43318436Sngieget_line(MY_OBJ * obj) 44318436Sngie{ 45318436Sngie FILE *fp = obj->obj.input; 46318436Sngie int col = 0; 47318436Sngie int j, tmpint, ch; 48318436Sngie 49318436Sngie while (1) { 50318436Sngie if ((ch = getc(fp)) == EOF) { 51318436Sngie obj->is_eof = 1; 52318436Sngie if (col) { 53318436Sngie break; 54318436Sngie } else { 55318436Sngie return NULL; 56318436Sngie } 57318436Sngie } 58318436Sngie if (ch == '\n') 59318436Sngie 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 */ 86static void 87print_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 107static int 108pause_for_ok(WINDOW *dialog, int height, int width) 109{ 110 /* *INDENT-OFF* */ 111 static DLG_KEYS_BINDING binding[] = { 112 HELPKEY_BINDINGS, 113 ENTERKEY_BINDINGS, 114 DLG_KEYS_DATA( DLGK_ENTER, ' ' ), 115 END_KEYS_BINDING 116 }; 117 /* *INDENT-ON* */ 118 119 int button = 0; 120 int key = 0, fkey; 121 int result = DLG_EXIT_UNKNOWN; 122 const char **buttons = dlg_ok_label(); 123 int check; 124 125 dlg_register_window(dialog, "progressbox", binding); 126 dlg_register_buttons(dialog, "progressbox", buttons); 127 128 dlg_draw_bottom_box(dialog); 129 mouse_mkbutton(height - 2, width / 2 - 4, 6, '\n'); 130 dlg_draw_buttons(dialog, height - 2, 0, buttons, button, FALSE, width); 131 132 while (result == DLG_EXIT_UNKNOWN) { 133 key = dlg_mouse_wgetch(dialog, &fkey); 134 if (dlg_result_key(key, fkey, &result)) 135 break; 136 137 if (!fkey && (check = dlg_char_to_button(key, buttons)) >= 0) { 138 result = check ? DLG_EXIT_HELP : DLG_EXIT_OK; 139 break; 140 } 141 142 if (fkey) { 143 switch (key) { 144 case DLGK_ENTER: 145 result = button ? DLG_EXIT_HELP : DLG_EXIT_OK; 146 break; 147 case DLGK_MOUSE(0): 148 result = DLG_EXIT_OK; 149 break; 150 case DLGK_MOUSE(1): 151 result = DLG_EXIT_HELP; 152 break; 153 default: 154 beep(); 155 break; 156 } 157 } else { 158 beep(); 159 } 160 } 161 dlg_unregister_window(dialog); 162 return result; 163} 164 165int 166dlg_progressbox(const char *title, 167 const char *cprompt, 168 int height, 169 int width, 170 int pauseopt, 171 FILE *fp) 172{ 173 int i; 174 int x, y, thigh; 175 WINDOW *dialog, *text; 176 MY_OBJ *obj; 177 char *prompt = dlg_strclone(cprompt); 178 int result; 179 180 dlg_tab_correct_str(prompt); 181 dlg_auto_size(title, prompt, &height, &width, MIN_HIGH, MIN_WIDE); 182 dlg_print_size(height, width); 183 dlg_ctl_size(height, width); 184 185 x = dlg_box_x_ordinate(width); 186 y = dlg_box_y_ordinate(height); 187 thigh = height - (2 * MARGIN); 188 189 dialog = dlg_new_window(height, width, y, x); 190 191 dlg_draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr); 192 dlg_draw_title(dialog, title); 193 dlg_draw_helpline(dialog, FALSE); 194 195 if (*prompt != '\0') { 196 int y2, x2; 197 198 wattrset(dialog, dialog_attr); 199 dlg_print_autowrap(dialog, prompt, height, width); 200 getyx(dialog, y2, x2); 201 ++y2; 202 wmove(dialog, y2, MARGIN); 203 for (i = 0; i < getmaxx(dialog) - 2 * MARGIN; i++) 204 (void) waddch(dialog, dlg_boxchar(ACS_HLINE)); 205 y += y2; 206 thigh -= y2; 207 } 208 209 /* Create window for text region, used for scrolling text */ 210 text = dlg_sub_window(dialog, 211 thigh, 212 width - (2 * MARGIN), 213 y + MARGIN, 214 x + MARGIN); 215 216 (void) wrefresh(dialog); 217 218 (void) wmove(dialog, thigh, (MARGIN + 1)); 219 (void) wnoutrefresh(dialog); 220 221 obj = dlg_calloc(MY_OBJ, 1); 222 assert_ptr(obj, "dlg_progressbox"); 223 224 obj->obj.input = fp; 225 obj->obj.win = dialog; 226 obj->text = text; 227 228 dlg_attr_clear(text, thigh, getmaxx(text), dialog_attr); 229 for (i = 0; get_line(obj); i++) { 230 if (i < thigh) { 231 print_line(obj, text, i, width - (2 * MARGIN)); 232 } else { 233 scrollok(text, TRUE); 234 scroll(text); 235 scrollok(text, FALSE); 236 print_line(obj, text, thigh - 1, width - (2 * MARGIN)); 237 } 238 (void) wrefresh(text); 239 if (obj->is_eof) 240 break; 241 } 242 243 if (pauseopt) { 244 scrollok(text, TRUE); 245 wscrl(text, 1 + MARGIN); 246 (void) wrefresh(text); 247 result = pause_for_ok(dialog, height, width); 248 } else { 249 wrefresh(dialog); 250 result = DLG_EXIT_OK; 251 } 252 253 dlg_del_window(dialog); 254 free(prompt); 255 free(obj); 256 257 return DLG_EXIT_OK; 258} 259 260/* 261 * Display text from a stdin in a scrolling window. 262 */ 263int 264dialog_progressbox(const char *title, const char *cprompt, int height, int width) 265{ 266 int result; 267 result = dlg_progressbox(title, 268 cprompt, 269 height, 270 width, 271 FALSE, 272 dialog_state.pipe_input); 273 dialog_state.pipe_input = 0; 274 return result; 275} 276