File: LayoutBox.c

package info (click to toggle)
gridengine 6.2-4
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 51,532 kB
  • ctags: 51,172
  • sloc: ansic: 418,155; java: 37,080; sh: 22,593; jsp: 7,699; makefile: 5,292; csh: 4,244; xml: 2,901; cpp: 2,086; perl: 1,895; tcl: 1,188; lisp: 669; ruby: 642; yacc: 393; lex: 266
file content (1011 lines) | stat: -rw-r--r-- 30,631 bytes parent folder | download | duplicates (3)
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
/* 
 * Motif Tools Library, Version 3.1
 * $Id$
 * 
 * Written by David Flanagan.
 * Copyright (c) 1992-2001 by David Flanagan.
 * All Rights Reserved.  See the file COPYRIGHT for details.
 * This is open source software.  See the file LICENSE for details.
 * There is no warranty for this software.  See NO_WARRANTY for details.
 *
 * $Log$
 * Revision 1.1.1.1  2001/07/18 11:06:02  root
 * Initial checkin.
 *
 * Revision 1.2  2001/06/12 16:25:28  andre
 * *** empty log message ***
 *
 *
 */

#include <Xmt/XmtP.h>
#include <Xmt/LayoutGP.h>

#define offset(field) XtOffsetOf(XmtLayoutBoxRec, layout_box.field)
static XtResource resources[] = {
{XmtNorientation, XmCOrientation, XmROrientation,
     sizeof(unsigned char), offset(orientation),
     XtRImmediate, (XtPointer)XmHORIZONTAL},	 
{XmtNbackground, XtCBackground, XtRPixel,
     sizeof(Pixel), offset(background),
     XtRImmediate, (XtPointer) -1},
{XmtNequal, XmtCEqual, XtRBoolean,
     sizeof(Boolean), offset(equal),
     XtRImmediate, (XtPointer)False},
{XmtNspaceType, XmtCSpaceType, XmtRXmtLayoutSpaceType,
     sizeof(unsigned char), offset(space_type),
     XtRImmediate, (XtPointer)XmtLayoutSpaceNone},
{XmtNspace, XmtCSpace, XtRDimension,
     sizeof(Dimension), offset(space),
     XtRImmediate, (XtPointer) 0},
{XmtNspaceStretch, XmtCSpaceStretch, XtRDimension,
     sizeof(Dimension), offset(space_stretch),
     XtRImmediate, (XtPointer) 1},
{XmtNitemStretch, XmtCItemStretch, XtRDimension,
     sizeof(Dimension), offset(item_stretch),
     XtRImmediate, (XtPointer) 1},
};
#undef offset

#if NeedFunctionPrototypes
static void Initialize(Widget, Widget, ArgList, Cardinal *);
static void Destroy(Widget);
static Boolean SetValues(Widget, Widget, Widget, ArgList, Cardinal *);
static void Resize(Widget);
static void Redisplay(Widget, XEvent *, Region);
static XtGeometryResult QueryGeometry(Widget, XtWidgetGeometry *,
                                      XtWidgetGeometry *);
#else
static void Initialize();
static void Destroy();
static Boolean SetValues();
static void Resize();
static void Redisplay();
static XtGeometryResult QueryGeometry();
#endif

#define superclass (&xmtLayoutGadgetClassRec)

externaldef(xmtlayoutboxclassrec) XmtLayoutBoxClassRec xmtLayoutBoxClassRec = {
{   /* rect_class fields  */
    /* superclass	  */	(WidgetClass)superclass,
    /* class_name	  */	"XmtLayoutBox",
    /* widget_size	  */	sizeof(XmtLayoutBoxRec),
    /* class_initialize   */    NULL,
    /* class_part_initialize*/	NULL,
    /* class_inited       */	FALSE,
    /* initialize	  */	Initialize,
    /* initialize_hook    */	NULL,		
    /* rect1		  */	NULL,
    /* rect2		  */	NULL,
    /* rect3	  	  */	0,
    /* resources	  */	resources,
    /* num_resources	  */	XtNumber(resources),
    /* xrm_class	  */	NULLQUARK,
    /* rect4		  */	FALSE,
    /* rect5		  */	FALSE,
    /* rect6		  */ 	FALSE,
    /* rect7		  */	FALSE,
    /* destroy		  */	Destroy,
    /* resize		  */	Resize,
    /* expose		  */	Redisplay,
    /* set_values	  */	SetValues,
    /* set_values_hook    */	NULL,			
    /* set_values_almost  */	XtInheritSetValuesAlmost,  
    /* get_values_hook    */	NULL,			
    /* rect9		  */	NULL,
    /* version		  */	XtVersion,
    /* callback_offsets   */    NULL,
    /* rect10	          */    NULL,
    /* query_geometry	  */	QueryGeometry,
    /* rect11		  */	NULL,
    /* extension	    */  NULL
  },
  { /* XmtLayoutGadget field */
    /* change_font        */    NULL
  },
  { /* XmtLayoutBox fields */
    /* extension          */	NULL
  }
};

