summaryrefslogtreecommitdiff
path: root/gui/layouts.inc
blob: 9f2f3bb09eb11d03ce56716888be2919c09e21ed (plain)
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
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
{
    fpGUI  -  Free Pascal Graphical User Interface
    Copyright (C) 2000 - 2001 by
      Areca Systems GmbH / Sebastian Guenther
    Copyright (C) 2006 by Graeme Geldenhuys 
      member of the fpGUI development team.

    Layout class declarations

    See the file COPYING.fpGUI, included in this distribution,
    for details about the copyright.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

 **********************************************************************}


{%mainunit fpgui.pp}

{$IFDEF read_interface}

// ===================================================================
//   Layouts
// ===================================================================

type

  ELayoutError = class(Exception);

  THorzAlign = (horzLeft, horzCenter, horzRight, horzFill);
  TVertAlign = (vertTop, vertCenter, vertBottom, vertFill);

  TLayoutItem = class(TCollectionItem)
  private
    FWidget: TWidget;
  published
    property Widget: TWidget read FWidget write FWidget;
  end;

  TWidgetArrayInfo = record
    min, def, max: Integer;
    MinFlag, MaxFlag: Boolean;
  end;

  TWidgetArrayInfoArray = array[0..(1 shl 30) div SizeOf(TWidgetArrayInfo) - 1] of TWidgetArrayInfo;
  PWidgetArrayInfoArray = ^TWidgetArrayInfoArray;


  TLayout = class(TContainerWidget)
  protected
    FWidgets: TCollection;
    FBorderSpacing: Integer;
    IsRecalcingLayout: Boolean;
    function    GetChildCount: Integer; override;
    function    GetChild(Index: Integer): TWidget; override;
    procedure   AddFixedChild(AChild: TWidget);
    property    BorderSpacing: Integer read FBorderSpacing write FBorderSpacing;
  public
    constructor Create(AOwner: TComponent); override;
    destructor  Destroy; override;
    function    ContainsChild(AChild: TWidget): Boolean; override;
  end;


// -------------------------------------------------------------------
//   FixedLayout
// -------------------------------------------------------------------

  TFixedItem = class(TLayoutItem)
  public
    Left: Integer;
    Top: Integer;
  end;


  { TFixedLayout }

  TFixedLayout = class(TLayout)
  protected
    procedure   CalcSizes; override;
  public
    constructor Create(AOwner: TComponent); override;
    procedure   AddWidget(AWidget: TWidget; ALeft, ATop: Integer);
    procedure   MoveWidget(AWidget: TWidget; ALeft, ATop: Integer);
  published
    property    Enabled;
  end;


// -------------------------------------------------------------------
//   DockingLayout
// -------------------------------------------------------------------

  TDockingMode = (dmTop, dmBottom, dmLeft, dmRight, dmClient, dmUndocked);

  TDockingItem = class(TLayoutItem)
  public
    Left, Top: Integer;
    DockingMode: TDockingMode;
  end;
  

  TDockingLayout = class(TLayout)
  protected
    procedure   CalcSizes; override;
  public
    constructor Create(AOwner: TComponent); override;
    procedure   AddWidget(AWidget: TWidget; ADockingMode: TDockingMode);
    procedure   AddWidget(AWidget: TWidget; ALeft, ATop: Integer);
  published
    property    Enabled;
  end;


// -------------------------------------------------------------------
//   BoxLayout
// -------------------------------------------------------------------

  TCustomBoxLayout = class(TLayout)
  private
    FHorzAlign: THorzAlign;
    FVertAlign: TVertAlign;
    FOrientation: TOrientation;
    FSpacing: Integer;
    procedure   SetOrientation(AOrientation: TOrientation);
  protected
    procedure   CalcSizes; override;
    procedure   Resized; override;
    property    HorzAlign: THorzAlign read FHorzAlign write FHorzAlign default horzFill;
    property    VertAlign: TVertAlign read FVertAlign write FVertAlign default vertFill;
    property    Orientation: TOrientation read FOrientation write SetOrientation default Horizontal;
    property    Spacing: Integer read FSpacing write FSpacing default 4;
  public
    constructor Create(AOwner: TComponent); override;
    procedure   InsertChild(AChild: TWidget); override;
    procedure   RemoveChild(AChild: TWidget); override;
  end;


  TBoxLayout = class(TCustomBoxLayout)
  published
    property CanExpandWidth;
    property CanExpandHeight;
    property Enabled;
    property BorderSpacing;
    property HorzAlign;
    property Orientation;
    property Spacing;
    property VertAlign;
  end;


// -------------------------------------------------------------------
//   GridLayout
// -------------------------------------------------------------------

  TGridItem = class(TLayoutItem)
  private
    FX, FY, FWidth, FHeight: Integer;
  public
    constructor Create(ACollection: TCollection); override;
  published
    property x: Integer read FX write FX default 1;
    property y: Integer read FY write FY default 1;
    property Width: Integer read FWidth write FWidth;
    property Height: Integer read FHeight write FHeight;
  end;


  { TCustomGridLayout }

  TCustomGridLayout = class(TLayout)
  private
    FColCount: Integer;
    FRowCount: Integer;
    FColSpacing: Integer;
    FRowSpacing: Integer;
//    FWidgets: TCollection;
    procedure   SetColCount(AColCount: Integer);
    procedure   SetRowCount(ARowCount: Integer);
    procedure   SetColSpacing(AColSpacing: Integer);
    procedure   SetRowSpacing(ARowSpacing: Integer);
  protected
    procedure   InitSizeInfos(var ColInfos, RowInfos: PWidgetArrayInfoArray);
    procedure   CalcSizes; override;
    procedure   Resized; override;
    property    GridPositions: TCollection read FWidgets write FWidgets;
    property    ColCount: Integer read FColCount write SetColCount default 2;
    property    RowCount: Integer read FRowCount write SetRowCount default 2;
    property    ColSpacing: Integer read FColSpacing write SetColSpacing default 4;
    property    RowSpacing: Integer read FRowSpacing write SetRowSpacing default 4;
  public
    constructor Create(AOwner: TComponent); override;
    procedure   AddWidget(AWidget: TWidget; x, y, w, h: Integer);
    procedure   MoveWidget(AWidget: TWidget; x, y, w, h: Integer);
  end;


  TGridLayout = class(TCustomGridLayout)
  published
    property Enabled;
    property ColCount;
    property RowCount;
    property ColSpacing;
    property RowSpacing;
    property GridPositions;
  end;

{$ENDIF read_interface}



{$IFDEF read_implementation}

// ===================================================================
//   Common layout widgets implementation
// ===================================================================

resourcestring
  SLayoutWidgetNotFound = 'Layout child widget not found';


procedure AddToSizes(infos: PWidgetArrayInfoArray; count: Integer; TooMuch: Integer);
var
  i, add, FoundElements: Integer;
begin
  while TooMuch > 0 do
  begin
    add := TooMuch;
    FoundElements := 0;
    for i := 0 to count - 1 do
    begin
      if not infos^[i].MaxFlag then
        continue;
      Inc(FoundElements);
      if infos^[i].def + add > infos^[i].max then
        add := infos^[i].max - infos^[i].def;
    end;
    if FoundElements > 0 then
    begin
      add := add div FoundElements;
      if add <= 0 then
        add := 1;
    end else
      break;

    for i := 0 to count - 1 do
    begin
      if not infos^[i].MaxFlag then
        continue;
      Inc(infos^[i].def, add);
      Dec(TooMuch, add);
      if TooMuch = 0 then
        exit;
      if infos^[i].def = infos^[i].max then
        infos^[i].MaxFlag := False;
    end;
  end;
end;

procedure SubFromSizes(infos: PWidgetArrayInfoArray; count: Integer; TooMuch: Integer);
var
  i, sub, FoundElements: Integer;
begin
  while TooMuch > 0 do
  begin
    sub := TooMuch;
    FoundElements := 0;
    for i := 0 to count - 1 do
    begin
      if not infos^[i].MinFlag then
        continue;
      Inc(FoundElements);
      if infos^[i].def - sub < infos^[i].min then
        sub := infos^[i].def - infos^[i].min;
    end;
    if FoundElements > 0 then
    begin
      sub := sub div FoundElements;
      if sub <= 0 then
        sub := 1;
    end else
      break;

    for i := 0 to count - 1 do
    begin
      if not infos^[i].MinFlag then
        continue;
      Dec(infos^[i].def, sub);
      Dec(TooMuch, sub);
      if TooMuch = 0 then
        exit;
      if infos^[i].def = infos^[i].min then
        infos^[i].MinFlag := False;
    end;
  end;
end;

procedure CorrectSizes(infos: PWidgetArrayInfoArray; count: Integer; SizeDiff: Integer);
var
  TooMuch: Integer;
begin
  TooMuch := SizeDiff;
  if TooMuch > 0 then
    AddToSizes(infos, count, TooMuch)
  else if TooMuch < 0 then
    SubFromSizes(infos, count, -TooMuch);
end;


// -------------------------------------------------------------------
//   TLayout
// -------------------------------------------------------------------

// public methods

constructor TLayout.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FCanExpandWidth := True;
  FCanExpandHeight := True;
end;


destructor TLayout.Destroy;
begin
  FWidgets.Free;
  inherited Destroy;
end;


function TLayout.ContainsChild(AChild: TWidget): Boolean;
var
  i: Integer;
begin
  for i := 0 to FWidgets.Count - 1 do
    if TLayoutItem(FWidgets.Items[i]).Widget = AChild then
    begin
      Result := True;
      exit;
    end;
  Result := False;
end;


// protected methods

procedure TLayout.AddFixedChild(AChild: TWidget);
begin
end;

function TLayout.GetChildCount: Integer;
begin
  Result := FWidgets.Count;
end;

function TLayout.GetChild(Index: Integer): TWidget;
begin
  Result := TLayoutItem(FWidgets.Items[Index]).Widget;
end;


// -------------------------------------------------------------------
//   TFixedLayout
// -------------------------------------------------------------------

constructor TFixedLayout.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FWidgets := TCollection.Create(TFixedItem);
end;

procedure TFixedLayout.AddWidget(AWidget: TWidget; ALeft, ATop: Integer);
var
  item: TFixedItem;
begin
  AWidget.Parent := Self;
  item := TFixedItem(FWidgets.Add);
  item.Left := ALeft;
  item.Top := ATop;
  item.Widget := AWidget;
  AWidget.SetBounds(Point(item.Left, item.Top), item.Widget.DefSize);
  AddFixedChild(AWidget);
end;

procedure TFixedLayout.MoveWidget(AWidget: TWidget; ALeft, ATop: Integer);
var
  i: integer;
  item: TFixedItem;
begin
  for i := 0 to FWidgets.Count - 1 do
  begin
    item := TFixedItem(FWidgets.Items[i]);
    if item.Widget = AWidget then
    begin
      item.Left := ALeft;
      item.Top := ATop;
      AWidget.SetBounds(Point(item.Left, item.Top), item.Widget.DefSize);
      exit;
    end;
  end;
  raise ELayoutError.Create(SLayoutWidgetNotFound);
end;

procedure TFixedLayout.CalcSizes;
var
  i: Integer;
  item: TFixedItem;
begin
  if FWidgets.Count = 0 then
    FDefSize := gfxbase.Size(50, 50)
  else
    for i := 0 to FWidgets.Count - 1 do
    begin
      item := TFixedItem(FWidgets.Items[i]);
      FDefSize.cx := Max(DefSize.cx, item.Left + item.Widget.DefSize.cx);
      FDefSize.cy := Max(DefSize.cx, item.Top + item.Widget.DefSize.cy);
    end;
end;

{procedure TFixedLayout.EvLayoutChildren(Canvas: TGfxCanvas);
var
  i: Integer;
  item: TFixedItem;
begin
  for i := 0 to FWidgets.Count - 1 do
  begin
    item := TFixedItem(FWidgets.Items[i]);
    item.Widget.SetBounds(item.Left, item.Top, item.Widget.DefSize.cx, item.Widget.DefSize.cy);
  end;
end;}


// -------------------------------------------------------------------
//   TDockingLayout
// -------------------------------------------------------------------

constructor TDockingLayout.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FWidgets := TCollection.Create(TDockingItem);
end;

procedure TDockingLayout.CalcSizes;
var
  i: Integer;
  item: TDockingItem;
  w, cw: TWidget;
