yesno.c 3.0 KB

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