externaldef(xmtlayoutboxgadgetclass)
WidgetClass xmtLayoutBoxGadgetClass = (WidgetClass) &xmtLayoutBoxClassRec;

/* ARGSUSED */
#if NeedFunctionPrototypes
static void Initialize(Widget request, Widget init,
		       ArgList arglist, Cardinal *num_args)
#else
static void Initialize(request, init, arglist, num_args)
Widget request;
Widget init;
ArgList arglist;
Cardinal *num_args;
#endif
{
    XmtLayoutBoxGadget lb = (XmtLayoutBoxGadget)init;

    if (lb->layout_box.orientation == XmHORIZONTAL)
	Constraint(init, type) = XmtLayoutRow;
    else
	Constraint(init, type) = XmtLayoutCol;
	
    if (lb->layout_box.background != (Pixel)-1) {
	XGCValues gcv;
	gcv.foreground = lb->layout_box.background;
	lb->layout_box.gc = XtGetGC(init, GCForeground, &gcv);
    }
    else
	lb->layout_box.gc = NULL;
}

#if NeedFunctionPrototypes
static void Destroy(Widget w)
#else
static void Destroy(w)
Widget w;
#endif
{
    XmtLayoutBoxGadget lb = (XmtLayoutBoxGadget)w;
    Widget child;

    if (lb->layout_box.gc) XtReleaseGC(w, lb->layout_box.gc);

    /*
     * we destroy anything within this box.  This seems as reasonable
     * a thing to do as moving those widgets elsewhere.  Except if
     * the entire Layout is being destroyed, in which case we can
     * skip this.
     */
    if (!(XtParent(w))->core.being_destroyed)
	ForEachItem(lb, child) XtDestroyWidget(child);
}

/* ARGSUSED */
#if NeedFunctionPrototypes
static Boolean SetValues(Widget current, Widget request, Widget set,
			 ArgList arglist, Cardinal *num_args)
#else
static Boolean SetValues(current, request, set, arglist, num_args)
Widget current;
Widget request;
Widget set;
ArgList arglist;
Cardinal *num_args;
#endif
{
    XmtLayoutBoxGadget cg = (XmtLayoutBoxGadget) current;
    XmtLayoutBoxGadget sg = (XmtLayoutBoxGadget) set;
    Boolean redisplay = False;
    Boolean relayout = False;

    if (sg->layout_box.background != cg->layout_box.background) {
	XGCValues gcv;
	if (sg->layout_box.gc) XtReleaseGC(set, sg->layout_box.gc);
	if (sg->layout_box.background != -1) {
	    gcv.foreground = sg->layout_box.background;
	    sg->layout_box.gc = XtGetGC(set, GCForeground, &gcv);
	}
	else
	    sg->layout_box.gc = NULL;
	redisplay = True;
    }

    if (sg->layout_box.orientation != cg->layout_box.orientation) {
	if ((sg->layout_box.orientation != XmHORIZONTAL) &&
	    (sg->layout_box.orientation != XmVERTICAL))
	    sg->layout_box.orientation = cg->layout_box.orientation;
	else
	    relayout = True;
    }

    if ((sg->layout_box.space != cg->layout_box.space) ||
	(sg->layout_box.space_type != cg->layout_box.space_type) ||
	(sg->layout_box.space_stretch!=cg->layout_box.space_stretch) ||
        (sg->layout_box.item_stretch !=
	 cg->layout_box.item_stretch) ||
	(sg->layout_box.equal != cg->layout_box.equal))
	relayout = True;

    if (relayout) 
	_XmtLayoutChildren((XmtLayoutWidget)XtParent(set), True);

    return redisplay | relayout;
}

	
/*
 * The story with margins and frames and captions:
 * Any child (including boxes) of an XmtLayout widget can have a
 * frame and/or a caption and/or a margin.
 * The XmtLayout widget will draw the frames and captions appropriately.
 * The size of the frame, caption and margin are not included in any child's
 * core x,y,width, and height fields.  This includes boxes.
 * The children should not include the frame, caption or margin when
 * calculating their preferred size.
 * The frame, caption and margin *are* included when doing stretching 
 * and shrinking calculations.
 * The frame, caption and margin are not included when adjusting the
 * size of children in an "equal" row or column.
 * If the programmer specifies a size for the child, then that size
 * does not include any frame or caption or margin.
 * The frame and caption are inside the margin.
 */

    
