File: TabHeader.pas

package info (click to toggle)
mysql-query-browser 1.1.6-1sarge0
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 36,320 kB
  • ctags: 24,680
  • sloc: pascal: 203,479; xml: 136,561; ansic: 47,502; cpp: 28,926; sh: 12,433; objc: 4,823; java: 1,849; php: 1,485; python: 1,225; sql: 1,128; makefile: 872
file content (1194 lines) | stat: -rw-r--r-- 34,906 bytes parent folder | download
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
// Copyright (C) 2003, 2004 MySQL AB
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// 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.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

unit TabHeader;

interface

uses
  gnugettext, Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, ExtCtrls, TntClasses, TntExtCtrls, TntForms,
  PNGImage, AuxFuncs, Contnrs, tntmenus, VirtualTrees, Menus;

type
  TPageChangeEvent = procedure(Sender: TObject; PreviousSelectedTab: Integer; PreviousSelectedObj: TObject;
    NewSelectedTab: Integer; Obj: TObject) of object;

  TTab = class;

  TExtTntPaintBox = class;

  TTabHeaderFrame = class(TTntFrame)
    TabHeaderPopupMenu: TTntPopupMenu;
    CloseTabsheetMI: TTntMenuItem;
    N1: TTntMenuItem;
    RenameTabsheetMI: TTntMenuItem;
    procedure TntFrameResize(Sender: TObject);
    procedure RenameTabsheetMIClick(Sender: TObject);
    procedure CloseTabsheetMIClick(Sender: TObject);
    procedure TabHeaderPopupMenuPopup(Sender: TObject);
  private
    FPaintBox: TExtTntPaintBox;

    FTabs: TObjectList;
    FTabListBtnShown: Boolean;

    FNewTabPNGImg,
    FListTabPNGImg,
    FTabCloseImage,
    FInlineHelpTabImage,
    FResultSetTabImage,
    FScriptTabImage: TPNGObject;

    FSelectedTab,
    FHighlightedTab,
    FHighlightedButton: Integer;

    FOnBeforePageChange,
    FOnPageChange: TPageChangeEvent;


    FOnRequestNewPage: TNotifyEvent;
    FOnPageDelete: TNotifyEvent;
    FOnBeforePageDelete: TCloseQueryEvent;

    FTabsPopupMenu: TTntPopupMenu;

    FPaintBoxBufferedImage: TBitmap;
    FShowButtons,
    FDrawBottom,
    FTopSpacer: Boolean;
    FAutoHideTabHeader: Boolean;
    FShowDeleteButtons: Boolean;

    FClickedTabIndex: Integer;

    procedure SetAutoHideTabHeader(const Value: Boolean);
    procedure SetDrawBottom(const Value: Boolean);
    procedure SetShowButtons(const Value: Boolean);
    procedure SetShowDeleteButtons(const Value: Boolean);
  protected
    procedure ComputeTabs;
    procedure DragOver(Source: TObject; X: Integer; Y: Integer; State: TDragState; var Accept: Boolean); override;
    function GetSelectedTab: Integer;
    function GetTab(index: Integer): TTab;
    function GetTabIndexFromPos(X,Y: Integer): Integer;
    function GetTabIndexFromObject(Obj: TObject): Integer;
    function GetTabIndexFromPanel(TabPanel: TTntPanel): Integer;
    procedure SetSelectedTab(SelectedTabIndex: Integer);
    procedure TabDragDrop(Sender, Source: TObject; X, Y: Integer);
    procedure TabDragOver(Sender, Source: TObject; X, Y: Integer; State: TDragState; var Accept: Boolean);
    procedure TabMenuItemSelected(Sender: TObject);
    procedure TabPaintBoxMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure TabPaintBoxMouseLeave(Sender: TObject);
    procedure TabPaintBoxMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
    procedure TabPaintBoxPaint(Sender: TObject);
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;

    function AddTabSheet(AOwner: TComponent; Caption, ImageName: WideString; TabPanel: TTntPanel; Obj: TObject = nil;
      SelectNewPage: Boolean = True; DockTabPanel: Boolean = True; TabPanelHeight: Integer = -1): Integer;
    procedure ClearTabSheets;
    function DeleteTab(TabIndex: Integer; AllowDeletionOfLastTab: Boolean = False): Boolean;
    function TabCount: Integer;
    procedure Show;
    procedure Hide;
    procedure SelectNextTabSheet;
    function GetTabIndex(Tab: TTab): Integer;

    property AutoHideTabHeader: Boolean read FAutoHideTabHeader write SetAutoHideTabHeader;
    property DrawBottom: Boolean read FDrawBottom write SetDrawBottom;
    property SelectedTab: Integer read GetSelectedTab write SetSelectedTab;
    property ShowButtons: Boolean read FShowButtons write SetShowButtons;
    property ShowDeleteButtons: Boolean read FShowDeleteButtons write SetShowDeleteButtons default True;
    property TabSheets[Index: Integer]: TTab read GetTab;
    property TabIndexFromObject[Obj: TObject]: Integer read GetTabIndexFromObject;
    property TabIndexFromPanel[TabPanel: TTntPanel]: Integer read GetTabIndexFromPanel;

    property OnBeforePageChange: TPageChangeEvent read FOnBeforePageChange write FOnBeforePageChange;
    property OnPageChange: TPageChangeEvent read FOnPageChange write FOnPageChange;
    property OnBeforePageDelete: TCloseQueryEvent read FOnBeforePageDelete write FOnBeforePageDelete;
    property OnPageDelete: TNotifyEvent read FOnPageDelete write FOnPageDelete;
    property OnRequestNewPage: TNotifyEvent read FOnRequestNewPage write FOnRequestNewPage;
  end;

  TTab = class(TObject)
  private
    FImage: TPngObject;
    FBounds: TRect;
  protected
    procedure SetCaption(Caption: Widestring);
  public
    FCaption: WideString;
    FTabHeader: TTabHeaderFrame;

    TabPanel: TTntPanel;
    Obj: TObject;
    DockTabPanel: Boolean;
    RegularTextWidth: Integer;
    BoldTextWidth: Integer;

    constructor Create(AOwner: TComponent; TabHeader: TTabHeaderFrame;
      Caption, ImageName: WideString; TabPanel: TTntPanel; Obj: TObject;
      DockTabPanel: Boolean); reintroduce;
    destructor Destroy; override;

    property Caption: WideString read FCaption write SetCaption;
  end;

  TExtTntPaintBox = class(TTntPaintBox)
    procedure CMMouseLeave(var Message: TMessage); message CM_MOUSELEAVE;
  private
    FOnMouseLeave: TNotifyEvent;
  published
    property OnMouseLeave: TNotifyEvent read FOnMouseLeave write FOnMouseLeave;
  end;

