]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/dc.cpp
1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ===========================================================================
14 // ===========================================================================
16 // ---------------------------------------------------------------------------
18 // ---------------------------------------------------------------------------
21 #pragma implementation "dc.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
32 #include "wx/window.h"
35 #include "wx/dialog.h"
37 #include "wx/bitmap.h"
38 #include "wx/dcmemory.h"
43 #include "wx/sysopt.h"
44 #include "wx/dcprint.h"
45 #include "wx/module.h"
50 #include "wx/msw/private.h" // needs to be before #include <commdlg.h>
52 #if wxUSE_COMMON_DIALOGS
60 IMPLEMENT_ABSTRACT_CLASS(wxDC
, wxDCBase
)
62 // ---------------------------------------------------------------------------
64 // ---------------------------------------------------------------------------
66 static const int VIEWPORT_EXTENT
= 1000;
68 static const int MM_POINTS
= 9;
69 static const int MM_METRIC
= 10;
71 // usually this is defined in math.h
73 static const double M_PI
= 3.14159265358979323846;
76 // ROPs which don't have standard names (see "Ternary Raster Operations" in the
77 // MSDN docs for how this and other numbers in wxDC::Blit() are obtained)
78 #define DSTCOPY 0x00AA0029 // a.k.a. NOP operation
80 // ---------------------------------------------------------------------------
82 // ---------------------------------------------------------------------------
84 // convert degrees to radians
85 static inline double DegToRad(double deg
) { return (deg
* M_PI
) / 180.0; }
87 // ----------------------------------------------------------------------------
89 // ----------------------------------------------------------------------------
91 // instead of duplicating the same code which sets and then restores text
92 // colours in each wxDC method working with wxSTIPPLE_MASK_OPAQUE brushes,
93 // encapsulate this in a small helper class
95 // wxColourChanger: changes the text colours in the ctor if required and
96 // restores them in the dtor
100 wxColourChanger(wxDC
& dc
);
106 COLORREF m_colFgOld
, m_colBgOld
;
111 // ===========================================================================
113 // ===========================================================================
115 // ----------------------------------------------------------------------------
117 // ----------------------------------------------------------------------------
119 wxColourChanger::wxColourChanger(wxDC
& dc
) : m_dc(dc
)
121 if ( dc
.GetBrush().GetStyle() == wxSTIPPLE_MASK_OPAQUE
)
123 HDC hdc
= GetHdcOf(dc
);
124 m_colFgOld
= ::GetTextColor(hdc
);
125 m_colBgOld
= ::GetBkColor(hdc
);
127 // note that Windows convention is opposite to wxWindows one, this is
128 // why text colour becomes the background one and vice versa
129 const wxColour
& colFg
= dc
.GetTextForeground();
132 ::SetBkColor(hdc
, colFg
.GetPixel());
135 const wxColour
& colBg
= dc
.GetTextBackground();
138 ::SetTextColor(hdc
, colBg
.GetPixel());
142 dc
.GetBackgroundMode() == wxTRANSPARENT
? TRANSPARENT
145 // flag which telsl us to undo changes in the dtor
150 // nothing done, nothing to undo
155 wxColourChanger::~wxColourChanger()
159 // restore the colours we changed
160 HDC hdc
= GetHdcOf(m_dc
);
162 ::SetBkMode(hdc
, TRANSPARENT
);
163 ::SetTextColor(hdc
, m_colFgOld
);
164 ::SetBkColor(hdc
, m_colBgOld
);
168 // ---------------------------------------------------------------------------
170 // ---------------------------------------------------------------------------
172 // Default constructor
186 m_windowExtX
= VIEWPORT_EXTENT
;
187 m_windowExtY
= VIEWPORT_EXTENT
;
195 SelectOldObjects(m_hDC
);
197 // if we own the HDC, we delete it, otherwise we just release it
201 ::DeleteDC(GetHdc());
203 else // we don't own our HDC
207 ::ReleaseDC(GetHwndOf(m_canvas
), GetHdc());
211 // Must have been a wxScreenDC
212 ::ReleaseDC((HWND
) NULL
, GetHdc());
218 // This will select current objects out of the DC,
219 // which is what you have to do before deleting the
221 void wxDC::SelectOldObjects(WXHDC dc
)
227 ::SelectObject((HDC
) dc
, (HBITMAP
) m_oldBitmap
);
228 if (m_selectedBitmap
.Ok())
230 m_selectedBitmap
.SetSelectedInto(NULL
);
236 ::SelectObject((HDC
) dc
, (HPEN
) m_oldPen
);
241 ::SelectObject((HDC
) dc
, (HBRUSH
) m_oldBrush
);
246 ::SelectObject((HDC
) dc
, (HFONT
) m_oldFont
);
251 ::SelectPalette((HDC
) dc
, (HPALETTE
) m_oldPalette
, TRUE
);
256 m_brush
= wxNullBrush
;
258 m_palette
= wxNullPalette
;
260 m_backgroundBrush
= wxNullBrush
;
261 m_selectedBitmap
= wxNullBitmap
;
264 // ---------------------------------------------------------------------------
266 // ---------------------------------------------------------------------------
268 void wxDC::UpdateClipBox()
270 #ifdef __WXMICROWIN__
271 if (!GetHDC()) return;
275 GetClipBox(GetHdc(), &rect
);
277 m_clipX1
= (wxCoord
) XDEV2LOG(rect
.left
);
278 m_clipY1
= (wxCoord
) YDEV2LOG(rect
.top
);
279 m_clipX2
= (wxCoord
) XDEV2LOG(rect
.right
);
280 m_clipY2
= (wxCoord
) YDEV2LOG(rect
.bottom
);
283 void wxDC::DoSetClippingRegion(wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
)
285 #ifdef __WXMICROWIN__
286 if (!GetHDC()) return;
291 // the region coords are always the device ones, so do the translation
294 // FIXME: possible +/-1 error here, to check!
295 HRGN hrgn
= ::CreateRectRgn(LogicalToDeviceX(x
),
297 LogicalToDeviceX(x
+ w
),
298 LogicalToDeviceY(y
+ h
));
301 wxLogLastError(_T("CreateRectRgn"));
305 if ( ::SelectClipRgn(GetHdc(), hrgn
) == ERROR
)
307 wxLogLastError(_T("SelectClipRgn"));
315 void wxDC::DoSetClippingRegionAsRegion(const wxRegion
& region
)
317 #ifdef __WXMICROWIN__
318 if (!GetHDC()) return;
321 wxCHECK_RET( GetHrgnOf(region
), wxT("invalid clipping region") );
326 SelectClipRgn(GetHdc(), GetHrgnOf(region
));
328 ExtSelectClipRgn(GetHdc(), GetHrgnOf(region
), RGN_AND
);
334 void wxDC::DestroyClippingRegion()
336 #ifdef __WXMICROWIN__
337 if (!GetHDC()) return;
340 if (m_clipping
&& m_hDC
)
342 // TODO: this should restore the previous clipping region,
343 // so that OnPaint processing works correctly, and the update clipping region
344 // doesn't get destroyed after the first DestroyClippingRegion.
345 HRGN rgn
= CreateRectRgn(0, 0, 32000, 32000);
346 SelectClipRgn(GetHdc(), rgn
);
353 // ---------------------------------------------------------------------------
354 // query capabilities
355 // ---------------------------------------------------------------------------
357 bool wxDC::CanDrawBitmap() const
362 bool wxDC::CanGetTextExtent() const
364 #ifdef __WXMICROWIN__
365 // TODO Extend MicroWindows' GetDeviceCaps function
368 // What sort of display is it?
369 int technology
= ::GetDeviceCaps(GetHdc(), TECHNOLOGY
);
371 return (technology
== DT_RASDISPLAY
) || (technology
== DT_RASPRINTER
);
375 int wxDC::GetDepth() const
377 #ifdef __WXMICROWIN__
378 if (!GetHDC()) return 16;
381 return (int)::GetDeviceCaps(GetHdc(), BITSPIXEL
);
384 // ---------------------------------------------------------------------------
386 // ---------------------------------------------------------------------------
390 #ifdef __WXMICROWIN__
391 if (!GetHDC()) return;
397 GetClientRect((HWND
) m_canvas
->GetHWND(), &rect
);
401 // No, I think we should simply ignore this if printing on e.g.
403 // wxCHECK_RET( m_selectedBitmap.Ok(), wxT("this DC can't be cleared") );
404 if (!m_selectedBitmap
.Ok())
407 rect
.left
= 0; rect
.top
= 0;
408 rect
.right
= m_selectedBitmap
.GetWidth();
409 rect
.bottom
= m_selectedBitmap
.GetHeight();
412 (void) ::SetMapMode(GetHdc(), MM_TEXT
);
414 DWORD colour
= GetBkColor(GetHdc());
415 HBRUSH brush
= CreateSolidBrush(colour
);
416 FillRect(GetHdc(), &rect
, brush
);
419 ::SetMapMode(GetHdc(), MM_ANISOTROPIC
);
420 ::SetViewportExtEx(GetHdc(), VIEWPORT_EXTENT
, VIEWPORT_EXTENT
, NULL
);
421 ::SetWindowExtEx(GetHdc(), m_windowExtX
, m_windowExtY
, NULL
);
422 ::SetViewportOrgEx(GetHdc(), (int)m_deviceOriginX
, (int)m_deviceOriginY
, NULL
);
423 ::SetWindowOrgEx(GetHdc(), (int)m_logicalOriginX
, (int)m_logicalOriginY
, NULL
);
426 void wxDC::DoFloodFill(wxCoord x
, wxCoord y
, const wxColour
& col
, int style
)
428 #ifdef __WXMICROWIN__
429 if (!GetHDC()) return;
432 if ( !::ExtFloodFill(GetHdc(), XLOG2DEV(x
), YLOG2DEV(y
),
434 style
== wxFLOOD_SURFACE
? FLOODFILLSURFACE
437 // quoting from the MSDN docs:
439 // Following are some of the reasons this function might fail:
441 // * The filling could not be completed.
442 // * The specified point has the boundary color specified by the
443 // crColor parameter (if FLOODFILLBORDER was requested).
444 // * The specified point does not have the color specified by
445 // crColor (if FLOODFILLSURFACE was requested)
446 // * The point is outside the clipping region that is, it is not
447 // visible on the device.
449 wxLogLastError(wxT("ExtFloodFill"));
452 CalcBoundingBox(x
, y
);
455 bool wxDC::DoGetPixel(wxCoord x
, wxCoord y
, wxColour
*col
) const
457 #ifdef __WXMICROWIN__
458 if (!GetHDC()) return FALSE
;
461 wxCHECK_MSG( col
, FALSE
, _T("NULL colour parameter in wxDC::GetPixel") );
463 // get the color of the pixel
464 COLORREF pixelcolor
= ::GetPixel(GetHdc(), XLOG2DEV(x
), YLOG2DEV(y
));
466 wxRGBToColour(*col
, pixelcolor
);
471 void wxDC::DoCrossHair(wxCoord x
, wxCoord y
)
473 #ifdef __WXMICROWIN__
474 if (!GetHDC()) return;
477 wxCoord x1
= x
-VIEWPORT_EXTENT
;
478 wxCoord y1
= y
-VIEWPORT_EXTENT
;
479 wxCoord x2
= x
+VIEWPORT_EXTENT
;
480 wxCoord y2
= y
+VIEWPORT_EXTENT
;
482 (void)MoveToEx(GetHdc(), XLOG2DEV(x1
), YLOG2DEV(y
), NULL
);
483 (void)LineTo(GetHdc(), XLOG2DEV(x2
), YLOG2DEV(y
));
485 (void)MoveToEx(GetHdc(), XLOG2DEV(x
), YLOG2DEV(y1
), NULL
);
486 (void)LineTo(GetHdc(), XLOG2DEV(x
), YLOG2DEV(y2
));
488 CalcBoundingBox(x1
, y1
);
489 CalcBoundingBox(x2
, y2
);
492 void wxDC::DoDrawLine(wxCoord x1
, wxCoord y1
, wxCoord x2
, wxCoord y2
)
494 #ifdef __WXMICROWIN__
495 if (!GetHDC()) return;
498 (void)MoveToEx(GetHdc(), XLOG2DEV(x1
), YLOG2DEV(y1
), NULL
);
499 (void)LineTo(GetHdc(), XLOG2DEV(x2
), YLOG2DEV(y2
));
501 // Normalization: Windows doesn't draw the last point of the line.
502 // But apparently neither does GTK+, so we take it out again.
503 // (void)LineTo(GetHdc(), XLOG2DEV(x2) + 1, YLOG2DEV(y2));
505 CalcBoundingBox(x1
, y1
);
506 CalcBoundingBox(x2
, y2
);
509 // Draws an arc of a circle, centred on (xc, yc), with starting point (x1, y1)
510 // and ending at (x2, y2)
511 void wxDC::DoDrawArc(wxCoord x1
, wxCoord y1
,
512 wxCoord x2
, wxCoord y2
,
513 wxCoord xc
, wxCoord yc
)
515 #ifdef __WXMICROWIN__
516 if (!GetHDC()) return;
519 wxColourChanger
cc(*this); // needed for wxSTIPPLE_MASK_OPAQUE handling
523 double radius
= (double)sqrt(dx
*dx
+dy
*dy
);
524 wxCoord r
= (wxCoord
)radius
;
526 // treat the special case of full circle separately
527 if ( x1
== x2
&& y1
== y2
)
529 DrawEllipse(xc
- r
, yc
- r
, 2*r
, 2*r
);
533 wxCoord xx1
= XLOG2DEV(x1
);
534 wxCoord yy1
= YLOG2DEV(y1
);
535 wxCoord xx2
= XLOG2DEV(x2
);
536 wxCoord yy2
= YLOG2DEV(y2
);
537 wxCoord xxc
= XLOG2DEV(xc
);
538 wxCoord yyc
= YLOG2DEV(yc
);
539 wxCoord ray
= (wxCoord
) sqrt(double((xxc
-xx1
)*(xxc
-xx1
)+(yyc
-yy1
)*(yyc
-yy1
)));
541 wxCoord xxx1
= (wxCoord
) (xxc
-ray
);
542 wxCoord yyy1
= (wxCoord
) (yyc
-ray
);
543 wxCoord xxx2
= (wxCoord
) (xxc
+ray
);
544 wxCoord yyy2
= (wxCoord
) (yyc
+ray
);
546 if ( m_brush
.Ok() && m_brush
.GetStyle() != wxTRANSPARENT
)
548 // Have to add 1 to bottom-right corner of rectangle
549 // to make semi-circles look right (crooked line otherwise).
550 // Unfortunately this is not a reliable method, depends
551 // on the size of shape.
552 // TODO: figure out why this happens!
553 Pie(GetHdc(),xxx1
,yyy1
,xxx2
+1,yyy2
+1, xx1
,yy1
,xx2
,yy2
);
557 Arc(GetHdc(),xxx1
,yyy1
,xxx2
,yyy2
, xx1
,yy1
,xx2
,yy2
);
560 CalcBoundingBox(xc
- r
, yc
- r
);
561 CalcBoundingBox(xc
+ r
, yc
+ r
);
564 void wxDC::DoDrawCheckMark(wxCoord x1
, wxCoord y1
,
565 wxCoord width
, wxCoord height
)
567 #ifdef __WXMICROWIN__
568 if (!GetHDC()) return;
571 wxCoord x2
= x1
+ width
,
574 #if defined(__WIN32__) && !defined(__SC__) && !defined(__WXMICROWIN__)
581 DrawFrameControl(GetHdc(), &rect
, DFC_MENU
, DFCS_MENUCHECK
);
583 // In WIN16, draw a cross
584 HPEN blackPen
= ::CreatePen(PS_SOLID
, 1, RGB(0, 0, 0));
585 HPEN whiteBrush
= (HPEN
)::GetStockObject(WHITE_BRUSH
);
586 HPEN hPenOld
= (HPEN
)::SelectObject(GetHdc(), blackPen
);
587 HPEN hBrushOld
= (HPEN
)::SelectObject(GetHdc(), whiteBrush
);
588 ::SetROP2(GetHdc(), R2_COPYPEN
);
589 Rectangle(GetHdc(), x1
, y1
, x2
, y2
);
590 MoveToEx(GetHdc(), x1
, y1
, NULL
);
591 LineTo(GetHdc(), x2
, y2
);
592 MoveToEx(GetHdc(), x2
, y1
, NULL
);
593 LineTo(GetHdc(), x1
, y2
);
594 ::SelectObject(GetHdc(), hPenOld
);
595 ::SelectObject(GetHdc(), hBrushOld
);
596 ::DeleteObject(blackPen
);
599 CalcBoundingBox(x1
, y1
);
600 CalcBoundingBox(x2
, y2
);
603 void wxDC::DoDrawPoint(wxCoord x
, wxCoord y
)
605 #ifdef __WXMICROWIN__
606 if (!GetHDC()) return;
609 COLORREF color
= 0x00ffffff;
612 color
= m_pen
.GetColour().GetPixel();
615 SetPixel(GetHdc(), XLOG2DEV(x
), YLOG2DEV(y
), color
);
617 CalcBoundingBox(x
, y
);
620 void wxDC::DoDrawPolygon(int n
, wxPoint points
[], wxCoord xoffset
, wxCoord yoffset
,int fillStyle
)
622 #ifdef __WXMICROWIN__
623 if (!GetHDC()) return;
626 wxColourChanger
cc(*this); // needed for wxSTIPPLE_MASK_OPAQUE handling
628 // Do things less efficiently if we have offsets
629 if (xoffset
!= 0 || yoffset
!= 0)
631 POINT
*cpoints
= new POINT
[n
];
633 for (i
= 0; i
< n
; i
++)
635 cpoints
[i
].x
= (int)(points
[i
].x
+ xoffset
);
636 cpoints
[i
].y
= (int)(points
[i
].y
+ yoffset
);
638 CalcBoundingBox(cpoints
[i
].x
, cpoints
[i
].y
);
640 int prev
= SetPolyFillMode(GetHdc(),fillStyle
==wxODDEVEN_RULE
?ALTERNATE
:WINDING
);
641 (void)Polygon(GetHdc(), cpoints
, n
);
642 SetPolyFillMode(GetHdc(),prev
);
648 for (i
= 0; i
< n
; i
++)
649 CalcBoundingBox(points
[i
].x
, points
[i
].y
);
651 int prev
= SetPolyFillMode(GetHdc(),fillStyle
==wxODDEVEN_RULE
?ALTERNATE
:WINDING
);
652 (void)Polygon(GetHdc(), (POINT
*) points
, n
);
653 SetPolyFillMode(GetHdc(),prev
);
657 void wxDC::DoDrawLines(int n
, wxPoint points
[], wxCoord xoffset
, wxCoord yoffset
)
659 #ifdef __WXMICROWIN__
660 if (!GetHDC()) return;
663 // Do things less efficiently if we have offsets
664 if (xoffset
!= 0 || yoffset
!= 0)
666 POINT
*cpoints
= new POINT
[n
];
668 for (i
= 0; i
< n
; i
++)
670 cpoints
[i
].x
= (int)(points
[i
].x
+ xoffset
);
671 cpoints
[i
].y
= (int)(points
[i
].y
+ yoffset
);
673 CalcBoundingBox(cpoints
[i
].x
, cpoints
[i
].y
);
675 (void)Polyline(GetHdc(), cpoints
, n
);
681 for (i
= 0; i
< n
; i
++)
682 CalcBoundingBox(points
[i
].x
, points
[i
].y
);
684 (void)Polyline(GetHdc(), (POINT
*) points
, n
);
688 void wxDC::DoDrawRectangle(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
690 #ifdef __WXMICROWIN__
691 if (!GetHDC()) return;
694 wxColourChanger
cc(*this); // needed for wxSTIPPLE_MASK_OPAQUE handling
696 wxCoord x2
= x
+ width
;
697 wxCoord y2
= y
+ height
;
699 if ((m_logicalFunction
== wxCOPY
) && (m_pen
.GetStyle() == wxTRANSPARENT
))
702 rect
.left
= XLOG2DEV(x
);
703 rect
.top
= YLOG2DEV(y
);
704 rect
.right
= XLOG2DEV(x2
);
705 rect
.bottom
= YLOG2DEV(y2
);
706 (void)FillRect(GetHdc(), &rect
, (HBRUSH
)m_brush
.GetResourceHandle() );
710 // Windows draws the filled rectangles without outline (i.e. drawn with a
711 // transparent pen) one pixel smaller in both directions and we want them
712 // to have the same size regardless of which pen is used - adjust
714 // I wonder if this shouldn´t be done after the LOG2DEV() conversions. RR.
715 if ( m_pen
.GetStyle() == wxTRANSPARENT
)
721 (void)Rectangle(GetHdc(), XLOG2DEV(x
), YLOG2DEV(y
), XLOG2DEV(x2
), YLOG2DEV(y2
));
725 CalcBoundingBox(x
, y
);
726 CalcBoundingBox(x2
, y2
);
729 void wxDC::DoDrawRoundedRectangle(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
, double radius
)
731 #ifdef __WXMICROWIN__
732 if (!GetHDC()) return;
735 wxColourChanger
cc(*this); // needed for wxSTIPPLE_MASK_OPAQUE handling
737 // Now, a negative radius value is interpreted to mean
738 // 'the proportion of the smallest X or Y dimension'
742 double smallest
= 0.0;
747 radius
= (- radius
* smallest
);
750 wxCoord x2
= (x
+width
);
751 wxCoord y2
= (y
+height
);
753 // Windows draws the filled rectangles without outline (i.e. drawn with a
754 // transparent pen) one pixel smaller in both directions and we want them
755 // to have the same size regardless of which pen is used - adjust
756 if ( m_pen
.GetStyle() == wxTRANSPARENT
)
762 (void)RoundRect(GetHdc(), XLOG2DEV(x
), YLOG2DEV(y
), XLOG2DEV(x2
),
763 YLOG2DEV(y2
), (int) (2*XLOG2DEV(radius
)), (int)( 2*YLOG2DEV(radius
)));
765 CalcBoundingBox(x
, y
);
766 CalcBoundingBox(x2
, y2
);
769 void wxDC::DoDrawEllipse(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
771 #ifdef __WXMICROWIN__
772 if (!GetHDC()) return;
775 wxColourChanger
cc(*this); // needed for wxSTIPPLE_MASK_OPAQUE handling
777 wxCoord x2
= (x
+width
);
778 wxCoord y2
= (y
+height
);
780 (void)Ellipse(GetHdc(), XLOG2DEV(x
), YLOG2DEV(y
), XLOG2DEV(x2
), YLOG2DEV(y2
));
782 CalcBoundingBox(x
, y
);
783 CalcBoundingBox(x2
, y2
);
786 // Chris Breeze 20/5/98: first implementation of DrawEllipticArc on Windows
787 void wxDC::DoDrawEllipticArc(wxCoord x
,wxCoord y
,wxCoord w
,wxCoord h
,double sa
,double ea
)
789 #ifdef __WXMICROWIN__
790 if (!GetHDC()) return;
793 wxColourChanger
cc(*this); // needed for wxSTIPPLE_MASK_OPAQUE handling
798 int rx1
= XLOG2DEV(x
+w
/2);
799 int ry1
= YLOG2DEV(y
+h
/2);
806 rx1
+= (int)(100.0 * abs(w
) * cos(sa
));
807 ry1
-= (int)(100.0 * abs(h
) * m_signY
* sin(sa
));
808 rx2
+= (int)(100.0 * abs(w
) * cos(ea
));
809 ry2
-= (int)(100.0 * abs(h
) * m_signY
* sin(ea
));
811 // draw pie with NULL_PEN first and then outline otherwise a line is
812 // drawn from the start and end points to the centre
813 HPEN hpenOld
= (HPEN
) ::SelectObject(GetHdc(), (HPEN
) ::GetStockObject(NULL_PEN
));
816 (void)Pie(GetHdc(), XLOG2DEV(x
), YLOG2DEV(y
), XLOG2DEV(x2
)+1, YLOG2DEV(y2
)+1,
821 (void)Pie(GetHdc(), XLOG2DEV(x
), YLOG2DEV(y
)-1, XLOG2DEV(x2
)+1, YLOG2DEV(y2
),
822 rx1
, ry1
-1, rx2
, ry2
-1);
825 ::SelectObject(GetHdc(), hpenOld
);
827 (void)Arc(GetHdc(), XLOG2DEV(x
), YLOG2DEV(y
), XLOG2DEV(x2
), YLOG2DEV(y2
),
830 CalcBoundingBox(x
, y
);
831 CalcBoundingBox(x2
, y2
);
834 void wxDC::DoDrawIcon(const wxIcon
& icon
, wxCoord x
, wxCoord y
)
836 #ifdef __WXMICROWIN__
837 if (!GetHDC()) return;
840 wxCHECK_RET( icon
.Ok(), wxT("invalid icon in DrawIcon") );
843 ::DrawIconEx(GetHdc(), XLOG2DEV(x
), YLOG2DEV(y
), GetHiconOf(icon
), icon
.GetWidth(), icon
.GetHeight(), 0, NULL
, DI_NORMAL
);
845 ::DrawIcon(GetHdc(), XLOG2DEV(x
), YLOG2DEV(y
), GetHiconOf(icon
));
848 CalcBoundingBox(x
, y
);
849 CalcBoundingBox(x
+ icon
.GetWidth(), y
+ icon
.GetHeight());
852 void wxDC::DoDrawBitmap( const wxBitmap
&bmp
, wxCoord x
, wxCoord y
, bool useMask
)
854 #ifdef __WXMICROWIN__
855 if (!GetHDC()) return;
858 wxCHECK_RET( bmp
.Ok(), _T("invalid bitmap in wxDC::DrawBitmap") );
860 int width
= bmp
.GetWidth(),
861 height
= bmp
.GetHeight();
863 HBITMAP hbmpMask
= 0;
867 wxMask
*mask
= bmp
.GetMask();
869 hbmpMask
= (HBITMAP
)mask
->GetMaskBitmap();
873 // don't give assert here because this would break existing
874 // programs - just silently ignore useMask parameter
882 // use MaskBlt() with ROP which doesn't do anything to dst in the mask
884 // On some systems, MaskBlt succeeds yet is much much slower
885 // than the wxWindows fall-back implementation. So we need
886 // to be able to switch this on and off at runtime.
888 #if wxUSE_SYSTEM_OPTIONS
889 if (wxSystemOptions::GetOptionInt(wxT("no-maskblt")) == 0)
892 HDC hdcMem
= ::CreateCompatibleDC(GetHdc());
893 ::SelectObject(hdcMem
, GetHbitmapOf(bmp
));
895 ok
= ::MaskBlt(GetHdc(), x
, y
, width
, height
,
898 MAKEROP4(SRCCOPY
, DSTCOPY
)) != 0;
905 // Rather than reproduce wxDC::Blit, let's do it at the wxWin API
908 memDC
.SelectObject(bmp
);
910 Blit(x
, y
, width
, height
, &memDC
, 0, 0, wxCOPY
, useMask
);
912 memDC
.SelectObject(wxNullBitmap
);
915 else // no mask, just use BitBlt()
918 HDC memdc
= ::CreateCompatibleDC( cdc
);
919 HBITMAP hbitmap
= (HBITMAP
) bmp
.GetHBITMAP( );
921 wxASSERT_MSG( hbitmap
, wxT("bitmap is ok but HBITMAP is NULL?") );
923 COLORREF old_textground
= ::GetTextColor(GetHdc());
924 COLORREF old_background
= ::GetBkColor(GetHdc());
925 if (m_textForegroundColour
.Ok())
927 ::SetTextColor(GetHdc(), m_textForegroundColour
.GetPixel() );
929 if (m_textBackgroundColour
.Ok())
931 ::SetBkColor(GetHdc(), m_textBackgroundColour
.GetPixel() );
934 ::SelectObject( memdc
, hbitmap
);
935 ::BitBlt( cdc
, x
, y
, width
, height
, memdc
, 0, 0, SRCCOPY
);
938 ::SetTextColor(GetHdc(), old_textground
);
939 ::SetBkColor(GetHdc(), old_background
);
943 void wxDC::DoDrawText(const wxString
& text
, wxCoord x
, wxCoord y
)
945 #ifdef __WXMICROWIN__
946 if (!GetHDC()) return;
949 DrawAnyText(text
, x
, y
);
951 // update the bounding box
952 CalcBoundingBox(x
, y
);
955 GetTextExtent(text
, &w
, &h
);
956 CalcBoundingBox(x
+ w
, y
+ h
);
959 void wxDC::DrawAnyText(const wxString
& text
, wxCoord x
, wxCoord y
)
961 #ifdef __WXMICROWIN__
962 if (!GetHDC()) return;
965 // prepare for drawing the text
966 if ( m_textForegroundColour
.Ok() )
967 SetTextColor(GetHdc(), m_textForegroundColour
.GetPixel());
969 DWORD old_background
= 0;
970 if ( m_textBackgroundColour
.Ok() )
972 old_background
= SetBkColor(GetHdc(), m_textBackgroundColour
.GetPixel() );
975 SetBkMode(GetHdc(), m_backgroundMode
== wxTRANSPARENT
? TRANSPARENT
978 if ( ::TextOut(GetHdc(), XLOG2DEV(x
), YLOG2DEV(y
),
979 text
.c_str(), text
.length()) == 0 )
981 wxLogLastError(wxT("TextOut"));
984 // restore the old parameters (text foreground colour may be left because
985 // it never is set to anything else, but background should remain
986 // transparent even if we just drew an opaque string)
987 if ( m_textBackgroundColour
.Ok() )
988 (void)SetBkColor(GetHdc(), old_background
);
990 SetBkMode(GetHdc(), TRANSPARENT
);
993 void wxDC::DoDrawRotatedText(const wxString
& text
,
994 wxCoord x
, wxCoord y
,
997 #ifdef __WXMICROWIN__
998 if (!GetHDC()) return;
1001 // we test that we have some font because otherwise we should still use the
1002 // "else" part below to avoid that DrawRotatedText(angle = 180) and
1003 // DrawRotatedText(angle = 0) use different fonts (we can't use the default
1004 // font for drawing rotated fonts unfortunately)
1005 if ( (angle
== 0.0) && m_font
.Ok() )
1007 DoDrawText(text
, x
, y
);
1009 #ifndef __WXMICROWIN__
1012 // NB: don't take DEFAULT_GUI_FONT (a.k.a. wxSYS_DEFAULT_GUI_FONT)
1013 // because it's not TrueType and so can't have non zero
1014 // orientation/escapement under Win9x
1015 wxFont font
= m_font
.Ok() ? m_font
: *wxSWISS_FONT
;
1016 HFONT hfont
= (HFONT
)font
.GetResourceHandle();
1018 if ( ::GetObject(hfont
, sizeof(lf
), &lf
) == 0 )
1020 wxLogLastError(wxT("GetObject(hfont)"));
1023 // GDI wants the angle in tenth of degree
1024 long angle10
= (long)(angle
* 10);
1025 lf
.lfEscapement
= angle10
;
1026 lf
. lfOrientation
= angle10
;
1028 hfont
= ::CreateFontIndirect(&lf
);
1031 wxLogLastError(wxT("CreateFont"));
1035 HFONT hfontOld
= (HFONT
)::SelectObject(GetHdc(), hfont
);
1037 DrawAnyText(text
, x
, y
);
1039 (void)::SelectObject(GetHdc(), hfontOld
);
1040 (void)::DeleteObject(hfont
);
1043 // call the bounding box by adding all four vertices of the rectangle
1044 // containing the text to it (simpler and probably not slower than
1045 // determining which of them is really topmost/leftmost/...)
1047 GetTextExtent(text
, &w
, &h
);
1049 double rad
= DegToRad(angle
);
1051 // "upper left" and "upper right"
1052 CalcBoundingBox(x
, y
);
1053 CalcBoundingBox(x
+ w
*cos(rad
), y
- h
*sin(rad
));
1055 // "bottom left" and "bottom right"
1056 x
+= (wxCoord
)(h
*sin(rad
));
1057 y
+= (wxCoord
)(h
*cos(rad
));
1058 CalcBoundingBox(x
, y
);
1059 CalcBoundingBox(x
+ h
*sin(rad
), y
+ h
*cos(rad
));
1064 // ---------------------------------------------------------------------------
1066 // ---------------------------------------------------------------------------
1068 void wxDC::SetPalette(const wxPalette
& palette
)
1070 #ifdef __WXMICROWIN__
1071 if (!GetHDC()) return;
1074 // Set the old object temporarily, in case the assignment deletes an object
1075 // that's not yet selected out.
1078 ::SelectPalette(GetHdc(), (HPALETTE
) m_oldPalette
, TRUE
);
1082 m_palette
= palette
;
1084 if (!m_palette
.Ok())
1086 // Setting a NULL colourmap is a way of restoring
1087 // the original colourmap
1090 ::SelectPalette(GetHdc(), (HPALETTE
) m_oldPalette
, TRUE
);
1097 if (m_palette
.Ok() && m_palette
.GetHPALETTE())
1099 HPALETTE oldPal
= ::SelectPalette(GetHdc(), (HPALETTE
) m_palette
.GetHPALETTE(), TRUE
);
1101 m_oldPalette
= (WXHPALETTE
) oldPal
;
1103 ::RealizePalette(GetHdc());
1107 void wxDC::SetFont(const wxFont
& the_font
)
1109 #ifdef __WXMICROWIN__
1110 if (!GetHDC()) return;
1113 // Set the old object temporarily, in case the assignment deletes an object
1114 // that's not yet selected out.
1117 ::SelectObject(GetHdc(), (HFONT
) m_oldFont
);
1126 ::SelectObject(GetHdc(), (HFONT
) m_oldFont
);
1130 if (m_font
.Ok() && m_font
.GetResourceHandle())
1132 HFONT f
= (HFONT
) ::SelectObject(GetHdc(), (HFONT
) m_font
.GetResourceHandle());
1133 if (f
== (HFONT
) NULL
)
1135 wxLogDebug(wxT("::SelectObject failed in wxDC::SetFont."));
1138 m_oldFont
= (WXHFONT
) f
;
1142 void wxDC::SetPen(const wxPen
& pen
)
1144 #ifdef __WXMICROWIN__
1145 if (!GetHDC()) return;
1148 // Set the old object temporarily, in case the assignment deletes an object
1149 // that's not yet selected out.
1152 ::SelectObject(GetHdc(), (HPEN
) m_oldPen
);
1161 ::SelectObject(GetHdc(), (HPEN
) m_oldPen
);
1167 if (m_pen
.GetResourceHandle())
1169 HPEN p
= (HPEN
) ::SelectObject(GetHdc(), (HPEN
)m_pen
.GetResourceHandle());
1171 m_oldPen
= (WXHPEN
) p
;
1176 void wxDC::SetBrush(const wxBrush
& brush
)
1178 #ifdef __WXMICROWIN__
1179 if (!GetHDC()) return;
1182 // Set the old object temporarily, in case the assignment deletes an object
1183 // that's not yet selected out.
1186 ::SelectObject(GetHdc(), (HBRUSH
) m_oldBrush
);
1195 ::SelectObject(GetHdc(), (HBRUSH
) m_oldBrush
);
1201 // to make sure the brush is alligned with the logical coordinates
1202 wxBitmap
*stipple
= m_brush
.GetStipple();
1203 if ( stipple
&& stipple
->Ok() )
1206 ::SetBrushOrgEx(GetHdc(),
1207 m_deviceOriginX
% stipple
->GetWidth(),
1208 m_deviceOriginY
% stipple
->GetHeight(),
1209 NULL
); // don't need previous brush origin
1211 ::SetBrushOrg(GetHdc(),
1212 m_deviceOriginX
% stipple
->GetWidth(),
1213 m_deviceOriginY
% stipple
->GetHeight());
1217 if ( m_brush
.GetResourceHandle() )
1220 b
= (HBRUSH
) ::SelectObject(GetHdc(), (HBRUSH
)m_brush
.GetResourceHandle());
1222 m_oldBrush
= (WXHBRUSH
) b
;
1227 void wxDC::SetBackground(const wxBrush
& brush
)
1229 #ifdef __WXMICROWIN__
1230 if (!GetHDC()) return;
1233 m_backgroundBrush
= brush
;
1235 if (!m_backgroundBrush
.Ok())
1240 bool customColours
= TRUE
;
1241 // If we haven't specified wxUSER_COLOURS, don't allow the panel/dialog box to
1242 // change background colours from the control-panel specified colours.
1243 if (m_canvas
->IsKindOf(CLASSINFO(wxWindow
)) && ((m_canvas
->GetWindowStyleFlag() & wxUSER_COLOURS
) != wxUSER_COLOURS
))
1244 customColours
= FALSE
;
1248 if (m_backgroundBrush
.GetStyle()==wxTRANSPARENT
)
1250 m_canvas
->SetTransparent(TRUE
);
1254 // New behaviour, 10/2/99: setting the background brush of a DC
1255 // doesn't affect the window background colour. However,
1256 // I'm leaving in the transparency setting because it's needed by
1257 // various controls (e.g. wxStaticText) to determine whether to draw
1258 // transparently or not. TODO: maybe this should be a new function
1259 // wxWindow::SetTransparency(). Should that apply to the child itself, or the
1261 // m_canvas->SetBackgroundColour(m_backgroundBrush.GetColour());
1262 m_canvas
->SetTransparent(FALSE
);
1266 COLORREF new_color
= m_backgroundBrush
.GetColour().GetPixel();
1268 (void)SetBkColor(GetHdc(), new_color
);
1272 void wxDC::SetBackgroundMode(int mode
)
1274 #ifdef __WXMICROWIN__
1275 if (!GetHDC()) return;
1278 m_backgroundMode
= mode
;
1280 // SetBackgroundColour now only refers to text background
1281 // and m_backgroundMode is used there
1284 if (m_backgroundMode == wxTRANSPARENT)
1285 ::SetBkMode(GetHdc(), TRANSPARENT);
1287 ::SetBkMode(GetHdc(), OPAQUE);
1288 Last change: AC 29 Jan 101 8:54 pm
1292 void wxDC::SetLogicalFunction(int function
)
1294 #ifdef __WXMICROWIN__
1295 if (!GetHDC()) return;
1298 m_logicalFunction
= function
;
1303 void wxDC::SetRop(WXHDC dc
)
1305 if ( !dc
|| m_logicalFunction
< 0 )
1310 switch (m_logicalFunction
)
1312 case wxCLEAR
: rop
= R2_BLACK
; break;
1313 case wxXOR
: rop
= R2_XORPEN
; break;
1314 case wxINVERT
: rop
= R2_NOT
; break;
1315 case wxOR_REVERSE
: rop
= R2_MERGEPENNOT
; break;
1316 case wxAND_REVERSE
: rop
= R2_MASKPENNOT
; break;
1317 case wxCOPY
: rop
= R2_COPYPEN
; break;
1318 case wxAND
: rop
= R2_MASKPEN
; break;
1319 case wxAND_INVERT
: rop
= R2_MASKNOTPEN
; break;
1320 case wxNO_OP
: rop
= R2_NOP
; break;
1321 case wxNOR
: rop
= R2_NOTMERGEPEN
; break;
1322 case wxEQUIV
: rop
= R2_NOTXORPEN
; break;
1323 case wxSRC_INVERT
: rop
= R2_NOTCOPYPEN
; break;
1324 case wxOR_INVERT
: rop
= R2_MERGENOTPEN
; break;
1325 case wxNAND
: rop
= R2_NOTMASKPEN
; break;
1326 case wxOR
: rop
= R2_MERGEPEN
; break;
1327 case wxSET
: rop
= R2_WHITE
; break;
1330 wxFAIL_MSG( wxT("unsupported logical function") );
1334 SetROP2(GetHdc(), rop
);
1337 bool wxDC::StartDoc(const wxString
& WXUNUSED(message
))
1339 // We might be previewing, so return TRUE to let it continue.
1347 void wxDC::StartPage()
1351 void wxDC::EndPage()
1355 // ---------------------------------------------------------------------------
1357 // ---------------------------------------------------------------------------
1359 wxCoord
wxDC::GetCharHeight() const
1361 #ifdef __WXMICROWIN__
1362 if (!GetHDC()) return 0;
1365 TEXTMETRIC lpTextMetric
;
1367 GetTextMetrics(GetHdc(), &lpTextMetric
);
1369 return YDEV2LOGREL(lpTextMetric
.tmHeight
);
1372 wxCoord
wxDC::GetCharWidth() const
1374 #ifdef __WXMICROWIN__
1375 if (!GetHDC()) return 0;
1378 TEXTMETRIC lpTextMetric
;
1380 GetTextMetrics(GetHdc(), &lpTextMetric
);
1382 return XDEV2LOGREL(lpTextMetric
.tmAveCharWidth
);
1385 void wxDC::DoGetTextExtent(const wxString
& string
, wxCoord
*x
, wxCoord
*y
,
1386 wxCoord
*descent
, wxCoord
*externalLeading
,
1389 #ifdef __WXMICROWIN__
1394 if (descent
) *descent
= 0;
1395 if (externalLeading
) *externalLeading
= 0;
1403 wxASSERT_MSG( font
->Ok(), _T("invalid font in wxDC::GetTextExtent") );
1405 hfontOld
= (HFONT
)::SelectObject(GetHdc(), GetHfontOf(*font
));
1407 else // don't change the font
1415 GetTextExtentPoint(GetHdc(), string
, string
.length(), &sizeRect
);
1416 GetTextMetrics(GetHdc(), &tm
);
1418 if (x
) *x
= XDEV2LOGREL(sizeRect
.cx
);
1419 if (y
) *y
= YDEV2LOGREL(sizeRect
.cy
);
1420 if (descent
) *descent
= tm
.tmDescent
;
1421 if (externalLeading
) *externalLeading
= tm
.tmExternalLeading
;
1425 ::SelectObject(GetHdc(), hfontOld
);
1429 void wxDC::SetMapMode(int mode
)
1431 #ifdef __WXMICROWIN__
1432 if (!GetHDC()) return;
1435 m_mappingMode
= mode
;
1437 int pixel_width
= 0;
1438 int pixel_height
= 0;
1442 pixel_width
= GetDeviceCaps(GetHdc(), HORZRES
);
1443 pixel_height
= GetDeviceCaps(GetHdc(), VERTRES
);
1444 mm_width
= GetDeviceCaps(GetHdc(), HORZSIZE
);
1445 mm_height
= GetDeviceCaps(GetHdc(), VERTSIZE
);
1447 if ((pixel_width
== 0) || (pixel_height
== 0) || (mm_width
== 0) || (mm_height
== 0))
1452 double mm2pixelsX
= pixel_width
/mm_width
;
1453 double mm2pixelsY
= pixel_height
/mm_height
;
1459 m_logicalScaleX
= (twips2mm
* mm2pixelsX
);
1460 m_logicalScaleY
= (twips2mm
* mm2pixelsY
);
1465 m_logicalScaleX
= (pt2mm
* mm2pixelsX
);
1466 m_logicalScaleY
= (pt2mm
* mm2pixelsY
);
1471 m_logicalScaleX
= mm2pixelsX
;
1472 m_logicalScaleY
= mm2pixelsY
;
1477 m_logicalScaleX
= (mm2pixelsX
/10.0);
1478 m_logicalScaleY
= (mm2pixelsY
/10.0);
1484 m_logicalScaleX
= 1.0;
1485 m_logicalScaleY
= 1.0;
1490 if (::GetMapMode(GetHdc()) != MM_ANISOTROPIC
)
1491 ::SetMapMode(GetHdc(), MM_ANISOTROPIC
);
1493 SetViewportExtEx(GetHdc(), VIEWPORT_EXTENT
, VIEWPORT_EXTENT
, NULL
);
1494 m_windowExtX
= (int)MS_XDEV2LOGREL(VIEWPORT_EXTENT
);
1495 m_windowExtY
= (int)MS_YDEV2LOGREL(VIEWPORT_EXTENT
);
1496 ::SetWindowExtEx(GetHdc(), m_windowExtX
, m_windowExtY
, NULL
);
1497 ::SetViewportOrgEx(GetHdc(), (int)m_deviceOriginX
, (int)m_deviceOriginY
, NULL
);
1498 ::SetWindowOrgEx(GetHdc(), (int)m_logicalOriginX
, (int)m_logicalOriginY
, NULL
);
1501 void wxDC::SetUserScale(double x
, double y
)
1503 #ifdef __WXMICROWIN__
1504 if (!GetHDC()) return;
1510 SetMapMode(m_mappingMode
);
1513 void wxDC::SetAxisOrientation(bool xLeftRight
, bool yBottomUp
)
1515 #ifdef __WXMICROWIN__
1516 if (!GetHDC()) return;
1519 m_signX
= xLeftRight
? 1 : -1;
1520 m_signY
= yBottomUp
? -1 : 1;
1522 SetMapMode(m_mappingMode
);
1525 void wxDC::SetSystemScale(double x
, double y
)
1527 #ifdef __WXMICROWIN__
1528 if (!GetHDC()) return;
1534 SetMapMode(m_mappingMode
);
1537 void wxDC::SetLogicalOrigin(wxCoord x
, wxCoord y
)
1539 #ifdef __WXMICROWIN__
1540 if (!GetHDC()) return;
1543 m_logicalOriginX
= x
;
1544 m_logicalOriginY
= y
;
1546 ::SetWindowOrgEx(GetHdc(), (int)m_logicalOriginX
, (int)m_logicalOriginY
, NULL
);
1549 void wxDC::SetDeviceOrigin(wxCoord x
, wxCoord y
)
1551 #ifdef __WXMICROWIN__
1552 if (!GetHDC()) return;
1555 m_deviceOriginX
= x
;
1556 m_deviceOriginY
= y
;
1558 ::SetViewportOrgEx(GetHdc(), (int)m_deviceOriginX
, (int)m_deviceOriginY
, NULL
);
1561 // ---------------------------------------------------------------------------
1562 // coordinates transformations
1563 // ---------------------------------------------------------------------------
1565 wxCoord
wxDCBase::DeviceToLogicalX(wxCoord x
) const
1567 double xRel
= x
- m_deviceOriginX
;
1568 xRel
/= m_logicalScaleX
*m_userScaleX
*m_signX
*m_scaleX
;
1569 return (wxCoord
)(xRel
+ m_logicalOriginX
);
1572 wxCoord
wxDCBase::DeviceToLogicalXRel(wxCoord x
) const
1574 return (wxCoord
) ((x
)/(m_logicalScaleX
*m_userScaleX
*m_signX
*m_scaleX
));
1577 wxCoord
wxDCBase::DeviceToLogicalY(wxCoord y
) const
1579 double yRel
= y
- m_deviceOriginY
;
1580 yRel
/= m_logicalScaleY
*m_userScaleY
*m_signY
*m_scaleY
;
1581 return (wxCoord
)(yRel
+ m_logicalOriginY
);
1584 wxCoord
wxDCBase::DeviceToLogicalYRel(wxCoord y
) const
1586 return (wxCoord
) ((y
)/(m_logicalScaleY
*m_userScaleY
*m_signY
*m_scaleY
));
1589 wxCoord
wxDCBase::LogicalToDeviceX(wxCoord x
) const
1591 return (wxCoord
) ((x
- m_logicalOriginX
)*m_logicalScaleX
*m_userScaleX
*m_signX
*m_scaleX
+ m_deviceOriginX
);
1594 wxCoord
wxDCBase::LogicalToDeviceXRel(wxCoord x
) const
1596 return (wxCoord
) (x
*m_logicalScaleX
*m_userScaleX
*m_signX
*m_scaleX
);
1599 wxCoord
wxDCBase::LogicalToDeviceY(wxCoord y
) const
1601 return (wxCoord
) ((y
- m_logicalOriginY
)*m_logicalScaleY
*m_userScaleY
*m_signY
*m_scaleY
+ m_deviceOriginY
);
1604 wxCoord
wxDCBase::LogicalToDeviceYRel(wxCoord y
) const
1606 return (wxCoord
) (y
*m_logicalScaleY
*m_userScaleY
*m_signY
*m_scaleY
);
1609 // ---------------------------------------------------------------------------
1611 // ---------------------------------------------------------------------------
1613 bool wxDC::DoBlit(wxCoord xdest
, wxCoord ydest
,
1614 wxCoord width
, wxCoord height
,
1615 wxDC
*source
, wxCoord xsrc
, wxCoord ysrc
,
1616 int rop
, bool useMask
,
1617 wxCoord xsrcMask
, wxCoord ysrcMask
)
1619 #ifdef __WXMICROWIN__
1620 if (!GetHDC()) return FALSE
;
1623 wxMask
*mask
= NULL
;
1626 const wxBitmap
& bmp
= source
->m_selectedBitmap
;
1627 mask
= bmp
.GetMask();
1629 if ( !(bmp
.Ok() && mask
&& mask
->GetMaskBitmap()) )
1631 // don't give assert here because this would break existing
1632 // programs - just silently ignore useMask parameter
1637 if (xsrcMask
== -1 && ysrcMask
== -1)
1639 xsrcMask
= xsrc
; ysrcMask
= ysrc
;
1642 COLORREF old_textground
= ::GetTextColor(GetHdc());
1643 COLORREF old_background
= ::GetBkColor(GetHdc());
1644 if (m_textForegroundColour
.Ok())
1646 ::SetTextColor(GetHdc(), m_textForegroundColour
.GetPixel() );
1648 if (m_textBackgroundColour
.Ok())
1650 ::SetBkColor(GetHdc(), m_textBackgroundColour
.GetPixel() );
1653 DWORD dwRop
= SRCCOPY
;
1656 case wxXOR
: dwRop
= SRCINVERT
; break;
1657 case wxINVERT
: dwRop
= DSTINVERT
; break;
1658 case wxOR_REVERSE
: dwRop
= 0x00DD0228; break;
1659 case wxAND_REVERSE
: dwRop
= SRCERASE
; break;
1660 case wxCLEAR
: dwRop
= BLACKNESS
; break;
1661 case wxSET
: dwRop
= WHITENESS
; break;
1662 case wxOR_INVERT
: dwRop
= MERGEPAINT
; break;
1663 case wxAND
: dwRop
= SRCAND
; break;
1664 case wxOR
: dwRop
= SRCPAINT
; break;
1665 case wxEQUIV
: dwRop
= 0x00990066; break;
1666 case wxNAND
: dwRop
= 0x007700E6; break;
1667 case wxAND_INVERT
: dwRop
= 0x00220326; break;
1668 case wxCOPY
: dwRop
= SRCCOPY
; break;
1669 case wxNO_OP
: dwRop
= DSTCOPY
; break;
1670 case wxSRC_INVERT
: dwRop
= NOTSRCCOPY
; break;
1671 case wxNOR
: dwRop
= NOTSRCCOPY
; break;
1673 wxFAIL_MSG( wxT("unsupported logical function") );
1677 bool success
= FALSE
;
1682 // we want the part of the image corresponding to the mask to be
1683 // transparent, so use "DSTCOPY" ROP for the mask points (the usual
1684 // meaning of fg and bg is inverted which corresponds to wxWin notion
1685 // of the mask which is also contrary to the Windows one)
1687 // On some systems, MaskBlt succeeds yet is much much slower
1688 // than the wxWindows fall-back implementation. So we need
1689 // to be able to switch this on and off at runtime.
1690 #if wxUSE_SYSTEM_OPTIONS
1691 if (wxSystemOptions::GetOptionInt(wxT("no-maskblt")) == 0)
1694 success
= ::MaskBlt(GetHdc(), xdest
, ydest
, width
, height
,
1695 GetHdcOf(*source
), xsrc
, ysrc
,
1696 (HBITMAP
)mask
->GetMaskBitmap(), xsrcMask
, ysrcMask
,
1697 MAKEROP4(dwRop
, DSTCOPY
)) != 0;
1703 // Blit bitmap with mask
1706 HBITMAP buffer_bmap
;
1708 #if wxUSE_DC_CACHEING
1709 // create a temp buffer bitmap and DCs to access it and the mask
1710 wxDCCacheEntry
* dcCacheEntry1
= FindDCInCache(NULL
, source
->GetHDC());
1711 dc_mask
= (HDC
) dcCacheEntry1
->m_dc
;
1713 wxDCCacheEntry
* dcCacheEntry2
= FindDCInCache(dcCacheEntry1
, GetHDC());
1714 dc_buffer
= (HDC
) dcCacheEntry2
->m_dc
;
1716 wxDCCacheEntry
* bitmapCacheEntry
= FindBitmapInCache(GetHDC(),
1719 buffer_bmap
= (HBITMAP
) bitmapCacheEntry
->m_bitmap
;
1720 #else // !wxUSE_DC_CACHEING
1721 // create a temp buffer bitmap and DCs to access it and the mask
1722 dc_mask
= ::CreateCompatibleDC(GetHdcOf(*source
));
1723 dc_buffer
= ::CreateCompatibleDC(GetHdc());
1724 buffer_bmap
= ::CreateCompatibleBitmap(GetHdc(), width
, height
);
1725 ::SelectObject(dc_mask
, (HBITMAP
) mask
->GetMaskBitmap());
1726 ::SelectObject(dc_buffer
, buffer_bmap
);
1727 #endif // wxUSE_DC_CACHEING/!wxUSE_DC_CACHEING
1729 // copy dest to buffer
1730 if ( !::BitBlt(dc_buffer
, 0, 0, (int)width
, (int)height
,
1731 GetHdc(), xdest
, ydest
, SRCCOPY
) )
1733 wxLogLastError(wxT("BitBlt"));
1736 // copy src to buffer using selected raster op
1737 if ( !::BitBlt(dc_buffer
, 0, 0, (int)width
, (int)height
,
1738 GetHdcOf(*source
), xsrc
, ysrc
, dwRop
) )
1740 wxLogLastError(wxT("BitBlt"));
1743 // set masked area in buffer to BLACK (pixel value 0)
1744 COLORREF prevBkCol
= ::SetBkColor(GetHdc(), RGB(255, 255, 255));
1745 COLORREF prevCol
= ::SetTextColor(GetHdc(), RGB(0, 0, 0));
1746 if ( !::BitBlt(dc_buffer
, 0, 0, (int)width
, (int)height
,
1747 dc_mask
, xsrcMask
, ysrcMask
, SRCAND
) )
1749 wxLogLastError(wxT("BitBlt"));
1752 // set unmasked area in dest to BLACK
1753 ::SetBkColor(GetHdc(), RGB(0, 0, 0));
1754 ::SetTextColor(GetHdc(), RGB(255, 255, 255));
1755 if ( !::BitBlt(GetHdc(), xdest
, ydest
, (int)width
, (int)height
,
1756 dc_mask
, xsrcMask
, ysrcMask
, SRCAND
) )
1758 wxLogLastError(wxT("BitBlt"));
1760 ::SetBkColor(GetHdc(), prevBkCol
); // restore colours to original values
1761 ::SetTextColor(GetHdc(), prevCol
);
1763 // OR buffer to dest
1764 success
= ::BitBlt(GetHdc(), xdest
, ydest
,
1765 (int)width
, (int)height
,
1766 dc_buffer
, 0, 0, SRCPAINT
) != 0;
1769 wxLogLastError(wxT("BitBlt"));
1772 // tidy up temporary DCs and bitmap
1773 ::SelectObject(dc_mask
, 0);
1774 ::SelectObject(dc_buffer
, 0);
1776 #if !wxUSE_DC_CACHEING
1778 ::DeleteDC(dc_mask
);
1779 ::DeleteDC(dc_buffer
);
1780 ::DeleteObject(buffer_bmap
);
1785 else // no mask, just BitBlt() it
1787 success
= ::BitBlt(GetHdc(), xdest
, ydest
,
1788 (int)width
, (int)height
,
1789 GetHdcOf(*source
), xsrc
, ysrc
, dwRop
) != 0;
1792 wxLogLastError(wxT("BitBlt"));
1795 ::SetTextColor(GetHdc(), old_textground
);
1796 ::SetBkColor(GetHdc(), old_background
);
1801 void wxDC::DoGetSize(int *w
, int *h
) const
1803 #ifdef __WXMICROWIN__
1804 if (!GetHDC()) return;
1807 if ( w
) *w
= ::GetDeviceCaps(GetHdc(), HORZRES
);
1808 if ( h
) *h
= ::GetDeviceCaps(GetHdc(), VERTRES
);
1811 void wxDC::DoGetSizeMM(int *w
, int *h
) const
1813 #ifdef __WXMICROWIN__
1814 if (!GetHDC()) return;
1817 if ( w
) *w
= ::GetDeviceCaps(GetHdc(), HORZSIZE
);
1818 if ( h
) *h
= ::GetDeviceCaps(GetHdc(), VERTSIZE
);
1821 wxSize
wxDC::GetPPI() const
1823 #ifdef __WXMICROWIN__
1824 if (!GetHDC()) return wxSize();
1827 int x
= ::GetDeviceCaps(GetHdc(), LOGPIXELSX
);
1828 int y
= ::GetDeviceCaps(GetHdc(), LOGPIXELSY
);
1830 return wxSize(x
, y
);
1833 // For use by wxWindows only, unless custom units are required.
1834 void wxDC::SetLogicalScale(double x
, double y
)
1836 #ifdef __WXMICROWIN__
1837 if (!GetHDC()) return;
1840 m_logicalScaleX
= x
;
1841 m_logicalScaleY
= y
;
1844 #if WXWIN_COMPATIBILITY
1845 void wxDC::DoGetTextExtent(const wxString
& string
, float *x
, float *y
,
1846 float *descent
, float *externalLeading
,
1847 wxFont
*theFont
, bool use16bit
) const
1849 #ifdef __WXMICROWIN__
1850 if (!GetHDC()) return;
1853 wxCoord x1
, y1
, descent1
, externalLeading1
;
1854 GetTextExtent(string
, & x1
, & y1
, & descent1
, & externalLeading1
, theFont
, use16bit
);
1857 *descent
= descent1
;
1858 if (externalLeading
)
1859 *externalLeading
= externalLeading1
;
1863 #if wxUSE_DC_CACHEING
1866 * This implementation is a bit ugly and uses the old-fashioned wxList class, so I will
1867 * improve it in due course, either using arrays, or simply storing pointers to one
1868 * entry for the bitmap, and two for the DCs. -- JACS
1871 wxList
wxDC::sm_bitmapCache
;
1872 wxList
wxDC::sm_dcCache
;
1874 wxDCCacheEntry::wxDCCacheEntry(WXHBITMAP hBitmap
, int w
, int h
, int depth
)
1883 wxDCCacheEntry::wxDCCacheEntry(WXHDC hDC
, int depth
)
1892 wxDCCacheEntry::~wxDCCacheEntry()
1895 ::DeleteObject((HBITMAP
) m_bitmap
);
1897 ::DeleteDC((HDC
) m_dc
);
1900 wxDCCacheEntry
* wxDC::FindBitmapInCache(WXHDC dc
, int w
, int h
)
1902 int depth
= ::GetDeviceCaps((HDC
) dc
, PLANES
) * ::GetDeviceCaps((HDC
) dc
, BITSPIXEL
);
1903 wxNode
* node
= sm_bitmapCache
.First();
1906 wxDCCacheEntry
* entry
= (wxDCCacheEntry
*) node
->Data();
1908 if (entry
->m_depth
== depth
)
1910 if (entry
->m_width
< w
|| entry
->m_height
< h
)
1912 ::DeleteObject((HBITMAP
) entry
->m_bitmap
);
1913 entry
->m_bitmap
= (WXHBITMAP
) ::CreateCompatibleBitmap((HDC
) dc
, w
, h
);
1914 if ( !entry
->m_bitmap
)
1916 wxLogLastError(wxT("CreateCompatibleBitmap"));
1918 entry
->m_width
= w
; entry
->m_height
= h
;
1924 node
= node
->Next();
1926 WXHBITMAP hBitmap
= (WXHBITMAP
) ::CreateCompatibleBitmap((HDC
) dc
, w
, h
);
1929 wxLogLastError(wxT("CreateCompatibleBitmap"));
1931 wxDCCacheEntry
* entry
= new wxDCCacheEntry(hBitmap
, w
, h
, depth
);
1932 AddToBitmapCache(entry
);
1936 wxDCCacheEntry
* wxDC::FindDCInCache(wxDCCacheEntry
* notThis
, WXHDC dc
)
1938 int depth
= ::GetDeviceCaps((HDC
) dc
, PLANES
) * ::GetDeviceCaps((HDC
) dc
, BITSPIXEL
);
1939 wxNode
* node
= sm_dcCache
.First();
1942 wxDCCacheEntry
* entry
= (wxDCCacheEntry
*) node
->Data();
1944 // Don't return the same one as we already have
1945 if (!notThis
|| (notThis
!= entry
))
1947 if (entry
->m_depth
== depth
)
1953 node
= node
->Next();
1955 WXHDC hDC
= (WXHDC
) ::CreateCompatibleDC((HDC
) dc
);
1958 wxLogLastError(wxT("CreateCompatibleDC"));
1960 wxDCCacheEntry
* entry
= new wxDCCacheEntry(hDC
, depth
);
1961 AddToDCCache(entry
);
1965 void wxDC::AddToBitmapCache(wxDCCacheEntry
* entry
)
1967 sm_bitmapCache
.Append(entry
);
1970 void wxDC::AddToDCCache(wxDCCacheEntry
* entry
)
1972 sm_dcCache
.Append(entry
);
1975 void wxDC::ClearCache()
1977 sm_bitmapCache
.DeleteContents(TRUE
);
1978 sm_bitmapCache
.Clear();
1979 sm_bitmapCache
.DeleteContents(FALSE
);
1980 sm_dcCache
.DeleteContents(TRUE
);
1982 sm_dcCache
.DeleteContents(FALSE
);
1985 // Clean up cache at app exit
1986 class wxDCModule
: public wxModule
1989 virtual bool OnInit() { return TRUE
; }
1990 virtual void OnExit() { wxDC::ClearCache(); }
1993 DECLARE_DYNAMIC_CLASS(wxDCModule
)
1996 IMPLEMENT_DYNAMIC_CLASS(wxDCModule
, wxModule
)
1999 // wxUSE_DC_CACHEING