util.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #include <newt.h>
  2. #include <signal.h>
  3. #include <stdio.h>
  4. #include <stdbool.h>
  5. #include <string.h>
  6. #include <sys/ttydefaults.h>
  7. #include "../cache.h"
  8. #include "../debug.h"
  9. #include "browser.h"
  10. #include "helpline.h"
  11. #include "util.h"
  12. static void newt_form__set_exit_keys(newtComponent self)
  13. {
  14. newtFormAddHotKey(self, NEWT_KEY_LEFT);
  15. newtFormAddHotKey(self, NEWT_KEY_ESCAPE);
  16. newtFormAddHotKey(self, 'Q');
  17. newtFormAddHotKey(self, 'q');
  18. newtFormAddHotKey(self, CTRL('c'));
  19. }
  20. static newtComponent newt_form__new(void)
  21. {
  22. newtComponent self = newtForm(NULL, NULL, 0);
  23. if (self)
  24. newt_form__set_exit_keys(self);
  25. return self;
  26. }
  27. int ui__popup_menu(int argc, char * const argv[])
  28. {
  29. struct newtExitStruct es;
  30. int i, rc = -1, max_len = 5;
  31. newtComponent listbox, form = newt_form__new();
  32. if (form == NULL)
  33. return -1;
  34. listbox = newtListbox(0, 0, argc, NEWT_FLAG_RETURNEXIT);
  35. if (listbox == NULL)
  36. goto out_destroy_form;
  37. newtFormAddComponent(form, listbox);
  38. for (i = 0; i < argc; ++i) {
  39. int len = strlen(argv[i]);
  40. if (len > max_len)
  41. max_len = len;
  42. if (newtListboxAddEntry(listbox, argv[i], (void *)(long)i))
  43. goto out_destroy_form;
  44. }
  45. newtCenteredWindow(max_len, argc, NULL);
  46. newtFormRun(form, &es);
  47. rc = newtListboxGetCurrent(listbox) - NULL;
  48. if (es.reason == NEWT_EXIT_HOTKEY)
  49. rc = -1;
  50. newtPopWindow();
  51. out_destroy_form:
  52. newtFormDestroy(form);
  53. return rc;
  54. }
  55. int ui__help_window(const char *text)
  56. {
  57. struct newtExitStruct es;
  58. newtComponent tb, form = newt_form__new();
  59. int rc = -1;
  60. int max_len = 0, nr_lines = 0;
  61. const char *t;
  62. if (form == NULL)
  63. return -1;
  64. t = text;
  65. while (1) {
  66. const char *sep = strchr(t, '\n');
  67. int len;
  68. if (sep == NULL)
  69. sep = strchr(t, '\0');
  70. len = sep - t;
  71. if (max_len < len)
  72. max_len = len;
  73. ++nr_lines;
  74. if (*sep == '\0')
  75. break;
  76. t = sep + 1;
  77. }
  78. tb = newtTextbox(0, 0, max_len, nr_lines, 0);
  79. if (tb == NULL)
  80. goto out_destroy_form;
  81. newtTextboxSetText(tb, text);
  82. newtFormAddComponent(form, tb);
  83. newtCenteredWindow(max_len, nr_lines, NULL);
  84. newtFormRun(form, &es);
  85. newtPopWindow();
  86. rc = 0;
  87. out_destroy_form:
  88. newtFormDestroy(form);
  89. return rc;
  90. }
  91. static const char yes[] = "Yes", no[] = "No",
  92. warning_str[] = "Warning!", ok[] = "Ok";
  93. bool ui__dialog_yesno(const char *msg)
  94. {
  95. /* newtWinChoice should really be accepting const char pointers... */
  96. return newtWinChoice(NULL, (char *)yes, (char *)no, (char *)msg) == 1;
  97. }
  98. void ui__warning(const char *format, ...)
  99. {
  100. va_list args;
  101. va_start(args, format);
  102. if (use_browser > 0)
  103. newtWinMessagev((char *)warning_str, (char *)ok,
  104. (char *)format, args);
  105. else
  106. vfprintf(stderr, format, args);
  107. va_end(args);
  108. }