Bug 460926 A11y hierachy is broken on Ubuntu 8.10 (GNOME 2.24), r=Evan.Yan sr=roc
[wine-gecko.git] / gfx / thebes / src / gfxWindowsSurface.cpp
blob8aef18482517b1e00be211de871bf02169d5ae42
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
13 * License.
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.
21 * Contributor(s):
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"
42 #include "cairo.h"
43 #include "cairo-win32.h"
45 #include "nsString.h"
47 gfxWindowsSurface::gfxWindowsSurface(HWND wnd) :
48 mOwnsDC(PR_TRUE), mForPrinting(PR_FALSE), mWnd(wnd)
50 mDC = ::GetDC(mWnd);
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)
58 mOwnsDC = PR_TRUE;
60 #ifdef NS_PRINTING
61 if (flags & FLAG_FOR_PRINTING) {
62 Init(cairo_win32_printing_surface_create(mDC));
63 mForPrinting = PR_TRUE;
64 } else
65 #endif
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))
73 return;
75 cairo_surface_t *surf = cairo_win32_surface_create_with_dib((cairo_format_t)imageFormat,
76 size.width, size.height);
77 Init(surf);
79 if (CairoStatus() == 0)
80 mDC = cairo_win32_surface_get_dc(CairoSurface());
81 else
82 mDC = nsnull;
85 gfxWindowsSurface::gfxWindowsSurface(HDC dc, const gfxIntSize& size, gfxImageFormat imageFormat) :
86 mOwnsDC(PR_FALSE), mForPrinting(PR_FALSE), mWnd(nsnull)
88 if (!CheckSurfaceSize(size))
89 return;
91 cairo_surface_t *surf = cairo_win32_surface_create_with_ddb(dc, (cairo_format_t)imageFormat,
92 size.width, size.height);
93 Init(surf);
95 if (CairoStatus() == 0)
96 mDC = cairo_win32_surface_get_dc(CairoSurface());
97 else
98 mDC = nsnull;
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);
107 else
108 mDC = nsnull;
110 if (cairo_surface_get_type(csurf) == CAIRO_SURFACE_TYPE_WIN32_PRINTING)
111 mForPrinting = PR_TRUE;
113 Init(csurf, PR_TRUE);
116 gfxWindowsSurface::~gfxWindowsSurface()
118 if (mOwnsDC) {
119 if (mWnd)
120 ::ReleaseDC(mWnd, mDC);
121 else
122 ::DeleteDC(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?");
131 return nsnull;
134 NS_ASSERTION(CairoSurface() != nsnull, "CairoSurface() shouldn't be nsnull when mSurfaceValid is TRUE!");
136 if (mForPrinting)
137 return nsnull;
139 cairo_surface_t *isurf = cairo_win32_surface_get_image(CairoSurface());
140 if (!isurf)
141 return nsnull;
143 nsRefPtr<gfxASurface> asurf = gfxASurface::Wrap(isurf);
144 gfxImageSurface *imgsurf = (gfxImageSurface*) asurf.get();
145 NS_ADDREF(imgsurf);
146 return imgsurf;
149 already_AddRefed<gfxWindowsSurface>
150 gfxWindowsSurface::OptimizeToDDB(HDC dc, const gfxIntSize& size, gfxImageFormat format)
152 if (mForPrinting)
153 return nsnull;
155 if (format != ImageFormatRGB24)
156 return nsnull;
158 nsRefPtr<gfxWindowsSurface> wsurf = new gfxWindowsSurface(dc, size, format);
159 if (wsurf->CairoStatus() != 0)
160 return nsnull;
162 gfxContext tmpCtx(wsurf);
163 tmpCtx.SetOperator(gfxContext::OPERATOR_SOURCE);
164 tmpCtx.SetSource(this);
165 tmpCtx.Paint();
167 gfxWindowsSurface *raw = (gfxWindowsSurface*) (wsurf.get());
168 NS_ADDREF(raw);
169 return raw;
172 nsresult gfxWindowsSurface::BeginPrinting(const nsAString& aTitle,
173 const nsAString& aPrintToFileName)
175 #define DOC_TITLE_LENGTH 30
176 DOCINFOW docinfo;
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;
189 docinfo.fwType = 0;
191 ::StartDocW(mDC, &docinfo);
193 return NS_OK;
196 nsresult gfxWindowsSurface::EndPrinting()
198 int result = ::EndDoc(mDC);
199 if (result <= 0)
200 return NS_ERROR_FAILURE;
202 return NS_OK;
205 nsresult gfxWindowsSurface::AbortPrinting()
207 int result = ::AbortDoc(mDC);
208 if (result <= 0)
209 return NS_ERROR_FAILURE;
210 return NS_OK;
213 nsresult gfxWindowsSurface::BeginPage()
215 int result = ::StartPage(mDC);
216 if (result <= 0)
217 return NS_ERROR_FAILURE;
218 return NS_OK;
221 nsresult gfxWindowsSurface::EndPage()
223 if (mForPrinting)
224 cairo_surface_show_page(CairoSurface());
225 int result = ::EndPage(mDC);
226 if (result <= 0)
227 return NS_ERROR_FAILURE;
228 return NS_OK;
231 PRInt32 gfxWindowsSurface::GetDefaultContextFlags() const
233 if (mForPrinting)
234 return gfxContext::FLAG_SIMPLIFY_OPERATORS |
235 gfxContext::FLAG_DISABLE_SNAPPING;
237 return 0;