//----------------------------------------------------------------------------------------------------------------------

implementation

{$R *.dfm}

uses
  Options, ColorTypes, ColorTools, CommonTypes, PNGTools;

const
  SeparatorWidth = 4;         // The space between the tab text and the images in a tab.
  TabBorderColor = $009C9B91; // The color of the tab border.
  BottomSpace    = 4;         // The space below the tabs.
  Tabheight      = 30;        // Height of the tab area.

//----------------------------------------------------------------------------------------------------------------------

procedure TExtTntPaintBox.CMMouseLeave(var Message: TMessage);

begin
  if (Assigned(FOnMouseLeave)) then
    FOnMouseLeave(self);
end;

//----------------------------------------------------------------------------------------------------------------------

constructor TTabHeaderFrame.Create(AOwner: TComponent);

begin
  inherited Create(AOwner);

  InitFrame(self);

  ControlStyle := ControlStyle + [csOpaque];

  // Set font according to user settings.
  Font.Name := MYXCommonOptions.DefaultFontName;
  Font.Height := MYXCommonOptions.DefaultFontHeight;

  FOnPageChange := nil;
  FOnBeforePageChange := nil;

  FPaintBox := TExtTntPaintBox.Create(self);
  FPaintBox.Parent := self;
  FPaintBox.Align := alClient;
  FPaintBox.OnMouseDown := TabPaintBoxMouseDown;
  FPaintBox.OnMouseMove := TabPaintBoxMouseMove;
  FPaintBox.OnMouseLeave := TabPaintBoxMouseLeave;
  FPaintBox.OnPaint := TabPaintBoxPaint;
  FPaintBox.OnDragOver := TabDragOver;
  FPaintBox.OnDragDrop := TabDragDrop;

  FPaintBoxBufferedImage := nil;

  FShowButtons := False;
  FShowDeleteButtons := True;
  DrawBottom := False;
  FTopSpacer := False;
  FSelectedTab := -1;
  FHighlightedTab := -1;
  FHighlightedButton := -1;
  FAutoHideTabHeader := False;
  FClickedTabIndex := -1;

  FTabListBtnShown := False;

  FTabsPopupMenu := TTntPopupMenu.Create(nil);

  FNewTabPNGImg := LoadPNGImageFromResource('tab_new');
  FListTabPNGImg := LoadPNGImageFromResource('tab_list');
  FTabCloseImage := LoadPNGImageFromResource('tabsheet_icon_close2');
  FInlineHelpTabImage := LoadPNGImageFromResource('tabsheet_icon_close2');
  FResultSetTabImage := LoadPNGImageFromResource('tabsheet_icon_close2');
  FScriptTabImage := LoadPNGImageFromResource('tabsheet_icon_close2');

  FTabs := TObjectList.Create;
end;

//----------------------------------------------------------------------------------------------------------------------

destructor TTabHeaderFrame.Destroy;

