Arnaldo Carvalho de Melo | ae55795 | 2011-10-26 08:00:55 -0200 | [diff] [blame] | 1 | #include "../util.h" |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 2 | #include <signal.h> |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 3 | #include <stdbool.h> |
| 4 | #include <string.h> |
Arnaldo Carvalho de Melo | 7081e08 | 2010-03-12 10:48:12 -0300 | [diff] [blame] | 5 | #include <sys/ttydefaults.h> |
Arnaldo Carvalho de Melo | f9224c5 | 2010-03-11 20:12:44 -0300 | [diff] [blame] | 6 | |
Arnaldo Carvalho de Melo | 1e6dd07 | 2010-08-10 15:58:50 -0300 | [diff] [blame] | 7 | #include "../cache.h" |
| 8 | #include "../debug.h" |
| 9 | #include "browser.h" |
Arnaldo Carvalho de Melo | 1056d3d | 2011-10-26 07:11:03 -0200 | [diff] [blame] | 10 | #include "keysyms.h" |
Arnaldo Carvalho de Melo | 1e6dd07 | 2010-08-10 15:58:50 -0300 | [diff] [blame] | 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 | ae55795 | 2011-10-26 08:00:55 -0200 | [diff] [blame] | 14 | #include "libslang.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 | 1e6dd07 | 2010-08-10 15:58:50 -0300 | [diff] [blame] | 59 | int ui__popup_menu(int argc, char * const argv[]) |
Arnaldo Carvalho de Melo | 53c5401 | 2010-03-24 16:40:14 -0300 | [diff] [blame] | 60 | { |
Arnaldo Carvalho de Melo | 1056d3d | 2011-10-26 07:11:03 -0200 | [diff] [blame] | 61 | struct ui_browser menu = { |
| 62 | .entries = (void *)argv, |
| 63 | .refresh = ui_browser__argv_refresh, |
| 64 | .seek = ui_browser__argv_seek, |
| 65 | .write = ui_browser__argv_write, |
| 66 | .nr_entries = argc, |
| 67 | }; |
Arnaldo Carvalho de Melo | 53c5401 | 2010-03-24 16:40:14 -0300 | [diff] [blame] | 68 | |
Arnaldo Carvalho de Melo | 1056d3d | 2011-10-26 07:11:03 -0200 | [diff] [blame] | 69 | return popup_menu__run(&menu); |
Arnaldo Carvalho de Melo | 53c5401 | 2010-03-24 16:40:14 -0300 | [diff] [blame] | 70 | } |
| 71 | |
Namhyung Kim | aa49f6e | 2012-03-16 17:50:52 +0900 | [diff] [blame] | 72 | int ui_browser__input_window(const char *title, const char *text, char *input, |
| 73 | const char *exit_msg, int delay_secs) |
| 74 | { |
| 75 | int x, y, len, key; |
| 76 | int max_len = 60, nr_lines = 0; |
| 77 | static char buf[50]; |
| 78 | const char *t; |
| 79 | |
| 80 | t = text; |
| 81 | while (1) { |
| 82 | const char *sep = strchr(t, '\n'); |
| 83 | |
| 84 | if (sep == NULL) |
| 85 | sep = strchr(t, '\0'); |
| 86 | len = sep - t; |
| 87 | if (max_len < len) |
| 88 | max_len = len; |
| 89 | ++nr_lines; |
| 90 | if (*sep == '\0') |
| 91 | break; |
| 92 | t = sep + 1; |
| 93 | } |
| 94 | |
| 95 | max_len += 2; |
| 96 | nr_lines += 8; |
| 97 | y = SLtt_Screen_Rows / 2 - nr_lines / 2; |
| 98 | x = SLtt_Screen_Cols / 2 - max_len / 2; |
| 99 | |
| 100 | SLsmg_set_color(0); |
| 101 | SLsmg_draw_box(y, x++, nr_lines, max_len); |
| 102 | if (title) { |
| 103 | SLsmg_gotorc(y, x + 1); |
| 104 | SLsmg_write_string((char *)title); |
| 105 | } |
| 106 | SLsmg_gotorc(++y, x); |
| 107 | nr_lines -= 7; |
| 108 | max_len -= 2; |
| 109 | SLsmg_write_wrapped_string((unsigned char *)text, y, x, |
| 110 | nr_lines, max_len, 1); |
Namhyung Kim | f7e5410 | 2012-03-19 11:46:20 +0900 | [diff] [blame] | 111 | y += nr_lines; |
| 112 | len = 5; |
| 113 | while (len--) { |
| 114 | SLsmg_gotorc(y + len - 1, x); |
| 115 | SLsmg_write_nstring((char *)" ", max_len); |
| 116 | } |
| 117 | SLsmg_draw_box(y++, x + 1, 3, max_len - 2); |
Namhyung Kim | aa49f6e | 2012-03-16 17:50:52 +0900 | [diff] [blame] | 118 | |
| 119 | SLsmg_gotorc(y + 3, x); |
| 120 | SLsmg_write_nstring((char *)exit_msg, max_len); |
| 121 | SLsmg_refresh(); |
| 122 | |
| 123 | x += 2; |
| 124 | len = 0; |
| 125 | key = ui__getch(delay_secs); |
| 126 | while (key != K_TIMER && key != K_ENTER && key != K_ESC) { |
| 127 | if (key == K_BKSPC) { |
| 128 | if (len == 0) |
| 129 | goto next_key; |
| 130 | SLsmg_gotorc(y, x + --len); |
| 131 | SLsmg_write_char(' '); |
| 132 | } else { |
| 133 | buf[len] = key; |
| 134 | SLsmg_gotorc(y, x + len++); |
| 135 | SLsmg_write_char(key); |
| 136 | } |
| 137 | SLsmg_refresh(); |
| 138 | |
| 139 | /* XXX more graceful overflow handling needed */ |
| 140 | if (len == sizeof(buf) - 1) { |
| 141 | ui_helpline__push("maximum size of symbol name reached!"); |
| 142 | key = K_ENTER; |
| 143 | break; |
| 144 | } |
| 145 | next_key: |
| 146 | key = ui__getch(delay_secs); |
| 147 | } |
| 148 | |
| 149 | buf[len] = '\0'; |
| 150 | strncpy(input, buf, len+1); |
| 151 | return key; |
| 152 | } |
| 153 | |
Arnaldo Carvalho de Melo | ae55795 | 2011-10-26 08:00:55 -0200 | [diff] [blame] | 154 | int ui__question_window(const char *title, const char *text, |
| 155 | const char *exit_msg, int delay_secs) |
Arnaldo Carvalho de Melo | a9a4ab7 | 2010-05-16 21:04:27 -0300 | [diff] [blame] | 156 | { |
Arnaldo Carvalho de Melo | ae55795 | 2011-10-26 08:00:55 -0200 | [diff] [blame] | 157 | int x, y; |
Arnaldo Carvalho de Melo | a9a4ab7 | 2010-05-16 21:04:27 -0300 | [diff] [blame] | 158 | int max_len = 0, nr_lines = 0; |
| 159 | const char *t; |
| 160 | |
Arnaldo Carvalho de Melo | a9a4ab7 | 2010-05-16 21:04:27 -0300 | [diff] [blame] | 161 | t = text; |
| 162 | while (1) { |
| 163 | const char *sep = strchr(t, '\n'); |
| 164 | int len; |
| 165 | |
| 166 | if (sep == NULL) |
| 167 | sep = strchr(t, '\0'); |
| 168 | len = sep - t; |
| 169 | if (max_len < len) |
| 170 | max_len = len; |
| 171 | ++nr_lines; |
| 172 | if (*sep == '\0') |
| 173 | break; |
| 174 | t = sep + 1; |
| 175 | } |
| 176 | |
Arnaldo Carvalho de Melo | ae55795 | 2011-10-26 08:00:55 -0200 | [diff] [blame] | 177 | max_len += 2; |
| 178 | nr_lines += 4; |
| 179 | y = SLtt_Screen_Rows / 2 - nr_lines / 2, |
| 180 | x = SLtt_Screen_Cols / 2 - max_len / 2; |
Arnaldo Carvalho de Melo | a9a4ab7 | 2010-05-16 21:04:27 -0300 | [diff] [blame] | 181 | |
Arnaldo Carvalho de Melo | ae55795 | 2011-10-26 08:00:55 -0200 | [diff] [blame] | 182 | SLsmg_set_color(0); |
| 183 | SLsmg_draw_box(y, x++, nr_lines, max_len); |
| 184 | if (title) { |
| 185 | SLsmg_gotorc(y, x + 1); |
| 186 | SLsmg_write_string((char *)title); |
| 187 | } |
| 188 | SLsmg_gotorc(++y, x); |
| 189 | nr_lines -= 2; |
| 190 | max_len -= 2; |
| 191 | SLsmg_write_wrapped_string((unsigned char *)text, y, x, |
| 192 | nr_lines, max_len, 1); |
| 193 | SLsmg_gotorc(y + nr_lines - 2, x); |
| 194 | SLsmg_write_nstring((char *)" ", max_len); |
| 195 | SLsmg_gotorc(y + nr_lines - 1, x); |
| 196 | SLsmg_write_nstring((char *)exit_msg, max_len); |
| 197 | SLsmg_refresh(); |
| 198 | return ui__getch(delay_secs); |
Arnaldo Carvalho de Melo | a9a4ab7 | 2010-05-16 21:04:27 -0300 | [diff] [blame] | 199 | } |
| 200 | |
Arnaldo Carvalho de Melo | ae55795 | 2011-10-26 08:00:55 -0200 | [diff] [blame] | 201 | int ui__help_window(const char *text) |
| 202 | { |
| 203 | return ui__question_window("Help", text, "Press any key...", 0); |
| 204 | } |
Cyrill Gorcunov | a3da8e4 | 2010-11-06 11:47:24 +0300 | [diff] [blame] | 205 | |
Arnaldo Carvalho de Melo | 4610e41 | 2011-10-26 12:04:37 -0200 | [diff] [blame] | 206 | int ui__dialog_yesno(const char *msg) |
Arnaldo Carvalho de Melo | 53c5401 | 2010-03-24 16:40:14 -0300 | [diff] [blame] | 207 | { |
Arnaldo Carvalho de Melo | 4610e41 | 2011-10-26 12:04:37 -0200 | [diff] [blame] | 208 | return ui__question_window(NULL, msg, "Enter: Yes, ESC: No", 0); |
Arnaldo Carvalho de Melo | ae55795 | 2011-10-26 08:00:55 -0200 | [diff] [blame] | 209 | } |
| 210 | |
Arnaldo Carvalho de Melo | 4610e41 | 2011-10-26 12:04:37 -0200 | [diff] [blame] | 211 | int __ui__warning(const char *title, const char *format, va_list args) |
Arnaldo Carvalho de Melo | ae55795 | 2011-10-26 08:00:55 -0200 | [diff] [blame] | 212 | { |
| 213 | char *s; |
| 214 | |
| 215 | if (use_browser > 0 && vasprintf(&s, format, args) > 0) { |
Arnaldo Carvalho de Melo | 4610e41 | 2011-10-26 12:04:37 -0200 | [diff] [blame] | 216 | int key; |
| 217 | |
Arnaldo Carvalho de Melo | ae55795 | 2011-10-26 08:00:55 -0200 | [diff] [blame] | 218 | pthread_mutex_lock(&ui__lock); |
Arnaldo Carvalho de Melo | 4610e41 | 2011-10-26 12:04:37 -0200 | [diff] [blame] | 219 | key = ui__question_window(title, s, "Press any key...", 0); |
Arnaldo Carvalho de Melo | ae55795 | 2011-10-26 08:00:55 -0200 | [diff] [blame] | 220 | pthread_mutex_unlock(&ui__lock); |
| 221 | free(s); |
Arnaldo Carvalho de Melo | 4610e41 | 2011-10-26 12:04:37 -0200 | [diff] [blame] | 222 | return key; |
Arnaldo Carvalho de Melo | ae55795 | 2011-10-26 08:00:55 -0200 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | fprintf(stderr, "%s:\n", title); |
| 226 | vfprintf(stderr, format, args); |
Arnaldo Carvalho de Melo | 4610e41 | 2011-10-26 12:04:37 -0200 | [diff] [blame] | 227 | return K_ESC; |
Arnaldo Carvalho de Melo | 53c5401 | 2010-03-24 16:40:14 -0300 | [diff] [blame] | 228 | } |
Arnaldo Carvalho de Melo | 068ffaa | 2010-11-27 02:41:01 -0200 | [diff] [blame] | 229 | |
Arnaldo Carvalho de Melo | 4610e41 | 2011-10-26 12:04:37 -0200 | [diff] [blame] | 230 | int ui__warning(const char *format, ...) |
Arnaldo Carvalho de Melo | 068ffaa | 2010-11-27 02:41:01 -0200 | [diff] [blame] | 231 | { |
Arnaldo Carvalho de Melo | 4610e41 | 2011-10-26 12:04:37 -0200 | [diff] [blame] | 232 | int key; |
Arnaldo Carvalho de Melo | 068ffaa | 2010-11-27 02:41:01 -0200 | [diff] [blame] | 233 | va_list args; |
| 234 | |
| 235 | va_start(args, format); |
Arnaldo Carvalho de Melo | 4610e41 | 2011-10-26 12:04:37 -0200 | [diff] [blame] | 236 | key = __ui__warning("Warning", format, args); |
Arnaldo Carvalho de Melo | ae55795 | 2011-10-26 08:00:55 -0200 | [diff] [blame] | 237 | va_end(args); |
Arnaldo Carvalho de Melo | 4610e41 | 2011-10-26 12:04:37 -0200 | [diff] [blame] | 238 | return key; |
Arnaldo Carvalho de Melo | ae55795 | 2011-10-26 08:00:55 -0200 | [diff] [blame] | 239 | } |
| 240 | |
Arnaldo Carvalho de Melo | 4610e41 | 2011-10-26 12:04:37 -0200 | [diff] [blame] | 241 | int ui__error(const char *format, ...) |
Arnaldo Carvalho de Melo | ae55795 | 2011-10-26 08:00:55 -0200 | [diff] [blame] | 242 | { |
Arnaldo Carvalho de Melo | 4610e41 | 2011-10-26 12:04:37 -0200 | [diff] [blame] | 243 | int key; |
Arnaldo Carvalho de Melo | ae55795 | 2011-10-26 08:00:55 -0200 | [diff] [blame] | 244 | va_list args; |
| 245 | |
| 246 | va_start(args, format); |
Arnaldo Carvalho de Melo | 4610e41 | 2011-10-26 12:04:37 -0200 | [diff] [blame] | 247 | key = __ui__warning("Error", format, args); |
Arnaldo Carvalho de Melo | 068ffaa | 2010-11-27 02:41:01 -0200 | [diff] [blame] | 248 | va_end(args); |
Arnaldo Carvalho de Melo | 4610e41 | 2011-10-26 12:04:37 -0200 | [diff] [blame] | 249 | return key; |
Arnaldo Carvalho de Melo | 068ffaa | 2010-11-27 02:41:01 -0200 | [diff] [blame] | 250 | } |