Branch libreoffice-5-0-4
[LibreOffice.git] / vcl / win / source / gdi / cairo_win32_cairo.cxx
blob65dd478bd1dc96edff25fa7549e4d78ada271c7a
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 <config_cairo_canvas.h>
22 #if ENABLE_CAIRO_CANVAS
23 /************************************************************************
24 * Win32 surface backend for LibreOffice Cairo Canvas *
25 ************************************************************************/
27 #include <osl/diagnose.h>
28 #include <vcl/bitmap.hxx>
29 #include <vcl/virdev.hxx>
30 #include <vcl/sysdata.hxx>
32 #include "cairo_win32_cairo.hxx"
34 namespace cairo
37 #include <cairo-win32.h>
39 /**
40 * Surface::Surface: Create generic Canvas surface using given Cairo Surface
42 * @param pSurface Cairo Surface
44 * This constructor only stores data, it does no processing.
45 * It is used with e.g. cairo_image_surface_create_for_data()
46 * and Surface::getSimilar()
48 * Set the mpSurface to the new surface or NULL
49 **/
50 Win32Surface::Win32Surface( const CairoSurfaceSharedPtr& pSurface ) :
51 mpSurface( pSurface )
54 /**
55 * Surface::Surface: Create Canvas surface from Window reference.
56 * @param pSysData Platform native system environment data (struct SystemEnvData in vcl/inc/sysdata.hxx)
57 * @param x horizontal location of the new surface
58 * @param y vertical location of the new surface
60 * pSysData contains the platform native Window reference.
61 * pSysData is used to create a surface on the Window
63 * Set the mpSurface to the new surface or NULL
64 **/
65 Win32Surface::Win32Surface( HDC hDC, int x, int y) :
66 mpSurface(
67 cairo_win32_surface_create(hDC),
68 &cairo_surface_destroy)
70 cairo_surface_set_device_offset( mpSurface.get(), x, y );
73 /**
74 * Surface::Surface: Create platform native Canvas surface from BitmapSystemData
75 * @param pBmpData Platform native image data (struct BitmapSystemData in vcl/inc/bitmap.hxx)
77 * Create a surface based on image data on pBmpData
79 * Set the mpSurface to the new surface or NULL
80 **/
81 Win32Surface::Win32Surface( const BitmapSystemData& rBmpData ) :
82 mpSurface()
84 OSL_ASSERT(rBmpData.pDIB == NULL);
86 if(rBmpData.pDIB != NULL)
88 // So just leave mpSurface to NULL, little else we can do at
89 // this stage. Hopefully the Win32 patch to
90 // cairocanvas::DeviceHelper::getSurface(BitmapSystemData&,
91 // const Size&) will catch the cases where this
92 // constructor would be called with a DIB bitmap, and we
93 // will never get here. At least it worked for Ballmer.ppt.
95 else
97 HDC hDC = CreateCompatibleDC(NULL);
98 void* hOrigBitmap;
99 OSL_TRACE ("Surface::Surface(): Selecting bitmap %p into DC %p", rBmpData.pDDB, hDC);
100 hOrigBitmap = SelectObject( hDC, (HANDLE)rBmpData.pDDB );
101 if(hOrigBitmap == NULL)
102 OSL_TRACE ("SelectObject failed: %d", GetLastError ());
103 mpSurface.reset(
104 cairo_win32_surface_create(hDC),
105 &cairo_surface_destroy);
110 * Surface::getCairo: Create Cairo (drawing object) for the Canvas surface
112 * @return new Cairo or NULL
114 CairoSharedPtr Win32Surface::getCairo() const
116 return CairoSharedPtr( cairo_create(mpSurface.get()),
117 &cairo_destroy );
121 * Surface::getSimilar: Create new similar Canvas surface
122 * @param aContent format of the new surface (cairo_content_t from cairo/src/cairo.h)
123 * @param width width of the new surface
124 * @param height height of the new surface
126 * Creates a new Canvas surface. This normally creates platform native surface, even though
127 * generic function is used.
129 * Cairo surface from aContent (cairo_content_t)
131 * @return new surface or NULL
133 SurfaceSharedPtr Win32Surface::getSimilar( int aContent, int width, int height ) const
135 return SurfaceSharedPtr(
136 new Win32Surface(
137 CairoSurfaceSharedPtr(
138 cairo_surface_create_similar( mpSurface.get(), aContent, width, height ),
139 &cairo_surface_destroy )));
142 void Win32Surface::flush() const
144 GdiFlush();
148 * Surface::getDepth: Get the color depth of the Canvas surface.
150 * @return color depth
152 int Win32Surface::getDepth() const
154 if (mpSurface)
156 switch (cairo_surface_get_content (mpSurface.get()))
158 case CAIRO_CONTENT_ALPHA: return 8; break;
159 case CAIRO_CONTENT_COLOR: return 24; break;
160 case CAIRO_CONTENT_COLOR_ALPHA: return 32; break;
163 OSL_TRACE("Canvas::cairo::Surface::getDepth(): ERROR - depth unspecified!");
164 return -1;
169 * cairo::createVirtualDevice: Create a VCL virtual device for the CGContext in the cairo Surface
171 * @return The new virtual device
173 VclPtr<VirtualDevice> Win32Surface::createVirtualDevice() const
175 SystemGraphicsData aSystemGraphicsData;
176 aSystemGraphicsData.nSize = sizeof( SystemGraphicsData );
177 aSystemGraphicsData.hDC = cairo_win32_surface_get_dc( mpSurface.get() );
178 return VclPtr<VirtualDevice>::Create( &aSystemGraphicsData, Size(1, 1), sal::static_int_cast<USHORT>( getDepth() ) );
181 } // namespace cairo
183 #endif // #ENABLE_CAIRO_CANVAS
185 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */