summaryrefslogtreecommitdiff
path: root/src/gui/fpg_tab.pas
blob: 29addb1286b2fc3d80cd475af7c6ef1aeddb4373 (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
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
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
{
    fpGUI  -  Free Pascal GUI Toolkit

    Copyright (C) 2006 - 2012 See the file AUTHORS.txt, included in this
    distribution, for details of the copyright.

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

    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.

    Description:
      Defines a Page Control and Tab Sheets.
}

unit fpg_tab;

{$mode objfpc}{$H+}

{
  TODO:
    * Tab Styles (tab, button, flat button, angled)
    * Better keyboard support
    * Focus rectangle drawn on tabs itself
    * FindNextPage() must be implemented
    * Popup menu for tab selection. Should occur with RClick on tabs.
}

interface

uses
  Classes,
  SysUtils,
  fpg_base,
  fpg_main,
  fpg_widget,
  fpg_button,
  fpg_menu;
  
type
  // forward declaration
  TfpgPageControl = class;
  
  TfpgTabStyle    = (tsTabs, tsButtons, tsFlatButtons);
  TfpgTabPosition = (tpTop, tpBottom, tpLeft, tpRight, tpNone);
  TfpgTabOption   = (to_PMenuClose, to_PMenuShowAvailTabs);

  TfpgTabOptions = set of TfpgTabOption;


  TfpgTabSheet = class(TfpgWidget)
  private
    FPageControl: TfpgPageControl;
    FText: string;
    FTabVisible: boolean;
    function    GetPageControl: TfpgPageControl;
    function    GetPageIndex: Integer;
    function    GetText: string;
    procedure   SetPageIndex(const AValue: Integer);
    procedure   SetText(const AValue: string);
    procedure   SetPageControl(APageControl: TfpgPageControl);
  protected
    procedure   HandlePaint; override;
    procedure   SetName(const NewName: TComponentName); override;
  public
    constructor Create(AOwner: TComponent); override;
    constructor CreateWithTitle(AOwner: TComponent; const AText: TfpgString = ''); virtual;
    destructor  Destroy; override;
    procedure   AfterConstruction; override;
    property    PageIndex: Integer read GetPageIndex write SetPageIndex;
    property    PageControl: TfpgPageControl read FPageControl write SetPageControl;
    property    TabVisible: boolean read FTabVisible write FTabVisible;
  published
    property    BackgroundColor;
    property    Enabled;
    property    Text: string read GetText write SetText;
    property    OnPaint;
  end;


  TTabSheetChange = procedure(Sender: TObject; NewActiveSheet: TfpgTabSheet) of object;
  TTabSheetClosing = procedure(Sender: TObject; ATabSheet: TfpgTabSheet) of object;
  
  
  TfpgPageControl = class(TfpgWidget)
  private
    FFont: TfpgFont;
    FActivePage: TfpgTabSheet;
    FMargin: integer;
    FFixedTabWidth: integer;
    FFixedTabHeight: Integer;
    FOnClosingTabSheet: TTabSheetClosing;
    FPages: TList;
    FActivePageIndex: integer;
    FOnChange: TTabSheetChange;
    FRightButton: TfpgButton;         // bottom/right
    FLeftButton: TfpgButton;          // left/top
    FFirstTabButton: TfpgTabSheet;    // when tabs don't fit in screen this is the first button on screen when tabs are scrolled
    FSortPages: boolean;
    FStyle: TfpgTabStyle;
    FTabPosition: TfpgTabPosition;
    FPopupMenu: TfpgPopupMenu;
    FTabOptions: TfpgTabOptions;
    FLastRClickPos: TfpgPoint;
    FUpdateCount: Integer;
    FActiveTabColor: TfpgColor;
    function    GetActivePageIndex: integer;
    function    GetPage(AIndex: integer): TfpgTabSheet;
    function    GetPageCount: Integer;
    procedure   InsertPage(var APage: TfpgTabSheet; SuppressOnChangeEvent: boolean = False);
    procedure   RemovePage(const APage: TfpgTabSheet);
    procedure   SetActivePageIndex(const AValue: integer);
    procedure   SetActivePage(const AValue: TfpgTabSheet);
    procedure   PositionTabSheets;
    procedure   PositionTabSheet(var APage: TfpgTabSheet);
    function    MaxButtonWidthSum: integer;
    function    MaxButtonHeightSum: integer;
    function    MaxButtonWidth: integer;
    function    ButtonHeight: integer;
    function    ButtonWidth(AText: string): integer;
    procedure   SetFixedTabWidth(const AValue: integer);
    procedure   SetFixedTabHeight(const AValue: integer);
    function    GetTabText(AText: string): string;
    procedure   LeftButtonClick(Sender: TObject);
    procedure   RightButtonClick(Sender: TObject);
    function    FindNextPage(ACurrent: TfpgTabSheet; AForward: boolean): TfpgTabSheet;
    procedure   SetSortPages(const AValue: boolean);
    procedure   SetStyle(const AValue: TfpgTabStyle);
    procedure   SetTabPosition(const AValue: TfpgTabPosition);
    procedure   DoPageChange(ATabSheet: TfpgTabSheet);
    procedure   DoTabSheetClosing(ATabSheet: TfpgTabSheet);
    function    DrawTab(const rect: TfpgRect; const Selected: Boolean = False; const Mode: Integer = 1): TfpgRect;
    procedure   pmCloseTab(Sender: TObject);
    function    GetActiveTabColor: TfpgColor;
    procedure   SetActiveTabColor(AValue: TfpgColor);
  protected
    procedure   SetBackgroundColor(const AValue: TfpgColor); override;
    procedure   OrderSheets; // currently using bubblesort
    procedure   RePaintTitles; virtual;
    procedure   HandlePaint; override;
    procedure   HandleShow; override;
    procedure   HandleLMouseUp(x, y: integer; shiftstate: TShiftState); override;
    procedure   HandleRMouseUp(x, y: integer; shiftstate: TShiftState); override;
    procedure   HandleKeyPress(var keycode: word; var shiftstate: TShiftState; var consumed: boolean); override;
    procedure   RePaint; override;
  public
    constructor Create(AOwner: TComponent); override;
    destructor  Destroy; override;
    procedure   BeginUpdate;
    procedure   EndUpdate;
    function    TabSheetAtPos(const x, y: integer): TfpgTabSheet;
    function    AppendTabSheet(ATitle: string): TfpgTabSheet;
    procedure   RemoveTabSheet(ATabSheet: TfpgTabSheet);
    property    PageCount: Integer read GetPageCount;
    property    ActivePage: TfpgTabSheet read FActivePage write SetActivePage;
    property    Pages[AIndex: integer]: TfpgTabSheet read GetPage;
    property    OnChange: TTabSheetChange read FOnChange write FOnChange;
    property    OnClosingTabSheet: TTabSheetClosing read FOnClosingTabSheet write FOnClosingTabSheet;
  published
    property    ActivePageIndex: integer read GetActivePageIndex write SetActivePageIndex default 0;
    property    ActiveTabColor: TfpgColor read GetActiveTabColor write SetActiveTabColor default clWindowBackground;
    property    Align;
    property    BackgroundColor;
    property    Enabled;
    property    FixedTabWidth: integer read FFixedTabWidth write SetFixedTabWidth default 0;
    property    FixedTabHeight: integer read FFixedTabHeight write SetFixedTabHeight default 21;
    property    Hint;
    property    Options: TfpgTabOptions read FTabOptions write FTabOptions;
    property    ParentShowHint;
    property    ShowHint;
    property    SortPages: boolean read FSortPages write SetSortPages default False;
    property    Style: TfpgTabStyle read FStyle write SetStyle default tsTabs;
    property    TabOrder;
    property    TabPosition: TfpgTabPosition read FTabPosition write SetTabPosition default tpTop;
    property    TextColor;
    property    OnShowHint;
  end;


implementation

uses
  fpg_stringutils;

const
  DFL_TAB_HEIGHT = 21;
  DFL_TAB_WIDTH = 0;
  
  
// compare function used by FPages.Sort

function SortCompare(Item1, Item2: Pointer): integer;
begin
  Result := CompareText(TfpgTabSheet(Item1).Text, TfpgTabSheet(Item2).Text);
end;

{ TfpgTabSheet }

function TfpgTabSheet.GetPageControl: TfpgPageControl;
begin
  if Owner is TfpgPageControl then
    Result := TfpgPageControl(Owner)
  else
    Result := nil;
end;

function TfpgTabSheet.GetPageIndex: Integer;
begin
  if PageControl <> nil then
    Result := PageControl.FPages.IndexOf(Self)
  else
    Result := -1;
end;

function TfpgTabSheet.GetText: string;
begin
  Result := FText;
end;

procedure TfpgTabSheet.SetPageIndex(const AValue: Integer);
begin
  if PageControl <> nil then
  begin
    PageControl.FPages.Move(PageIndex, AValue);
    PageControl.RePaint;//Titles;
  end;
end;

procedure TfpgTabSheet.SetText(const AValue: string);
begin
  if FText = AValue then
    Exit; //==>
  FText := AValue;
  if PageControl <> nil then
    PageControl.Invalidate;
end;

procedure TfpgTabSheet.HandlePaint;
begin
  inherited HandlePaint;
  Canvas.Clear(FBackgroundColor);
end;

procedure TfpgTabSheet.SetName(const NewName: TComponentName);
var
  old: String;
begin
  old := NewName;
  inherited SetName(NewName);
  if (csDesigning in ComponentState) then
  begin
    if (Text = '') or (Text = old) then
      Text := NewName;
  end;
end;

constructor TfpgTabSheet.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FText := '';
  FTabVisible:= True;
  FFocusable := True;
  FBackgroundColor := Parent.BackgroundColor;
  FTextColor := Parent.TextColor;
  FIsContainer := True;
end;

constructor TfpgTabSheet.CreateWithTitle(AOwner: TComponent; const AText: TfpgString);
begin
  Create(AOwner);
  FText := AText;
end;

destructor TfpgTabSheet.Destroy;
begin
  if FPageControl <> nil then
    FPageControl.RemovePage(self);
  inherited Destroy;
end;

procedure TfpgTabSheet.AfterConstruction;
begin
  if (Owner <> nil) and (Owner is TfpgPageControl) then
  begin
    FPageControl:=TfpgPageControl(Owner);
    FPageControl.InsertPage(self, True);
  end;
  inherited AfterConstruction;
end;

procedure TfpgTabSheet.SetPageControl(APageControl: TfpgPageControl);
begin
  FPageControl := APageControl;
  if APageControl <> nil then
    FPageControl.InsertPage(Self);
end;

  
{ TfpgPageControl }

function TfpgPageControl.GetActivePageIndex: integer;
begin
  Result := FActivePageIndex;
end;

function TfpgPageControl.GetPage(AIndex: integer): TfpgTabSheet;
begin
  Result := nil;
  if (AIndex >= 0) and (AIndex < FPages.Count) then
    Result := TfpgTabSheet(FPages[AIndex]);
end;

function TfpgPageControl.GetPageCount: Integer;
begin
  Result := FPages.Count;
end;

procedure TfpgPageControl.InsertPage(var APage: TfpgTabSheet; SuppressOnChangeEvent: boolean = False);
begin
  if FPages.IndexOf(APage) <> -1 then
    Exit; //==>   The page has already been added.
  FPages.Add(APage);
  PositionTabSheets;
  { TODO: This behaviour could maybe be controlled by a Options property }
  if FPages.Count=1 then
  begin
    if SuppressOnChangeEvent then
      Loading;
    ActivePage := APage;
    if SuppressOnChangeEvent then
      Loaded;
  end;
end;

procedure TfpgPageControl.RemovePage(const APage: TfpgTabSheet);
var
  i: integer;
begin
  if APage = nil then
    Exit; // ==>
  if FPages.Count =0 then
    Exit; // ==>
  
  if FPages.Count > 1 then              
  begin
     i:=FPages.IndexOf(APage);
     FPages.Remove(APage);
    APage.PageControl:=nil;
    APage.Visible:=false;
    if i = ActivePageIndex then
    begin	    
      if i > FPages.Count-1 then
         ActivePage:=TfpgTabSheet(FPages.Last)
      else if i = 0 then
        ActivePage:= TfpgTabSheet(FPages.First)
      else
        ActivePage:=TfpgTabSheet(FPages[i]);
    end
    else if i < ActivePageIndex then
      ActivePage:=TfpgTabSheet(Pages[i-1]);	      
  end      
  else
  begin
    FPages.Remove(APage);
    APage.PageControl := nil;
    APage.Visible := False;
    ActivePage := nil;
  end;
end;

procedure TfpgPageControl.SetActivePageIndex(const AValue: integer);
begin
  if FPages.Count = 0 then
    exit;
  if (AValue >= 0) or (AValue < FPages.Count) then
    ActivePage := TfpgTabSheet(FPages[AValue]);
end;

procedure TfpgPageControl.SetActivePage(const AValue: TfpgTabSheet);
begin
  if FActivePage = AValue then
    Exit; //==>
  FActivePage := AValue;
  ActiveWidget := AValue;
  if AValue <> nil then
    FActivePageIndex := FPages.IndexOf(AValue);
  RePaint;
  DoPageChange(FActivePage);
end;

procedure TfpgPageControl.PositionTabSheets;
var
  i: integer;
  t: TfpgTabSheet;
begin
  for i := 0 to FPages.Count-1 do
  begin
    t := TfpgTabSheet(FPages[i]);
    PositionTabSheet(t);
    t.Anchors := [anLeft, anTop, anRight, anBottom];
  end;
end;

procedure TfpgPageControl.PositionTabSheet(var APage: TfpgTabSheet);
var
  r: TRect;
  w: integer;
  wd: integer;  { width delta }
  h: integer;
  hd: integer;  { height delta }
  msg: TfpgMessageParams;
begin
  // PageControl has bevelled edges in some themes
  r := fpgStyle.GetControlFrameBorders;

  { Calculate and set Width and Height }
  if TabPosition in [tpTop, tpBottom] then
  begin
    w := Width - (FMargin*2) - r.Left - r.Right;
    wd := APage.Width - w;
    APage.Width   := w;
    h := Height - ButtonHeight - (FMargin*2) - r.Top - r.Bottom;
    hd := APage.Height - h;
    APage.Height  := h;
  end
  else if TabPosition in [tpLeft, tpRight] then
  begin
    w := Width - MaxButtonWidth - (FMargin*2) - r.Left - r.Right;
    wd := APage.Width - w;
    APage.Width   := w;
    h := Height - (FMargin*2) - r.Top - r.Bottom;
    hd := APage.Height - h;
    APage.Height  := h;
  end
  else
  begin   // tpNone
    w := Width - (FMargin*2) - r.Left - r.Right;
    wd := APage.Width - w;
    APage.Width   := w;
    h := Height - (FMargin*2) - r.Top - r.Bottom;
    hd := APage.Height - h;
    APage.Height  := h;
  end;

  { Calculate and set Top and Left }
  if TabPosition = tpTop then
  begin
    APage.Left    := FMargin + r.Left;
    APage.Top     := ButtonHeight + FMargin + r.Top;
  end
  else if TabPosition = tpBottom then
  begin
    APage.Left    := FMargin + r.Left;
    APage.Top     := FMargin + r.Top;
  end
  else if TabPosition = tpLeft then
  begin
    APage.Left    := MaxButtonWidth + FMargin + r.Left;
    APage.Top     := FMargin + r.Top;
  end
  else if TabPosition = tpRight then
  begin
    APage.Left    := FMargin + r.Left;
    APage.Top     := FMargin + r.Top;
  end;

  if TabPosition in [tpNone] then
  begin
    APage.Left    := FMargin + r.Left;
    APage.Top     := FMargin + r.Top;
  end;

  APage.UpdateWindowPosition; { Internal state is now resolved }
end;

function TfpgPageControl.MaxButtonWidthSum: integer;
var
  i: integer;
  t: TfpgTabSheet;
begin
  {$IFDEF DEBUG}writeln(Classname + '.MaxButtonWidthSum');{$ENDIF}
  Result := 0;
  
  for i := 0 to FPages.Count-1 do
  begin
    t := TfpgTabSheet(FPages[i]);
    Result := Result + ButtonWidth(t.Text);
  end;
end;

function TfpgPageControl.MaxButtonHeightSum: integer;
begin
  result := PageCount * ButtonHeight;
end;

function TfpgPageControl.MaxButtonWidth: integer;
var
  t: TfpgTabSheet;
  i: integer;
begin
  Result := 0;
  if FixedTabWidth > 0 then
  begin
    Result := FixedTabWidth;
  end
  else
  begin
    for i := 0 to FPages.Count-1 do
    begin
      t := TfpgTabSheet(FPages[i]);
      if ButtonWidth(t.Text) > Result then
        Result := ButtonWidth(t.Text);
    end;
  end;
end;

function TfpgPageControl.ButtonHeight: integer;
begin
  if FFixedTabHeight > 0 then
    result := FFixedTabHeight
  else
    result := FFont.Height + 10;   { TODO: correct this }
end;

function TfpgPageControl.ButtonWidth(AText: string): integer;
begin
  if FFixedTabWidth > 0 then
    result := FFixedTabWidth
  else
    result := FFont.TextWidth(AText) + 10;
end;

procedure TfpgPageControl.SetFixedTabWidth(const AValue: integer);
begin
  if FFixedTabWidth = AValue then
    Exit; //==>
  if AValue >= 5 then
  begin
    FFixedTabWidth := AValue;
    RePaint;
  end;
end;

procedure TfpgPageControl.SetFixedTabHeight(const AValue: integer);
begin
  if FFixedTabHeight = AValue then
    Exit; //==>
  if AValue >= 5 then
  begin
    FFixedTabHeight := AValue;
    RePaint;
  end;
end;

function TfpgPageControl.GetTabText(AText: string): string;
var
  s, s1: string;
  i: integer;
begin
  {$IFDEF DEBUG}writeln(Classname + '.GetTabText');{$ENDIF}
  Result  := AText;
  s       := AText;
  s1      := '';
  i       := 1;
  if FFixedTabWidth > 0 then
  begin
    while FFont.TextWidth(s1) < (FFixedTabWidth-10) do
    begin
      if Length(s1) = Length(s) then
        Break;
      s1 := UTF8Copy(s, 1, i);
      inc(i);
    end;
    if FFont.TextWidth(s1) > (FFixedTabWidth-10) then
      UTF8Delete(s1, UTF8Length(s1), 1);
    if Length(s1) > 0 then
      s1 := Trim(s1);
    Result := s1;
  end;
end;

procedure TfpgPageControl.LeftButtonClick(Sender: TObject);
begin
  {$IFDEF DEBUG}writeln(Classname + '.LeftButtonClick');{$ENDIF}
  if FFirstTabButton <> nil then
  begin
    if TfpgTabSheet(FPages.First) <> FFirstTabButton then
    begin
      FFirstTabButton := TfpgTabSheet(FPages[FPages.IndexOf(FFirstTabButton)-1]);
      RePaint;
    end;
  end;
end;

procedure TfpgPageControl.RightButtonClick(Sender: TObject);
begin
  {$IFDEF DEBUG}writeln(Classname + '.RightButtonClick');{$ENDIF}
  if FFirstTabButton <> nil then
  begin
    if TfpgTabSheet(FPages.Last) <> FFirstTabButton then
    begin
      FFirstTabButton := TfpgTabSheet(FPages[FPages.IndexOf(FFirstTabButton)+1]);
      RePaint;
    end;
  end;
end;

function TfpgPageControl.FindNextPage(ACurrent: TfpgTabSheet; AForward: boolean): TfpgTabSheet;
begin
  // To be completed
  result := nil;
end;

procedure TfpgPageControl.SetSortPages(const AValue: boolean);
begin
  if FSortPages = AValue then
    Exit; //==>
  FSortPages := AValue;
  RePaint;
end;

procedure TfpgPageControl.SetStyle(const AValue: TfpgTabStyle);
begin
  if FStyle = AValue then
    Exit; //==>
  FStyle := AValue;
  Invalidate;
end;

procedure TfpgPageControl.SetTabPosition(const AValue: TfpgTabPosition);
begin
  if FTabPosition = AValue then
    Exit; //==>
  FTabPosition := AValue;
  if FTabPosition = tpNone then
  begin
    FLeftButton.Visible := False;
    FRightButton.Visible := False;
  end;
  RePaint;
end;

procedure TfpgPageControl.DoPageChange(ATabSheet: TfpgTabSheet);
begin
  if (csLoading in ComponentState) then
    Exit;
  if (csDesigning in ComponentState) then
    Exit;
  if Assigned(FOnChange) then
    FOnChange(self, ATabSheet);
end;

procedure TfpgPageControl.DoTabSheetClosing(ATabSheet: TfpgTabSheet);
begin
  if (csLoading in ComponentState) then
    Exit;
  if (csDesigning in ComponentState) then
    Exit;
  if Assigned(FOnClosingTabSheet) then
    FOnClosingTabSheet(self, ATabSheet);
end;

{ Mode = 1 means the background tabs. Mode = 2 means the Active Tab }
function TfpgPageControl.DrawTab(const rect: TfpgRect; const Selected: Boolean = False; const Mode: Integer = 1): TfpgRect;
var
  r: TfpgRect;
begin
  r := rect;
  if Selected then
  begin
    Result := rect;
    InflateRect(Result, 2, 2);
    Exit; //==>
  end;

  if Mode = 2 then
  begin
    r.Height -= 1;
    if TabPosition = tpBottom then
      r.Top += 1;
    Canvas.SetColor(ActiveTabColor);
  end
  else
    Canvas.SetColor(BackgroundColor);

  case TabPosition of
    tpTop:
      begin
        Canvas.FillRectangle(r.Left+1, r.Top+1, r.Width-3, r.Height-2);     // fill tab background
        Canvas.SetColor(clHilite2);
        Canvas.DrawLine(r.Left, r.Bottom-2 , r.Left, r.Top+2);        // left edge
        Canvas.DrawLine(r.Left, r.Top+2 , r.Left+2, r.Top);           // left rounder edge
        Canvas.DrawLine(r.Left+2,  r.Top, r.Right-1, r.Top);          // top edge
        Canvas.SetColor(clShadow1);
        Canvas.DrawLine(r.Right-1, r.Top+1, r.Right-1, r.Bottom-1);   // right inner edge
        Canvas.SetColor(clShadow2);
        Canvas.DrawLine(r.Right-1, r.Top+1, r.Right, r.Top+2);        // right rounded edge (1px)
        Canvas.DrawLine(r.Right, r.Top+2, r.Right, r.Bottom-1);       // right outer edge
      end;

    tpBottom:
      begin
        Canvas.FillRectangle(r.Left, r.Top, r.Width-1, r.Height-2);   // fill tab background
        Canvas.SetColor(clHilite2);
        Canvas.DrawLine(r.Left, r.Top, r.Left, r.Bottom-1);           // left edge
        Canvas.SetColor(clShadow2);
        Canvas.DrawLine(r.Left+2,  r.Bottom, r.Right-1, r.Bottom);    // bottom outer edge
        Canvas.SetColor(clShadow1);
        Canvas.DrawLine(r.Right-1, r.Bottom-1, r.Right-1, r.Top-1);   // right inner edge
        Canvas.DrawLine(r.Left+1,  r.Bottom-1, r.Right-1, r.Bottom-1);// bottom inner edge
        Canvas.SetColor(clShadow2);
        Canvas.DrawLine(r.Right-1, r.Bottom-1, r.Right, r.Bottom-2);  // right rounded edge (1px)
        Canvas.DrawLine(r.Right, r.Bottom-2, r.Right, r.Top-1);       // right outer edge
        if Mode = 2 then  { selected tab }
        begin
          Canvas.SetColor(ActiveTabColor);
          Canvas.DrawLine(r.Left+1, r.Top-1, r.Right-1, r.Top-1);
        end;
      end;

    tpLeft:
      begin
        if Mode = 2 then  { selected tab }
        begin
          r.Width  := r.Width - 1;
          r.Height := r.Height + 2;
        end;
        with Canvas do
        begin
          FillRectangle(r.Left+1, r.Top+1, r.Width-2, r.Height-3);
          SetColor(clHilite2);
          DrawLine(r.Left, r.Bottom-2, r.Left, r.Top+2);
          DrawLine(r.Left, r.Top+2, r.Left+2, r.Top);
          DrawLine(r.Left+2, r.Top, r.Right-1, r.Top);
          SetColor(clShadow1);
          DrawLine(r.Left+2, r.Bottom-1, r.Right-1, r.Bottom-1);
          SetColor(clShadow2);
          DrawLine(r.Left+1, r.Bottom-1, r.Left+3, r.Bottom);
          DrawLine(r.Left+2, r.Bottom, r.Right, r.Bottom);
        end;
      end;

    tpRight:
      begin
        if Mode = 2 then
        begin
          r.Height := r.Height + 2;
        end;
        with Canvas do
        begin
          FillRectangle(r.Left+1, r.Top+1, r.Width-2, r.Height-3);
          SetColor(clHilite2);
          DrawLine(r.Left+1, r.Top, r.Right-2, r.Top);
          SetColor(clShadow1);
          DrawLine(r.Right-2,r.Top,r.Right-1,r.Top+1);
          DrawLine(r.Left+2, r.Bottom-1, r.Right-2, r.Bottom-1);
          DrawLine(r.Right-3, r.Bottom-1, r.Right-1, r.Bottom-3);
          DrawLine(r.Right-1, r.Bottom-3, r.Right-1, r.Top);
          SetColor(clShadow2);
          DrawLine(r.Left+2,r.Bottom,r.Right-3, r.Bottom);
          DrawLine(r.Right-3, r.Bottom, r.Right, r.Bottom-3);
          DrawLine(r.Right, r.Top+2, r.Right, r.Bottom-2);
        end;
      end;
  end;  { case }
end;

procedure TfpgPageControl.pmCloseTab(Sender: TObject);
var
  ts: TfpgTabSheet;
begin
  ts := TabSheetAtPos(FLastRClickPos.x, FLastRClickPos.y);
  if not Assigned(ts) then
    ts := ActivePage;
  if ts = nil then
    exit;
  RemovePage(ts);
  DoTabSheetClosing(ts);
  ts.Free;
end;

function TfpgPageControl.GetActiveTabColor: TfpgColor;
begin
  Result := FActiveTabColor;
end;

procedure TfpgPageControl.SetActiveTabColor(AValue: TfpgColor);
begin
  if FActiveTabColor <> AValue then
  begin
    FActiveTabColor := AValue;
    RePaint;
  end;
end;

procedure TfpgPageControl.SetBackgroundColor(const AValue: TfpgColor);
var
  lWasMatch: boolean;
begin
  lWasMatch := FBackgroundColor = FActiveTabColor;
  inherited SetBackgroundColor(AValue);
  if lWasMatch then
    ActiveTabColor := FBackgroundColor;
end;

procedure TfpgPageControl.OrderSheets;
begin
  FPages.Sort(@SortCompare);
  FActivePageIndex := FPages.IndexOf(ActivePage);
end;

procedure TfpgPageControl.RePaintTitles;
const
  TAB_HEIGHT = 21;
var
  TabW, TabH: Integer;
  r2: TfpgRect;
  r3: TfpgRect;
  h: TfpgTabSheet;
  lp: integer;
  toffset: integer;
  TextLeft, TextTop: Integer;
  dx: integer;
  lTxtFlags: TfpgTextFlags;
  ActivePageVisible: Boolean;
begin
  if not HasHandle then
    Exit; //==>
    
  if PageCount = 0 then
    Exit; //==>

  TabW:=FixedTabWidth;
  TabH:=FixedTabHeight;
  ActivePageVisible := false;
  If TabH = 0 then
    TabH := TAB_HEIGHT;
  h := TfpgTabSheet(FPages.First);
  if h = nil then
    Exit; //==>
  
  Canvas.SetTextColor(TextColor);
  lTxtFlags := [];
  if not Enabled then
    Include(lTxtFlags, txtDisabled);


  if TabPosition in [tpTop, tpBottom] then
  begin
    if MaxButtonWidthSum > (Width-(FMargin*2)) then
    begin
      if FFirstTabButton = nil then
        FFirstTabButton := h
      else
        h := FFirstTabButton;
      if TabPosition = tpTop then
      begin
        FLeftButton.SetPosition(Width - (FRightButton.Width * 2), FMargin, FRightButton.Height, FRightButton.Height);
        FRightButton.SetPosition(Width - FRightButton.Width, FMargin, FRightButton.Height, FRightButton.Height);
      end
      else
      begin
        FLeftButton.SetPosition(Width - (FRightButton.Width * 2), Height - ButtonHeight - FMargin, FRightButton.Height, FRightButton.Height);
        FRightButton.SetPosition(Width - FRightButton.Width, Height - ButtonHeight - FMargin, FRightButton.Height, FRightButton.Height);
      end;
      FLeftButton.Visible   := True;
      FRightButton.Visible  := True;
    end
    else
    begin
      FLeftButton.Visible   := False;
      FRightButton.Visible  := False;
    end;
  end;

  if TabPosition in [tpLeft, tpRight] then
  begin
    if MaxButtonHeightSum > (Height-(FMargin*2)) then
    begin
      if FFirstTabButton = nil then
        FFirstTabButton := h
      else
        h := FFirstTabButton;
      if TabPosition = tpLeft then
      begin
        FLeftButton.SetPosition(MaxButtonWidth - (FRightButton.Width * 2), Height - ButtonHeight - FMargin, FRightButton.Height, FRightButton.Height);
        FRightButton.SetPosition(MaxButtonWidth - FRightButton.Width, Height - ButtonHeight - FMargin, FRightButton.Height, FRightButton.Height);
      end
      else
      begin
        FLeftButton.SetPosition(Width - MaxButtonWidth, Height - ButtonHeight - FMargin, FRightButton.Height, FRightButton.Height);
        FRightButton.SetPosition(Width - MaxButtonWidth + FRightButton.Width, Height - ButtonHeight - FMargin, FRightButton.Height, FRightButton.Height);
      end;
      FLeftButton.Visible   := True;
      FRightButton.Visible  := True;
    end
    else
    begin
      FLeftButton.Visible   := False;
      FRightButton.Visible  := False;
    end;
  end;

  case TabPosition of
    tpNone:
      begin
        while h <> nil do
        begin
          if h <> ActivePage then
            h.Visible:=false
          else
            h.Visible:=True;
          h.SetPosition(FMargin+2, FMargin+2 , Width - (FMargin*2) - 4, Height - ((FMargin+2)*2));
          if h <> TfpgTabSheet(FPages.Last) then
            h := TfpgTabSheet(FPages[FPages.IndexOf(h)+1])
          else
            h := nil;
        end;
        r2.Left    := 0;
        r2.Top     := 0;
        r2.Width   := Width;
        r2.Height  := Height;
        Canvas.DrawButtonFace(r2, []);
      end;

    tpBottom:
      begin
        lTxtFlags += TextFlagsDflt;
        lp := 0;
        r2.SetRect(2, Height - ButtonHeight, 50, TabH-2);
        while h <> nil do
        begin
          if h <> ActivePage then
          begin
            toffset := 1;
            h.Visible := False;
          end
          else
          begin
            toffset := 2;
            h.Visible := True;
            h.SetPosition(FMargin+2, FMargin+2 , Width - (FMargin*2) - 4, Height - TabH - (FMargin+2)*2);
          end;
          // paint tab button
          r2.Width := ButtonWidth(h.Text);
          r3 := DrawTab(r2, h = ActivePage);
          // paint text on non-active tabs
          if h <> ActivePage then
            Canvas.DrawText(lp + (ButtonWidth(h.Text) div 2) - FFont.TextWidth(GetTabText(h.Text)) div 2,
                Height-TabH+toffset, GetTabText(h.Text), lTxtFlags);

          r2.Left := r2.Left + r2.Width;
          lp := lp + ButtonWidth(h.Text);
          if h <> TfpgTabSheet(FPages.Last) then
            h := TfpgTabSheet(FPages[FPages.IndexOf(h)+1])
          else
            h := nil;
        end;
        // Draw Page Control body rectangle (client area)
        r2.Left    := 0;
        r2.Top     := 0;
        r2.Width   := Width;
        r2.Height  := Height - TabH;
        Canvas.DrawButtonFace(r2, []);
        // Draw text of ActivePage, because we didn't before.
        DrawTab(r3, false, 2);
        Canvas.DrawText(r3.Left+4, r3.Top+5, r3.Width, r3.Height, ActivePage.Text, lTxtFlags);
      end;

    tpTop:
      begin
        lTxtFlags += TextFlagsDflt;
        lp := 0;
        r2.SetRect(2, 2, 50, TabH);
        while h <> nil do
        begin
          if h <> ActivePage then
          begin
            toffset := 4;
            h.Visible := False;
          end
          else
          begin
            toffset := 2;
            h.Visible := True;
            h.SetPosition(FMargin+2, FMargin+2 + r2.Height, Width - (FMargin*2) - 4, Height - r2.Height - ((FMargin+2)*2));
          end;
          // paint tab button
          r2.Width := ButtonWidth(h.Text);
          r3 := DrawTab(r2, h = ActivePage);
          // paint text on non-active tabs
          if h <> ActivePage then
            Canvas.DrawText(lp + (ButtonWidth(h.Text) div 2) - FFont.TextWidth(GetTabText(h.Text)) div 2,
                FMargin+toffset, GetTabText(h.Text), lTxtFlags);
          r2.Left := r2.Left + r2.Width;
          lp := lp + ButtonWidth(h.Text);
          if h <> TfpgTabSheet(FPages.Last) then
            h := TfpgTabSheet(FPages[FPages.IndexOf(h)+1])
          else
            h := nil;
        end;
        // Draw Page Control body rectangle (client area)
        r2.Left    := 0;
        r2.Top     := r2.Top + r2.Height-2;
        r2.Width   := Width;
        r2.Height  := Height - r2.Height;
        Canvas.DrawButtonFace(r2, []);

        // Draw text of ActivePage, because we didn't before.
        DrawTab(r3, false, 2);
        Canvas.DrawText(r3.Left+4, r3.Top+3, r3.Width, r3.Height, ActivePage.Text, lTxtFlags);
      end;

    tpRight:
      begin
        lTxtFlags += [txtVCenter, txtLeft];
        lp := 0;
        TabW := MaxButtonWidth;
        r2.SetRect(Width - 2 - TabW, 2, TabW, TabH);
        while h <> nil do
        begin
          if h <> ActivePage then
          begin
            toffset := 4;
            h.Visible := False;
          end
          else
          begin
            toffset := 2;
            h.Visible := True;
            { set tab content page (client area) size }
            h.SetPosition(FMargin+2, FMargin+2, Width - ((FMargin+2)*2) - TabW, Height - ((FMargin+2)*2));
          end;
          // paint tab button
          r3 := DrawTab(r2, h = ActivePage);

          // paint text on non-active tabs
          if h <> ActivePage then
            Canvas.DrawText(r2.left+toffset, r2.Top, r2.Width, r2.Height, GetTabText(h.Text), lTxtFlags);
          r2.Top += r2.Height;
          lp := r2.Top;
          if h <> TfpgTabSheet(FPages.Last) then
            h := TfpgTabSheet(FPages[FPages.IndexOf(h)+1])
          else
            h := nil;
        end;
        // Draw Page Control body rectangle (client area)
        r2.Left    := 0;
        r2.Top     := 0;
        r2.Width   := Width - TabW;
        r2.Height  := Height;
        Canvas.DrawButtonFace(r2, []);

        // Draw text of ActivePage, because we didn't before.
        DrawTab(r3, false, 2);
        Canvas.DrawText(r3.left+toffset, r3.Top, r3.Width, r3.Height, ActivePage.Text, lTxtFlags);
      end;

    tpLeft:
      begin
        lTxtFlags += [txtVCenter, txtLeft];
        lp := 0;
        TabW := MaxButtonWidth;
        r2.SetRect(2, 2, TabW, TabH);
        while h <> nil do
        begin
          if h <> ActivePage then
          begin
            toffset := 4;
            h.Visible := False;
          end
          else
          begin
            toffset := 2;
            h.Visible := True;
            { set tab content page (client area) size }
            h.SetPosition(FMargin+2+TabW, FMargin+2, Width - ((FMargin+2)*2) - TabW, Height - ((FMargin+2)*2));
          end;
          // paint tab button
          r3 := DrawTab(r2, h = ActivePage);

          // paint text on non-active tabs
          if h <> ActivePage then
            Canvas.DrawText(r2.left+toffset, r2.Top, r2.Width, r2.Height, GetTabText(h.Text), lTxtFlags);
          r2.Top += r2.Height;
          lp := r2.Top;
          if h <> TfpgTabSheet(FPages.Last) then
            h := TfpgTabSheet(FPages[FPages.IndexOf(h)+1])
          else
            h := nil;
        end;
        // Draw Page Control body rectangle (client area)
        r2.Left    := TabW;
        r2.Top     := 0;
        r2.Width   := Width - TabW;
        r2.Height  := Height;
        Canvas.DrawButtonFace(r2, []);

        // Draw text of ActivePage, because we didn't before.
        DrawTab(r3, false, 2);
        Canvas.DrawText(r3.left+toffset, r3.Top, r3.Width, r3.Height, ActivePage.Text, lTxtFlags);
      end;
  end; { case }

end;

procedure TfpgPageControl.HandlePaint;
begin
  if SortPages then
    OrderSheets;
  Canvas.ClearClipRect;
  Canvas.Clear(FBackgroundColor);
  
  // To make it more visible in the UI Designer
  if csDesigning in ComponentState then
  begin
    Canvas.SetColor(clInactiveWgFrame);
    Canvas.DrawRectangle(0, 0, Width, Height);
    if PageCount = 0 then
    begin
      Canvas.SetTextColor(clText1);
      Canvas.DrawString(2, 2, Name + ': ' + Classname);
    end;
  end;
  RePaintTitles;
end;

procedure TfpgPageControl.HandleShow;
begin
  inherited HandleShow;
  FLeftButton.Visible := False;
  FRightButton.Visible := False;
end;

procedure TfpgPageControl.HandleLMouseUp(x, y: integer; shiftstate: TShiftState);
var
  ts: TfpgTabSheet;
begin
//  debugln('>> TfpgPageControl.HandleLMouseUp');
  ts := TfpgTabSheet(FPages.First);
  if ts = nil then
    exit; //==>  { This means there are no tabs }
  
  ts := TabSheetAtPos(x, y);
  
  if Assigned(ts) then
    ActivePage := ts;

  inherited HandleLMouseUp(x, y, shiftstate);
end;

procedure TfpgPageControl.HandleRMouseUp(x, y: integer; shiftstate: TShiftState);
var
  ts: TfpgTabSheet;
  s: TfpgString;
begin
  inherited HandleRMouseUp(x, y, shiftstate);

  { store the position for later usage }
  FLastRClickPos := fpgPoint(x,y);

  if to_PMenuClose in FTabOptions then
  begin
    ts := TabSheetAtPos(x, y);
    {$NOTE TODO: This text needs to become a resource string }
    if Assigned(ts) then
      s := Format('Close "%s" Tab', [ts.Text])
    else
      s := 'Close Tab';
      
    if not Assigned(FPopupMenu) then
    begin
      FPopupMenu := TfpgPopupMenu.Create(self);
      FPopupMenu.AddMenuItem(s, '', @pmCloseTab);
    end
    else
    begin
      FPopupMenu.MenuItem(0).Text := s;    { This is dangerous but works for now }
    end;
    FPopupMenu.ShowAt(self, x, y);
  end;
end;

procedure TfpgPageControl.HandleKeyPress(var keycode: word;
  var shiftstate: TShiftState; var consumed: boolean);
var
  i: integer;
begin
  i := ActivePageIndex;
  if ssAlt in shiftstate then
  case keycode of
    keyLeft:
        begin
          if ActivePage <> TfpgTabSheet(FPages.First) then
          begin
            ActivePage := TfpgTabSheet(FPages[i-1]);
            consumed := True;
          end;
        end;

    keyRight:
        begin
          if ActivePage <> TfpgTabSheet(FPages.Last) then
          begin
            ActivePage := TfpgTabSheet(FPages[i+1]);
            consumed := True;
          end;
        end;
  end;  { case/else }
  if not consumed then
    inherited HandleKeyPress(keycode, shiftstate, consumed);
end;

procedure TfpgPageControl.RePaint;
begin
  if FUpdateCount > 0 then
    Exit;
  inherited RePaint;
end;

constructor TfpgPageControl.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FFont   := fpgStyle.DefaultFont;
  FPages  := TList.Create;
  Width   := 150;
  Height  := 100;
  FIsContainer := True;
  FTabOptions  := [];
  FActivePageIndex := 0;

  FTextColor        := Parent.TextColor;
  FBackgroundColor  := Parent.BackgroundColor;
  FActiveTabColor   := FBackgroundColor;
  FFocusable        := True;
  FOnChange         := nil;
  FFixedTabWidth    := 0;
  FFixedTabHeight   := 21;
  FFirstTabButton   := nil;
  FStyle            := tsTabs;
  FTabPosition      := tpTop;
  FMargin           := 1;
  FSortPages        := False;

  FLeftButton := TfpgButton.Create(self);
  FLeftButton.Text      := '<';
  FLeftButton.Height    := 20;
  FLeftButton.Width     := 20;
  FLeftButton.OnClick   := @LeftButtonClick;

  FRightButton := TfpgButton.Create(self);
  FRightButton.Text     := '>';
  FRightButton.Height   := 20;
  FRightButton.Width    := 20;
  FRightButton.OnClick  := @RightButtonClick;
end;

destructor TfpgPageControl.Destroy;
var i: integer;
begin
  FOnChange := nil;
  for i:=0 to FPages.Count-1 do
    TfpgTabSheet(FPages[i]).PageControl:=nil;
  FPages.Free;
  ActiveWidget := nil;
  FFirstTabButton := nil;
  inherited Destroy;
end;

procedure TfpgPageControl.BeginUpdate;
begin
  Inc(FUpdateCount);
end;

procedure TfpgPageControl.EndUpdate;
begin
  Dec(FUpdateCount);
  if FUpdateCount <= 0 then
    RePaint;
end;

function TfpgPageControl.TabSheetAtPos(const x, y: integer): TfpgTabSheet;
var
  h: TfpgTabSheet;
  lp: integer;  // left position
  bw: integer;  // button width
  bh: integer;  // button height
  p1, p2: integer;    // tab boundaries for mouse click to take affect
begin
  Result := nil;
  h := TfpgTabSheet(FPages.First);

  lp := FMargin;
  if MaxButtonWidthSum > (Width-(FMargin*2)) then
    h := FFirstTabButton;

  case TabPosition of
    tpTop:
      begin
        p1 := FMargin;
        p2 := ButtonHeight;
      end;

    tpBottom:
      begin
        p1 := Height - FMargin - ButtonHeight;
        p2 := Height - FMargin;
      end;

    tpRight:
      begin
        p1 := Width - MaxButtonWidth;
        p2 := Width;
      end;

    tpLeft:
      begin
        p1 := FMargin;
        p2 := FMargin + MaxButtonWidth;
      end;
  end;

  if TabPosition in [tpTop, tpBottom] then
  begin
    if (y > p1) and (y < p2) then
    begin
       while h <> nil do
       begin
          bw := ButtonWidth(h.Text);  // initialize button width
          if (x > lp) and (x < lp + bw) then
          begin
            if h <> ActivePage then
              Result := h;
            exit;
          end;  { if }
          lp := lp + bw;
          if h <> TfpgTabSheet(FPages.Last) then
            h := TfpgTabSheet(FPages[FPages.IndexOf(h)+1])
          else
            h := nil;
       end;  { while }
    end;  { if }
  end;

  if TabPosition in [tpLeft, tpRight] then
  begin
    if (x > p1) and (x < p2) then
    begin
      while h <> nil do
      begin
        bh := ButtonHeight;  // initialize button height
        if (y > lp) and (y < lp + bh) then
        begin
          if h <> ActivePage then
            Result := h;
          exit;
        end;  { if }
        lp := lp + bh;
        if h <> TfpgTabSheet(FPages.Last) then
          h := TfpgTabSheet(FPages[FPages.IndexOf(h)+1])
        else
          h := nil;
      end;  { while }
    end;  { if }
  end;
end;

function TfpgPageControl.AppendTabSheet(ATitle: string): TfpgTabSheet;
begin
  Result := TfpgTabSheet.Create(self);
  Result.Text := ATitle;
  InsertPage(Result);
end;

procedure TfpgPageControl.RemoveTabSheet(ATabSheet: TfpgTabSheet);
begin
  RemovePage(ATabSheet);
end;

end.