Commit | Line | Data |
---|---|---|
f9224c5c | 1 | #include <newt.h> |
d1b4f249 ACM |
2 | #include <signal.h> |
3 | #include <stdio.h> | |
4 | #include <stdbool.h> | |
5 | #include <string.h> | |
7081e087 | 6 | #include <sys/ttydefaults.h> |
f9224c5c | 7 | |
1e6dd077 ACM |
8 | #include "../cache.h" |
9 | #include "../debug.h" | |
10 | #include "browser.h" | |
11 | #include "helpline.h" | |
a1ceb741 | 12 | #include "ui.h" |
1e6dd077 | 13 | #include "util.h" |
f9224c5c | 14 | |
7081e087 ACM |
15 | static void newt_form__set_exit_keys(newtComponent self) |
16 | { | |
a308f3a8 | 17 | newtFormAddHotKey(self, NEWT_KEY_LEFT); |
7081e087 ACM |
18 | newtFormAddHotKey(self, NEWT_KEY_ESCAPE); |
19 | newtFormAddHotKey(self, 'Q'); | |
20 | newtFormAddHotKey(self, 'q'); | |
21 | newtFormAddHotKey(self, CTRL('c')); | |
22 | } | |
23 | ||
4c1c952e | 24 | static newtComponent newt_form__new(void) |
7081e087 ACM |
25 | { |
26 | newtComponent self = newtForm(NULL, NULL, 0); | |
27 | if (self) | |
28 | newt_form__set_exit_keys(self); | |
29 | return self; | |
30 | } | |
31 | ||
1e6dd077 | 32 | int ui__popup_menu(int argc, char * const argv[]) |
53c54019 ACM |
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 | ||
7f826453 | 45 | newtFormAddComponent(form, listbox); |
53c54019 ACM |
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 | ||
d1b4f249 | 66 | int ui__help_window(const char *text) |
a9a4ab74 ACM |
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 | ||
068ffaa8 ACM |
108 | static const char yes[] = "Yes", no[] = "No", |
109 | warning_str[] = "Warning!", ok[] = "Ok"; | |
a3da8e45 | 110 | |
1e6dd077 | 111 | bool ui__dialog_yesno(const char *msg) |
53c54019 ACM |
112 | { |
113 | /* newtWinChoice should really be accepting const char pointers... */ | |
a3da8e45 | 114 | return newtWinChoice(NULL, (char *)yes, (char *)no, (char *)msg) == 1; |
53c54019 | 115 | } |
068ffaa8 ACM |
116 | |
117 | void ui__warning(const char *format, ...) | |
118 | { | |
119 | va_list args; | |
120 | ||
121 | va_start(args, format); | |
a1ceb741 ACM |
122 | if (use_browser > 0) { |
123 | pthread_mutex_lock(&ui__lock); | |
068ffaa8 ACM |
124 | newtWinMessagev((char *)warning_str, (char *)ok, |
125 | (char *)format, args); | |
a1ceb741 ACM |
126 | pthread_mutex_unlock(&ui__lock); |
127 | } else | |
068ffaa8 ACM |
128 | vfprintf(stderr, format, args); |
129 | va_end(args); | |
130 | } |