File: toolbar.inc

package info (click to toggle)
lazarus 4.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 275,760 kB
  • sloc: pascal: 2,341,904; xml: 509,420; makefile: 348,726; cpp: 93,608; sh: 3,387; java: 609; perl: 297; sql: 222; ansic: 137
file content (1125 lines) | stat: -rw-r--r-- 33,251 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
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
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
{%MainUnit ../comctrls.pp}

{******************************************************************************
                                  TToolbar
 ******************************************************************************

 *****************************************************************************
  This file is part of the Lazarus Component Library (LCL)

  See the file COPYING.modifiedLGPL.txt, included in this distribution,
  for details about the license.
 *****************************************************************************

}

function CompareToolBarControlHorz(Control1, Control2: TControl): integer;
var
  ToolBar: TToolBar;
  Row1: Integer;
  Row2: Integer;
  HalfBtnHeight, BtnHeight: Integer;
begin
  Result := 0;
  if not (Control1.Parent is TToolBar) then Exit;

  ToolBar := TToolBar(Control1.Parent);
  BtnHeight := ToolBar.ButtonHeight;
  if BtnHeight <= 0 then BtnHeight := 1;
  HalfBtnHeight := BtnHeight div 2;

  Row1 := (Control1.Top + HalfBtnHeight) div BtnHeight;
  Row2 := (Control2.Top + HalfBtnHeight) div BtnHeight;
  Result := CompareValue(Row1, Row2);
  if Result = 0 then
  begin
    Result := CompareValue(Control1.Left, Control2.Left);
    if ToolBar.UseRightToLeftAlignment then
      Result:=-Result;
  end;
  if Result = 0 then
  begin
    Row1 := ToolBar.GetControlIndex(Control1);
    Row2 := ToolBar.GetControlIndex(Control2);
    Result := CompareValue(Row1, Row2);
  end;
end;

function CompareToolBarControlVert(Control1, Control2: TControl): integer;
var
  ToolBar: TToolBar;
  Col1: Integer;
  Col2: Integer;
  HalfBtnWidth, BtnWidth: Integer;
begin
  Result := 0;
  if not (Control1.Parent is TToolBar) then Exit;

  ToolBar := TToolBar(Control1.Parent);
  BtnWidth := ToolBar.ButtonWidth;
  if BtnWidth <= 0 then BtnWidth := 1;
  HalfBtnWidth := BtnWidth div 2;

  Col1 := (Control1.Left + HalfBtnWidth) div BtnWidth;
  Col2 := (Control2.Left + HalfBtnWidth) div BtnWidth;
  Result := CompareValue(Col1, Col2);
  if Result = 0 then
    Result := CompareValue(Control1.Top, Control2.Top);
  if Result = 0 then
  begin
    Col1 := ToolBar.GetControlIndex(Control1);
    Col2 := ToolBar.GetControlIndex(Control2);
    Result := CompareValue(Col1, Col2);
  end;
end;

{------------------------------------------------------------------------------
  Method: TToolbar.Create
  Params:  AOwner: the owner of the class
  Returns: Nothing

  Constructor for the class.
 ------------------------------------------------------------------------------}
constructor TToolBar.Create(TheOwner: TComponent);
var
  Details: TThemedElementDetails;
begin
  inherited Create(TheOwner);
  ControlStyle := [csAcceptsControls, csCaptureMouse, csClickEvents,
    csDoubleClicks, csMenuEvents, csSetCaption, csParentBackground, csOpaque];
  FFlat := True;
  Height := 32;
  // ToDo: Test the scaling code. Widths are scaled in many places.
  Details := ThemeServices.GetElementDetails(ttbSplitButtonDropDownNormal);
  FThemeDropDownWidth := ThemeServices.GetDetailSize(Details).cx;
  Details := ThemeServices.GetElementDetails(ttbDropDownButtonNormal);
  FThemeButtonDropWidth := ThemeServices.GetDetailSize(Details).cx;
  FButtonHeight := -1;
  FButtonWidth := -1;
  FDropDownWidth := -1;
  FNewStyle := True;
  FWrapable := True;
  FButtons := TList.Create;
  FIndent := 1;
  FList := False;
  FImageChangeLink := TChangeLink.Create;
  FImageChangeLink.OnChange := @ImageListChange;
  FDisabledImageChangeLink := TChangeLink.Create;
  FDisabledImageChangeLink.OnChange := @DisabledImageListChange;
  FHotImageChangeLink := TChangeLink.Create;
  FHotImageChangeLink.OnChange := @HotImageListChange;
  EdgeBorders := [ebTop];
  with GetControlClassDefaultSize do
    SetInitialBounds(0, 0, CX, CY);
  Align := alTop;
end;

destructor TToolBar.Destroy;
var
  I: Integer;
