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