2 * Copyright (C) 2007 Kevin Ollivier <kevino@theolliviers.com>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "GraphicsContext.h"
29 #include "AffineTransform.h"
30 #include "FloatRect.h"
33 #include "NotImplemented.h"
34 #include <wtf/MathExtras.h>
40 #include <wx/window.h>
41 #include <wx/dcclient.h>
42 #include <wx/dcgraph.h>
43 #include <wx/graphics.h>
46 #include <Carbon/Carbon.h>
53 int getWxCompositingOperation(CompositeOperator op, bool hasAlpha)
55 // FIXME: Add support for more operators.
56 if (op == CompositeSourceOver && !hasAlpha)
72 static int strokeStyleToWxPenStyle(int p)
76 if (p == DottedStroke)
78 if (p == DashedStroke)
86 class GraphicsContextPlatformPrivate {
88 GraphicsContextPlatformPrivate();
89 ~GraphicsContextPlatformPrivate();
97 wxRegion gtkCurrentClipRgn;
98 wxRegion gtkPaintClipRgn;
101 GraphicsContextPlatformPrivate::GraphicsContextPlatformPrivate() :
104 gtkCurrentClipRgn(wxRegion()),
105 gtkPaintClipRgn(wxRegion())
109 GraphicsContextPlatformPrivate::~GraphicsContextPlatformPrivate()
114 void GraphicsContext::platformInit(PlatformGraphicsContext* context)
116 m_data = new GraphicsContextPlatformPrivate;
117 setPaintingDisabled(!context);
120 // Make sure the context starts in sync with our state.
121 setPlatformFillColor(fillColor(), ColorSpaceDeviceRGB);
122 setPlatformStrokeColor(strokeColor(), ColorSpaceDeviceRGB);
125 m_data->context = (wxGCDC*)context;
127 m_data->context = (wxWindowDC*)context;
131 void GraphicsContext::platformDestroy()
136 PlatformGraphicsContext* GraphicsContext::platformContext() const
138 return (PlatformGraphicsContext*)m_data->context;
141 void GraphicsContext::savePlatformState()
146 wxGraphicsContext* gc = m_data->context->GetGraphicsContext();
149 // when everything is working with USE_WXGC, we can remove this
151 CGContextRef context;
152 wxGraphicsContext* gc = m_data->context->GetGraphicsContext();
154 context = (CGContextRef)gc->GetNativeContext();
156 CGContextSaveGState(context);
158 HDC dc = (HDC)m_data->context->GetHDC();
159 m_data->mswDCStateID = ::SaveDC(dc);
161 m_data->gtkCurrentClipRgn = m_data->context->m_currentClippingRegion;
162 m_data->gtkPaintClipRgn = m_data->context->m_paintClippingRegion;
168 void GraphicsContext::restorePlatformState()
173 wxGraphicsContext* gc = m_data->context->GetGraphicsContext();
177 CGContextRef context;
178 wxGraphicsContext* gc = m_data->context->GetGraphicsContext();
180 context = (CGContextRef)gc->GetNativeContext();
182 CGContextRestoreGState(context);
184 HDC dc = (HDC)m_data->context->GetHDC();
185 ::RestoreDC(dc, m_data->mswDCStateID);
187 m_data->context->m_currentClippingRegion = m_data->gtkCurrentClipRgn;
188 m_data->context->m_paintClippingRegion = m_data->gtkPaintClipRgn;
195 // Draws a filled rectangle with a stroked border.
196 void GraphicsContext::drawRect(const IntRect& rect)
198 if (paintingDisabled())
201 m_data->context->SetPen(wxPen(strokeColor(), strokeThickness(), strokeStyleToWxPenStyle(strokeStyle())));
202 m_data->context->DrawRectangle(rect.x(), rect.y(), rect.width(), rect.height());
205 // This is only used to draw borders.
206 void GraphicsContext::drawLine(const IntPoint& point1, const IntPoint& point2)
208 if (paintingDisabled())
211 FloatPoint p1 = point1;
212 FloatPoint p2 = point2;
214 m_data->context->SetPen(wxPen(strokeColor(), strokeThickness(), strokeStyleToWxPenStyle(strokeStyle())));
215 m_data->context->DrawLine(point1.x(), point1.y(), point2.x(), point2.y());
218 // This method is only used to draw the little circles used in lists.
219 void GraphicsContext::drawEllipse(const IntRect& rect)
221 if (paintingDisabled())
224 m_data->context->SetPen(wxPen(strokeColor(), strokeThickness(), strokeStyleToWxPenStyle(strokeStyle())));
225 m_data->context->DrawEllipse(rect.x(), rect.y(), rect.width(), rect.height());
228 void GraphicsContext::strokeArc(const IntRect& rect, int startAngle, int angleSpan)
230 if (paintingDisabled())
233 m_data->context->SetPen(wxPen(strokeColor(), strokeThickness(), strokeStyleToWxPenStyle(strokeStyle())));
234 m_data->context->DrawEllipticArc(rect.x(), rect.y(), rect.width(), rect.height(), startAngle, angleSpan);
237 void GraphicsContext::drawConvexPolygon(size_t npoints, const FloatPoint* points, bool shouldAntialias)
239 if (paintingDisabled())
245 wxPoint* polygon = new wxPoint[npoints];
246 for (size_t i = 0; i < npoints; i++)
247 polygon[i] = wxPoint(points[i].x(), points[i].y());
248 m_data->context->SetPen(wxPen(strokeColor(), strokeThickness(), strokeStyleToWxPenStyle(strokeStyle())));
249 m_data->context->DrawPolygon((int)npoints, polygon);
253 void GraphicsContext::clipConvexPolygon(size_t numPoints, const FloatPoint* points, bool antialiased)
255 if (paintingDisabled())
261 // FIXME: IMPLEMENT!!
264 void GraphicsContext::fillRect(const FloatRect& rect, const Color& color, ColorSpace colorSpace)
266 if (paintingDisabled())
271 m_data->context->SetPen(*wxTRANSPARENT_PEN);
272 m_data->context->SetBrush(wxBrush(color));
273 m_data->context->DrawRectangle(rect.x(), rect.y(), rect.width(), rect.height());
275 restorePlatformState();
278 void GraphicsContext::fillRoundedRect(const IntRect& rect, const IntSize& topLeft, const IntSize& topRight, const IntSize& bottomLeft, const IntSize& bottomRight, const Color& color, ColorSpace colorSpace)
280 if (paintingDisabled())
286 void GraphicsContext::drawFocusRing(const Path& path, int width, int offset, const Color& color)
291 void GraphicsContext::drawFocusRing(const Vector<IntRect>& rects, int width, int offset, const Color& color)
293 if (paintingDisabled())
299 void GraphicsContext::clip(const FloatRect& r)
301 wxWindowDC* windc = dynamic_cast<wxWindowDC*>(m_data->context);
305 #if !defined(__WXGTK__) || wxCHECK_VERSION(2,9,0)
306 wxWindow* window = windc->GetWindow();
308 wxWindow* window = windc->m_owner;
311 wxWindow* parent = window->GetParent();
312 // we need to convert from WebView "global" to WebFrame "local" coords.
313 // FIXME: We only want to go to the top WebView.
315 pos += window->GetPosition();
316 parent = parent->GetParent();
321 m_data->context->SetClippingRegion(r.x() - pos.x, r.y() - pos.y, r.width() + pos.x, r.height() + pos.y);
324 void GraphicsContext::clipOut(const Path&)
329 void GraphicsContext::clipOut(const IntRect&)
334 void GraphicsContext::clipPath(const Path&, WindRule)
339 void GraphicsContext::drawLineForText(const FloatPoint& origin, float width, bool printing)
341 if (paintingDisabled())
344 FloatPoint endPoint = origin + FloatSize(width, 0);
345 m_data->context->SetPen(wxPen(strokeColor(), strokeThickness(), wxSOLID));
346 m_data->context->DrawLine(origin.x(), origin.y(), endPoint.x(), endPoint.y());
349 void GraphicsContext::drawLineForTextChecking(const FloatPoint& origin, float width, TextCheckingLineStyle style)
352 case TextCheckingSpellingLineStyle:
353 m_data->context->SetPen(wxPen(*wxRED, 2, wxLONG_DASH));
355 case TextCheckingGrammarLineStyle:
356 m_data->context->SetPen(wxPen(*wxGREEN, 2, wxLONG_DASH));
361 m_data->context->DrawLine(origin.x(), origin.y(), origin.x() + width, origin.y());
364 void GraphicsContext::clip(const Path&)
369 void GraphicsContext::canvasClip(const Path& path)
374 AffineTransform GraphicsContext::getCTM() const
377 wxGraphicsContext* gc = m_data->context->GetGraphicsContext();
379 wxGraphicsMatrix matrix = gc->GetTransform();
380 double a, b, c, d, e, f;
381 matrix.Get(&a, &b, &c, &d, &e, &f);
382 return AffineTransform(a, b, c, d, e, f);
385 return AffineTransform();
388 void GraphicsContext::translate(float tx, float ty)
391 if (m_data->context) {
392 wxGraphicsContext* gc = m_data->context->GetGraphicsContext();
393 gc->Translate(tx, ty);
398 void GraphicsContext::rotate(float angle)
401 if (m_data->context) {
402 wxGraphicsContext* gc = m_data->context->GetGraphicsContext();
408 void GraphicsContext::scale(const FloatSize& scale)
411 if (m_data->context) {
412 wxGraphicsContext* gc = m_data->context->GetGraphicsContext();
413 gc->Scale(scale.width(), scale.height());
419 FloatRect GraphicsContext::roundToDevicePixels(const FloatRect& frect)
423 wxCoord x = (wxCoord)frect.x();
424 wxCoord y = (wxCoord)frect.y();
426 x = m_data->context->LogicalToDeviceX(x);
427 y = m_data->context->LogicalToDeviceY(y);
428 result.setX((float)x);
429 result.setY((float)y);
430 x = (wxCoord)frect.width();
431 y = (wxCoord)frect.height();
432 x = m_data->context->LogicalToDeviceXRel(x);
433 y = m_data->context->LogicalToDeviceYRel(y);
434 result.setWidth((float)x);
435 result.setHeight((float)y);
439 void GraphicsContext::setURLForRect(const KURL&, const IntRect&)
444 void GraphicsContext::setPlatformCompositeOperation(CompositeOperator op)
448 #if wxCHECK_VERSION(2,9,0)
449 m_data->context->SetLogicalFunction(static_cast<wxRasterOperationMode>(getWxCompositingOperation(op, false)));
451 m_data->context->SetLogicalFunction(getWxCompositingOperation(op, false));
456 void GraphicsContext::setPlatformStrokeColor(const Color& color, ColorSpace colorSpace)
458 if (paintingDisabled())
462 m_data->context->SetPen(wxPen(color, strokeThickness(), strokeStyleToWxPenStyle(strokeStyle())));
465 void GraphicsContext::setPlatformStrokeThickness(float thickness)
467 if (paintingDisabled())
471 m_data->context->SetPen(wxPen(strokeColor(), thickness, strokeStyleToWxPenStyle(strokeStyle())));
475 void GraphicsContext::setPlatformFillColor(const Color& color, ColorSpace colorSpace)
477 if (paintingDisabled())
481 m_data->context->SetBrush(wxBrush(color));
484 void GraphicsContext::concatCTM(const AffineTransform& transform)
486 if (paintingDisabled())
490 wxGraphicsContext* gc = m_data->context->GetGraphicsContext();
492 gc->ConcatTransform(transform);
497 void GraphicsContext::setCTM(const AffineTransform& transform)
499 if (paintingDisabled())
503 wxGraphicsContext* gc = m_data->context->GetGraphicsContext();
505 gc->SetTransform(transform);
510 void GraphicsContext::setPlatformShouldAntialias(bool enable)
512 if (paintingDisabled())
517 void GraphicsContext::setImageInterpolationQuality(InterpolationQuality)
521 InterpolationQuality GraphicsContext::imageInterpolationQuality() const
523 return InterpolationDefault;
526 void GraphicsContext::fillPath(const Path& path)
529 wxGraphicsContext* gc = m_data->context->GetGraphicsContext();
531 gc->FillPath(*path.platformPath());
535 void GraphicsContext::strokePath(const Path& path)
538 wxGraphicsContext* gc = m_data->context->GetGraphicsContext();
540 gc->StrokePath(*path.platformPath());
544 void GraphicsContext::fillRect(const FloatRect& rect)
546 if (paintingDisabled())
550 void GraphicsContext::setPlatformShadow(FloatSize const&, float, Color const&, ColorSpace)
555 void GraphicsContext::clearPlatformShadow()
560 void GraphicsContext::beginTransparencyLayer(float)
565 void GraphicsContext::endTransparencyLayer()
570 void GraphicsContext::clearRect(const FloatRect&)
575 void GraphicsContext::strokeRect(const FloatRect&, float)
580 void GraphicsContext::setLineCap(LineCap)
585 void GraphicsContext::setLineDash(const DashArray&, float dashOffset)
590 void GraphicsContext::setLineJoin(LineJoin)
595 void GraphicsContext::setMiterLimit(float)
600 void GraphicsContext::setAlpha(float)
605 void GraphicsContext::addInnerRoundedRectClip(const IntRect& rect, int thickness)
611 HDC GraphicsContext::getWindowsContext(const IntRect& dstRect, bool supportAlphaBlend, bool mayCreateBitmap)
613 if (dstRect.isEmpty())
616 // Create a bitmap DC in which to draw.
617 BITMAPINFO bitmapInfo;
618 bitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
619 bitmapInfo.bmiHeader.biWidth = dstRect.width();
620 bitmapInfo.bmiHeader.biHeight = dstRect.height();
621 bitmapInfo.bmiHeader.biPlanes = 1;
622 bitmapInfo.bmiHeader.biBitCount = 32;
623 bitmapInfo.bmiHeader.biCompression = BI_RGB;
624 bitmapInfo.bmiHeader.biSizeImage = 0;
625 bitmapInfo.bmiHeader.biXPelsPerMeter = 0;
626 bitmapInfo.bmiHeader.biYPelsPerMeter = 0;
627 bitmapInfo.bmiHeader.biClrUsed = 0;
628 bitmapInfo.bmiHeader.biClrImportant = 0;
631 HBITMAP bitmap = ::CreateDIBSection(0, &bitmapInfo, DIB_RGB_COLORS, &pixels, 0, 0);
635 HDC displayDC = ::GetDC(0);
636 HDC bitmapDC = ::CreateCompatibleDC(displayDC);
637 ::ReleaseDC(0, displayDC);
639 ::SelectObject(bitmapDC, bitmap);
641 // Fill our buffer with clear if we're going to alpha blend.
642 if (supportAlphaBlend) {
644 GetObject(bitmap, sizeof(bmpInfo), &bmpInfo);
645 int bufferSize = bmpInfo.bmWidthBytes * bmpInfo.bmHeight;
646 memset(bmpInfo.bmBits, 0, bufferSize);
651 void GraphicsContext::releaseWindowsContext(HDC hdc, const IntRect& dstRect, bool supportAlphaBlend, bool mayCreateBitmap)
655 if (!dstRect.isEmpty()) {
657 HBITMAP bitmap = static_cast<HBITMAP>(GetCurrentObject(hdc, OBJ_BITMAP));
659 GetObject(bitmap, sizeof(info), &info);
660 ASSERT(info.bmBitsPixel == 32);
663 bmp.SetHBITMAP(bitmap);
664 #if !wxCHECK_VERSION(2,9,0)
665 if (supportAlphaBlend)
668 m_data->context->DrawBitmap(bmp, dstRect.x(), dstRect.y(), supportAlphaBlend);
670 ::DeleteObject(bitmap);