-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcairoui-main.c
235 lines (196 loc) · 4.54 KB
/
cairoui-main.c
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
* cairoui-main.c
*
* a test program for cairoui
* show a square; change color with 'c', position with 'p'
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <cairo.h>
#include "cairoio.h"
#include "cairoui.h"
#include "cairoio-fb.h"
#include "cairoio-x11.h"
/*************************** CALLBACK DATA ************************/
/*
* callback data
*/
struct cairoui_cb {
int immediate;
char help[80];
int color;
int x;
int y;
};
/******************************* WINDOWS **************************/
/*
* window names
*/
enum window {
WINDOW_DOCUMENT,
WINDOW_COLOR,
WINDOW_POSITION
};
/*
* document
*/
int document(int c, struct cairoui *cairoui) {
(void) cairoui;
switch (c) {
case 'c':
return WINDOW_COLOR;
case 'p':
return WINDOW_POSITION;
case 'q':
return CAIROUI_EXIT;
default:
return WINDOW_DOCUMENT;
}
}
/*
* color
*/
int color(int c, struct cairoui *cairoui) {
struct cairoui_cb *cairoui_cb = cairoui->cb;
static char *colortext[] = {
"select color",
"red",
"green",
"blue",
NULL
};
static int line = 0;
static int selected = 1;
int res;
if (c == KEY_INIT)
selected = cairoui_cb->color + 1;
res = cairoui_list(c, cairoui, colortext, &line, &selected);
if (res == CAIROUI_LEAVE)
return WINDOW_DOCUMENT;
if (res != CAIROUI_DONE)
return WINDOW_COLOR;
switch (selected) {
case 1:
case 2:
case 3:
cairoui_cb->color = selected - 1;
break;
}
return cairoui_cb->immediate ? CAIROUI_REFRESH : WINDOW_DOCUMENT;
}
/*
* position
*/
int position(int c, struct cairoui *cairoui) {
struct cairoui_cb *cairoui_cb = cairoui->cb;
static char positionstring[100] = "";
static int pos = 0;
int res;
res = cairoui_number(c, cairoui,
"position: ", positionstring, &pos, NULL,
&cairoui_cb->x, 0, 200);
cairoui_cb->y = cairoui_cb->x;
if (res == CAIROUI_DONE)
return cairoui_cb->immediate ?
CAIROUI_REFRESH : WINDOW_DOCUMENT;
if (res == CAIROUI_LEAVE)
return WINDOW_DOCUMENT;
cairoui_printlabel(cairoui, cairoui_cb->help,
NO_TIMEOUT, "down=increase up=decrease");
return WINDOW_POSITION;
}
/*
* window list
*/
struct windowlist windowlist[] = {
{ WINDOW_DOCUMENT, "DOCUMENT", document }, // always 0
{ WINDOW_COLOR, "COLOR", color },
{ WINDOW_POSITION, "POSITION", position },
{ 0, NULL, NULL }
};
/***************************** LABELS ******************************/
/*
* help label
*/
void helplabel(struct cairoui *cairoui) {
struct cairoui_cb *cairoui_cb = cairoui->cb;
if (cairoui_cb->help[0] == '\0')
return;
cairoui_label(cairoui, cairoui_cb->help, 1);
cairoui_cb->help[0] = '\0';
}
/*
* list of labels
*/
void (*labellist[])(struct cairoui *) = {
helplabel,
NULL
};
/************************ CAIROUI CALLBACKS ************************/
/*
* draw
*/
void draw(struct cairoui *cairoui) {
struct cairoui_cb *cairoui_cb = cairoui->cb;
cairo_identity_matrix(cairoui->cr);
switch (cairoui_cb->color) {
case 0:
cairo_set_source_rgb(cairoui->cr, 1.0, 0.0, 0.0);
break;
case 1:
cairo_set_source_rgb(cairoui->cr, 0.0, 1.0, 0.0);
break;
case 2:
cairo_set_source_rgb(cairoui->cr, 0.0, 0.0, 1.0);
break;
}
cairo_rectangle(cairoui->cr, cairoui_cb->x, cairoui_cb->y, 100, 100);
cairo_fill(cairoui->cr);
}
/******************************* MAIN ***************************/
/*
* main
*/
int main(int argn, char *argv[]) {
char *mainopts = "h", *allopts;
char *outdev = NULL;
int doublebuffering = 1;
int canopen;
struct cairoui_cb cairoui_cb;
struct cairoui cairoui;
int firstwindow = WINDOW_DOCUMENT;
/* select the cairo device */
cairoui.cairodevice =
getenv("DISPLAY") ? &cairodevicex11 : &cairodevicefb;
/* merge general and device-specific options */
allopts = malloc(strlen(mainopts) +
strlen(cairoui.cairodevice->options) + 1);
strcpy(allopts, mainopts);
strcat(allopts, cairoui.cairodevice->options);
/* open output device as cairo */
canopen = cairoui.cairodevice->init(cairoui.cairodevice,
outdev, doublebuffering, argn, argv, allopts);
if (canopen == -1) {
cairoui.cairodevice->finish(cairoui.cairodevice);
exit(EXIT_FAILURE);
}
free(allopts);
/* initialize the cairo user interface */
cairoui_default(&cairoui);
cairoui.cb = &cairoui_cb;
cairoui_cb.immediate = TRUE;
cairoui_cb.help[0] = '\0';
cairoui_cb.color = 1;
cairoui_cb.x = 10;
cairoui_cb.y = 10;
cairoui.draw = draw;
cairoui.windowlist = windowlist;
cairoui.labellist = labellist;
cairoui.log = LEVEL_MAIN;
// cairoui_cb.immediate = FALSE;
// firstwindow = WINDOW_COLOR;
/* event loop */
cairoui_main(&cairoui, firstwindow);
return EXIT_SUCCESS;
}