begin
  for I := 0 to FButtons.Count - 1 do
    if TControl(FButtons[I]) is TToolButton then
      TToolButton(FButtons[I]).FToolBar := nil;

  FreeThenNil(FButtons);
  FreeThenNil(FHotImageChangeLink);
  FreeThenNil(FImageChangeLink);
  FreeThenNil(FDisabledImageChangeLink);
  inherited Destroy;
end;

procedure TToolBar.FlipChildren(AllLevels: Boolean);
begin
  if AllLevels then ;
  // no flipping
end;

procedure TToolBar.CreateWnd;
begin
  BeginUpdate;
  try
    DisableAutoSizing{$IFDEF DebugDisableAutoSizing}('TToolBar.CreateWnd'){$ENDIF};
    try
      inherited CreateWnd;
      UpdateVisibleBar;
    finally
      EnableAutoSizing{$IFDEF DebugDisableAutoSizing}('TToolBar.CreateWnd'){$ENDIF};
    end;
  finally
    EndUpdate;
  end;
end;

procedure TToolBar.AlignControls(AControl: TControl;
  var RemainingClientRect: TRect);
var
  NewWidth, NewHeight: integer;
begin
  if tbfPlacingControls in FToolBarFlags then exit;
  Include(FToolBarFlags, tbfPlacingControls);
  DisableAlign;
  try
    AdjustClientRect(RemainingClientRect);
    if IsVertical then
      WrapButtons(Height, NewWidth, NewHeight, False)
    else
      WrapButtons(Width, NewWidth, NewHeight, False);
  finally
    Exclude(FToolBarFlags, tbfPlacingControls);
    EnableAlign;
  end;
end;

procedure TToolBar.RepositionButton(Index: Integer);
begin
  if ([csLoading,csDestroying]*ComponentState<>[]) then exit;
  UpdateVisibleBar;
end;

procedure TToolBar.RepositionButtons(Index: Integer);
begin
  UpdateVisibleBar;
end;

procedure TToolBar.SetButtonHeight(const AValue: Integer);
begin
  SetButtonSize(ButtonWidth,AValue);
end;

procedure TToolBar.SetButtonWidth(const AValue: Integer);
begin
  SetButtonSize(AValue,ButtonHeight);
end;

procedure TToolBar.ImageListChange(Sender: TObject);
begin
  if (Sender = Images) then UpdateVisibleBar;
end;

procedure TToolBar.SetShowCaptions(const AValue: Boolean);
begin
  if FShowCaptions = AValue then exit;
  FShowCaptions := AValue;
  UpdateVisibleBar;
end;

procedure TToolBar.CloseCurrentMenu;
begin
  FCurrentMenu.Close;
  // move menu items back
  if Assigned(FSrcMenuItem) then
  begin
    MoveSubMenuItems(FCurrentMenu.Items, FSrcMenuItem);
    if Assigned(FDropDownButton) then
      FDropDownButton.Down := False;
  end;
end;

procedure TToolBar.MoveSubMenuItems(SrcMenuItem, DestMenuItem: TMenuItem);
var
  i: Integer;
  MovingMenuItem: TMenuItem;
begin
  if (SrcMenuItem = nil) or (DestMenuItem = nil) or (SrcMenuItem = DestMenuItem) then
    Exit;
  for i := SrcMenuItem.Count - 1 downto 0 do
  begin
    MovingMenuItem := SrcMenuItem.Items[i];
    SrcMenuItem.Delete(i);
    DestMenuItem.Insert(0, MovingMenuItem);
  end;
end;

procedure TToolBar.AddButton(Button: TToolButton);
begin
  FButtons.Add(Button);
end;

procedure TToolBar.RemoveButton(Button: TToolButton);
begin
  if FDropDownButton=Button then FDropDownButton:=nil;
  FButtons.Remove(Button);
end;

function TToolBar.IsVertical: Boolean;
begin
  if (Parent is TCoolBar) then
    Exit(TCoolBar(Parent).Vertical);

  if Align in [alNone, alClient, alCustom] then
    Result := Height > Width
  else
    Result := Align in [alLeft, alRight];
end;

class procedure TToolBar.WSRegisterClass;
begin
  inherited WSRegisterClass;
  RegisterToolBar;
end;

procedure TToolBar.ApplyFontForButtons;
var
  i: integer;
begin
  for i := 0 to ButtonCount - 1 do
    Buttons[i].Font := Font;
end;

function TToolBar.ButtonHeightIsStored: Boolean;
begin
  Result := FButtonHeight >= 0;
end;

function TToolBar.ButtonWidthIsStored: Boolean;
begin
  Result := FButtonWidth >= 0;
end;

function TToolBar.GetButton(Index: Integer): TToolButton;
begin
  Result := TToolButton(FButtons[Index]);
end;

function TToolBar.GetButtonCount: Integer;
begin
  Result := FButtons.Count;
end;

function TToolBar.GetTransparent: Boolean;
begin
  Result := not (csOpaque in ControlStyle);
end;