#if NeedFunctionPrototypes
static void HorizontalMargins(XmtLayoutWidget lw, Widget child,
			      int *total, int *left)
#else
static void HorizontalMargins(lw, child, total, left)
XmtLayoutWidget lw;
Widget child;
int *total;
int *left;
#endif
{
    XmtLayoutConstraintsPart *cc = LayoutConstraintsRec(child);
    int caption = 0;
    Boolean left_caption = False;
    XmtLayoutFrameType frame_type;
    XmtLayoutFramePosition frame_position;
    int frame = 0;

    *total = 2*cc->margin_width;
    *left = cc->margin_width;
    
    if (cc->caption) {
	if ((cc->caption_position == XmtLayoutLeft) ||
	    (cc->caption_position == XmtLayoutRight)) {
	    caption = cc->caption_width + cc->caption_margin;
	    *total += caption;
	    if (cc->caption_position == XmtLayoutLeft) {
		left_caption = True;
		*left += caption;
	    }
	}
    }

    /* if there is no frame, we're done */
    if ((cc->frame_type == XmtLayoutFrameNone) &&
	!lw->layout.debug_layout)
	return;

    /* get the frame args.  If debug_layout is set, force fixed values */
    if (lw->layout.debug_layout) {
	frame_type = XmtLayoutFrameBox;
	frame_position = XmtLayoutFrameOutside;
	frame = 6;
    }
    else {
	frame_type = cc->frame_type;
	frame_position = cc->frame_position;
	frame = cc->frame_margin + cc->frame_thickness;
    }
    
    if (frame_type == XmtLayoutFrameBox) {
	if (!left_caption || (frame_position!=XmtLayoutFrameThrough))
	    *left += frame;
	else if (frame > caption)
	    *left += frame - caption;

    	*total += frame;
	if (!caption || (frame_position != XmtLayoutFrameThrough))
	    *total += frame;
	else if (frame > caption)
	    *total += frame - caption;
    }
    else if (frame_type == XmtLayoutFrameLeft) {
	if (!left_caption || (frame_position != XmtLayoutFrameThrough)) {
	    *left += frame;
	    *total += frame;
	}
	else if (frame > caption) {
	    *left += frame - caption;
	    *total += frame - caption;
	}
    }
    else if (frame_type == XmtLayoutFrameRight) {
	if (!caption || left_caption ||
	    (frame_position != XmtLayoutFrameThrough))
	    *total += frame;
	else if (frame > caption)
	    *total += frame - caption;
    }
}


#if NeedFunctionPrototypes
static void VerticalMargins(XmtLayoutWidget lw, Widget child,
			    int *total, int *top)
#else
static void VerticalMargins(lw, child, total, top)
XmtLayoutWidget lw;
Widget child;
int *total;
int *top;
#endif
{
    XmtLayoutConstraintsPart *cc = LayoutConstraintsRec(child);
    int caption = 0;
    Boolean top_caption = False;
    XmtLayoutFrameType frame_type;
    XmtLayoutFramePosition frame_position;
    int frame = 0;

    *total = 2*cc->margin_height;
    *top = cc->margin_height;
    
    if (cc->caption) {
	if ((cc->caption_position == XmtLayoutTop) ||
	    (cc->caption_position == XmtLayoutBottom)) {
	    caption = cc->caption_height + cc->caption_margin;
	    *total += caption;
	    if (cc->caption_position == XmtLayoutTop) {
		top_caption = True;
		*top += caption;
	    }
	}
    }
    
    /* if there is no frame, we're done */
    if ((cc->frame_type == XmtLayoutFrameNone) &&
	!lw->layout.debug_layout)
	return;

    /* get the frame args.  If debug_layout is set, force fixed values */
    if (lw->layout.debug_layout) {
	frame_type = XmtLayoutFrameBox;
	frame_position = XmtLayoutFrameOutside;
	frame = 6;
    }
    else {
	frame_type = cc->frame_type;
	frame_position = cc->frame_position;
	frame = cc->frame_margin + cc->frame_thickness;
    }

    if (frame_type == XmtLayoutFrameBox) {
	if (!top_caption || (frame_position != XmtLayoutFrameThrough))
	    *top += frame;
	else if (frame > caption)
	    *top += frame - caption;

    	*total += frame;
	if (!caption || (frame_position != XmtLayoutFrameThrough))
	    *total += frame;
	else if (frame > caption)
	    *total += frame - caption;
    }
    else if (frame_type == XmtLayoutFrameTop) {
	if (!top_caption || (frame_position != XmtLayoutFrameThrough)) {
	    *top += frame;
	    *total += frame;
	}
	else if (frame > caption) {
	    *top += frame - caption;
	    *total += frame - caption;
	}
    }
    else if (frame_type == XmtLayoutFrameBottom) {
	if (!caption || top_caption ||
	    (frame_position != XmtLayoutFrameThrough))
	    *total += frame;
	else if (frame > caption)
	    *total += frame - caption;
    }
}

