root/scripts/lxdialog/yesno.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. print_buttons
  2. dialog_yesno

   1 /*
   2  *  yesno.c -- implements the yes/no box
   3  *
   4  *  ORIGINAL AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
   5  *  MODIFIED FOR LINUX KERNEL CONFIG BY: William Roadcap (roadcap@cfw.com)
   6  *
   7  *  This program is free software; you can redistribute it and/or
   8  *  modify it under the terms of the GNU General Public License
   9  *  as published by the Free Software Foundation; either version 2
  10  *  of the License, or (at your option) any later version.
  11  *
  12  *  This program is distributed in the hope that it will be useful,
  13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15  *  GNU General Public License for more details.
  16  *
  17  *  You should have received a copy of the GNU General Public License
  18  *  along with this program; if not, write to the Free Software
  19  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20  */
  21 
  22 #include "dialog.h"
  23 
  24 /*
  25  * Display termination buttons
  26  */
  27 static void
  28 print_buttons(WINDOW *dialog, int height, int width, int okval, int cancelval)
     /* [previous][next][first][last][top][bottom][index][help] */
  29 {
  30     int x = width / 2 - 10;
  31     int y = height - 2;
  32 
  33     print_button (dialog, " Yes ", y, x, okval);
  34     print_button (dialog, "  No  ", y, x + 13, cancelval);
  35 
  36     wrefresh (dialog);
  37 }
  38 
  39 /*
  40  * Display a dialog box with two buttons - Yes and No
  41  */
  42 int
  43 dialog_yesno (const char *title, const char *prompt, int height, int width)
     /* [previous][next][first][last][top][bottom][index][help] */
  44 {
  45     int i, x, y, key = 0, button = 0;
  46     WINDOW *dialog;
  47 
  48     /* center dialog box on screen */
  49     x = (COLS - width) / 2;
  50     y = (LINES - height) / 2;
  51 
  52     draw_shadow (stdscr, y, x, height, width);
  53 
  54     dialog = newwin (height, width, y, x);
  55     keypad (dialog, TRUE);
  56 
  57     draw_box (dialog, 0, 0, height, width, dialog_attr, border_attr);
  58     wattrset (dialog, border_attr);
  59     mvwaddch (dialog, height-3, 0, ACS_LTEE);
  60     for (i = 0; i < width - 2; i++)
  61         waddch (dialog, ACS_HLINE);
  62     wattrset (dialog, dialog_attr);
  63     waddch (dialog, ACS_RTEE);
  64 
  65     if (title != NULL) {
  66         wattrset (dialog, title_attr);
  67         mvwaddch (dialog, 0, (width - strlen(title))/2 - 1, ' ');
  68         waddstr (dialog, (char *)title);
  69         waddch (dialog, ' ');
  70     }
  71 
  72     wattrset (dialog, dialog_attr);
  73     print_autowrap (dialog, prompt, width - 2, 1, 3);
  74 
  75     print_buttons(dialog, height, width, TRUE, FALSE);
  76 
  77     while (key != ESC) {
  78         key = wgetch (dialog);
  79         switch (key) {
  80         case 'Y':
  81         case 'y':
  82             delwin (dialog);
  83             return 0;
  84         case 'N':
  85         case 'n':
  86             delwin (dialog);
  87             return 1;
  88 
  89         case TAB:
  90         case KEY_UP:
  91         case KEY_DOWN:
  92         case KEY_LEFT:
  93         case KEY_RIGHT:
  94             if (!button) {
  95                 button = 1;     /* "No" button selected */
  96                 print_buttons(dialog, height, width, FALSE, TRUE);
  97             } else {
  98                 button = 0;     /* "Yes" button selected */
  99                 print_buttons(dialog, height, width, TRUE, FALSE);
 100             }
 101             wrefresh (dialog);
 102             break;
 103         case ' ':
 104         case '\n':
 105             delwin (dialog);
 106             return button;
 107         case ESC:
 108             break;
 109         }
 110     }
 111 
 112     delwin (dialog);
 113     return -1;                  /* ESC pressed */
 114 }

/* [previous][next][first][last][top][bottom][index][help] */