procedure TToolBar.SetList(const AValue: Boolean);
begin
  if FList = AValue then exit;
  FList := AValue;
  UpdateVisibleBar;
end;

procedure TToolBar.SetFlat(const AValue: Boolean);
begin
  if FFlat = AValue then exit;
  FFlat := AValue;
  Invalidate;
end;

procedure TToolBar.SetTransparent(const AValue: Boolean);
begin
  if GetTransparent = AValue then exit;
  if AValue then
    ControlStyle := ControlStyle - [csOpaque]
  else
    ControlStyle := ControlStyle + [csOpaque];
  Invalidate;
end;

procedure TToolBar.SetWrapable(const AValue: Boolean);
begin
  if FWrapable = AValue then exit;
  FWrapable := AValue;
  ReAlign;
end;

procedure TToolBar.Notification(AComponent: TComponent;
  Operation: TOperation);
begin
  inherited Notification(AComponent, Operation);
  if Operation = opRemove then
  begin
    if AComponent = FImages then Images := nil;
    if AComponent = FHotImages then HotImages := nil;
    if AComponent = FDisabledImages then DisabledImages := nil;
  end;
end;

procedure TToolBar.SetImages(const AValue: TCustomImageList);
begin
  if FImages = AValue then Exit;
  if FImages <> nil then
    FImages.UnRegisterChanges(FImageChangeLink);
  FImages := AValue;
  if FImages <> nil then
  begin
    FImages.RegisterChanges(FImageChangeLink);
    FImages.FreeNotification(Self);
  end;
  UpdateVisibleBar;
end;

procedure TToolBar.SetImagesWidth(const aImagesWidth: Integer);
begin
  if FImagesWidth = aImagesWidth then Exit;
  FImagesWidth := aImagesWidth;
  UpdateVisibleBar;
end;

procedure TToolBar.DisabledImageListChange(Sender: TObject);
begin
  if (Sender = DisabledImages) then UpdateVisibleBar;
end;

procedure TToolBar.DoAutoAdjustLayout(const AMode: TLayoutAdjustmentPolicy;
  const AXProportion, AYProportion: Double);
begin
  inherited;

  if AMode in [lapAutoAdjustWithoutHorizontalScrolling, lapAutoAdjustForDPI] then
  begin
    BeginUpdate;
    try
      if ButtonWidthIsStored then
        ButtonWidth := Round(ButtonWidth * AXProportion);
      if ButtonHeightIsStored then
        ButtonHeight := Round(ButtonHeight * AYProportion);
      if DropDownWidthIsStored then
        DropDownWidth := Round(DropDownWidth * AXProportion);
      FRealizedButtonHeight := 0;
      FRealizedButtonWidth := 0;
      FRealizedDropDownWidth := 0;
      FRealizedButtonDropWidth := 0;
      FToolBarFlags := FToolBarFlags + [tbfUpdateVisibleBarNeeded];
    finally
      EndUpdate;
    end;
  end;
end;

procedure TToolBar.SetDisabledImages(const AValue: TCustomImageList);
begin
  if FDisabledImages = AValue then Exit;
  if FDisabledImages <> nil then
    FDisabledImages.UnRegisterChanges(FDisabledImageChangeLink);
  FDisabledImages := AValue;
  if FDisabledImages <> nil then
  begin
    FDisabledImages.RegisterChanges(FDisabledImageChangeLink);
    FDisabledImages.FreeNotification(Self);
  end;
  UpdateVisibleBar;
end;

procedure TToolBar.SetDropDownWidth(const ADropDownWidth: Integer);
begin
  if FDropDownWidth = ADropDownWidth then Exit;
  FDropDownWidth := ADropDownWidth;
  UpdateVisibleBar;
end;

procedure TToolBar.HotImageListChange(Sender: TObject);
begin
  if (Sender = HotImages) then UpdateVisibleBar;
end;

procedure TToolBar.UpdateVisibleBar;
var
  i: Integer;
begin
  if ([csLoading,csDestroying]*ComponentState<>[]) or (not HandleAllocated) then
  begin
    Include(FToolBarFlags,tbfUpdateVisibleBarNeeded);
    Exit;
  end;
  for i := 0 to FButtons.Count - 1 do
  begin
    TControl(FButtons[i]).InvalidatePreferredSize;
    TControl(FButtons[i]).AdjustSize;
  end;
  AdjustSize;
  Invalidate;
  Exclude(FToolBarFlags,tbfUpdateVisibleBarNeeded);
end;

procedure TToolBar.SetHotImages(const AValue: TCustomImageList);
begin
  if FHotImages = AValue then Exit;
  if FHotImages <> nil then
    FHotImages.UnRegisterChanges(FHotImageChangeLink);
  FHotImages := AValue;
  if FHotImages <> nil then
  begin
    FHotImages.RegisterChanges(FHotImageChangeLink);
    FHotImages.FreeNotification(Self);
  end;
  UpdateVisibleBar;
