Debian Codegraph @

gdraw/gflowbox.c

raw symbols SCIP
1 /* Copyright (C) 2006-2012 by George Williams, 2021 by Skef Iterum */
2 /*
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are met:
5
6 * Redistributions of source code must retain the above copyright notice, this
7 * list of conditions and the following disclaimer.
8
9 * Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12
13 * The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 /* XXX NOTE:
29 *
30 * Although the GFlowBox has code for vertical layout with the
31 * gg_flow_vert flag it is not debugged. Labels are also always
32 * laid out horizontally before the elements. The first person
33 * who wants to use a GFlowBox vertically will have to work through
34 * the relevant issues.
35 */
36
37 #include <fontforge-config.h>
38
39 #include "gdraw.h"
40 #include "ggadgetP.h"
41
42 #define GG_Pad10 ((GGadget *) -4) // Must match GCD_Hpad10 in ggadget.h
43
44 static GBox flowbox_box = GBOX_EMPTY; /* Don't initialize here */
45
46 extern GResInfo gscroll1box_ri;
47 GResInfo gflowbox_ri = {
48 &gscroll1box_ri, &ggadget_ri, NULL, NULL,
49 &flowbox_box,
50 NULL,
51 NULL,
52 NULL,
53 N_("Flow Box"),
54 N_("A box drawn around other gadgets to flow its contents"),
55 "GFlowBox",
56 "Gdraw",
57 false,
58 false,
59 omf_border_type | omf_border_width | omf_padding,
60 { bt_none, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
61 GBOX_EMPTY,
62 NULL,
63 NULL,
64 NULL
65 };
66
67 static void GFlowBox_destroy(GGadget *g) {
68 GFlowBox *fb = (GFlowBox *) g;
69 int c;
70
71 if (fb->label != NULL) {
72 GGadgetDestroy(fb->label);
73 }
74 for (c = 0; c < fb->count; ++c)
75 if (fb->children[c] != GG_Pad10) {
76 GGadgetDestroy(fb->children[c]);
77 }
78 free(fb->children);
79 _ggadget_destroy(g);
80 }
81
82 static void GFlowBoxMove(GGadget *g, int32_t x, int32_t y) {
83 GFlowBox *fb = (GFlowBox *) g;
84 int offx = x - g->r.x, offy = y - g->r.y;
85 int c;
86
87 if (fb->label != NULL) {
88 GGadgetMove(fb->label, fb->label->inner.x + offx, fb->label->inner.y + offy);
89 }
90 for (c = 0; c < fb->count; ++c)
91 if (fb->children[c] != GG_Pad10)
92 GGadgetMove(fb->children[c],
93 fb->children[c]->r.x + offx, fb->children[c]->r.y + offy);
94 _ggadget_move(g, x, y);
95 }
96
97 struct childsizedata {
98 int extra_space; /* a default button has "extra_space" */
99 int min, oppomin;
100 int size, opposize;
101 int offset;
102 int rowcol;
103 int oppooffset; // Relative to rowcol
104 };
105
106 struct fsizeinfo {
107 struct childsizedata *childsize;
108 int *rowcolbaseline;
109 int max, oppomax;
110 int min;
111 int size, opposize;
112 int label_width, label_height, label_size;
113 int rowcols;
114 };
115
116 static void GFlowBoxGatherMinInfo(GFlowBox *fb, struct fsizeinfo *si) {
117 GRect outer;
118 int c, ten = GDrawPointsToPixels(fb->g.base, 10);
119
120 memset(si, 0, sizeof(*si));
121 si->childsize = calloc(fb->count, sizeof(struct childsizedata));
122
123 for (c = 0; c < fb->count; ++c) {
124 GGadget *g = fb->children[c];
125 struct childsizedata *cs = si->childsize + c;
126 if (g->state == gs_invisible) {
127 continue;
128 }
129 if (g == GG_Pad10) {
130 cs->min = ten;
131 cs->oppomin = 0;
132 } else {
133 GGadgetGetDesiredSize(g, &outer, NULL);
134 cs->extra_space = GBoxExtraSpace(g);
135 if (fb->vertical) {
136 cs->min = outer.height;
137 cs->oppomin = outer.width;
138 } else {
139 cs->min = outer.width;
140 cs->oppomin = outer.height;
141 }
142 }
143 si->max += cs->min;
144 if (c != 0) {
145 si->max += fb->vertical ? fb->vpad : fb->hpad;
146 }
147 if (si->oppomax < cs->oppomin) {
148 si->oppomax = cs->oppomin;
149 }
150 if (si->min < cs->min) {
151 si->min = cs->min;
152 }
153 }
154
155 if (fb->label != NULL) {
156 GGadgetGetDesiredSize(fb->label, &outer, NULL);
157 si->label_width = outer.width;
158 if (fb->label_size != -1) {
159 si->label_size = fb->label_size;
160 } else {
161 si->label_size = si->label_width;
162 }
163 si->label_height = outer.height;
164 if (!fb->vertical) {
165 si->min += si->label_size + fb->lpad;
166 si->max += si->label_size + fb->lpad;
167 }
168 }
169
170 }
171
172 static void GFlowBoxSizeTo(GFlowBox *fb, struct fsizeinfo *si, int size) {
173 int rowcolstart = 0, rowcoloffset = 0, cursize = 0, currowcolsize = 0;
174 int pad = fb->vertical ? fb->vpad : fb->hpad;
175 int oppopad = fb->vertical ? fb->hpad : fb->vpad;
176 int c, j, subsize, exp, move;
177
178 si->rowcols = 0;
179 if (si->rowcolbaseline != NULL) {
180 free(si->rowcolbaseline);
181 }
182 si->rowcolbaseline = calloc(fb->count, sizeof(int));
183
184 if (!fb->vertical && fb->label != NULL) {
185 subsize = size - si->label_size - fb->lpad;
186 } else {
187 subsize = size;
188 }
189
190 for (c = 0; c < fb->count; ++c) {
191 struct childsizedata *cs = si->childsize + c;
192 if (c != rowcolstart && cursize + cs->min + pad > subsize) {
193 move = subsize - cursize;
194 exp = move / (c - rowcolstart);
195 for (j = rowcolstart; j < c; ++j) {
196 if (fb->just & gg_flow_right) {
197 si->childsize[j].offset += move;
198 } else if (fb->just & gg_flow_center) {
199 si->childsize[j].offset += move / 2;
200 } else if (fb->just & gg_flow_expand) {
201 si->childsize[j].offset += exp * (j - rowcolstart);
202 si->childsize[j].size += exp;
203 }
204 }
205 si->rowcolbaseline[si->rowcols] = rowcoloffset;
206 si->rowcols++;
207 rowcoloffset += currowcolsize + oppopad;
208 currowcolsize = 0;
209 rowcolstart = c;
210 cursize = 0;
211 }
212 if (currowcolsize < cs->oppomin) {
213 currowcolsize = cs->oppomin;
214 }
215 if (c != rowcolstart) {
216 cursize += pad;
217 }
218 cs->offset = cursize;
219 cs->size = cs->min;
220 cs->opposize = cs->oppomin;
221 cs->rowcol = si->rowcols;
222 cursize += cs->min;
223 }
224 move = subsize - cursize;
225 exp = move / (c - rowcolstart);
226 for (j = rowcolstart; j < fb->count; ++j) {
227 if (fb->just & gg_flow_right) {
228 si->childsize[j].offset += move;
229 } else if (fb->just & gg_flow_center) {
230 si->childsize[j].offset += move / 2;
231 } else if (fb->just & gg_flow_expand) {
232 si->childsize[j].offset += exp * (j - rowcolstart);
233 si->childsize[j].size += exp;
234 }
235 }
236 si->rowcolbaseline[si->rowcols] = rowcoloffset;
237 si->rowcols++;
238 si->size = size;
239 si->opposize = rowcoloffset + currowcolsize;
240 }
241
242 static void GFlowBoxResize(GGadget *g, int32_t width, int32_t height) {
243 struct fsizeinfo si;
244 GFlowBox *fb = (GFlowBox *) g;
245 int bp = GBoxBorderWidth(g->base, g->box);
246 int c, size, opposize, move;
247 int x, y, cw, ch, startoff;
248 int old_enabled = GDrawEnableExposeRequests(g->base, false);
249 int has_ojust = fb->just & (gg_flow_oright | gg_flow_ocenter);
250
251 GFlowBoxGatherMinInfo(fb, &si);
252 width -= 2 * bp;
253 height -= 2 * bp;
254
255 if (!fb->vertical && fb->label != NULL) {
256 startoff = si.label_size + fb->lpad;
257 } else {
258 startoff = 0;
259 }
260
261 size = fb->vertical ? height : width;
262 opposize = fb->vertical ? width : height;
263 if (size < si.min) {
264 size = si.min;
265 }
266
267 fb->g.inner.x = fb->g.r.x + bp;
268 fb->g.inner.y = fb->g.r.y + bp;
269
270 GFlowBoxSizeTo(fb, &si, size);
271
272 move = opposize - si.opposize;
273 if (has_ojust && move > 0) {
274 for (int rc = 0; rc < si.rowcols; ++rc) {
275 if (fb->just & gg_flow_oright) { // oright is also obottom
276 si.rowcolbaseline[rc] += move;
277 } else if (fb->just & gg_flow_ocenter) {
278 si.rowcolbaseline[rc] += move / 2;
279 }
280 }
281 }
282
283 for (c = 0; c < fb->count; ++c) {
284 GGadget *cg = fb->children[c];
285 struct childsizedata *cs = si.childsize + c;
286 if (cg == GG_Pad10) {
287 continue;
288 }
289 if (fb->vertical) {
290 x = cs->oppooffset + si.rowcolbaseline[cs->rowcol];
291 y = cs->offset;
292 cw = cs->opposize;
293 ch = cs->size;
294 } else {
295 x = startoff + cs->offset;
296 y = cs->oppooffset + si.rowcolbaseline[cs->rowcol];
297 cw = cs->size;
298 ch = cs->opposize;
299 }
300 if (cg->state != gs_invisible) {
301 GGadgetResize(cg, cw, ch);
302 }
303 GGadgetMove(cg, fb->g.inner.x + x, fb->g.inner.y + y);
304 }
305
306 if (!fb->vertical && fb->label != NULL) {
307 x = 0;
308 cw = si.label_width;
309 move = si.label_size - si.label_width;
310 if (move > 0) {
311 if (fb->just & gg_flow_lright) {
312 x += move;
313 } else if (fb->just & gg_flow_lhcenter) {
314 x += move / 2;
315 }
316 }
317 y = 0;
318 ch = si.label_height;
319 move = height - si.label_height;
320 if (move > 0) {
321 if (fb->just & gg_flow_lbottom) {
322 y += move;
323 } else if (fb->just & gg_flow_lvcenter) {
324 y += move / 2;
325 }
326 }
327 GGadgetResize(fb->label, cw, ch);
328 GGadgetMove(fb->label, fb->g.inner.x + x, fb->g.inner.y + y);
329 }
330
331 free(si.childsize);
332 free(si.rowcolbaseline);
333
334 fb->g.inner.width = fb->vertical ? si.opposize : si.size;
335 fb->g.inner.height = fb->vertical ? si.size : si.opposize;
336 fb->g.r.width = fb->g.inner.width + 2 * bp;
337 fb->g.r.height = fb->g.inner.height + 2 * bp;
338
339 GDrawEnableExposeRequests(g->base, old_enabled);
340 GDrawRequestExpose(g->base, &g->r, false);
341 }
342
343 void _GFlowBoxGetDesiredSize(GGadget *g, GRect *outer, GRect *inner, int squashed, int ignore_desired) {
344 struct fsizeinfo si;
345 GFlowBox *fb = (GFlowBox *) g;
346 int bp = GBoxBorderWidth(g->base, g->box);
347 int width, height;
348
349 GFlowBoxGatherMinInfo(fb, &si);
350 if (fb->vertical) {
351 if (squashed) {
352 GFlowBoxSizeTo(fb, &si, si.min);
353 height = si.size;
354 width = si.opposize;
355 } else if (g->desired_height > 0 && !ignore_desired) {
356 int dh = g->desired_height - 2 * bp;
357 if (dh >= si.max) {
358 height = dh;
359 width = si.oppomax;
360 } else {
361 GFlowBoxSizeTo(fb, &si, dh);
362 height = si.size;
363 width = si.opposize;
364 }
365 } else {
366 height = si.max;
367 width = si.oppomax;
368 }
369 } else {
370 if (squashed) {
371 GFlowBoxSizeTo(fb, &si, si.min);
372 width = si.size;
373 height = si.opposize;
374 } else if (g->desired_width > 0 && !ignore_desired) {
375 int dw = g->desired_width - 2 * bp;
376 if (dw >= si.max) {
377 width = dw;
378 height = si.oppomax;
379 } else {
380 GFlowBoxSizeTo(fb, &si, dw);
381 width = si.size;
382 height = si.opposize;
383 }
384 } else {
385 width = si.max;
386 height = si.oppomax;
387 }
388 if (height < si.label_height) {
389 height = si.label_height;
390 }
391 }
392
393 if (inner != NULL) {
394 inner->x = inner->y = bp;
395 inner->width = width;
396 inner->height = height;
397 }
398 if (outer != NULL) {
399 outer->x = outer->y = 0;
400 outer->width = width + 2 * bp;
401 outer->height = height + 2 * bp;
402 }
403 free(si.childsize);
404 free(si.rowcolbaseline);
405 }
406
407 static void GFlowBoxGetDesiredSize(GGadget *g, GRect *outer, GRect *inner) {
408 _GFlowBoxGetDesiredSize(g, outer, inner, false, false);
409 }
410
411 /* A GScroll1Box will size a GFlowBox larger than it requests when appropriate.
412 * If we return true here the GHVBox/GContainer code will confuse things.
413 */
414 static int GFlowBoxFillsWindow(GGadget *g) {
415 return false;
416 }
417
418 static int expose_nothing(GWindow pixmap, GGadget *g, GEvent *event) {
419 if (g->state != gs_invisible) {
420 GBoxDrawBorder(pixmap, &g->r, g->box, g->state, false);
421 }
422 return true;
423 }
424
425 struct gfuncs gflowbox_funcs = {
426 0,
427 sizeof(struct gfuncs),
428
429 expose_nothing, /* Expose */
430 _ggadget_noop, /* Mouse */
431 _ggadget_noop, /* Key */
432 NULL,
433 NULL, /* Focus */
434 NULL,
435 NULL,
436
437 _ggadget_redraw,
438 GFlowBoxMove,
439 GFlowBoxResize,
440 _ggadget_setvisible,
441 _ggadget_setenabled,
442 _ggadget_getsize,
443 _ggadget_getinnersize,
444
445 GFlowBox_destroy,
446
447 NULL,
448 NULL,
449 NULL,
450 NULL,
451 NULL,
452
453 NULL,
454 NULL,
455
456 NULL,
457 NULL,
458 NULL,
459 NULL,
460 NULL,
461 NULL,
462 NULL,
463 NULL,
464 NULL,
465 NULL,
466 NULL,
467
468 GFlowBoxGetDesiredSize,
469 _ggadget_setDesiredSize,
470 GFlowBoxFillsWindow,
471 NULL
472 };
473
474 void GFlowBoxSetPadding(GGadget *g, int hpad, int vpad, int lpad) {
475 GFlowBox *fb = (GFlowBox *) g;
476 if (hpad >= 0) {
477 fb->hpad = hpad;
478 }
479 if (vpad >= 0) {
480 fb->vpad = vpad;
481 }
482 if (lpad >= 0) {
483 fb->lpad = lpad;
484 }
485 }
486
487 int GFlowBoxGetLabelSize(GGadget *g) {
488 GFlowBox *fb = (GFlowBox *) g;
489 GRect outer;
490 if (fb->label_size != -1) {
491 return fb->label_size;
492 }
493
494 if (fb->label != NULL) {
495 GGadgetGetDesiredSize(fb->label, &outer, NULL);
496 return outer.width;
497 }
498
499 return 0;
500 }
501
502 void GFlowBoxSetLabelSize(GGadget *g, int size) {
503 GFlowBox *fb = (GFlowBox *) g;
504 if (size >= 0 && !(fb->just & gg_flow_noalignlabel)) {
505 fb->label_size = size;
506 }
507 }
508
509 int GGadgetIsGFlowBox(GGadget *g) {
510 return g->funcs->get_desired_size == &GFlowBoxGetDesiredSize;
511 }
512
513 GGadget *GFlowBoxCreate(struct gwindow *base, GGadgetData *gd, void *data) {
514 GFlowBox *fb = calloc(1, sizeof(GFlowBox));
515 GGadgetCreateData *label = (GGadgetCreateData *)(gd->label);
516
517 GResEditDoInit(&gflowbox_ri);
518
519 for (fb->count = 0; gd->u.boxelements[fb->count] != NULL; ++fb->count);
520
521 fb->g.funcs = &gflowbox_funcs;
522 _GGadget_Create(&fb->g, base, gd, data, &flowbox_box);
523 fb->hpad = fb->vpad = GDrawPointsToPixels(base, 10);
524 fb->lpad = GDrawPointsToPixels(base, 5);
525 fb->label_size = -1;
526 fb->vertical = (gd->flags & gg_flow_vert) ? true : false;
527 fb->just = gd->flags;
528
529 fb->g.takes_input = false;
530 fb->g.takes_keyboard = false;
531 fb->g.focusable = false;
532
533 if (label != NULL) {
534 fb->label = label->ret = (label->creator)(base, &label->gd, label->data);
535 fb->label->contained = true;
536 }
537
538 fb->children = malloc(fb->count * sizeof(GGadget *));
539 for (int c = 0; c < fb->count; ++c) {
540 GGadgetCreateData *gcd = gd->u.boxelements[c];
541 if (gcd == GCD_HPad10) {
542 fb->children[c] = (GGadget *) gcd;
543 } else {
544 gcd->gd.pos.x = gcd->gd.pos.y = 1;
545 fb->children[c] = gcd->ret = (gcd->creator)(base, &gcd->gd, gcd->data);
546 gcd->ret->contained = true;
547 }
548 }
549 return &fb->g;
550 }