begin
  if FWidgets.Count = 0 then
  begin
    FDefSize := gfxbase.Size(200, 200);
    exit;
  end;

  // Find the client widget (widget with DockingMode "dmClient")
  cw := nil;
  for i := 0 to FWidgets.Count - 1 do
  begin
    item := TDockingItem(FWidgets.Items[i]);
    if item.DockingMode = dmClient then
    begin
      cw := item.Widget;
      break;
    end;
  end;

  if Assigned(cw) then
  begin
    FMinSize := cw.MinSize;
    FMaxSize := cw.MaxSize;
    FDefSize := cw.DefSize;
  end else
    FDefSize := gfxbase.Size(200, 200);

  for i := 0 to FWidgets.Count - 1 do
  begin
    item := TDockingItem(FWidgets.Items[i]);
    w := item.Widget;
    case item.DockingMode of
      dmTop, dmBottom:
        begin
          if MinSize.cx < w.MinSize.cx then
	    FMinSize.cx := w.MinSize.cx;
	  Inc(FMinSize.cy, w.MinSize.cy);
	  if MaxSize.cx < w.MaxSize.cx then
	    FMaxSize.cx := w.MaxSize.cx;
	  if MaxSize.cy < InfiniteSize then
	    Inc(FMaxSize.cy, w.MaxSize.cy);
	  if DefSize.cx < w.DefSize.cx then
	    FDefSize.cx := w.DefSize.cx;
	  Inc(FDefSize.cy, w.DefSize.cy);
        end;
      dmLeft, dmRight:
        begin
	  Inc(FMinSize.cx, w.MinSize.cx);
          if MinSize.cy < w.MinSize.cy then
	    FMinSize.cy := w.MinSize.cy;
	  if MaxSize.cx < InfiniteSize then
	    Inc(FMaxSize.cx, w.MaxSize.cx);
	  if MaxSize.cy < w.MaxSize.cy then
	    FMaxSize.cy := w.MaxSize.cy;
	  Inc(FDefSize.cx, w.DefSize.cx);
	  if DefSize.cy < w.DefSize.cy then
	    FDefSize.cy := w.DefSize.cy;
        end;
    end;
  end;
end;


{procedure TDockingLayout.EvLayoutChildren(Canvas: TGfxCanvas);
var
  clx, cly, clw, clh: Integer;   // Client rectangle
  ClientWidget: TWidget;
  i, WidgetW, WidgetH: Integer;
  item: TDockingItem;
begin
  clx := 0;
  cly := 0;
  clw := BoundsSize.cx;
  clh := BoundsSize.cy;
  // WriteLn('=> DockingLayout.EvLayoutChildren ', BoundsSize.cx, ' x ', BoundsSize.cy);
  if (clw = 0) or (clh = 0) then
    exit;

  // Process all attached widgets
  ClientWidget := nil;
  for i := 0 to FWidgets.Count - 1 do
  begin
    item := TDockingItem(FWidgets.Items[i]);
    case item.DockingMode of
      dmLeft:
        begin
	  WidgetW := item.Widget.DefSize.cx;
	  WidgetH := clh;
	  item.Left := clx;
	  item.Top := cly;
	  Inc(clx, WidgetW);
	  Dec(clw, WidgetW);
        end;
      dmTop:
        begin
	  WidgetW := clw;
	  WidgetH := item.Widget.DefSize.cy;
	  item.Left := clx;
	  item.Top := cly;
	  Inc(cly, WidgetH);
	  Dec(clh, WidgetH);
        end;
      dmRight:
        begin
	  WidgetW := item.Widget.DefSize.cx;
	  WidgetH := clh;
	  item.Left := clx + clw - WidgetW;
          item.Top := cly;
	  Dec(clw, WidgetW);
        end;
      dmBottom:
        begin
	  WidgetH := item.Widget.DefSize.cy;
	  WidgetW := clw;
	  item.Left := clx;
	  item.Top := cly + clh - WidgetH;
	  Dec(clh, WidgetH);
        end;
      dmClient:
	ClientWidget := item.Widget;
    end;
    if item.DockingMode <> dmClient then
      item.Widget.SetBounds(item.Left, item.Top, WidgetW, WidgetH);
  end;
  if Assigned(ClientWidget) then
    ClientWidget.SetBounds(clx, cly, clw, clh);
end;}

