Arnaldo Carvalho de Melo | f9224c5 | 2010-03-11 20:12:44 -0300 | [diff] [blame] | 1 | #include <newt.h> |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 2 | #include <signal.h> |
| 3 | #include <stdio.h> |
| 4 | #include <stdbool.h> |
| 5 | #include <string.h> |
Arnaldo Carvalho de Melo | 7081e08 | 2010-03-12 10:48:12 -0300 | [diff] [blame] | 6 | #include <sys/ttydefaults.h> |
Arnaldo Carvalho de Melo | f9224c5 | 2010-03-11 20:12:44 -0300 | [diff] [blame] | 7 | |
Arnaldo Carvalho de Melo | 1e6dd07 | 2010-08-10 15:58:50 -0300 | [diff] [blame] | 8 | #include "../cache.h" |
| 9 | #include "../debug.h" |
| 10 | #include "browser.h" |
| 11 | #include "helpline.h" |
Arnaldo Carvalho de Melo | a1ceb741 | 2011-03-01 10:24:43 -0300 | [diff] [blame^] | 12 | #include "ui.h" |
Arnaldo Carvalho de Melo | 1e6dd07 | 2010-08-10 15:58:50 -0300 | [diff] [blame] | 13 | #include "util.h" |
Arnaldo Carvalho de Melo | f9224c5 | 2010-03-11 20:12:44 -0300 | [diff] [blame] | 14 | |
Arnaldo Carvalho de Melo | 7081e08 | 2010-03-12 10:48:12 -0300 | [diff] [blame] | 15 | static void newt_form__set_exit_keys(newtComponent self) |
| 16 | { |
Arnaldo Carvalho de Melo | a308f3a | 2010-05-16 20:29:38 -0300 | [diff] [blame] | 17 | newtFormAddHotKey(self, NEWT_KEY_LEFT); |
Arnaldo Carvalho de Melo | 7081e08 | 2010-03-12 10:48:12 -0300 | [diff] [blame] | 18 | newtFormAddHotKey(self, NEWT_KEY_ESCAPE); |
| 19 | newtFormAddHotKey(self, 'Q'); |
| 20 | newtFormAddHotKey(self, 'q'); |
| 21 | newtFormAddHotKey(self, CTRL('c')); |
| 22 | } |
| 23 | |
Arnaldo Carvalho de Melo | 4c1c952 | 2010-08-12 12:37:51 -0300 | [diff] [blame] | 24 | static newtComponent newt_form__new(void) |
Arnaldo Carvalho de Melo | 7081e08 | 2010-03-12 10:48:12 -0300 | [diff] [blame] | 25 | { |
| 26 | newtComponent self = newtForm(NULL, NULL, 0); |
| 27 | if (self) |
| 28 | newt_form__set_exit_keys(self); |
| 29 | return self; |
| 30 | } |
| 31 | |
Arnaldo Carvalho de Melo | 1e6dd07 | 2010-08-10 15:58:50 -0300 | [diff] [blame] | 32 | int ui__popup_menu(int argc, char * const argv[]) |
Arnaldo Carvalho de Melo | 53c5401 | 2010-03-24 16:40:14 -0300 | [diff] [blame] | 33 | { |
| 34 | struct newtExitStruct es; |
| 35 | int i, rc = -1, max_len = 5; |
| 36 | newtComponent listbox, form = newt_form__new(); |
| 37 | |
| 38 | if (form == NULL) |
| 39 | return -1; |
| 40 | |
| 41 | listbox = newtListbox(0, 0, argc, NEWT_FLAG_RETURNEXIT); |
| 42 | if (listbox == NULL) |
| 43 | goto out_destroy_form; |
| 44 | |
Arnaldo Carvalho de Melo | 7f82645 | 2010-05-10 10:51:25 -0300 | [diff] [blame] | 45 | newtFormAddComponent(form, listbox); |
Arnaldo Carvalho de Melo | 53c5401 | 2010-03-24 16:40:14 -0300 | [diff] [blame] | 46 | |
| 47 | for (i = 0; i < argc; ++i) { |
| 48 | int len = strlen(argv[i]); |
| 49 | if (len > max_len) |
| 50 | max_len = len; |
| 51 | if (newtListboxAddEntry(listbox, argv[i], (void *)(long)i)) |
| 52 | goto out_destroy_form; |
| 53 | } |
| 54 | |
| 55 | newtCenteredWindow(max_len, argc, NULL); |
| 56 | newtFormRun(form, &es); |
| 57 | rc = newtListboxGetCurrent(listbox) - NULL; |
| 58 | if (es.reason == NEWT_EXIT_HOTKEY) |
| 59 | rc = -1; |
| 60 | newtPopWindow(); |
| 61 | out_destroy_form: |
| 62 | newtFormDestroy(form); |
| 63 | return rc; |
| 64 | } |
| 65 | |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 66 | int ui__help_window(const char *text) |
Arnaldo Carvalho de Melo | a9a4ab7 | 2010-05-16 21:04:27 -0300 | [diff] [blame] | 67 | { |
| 68 | struct newtExitStruct es; |
| 69 | newtComponent tb, form = newt_form__new(); |
| 70 | int rc = -1; |
| 71 | int max_len = 0, nr_lines = 0; |
| 72 | const char *t; |
| 73 | |
| 74 | if (form == NULL) |
| 75 | return -1; |
| 76 | |
| 77 | t = text; |
| 78 | while (1) { |
| 79 | const char *sep = strchr(t, '\n'); |
| 80 | int len; |
| 81 | |
| 82 | if (sep == NULL) |
| 83 | sep = strchr(t, '\0'); |
| 84 | len = sep - t; |
| 85 | if (max_len < len) |
| 86 | max_len = len; |
| 87 | ++nr_lines; |
| 88 | if (*sep == '\0') |
| 89 | break; |
| 90 | t = sep + 1; |
| 91 | } |
| 92 | |
| 93 | tb = newtTextbox(0, 0, max_len, nr_lines, 0); |
| 94 | if (tb == NULL) |
| 95 | goto out_destroy_form; |
| 96 | |
| 97 | newtTextboxSetText(tb, text); |
| 98 | newtFormAddComponent(form, tb); |
| 99 | newtCenteredWindow(max_len, nr_lines, NULL); |
| 100 | newtFormRun(form, &es); |
| 101 | newtPopWindow(); |
| 102 | rc = 0; |
| 103 | out_destroy_form: |
| 104 | newtFormDestroy(form); |
| 105 | return rc; |
| 106 | } |
| 107 | |
Arnaldo Carvalho de Melo | 068ffaa | 2010-11-27 02:41:01 -0200 | [diff] [blame] | 108 | static const char yes[] = "Yes", no[] = "No", |
| 109 | warning_str[] = "Warning!", ok[] = "Ok"; |
Cyrill Gorcunov | a3da8e4 | 2010-11-06 11:47:24 +0300 | [diff] [blame] | 110 | |
Arnaldo Carvalho de Melo | 1e6dd07 | 2010-08-10 15:58:50 -0300 | [diff] [blame] | 111 | bool ui__dialog_yesno(const char *msg) |
Arnaldo Carvalho de Melo | 53c5401 | 2010-03-24 16:40:14 -0300 | [diff] [blame] | 112 | { |
| 113 | /* newtWinChoice should really be accepting const char pointers... */ |
Cyrill Gorcunov | a3da8e4 | 2010-11-06 11:47:24 +0300 | [diff] [blame] | 114 | return newtWinChoice(NULL, (char *)yes, (char *)no, (char *)msg) == 1; |
Arnaldo Carvalho de Melo | 53c5401 | 2010-03-24 16:40:14 -0300 | [diff] [blame] | 115 | } |
Arnaldo Carvalho de Melo | 068ffaa | 2010-11-27 02:41:01 -0200 | [diff] [blame] | 116 | |
| 117 | void ui__warning(const char *format, ...) |
| 118 | { |
| 119 | va_list args; |
| 120 | |
| 121 | va_start(args, format); |
Arnaldo Carvalho de Melo | a1ceb741 | 2011-03-01 10:24:43 -0300 | [diff] [blame^] | 122 | if (use_browser > 0) { |
| 123 | pthread_mutex_lock(&ui__lock); |
Arnaldo Carvalho de Melo | 068ffaa | 2010-11-27 02:41:01 -0200 | [diff] [blame] | 124 | newtWinMessagev((char *)warning_str, (char *)ok, |
| 125 | (char *)format, args); |
Arnaldo Carvalho de Melo | a1ceb741 | 2011-03-01 10:24:43 -0300 | [diff] [blame^] | 126 | pthread_mutex_unlock(&ui__lock); |
| 127 | } else |
Arnaldo Carvalho de Melo | 068ffaa | 2010-11-27 02:41:01 -0200 | [diff] [blame] | 128 | vfprintf(stderr, format, args); |
| 129 | va_end(args); |
| 130 | } |