File: windows.c

package info (click to toggle)
newt 0.21-8
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 296 kB
  • ctags: 753
  • sloc: ansic: 4,813; python: 312; makefile: 179; sh: 2
file content (127 lines) | stat: -rw-r--r-- 3,034 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "errno.h"
#include "newt.h"

static void * newtvwindow(char * title, char * button1, char * button2, 
		       char * button3, char * message, va_list args) {
    newtComponent b1, b2 = NULL, b3 = NULL, t, f, answer;
    char * buf = NULL;
    int size = 0;
    int i = 0;
    int width, height;
    char * flowedText;
    newtGrid grid, buttonGrid;

    do {
	size += 1000;
	if (buf) free(buf);
	buf = malloc(size);
	i = vsnprintf(buf, size, message, args);
    } while (i == size);

    flowedText = newtReflowText(buf, 35, 5, 5, &width, &height);
    if (height > 6) {
	free(flowedText);
	flowedText = newtReflowText(buf, 60, 5, 5, &width, &height);
    }
    free(buf);

    t = newtTextbox(-1, -1, width, height, NEWT_TEXTBOX_WRAP);
    newtTextboxSetText(t, flowedText);
    free(flowedText);

    if (button3) {
	buttonGrid = newtButtonBar(button1, &b1, button2, &b2, 
				   button3, &b3, NULL);
    } else if (button2) {
	buttonGrid = newtButtonBar(button1, &b1, button2, &b2, NULL);
    } else {
	buttonGrid = newtButtonBar(button1, &b1, NULL);
    }

    newtGridSetField(buttonGrid, 0, 0, NEWT_GRID_COMPONENT, b1, 
		     0, 0, button2 ? 1 : 0, 0, 0, 0);

    grid = newtCreateGrid(1, 2);
    newtGridSetField(grid, 0, 0, NEWT_GRID_COMPONENT, t, 0, 0, 0, 1, 0, 0);
    newtGridSetField(grid, 0, 1, NEWT_GRID_SUBGRID, buttonGrid, 0, 0, 0, 0, 0, 
			NEWT_GRID_FLAG_GROWX);
    newtGridWrappedWindow(grid, title);

    f = newtForm(NULL, NULL, 0);
    newtFormAddComponents(f, t, b1, NULL);

    if (button2)
	newtFormAddComponent(f, b2);
    if (button3)
	newtFormAddComponent(f, b3);

    answer = newtRunForm(f);
    newtGridFree(grid, 1);
 
    newtFormDestroy(f);
    newtPopWindow();

    if (answer == f)
	return NULL;
    else if (answer == b1)
	return button1;
    else if (answer == b2)
	return button2;

    return button3;
}

int newtWinChoice(char * title, char * button1, char * button2, 
		   char * message, ...) {
    va_list args;
    void * rc;

    va_start(args, message);
    rc = newtvwindow(title, button1, button2, NULL, message, args);
    va_end(args);

    if (rc == button1)
	return 0;
    else if (rc == button2)
	return 1;

    return 2;
}

void newtWinMessage(char * title, char * buttonText, char * text, ...) {
    va_list args;

    va_start(args, text);
    newtvwindow(title, buttonText, NULL, NULL, text, args);
    va_end(args);
}

void newtWinMessagev(char * title, char * buttonText, char * text, 
		     va_list argv) {
    newtvwindow(title, buttonText, NULL, NULL, text, argv);
}

int newtWinTernary(char * title, char * button1, char * button2, 
		   char * button3, char * message, ...) {
    va_list args;
    void * rc;

    va_start(args, message);
    rc = newtvwindow(title, button1, button2, button3, message, args);
    va_end(args);

    if (rc == button1)
	return 1;
    else if (rc == button2)
	return 2;
    else if (rc == button3)
	return 3;

    return 0;
}