cocoa: implementing TCocoaWSCustomTabControl.GetTabRect

git-svn-id: trunk@62497 -
This commit is contained in:
dmitry 2020-01-05 06:34:36 +00:00
parent 439be6f619
commit 2c9df5c82a
2 changed files with 92 additions and 2 deletions

View File

@ -56,9 +56,8 @@ type
prevarr : NSButton;
nextarr : NSButton;
leftmost : Integer; // index of the left-most tab shown
public
leftmost : Integer; // index of the left-most tab shown
ignoreChange: Boolean;
callback: ITabControlCallback;
@ -116,8 +115,46 @@ type
function IndexOfTab(ahost: TCocoaTabControl; atab: NSTabViewItem): Integer;
// Hack: The function attempts to determine the tabs view
// if the view is found it would return its frame rect in LCL coordinates
// if the view cannot be determinted, the function returns false
// This is implemented as ObjC method, because "prevarr" and "nextarr"
// are private methods.
// It's unknown, if it's safe to cache the result, so the search is performed
// everytime
function GetTabsRect(tabs: TCocoaTabControl; var r: TRect): Boolean;
implementation
function GetTabsRect(tabs: TCocoaTabControl; var r: TRect): Boolean;
var
i : integer;
sv : NSView;
f : NSRect;
begin
Result:=Assigned(tabs);
if not Result then Exit;
for i:=0 to Integer(tabs.subviews.count)-1 do
begin
sv:=NSView(tabs.subviews.objectAtIndex(i));
if not Assigned(sv)
or (sv = tabs.nextarr)
or (sv = tabs.prevarr)
or (sv.isKindOfClass(TCocoaTabPageView))
then Continue;
f := sv.frame;
if tabs.isFlipped then
r := NSRectToRect( f )
else
NSToLCLRect( f, tabs.frame.size.height, r );
Result := true;
Exit;
end;
Result:=false;
end;
function AllocArrowButton(isPrev: Boolean): NSButton;
var
btn : NSButton;

View File

@ -90,6 +90,7 @@ type
//class function GetNotebookMinTabWidth(const AWinControl: TWinControl): integer; override;
//class function GetPageRealIndex(const ATabControl: TCustomTabControl; AIndex: Integer): Integer; override;
class function GetTabIndexAtPos(const ATabControl: TCustomTabControl; const AClientPos: TPoint): integer; override;
class function GetTabRect(const ATabControl: TCustomTabControl; const AIndex: Integer): TRect; override;
class procedure SetPageIndex(const ATabControl: TCustomTabControl; const AIndex: integer); override;
class procedure SetTabPosition(const ATabControl: TCustomTabControl; const ATabPosition: TTabPosition); override;
class procedure ShowTabs(const ATabControl: TCustomTabControl; AShowTabs: boolean); override;
@ -787,6 +788,58 @@ begin
Result := lTabControl.exttabIndexOfTabViewItem(lTabPage);
end;
class function TCocoaWSCustomTabControl.GetTabRect(
const ATabControl: TCustomTabControl; const AIndex: Integer): TRect;
var
lTabControl: TCocoaTabControl;
lTabPage: NSTabViewItem;
tb : TCocoaTabPageView;
i : integer;
idx : integer;
tr : TRect;
w : array of Double;
mw : Double;
ofs : Double; // aprx offset between label and the text (from both sides)
x : Double;
begin
Result:=inherited GetTabRect(ATabControl, AIndex);
if not Assigned(ATabControl) or not ATabControl.HandleAllocated then Exit;
lTabControl := TCocoaTabControl(ATabControl.Handle);
// unable to determine the rectangle view
if (AIndex<0) or (AIndex>=ATabControl.PageCount) then Exit;
tb := TCocoaTabPageView(ATabControl.Page[AIndex].Handle);
if not Assigned(tb) then Exit;
idx := lTabControl.tabViewItems.indexOfObject( tb.tabPage );
if (idx = Integer(NSNotFound)) then Exit;
if not GetTabsRect(lTabControl, tr) then Exit;
SetLength(w, lTabControl.tabViewItems.count);
if (length(w) = 0) then Exit; // no tabs!
mw := 0;
for i:=0 to Integer(lTabControl.tabViewItems.count)-1 do
begin
lTabPage := lTabControl.tabViewItemAtIndex(i);
w[i] := lTabPage.sizeOfLabel(false).width;
mw := mw + w[i];
end;
if (mw = 0) then Exit; // 0 for the total tabs width?
ofs := (tr.Right - tr.Left - mw) / length(w);
x := tr.Left;
for i:=0 to idx-1 do
x:=x+ofs+w[i];
Result.Left := Round(x);
Result.Top := tr.Top;
Result.Right := Round(Result.Left + w[idx]);
Result.Bottom := tr.Bottom;
end;
class procedure TCocoaWSCustomTabControl.SetPageIndex(const ATabControl: TCustomTabControl; const AIndex: integer);
var
i : NSInteger;