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
21114603Sobrien#include <sys/cdefs.h>
22114603Sobrien__FBSDID("$FreeBSD$");
233515Sache
243740Sache#include <dialog.h>
253515Sache#include "dialog.priv.h"
263515Sache
2770004Sjkh/* Actual work function */
2870004Sjkhstatic int dialog_yesno_proc(unsigned char *title, unsigned char *prompt,
2970004Sjkh			     int height, int width, int yesdefault);
303515Sache
313515Sache/*
323515Sache * Display a dialog box with two buttons - Yes and No
333515Sache */
3470004Sjkhint
3570004Sjkhdialog_yesno(unsigned char *title, unsigned char *prompt, int height, int width)
363515Sache{
3770004Sjkh  return dialog_yesno_proc(title, prompt, height, width, TRUE);
3870004Sjkh}
3970004Sjkh
4070004Sjkh/*
4170004Sjkh * Display a dialog box with two buttons - No and Yes
4270004Sjkh */
4370004Sjkhint
4470004Sjkhdialog_noyes(unsigned char *title, unsigned char *prompt, int height, int width)
4570004Sjkh{
4670004Sjkh  return dialog_yesno_proc(title, prompt, height, width, FALSE);
4770004Sjkh}
4870004Sjkh
4970004Sjkhstatic int
5070004Sjkhdialog_yesno_proc(unsigned char *title, unsigned char *prompt, int height, int width, int yesdefault)
5170004Sjkh{
5272986Sjkh  int i, j, x, y, key, button;
533515Sache  WINDOW *dialog;
547959Sache  char *tmphlp;
553515Sache
567959Sache  /* disable helpline */
577959Sache  tmphlp = get_helpline();
587959Sache  use_helpline(NULL);
597959Sache
604527Sache  if (height < 0)
614527Sache	height = strheight(prompt)+4;
624527Sache  if (width < 0) {
634527Sache	i = strwidth(prompt);
646035Sache	j = ((title != NULL) ? strwidth(title) : 0);
654527Sache	width = MAX(i,j)+4;
664527Sache  }
676035Sache  width = MAX(width,23);
684527Sache
694658Sache  if (width > COLS)
704658Sache	width = COLS;
714658Sache  if (height > LINES)
724658Sache	height = LINES;
733515Sache  /* center dialog box on screen */
7413135Sjkh  x = DialogX ? DialogX : (COLS - width)/2;
7513135Sjkh  y = DialogY ? DialogY : (LINES - height)/2;
768858Srgrimes
773515Sache#ifdef HAVE_NCURSES
783515Sache  if (use_shadow)
793515Sache    draw_shadow(stdscr, y, x, height, width);
803515Sache#endif
813515Sache  dialog = newwin(height, width, y, x);
824024Sache  if (dialog == NULL) {
834024Sache    endwin();
844024Sache    fprintf(stderr, "\nnewwin(%d,%d,%d,%d) failed, maybe wrong dims\n", height,width,y,x);
854024Sache    exit(1);
864024Sache  }
873515Sache  keypad(dialog, TRUE);
883515Sache
893515Sache  draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);
903515Sache  wattrset(dialog, border_attr);
913515Sache  wmove(dialog, height-3, 0);
923515Sache  waddch(dialog, ACS_LTEE);
933515Sache  for (i = 0; i < width-2; i++)
943515Sache    waddch(dialog, ACS_HLINE);
953515Sache  wattrset(dialog, dialog_attr);
963515Sache  waddch(dialog, ACS_RTEE);
973515Sache  wmove(dialog, height-2, 1);
983515Sache  for (i = 0; i < width-2; i++)
993515Sache    waddch(dialog, ' ');
1003515Sache
1013515Sache  if (title != NULL) {
1023515Sache    wattrset(dialog, title_attr);
1033515Sache    wmove(dialog, 0, (width - strlen(title))/2 - 1);
1043515Sache    waddch(dialog, ' ');
1053515Sache    waddstr(dialog, title);
1063515Sache    waddch(dialog, ' ');
1073515Sache  }
1083515Sache  wattrset(dialog, dialog_attr);
1093950Sache  wmove(dialog, 1, 2);
1103950Sache  print_autowrap(dialog, prompt, height-1, width-2, width, 1, 2, TRUE, FALSE);
1113515Sache
1126458Sache  display_helpline(dialog, height-1, width);
1136458Sache
1143515Sache  x = width/2-10;
1153515Sache  y = height-2;
1163515Sache
11772986Sjkh  /* preset button 0 or 1 for YES or NO as the default */
11872986Sjkh  key = 0;
11972986Sjkh  button = !yesdefault;
1203515Sache  while (key != ESC) {
12172986Sjkh    print_button(dialog, "  No  ", y, x+13, button);
12272986Sjkh    print_button(dialog, " Yes " , y, x, !button);
12379843Seric    if (button)
12479843Seric	wmove(dialog, y, x+16);
12579843Seric    else
12679843Seric	wmove(dialog, y, x+2);
12772986Sjkh    wrefresh(dialog);
12872986Sjkh
1293515Sache    key = wgetch(dialog);
1303515Sache    switch (key) {
1313515Sache      case 'Y':
1323515Sache      case 'y':
1333515Sache        delwin(dialog);
1347959Sache	restore_helpline(tmphlp);
1353515Sache        return 0;
1363515Sache      case 'N':
1373515Sache      case 'n':
1383515Sache        delwin(dialog);
1397959Sache	restore_helpline(tmphlp);
1403515Sache        return 1;
1413515Sache      case KEY_BTAB:
1423515Sache      case TAB:
1433515Sache      case KEY_UP:
1443515Sache      case KEY_DOWN:
1453515Sache      case KEY_LEFT:
1463515Sache      case KEY_RIGHT:
14772986Sjkh        button = !button;
14872986Sjkh        /* redrawn at the loop's entry */
1493515Sache        break;
1503515Sache      case ' ':
1513950Sache      case '\r':
1523515Sache      case '\n':
1533515Sache        delwin(dialog);
1547959Sache	restore_helpline(tmphlp);
15572986Sjkh        return button;
1563515Sache      case ESC:
1573515Sache        break;
1586458Sache    case KEY_F(1):
1596458Sache    case '?':
1606458Sache	display_helpfile();
1616458Sache	break;
1623515Sache    }
1633515Sache  }
1643515Sache
1653515Sache  delwin(dialog);
1667959Sache  restore_helpline(tmphlp);
1673515Sache  return -1;    /* ESC pressed */
1683515Sache}
1693515Sache/* End of dialog_yesno() */
170