]> git.ipfire.org Git - thirdparty/newt.git/blob - windows.c
added windows.c
[thirdparty/newt.git] / windows.c
1 #include <errno.h>
2 #include <newt.h>
3 #include <stdarg.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7
8 #include "errno.h"
9
10 static int newtvwindow(char * title, char * button1, char * button2,
11 char * message, va_list args) {
12 newtComponent b1, b2 = NULL, t, f, answer;
13 char * buf = NULL;
14 int size = 0;
15 int i = 0;
16 int width, height;
17 char * flowedText;
18 newtGrid grid, buttonGrid;
19
20 do {
21 size += 1000;
22 if (buf) free(buf);
23 buf = malloc(size);
24 i = vsnprintf(buf, size, message, args);
25 } while (i == size);
26
27 flowedText = newtReflowText(buf, 35, 5, 5, &width, &height);
28 if (height > 10) {
29 free(flowedText);
30 flowedText = newtReflowText(buf, 60, 5, 5, &width, &height);
31 }
32 free(buf);
33
34 b1 = newtButton(-1, -1, button1);
35 t = newtTextbox(-1, -1, width, height, NEWT_TEXTBOX_WRAP);
36 newtTextboxSetText(t, flowedText);
37 free(flowedText);
38
39 if (button2) {
40 b2 = newtButton(-1, -1, button2);
41 buttonGrid = newtCreateGrid(2, 1);
42 newtGridSetField(buttonGrid, 1, 0, NEWT_GRID_COMPONENT, button2,
43 1, 0, 0, 0, 0, 0);
44 } else {
45 buttonGrid = newtCreateGrid(1, 1);
46 }
47
48 newtGridSetField(buttonGrid, 0, 0, NEWT_GRID_COMPONENT, b1,
49 0, 0, button2 ? 1 : 0, 0, 0, 0);
50
51 grid = newtCreateGrid(1, 2);
52 newtGridSetField(grid, 0, 0, NEWT_GRID_COMPONENT, t, 0, 0, 0, 1, 0, 0);
53 newtGridSetField(grid, 0, 1, NEWT_GRID_SUBGRID, buttonGrid, 0, 0, 0, 0, 0,
54 NEWT_GRID_FLAG_GROWX);
55 newtGridWrappedWindow(grid, title);
56
57 f = newtForm(NULL, NULL, 0);
58 newtFormAddComponents(f, t, b1, NULL);
59
60 answer = newtRunForm(f);
61 newtGridFree(grid, 1);
62
63 newtFormDestroy(f);
64 newtPopWindow();
65
66 if (answer == f)
67 return 2;
68 else if (answer == b2)
69 return 1;
70
71 return 0;
72 }
73
74 int newtWinChoice(char * title, char * button1, char * button2,
75 char * message, ...) {
76 va_list args;
77 int rc;
78
79 va_start(args, message);
80 rc = newtvwindow(title, button1, button2, message, args);
81 va_end(args);
82
83 return rc;
84 }
85
86 void newtWinMessage(char * title, char * buttonText, char * text, ...) {
87 va_list args;
88
89 va_start(args, text);
90 newtvwindow(title, buttonText, NULL, text, args);
91 va_end(args);
92 }
93
94 void newtWinMessagev(char * title, char * buttonText, char * text,
95 va_list argv) {
96 newtvwindow(title, buttonText, NULL, text, argv);
97 }