Back to index

tetex-bin  3.0
yesno.c
Go to the documentation of this file.
00001 /*
00002  *  yesno.c -- implements the yes/no box
00003  *
00004  *  AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
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 /*
00026  * Display a dialog box with two buttons - Yes and No
00027  */
00028 int dialog_yesno(char *title, char * prompt, int height, int width)
00029 {
00030   int i, x, y, key = 0, button = 0;
00031   WINDOW *dialog;
00032 
00033   /* center dialog box on screen */
00034   x = (COLS - width)/2;
00035   y = (LINES - height)/2;
00036   
00037 #ifdef HAVE_NCURSES
00038   if (use_shadow)
00039     draw_shadow(stdscr, y, x, height, width);
00040 #endif
00041   dialog = newwin(height, width, y, x);
00042   keypad(dialog, TRUE);
00043 
00044   draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);
00045   wattrset(dialog, border_attr);
00046   wmove(dialog, height-3, 0);
00047   waddch(dialog, ACS_LTEE);
00048   for (i = 0; i < width-2; i++)
00049     waddch(dialog, ACS_HLINE);
00050   wattrset(dialog, dialog_attr);
00051   waddch(dialog, ACS_RTEE);
00052   wmove(dialog, height-2, 1);
00053   for (i = 0; i < width-2; i++)
00054     waddch(dialog, ' ');
00055 
00056   if (title != NULL) {
00057     wattrset(dialog, title_attr);
00058     wmove(dialog, 0, (width - strlen(title))/2 - 1);
00059     waddch(dialog, ' ');
00060     waddstr(dialog, title);
00061     waddch(dialog, ' ');
00062   }
00063   wattrset(dialog, dialog_attr);
00064   print_autowrap(dialog, prompt, width, 1, 3);
00065 
00066   x = width/2-10;
00067   y = height-2;
00068   print_button(dialog, "  No  ", y, x+13, FALSE);
00069   print_button(dialog, " Yes ", y, x, TRUE);
00070   wrefresh(dialog);
00071 
00072   while (key != ESC) {
00073     key = wgetch(dialog);
00074     switch (key) {
00075       case 'Y':
00076       case 'y':
00077         delwin(dialog);
00078         return 0;
00079       case 'N':
00080       case 'n':
00081         delwin(dialog);
00082         return 1;
00083       case TAB:
00084       case KEY_UP:
00085       case KEY_DOWN:
00086       case KEY_LEFT:
00087       case KEY_RIGHT:
00088         if (!button) {
00089           button = 1;    /* Indicates "No" button is selected */
00090           print_button(dialog, " Yes ", y, x, FALSE);
00091           print_button(dialog, "  No  ", y, x+13, TRUE);
00092         }
00093         else {
00094           button = 0;    /* Indicates "Yes" button is selected */
00095           print_button(dialog, "  No  ", y, x+13, FALSE);
00096           print_button(dialog, " Yes ", y, x, TRUE);
00097         }
00098         wrefresh(dialog);
00099         break;
00100       case ' ':
00101       case '\n':
00102         delwin(dialog);
00103         return button;
00104       case ESC:
00105         break;
00106     }
00107   }
00108 
00109   delwin(dialog);
00110   return -1;    /* ESC pressed */
00111 }
00112 /* End of dialog_yesno() */