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