#if NeedFunctionPrototypes
static void HideHomelessChildren(Widget w)
#else
static void HideHomelessChildren(w)
Widget w;
#endif
{
    XmtLayoutBoxGadget lb = (XmtLayoutBoxGadget)w;
    Widget child;
    XmtLayoutConstraintsPart *cc;  /* child constraints */

    /*
     * If this box is already positioned outside of the window,
     * then it and its descendants are already hidden.
     */
    if ((w->core.x + w->core.width < 0) &&
	(w->core.y + w->core.height < 0)) return;

    /*
     * Otherwise, we've got to move this box and everything it
     * contains (recursively) out of the window.
     */
    XmtSetRectObj(w);
    XtMoveWidget(w, -w->core.width-20, -w->core.height-20);
    XmtUnsetRectObj(w);

    ForEachItem(lb, child) {
	cc = LayoutConstraintsRec(child);

	/*
	 * If the child is itself a container, then recurse on it,
	 * to hide its contents.
	 */
	if ((cc->type == XmtLayoutRow) || (cc->type == XmtLayoutCol))
	    HideHomelessChildren(child);

	/*
	 * hide the child.  We do this differently depending on whether
	 * the child is an XmtLayoutGadget or not.
	 */
	if (cc->type == XmtLayoutChild)
	    XtMoveWidget(child, -child->core.width-20, -child->core.height-20);
	else {
	    XmtSetRectObj(child);
	    XtMoveWidget(child, -child->core.width-20, -child->core.height-20);
	    XmtUnsetRectObj(child);
	}
    }
}