end;

procedure TToolBar.SetIndent(const AValue: Integer);
begin
  if FIndent = AValue then exit;
  FIndent := AValue;
  UpdateVisibleBar;
end;

procedure TToolBar.Loaded;
begin
  inherited Loaded;
  UpdateVisibleBar;
end;

procedure TToolBar.EndUpdate;
begin
  inherited EndUpdate;
  if FUpdateCount=0 then begin
    if tbfUpdateVisibleBarNeeded in FToolBarFlags then
      UpdateVisibleBar;
  end;
end;

function TToolBar.GetEnumerator: TToolBarEnumerator;
begin
  Result := TToolBarEnumerator.Create(Self);
end;

function TToolBar.GetDropDownWidth: Integer;
begin
  if FDropDownWidth < 0 then
  begin
    if FRealizedDropDownWidth = 0 then
      FRealizedDropDownWidth := ScaleScreenToFont(FThemeDropDownWidth);
    Result := FRealizedDropDownWidth;
  end else
    Result := FDropDownWidth;
end;

function TToolBar.GetButtonDropWidth: Integer;
begin
  if FDropDownWidth < 0 then
  begin
    if FRealizedButtonDropWidth = 0 then
      FRealizedButtonDropWidth := ScaleScreenToFont(FThemeButtonDropWidth);
    Result := FRealizedButtonDropWidth;
  end else
    Result := FDropDownWidth+FThemeButtonDropWidth-FThemeDropDownWidth;
end;

function TToolBar.GetButtonHeight: Integer;
begin
  if FButtonHeight < 0 then
  begin
    if FRealizedButtonHeight = 0 then
      FRealizedButtonHeight := Scale96ToFont(cDefButtonHeight);
    Result := FRealizedButtonHeight;
  end else
    Result := FButtonHeight;
end;

function TToolBar.GetButtonWidth: Integer;
begin
  if FButtonWidth < 0 then
  begin
    if FRealizedButtonWidth = 0 then
      FRealizedButtonWidth := Scale96ToFont(cDefButtonWidth);
    Result := FRealizedButtonWidth;
  end else
    Result := FButtonWidth;
end;

procedure TToolBar.Paint;
begin
  if csDesigning in ComponentState then
  begin
    Canvas.Pen.Color:=clRed;
    Canvas.FrameRect(Clientrect);
  end;
  inherited Paint;
  if Assigned(OnPaint) then
    OnPaint(Self);
end;

procedure TToolBar.SetButtonSize(NewButtonWidth, NewButtonHeight: integer);
var
  CurControl: TControl;
  NewWidth: Integer;
  NewHeight: Integer;
  i, RealButtonWidth, RealButtonHeight: Integer;
  ChangeW, ChangeH: Boolean;
begin
  ChangeW := FButtonWidth <> NewButtonWidth;
  ChangeH := FButtonHeight <> NewButtonHeight;
  if not (ChangeW or ChangeH) then Exit;
  
  FButtonWidth:=NewButtonWidth;
  FButtonHeight:=NewButtonHeight;
  RealButtonWidth := ButtonWidth;
  RealButtonHeight := ButtonHeight;
  if FUpdateCount > 0 then Exit;
  if [csLoading, csDestroying] * ComponentState <> [] then Exit;

  // set all children to ButtonWidth ButtonHeight
  BeginUpdate;
  try
    for i:=ControlCount-1 downto 0 do
    begin
      CurControl := Controls[i];
      CurControl.InvalidatePreferredSize;
      NewWidth := CurControl.Width;
      NewHeight := CurControl.Height;

      // width
      if ChangeW
      and (RealButtonWidth > 0)
      and not CurControl.AutoSize
      and (CurControl is TToolButton)
      and (CurControl.Align in [alNone, alLeft, alRight])
      then begin
        if TToolButton(CurControl).Style in [tbsButton,tbsCheck,tbsDropDown]
        then begin
          CurControl.GetPreferredSize(NewWidth,NewHeight);
          if NewWidth < RealButtonWidth then
            NewWidth := RealButtonWidth;
        end;
      end;
      
      // height
      // in horizontal toolbars the height is set by the toolbar independent of autosize
      if ChangeH
      and (RealButtonHeight > 0)
      and ((Align in [alTop, alBottom]) or not CurControl.AutoSize)
      then NewHeight := RealButtonHeight;
        
      CurControl.SetBounds(CurControl.Left, CurControl.Top, NewWidth, NewHeight);
    end;
  finally
    EndUpdate;
  end;
end;

function TToolBar.CanFocus: Boolean;
begin
  Result := False;
end;

procedure TToolBar.DoAutoSize;
begin
  // children are moved in ControlsAligned independent of AutoSize=true
end;

function TToolBar.DropDownWidthIsStored: Boolean;
begin
  Result := FDropDownWidth >= 0;
end;

