1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/dfb/dc.cpp
3 // Purpose: wxDFBDCImpl class
4 // Author: Vaclav Slavik
7 // Copyright: (c) 2006 REA Elektronik GmbH
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ===========================================================================
13 // ===========================================================================
15 // ---------------------------------------------------------------------------
17 // ---------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
27 #include "wx/dcmemory.h"
31 #include "wx/dfb/dc.h"
32 #include "wx/dfb/private.h"
34 // these values are used to initialize newly created DC
35 #define DEFAULT_FONT (*wxNORMAL_FONT)
36 #define DEFAULT_PEN (*wxBLACK_PEN)
37 #define DEFAULT_BRUSH (*wxWHITE_BRUSH)
39 // ===========================================================================
41 // ===========================================================================
43 //-----------------------------------------------------------------------------
45 //-----------------------------------------------------------------------------
47 IMPLEMENT_ABSTRACT_CLASS(wxDFBDCImpl
, wxDCImpl
)
49 void wxDFBDCImpl::DFBInit(const wxIDirectFBSurfacePtr
& surface
)
53 wxCHECK_RET( surface
!= NULL
, "invalid surface" );
55 m_mm_to_pix_x
= (double)wxGetDisplaySize().GetWidth() /
56 (double)wxGetDisplaySizeMM().GetWidth();
57 m_mm_to_pix_y
= (double)wxGetDisplaySize().GetHeight() /
58 (double)wxGetDisplaySizeMM().GetHeight();
60 SetFont(DEFAULT_FONT
);
62 SetBrush(DEFAULT_BRUSH
);
66 // ---------------------------------------------------------------------------
68 // ---------------------------------------------------------------------------
70 void wxDFBDCImpl::DoSetClippingRegion(wxCoord cx
, wxCoord cy
, wxCoord cw
, wxCoord ch
)
72 wxCHECK_RET( IsOk(), wxT("invalid dc") );
74 wxSize
size(GetSize());
76 wxASSERT_MSG( !m_clipping
,
77 "narrowing clipping region not implemented yet" );
79 // NB: We intersect the clipping rectangle with surface's area here because
80 // DirectFB will return an error if you try to set clipping rectangle
81 // that is partially outside of the surface.
83 r
.x1
= wxMax(0, XLOG2DEV(cx
));
84 r
.y1
= wxMax(0, YLOG2DEV(cy
));
85 r
.x2
= wxMin(r
.x1
+ XLOG2DEVREL(cw
), size
.x
) - 1;
86 r
.y2
= wxMin(r
.y1
+ YLOG2DEVREL(ch
), size
.y
) - 1;
88 if ( !m_surface
->SetClip(&r
) )
93 m_clipX2
= cx
+ cw
- 1;
94 m_clipY2
= cy
+ ch
-1;
98 void wxDFBDCImpl::DoSetDeviceClippingRegion(const wxRegion
& region
)
100 // NB: this can be done because wxDFB only supports rectangular regions
101 wxRect rect
= region
.AsRect();
103 // our parameter is in physical coordinates while DoSetClippingRegion()
104 // takes logical ones
105 rect
.x
= XDEV2LOG(rect
.x
);
106 rect
.y
= YDEV2LOG(rect
.y
);
107 rect
.width
= XDEV2LOG(rect
.width
);
108 rect
.height
= YDEV2LOG(rect
.height
);
110 DoSetClippingRegion(rect
.x
, rect
.y
, rect
.width
, rect
.height
);
113 void wxDFBDCImpl::DestroyClippingRegion()
115 wxCHECK_RET( IsOk(), wxT("invalid dc") );
117 m_surface
->SetClip(NULL
);
122 // ---------------------------------------------------------------------------
123 // query capabilities
124 // ---------------------------------------------------------------------------
126 int wxDFBDCImpl::GetDepth() const
128 return m_surface
->GetDepth();
131 // ---------------------------------------------------------------------------
133 // ---------------------------------------------------------------------------
135 void wxDFBDCImpl::Clear()
137 wxCHECK_RET( IsOk(), wxT("invalid dc") );
139 if ( m_backgroundBrush
.GetStyle() == wxTRANSPARENT
)
142 wxColour clr
= m_backgroundBrush
.GetColour();
143 m_surface
->Clear(clr
.Red(), clr
.Green(), clr
.Blue(), clr
.Alpha());
145 wxSize
size(GetSize());
146 CalcBoundingBox(XDEV2LOG(0), YDEV2LOG(0));
147 CalcBoundingBox(XDEV2LOG(size
.x
), YDEV2LOG(size
.y
));
150 extern bool wxDoFloodFill(wxDC
*dc
, wxCoord x
, wxCoord y
,
151 const wxColour
& col
, wxFloodFillStyle style
);
153 bool wxDFBDCImpl::DoFloodFill(wxCoord x
, wxCoord y
,
154 const wxColour
& col
, wxFloodFillStyle style
)
156 return wxDoFloodFill(GetOwner(), x
, y
, col
, style
);
159 bool wxDFBDCImpl::DoGetPixel(wxCoord x
, wxCoord y
, wxColour
*col
) const
161 wxCHECK_MSG( col
, false, "NULL colour parameter in wxDFBDCImpl::GetPixel");
163 wxFAIL_MSG( "GetPixel not implemented" );
171 void wxDFBDCImpl::DoCrossHair(wxCoord x
, wxCoord y
)
173 wxCHECK_RET( IsOk(), wxT("invalid dc") );
175 wxFAIL_MSG( "CrossHair not implemented" );
181 void wxDFBDCImpl::DoDrawLine(wxCoord x1
, wxCoord y1
, wxCoord x2
, wxCoord y2
)
183 wxCHECK_RET( IsOk(), wxT("invalid dc") );
185 if ( m_pen
.GetStyle() == wxTRANSPARENT
)
188 wxCoord xx1
= XLOG2DEV(x1
);
189 wxCoord yy1
= YLOG2DEV(y1
);
190 wxCoord xx2
= XLOG2DEV(x2
);
191 wxCoord yy2
= YLOG2DEV(y2
);
193 // FIXME: DrawLine() shouldn't draw the last pixel, but DFB's DrawLine()
194 // does draw it. We should undo any change to the last pixel by
195 // using GetPixel() and PutPixel(), but until they are implemented,
196 // handle at least the special case of vertical and horizontal
202 else if ( yy1
> yy2
)
209 else if ( xx1
> xx2
)
213 m_surface
->DrawLine(xx1
, yy1
, xx2
, yy2
);
215 CalcBoundingBox(x1
, y1
);
216 CalcBoundingBox(x2
, y2
);
219 // Draws an arc of a circle, centred on (xc, yc), with starting point (x1, y1)
220 // and ending at (x2, y2)
221 void wxDFBDCImpl::DoDrawArc(wxCoord
WXUNUSED(x1
), wxCoord
WXUNUSED(y1
),
222 wxCoord
WXUNUSED(x2
), wxCoord
WXUNUSED(y2
),
223 wxCoord
WXUNUSED(xc
), wxCoord
WXUNUSED(yc
))
225 wxCHECK_RET( IsOk(), wxT("invalid dc") );
227 wxFAIL_MSG( "DrawArc not implemented" );
230 void wxDFBDCImpl::DoDrawPoint(wxCoord x
, wxCoord y
)
232 wxCHECK_RET( IsOk(), wxT("invalid dc") );
234 // NB: DirectFB API doesn't provide a function for drawing points, so
235 // implement it as 1px long line. This is inefficient, but then, so is
236 // using DrawPoint() for drawing more than a few points.
237 DoDrawLine(x
, y
, x
, y
);
239 // FIXME_DFB: implement special cases for common formats (RGB24,RGBA/RGB32)
242 void wxDFBDCImpl::DoDrawPolygon(int WXUNUSED(n
), wxPoint
WXUNUSED(points
)[],
243 wxCoord
WXUNUSED(xoffset
), wxCoord
WXUNUSED(yoffset
),
244 wxPolygonFillMode
WXUNUSED(fillStyle
))
246 wxCHECK_RET( IsOk(), wxT("invalid dc") );
248 wxFAIL_MSG( "DrawPolygon not implemented" );
251 void wxDFBDCImpl::DoDrawLines(int WXUNUSED(n
), wxPoint
WXUNUSED(points
)[],
252 wxCoord
WXUNUSED(xoffset
), wxCoord
WXUNUSED(yoffset
))
254 wxCHECK_RET( IsOk(), wxT("invalid dc") );
256 // TODO: impl. using DirectDB's DrawLines
257 wxFAIL_MSG( "DrawLines not implemented" );
260 void wxDFBDCImpl::DoDrawRectangle(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
262 wxCHECK_RET( IsOk(), wxT("invalid dc") );
264 wxCoord xx
= XLOG2DEV(x
);
265 wxCoord yy
= YLOG2DEV(y
);
266 wxCoord ww
= m_signX
* XLOG2DEVREL(width
);
267 wxCoord hh
= m_signY
* YLOG2DEVREL(height
);
269 if ( ww
== 0 || hh
== 0 ) return;
282 if ( m_brush
.GetStyle() != wxTRANSPARENT
)
284 SelectColour(m_brush
.GetColour());
285 m_surface
->FillRectangle(xx
, yy
, ww
, hh
);
286 // restore pen's colour, because other drawing functions expect the
287 // colour to be set to the pen:
288 SelectColour(m_pen
.GetColour());
291 if ( m_pen
.GetStyle() != wxTRANSPARENT
)
293 m_surface
->DrawRectangle(xx
, yy
, ww
, hh
);
296 CalcBoundingBox(x
, y
);
297 CalcBoundingBox(x
+ width
, y
+ height
);
300 void wxDFBDCImpl::DoDrawRoundedRectangle(wxCoord
WXUNUSED(x
),
302 wxCoord
WXUNUSED(width
),
303 wxCoord
WXUNUSED(height
),
304 double WXUNUSED(radius
))
306 wxCHECK_RET( IsOk(), wxT("invalid dc") );
308 wxFAIL_MSG( "DrawRoundedRectangle not implemented" );
311 void wxDFBDCImpl::DoDrawEllipse(wxCoord
WXUNUSED(x
),
313 wxCoord
WXUNUSED(width
),
314 wxCoord
WXUNUSED(height
))
316 wxCHECK_RET( IsOk(), wxT("invalid dc") );
318 wxFAIL_MSG( "DrawElipse not implemented" );
321 void wxDFBDCImpl::DoDrawEllipticArc(wxCoord
WXUNUSED(x
),
328 wxCHECK_RET( IsOk(), wxT("invalid dc") );
330 wxFAIL_MSG( "DrawElipticArc not implemented" );
333 void wxDFBDCImpl::DoDrawText(const wxString
& text
, wxCoord x
, wxCoord y
)
335 wxCHECK_RET( IsOk(), wxT("invalid dc") );
337 wxCoord xx
= XLOG2DEV(x
);
338 wxCoord yy
= YLOG2DEV(y
);
340 // update the bounding box
342 CalcBoundingBox(x
, y
);
343 DoGetTextExtent(text
, &w
, &h
);
344 CalcBoundingBox(x
+ w
, y
+ h
);
346 // if background mode is solid, DrawText must paint text's background:
347 if ( m_backgroundMode
== wxSOLID
)
349 wxCHECK_RET( m_textBackgroundColour
.Ok(),
350 wxT("invalid background color") );
352 SelectColour(m_textBackgroundColour
);
353 m_surface
->FillRectangle(xx
, yy
, XLOG2DEVREL(w
), YLOG2DEVREL(h
));
356 // finally draw the text itself:
357 wxCHECK_RET( m_textForegroundColour
.Ok(),
358 wxT("invalid foreground color") );
359 SelectColour(m_textForegroundColour
);
360 m_surface
->DrawString(text
.utf8_str(), -1, xx
, yy
, DSTF_LEFT
| DSTF_TOP
);
362 // restore pen's colour, because other drawing functions expect the colour
363 // to be set to the pen:
364 SelectColour(m_pen
.GetColour());
367 void wxDFBDCImpl::DoDrawRotatedText(const wxString
& WXUNUSED(text
),
368 wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
),
369 double WXUNUSED(angle
))
371 wxCHECK_RET( IsOk(), wxT("invalid dc") );
373 wxFAIL_MSG( "DrawRotatedText not implemented" );
376 // ---------------------------------------------------------------------------
378 // ---------------------------------------------------------------------------
380 void wxDFBDCImpl::SetPen(const wxPen
& pen
)
382 m_pen
= pen
.Ok() ? pen
: DEFAULT_PEN
;
384 SelectColour(m_pen
.GetColour());
387 void wxDFBDCImpl::SetBrush(const wxBrush
& brush
)
389 m_brush
= brush
.Ok() ? brush
: DEFAULT_BRUSH
;
392 void wxDFBDCImpl::SelectColour(const wxColour
& clr
)
394 m_surface
->SetColor(clr
.Red(), clr
.Green(), clr
.Blue(), clr
.Alpha());
395 #warning "use SetColorIndex?"
399 void wxDFBDCImpl::SetPalette(const wxPalette
& WXUNUSED(palette
))
401 wxCHECK_RET( IsOk(), wxT("invalid dc") );
403 wxFAIL_MSG( "SetPalette not implemented" );
405 #endif // wxUSE_PALETTE
407 void wxDFBDCImpl::SetFont(const wxFont
& font
)
409 wxCHECK_RET( IsOk(), wxT("invalid dc") );
411 wxFont
f(font
.Ok() ? font
: DEFAULT_FONT
);
413 wxFont
oldfont(m_font
);
417 if ( !m_surface
->SetFont(GetCurrentFont()) )
424 wxIDirectFBFontPtr
wxDFBDCImpl::GetCurrentFont() const
426 bool aa
= (GetDepth() > 8);
427 return m_font
.GetDirectFBFont(aa
);
430 void wxDFBDCImpl::SetBackground(const wxBrush
& brush
)
432 wxCHECK_RET( IsOk(), wxT("invalid dc") );
434 if (!brush
.Ok()) return;
436 m_backgroundBrush
= brush
;
439 void wxDFBDCImpl::SetBackgroundMode(int mode
)
441 m_backgroundMode
= mode
;
444 void wxDFBDCImpl::SetLogicalFunction(wxRasterOperationMode function
)
446 wxCHECK_RET( IsOk(), wxT("invalid dc") );
448 // NB: we could also support XOR, but for blitting only (via DSBLIT_XOR);
449 // and possibly others via SetSrc/DstBlendFunction()
450 wxASSERT_MSG( function
== wxCOPY
,
451 "only wxCOPY logical function supported" );
453 m_logicalFunction
= function
;
456 bool wxDFBDCImpl::StartDoc(const wxString
& WXUNUSED(message
))
458 // We might be previewing, so return true to let it continue.
462 void wxDFBDCImpl::EndDoc()
466 void wxDFBDCImpl::StartPage()
470 void wxDFBDCImpl::EndPage()
474 // ---------------------------------------------------------------------------
476 // ---------------------------------------------------------------------------
478 wxCoord
wxDFBDCImpl::GetCharHeight() const
480 wxCHECK_MSG( IsOk(), -1, wxT("invalid dc") );
481 wxCHECK_MSG( m_font
.Ok(), -1, wxT("no font selected") );
484 GetCurrentFont()->GetHeight(&h
);
485 return YDEV2LOGREL(h
);
488 wxCoord
wxDFBDCImpl::GetCharWidth() const
490 wxCHECK_MSG( IsOk(), -1, wxT("invalid dc") );
491 wxCHECK_MSG( m_font
.Ok(), -1, wxT("no font selected") );
494 GetCurrentFont()->GetStringWidth("H", 1, &w
);
495 // VS: YDEV is corrent, it should *not* be XDEV, because font's are only
496 // scaled according to m_scaleY
497 return YDEV2LOGREL(w
);
500 void wxDFBDCImpl::DoGetTextExtent(const wxString
& string
, wxCoord
*x
, wxCoord
*y
,
501 wxCoord
*descent
, wxCoord
*externalLeading
,
502 const wxFont
*theFont
) const
504 wxCHECK_RET( IsOk(), wxT("invalid dc") );
505 wxCHECK_RET( m_font
.Ok(), wxT("no font selected") );
506 wxCHECK_RET( !theFont
|| theFont
->Ok(), wxT("invalid font") );
509 if ( theFont
!= NULL
)
512 wxConstCast(this, wxDFBDCImpl
)->SetFont(*theFont
);
515 wxCoord xx
= 0, yy
= 0;
517 wxIDirectFBFontPtr f
= GetCurrentFont();
519 if ( f
->GetStringExtents(string
.utf8_str(), -1, &rect
, NULL
) )
521 // VS: YDEV is corrent, it should *not* be XDEV, because font's are
522 // only scaled according to m_scaleY
523 xx
= YDEV2LOGREL(rect
.w
);
524 yy
= YDEV2LOGREL(rect
.h
);
529 if ( f
->GetDescender(&d
) )
530 *descent
= YDEV2LOGREL(-d
);
538 if ( externalLeading
) *externalLeading
= 0;
540 if ( theFont
!= NULL
)
541 wxConstCast(this, wxDFBDCImpl
)->SetFont(oldFont
);
546 // ---------------------------------------------------------------------------
548 // ---------------------------------------------------------------------------
550 // FIXME_DFB: scaling affects pixel size of font, pens, brushes, which
551 // is not currently implemented here; probably makes sense to
552 // switch to Cairo instead of implementing everything for DFB
554 void wxDFBDCImpl::DoGetSize(int *w
, int *h
) const
556 wxCHECK_RET( IsOk(), wxT("invalid dc") );
558 m_surface
->GetSize(w
, h
);
561 void wxDFBDCImpl::DoGetSizeMM(int *width
, int *height
) const
563 #warning "move this to common code?"
567 if ( width
) *width
= int(double(w
) / (m_userScaleX
*m_mm_to_pix_x
));
568 if ( height
) *height
= int(double(h
) / (m_userScaleY
*m_mm_to_pix_y
));
571 wxSize
wxDFBDCImpl::GetPPI() const
573 #warning "move this to common code?"
574 return wxSize(int(double(m_mm_to_pix_x
) * inches2mm
),
575 int(double(m_mm_to_pix_y
) * inches2mm
));
579 // ---------------------------------------------------------------------------
581 // ---------------------------------------------------------------------------
583 bool wxDFBDCImpl::DoBlit(wxCoord xdest
, wxCoord ydest
,
584 wxCoord width
, wxCoord height
,
585 wxDC
*source
, wxCoord xsrc
, wxCoord ysrc
,
586 wxRasterOperationMode rop
, bool useMask
,
587 wxCoord xsrcMask
, wxCoord ysrcMask
)
589 wxCHECK_MSG( IsOk(), false, "invalid dc" );
590 wxCHECK_MSG( source
, false, "invalid source dc" );
592 // NB: we could also support XOR here (via DSBLIT_XOR)
593 // and possibly others via SetSrc/DstBlendFunction()
594 wxCHECK_MSG( rop
== wxCOPY
, false, "only wxCOPY function supported" );
596 // transform the source DC coords to the device ones
597 xsrc
= source
->LogicalToDeviceX(xsrc
);
598 ysrc
= source
->LogicalToDeviceY(ysrc
);
600 // FIXME_DFB: use the mask origin when drawing transparently
601 wxASSERT_MSG( xsrcMask
== -1 && ysrcMask
== -1,
602 "non-default source mask offset not implemented" );
604 if (xsrcMask
== -1 && ysrcMask
== -1)
606 xsrcMask
= xsrc
; ysrcMask
= ysrc
;
610 xsrcMask
= source
->LogicalToDeviceX(xsrcMask
);
611 ysrcMask
= source
->LogicalToDeviceY(ysrcMask
);
615 wxMemoryDC
*sourceAsMemDC
= wxDynamicCast(source
, wxMemoryDC
);
618 DoDrawSubBitmap(sourceAsMemDC
->GetSelectedBitmap(),
627 return DoBlitFromSurface
629 static_cast<wxDFBDCImpl
*>(source
->GetImpl())
630 ->GetDirectFBSurface(),
640 void wxDFBDCImpl::DoDrawBitmap(const wxBitmap
&bmp
, wxCoord x
, wxCoord y
, bool useMask
)
642 wxCHECK_RET( IsOk(), wxT("invalid dc") );
643 wxCHECK_RET( bmp
.Ok(), wxT("invalid bitmap") );
646 0, 0, bmp
.GetWidth(), bmp
.GetHeight(),
648 m_logicalFunction
, useMask
);
651 void wxDFBDCImpl::DoDrawIcon(const wxIcon
& icon
, wxCoord x
, wxCoord y
)
653 // VZ: egcs 1.0.3 refuses to compile this without cast, no idea why
654 DoDrawBitmap((const wxBitmap
&)icon
, x
, y
, true);
657 void wxDFBDCImpl::DoDrawSubBitmap(const wxBitmap
&bmp
,
658 wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
,
659 wxCoord destx
, wxCoord desty
, int rop
, bool useMask
)
661 wxCHECK_RET( IsOk(), wxT("invalid dc") );
662 wxCHECK_RET( bmp
.Ok(), wxT("invalid bitmap") );
664 // NB: we could also support XOR here (via DSBLIT_XOR)
665 // and possibly others via SetSrc/DstBlendFunction()
666 wxCHECK_RET( rop
== wxCOPY
, "only wxCOPY function supported" );
668 if ( bmp
.GetDepth() == 1 )
670 // Mono bitmaps are handled in special way -- all 1s are drawn in
671 // foreground colours, all 0s in background colour.
672 wxFAIL_MSG( "drawing mono bitmaps not implemented" );
676 if ( useMask
&& bmp
.GetMask() )
678 // FIXME_DFB: see MGL sources for a way to do it, but it's not directly
679 // applicable because DirectFB doesn't implement ROPs; OTOH,
680 // it has blitting modes that can be useful; finally, see
681 // DFB's SetSrcBlendFunction() and SetSrcColorKey()
682 wxFAIL_MSG( "drawing bitmaps with masks not implemented" );
686 DoBlitFromSurface(bmp
.GetDirectFBSurface(),
692 bool wxDFBDCImpl::DoBlitFromSurface(const wxIDirectFBSurfacePtr
& src
,
693 wxCoord srcx
, wxCoord srcy
,
694 wxCoord w
, wxCoord h
,
695 wxCoord dstx
, wxCoord dsty
)
697 // don't do anything if the source rectangle is outside of source surface,
698 // DirectFB would assert in that case:
700 src
->GetSize(&srcsize
.x
, &srcsize
.y
);
701 if ( !wxRect(srcx
, srcy
, w
, h
).Intersects(wxRect(srcsize
)) )
703 wxLogDebug("Blitting from area outside of the source surface, caller code needs fixing.");
707 CalcBoundingBox(dstx
, dsty
);
708 CalcBoundingBox(dstx
+ w
, dsty
+ h
);
710 DFBRectangle srcRect
= { srcx
, srcy
, w
, h
};
711 DFBRectangle dstRect
= { XLOG2DEV(dstx
), YLOG2DEV(dsty
),
712 XLOG2DEVREL(w
), YLOG2DEVREL(h
) };
714 wxIDirectFBSurfacePtr
dst(m_surface
);
716 // FIXME: this will have to be different in useMask case, see above
717 DFBSurfaceBlittingFlags blitFlag
= (src
->GetPixelFormat() == DSPF_ARGB
)
718 ? DSBLIT_BLEND_ALPHACHANNEL
720 if ( !dst
->SetBlittingFlags(blitFlag
) )
723 if ( srcRect
.w
!= dstRect
.w
|| srcRect
.h
!= dstRect
.h
)
725 // the bitmap is drawn stretched:
726 dst
->StretchBlit(src
, &srcRect
, &dstRect
);
730 // no stretching, size is preserved:
731 dst
->Blit(src
, &srcRect
, dstRect
.x
, dstRect
.y
);