#if NeedFunctionPrototypes
static void Resize(Widget w)
#else
static void Resize(w)
Widget w;
#endif
{
    XmtLayoutWidget lw = (XmtLayoutWidget)XtParent(w);
    XmtLayoutBoxGadget lb = (XmtLayoutBoxGadget)w;
    XmtLayoutConstraintsPart *c = LayoutConstraintsRec(w);
    XmtLayoutConstraintsPart *cc;  /* child constraints */
    Boolean row;
    int extra_space;
    int child_num;
    Widget child;
    double factor;    /* "glue set ratio" */
    Boolean stretch = True;
    int major_start, minor_start;    /* instead of x, y */
    int major_length, minor_length;  /* instead of width, height */
    int cx, cy;  /* child x, y */
    int cw, ch;  /* child w, h */
    int *cmajor0, *cminor0;    /* addresses of child major & minor coord */
    int *cmajorlen, *cminorlen; /* addresses of child dimensions */
    int next;
    int margin_width, margin_height, top_margin, left_margin;
    int *major_margin, *minor_margin;
    int space_stretch;
    int total_space, space, interval = 0;
    
    if (!XtIsManaged(w)) return;

    if (lb->layout_box.orientation == XmHORIZONTAL) row = True;
    else row = False;

    if (row) {
	major_start = w->core.x;
	minor_start = w->core.y;
	major_length = w->core.width;
	minor_length = w->core.height;
	cmajor0 = &cx; cminor0 = &cy;
	cmajorlen = &cw; cminorlen = &ch;
	major_margin = &margin_width;
	minor_margin = &margin_height;
    }
    else {
	major_start = w->core.y;
	minor_start = w->core.x;
	major_length = w->core.height;
	minor_length = w->core.width;
	cmajor0 = &cy; cminor0 = &cx;
	cmajorlen = &ch; cminorlen = &cw;
	major_margin = &margin_height;
	minor_margin = &margin_width;
    }

    if (major_length <= 0) major_length = 1;
    if (minor_length <= 0) minor_length = 1;

    if (row)
	extra_space = major_length - c->pref_w;
    else
	extra_space = major_length - c->pref_h;
    
    /*
     * figure out stretch or shrink factor.
     * The total_stretch figure includes the stretchiness of the spaces,
     * if any.
     */
    if (extra_space > 0) {
	if (lb->layout_box.total_stretch == 0) factor = 0.0;
	else factor = extra_space/(double)lb->layout_box.total_stretch;
	stretch = True;
    }
    else if (extra_space < 0) {
	if (lb->layout_box.total_shrink == 0) factor = 0.0;
	else factor = -extra_space/(double)lb->layout_box.total_shrink;
	stretch = False;
    }
    else factor = 0.0;	

    /* compute the size of the interval for Interval or Tabbed spacing */
    if (lb->layout_box.space_type == XmtLayoutSpaceInterval)
	interval = major_length / ((int)lb->layout_box.num_items+1);
    else if ((lb->layout_box.space_type == XmtLayoutSpaceLTabbed) ||
	     (lb->layout_box.space_type == XmtLayoutSpaceCTabbed) ||
	     (lb->layout_box.space_type == XmtLayoutSpaceRTabbed)){
	if (lb->layout_box.num_items <= 1)
	    interval = major_length;
	else 
	    interval = major_length / ((int)lb->layout_box.num_items);
    }
	
    /* compute the size of the internal spaces, if any */
    if ((lb->layout_box.space_type == XmtLayoutSpaceEven) ||
	(lb->layout_box.space_type == XmtLayoutSpaceLREven)) {
	space = lb->layout_box.space;
	if (stretch) {
	    int num_spaces;
	    if (lb->layout_box.space_type == XmtLayoutSpaceEven)
		num_spaces = lb->layout_box.num_items + 1;
	    else {
		num_spaces = lb->layout_box.num_items - 1;
		if (num_spaces <= 0) num_spaces = 1;
	    }

	    if ((lb->layout_box.item_stretch == 0) ||
		(lb->layout_box.total_stretch == 0)) {
		/* in this case, all the stretchiness goes to the spaces */
		space += extra_space/num_spaces;
	    }
	    else {
		/* otherwise, spaces only get some of the extra space */
		space_stretch = lb->layout_box.total_stretch *
		    (int) lb->layout_box.space_stretch /
			((int) (lb->layout_box.space_stretch +
				lb->layout_box.item_stretch));
		/* rounding works better when we compute it this way */
		total_space = space * num_spaces + space_stretch * factor;
		space = total_space / num_spaces;
	    }
	}
    }
    else space = 0;

    /* compute the major position of the first child */
    *cmajor0 = major_start;
    if (lb->layout_box.space_type == XmtLayoutSpaceEven)
	*cmajor0 += space;

    /* loop through all items in the box */
    child_num = 0;
    ForEachItem(lb, child) {
	cc = LayoutConstraintsRec(child);
	/*
	 * If this is an unmanaged child, we don't have to lay it out.
	 * With the exception, however, that if it is an unmanaged row
	 * or column, we've got to make sure that the contents of that
	 * row or column are hidden.
	 */
	if (!XtIsManaged(child)) {
	    if ((cc->type == XmtLayoutRow) || (cc->type == XmtLayoutCol))
		HideHomelessChildren(child);
	    continue;
	}
	child_num++;
	
	/* Get basic child size and add margins */
	HorizontalMargins(lw, child, &margin_width, &left_margin);
	VerticalMargins(lw, child, &margin_height, &top_margin);
	cw = cc->pref_w + margin_width;
	ch = cc->pref_h + margin_height;

	/* if the box is "Equal", adjust major size of most children */
	if (lb->layout_box.equal) {
	    if ((cc->type != XmtLayoutSpace) &&
		(cc->type != XmtLayoutHSep) &&
		(cc->type != XmtLayoutVSep))
		*cmajorlen = lb->layout_box.equal_size + *major_margin;
	}
	
	/*
	 * stretch or shrink the child's width as needed.
	 * but don't stretch if we've explictly given all stretchiness to
	 * the spaces.  
	 */
	if (factor != 0.0) {
	    if (stretch) {
		if (lb->layout_box.item_stretch != 0)
		    *cmajorlen += (int)(cc->stretchability * factor);
	    }
	    else {
		*cmajorlen -= (int)(cc->shrinkability * factor);
	    }
	}
	
 	/*
	 * And never shrink to less than the size of the margins,
	 * because otherwise the child will have a negative size.
	 */
	if (*cmajorlen <= *major_margin) *cmajorlen = *major_margin+1;

	/* set the minor coordinate depending on justification */
	switch(cc->justification) {
	case XmtLayoutCentered:
	    *cminor0 = minor_start + (minor_length - *cminorlen)/2;
	    break;
	case XmtLayoutFlushLeft:
	    *cminor0 = minor_start;
	    break;
	case XmtLayoutFlushRight:
	    *cminor0 = minor_start + minor_length - *cminorlen;
	    break;
	case XmtLayoutFilled:
	    *cminor0 = minor_start;
	    *cminorlen = minor_length;
	    break;
	}

 	/*
	 * But don't let the minor size be less than the size of the margins,
	 * because otherwise the child will have a negative size.
	 */
	if (*cminorlen <= *minor_margin) *cminorlen = *minor_margin+1;

	/*
	 * for Interval spacing, center the child around the interval.
	 * for Tabbed spacing, position appropriately to the "tab stop".
	 * for LCR spacing, left, center or right justify the child.
	 * for other kinds, remember the starting point of next child.
	 */
	if (lb->layout_box.space_type == XmtLayoutSpaceInterval)
	    *cmajor0 = major_start + interval * child_num - *cmajorlen/2;
	else if (lb->layout_box.space_type == XmtLayoutSpaceLTabbed) 
	    *cmajor0 = major_start + interval * (child_num-1);
	else if (lb->layout_box.space_type == XmtLayoutSpaceCTabbed) 
	    *cmajor0 = major_start + interval * child_num
		- interval/2 - *cmajorlen/2;
	else if (lb->layout_box.space_type == XmtLayoutSpaceRTabbed) 
	    *cmajor0 = major_start + interval * child_num - *cmajorlen;
	else if (lb->layout_box.space_type == XmtLayoutSpaceLCR) {
	    /* first child is already handled */
	    if (child_num == 2)
		*cmajor0 = major_start + (major_length - *cmajorlen)/2;
	    else if (child_num == 3)
		*cmajor0 = major_start + major_length - *cmajorlen;
	}

	/*
	 * figure out where this child ends,
	 * so we know where the next begins (for some space types)
	 */
	next = *cmajor0 + *cmajorlen;
	
	/* indent by the size of the margins, frame and captions */
	cx += left_margin;
	cy += top_margin;
	cw -= margin_width;
	ch -= margin_height;
	
	/* set the child size and location */
	if (XmtIsLayoutGadget(child)) {
	    /*
	     * fake out _XmConfigureObject:
	     * 1) make the XmtLayoutGadget look like a RectObj, briefly.
	     * 2) if this is a box, make sure this is not a no-op
	     *    by setting the child's width to something other than cw.
	     */
	    XmtSetRectObj(child);
	    if (XtClass(child) == xmtLayoutBoxGadgetClass)
		child->core.width = 0;
	    _XmtConfigureObject(child, cx, cy, cw, ch,
				child->core.border_width);
	    XmtUnsetRectObj(child);
	    
	}
	else
	    _XmtConfigureObject(child, cx, cy, cw, ch,
				child->core.border_width);

	/* set starting position for next child for space types that care */
	*cmajor0 = next + space;
    }
}

