yesno.c 2.2 KB

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