begin
  FShowButtons := False;

  if (FPaintBoxBufferedImage <> nil) then
    FPaintBoxBufferedImage.Free;

  FTabs.Free;

  FNewTabPNGImg.Free;
  FListTabPNGImg.Free;
  FTabCloseImage.Free;
  FInlineHelpTabImage.Free;
  FResultSetTabImage.Free;
  FScriptTabImage.Free;

  FTabsPopupMenu.Free;

  inherited;
end;

//----------------------------------------------------------------------------------------------------------------------

procedure TTabHeaderFrame.SetAutoHideTabHeader(const Value: Boolean);

begin
  FAutoHideTabHeader := Value;
  Invalidate;
end;

//----------------------------------------------------------------------------------------------------------------------

procedure TTabHeaderFrame.SetDrawBottom(const Value: Boolean);

begin
  FDrawBottom := Value;
  Invalidate;
end;

//----------------------------------------------------------------------------------------------------------------------

procedure TTabHeaderFrame.SetShowButtons(const Value: Boolean);

begin
  FShowButtons := Value;
  Invalidate;
end;

//----------------------------------------------------------------------------------------------------------------------

procedure TTabHeaderFrame.SetShowDeleteButtons(const Value: Boolean);

begin
  if FShowDeleteButtons <> Value then
  begin
    FShowDeleteButtons := Value;
    ComputeTabs;
    Invalidate;
  end;
end;

//----------------------------------------------------------------------------------------------------------------------

procedure TTabHeaderFrame.ComputeTabs;

var
  I: Integer;
  DrawFlags: Cardinal;
  R, CaptionRect: TRect;
  Tab: TTab;
  TopSpace: Integer;

begin
  FPaintBox.Canvas.Font.Assign(Font);
  // Compute space for bold font style to have enough room.
  FPaintBox.Canvas.Font.Style := [fsBold];

  if FTopSpacer then
    TopSpace := 4
  else
    TopSpace := 0;

  DrawFlags := DT_CALCRECT or DT_SINGLELINE;
  CaptionRect := Rect(0, 0, 0, 0);

  R := Rect(SeparatorWidth, TopSpace, 0, Tabheight - 7 - BottomSpace + TopSpace);
  if FShowButtons then
    Inc(R.Left, FNewTabPNGImg.Width);

  for I := 0 to FTabs.Count - 1 do
  begin
    Tab := TTab(FTabs[I]);

    // Compute regular text width.
    FPaintBox.Canvas.Font.Style := [];
    Windows.DrawTextW(FPaintBox.Canvas.Handle, PWideChar(Tab.Caption), Length(Tab.Caption), CaptionRect, DrawFlags);

    TTab(FTabs[I]).RegularTextWidth := CaptionRect.Right;

    // Compute bold text width.
    FPaintBox.Canvas.Font.Style := [fsBold];

    // Compute overall width of the tab.
    if IsWinNTPlatform then
      Windows.DrawTextW(FPaintBox.Canvas.Handle, PWideChar(Tab.Caption), Length(Tab.Caption), CaptionRect, DrawFlags)
    else
      DrawTextW(FPaintBox.Canvas.Handle, PWideChar(Tab.Caption), Length(Tab.Caption), CaptionRect, DrawFlags, True);

    TTab(FTabs[I]).BoldTextWidth := CaptionRect.Right;

    R.Right := R.Left + 2 * SeparatorWidth + CaptionRect.Right;
    if FShowDeleteButtons and (FTabCloseImage <> nil) then
      Inc(R.Right, FTabCloseImage.Width + SeparatorWidth);
    if Tab.FImage <> nil then
      Inc(R.Right, Tab.FImage.Width + SeparatorWidth);

    Tab.FBounds := R;
    R.Left := R.Right;
  end;
end;

//----------------------------------------------------------------------------------------------------------------------

procedure TTabHeaderFrame.DragOver(Source: TObject; X, Y: Integer; State: TDragState; var Accept: Boolean);

var
  SchemaInfo: ISchemaInfo;

begin
  inherited;

  if Accept then
  begin
    TComponent(Source).Owner.GetInterface(ISchemaInfo, SchemaInfo);
    Accept := Assigned(SchemaInfo);
  end;
end;

//----------------------------------------------------------------------------------------------------------------------

function TTabHeaderFrame.GetSelectedTab: Integer;

begin
  Result := FSelectedTab;
end;

//----------------------------------------------------------------------------------------------------------------------

function TTabHeaderFrame.GetTab(index: Integer): TTab;

begin
  if (index < FTabs.Count) then
    Result := TTab(FTabs[index])
  else
    Result := nil;
end;

//----------------------------------------------------------------------------------------------------------------------

function TTabHeaderFrame.GetTabIndexFromPos(X, Y: Integer): Integer;

var
  I: Integer;
  Tab: TTab;
  
begin
  Result := -1;
  for I := 0 to FTabs.Count - 1 do
  begin
    Tab := TTab(FTabs[I]);
    if (X >= Tab.FBounds.Left + SeparatorWidth) and
      (X < Tab.FBounds.Right - SeparatorWidth) then
    begin
      Result := I;
      Break;
    end;
  end;
