1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        src/dfb/dc.cpp
3 // Purpose:     wxDFBDCImpl class
4 // Author:      Vaclav Slavik
5 // Created:     2006-08-07
6 // Copyright:   (c) 2006 REA Elektronik GmbH
7 // Licence:     wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9 
10 // ===========================================================================
11 // declarations
12 // ===========================================================================
13 
14 // ---------------------------------------------------------------------------
15 // headers
16 // ---------------------------------------------------------------------------
17 
18 // For compilers that support precompilation, includes "wx.h".
19 #include "wx/wxprec.h"
20 
21 #ifdef __BORLANDC__
22     #pragma hdrstop
23 #endif
24 
25 #ifndef WX_PRECOMP
26     #include "wx/dcmemory.h"
27     #include "wx/log.h"
28 #endif
29 
30 #include "wx/dfb/dc.h"
31 #include "wx/dfb/private.h"
32 
33 // these values are used to initialize newly created DC
34 #define DEFAULT_FONT      (*wxNORMAL_FONT)
35 #define DEFAULT_PEN       (*wxBLACK_PEN)
36 #define DEFAULT_BRUSH     (*wxWHITE_BRUSH)
37 
38 // ===========================================================================
39 // implementation
40 // ===========================================================================
41 
42 //-----------------------------------------------------------------------------
43 // wxDFBDCImpl
44 //-----------------------------------------------------------------------------
45 
IMPLEMENT_ABSTRACT_CLASS(wxDFBDCImpl,wxDCImpl)46 IMPLEMENT_ABSTRACT_CLASS(wxDFBDCImpl, wxDCImpl)
47 
48 void wxDFBDCImpl::DFBInit(const wxIDirectFBSurfacePtr& surface)
49 {
50     m_surface = surface;
51 
52     wxCHECK_RET( surface != NULL, "invalid surface" );
53 
54     m_mm_to_pix_x = (double)wxGetDisplaySize().GetWidth() /
55                     (double)wxGetDisplaySizeMM().GetWidth();
56     m_mm_to_pix_y = (double)wxGetDisplaySize().GetHeight() /
57                     (double)wxGetDisplaySizeMM().GetHeight();
58 
59     SetFont(DEFAULT_FONT);
60     SetPen(DEFAULT_PEN);
61     SetBrush(DEFAULT_BRUSH);
62 }
63 
64 
65 // ---------------------------------------------------------------------------
66 // clipping
67 // ---------------------------------------------------------------------------
68 
DoSetClippingRegion(wxCoord cx,wxCoord cy,wxCoord cw,wxCoord ch)69 void wxDFBDCImpl::DoSetClippingRegion(wxCoord cx, wxCoord cy, wxCoord cw, wxCoord ch)
70 {
71     wxCHECK_RET( IsOk(), wxT("invalid dc") );
72 
73     wxSize size(GetSize());
74 
75     wxASSERT_MSG( !m_clipping,
76                   "narrowing clipping region not implemented yet" );
77 
78     // NB: We intersect the clipping rectangle with surface's area here because
79     //     DirectFB will return an error if you try to set clipping rectangle
80     //     that is partially outside of the surface.
81     DFBRegion r;
82     r.x1 = wxMax(0, XLOG2DEV(cx));
83     r.y1 = wxMax(0, YLOG2DEV(cy));
84     r.x2 = wxMin(r.x1 + XLOG2DEVREL(cw), size.x) - 1;
85     r.y2 = wxMin(r.y1 + YLOG2DEVREL(ch), size.y) - 1;
86 
87     if ( !m_surface->SetClip(&r) )
88         return;
89 
90     m_clipX1 = cx;
91     m_clipY1 = cy;
92     m_clipX2 = cx + cw - 1;
93     m_clipY2 = cy + ch -1;
94     m_clipping = true;
95 }
96 
DoSetDeviceClippingRegion(const wxRegion & region)97 void wxDFBDCImpl::DoSetDeviceClippingRegion(const wxRegion& region)
98 {
99     // NB: this can be done because wxDFB only supports rectangular regions
100     wxRect rect = region.AsRect();
101 
102     // our parameter is in physical coordinates while DoSetClippingRegion()
103     // takes logical ones
104     rect.x = XDEV2LOG(rect.x);
105     rect.y = YDEV2LOG(rect.y);
106     rect.width = XDEV2LOG(rect.width);
107     rect.height = YDEV2LOG(rect.height);
108 
109     DoSetClippingRegion(rect.x, rect.y, rect.width, rect.height);
110 }
111 
DestroyClippingRegion()112 void wxDFBDCImpl::DestroyClippingRegion()
113 {
114     wxCHECK_RET( IsOk(), wxT("invalid dc") );
115 
116     m_surface->SetClip(NULL);
117 
118     ResetClipping();
119 }
120 
121 // ---------------------------------------------------------------------------
122 // query capabilities
123 // ---------------------------------------------------------------------------
124 
GetDepth() const125 int wxDFBDCImpl::GetDepth() const
126 {
127     return m_surface->GetDepth();
128 }
129 
130 // ---------------------------------------------------------------------------
131 // drawing
132 // ---------------------------------------------------------------------------
133 
Clear()134 void wxDFBDCImpl::Clear()
135 {
136     wxCHECK_RET( IsOk(), wxT("invalid dc") );
137 
138     if ( m_backgroundBrush.GetStyle() == wxTRANSPARENT )
139         return;
140 
141     wxColour clr = m_backgroundBrush.GetColour();
142     m_surface->Clear(clr.Red(), clr.Green(), clr.Blue(), clr.Alpha());
143 
144     wxSize size(GetSize());
145     CalcBoundingBox(XDEV2LOG(0), YDEV2LOG(0));
146     CalcBoundingBox(XDEV2LOG(size.x), YDEV2LOG(size.y));
147 }
148 
149 extern bool wxDoFloodFill(wxDC *dc, wxCoord x, wxCoord y,
150                           const wxColour & col, wxFloodFillStyle style);
151 
DoFloodFill(wxCoord x,wxCoord y,const wxColour & col,wxFloodFillStyle style)152 bool wxDFBDCImpl::DoFloodFill(wxCoord x, wxCoord y,
153                        const wxColour& col, wxFloodFillStyle style)
154 {
155     return wxDoFloodFill(GetOwner(), x, y, col, style);
156 }
157 
DoGetPixel(wxCoord x,wxCoord y,wxColour * col) const158 bool wxDFBDCImpl::DoGetPixel(wxCoord x, wxCoord y, wxColour *col) const
159 {
160     wxCHECK_MSG( col, false, "NULL colour parameter in wxDFBDCImpl::GetPixel");
161 
162     wxFAIL_MSG( "GetPixel not implemented" );
163 
164     wxUnusedVar(x);
165     wxUnusedVar(y);
166 
167     return false;
168 }
169 
DoCrossHair(wxCoord x,wxCoord y)170 void wxDFBDCImpl::DoCrossHair(wxCoord x, wxCoord y)
171 {
172     wxCHECK_RET( IsOk(), wxT("invalid dc") );
173 
174     wxFAIL_MSG( "CrossHair not implemented" );
175 
176     wxUnusedVar(x);
177     wxUnusedVar(y);
178 }
179 
DoDrawLine(wxCoord x1,wxCoord y1,wxCoord x2,wxCoord y2)180 void wxDFBDCImpl::DoDrawLine(wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2)
181 {
182     wxCHECK_RET( IsOk(), wxT("invalid dc") );
183 
184     if ( m_pen.GetStyle() == wxTRANSPARENT )
185         return;
186 
187     wxCoord xx1 = XLOG2DEV(x1);
188     wxCoord yy1 = YLOG2DEV(y1);
189     wxCoord xx2 = XLOG2DEV(x2);
190     wxCoord yy2 = YLOG2DEV(y2);
191 
192     // FIXME: DrawLine() shouldn't draw the last pixel, but DFB's DrawLine()
193     //        does draw it. We should undo any change to the last pixel by
194     //        using GetPixel() and PutPixel(), but until they are implemented,
195     //        handle at least the special case of vertical and horizontal
196     //        lines correctly:
197     if ( xx1 == xx2 )
198     {
199         if ( yy1 < yy2 )
200             yy2--;
201         else if ( yy1 > yy2 )
202             yy2++;
203     }
204     if ( yy1 == yy2 )
205     {
206         if ( xx1 < xx2 )
207             xx2--;
208         else if ( xx1 > xx2 )
209             xx2++;
210     }
211 
212     m_surface->DrawLine(xx1, yy1, xx2, yy2);
213 
214     CalcBoundingBox(x1, y1);
215     CalcBoundingBox(x2, y2);
216 }
217 
218 // Draws an arc of a circle, centred on (xc, yc), with starting point (x1, y1)
219 // and ending at (x2, y2)
DoDrawArc(wxCoord WXUNUSED (x1),wxCoord WXUNUSED (y1),wxCoord WXUNUSED (x2),wxCoord WXUNUSED (y2),wxCoord WXUNUSED (xc),wxCoord WXUNUSED (yc))220 void wxDFBDCImpl::DoDrawArc(wxCoord WXUNUSED(x1), wxCoord WXUNUSED(y1),
221                             wxCoord WXUNUSED(x2), wxCoord WXUNUSED(y2),
222                             wxCoord WXUNUSED(xc), wxCoord WXUNUSED(yc))
223 {
224     wxCHECK_RET( IsOk(), wxT("invalid dc") );
225 
226     wxFAIL_MSG( "DrawArc not implemented" );
227 }
228 
DoDrawPoint(wxCoord x,wxCoord y)229 void wxDFBDCImpl::DoDrawPoint(wxCoord x, wxCoord y)
230 {
231     wxCHECK_RET( IsOk(), wxT("invalid dc") );
232 
233     // NB: DirectFB API doesn't provide a function for drawing points, so
234     //     implement it as 1px long line. This is inefficient, but then, so is
235     //     using DrawPoint() for drawing more than a few points.
236     DoDrawLine(x, y, x, y);
237 
238     // FIXME_DFB: implement special cases for common formats (RGB24,RGBA/RGB32)
239 }
240 
DoDrawPolygon(int WXUNUSED (n),const wxPoint WXUNUSED (points)[],wxCoord WXUNUSED (xoffset),wxCoord WXUNUSED (yoffset),wxPolygonFillMode WXUNUSED (fillStyle))241 void wxDFBDCImpl::DoDrawPolygon(int WXUNUSED(n), const wxPoint WXUNUSED(points)[],
242                                 wxCoord WXUNUSED(xoffset), wxCoord WXUNUSED(yoffset),
243                                 wxPolygonFillMode WXUNUSED(fillStyle))
244 {
245     wxCHECK_RET( IsOk(), wxT("invalid dc") );
246 
247     wxFAIL_MSG( "DrawPolygon not implemented" );
248 }
249 
DoDrawLines(int WXUNUSED (n),const wxPoint WXUNUSED (points)[],wxCoord WXUNUSED (xoffset),wxCoord WXUNUSED (yoffset))250 void wxDFBDCImpl::DoDrawLines(int WXUNUSED(n), const wxPoint WXUNUSED(points)[],
251                               wxCoord WXUNUSED(xoffset), wxCoord WXUNUSED(yoffset))
252 {
253     wxCHECK_RET( IsOk(), wxT("invalid dc") );
254 
255     // TODO: impl. using DirectDB's DrawLines
256     wxFAIL_MSG( "DrawLines not implemented" );
257 }
258 
DoDrawRectangle(wxCoord x,wxCoord y,wxCoord width,wxCoord height)259 void wxDFBDCImpl::DoDrawRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
260 {
261     wxCHECK_RET( IsOk(), wxT("invalid dc") );
262 
263     wxCoord xx = XLOG2DEV(x);
264     wxCoord yy = YLOG2DEV(y);
265     wxCoord ww = m_signX * XLOG2DEVREL(width);
266     wxCoord hh = m_signY * YLOG2DEVREL(height);
267 
268     if ( ww == 0 || hh == 0 ) return;
269 
270     if ( ww < 0 )
271     {
272         ww = -ww;
273         xx = xx - ww;
274     }
275     if ( hh < 0 )
276     {
277         hh = -hh;
278         yy = yy - hh;
279     }
280 
281     if ( m_brush.GetStyle() != wxTRANSPARENT )
282     {
283         SelectColour(m_brush.GetColour());
284         m_surface->FillRectangle(xx, yy, ww, hh);
285         // restore pen's colour, because other drawing functions expect the
286         // colour to be set to the pen:
287         SelectColour(m_pen.GetColour());
288     }
289 
290     if ( m_pen.GetStyle() != wxTRANSPARENT )
291     {
292         m_surface->DrawRectangle(xx, yy, ww, hh);
293     }
294 
295     CalcBoundingBox(x, y);
296     CalcBoundingBox(x + width, y + height);
297 }
298 
DoDrawRoundedRectangle(wxCoord WXUNUSED (x),wxCoord WXUNUSED (y),wxCoord WXUNUSED (width),wxCoord WXUNUSED (height),double WXUNUSED (radius))299 void wxDFBDCImpl::DoDrawRoundedRectangle(wxCoord WXUNUSED(x),
300                                          wxCoord WXUNUSED(y),
301                                          wxCoord WXUNUSED(width),
302                                          wxCoord WXUNUSED(height),
303                                          double WXUNUSED(radius))
304 {
305     wxCHECK_RET( IsOk(), wxT("invalid dc") );
306 
307     wxFAIL_MSG( "DrawRoundedRectangle not implemented" );
308 }
309 
DoDrawEllipse(wxCoord WXUNUSED (x),wxCoord WXUNUSED (y),wxCoord WXUNUSED (width),wxCoord WXUNUSED (height))310 void wxDFBDCImpl::DoDrawEllipse(wxCoord WXUNUSED(x),
311                                 wxCoord WXUNUSED(y),
312                                 wxCoord WXUNUSED(width),
313                                 wxCoord WXUNUSED(height))
314 {
315     wxCHECK_RET( IsOk(), wxT("invalid dc") );
316 
317     wxFAIL_MSG( "DrawElipse not implemented" );
318 }
319 
DoDrawEllipticArc(wxCoord WXUNUSED (x),wxCoord WXUNUSED (y),wxCoord WXUNUSED (w),wxCoord WXUNUSED (h),double WXUNUSED (sa),double WXUNUSED (ea))320 void wxDFBDCImpl::DoDrawEllipticArc(wxCoord WXUNUSED(x),
321                                     wxCoord WXUNUSED(y),
322                                     wxCoord WXUNUSED(w),
323                                     wxCoord WXUNUSED(h),
324                                     double WXUNUSED(sa),
325                                     double WXUNUSED(ea))
326 {
327     wxCHECK_RET( IsOk(), wxT("invalid dc") );
328 
329     wxFAIL_MSG( "DrawElipticArc not implemented" );
330 }
331 
DoDrawText(const wxString & text,wxCoord x,wxCoord y)332 void wxDFBDCImpl::DoDrawText(const wxString& text, wxCoord x, wxCoord y)
333 {
334     wxCHECK_RET( IsOk(), wxT("invalid dc") );
335 
336     wxCoord xx = XLOG2DEV(x);
337     wxCoord yy = YLOG2DEV(y);
338 
339     // update the bounding box
340     wxCoord w, h;
341     CalcBoundingBox(x, y);
342     DoGetTextExtent(text, &w, &h);
343     CalcBoundingBox(x + w, y + h);
344 
345     // if background mode is solid, DrawText must paint text's background:
346     if ( m_backgroundMode == wxSOLID )
347     {
348         wxCHECK_RET( m_textBackgroundColour.IsOk(),
349                      wxT("invalid background color") );
350 
351         SelectColour(m_textBackgroundColour);
352         m_surface->FillRectangle(xx, yy, XLOG2DEVREL(w), YLOG2DEVREL(h));
353     }
354 
355     // finally draw the text itself:
356     wxCHECK_RET( m_textForegroundColour.IsOk(),
357                  wxT("invalid foreground color") );
358     SelectColour(m_textForegroundColour);
359     m_surface->DrawString(text.utf8_str(), -1, xx, yy, DSTF_LEFT | DSTF_TOP);
360 
361     // restore pen's colour, because other drawing functions expect the colour
362     // to be set to the pen:
363     SelectColour(m_pen.GetColour());
364 }
365 
DoDrawRotatedText(const wxString & WXUNUSED (text),wxCoord WXUNUSED (x),wxCoord WXUNUSED (y),double WXUNUSED (angle))366 void wxDFBDCImpl::DoDrawRotatedText(const wxString& WXUNUSED(text),
367                                     wxCoord WXUNUSED(x), wxCoord WXUNUSED(y),
368                                     double WXUNUSED(angle))
369 {
370     wxCHECK_RET( IsOk(), wxT("invalid dc") );
371 
372     wxFAIL_MSG( "DrawRotatedText not implemented" );
373 }
374 
375 // ---------------------------------------------------------------------------
376 // set GDI objects
377 // ---------------------------------------------------------------------------
378 
SetPen(const wxPen & pen)379 void wxDFBDCImpl::SetPen(const wxPen& pen)
380 {
381     m_pen = pen.IsOk() ? pen : DEFAULT_PEN;
382 
383     SelectColour(m_pen.GetColour());
384 }
385 
SetBrush(const wxBrush & brush)386 void wxDFBDCImpl::SetBrush(const wxBrush& brush)
387 {
388     m_brush = brush.IsOk() ? brush : DEFAULT_BRUSH;
389 }
390 
SelectColour(const wxColour & clr)391 void wxDFBDCImpl::SelectColour(const wxColour& clr)
392 {
393     m_surface->SetColor(clr.Red(), clr.Green(), clr.Blue(), clr.Alpha());
394     #warning "use SetColorIndex?"
395 }
396 
397 #if wxUSE_PALETTE
SetPalette(const wxPalette & WXUNUSED (palette))398 void wxDFBDCImpl::SetPalette(const wxPalette& WXUNUSED(palette))
399 {
400     wxCHECK_RET( IsOk(), wxT("invalid dc") );
401 
402     wxFAIL_MSG( "SetPalette not implemented" );
403 }
404 #endif // wxUSE_PALETTE
405 
SetFont(const wxFont & font)406 void wxDFBDCImpl::SetFont(const wxFont& font)
407 {
408     wxCHECK_RET( IsOk(), wxT("invalid dc") );
409 
410     wxFont f(font.IsOk() ? font : DEFAULT_FONT);
411 
412     wxFont oldfont(m_font);
413 
414     m_font = f;
415 
416     if ( !m_surface->SetFont(GetCurrentFont()) )
417     {
418         m_font = oldfont;
419         return;
420     }
421 }
422 
GetCurrentFont() const423 wxIDirectFBFontPtr wxDFBDCImpl::GetCurrentFont() const
424 {
425     bool aa = (GetDepth() > 8);
426     return m_font.GetDirectFBFont(aa);
427 }
428 
SetBackground(const wxBrush & brush)429 void wxDFBDCImpl::SetBackground(const wxBrush& brush)
430 {
431     wxCHECK_RET( IsOk(), wxT("invalid dc") );
432 
433     if (!brush.IsOk()) return;
434 
435     m_backgroundBrush = brush;
436 }
437 
SetBackgroundMode(int mode)438 void wxDFBDCImpl::SetBackgroundMode(int mode)
439 {
440     m_backgroundMode = mode;
441 }
442 
SetLogicalFunction(wxRasterOperationMode function)443 void wxDFBDCImpl::SetLogicalFunction(wxRasterOperationMode function)
444 {
445     wxCHECK_RET( IsOk(), wxT("invalid dc") );
446 
447     // NB: we could also support XOR, but for blitting only (via DSBLIT_XOR);
448     //     and possibly others via SetSrc/DstBlendFunction()
449     wxASSERT_MSG( function == wxCOPY,
450                   "only wxCOPY logical function supported" );
451 
452     m_logicalFunction = function;
453 }
454 
StartDoc(const wxString & WXUNUSED (message))455 bool wxDFBDCImpl::StartDoc(const wxString& WXUNUSED(message))
456 {
457     // We might be previewing, so return true to let it continue.
458     return true;
459 }
460 
EndDoc()461 void wxDFBDCImpl::EndDoc()
462 {
463 }
464 
StartPage()465 void wxDFBDCImpl::StartPage()
466 {
467 }
468 
EndPage()469 void wxDFBDCImpl::EndPage()
470 {
471 }
472 
473 // ---------------------------------------------------------------------------
474 // text metrics
475 // ---------------------------------------------------------------------------
476 
GetCharHeight() const477 wxCoord wxDFBDCImpl::GetCharHeight() const
478 {
479     wxCHECK_MSG( IsOk(), -1, wxT("invalid dc") );
480     wxCHECK_MSG( m_font.IsOk(), -1, wxT("no font selected") );
481 
482     int h = -1;
483     GetCurrentFont()->GetHeight(&h);
484     return YDEV2LOGREL(h);
485 }
486 
GetCharWidth() const487 wxCoord wxDFBDCImpl::GetCharWidth() const
488 {
489     wxCHECK_MSG( IsOk(), -1, wxT("invalid dc") );
490     wxCHECK_MSG( m_font.IsOk(), -1, wxT("no font selected") );
491 
492     int w = -1;
493     GetCurrentFont()->GetStringWidth("H", 1, &w);
494     // VS: YDEV is corrent, it should *not* be XDEV, because font's are only
495     //     scaled according to m_scaleY
496     return YDEV2LOGREL(w);
497 }
498 
DoGetTextExtent(const wxString & string,wxCoord * x,wxCoord * y,wxCoord * descent,wxCoord * externalLeading,const wxFont * theFont) const499 void wxDFBDCImpl::DoGetTextExtent(const wxString& string, wxCoord *x, wxCoord *y,
500                            wxCoord *descent, wxCoord *externalLeading,
501                            const wxFont *theFont) const
502 {
503     wxCHECK_RET( IsOk(), wxT("invalid dc") );
504     wxCHECK_RET( m_font.IsOk(), wxT("no font selected") );
505     wxCHECK_RET( !theFont || theFont->IsOk(), wxT("invalid font") );
506 
507     wxFont oldFont;
508     if ( theFont != NULL )
509     {
510         oldFont = m_font;
511         wxConstCast(this, wxDFBDCImpl)->SetFont(*theFont);
512     }
513 
514     wxCoord xx = 0, yy = 0;
515     DFBRectangle rect;
516     wxIDirectFBFontPtr f = GetCurrentFont();
517 
518     if ( f->GetStringExtents(string.utf8_str(), -1, &rect, NULL) )
519     {
520         // VS: YDEV is corrent, it should *not* be XDEV, because font's are
521         //     only scaled according to m_scaleY
522         xx = YDEV2LOGREL(rect.w);
523         yy = YDEV2LOGREL(rect.h);
524 
525         if ( descent )
526         {
527             int d;
528             if ( f->GetDescender(&d) )
529                 *descent = YDEV2LOGREL(-d);
530             else
531                 *descent = 0;
532         }
533     }
534 
535     if ( x ) *x = xx;
536     if ( y ) *y = yy;
537     if ( externalLeading ) *externalLeading = 0;
538 
539     if ( theFont != NULL )
540         wxConstCast(this, wxDFBDCImpl)->SetFont(oldFont);
541 }
542 
543 
544 
545 // ---------------------------------------------------------------------------
546 // mapping modes
547 // ---------------------------------------------------------------------------
548 
549 // FIXME_DFB: scaling affects pixel size of font, pens, brushes, which
550 //            is not currently implemented here; probably makes sense to
551 //            switch to Cairo instead of implementing everything for DFB
552 
DoGetSize(int * w,int * h) const553 void wxDFBDCImpl::DoGetSize(int *w, int *h) const
554 {
555     wxCHECK_RET( IsOk(), wxT("invalid dc") );
556 
557     m_surface->GetSize(w, h);
558 }
559 
DoGetSizeMM(int * width,int * height) const560 void wxDFBDCImpl::DoGetSizeMM(int *width, int *height) const
561 {
562     #warning "move this to common code?"
563     int w = 0;
564     int h = 0;
565     GetSize(&w, &h);
566     if ( width ) *width = int(double(w) / (m_userScaleX*m_mm_to_pix_x));
567     if ( height ) *height = int(double(h) / (m_userScaleY*m_mm_to_pix_y));
568 }
569 
GetPPI() const570 wxSize wxDFBDCImpl::GetPPI() const
571 {
572     #warning "move this to common code?"
573     return wxSize(int(double(m_mm_to_pix_x) * inches2mm),
574                   int(double(m_mm_to_pix_y) * inches2mm));
575 }
576 
577 
578 // ---------------------------------------------------------------------------
579 // Blitting
580 // ---------------------------------------------------------------------------
581 
DoBlit(wxCoord xdest,wxCoord ydest,wxCoord width,wxCoord height,wxDC * source,wxCoord xsrc,wxCoord ysrc,wxRasterOperationMode rop,bool useMask,wxCoord xsrcMask,wxCoord ysrcMask)582 bool wxDFBDCImpl::DoBlit(wxCoord xdest, wxCoord ydest,
583                          wxCoord width, wxCoord height,
584                          wxDC *source, wxCoord xsrc, wxCoord ysrc,
585                          wxRasterOperationMode rop, bool useMask,
586                          wxCoord xsrcMask, wxCoord ysrcMask)
587 {
588     wxCHECK_MSG( IsOk(), false, "invalid dc" );
589     wxCHECK_MSG( source, false, "invalid source dc" );
590 
591     // NB: we could also support XOR here (via DSBLIT_XOR)
592     //     and possibly others via SetSrc/DstBlendFunction()
593     wxCHECK_MSG( rop == wxCOPY, false, "only wxCOPY function supported" );
594 
595     // transform the source DC coords to the device ones
596     xsrc = source->LogicalToDeviceX(xsrc);
597     ysrc = source->LogicalToDeviceY(ysrc);
598 
599     // FIXME_DFB: use the mask origin when drawing transparently
600     wxASSERT_MSG( xsrcMask == -1 && ysrcMask == -1,
601                   "non-default source mask offset not implemented" );
602 #if 0
603     if (xsrcMask == -1 && ysrcMask == -1)
604     {
605         xsrcMask = xsrc; ysrcMask = ysrc;
606     }
607     else
608     {
609         xsrcMask = source->LogicalToDeviceX(xsrcMask);
610         ysrcMask = source->LogicalToDeviceY(ysrcMask);
611     }
612 #endif
613 
614     wxMemoryDC *sourceAsMemDC = wxDynamicCast(source, wxMemoryDC);
615     if ( sourceAsMemDC )
616     {
617         DoDrawSubBitmap(sourceAsMemDC->GetSelectedBitmap(),
618                         xsrc, ysrc,
619                         width, height,
620                         xdest, ydest,
621                         rop,
622                         useMask);
623     }
624     else
625     {
626         return DoBlitFromSurface
627                (
628                  static_cast<wxDFBDCImpl *>(source->GetImpl())
629                     ->GetDirectFBSurface(),
630                  xsrc, ysrc,
631                  width, height,
632                  xdest, ydest
633                );
634     }
635 
636     return true;
637 }
638 
DoDrawBitmap(const wxBitmap & bmp,wxCoord x,wxCoord y,bool useMask)639 void wxDFBDCImpl::DoDrawBitmap(const wxBitmap &bmp, wxCoord x, wxCoord y, bool useMask)
640 {
641     wxCHECK_RET( IsOk(), wxT("invalid dc") );
642     wxCHECK_RET( bmp.IsOk(), wxT("invalid bitmap") );
643 
644     DoDrawSubBitmap(bmp,
645                     0, 0, bmp.GetWidth(), bmp.GetHeight(),
646                     x, y,
647                     m_logicalFunction, useMask);
648 }
649 
DoDrawIcon(const wxIcon & icon,wxCoord x,wxCoord y)650 void wxDFBDCImpl::DoDrawIcon(const wxIcon& icon, wxCoord x, wxCoord y)
651 {
652     // VZ: egcs 1.0.3 refuses to compile this without cast, no idea why
653     DoDrawBitmap((const wxBitmap&)icon, x, y, true);
654 }
655 
DoDrawSubBitmap(const wxBitmap & bmp,wxCoord x,wxCoord y,wxCoord w,wxCoord h,wxCoord destx,wxCoord desty,int rop,bool useMask)656 void wxDFBDCImpl::DoDrawSubBitmap(const wxBitmap &bmp,
657                            wxCoord x, wxCoord y, wxCoord w, wxCoord h,
658                            wxCoord destx, wxCoord desty, int rop, bool useMask)
659 {
660     wxCHECK_RET( IsOk(), wxT("invalid dc") );
661     wxCHECK_RET( bmp.IsOk(), wxT("invalid bitmap") );
662 
663     // NB: we could also support XOR here (via DSBLIT_XOR)
664     //     and possibly others via SetSrc/DstBlendFunction()
665     wxCHECK_RET( rop == wxCOPY, "only wxCOPY function supported" );
666 
667     if ( bmp.GetDepth() == 1 )
668     {
669         // Mono bitmaps are handled in special way -- all 1s are drawn in
670         // foreground colours, all 0s in background colour.
671         wxFAIL_MSG( "drawing mono bitmaps not implemented" );
672         return;
673     }
674 
675     if ( useMask && bmp.GetMask() )
676     {
677         // FIXME_DFB: Could use blitting modes for this; also see
678         //            DFB's SetSrcBlendFunction() and SetSrcColorKey()
679         wxFAIL_MSG( "drawing bitmaps with masks not implemented" );
680         return;
681     }
682 
683     DoBlitFromSurface(bmp.GetDirectFBSurface(),
684                       x, y,
685                       w, h,
686                       destx, desty);
687 }
688 
DoBlitFromSurface(const wxIDirectFBSurfacePtr & src,wxCoord srcx,wxCoord srcy,wxCoord w,wxCoord h,wxCoord dstx,wxCoord dsty)689 bool wxDFBDCImpl::DoBlitFromSurface(const wxIDirectFBSurfacePtr& src,
690                                     wxCoord srcx, wxCoord srcy,
691                                     wxCoord w, wxCoord h,
692                                     wxCoord dstx, wxCoord dsty)
693 {
694     // don't do anything if the source rectangle is outside of source surface,
695     // DirectFB would assert in that case:
696     wxSize srcsize;
697     src->GetSize(&srcsize.x, &srcsize.y);
698     if ( !wxRect(srcx, srcy, w, h).Intersects(wxRect(srcsize)) )
699     {
700         wxLogDebug("Blitting from area outside of the source surface, caller code needs fixing.");
701         return false;
702     }
703 
704     CalcBoundingBox(dstx, dsty);
705     CalcBoundingBox(dstx + w, dsty + h);
706 
707     DFBRectangle srcRect = { srcx, srcy, w, h };
708     DFBRectangle dstRect = { XLOG2DEV(dstx), YLOG2DEV(dsty),
709                              XLOG2DEVREL(w), YLOG2DEVREL(h) };
710 
711     wxIDirectFBSurfacePtr dst(m_surface);
712 
713     // FIXME: this will have to be different in useMask case, see above
714     DFBSurfaceBlittingFlags blitFlag = (src->GetPixelFormat() == DSPF_ARGB)
715                                        ? DSBLIT_BLEND_ALPHACHANNEL
716                                        : DSBLIT_NOFX;
717     if ( !dst->SetBlittingFlags(blitFlag) )
718         return false;
719 
720     if ( srcRect.w != dstRect.w || srcRect.h != dstRect.h )
721     {
722         // the bitmap is drawn stretched:
723         dst->StretchBlit(src, &srcRect, &dstRect);
724     }
725     else
726     {
727         // no stretching, size is preserved:
728         dst->Blit(src, &srcRect, dstRect.x, dstRect.y);
729     }
730 
731     return true;
732 }
733