From fda9deecd3200a13b571f918580ded13ce679ac8 Mon Sep 17 00:00:00 2001 From: ondrej Date: Sun, 13 May 2018 20:57:16 +0000 Subject: [PATCH] Cocoa: TBitBtn retina support. Issue #33713 git-svn-id: trunk@57928 - --- lcl/interfaces/cocoa/cocoa_extra.pas | 9 +++++++++ lcl/interfaces/cocoa/cocoawsbuttons.pp | 12 ++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lcl/interfaces/cocoa/cocoa_extra.pas b/lcl/interfaces/cocoa/cocoa_extra.pas index 764c21e17a..535ed332da 100644 --- a/lcl/interfaces/cocoa/cocoa_extra.pas +++ b/lcl/interfaces/cocoa/cocoa_extra.pas @@ -26,6 +26,14 @@ uses // Libs MacOSAll, CocoaAll; +type + NSImageScaling = NSUInteger; +const // NSImageScaling values + NSImageScaleProportionallyDown = 0; + NSImageScaleAxesIndependently = 1; + NSImageScaleNone = 2; + NSImageScaleProportionallyUpOrDown = 3; + type NSMenuFix = objccategory external (NSMenu) function itemAtIndex(index: NSInteger): NSMenuItem; message 'itemAtIndex:'; @@ -38,6 +46,7 @@ type NSButtonSoundExtensionsCategory = objccategory external (NSButton) function intrinsicContentSize(): NSSize; message 'intrinsicContentSize'; + procedure setImageScaling(aScaling: NSImageScaling); message 'setImageScaling:'; end; // The following dummy categories fix bugs in the Cocoa bindings available in FPC diff --git a/lcl/interfaces/cocoa/cocoawsbuttons.pp b/lcl/interfaces/cocoa/cocoawsbuttons.pp index 6539fc0a57..d51d74c75f 100644 --- a/lcl/interfaces/cocoa/cocoawsbuttons.pp +++ b/lcl/interfaces/cocoa/cocoawsbuttons.pp @@ -125,6 +125,7 @@ var AIndex: Integer; AEffect: TGraphicsDrawEffect; AImgRes: TScaledImageListResolution; + ImgSize: NSSize; begin //WriteLn('[TCocoaWSBitBtn.SetGlyph]'); Img := nil; @@ -132,13 +133,20 @@ begin begin AGlyph := TBitmap.Create; AValue.GetImageIndexAndEffect(bsUp, ABitBtn.Font.PixelsPerInch, - 1{To-Do: ABitBtn.GetCanvasScaleFactor}, AImgRes, AIndex, AEffect); + ABitBtn.GetCanvasScaleFactor, AImgRes, AIndex, AEffect); AImgRes.GetBitmap(AIndex, AGlyph, AEffect); Img := TCocoaBitmap(AGlyph.Handle).image; + if AImgRes.Resolution.ImageList.Scaled then // resize only if the image list is scaled + begin + ImgSize := Img.size; + ImgSize.height := ImgSize.height / ABitBtn.GetCanvasScaleFactor; + ImgSize.width := ImgSize.width / ABitBtn.GetCanvasScaleFactor; + Img.setSize(ImgSize); + end; lButtonHandle := TCocoaButton(ABitBtn.Handle); lButtonHandle.setImage(Img); lButtonHandle.setImagePosition(LCLGlyphPosToCocoa(ABitBtn.Layout)); - //To-Do: tell Cocoa lButtonHandle should not scale the image (and enable ABitBtn.GetCanvasScaleFactor above) + lButtonHandle.setImageScaling(NSImageScaleNone); // do not scale - retina scaling is done above with Img.setSize if Assigned(lButtonHandle.Glyph) then FreeAndNil(lButtonHandle.Glyph); lButtonHandle.Glyph := AGlyph;