blob: 04600e26ceea21d08b701570494c5e72210aaa9e [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"
12#include "util.h"
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -030013
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -030014newtComponent newt_form__new(void);
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -030015
Arnaldo Carvalho de Melo7081e082010-03-12 10:48:12 -030016static void newt_form__set_exit_keys(newtComponent self)
17{
Arnaldo Carvalho de Meloa308f3a2010-05-16 20:29:38 -030018 newtFormAddHotKey(self, NEWT_KEY_LEFT);
Arnaldo Carvalho de Melo7081e082010-03-12 10:48:12 -030019 newtFormAddHotKey(self, NEWT_KEY_ESCAPE);
20 newtFormAddHotKey(self, 'Q');
21 newtFormAddHotKey(self, 'q');
22 newtFormAddHotKey(self, CTRL('c'));
23}
24
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -030025newtComponent newt_form__new(void)
Arnaldo Carvalho de Melo7081e082010-03-12 10:48:12 -030026{
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 Melo1e6dd072010-08-10 15:58:50 -030033int ui__popup_menu(int argc, char * const argv[])
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -030034{
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 Melo7f826452010-05-10 10:51:25 -030046 newtFormAddComponent(form, listbox);
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -030047
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();
62out_destroy_form:
63 newtFormDestroy(form);
64 return rc;
65}
66
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -030067int ui__help_window(const char *text)
Arnaldo Carvalho de Meloa9a4ab72010-05-16 21:04:27 -030068{
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;
104out_destroy_form:
105 newtFormDestroy(form);
106 return rc;
107}
108
Arnaldo Carvalho de Melo1e6dd072010-08-10 15:58:50 -0300109bool ui__dialog_yesno(const char *msg)
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -0300110{
111 /* newtWinChoice should really be accepting const char pointers... */
112 char yes[] = "Yes", no[] = "No";
Arnaldo Carvalho de Meloc0ed55d2010-04-05 12:04:23 -0300113 return newtWinChoice(NULL, yes, no, (char *)msg) == 1;
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -0300114}