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