]>
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 because it's not TrueType and so
1013 // can't have non zero orientation/escapement
1014 wxFont font
= m_font
.Ok() ? m_font
: *wxNORMAL_FONT
;
1015 HFONT hfont
= (HFONT
)font
.GetResourceHandle();
1017 if ( ::GetObject(hfont
, sizeof(lf
), &lf
) == 0 )
1019 wxLogLastError(wxT("GetObject(hfont)"));
1022 // GDI wants the angle in tenth of degree
1023 long angle10
= (long)(angle
* 10);
1024 lf
.lfEscapement
= angle10
;
1025 lf
. lfOrientation
= angle10
;
1027 hfont
= ::CreateFontIndirect(&lf
);
1030 wxLogLastError(wxT("CreateFont"));
1034 HFONT hfontOld
= (HFONT
)::SelectObject(GetHdc(), hfont
);
1036 DrawAnyText(text
, x
, y
);
1038 (void)::SelectObject(GetHdc(), hfontOld
);
1039 (void)::DeleteObject(hfont
);
1042 // call the bounding box by adding all four vertices of the rectangle
1043 // containing the text to it (simpler and probably not slower than
1044 // determining which of them is really topmost/leftmost/...)
1046 GetTextExtent(text
, &w
, &h
);
1048 double rad
= DegToRad(angle
);
1050 // "upper left" and "upper right"
1051 CalcBoundingBox(x
, y
);
1052 CalcBoundingBox(x
+ w
*cos(rad
), y
- h
*sin(rad
));
1054 // "bottom left" and "bottom right"
1055 x
+= (wxCoord
)(h
*sin(rad
));
1056 y
+= (wxCoord
)(h
*cos(rad
));
1057 CalcBoundingBox(x
, y
);
1058 CalcBoundingBox(x
+ h
*sin(rad
), y
+ h
*cos(rad
));
1063 // ---------------------------------------------------------------------------
1065 // ---------------------------------------------------------------------------
1067 void wxDC::SetPalette(const wxPalette
& palette
)
1069 #ifdef __WXMICROWIN__
1070 if (!GetHDC()) return;
1073 // Set the old object temporarily, in case the assignment deletes an object
1074 // that's not yet selected out.
1077 ::SelectPalette(GetHdc(), (HPALETTE
) m_oldPalette
, TRUE
);
1081 m_palette
= palette
;
1083 if (!m_palette
.Ok())
1085 // Setting a NULL colourmap is a way of restoring
1086 // the original colourmap
1089 ::SelectPalette(GetHdc(), (HPALETTE
) m_oldPalette
, TRUE
);
1096 if (m_palette
.Ok() && m_palette
.GetHPALETTE())
1098 HPALETTE oldPal
= ::SelectPalette(GetHdc(), (HPALETTE
) m_palette
.GetHPALETTE(), TRUE
);
1100 m_oldPalette
= (WXHPALETTE
) oldPal
;
1102 ::RealizePalette(GetHdc());
1106 void wxDC::SetFont(const wxFont
& the_font
)
1108 #ifdef __WXMICROWIN__
1109 if (!GetHDC()) return;
1112 // Set the old object temporarily, in case the assignment deletes an object
1113 // that's not yet selected out.
1116 ::SelectObject(GetHdc(), (HFONT
) m_oldFont
);
1125 ::SelectObject(GetHdc(), (HFONT
) m_oldFont
);
1129 if (m_font
.Ok() && m_font
.GetResourceHandle())
1131 HFONT f
= (HFONT
) ::SelectObject(GetHdc(), (HFONT
) m_font
.GetResourceHandle());
1132 if (f
== (HFONT
) NULL
)
1134 wxLogDebug(wxT("::SelectObject failed in wxDC::SetFont."));
1137 m_oldFont
= (WXHFONT
) f
;
1141 void wxDC::SetPen(const wxPen
& pen
)
1143 #ifdef __WXMICROWIN__
1144 if (!GetHDC()) return;
1147 // Set the old object temporarily, in case the assignment deletes an object
1148 // that's not yet selected out.
1151 ::SelectObject(GetHdc(), (HPEN
) m_oldPen
);
1160 ::SelectObject(GetHdc(), (HPEN
) m_oldPen
);
1166 if (m_pen
.GetResourceHandle())
1168 HPEN p
= (HPEN
) ::SelectObject(GetHdc(), (HPEN
)m_pen
.GetResourceHandle());
1170 m_oldPen
= (WXHPEN
) p
;
1175 void wxDC::SetBrush(const wxBrush
& brush
)
1177 #ifdef __WXMICROWIN__
1178 if (!GetHDC()) return;
1181 // Set the old object temporarily, in case the assignment deletes an object
1182 // that's not yet selected out.
1185 ::SelectObject(GetHdc(), (HBRUSH
) m_oldBrush
);
1194 ::SelectObject(GetHdc(), (HBRUSH
) m_oldBrush
);
1200 // to make sure the brush is alligned with the logical coordinates
1201 wxBitmap
*stipple
= m_brush
.GetStipple();
1202 if ( stipple
&& stipple
->Ok() )
1205 ::SetBrushOrgEx(GetHdc(),
1206 m_deviceOriginX
% stipple
->GetWidth(),
1207 m_deviceOriginY
% stipple
->GetHeight(),
1208 NULL
); // don't need previous brush origin
1210 ::SetBrushOrg(GetHdc(),
1211 m_deviceOriginX
% stipple
->GetWidth(),
1212 m_deviceOriginY
% stipple
->GetHeight());
1216 if ( m_brush
.GetResourceHandle() )
1219 b
= (HBRUSH
) ::SelectObject(GetHdc(), (HBRUSH
)m_brush
.GetResourceHandle());
1221 m_oldBrush
= (WXHBRUSH
) b
;
1226 void wxDC::SetBackground(const wxBrush
& brush
)
1228 #ifdef __WXMICROWIN__
1229 if (!GetHDC()) return;
1232 m_backgroundBrush
= brush
;
1234 if (!m_backgroundBrush
.Ok())
1239 bool customColours
= TRUE
;
1240 // If we haven't specified wxUSER_COLOURS, don't allow the panel/dialog box to
1241 // change background colours from the control-panel specified colours.
1242 if (m_canvas
->IsKindOf(CLASSINFO(wxWindow
)) && ((m_canvas
->GetWindowStyleFlag() & wxUSER_COLOURS
) != wxUSER_COLOURS
))
1243 customColours
= FALSE
;
1247 if (m_backgroundBrush
.GetStyle()==wxTRANSPARENT
)
1249 m_canvas
->SetTransparent(TRUE
);
1253 // New behaviour, 10/2/99: setting the background brush of a DC
1254 // doesn't affect the window background colour. However,
1255 // I'm leaving in the transparency setting because it's needed by
1256 // various controls (e.g. wxStaticText) to determine whether to draw
1257 // transparently or not. TODO: maybe this should be a new function
1258 // wxWindow::SetTransparency(). Should that apply to the child itself, or the
1260 // m_canvas->SetBackgroundColour(m_backgroundBrush.GetColour());
1261 m_canvas
->SetTransparent(FALSE
);
1265 COLORREF new_color
= m_backgroundBrush
.GetColour().GetPixel();
1267 (void)SetBkColor(GetHdc(), new_color
);
1271 void wxDC::SetBackgroundMode(int mode
)
1273 #ifdef __WXMICROWIN__
1274 if (!GetHDC()) return;
1277 m_backgroundMode
= mode
;
1279 // SetBackgroundColour now only refers to text background
1280 // and m_backgroundMode is used there
1283 if (m_backgroundMode == wxTRANSPARENT)
1284 ::SetBkMode(GetHdc(), TRANSPARENT);
1286 ::SetBkMode(GetHdc(), OPAQUE);
1287 Last change: AC 29 Jan 101 8:54 pm
1291 void wxDC::SetLogicalFunction(int function
)
1293 #ifdef __WXMICROWIN__
1294 if (!GetHDC()) return;
1297 m_logicalFunction
= function
;
1302 void wxDC::SetRop(WXHDC dc
)
1304 if ( !dc
|| m_logicalFunction
< 0 )
1309 switch (m_logicalFunction
)
1311 case wxCLEAR
: rop
= R2_BLACK
; break;
1312 case wxXOR
: rop
= R2_XORPEN
; break;
1313 case wxINVERT
: rop
= R2_NOT
; break;
1314 case wxOR_REVERSE
: rop
= R2_MERGEPENNOT
; break;
1315 case wxAND_REVERSE
: rop
= R2_MASKPENNOT
; break;
1316 case wxCOPY
: rop
= R2_COPYPEN
; break;
1317 case wxAND
: rop
= R2_MASKPEN
; break;
1318 case wxAND_INVERT
: rop
= R2_MASKNOTPEN
; break;
1319 case wxNO_OP
: rop
= R2_NOP
; break;
1320 case wxNOR
: rop
= R2_NOTMERGEPEN
; break;
1321 case wxEQUIV
: rop
= R2_NOTXORPEN
; break;
1322 case wxSRC_INVERT
: rop
= R2_NOTCOPYPEN
; break;
1323 case wxOR_INVERT
: rop
= R2_MERGENOTPEN
; break;
1324 case wxNAND
: rop
= R2_NOTMASKPEN
; break;
1325 case wxOR
: rop
= R2_MERGEPEN
; break;
1326 case wxSET
: rop
= R2_WHITE
; break;
1329 wxFAIL_MSG( wxT("unsupported logical function") );
1333 SetROP2(GetHdc(), rop
);
1336 bool wxDC::StartDoc(const wxString
& WXUNUSED(message
))
1338 // We might be previewing, so return TRUE to let it continue.
1346 void wxDC::StartPage()
1350 void wxDC::EndPage()
1354 // ---------------------------------------------------------------------------
1356 // ---------------------------------------------------------------------------
1358 wxCoord
wxDC::GetCharHeight() const
1360 #ifdef __WXMICROWIN__
1361 if (!GetHDC()) return 0;
1364 TEXTMETRIC lpTextMetric
;
1366 GetTextMetrics(GetHdc(), &lpTextMetric
);
1368 return YDEV2LOGREL(lpTextMetric
.tmHeight
);
1371 wxCoord
wxDC::GetCharWidth() const
1373 #ifdef __WXMICROWIN__
1374 if (!GetHDC()) return 0;
1377 TEXTMETRIC lpTextMetric
;
1379 GetTextMetrics(GetHdc(), &lpTextMetric
);
1381 return XDEV2LOGREL(lpTextMetric
.tmAveCharWidth
);
1384 void wxDC::DoGetTextExtent(const wxString
& string
, wxCoord
*x
, wxCoord
*y
,
1385 wxCoord
*descent
, wxCoord
*externalLeading
,
1388 #ifdef __WXMICROWIN__
1393 if (descent
) *descent
= 0;
1394 if (externalLeading
) *externalLeading
= 0;
1402 wxASSERT_MSG( font
->Ok(), _T("invalid font in wxDC::GetTextExtent") );
1404 hfontOld
= (HFONT
)::SelectObject(GetHdc(), GetHfontOf(*font
));
1406 else // don't change the font
1414 GetTextExtentPoint(GetHdc(), string
, string
.length(), &sizeRect
);
1415 GetTextMetrics(GetHdc(), &tm
);
1417 if (x
) *x
= XDEV2LOGREL(sizeRect
.cx
);
1418 if (y
) *y
= YDEV2LOGREL(sizeRect
.cy
);
1419 if (descent
) *descent
= tm
.tmDescent
;
1420 if (externalLeading
) *externalLeading
= tm
.tmExternalLeading
;
1424 ::SelectObject(GetHdc(), hfontOld
);
1428 void wxDC::SetMapMode(int mode
)
1430 #ifdef __WXMICROWIN__
1431 if (!GetHDC()) return;
1434 m_mappingMode
= mode
;
1436 int pixel_width
= 0;
1437 int pixel_height
= 0;
1441 pixel_width
= GetDeviceCaps(GetHdc(), HORZRES
);
1442 pixel_height
= GetDeviceCaps(GetHdc(), VERTRES
);
1443 mm_width
= GetDeviceCaps(GetHdc(), HORZSIZE
);
1444 mm_height
= GetDeviceCaps(GetHdc(), VERTSIZE
);
1446 if ((pixel_width
== 0) || (pixel_height
== 0) || (mm_width
== 0) || (mm_height
== 0))
1451 double mm2pixelsX
= pixel_width
/mm_width
;
1452 double mm2pixelsY
= pixel_height
/mm_height
;
1458 m_logicalScaleX
= (twips2mm
* mm2pixelsX
);
1459 m_logicalScaleY
= (twips2mm
* mm2pixelsY
);
1464 m_logicalScaleX
= (pt2mm
* mm2pixelsX
);
1465 m_logicalScaleY
= (pt2mm
* mm2pixelsY
);
1470 m_logicalScaleX
= mm2pixelsX
;
1471 m_logicalScaleY
= mm2pixelsY
;
1476 m_logicalScaleX
= (mm2pixelsX
/10.0);
1477 m_logicalScaleY
= (mm2pixelsY
/10.0);
1483 m_logicalScaleX
= 1.0;
1484 m_logicalScaleY
= 1.0;
1489 if (::GetMapMode(GetHdc()) != MM_ANISOTROPIC
)
1490 ::SetMapMode(GetHdc(), MM_ANISOTROPIC
);
1492 SetViewportExtEx(GetHdc(), VIEWPORT_EXTENT
, VIEWPORT_EXTENT
, NULL
);
1493 m_windowExtX
= (int)MS_XDEV2LOGREL(VIEWPORT_EXTENT
);
1494 m_windowExtY
= (int)MS_YDEV2LOGREL(VIEWPORT_EXTENT
);
1495 ::SetWindowExtEx(GetHdc(), m_windowExtX
, m_windowExtY
, NULL
);
1496 ::SetViewportOrgEx(GetHdc(), (int)m_deviceOriginX
, (int)m_deviceOriginY
, NULL
);
1497 ::SetWindowOrgEx(GetHdc(), (int)m_logicalOriginX
, (int)m_logicalOriginY
, NULL
);
1500 void wxDC::SetUserScale(double x
, double y
)
1502 #ifdef __WXMICROWIN__
1503 if (!GetHDC()) return;
1509 SetMapMode(m_mappingMode
);
1512 void wxDC::SetAxisOrientation(bool xLeftRight
, bool yBottomUp
)
1514 #ifdef __WXMICROWIN__
1515 if (!GetHDC()) return;
1518 m_signX
= xLeftRight
? 1 : -1;
1519 m_signY
= yBottomUp
? -1 : 1;
1521 SetMapMode(m_mappingMode
);
1524 void wxDC::SetSystemScale(double x
, double y
)
1526 #ifdef __WXMICROWIN__
1527 if (!GetHDC()) return;
1533 SetMapMode(m_mappingMode
);
1536 void wxDC::SetLogicalOrigin(wxCoord x
, wxCoord y
)
1538 #ifdef __WXMICROWIN__
1539 if (!GetHDC()) return;
1542 m_logicalOriginX
= x
;
1543 m_logicalOriginY
= y
;
1545 ::SetWindowOrgEx(GetHdc(), (int)m_logicalOriginX
, (int)m_logicalOriginY
, NULL
);
1548 void wxDC::SetDeviceOrigin(wxCoord x
, wxCoord y
)
1550 #ifdef __WXMICROWIN__
1551 if (!GetHDC()) return;
1554 m_deviceOriginX
= x
;
1555 m_deviceOriginY
= y
;
1557 ::SetViewportOrgEx(GetHdc(), (int)m_deviceOriginX
, (int)m_deviceOriginY
, NULL
);
1560 // ---------------------------------------------------------------------------
1561 // coordinates transformations
1562 // ---------------------------------------------------------------------------
1564 wxCoord
wxDCBase::DeviceToLogicalX(wxCoord x
) const
1566 double xRel
= x
- m_deviceOriginX
;
1567 xRel
/= m_logicalScaleX
*m_userScaleX
*m_signX
*m_scaleX
;
1568 return (wxCoord
)(xRel
+ m_logicalOriginX
);
1571 wxCoord
wxDCBase::DeviceToLogicalXRel(wxCoord x
) const
1573 return (wxCoord
) ((x
)/(m_logicalScaleX
*m_userScaleX
*m_signX
*m_scaleX
));
1576 wxCoord
wxDCBase::DeviceToLogicalY(wxCoord y
) const
1578 double yRel
= y
- m_deviceOriginY
;
1579 yRel
/= m_logicalScaleY
*m_userScaleY
*m_signY
*m_scaleY
;
1580 return (wxCoord
)(yRel
+ m_logicalOriginY
);
1583 wxCoord
wxDCBase::DeviceToLogicalYRel(wxCoord y
) const
1585 return (wxCoord
) ((y
)/(m_logicalScaleY
*m_userScaleY
*m_signY
*m_scaleY
));
1588 wxCoord
wxDCBase::LogicalToDeviceX(wxCoord x
) const
1590 return (wxCoord
) ((x
- m_logicalOriginX
)*m_logicalScaleX
*m_userScaleX
*m_signX
*m_scaleX
+ m_deviceOriginX
);
1593 wxCoord
wxDCBase::LogicalToDeviceXRel(wxCoord x
) const
1595 return (wxCoord
) (x
*m_logicalScaleX
*m_userScaleX
*m_signX
*m_scaleX
);
1598 wxCoord
wxDCBase::LogicalToDeviceY(wxCoord y
) const
1600 return (wxCoord
) ((y
- m_logicalOriginY
)*m_logicalScaleY
*m_userScaleY
*m_signY
*m_scaleY
+ m_deviceOriginY
);
1603 wxCoord
wxDCBase::LogicalToDeviceYRel(wxCoord y
) const
1605 return (wxCoord
) (y
*m_logicalScaleY
*m_userScaleY
*m_signY
*m_scaleY
);
1608 // ---------------------------------------------------------------------------
1610 // ---------------------------------------------------------------------------
1612 bool wxDC::DoBlit(wxCoord xdest
, wxCoord ydest
,
1613 wxCoord width
, wxCoord height
,
1614 wxDC
*source
, wxCoord xsrc
, wxCoord ysrc
,
1615 int rop
, bool useMask
,
1616 wxCoord xsrcMask
, wxCoord ysrcMask
)
1618 #ifdef __WXMICROWIN__
1619 if (!GetHDC()) return FALSE
;
1622 wxMask
*mask
= NULL
;
1625 const wxBitmap
& bmp
= source
->m_selectedBitmap
;
1626 mask
= bmp
.GetMask();
1628 if ( !(bmp
.Ok() && mask
&& mask
->GetMaskBitmap()) )
1630 // don't give assert here because this would break existing
1631 // programs - just silently ignore useMask parameter
1636 if (xsrcMask
== -1 && ysrcMask
== -1)
1638 xsrcMask
= xsrc
; ysrcMask
= ysrc
;
1641 COLORREF old_textground
= ::GetTextColor(GetHdc());
1642 COLORREF old_background
= ::GetBkColor(GetHdc());
1643 if (m_textForegroundColour
.Ok())
1645 ::SetTextColor(GetHdc(), m_textForegroundColour
.GetPixel() );
1647 if (m_textBackgroundColour
.Ok())
1649 ::SetBkColor(GetHdc(), m_textBackgroundColour
.GetPixel() );
1652 DWORD dwRop
= SRCCOPY
;
1655 case wxXOR
: dwRop
= SRCINVERT
; break;
1656 case wxINVERT
: dwRop
= DSTINVERT
; break;
1657 case wxOR_REVERSE
: dwRop
= 0x00DD0228; break;
1658 case wxAND_REVERSE
: dwRop
= SRCERASE
; break;
1659 case wxCLEAR
: dwRop
= BLACKNESS
; break;
1660 case wxSET
: dwRop
= WHITENESS
; break;
1661 case wxOR_INVERT
: dwRop
= MERGEPAINT
; break;
1662 case wxAND
: dwRop
= SRCAND
; break;
1663 case wxOR
: dwRop
= SRCPAINT
; break;
1664 case wxEQUIV
: dwRop
= 0x00990066; break;
1665 case wxNAND
: dwRop
= 0x007700E6; break;
1666 case wxAND_INVERT
: dwRop
= 0x00220326; break;
1667 case wxCOPY
: dwRop
= SRCCOPY
; break;
1668 case wxNO_OP
: dwRop
= DSTCOPY
; break;
1669 case wxSRC_INVERT
: dwRop
= NOTSRCCOPY
; break;
1670 case wxNOR
: dwRop
= NOTSRCCOPY
; break;
1672 wxFAIL_MSG( wxT("unsupported logical function") );
1676 bool success
= FALSE
;
1681 // we want the part of the image corresponding to the mask to be
1682 // transparent, so use "DSTCOPY" ROP for the mask points (the usual
1683 // meaning of fg and bg is inverted which corresponds to wxWin notion
1684 // of the mask which is also contrary to the Windows one)
1686 // On some systems, MaskBlt succeeds yet is much much slower
1687 // than the wxWindows fall-back implementation. So we need
1688 // to be able to switch this on and off at runtime.
1689 #if wxUSE_SYSTEM_OPTIONS
1690 if (wxSystemOptions::GetOptionInt(wxT("no-maskblt")) == 0)
1693 success
= ::MaskBlt(GetHdc(), xdest
, ydest
, width
, height
,
1694 GetHdcOf(*source
), xsrc
, ysrc
,
1695 (HBITMAP
)mask
->GetMaskBitmap(), xsrcMask
, ysrcMask
,
1696 MAKEROP4(dwRop
, DSTCOPY
)) != 0;
1702 // Blit bitmap with mask
1705 HBITMAP buffer_bmap
;
1707 #if wxUSE_DC_CACHEING
1708 // create a temp buffer bitmap and DCs to access it and the mask
1709 wxDCCacheEntry
* dcCacheEntry1
= FindDCInCache(NULL
, source
->GetHDC());
1710 dc_mask
= (HDC
) dcCacheEntry1
->m_dc
;
1712 wxDCCacheEntry
* dcCacheEntry2
= FindDCInCache(dcCacheEntry1
, GetHDC());
1713 dc_buffer
= (HDC
) dcCacheEntry2
->m_dc
;
1715 wxDCCacheEntry
* bitmapCacheEntry
= FindBitmapInCache(GetHDC(),
1718 buffer_bmap
= (HBITMAP
) bitmapCacheEntry
->m_bitmap
;
1719 #else // !wxUSE_DC_CACHEING
1720 // create a temp buffer bitmap and DCs to access it and the mask
1721 dc_mask
= ::CreateCompatibleDC(GetHdcOf(*source
));
1722 dc_buffer
= ::CreateCompatibleDC(GetHdc());
1723 buffer_bmap
= ::CreateCompatibleBitmap(GetHdc(), width
, height
);
1724 ::SelectObject(dc_mask
, (HBITMAP
) mask
->GetMaskBitmap());
1725 ::SelectObject(dc_buffer
, buffer_bmap
);
1726 #endif // wxUSE_DC_CACHEING/!wxUSE_DC_CACHEING
1728 // copy dest to buffer
1729 if ( !::BitBlt(dc_buffer
, 0, 0, (int)width
, (int)height
,
1730 GetHdc(), xdest
, ydest
, SRCCOPY
) )
1732 wxLogLastError(wxT("BitBlt"));
1735 // copy src to buffer using selected raster op
1736 if ( !::BitBlt(dc_buffer
, 0, 0, (int)width
, (int)height
,
1737 GetHdcOf(*source
), xsrc
, ysrc
, dwRop
) )
1739 wxLogLastError(wxT("BitBlt"));
1742 // set masked area in buffer to BLACK (pixel value 0)
1743 COLORREF prevBkCol
= ::SetBkColor(GetHdc(), RGB(255, 255, 255));
1744 COLORREF prevCol
= ::SetTextColor(GetHdc(), RGB(0, 0, 0));
1745 if ( !::BitBlt(dc_buffer
, 0, 0, (int)width
, (int)height
,
1746 dc_mask
, xsrcMask
, ysrcMask
, SRCAND
) )
1748 wxLogLastError(wxT("BitBlt"));
1751 // set unmasked area in dest to BLACK
1752 ::SetBkColor(GetHdc(), RGB(0, 0, 0));
1753 ::SetTextColor(GetHdc(), RGB(255, 255, 255));
1754 if ( !::BitBlt(GetHdc(), xdest
, ydest
, (int)width
, (int)height
,
1755 dc_mask
, xsrcMask
, ysrcMask
, SRCAND
) )
1757 wxLogLastError(wxT("BitBlt"));
1759 ::SetBkColor(GetHdc(), prevBkCol
); // restore colours to original values
1760 ::SetTextColor(GetHdc(), prevCol
);
1762 // OR buffer to dest
1763 success
= ::BitBlt(GetHdc(), xdest
, ydest
,
1764 (int)width
, (int)height
,
1765 dc_buffer
, 0, 0, SRCPAINT
) != 0;
1768 wxLogLastError(wxT("BitBlt"));
1771 // tidy up temporary DCs and bitmap
1772 ::SelectObject(dc_mask
, 0);
1773 ::SelectObject(dc_buffer
, 0);
1775 #if !wxUSE_DC_CACHEING
1777 ::DeleteDC(dc_mask
);
1778 ::DeleteDC(dc_buffer
);
1779 ::DeleteObject(buffer_bmap
);
1784 else // no mask, just BitBlt() it
1786 success
= ::BitBlt(GetHdc(), xdest
, ydest
,
1787 (int)width
, (int)height
,
1788 GetHdcOf(*source
), xsrc
, ysrc
, dwRop
) != 0;
1791 wxLogLastError(wxT("BitBlt"));
1794 ::SetTextColor(GetHdc(), old_textground
);
1795 ::SetBkColor(GetHdc(), old_background
);
1800 void wxDC::DoGetSize(int *w
, int *h
) const
1802 #ifdef __WXMICROWIN__
1803 if (!GetHDC()) return;
1806 if ( w
) *w
= ::GetDeviceCaps(GetHdc(), HORZRES
);
1807 if ( h
) *h
= ::GetDeviceCaps(GetHdc(), VERTRES
);
1810 void wxDC::DoGetSizeMM(int *w
, int *h
) const
1812 #ifdef __WXMICROWIN__
1813 if (!GetHDC()) return;
1816 if ( w
) *w
= ::GetDeviceCaps(GetHdc(), HORZSIZE
);
1817 if ( h
) *h
= ::GetDeviceCaps(GetHdc(), VERTSIZE
);
1820 wxSize
wxDC::GetPPI() const
1822 #ifdef __WXMICROWIN__
1823 if (!GetHDC()) return wxSize();
1826 int x
= ::GetDeviceCaps(GetHdc(), LOGPIXELSX
);
1827 int y
= ::GetDeviceCaps(GetHdc(), LOGPIXELSY
);
1829 return wxSize(x
, y
);
1832 // For use by wxWindows only, unless custom units are required.
1833 void wxDC::SetLogicalScale(double x
, double y
)
1835 #ifdef __WXMICROWIN__
1836 if (!GetHDC()) return;
1839 m_logicalScaleX
= x
;
1840 m_logicalScaleY
= y
;
1843 #if WXWIN_COMPATIBILITY
1844 void wxDC::DoGetTextExtent(const wxString
& string
, float *x
, float *y
,
1845 float *descent
, float *externalLeading
,
1846 wxFont
*theFont
, bool use16bit
) const
1848 #ifdef __WXMICROWIN__
1849 if (!GetHDC()) return;
1852 wxCoord x1
, y1
, descent1
, externalLeading1
;
1853 GetTextExtent(string
, & x1
, & y1
, & descent1
, & externalLeading1
, theFont
, use16bit
);
1856 *descent
= descent1
;
1857 if (externalLeading
)
1858 *externalLeading
= externalLeading1
;
1862 #if wxUSE_DC_CACHEING
1865 * This implementation is a bit ugly and uses the old-fashioned wxList class, so I will
1866 * improve it in due course, either using arrays, or simply storing pointers to one
1867 * entry for the bitmap, and two for the DCs. -- JACS
1870 wxList
wxDC::sm_bitmapCache
;
1871 wxList
wxDC::sm_dcCache
;
1873 wxDCCacheEntry::wxDCCacheEntry(WXHBITMAP hBitmap
, int w
, int h
, int depth
)
1882 wxDCCacheEntry::wxDCCacheEntry(WXHDC hDC
, int depth
)
1891 wxDCCacheEntry::~wxDCCacheEntry()
1894 ::DeleteObject((HBITMAP
) m_bitmap
);
1896 ::DeleteDC((HDC
) m_dc
);
1899 wxDCCacheEntry
* wxDC::FindBitmapInCache(WXHDC dc
, int w
, int h
)
1901 int depth
= ::GetDeviceCaps((HDC
) dc
, PLANES
) * ::GetDeviceCaps((HDC
) dc
, BITSPIXEL
);
1902 wxNode
* node
= sm_bitmapCache
.First();
1905 wxDCCacheEntry
* entry
= (wxDCCacheEntry
*) node
->Data();
1907 if (entry
->m_depth
== depth
)
1909 if (entry
->m_width
< w
|| entry
->m_height
< h
)
1911 ::DeleteObject((HBITMAP
) entry
->m_bitmap
);
1912 entry
->m_bitmap
= (WXHBITMAP
) ::CreateCompatibleBitmap((HDC
) dc
, w
, h
);
1913 if ( !entry
->m_bitmap
)
1915 wxLogLastError(wxT("CreateCompatibleBitmap"));
1917 entry
->m_width
= w
; entry
->m_height
= h
;
1923 node
= node
->Next();
1925 WXHBITMAP hBitmap
= (WXHBITMAP
) ::CreateCompatibleBitmap((HDC
) dc
, w
, h
);
1928 wxLogLastError(wxT("CreateCompatibleBitmap"));
1930 wxDCCacheEntry
* entry
= new wxDCCacheEntry(hBitmap
, w
, h
, depth
);
1931 AddToBitmapCache(entry
);
1935 wxDCCacheEntry
* wxDC::FindDCInCache(wxDCCacheEntry
* notThis
, WXHDC dc
)
1937 int depth
= ::GetDeviceCaps((HDC
) dc
, PLANES
) * ::GetDeviceCaps((HDC
) dc
, BITSPIXEL
);
1938 wxNode
* node
= sm_dcCache
.First();
1941 wxDCCacheEntry
* entry
= (wxDCCacheEntry
*) node
->Data();
1943 // Don't return the same one as we already have
1944 if (!notThis
|| (notThis
!= entry
))
1946 if (entry
->m_depth
== depth
)
1952 node
= node
->Next();
1954 WXHDC hDC
= (WXHDC
) ::CreateCompatibleDC((HDC
) dc
);
1957 wxLogLastError(wxT("CreateCompatibleDC"));
1959 wxDCCacheEntry
* entry
= new wxDCCacheEntry(hDC
, depth
);
1960 AddToDCCache(entry
);
1964 void wxDC::AddToBitmapCache(wxDCCacheEntry
* entry
)
1966 sm_bitmapCache
.Append(entry
);
1969 void wxDC::AddToDCCache(wxDCCacheEntry
* entry
)
1971 sm_dcCache
.Append(entry
);
1974 void wxDC::ClearCache()
1976 sm_bitmapCache
.DeleteContents(TRUE
);
1977 sm_bitmapCache
.Clear();
1978 sm_bitmapCache
.DeleteContents(FALSE
);
1979 sm_dcCache
.DeleteContents(TRUE
);
1981 sm_dcCache
.DeleteContents(FALSE
);
1984 // Clean up cache at app exit
1985 class wxDCModule
: public wxModule
1988 virtual bool OnInit() { return TRUE
; }
1989 virtual void OnExit() { wxDC::ClearCache(); }
1992 DECLARE_DYNAMIC_CLASS(wxDCModule
)
1995 IMPLEMENT_DYNAMIC_CLASS(wxDCModule
, wxModule
)
1998 // wxUSE_DC_CACHEING