/* ARGSUSED */
#if NeedFunctionPrototypes
static XtGeometryResult QueryGeometry(Widget w,
				      XtWidgetGeometry *request,
				      XtWidgetGeometry *reply)
#else
static XtGeometryResult QueryGeometry(w, request, reply)
Widget w;
XtWidgetGeometry *request;
XtWidgetGeometry *reply;
#endif
{
    XmtLayoutBoxGadget lb = (XmtLayoutBoxGadget) w;
    XmtLayoutWidget lw = (XmtLayoutWidget) XtParent(w);
    XmtLayoutConstraintsPart *cc;
    int total_width, total_height;
    int total_stretch, total_shrink;
    int max_width, max_height;
    int equal_width, equal_height;
    int total_equal_width, total_equal_height;
    int width, height;
    int margin_width, margin_height, top_margin, left_margin;
    int num_items;
    XtWidgetGeometry geometry;
    Widget child;

    total_width = total_height = 0;
    total_stretch = total_shrink = 0;
    equal_width = equal_height = total_equal_width = total_equal_height = 0;
    max_width = max_height = 0;
    num_items = 0;

    /* figure out the size of each child */
    ForEachItem(lb, child) {
	/*
	 * For each managed child:
	 *   figure out the basic size w/o margins.  This may be a hardcoded
	 *     or a preferred size, or it may be the caption size.
	 *   if this is an "equal" box
	 *     compute the maximum marginless size
	 *     compute the total size of non-spaces and non-separators.
	 *   add the margins in
	 *   compute the total and maxmimum sizes with margins
	 *   compute the total stretchability and shrinkability
	 */
	if (!XtIsManaged(child)) continue;
	num_items++;
	cc = LayoutConstraintsRec(child);
	 
	/*
	 * Figure out the basic size of the child without margins.
	 * This is either the preferred size of the child, or the
	 * sizes specified on the layoutWidth and layoutHeight resources.
	 * In either case, this basic size may have to be adjusted if it
	 * is too small for the specified caption.
	 */
	 
	/*
	 * ask the child its preferred size, unless it will be overridden
	 * by resources and the child is not a box we need to recursively
	 * measure.
	 */
	if ((cc->width == 0) || (cc->height == 0) ||
	    (cc->type==XmtLayoutRow) || (cc->type==XmtLayoutCol))
	    XtQueryGeometry(child, NULL, &geometry);

	/* override widget's preferences with resources, if specified */
	if (cc->width) width = cc->width;
	else width = geometry.width;
	if (cc->height) height = cc->height;
	else height = geometry.height;

	/*
	 * if a top or bottom caption is wider than this width, or a
	 * left or right caption is taller than this height, increase
	 * the basic size to match.
	 */
	if (cc->caption) {
	    if ((cc->caption_position == XmtLayoutTop) ||
		(cc->caption_position == XmtLayoutBottom)) {
		if (cc->caption_width > width) width = cc->caption_width;
	    }
	    else { /* caption on the right or left */
		if (cc->caption_height > height) height = cc->caption_height;
	    }
	}

	/* remember this basic preferred or specified size */
	cc->pref_w = width;
	cc->pref_h = height;

	/* handle rows or columns that are "equal" */
	if (lb->layout_box.equal) {
	    /* get maximum marginless sizes */
	    if (width > equal_width) equal_width = width;
	    if (height > equal_height) equal_height = height;

	    /* compute the total size of non-spaces and separators */
	    if ((cc->type != XmtLayoutSpace) &&
		(cc->type != XmtLayoutHSep) &&
		(cc->type != XmtLayoutVSep)) {
		total_equal_width += width;
		total_equal_height += height;
	    }
	}
	    
	/* compute the margins, and add them in */
	HorizontalMargins(lw, child, &margin_width, &left_margin);
	VerticalMargins(lw, child, &margin_height, &top_margin);
	width += margin_width;
	height += margin_height;
	
	/* compute total and maximum sizes */
	total_width += width;
	total_height += height;
	if (max_width < width) max_width = width;
	if (max_height < height) max_height = height;

	/* compute total stretch and shrink */
	total_stretch += cc->stretchability;
	total_shrink += cc->shrinkability;
    }
    
    /*
     * if the row or column has the Equal flag set, go back through
     * the children and recompute the box width or height.
     * Note that spaces and separators aren't affected.
     */
    if (lb->layout_box.equal) {
	if (lb->layout_box.orientation == XmHORIZONTAL)
	    total_width -= total_equal_width;
	else
	    total_height -= total_equal_height;
	
	ForEachItem(lb, child) {
	    if (!XtIsManaged(child)) continue;
	    cc = LayoutConstraintsRec(child);
	    if ((cc->type != XmtLayoutSpace) &&
		(cc->type != XmtLayoutHSep) &&
		(cc->type != XmtLayoutVSep)) {
		if (lb->layout_box.orientation == XmHORIZONTAL)
		    total_width += equal_width;
		else
		    total_height += equal_height;
	    }
	}
    }

    /* check for <= 3 children with LCR spacing */
    if ((lb->layout_box.space_type == XmtLayoutSpaceLCR) && (num_items > 3)) {
	lb->layout_box.space_type = XmtLayoutSpaceLREven;
	XmtWarningMsg("XmtLayout", "lcr",
		      "Widget '%s':\n\tSpace type is LCR, but number of childre is > 3.\n\tUsing LREven instead.",
		      XtName((Widget)lw));
    }

    /* adjust sizes depending on space model */
    if ((lb->layout_box.space_type == XmtLayoutSpaceEven) ||
	(lb->layout_box.space_type == XmtLayoutSpaceInterval)) {
	total_width += lb->layout_box.space * (num_items + 1);
	total_height += lb->layout_box.space * (num_items + 1);
    }
    else if ((lb->layout_box.space_type == XmtLayoutSpaceLREven) ||
	     (lb->layout_box.space_type == XmtLayoutSpaceLCR)) {
	total_width += lb->layout_box.space * (num_items - 1);
	total_height += lb->layout_box.space * (num_items - 1);
    }
    else if ((lb->layout_box.space_type == XmtLayoutSpaceLTabbed) ||
	     (lb->layout_box.space_type == XmtLayoutSpaceCTabbed) ||
	     (lb->layout_box.space_type == XmtLayoutSpaceRTabbed)) {
	total_width += lb->layout_box.space * num_items;
	total_height += lb->layout_box.space * num_items;
    }

    /* adjust the total_stretch to account for the spaces */
    if (lb->layout_box.space_type != XmtLayoutSpaceNone) {
	if (lb->layout_box.item_stretch == 0)
	    total_stretch = INFINITY;
	else
	    total_stretch = total_stretch + total_stretch *
		(int)lb->layout_box.space_stretch /
		    (int)lb->layout_box.item_stretch;
    }
    
    /* record information that we'll need when we lay this box out */
    lb->layout_box.total_stretch = total_stretch;
    lb->layout_box.total_shrink =  total_shrink;
    if (lb->layout_box.orientation == XmHORIZONTAL)
	lb->layout_box.equal_size = equal_width;
    else
	lb->layout_box.equal_size = equal_height;
    lb->layout_box.num_items = num_items;

    /* reply to the geometry query */
    reply->request_mode = CWWidth | CWHeight;
    if (lb->layout_box.orientation == XmHORIZONTAL) {
	reply->width = total_width;
	reply->height = max_height;
    }
    else {
	reply->width = max_width;
	reply->height = total_height;
    }
    return XtGeometryYes;
}

