blob: fdf1fc8f08bc83b2300311870f9b68ee559ef7c6 [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"
11#include "helpline.h"
Arnaldo Carvalho de Meloa1ceb7412011-03-01 10:24:43 -030012#include "ui.h"
Arnaldo Carvalho de Melo1e6dd072010-08-10 15:58:50 -030013#include "util.h"
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -030014
Arnaldo Carvalho de Melo7081e082010-03-12 10:48:12 -030015static void newt_form__set_exit_keys(newtComponent self)
16{
Arnaldo Carvalho de Meloa308f3a2010-05-16 20:29:38 -030017 newtFormAddHotKey(self, NEWT_KEY_LEFT);
Arnaldo Carvalho de Melo7081e082010-03-12 10:48:12 -030018 newtFormAddHotKey(self, NEWT_KEY_ESCAPE);
19 newtFormAddHotKey(self, 'Q');
20 newtFormAddHotKey(self, 'q');
21 newtFormAddHotKey(self, CTRL('c'));
22}
23
Arnaldo Carvalho de Melo4c1c9522010-08-12 12:37:51 -030024static newtComponent newt_form__new(void)
Arnaldo Carvalho de Melo7081e082010-03-12 10:48:12 -030025{
26 newtComponent self = newtForm(NULL, NULL, 0);
27 if (self)
28 newt_form__set_exit_keys(self);
29 return self;
30}
31
Arnaldo Carvalho de Melo1e6dd072010-08-10 15:58:50 -030032int ui__popup_menu(int argc, char * const argv[])
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -030033{
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
Arnaldo Carvalho de Melo7f826452010-05-10 10:51:25 -030045 newtFormAddComponent(form, listbox);
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -030046
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();
61out_destroy_form:
62 newtFormDestroy(form);
63 return rc;
64}
65
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -030066int ui__help_window(const char *text)
Arnaldo Carvalho de Meloa9a4ab72010-05-16 21:04:27 -030067{
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;
103out_destroy_form:
104 newtFormDestroy(form);
105 return rc;
106}
107
Arnaldo Carvalho de Melo068ffaa2010-11-27 02:41:01 -0200108static const char yes[] = "Yes", no[] = "No",
109 warning_str[] = "Warning!", ok[] = "Ok";
Cyrill Gorcunova3da8e42010-11-06 11:47:24 +0300110
Arnaldo Carvalho de Melo1e6dd072010-08-10 15:58:50 -0300111bool ui__dialog_yesno(const char *msg)
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -0300112{
113 /* newtWinChoice should really be accepting const char pointers... */
Cyrill Gorcunova3da8e42010-11-06 11:47:24 +0300114 return newtWinChoice(NULL, (char *)yes, (char *)no, (char *)msg) == 1;
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -0300115}
Arnaldo Carvalho de Melo068ffaa2010-11-27 02:41:01 -0200116
117void ui__warning(const char *format, ...)
118{
119 va_list args;
120
121 va_start(args, format);
Arnaldo Carvalho de Meloa1ceb7412011-03-01 10:24:43 -0300122 if (use_browser > 0) {
123 pthread_mutex_lock(&ui__lock);
Arnaldo Carvalho de Melo068ffaa2010-11-27 02:41:01 -0200124 newtWinMessagev((char *)warning_str, (char *)ok,
125 (char *)format, args);
Arnaldo Carvalho de Meloa1ceb7412011-03-01 10:24:43 -0300126 pthread_mutex_unlock(&ui__lock);
127 } else
Arnaldo Carvalho de Melo068ffaa2010-11-27 02:41:01 -0200128 vfprintf(stderr, format, args);
129 va_end(args);
130}