yesno.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. #include "dialog.h"
  22. /*
  23. * Display termination buttons
  24. */
  25. static void print_buttons(WINDOW * dialog, int height, int width, int selected)
  26. {
  27. int x = width / 2 - 10;
  28. int y = height - 2;
  29. print_button(dialog, " Yes ", y, x, selected == 0);
  30. print_button(dialog, " No ", y, x + 13, selected == 1);
  31. wmove(dialog, y, x + 1 + 13 * selected);
  32. wrefresh(dialog);
  33. }
  34. /*
  35. * Display a dialog box with two buttons - Yes and No
  36. */
  37. int dialog_yesno(const char *title, const char *prompt, int height, int width)
  38. {
  39. int i, x, y, key = 0, button = 0;
  40. WINDOW *dialog;
  41. /* center dialog box on screen */
  42. x = (COLS - width) / 2;
  43. y = (LINES - height) / 2;
  44. draw_shadow(stdscr, y, x, height, width);
  45. dialog = newwin(height, width, y, x);
  46. keypad(dialog, TRUE);
  47. draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);
  48. wattrset(dialog, border_attr);
  49. mvwaddch(dialog, height - 3, 0, ACS_LTEE);
  50. for (i = 0; i < width - 2; i++)
  51. waddch(dialog, ACS_HLINE);
  52. wattrset(dialog, dialog_attr);
  53. waddch(dialog, ACS_RTEE);
  54. print_title(dialog, title, width);
  55. wattrset(dialog, dialog_attr);
  56. print_autowrap(dialog, prompt, width - 2, 1, 3);
  57. print_buttons(dialog, height, width, 0);
  58. while (key != ESC) {
  59. key = wgetch(dialog);
  60. switch (key) {
  61. case 'Y':
  62. case 'y':
  63. delwin(dialog);
  64. return 0;
  65. case 'N':
  66. case 'n':
  67. delwin(dialog);
  68. return 1;
  69. case TAB:
  70. case KEY_LEFT:
  71. case KEY_RIGHT:
  72. button = ((key == KEY_LEFT ? --button : ++button) < 0) ? 1 : (button > 1 ? 0 : button);
  73. print_buttons(dialog, height, width, button);
  74. wrefresh(dialog);
  75. break;
  76. case ' ':
  77. case '\n':
  78. delwin(dialog);
  79. return button;
  80. case ESC:
  81. break;
  82. }
  83. }
  84. delwin(dialog);
  85. return -1; /* ESC pressed */
  86. }