Version 4.3.0.0.beta1, tag libreoffice-4.3.0.0.beta1
[LibreOffice.git] / vcl / quartz / salgdiutils.cxx
blobc25120319b740793814366cd3b3a2b450f79b0a7
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 )
38 mpFrame = pFrame;
40 mbWindow = true;
41 mbPrinter = false;
42 mbVirDev = false;
45 void AquaSalGraphics::SetPrinterGraphics( CGContextRef xContext, long nDPIX, long nDPIY )
47 mbWindow = false;
48 mbPrinter = true;
49 mbVirDev = false;
51 mrContext = xContext;
52 mnRealDPIX = nDPIX;
53 mnRealDPIY = nDPIY;
55 // a previously set clip path is now invalid
56 if( mxClipPath )
58 CGPathRelease( mxClipPath );
59 mxClipPath = NULL;
62 if( mrContext )
64 CGContextSetFillColorSpace( mrContext, GetSalData()->mxRGBSpace );
65 CGContextSetStrokeColorSpace( mrContext, GetSalData()->mxRGBSpace );
66 CGContextSaveGState( mrContext );
67 SetState();
71 void AquaSalGraphics::InvalidateContext()
73 UnsetState();
74 mrContext = 0;
77 void AquaSalGraphics::UnsetState()
79 if( mrContext )
81 CG_TRACE( "CGContextRestoreGState(" << mrContext << ")" );
82 CGContextRestoreGState( mrContext );
83 mrContext = 0;
85 if( mxClipPath )
87 CG_TRACE( "CGPathRelease(" << mxClipPath << ")" );
88 CGPathRelease( mxClipPath );
89 mxClipPath = NULL;
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) )
106 mnWidth = nWidth;
107 mnHeight = nHeight;
108 // prepare to release the corresponding resources
109 rReleaseContext = mrContext;
110 rReleaseLayer = mxLayer;
111 mrContext = NULL;
112 mxLayer = NULL;
115 if( !mrContext )
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 );
122 if( mxLayer )
124 mrContext = CGLayerGetContext( mxLayer );
125 CG_TRACE( "CGLayerGetContext(" << mxLayer << ") = " << mrContext );
128 if( mrContext )
130 // copy original layer to resized layer
131 if( rReleaseLayer )
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 );
143 SetState();
145 // re-enable XOR emulation for the new context
146 if( mpXorEmulation )
147 mpXorEmulation->SetTarget( mnWidth, mnHeight, mnBitmapDepth, mrContext, mxLayer );
151 if( rReleaseLayer )
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()
169 if(!mrContext)
171 CheckContext();
173 return mrContext;
176 void AquaSalGraphics::RefreshRect(float lX, float lY, float lWidth, float lHeight)
178 if( ! mbWindow ) // view only on Window graphics
179 return;
181 if( mpFrame )
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& )
195 if( !mpFrame )
196 return;
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();
204 if( rClip )
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 );
215 ApplyXorContext();
216 CG_TRACE( "CGContextDrawLayerAtPoint(" << rCGContext << "," << CGPointZero << "," << mxLayer << ")" );
217 CGContextDrawLayerAtPoint( rCGContext, CGPointZero, mxLayer );
218 if( rClip ) // cleanup clipping
219 CGContextRestoreGState( rCGContext );
221 else
222 DBG_ASSERT( mpFrame->mbInitShow, "UpdateWindow called on uneligible graphics" );
225 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */