~ubuntu-branches/ubuntu/dapper/newt/dapper-security

« back to all changes in this revision

Viewing changes to windows.c

  • Committer: Bazaar Package Importer
  • Author(s): Junichi Uekawa
  • Date: 2002-03-31 09:38:18 UTC
  • Revision ID: james.westby@ubuntu.com-20020331093818-t3cla7103r07qnyw
Tags: upstream-0.50.17
ImportĀ upstreamĀ versionĀ 0.50.17

Show diffs side-by-side

added added

removed removed

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