#if NeedFunctionPrototypes
static void Redisplay(Widget w, XEvent *event, Region region)
#else
static void Redisplay(w, event, region)
Widget w;
XEvent *event;
Region region;
#endif
{
    XmtLayoutBoxGadget lb = (XmtLayoutBoxGadget) w;
    XmtLayoutWidget lw = (XmtLayoutWidget)XtParent(w);
    Widget child;
    XRectangle rect;
    Boolean filled = False;

    if (lw->core.window == None) return;

    if (lb->layout_box.gc != NULL) {
	XFillRectangle(lw->core.screen->display, lw->core.window,
		       lb->layout_box.gc,
		       lb->rectangle.x, lb->rectangle.y,
		       lb->rectangle.width, lb->rectangle.height);
	if (region) {
	    rect.x = lb->rectangle.x;
	    rect.y = lb->rectangle.y;
	    rect.width = lb->rectangle.width;
	    rect.height = lb->rectangle.height;
	    XUnionRectWithRegion(&rect, region, region);
	}
	filled = True;
    }

    ForEachItem(lb, child) {
        if (XmtIsLayoutGadget(child) && XtIsManaged(child)) {
	    if (!region || filled ||
		XRectInRegion(region, child->core.x, child->core.y,
			      child->core.width, child->core.height))
		_XmtRedisplayGadget(child, event, region);
	}
    }
}

