Back to index

tetex-bin  3.0
gauge.c
Go to the documentation of this file.
00001 /*
00002  *  guage.c -- implements the guage dialog
00003  *
00004  *  AUTHOR: Marc Ewing, Red Hat Software
00005  *
00006  *  This program is free software; you can redistribute it and/or
00007  *  modify it under the terms of the GNU General Public License
00008  *  as published by the Free Software Foundation; either version 2
00009  *  of the License, or (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00019  */
00020 
00021 
00022 #include "dialog.h"
00023 
00024 
00025 #define BUFLEN 1024
00026 /*
00027  * Display a guage, or progress meter.  Starts at percent% and
00028  * reads stdin.  If stdin is not XXX, then it is interpreted as
00029  * a percentage, and the display is updated accordingly.  Otherwise
00030  * the next line is the percentage, and subsequent lines up to
00031  * another XXX are used for the new prompt.  Note that the size
00032  * of the window never changes, so the prompt can not get any
00033  * larger than the height and width specified.
00034  */
00035 int dialog_guage(char *title, char *prompt, int height, int width, int percent)
00036 {
00037   int i, x, y;
00038   char buf[BUFLEN];
00039   char prompt_buf[BUFLEN];
00040   WINDOW *dialog;
00041 
00042   /* center dialog box on screen */
00043   x = (COLS - width)/2;
00044   y = (LINES - height)/2;
00045 
00046 #ifdef HAVE_NCURSES
00047   if (use_shadow)
00048     draw_shadow(stdscr, y, x, height, width);
00049 #endif
00050   dialog = newwin(height, width, y, x);
00051   keypad(dialog, TRUE);
00052 
00053   do {
00054       werase(dialog);
00055       draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);
00056 
00057       if (title != NULL) {
00058          wattrset(dialog, title_attr);
00059          wmove(dialog, 0, (width - strlen(title))/2 - 1);
00060          waddch(dialog, ' ');
00061          waddstr(dialog, title);
00062          waddch(dialog, ' ');
00063       }
00064 
00065       wattrset(dialog, dialog_attr);
00066       print_autowrap(dialog, prompt, width-2, 1, 2);
00067 
00068       draw_box(dialog, height - 4, 3, 3, width - 6, dialog_attr, border_attr);
00069 
00070       wmove(dialog, height - 3, 4);
00071       wattrset(dialog, title_attr);
00072       for (i = 0; i < (width - 8); i++) {
00073          waddch(dialog, ' ');
00074       }
00075 
00076       wattrset(dialog, title_attr);
00077       wmove(dialog, height - 3, (width / 2) - 2);
00078       sprintf(buf, "%3d%%", percent);
00079       waddstr(dialog, buf);
00080 
00081       x = (percent * (width - 8)) / 100;
00082       wattrset(dialog, item_selected_attr);
00083       wmove(dialog, height - 3, 4);
00084       for (i = 0; i < x; i++) {
00085          waddch(dialog, winch(dialog));
00086       }
00087   
00088       wrefresh(dialog);
00089 
00090       if (feof(stdin)) {
00091          break;
00092       }
00093       
00094       fgets(buf, BUFLEN, stdin);
00095       if (buf[0] == 'X') {
00096          /* Next line is percentage */
00097          fgets(buf, BUFLEN, stdin);
00098          percent = atoi(buf);
00099 
00100          /* Rest is message text */
00101          prompt_buf[0] = '\0';
00102          while (strncmp(fgets(buf, BUFLEN, stdin), "XXX", 3)) {
00103              strcat(prompt_buf, buf);
00104          }
00105          prompt = prompt_buf;
00106       } else {
00107          percent = atoi(buf);
00108       }
00109   } while (1);
00110   
00111   delwin(dialog);
00112   return(0);
00113 }
00114 /* End of dialog_msgbox() */