procedure TToolBar.CalculatePreferredSize(var PreferredWidth,
  PreferredHeight: integer; WithThemeSpace: Boolean);
var
  NewWidth: Integer;
  NewHeight: Integer;
  FixedWidth: Boolean;
begin
  NewWidth:=0;
  NewHeight:=0;

  FixedWidth:=false;
  if (Parent<>nil)
  and (not Parent.AutoSize)
  and AnchorSideLeft.IsAnchoredToParent(akLeft)
  and AnchorSideRight.IsAnchoredToParent(akRight) then begin
    // the width depends on the parent
    // the width is fixed
    FixedWidth:=true;
    WrapButtons(Width, NewWidth, NewHeight, True);
    PreferredWidth := NewWidth;
    PreferredHeight := NewHeight;
    //DebugLn(['TToolBar.CalculatePreferredSize fixed width: ',PreferredWidth,'x',PreferredHeight]);
  end;
  if not FixedWidth then begin
    WrapButtons(Screen.Width,NewWidth,NewHeight,true);
    PreferredWidth := NewWidth;
    PreferredHeight := NewHeight;
  end;
  //DebugLn(['TToolBar.CalculatePreferredSize ',DbgSName(Self),' ',PreferredWidth,'x',PreferredHeight,' Count=',ControlCount]);
end;

{------------------------------------------------------------------------------
  function TToolBar.WrapButtons(var NewWidth, NewHeight: Integer): Boolean;
  
  Position all controls, that have Align=alNone.
  The controls are put from left to right.
  If the controls don't fit in a row and Wrapable=true, then the next row is
  started.
  If Wrapable=false, then the row is wrapped after the first button with
  Wrap=true.
------------------------------------------------------------------------------}
function TToolBar.WrapButtons(UseSize: integer;
  out NewWidth, NewHeight: Integer; Simulate: boolean): Boolean;
var
  ARect: TRect;
  x, y, w, h: Integer;
  NewControlWidth, NewControlHeight: Integer;
  CurControl: TControl;
  ObstacleControls: TFPList;
  FullSizeObstacleControls: TFPList;
  StartX, StartY: Integer;
  Vertical: Boolean; // true = ToolBar is vertical, controls are put in rows
  RowsLeftToRight: Boolean; // rows are left to right
  RealButtonWidth, RealButtonHeight: Integer;

  procedure CalculatePosition;
  var
    AlignedControl: TControl;
    NewBounds: TRect;
    SiblingBounds: TRect;
    j: Integer;
    PreferredBtnWidth, PreferredBtnHeight: Integer;
    Intersects: Boolean;
    IntersectsWithLimitedHeightControl: Boolean;
    StartedAtRowStart: Boolean;
  begin
    // compute the size
    if (CurControl is TToolButton) and (not CurControl.AutoSize) then
    begin
      PreferredBtnWidth := 0;
      PreferredBtnHeight := 0;
      CurControl.GetPreferredSize(PreferredBtnWidth, PreferredBtnHeight);
      if Vertical then
      begin
        // column layout
        NewControlHeight := PreferredBtnHeight;
        NewControlWidth := RealButtonWidth;
      end
      else
      begin
        // row layout
        NewControlHeight := RealButtonHeight;
        NewControlWidth := PreferredBtnWidth;
      end;
      if (TToolButton(CurControl).Style in [tbsButton, tbsDropDown, tbsCheck]) then
      begin
        if Vertical then
        begin
          // column layout
          if (NewControlHeight < RealButtonHeight) then
            NewControlHeight := RealButtonHeight;
        end
        else begin
          // row layout
          if (NewControlWidth < RealButtonWidth) then
            NewControlWidth := RealButtonWidth;
        end;
      end;
      //debugln(['CalculatePosition preferred toolbutton size ',DbgSName(CurControl),' ',NewControlWidth,' ',NewControlHeight]);
    end
    else
    if Vertical then
    begin
      // column layout
      NewControlWidth := RealButtonWidth;
      NewControlHeight := CurControl.Height;
    end
    else
    begin
      // row layout
      NewControlWidth := CurControl.Width;
      NewControlHeight := RealButtonHeight;
    end;

    if Vertical or RowsLeftToRight then
      NewBounds := Bounds(x, y, NewControlWidth, NewControlHeight)
    else
      NewBounds := Bounds(x-NewControlWidth, y, NewControlWidth, NewControlHeight);

    //DebugLn(['CalculatePosition ',DbgSName(CurControl),' NewBounds=',dbgs(NewBounds),' x=',x,' y=',y]);
    if Vertical then
      StartedAtRowStart := y = StartY
    else
      StartedAtRowStart := x = StartX;
    repeat
      // move control until it does not overlap
      IntersectsWithLimitedHeightControl := False;
      j := 0;
      while j < ObstacleControls.Count do
      begin
        AlignedControl := TControl(ObstacleControls[j]);
        SiblingBounds := AlignedControl.BoundsRect;
        Intersects:=(SiblingBounds.Right > NewBounds.Left) and
                    (SiblingBounds.Left < NewBounds.Right) and
                    (SiblingBounds.Bottom > NewBounds.Top) and
                    (SiblingBounds.Top < NewBounds.Bottom);
        if Intersects then
        begin
          //DebugLn(['CalculatePosition Move ',NewBounds.Left,'->',SiblingBounds.Right]);
          if Vertical then
          begin
            // column layout
            NewBounds.Top := SiblingBounds.Bottom;
            NewBounds.Bottom := NewBounds.Top + NewControlHeight;
          end
          else
          begin
            // row layout
            if RowsLeftToRight then
            begin
              NewBounds.Left := SiblingBounds.Right;
              NewBounds.Right := NewBounds.Left + NewControlWidth;
            end else begin
              NewBounds.Right := SiblingBounds.Left;
              NewBounds.Left := NewBounds.Right - NewControlWidth;
            end;
          end;
          j := 0; // check again, needed, because ObstacleControls are not sorted
                // (and can not be sorted, because they can overlap)
          if FullSizeObstacleControls.IndexOf(AlignedControl) < 0 then
            IntersectsWithLimitedHeightControl := True;
        end
        else
          inc(j);
      end;
      if Vertical then
      begin
        // column layout
        if (not Wrapable) or
           (NewBounds.Bottom <= ARect.Bottom) or (NewBounds.Top = StartY) or
           (StartedAtRowStart and not IntersectsWithLimitedHeightControl) then
        begin
          // control fits into the row
          //DebugLn(['CalculatePosition fits: ',DbgSName(CurControl),' ',dbgs(NewBounds)]);
          x := NewBounds.Left;
          y := NewBounds.Top;
          break;
        end;

        // try next row
        NewBounds.Top := StartY;
        NewBounds.Bottom := NewBounds.Top + NewControlHeight;
        inc(NewBounds.Left, RealButtonWidth);
        inc(NewBounds.Right, RealButtonWidth);
      end
      else
      begin
        // row layout
        if (not Wrapable)
        or (StartedAtRowStart and not IntersectsWithLimitedHeightControl)
        or (RowsLeftToRight and ((NewBounds.Left = StartX) or (NewBounds.Right <= ARect.Right)))
        or ((not RowsLeftToRight) and ((NewBounds.Right = StartX) or (NewBounds.Left >= ARect.Left)))
        then begin
          // control fits into the row
          //DebugLn(['CalculatePosition fits: ',DbgSName(CurControl),' ',dbgs(NewBounds)]);
          x := NewBounds.Left;
          y := NewBounds.Top;
          break;
        end;

        //debugln(['CalculatePosition overlaps: ',DbgSName(CurControl),' ',dbgs(NewBounds),' ARect=',DbgS(ARect),' StartX=',StartX]);

        // try next row
        inc(NewBounds.Top, RealButtonHeight);
        inc(NewBounds.Bottom, RealButtonHeight);
        if RowsLeftToRight then
        begin
          NewBounds.Left := StartX;
          NewBounds.Right := NewBounds.Left + NewControlWidth;
        end else begin
          NewBounds.Right := StartX;
          NewBounds.Left := NewBounds.Right - NewControlWidth;
        end;
      end;
      StartedAtRowStart := True;
      //DebugLn('CalculatePosition Next Row ',DbgSName(CurControl),' ',dbgs(NewBounds));
    until false;
  end;

  function AnchoredToParent(AControl: TControl; Side: TAnchorKind): boolean;
  var
    AnchorControl: TControl;
    AnchorSide: TAnchorSideReference;
    p: integer;
  begin
    if not (Side in CurControl.Anchors) then exit(false);
    AnchorControl:=nil;
    CurControl.AnchorSide[Side].GetSidePosition(AnchorControl,AnchorSide,P);
    if AnchorControl=nil then
      AnchorControl:=CurControl;
    Result:=(Side in AnchorControl.Anchors);
  end;

var
  OrderedControls: TFPList;
  CurClientRect: TRect;
  AdjustClientFrame: TRect;
  i: Integer;
  GrowSide: TAnchorKind; // when a line is full, grow the TToolBar in this direction
  SeparatorWidthChange: Boolean;
begin
  //DebugLn(['WrapButtons ',DbgSName(Self),' Wrapable=',Wrapable,' ',dbgs(BoundsRect),' Vertical=',IsVertical,' RTL=',UseRightToLeftAlignment,' Simulate=',Simulate]);
  Result := True;
  RealButtonWidth := ButtonWidth;
  RealButtonHeight := ButtonHeight;
  Vertical := IsVertical;
  NewWidth := 0;
  NewHeight := 0;
  ObstacleControls := TFPList.Create;
  FullSizeObstacleControls := TFPList.Create;
  OrderedControls := TFPList.Create;
  if not Simulate then
    FRowCount := 0;
  DisableAlign;
  BeginUpdate;
  try
    if Vertical then
    begin
      GrowSide := akRight;
      RowsLeftToRight := true;
    end
    else begin
      GrowSide := akBottom;
      RowsLeftToRight:=not UseRightToLeftAlignment;
    end;
    for i:=0 to ControlCount-1 do
    begin
      CurControl := Controls[i];
      if CurControl.Align = alNone then begin
        // this control will be auto positioned and auto sized by this function
        // => set to Left,Top anchoring
        CurControl.Anchors:=[akLeft,akTop];
        CurControl.AnchorSide[akLeft].Control:=nil;
        CurControl.AnchorSide[akTop].Control:=nil;
        OrderedControls.Add(CurControl);
      end else begin
        // this control will be positioned/sized by the default LCL functions
        // the OrderedControls will be positioned around them (without overlapping)
        ObstacleControls.Add(CurControl);
        // check if this obstacle auto grows, for example if this toolbar is
        // aligned to the top, check if the obstacle grows downwards (Align=alLeft)
        if AnchoredToParent(CurControl,GrowSide) then begin
          // this obstacle auto grows (important for the wrap algorithm)
          FullSizeObstacleControls.Add(CurControl);
        end;
      end;
    end;
    // sort OrderedControls
    if Vertical then
      OrderedControls.Sort(TListSortCompare(@CompareToolBarControlVert))
    else
      OrderedControls.Sort(TListSortCompare(@CompareToolBarControlHorz));

    // position OrderedControls
    CurClientRect := ClientRect;
    if Vertical then
      inc(CurClientRect.Bottom, UseSize - Height)
    else
      inc(CurClientRect.Right, UseSize - Width);
    ARect := CurClientRect;
    AdjustClientRect(ARect);
    AdjustClientFrame.Left := ARect.Left - CurClientRect.Left;
    AdjustClientFrame.Top := ARect.Top - CurClientRect.Top;
    AdjustClientFrame.Right := CurClientRect.Right - ARect.Right;
    AdjustClientFrame.Bottom := CurClientRect.Bottom - ARect.Bottom;
    //DebugLn(['TToolBar.WrapButtons ',DbgSName(Self),' ARect=',dbgs(ARect)]);
    // important: top, left button must start in the corner of AdjustClientRect
    // otherwise Toolbar.AutoSize=true will create an endless loop
    if Vertical or RowsLeftToRight then
      StartX := ARect.Left
    else
      StartX := ARect.Right;
    StartY := ARect.Top;
    x := StartX;
    y := StartY;
    //debugln(['TToolBar.WrapButtons Start=',StartX,' ',StartY]);
    for i := 0 to OrderedControls.Count - 1 do
    begin
      CurControl := TControl(OrderedControls[i]);
      if not CurControl.IsControlVisible then
        Continue;
      CalculatePosition;
      //DebugLn(['WrapButtons ',DbgSName(CurControl),' ',x,',',y,',',CurControl.Width,'x',CurControl.Height]);
      if CurControl.AutoSize then
      begin
        w := CurControl.Width;
        h := CurControl.Height;
      end
      else
      begin
        w := NewControlWidth;
        h := NewControlHeight;
      end;

      w := CurControl.Constraints.MinMaxWidth(w);
      h := CurControl.Constraints.MinMaxHeight(h);
      SeparatorWidthChange := (CurControl is TToolButton) and
        (TToolButton(CurControl).Style in [tbsSeparator, tbsDivider]);
      if SeparatorWidthChange then begin
        if not Vertical then begin
          SeparatorWidthChange := (w <> CurControl.Width);
          w := CurControl.Width;
        end else begin
          SeparatorWidthChange := (h <> CurControl.Height);
          h := CurControl.Height;
        end;
      end;
      if Vertical <> FPrevVertical then  //swap h/w when orientation changed
      begin
        if (CurControl is TToolButton) and
          (TToolButton(CurControl).Style in [tbsSeparator, tbsDivider]) then
          begin
            if not Vertical then
              w := CurControl.Height
            else
              h := CurControl.Width;
          end;
      end;
      if (CurControl.Left <> x) or (CurControl.Top <> y) or
         (CurControl.Width <> w) or (CurControl.Height <> h) then
      begin
        //DebugLn(['TToolBar.WrapButtons moving child: ',DbgSName(CurControl),' Old=',dbgs(CurControl.BoundsRect),' New=',dbgs(Bounds(x,y,w,h))]);
        if not Simulate then
        begin
          //DebugLn(['TToolBar.WrapButtons moving child: ',DbgSName(CurControl),' Old=',dbgs(CurControl.BoundsRect),' New=',dbgs(Bounds(x,y,w,h))]);
          if SeparatorWidthChange then
            CurControl.SetBoundsKeepBase(x,y,w,h)
          else
            CurControl.SetBounds(x,y,w,h);
          //DebugLn(['TToolBar.WrapButtons moved child: ',DbgSName(CurControl),' ',dbgs(CurControl.BoundsRect)]);
        end;
      end;
      
      // adjust NewWidth, NewHeight
      if Vertical or RowsLeftToRight then
        NewWidth := Max(NewWidth, x + w + AdjustClientFrame.Right)
      else
        NewWidth := Max(NewWidth, ARect.Right - x + ARect.Left + AdjustClientFrame.Right);
      NewHeight := Max(NewHeight, y + h + AdjustClientFrame.Bottom);

      // step to next position
      if Vertical then
      begin
        inc(y, h);
        if not Wrapable and
          (CurControl is TToolButton) and
          (TToolButton(CurControl).Wrap) then
        begin
          // user forced wrap -> start new line
          y := StartY;
          inc(x, RealButtonWidth);
          if not Simulate then
            inc(FRowCount);
        end;
      end
      else
      begin
        if RowsLeftToRight then
          inc(x, w);
        if not Wrapable and
          (CurControl is TToolButton) and
          (TToolButton(CurControl).Wrap) then
        begin
          // user forced wrap -> start new line
          x := StartX;
          inc(y, RealButtonHeight);
          if not Simulate then
            inc(FRowCount);
        end;
      end;
    end;
  finally
    ObstacleControls.Free;
    OrderedControls.Free;
    FullSizeObstacleControls.Free;
    EndUpdate;
    EnableAlign;
    FPrevVertical := Vertical;
  end;
