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::DoSetClippingRegionAsRegion(const wxRegion
& region
)
100 // NB: this can be done because wxDFB only supports rectangular regions
101 GetOwner()->SetClippingRegion(region
.AsRect());
104 void wxDFBDCImpl::DestroyClippingRegion()
106 wxCHECK_RET( IsOk(), wxT("invalid dc") );
108 m_surface
->SetClip(NULL
);
113 // ---------------------------------------------------------------------------
114 // query capabilities
115 // ---------------------------------------------------------------------------
117 int wxDFBDCImpl::GetDepth() const
119 return m_surface
->GetDepth();
122 // ---------------------------------------------------------------------------
124 // ---------------------------------------------------------------------------
126 void wxDFBDCImpl::Clear()
128 wxCHECK_RET( IsOk(), wxT("invalid dc") );
130 if ( m_backgroundBrush
.GetStyle() == wxTRANSPARENT
)
133 wxColour clr
= m_backgroundBrush
.GetColour();
134 m_surface
->Clear(clr
.Red(), clr
.Green(), clr
.Blue(), clr
.Alpha());
136 wxSize
size(GetSize());
137 CalcBoundingBox(XDEV2LOG(0), YDEV2LOG(0));
138 CalcBoundingBox(XDEV2LOG(size
.x
), YDEV2LOG(size
.y
));
141 extern bool wxDoFloodFill(wxDC
*dc
, wxCoord x
, wxCoord y
,
142 const wxColour
& col
, int style
);
144 bool wxDFBDCImpl::DoFloodFill(wxCoord x
, wxCoord y
,
145 const wxColour
& col
, int style
)
147 return wxDoFloodFill(GetOwner(), x
, y
, col
, style
);
150 bool wxDFBDCImpl::DoGetPixel(wxCoord x
, wxCoord y
, wxColour
*col
) const
152 wxCHECK_MSG( col
, false, "NULL colour parameter in wxDFBDCImpl::GetPixel");
154 wxFAIL_MSG( "GetPixel not implemented" );
162 void wxDFBDCImpl::DoCrossHair(wxCoord x
, wxCoord y
)
164 wxCHECK_RET( IsOk(), wxT("invalid dc") );
166 wxFAIL_MSG( "CrossHair not implemented" );
172 void wxDFBDCImpl::DoDrawLine(wxCoord x1
, wxCoord y1
, wxCoord x2
, wxCoord y2
)
174 wxCHECK_RET( IsOk(), wxT("invalid dc") );
176 if ( m_pen
.GetStyle() == wxTRANSPARENT
)
179 wxCoord xx1
= XLOG2DEV(x1
);
180 wxCoord yy1
= YLOG2DEV(y1
);
181 wxCoord xx2
= XLOG2DEV(x2
);
182 wxCoord yy2
= YLOG2DEV(y2
);
184 // FIXME: DrawLine() shouldn't draw the last pixel, but DFB's DrawLine()
185 // does draw it. We should undo any change to the last pixel by
186 // using GetPixel() and PutPixel(), but until they are implemented,
187 // handle at least the special case of vertical and horizontal
193 else if ( yy1
> yy2
)
200 else if ( xx1
> xx2
)
204 m_surface
->DrawLine(xx1
, yy1
, xx2
, yy2
);
206 CalcBoundingBox(x1
, y1
);
207 CalcBoundingBox(x2
, y2
);
210 // Draws an arc of a circle, centred on (xc, yc), with starting point (x1, y1)
211 // and ending at (x2, y2)
212 void wxDFBDCImpl::DoDrawArc(wxCoord
WXUNUSED(x1
), wxCoord
WXUNUSED(y1
),
213 wxCoord
WXUNUSED(x2
), wxCoord
WXUNUSED(y2
),
214 wxCoord
WXUNUSED(xc
), wxCoord
WXUNUSED(yc
))
216 wxCHECK_RET( IsOk(), wxT("invalid dc") );
218 wxFAIL_MSG( "DrawArc not implemented" );
221 void wxDFBDCImpl::DoDrawPoint(wxCoord x
, wxCoord y
)
223 wxCHECK_RET( IsOk(), wxT("invalid dc") );
225 // NB: DirectFB API doesn't provide a function for drawing points, so
226 // implement it as 1px long line. This is inefficient, but then, so is
227 // using DrawPoint() for drawing more than a few points.
228 DoDrawLine(x
, y
, x
, y
);
230 // FIXME_DFB: implement special cases for common formats (RGB24,RGBA/RGB32)
233 void wxDFBDCImpl::DoDrawPolygon(int WXUNUSED(n
), wxPoint
WXUNUSED(points
)[],
234 wxCoord
WXUNUSED(xoffset
), wxCoord
WXUNUSED(yoffset
),
235 int WXUNUSED(fillStyle
))
237 wxCHECK_RET( IsOk(), wxT("invalid dc") );
239 wxFAIL_MSG( "DrawPolygon not implemented" );
242 void wxDFBDCImpl::DoDrawLines(int WXUNUSED(n
), wxPoint
WXUNUSED(points
)[],
243 wxCoord
WXUNUSED(xoffset
), wxCoord
WXUNUSED(yoffset
))
245 wxCHECK_RET( IsOk(), wxT("invalid dc") );
247 // TODO: impl. using DirectDB's DrawLines
248 wxFAIL_MSG( "DrawLines not implemented" );
251 void wxDFBDCImpl::DoDrawRectangle(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
253 wxCHECK_RET( IsOk(), wxT("invalid dc") );
255 wxCoord xx
= XLOG2DEV(x
);
256 wxCoord yy
= YLOG2DEV(y
);
257 wxCoord ww
= m_signX
* XLOG2DEVREL(width
);
258 wxCoord hh
= m_signY
* YLOG2DEVREL(height
);
260 if ( ww
== 0 || hh
== 0 ) return;
273 if ( m_brush
.GetStyle() != wxTRANSPARENT
)
275 SelectColour(m_brush
.GetColour());
276 m_surface
->FillRectangle(xx
, yy
, ww
, hh
);
277 // restore pen's colour, because other drawing functions expect the
278 // colour to be set to the pen:
279 SelectColour(m_pen
.GetColour());
282 if ( m_pen
.GetStyle() != wxTRANSPARENT
)
284 m_surface
->DrawRectangle(xx
, yy
, ww
, hh
);
287 CalcBoundingBox(x
, y
);
288 CalcBoundingBox(x
+ width
, y
+ height
);
291 void wxDFBDCImpl::DoDrawRoundedRectangle(wxCoord
WXUNUSED(x
),
293 wxCoord
WXUNUSED(width
),
294 wxCoord
WXUNUSED(height
),
295 double WXUNUSED(radius
))
297 wxCHECK_RET( IsOk(), wxT("invalid dc") );
299 wxFAIL_MSG( "DrawRoundedRectangle not implemented" );
302 void wxDFBDCImpl::DoDrawEllipse(wxCoord
WXUNUSED(x
),
304 wxCoord
WXUNUSED(width
),
305 wxCoord
WXUNUSED(height
))
307 wxCHECK_RET( IsOk(), wxT("invalid dc") );
309 wxFAIL_MSG( "DrawElipse not implemented" );
312 void wxDFBDCImpl::DoDrawEllipticArc(wxCoord
WXUNUSED(x
),
319 wxCHECK_RET( IsOk(), wxT("invalid dc") );
321 wxFAIL_MSG( "DrawElipticArc not implemented" );
324 void wxDFBDCImpl::DoDrawText(const wxString
& text
, wxCoord x
, wxCoord y
)
326 wxCHECK_RET( IsOk(), wxT("invalid dc") );
328 wxCoord xx
= XLOG2DEV(x
);
329 wxCoord yy
= YLOG2DEV(y
);
331 // update the bounding box
333 CalcBoundingBox(x
, y
);
334 DoGetTextExtent(text
, &w
, &h
);
335 CalcBoundingBox(x
+ w
, y
+ h
);
337 // if background mode is solid, DrawText must paint text's background:
338 if ( m_backgroundMode
== wxSOLID
)
340 wxCHECK_RET( m_textBackgroundColour
.Ok(),
341 wxT("invalid background color") );
343 SelectColour(m_textBackgroundColour
);
344 m_surface
->FillRectangle(xx
, yy
, XLOG2DEVREL(w
), YLOG2DEVREL(h
));
347 // finally draw the text itself:
348 wxCHECK_RET( m_textForegroundColour
.Ok(),
349 wxT("invalid foreground color") );
350 SelectColour(m_textForegroundColour
);
351 m_surface
->DrawString(text
.utf8_str(), -1, xx
, yy
, DSTF_LEFT
| DSTF_TOP
);
353 // restore pen's colour, because other drawing functions expect the colour
354 // to be set to the pen:
355 SelectColour(m_pen
.GetColour());
358 void wxDFBDCImpl::DoDrawRotatedText(const wxString
& WXUNUSED(text
),
359 wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
),
360 double WXUNUSED(angle
))
362 wxCHECK_RET( IsOk(), wxT("invalid dc") );
364 wxFAIL_MSG( "DrawRotatedText not implemented" );
367 // ---------------------------------------------------------------------------
369 // ---------------------------------------------------------------------------
371 void wxDFBDCImpl::SetPen(const wxPen
& pen
)
373 m_pen
= pen
.Ok() ? pen
: DEFAULT_PEN
;
375 SelectColour(m_pen
.GetColour());
378 void wxDFBDCImpl::SetBrush(const wxBrush
& brush
)
380 m_brush
= brush
.Ok() ? brush
: DEFAULT_BRUSH
;
383 void wxDFBDCImpl::SelectColour(const wxColour
& clr
)
385 m_surface
->SetColor(clr
.Red(), clr
.Green(), clr
.Blue(), clr
.Alpha());
386 #warning "use SetColorIndex?"
390 void wxDFBDCImpl::SetPalette(const wxPalette
& WXUNUSED(palette
))
392 wxCHECK_RET( IsOk(), wxT("invalid dc") );
394 wxFAIL_MSG( "SetPalette not implemented" );
396 #endif // wxUSE_PALETTE
398 void wxDFBDCImpl::SetFont(const wxFont
& font
)
400 wxCHECK_RET( IsOk(), wxT("invalid dc") );
402 wxFont
f(font
.Ok() ? font
: DEFAULT_FONT
);
404 wxFont
oldfont(m_font
);
408 if ( !m_surface
->SetFont(GetCurrentFont()) )
415 wxIDirectFBFontPtr
wxDFBDCImpl::GetCurrentFont() const
417 bool aa
= (GetDepth() > 8);
418 return m_font
.GetDirectFBFont(aa
);
421 void wxDFBDCImpl::SetBackground(const wxBrush
& brush
)
423 wxCHECK_RET( IsOk(), wxT("invalid dc") );
425 if (!brush
.Ok()) return;
427 m_backgroundBrush
= brush
;
430 void wxDFBDCImpl::SetBackgroundMode(int mode
)
432 m_backgroundMode
= mode
;
435 void wxDFBDCImpl::SetLogicalFunction(int function
)
437 wxCHECK_RET( IsOk(), wxT("invalid dc") );
439 // NB: we could also support XOR, but for blitting only (via DSBLIT_XOR);
440 // and possibly others via SetSrc/DstBlendFunction()
441 wxASSERT_MSG( function
== wxCOPY
,
442 "only wxCOPY logical function supported" );
444 m_logicalFunction
= function
;
447 bool wxDFBDCImpl::StartDoc(const wxString
& WXUNUSED(message
))
449 // We might be previewing, so return true to let it continue.
453 void wxDFBDCImpl::EndDoc()
457 void wxDFBDCImpl::StartPage()
461 void wxDFBDCImpl::EndPage()
465 // ---------------------------------------------------------------------------
467 // ---------------------------------------------------------------------------
469 wxCoord
wxDFBDCImpl::GetCharHeight() const
471 wxCHECK_MSG( IsOk(), -1, wxT("invalid dc") );
472 wxCHECK_MSG( m_font
.Ok(), -1, wxT("no font selected") );
475 GetCurrentFont()->GetHeight(&h
);
476 return YDEV2LOGREL(h
);
479 wxCoord
wxDFBDCImpl::GetCharWidth() const
481 wxCHECK_MSG( IsOk(), -1, wxT("invalid dc") );
482 wxCHECK_MSG( m_font
.Ok(), -1, wxT("no font selected") );
485 GetCurrentFont()->GetStringWidth("H", 1, &w
);
486 // VS: YDEV is corrent, it should *not* be XDEV, because font's are only
487 // scaled according to m_scaleY
488 return YDEV2LOGREL(w
);
491 void wxDFBDCImpl::DoGetTextExtent(const wxString
& string
, wxCoord
*x
, wxCoord
*y
,
492 wxCoord
*descent
, wxCoord
*externalLeading
,
493 const wxFont
*theFont
) const
495 wxCHECK_RET( IsOk(), wxT("invalid dc") );
496 wxCHECK_RET( m_font
.Ok(), wxT("no font selected") );
497 wxCHECK_RET( !theFont
|| theFont
->Ok(), wxT("invalid font") );
500 if ( theFont
!= NULL
)
503 wxConstCast(this, wxDFBDCImpl
)->SetFont(*theFont
);
506 wxCoord xx
= 0, yy
= 0;
508 wxIDirectFBFontPtr f
= GetCurrentFont();
510 if ( f
->GetStringExtents(string
.utf8_str(), -1, &rect
, NULL
) )
512 // VS: YDEV is corrent, it should *not* be XDEV, because font's are
513 // only scaled according to m_scaleY
514 xx
= YDEV2LOGREL(rect
.w
);
515 yy
= YDEV2LOGREL(rect
.h
);
520 if ( f
->GetDescender(&d
) )
521 *descent
= YDEV2LOGREL(-d
);
529 if ( externalLeading
) *externalLeading
= 0;
531 if ( theFont
!= NULL
)
532 wxConstCast(this, wxDFBDCImpl
)->SetFont(oldFont
);
537 // ---------------------------------------------------------------------------
539 // ---------------------------------------------------------------------------
541 // FIXME_DFB: scaling affects pixel size of font, pens, brushes, which
542 // is not currently implemented here; probably makes sense to
543 // switch to Cairo instead of implementing everything for DFB
545 void wxDFBDCImpl::DoGetSize(int *w
, int *h
) const
547 wxCHECK_RET( IsOk(), wxT("invalid dc") );
549 m_surface
->GetSize(w
, h
);
552 void wxDFBDCImpl::DoGetSizeMM(int *width
, int *height
) const
554 #warning "move this to common code?"
558 if ( width
) *width
= int(double(w
) / (m_userScaleX
*m_mm_to_pix_x
));
559 if ( height
) *height
= int(double(h
) / (m_userScaleY
*m_mm_to_pix_y
));
562 wxSize
wxDFBDCImpl::GetPPI() const
564 #warning "move this to common code?"
565 return wxSize(int(double(m_mm_to_pix_x
) * inches2mm
),
566 int(double(m_mm_to_pix_y
) * inches2mm
));
570 // ---------------------------------------------------------------------------
572 // ---------------------------------------------------------------------------
574 bool wxDFBDCImpl::DoBlit(wxCoord xdest
, wxCoord ydest
,
575 wxCoord width
, wxCoord height
,
576 wxDC
*source
, wxCoord xsrc
, wxCoord ysrc
,
577 int rop
, bool useMask
,
578 wxCoord xsrcMask
, wxCoord ysrcMask
)
580 wxCHECK_MSG( IsOk(), false, "invalid dc" );
581 wxCHECK_MSG( source
, false, "invalid source dc" );
583 // NB: we could also support XOR here (via DSBLIT_XOR)
584 // and possibly others via SetSrc/DstBlendFunction()
585 wxCHECK_MSG( rop
== wxCOPY
, false, "only wxCOPY function supported" );
587 // transform the source DC coords to the device ones
588 xsrc
= source
->LogicalToDeviceX(xsrc
);
589 ysrc
= source
->LogicalToDeviceY(ysrc
);
591 // FIXME_DFB: use the mask origin when drawing transparently
592 wxASSERT_MSG( xsrcMask
== -1 && ysrcMask
== -1,
593 "non-default source mask offset not implemented" );
595 if (xsrcMask
== -1 && ysrcMask
== -1)
597 xsrcMask
= xsrc
; ysrcMask
= ysrc
;
601 xsrcMask
= source
->LogicalToDeviceX(xsrcMask
);
602 ysrcMask
= source
->LogicalToDeviceY(ysrcMask
);
606 wxMemoryDC
*sourceAsMemDC
= wxDynamicCast(source
, wxMemoryDC
);
609 DoDrawSubBitmap(sourceAsMemDC
->GetSelectedBitmap(),
618 return DoBlitFromSurface
620 static_cast<wxDFBDCImpl
*>(source
->GetImpl())
621 ->GetDirectFBSurface(),
631 void wxDFBDCImpl::DoDrawBitmap(const wxBitmap
&bmp
, wxCoord x
, wxCoord y
, bool useMask
)
633 wxCHECK_RET( IsOk(), wxT("invalid dc") );
634 wxCHECK_RET( bmp
.Ok(), wxT("invalid bitmap") );
637 0, 0, bmp
.GetWidth(), bmp
.GetHeight(),
639 m_logicalFunction
, useMask
);
642 void wxDFBDCImpl::DoDrawIcon(const wxIcon
& icon
, wxCoord x
, wxCoord y
)
644 // VZ: egcs 1.0.3 refuses to compile this without cast, no idea why
645 DoDrawBitmap((const wxBitmap
&)icon
, x
, y
, true);
648 void wxDFBDCImpl::DoDrawSubBitmap(const wxBitmap
&bmp
,
649 wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
,
650 wxCoord destx
, wxCoord desty
, int rop
, bool useMask
)
652 wxCHECK_RET( IsOk(), wxT("invalid dc") );
653 wxCHECK_RET( bmp
.Ok(), wxT("invalid bitmap") );
655 // NB: we could also support XOR here (via DSBLIT_XOR)
656 // and possibly others via SetSrc/DstBlendFunction()
657 wxCHECK_RET( rop
== wxCOPY
, "only wxCOPY function supported" );
659 if ( bmp
.GetDepth() == 1 )
661 // Mono bitmaps are handled in special way -- all 1s are drawn in
662 // foreground colours, all 0s in background colour.
663 wxFAIL_MSG( "drawing mono bitmaps not implemented" );
667 if ( useMask
&& bmp
.GetMask() )
669 // FIXME_DFB: see MGL sources for a way to do it, but it's not directly
670 // applicable because DirectFB doesn't implement ROPs; OTOH,
671 // it has blitting modes that can be useful; finally, see
672 // DFB's SetSrcBlendFunction() and SetSrcColorKey()
673 wxFAIL_MSG( "drawing bitmaps with masks not implemented" );
677 DoBlitFromSurface(bmp
.GetDirectFBSurface(),
683 bool wxDFBDCImpl::DoBlitFromSurface(const wxIDirectFBSurfacePtr
& src
,
684 wxCoord srcx
, wxCoord srcy
,
685 wxCoord w
, wxCoord h
,
686 wxCoord dstx
, wxCoord dsty
)
688 // don't do anything if the source rectangle is outside of source surface,
689 // DirectFB would assert in that case:
691 src
->GetSize(&srcsize
.x
, &srcsize
.y
);
692 if ( !wxRect(srcx
, srcy
, w
, h
).Intersects(wxRect(srcsize
)) )
694 wxLogDebug("Blitting from area outside of the source surface, caller code needs fixing.");
698 CalcBoundingBox(dstx
, dsty
);
699 CalcBoundingBox(dstx
+ w
, dsty
+ h
);
701 DFBRectangle srcRect
= { srcx
, srcy
, w
, h
};
702 DFBRectangle dstRect
= { XLOG2DEV(dstx
), YLOG2DEV(dsty
),
703 XLOG2DEVREL(w
), YLOG2DEVREL(h
) };
705 wxIDirectFBSurfacePtr
dst(m_surface
);
707 // FIXME: this will have to be different in useMask case, see above
708 DFBSurfaceBlittingFlags blitFlag
= (src
->GetPixelFormat() == DSPF_ARGB
)
709 ? DSBLIT_BLEND_ALPHACHANNEL
711 if ( !dst
->SetBlittingFlags(blitFlag
) )
714 if ( srcRect
.w
!= dstRect
.w
|| srcRect
.h
!= dstRect
.h
)
716 // the bitmap is drawn stretched:
717 dst
->StretchBlit(src
, &srcRect
, &dstRect
);
721 // no stretching, size is preserved:
722 dst
->Blit(src
, &srcRect
, dstRect
.x
, dstRect
.y
);