end;

//----------------------------------------------------------------------------------------------------------------------

function TTabHeaderFrame.GetTabIndexFromObject(Obj: TObject): Integer;

var
  I: Integer;

begin
  Result := -1;

  for I:=0 to FTabs.Count-1 do
    if(TTab(FTabs[i]).Obj=Obj)then
    begin
      Result := I;
      break;
    end;
end;

//----------------------------------------------------------------------------------------------------------------------

function TTabHeaderFrame.GetTabIndexFromPanel(TabPanel: TTntPanel): Integer;

var
  I: Integer;

begin
  Result := -1;

  for I:=0 to FTabs.Count-1 do
    if(TTab(FTabs[i]).TabPanel=TabPanel)then
    begin
      Result := I;
      break;
    end;
end;

//----------------------------------------------------------------------------------------------------------------------

procedure TTabHeaderFrame.SetSelectedTab(SelectedTabIndex: Integer);

var
  prevSelectedTab: Integer;
  prevSelectedObj: TObject;

begin
  if SelectedTabIndex=-1 then
    Exit;

  if SelectedTabIndex > FTabs.Count - 1 then
    SelectedTabIndex := FTabs.Count - 1;
  if SelectedTabIndex <> FSelectedTab then
  begin
    prevSelectedTab := FSelectedTab;
    if (prevSelectedTab>=0) and (prevSelectedTab<FTabs.Count) then
      prevSelectedObj := TTab(FTabs[FSelectedTab]).Obj
    else
      prevSelectedObj := nil;

    // Call event if assigned.
    if (Assigned(FOnBeforePageChange)) then
    begin
      if prevSelectedTab > -1 then
        FOnBeforePageChange(self,
          prevSelectedTab, prevSelectedObj,
          SelectedTabIndex, TTab(FTabs[prevSelectedTab]).Obj)
      else
        FOnBeforePageChange(self,
          prevSelectedTab, prevSelectedObj,
          SelectedTabIndex, nil);
    end;

    if (FSelectedTab > -1) and (FSelectedTab < FTabs.Count) then
      if (Assigned(TTab(FTabs[FSelectedTab]).TabPanel)) then
        TTab(FTabs[FSelectedTab]).TabPanel.Hide;

    FSelectedTab := SelectedTabIndex;

    if (FSelectedTab > -1) and (FSelectedTab < FTabs.Count) then
      if (Assigned(TTab(FTabs[FSelectedTab]).TabPanel)) then
      begin
        TTab(FTabs[FSelectedTab]).TabPanel.Show;
        TTab(FTabs[FSelectedTab]).TabPanel.BringToFront;
      end;

    // Call event if assigned.
    if (Assigned(FOnPageChange)) then
      FOnPageChange(self,
        prevSelectedTab, prevSelectedObj,
        FSelectedTab, TTab(FTabs[FSelectedTab]).Obj);

    ComputeTabs;
    Invalidate;
  end;
end;

//----------------------------------------------------------------------------------------------------------------------

procedure TTabHeaderFrame.TabDragDrop(Sender, Source: TObject; X, Y: Integer);

begin
  SelectedTab := GetTabIndexFromPos(X, Y);
  if Assigned(OnDragDrop) then
    OnDragDrop(Sender, Source, X, Y);
end;

//----------------------------------------------------------------------------------------------------------------------

procedure TTabHeaderFrame.TabDragOver(Sender, Source: TObject; X, Y: Integer; State: TDragState; var Accept: Boolean);

begin
  FHighlightedTab := GetTabIndexFromPos(X, Y);
  Invalidate;
  Update;

  if Assigned(OnDragOver) then
    OnDragOver(Sender, Source, X, Y, State, Accept);
end;

//----------------------------------------------------------------------------------------------------------------------

procedure TTabHeaderFrame.TabMenuItemSelected(Sender: TObject);

begin
  SelectedTab := TTntMenuItem(Sender).Tag;
end;

//----------------------------------------------------------------------------------------------------------------------

procedure TTabHeaderFrame.TabPaintBoxMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y:
  Integer);

var
  I: Integer;
  Pnt: TPoint;
  MenuItem: TTntMenuItem;
  PreAreaWidth: Integer;
  ActiveHeight: Integer;
  Tab: TTab;
  TopSpace: Integer;