#if NeedFunctionPrototypes
Widget XmtCreateLayoutBox(Widget parent, String name,
			  ArgList arglist, Cardinal num_args)
#else
Widget XmtCreateLayoutBox(parent, name, arglist, num_args)
Widget parent;
String name;
ArgList arglist;
Cardinal num_args;
#endif
{
    return XtCreateWidget(name, xmtLayoutBoxGadgetClass, parent,
			  arglist, num_args);
}

#if NeedFunctionPrototypes
Widget XmtCreateLayoutRow(Widget parent, String name,
			  ArgList arglist, Cardinal num_args)
#else
Widget XmtCreateLayoutRow(parent, name, arglist, num_args)
Widget parent;
String name;
ArgList arglist;
Cardinal num_args;
#endif
{
    Widget w;
    w = XtVaCreateWidget(name, xmtLayoutBoxGadgetClass, parent,
			 XmtNorientation, XmHORIZONTAL, NULL);
    XtSetValues(w, arglist, num_args);
    return w;
}

#if NeedFunctionPrototypes
Widget XmtCreateLayoutCol(Widget parent, String name,
			  ArgList arglist, Cardinal num_args)
#else
Widget XmtCreateLayoutCol(parent, name, arglist, num_args)
Widget parent;
String name;
ArgList arglist;
Cardinal num_args;
#endif
{
    Widget w;
    w = XtVaCreateWidget(name, xmtLayoutBoxGadgetClass, parent,
			 XmtNorientation, XmVERTICAL, NULL);
    XtSetValues(w, arglist, num_args);
    return w;
}