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

« back to all changes in this revision

Viewing changes to debian/patches/65_autosize.patch

  • Committer: Bazaar Package Importer
  • Author(s): Colin Watson
  • Date: 2005-03-22 12:44:37 UTC
  • mfrom: (2.1.1 sarge)
  • Revision ID: james.westby@ubuntu.com-20050322124437-nuhl0pqjcijjno9z
Tags: 0.51.6-20ubuntu3
Add Xhosa translation (thanks, Adi Attar).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
diff -ruN newt-0.51.6-old/dialogboxes.c newt-0.51.6/dialogboxes.c
 
2
--- newt-0.51.6-old/dialogboxes.c       2004-03-09 12:38:14.000000000 +0000
 
3
+++ newt-0.51.6/dialogboxes.c   2004-03-09 12:38:15.000000000 +0000
 
4
@@ -6,6 +6,7 @@
 
5
 #include <string.h>
 
6
 #include <stdlib.h>
 
7
 #include <unistd.h>
 
8
+#include <slang.h>
 
9
 
 
10
 #include "nls.h"
 
11
 #include "dialogboxes.h"
 
12
@@ -18,6 +19,17 @@
 
13
 
 
14
 /* globals -- ick */
 
15
 static int buttonHeight = 1;
 
16
+
 
17
+int max (int a, int b)
 
18
+{
 
19
+       return (a > b) ? a : b;
 
20
+}
 
21
+
 
22
+int min (int a, int b)
 
23
+{
 
24
+       return ( a < b) ? a : b ;
 
25
+}
 
26
 
27
 static newtComponent (*makeButton)(int left, int right, const char * text) = 
 
28
                newtCompactButton;
 
29
 
 
30
@@ -179,11 +191,12 @@
 
31
     int allocedItems = 5;
 
32
     int i, top;
 
33
     int rc = DLG_OKAY;
 
34
-    char buf[MAXBUF], format[MAXFOTMAT];
 
35
+    char buf[MAXBUF], format[MAXFORMAT];
 
36
     int maxTagWidth = 0;
 
37
     int maxTextWidth = 0;
 
38
     int defItem = -1;
 
39
     int scrollFlag;
 
40
+    int lineWidth, textWidth, tagWidth;
 
41
     struct {
 
42
        const char * text;
 
43
        const char * tag;
 
44
@@ -235,16 +248,35 @@
 
45
        i = 2;
 
46
     }
 
47
 
 
48
-    listBox = newtListbox(3 + ((width - 10 - maxTagWidth - maxTextWidth - i) 
 
49
-                                       / 2),
 
50
-                         top + 1, listHeight, 
 
51
-                           NEWT_FLAG_RETURNEXIT | scrollFlag);
 
52
-
 
53
-    snprintf(format, MAXFORMAT, "%%-%ds  %%s", maxTagWidth);
 
54
-    for (i = 0; i < numItems; i++) {
 
55
-       snprintf(buf, MAXBUF, format, itemInfo[i].tag, itemInfo[i].text);
 
56
-       newtListboxAddEntry(listBox, buf, (void *) i);
 
57
-    }
 
58
+    lineWidth = min(maxTagWidth + maxTextWidth + i, SLtt_Screen_Cols - 10);
 
59
+    listBox = newtListbox( (width - lineWidth) / 2 , top + 1, listHeight,
 
60
+                          NEWT_FLAG_RETURNEXIT | scrollFlag);
 
61
+
 
62
+    textWidth = maxTextWidth;
 
63
+    tagWidth = maxTagWidth;
 
64
+    if (maxTextWidth == 0) {
 
65
+        tagWidth = lineWidth;
 
66
+    } else {
 
67
+       if (maxTextWidth + maxTagWidth + i > lineWidth)
 
68
+               tagWidth = textWidth = (lineWidth / 2) - 2;
 
69
+       else {
 
70
+               tagWidth++;
 
71
+               textWidth++;
 
72
+       }
 
73
+    }
 
74
+
 