begin
  PreAreaWidth := SeparatorWidth;
  if FShowButtons then
    Inc(PreAreaWidth, FNewTabPNGImg.Width);

  if FTopSpacer then
    TopSpace := 4
  else
    TopSpace := 0;

  FClickedTabIndex := -1;

  ActiveHeight := Tabheight - 7 - BottomSpace + TopSpace;

  if (Y < ActiveHeight) then
  begin
    // User clicked on New Tab icon
    if (X < PreAreaWidth) and FShowButtons then
    begin
      if (Assigned(FOnRequestNewPage)) then
        FOnRequestNewPage(self);
    end
    else
      // User clicked on Tabsheet
      if (X < Width - (PreAreaWidth + Ord(FTabListBtnShown) * PreAreaWidth) * Ord(FShowButtons)) then
      begin
        for I := 0 to FTabs.Count - 1 do
        begin
          Tab := TTab(FTabs[I]);
          if (X >= Tab.FBounds.Left + SeparatorWidth) and
            (X < Tab.FBounds.Right - SeparatorWidth) then
          begin
            // We found a tab under mouse.
            if (Button=mbRight) and (FShowButtons) then
            begin
              FClickedTabIndex := I;
              TabHeaderPopupMenu.Popup(Mouse.CursorPos.X, Mouse.CursorPos.Y)
            end
            else
              //See if it is to be closed.
              if FShowDeleteButtons and (FTabCloseImage <> nil) then
              begin
                if X > (Tab.FBounds.Right - 2 * SeparatorWidth - FTabCloseImage.Width) then
                  DeleteTab(I)
                else
                  SelectedTab := I;
              end
              else
                SelectedTab := I;
            Break;
          end;
        end;
      end
      else
        // User clicked on Tab List icon.
        if X >= (Width - PreAreaWidth) then
        begin
          Pnt := TControl(Sender).ClientToScreen(Point(0, 0));

          FTabsPopupMenu.Items.Clear;
          for i := 0 to FTabs.Count - 1 do
          begin
            MenuItem := TTntMenuItem.Create(FTabsPopupMenu);
            MenuItem.Caption := TTab(FTabs[i]).Caption;
            MenuItem.Tag := i;
            MenuItem.OnClick := TabMenuItemSelected;
            FTabsPopupMenu.Items.Add(MenuItem);
          end;

          FTabsPopupMenu.Popup(Pnt.X + Width - 2 * PreAreaWidth + 1, Pnt.Y + 16);
        end;
  end;
end;

//----------------------------------------------------------------------------------------------------------------------

procedure TTabHeaderFrame.TabPaintBoxMouseLeave(Sender: TObject);

begin
  FHighlightedTab := -1;
  FHighlightedButton := -1;

  Invalidate;
end;

//----------------------------------------------------------------------------------------------------------------------

procedure TTabHeaderFrame.TabPaintBoxMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);

var
  PreAreaWidth: Integer;
  ActiveHeight: Integer;
  TopSpace: Integer;

begin
  FHighlightedButton := -1;
  FHighlightedTab := -1;

  PreAreaWidth := SeparatorWidth;
  if FShowButtons then
    Inc(PreAreaWidth, FNewTabPNGImg.Width);

  if FTopSpacer then
    TopSpace := 4
  else
    TopSpace := 0;

  ActiveHeight := Tabheight - 7 - BottomSpace + TopSpace;

  if (Y < ActiveHeight) then
  begin
    // User clicked on New Tab icon
    if (X < PreAreaWidth) and FShowButtons then
    begin
      FHighlightedButton := 0;
      Invalidate;
    end
    else
      // User clicked on Tabsheet
      if (X < Width - (PreAreaWidth + Ord(FTabListBtnShown) * PreAreaWidth) * Ord(FShowButtons)) then
      begin
        FHighlightedTab := GetTabIndexFromPos(X, Y);
        if FHighlightedTab > -1 then
          Invalidate;
      end
      else
        // User clicked on Tab List icon.
        if (X >= Width - PreAreaWidth) then
        begin
          FHighlightedButton := 2;
          Invalidate;
        end;
  end;
end;

//----------------------------------------------------------------------------------------------------------------------

procedure TTabHeaderFrame.TabPaintBoxPaint(Sender: TObject);

var
  I,
    TopSpace: Integer;

  CurrentLeft, Y: Integer;
  Tab: TTab;
  CaptionRect: TRect;
  DrawFlags: Cardinal;
  DrawListButton: Boolean;
  Bottom: Integer;
  TempColor: TRGB;

