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" |
Arnaldo Carvalho de Melo | 1056d3d | 2011-10-26 07:11:03 -0200 | [diff] [blame^] | 11 | #include "keysyms.h" |
Arnaldo Carvalho de Melo | 1e6dd07 | 2010-08-10 15:58:50 -0300 | [diff] [blame] | 12 | #include "helpline.h" |
Arnaldo Carvalho de Melo | a1ceb741 | 2011-03-01 10:24:43 -0300 | [diff] [blame] | 13 | #include "ui.h" |
Arnaldo Carvalho de Melo | 1e6dd07 | 2010-08-10 15:58:50 -0300 | [diff] [blame] | 14 | #include "util.h" |
Arnaldo Carvalho de Melo | f9224c5 | 2010-03-11 20:12:44 -0300 | [diff] [blame] | 15 | |
Arnaldo Carvalho de Melo | 1056d3d | 2011-10-26 07:11:03 -0200 | [diff] [blame^] | 16 | static void ui_browser__argv_write(struct ui_browser *browser, |
| 17 | void *entry, int row) |
| 18 | { |
| 19 | char **arg = entry; |
| 20 | bool current_entry = ui_browser__is_current_entry(browser, row); |
| 21 | |
| 22 | ui_browser__set_color(browser, current_entry ? HE_COLORSET_SELECTED : |
| 23 | HE_COLORSET_NORMAL); |
| 24 | slsmg_write_nstring(*arg, browser->width); |
| 25 | } |
| 26 | |
| 27 | static int popup_menu__run(struct ui_browser *menu) |
| 28 | { |
| 29 | int key; |
| 30 | |
| 31 | if (ui_browser__show(menu, " ", "ESC: exit, ENTER|->: Select option") < 0) |
| 32 | return -1; |
| 33 | |
| 34 | while (1) { |
| 35 | key = ui_browser__run(menu, 0); |
| 36 | |
| 37 | switch (key) { |
| 38 | case K_RIGHT: |
| 39 | case K_ENTER: |
| 40 | key = menu->index; |
| 41 | break; |
| 42 | case K_LEFT: |
| 43 | case K_ESC: |
| 44 | case 'q': |
| 45 | case CTRL('c'): |
| 46 | key = -1; |
| 47 | break; |
| 48 | default: |
| 49 | continue; |
| 50 | } |
| 51 | |
| 52 | break; |
| 53 | } |
| 54 | |
| 55 | ui_browser__hide(menu); |
| 56 | return key; |
| 57 | } |
| 58 | |
Arnaldo Carvalho de Melo | 7081e08 | 2010-03-12 10:48:12 -0300 | [diff] [blame] | 59 | static void newt_form__set_exit_keys(newtComponent self) |
| 60 | { |
Arnaldo Carvalho de Melo | a308f3a | 2010-05-16 20:29:38 -0300 | [diff] [blame] | 61 | newtFormAddHotKey(self, NEWT_KEY_LEFT); |
Arnaldo Carvalho de Melo | 7081e08 | 2010-03-12 10:48:12 -0300 | [diff] [blame] | 62 | newtFormAddHotKey(self, NEWT_KEY_ESCAPE); |
| 63 | newtFormAddHotKey(self, 'Q'); |
| 64 | newtFormAddHotKey(self, 'q'); |
| 65 | newtFormAddHotKey(self, CTRL('c')); |
| 66 | } |
| 67 | |
Arnaldo Carvalho de Melo | 4c1c952 | 2010-08-12 12:37:51 -0300 | [diff] [blame] | 68 | static newtComponent newt_form__new(void) |
Arnaldo Carvalho de Melo | 7081e08 | 2010-03-12 10:48:12 -0300 | [diff] [blame] | 69 | { |
| 70 | newtComponent self = newtForm(NULL, NULL, 0); |
| 71 | if (self) |
| 72 | newt_form__set_exit_keys(self); |
| 73 | return self; |
| 74 | } |
| 75 | |
Arnaldo Carvalho de Melo | 1e6dd07 | 2010-08-10 15:58:50 -0300 | [diff] [blame] | 76 | int ui__popup_menu(int argc, char * const argv[]) |
Arnaldo Carvalho de Melo | 53c5401 | 2010-03-24 16:40:14 -0300 | [diff] [blame] | 77 | { |
Arnaldo Carvalho de Melo | 1056d3d | 2011-10-26 07:11:03 -0200 | [diff] [blame^] | 78 | struct ui_browser menu = { |
| 79 | .entries = (void *)argv, |
| 80 | .refresh = ui_browser__argv_refresh, |
| 81 | .seek = ui_browser__argv_seek, |
| 82 | .write = ui_browser__argv_write, |
| 83 | .nr_entries = argc, |
| 84 | }; |
Arnaldo Carvalho de Melo | 53c5401 | 2010-03-24 16:40:14 -0300 | [diff] [blame] | 85 | |
Arnaldo Carvalho de Melo | 1056d3d | 2011-10-26 07:11:03 -0200 | [diff] [blame^] | 86 | return popup_menu__run(&menu); |
Arnaldo Carvalho de Melo | 53c5401 | 2010-03-24 16:40:14 -0300 | [diff] [blame] | 87 | } |
| 88 | |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 89 | int ui__help_window(const char *text) |
Arnaldo Carvalho de Melo | a9a4ab7 | 2010-05-16 21:04:27 -0300 | [diff] [blame] | 90 | { |
| 91 | struct newtExitStruct es; |
| 92 | newtComponent tb, form = newt_form__new(); |
| 93 | int rc = -1; |
| 94 | int max_len = 0, nr_lines = 0; |
| 95 | const char *t; |
| 96 | |
| 97 | if (form == NULL) |
| 98 | return -1; |
| 99 | |
| 100 | t = text; |
| 101 | while (1) { |
| 102 | const char *sep = strchr(t, '\n'); |
| 103 | int len; |
| 104 | |
| 105 | if (sep == NULL) |
| 106 | sep = strchr(t, '\0'); |
| 107 | len = sep - t; |
| 108 | if (max_len < len) |
| 109 | max_len = len; |
| 110 | ++nr_lines; |
| 111 | if (*sep == '\0') |
| 112 | break; |
| 113 | t = sep + 1; |
| 114 | } |
| 115 | |
| 116 | tb = newtTextbox(0, 0, max_len, nr_lines, 0); |
| 117 | if (tb == NULL) |
| 118 | goto out_destroy_form; |
| 119 | |
| 120 | newtTextboxSetText(tb, text); |
| 121 | newtFormAddComponent(form, tb); |
| 122 | newtCenteredWindow(max_len, nr_lines, NULL); |
| 123 | newtFormRun(form, &es); |
| 124 | newtPopWindow(); |
| 125 | rc = 0; |
| 126 | out_destroy_form: |
| 127 | newtFormDestroy(form); |
| 128 | return rc; |
| 129 | } |
| 130 | |
Arnaldo Carvalho de Melo | 068ffaa | 2010-11-27 02:41:01 -0200 | [diff] [blame] | 131 | static const char yes[] = "Yes", no[] = "No", |
| 132 | warning_str[] = "Warning!", ok[] = "Ok"; |
Cyrill Gorcunov | a3da8e4 | 2010-11-06 11:47:24 +0300 | [diff] [blame] | 133 | |
Arnaldo Carvalho de Melo | 1e6dd07 | 2010-08-10 15:58:50 -0300 | [diff] [blame] | 134 | bool ui__dialog_yesno(const char *msg) |
Arnaldo Carvalho de Melo | 53c5401 | 2010-03-24 16:40:14 -0300 | [diff] [blame] | 135 | { |
| 136 | /* newtWinChoice should really be accepting const char pointers... */ |
Cyrill Gorcunov | a3da8e4 | 2010-11-06 11:47:24 +0300 | [diff] [blame] | 137 | return newtWinChoice(NULL, (char *)yes, (char *)no, (char *)msg) == 1; |
Arnaldo Carvalho de Melo | 53c5401 | 2010-03-24 16:40:14 -0300 | [diff] [blame] | 138 | } |
Arnaldo Carvalho de Melo | 068ffaa | 2010-11-27 02:41:01 -0200 | [diff] [blame] | 139 | |
| 140 | void ui__warning(const char *format, ...) |
| 141 | { |
| 142 | va_list args; |
| 143 | |
| 144 | va_start(args, format); |
Arnaldo Carvalho de Melo | a1ceb741 | 2011-03-01 10:24:43 -0300 | [diff] [blame] | 145 | if (use_browser > 0) { |
| 146 | pthread_mutex_lock(&ui__lock); |
Arnaldo Carvalho de Melo | 068ffaa | 2010-11-27 02:41:01 -0200 | [diff] [blame] | 147 | newtWinMessagev((char *)warning_str, (char *)ok, |
| 148 | (char *)format, args); |
Arnaldo Carvalho de Melo | a1ceb741 | 2011-03-01 10:24:43 -0300 | [diff] [blame] | 149 | pthread_mutex_unlock(&ui__lock); |
| 150 | } else |
Arnaldo Carvalho de Melo | 068ffaa | 2010-11-27 02:41:01 -0200 | [diff] [blame] | 151 | vfprintf(stderr, format, args); |
| 152 | va_end(args); |
| 153 | } |