Debian Codegraph @

gdraw/ghvbox.c

raw symbols SCIP
1 /* Copyright (C) 2006-2012 by George Williams */
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 #include <fontforge-config.h>
29
30 #include "gdraw.h"
31 #include "ggadgetP.h"
32
33 /* If the locale is hebrew or arabic should we lay out boxes right to left???*/
34
35 #define GG_Glue ((GGadget *) -1) /* Special entries */
36 #define GG_ColSpan ((GGadget *) -2) /* for box elements */
37 #define GG_RowSpan ((GGadget *) -3) /* Must match those in ggadget.h */
38 #define GG_HPad10 ((GGadget *) -4)
39
40 static GBox hvgroup_box = GBOX_EMPTY; /* Don't initialize here */
41 static GBox hvbox_box = GBOX_EMPTY; /* Don't initialize here */
42
43 extern GResInfo gflowbox_ri;
44 GResInfo ghvgroupbox_ri = {
45 &gflowbox_ri, &ggadget_ri, NULL, NULL,
46 &hvgroup_box,
47 NULL,
48 NULL,
49 NULL,
50 N_("HV Group Box"),
51 N_("A box drawn around other gadgets"),
52 "GGroup",
53 "Gdraw",
54 false,
55 false,
56 omf_border_type|omf_border_shape|omf_padding|omf_main_background|omf_disabled_background,
57 { bt_engraved, bs_rect, 0, 2, 0, 0, 0, 0, 0, 0, COLOR_TRANSPARENT, 0, COLOR_TRANSPARENT, 0, 0, 0, 0, 0, 0 },
58 GBOX_EMPTY,
59 NULL,
60 NULL,
61 NULL
62 };
63 GResInfo ghvbox_ri = {
64 &ghvgroupbox_ri, &ggadget_ri, NULL, NULL,
65 &hvbox_box,
66 NULL,
67 NULL,
68 NULL,
69 N_("HV Box"),
70 N_("A container for other gadgets"),
71 "GHVBox",
72 "Gdraw",
73 false,
74 false,
75 omf_border_type|omf_border_shape|omf_padding,
76 { bt_none, bs_rect, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
77 GBOX_EMPTY,
78 NULL,
79 NULL,
80 NULL
81 };
82
83 static void _GHVBox_Init(void) {
84 GResEditDoInit(&ghvgroupbox_ri);
85 GResEditDoInit(&ghvbox_ri);
86 }
87
88 static void GHVBox_destroy(GGadget *g) {
89 GHVBox *gb = (GHVBox *) g;
90 int i;
91
92 if ( gb->label != NULL )
93 GGadgetDestroy( gb->label );
94 for ( i=0; i<gb->rows*gb->cols; ++i )
95 if ( gb->children[i]!=GG_Glue && gb->children[i]!=GG_ColSpan &&
96 gb->children[i]!=GG_RowSpan && gb->children[i]!=GG_HPad10 )
97 GGadgetDestroy(gb->children[i]);
98 free(gb->children);
99 _ggadget_destroy(g);
100 }
101
102 static void GHVBoxMove(GGadget *g, int32_t x, int32_t y) {
103 GHVBox *gb = (GHVBox *) g;
104 int offx = x-g->r.x, offy = y-g->r.y;
105 int i;
106
107 if ( gb->label!=NULL )
108 GGadgetMove(gb->label,gb->label->inner.x+offx,gb->label->inner.y+offy);
109 for ( i=0; i<gb->rows*gb->cols; ++i )
110 if ( gb->children[i]!=GG_Glue && gb->children[i]!=GG_ColSpan &&
111 gb->children[i]!=GG_RowSpan && gb->children[i]!=GG_HPad10 )
112 GGadgetMove(gb->children[i],
113 gb->children[i]->r.x+offx, gb->children[i]->r.y+offy);
114 _ggadget_move(g,x,y);
115 }
116
117 struct sizedata {
118 int extra_space; /* a default button has "extra_space" */
119 int min; /* This value includes padding */
120 int sized;
121 int allocated;
122 int allglue;
123 };
124
125 struct sizeinfo {
126 struct sizedata *cols;
127 struct sizedata *rows;
128 int label_height, label_width;
129 int width, height;
130 int minwidth, minheight;
131 };
132
133 static void GHVBoxGatherSizeInfo(GHVBox *gb,struct sizeinfo *si) {
134 int i,c,r,spanc, spanr, totc,totr, max, plus, extra, es;
135 GRect outer;
136 int ten = GDrawPointsToPixels(gb->g.base,10);
137
138 memset(si,0,sizeof(*si));
139 si->cols = calloc(gb->cols,sizeof(struct sizedata));
140 si->rows = calloc(gb->rows,sizeof(struct sizedata));
141 for ( c=0; c<gb->cols; ++c ) si->cols[c].allglue = true;
142 for ( r=0; r<gb->rows; ++r ) si->rows[r].allglue = true;
143
144 for ( r=0; r<gb->rows; ++r ) {
145 for ( c=0; c<gb->cols; ++c ) {
146 GGadget *g = gb->children[r*gb->cols+c];
147 if ( g==GG_Glue ) {
148 if ( c+1!=gb->cols && si->cols[c].min<gb->hpad ) si->cols[c].min = gb->hpad;
149 if ( r+1!=gb->rows && si->rows[r].min<gb->vpad ) si->rows[r].min = gb->vpad;
150 } else if ( g==GG_HPad10 ) {
151 if ( c+1!=gb->cols && si->cols[c].min<gb->hpad+ten ) si->cols[c].min = gb->hpad+ten;
152 if ( r+1!=gb->rows && si->rows[r].min<gb->vpad ) si->rows[r].min = gb->vpad+ten/2;
153 si->cols[c].allglue = false;
154 } else if ( g==GG_ColSpan || g==GG_RowSpan )
155 /* Skip it */;
156 else if ( (r+1<gb->rows && gb->children[(r+1)*gb->cols+c]==GG_RowSpan) ||
157 (c+1<gb->cols && gb->children[r*gb->cols+c+1]==GG_ColSpan))
158 /* This gadget spans some columns or rows. Come back later */;
159 else {
160 GGadgetGetDesiredSize(g,&outer,NULL);
161 es = GBoxExtraSpace(g);
162 if ( c+1!=gb->cols && g->state!=gs_invisible )
163 outer.width += gb->hpad;
164 if ( r+1!=gb->rows && g->state!=gs_invisible )
165 outer.height += gb->vpad;
166 si->cols[c].allglue = false;
167 if ( si->cols[c].extra_space<es ) si->cols[c].extra_space=es;
168 if ( si->cols[c].min<outer.width ) si->cols[c].min=outer.width;
169 si->rows[r].allglue = false;
170 if ( si->rows[r].min<outer.height ) si->rows[r].min=outer.height;
171 if ( si->rows[r].extra_space<es ) si->rows[r].extra_space=es;
172 }
173 }
174 }
175
176 for ( r=0; r<gb->rows; ++r ) {
177 for ( c=0; c<gb->cols; ++c ) {
178 GGadget *g = gb->children[r*gb->cols+c];
179 if ( g==GG_Glue || g==GG_ColSpan || g==GG_RowSpan || g==GG_HPad10 )
180 /* Skip it */;
181 else if ( (r+1<gb->rows && gb->children[(r+1)*gb->cols+c]==GG_RowSpan) ||
182 (c+1<gb->cols && gb->children[r*gb->cols+c+1]==GG_ColSpan)) {
183 si->rows[r].allglue = false;
184 totr = si->rows[r].min;
185 for ( spanr=1; r+spanr<gb->rows &&
186 gb->children[(r+spanr)*gb->cols+c]==GG_RowSpan; ++spanr ) {
187 si->rows[r+spanr].allglue = false;
188 totr += si->rows[r+spanr].min;
189 }
190 si->cols[c].allglue = false;
191 totc = si->cols[c].min;
192 for ( spanc=1; c+spanc<gb->cols &&
193 gb->children[r*gb->cols+c+spanc]==GG_ColSpan; ++spanc ) {
194 si->cols[c+spanc].allglue = false;
195 totc += si->cols[c+spanc].min;
196 }
197 GGadgetGetDesiredSize(g,&outer,NULL);
198 es = GBoxExtraSpace(g);
199 if ( c+spanc!=gb->cols && g->state!=gs_invisible )
200 outer.width += gb->hpad;
201 if ( r+spanr!=gb->rows && g->state!=gs_invisible )
202 outer.height += gb->vpad;
203 if ( outer.width>totc ) {
204 plus = (outer.width-totc)/spanc;
205 extra = (outer.width-totc-spanc*plus);
206 for ( i=0; i<spanc; ++i ) {
207 si->cols[c+i].min += plus + (extra>0);
208 --extra;
209 }
210 }
211 if ( outer.height>totr ) {
212 plus = (outer.height-totr)/spanr;
213 extra = (outer.height-totr-spanr*plus);
214 for ( i=0; i<spanr; ++i ) {
215 si->rows[r+i].min += plus + (extra>0);
216 --extra;
217 }
218 }
219 if ( es!=0 ) {
220 for ( i=0; i<spanc; ++i ) {
221 if ( es>si->cols[c+i].extra_space )
222 si->cols[c+i].extra_space = es;
223 }
224 for ( i=0; i<spanr; ++i ) {
225 if ( es>si->rows[r+i].extra_space )
226 si->rows[r+i].extra_space = es;
227 }
228 }
229 }
230 }
231 }
232
233 if ( gb->label!=NULL ) {
234 GGadgetGetDesiredSize(gb->label,&outer,NULL);
235 totc = 0;
236 for ( c=0; c<gb->cols ; ++c )
237 totc += si->cols[c].min;
238 outer.width += 20; /* Set back on each side */
239 if ( outer.width>totc ) {
240 plus = (outer.width-totc)/gb->cols;
241 extra = (outer.width-totc-gb->cols*plus);
242 for ( i=0; i<gb->cols; ++i ) {
243 si->cols[i].min += plus + (extra>0);
244 --extra;
245 }
246 }
247 si->label_height = outer.height;
248 si->label_width = outer.width;
249 }
250
251 for ( max=c=0; c<gb->cols; ++c )
252 si->cols[c].sized = si->cols[c].min;
253 for ( max=r=0; r<gb->rows; ++r )
254 si->rows[r].sized = si->rows[r].min;
255
256 if ( gb->grow_col==gb_samesize ) {
257 for ( max=c=0; c<gb->cols; ++c )
258 if ( max<si->cols[c].sized ) max = si->cols[c].sized;
259 for ( c=0; c<gb->cols; ++c )
260 if ( si->cols[c].min!=0 || si->cols[c].allglue )
261 si->cols[c].sized = max;
262 } else if ( gb->grow_col==gb_expandgluesame ) {
263 for ( max=c=0; c<gb->cols; ++c )
264 if ( max<si->cols[c].sized && !si->cols[c].allglue ) max = si->cols[c].sized;
265 for ( c=0; c<gb->cols; ++c )
266 if ( !si->cols[c].allglue && si->cols[c].min!=0 ) /* Must have at least one visible element */
267 si->cols[c].sized = max;
268 }
269
270 if ( gb->grow_row==gb_samesize ) {
271 for ( max=r=0; r<gb->rows; ++r )
272 if ( max<si->rows[r].sized ) max = si->rows[r].sized;
273 for ( r=0; r<gb->rows; ++r )
274 if ( si->rows[r].min!=0 || si->rows[r].allglue )
275 si->rows[r].sized = max;
276 } else if ( gb->grow_row==gb_expandgluesame ) {
277 for ( max=r=0; r<gb->rows; ++r )
278 if ( max<si->rows[r].sized && !si->rows[r].allglue ) max = si->rows[r].sized;
279 for ( r=0; r<gb->rows; ++r )
280 if ( !si->rows[r].allglue && si->rows[r].min>0 )
281 si->rows[r].sized = max;
282 }
283
284 for ( i=si->width = si->minwidth = 0; i<gb->cols; ++i ) {
285 si->width += si->cols[i].sized;
286 si->minwidth += si->cols[i].min;
287 }
288 for ( i=0, si->height=si->minheight = si->label_height; i<gb->rows; ++i ) {
289 si->height += si->rows[i].sized;
290 si->minheight += si->rows[i].min;
291 }
292 }
293
294 static void GHVBoxResize(GGadget *g, int32_t width, int32_t height) {
295 struct sizeinfo si;
296 GHVBox *gb = (GHVBox *) g;
297 int bp = GBoxBorderWidth(g->base,g->box);
298 int i,c,r,spanc, spanr, totc,totr, glue_cnt, plus, extra;
299 int x,y;
300 int old_enabled = GDrawEnableExposeRequests(g->base,false);
301
302 GHVBoxGatherSizeInfo(gb,&si);
303 width -= 2*bp; height -= 2*bp;
304
305 if(width < si.minwidth) width = si.minwidth;
306 if(height < si.minheight) height = si.minheight;
307
308 gb->g.inner.x = gb->g.r.x + bp; gb->g.inner.y = gb->g.r.y + bp;
309 if ( gb->label!=NULL ) {
310 gb->label_height = si.label_height;
311 g->inner.y += si.label_height;
312 }
313
314 if ( si.width!=width ) {
315 int vcols=0;
316 for ( i=0; i<gb->cols-1; ++i )
317 if ( si.cols[i].sized>0 )
318 ++vcols;
319 int vcols1 = vcols;
320 if(si.cols[gb->cols-1].sized > 0)
321 ++ vcols1;
322 if ( width<si.width ) {
323 for ( i=0; i<gb->cols; ++i )
324 si.cols[i].sized = si.cols[i].min;
325 si.width = si.minwidth;
326 if ( width<si.width && gb->hpad>1 && vcols>0 ) {
327 int reduce_pad = (si.width-width)/vcols + 1;
328 if ( reduce_pad>gb->hpad-1 ) reduce_pad = gb->hpad-1;
329 for ( i=0; i<gb->cols-1; ++i )
330 if ( si.cols[i].sized > 0 )
331 si.cols[i].sized -= reduce_pad;
332 si.width -= vcols*reduce_pad;
333 }
334 }
335 if((width > si.width) && (gb->grow_col==gb_expandglue || gb->grow_col==gb_expandgluesame )) {
336 for ( i=glue_cnt=0; i<gb->cols; ++i )
337 if ( si.cols[i].allglue )
338 ++glue_cnt;
339 if ( glue_cnt!=0 ) {
340 plus = (width-si.width)/glue_cnt;
341 extra = (width-si.width-glue_cnt*plus);
342 for ( i=0; i<gb->cols; ++i ) if ( si.cols[i].allglue ) {
343 si.cols[i].sized += plus + (extra>0);
344 si.width += plus + (extra>0);
345 --extra;
346 }
347 }
348 }
349 if ((width != si.width) && gb->grow_col>=0 ) {
350 int * ss = &(si.cols[gb->grow_col].sized);
351 int w = si.width - *ss;
352 *ss += (width-si.width);
353 if(*ss < gb->hpad + 3)
354 *ss = gb->hpad + 3;
355 si.width = w + *ss;
356 }
357 if ((width > si.width) && (vcols1!=0)) {
358 plus = (width-si.width)/vcols1;
359 extra = (width-si.width-vcols1*plus);
360 for ( i=0; i<gb->cols; ++i ) {
361 if ( si.cols[i].sized>0 ) {
362 si.cols[i].sized += plus + (extra>0);
363 si.width += plus + (extra>0);
364 --extra;
365 }
366 }
367 }
368 width = si.width;
369 }
370
371 if ( si.height!=height ) {
372 int vrows=0;
373 for ( i=0; i<gb->rows-1; ++i )
374 if ( si.rows[i].sized>0 )
375 ++vrows;
376 int vrows1 = vrows;
377 if(si.rows[gb->rows-1].sized > 0)
378 ++ vrows1;
379 if ( height<si.height ) {
380 for ( i=0; i<gb->rows; ++i )
381 si.rows[i].sized = si.rows[i].min;
382 si.height = si.minheight;
383 if ( height<si.height && gb->vpad>1 && vrows>0 ) {
384 int reduce_pad = (si.height-height)/vrows + 1;
385 if ( reduce_pad>gb->vpad-1 ) reduce_pad = gb->vpad-1;
386 for ( i=0; i<gb->rows-1; ++i )
387 if ( si.rows[i].sized > 0 )
388 si.rows[i].sized -= reduce_pad;
389 si.height -= vrows*reduce_pad;
390 }
391 }
392 if((height > si.height) && (gb->grow_row==gb_expandglue || gb->grow_row==gb_expandgluesame )) {
393 for ( i=glue_cnt=0; i<gb->rows; ++i )
394 if ( si.rows[i].allglue )
395 ++glue_cnt;
396 if ( glue_cnt!=0 ){
397 plus = (height-si.height)/glue_cnt;
398 extra = (height-si.height-glue_cnt*plus);
399 for ( i=0; i<gb->rows; ++i ) if ( si.rows[i].allglue ) {
400 si.rows[i].sized += plus + (extra>0);
401 si.height += plus + (extra>0);
402 --extra;
403 }
404 }
405 }
406 if ((height != si.height) && gb->grow_row>=0 ) {
407 int * ss = &(si.rows[gb->grow_row].sized);
408 int h = si.height - *ss;
409 *ss += (height-si.height);
410 if(*ss < gb->vpad + 3)
411 *ss = gb->vpad + 3;
412 si.height = h + *ss;
413 }
414 if ((height > si.height) && (vrows1!=0)) {
415 plus = (height-si.height)/vrows1;
416 extra = (height-si.height-vrows1*plus);
417 for ( i=0; i<gb->rows; ++i ) {
418 if ( si.rows[i].sized>0 ) {
419 si.rows[i].sized += plus + (extra>0);
420 si.height += plus + (extra>0);
421 --extra;
422 }
423 }
424 }
425 height = si.height;
426 }
427
428 y = gb->g.inner.y;
429 if ( gb->label!=NULL ) {
430 if ( gb->label->state!=gs_invisible )
431 GGadgetResize( gb->label, si.label_width, si.label_height);
432 GGadgetMove( gb->label, gb->g.inner.x+10, y-si.label_height-bp/2);
433 }
434 for ( r=0; r<gb->rows; ++r ) {
435 x = gb->g.inner.x;
436 for ( c=0; c<gb->cols; ++c ) {
437 GGadget *g = gb->children[r*gb->cols+c];
438 if ( g==GG_Glue || g==GG_ColSpan || g==GG_RowSpan || g==GG_HPad10 )
439 /* Skip it */;
440 else {
441 int xes, yes, es;
442 totr = si.rows[r].sized;
443 for ( spanr=1; r+spanr<gb->rows &&
444 gb->children[(r+spanr)*gb->cols+c]==GG_RowSpan; ++spanr )
445 totr += si.rows[r+spanr].sized;
446 totc = si.cols[c].sized;
447 for ( spanc=1; c+spanc<gb->cols &&
448 gb->children[r*gb->cols+c+spanc]==GG_ColSpan; ++spanc )
449 totc += si.cols[c+spanc].sized;
450 if ( r+spanr!=gb->rows ) totr -= gb->vpad;
451 if ( c+spanc!=gb->cols ) totc -= gb->hpad;
452 es = GBoxExtraSpace(g);
453 xes = si.cols[c].extra_space - es;
454 yes = si.rows[r].extra_space - es;
455 if ( g->state!=gs_invisible )
456 GGadgetResize(g,totc-2*xes,totr-2*yes);
457 GGadgetMove(g,x+xes,y+yes);
458 }
459 x += si.cols[c].sized;
460 }
461 y += si.rows[r].sized;
462 }
463
464 free(si.cols); free(si.rows);
465
466 gb->g.inner.width = width; gb->g.inner.height = height;
467 gb->g.r.width = width + 2*bp; gb->g.r.height = height + 2*bp;
468 GDrawEnableExposeRequests(g->base,old_enabled);
469 GDrawRequestExpose(g->base,&g->r,false);
470 }
471
472 static void GHVBoxGetDesiredSize(GGadget *g, GRect *outer, GRect *inner) {
473 struct sizeinfo si;
474 GHVBox *gb = (GHVBox *) g;
475 int bp = GBoxBorderWidth(g->base,g->box);
476
477 GHVBoxGatherSizeInfo(gb,&si);
478 if ( g->desired_width>0 ) si.width = g->desired_width;
479 if ( g->desired_height>0 ) si.height = g->desired_height;
480
481 if ( inner!=NULL ) {
482 inner->x = inner->y = 0;
483 inner->width = si.width; inner->height = si.height;
484 }
485 if ( outer!=NULL ) {
486 outer->x = outer->y = 0;
487 outer->width = si.width+2*bp; outer->height = si.height+2*bp;
488 }
489 free(si.cols); free(si.rows);
490 }
491
492 static int GHVBoxFillsWindow(GGadget *g) {
493 return( true );
494 }
495
496 static int expose_nothing(GWindow pixmap, GGadget *g, GEvent *event) {
497 GHVBox *gb = (GHVBox *) g;
498 GRect r;
499
500 if ( g->state==gs_invisible )
501 return( true );
502
503 if ( gb->label==NULL )
504 GBoxDrawBorder(pixmap,&g->r,g->box,g->state,false);
505 else {
506 r = g->r;
507 r.y += gb->label_height/2;
508 r.height -= gb->label_height/2;
509 GBoxDrawBorder(pixmap,&r,g->box,g->state,false);
510 /* Background is transparent */
511 (gb->label->funcs->handle_expose)(pixmap,gb->label,event);
512 }
513 return( true );
514 }
515
516 struct gfuncs ghvbox_funcs = {
517 0,
518 sizeof(struct gfuncs),
519
520 expose_nothing, /* Expose */
521 _ggadget_noop, /* Mouse */
522 _ggadget_noop, /* Key */
523 NULL,
524 NULL, /* Focus */
525 NULL,
526 NULL,
527
528 _ggadget_redraw,
529 GHVBoxMove,
530 GHVBoxResize,
531 _ggadget_setvisible,
532 _ggadget_setenabled,
533 _ggadget_getsize,
534 _ggadget_getinnersize,
535
536 GHVBox_destroy,
537
538 NULL,
539 NULL,
540 NULL,
541 NULL,
542 NULL,
543
544 NULL,
545 NULL,
546
547 NULL,
548 NULL,
549 NULL,
550 NULL,
551 NULL,
552 NULL,
553 NULL,
554 NULL,
555 NULL,
556 NULL,
557 NULL,
558
559 GHVBoxGetDesiredSize,
560 _ggadget_setDesiredSize,
561 GHVBoxFillsWindow,
562 NULL
563 };
564
565 void GHVBoxSetExpandableCol(GGadget *g,int col) {
566 GHVBox *gb = (GHVBox *) g;
567 if ( col<gb->cols )
568 gb->grow_col = col;
569 }
570
571 void GHVBoxSetExpandableRow(GGadget *g,int row) {
572 GHVBox *gb = (GHVBox *) g;
573 if ( row < gb->rows )
574 gb->grow_row = row;
575 }
576
577 void GHVBoxSetPadding(GGadget *g,int hpad, int vpad) {
578 GHVBox *gb = (GHVBox *) g;
579 gb->hpad = hpad;
580 gb->vpad = vpad;
581 }
582
583 static GHVBox *_GHVBoxCreate(struct gwindow *base, GGadgetData *gd,void *data,
584 int hcnt, int vcnt, GBox *def_box) {
585 GHVBox *gb = calloc(1,sizeof(GHVBox));
586 int i, h, v;
587 GGadgetCreateData *label = (GGadgetCreateData *) (gd->label);
588
589 _GHVBox_Init();
590
591 gd->label = NULL;
592 gb->g.funcs = &ghvbox_funcs;
593 _GGadget_Create(&gb->g,base,gd,data,def_box);
594 gb->rows = vcnt; gb->cols = hcnt;
595 gb->grow_col = gb->grow_row = gb_expandall;
596 gb->hpad = gb->vpad = GDrawPointsToPixels(base,2);
597
598 gb->g.takes_input = false; gb->g.takes_keyboard = false; gb->g.focusable = false;
599
600 if ( label != NULL ) {
601 gb->label = label->ret =
602 (label->creator)(base,&label->gd,label->data);
603 gb->label->contained = true;
604 }
605
606 gb->children = malloc(vcnt*hcnt*sizeof(GGadget *));
607 for ( i=v=0; v<vcnt; ++v ) {
608 for ( h=0; h<hcnt && gd->u.boxelements[i]!=NULL; ++h, ++i ) {
609 GGadgetCreateData *gcd = gd->u.boxelements[i];
610 if ( gcd==GCD_Glue ||
611 gcd==GCD_ColSpan ||
612 gcd==GCD_RowSpan ||
613 gcd==GCD_HPad10 )
614 gb->children[v*hcnt+h] = (GGadget *) gcd;
615 else {
616 gcd->gd.pos.x = gcd->gd.pos.y = 1;
617 gb->children[v*hcnt+h] = gcd->ret =
618 (gcd->creator)(base,&gcd->gd,gcd->data);
619 gcd->ret->contained = true;
620 }
621 }
622 while ( h<hcnt )
623 gb->children[v*hcnt+h++] = GG_Glue;
624 if ( gd->u.boxelements[i]==NULL )
625 ++i;
626 }
627 return( gb );
628 }
629
630 GGadget *GHBoxCreate(struct gwindow *base, GGadgetData *gd,void *data) {
631 GHVBox *gb;
632 int hcnt;
633
634 for ( hcnt=0; gd->u.boxelements[hcnt]!=NULL; ++hcnt );
635 gb = _GHVBoxCreate(base,gd,data,hcnt,1,&hvbox_box);
636
637 return( &gb->g );
638 }
639
640 GGadget *GVBoxCreate(struct gwindow *base, GGadgetData *gd,void *data) {
641 GHVBox *gb;
642 int vcnt;
643
644 for ( vcnt=0; gd->u.boxelements[vcnt]!=NULL; ++vcnt );
645 gb = _GHVBoxCreate(base,gd,data,1,vcnt,&hvbox_box);
646
647 return( &gb->g );
648 }
649
650 GGadget *GHVBoxCreate(struct gwindow *base, GGadgetData *gd,void *data) {
651 GHVBox *gb;
652 int hcnt, vcnt, i;
653
654 for ( hcnt=0; gd->u.boxelements[hcnt]!=NULL; ++hcnt );
655 for ( i=0, vcnt=1; gd->u.boxelements[i]!=NULL || gd->u.boxelements[i+1]!=NULL ; ++i )
656 if ( gd->u.boxelements[i]==NULL )
657 ++vcnt;
658 gb = _GHVBoxCreate(base,gd,data,hcnt,vcnt,&hvbox_box);
659
660 return( &gb->g );
661 }
662
663 GGadget *GHVGroupCreate(struct gwindow *base, GGadgetData *gd,void *data) {
664 GHVBox *gb;
665 int hcnt, vcnt, i;
666
667 for ( hcnt=0; gd->u.boxelements[hcnt]!=NULL; ++hcnt );
668 for ( i=0, vcnt=1; gd->u.boxelements[i]!=NULL || gd->u.boxelements[i+1]!=NULL ; ++i )
669 if ( gd->u.boxelements[i]==NULL )
670 ++vcnt;
671 gb = _GHVBoxCreate(base,gd,data,hcnt,vcnt,&hvgroup_box);
672
673 return( &gb->g );
674 }
675
676 static void _GHVBoxFitWindow(GGadget *g, int center) {
677 GRect outer, cur, screen;
678
679 if ( !GGadgetFillsWindow(g)) {
680 fprintf( stderr, "Call to GHVBoxFitWindow in something not an HVBox\n" );
681 return;
682 }
683 GHVBoxGetDesiredSize(g,&outer, NULL );
684 GDrawGetSize(GDrawGetRoot(NULL),&screen);
685 if ( outer.width > screen.width-20 ) outer.width = screen.width-20;
686 if ( outer.height > screen.height-40 ) outer.height = screen.height-40;
687 GDrawGetSize(g->base,&cur);
688 /* Make any offset symmetrical */
689 outer.width += 2*g->r.x;
690 outer.height += 2*g->r.y;
691 if ( cur.width!=outer.width || cur.height!=outer.height ) {
692 GDrawResize(g->base, outer.width, outer.height );
693 /* We want to get the resize before we set the window visible */
694 /* and window managers make synchronizing an issue... */
695 GDrawSync(GDrawGetDisplayOfWindow(g->base));
696 GDrawProcessPendingEvents(GDrawGetDisplayOfWindow(g->base));
697 GDrawSync(GDrawGetDisplayOfWindow(g->base));
698 GDrawProcessPendingEvents(GDrawGetDisplayOfWindow(g->base));
699 } else
700 GGadgetResize(g, outer.width-2*g->r.x, outer.height-2*g->r.y );
701 if ( center ) {
702 GDrawMove(g->base,(screen.width-outer.width)/2,(screen.height-outer.height)/2);
703 /* I don't think this one matters as much, but try a little */
704 GDrawSync(GDrawGetDisplayOfWindow(g->base));
705 GDrawProcessPendingEvents(GDrawGetDisplayOfWindow(g->base));
706 }
707 }
708
709 void GHVBoxFitWindow(GGadget *g) {
710 _GHVBoxFitWindow(g,false);
711 }
712
713 void GHVBoxFitWindowCentered(GGadget *g) {
714 _GHVBoxFitWindow(g,true);
715 }
716
717 void GHVBoxReflow(GGadget *g) {
718 GHVBoxResize(g, g->r.width, g->r.height);
719 }