begin
  if (FPaintBoxBufferedImage = nil) then
    FPaintBoxBufferedImage := TBitmap.Create;

  FPaintBoxBufferedImage.Width := FPaintBox.Width;
  FPaintBoxBufferedImage.Height := FPaintBox.Height;

  with FPaintBoxBufferedImage.Canvas do
  begin
    Brush.Style := bsSolid;

    if FTopSpacer then
      TopSpace := 4
    else
      TopSpace := 0;

    Font.Assign(Self.Font);

    // Initialize background. Since we are double-buffering it is more efficient to erase the whole background in
    // one go instead in several steps.
    Brush.Color := clBtnFace;
    FillRect(ClientRect);

    // Bottom white line
    Brush.Color := clWhite;
    FillRect(Rect(1, 23 - 4 + TopSpace, Width - 1, Height));

    CurrentLeft := SeparatorWidth;
    Bottom := Tabheight - 7 - BottomSpace + TopSpace;

    // Left vertical dark border line.
    Pen.Color := TabBorderColor;
    MoveTo(0, Tabheight - BottomSpace + TopSpace);
    LineTo(0, Bottom - 1);

    // Draw Buttons if enabled.
    if FShowButtons then
    begin
      if FHighlightedButton = 0 then
        FNewTabPNGImg.Draw(FPaintBoxBufferedImage.Canvas, Rect(3, 0, 3 + FNewTabPNGImg.Width, FNewTabPNGImg.Height))
      else
        FNewTabPNGImg.DrawFaded(FPaintBoxBufferedImage.Canvas, Rect(3, 0, 3 + FNewTabPNGImg.Width,
          FNewTabPNGImg.Height), 60);

      Inc(CurrentLeft, FNewTabPNGImg.Width);
    end;

    // Draw horizontal line left to the first tab.
    LineTo(CurrentLeft, Bottom - 1);

    // Draw tabulators.
    DrawFlags := DT_SINGLELINE or DT_NOCLIP or DT_VCENTER;
    CaptionRect := Rect(0, 0, 0, 0);
    DrawListButton := False;
    for I := 0 to FTabs.Count - 1 do
    begin
      Tab := TTab(FTabs[I]);

      if Tab.FBounds.Right > (Width - SeparatorWidth) then
      begin
        // Stop painting if we filled up the client area.
        DrawListButton := True;
        Break;
      end;

      if FSelectedTab = I then
      begin
        // This is the active tab.
        Brush.Color := clWhite;
        FillRect(Tab.FBounds);

        with Tab.FBounds do
        begin
          LineTo(Left, Top);
          LineTo(Right, Top);
          LineTo(Right, Bottom - 1);
        end;
      end
      else
      begin
        if FHighlightedTab = I then
        begin
          // This is the hot tab.
          TempColor := MakeRGB(clBtnFace);
          Brush.Color := MakeColorRef(DarkenColor(TempColor, 0.05), 1);
          Pen.Color := Brush.Color;
          with Tab.FBounds do
            RoundRect(Left + 2, Top + 1, Right - 2, Bottom - 2, 3, 3);

          Pen.Color := TabBorderColor;
        end;
        LineTo(Tab.FBounds.Right, Tab.FBounds.Bottom - 1);
      end;

      CurrentLeft := Tab.FBounds.Left + SeparatorWidth;

      if not (FSelectedTab = I) and (Tab.RegularTextWidth <> 0) then
        CurrentLeft := CurrentLeft + (Tab.BoldTextWidth - Tab.RegularTextWidth) div 2;

      if Tab.FImage <> nil then
      begin
        Y := (Tab.FBounds.Bottom - Tab.FBounds.Top - Tab.FImage.Height) div 2;
        if FSelectedTab = I then
          Tab.FImage.Draw(FPaintBoxBufferedImage.Canvas, Rect(CurrentLeft, Y, Tab.FImage.Width, Tab.FImage.Height))
        else
          Tab.FImage.DrawFaded(FPaintBoxBufferedImage.Canvas, Rect(CurrentLeft, Y, Tab.FImage.Width,
            Tab.FImage.Height), 200);

        Inc(CurrentLeft, Tab.FImage.Width + SeparatorWidth);
      end;

      CaptionRect := Tab.FBounds;
      CaptionRect.Left := CurrentLeft;
      if FShowDeleteButtons and (FTabCloseImage <> nil) then
      begin
        Dec(CaptionRect.Right, FTabCloseImage.Width + SeparatorWidth);

        // The close button image is only drawn if this is the hot or the active tab.
        if ((FHighlightedTab = I) or (FSelectedTab = I)) and (FTabs.Count > 1) then
        begin
          Y := (Tab.FBounds.Bottom - Tab.FBounds.Top -
            FTabCloseImage.Height) div 2 + 1;

          if not (FHighlightedTab = I) then
            FTabCloseImage.DrawGrayscale(FPaintBoxBufferedImage.Canvas,
              Rect(CaptionRect.Right, Y,
              FTabCloseImage.Width, FTabCloseImage.Height), 150)
          else
            FTabCloseImage.Draw(FPaintBoxBufferedImage.Canvas,
              Rect(CaptionRect.Right, Y,
              FTabCloseImage.Width, FTabCloseImage.Height));
        end;
      end;

      if FSelectedTab = I then
      begin
        Font.Color := clBlack;
        Font.Style := [fsBold];
      end
      else
      begin
        if FHighlightedTab = I then
          Font.Color := clWhite
        else
          Font.Color := clBtnShadow;
        Font.Style := [];
      end;

      SetBkMode(Handle, TRANSPARENT);
      if IsWinNTPlatform then
        Windows.DrawTextW(Handle, PWideChar(Tab.Caption), Length(Tab.Caption), CaptionRect, DrawFlags)
      else
        DrawTextW(Handle, PWideChar(Tab.Caption), Length(Tab.Caption), CaptionRect, DrawFlags, True);
    end;

    // Draw border line from last tab to the right and then down.
    LineTo(Width - 1, Bottom - 1);
    LineTo(Width - 1, Tabheight - BottomSpace + TopSpace);

    if FShowButtons then
    begin
      if DrawListButton then
      begin
        if (FHighlightedButton = 2) then
          FListTabPNGImg.Draw(FPaintBoxBufferedImage.Canvas,
            Rect(Width - SeparatorWidth - FListTabPNGImg.Width, 0, Width - SeparatorWidth, FListTabPNGImg.Height))
        else
          FListTabPNGImg.DrawFaded(FPaintBoxBufferedImage.Canvas,
            Rect(Width - SeparatorWidth - FListTabPNGImg.Width, 0, Width - SeparatorWidth, FListTabPNGImg.Height), 60);

        FTabListBtnShown := True;
      end
      else
        FTabListBtnShown := False;
    end;

    if FDrawBottom then
    begin
      MoveTo(Width - 1, TabHeight - 4 + TopSpace);
      LineTo(Width - 1, Height - 1);
      LineTo(0, Height - 1);
      LineTo(0, TabHeight - 4 + TopSpace);
    end;
  end;

  FPaintBox.Canvas.Draw(0, 0, FPaintBoxBufferedImage);
