blob: 37e6fe081a58bfbfe2923920ab0e74a55fa5a9ed [file] [log] [blame]
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -03001#include <newt.h>
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03002#include <signal.h>
3#include <stdio.h>
4#include <stdbool.h>
5#include <string.h>
Arnaldo Carvalho de Melo7081e082010-03-12 10:48:12 -03006#include <sys/ttydefaults.h>
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -03007
Arnaldo Carvalho de Melo1e6dd072010-08-10 15:58:50 -03008#include "../cache.h"
9#include "../debug.h"
10#include "browser.h"
Arnaldo Carvalho de Melo1056d3d2011-10-26 07:11:03 -020011#include "keysyms.h"
Arnaldo Carvalho de Melo1e6dd072010-08-10 15:58:50 -030012#include "helpline.h"
Arnaldo Carvalho de Meloa1ceb7412011-03-01 10:24:43 -030013#include "ui.h"
Arnaldo Carvalho de Melo1e6dd072010-08-10 15:58:50 -030014#include "util.h"
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -030015
Arnaldo Carvalho de Melo1056d3d2011-10-26 07:11:03 -020016static 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
27static 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 Melo7081e082010-03-12 10:48:12 -030059static void newt_form__set_exit_keys(newtComponent self)
60{
Arnaldo Carvalho de Meloa308f3a2010-05-16 20:29:38 -030061 newtFormAddHotKey(self, NEWT_KEY_LEFT);
Arnaldo Carvalho de Melo7081e082010-03-12 10:48:12 -030062 newtFormAddHotKey(self, NEWT_KEY_ESCAPE);
63 newtFormAddHotKey(self, 'Q');
64 newtFormAddHotKey(self, 'q');
65 newtFormAddHotKey(self, CTRL('c'));
66}
67
Arnaldo Carvalho de Melo4c1c9522010-08-12 12:37:51 -030068static newtComponent newt_form__new(void)
Arnaldo Carvalho de Melo7081e082010-03-12 10:48:12 -030069{
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 Melo1e6dd072010-08-10 15:58:50 -030076int ui__popup_menu(int argc, char * const argv[])
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -030077{
Arnaldo Carvalho de Melo1056d3d2011-10-26 07:11:03 -020078 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 Melo53c54012010-03-24 16:40:14 -030085
Arnaldo Carvalho de Melo1056d3d2011-10-26 07:11:03 -020086 return popup_menu__run(&menu);
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -030087}
88
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -030089int ui__help_window(const char *text)
Arnaldo Carvalho de Meloa9a4ab72010-05-16 21:04:27 -030090{
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;
126out_destroy_form:
127 newtFormDestroy(form);
128 return rc;
129}
130
Arnaldo Carvalho de Melo068ffaa2010-11-27 02:41:01 -0200131static const char yes[] = "Yes", no[] = "No",
132 warning_str[] = "Warning!", ok[] = "Ok";
Cyrill Gorcunova3da8e42010-11-06 11:47:24 +0300133
Arnaldo Carvalho de Melo1e6dd072010-08-10 15:58:50 -0300134bool ui__dialog_yesno(const char *msg)
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -0300135{
136 /* newtWinChoice should really be accepting const char pointers... */
Cyrill Gorcunova3da8e42010-11-06 11:47:24 +0300137 return newtWinChoice(NULL, (char *)yes, (char *)no, (char *)msg) == 1;
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -0300138}
Arnaldo Carvalho de Melo068ffaa2010-11-27 02:41:01 -0200139
140void ui__warning(const char *format, ...)
141{
142 va_list args;
143
144 va_start(args, format);
Arnaldo Carvalho de Meloa1ceb7412011-03-01 10:24:43 -0300145 if (use_browser > 0) {
146 pthread_mutex_lock(&ui__lock);
Arnaldo Carvalho de Melo068ffaa2010-11-27 02:41:01 -0200147 newtWinMessagev((char *)warning_str, (char *)ok,
148 (char *)format, args);
Arnaldo Carvalho de Meloa1ceb7412011-03-01 10:24:43 -0300149 pthread_mutex_unlock(&ui__lock);
150 } else
Arnaldo Carvalho de Melo068ffaa2010-11-27 02:41:01 -0200151 vfprintf(stderr, format, args);
152 va_end(args);
153}