yesno.c revision 4658
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
424658Sache  if (width > COLS)
434658Sache	width = COLS;
444658Sache  if (height > LINES)
454658Sache	height = LINES;
463515Sache  /* center dialog box on screen */
473515Sache  x = (COLS - width)/2;
483515Sache  y = (LINES - height)/2;
493515Sache
503515Sache#ifdef HAVE_NCURSES
513515Sache  if (use_shadow)
523515Sache    draw_shadow(stdscr, y, x, height, width);
533515Sache#endif
543515Sache  dialog = newwin(height, width, y, x);
554024Sache  if (dialog == NULL) {
564024Sache    endwin();
574024Sache    fprintf(stderr, "\nnewwin(%d,%d,%d,%d) failed, maybe wrong dims\n", height,width,y,x);
584024Sache    exit(1);
594024Sache  }
603515Sache  keypad(dialog, TRUE);
613515Sache
623515Sache  draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);
633515Sache  wattrset(dialog, border_attr);
643515Sache  wmove(dialog, height-3, 0);
653515Sache  waddch(dialog, ACS_LTEE);
663515Sache  for (i = 0; i < width-2; i++)
673515Sache    waddch(dialog, ACS_HLINE);
683515Sache  wattrset(dialog, dialog_attr);
693515Sache  waddch(dialog, ACS_RTEE);
703515Sache  wmove(dialog, height-2, 1);
713515Sache  for (i = 0; i < width-2; i++)
723515Sache    waddch(dialog, ' ');
733515Sache
743515Sache  if (title != NULL) {
753515Sache    wattrset(dialog, title_attr);
763515Sache    wmove(dialog, 0, (width - strlen(title))/2 - 1);
773515Sache    waddch(dialog, ' ');
783515Sache    waddstr(dialog, title);
793515Sache    waddch(dialog, ' ');
803515Sache  }
813515Sache  wattrset(dialog, dialog_attr);
823950Sache  wmove(dialog, 1, 2);
833950Sache  print_autowrap(dialog, prompt, height-1, width-2, width, 1, 2, TRUE, FALSE);
843515Sache
853515Sache  x = width/2-10;
863515Sache  y = height-2;
873515Sache  print_button(dialog, "  No  ", y, x+13, FALSE);
883515Sache  print_button(dialog, " Yes ", y, x, TRUE);
893515Sache  wrefresh(dialog);
903515Sache
913515Sache  while (key != ESC) {
923515Sache    key = wgetch(dialog);
933515Sache    switch (key) {
943515Sache      case 'Y':
953515Sache      case 'y':
963515Sache        delwin(dialog);
973515Sache        return 0;
983515Sache      case 'N':
993515Sache      case 'n':
1003515Sache        delwin(dialog);
1013515Sache        return 1;
1023515Sache      case KEY_BTAB:
1033515Sache      case TAB:
1043515Sache      case KEY_UP:
1053515Sache      case KEY_DOWN:
1063515Sache      case KEY_LEFT:
1073515Sache      case KEY_RIGHT:
1083515Sache        if (!button) {
1093515Sache          button = 1;    /* Indicates "No" button is selected */
1103515Sache          print_button(dialog, " Yes ", y, x, FALSE);
1113515Sache          print_button(dialog, "  No  ", y, x+13, TRUE);
1123515Sache        }
1133515Sache        else {
1143515Sache          button = 0;    /* Indicates "Yes" button is selected */
1153515Sache          print_button(dialog, "  No  ", y, x+13, FALSE);
1163515Sache          print_button(dialog, " Yes ", y, x, TRUE);
1173515Sache        }
1183515Sache        wrefresh(dialog);
1193515Sache        break;
1203515Sache      case ' ':
1213950Sache      case '\r':
1223515Sache      case '\n':
1233515Sache        delwin(dialog);
1243515Sache        return button;
1253515Sache      case ESC:
1263515Sache        break;
1273515Sache    }
1283515Sache  }
1293515Sache
1303515Sache  delwin(dialog);
1313515Sache  return -1;    /* ESC pressed */
1323515Sache}
1333515Sache/* End of dialog_yesno() */
134