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
|
{%MainUnit ../comctrls.pp}
{******************************************************************************
TNBPages
******************************************************************************
*****************************************************************************
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.
*****************************************************************************
}
{off $DEFINE NOTEBOOK_DEBUG}
const
TabControlOptionStr: Array[TCTabControlOption] of String = (
'nboShowCloseButtons', 'nboMultiLine', 'nboHidePageListPopup',
'nboKeyboardTabSwitch', 'nboShowAddTabButton', 'nboDoChangeOnSetIndex'
);
function DbgS(Opt: TCTabControlOptions): String; overload;
var
O: TCTabControlOption;
begin
Result := '';
for O in Opt do Result := Result + TabControlOptionStr[O] + ',';
if (Length(Result) > 0) then System.Delete(Result, Length(Result), 1);
Result := '[' + Result + ']';
end;
{------------------------------------------------------------------------------
TNBPages Constructor
------------------------------------------------------------------------------}
constructor TNBPages.Create(theNotebook: TCustomTabControl);
begin
inherited Create(theNotebook);
FPageList := TListWithEvent.Create;
FPageList.OnChange:=@PageListChange;
FNotebook := theNotebook;
end;
destructor TNBPages.Destroy;
begin
inherited Destroy;
FreeAndNil(FPageList);
end;
{------------------------------------------------------------------------------
procedure TNBPages.PageListChange(Ptr: Pointer; AnAction: TListNotification);
------------------------------------------------------------------------------}
procedure TNBPages.PageListChange(Ptr: Pointer; AnAction: TListNotification);
var
APage: TCustomPage;
begin
if (AnAction=lnAdded) then begin
APage:=TObject(Ptr) as TCustomPage;
if not (pfInserting in APage.FFlags) then
APage.Parent:=FNotebook;
end;
end;
function TNBPages.Get(Index: Integer): String;
begin
Result := TCustomPage(FPageList[Index]).Caption;
end;
function TNBPages.GetCount: Integer;
begin
Result := FPageList.Count;
end;
function TNBPages.GetObject(Index: Integer): TObject;
begin
Result := TObject(FPageList[Index]);
end;
procedure TNBPages.Put(Index: Integer; const S: String);
begin
TCustomPage(FPageList[Index]).Caption := S;
end;
function TNBPages.IndexOfPage(const AnObject: TPersistent): Integer;
begin
Result := FPageList.IndexOf(AnObject);
end;
procedure TNBPages.InsertPage(Index: Integer; const APage: TCustomPage);
begin
FPageList.Insert(Index, APage);
end;
procedure TNBPages.DeletePage(Index: Integer);
begin
FPageList.Delete(Index);
end;
function TNBPages.GetPage(Index: Integer): TCustomPage;
begin
Result := TCustomPage(GetObject(Index));
end;
{------------------------------------------------------------------------------
TNBPages Clear
------------------------------------------------------------------------------}
procedure TNBPages.Clear;
var
i: Integer;
begin
// remove the pages in reverse order but skip the Active Page,
// and remove the Active Page at the end,
// to avoid activating other Pages.
for i:=FNoteBook.PageCount-1 downto 0 do
if i<>FNoteBook.PageIndex then Delete(i);
if FNoteBook.PageCount>0 then Delete(0);
end;
{------------------------------------------------------------------------------
TNBPages Delete
------------------------------------------------------------------------------}
procedure TNBPages.Delete(Index: Integer);
var
APage: TCustomPage;
begin
// Make sure Index is in the range of valid pages to delete
{$IFDEF NOTEBOOK_DEBUG}
//DebugLn('TNBPages.Delete A Index=',Index);
DebugLn(['TNBPages.Delete B ',FNotebook.Name,' Index=',Index,' FPageList.Count=',FPageList.Count,' FNotebook.PageIndex=',FNotebook.PageIndex]);
{$ENDIF}
if (Index >= 0) and
(Index < FPageList.Count) then
begin
APage := TCustomPage(FPageList[Index]);
// delete handle
APage.Parent := nil;
// free the page
Application.ReleaseComponent(APage);
end;
{$IFDEF NOTEBOOK_DEBUG}
DebugLn(['TNBPages.Delete END ',FNotebook.Name,' Index=',Index,' FPageList.Count=',FPageList.Count,' FNotebook.PageIndex=',FNotebook.PageIndex]);
{$ENDIF}
end;
{------------------------------------------------------------------------------
TNBPages Insert
------------------------------------------------------------------------------}
procedure TNBPages.Insert(Index: Integer; const S: String);
var
NewPage: TCustomPage;
NewOwner: TComponent;
begin
{$IFDEF NOTEBOOK_DEBUG}
DebugLn(['TNBPages.Insert A ',FNotebook.Name,' Index=',Index,' S="',S,'"']);
{$ENDIF}
NewOwner := FNotebook.Owner;
if NewOwner = nil then
NewOwner := FNotebook;
NewPage := FNotebook.GetPageClass.Create(NewOwner);
with NewPage do
Caption := S;
{$IFDEF NOTEBOOK_DEBUG}
DebugLn(['TNBPages.Insert B ',FNotebook.Name,' Index=',Index,' S="',S,'"']);
{$ENDIF}
FNotebook.InsertPage(NewPage,Index);
{$IFDEF NOTEBOOK_DEBUG}
DebugLn(['TNBPages.Insert END ',FNotebook.Name,' Index=',Index,' S="',S,'"']);
{$ENDIF}
end;
{------------------------------------------------------------------------------
TNBPages Move
------------------------------------------------------------------------------}
procedure TNBPages.Move(CurIndex, NewIndex: Integer);
var
APage: TCustomPage;
NewControlIndex: integer;
ActivePageIndex: Integer;
ActivePage: TCustomPage;
begin
if CurIndex = NewIndex then Exit;
ActivePageIndex := FNotebook.PageIndex;
if (FNotebook.PageIndex >= 0) and (FNotebook.PageIndex < Count) then
ActivePage := GetPage(ActivePageIndex)
else
ActivePage := nil;
//NewPageIndex := NewIndex;
APage := TCustomPage(FPageList[CurIndex]);
// calculate new control index (i.e. ZOrderPosition)
if NewIndex >= FPageList.Count - 1 then
NewControlIndex := FNotebook.ControlCount-1
else
NewControlIndex := FNotebook.GetControlIndex(TCustomPage(FPageList[NewIndex]));
FNotebook.DisableAutoSizing{$IFDEF DebugDisableAutoSizing}('TNBPages.Move'){$ENDIF};
try
// move Page in notebook handle
FNotebook.WSMovePage(APage, NewIndex);
// move Page in FPageList
FPageList.Move(CurIndex, NewIndex);
// move in wincontrol list
FNotebook.SetControlIndex(APage, NewControlIndex);
// update PageIndex
if ActivePage <> nil then
FNotebook.InternalSetPageIndex(IndexOfPage(ActivePage))
else // Can not restore an invalid page index.
if FNotebook.PageIndex >= 0 then // keep if -1
FNotebook.PageIndex := NewIndex;
finally
FNotebook.EnableAutoSizing{$IFDEF DebugDisableAutoSizing}('TNBPages.Move'){$ENDIF};
end;
end;
{ TNBNoPages }
function TNBNoPages.Get(Index: Integer): String;
begin
Result := '';
end;
function TNBNoPages.GetCount: Integer;
begin
Result := 0;
end;
function TNBNoPages.IndexOfPage(const AnObject: TPersistent): Integer;
begin
Result := -1;
end;
function TNBNoPages.GetPage(Index: Integer): TCustomPage;
begin
Result := nil;
end;
{******************************************************************************
TCustomTabControl
******************************************************************************}
{------------------------------------------------------------------------------
TCustomTabControl Constructor
------------------------------------------------------------------------------}
constructor TCustomTabControl.Create(TheOwner: TComponent);
begin
inherited Create(TheOwner);
fCompStyle := csNoteBook;
FAccess := GetListClass.Create(Self);
FImageListChangeLink := TChangeLink.Create;
FImageListChangeLink.OnChange := @DoImageListChange;
FImageListChangeLink.OnDestroyResolutionHandle := @DoImageListDestroyResolutionHandle;
FPageIndex := -1;
ControlStyle := []; // do not add csAcceptsControls
TabPosition := tpTop;
TabStop := true;
ShowTabs := True;
with GetControlClassDefaultSize do
SetInitialBounds(0, 0, CX, CY);
// Accessibility
AccessibleDescription := rsTCustomTabControlAccessibilityDescription;
AccessibleRole := larTabControl;
end;
{------------------------------------------------------------------------------
Method: TCustomTabControl.CreateWnd
Params: None
Returns: Nothing
Creates the interface object.
------------------------------------------------------------------------------}
procedure TCustomTabControl.CreateWnd;
var
i: Integer;
lPage: TCustomPage;
begin
{$IFDEF NOTEBOOK_DEBUG}
DebugLn(['TCustomTabControl.CreateWnd ',dbgsName(Self),' HandleAllocated=',HandleAllocated]);
{$ENDIF}
inherited CreateWnd;
DisableAlign;
try
FAddingPages := True;
for i := 0 to PageCount -1 do
begin
{$IFDEF NOTEBOOK_DEBUG}
DebugLn(['TCustomTabControl.CreateWnd ',dbgsName(Self),' Page.Caption=',Page[i].Caption,' pfAdded=',pfAdded in Page[i].Flags]);
{$ENDIF}
lPage := Page[i];
AddRemovePageHandle(lPage);
end;
FAddingPages := False;
DoSendShowTabs;
DoSendPageIndex;
ReAlign;
finally
EnableAlign;
end;
end;
procedure TCustomTabControl.Loaded;
begin
inherited Loaded;
if HandleAllocated then
DoSendPageIndex;
end;
procedure TCustomTabControl.DoChange;
begin
if Assigned(OnChange) then
OnChange(Self);
end;
procedure TCustomTabControl.InitializeWnd;
begin
inherited InitializeWnd;
//DebugLn(['TCustomTabControl.InitializeWnd ',DbgSName(Self),' fPageIndex=',fPageIndex]);
FPageIndexOnLastChange := PageIndex;
end;
{------------------------------------------------------------------------------
Method: TCustomTabControl.Destroy
Params: None
Returns: Nothing
Destructor for the class.
------------------------------------------------------------------------------}
destructor TCustomTabControl.Destroy;
begin
FImageListChangeLink.Free;
Pages.Clear;
FreeAndNil(FAccess);
Application.RemoveAsyncCalls(Self);
inherited Destroy;
end;
function TCustomTabControl.TabRect(AIndex: Integer): TRect;
begin
if HandleAllocated then
Result := TWSCustomTabControlClass(WidgetSetClass).GetTabRect(Self, AIndex)
else
Result := Rect(-1, -1, -1, -1);
end;
function TCustomTabControl.GetImageIndex(ThePageIndex: Integer): Integer;
var
APage: TCustomPage;
begin
APage := Page[ThePageIndex];
if APage <> nil then
Result := APage.ImageIndex
else
Result := -1;
if Assigned(OnGetImageIndex) then
OnGetImageIndex(Self, ThePageIndex, Result);
end;
function TCustomTabControl.IndexOf(APage: TPersistent): integer;
begin
Result:=TNBPages(FAccess).IndexOfPage(APage);
end;
function TCustomTabControl.CustomPage(Index: integer): TCustomPage;
begin
Result:=GetPage(Index);
end;
function TCustomTabControl.CanChangePageIndex: boolean;
begin
Result := CanChange;
end;
function TCustomTabControl.CanChange: Boolean;
begin
Result := True;
if ([csDesigning, csDestroying] * ComponentState = []) and Assigned(OnChanging) then
OnChanging(Self, Result);
end;
function TCustomTabControl.GetMinimumTabWidth: integer;
begin
Result := TWSCustomTabControlClass(WidgetSetClass).GetNotebookMinTabWidth(Self);
//debugln('TCustomTabControl.GetMinimumTabWidth A ',dbgs(Result));
end;
function TCustomTabControl.GetMinimumTabHeight: integer;
begin
Result := TWSCustomTabControlClass(WidgetSetClass).GetNotebookMinTabHeight(Self);
//debugln('TCustomTabControl.GetMinimumTabHeight A ',dbgs(Result));
end;
function TCustomTabControl.GetCapabilities: TCTabControlCapabilities;
begin
Result:=TWSCustomTabControlClass(WidgetSetClass).GetCapabilities;
end;
function TCustomTabControl.PageToTabIndex(AIndex: integer): integer;
var
i: integer;
begin
(* Map LCL Page into widgetset Tab index.
Taken from TWin32WSCustomNotebook.GetPageRealIndex (modified)
*)
if (AIndex < 0) or (AIndex >= PageCount) then
exit(-1);
Result := AIndex;
if csDesigning in ComponentState then
exit; //all pages are visible
// it is possible to show pages without visible tabs, but then, no tab index
// can be sendet back, issue #21723
if not Page[AIndex].TabVisible then
exit(-1);
for i := 0 to AIndex - 1 do begin
if not Page[i].TabVisible then
dec(Result); //exclude invisible page
end;
end;
function TCustomTabControl.TabToPageIndex(AIndex: integer): integer;
var
I: integer;
begin
(* Map widgetset Tab index into LCL Page index.
Taken from win32 NotebookPageRealToLCLIndex
*)
Result := AIndex;
if (csDesigning in ComponentState) then
exit; //all pages are visible
I := 0;
while (I < PageCount) and (I <= Result) do
begin
if not Page[I].TabVisible then
Inc(Result); //insert invisible page
Inc(I);
end;
end;
{------------------------------------------------------------------------------
method TCustomTabControl DoCloseTabClicked
Params: APage: TCustomPage
Result: none
Called whenever the user closes the tab.
------------------------------------------------------------------------------}
procedure TCustomTabControl.DoCloseTabClicked(APage: TCustomPage);
begin
if Assigned(OnCloseTabClicked) then OnCloseTabClicked(APage);
end;
{------------------------------------------------------------------------------
TCustomTabControl GetActivePage
------------------------------------------------------------------------------}
function TCustomTabControl.GetActivePage: String;
begin
if (FPageIndex >= 0) and (FPageIndex < PageCount) then
Result := Page[FPageIndex].Caption
else
Result := '';
end;
{------------------------------------------------------------------------------
function TCustomTabControl.GetActivePageComponent: TCustomPage;
------------------------------------------------------------------------------}
function TCustomTabControl.GetActivePageComponent: TCustomPage;
begin
if (FPageIndex >= 0) and (FPageIndex < PageCount) then
Result := Page[FPageIndex]
else
Result := nil;
end;
function TCustomTabControl.GetDisplayRect: TRect;
begin
Result := GetClientRect; //???
end;
function TCustomTabControl.GetMultiLine: Boolean;
begin
Result := nboMultiLine in Options;
end;
{------------------------------------------------------------------------------
TCustomTabControl SetActivePage
------------------------------------------------------------------------------}
procedure TCustomTabControl.SetActivePage(const Value: String);
var
i: Integer;
begin
for i := 0 to PageCount - 1 do
begin
if Page[i].Caption = Value then
begin
SetPageIndex(i);
Exit;
end;
end;
end;
procedure TCustomTabControl.SetActivePageComponent(const AValue: TCustomPage);
begin
PageIndex := IndexOf(AValue); // -1 for unpaged
end;
procedure TCustomTabControl.SetImageListAsync(Data: PtrInt);
begin
DoImageListChange(Self);
end;
procedure TCustomTabControl.SetImages(const AValue: TCustomImageList);
begin
if FImages = AValue then Exit;
if FImages <> nil then
begin
FImages.UnRegisterChanges(FImageListChangeLink);
FImages.RemoveFreeNotification(Self);
end;
FImages := AValue;
if FImages <> nil then
begin
FImages.FreeNotification(Self);
FImages.RegisterChanges(FImageListChangeLink);
end;
DoImageListChange(Self);
UpdateTabProperties;
end;
procedure TCustomTabControl.SetImagesWidth(const aImagesWidth: Integer);
begin
if FImagesWidth = aImagesWidth then Exit;
FImagesWidth := aImagesWidth;
DoImageListChange(Self);
UpdateTabProperties;
end;
procedure TCustomTabControl.SetOptions(const AValue: TCTabControlOptions);
var
ChangedOptions: TCTabControlOptions;
begin
if FOptions = AValue then Exit;
ChangedOptions := (FOptions - AValue) + (AValue - FOptions);
FOptions := AValue;
if nboShowCloseButtons in ChangedOptions then
UpdateTabProperties;
if HandleAllocated then
TWSCustomTabControlClass(WidgetSetClass).UpdateProperties(Self);
end;
{------------------------------------------------------------------------------
TCustomTabControl SetPageIndex
------------------------------------------------------------------------------}
procedure TCustomTabControl.SetPageIndex(AValue: Integer);
begin
if (AValue < -1) or (AValue >= PageCount) then Exit;
//debugln('TCustomTabControl.SetPageIndex A ',dbgsName(Self),' AValue=',dbgs(AValue),' fPageIndex=',dbgs(fPageIndex),' PageCount=',dbgs(PageCount),' HandleAllocated=',dbgs(HandleAllocated),' ',dbgs(ComponentState));
if FPageIndex = AValue then exit;
if (nboDoChangeOnSetIndex in Options) and (not CanChangePageIndex) then exit; //Delphi does not call CanChange either
//debugln('TCustomTabControl.SetPageIndex B ',dbgsName(Self),' AValue=',dbgs(AValue),' fPageIndex=',dbgs(fPageIndex),' PageCount=',dbgs(PageCount),' HandleAllocated=',dbgs(HandleAllocated));
InternalSetPageIndex(AValue);
//debugln(['TCustomTabControl.SetPageIndex C ',dbgsName(Self)]);
//debugln([' FOptions = ',DbgS(Foptions)]);
if ([csDesigning, csLoading, csDestroying] * ComponentState = [])
and (nboDoChangeOnSetIndex in Options) then
DoChange;
end;
{$IFDEF old}
{------------------------------------------------------------------------------
TCustomTabControl GetPageIndex
------------------------------------------------------------------------------}
function TCustomTabControl.GetPageIndex: Integer;
begin
Result := FPageIndex;
end;
{$ELSE}
//if override is required, make virtual first!
{$ENDIF}
procedure TCustomTabControl.InsertPage(APage: TCustomPage; Index: Integer);
var
NewZPosition: integer;
begin
// only called from TNBPages, but not TNBNoPages
// Also called from TCustomPage.SetParent
if (IndexOf(APage) >= 0) then Exit;
{$IFDEF NOTEBOOK_DEBUG}
DebugLn(['TCustomTabControl.InsertPage A ',dbgsName(Self),' Index=',Index,' Name=',
APage.Name,' Caption=',APage.Caption]);
{$ENDIF}
APage.DisableAlign;
try
if Index < PageCount then
NewZPosition := GetControlIndex(Page[Index])
else
NewZPosition := -1;
Include(APage.FFlags, pfInserting);
TNBPages(FAccess).InsertPage(Index, APage);
Exclude(APage.FFlags, pfInserting);
APage.Parent := Self; // will recursively call
if NewZPosition >= 0 then
SetControlIndex(APage, NewZPosition);
if PageIndex = -1 then
FPageIndex := Index;
UpdateDesignerFlags(Index);
if HandleAllocated and (not (csLoading in ComponentState)) then
begin
AddRemovePageHandle(APage);
if PageIndex = Index then
DoSendPageIndex;
end;
finally
APage.EnableAlign;
end;
{$IFDEF NOTEBOOK_DEBUG}
DebugLn(['TCustomTabControl.InsertPage END ',dbgsName(Self),' Index=',
Index,' Name=',APage.Name,' Caption=',APage.Caption]);
{$ENDIF}
end;
{------------------------------------------------------------------------------
TCustomTabControl MoveTab
------------------------------------------------------------------------------}
procedure TCustomTabControl.MoveTab(Sender: TObject; NewIndex: Integer);
begin
if Assigned(Sender) and (NewIndex < PageCount) then
begin
TNBPages(fAccess).Move(TCustomPage(Sender).PageIndex, NewIndex);
Change;
end;
end;
procedure TCustomTabControl.SetMultiLine(const AValue: Boolean);
begin
if AValue then
Options := Options + [nboMultiLine]
else
Options := Options - [nboMultiLine];
end;
procedure TCustomTabControl.SetStyle(AValue: TTabStyle);
begin
if FStyle = AValue then Exit;
FStyle := AValue;
end;
{------------------------------------------------------------------------------
function TCustomTabControl.FindVisiblePage(Index: Integer): Integer;
It tries to find the next (at right) visible page. If no one is found,
it tries to to find the previous (at left) visible page.
Returns -1 if there's no visible pages.
------------------------------------------------------------------------------}
function TCustomTabControl.FindVisiblePage(Index: Integer): Integer;
begin
for Result := Index to PageCount - 1 do
if Page[Result].TabVisible then
exit;
// if arrived here no visible forward page was found, search backwards
for Result := Index - 1 downto 0 do
if Page[Result].TabVisible then
exit;
Result := -1;
end;
procedure TCustomTabControl.PageRemoved(Index: Integer);
var
NewPageIndex: Integer;
begin
if not (csLoading in ComponentState) then
begin
// if this page is showing, then show the next page before deleting it
if Index = FPageIndex then
begin
NewPageIndex := FindVisiblePage(Index);
if NewPageIndex >= 0 then
PageIndex := NewPageIndex
else
FPageIndex := NewPageIndex;
end;
end;
end;
procedure TCustomTabControl.WSMovePage(APage: TCustomPage; NewIndex: Integer);
var
RealIndex: Integer;
i: Integer;
begin
//DebugLn(['TCustomTabControl.WSMovePage APage=',DbgSName(APage),' NewIndex=',NewIndex,' pfAdded=',pfAdded in APage.FFlags]);
if HandleAllocated and (pfAdded in APage.FFlags) then
begin
RealIndex := 0;
i := 0;
repeat
if (i = NewIndex) or (i = PageCount) then break;
if pfAdded in Page[i].FFlags then inc(RealIndex);
inc(i);
until false;
//DebugLn(['TCustomTabControl.WSMovePage APage=',DbgSName(APage),' NewIndex=',NewIndex,' RealIndex=',RealIndex]);
TWSCustomTabControlClass(WidgetSetClass).MovePage(Self, APage, RealIndex);
end;
end;
procedure TCustomTabControl.AddRemovePageHandle(APage: TCustomPage);
begin
DisableAutoSizing{$IFDEF DebugDisableAutoSizing}('TCustomTabControl.AddRemovePageHandle'){$ENDIF};
try
if (not (csDestroying in APage.ComponentState))
and (APage.TabVisible or (csDesigning in ComponentState)) then begin
{$IFDEF NOTEBOOK_DEBUG}
DebugLn(['TCustomTabControl.AddRemovePageHandle ADD ',DbgSName(APage),' pfAdded=',pfAdded in APage.FFlags]);
{$ENDIF}
if (pfAdded in APage.FFlags) then exit;
Include(APage.FFlags,pfAdding);
TWSCustomTabControlClass(WidgetSetClass).AddPage(Self, APage, APage.VisibleIndex);
APage.FFlags:=APage.FFlags+[pfAdded]-[pfAdding];
APage.AdjustSize;
end else begin
{$IFDEF NOTEBOOK_DEBUG}
DebugLn(['TCustomTabControl.AddRemovePageHandle REMOVE ',DbgSName(APage),' pfAdded=',pfAdded in APage.FFlags]);
{$ENDIF}
if not (pfAdded in APage.FFlags) or (pfRemoving in APage.FFlags) then
exit;
APage.FFlags := APage.FFlags - [pfAdded] + [pfRemoving];
TWSCustomTabControlClass(WidgetSetClass).RemovePage(Self, APage.VisibleIndex);
if APage.HandleAllocated then
APage.DestroyHandle;
Exclude(APage.FFlags, pfRemoving);
end;
finally
EnableAutoSizing{$IFDEF DebugDisableAutoSizing}('TCustomTabControl.AddRemovePageHandle'){$ENDIF};
end;
end;
procedure TCustomTabControl.RemovePage(Index: Integer);
var
APage: TCustomPage;
begin
// Make sure Index is in the range of valid pages to delete
{$IFDEF NOTEBOOK_DEBUG}
DebugLn(['TCustomTabControl.RemovePage A ',dbgsName(Self),' Index=',Index,
' FAccess.Count=',PageCount,' PageIndex=',PageIndex]);
{$ENDIF}
if (Index >= 0) and (Index < PageCount) then
begin
APage:=Page[Index];
APage.FTabVisible:=false;
if HandleAllocated then
AddRemovePageHandle(APage);
PageRemoved(Index);
TNBPages(FAccess).DeletePage(Index);
APage.Parent:=nil;
if FPageIndex >= Index then
Dec(FPageIndex);
end;
{$IFDEF NOTEBOOK_DEBUG}
DebugLn(['TCustomTabControl.RemovePage END ',dbgsName(Self),' Index=',Index,' FAccess.Count=',FAccess.Count,' PageIndex=',PageIndex]);
{$ENDIF}
end;
procedure TCustomTabControl.MaybeSelectFirstControlOnPage(APage: TCustomPage);
var
ParentForm: TCustomForm;
C, ActiveControl: TWinControl;
begin
Assert(Assigned(APage),'TCustomTabControl.MaybeSelectFirstControlOnPage: APage=nil');
ParentForm := GetParentForm(Self);
//Debugln(['TCustomTabControl.ActivateFirstControl: Self=',DbgSName(Self),', APage=',DbgSName(APage),', ParentForm=',DbgSName(ParentForm)]);
if not (Assigned(ParentForm) and APage.Visible and APage.Enabled and (APage.ControlCount > 0)) then
Exit;
//Debugln(['TCustomTabControl.ActivateFirstControl: APage.Visible=',APage.Visible,', APage.Enabled=',APage.Enabled,', (APage.ControlCount > 0)=', (APage.ControlCount > 0)]);
ActiveControl := ParentForm.ActiveControl;
//don't steal focus if a control outside is ActiveControl
if (Self = ActiveControl) or Self.ContainsControl(ActiveControl) then
begin
C := APage.FindNextControl(Self, True, True, False);
if Assigned(C) then
ParentForm.ActiveControl := C;
end;
end;
{------------------------------------------------------------------------------
function TCustomTabControl.IsStoredActivePage: boolean;
------------------------------------------------------------------------------}
function TCustomTabControl.IsStoredActivePage: boolean;
begin
Result:=false;
end;
procedure TCustomTabControl.KeyDown(var Key: Word; Shift: TShiftState);
begin
if (nboKeyboardTabSwitch in Options) and (Key = VK_TAB) and (PageCount > 0) then
begin
if Shift = [ssCtrl] then
begin
Key := 0;
PageIndex := (PageIndex + 1) mod PageCount;
Exit;
end
else if Shift = [ssCtrl, ssShift] then
begin
Key := 0;
PageIndex := (PageIndex + PageCount - 1) mod PageCount;
Exit;
end;
end;
inherited KeyDown(Key, Shift);
end;
{------------------------------------------------------------------------------
TCustomTabControl GetPageCount
------------------------------------------------------------------------------}
function TCustomTabControl.GetPageCount: integer;
begin
Result := FAccess.Count
end;
{------------------------------------------------------------------------------
TCustomTabControl SetPages
------------------------------------------------------------------------------}
procedure TCustomTabControl.SetPages(AValue: TStrings);
begin
FAccess.Assign(AValue);
end;
{------------------------------------------------------------------------------
TCustomTabControl GetPage
------------------------------------------------------------------------------}
function TCustomTabControl.GetPage(AIndex: Integer): TCustomPage;
begin
Result := TNBPages(FAccess).GetPage(AIndex);
end;
{------------------------------------------------------------------------------
TCustomTabControl SetShowTabs
------------------------------------------------------------------------------}
procedure TCustomTabControl.SetShowTabs(AValue: Boolean);
begin
if fShowTabs=AValue then exit;
fShowTabs := AValue;
DoSendShowTabs;
end;
{------------------------------------------------------------------------------
TCustomTabControl SetTabHeight
------------------------------------------------------------------------------}
procedure TCustomTabControl.SetTabHeight(AValue: Smallint);
begin
if FTabHeight = AValue then Exit;
if not (nbcTabsSizeable in GetCapabilities) then Exit;
FTabHeight := AValue;
DoSendTabSize;
end;
function TCustoMTabControl.TabHeightIsStored: Boolean;
begin
Result := TabHeight > 0;
end;
{------------------------------------------------------------------------------
TCustomTabControl SetTabPosition
------------------------------------------------------------------------------}
procedure TCustomTabControl.SetTabPosition(tabPos: TTabPosition);
begin
if fTabPosition = tabPos then exit;
fTabPosition := tabPos;
DoSendTabPosition;
end;
{------------------------------------------------------------------------------
TCustomTabControl SetTabWidth
------------------------------------------------------------------------------}
procedure TCustomTabControl.SetTabWidth(AValue: Smallint);
begin
if FTabWidth = AValue then Exit;
if not (nbcTabsSizeable in GetCapabilities) then Exit;
FTabWidth := AValue;
DoSendTabSize;
end;
function TCustomTabControl.TabWidthIsStored: Boolean;
begin
Result := TabWidth > 0;
end;
{------------------------------------------------------------------------------
procedure TCustomTabControl.UpdateAllDesignerFlags;
------------------------------------------------------------------------------}
procedure TCustomTabControl.UpdateAllDesignerFlags;
var
i: integer;
begin
for i:=0 to PageCount-1 do
UpdateDesignerFlags(i);
end;
{------------------------------------------------------------------------------
procedure TCustomTabControl.UpdateDesignerFlags(APageIndex: integer);
------------------------------------------------------------------------------}
procedure TCustomTabControl.UpdateDesignerFlags(APageIndex: integer);
begin
if APageIndex<>fPageIndex then
Page[APageIndex].ControlStyle:=
Page[APageIndex].ControlStyle+[csNoDesignVisible]
else
Page[APageIndex].ControlStyle:=
Page[APageIndex].ControlStyle-[csNoDesignVisible];
end;
function TCustomTabControl.GetPageClass: TCustomPageClass;
begin
Result := TCustomPage;
end;
function TCustomTabControl.GetListClass: TNBBasePagesClass;
begin
Result := TNBPages;
end;
class procedure TCustomTabControl.WSRegisterClass;
begin
inherited WSRegisterClass;
RegisterCustomTabControl();
end;
{------------------------------------------------------------------------------
TCustomTabControl ReadState
------------------------------------------------------------------------------}
procedure TCustomTabControl.ReadState(Reader: TReader);
begin
// do not clear. Think about loading ancestor + loading descendant stream.
// fAccess.Clear;
inherited ReadState(Reader);
end;
{------------------------------------------------------------------------------
TCustomTabControl ShowControl
------------------------------------------------------------------------------}
procedure TCustomTabControl.ShowControl(APage: TControl);
var
i: LongInt;
begin
{ Find a child control that matches the one passed in and display
the page that contains that control. This method is necessary
for compatibility with Delphi }
for i := 0 to PageCount - 1 do begin
if Page[i] = APage then begin
PageIndex := i;
Exit;
end;
end;
inherited ShowControl(APage);
end;
{------------------------------------------------------------------------------
function TCustomTabControl.IndexOfTabAt(X, Y: Integer): Integer;
Returns the index of the visible tab at the client position.
------------------------------------------------------------------------------}
function TCustomTabControl.IndexOfTabAt(X, Y: Integer): Integer;
begin
Result := IndexOfTabAt(Point(X, Y));
end;
{------------------------------------------------------------------------------
function TCustomTabControl.IndexOfTabAt(P: TPoint): Integer;
Returns the index of the visible tab at the client position.
For example:
Index:=NoteBook1.IndexOfTabAt(
NoteBook1.ScreenToClient(Mouse.CursorPos));
------------------------------------------------------------------------------}
function TCustomTabControl.IndexOfTabAt(P: TPoint): Integer;
begin
if HandleAllocated then
Result := TWSCustomTabControlClass(WidgetSetClass).GetTabIndexAtPos(Self, P)
else
Result := -1;
end;
{------------------------------------------------------------------------------
function TCustomTabControl.IndexOfPageAt(X, Y: Integer): Integer;
Returns the index of the page at the client position.
------------------------------------------------------------------------------}
function TCustomTabControl.IndexOfPageAt(X, Y: Integer): Integer;
begin
Result := IndexOfPageAt(Point(X, Y));
end;
{------------------------------------------------------------------------------
function TCustomTabControl.IndexOfPageAt(X, Y: Integer): Integer;
Returns the index of the page at the client position.
For example:
Index:=NoteBook1.IndexOfPageAt(
NoteBook1.ScreenToClient(Mouse.CursorPos));
------------------------------------------------------------------------------}
function TCustomTabControl.IndexOfPageAt(P: TPoint): Integer;
begin
Result := IndexOfTabAt(P);
if Result <> -1 then
Result := TabToPageIndex(Result);
end;
{------------------------------------------------------------------------------
method TCustomTabControl UpdateTabProperties
Params: none
Result: none
Tells the interface to update all tabs.
------------------------------------------------------------------------------}
procedure TCustomTabControl.UpdateTabProperties;
var
i: integer;
begin
if not HandleAllocated or (csLoading in ComponentState) then exit;
for i := 0 to PageCount - 1 do
TWSCustomPageClass(Page[i].WidgetSetClass).UpdateProperties(Page[i]);
end;
class function TCustomTabControl.GetControlClassDefaultSize: TSize;
begin
Result.CX := 200;
Result.CY := 200;
end;
procedure TCustomTabControl.Notification(AComponent: TComponent;
Operation: TOperation);
begin
inherited Notification(AComponent, Operation);
if (Operation = opRemove) and (AComponent = Images) then
SetImages(nil);
end;
{------------------------------------------------------------------------------
TCustomTabControl Change
------------------------------------------------------------------------------}
procedure TCustomTabControl.Change;
var
APage: TCustomPage;
begin
//DebugLn(['TCustomTabControl.Change ',DbgSName(Self),' fPageIndex=',fPageIndex]);
ShowCurrentPage;
FPageIndexOnLastChange := FPageIndex;
if ([csLoading,csDestroying]*ComponentState=[]) and (not FAddingPages) then
begin
APage := ActivePageComponent;
//debugln(['TCustomTabControl.Change: Self=',DbgSName(Self),', APage',DbgSName(APage)]);
if Assigned(APage) then
MaybeSelectFirstControlOnPage(APage);
DoChange;
end;
end;
function TCustomTabControl.DialogChar(var Message: TLMKey): boolean;
var
destPage: TCustomPage;
begin
// broadcast only to active page
Result := false;
destPage := GetActivePageComponent;
if destPage <> nil then
Result := destPage.DialogChar(Message);
end;
procedure TCustomTabControl.DoAutoAdjustLayout(
const AMode: TLayoutAdjustmentPolicy; const AXProportion,
AYProportion: Double);
begin
inherited DoAutoAdjustLayout(AMode, AXProportion, AYProportion);
if AMode in [lapAutoAdjustWithoutHorizontalScrolling, lapAutoAdjustForDPI] then
begin
if Assigned(Images) then
DoImageListChange(Self);
if TabHeightIsStored then
TabHeight := Round(TabHeight * AYProportion);
if TabWidthIsStored then
TabWidth := Round(TabWidth * AXProportion);
end;
end;
procedure TCustomTabControl.InternalSetPageIndex(AValue: Integer);
var
APage: TCustomPage;
begin
FPageIndex := AValue;
UpdateAllDesignerFlags;
DoSendPageIndex;
APage := GetActivePageComponent;
if Assigned(APage) then
MaybeSelectFirstControlOnPage(APage);
end;
{------------------------------------------------------------------------------
TCustomTabControl CNNotify
------------------------------------------------------------------------------}
procedure TCustomTabControl.CNNotify(var Message: TLMNotify);
var
OldPageIndex: LongInt;
begin
with Message do
case NMHdr^.code of
TCN_SELCHANGE:
begin
// set the page from the NMHDR^.idfrom
if (not FAddingPages) and not
(csDestroyingHandle in ControlState) then
begin
OldPageIndex := FPageIndex;
FPageIndex := PtrInt(NMHDR^.idfrom);
if FPageIndex >= PageCount then
FPageIndex := -1;
//debugln(['TCustomTabControl.CNNotify ',DbgSName(Self),' A Old=',OldPageIndex,' fPageIndex=',fPageIndex,' FLoadedPageIndex=',FLoadedPageIndex]);
//if PageIndex>=0 then DebugLn(['TCustomTabControl.CNNotify Page=',DbgSName(Page[PageIndex]),' Visible=',Page[PageIndex].Visible]);
UpdateAllDesignerFlags;
if ([csLoading,csDestroying]*ComponentState=[]) then
begin
if OldPageIndex <> FPageIndex then
begin
if csDesigning in ComponentState then
OwnerFormDesignerModified(Self);
//DebugLn(['TCustomTabControl.CNNotify ',DbgSName(Page[PageIndex]),' ',Page[PageIndex].Visible]);
Change;
end;
end;
end;
end;
TCN_SELCHANGING:
begin
if CanChangePageIndex and not
(csDestroyingHandle in ControlState) then
Result := 0
else
Result := 1;
//debugln('TCustomTabControl.CNNotify TCN_SELCHANGING Result=',dbgs(Result));
end;
else
begin
{$IFDEF NOTEBOOK_DEBUG}
DebugLn(['[TCustomTabControl.CNNotify] unhandled NMHdr code:', NMHdr^.code]);
{$ENDIF}
end;
end;
end;
function HasFocusedControl(APage: TCustomPage): Boolean;
var
i: Integer;
lForm: TCustomForm;
begin
Result := False;
lForm := GetParentForm(APage);
if (lForm=nil) or not lForm.Focused then Exit;
for i := 0 to APage.ControlCount - 1 do
if APage.Controls[i] = lForm.ActiveControl then
Exit(True);
end;
procedure TCustomTabControl.ShowCurrentPage;
// Makes sure Visible = true for page which has index FPageIndex
var
CurPage: TCustomPage;
begin
CurPage := nil;
if (FPageIndex < 0) or (FPageIndex >= PageCount) then Exit;
CurPage := Page[FPageIndex];
CurPage.Visible := True;
//DebugLn(['TCustomTabControl.ShowCurrentPage CurPage.AutoSizeDelayed=',CurPage.AutoSizeDelayed,' ',dbgs(CurPage.ComponentState),' ',CurPage.HandleAllocated]);
if (FPageIndexOnLastChange < 0) or (FPageIndexOnLastChange >= PageCount)
or (FPageIndexOnLastChange = FPageIndex) then
Exit;
if CurPage.Enabled and HasFocusedControl(Page[FPageIndexOnLastChange]) then
CurPage.SetFocus;
Page[FPageIndexOnLastChange].Visible := False;
end;
{------------------------------------------------------------------------------
procedure TCustomTabControl.DoSendPageIndex;
------------------------------------------------------------------------------}
procedure TCustomTabControl.DoSendPageIndex;
begin
//DebugLn('[TCustomTabControl.DoSendPageIndex] A ',dbgsName(Self),' PageIndex=',dbgs(fPageIndex),' ',dbgs(csLoading in ComponentState),' ',dbgs(HandleAllocated));
if not HandleAllocated or (csLoading in ComponentState) then exit;
{$IFDEF NOTEBOOK_DEBUG}
//DebugLn('[TCustomTabControl.DoSendPageIndex] B ',dbgsName(Self),' PageIndex=',dbgs(fPageIndex));
{$ENDIF}
ShowCurrentPage;
FPageIndexOnLastChange := FPageIndex;
TWSCustomTabControlClass(WidgetSetClass).SetPageIndex(Self, FPageIndex);
{$IFDEF NOTEBOOK_DEBUG}
//DebugLn('[TCustomTabControl.DoSendPageIndex] END ',dbgs(FPageIndex));
{$ENDIF}
end;
{------------------------------------------------------------------------------
procedure TCustomTabControl.DoSendShowTabs;
------------------------------------------------------------------------------}
procedure TCustomTabControl.DoSendShowTabs;
begin
if not HandleAllocated or (csLoading in ComponentState) then exit;
{$IFDEF NOTEBOOK_DEBUG}
DebugLn('[TCustomTabControl.DoSendShowTabs] A ',dbgsName(Self));
{$ENDIF}
TWSCustomTabControlClass(WidgetSetClass).ShowTabs(Self, FShowTabs);
{$IFDEF NOTEBOOK_DEBUG}
DebugLn('[TCustomTabControl.DoSendShowTabs] B ',dbgsName(Self));
{$ENDIF}
end;
{------------------------------------------------------------------------------
procedure TCustomTabControl.DoSendTabPosition;
------------------------------------------------------------------------------}
procedure TCustomTabControl.DoSendTabPosition;
begin
if not HandleAllocated or (csLoading in ComponentState) then exit;
TWSCustomTabControlClass(WidgetSetClass).SetTabPosition(Self, FTabPosition);
end;
procedure TCustomTabControl.DoSendTabSize;
begin
if not HandleAllocated or (csLoading in ComponentState) then exit;
TWSCustomTabControlClass(WidgetSetClass).SetTabSize(Self, FTabWidth, FTabHeight);
DoSendTabPosition;
Invalidate;
end;
procedure TCustomTabControl.DoImageListChange(Sender: TObject);
begin
if HandleAllocated then
begin
if Assigned(Images) then
TWSCustomTabControlClass(WidgetSetClass).SetImageList(Self, Images.ResolutionForPPI[ImagesWidth, Font.PixelsPerInch, 1].Resolution) // to-do: support scaling factor
else
TWSCustomTabControlClass(WidgetSetClass).SetImageList(Self, nil);
end;
end;
procedure TCustomTabControl.DoImageListDestroyResolutionHandle(
Sender: TCustomImageList; AWidth: Integer; AReferenceHandle: TLCLHandle);
begin
TWSCustomTabControlClass(WidgetSetClass).SetImageList(Self, nil);
Application.QueueAsyncCall(@SetImageListAsync, 0);
end;
|