end;

//----------------------------------------------------------------------------------------------------------------------

function TTabHeaderFrame.DeleteTab(TabIndex: Integer; AllowDeletionOfLastTab: Boolean): Boolean;

var
  CanClose: Boolean;
  LastActiveTab: Integer;

begin
  Result := True;

  if ((FTabs.Count > 1) and (TabIndex > -1) and (TabIndex < FTabs.Count)) or
    (AllowDeletionOfLastTab and (FTabs.Count = 1)) then
  begin
    CanClose := True;

    if Assigned(FOnBeforePageDelete) then
      FOnBeforePageDelete(FTabs[TabIndex], CanClose);

    if not CanClose then
    begin
      Result := False;
      Exit;
    end;

    if TabIndex = FHighlightedTab then
      FHighlightedTab := -1;

    LastActiveTab := FSelectedTab;
    if TabIndex = FSelectedTab then
    begin
      if TabIndex = FTabs.Count - 1 then
      begin
        SelectedTab := FSelectedTab - 1;
        // Avoid the adjustment done below. In this special case the index is already correct.
        Dec(LastActiveTab);
      end
      else
        SelectedTab := TabIndex + 1;
    end;

    if (Assigned(FOnPageDelete)) then
      FOnPageDelete(FTabs[TabIndex]);

    TTab(FTabs[TabIndex]).Obj.Free;
    TTab(FTabs[TabIndex]).TabPanel.Free;
    FTabs.Delete(TabIndex);

    // If the active tabsheet or one before it was deleted then we have to update the selected tab index.
    if TabIndex <= LastActiveTab then
      Dec(FSelectedTab);
    ComputeTabs;
    if (FTabs.Count = 1) and (FAutoHideTabHeader) then
      Hide
    else
      Invalidate;
  end;
end;

//----------------------------------------------------------------------------------------------------------------------

function TTabHeaderFrame.AddTabSheet(AOwner: TComponent; Caption, ImageName: WideString; TabPanel: TTntPanel; Obj:
  TObject;
  SelectNewPage: Boolean; DockTabPanel: Boolean; TabPanelHeight: Integer): Integer;

begin
  FTabs.Add(TTab.Create(AOwner, self, Caption, ImageName, TabPanel, Obj, DockTabPanel));
  ComputeTabs;
  Invalidate;

  if (TabPanel <> nil) then
  begin
    TabPanel.Visible := False;

    if (DockTabPanel) then
    begin
      TabPanel.Parent := Parent;
      //Get new Handles for all docked controls
      InitHandles(TabPanel);

      TabPanel.Align := alNone;
      TabPanel.Left := Left + 4;
      TabPanel.Top := Top + 23;
      TabPanel.Width := Width - 9;
      TabPanel.Height := Height - 28;
    end
    else
      if (TabPanelHeight > -1) then
      begin
        TabPanel.Left := 0;
        TabPanel.Width := Width;

        if (Visible) then
        begin
          TabPanel.Top := Top + 24;
          TabPanel.Height := TabPanelHeight - 24;
        end
        else
        begin
          TabPanel.Top := Top;
          TabPanel.Height := TabPanelHeight;
        end;
      end;

    if (Align = alBottom) then
      TabPanel.Anchors := [akLeft, akRight, akBottom]
    else
      TabPanel.Anchors := [akLeft, akTop, akRight, akBottom];

    TabPanel.BringToFront;
  end;

  if (SelectNewPage) then
    SelectedTab := FTabs.Count - 1;

  Result := FTabs.Count - 1;
end;

