1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "basebmp/scanlineformats.hxx"
21 #include "basebmp/color.hxx"
23 #include "basegfx/range/b2drectangle.hxx"
24 #include "basegfx/range/b2irange.hxx"
25 #include "basegfx/vector/b2ivector.hxx"
26 #include "basegfx/polygon/b2dpolygon.hxx"
27 #include "basegfx/polygon/b2dpolygontools.hxx"
29 #include "vcl/svapp.hxx"
31 #include "quartz/salgdi.h"
32 #include "quartz/utils.h"
33 #include "osx/salframe.h"
34 #include "osx/saldata.hxx"
36 void AquaSalGraphics::SetWindowGraphics( AquaSalFrame
* pFrame
)
45 void AquaSalGraphics::SetPrinterGraphics( CGContextRef xContext
, long nDPIX
, long nDPIY
)
55 // a previously set clip path is now invalid
58 CGPathRelease( mxClipPath
);
64 CGContextSetFillColorSpace( mrContext
, GetSalData()->mxRGBSpace
);
65 CGContextSetStrokeColorSpace( mrContext
, GetSalData()->mxRGBSpace
);
66 CGContextSaveGState( mrContext
);
71 void AquaSalGraphics::InvalidateContext()
77 void AquaSalGraphics::UnsetState()
81 CG_TRACE( "CGContextRestoreGState(" << mrContext
<< ")" );
82 CGContextRestoreGState( mrContext
);
87 CG_TRACE( "CGPathRelease(" << mxClipPath
<< ")" );
88 CGPathRelease( mxClipPath
);
93 bool AquaSalGraphics::CheckContext()
95 if( mbWindow
&& mpFrame
&& mpFrame
->getNSWindow() )
97 const unsigned int nWidth
= mpFrame
->maGeometry
.nWidth
;
98 const unsigned int nHeight
= mpFrame
->maGeometry
.nHeight
;
100 CGContextRef rReleaseContext
= 0;
101 CGLayerRef rReleaseLayer
= NULL
;
103 // check if a new drawing context is needed (e.g. after a resize)
104 if( (unsigned(mnWidth
) != nWidth
) || (unsigned(mnHeight
) != nHeight
) )
108 // prepare to release the corresponding resources
109 rReleaseContext
= mrContext
;
110 rReleaseLayer
= mxLayer
;
117 const CGSize aLayerSize
= { static_cast<CGFloat
>(nWidth
), static_cast<CGFloat
>(nHeight
) };
118 NSGraphicsContext
* pNSGContext
= [NSGraphicsContext graphicsContextWithWindow
: mpFrame
->getNSWindow()];
119 CGContextRef xCGContext
= reinterpret_cast<CGContextRef
>([pNSGContext graphicsPort
]);
120 mxLayer
= CGLayerCreateWithContext( xCGContext
, aLayerSize
, NULL
);
121 CG_TRACE( "CGLayerCreateWithContext(" << xCGContext
<< "," << aLayerSize
<< ",NULL) = " << mxLayer
);
124 mrContext
= CGLayerGetContext( mxLayer
);
125 CG_TRACE( "CGLayerGetContext(" << mxLayer
<< ") = " << mrContext
);
130 // copy original layer to resized layer
133 CG_TRACE( "CGContextDrawLayerAtPoint(" << mrContext
<< "," << CGPointZero
<< "," << rReleaseLayer
<< ")" );
134 CGContextDrawLayerAtPoint( mrContext
, CGPointZero
, rReleaseLayer
);
137 CGContextTranslateCTM( mrContext
, 0, nHeight
);
138 CGContextScaleCTM( mrContext
, 1.0, -1.0 );
139 CGContextSetFillColorSpace( mrContext
, GetSalData()->mxRGBSpace
);
140 CGContextSetStrokeColorSpace( mrContext
, GetSalData()->mxRGBSpace
);
141 CG_TRACE( "CGContextSaveGState(" << mrContext
<< ") " << ++mnContextStackDepth
);
142 CGContextSaveGState( mrContext
);
145 // re-enable XOR emulation for the new context
147 mpXorEmulation
->SetTarget( mnWidth
, mnHeight
, mnBitmapDepth
, mrContext
, mxLayer
);
153 CG_TRACE( "CGLayerRelease(" << rReleaseLayer
<< ")" );
154 CGLayerRelease( rReleaseLayer
);
156 else if( rReleaseContext
)
158 CG_TRACE( "CGContextRelease(" << rReleaseContext
<< ")" );
159 CGContextRelease( rReleaseContext
);
163 DBG_ASSERT( mrContext
|| mbPrinter
, "<<<WARNING>>> AquaSalGraphics::CheckContext() FAILED!!!!\n" );
164 return (mrContext
!= NULL
);
167 CGContextRef
AquaSalGraphics::GetContext()
176 void AquaSalGraphics::RefreshRect(float lX
, float lY
, float lWidth
, float lHeight
)
178 if( ! mbWindow
) // view only on Window graphics
183 // update a little more around the designated rectangle
184 // this helps with antialiased rendering
185 const Rectangle
aVclRect(Point(static_cast<long int>(lX
-1),
186 static_cast<long int>(lY
-1) ),
187 Size( static_cast<long int>(lWidth
+2),
188 static_cast<long int>(lHeight
+2) ) );
189 mpFrame
->maInvalidRect
.Union( aVclRect
);
193 void AquaSalGraphics::UpdateWindow( NSRect
& )
197 NSGraphicsContext
* pContext
= [NSGraphicsContext currentContext
];
198 if( (mxLayer
!= NULL
) && (pContext
!= NULL
) )
200 CGContextRef rCGContext
= reinterpret_cast<CGContextRef
>([pContext graphicsPort
]);
201 CG_TRACE( "[[NSGraphicsContext currentContext] graphicsPort] = " << rCGContext
);
203 CGMutablePathRef rClip
= mpFrame
->getClipPath();
206 CGContextSaveGState( rCGContext
);
207 CG_TRACE( "CGContextBeginPath(" << rCGContext
<< ")" );
208 CGContextBeginPath( rCGContext
);
209 CG_TRACE( "CGContextAddPath(" << rCGContext
<< "," << rClip
<< ")" );
210 CGContextAddPath( rCGContext
, rClip
);
211 CG_TRACE( "CGContextClip(" << rCGContext
<< ")" );
212 CGContextClip( rCGContext
);
216 CG_TRACE( "CGContextDrawLayerAtPoint(" << rCGContext
<< "," << CGPointZero
<< "," << mxLayer
<< ")" );
217 CGContextDrawLayerAtPoint( rCGContext
, CGPointZero
, mxLayer
);
218 if( rClip
) // cleanup clipping
219 CGContextRestoreGState( rCGContext
);
222 DBG_ASSERT( mpFrame
->mbInitShow
, "UpdateWindow called on uneligible graphics" );
225 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */