From 50408a84b9bb328d26b34a2ed58fa57242a97e7e Mon Sep 17 00:00:00 2001 From: freq Date: Wed, 29 Jan 2014 20:05:30 +0000 Subject: [PATCH] Cocoa: Add background color feature for TWinControls git-svn-id: trunk@43840 - --- lcl/interfaces/cocoa/cocoagdiobjects.pas | 32 +++++++++++++++++++----- lcl/interfaces/cocoa/cocoaprivate.pp | 4 +++ lcl/interfaces/cocoa/cocoawscommon.pas | 29 +++++++++++++++++++++ lcl/interfaces/cocoa/cocoawsforms.pp | 2 ++ 4 files changed, 61 insertions(+), 6 deletions(-) diff --git a/lcl/interfaces/cocoa/cocoagdiobjects.pas b/lcl/interfaces/cocoa/cocoagdiobjects.pas index c1b63fe31c..07b41759dc 100644 --- a/lcl/interfaces/cocoa/cocoagdiobjects.pas +++ b/lcl/interfaces/cocoa/cocoagdiobjects.pas @@ -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; diff --git a/lcl/interfaces/cocoa/cocoaprivate.pp b/lcl/interfaces/cocoa/cocoaprivate.pp index 408b9cf70d..f418f02503 100644 --- a/lcl/interfaces/cocoa/cocoaprivate.pp +++ b/lcl/interfaces/cocoa/cocoaprivate.pp @@ -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; diff --git a/lcl/interfaces/cocoa/cocoawscommon.pas b/lcl/interfaces/cocoa/cocoawscommon.pas index 6434d12f96..e8717c6d43 100644 --- a/lcl/interfaces/cocoa/cocoawscommon.pas +++ b/lcl/interfaces/cocoa/cocoawscommon.pas @@ -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 diff --git a/lcl/interfaces/cocoa/cocoawsforms.pp b/lcl/interfaces/cocoa/cocoawsforms.pp index f7b572923b..304077b55e 100644 --- a/lcl/interfaces/cocoa/cocoawsforms.pp +++ b/lcl/interfaces/cocoa/cocoawsforms.pp @@ -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;