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 && !defined(__WXMICROWIN__)
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
183 #endif // wxUSE_PALETTE
188 m_windowExtX
= VIEWPORT_EXTENT
;
189 m_windowExtY
= VIEWPORT_EXTENT
;
197 SelectOldObjects(m_hDC
);
199 // if we own the HDC, we delete it, otherwise we just release it
203 ::DeleteDC(GetHdc());
205 else // we don't own our HDC
209 ::ReleaseDC(GetHwndOf(m_canvas
), GetHdc());
213 // Must have been a wxScreenDC
214 ::ReleaseDC((HWND
) NULL
, GetHdc());
220 // This will select current objects out of the DC,
221 // which is what you have to do before deleting the
223 void wxDC
::SelectOldObjects(WXHDC dc
)
229 ::SelectObject((HDC
) dc
, (HBITMAP
) m_oldBitmap
);
230 if (m_selectedBitmap
.Ok())
232 m_selectedBitmap
.SetSelectedInto(NULL
);
238 ::SelectObject((HDC
) dc
, (HPEN
) m_oldPen
);
243 ::SelectObject((HDC
) dc
, (HBRUSH
) m_oldBrush
);
248 ::SelectObject((HDC
) dc
, (HFONT
) m_oldFont
);
255 ::SelectPalette((HDC
) dc
, (HPALETTE
) m_oldPalette
, FALSE
);
258 #endif // wxUSE_PALETTE
261 m_brush
= wxNullBrush
;
264 m_palette
= wxNullPalette
;
265 #endif // wxUSE_PALETTE
267 m_backgroundBrush
= wxNullBrush
;
268 m_selectedBitmap
= wxNullBitmap
;
271 // ---------------------------------------------------------------------------
273 // ---------------------------------------------------------------------------
275 void wxDC
::UpdateClipBox()
277 #ifdef __WXMICROWIN__
278 if (!GetHDC()) return;
282 ::GetClipBox(GetHdc(), &rect
);
284 m_clipX1
= (wxCoord
) XDEV2LOG(rect
.left
);
285 m_clipY1
= (wxCoord
) YDEV2LOG(rect
.top
);
286 m_clipX2
= (wxCoord
) XDEV2LOG(rect
.right
);
287 m_clipY2
= (wxCoord
) YDEV2LOG(rect
.bottom
);
290 // common part of DoSetClippingRegion() and DoSetClippingRegionAsRegion()
291 void wxDC
::SetClippingHrgn(WXHRGN hrgn
)
293 wxCHECK_RET( hrgn
, wxT("invalid clipping region") );
295 #ifdef __WXMICROWIN__
296 if (!GetHdc()) return;
297 #endif // __WXMICROWIN__
299 // note that we combine the new clipping region with the existing one: this
300 // is compatible with what the other ports do and is the documented
301 // behaviour now (starting with 2.3.3)
304 if ( !::GetClipBox(GetHdc(), &rectClip
) )
307 HRGN hrgnDest
= ::CreateRectRgn(0, 0, 0, 0);
308 HRGN hrgnClipOld
= ::CreateRectRgn(rectClip
.left
, rectClip
.top
,
309 rectClip
.right
, rectClip
.bottom
);
311 if ( ::CombineRgn(hrgnDest
, hrgnClipOld
, (HRGN
)hrgn
, RGN_AND
) != ERROR
)
313 ::SelectClipRgn(GetHdc(), hrgnDest
);
316 ::DeleteObject(hrgnClipOld
);
317 ::DeleteObject(hrgnDest
);
319 if ( ::ExtSelectClipRgn(GetHdc(), (HRGN
)hrgn
, RGN_AND
) == ERROR
)
321 wxLogLastError(_T("ExtSelectClipRgn"));
332 void wxDC
::DoSetClippingRegion(wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
)
334 // the region coords are always the device ones, so do the translation
337 // FIXME: possible +/-1 error here, to check!
338 HRGN hrgn
= ::CreateRectRgn(LogicalToDeviceX(x
),
340 LogicalToDeviceX(x
+ w
),
341 LogicalToDeviceY(y
+ h
));
344 wxLogLastError(_T("CreateRectRgn"));
348 SetClippingHrgn((WXHRGN
)hrgn
);
350 ::DeleteObject(hrgn
);
354 void wxDC
::DoSetClippingRegionAsRegion(const wxRegion
& region
)
356 SetClippingHrgn(region
.GetHRGN());
359 void wxDC
::DestroyClippingRegion()
361 #ifdef __WXMICROWIN__
362 if (!GetHDC()) return;
365 if (m_clipping
&& m_hDC
)
367 // TODO: this should restore the previous clipping region,
368 // so that OnPaint processing works correctly, and the update
369 // clipping region doesn't get destroyed after the first
370 // DestroyClippingRegion.
371 HRGN rgn
= CreateRectRgn(0, 0, 32000, 32000);
372 ::SelectClipRgn(GetHdc(), rgn
);
379 // ---------------------------------------------------------------------------
380 // query capabilities
381 // ---------------------------------------------------------------------------
383 bool wxDC
::CanDrawBitmap() const
388 bool wxDC
::CanGetTextExtent() const
390 #ifdef __WXMICROWIN__
391 // TODO Extend MicroWindows' GetDeviceCaps function
394 // What sort of display is it?
395 int technology
= ::GetDeviceCaps(GetHdc(), TECHNOLOGY
);
397 return (technology
== DT_RASDISPLAY
) || (technology
== DT_RASPRINTER
);
401 int wxDC
::GetDepth() const
403 #ifdef __WXMICROWIN__
404 if (!GetHDC()) return 16;
407 return (int)::GetDeviceCaps(GetHdc(), BITSPIXEL
);
410 // ---------------------------------------------------------------------------
412 // ---------------------------------------------------------------------------
416 #ifdef __WXMICROWIN__
417 if (!GetHDC()) return;
423 GetClientRect((HWND
) m_canvas
->GetHWND(), &rect
);
427 // No, I think we should simply ignore this if printing on e.g.
429 // wxCHECK_RET( m_selectedBitmap.Ok(), wxT("this DC can't be cleared") );
430 if (!m_selectedBitmap
.Ok())
433 rect
.left
= 0; rect
.top
= 0;
434 rect
.right
= m_selectedBitmap
.GetWidth();
435 rect
.bottom
= m_selectedBitmap
.GetHeight();
438 (void) ::SetMapMode(GetHdc(), MM_TEXT
);
440 DWORD colour
= GetBkColor(GetHdc());
441 HBRUSH brush
= CreateSolidBrush(colour
);
442 FillRect(GetHdc(), &rect
, brush
);
445 ::SetMapMode(GetHdc(), MM_ANISOTROPIC
);
446 ::SetViewportExtEx(GetHdc(), VIEWPORT_EXTENT
, VIEWPORT_EXTENT
, NULL
);
447 ::SetWindowExtEx(GetHdc(), m_windowExtX
, m_windowExtY
, NULL
);
448 ::SetViewportOrgEx(GetHdc(), (int)m_deviceOriginX
, (int)m_deviceOriginY
, NULL
);
449 ::SetWindowOrgEx(GetHdc(), (int)m_logicalOriginX
, (int)m_logicalOriginY
, NULL
);
452 void wxDC
::DoFloodFill(wxCoord x
, wxCoord y
, const wxColour
& col
, int style
)
454 #ifdef __WXMICROWIN__
455 if (!GetHDC()) return;
458 if ( !::ExtFloodFill(GetHdc(), XLOG2DEV(x
), YLOG2DEV(y
),
460 style
== wxFLOOD_SURFACE ? FLOODFILLSURFACE
463 // quoting from the MSDN docs:
465 // Following are some of the reasons this function might fail:
467 // * The filling could not be completed.
468 // * The specified point has the boundary color specified by the
469 // crColor parameter (if FLOODFILLBORDER was requested).
470 // * The specified point does not have the color specified by
471 // crColor (if FLOODFILLSURFACE was requested)
472 // * The point is outside the clipping region that is, it is not
473 // visible on the device.
475 wxLogLastError(wxT("ExtFloodFill"));
478 CalcBoundingBox(x
, y
);
481 bool wxDC
::DoGetPixel(wxCoord x
, wxCoord y
, wxColour
*col
) const
483 #ifdef __WXMICROWIN__
484 if (!GetHDC()) return FALSE
;
487 wxCHECK_MSG( col
, FALSE
, _T("NULL colour parameter in wxDC::GetPixel") );
489 // get the color of the pixel
490 COLORREF pixelcolor
= ::GetPixel(GetHdc(), XLOG2DEV(x
), YLOG2DEV(y
));
492 wxRGBToColour(*col
, pixelcolor
);
497 void wxDC
::DoCrossHair(wxCoord x
, wxCoord y
)
499 #ifdef __WXMICROWIN__
500 if (!GetHDC()) return;
503 wxCoord x1
= x
-VIEWPORT_EXTENT
;
504 wxCoord y1
= y
-VIEWPORT_EXTENT
;
505 wxCoord x2
= x
+VIEWPORT_EXTENT
;
506 wxCoord y2
= y
+VIEWPORT_EXTENT
;
508 (void)MoveToEx(GetHdc(), XLOG2DEV(x1
), YLOG2DEV(y
), NULL
);
509 (void)LineTo(GetHdc(), XLOG2DEV(x2
), YLOG2DEV(y
));
511 (void)MoveToEx(GetHdc(), XLOG2DEV(x
), YLOG2DEV(y1
), NULL
);
512 (void)LineTo(GetHdc(), XLOG2DEV(x
), YLOG2DEV(y2
));
514 CalcBoundingBox(x1
, y1
);
515 CalcBoundingBox(x2
, y2
);
518 void wxDC
::DoDrawLine(wxCoord x1
, wxCoord y1
, wxCoord x2
, wxCoord y2
)
520 #ifdef __WXMICROWIN__
521 if (!GetHDC()) return;
524 (void)MoveToEx(GetHdc(), XLOG2DEV(x1
), YLOG2DEV(y1
), NULL
);
525 (void)LineTo(GetHdc(), XLOG2DEV(x2
), YLOG2DEV(y2
));
527 // Normalization: Windows doesn't draw the last point of the line.
528 // But apparently neither does GTK+, so we take it out again.
529 // (void)LineTo(GetHdc(), XLOG2DEV(x2) + 1, YLOG2DEV(y2));
531 CalcBoundingBox(x1
, y1
);
532 CalcBoundingBox(x2
, y2
);
535 // Draws an arc of a circle, centred on (xc, yc), with starting point (x1, y1)
536 // and ending at (x2, y2)
537 void wxDC
::DoDrawArc(wxCoord x1
, wxCoord y1
,
538 wxCoord x2
, wxCoord y2
,
539 wxCoord xc
, wxCoord yc
)
541 #ifdef __WXMICROWIN__
542 if (!GetHDC()) return;
545 wxColourChanger
cc(*this); // needed for wxSTIPPLE_MASK_OPAQUE handling
549 double radius
= (double)sqrt(dx
*dx
+dy
*dy
);
550 wxCoord r
= (wxCoord
)radius
;
552 // treat the special case of full circle separately
553 if ( x1
== x2
&& y1
== y2
)
555 DrawEllipse(xc
- r
, yc
- r
, 2*r
, 2*r
);
559 wxCoord xx1
= XLOG2DEV(x1
);
560 wxCoord yy1
= YLOG2DEV(y1
);
561 wxCoord xx2
= XLOG2DEV(x2
);
562 wxCoord yy2
= YLOG2DEV(y2
);
563 wxCoord xxc
= XLOG2DEV(xc
);
564 wxCoord yyc
= YLOG2DEV(yc
);
565 wxCoord ray
= (wxCoord
) sqrt(double((xxc
-xx1
)*(xxc
-xx1
)+(yyc
-yy1
)*(yyc
-yy1
)));
567 wxCoord xxx1
= (wxCoord
) (xxc
-ray
);
568 wxCoord yyy1
= (wxCoord
) (yyc
-ray
);
569 wxCoord xxx2
= (wxCoord
) (xxc
+ray
);
570 wxCoord yyy2
= (wxCoord
) (yyc
+ray
);
572 if ( m_brush
.Ok() && m_brush
.GetStyle() != wxTRANSPARENT
)
574 // Have to add 1 to bottom-right corner of rectangle
575 // to make semi-circles look right (crooked line otherwise).
576 // Unfortunately this is not a reliable method, depends
577 // on the size of shape.
578 // TODO: figure out why this happens!
579 Pie(GetHdc(),xxx1
,yyy1
,xxx2
+1,yyy2
+1, xx1
,yy1
,xx2
,yy2
);
583 Arc(GetHdc(),xxx1
,yyy1
,xxx2
,yyy2
, xx1
,yy1
,xx2
,yy2
);
586 CalcBoundingBox(xc
- r
, yc
- r
);
587 CalcBoundingBox(xc
+ r
, yc
+ r
);
590 void wxDC
::DoDrawCheckMark(wxCoord x1
, wxCoord y1
,
591 wxCoord width
, wxCoord height
)
593 #ifdef __WXMICROWIN__
594 if (!GetHDC()) return;
597 wxCoord x2
= x1
+ width
,
600 #if defined(__WIN32__) && !defined(__SC__) && !defined(__WXMICROWIN__)
607 DrawFrameControl(GetHdc(), &rect
, DFC_MENU
, DFCS_MENUCHECK
);
609 // In WIN16, draw a cross
610 HPEN blackPen
= ::CreatePen(PS_SOLID
, 1, RGB(0, 0, 0));
611 HPEN whiteBrush
= (HPEN
)::GetStockObject(WHITE_BRUSH
);
612 HPEN hPenOld
= (HPEN
)::SelectObject(GetHdc(), blackPen
);
613 HPEN hBrushOld
= (HPEN
)::SelectObject(GetHdc(), whiteBrush
);
614 ::SetROP2(GetHdc(), R2_COPYPEN
);
615 Rectangle(GetHdc(), x1
, y1
, x2
, y2
);
616 MoveToEx(GetHdc(), x1
, y1
, NULL
);
617 LineTo(GetHdc(), x2
, y2
);
618 MoveToEx(GetHdc(), x2
, y1
, NULL
);
619 LineTo(GetHdc(), x1
, y2
);
620 ::SelectObject(GetHdc(), hPenOld
);
621 ::SelectObject(GetHdc(), hBrushOld
);
622 ::DeleteObject(blackPen
);
625 CalcBoundingBox(x1
, y1
);
626 CalcBoundingBox(x2
, y2
);
629 void wxDC
::DoDrawPoint(wxCoord x
, wxCoord y
)
631 #ifdef __WXMICROWIN__
632 if (!GetHDC()) return;
635 COLORREF color
= 0x00ffffff;
638 color
= m_pen
.GetColour().GetPixel();
641 SetPixel(GetHdc(), XLOG2DEV(x
), YLOG2DEV(y
), color
);
643 CalcBoundingBox(x
, y
);
646 void wxDC
::DoDrawPolygon(int n
, wxPoint points
[], wxCoord xoffset
, wxCoord yoffset
,int fillStyle
)
648 #ifdef __WXMICROWIN__
649 if (!GetHDC()) return;
652 wxColourChanger
cc(*this); // needed for wxSTIPPLE_MASK_OPAQUE handling
654 // Do things less efficiently if we have offsets
655 if (xoffset
!= 0 || yoffset
!= 0)
657 POINT
*cpoints
= new POINT
[n
];
659 for (i
= 0; i
< n
; i
++)
661 cpoints
[i
].x
= (int)(points
[i
].x
+ xoffset
);
662 cpoints
[i
].y
= (int)(points
[i
].y
+ yoffset
);
664 CalcBoundingBox(cpoints
[i
].x
, cpoints
[i
].y
);
666 int prev
= SetPolyFillMode(GetHdc(),fillStyle
==wxODDEVEN_RULE?ALTERNATE
:WINDING
);
667 (void)Polygon(GetHdc(), cpoints
, n
);
668 SetPolyFillMode(GetHdc(),prev
);
674 for (i
= 0; i
< n
; i
++)
675 CalcBoundingBox(points
[i
].x
, points
[i
].y
);
677 int prev
= SetPolyFillMode(GetHdc(),fillStyle
==wxODDEVEN_RULE?ALTERNATE
:WINDING
);
678 (void)Polygon(GetHdc(), (POINT
*) points
, n
);
679 SetPolyFillMode(GetHdc(),prev
);
683 void wxDC
::DoDrawLines(int n
, wxPoint points
[], wxCoord xoffset
, wxCoord yoffset
)
685 #ifdef __WXMICROWIN__
686 if (!GetHDC()) return;
689 // Do things less efficiently if we have offsets
690 if (xoffset
!= 0 || yoffset
!= 0)
692 POINT
*cpoints
= new POINT
[n
];
694 for (i
= 0; i
< n
; i
++)
696 cpoints
[i
].x
= (int)(points
[i
].x
+ xoffset
);
697 cpoints
[i
].y
= (int)(points
[i
].y
+ yoffset
);
699 CalcBoundingBox(cpoints
[i
].x
, cpoints
[i
].y
);
701 (void)Polyline(GetHdc(), cpoints
, n
);
707 for (i
= 0; i
< n
; i
++)
708 CalcBoundingBox(points
[i
].x
, points
[i
].y
);
710 (void)Polyline(GetHdc(), (POINT
*) points
, n
);
714 void wxDC
::DoDrawRectangle(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
716 #ifdef __WXMICROWIN__
717 if (!GetHDC()) return;
720 wxColourChanger
cc(*this); // needed for wxSTIPPLE_MASK_OPAQUE handling
722 wxCoord x2
= x
+ width
;
723 wxCoord y2
= y
+ height
;
725 if ((m_logicalFunction
== wxCOPY
) && (m_pen
.GetStyle() == wxTRANSPARENT
))
728 rect
.left
= XLOG2DEV(x
);
729 rect
.top
= YLOG2DEV(y
);
730 rect
.right
= XLOG2DEV(x2
);
731 rect
.bottom
= YLOG2DEV(y2
);
732 (void)FillRect(GetHdc(), &rect
, (HBRUSH
)m_brush
.GetResourceHandle() );
736 // Windows draws the filled rectangles without outline (i.e. drawn with a
737 // transparent pen) one pixel smaller in both directions and we want them
738 // to have the same size regardless of which pen is used - adjust
740 // I wonder if this shouldn´t be done after the LOG2DEV() conversions. RR.
741 if ( m_pen
.GetStyle() == wxTRANSPARENT
)
747 (void)Rectangle(GetHdc(), XLOG2DEV(x
), YLOG2DEV(y
), XLOG2DEV(x2
), YLOG2DEV(y2
));
751 CalcBoundingBox(x
, y
);
752 CalcBoundingBox(x2
, y2
);
755 void wxDC
::DoDrawRoundedRectangle(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
, double radius
)
757 #ifdef __WXMICROWIN__
758 if (!GetHDC()) return;
761 wxColourChanger
cc(*this); // needed for wxSTIPPLE_MASK_OPAQUE handling
763 // Now, a negative radius value is interpreted to mean
764 // 'the proportion of the smallest X or Y dimension'
768 double smallest
= 0.0;
773 radius
= (- radius
* smallest
);
776 wxCoord x2
= (x
+width
);
777 wxCoord y2
= (y
+height
);
779 // Windows draws the filled rectangles without outline (i.e. drawn with a
780 // transparent pen) one pixel smaller in both directions and we want them
781 // to have the same size regardless of which pen is used - adjust
782 if ( m_pen
.GetStyle() == wxTRANSPARENT
)
788 (void)RoundRect(GetHdc(), XLOG2DEV(x
), YLOG2DEV(y
), XLOG2DEV(x2
),
789 YLOG2DEV(y2
), (int) (2*XLOG2DEV(radius
)), (int)( 2*YLOG2DEV(radius
)));
791 CalcBoundingBox(x
, y
);
792 CalcBoundingBox(x2
, y2
);
795 void wxDC
::DoDrawEllipse(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
797 #ifdef __WXMICROWIN__
798 if (!GetHDC()) return;
801 wxColourChanger
cc(*this); // needed for wxSTIPPLE_MASK_OPAQUE handling
803 wxCoord x2
= (x
+width
);
804 wxCoord y2
= (y
+height
);
806 (void)Ellipse(GetHdc(), XLOG2DEV(x
), YLOG2DEV(y
), XLOG2DEV(x2
), YLOG2DEV(y2
));
808 CalcBoundingBox(x
, y
);
809 CalcBoundingBox(x2
, y2
);
812 // Chris Breeze 20/5/98: first implementation of DrawEllipticArc on Windows
813 void wxDC
::DoDrawEllipticArc(wxCoord x
,wxCoord y
,wxCoord w
,wxCoord h
,double sa
,double ea
)
815 #ifdef __WXMICROWIN__
816 if (!GetHDC()) return;
819 wxColourChanger
cc(*this); // needed for wxSTIPPLE_MASK_OPAQUE handling
824 int rx1
= XLOG2DEV(x
+w
/2);
825 int ry1
= YLOG2DEV(y
+h
/2);
832 rx1
+= (int)(100.0 * abs(w
) * cos(sa
));
833 ry1
-= (int)(100.0 * abs(h
) * m_signY
* sin(sa
));
834 rx2
+= (int)(100.0 * abs(w
) * cos(ea
));
835 ry2
-= (int)(100.0 * abs(h
) * m_signY
* sin(ea
));
837 // draw pie with NULL_PEN first and then outline otherwise a line is
838 // drawn from the start and end points to the centre
839 HPEN hpenOld
= (HPEN
) ::SelectObject(GetHdc(), (HPEN
) ::GetStockObject(NULL_PEN
));
842 (void)Pie(GetHdc(), XLOG2DEV(x
), YLOG2DEV(y
), XLOG2DEV(x2
)+1, YLOG2DEV(y2
)+1,
847 (void)Pie(GetHdc(), XLOG2DEV(x
), YLOG2DEV(y
)-1, XLOG2DEV(x2
)+1, YLOG2DEV(y2
),
848 rx1
, ry1
-1, rx2
, ry2
-1);
851 ::SelectObject(GetHdc(), hpenOld
);
853 (void)Arc(GetHdc(), XLOG2DEV(x
), YLOG2DEV(y
), XLOG2DEV(x2
), YLOG2DEV(y2
),
856 CalcBoundingBox(x
, y
);
857 CalcBoundingBox(x2
, y2
);
860 void wxDC
::DoDrawIcon(const wxIcon
& icon
, wxCoord x
, wxCoord y
)
862 #ifdef __WXMICROWIN__
863 if (!GetHDC()) return;
866 wxCHECK_RET( icon
.Ok(), wxT("invalid icon in DrawIcon") );
869 ::DrawIconEx(GetHdc(), XLOG2DEV(x
), YLOG2DEV(y
), GetHiconOf(icon
), icon
.GetWidth(), icon
.GetHeight(), 0, NULL
, DI_NORMAL
);
871 ::DrawIcon(GetHdc(), XLOG2DEV(x
), YLOG2DEV(y
), GetHiconOf(icon
));
874 CalcBoundingBox(x
, y
);
875 CalcBoundingBox(x
+ icon
.GetWidth(), y
+ icon
.GetHeight());
878 void wxDC
::DoDrawBitmap( const wxBitmap
&bmp
, wxCoord x
, wxCoord y
, bool useMask
)
880 #ifdef __WXMICROWIN__
881 if (!GetHDC()) return;
884 wxCHECK_RET( bmp
.Ok(), _T("invalid bitmap in wxDC::DrawBitmap") );
886 int width
= bmp
.GetWidth(),
887 height
= bmp
.GetHeight();
889 HBITMAP hbmpMask
= 0;
893 #endif // wxUSE_PALETTE
897 wxMask
*mask
= bmp
.GetMask();
899 hbmpMask
= (HBITMAP
)mask
->GetMaskBitmap();
903 // don't give assert here because this would break existing
904 // programs - just silently ignore useMask parameter
911 // use MaskBlt() with ROP which doesn't do anything to dst in the mask
913 // On some systems, MaskBlt succeeds yet is much much slower
914 // than the wxWindows fall-back implementation. So we need
915 // to be able to switch this on and off at runtime.
917 #if wxUSE_SYSTEM_OPTIONS
918 if (wxSystemOptions
::GetOptionInt(wxT("no-maskblt")) == 0)
922 HDC hdcMem
= ::CreateCompatibleDC(GetHdc());
923 HGDIOBJ hOldBitmap
= ::SelectObject(hdcMem
, GetHbitmapOf(bmp
));
925 wxPalette
*pal
= bmp
.GetPalette();
926 if ( pal
&& ::GetDeviceCaps(cdc
,BITSPIXEL
) <= 8 )
928 oldPal
= ::SelectPalette(hdcMem
, GetHpaletteOf(pal
), FALSE
);
929 ::RealizePalette(hdcMem
);
931 #endif // wxUSE_PALETTE
933 ok
= ::MaskBlt(cdc
, x
, y
, width
, height
,
936 MAKEROP4(SRCCOPY
, DSTCOPY
)) != 0;
940 ::SelectPalette(hdcMem
, oldPal
, FALSE
);
941 #endif // wxUSE_PALETTE
943 ::SelectObject(hdcMem
, hOldBitmap
);
950 // Rather than reproduce wxDC::Blit, let's do it at the wxWin API
953 memDC
.SelectObject(bmp
);
955 Blit(x
, y
, width
, height
, &memDC
, 0, 0, wxCOPY
, useMask
);
957 memDC
.SelectObject(wxNullBitmap
);
960 else // no mask, just use BitBlt()
963 HDC memdc
= ::CreateCompatibleDC( cdc
);
964 HBITMAP hbitmap
= (HBITMAP
) bmp
.GetHBITMAP( );
966 wxASSERT_MSG( hbitmap
, wxT("bitmap is ok but HBITMAP is NULL?") );
968 COLORREF old_textground
= ::GetTextColor(GetHdc());
969 COLORREF old_background
= ::GetBkColor(GetHdc());
970 if (m_textForegroundColour
.Ok())
972 ::SetTextColor(GetHdc(), m_textForegroundColour
.GetPixel() );
974 if (m_textBackgroundColour
.Ok())
976 ::SetBkColor(GetHdc(), m_textBackgroundColour
.GetPixel() );
980 wxPalette
*pal
= bmp
.GetPalette();
981 if ( pal
&& ::GetDeviceCaps(cdc
,BITSPIXEL
) <= 8 )
983 oldPal
= ::SelectPalette(memdc
, GetHpaletteOf(pal
), FALSE
);
984 ::RealizePalette(memdc
);
986 #endif // wxUSE_PALETTE
988 HGDIOBJ hOldBitmap
= ::SelectObject( memdc
, hbitmap
);
989 ::BitBlt( cdc
, x
, y
, width
, height
, memdc
, 0, 0, SRCCOPY
);
993 ::SelectPalette(memdc
, oldPal
, FALSE
);
994 #endif // wxUSE_PALETTE
996 ::SelectObject( memdc
, hOldBitmap
);
999 ::SetTextColor(GetHdc(), old_textground
);
1000 ::SetBkColor(GetHdc(), old_background
);
1004 void wxDC
::DoDrawText(const wxString
& text
, wxCoord x
, wxCoord y
)
1006 #ifdef __WXMICROWIN__
1007 if (!GetHDC()) return;
1010 DrawAnyText(text
, x
, y
);
1012 // update the bounding box
1013 CalcBoundingBox(x
, y
);
1016 GetTextExtent(text
, &w
, &h
);
1017 CalcBoundingBox(x
+ w
, y
+ h
);
1020 void wxDC
::DrawAnyText(const wxString
& text
, wxCoord x
, wxCoord y
)
1022 #ifdef __WXMICROWIN__
1023 if (!GetHDC()) return;
1026 // prepare for drawing the text
1027 if ( m_textForegroundColour
.Ok() )
1028 SetTextColor(GetHdc(), m_textForegroundColour
.GetPixel());
1030 DWORD old_background
= 0;
1031 if ( m_textBackgroundColour
.Ok() )
1033 old_background
= SetBkColor(GetHdc(), m_textBackgroundColour
.GetPixel() );
1036 SetBkMode(GetHdc(), m_backgroundMode
== wxTRANSPARENT ? TRANSPARENT
1039 if ( ::TextOut(GetHdc(), XLOG2DEV(x
), YLOG2DEV(y
),
1040 text
.c_str(), text
.length()) == 0 )
1042 wxLogLastError(wxT("TextOut"));
1045 // restore the old parameters (text foreground colour may be left because
1046 // it never is set to anything else, but background should remain
1047 // transparent even if we just drew an opaque string)
1048 if ( m_textBackgroundColour
.Ok() )
1049 (void)SetBkColor(GetHdc(), old_background
);
1051 SetBkMode(GetHdc(), TRANSPARENT
);
1054 void wxDC
::DoDrawRotatedText(const wxString
& text
,
1055 wxCoord x
, wxCoord y
,
1058 #ifdef __WXMICROWIN__
1059 if (!GetHDC()) return;
1062 // we test that we have some font because otherwise we should still use the
1063 // "else" part below to avoid that DrawRotatedText(angle = 180) and
1064 // DrawRotatedText(angle = 0) use different fonts (we can't use the default
1065 // font for drawing rotated fonts unfortunately)
1066 if ( (angle
== 0.0) && m_font
.Ok() )
1068 DoDrawText(text
, x
, y
);
1070 #ifndef __WXMICROWIN__
1073 // NB: don't take DEFAULT_GUI_FONT (a.k.a. wxSYS_DEFAULT_GUI_FONT)
1074 // because it's not TrueType and so can't have non zero
1075 // orientation/escapement under Win9x
1076 wxFont font
= m_font
.Ok() ? m_font
: *wxSWISS_FONT
;
1077 HFONT hfont
= (HFONT
)font
.GetResourceHandle();
1079 if ( ::GetObject(hfont
, sizeof(lf
), &lf
) == 0 )
1081 wxLogLastError(wxT("GetObject(hfont)"));
1084 // GDI wants the angle in tenth of degree
1085 long angle10
= (long)(angle
* 10);
1086 lf
.lfEscapement
= angle10
;
1087 lf
. lfOrientation
= angle10
;
1089 hfont
= ::CreateFontIndirect(&lf
);
1092 wxLogLastError(wxT("CreateFont"));
1096 HFONT hfontOld
= (HFONT
)::SelectObject(GetHdc(), hfont
);
1098 DrawAnyText(text
, x
, y
);
1100 (void)::SelectObject(GetHdc(), hfontOld
);
1101 (void)::DeleteObject(hfont
);
1104 // call the bounding box by adding all four vertices of the rectangle
1105 // containing the text to it (simpler and probably not slower than
1106 // determining which of them is really topmost/leftmost/...)
1108 GetTextExtent(text
, &w
, &h
);
1110 double rad
= DegToRad(angle
);
1112 // "upper left" and "upper right"
1113 CalcBoundingBox(x
, y
);
1114 CalcBoundingBox(x
+ w
*cos(rad
), y
- h
*sin(rad
));
1116 // "bottom left" and "bottom right"
1117 x
+= (wxCoord
)(h
*sin(rad
));
1118 y
+= (wxCoord
)(h
*cos(rad
));
1119 CalcBoundingBox(x
, y
);
1120 CalcBoundingBox(x
+ h
*sin(rad
), y
+ h
*cos(rad
));
1125 // ---------------------------------------------------------------------------
1127 // ---------------------------------------------------------------------------
1131 void wxDC
::SetPalette(const wxPalette
& palette
)
1133 #ifdef __WXMICROWIN__
1134 if (!GetHDC()) return;
1137 // Set the old object temporarily, in case the assignment deletes an object
1138 // that's not yet selected out.
1141 ::SelectPalette(GetHdc(), (HPALETTE
) m_oldPalette
, FALSE
);
1145 m_palette
= palette
;
1147 if (!m_palette
.Ok())
1149 // Setting a NULL colourmap is a way of restoring
1150 // the original colourmap
1153 ::SelectPalette(GetHdc(), (HPALETTE
) m_oldPalette
, FALSE
);
1160 if (m_palette
.Ok() && m_palette
.GetHPALETTE())
1162 HPALETTE oldPal
= ::SelectPalette(GetHdc(), (HPALETTE
) m_palette
.GetHPALETTE(), FALSE
);
1164 m_oldPalette
= (WXHPALETTE
) oldPal
;
1166 ::RealizePalette(GetHdc());
1170 #endif // wxUSE_PALETTE
1172 void wxDC
::SetFont(const wxFont
& the_font
)
1174 #ifdef __WXMICROWIN__
1175 if (!GetHDC()) return;
1178 // Set the old object temporarily, in case the assignment deletes an object
1179 // that's not yet selected out.
1182 ::SelectObject(GetHdc(), (HFONT
) m_oldFont
);
1191 ::SelectObject(GetHdc(), (HFONT
) m_oldFont
);
1195 if (m_font
.Ok() && m_font
.GetResourceHandle())
1197 HFONT f
= (HFONT
) ::SelectObject(GetHdc(), (HFONT
) m_font
.GetResourceHandle());
1198 if (f
== (HFONT
) NULL
)
1200 wxLogDebug(wxT("::SelectObject failed in wxDC::SetFont."));
1203 m_oldFont
= (WXHFONT
) f
;
1207 void wxDC
::SetPen(const wxPen
& pen
)
1209 #ifdef __WXMICROWIN__
1210 if (!GetHDC()) return;
1213 // Set the old object temporarily, in case the assignment deletes an object
1214 // that's not yet selected out.
1217 ::SelectObject(GetHdc(), (HPEN
) m_oldPen
);
1226 ::SelectObject(GetHdc(), (HPEN
) m_oldPen
);
1232 if (m_pen
.GetResourceHandle())
1234 HPEN p
= (HPEN
) ::SelectObject(GetHdc(), (HPEN
)m_pen
.GetResourceHandle());
1236 m_oldPen
= (WXHPEN
) p
;
1241 void wxDC
::SetBrush(const wxBrush
& brush
)
1243 #ifdef __WXMICROWIN__
1244 if (!GetHDC()) return;
1247 // Set the old object temporarily, in case the assignment deletes an object
1248 // that's not yet selected out.
1251 ::SelectObject(GetHdc(), (HBRUSH
) m_oldBrush
);
1260 ::SelectObject(GetHdc(), (HBRUSH
) m_oldBrush
);
1266 // to make sure the brush is alligned with the logical coordinates
1267 wxBitmap
*stipple
= m_brush
.GetStipple();
1268 if ( stipple
&& stipple
->Ok() )
1271 ::SetBrushOrgEx(GetHdc(),
1272 m_deviceOriginX
% stipple
->GetWidth(),
1273 m_deviceOriginY
% stipple
->GetHeight(),
1274 NULL
); // don't need previous brush origin
1276 ::SetBrushOrg(GetHdc(),
1277 m_deviceOriginX
% stipple
->GetWidth(),
1278 m_deviceOriginY
% stipple
->GetHeight());
1282 if ( m_brush
.GetResourceHandle() )
1285 b
= (HBRUSH
) ::SelectObject(GetHdc(), (HBRUSH
)m_brush
.GetResourceHandle());
1287 m_oldBrush
= (WXHBRUSH
) b
;
1292 void wxDC
::SetBackground(const wxBrush
& brush
)
1294 #ifdef __WXMICROWIN__
1295 if (!GetHDC()) return;
1298 m_backgroundBrush
= brush
;
1300 if (!m_backgroundBrush
.Ok())
1305 bool customColours
= TRUE
;
1306 // If we haven't specified wxUSER_COLOURS, don't allow the panel/dialog box to
1307 // change background colours from the control-panel specified colours.
1308 if (m_canvas
->IsKindOf(CLASSINFO(wxWindow
)) && ((m_canvas
->GetWindowStyleFlag() & wxUSER_COLOURS
) != wxUSER_COLOURS
))
1309 customColours
= FALSE
;
1313 if (m_backgroundBrush
.GetStyle()==wxTRANSPARENT
)
1315 m_canvas
->SetTransparent(TRUE
);
1319 // New behaviour, 10/2/99: setting the background brush of a DC
1320 // doesn't affect the window background colour. However,
1321 // I'm leaving in the transparency setting because it's needed by
1322 // various controls (e.g. wxStaticText) to determine whether to draw
1323 // transparently or not. TODO: maybe this should be a new function
1324 // wxWindow::SetTransparency(). Should that apply to the child itself, or the
1326 // m_canvas->SetBackgroundColour(m_backgroundBrush.GetColour());
1327 m_canvas
->SetTransparent(FALSE
);
1331 COLORREF new_color
= m_backgroundBrush
.GetColour().GetPixel();
1333 (void)SetBkColor(GetHdc(), new_color
);
1337 void wxDC
::SetBackgroundMode(int mode
)
1339 #ifdef __WXMICROWIN__
1340 if (!GetHDC()) return;
1343 m_backgroundMode
= mode
;
1345 // SetBackgroundColour now only refers to text background
1346 // and m_backgroundMode is used there
1349 void wxDC
::SetLogicalFunction(int function
)
1351 #ifdef __WXMICROWIN__
1352 if (!GetHDC()) return;
1355 m_logicalFunction
= function
;
1360 void wxDC
::SetRop(WXHDC dc
)
1362 if ( !dc
|| m_logicalFunction
< 0 )
1367 switch (m_logicalFunction
)
1369 case wxCLEAR
: rop
= R2_BLACK
; break;
1370 case wxXOR
: rop
= R2_XORPEN
; break;
1371 case wxINVERT
: rop
= R2_NOT
; break;
1372 case wxOR_REVERSE
: rop
= R2_MERGEPENNOT
; break;
1373 case wxAND_REVERSE
: rop
= R2_MASKPENNOT
; break;
1374 case wxCOPY
: rop
= R2_COPYPEN
; break;
1375 case wxAND
: rop
= R2_MASKPEN
; break;
1376 case wxAND_INVERT
: rop
= R2_MASKNOTPEN
; break;
1377 case wxNO_OP
: rop
= R2_NOP
; break;
1378 case wxNOR
: rop
= R2_NOTMERGEPEN
; break;
1379 case wxEQUIV
: rop
= R2_NOTXORPEN
; break;
1380 case wxSRC_INVERT
: rop
= R2_NOTCOPYPEN
; break;
1381 case wxOR_INVERT
: rop
= R2_MERGENOTPEN
; break;
1382 case wxNAND
: rop
= R2_NOTMASKPEN
; break;
1383 case wxOR
: rop
= R2_MERGEPEN
; break;
1384 case wxSET
: rop
= R2_WHITE
; break;
1387 wxFAIL_MSG( wxT("unsupported logical function") );
1391 SetROP2(GetHdc(), rop
);
1394 bool wxDC
::StartDoc(const wxString
& WXUNUSED(message
))
1396 // We might be previewing, so return TRUE to let it continue.
1404 void wxDC
::StartPage()
1408 void wxDC
::EndPage()
1412 // ---------------------------------------------------------------------------
1414 // ---------------------------------------------------------------------------
1416 wxCoord wxDC
::GetCharHeight() const
1418 #ifdef __WXMICROWIN__
1419 if (!GetHDC()) return 0;
1422 TEXTMETRIC lpTextMetric
;
1424 GetTextMetrics(GetHdc(), &lpTextMetric
);
1426 return YDEV2LOGREL(lpTextMetric
.tmHeight
);
1429 wxCoord wxDC
::GetCharWidth() const
1431 #ifdef __WXMICROWIN__
1432 if (!GetHDC()) return 0;
1435 TEXTMETRIC lpTextMetric
;
1437 GetTextMetrics(GetHdc(), &lpTextMetric
);
1439 return XDEV2LOGREL(lpTextMetric
.tmAveCharWidth
);
1442 void wxDC
::DoGetTextExtent(const wxString
& string
, wxCoord
*x
, wxCoord
*y
,
1443 wxCoord
*descent
, wxCoord
*externalLeading
,
1446 #ifdef __WXMICROWIN__
1451 if (descent
) *descent
= 0;
1452 if (externalLeading
) *externalLeading
= 0;
1455 #endif // __WXMICROWIN__
1460 wxASSERT_MSG( font
->Ok(), _T("invalid font in wxDC::GetTextExtent") );
1462 hfontOld
= (HFONT
)::SelectObject(GetHdc(), GetHfontOf(*font
));
1464 else // don't change the font
1472 GetTextExtentPoint(GetHdc(), string
, string
.length(), &sizeRect
);
1473 GetTextMetrics(GetHdc(), &tm
);
1475 if (x
) *x
= XDEV2LOGREL(sizeRect
.cx
);
1476 if (y
) *y
= YDEV2LOGREL(sizeRect
.cy
);
1477 if (descent
) *descent
= tm
.tmDescent
;
1478 if (externalLeading
) *externalLeading
= tm
.tmExternalLeading
;
1482 ::SelectObject(GetHdc(), hfontOld
);
1486 void wxDC
::SetMapMode(int mode
)
1488 #ifdef __WXMICROWIN__
1489 if (!GetHDC()) return;
1492 m_mappingMode
= mode
;
1494 int pixel_width
= 0;
1495 int pixel_height
= 0;
1499 pixel_width
= GetDeviceCaps(GetHdc(), HORZRES
);
1500 pixel_height
= GetDeviceCaps(GetHdc(), VERTRES
);
1501 mm_width
= GetDeviceCaps(GetHdc(), HORZSIZE
);
1502 mm_height
= GetDeviceCaps(GetHdc(), VERTSIZE
);
1504 if ((pixel_width
== 0) || (pixel_height
== 0) || (mm_width
== 0) || (mm_height
== 0))
1509 double mm2pixelsX
= pixel_width
/mm_width
;
1510 double mm2pixelsY
= pixel_height
/mm_height
;
1516 m_logicalScaleX
= (twips2mm
* mm2pixelsX
);
1517 m_logicalScaleY
= (twips2mm
* mm2pixelsY
);
1522 m_logicalScaleX
= (pt2mm
* mm2pixelsX
);
1523 m_logicalScaleY
= (pt2mm
* mm2pixelsY
);
1528 m_logicalScaleX
= mm2pixelsX
;
1529 m_logicalScaleY
= mm2pixelsY
;
1534 m_logicalScaleX
= (mm2pixelsX
/10.0);
1535 m_logicalScaleY
= (mm2pixelsY
/10.0);
1541 m_logicalScaleX
= 1.0;
1542 m_logicalScaleY
= 1.0;
1547 if (::GetMapMode(GetHdc()) != MM_ANISOTROPIC
)
1548 ::SetMapMode(GetHdc(), MM_ANISOTROPIC
);
1550 SetViewportExtEx(GetHdc(), VIEWPORT_EXTENT
, VIEWPORT_EXTENT
, NULL
);
1551 m_windowExtX
= (int)MS_XDEV2LOG(VIEWPORT_EXTENT
);
1552 m_windowExtY
= (int)MS_YDEV2LOG(VIEWPORT_EXTENT
);
1553 ::SetWindowExtEx(GetHdc(), m_windowExtX
, m_windowExtY
, NULL
);
1554 ::SetViewportOrgEx(GetHdc(), (int)m_deviceOriginX
, (int)m_deviceOriginY
, NULL
);
1555 ::SetWindowOrgEx(GetHdc(), (int)m_logicalOriginX
, (int)m_logicalOriginY
, NULL
);
1558 void wxDC
::SetUserScale(double x
, double y
)
1560 #ifdef __WXMICROWIN__
1561 if (!GetHDC()) return;
1567 SetMapMode(m_mappingMode
);
1570 void wxDC
::SetAxisOrientation(bool xLeftRight
, bool yBottomUp
)
1572 #ifdef __WXMICROWIN__
1573 if (!GetHDC()) return;
1576 m_signX
= xLeftRight ?
1 : -1;
1577 m_signY
= yBottomUp ?
-1 : 1;
1579 SetMapMode(m_mappingMode
);
1582 void wxDC
::SetSystemScale(double x
, double y
)
1584 #ifdef __WXMICROWIN__
1585 if (!GetHDC()) return;
1591 SetMapMode(m_mappingMode
);
1594 void wxDC
::SetLogicalOrigin(wxCoord x
, wxCoord y
)
1596 #ifdef __WXMICROWIN__
1597 if (!GetHDC()) return;
1600 m_logicalOriginX
= x
;
1601 m_logicalOriginY
= y
;
1603 ::SetWindowOrgEx(GetHdc(), (int)m_logicalOriginX
, (int)m_logicalOriginY
, NULL
);
1606 void wxDC
::SetDeviceOrigin(wxCoord x
, wxCoord y
)
1608 #ifdef __WXMICROWIN__
1609 if (!GetHDC()) return;
1612 m_deviceOriginX
= x
;
1613 m_deviceOriginY
= y
;
1615 ::SetViewportOrgEx(GetHdc(), (int)m_deviceOriginX
, (int)m_deviceOriginY
, NULL
);
1618 // ---------------------------------------------------------------------------
1619 // coordinates transformations
1620 // ---------------------------------------------------------------------------
1622 wxCoord wxDCBase
::DeviceToLogicalX(wxCoord x
) const
1624 double xRel
= x
- m_deviceOriginX
;
1625 xRel
/= m_logicalScaleX
*m_userScaleX
*m_signX
*m_scaleX
;
1626 return (wxCoord
)(xRel
+ m_logicalOriginX
);
1629 wxCoord wxDCBase
::DeviceToLogicalXRel(wxCoord x
) const
1631 // axis orientation is not taken into account for conversion of a distance
1632 return (wxCoord
) ((x
)/(m_logicalScaleX
*m_userScaleX
*m_scaleX
));
1635 wxCoord wxDCBase
::DeviceToLogicalY(wxCoord y
) const
1637 double yRel
= y
- m_deviceOriginY
;
1638 yRel
/= m_logicalScaleY
*m_userScaleY
*m_signY
*m_scaleY
;
1639 return (wxCoord
)(yRel
+ m_logicalOriginY
);
1642 wxCoord wxDCBase
::DeviceToLogicalYRel(wxCoord y
) const
1644 // axis orientation is not taken into account for conversion of a distance
1645 return (wxCoord
) ((y
)/(m_logicalScaleY
*m_userScaleY
*m_scaleY
));
1648 wxCoord wxDCBase
::LogicalToDeviceX(wxCoord x
) const
1650 return (wxCoord
) ((x
- m_logicalOriginX
)*m_logicalScaleX
*m_userScaleX
*m_signX
*m_scaleX
+ m_deviceOriginX
);
1653 wxCoord wxDCBase
::LogicalToDeviceXRel(wxCoord x
) const
1655 // axis orientation is not taken into account for conversion of a distance
1656 return (wxCoord
) (x
*m_logicalScaleX
*m_userScaleX
*m_scaleX
);
1659 wxCoord wxDCBase
::LogicalToDeviceY(wxCoord y
) const
1661 return (wxCoord
) ((y
- m_logicalOriginY
)*m_logicalScaleY
*m_userScaleY
*m_signY
*m_scaleY
+ m_deviceOriginY
);
1664 wxCoord wxDCBase
::LogicalToDeviceYRel(wxCoord y
) const
1666 // axis orientation is not taken into account for conversion of a distance
1667 return (wxCoord
) (y
*m_logicalScaleY
*m_userScaleY
*m_scaleY
);
1670 // ---------------------------------------------------------------------------
1672 // ---------------------------------------------------------------------------
1674 bool wxDC
::DoBlit(wxCoord xdest
, wxCoord ydest
,
1675 wxCoord width
, wxCoord height
,
1676 wxDC
*source
, wxCoord xsrc
, wxCoord ysrc
,
1677 int rop
, bool useMask
,
1678 wxCoord xsrcMask
, wxCoord ysrcMask
)
1680 #ifdef __WXMICROWIN__
1681 if (!GetHDC()) return FALSE
;
1684 wxMask
*mask
= NULL
;
1687 const wxBitmap
& bmp
= source
->m_selectedBitmap
;
1688 mask
= bmp
.GetMask();
1690 if ( !(bmp
.Ok() && mask
&& mask
->GetMaskBitmap()) )
1692 // don't give assert here because this would break existing
1693 // programs - just silently ignore useMask parameter
1698 if (xsrcMask
== -1 && ysrcMask
== -1)
1700 xsrcMask
= xsrc
; ysrcMask
= ysrc
;
1703 COLORREF old_textground
= ::GetTextColor(GetHdc());
1704 COLORREF old_background
= ::GetBkColor(GetHdc());
1705 if (m_textForegroundColour
.Ok())
1707 ::SetTextColor(GetHdc(), m_textForegroundColour
.GetPixel() );
1709 if (m_textBackgroundColour
.Ok())
1711 ::SetBkColor(GetHdc(), m_textBackgroundColour
.GetPixel() );
1714 DWORD dwRop
= SRCCOPY
;
1717 case wxXOR
: dwRop
= SRCINVERT
; break;
1718 case wxINVERT
: dwRop
= DSTINVERT
; break;
1719 case wxOR_REVERSE
: dwRop
= 0x00DD0228; break;
1720 case wxAND_REVERSE
: dwRop
= SRCERASE
; break;
1721 case wxCLEAR
: dwRop
= BLACKNESS
; break;
1722 case wxSET
: dwRop
= WHITENESS
; break;
1723 case wxOR_INVERT
: dwRop
= MERGEPAINT
; break;
1724 case wxAND
: dwRop
= SRCAND
; break;
1725 case wxOR
: dwRop
= SRCPAINT
; break;
1726 case wxEQUIV
: dwRop
= 0x00990066; break;
1727 case wxNAND
: dwRop
= 0x007700E6; break;
1728 case wxAND_INVERT
: dwRop
= 0x00220326; break;
1729 case wxCOPY
: dwRop
= SRCCOPY
; break;
1730 case wxNO_OP
: dwRop
= DSTCOPY
; break;
1731 case wxSRC_INVERT
: dwRop
= NOTSRCCOPY
; break;
1732 case wxNOR
: dwRop
= NOTSRCCOPY
; break;
1734 wxFAIL_MSG( wxT("unsupported logical function") );
1738 bool success
= FALSE
;
1743 // we want the part of the image corresponding to the mask to be
1744 // transparent, so use "DSTCOPY" ROP for the mask points (the usual
1745 // meaning of fg and bg is inverted which corresponds to wxWin notion
1746 // of the mask which is also contrary to the Windows one)
1748 // On some systems, MaskBlt succeeds yet is much much slower
1749 // than the wxWindows fall-back implementation. So we need
1750 // to be able to switch this on and off at runtime.
1751 #if wxUSE_SYSTEM_OPTIONS
1752 if (wxSystemOptions
::GetOptionInt(wxT("no-maskblt")) == 0)
1755 success
= ::MaskBlt(GetHdc(), xdest
, ydest
, width
, height
,
1756 GetHdcOf(*source
), xsrc
, ysrc
,
1757 (HBITMAP
)mask
->GetMaskBitmap(), xsrcMask
, ysrcMask
,
1758 MAKEROP4(dwRop
, DSTCOPY
)) != 0;
1764 // Blit bitmap with mask
1767 HBITMAP buffer_bmap
;
1769 #if wxUSE_DC_CACHEING
1770 // create a temp buffer bitmap and DCs to access it and the mask
1771 wxDCCacheEntry
* dcCacheEntry1
= FindDCInCache(NULL
, source
->GetHDC());
1772 dc_mask
= (HDC
) dcCacheEntry1
->m_dc
;
1774 wxDCCacheEntry
* dcCacheEntry2
= FindDCInCache(dcCacheEntry1
, GetHDC());
1775 dc_buffer
= (HDC
) dcCacheEntry2
->m_dc
;
1777 wxDCCacheEntry
* bitmapCacheEntry
= FindBitmapInCache(GetHDC(),
1780 buffer_bmap
= (HBITMAP
) bitmapCacheEntry
->m_bitmap
;
1781 #else // !wxUSE_DC_CACHEING
1782 // create a temp buffer bitmap and DCs to access it and the mask
1783 dc_mask
= ::CreateCompatibleDC(GetHdcOf(*source
));
1784 dc_buffer
= ::CreateCompatibleDC(GetHdc());
1785 buffer_bmap
= ::CreateCompatibleBitmap(GetHdc(), width
, height
);
1786 #endif // wxUSE_DC_CACHEING/!wxUSE_DC_CACHEING
1787 HGDIOBJ hOldMaskBitmap
= ::SelectObject(dc_mask
, (HBITMAP
) mask
->GetMaskBitmap());
1788 HGDIOBJ hOldBufferBitmap
= ::SelectObject(dc_buffer
, buffer_bmap
);
1790 // copy dest to buffer
1791 if ( !::BitBlt(dc_buffer
, 0, 0, (int)width
, (int)height
,
1792 GetHdc(), xdest
, ydest
, SRCCOPY
) )
1794 wxLogLastError(wxT("BitBlt"));
1797 // copy src to buffer using selected raster op
1798 if ( !::BitBlt(dc_buffer
, 0, 0, (int)width
, (int)height
,
1799 GetHdcOf(*source
), xsrc
, ysrc
, dwRop
) )
1801 wxLogLastError(wxT("BitBlt"));
1804 // set masked area in buffer to BLACK (pixel value 0)
1805 COLORREF prevBkCol
= ::SetBkColor(GetHdc(), RGB(255, 255, 255));
1806 COLORREF prevCol
= ::SetTextColor(GetHdc(), RGB(0, 0, 0));
1807 if ( !::BitBlt(dc_buffer
, 0, 0, (int)width
, (int)height
,
1808 dc_mask
, xsrcMask
, ysrcMask
, SRCAND
) )
1810 wxLogLastError(wxT("BitBlt"));
1813 // set unmasked area in dest to BLACK
1814 ::SetBkColor(GetHdc(), RGB(0, 0, 0));
1815 ::SetTextColor(GetHdc(), RGB(255, 255, 255));
1816 if ( !::BitBlt(GetHdc(), xdest
, ydest
, (int)width
, (int)height
,
1817 dc_mask
, xsrcMask
, ysrcMask
, SRCAND
) )
1819 wxLogLastError(wxT("BitBlt"));
1821 ::SetBkColor(GetHdc(), prevBkCol
); // restore colours to original values
1822 ::SetTextColor(GetHdc(), prevCol
);
1824 // OR buffer to dest
1825 success
= ::BitBlt(GetHdc(), xdest
, ydest
,
1826 (int)width
, (int)height
,
1827 dc_buffer
, 0, 0, SRCPAINT
) != 0;
1830 wxLogLastError(wxT("BitBlt"));
1833 // tidy up temporary DCs and bitmap
1834 ::SelectObject(dc_mask
, hOldMaskBitmap
);
1835 ::SelectObject(dc_buffer
, hOldBufferBitmap
);
1837 #if !wxUSE_DC_CACHEING
1839 ::DeleteDC(dc_mask
);
1840 ::DeleteDC(dc_buffer
);
1841 ::DeleteObject(buffer_bmap
);
1846 else // no mask, just BitBlt() it
1848 success
= ::BitBlt(GetHdc(), xdest
, ydest
,
1849 (int)width
, (int)height
,
1850 GetHdcOf(*source
), xsrc
, ysrc
, dwRop
) != 0;
1853 wxLogLastError(wxT("BitBlt"));
1856 ::SetTextColor(GetHdc(), old_textground
);
1857 ::SetBkColor(GetHdc(), old_background
);
1862 void wxDC
::DoGetSize(int *w
, int *h
) const
1864 #ifdef __WXMICROWIN__
1865 if (!GetHDC()) return;
1868 if ( w
) *w
= ::GetDeviceCaps(GetHdc(), HORZRES
);
1869 if ( h
) *h
= ::GetDeviceCaps(GetHdc(), VERTRES
);
1872 void wxDC
::DoGetSizeMM(int *w
, int *h
) const
1874 #ifdef __WXMICROWIN__
1875 if (!GetHDC()) return;
1878 if ( w
) *w
= ::GetDeviceCaps(GetHdc(), HORZSIZE
);
1879 if ( h
) *h
= ::GetDeviceCaps(GetHdc(), VERTSIZE
);
1882 wxSize wxDC
::GetPPI() const
1884 #ifdef __WXMICROWIN__
1885 if (!GetHDC()) return wxSize();
1888 int x
= ::GetDeviceCaps(GetHdc(), LOGPIXELSX
);
1889 int y
= ::GetDeviceCaps(GetHdc(), LOGPIXELSY
);
1891 return wxSize(x
, y
);
1894 // For use by wxWindows only, unless custom units are required.
1895 void wxDC
::SetLogicalScale(double x
, double y
)
1897 #ifdef __WXMICROWIN__
1898 if (!GetHDC()) return;
1901 m_logicalScaleX
= x
;
1902 m_logicalScaleY
= y
;
1905 #if WXWIN_COMPATIBILITY
1906 void wxDC
::DoGetTextExtent(const wxString
& string
, float *x
, float *y
,
1907 float *descent
, float *externalLeading
,
1908 wxFont
*theFont
, bool use16bit
) const
1910 #ifdef __WXMICROWIN__
1911 if (!GetHDC()) return;
1914 wxCoord x1
, y1
, descent1
, externalLeading1
;
1915 GetTextExtent(string
, & x1
, & y1
, & descent1
, & externalLeading1
, theFont
, use16bit
);
1918 *descent
= descent1
;
1919 if (externalLeading
)
1920 *externalLeading
= externalLeading1
;
1924 #if wxUSE_DC_CACHEING
1927 * This implementation is a bit ugly and uses the old-fashioned wxList class, so I will
1928 * improve it in due course, either using arrays, or simply storing pointers to one
1929 * entry for the bitmap, and two for the DCs. -- JACS
1932 wxList wxDC
::sm_bitmapCache
;
1933 wxList wxDC
::sm_dcCache
;
1935 wxDCCacheEntry
::wxDCCacheEntry(WXHBITMAP hBitmap
, int w
, int h
, int depth
)
1944 wxDCCacheEntry
::wxDCCacheEntry(WXHDC hDC
, int depth
)
1953 wxDCCacheEntry
::~wxDCCacheEntry()
1956 ::DeleteObject((HBITMAP
) m_bitmap
);
1958 ::DeleteDC((HDC
) m_dc
);
1961 wxDCCacheEntry
* wxDC
::FindBitmapInCache(WXHDC dc
, int w
, int h
)
1963 int depth
= ::GetDeviceCaps((HDC
) dc
, PLANES
) * ::GetDeviceCaps((HDC
) dc
, BITSPIXEL
);
1964 wxNode
* node
= sm_bitmapCache
.First();
1967 wxDCCacheEntry
* entry
= (wxDCCacheEntry
*) node
->Data();
1969 if (entry
->m_depth
== depth
)
1971 if (entry
->m_width
< w
|| entry
->m_height
< h
)
1973 ::DeleteObject((HBITMAP
) entry
->m_bitmap
);
1974 entry
->m_bitmap
= (WXHBITMAP
) ::CreateCompatibleBitmap((HDC
) dc
, w
, h
);
1975 if ( !entry
->m_bitmap
)
1977 wxLogLastError(wxT("CreateCompatibleBitmap"));
1979 entry
->m_width
= w
; entry
->m_height
= h
;
1985 node
= node
->Next();
1987 WXHBITMAP hBitmap
= (WXHBITMAP
) ::CreateCompatibleBitmap((HDC
) dc
, w
, h
);
1990 wxLogLastError(wxT("CreateCompatibleBitmap"));
1992 wxDCCacheEntry
* entry
= new wxDCCacheEntry(hBitmap
, w
, h
, depth
);
1993 AddToBitmapCache(entry
);
1997 wxDCCacheEntry
* wxDC
::FindDCInCache(wxDCCacheEntry
* notThis
, WXHDC dc
)
1999 int depth
= ::GetDeviceCaps((HDC
) dc
, PLANES
) * ::GetDeviceCaps((HDC
) dc
, BITSPIXEL
);
2000 wxNode
* node
= sm_dcCache
.First();
2003 wxDCCacheEntry
* entry
= (wxDCCacheEntry
*) node
->Data();
2005 // Don't return the same one as we already have
2006 if (!notThis
|| (notThis
!= entry
))
2008 if (entry
->m_depth
== depth
)
2014 node
= node
->Next();
2016 WXHDC hDC
= (WXHDC
) ::CreateCompatibleDC((HDC
) dc
);
2019 wxLogLastError(wxT("CreateCompatibleDC"));
2021 wxDCCacheEntry
* entry
= new wxDCCacheEntry(hDC
, depth
);
2022 AddToDCCache(entry
);
2026 void wxDC
::AddToBitmapCache(wxDCCacheEntry
* entry
)
2028 sm_bitmapCache
.Append(entry
);
2031 void wxDC
::AddToDCCache(wxDCCacheEntry
* entry
)
2033 sm_dcCache
.Append(entry
);
2036 void wxDC
::ClearCache()
2038 sm_bitmapCache
.DeleteContents(TRUE
);
2039 sm_bitmapCache
.Clear();
2040 sm_bitmapCache
.DeleteContents(FALSE
);
2041 sm_dcCache
.DeleteContents(TRUE
);
2043 sm_dcCache
.DeleteContents(FALSE
);
2046 // Clean up cache at app exit
2047 class wxDCModule
: public wxModule
2050 virtual bool OnInit() { return TRUE
; }
2051 virtual void OnExit() { wxDC
::ClearCache(); }
2054 DECLARE_DYNAMIC_CLASS(wxDCModule
)
2057 IMPLEMENT_DYNAMIC_CLASS(wxDCModule
, wxModule
)
2060 // wxUSE_DC_CACHEING