procedure TDockingLayout.AddWidget(AWidget: TWidget; ADockingMode: TDockingMode);
var
  item: TDockingItem;
begin
  AWidget.Parent := Self;
  item := TDockingItem(FWidgets.Add);
  item.Widget := AWidget;
  item.DockingMode := ADockingMode;
  AddFixedChild(AWidget);
end;

procedure TDockingLayout.AddWidget(AWidget: TWidget; ALeft, ATop: Integer);
var
  item: TDockingItem;
begin
  AWidget.Parent := Self;
  item := TDockingItem(FWidgets.Add);
  item.Widget := AWidget;
  item.DockingMode := dmUndocked;
  item.Left := ALeft;
  item.Top := ATop;
  AddFixedChild(AWidget);
end;


// -------------------------------------------------------------------
//   TCustomBoxLayout
// -------------------------------------------------------------------

procedure TCustomBoxLayout.SetOrientation(AOrientation: TOrientation);
begin
  if AOrientation <> FOrientation then
  begin
    FOrientation := AOrientation;
    Update;
  end;
end;


constructor TCustomBoxLayout.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FWidgets := TCollection.Create(TLayoutItem);
  FOrientation := Horizontal;
  FHorzAlign := horzFill;
  FVertAlign := vertFill;
  FSpacing := 4;
end;


procedure TCustomBoxLayout.InsertChild(AChild: TWidget);
var
  item: TLayoutItem;
begin
  if not ContainsChild(AChild) then
  begin
    item := TLayoutItem(FWidgets.Add);
    item.Widget := AChild;
    AChild.Parent := Self;
  end;
end;


procedure TCustomBoxLayout.RemoveChild(AChild: TWidget);
begin
  {$Warning Not implemented yet.}
  raise Exception.Create('TCustomBoxLayout.RemoveChild - Not implemented yet');
end;


procedure TCustomBoxLayout.CalcSizes;
var
  i: Integer;
  item: TLayoutItem;
begin
  i := (FWidgets.Count - 1) * FSpacing;
  if Orientation = Horizontal then
  begin
    FMinSize := Size(i, 0);
    FDefSize := MinSize;
    if HorzAlign = horzFill then
      FMaxSize.cx := i
    else
      FMaxSize.cx := InfiniteSize;
    FMaxSize.cy := InfiniteSize;
  end else
  begin
    FMinSize := Size(0, i);
    FDefSize := MinSize;
    FMaxSize.cx := InfiniteSize;
    if VertAlign = vertFill then
      FMaxSize.cy := i
    else
      FMaxSize.cy := InfiniteSize;
  end;

  for i := 0 to FWidgets.Count - 1 do
  begin
    item := TLayoutItem(FWidgets.Items[i]);
    if Orientation = Horizontal then
    begin
      Inc(FMinSize.cx, item.Widget.MinSize.cx);
      Inc(FDefSize.cx, item.Widget.DefSize.cx);
      FMaxSize.cx := Min(InfiniteSize, MaxSize.cx + item.Widget.MaxSize.cx);
      FMaxSize.cy := Min(MaxSize.cy, item.Widget.MaxSize.cy);
      if MinSize.cy < item.Widget.MinSize.cy then
        FMinSize.cy := item.Widget.MinSize.cy;
      if DefSize.cy < item.Widget.DefSize.cy then
        FDefSize.cy := item.Widget.DefSize.cy;
      if MaxSize.cy > item.Widget.MaxSize.cy then
        FMaxSize.cy := item.Widget.MaxSize.cy;
    end else
    begin
      Inc(FMinSize.cy, item.Widget.MinSize.cy);
      Inc(FDefSize.cy, item.Widget.DefSize.cy);
      FMaxSize.cx := Min(MaxSize.cx, item.Widget.MaxSize.cx);
      FMaxSize.cy := Min(InfiniteSize, MaxSize.cy + item.Widget.MaxSize.cy);
      if MinSize.cx < item.Widget.MinSize.cx then
        FMinSize.cx := item.Widget.MinSize.cx;
      if DefSize.cx < item.Widget.DefSize.cx then
        FDefSize.cx := item.Widget.DefSize.cx;
      if MaxSize.cx > item.Widget.MaxSize.cx then
        FMaxSize.cx := item.Widget.MaxSize.cx;
    end;
  end;

  Inc(FMinSize.cx, 2 * FBorderSpacing);
  Inc(FMinSize.cy, 2 * FBorderSpacing);
  Inc(FDefSize.cx, 2 * FBorderSpacing);
  Inc(FDefSize.cy, 2 * FBorderSpacing);
  FMaxSize.cx := Min(InfiniteSize, MaxSize.cx + 2 * FBorderSpacing);
  FMaxSize.cy := Min(InfiniteSize, MaxSize.cy + 2 * FBorderSpacing);
end;

procedure TCustomBoxLayout.Resized;
var
  sizes: PWidgetArrayInfoArray;
  i, x, y, xpos, ypos, w, h, sum: Integer;
  item: TLayoutItem;
begin
  GetMem(sizes, FWidgets.Count * SizeOf(TWidgetArrayInfo));

  for i := 0 to FWidgets.Count - 1 do
  begin
    sizes^[i].min := 0;
    sizes^[i].def := 0;
    sizes^[i].max := InfiniteSize;
    sizes^[i].MinFlag := True;
    sizes^[i].MaxFlag := True;
  end;

  if Orientation = Horizontal then
    for i := 0 to FWidgets.Count - 1 do
    begin
      item := TLayoutItem(FWidgets.Items[i]);
      sizes^[i].min := Max(sizes^[i].min, item.Widget.MinSize.cx);
      sizes^[i].def := Max(sizes^[i].def, item.Widget.DefSize.cx);
      sizes^[i].max := Min(sizes^[i].max, item.Widget.MaxSize.cx);
    end
  else
    for i := 0 to FWidgets.Count - 1 do
    begin
      item := TLayoutItem(FWidgets.Items[i]);
      sizes^[i].min := Max(sizes^[i].min, item.Widget.MinSize.cy);
      sizes^[i].def := Max(sizes^[i].def, item.Widget.DefSize.cy);
      sizes^[i].max := Min(sizes^[i].max, item.Widget.MaxSize.cy);
    end;

  for i := 0 to FWidgets.Count - 1 do
  begin
    if sizes^[i].def = 0 then sizes^[i].def := 20;
    if sizes^[i].min >= sizes^[i].def then
      sizes^[i].MinFlag := False;
    if sizes^[i].max <= sizes^[i].def then
      sizes^[i].MaxFlag := False;
  end;

  if Orientation = Horizontal then
  begin
    if FHorzAlign = horzFill then
      CorrectSizes(sizes, FWidgets.Count, BoundsSize.cx - DefSize.cx)
  end else
    if FVertAlign = vertFill then
      CorrectSizes(sizes, FWidgets.Count, BoundsSize.cy - DefSize.cy);

  sum := (FWidgets.Count - 1) * FSpacing;
  for i := 0 to FWidgets.Count - 1 do
    Inc(sum, sizes^[i].def);

  if Orientation = Horizontal then
    case FHorzAlign of
      horzCenter: x := (BoundsSize.cx - sum) div 2;
      horzRight:  x := BoundsSize.cx - FBorderSpacing - sum;
      else	  x := FBorderSpacing;
    end
  else
    case FVertAlign of
      vertCenter: y := (BoundsSize.cy - sum) div 2;
      vertBottom: y := BoundsSize.cy - FBorderSpacing - sum;
      else        y := FBorderSpacing;
    end;

  for i := 0 to FWidgets.Count - 1 do
  begin
    item := TLayoutItem(FWidgets.Items[i]);
    if Orientation = Horizontal then
    begin
      xpos := x;
      w := sizes^[i].def;
      h := Min(BoundsSize.cy, item.Widget.DefSize.cy);
      case FVertAlign of
        vertCenter: ypos := (BoundsSize.cy - h) div 2;
	      vertBottom: ypos := BoundsSize.cy - FBorderSpacing - h;
	      else        ypos := FBorderSpacing;
      end;
      Inc(x, sizes^[i].def + FSpacing);
      if FVertAlign = vertFill then
        h := Min(BoundsSize.cy, item.Widget.MaxSize.cy);
    end
    else
    begin
      ypos := y;
      w := Min(BoundsSize.cx, item.Widget.DefSize.cx);
      h := sizes^[i].def;
      case FHorzAlign of
        horzCenter: xpos := (BoundsSize.cx - w) div 2;
	      horzRight : xpos := BoundsSize.cx - FBorderSpacing - w;
	      else        xpos := FBorderSpacing;
      end;
      Inc(y, sizes^[i].def + FSpacing);
      if FHorzAlign = horzFill then
        w := Min(BoundsSize.cx, item.Widget.MaxSize.cx);
    end;
    item.Widget.SetBounds(Point(xpos, ypos), gfxbase.Size(w, h));
  end;

  FreeMem(sizes);
end;


// -------------------------------------------------------------------
//   TCustomGridLayout
// -------------------------------------------------------------------

constructor TGridItem.Create(ACollection: TCollection);
begin
  inherited Create(ACollection);
  Width := 1;
  Height := 1;
end;


procedure TCustomGridLayout.SetColCount(AColCount: Integer);
begin
  if AColCount <> FColCount then
  begin
    FColCount := AColCount;
    Update;
  end;
end;


procedure TCustomGridLayout.SetRowCount(ARowCount: Integer);
begin
  if ARowCount <> FRowCount then
  begin
    FRowCount := ARowCount;
    Update;
  end;
end;


procedure TCustomGridLayout.SetColSpacing(AColSpacing: Integer);
begin
  if AColSpacing <> FColSpacing then
  begin
    FColSpacing := AColSpacing;
    Update;
  end;
end;


procedure TCustomGridLayout.SetRowSpacing(ARowSpacing: Integer);
begin
  if ARowSpacing <> FRowSpacing then
  begin
    FRowSpacing := ARowSpacing;
    Update;
  end;
end;


procedure TCustomGridLayout.InitSizeInfos(var ColInfos, RowInfos: PWidgetArrayInfoArray);
var
  i: Integer;
  item: TGridItem;
begin
  GetMem(ColInfos, FColCount * SizeOf(TWidgetArrayInfo));
  GetMem(RowInfos, FRowCount * SizeOf(TWidgetArrayInfo));

  for i := 0 to FColCount - 1 do
  begin
    ColInfos^[i].min := 0;
    ColInfos^[i].def := 0;
    ColInfos^[i].max := InfiniteSize;
    ColInfos^[i].MinFlag := True;
    ColInfos^[i].MaxFlag := True;
  end;

  for i := 0 to FRowCount - 1 do
  begin
    RowInfos^[i].min := 0;
    RowInfos^[i].def := 0;
    RowInfos^[i].max := InfiniteSize;
    RowInfos^[i].MinFlag := True;
    RowInfos^[i].MaxFlag := True;
  end;

  for i := 0 to FWidgets.Count - 1 do
  begin
    item := TGridItem(FWidgets.Items[i]);
    ColInfos^[item.x].min := Max(ColInfos^[item.x].min, item.Widget.MinSize.cx);
    ColInfos^[item.x].def := Max(ColInfos^[item.x].def, item.Widget.DefSize.cx);
    ColInfos^[item.x].max := Min(ColInfos^[item.x].max, item.Widget.MaxSize.cx);
    RowInfos^[item.y].min := Max(RowInfos^[item.y].min, item.Widget.MinSize.cy);
    RowInfos^[item.y].def := Max(RowInfos^[item.y].def, item.Widget.DefSize.cy);
    RowInfos^[item.y].max := Min(RowInfos^[item.y].max, item.Widget.MaxSize.cy);
  end;

  for i := 0 to FColCount - 1 do
  begin
    if ColInfos^[i].def = 0 then
      ColInfos^[i].def := 20;
    if ColInfos^[i].min >= ColInfos^[i].def then
      ColInfos^[i].MinFlag := False;
    if ColInfos^[i].max <= ColInfos^[i].def then
      ColInfos^[i].MaxFlag := False;
  end;

  for i := 0 to FRowCount - 1 do
  begin
    if RowInfos^[i].def = 0 then
      RowInfos^[i].def := 20;
    if RowInfos^[i].min >= RowInfos^[i].def then
      RowInfos^[i].MinFlag := False;
    if RowInfos^[i].max <= RowInfos^[i].def then
      RowInfos^[i].MaxFlag := False;
  end;