//----------------------------------------------------------------------------------------------------------------------

procedure TTabHeaderFrame.ClearTabSheets;

begin
  FTabs.Clear;
  Invalidate;
end;

//----------------------------------------------------------------------------------------------------------------------

function TTabHeaderFrame.TabCount: Integer;

begin
  Result := FTabs.Count;
end;

//----------------------------------------------------------------------------------------------------------------------

procedure TTabHeaderFrame.Show;
var
  i: Integer;
begin
  if (not (Visible)) then
  begin
    for i := 0 to FTabs.Count - 1 do
    begin
      TTab(FTabs[i]).TabPanel.Height := TTab(FTabs[i]).TabPanel.Height - 24;
      TTab(FTabs[i]).TabPanel.Top := TTab(FTabs[i]).TabPanel.Top + 24;
    end;
  end;

  inherited Show;
end;

//----------------------------------------------------------------------------------------------------------------------

procedure TTabHeaderFrame.Hide;

var
  i: Integer;

begin
  if (Visible) then
  begin
    for i := 0 to FTabs.Count - 1 do
    begin
      TTab(FTabs[i]).TabPanel.Top := TTab(FTabs[i]).TabPanel.Top - 24;
      TTab(FTabs[i]).TabPanel.Height := TTab(FTabs[i]).TabPanel.Height + 24;
    end;
  end;

  inherited Hide;
end;

//----------------------------------------------------------------------------------------------------------------------

procedure TTabHeaderFrame.TntFrameResize(Sender: TObject);

var
  i: Integer;

begin
  for i := 0 to FTabs.Count - 1 do
  begin
    if (TTab(FTabs[i]).DockTabPanel) and (TTab(FTabs[i]).TabPanel <> nil) then
    begin
      TTab(FTabs[i]).TabPanel.Left := Left + 4;
      TTab(FTabs[i]).TabPanel.Top := Top + 23;
      TTab(FTabs[i]).TabPanel.Width := Width - 9;
      TTab(FTabs[i]).TabPanel.Height := Height - 28;
    end;
  end;
end;

//----------------------------------------------------------------------------------------------------------------------

procedure TTabHeaderFrame.SelectNextTabSheet;

var
  NextTab: Integer;

begin
  NextTab := FSelectedTab + 1;
  if (NextTab >= TabCount) then
    NextTab := 0;

  SelectedTab := NextTab;
end;

//----------------------------------------------------------------------------------------------------------------------

function TTabHeaderFrame.GetTabIndex(Tab: TTab): Integer;

begin
  Result := FTabs.IndexOf(Tab);
end;

//----------------------------------------------------------------------------------------------------------------------

procedure TTabHeaderFrame.RenameTabsheetMIClick(Sender: TObject);

var
  TabName: WideString;

begin
  if (FClickedTabIndex>=0) and (TabSheets[FClickedTabIndex]<>nil) then
  begin
    TabName := TabSheets[FClickedTabIndex].Caption;

    if ShowModalEditDialog(_('Tabsheet Caption'),
      _('Please enter the new name of the tabsheet.'), myx_mtEdit,
        _('Rename')+#13#10+_('Cancel'), True, _('Name:'),
        TabName)=1 then
    begin
      TabSheets[FClickedTabIndex].Caption := TabName;
      ComputeTabs;
      Invalidate;
    end;
  end;
end;

//----------------------------------------------------------------------------------------------------------------------

procedure TTabHeaderFrame.TabHeaderPopupMenuPopup(Sender: TObject);
begin
  CloseTabsheetMI.Enabled := TabCount>1;
end;

//---TTabHeaderFrame-------------------------------------------------------------------------------------------------------------------

procedure TTabHeaderFrame.CloseTabsheetMIClick(Sender: TObject);
begin
  if (FClickedTabIndex>=0) and (FClickedTabIndex<TabCount) then
    DeleteTab(FClickedTabIndex);
end;

//----------------------------------------------------------------------------------------------------------------------

constructor TTab.Create(AOwner: TComponent; TabHeader: TTabHeaderFrame;
  Caption, ImageName: WideString; TabPanel: TTntPanel; Obj: TObject;
  DockTabPanel: Boolean);
begin
  FCaption := Caption;
  FTabHeader := TabHeader;

  self.TabPanel := TabPanel;
  self.Obj := Obj;
  self.DockTabPanel := DockTabPanel;
  if ImageName <> '' then
    FImage := LoadPNGImageFromResource(ImageName);
  self.RegularTextWidth := 0;
end;

//----------------------------------------------------------------------------------------------------------------------

destructor TTab.Destroy;

begin
  inherited;
end;

//----------------------------------------------------------------------------------------------------------------------

procedure TTab.SetCaption(Caption: Widestring);

begin
  FCaption := Caption;

  FTabHeader.ComputeTabs;
  FTabHeader.Invalidate;
end;

//----------------------------------------------------------------------------------------------------------------------

end.