yesno.c revision 4527
13515Sache/*
23515Sache *  yesno.c -- implements the yes/no box
33515Sache *
43515Sache *  AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
53515Sache *
63515Sache *  This program is free software; you can redistribute it and/or
73515Sache *  modify it under the terms of the GNU General Public License
83515Sache *  as published by the Free Software Foundation; either version 2
93515Sache *  of the License, or (at your option) any later version.
103515Sache *
113515Sache *  This program is distributed in the hope that it will be useful,
123515Sache *  but WITHOUT ANY WARRANTY; without even the implied warranty of
133515Sache *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
143515Sache *  GNU General Public License for more details.
153515Sache *
163515Sache *  You should have received a copy of the GNU General Public License
173515Sache *  along with this program; if not, write to the Free Software
183515Sache *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
193515Sache */
203515Sache
213515Sache
223740Sache#include <dialog.h>
233515Sache#include "dialog.priv.h"
243515Sache
253515Sache
263515Sache/*
273515Sache * Display a dialog box with two buttons - Yes and No
283515Sache */
293515Sacheint dialog_yesno(unsigned char *title, unsigned char * prompt, int height, int width)
303515Sache{
314527Sache  int i, j, x, y, key = 0, button = 0;
323515Sache  WINDOW *dialog;
333515Sache
344527Sache  if (height < 0)
354527Sache	height = strheight(prompt)+4;
364527Sache  if (width < 0) {
374527Sache	i = strwidth(prompt);
384527Sache	j = strwidth(title);
394527Sache	width = MAX(i,j)+4;
404527Sache  }
414527Sache
423515Sache  /* center dialog box on screen */
433515Sache  x = (COLS - width)/2;
443515Sache  y = (LINES - height)/2;
453515Sache
463515Sache#ifdef HAVE_NCURSES
473515Sache  if (use_shadow)
483515Sache    draw_shadow(stdscr, y, x, height, width);
493515Sache#endif
503515Sache  dialog = newwin(height, width, y, x);
514024Sache  if (dialog == NULL) {
524024Sache    endwin();
534024Sache    fprintf(stderr, "\nnewwin(%d,%d,%d,%d) failed, maybe wrong dims\n", height,width,y,x);
544024Sache    exit(1);
554024Sache  }
563515Sache  keypad(dialog, TRUE);
573515Sache
583515Sache  draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);
593515Sache  wattrset(dialog, border_attr);
603515Sache  wmove(dialog, height-3, 0);
613515Sache  waddch(dialog, ACS_LTEE);
623515Sache  for (i = 0; i < width-2; i++)
633515Sache    waddch(dialog, ACS_HLINE);
643515Sache  wattrset(dialog, dialog_attr);
653515Sache  waddch(dialog, ACS_RTEE);
663515Sache  wmove(dialog, height-2, 1);
673515Sache  for (i = 0; i < width-2; i++)
683515Sache    waddch(dialog, ' ');
693515Sache
703515Sache  if (title != NULL) {
713515Sache    wattrset(dialog, title_attr);
723515Sache    wmove(dialog, 0, (width - strlen(title))/2 - 1);
733515Sache    waddch(dialog, ' ');
743515Sache    waddstr(dialog, title);
753515Sache    waddch(dialog, ' ');
763515Sache  }
773515Sache  wattrset(dialog, dialog_attr);
783950Sache  wmove(dialog, 1, 2);
793950Sache  print_autowrap(dialog, prompt, height-1, width-2, width, 1, 2, TRUE, FALSE);
803515Sache
813515Sache  x = width/2-10;
823515Sache  y = height-2;
833515Sache  print_button(dialog, "  No  ", y, x+13, FALSE);
843515Sache  print_button(dialog, " Yes ", y, x, TRUE);
853515Sache  wrefresh(dialog);
863515Sache
873515Sache  while (key != ESC) {
883515Sache    key = wgetch(dialog);
893515Sache    switch (key) {
903515Sache      case 'Y':
913515Sache      case 'y':
923515Sache        delwin(dialog);
933515Sache        return 0;
943515Sache      case 'N':
953515Sache      case 'n':
963515Sache        delwin(dialog);
973515Sache        return 1;
983515Sache      case KEY_BTAB:
993515Sache      case TAB:
1003515Sache      case KEY_UP:
1013515Sache      case KEY_DOWN:
1023515Sache      case KEY_LEFT:
1033515Sache      case KEY_RIGHT:
1043515Sache        if (!button) {
1053515Sache          button = 1;    /* Indicates "No" button is selected */
1063515Sache          print_button(dialog, " Yes ", y, x, FALSE);
1073515Sache          print_button(dialog, "  No  ", y, x+13, TRUE);
1083515Sache        }
1093515Sache        else {
1103515Sache          button = 0;    /* Indicates "Yes" button is selected */
1113515Sache          print_button(dialog, "  No  ", y, x+13, FALSE);
1123515Sache          print_button(dialog, " Yes ", y, x, TRUE);
1133515Sache        }
1143515Sache        wrefresh(dialog);
1153515Sache        break;
1163515Sache      case ' ':
1173950Sache      case '\r':
1183515Sache      case '\n':
1193515Sache        delwin(dialog);
1203515Sache        return button;
1213515Sache      case ESC:
1223515Sache        break;
1233515Sache    }
1243515Sache  }
1253515Sache
1263515Sache  delwin(dialog);
1273515Sache  return -1;    /* ESC pressed */
1283515Sache}
1293515Sache/* End of dialog_yesno() */
130