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" |
| 12 | #include "util.h" |
Arnaldo Carvalho de Melo | f9224c5 | 2010-03-11 20:12:44 -0300 | [diff] [blame] | 13 | |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 14 | newtComponent newt_form__new(void); |
Arnaldo Carvalho de Melo | 5f4d3f8 | 2010-03-26 21:16:22 -0300 | [diff] [blame] | 15 | |
Arnaldo Carvalho de Melo | 7081e08 | 2010-03-12 10:48:12 -0300 | [diff] [blame] | 16 | static void newt_form__set_exit_keys(newtComponent self) |
| 17 | { |
Arnaldo Carvalho de Melo | a308f3a | 2010-05-16 20:29:38 -0300 | [diff] [blame] | 18 | newtFormAddHotKey(self, NEWT_KEY_LEFT); |
Arnaldo Carvalho de Melo | 7081e08 | 2010-03-12 10:48:12 -0300 | [diff] [blame] | 19 | newtFormAddHotKey(self, NEWT_KEY_ESCAPE); |
| 20 | newtFormAddHotKey(self, 'Q'); |
| 21 | newtFormAddHotKey(self, 'q'); |
| 22 | newtFormAddHotKey(self, CTRL('c')); |
| 23 | } |
| 24 | |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 25 | newtComponent newt_form__new(void) |
Arnaldo Carvalho de Melo | 7081e08 | 2010-03-12 10:48:12 -0300 | [diff] [blame] | 26 | { |
| 27 | newtComponent self = newtForm(NULL, NULL, 0); |
| 28 | if (self) |
| 29 | newt_form__set_exit_keys(self); |
| 30 | return self; |
| 31 | } |
| 32 | |
Arnaldo Carvalho de Melo | 1e6dd07 | 2010-08-10 15:58:50 -0300 | [diff] [blame^] | 33 | int ui__popup_menu(int argc, char * const argv[]) |
Arnaldo Carvalho de Melo | 53c5401 | 2010-03-24 16:40:14 -0300 | [diff] [blame] | 34 | { |
| 35 | struct newtExitStruct es; |
| 36 | int i, rc = -1, max_len = 5; |
| 37 | newtComponent listbox, form = newt_form__new(); |
| 38 | |
| 39 | if (form == NULL) |
| 40 | return -1; |
| 41 | |
| 42 | listbox = newtListbox(0, 0, argc, NEWT_FLAG_RETURNEXIT); |
| 43 | if (listbox == NULL) |
| 44 | goto out_destroy_form; |
| 45 | |
Arnaldo Carvalho de Melo | 7f82645 | 2010-05-10 10:51:25 -0300 | [diff] [blame] | 46 | newtFormAddComponent(form, listbox); |
Arnaldo Carvalho de Melo | 53c5401 | 2010-03-24 16:40:14 -0300 | [diff] [blame] | 47 | |
| 48 | for (i = 0; i < argc; ++i) { |
| 49 | int len = strlen(argv[i]); |
| 50 | if (len > max_len) |
| 51 | max_len = len; |
| 52 | if (newtListboxAddEntry(listbox, argv[i], (void *)(long)i)) |
| 53 | goto out_destroy_form; |
| 54 | } |
| 55 | |
| 56 | newtCenteredWindow(max_len, argc, NULL); |
| 57 | newtFormRun(form, &es); |
| 58 | rc = newtListboxGetCurrent(listbox) - NULL; |
| 59 | if (es.reason == NEWT_EXIT_HOTKEY) |
| 60 | rc = -1; |
| 61 | newtPopWindow(); |
| 62 | out_destroy_form: |
| 63 | newtFormDestroy(form); |
| 64 | return rc; |
| 65 | } |
| 66 | |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 67 | int ui__help_window(const char *text) |
Arnaldo Carvalho de Melo | a9a4ab7 | 2010-05-16 21:04:27 -0300 | [diff] [blame] | 68 | { |
| 69 | struct newtExitStruct es; |
| 70 | newtComponent tb, form = newt_form__new(); |
| 71 | int rc = -1; |
| 72 | int max_len = 0, nr_lines = 0; |
| 73 | const char *t; |
| 74 | |
| 75 | if (form == NULL) |
| 76 | return -1; |
| 77 | |
| 78 | t = text; |
| 79 | while (1) { |
| 80 | const char *sep = strchr(t, '\n'); |
| 81 | int len; |
| 82 | |
| 83 | if (sep == NULL) |
| 84 | sep = strchr(t, '\0'); |
| 85 | len = sep - t; |
| 86 | if (max_len < len) |
| 87 | max_len = len; |
| 88 | ++nr_lines; |
| 89 | if (*sep == '\0') |
| 90 | break; |
| 91 | t = sep + 1; |
| 92 | } |
| 93 | |
| 94 | tb = newtTextbox(0, 0, max_len, nr_lines, 0); |
| 95 | if (tb == NULL) |
| 96 | goto out_destroy_form; |
| 97 | |
| 98 | newtTextboxSetText(tb, text); |
| 99 | newtFormAddComponent(form, tb); |
| 100 | newtCenteredWindow(max_len, nr_lines, NULL); |
| 101 | newtFormRun(form, &es); |
| 102 | newtPopWindow(); |
| 103 | rc = 0; |
| 104 | out_destroy_form: |
| 105 | newtFormDestroy(form); |
| 106 | return rc; |
| 107 | } |
| 108 | |
Arnaldo Carvalho de Melo | 1e6dd07 | 2010-08-10 15:58:50 -0300 | [diff] [blame^] | 109 | bool ui__dialog_yesno(const char *msg) |
Arnaldo Carvalho de Melo | 53c5401 | 2010-03-24 16:40:14 -0300 | [diff] [blame] | 110 | { |
| 111 | /* newtWinChoice should really be accepting const char pointers... */ |
| 112 | char yes[] = "Yes", no[] = "No"; |
Arnaldo Carvalho de Melo | c0ed55d | 2010-04-05 12:04:23 -0300 | [diff] [blame] | 113 | return newtWinChoice(NULL, yes, no, (char *)msg) == 1; |
Arnaldo Carvalho de Melo | 53c5401 | 2010-03-24 16:40:14 -0300 | [diff] [blame] | 114 | } |