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