04600e26ceea21d08b701570494c5e72210aaa9e
[linux-2.6.git] / tools / perf / util / ui / util.c
1 #include <newt.h>
2 #include <signal.h>
3 #include <stdio.h>
4 #include <stdbool.h>
5 #include <string.h>
6 #include <sys/ttydefaults.h>
7
8 #include "../cache.h"
9 #include "../debug.h"
10 #include "browser.h"
11 #include "helpline.h"
12 #include "util.h"
13
14 newtComponent newt_form__new(void);
15
16 static void newt_form__set_exit_keys(newtComponent self)
17 {
18         newtFormAddHotKey(self, NEWT_KEY_LEFT);
19         newtFormAddHotKey(self, NEWT_KEY_ESCAPE);
20         newtFormAddHotKey(self, 'Q');
21         newtFormAddHotKey(self, 'q');
22         newtFormAddHotKey(self, CTRL('c'));
23 }
24
25 newtComponent newt_form__new(void)
26 {
27         newtComponent self = newtForm(NULL, NULL, 0);
28         if (self)
29                 newt_form__set_exit_keys(self);
30         return self;
31 }
32
33 int ui__popup_menu(int argc, char * const argv[])
34 {
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
46         newtFormAddComponent(form, listbox);
47
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();
62 out_destroy_form:
63         newtFormDestroy(form);
64         return rc;
65 }
66
67 int ui__help_window(const char *text)
68 {
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;
104 out_destroy_form:
105         newtFormDestroy(form);
106         return rc;
107 }
108
109 bool ui__dialog_yesno(const char *msg)
110 {
111         /* newtWinChoice should really be accepting const char pointers... */
112         char yes[] = "Yes", no[] = "No";
113         return newtWinChoice(NULL, yes, no, (char *)msg) == 1;
114 }