end;


procedure TCustomGridLayout.CalcSizes;
var
  ColInfos, RowInfos: PWidgetArrayInfoArray;
  i: Integer;
begin
  MinSize.cx := (FColCount - 1) * FColSpacing;
  MinSize.cy := (FRowCount - 1) * FRowSpacing;
  DefSize.cx := (FColCount - 1) * FColSpacing;
  DefSize.cy := (FRowCount - 1) * FRowSpacing;
  MaxSize.cx := (FColCount - 1) * FColSpacing;
  MaxSize.cy := (FRowCount - 1) * FRowSpacing;

  InitSizeInfos(ColInfos, RowInfos);

  for i := 0 to FColCount - 1 do
  begin
    Inc(FMinSize.cx, ColInfos^[i].min);
    Inc(FDefSize.cx, ColInfos^[i].def);
    FMaxSize.cx := Min(InfiniteSize, MaxSize.cx + ColInfos^[i].max);
  end;

  for i := 0 to FRowCount - 1 do
  begin
    Inc(FMinSize.cy, RowInfos^[i].min);
    Inc(FDefSize.cy, RowInfos^[i].def);
    FMaxSize.cy := Min(InfiniteSize, MaxSize.cy + RowInfos^[i].max);
  end;

  FreeMem(RowInfos);
  FreeMem(ColInfos);
end;


procedure TCustomGridLayout.Resized;
var
  ColInfos, RowInfos: PWidgetArrayInfoArray;
  i, j, x, y, w, h: Integer;
  item: TGridItem;
begin
  InitSizeInfos(ColInfos, RowInfos);

  CorrectSizes(ColInfos, FColCount, BoundsSize.cx - DefSize.cx);
  CorrectSizes(RowInfos, FRowCount, BoundsSize.cy - DefSize.cy);

  for i := 0 to FWidgets.Count - 1 do
  begin
    item := TGridItem(FWidgets.Items[i]);
    x := 0;
    for j := 0 to item.x - 1 do
      Inc(x, ColInfos^[j].def);
    y := 0;
    for j := 0 to item.y - 1 do
      Inc(y, RowInfos^[j].def);
    w := 0;
    for j := 0 to item.Width - 1 do
      Inc(w, ColInfos^[item.x + j].def);
    h := 0;
    for j := 0 to item.Height - 1 do
      Inc(h, RowInfos^[item.y + j].def);
    Inc(w, (item.Width - 1) * FColSpacing);
    Inc(h, (item.Height - 1) * FRowSpacing);
    item.Widget.SetBounds(Point(x + item.x * FColSpacing,
      y + item.y * FRowSpacing), gfxbase.Size(w, h));
  end;

  FreeMem(ColInfos);
  FreeMem(RowInfos);
end;

constructor TCustomGridLayout.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FWidgets := TCollection.Create(TGridItem);
  FColCount := 2;
  FRowCount := 2;
  FColSpacing := 4;
  FRowSpacing := 4;
end;

procedure TCustomGridLayout.AddWidget(AWidget: TWidget; x, y, w, h: Integer);
var
  item: TGridItem;
begin
  AWidget.Parent := Self;
  item := TGridItem(FWidgets.Add);
  item.Widget := AWidget;
  item.x := x;
  item.y := y;
  item.Width := w;
  item.Height := h;
end;

procedure TCustomGridLayout.MoveWidget(AWidget: TWidget; x, y, w, h: Integer);
var
  i: integer;
  item: TGridItem;
begin
  for i := 0 to FWidgets.Count - 1 do
  begin
    item := TGridItem(FWidgets.Items[i]);
    if item.Widget = AWidget then
    begin
      item.x := x;
      item.y := y;
      item.Width := w;
      item.Height := h;
      Update;
      exit;
    end;
  end;
  raise ELayoutError.Create(SLayoutWidgetNotFound);
end;


{$ENDIF read_implementation}