end;

procedure TToolBar.CNDropDownClosed(var Message: TLMessage);
begin
  CloseCurrentMenu;
end;

procedure TToolBar.AdjustClientRect(var ARect: TRect);
begin
  inherited AdjustClientRect(ARect);
  inc(ARect.Left, Indent);
end;

class function TToolBar.GetControlClassDefaultSize: TSize;
begin
  Result.CX := 150;
  Result.CY := 26;
end;

function TToolBar.FindButtonFromAccel(Accel: Word): TToolButton;
var
  i: Integer;
begin
  for i := 0 to FButtons.Count - 1 do
    if TControl(FButtons[i]) is TToolButton then
    begin
      Result := Buttons[i];
      if Result.Visible and Result.Enabled and IsAccel(Accel, Result.Caption) then
        Exit;
    end;
  Result := nil;
end;

procedure TToolBar.FontChanged(Sender: TObject);
begin
  inherited FontChanged(Sender);
  ApplyFontForButtons;
  FRealizedButtonWidth := 0;
  FRealizedButtonHeight := 0;
  FRealizedDropDownWidth := 0;
  FRealizedButtonDropWidth := 0;
end;

function TToolBar.CheckMenuDropdown(Button: TToolButton): Boolean;
var
  APoint: TPoint;
begin
  Result := False;
  if Button = nil then
    Exit;
  if Assigned(FCurrentMenu) then
  begin
    CloseCurrentMenu;
    if FCurrentMenuAutoFree then
    begin
      FreeAndNil(FCurrentMenu);
      FCurrentMenuAutoFree := False;
    end;
  end;
  FSrcMenuItem := nil;
  FSrcMenu := nil;
  FDropDownButton := Button;
  if Assigned(Button.DropdownMenu) then
    // the button has a popupenu
    FCurrentMenu := Button.DropdownMenu
  else
  if Assigned(Button.MenuItem) then
  begin
    // the button has a menuitem

    // since the button is clicked - menu item must be clicked too
    Button.MenuItem.Click;
    // -> create a temporary TPopupMenu and move all child menuitems
    FCurrentMenuAutoFree := True;
    FCurrentMenu := TPopupMenu.Create(Self);
    FSrcMenuItem := Button.MenuItem;
    FSrcMenu := FSrcMenuItem.GetParentMenu;
    FCurrentMenu.Items.HelpContext := FSrcMenuItem.HelpContext;
    if Assigned(FSrcMenu) then
      FCurrentMenu.Images := FSrcMenu.Images;
    MoveSubMenuItems(FSrcMenuItem, FCurrentMenu.Items);
  end
  else
    Exit;
  FCurrentMenu.PopupComponent := Self;
  APoint := Button.ClientToScreen(Point(0, Button.ClientHeight));
  if FCurrentMenu.IsRightToLeft then Inc(APoint.X, Button.Width);
  FCurrentMenu.Popup(APoint.X, APoint.Y);
  // The next command will be executed after popup menu close because Popup is a
  // syncronous method. We can't send this message on Menu.Close event because
  // Click happen after the Close event and if we remove all the menu items there
  // we will not be able to handle the Click event
  // we also need to postpone this message to allow after Popup cleanup and click happen
  PostMessage(Handle, CN_DROPDOWNCLOSED, 0, 0);
  Result := True;
end;

procedure TToolBar.ClickButton(Button: TToolButton);
begin
  Button.Click;
end;