1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 * ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is Oracle Corporation code.
17 * The Initial Developer of the Original Code is Oracle Corporation.
18 * Portions created by the Initial Developer are Copyright (C) 2005
19 * the Initial Developer. All Rights Reserved.
22 * Stuart Parmenter <pavlov@pavlov.net>
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 #include "gfxWindowsSurface.h"
39 #include "gfxContext.h"
40 #include "gfxPlatform.h"
43 #include "cairo-win32.h"
47 gfxWindowsSurface::gfxWindowsSurface(HWND wnd
) :
48 mOwnsDC(PR_TRUE
), mForPrinting(PR_FALSE
), mWnd(wnd
)
51 Init(cairo_win32_surface_create(mDC
));
54 gfxWindowsSurface::gfxWindowsSurface(HDC dc
, PRUint32 flags
) :
55 mOwnsDC(PR_FALSE
), mForPrinting(PR_FALSE
), mDC(dc
), mWnd(nsnull
)
57 if (flags
& FLAG_TAKE_DC
)
61 if (flags
& FLAG_FOR_PRINTING
) {
62 Init(cairo_win32_printing_surface_create(mDC
));
63 mForPrinting
= PR_TRUE
;
66 Init(cairo_win32_surface_create(mDC
));
69 gfxWindowsSurface::gfxWindowsSurface(const gfxIntSize
& size
, gfxImageFormat imageFormat
) :
70 mOwnsDC(PR_FALSE
), mForPrinting(PR_FALSE
), mWnd(nsnull
)
72 if (!CheckSurfaceSize(size
))
75 cairo_surface_t
*surf
= cairo_win32_surface_create_with_dib((cairo_format_t
)imageFormat
,
76 size
.width
, size
.height
);
79 if (CairoStatus() == 0)
80 mDC
= cairo_win32_surface_get_dc(CairoSurface());
85 gfxWindowsSurface::gfxWindowsSurface(HDC dc
, const gfxIntSize
& size
, gfxImageFormat imageFormat
) :
86 mOwnsDC(PR_FALSE
), mForPrinting(PR_FALSE
), mWnd(nsnull
)
88 if (!CheckSurfaceSize(size
))
91 cairo_surface_t
*surf
= cairo_win32_surface_create_with_ddb(dc
, (cairo_format_t
)imageFormat
,
92 size
.width
, size
.height
);
95 if (CairoStatus() == 0)
96 mDC
= cairo_win32_surface_get_dc(CairoSurface());
102 gfxWindowsSurface::gfxWindowsSurface(cairo_surface_t
*csurf
) :
103 mOwnsDC(PR_FALSE
), mForPrinting(PR_FALSE
), mWnd(nsnull
)
105 if (cairo_surface_status(csurf
) == 0)
106 mDC
= cairo_win32_surface_get_dc(csurf
);
110 if (cairo_surface_get_type(csurf
) == CAIRO_SURFACE_TYPE_WIN32_PRINTING
)
111 mForPrinting
= PR_TRUE
;
113 Init(csurf
, PR_TRUE
);
116 gfxWindowsSurface::~gfxWindowsSurface()
120 ::ReleaseDC(mWnd
, mDC
);
126 already_AddRefed
<gfxImageSurface
>
127 gfxWindowsSurface::GetImageSurface()
129 if (!mSurfaceValid
) {
130 NS_WARNING ("GetImageSurface on an invalid (null) surface; who's calling this without checking for surface errors?");
134 NS_ASSERTION(CairoSurface() != nsnull
, "CairoSurface() shouldn't be nsnull when mSurfaceValid is TRUE!");
139 cairo_surface_t
*isurf
= cairo_win32_surface_get_image(CairoSurface());
143 nsRefPtr
<gfxASurface
> asurf
= gfxASurface::Wrap(isurf
);
144 gfxImageSurface
*imgsurf
= (gfxImageSurface
*) asurf
.get();
149 already_AddRefed
<gfxWindowsSurface
>
150 gfxWindowsSurface::OptimizeToDDB(HDC dc
, const gfxIntSize
& size
, gfxImageFormat format
)
155 if (format
!= ImageFormatRGB24
)
158 nsRefPtr
<gfxWindowsSurface
> wsurf
= new gfxWindowsSurface(dc
, size
, format
);
159 if (wsurf
->CairoStatus() != 0)
162 gfxContext
tmpCtx(wsurf
);
163 tmpCtx
.SetOperator(gfxContext::OPERATOR_SOURCE
);
164 tmpCtx
.SetSource(this);
167 gfxWindowsSurface
*raw
= (gfxWindowsSurface
*) (wsurf
.get());
172 nsresult
gfxWindowsSurface::BeginPrinting(const nsAString
& aTitle
,
173 const nsAString
& aPrintToFileName
)
175 #define DOC_TITLE_LENGTH 30
178 nsString
titleStr(aTitle
);
179 if (titleStr
.Length() > DOC_TITLE_LENGTH
) {
180 titleStr
.SetLength(DOC_TITLE_LENGTH
-3);
181 titleStr
.AppendLiteral("...");
184 nsString
docName(aPrintToFileName
);
185 docinfo
.cbSize
= sizeof(docinfo
);
186 docinfo
.lpszDocName
= titleStr
.Length() > 0 ? titleStr
.get() : L
"Mozilla Document";
187 docinfo
.lpszOutput
= docName
.Length() > 0 ? docName
.get() : nsnull
;
188 docinfo
.lpszDatatype
= NULL
;
191 ::StartDocW(mDC
, &docinfo
);
196 nsresult
gfxWindowsSurface::EndPrinting()
198 int result
= ::EndDoc(mDC
);
200 return NS_ERROR_FAILURE
;
205 nsresult
gfxWindowsSurface::AbortPrinting()
207 int result
= ::AbortDoc(mDC
);
209 return NS_ERROR_FAILURE
;
213 nsresult
gfxWindowsSurface::BeginPage()
215 int result
= ::StartPage(mDC
);
217 return NS_ERROR_FAILURE
;
221 nsresult
gfxWindowsSurface::EndPage()
224 cairo_surface_show_page(CairoSurface());
225 int result
= ::EndPage(mDC
);
227 return NS_ERROR_FAILURE
;
231 PRInt32
gfxWindowsSurface::GetDefaultContextFlags() const
234 return gfxContext::FLAG_SIMPLIFY_OPERATORS
|
235 gfxContext::FLAG_DISABLE_SNAPPING
;