75
+    if (!(flags & FLAG_NOTAGS)) {
 
76
+       snprintf(format, MAXFORMAT, "%%-%d.%ds  %%.%ds",tagWidth, tagWidth, textWidth  );
 
77
+       for (i = 0; i < numItems; i++) {
 
78
+           snprintf(buf, MAXBUF, format, itemInfo[i].tag, itemInfo[i].text);
 
79
+           newtListboxAddEntry(listBox, buf, (void *) i);
 
80
+       }
 
81
+     } else {
 
82
+        for (i = 0; i < numItems; i++) {
 
83
+           snprintf(buf, MAXBUF, "%s", itemInfo[i].text);
 
84
+           newtListboxAddEntry(listBox, buf, (void *) i);
 
85
+      }
 
86
+   }
 
87
 
 
88
     if (defItem != -1)
 
89
        newtListboxSetCurrent (listBox, defItem);
 
90
diff -ruN newt-0.51.6-old/dialogboxes.h newt-0.51.6/dialogboxes.h
 
91
--- newt-0.51.6-old/dialogboxes.h       2004-03-09 12:38:14.000000000 +0000
 
92
+++ newt-0.51.6/dialogboxes.h   2004-03-09 12:38:15.000000000 +0000
 
93
@@ -19,6 +19,9 @@
 
94
 #define DLG_CANCEL             1
 
95
 #define DLG_ESCAPE             2
 
96
 
 
97
+int min(int a, int b);
 
98
+int max(int a, int b);
 
99
+
 
100
 int messageBox(const char * text, int height, int width, int type, int flags);
 
101
 int checkList(const char * text, int height, int width, poptContext optCon,
 
102
                int useRadio, int flags, const char *** selections);
 
103
diff -ruN newt-0.51.6-old/whiptail.c newt-0.51.6/whiptail.c
 
104
--- newt-0.51.6-old/whiptail.c  2004-03-09 12:38:14.000000000 +0000
 
105
+++ newt-0.51.6/whiptail.c      2004-03-09 12:38:15.000000000 +0000
 
106
@@ -1,5 +1,3 @@
 
107
-/* a reasonable dialog */
 
108
-
 
109
 #include "config.h"
 
110
 #include <fcntl.h>
 
111
 #include <popt.h>
 
112
@@ -8,6 +6,7 @@
 
113
 #include <stdlib.h>
 
114
 #include <unistd.h>
 
115
 #include <wchar.h>
 
116
+#include <slang.h>
 
117
 
 
118
 #include "nls.h"
 
119
 #include "dialogboxes.h"
 
120
@@ -35,6 +34,227 @@
 
121
     exit(DLG_ERROR);
 
122
 }
 
123
 
 
124
+/* FIXME Copied from newt.c
 
125
+ * Place somewhere better -- dialogboxes? -- amck
 
126
+ */
 
127
+int wstrlen(const char *str, int len) {
 
128
+       mbstate_t ps;
 
129
+       wchar_t tmp;
 
130
+       int nchars = 0;
 
131
+
 
132
+       if (!str) return 0;
 
133
+      if (!len) return 0;
 
134
+       if (len < 0) len = strlen(str);
 
135
+      memset(&ps,0,sizeof(mbstate_t));
 
136
+       while (len > 0) {
 
137
+               int x,y;
 
138
+
 
139
+               x = mbrtowc(&tmp,str,len,&ps);
 
140
+               if (x >0) {
 
141
+                       str += x;
 
142
+                       len -= x;
 
143
+                       y = wcwidth(tmp);
 
144
+                       if (y>0)
 
145
+                         nchars+=y;
 
146
+               } else break;
 
147
+       }
 
148
+       return nchars;
 
149
+}
 
150
+
 
151
+/*
 
152
+ * The value of *width is increased if it is not as large as the width of
 
153
+ * the line.
 
154
+ */
 
155
+static const char * lineWidth(int * width, const char * line, int *chrs)
 
156
+{
 
157
+    const char *    s = line;
 
158
+
 
159
+    if ( line == NULL )
 
160
+       return 0;
 
161
+
 
162
+   while ( *s != '\0' && *s != '\n' )
 
163
+       s++;
 
164
+
 
165
+    if ( *s == '\n' )
 
166
+       s++;
 
167
+
 
168
+    *chrs = wstrlen (line, s - line );
 
169
+    *width = max(*width, *chrs);
 
170
+
 
171
+    return s;
 
172
+}
 
173
+
 
174
+
 
175
+/*
 
176
+ * cleanNewlines
 
177
+ * Handle newlines in text. Hack.
 
178
+ */
 
179
+void cleanNewlines (char *text)
 
180
+{
 
181
+       char *p = text;
 
182
+       while (*p) {
 
183
+               if ((*p == '\\') && (*(p+1) == 'n')) {
 
184
+                       *p = '\n';
 
185
+                       *(p+1) = ' ';
 
186
+               } else  {
 
187
+                       p++;
 
188
+               }
 
189
+       }
 
190
+}
 
191
+
 
192
+/*
 
193
+ * The height of a text string is added to height, and width is increased
 
194
+ * if it is not big enough to store the text string.
 
195
+ */
 
196
+static const char * textSize(int * height, int * width,
 
197
+                            int maxWidth,
 
198
+                            const char * text)
 
199
+{
 
200
+    int h = 0;
 
201
+    int w = 0;
 
202
+    int chrs = 0;
 
203
+
 
204
+
 
205
+    if ( text == NULL )
 
206
+       return 0;
 
207
+
 
208
+   while ( *text != '\0' ) {
 
209
+       h++;
 
210
+       text = lineWidth(width, text, &chrs);
 
211
+       /* Allow for text overflowing. May overestimate a bit */
 
212
+       h += chrs /  maxWidth;
 
213
+    }
 
214
+
 
215
+    h += 2;
 
216
+   w += 2;
 
217
+
 
218
+    *height += h;
 
219
+    *width += w;
 
220
+
 
221
+    *width = min(*width, maxWidth);
 
222
+    return text;
 
223
+}
 
224
+
 
225
+/*
 
226
+ * Add space for buttons.
 
227
+ * NOTE: when this is internationalized, the button width might change.
 
228
+ */
 
229
+static void spaceForButtons(int * height, int * width, int count, int full) {
 
230
+    /* Make space for the buttons */
 
231
+    if ( full ) {
 
232
+       *height += 4;
 
233
+       if ( count == 1 )
 
234
+           *width = max(*width, 7);
 
235
+       else
 
236
+           *width = max(*width, 20);
 
237
+    }
 
238
+    else {
 
239
+       *height += 2;
 
240
+       if ( count == 1 )
 
241
+           *width = max(*width, 7);
 
242
+       else
 
243
+           *width = max(*width, 19);
 
244
+    }
 
245
+}
 
246
+
 
247
+static int menuSize(int * height, int * width, enum mode mode,
 
248
+                   poptContext options) {
 
249
+    char **    argv = poptGetArgs(options);
 
250
+    char * *    items = argv;
 
251
+    int         h = 0;
 
252
+    int         tagWidth = 0;
 
253
+    int         descriptionWidth = 0;
 
254
+    int         overhead = 10;
 
255
+    static char buf[20];
 
256
+
 
257
+    if ( argv == 0 || *argv == 0 )
 
258
+       return 0;
 
259
+
 
260
+    argv++;
 
261
+    if ( mode == MODE_MENU )
 
262
+       overhead = 5;
 
263
+
 
264
+    while ( argv[0] != 0 && argv[1] ) {
 
265
+       tagWidth = max(tagWidth, strlen(argv[0]));
 
266
+       descriptionWidth = max(descriptionWidth, strlen(argv[1]));
 
267
+
 
268
+       if ( mode == MODE_MENU )
 
269
+           argv += 2;
 
270
+       else
 
271
+          argv += 3;
 
272
+       h++;
 
273
+    }
 
274
+
 
275
+    *width = max(*width, tagWidth + descriptionWidth + overhead);
 
276
+    *width = min(*width, SLtt_Screen_Cols);
 
277
+
 
278
+    h = min(h, SLtt_Screen_Rows - *height - 4);
 
279
+    *height = *height + h + 1;
 
280
+    sprintf(buf, "%d", h);
 
281
+   *items = buf;
 
282
+    return 0;
 
283
+}
 
284
+
 
285
+/*
 
286
+ * Guess the size of a window, given what will be displayed within it.
 
287
+ */
 
288
+static void guessSize(int * height, int * width, enum mode mode,
 
289
+                     int * flags, int fullButtons,
 
290
+                     const char * title, const char * text,
 
291
+                     poptContext options) {
 
292
+
 
293
+    int w = 0, h = 0, chrs = 0;
 
294
+
 
295
+    textSize(&h, &w, SLtt_Screen_Cols -4 , text);     /* Width and height for text */
 
296
+    lineWidth(&w, title, &chrs);             /* Width for title */
 
297
+
 
298
+    if ( w > 0 )
 
299
+       w += 4;
 
300
+
 
301
+    switch ( mode ) {
 
302
+       case MODE_CHECKLIST:
 
303
+       case MODE_RADIOLIST:
 
304
+       case MODE_MENU:
 
305
+           spaceForButtons(&h, &w, *flags & FLAG_NOCANCEL ? 1 : 2,
 
306
+            fullButtons);
 
307
+           menuSize(&h, &w, mode, options);
 
308
+               break;
 
309
+       case MODE_YESNO:
 
310
+       case MODE_MSGBOX:
 
311
+           spaceForButtons(&h, &w, 1, fullButtons);
 
312
+           break;
 
313
+       case MODE_INPUTBOX:
 
314
+           spaceForButtons(&h, &w, *flags & FLAG_NOCANCEL ? 1 : 2,
 
315
+            fullButtons);
 
316
+           h += 1;
 
317
+           break;
 
318
+       case MODE_GAUGE:
 
319
+           h += 2;
 
320
+           break;
 
321
+       case MODE_NONE:
 
322
+          break;
 
323
+       default:
 
324
+               break;
 
325
+    };
 
326
+
 
327
+    /*
 
328
+     * Fixed window-border overhead.
 
329
+     * NOTE: This will change if we add a way to turn off drop-shadow and/or
 
330
+     * box borders. That would be desirable for display-sized screens.
 
331
+     */
 
332
+    w += 2;
 
333
+    h += 2;
 
334
+
 
335
+   if ( h > SLtt_Screen_Rows - 1 ) {
 
336
+       h = SLtt_Screen_Rows - 1;
 
337
+       *flags |= FLAG_SCROLL_TEXT;
 
338
+       w += 2; /* Add width of slider - is this right? */
 
339
+    }
 
340
+
 
341
+    *width = min(max(*width, w), SLtt_Screen_Cols);
 
342
+    *height = max(*height, h);
 
343
+}
 
344
+
 
345
 char *
 
346
 readTextFile(const char * filename)
 
347
 {
 
348
@@ -79,6 +299,7 @@
 
349
     int flags = 0;
 
350
     int defaultNo = 0;
 
351
     int separateOutput = 0;
 
352
+    int fullButtons = 0;
 
353
     int outputfd = 2;
 
354
     FILE *output = stderr;
 
355
     const char * result;
 
356
@@ -158,6 +379,7 @@
 
357
            break;
 
358
 
 
359
          case OPT_FULLBUTTONS:
 
360
+           fullButtons = 1;
 
361
            useFullButtons(1);
 
362
            break;
 
363
 
 
364
@@ -195,7 +417,7 @@
 
365
 
 
366
     if (!(text = poptGetArg(optCon))) usage();
 
367
 
 
368
-    if ( mode == MODE_TEXTBOX ) text = readTextFile(text);
 
369
+    if  (mode == MODE_TEXTBOX ) text = readTextFile(text);
 
370
 
 
371
     if (!(nextArg = poptGetArg(optCon))) usage();
 
372
     height = strtoul(nextArg, &end, 10);
 
373
@@ -213,10 +435,18 @@
 
374
 
 
375
     newtInit();
 
376
     newtCls();
 
377
+
 
378
+    cleanNewlines(text);
 
379
+
 
380
+    if ( height <= 0 || width <= 0 )
 
381
+       guessSize(&height, &width, mode, &flags, fullButtons, title, text,
 
382
+                 optCon);
 
383
+
 
384
     width -= 2;
 
385
     height -= 2;
 
386
-    newtOpenWindow((80 - width) / 2, (24 - height) / 2, width, height, title);
 
387
 
 
388
+    newtOpenWindow((SLtt_Screen_Cols - width) / 2, 
 
389
+                   (SLtt_Screen_Rows - height) / 2, width, height, title);
 
390
     if (backtitle)
 
391
        newtDrawRootText(0, 0, backtitle);
 
392