yesno.c 2.8 KB

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