Debian Codegraph @

third_party/spiro/x3/x3common.c

raw symbols SCIP
1 /* Functions common to more than one platform. */
2
3 #include <stdlib.h>
4
5 #include "x3.h"
6 #include "x3common.h"
7
8 int n_x3needshow = 0;
9 int n_x3needshow_max = 0;
10 x3widget **x3needshow = NULL;
11
12 #if defined(X3_GTK) || defined(X3_WIN32)
13 int x3n_winopen = 0;
14 #endif
15
16 #if defined(X3_CARBON) || defined(X3_WIN32)
17
18 int n_x3needsizereqs = 0;
19 int n_x3needsizereqs_max = 0;
20 x3widget **x3needsizereqs = NULL;
21
22 int n_x3needsizeallocs = 0;
23 int n_x3needsizeallocs_max = 0;
24 x3widget **x3needsizeallocs = NULL;
25
26 void x3qsizereq(x3widget *w)
27 {
28 if (w && !(w->flags & x3flag_needsizereq)) {
29 x3qsizereq(w->parent);
30 if (n_x3needsizereqs == n_x3needsizereqs_max)
31 x3needsizereqs = (x3widget **)realloc(x3needsizereqs,
32 sizeof(x3widget *) *
33 (n_x3needsizereqs_max <<= 1));
34 x3needsizereqs[n_x3needsizereqs++] = w;
35 w->flags |= x3flag_needsizereq;
36 }
37 }
38
39 void x3add_default(x3widget *parent, x3widget *child)
40 {
41 const int n_children_init = 4;
42
43 child->parent = parent;
44 if (parent->n_children == 0)
45 parent->children = (x3widget **)malloc(sizeof(x3widget *) *
46 n_children_init);
47 else if (parent->n_children >= n_children_init &&
48 !(parent->n_children & (parent->n_children - 1)))
49 parent->children = (x3widget **)realloc(parent->children,
50 sizeof(x3widget *) *
51 (parent->n_children << 1));
52 parent->children[parent->n_children++] = child;
53 }
54
55 void x3add(x3widget *parent, x3widget *child)
56 {
57 parent->type->add(parent, child);
58 }
59
60 /* Widgets common to carbon and win32 platforms */
61
62 typedef struct {
63 x3widget base;
64 int homogeneous;
65 int spacing;
66 int cur_child_prop;
67 int *child_props; /* bit 0=expand, bit 1=fill, bits 2:31=padding */
68 } x3widget_box;
69
70 void x3vbox_sizereq(x3widget *w)
71 {
72 x3widget_box *wb = (x3widget_box *)w;
73 int i;
74 int spacing = wb->spacing;
75
76 w->sizerequest.x0 = 0;
77 w->sizerequest.y0 = 0;
78 w->sizerequest.x1 = 0;
79 w->sizerequest.y1 = 0;
80 for (i = 0; i < w->n_children; i++) {
81 x3widget *child = w->children[i];
82 int childw = child->sizerequest.x1 - child->sizerequest.x0;
83 int childh = child->sizerequest.y1 - child->sizerequest.y0;
84 int padding = wb->child_props[i] >> 2;
85
86 if (i < w->n_children - 1)
87 childh += spacing;
88 w->sizerequest.y1 += childh + 2 * padding;
89 if (childw > w->sizerequest.x1)
90 w->sizerequest.x1 = childw;
91 }
92 }
93
94 void x3vbox_sizealloc(x3widget *w, x3rect *r)
95 {
96 x3widget_box *wb = (x3widget_box *)w;
97 int i;
98 x3rect child_r = *r;
99 int spacing = wb->spacing;
100 int n_extend = 0;
101 int n_stretch, i_stretch = 0;
102 int extra;
103
104 /* todo: impl padding & homog, factor hbox/vbox common */
105 printf("vbox sizealloc = (%g, %g) - (%g, %g), req was %g x %g\n",
106 r->x0, r->y0, r->x1, r->y1,
107 w->sizerequest.x1, w->sizerequest.y1);
108 extra = r->y1 - r->y0 - w->sizerequest.y1;
109 for (i = 0; i < w->n_children; i++)
110 if (wb->child_props[i] & 1)
111 n_extend++;
112 n_stretch = n_extend ? n_extend : w->n_children;
113 printf("extra = %d, n_stretch = %d\n", extra, n_stretch);
114 for (i = 0; i < w->n_children; i++) {
115 x3widget *child = w->children[i];
116 int childh = child->sizerequest.y1 - child->sizerequest.y0;
117 int my_extra;
118 int next_top;
119
120 if (n_extend == 0 || (wb->child_props[i] & 1)) {
121 my_extra = (extra * (i_stretch + 1)) / n_stretch -
122 (extra * i_stretch) / n_stretch;
123 i_stretch++;
124 } else
125 my_extra = 0;
126 next_top = child_r.y0 + childh + spacing + my_extra;
127
128 if (wb->child_props[i] & 2) {
129 childh += my_extra;
130 } else {
131 child_r.y0 += my_extra >> 1;
132 }
133 child_r.y1 = child_r.y0 + childh;
134
135 child->type->sizealloc(child, &child_r);
136 child->flags &= ~x3flag_needsizealloc;
137
138 child_r.y0 = next_top;
139 }
140 }
141
142 void x3vbox_add(x3widget *w, x3widget *child)
143 {
144 x3widget_box *wb = (x3widget_box *)w;
145 const int n_children_init = 4;
146
147 if (w->n_children == 0)
148 wb->child_props = (int *)malloc(sizeof(int) * n_children_init);
149 else if (w->n_children >= n_children_init &&
150 !(w->n_children & (w->n_children - 1)))
151 wb->child_props = (int *)realloc(wb->child_props,
152 sizeof(int) * (w->n_children << 1));
153 wb->child_props[w->n_children] = wb->cur_child_prop;
154 x3add_default(w, child);
155 }
156
157 x3type x3vboxtype = { x3vbox_sizereq,
158 x3vbox_sizealloc,
159 x3vbox_add };
160
161 x3widget *x3vbox(x3widget *parent, int homogeneous, int spacing)
162 {
163 x3widget_box *result = (x3widget_box *)malloc(sizeof(x3widget_box));
164 x3widget_init(&result->base, &x3vboxtype);
165 x3add(parent, &result->base);
166 result->homogeneous = homogeneous;
167 result->spacing = spacing;
168 result->cur_child_prop = 3;
169 x3qsizereq(&result->base);
170 return &result->base;
171 }
172
173 void x3setpacking(x3widget *w, int fill, int expand, int padding)
174 {
175 if (w->type == &x3vboxtype) {
176 x3widget_box *wb = (x3widget_box *)w;
177 int child_props = padding << 2;
178
179 if (fill) child_props |= 1;
180 if (expand) child_props |= 2;
181 wb->cur_child_prop = child_props;
182 }
183 }
184
185 typedef struct {
186 x3widget base;
187 x3alignment alignment;
188 } x3widget_align;
189
190 void x3align_sizereq(x3widget *w)
191 {
192 w->sizerequest.x0 = 0;
193 w->sizerequest.y0 = 0;
194 w->sizerequest.x1 = 0;
195 w->sizerequest.y1 = 0;
196 if (w->n_children) {
197 x3widget *child = w->children[0];
198 int childw = child->sizerequest.x1 - child->sizerequest.x0;
199 int childh = child->sizerequest.y1 - child->sizerequest.y0;
200 w->sizerequest.x1 = childw;
201 w->sizerequest.y1 = childh;
202 }
203 }
204
205 void x3align_sizealloc(x3widget *w, x3rect *r)
206 {
207 x3widget_align *z = (x3widget_align *)w;
208 x3alignment a = z->alignment;
209 int xa = a & 3;
210 int ya = (a >> 2) & 3;
211 x3rect child_r = *r;
212
213 printf("align sizealloc = (%g, %g) - (%g, %g)\n",
214 r->x0, r->y0, r->x1, r->y1);
215 if (w->n_children) {
216 x3widget *child = w->children[0];
217 if (xa < 3) {
218 int childw = child->sizerequest.x1 - child->sizerequest.x0;
219 int pad = r->x1 - r->x0 - childw;
220 child_r.x0 += (pad * (1 + (xa >> 1) - (xa & 1)) + 1) >> 1;
221 child_r.x1 = child_r.x0 + childw;
222 }
223 if (ya < 3) {
224 int childh = child->sizerequest.y1 - child->sizerequest.y0;
225 int pad = r->y1 - r->y0 - childh;
226 child_r.y0 += (pad * (1 + (ya >> 1) - (ya & 1)) + 1) >> 1;
227 child_r.y1 = child_r.y0 + childh;
228 }
229
230 child->type->sizealloc(child, &child_r);
231 child->flags &= ~x3flag_needsizealloc;
232 }
233 }
234
235 x3type x3aligntype = { x3align_sizereq,
236 x3align_sizealloc,
237 x3add_default };
238
239 x3widget *x3align(x3widget *parent, x3alignment alignment)
240 {
241 x3widget *result = (x3widget *)malloc(sizeof(x3widget_align));
242 x3widget_init(result, &x3aligntype);
243 x3add(parent, result);
244 x3qsizereq(result);
245 ((x3widget_align *)result)->alignment = alignment;
246 return result;
247 }
248
249 typedef struct {
250 x3widget base;
251 int t, b, l, r;
252 } x3widget_pad;
253
254 void x3pad_sizereq(x3widget *w)
255 {
256 x3widget_pad *z = (x3widget_pad *)w;
257 w->sizerequest.x0 = 0;
258 w->sizerequest.y0 = 0;
259 w->sizerequest.x1 = 0;
260 w->sizerequest.y1 = 0;
261 if (w->n_children) {
262 x3widget *child = w->children[0];
263 int childw = child->sizerequest.x1 - child->sizerequest.x0;
264 int childh = child->sizerequest.y1 - child->sizerequest.y0;
265 w->sizerequest.x1 = childw + z->l + z->r;
266 w->sizerequest.y1 = childh + z->t + z->b;
267 }
268 }
269
270 void x3pad_sizealloc(x3widget *w, x3rect *r)
271 {
272 x3widget_pad *z = (x3widget_pad *)w;
273 x3rect child_r = *r;
274
275 printf("pad sizealloc = (%g, %g) - (%g, %g)\n",
276 r->x0, r->y0, r->x1, r->y1);
277 if (w->n_children) {
278 x3widget *child = w->children[0];
279 child_r.x0 += z->l;
280 child_r.x1 -= z->r;
281 child_r.y0 += z->t;
282 child_r.y1 -= z->b;
283
284 child->type->sizealloc(child, &child_r);
285 child->flags &= ~x3flag_needsizealloc;
286 }
287 }
288
289 x3type x3padtype = { x3pad_sizereq,
290 x3pad_sizealloc,
291 x3add_default };
292
293 x3widget *x3pad(x3widget *parent, int t, int b, int l, int r)
294 {
295 x3widget *result = (x3widget *)malloc(sizeof(x3widget_pad));
296 x3widget_init(result, &x3padtype);
297 x3add(parent, result);
298 x3qsizereq(result);
299 ((x3widget_pad *)result)->t = t;
300 ((x3widget_pad *)result)->b = b;
301 ((x3widget_pad *)result)->l = l;
302 ((x3widget_pad *)result)->r = r;
303 return result;
304 }
305
306 #endif
307
308 void x3initqs(void)
309 {
310 n_x3needshow = 0;
311 x3needshow = (x3widget **)malloc(sizeof(x3widget *) *
312 (n_x3needshow_max = 16));
313
314 #if defined(X3_CARBON) || defined(X3_WIN32)
315 n_x3needsizereqs = 0;
316 x3needsizereqs = (x3widget **)malloc(sizeof(x3widget *) *
317 (n_x3needsizereqs_max = 16));
318
319 n_x3needsizeallocs = 0;
320 x3needsizeallocs = (x3widget **)malloc(sizeof(x3widget *) *
321 (n_x3needsizeallocs_max = 16));
322 #endif
323 }
324
325 void x3qshow(x3widget *w)
326 {
327 if (n_x3needshow == n_x3needshow_max)
328 x3needshow = (x3widget **)realloc(x3needshow,
329 sizeof(x3widget *) *
330 (n_x3needshow_max <<= 1));
331 x3needshow[n_x3needshow++] = w;
332 }
333
334 void x3sync(void)
335 {
336 int i;
337
338 #if defined(X3_CARBON) || defined(X3_WIN32)
339
340 for (i = n_x3needsizereqs - 1; i >= 0; i--) {
341 x3widget *w = x3needsizereqs[i];
342 w->type->sizereq(w);
343 w->flags &= ~x3flag_needsizereq;
344 w->flags |= x3flag_needsizealloc;
345 }
346 for (i = 0; i < n_x3needsizereqs; i++) {
347 x3widget *w = x3needsizereqs[i];
348 if (w->flags & x3flag_needsizealloc) {
349 w->type->sizealloc(w, NULL);
350 w->flags &= ~x3flag_needsizealloc;
351 }
352 }
353 n_x3needsizereqs = 0;
354 #endif
355
356 for (i = 0; i < n_x3needshow; i++)
357 x3_window_show(x3needshow[i]);
358 n_x3needshow = 0;
359 }