util.c 2.7 KB

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