]>
Commit | Line | Data |
---|---|---|
3c96e675 | 1 | #include <errno.h> |
3c96e675 | 2 | #include <stdarg.h> |
3 | #include <stdio.h> | |
4 | #include <stdlib.h> | |
5 | #include <string.h> | |
6 | ||
7 | #include "errno.h" | |
3343d0ac | 8 | #include "newt.h" |
3c96e675 | 9 | |
0989946a | 10 | static void * newtvwindow(char * title, char * button1, char * button2, |
6f481af2 | 11 | char * button3, char * message, va_list args) { |
0989946a | 12 | newtComponent b1, b2 = NULL, b3 = NULL, t, f, answer; |
3c96e675 | 13 | char * buf = NULL; |
14 | int size = 0; | |
15 | int i = 0; | |
9eec2427 | 16 | int scroll = 0; |
3c96e675 | 17 | int width, height; |
18 | char * flowedText; | |
19 | newtGrid grid, buttonGrid; | |
20 | ||
21 | do { | |
f8df34a7 ML |
22 | va_list argscopy; |
23 | ||
24 | va_copy(argscopy, args); | |
6f481af2 | 25 | size += 1000; |
26 | if (buf) free(buf); | |
27 | buf = malloc(size); | |
f8df34a7 ML |
28 | i = vsnprintf(buf, size, message, argscopy); |
29 | va_end(argscopy); | |
f22cea2f | 30 | } while (i >= size || i == -1); |
3c96e675 | 31 | |
32 | flowedText = newtReflowText(buf, 35, 5, 5, &width, &height); | |
aab1a82c | 33 | if (height > 6) { |
6f481af2 | 34 | free(flowedText); |
35 | flowedText = newtReflowText(buf, 60, 5, 5, &width, &height); | |
3c96e675 | 36 | } |
37 | free(buf); | |
38 | ||
9eec2427 | 39 | if (height > 12) { |
6f481af2 | 40 | height = 12; |
41 | scroll = NEWT_FLAG_SCROLL; | |
9eec2427 | 42 | } |
43 | t = newtTextbox(-1, -1, width, height, NEWT_TEXTBOX_WRAP | scroll); | |
3c96e675 | 44 | newtTextboxSetText(t, flowedText); |
45 | free(flowedText); | |
46 | ||
0989946a | 47 | if (button3) { |
6f481af2 | 48 | buttonGrid = newtButtonBar(button1, &b1, button2, &b2, |
49 | button3, &b3, NULL); | |
0989946a | 50 | } else if (button2) { |
6f481af2 | 51 | buttonGrid = newtButtonBar(button1, &b1, button2, &b2, NULL); |
3c96e675 | 52 | } else { |
6f481af2 | 53 | buttonGrid = newtButtonBar(button1, &b1, NULL); |
3c96e675 | 54 | } |
55 | ||
56 | newtGridSetField(buttonGrid, 0, 0, NEWT_GRID_COMPONENT, b1, | |
6f481af2 | 57 | 0, 0, button2 ? 1 : 0, 0, 0, 0); |
3c96e675 | 58 | |
8f52cd47 | 59 | grid = newtCreateGrid(1, 2); |
60 | newtGridSetField(grid, 0, 0, NEWT_GRID_COMPONENT, t, 0, 0, 0, 0, 0, 0); | |
61 | newtGridSetField(grid, 0, 1, NEWT_GRID_SUBGRID, buttonGrid, | |
6f481af2 | 62 | 0, 1, 0, 0, 0, NEWT_GRID_FLAG_GROWX); |
3c96e675 | 63 | newtGridWrappedWindow(grid, title); |
64 | ||
65 | f = newtForm(NULL, NULL, 0); | |
66 | newtFormAddComponents(f, t, b1, NULL); | |
67 | ||
cccf98b7 | 68 | if (button2) |
6f481af2 | 69 | newtFormAddComponent(f, b2); |
0989946a | 70 | if (button3) |
6f481af2 | 71 | newtFormAddComponent(f, b3); |
cccf98b7 | 72 | |
3c96e675 | 73 | answer = newtRunForm(f); |
74 | newtGridFree(grid, 1); | |
75 | ||
76 | newtFormDestroy(f); | |
77 | newtPopWindow(); | |
78 | ||
79 | if (answer == f) | |
6f481af2 | 80 | return NULL; |
0989946a | 81 | else if (answer == b1) |
6f481af2 | 82 | return button1; |
3c96e675 | 83 | else if (answer == b2) |
6f481af2 | 84 | return button2; |
3c96e675 | 85 | |
0989946a | 86 | return button3; |
3c96e675 | 87 | } |
88 | ||
89 | int newtWinChoice(char * title, char * button1, char * button2, | |
6f481af2 | 90 | char * message, ...) { |
3c96e675 | 91 | va_list args; |
0989946a | 92 | void * rc; |
3c96e675 | 93 | |
94 | va_start(args, message); | |
0989946a | 95 | rc = newtvwindow(title, button1, button2, NULL, message, args); |
3c96e675 | 96 | va_end(args); |
97 | ||
0989946a | 98 | if (rc == button1) |
6f481af2 | 99 | return 1; |
63d30b64 | 100 | else if (rc == button2) |
6f481af2 | 101 | return 2; |
0989946a | 102 | |
63d30b64 | 103 | return 0; |
3c96e675 | 104 | } |
105 | ||
106 | void newtWinMessage(char * title, char * buttonText, char * text, ...) { | |
107 | va_list args; | |
108 | ||
109 | va_start(args, text); | |
0989946a | 110 | newtvwindow(title, buttonText, NULL, NULL, text, args); |
3c96e675 | 111 | va_end(args); |
112 | } | |
113 | ||
114 | void newtWinMessagev(char * title, char * buttonText, char * text, | |
6f481af2 | 115 | va_list argv) { |
0989946a | 116 | newtvwindow(title, buttonText, NULL, NULL, text, argv); |
117 | } | |
118 | ||
119 | int newtWinTernary(char * title, char * button1, char * button2, | |
6f481af2 | 120 | char * button3, char * message, ...) { |
0989946a | 121 | va_list args; |
122 | void * rc; | |
123 | ||
124 | va_start(args, message); | |
125 | rc = newtvwindow(title, button1, button2, button3, message, args); | |
126 | va_end(args); | |
127 | ||
128 | if (rc == button1) | |
6f481af2 | 129 | return 1; |
0989946a | 130 | else if (rc == button2) |
6f481af2 | 131 | return 2; |
0989946a | 132 | else if (rc == button3) |
6f481af2 | 133 | return 3; |
0989946a | 134 | |
135 | return 0; | |
3c96e675 | 136 | } |
63d30b64 | 137 | |
63d30b64 | 138 | int newtWinMenu(char * title, char * text, int suggestedWidth, int flexDown, |
6f481af2 | 139 | int flexUp, int maxListHeight, char ** items, int * listItem, |
140 | char * button1, ...) { | |
63d30b64 | 141 | newtComponent textbox, listbox, result, form; |
142 | va_list args; | |
d78245ab | 143 | newtComponent *buttons = NULL; |
63d30b64 | 144 | newtGrid grid, buttonBar; |
d78245ab | 145 | size_t totalButtons = 0, numButtons = 0; |
63d30b64 | 146 | int i, rc; |
147 | int needScroll; | |
148 | char * buttonName; | |
149 | ||
abedc66c | 150 | textbox = newtTextboxReflowed(-1, -1, text, suggestedWidth, flexDown, |
6f481af2 | 151 | flexUp, 0); |
63d30b64 | 152 | |
153 | for (i = 0; items[i]; i++) ; | |
154 | if (i < maxListHeight) maxListHeight = i; | |
155 | needScroll = i > maxListHeight; | |
156 | ||
157 | listbox = newtListbox(-1, -1, maxListHeight, | |
6f481af2 | 158 | (needScroll ? NEWT_FLAG_SCROLL : 0) | NEWT_FLAG_RETURNEXIT); |
63d30b64 | 159 | for (i = 0; items[i]; i++) { |
8bec7d99 | 160 | newtListboxAddEntry(listbox, items[i], (void *)(long) i); |
63d30b64 | 161 | } |
162 | ||
163 | newtListboxSetCurrent(listbox, *listItem); | |
164 | ||
63d30b64 | 165 | va_start(args, button1); |
d78245ab | 166 | for (buttonName = button1; buttonName; buttonName = va_arg(args, char *)) |
167 | ++totalButtons; | |
168 | va_end(args); | |
63d30b64 | 169 | |
3341bdc5 | 170 | buttons = (newtComponent *)alloca(sizeof(newtComponent)*(totalButtons)); |
d78245ab | 171 | va_start(args, button1); |
172 | for (buttonName = button1; buttonName; buttonName = va_arg(args, char *)) | |
173 | buttons[numButtons++] = newtButton(-1, -1, buttonName); | |
174 | va_end(args); | |
63d30b64 | 175 | |
176 | buttonBar = newtCreateGrid(numButtons, 1); | |
177 | for (i = 0; i < numButtons; i++) { | |
6f481af2 | 178 | newtGridSetField(buttonBar, i, 0, NEWT_GRID_COMPONENT, |
179 | buttons[i], | |
180 | i ? 1 : 0, 0, 0, 0, 0, 0); | |
63d30b64 | 181 | } |
182 | ||
8f52cd47 | 183 | grid = newtGridSimpleWindow(textbox, listbox, buttonBar); |
63d30b64 | 184 | newtGridWrappedWindow(grid, title); |
185 | ||
186 | form = newtForm(NULL, 0, 0); | |
187 | newtGridAddComponentsToForm(grid, form, 1); | |
188 | newtGridFree(grid, 1); | |
189 | ||
190 | result = newtRunForm(form); | |
191 | ||
192 | *listItem = ((long) newtListboxGetCurrent(listbox)); | |
193 | ||
f5cca18d ML |
194 | for (rc = 0; rc < numButtons && result != buttons[rc]; rc++) |
195 | ; | |
63d30b64 | 196 | if (rc == numButtons) |
6f481af2 | 197 | rc = 0; /* F12 or return-on-exit (which are the same for us) */ |
63d30b64 | 198 | else |
6f481af2 | 199 | rc++; |
63d30b64 | 200 | |
201 | newtFormDestroy(form); | |
202 | newtPopWindow(); | |
203 | ||
204 | return rc; | |
205 | } | |
abedc66c | 206 | |
abedc66c | 207 | int newtWinEntries(char * title, char * text, int suggestedWidth, int flexDown, |
6f481af2 | 208 | int flexUp, int dataWidth, |
209 | struct newtWinEntry * items, char * button1, ...) { | |
d78245ab | 210 | newtComponent *buttons, result, form, textw; |
abedc66c | 211 | newtGrid grid, buttonBar, subgrid; |
212 | int numItems; | |
213 | int rc, i; | |
d78245ab | 214 | size_t numButtons = 0, totalButtons = 0; |
abedc66c | 215 | char * buttonName; |
216 | va_list args; | |
217 | ||
218 | textw = newtTextboxReflowed(-1, -1, text, suggestedWidth, flexDown, | |
6f481af2 | 219 | flexUp, 0); |
abedc66c | 220 | |
221 | for (numItems = 0; items[numItems].text; numItems++); | |
222 | ||
d78245ab | 223 | buttonName = button1; |
abedc66c | 224 | va_start(args, button1); |
d78245ab | 225 | for (buttonName = button1; buttonName; buttonName = va_arg(args, char *)) |
226 | ++totalButtons; | |
227 | va_end(args); | |
228 | ||
3341bdc5 | 229 | buttons = (newtComponent *)alloca(sizeof(newtComponent)*(totalButtons)); |
d78245ab | 230 | va_start(args, button1); |
231 | for (buttonName = button1; buttonName; buttonName = va_arg(args, char *)) | |
232 | buttons[numButtons++] = newtButton(-1, -1, buttonName); | |
233 | va_end(args); | |
abedc66c | 234 | |
235 | buttonBar = newtCreateGrid(numButtons, 1); | |
236 | for (i = 0; i < numButtons; i++) { | |
6f481af2 | 237 | newtGridSetField(buttonBar, i, 0, NEWT_GRID_COMPONENT, |
238 | buttons[i], | |
239 | i ? 1 : 0, 0, 0, 0, 0, 0); | |
abedc66c | 240 | } |
241 | ||
242 | subgrid = newtCreateGrid(2, numItems); | |
243 | for (i = 0; i < numItems; i++) { | |
6f481af2 | 244 | newtGridSetField(subgrid, 0, i, NEWT_GRID_COMPONENT, |
245 | newtLabel(-1, -1, items[i].text), | |
246 | 0, 0, 0, 0, NEWT_ANCHOR_LEFT, 0); | |
247 | newtGridSetField(subgrid, 1, i, NEWT_GRID_COMPONENT, | |
248 | newtEntry(-1, -1, items[i].value ? | |
249 | *items[i].value : NULL, dataWidth, | |
f4275c66 | 250 | (const char **)items[i].value, items[i].flags), |
6f481af2 | 251 | 1, 0, 0, 0, 0, 0); |
abedc66c | 252 | } |
253 | ||
254 | grid = newtCreateGrid(1, 3); | |
255 | form = newtForm(NULL, 0, 0); | |
256 | newtGridSetField(grid, 0, 0, NEWT_GRID_COMPONENT, textw, | |
6f481af2 | 257 | 0, 0, 0, 0, NEWT_ANCHOR_LEFT, 0); |
abedc66c | 258 | newtGridSetField(grid, 0, 1, NEWT_GRID_SUBGRID, subgrid, |
6f481af2 | 259 | 0, 1, 0, 0, 0, 0); |
abedc66c | 260 | newtGridSetField(grid, 0, 2, NEWT_GRID_SUBGRID, buttonBar, |
6f481af2 | 261 | 0, 1, 0, 0, 0, NEWT_GRID_FLAG_GROWX); |
abedc66c | 262 | newtGridAddComponentsToForm(grid, form, 1); |
263 | newtGridWrappedWindow(grid, title); | |
264 | newtGridFree(grid, 1); | |
265 | ||
266 | result = newtRunForm(form); | |
267 | ||
268 | for (rc = 0; rc < numItems; rc++) | |
6f481af2 | 269 | *items[rc].value = strdup(*items[rc].value); |
abedc66c | 270 | |
271 | for (rc = 0; result != buttons[rc] && rc < numButtons; rc++); | |
272 | if (rc == numButtons) | |
6f481af2 | 273 | rc = 0; /* F12 */ |
abedc66c | 274 | else |
6f481af2 | 275 | rc++; |
abedc66c | 276 | |
277 | newtFormDestroy(form); | |
278 | newtPopWindow(); | |
279 | ||
280 | return rc; | |
281 | } |