Cocoa: Add background color feature for TWinControls

git-svn-id: trunk@43840 -
This commit is contained in:
freq 2014-01-29 20:05:30 +00:00
parent 4bddd1118e
commit 50408a84b9
4 changed files with 61 additions and 6 deletions

View File

@ -377,6 +377,7 @@ type
procedure Polygon(const Points: array of TPoint; NumPts: Integer; Winding: boolean);
procedure Polyline(const Points: array of TPoint; NumPts: Integer);
procedure Rectangle(X1, Y1, X2, Y2: Integer; FillRect: Boolean; UseBrush: TCocoaBrush);
procedure BackgroundFill(dirtyRect:NSRect);
procedure Ellipse(X1, Y1, X2, Y2: Integer);
procedure TextOut(X, Y: Integer; Options: Longint; Rect: PRect; UTF8Chars: PChar; Count: Integer; CharsDelta: PInteger);
procedure Frame(const R: TRect);
@ -631,21 +632,27 @@ begin
end;
procedure TCocoaColorObject.GetRGBA(AROP2: Integer; out AR, AG, AB, AA: Single);
var alpha:single;
begin
if FA then
alpha:=1
else
alpha:=0;
case AROP2 of
R2_BLACK:
begin
AR := 0;
AG := 0;
AB := 0;
AA := Byte(FA);
AA := alpha;
end;
R2_WHITE:
begin
AR := 1;
AG := 1;
AB := 1;
AA := Byte(FA);
AA := alpha;
end;
R2_NOP:
begin
@ -659,21 +666,21 @@ begin
AR := 1;
AG := 1;
AB := 1;
AA := Byte(FA);
AA := alpha;
end;
R2_NOTCOPYPEN:
begin
AR := (255 - FR) / 255;
AG := (255 - FG) / 255;
AB := (255 - FB) / 255;
AA := Byte(FA);
AA := alpha;
end;
else // copy
begin
AR := FR / 255;
AG := FG / 255;
AB := FB / 255;
AA := Byte(FA);
AA := alpha;
end;
end;
end;
@ -964,7 +971,6 @@ end;
constructor TCocoaTextLayout.Create;
var
S: NSString;
LocalPool: NSAutoReleasePool;
begin
inherited Create;
@ -1649,6 +1655,20 @@ begin
CGContextStrokePath(cg);
end;
procedure TCocoaContext.BackgroundFill(dirtyRect:NSRect);
var
cg: CGContextRef;
begin
cg := CGContext;
if not Assigned(cg) then Exit;
FBkBrush.Apply(Self);
CGContextFillRect(cg,CGRect(dirtyRect));
end;
procedure TCocoaContext.Ellipse(X1, Y1, X2, Y2:Integer);
var
cg: CGContextRef;

View File

@ -55,9 +55,12 @@ type
function GetHasCaret: Boolean;
function GetCallbackObject: TObject;
procedure SetHasCaret(AValue: Boolean);
function GetIsOpaque: Boolean;
procedure SetIsOpaque(AValue: Boolean);
// properties
property HasCaret: Boolean read GetHasCaret write SetHasCaret;
property IsOpaque: Boolean read GetIsOpaque write SetIsOpaque;
end;
{ LCLObjectExtension }
@ -90,6 +93,7 @@ type
{ LCLViewExtension }
LCLViewExtension = objccategory(NSView)
function lclInitWithCreateParams(const AParams: TCreateParams): id; message 'lclInitWithCreateParams:';
function lclIsVisible: Boolean; message 'lclIsVisible'; reintroduce;

View File

@ -28,8 +28,11 @@ type
FHasCaret: Boolean;
FTarget: TWinControl;
FBoundsReportedToChildren: boolean;
FIsOpaque:boolean;
function GetHasCaret: Boolean;
procedure SetHasCaret(AValue: Boolean);
function GetIsOpaque: Boolean;
procedure SetIsOpaque(AValue: Boolean);
protected
class function CocoaModifiersToKeyState(AModifiers: NSUInteger): PtrInt; static;
class function CocoaPressedMouseButtonsToKeyState(AMouseButtons: NSUInteger): PtrInt; static;
@ -59,6 +62,7 @@ type
property HasCaret: Boolean read GetHasCaret write SetHasCaret;
property Target: TWinControl read FTarget;
property IsOpaque: Boolean read GetIsOpaque write SetIsOpaque;
end;
TLCLCommonCallBackClass = class of TLCLCommonCallBack;
@ -80,6 +84,7 @@ type
class procedure SetBounds(const AWinControl: TWinControl; const ALeft, ATop, AWidth, AHeight: Integer); override;
class procedure SetCursor(const AWinControl: TWinControl; const ACursor: HCursor); override;
class procedure SetFont(const AWinControl: TWinControl; const AFont: TFont); override;
class procedure SetColor(const AWinControl: TWinControl); override;
class procedure ShowHide(const AWinControl: TWinControl); override;
class procedure Invalidate(const AWinControl: TWinControl); override;
end;
@ -242,6 +247,7 @@ begin
FPropStorage.Sorted := True;
FPropStorage.Duplicates := dupAccept;
FBoundsReportedToChildren:=false;
FIsOpaque:=false;
end;
destructor TLCLCommonCallback.Destroy;
@ -871,6 +877,7 @@ procedure TLCLCommonCallback.Draw(ControlContext: NSGraphicsContext;
const bounds, dirty: NSRect);
var
PS: PPaintStruct;
r: TRect;
begin
// todo: think more about draw call while previous draw still active
if Assigned(FContext) then
@ -879,6 +886,13 @@ begin
try
if FContext.InitDraw(Round(bounds.size.width), Round(bounds.size.height)) then
begin
if FIsOpaque then
begin
FContext.BkMode:=OPAQUE;
FContext.BkColor:=Target.Color;
FContext.BackgroundFill(dirty);
end;
New(PS);
try
FillChar(PS^, SizeOf(TPaintStruct), 0);
@ -923,6 +937,16 @@ begin
end;
end;
function TLCLCommonCallback.GetIsOpaque: Boolean;
begin
Result:= FIsOpaque;
end;
procedure TLCLCommonCallback.SetIsOpaque(AValue: Boolean);
begin
FIsOpaque:=AValue;
end;
{ TCocoaWSWinControl }
class function TCocoaWSWinControl.CreateHandle(const AWinControl: TWinControl;
@ -1111,6 +1135,11 @@ begin
end;
end;
class procedure TCocoaWSWinControl.SetColor(const AWinControl: TWinControl);
begin
invalidate(AWinControl);
end;
class procedure TCocoaWSWinControl.ShowHide(const AWinControl: TWinControl);
begin
if AWinControl.HandleAllocated then

View File

@ -401,6 +401,7 @@ var
cnt.callback := TCocoaWindow(win).callback;
cnt.callback.IsOpaque:=true;
win.setContentView(cnt);
if (AParams.WndParent <> 0) then
@ -418,6 +419,7 @@ var
begin
NSView(APArams.WndParent).addSubView(cnt);
cnt.window.setAcceptsMouseMovedEvents(True);
cnt.callback.IsOpaque:=true;
end;
end;