1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/dc.cpp
3 // Purpose: wxDC class for MSW port
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ===========================================================================
14 // ===========================================================================
16 // ---------------------------------------------------------------------------
18 // ---------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
29 #include "wx/window.h"
32 #include "wx/dialog.h"
34 #include "wx/bitmap.h"
35 #include "wx/dcmemory.h"
40 #include "wx/sysopt.h"
41 #include "wx/dcprint.h"
42 #include "wx/module.h"
43 #include "wx/dynlib.h"
45 #ifdef wxHAVE_RAW_BITMAP
46 #include "wx/rawbmp.h"
51 #include "wx/msw/wrapcdlg.h"
57 #define AC_SRC_ALPHA 1
60 /* Quaternary raster codes */
62 #define MAKEROP4(fore,back) (DWORD)((((back) << 8) & 0xFF000000) | (fore))
65 // apparently with MicroWindows it is possible that HDC is 0 so we have to
66 // check for this ourselves
68 #define WXMICROWIN_CHECK_HDC if ( !GetHDC() ) return;
69 #define WXMICROWIN_CHECK_HDC_RET(x) if ( !GetHDC() ) return x;
71 #define WXMICROWIN_CHECK_HDC
72 #define WXMICROWIN_CHECK_HDC_RET(x)
75 IMPLEMENT_ABSTRACT_CLASS(wxDC
, wxDCBase
)
77 // ---------------------------------------------------------------------------
79 // ---------------------------------------------------------------------------
81 static const int VIEWPORT_EXTENT
= 1000;
83 static const int MM_POINTS
= 9;
84 static const int MM_METRIC
= 10;
86 // ROPs which don't have standard names (see "Ternary Raster Operations" in the
87 // MSDN docs for how this and other numbers in wxDC::Blit() are obtained)
88 #define DSTCOPY 0x00AA0029 // a.k.a. NOP operation
90 // ----------------------------------------------------------------------------
91 // macros for logical <-> device coords conversion
92 // ----------------------------------------------------------------------------
95 We currently let Windows do all the translations itself so these macros are
96 not really needed (any more) but keep them to enhance readability of the
97 code by allowing to see where are the logical and where are the device
102 #define XLOG2DEV(x) ((x-m_logicalOriginX)*m_signX)
103 #define YLOG2DEV(y) ((y-m_logicalOriginY)*m_signY)
104 #define XDEV2LOG(x) ((x)*m_signX+m_logicalOriginX)
105 #define YDEV2LOG(y) ((y)*m_signY+m_logicalOriginY)
107 #define XLOG2DEV(x) (x)
108 #define YLOG2DEV(y) (y)
109 #define XDEV2LOG(x) (x)
110 #define YDEV2LOG(y) (y)
113 // ---------------------------------------------------------------------------
115 // ---------------------------------------------------------------------------
117 // convert degrees to radians
118 static inline double DegToRad(double deg
) { return (deg
* M_PI
) / 180.0; }
120 // call AlphaBlend() to blit contents of hdcSrc to hdcDst using alpha
122 // NB: bmpSrc is the bitmap selected in hdcSrc, it is not really needed
123 // to pass it to this function but as we already have it at the point
124 // of call anyhow we do
126 // return true if we could draw the bitmap in one way or the other, false
128 static bool AlphaBlt(HDC hdcDst
,
129 int x
, int y
, int w
, int h
,
130 int srcX
, int srcY
, HDC hdcSrc
,
131 const wxBitmap
& bmpSrc
);
133 #ifdef wxHAVE_RAW_BITMAP
135 // our (limited) AlphaBlend() replacement for Windows versions not providing it
137 wxAlphaBlend(HDC hdcDst
, int x
, int y
, int w
, int h
,
138 int srcX
, int srcY
, const wxBitmap
& bmp
);
140 #endif // wxHAVE_RAW_BITMAP
142 // ----------------------------------------------------------------------------
144 // ----------------------------------------------------------------------------
146 // instead of duplicating the same code which sets and then restores text
147 // colours in each wxDC method working with wxSTIPPLE_MASK_OPAQUE brushes,
148 // encapsulate this in a small helper class
150 // wxColourChanger: changes the text colours in the ctor if required and
151 // restores them in the dtor
152 class wxColourChanger
155 wxColourChanger(wxDC
& dc
);
161 COLORREF m_colFgOld
, m_colBgOld
;
165 DECLARE_NO_COPY_CLASS(wxColourChanger
)
168 // this class saves the old stretch blit mode during its life time
169 class StretchBltModeChanger
172 StretchBltModeChanger(HDC hdc
,
173 int WXUNUSED_IN_WINCE(mode
))
177 m_modeOld
= ::SetStretchBltMode(m_hdc
, mode
);
179 wxLogLastError(_T("SetStretchBltMode"));
183 ~StretchBltModeChanger()
186 if ( !::SetStretchBltMode(m_hdc
, m_modeOld
) )
187 wxLogLastError(_T("SetStretchBltMode"));
196 DECLARE_NO_COPY_CLASS(StretchBltModeChanger
)
199 // support for dynamic loading of msimg32.dll which we use for some functions
203 // return the symbol with the given name if the DLL not loaded or symbol
205 static void *GetSymbol(const wxChar
*name
)
209 if ( !ms_triedToLoad
)
211 ms_triedToLoad
= true;
212 ms_dll
.Load(_T("msimg32"));
215 return ms_dll
.IsLoaded() ? ms_dll
.GetSymbol(name
) : NULL
;
219 static wxDynamicLibrary ms_dll
;
220 static bool ms_triedToLoad
;
223 wxDynamicLibrary
wxMSImg32DLL::ms_dll
;
224 bool wxMSImg32DLL::ms_triedToLoad
= false;
226 // helper macro for getting the symbols from msimg32.dll: it supposes that a
227 // type "name_t" is defined and casts the returned symbol to it automatically
228 #define wxMSIMG32_SYMBOL(name) (name ## _t)wxMSImg32DLL::GetSymbol(_T(#name))
230 // ===========================================================================
232 // ===========================================================================
234 // ----------------------------------------------------------------------------
236 // ----------------------------------------------------------------------------
238 wxColourChanger::wxColourChanger(wxDC
& dc
) : m_dc(dc
)
240 const wxBrush
& brush
= dc
.GetBrush();
241 if ( brush
.Ok() && brush
.GetStyle() == wxSTIPPLE_MASK_OPAQUE
)
243 HDC hdc
= GetHdcOf(dc
);
244 m_colFgOld
= ::GetTextColor(hdc
);
245 m_colBgOld
= ::GetBkColor(hdc
);
247 // note that Windows convention is opposite to wxWidgets one, this is
248 // why text colour becomes the background one and vice versa
249 const wxColour
& colFg
= dc
.GetTextForeground();
252 ::SetBkColor(hdc
, colFg
.GetPixel());
255 const wxColour
& colBg
= dc
.GetTextBackground();
258 ::SetTextColor(hdc
, colBg
.GetPixel());
262 dc
.GetBackgroundMode() == wxTRANSPARENT
? TRANSPARENT
265 // flag which telsl us to undo changes in the dtor
270 // nothing done, nothing to undo
275 wxColourChanger::~wxColourChanger()
279 // restore the colours we changed
280 HDC hdc
= GetHdcOf(m_dc
);
282 ::SetBkMode(hdc
, TRANSPARENT
);
283 ::SetTextColor(hdc
, m_colFgOld
);
284 ::SetBkColor(hdc
, m_colBgOld
);
288 // ---------------------------------------------------------------------------
290 // ---------------------------------------------------------------------------
296 SelectOldObjects(m_hDC
);
298 // if we own the HDC, we delete it, otherwise we just release it
302 ::DeleteDC(GetHdc());
304 else // we don't own our HDC
308 ::ReleaseDC(GetHwndOf(m_canvas
), GetHdc());
312 // Must have been a wxScreenDC
313 ::ReleaseDC((HWND
) NULL
, GetHdc());
319 // This will select current objects out of the DC,
320 // which is what you have to do before deleting the
322 void wxDC::SelectOldObjects(WXHDC dc
)
328 ::SelectObject((HDC
) dc
, (HBITMAP
) m_oldBitmap
);
330 if (m_selectedBitmap
.Ok())
332 m_selectedBitmap
.SetSelectedInto(NULL
);
339 ::SelectObject((HDC
) dc
, (HPEN
) m_oldPen
);
344 ::SelectObject((HDC
) dc
, (HBRUSH
) m_oldBrush
);
349 ::SelectObject((HDC
) dc
, (HFONT
) m_oldFont
);
356 ::SelectPalette((HDC
) dc
, (HPALETTE
) m_oldPalette
, FALSE
);
359 #endif // wxUSE_PALETTE
362 m_brush
= wxNullBrush
;
365 m_palette
= wxNullPalette
;
366 #endif // wxUSE_PALETTE
368 m_backgroundBrush
= wxNullBrush
;
369 m_selectedBitmap
= wxNullBitmap
;
372 // ---------------------------------------------------------------------------
374 // ---------------------------------------------------------------------------
376 void wxDC::UpdateClipBox()
381 ::GetClipBox(GetHdc(), &rect
);
383 m_clipX1
= (wxCoord
) XDEV2LOG(rect
.left
);
384 m_clipY1
= (wxCoord
) YDEV2LOG(rect
.top
);
385 m_clipX2
= (wxCoord
) XDEV2LOG(rect
.right
);
386 m_clipY2
= (wxCoord
) YDEV2LOG(rect
.bottom
);
390 wxDC::DoGetClippingBox(wxCoord
*x
, wxCoord
*y
, wxCoord
*w
, wxCoord
*h
) const
392 // check if we should try to retrieve the clipping region possibly not set
393 // by our SetClippingRegion() but preset by Windows:this can only happen
394 // when we're associated with an existing HDC usign SetHDC(), see there
395 if ( m_clipping
&& !m_clipX1
&& !m_clipX2
)
397 wxDC
*self
= wxConstCast(this, wxDC
);
398 self
->UpdateClipBox();
400 if ( !m_clipX1
&& !m_clipX2
)
401 self
->m_clipping
= false;
404 wxDCBase::DoGetClippingBox(x
, y
, w
, h
);
407 // common part of DoSetClippingRegion() and DoSetClippingRegionAsRegion()
408 void wxDC::SetClippingHrgn(WXHRGN hrgn
)
410 wxCHECK_RET( hrgn
, wxT("invalid clipping region") );
414 // note that we combine the new clipping region with the existing one: this
415 // is compatible with what the other ports do and is the documented
416 // behaviour now (starting with 2.3.3)
417 #if defined(__WXWINCE__)
419 if ( !::GetClipBox(GetHdc(), &rectClip
) )
422 // GetClipBox returns logical coordinates, so transform to device
423 rectClip
.left
= LogicalToDeviceX(rectClip
.left
);
424 rectClip
.top
= LogicalToDeviceY(rectClip
.top
);
425 rectClip
.right
= LogicalToDeviceX(rectClip
.right
);
426 rectClip
.bottom
= LogicalToDeviceY(rectClip
.bottom
);
428 HRGN hrgnDest
= ::CreateRectRgn(0, 0, 0, 0);
429 HRGN hrgnClipOld
= ::CreateRectRgn(rectClip
.left
, rectClip
.top
,
430 rectClip
.right
, rectClip
.bottom
);
432 if ( ::CombineRgn(hrgnDest
, hrgnClipOld
, (HRGN
)hrgn
, RGN_AND
) != ERROR
)
434 ::SelectClipRgn(GetHdc(), hrgnDest
);
437 ::DeleteObject(hrgnClipOld
);
438 ::DeleteObject(hrgnDest
);
440 if ( ::ExtSelectClipRgn(GetHdc(), (HRGN
)hrgn
, RGN_AND
) == ERROR
)
442 wxLogLastError(_T("ExtSelectClipRgn"));
446 #endif // WinCE/!WinCE
453 void wxDC::DoSetClippingRegion(wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
)
455 // the region coords are always the device ones, so do the translation
458 // FIXME: possible +/-1 error here, to check!
459 HRGN hrgn
= ::CreateRectRgn(LogicalToDeviceX(x
),
461 LogicalToDeviceX(x
+ w
),
462 LogicalToDeviceY(y
+ h
));
465 wxLogLastError(_T("CreateRectRgn"));
469 SetClippingHrgn((WXHRGN
)hrgn
);
471 ::DeleteObject(hrgn
);
475 void wxDC::DoSetClippingRegionAsRegion(const wxRegion
& region
)
477 SetClippingHrgn(region
.GetHRGN());
480 void wxDC::DestroyClippingRegion()
484 if (m_clipping
&& m_hDC
)
487 // On a PocketPC device (not necessarily emulator), resetting
488 // the clip region as per the old method causes bad display
489 // problems. In fact setting a null region is probably OK
490 // on desktop WIN32 also, since the WIN32 docs imply that the user
491 // clipping region is independent from the paint clipping region.
492 ::SelectClipRgn(GetHdc(), 0);
494 // TODO: this should restore the previous clipping region,
495 // so that OnPaint processing works correctly, and the update
496 // clipping region doesn't get destroyed after the first
497 // DestroyClippingRegion.
498 HRGN rgn
= CreateRectRgn(0, 0, 32000, 32000);
499 ::SelectClipRgn(GetHdc(), rgn
);
504 wxDCBase::DestroyClippingRegion();
507 // ---------------------------------------------------------------------------
508 // query capabilities
509 // ---------------------------------------------------------------------------
511 bool wxDC::CanDrawBitmap() const
516 bool wxDC::CanGetTextExtent() const
518 #ifdef __WXMICROWIN__
519 // TODO Extend MicroWindows' GetDeviceCaps function
522 // What sort of display is it?
523 int technology
= ::GetDeviceCaps(GetHdc(), TECHNOLOGY
);
525 return (technology
== DT_RASDISPLAY
) || (technology
== DT_RASPRINTER
);
529 int wxDC::GetDepth() const
531 WXMICROWIN_CHECK_HDC_RET(16)
533 return (int)::GetDeviceCaps(GetHdc(), BITSPIXEL
);
536 // ---------------------------------------------------------------------------
538 // ---------------------------------------------------------------------------
547 GetClientRect((HWND
) m_canvas
->GetHWND(), &rect
);
551 // No, I think we should simply ignore this if printing on e.g.
553 // wxCHECK_RET( m_selectedBitmap.Ok(), wxT("this DC can't be cleared") );
554 if (!m_selectedBitmap
.Ok())
557 rect
.left
= 0; rect
.top
= 0;
558 rect
.right
= m_selectedBitmap
.GetWidth();
559 rect
.bottom
= m_selectedBitmap
.GetHeight();
563 (void) ::SetMapMode(GetHdc(), MM_TEXT
);
566 DWORD colour
= ::GetBkColor(GetHdc());
567 HBRUSH brush
= ::CreateSolidBrush(colour
);
568 ::FillRect(GetHdc(), &rect
, brush
);
569 ::DeleteObject(brush
);
572 int width
= DeviceToLogicalXRel(VIEWPORT_EXTENT
)*m_signX
,
573 height
= DeviceToLogicalYRel(VIEWPORT_EXTENT
)*m_signY
;
575 ::SetMapMode(GetHdc(), MM_ANISOTROPIC
);
577 ::SetViewportExtEx(GetHdc(), VIEWPORT_EXTENT
, VIEWPORT_EXTENT
, NULL
);
578 ::SetWindowExtEx(GetHdc(), width
, height
, NULL
);
579 ::SetViewportOrgEx(GetHdc(), (int)m_deviceOriginX
, (int)m_deviceOriginY
, NULL
);
580 ::SetWindowOrgEx(GetHdc(), (int)m_logicalOriginX
, (int)m_logicalOriginY
, NULL
);
584 bool wxDC::DoFloodFill(wxCoord
WXUNUSED_IN_WINCE(x
),
585 wxCoord
WXUNUSED_IN_WINCE(y
),
586 const wxColour
& WXUNUSED_IN_WINCE(col
),
587 int WXUNUSED_IN_WINCE(style
))
592 WXMICROWIN_CHECK_HDC_RET(false)
594 bool success
= (0 != ::ExtFloodFill(GetHdc(), XLOG2DEV(x
), YLOG2DEV(y
),
596 style
== wxFLOOD_SURFACE
? FLOODFILLSURFACE
597 : FLOODFILLBORDER
) ) ;
600 // quoting from the MSDN docs:
602 // Following are some of the reasons this function might fail:
604 // * The filling could not be completed.
605 // * The specified point has the boundary color specified by the
606 // crColor parameter (if FLOODFILLBORDER was requested).
607 // * The specified point does not have the color specified by
608 // crColor (if FLOODFILLSURFACE was requested)
609 // * The point is outside the clipping region that is, it is not
610 // visible on the device.
612 wxLogLastError(wxT("ExtFloodFill"));
615 CalcBoundingBox(x
, y
);
621 bool wxDC::DoGetPixel(wxCoord x
, wxCoord y
, wxColour
*col
) const
623 WXMICROWIN_CHECK_HDC_RET(false)
625 wxCHECK_MSG( col
, false, _T("NULL colour parameter in wxDC::GetPixel") );
627 // get the color of the pixel
628 COLORREF pixelcolor
= ::GetPixel(GetHdc(), XLOG2DEV(x
), YLOG2DEV(y
));
630 wxRGBToColour(*col
, pixelcolor
);
635 void wxDC::DoCrossHair(wxCoord x
, wxCoord y
)
639 wxCoord x1
= x
-VIEWPORT_EXTENT
;
640 wxCoord y1
= y
-VIEWPORT_EXTENT
;
641 wxCoord x2
= x
+VIEWPORT_EXTENT
;
642 wxCoord y2
= y
+VIEWPORT_EXTENT
;
644 wxDrawLine(GetHdc(), XLOG2DEV(x1
), YLOG2DEV(y
), XLOG2DEV(x2
), YLOG2DEV(y
));
645 wxDrawLine(GetHdc(), XLOG2DEV(x
), YLOG2DEV(y1
), XLOG2DEV(x
), YLOG2DEV(y2
));
647 CalcBoundingBox(x1
, y1
);
648 CalcBoundingBox(x2
, y2
);
651 void wxDC::DoDrawLine(wxCoord x1
, wxCoord y1
, wxCoord x2
, wxCoord y2
)
655 wxDrawLine(GetHdc(), XLOG2DEV(x1
), YLOG2DEV(y1
), XLOG2DEV(x2
), YLOG2DEV(y2
));
657 CalcBoundingBox(x1
, y1
);
658 CalcBoundingBox(x2
, y2
);
661 // Draws an arc of a circle, centred on (xc, yc), with starting point (x1, y1)
662 // and ending at (x2, y2)
663 void wxDC::DoDrawArc(wxCoord x1
, wxCoord y1
,
664 wxCoord x2
, wxCoord y2
,
665 wxCoord xc
, wxCoord yc
)
668 // Slower emulation since WinCE doesn't support Pie and Arc
669 double r
= sqrt( (x1
-xc
)*(x1
-xc
) + (y1
-yc
)*(y1
-yc
) );
670 double sa
= acos((x1
-xc
)/r
)/M_PI
*180; // between 0 and 180
671 if( y1
>yc
) sa
= -sa
; // below center
672 double ea
= atan2(yc
-y2
, x2
-xc
)/M_PI
*180;
673 DoDrawEllipticArcRot( xc
-r
, yc
-r
, 2*r
, 2*r
, sa
, ea
);
678 wxColourChanger
cc(*this); // needed for wxSTIPPLE_MASK_OPAQUE handling
682 double radius
= (double)sqrt(dx
*dx
+dy
*dy
);
683 wxCoord r
= (wxCoord
)radius
;
685 // treat the special case of full circle separately
686 if ( x1
== x2
&& y1
== y2
)
688 DrawEllipse(xc
- r
, yc
- r
, 2*r
, 2*r
);
692 wxCoord xx1
= XLOG2DEV(x1
);
693 wxCoord yy1
= YLOG2DEV(y1
);
694 wxCoord xx2
= XLOG2DEV(x2
);
695 wxCoord yy2
= YLOG2DEV(y2
);
696 wxCoord xxc
= XLOG2DEV(xc
);
697 wxCoord yyc
= YLOG2DEV(yc
);
698 wxCoord ray
= (wxCoord
) sqrt(double((xxc
-xx1
)*(xxc
-xx1
)+(yyc
-yy1
)*(yyc
-yy1
)));
700 wxCoord xxx1
= (wxCoord
) (xxc
-ray
);
701 wxCoord yyy1
= (wxCoord
) (yyc
-ray
);
702 wxCoord xxx2
= (wxCoord
) (xxc
+ray
);
703 wxCoord yyy2
= (wxCoord
) (yyc
+ray
);
705 if ( m_brush
.Ok() && m_brush
.GetStyle() != wxTRANSPARENT
)
707 // Have to add 1 to bottom-right corner of rectangle
708 // to make semi-circles look right (crooked line otherwise).
709 // Unfortunately this is not a reliable method, depends
710 // on the size of shape.
711 // TODO: figure out why this happens!
712 Pie(GetHdc(),xxx1
,yyy1
,xxx2
+1,yyy2
+1, xx1
,yy1
,xx2
,yy2
);
716 Arc(GetHdc(),xxx1
,yyy1
,xxx2
,yyy2
, xx1
,yy1
,xx2
,yy2
);
719 CalcBoundingBox(xc
- r
, yc
- r
);
720 CalcBoundingBox(xc
+ r
, yc
+ r
);
724 void wxDC::DoDrawCheckMark(wxCoord x1
, wxCoord y1
,
725 wxCoord width
, wxCoord height
)
729 wxCoord x2
= x1
+ width
,
732 #if defined(__WIN32__) && !defined(__SYMANTEC__) && !defined(__WXMICROWIN__)
740 DrawFrameControl(GetHdc(), &rect
, DFC_BUTTON
, DFCS_BUTTONCHECK
);
742 DrawFrameControl(GetHdc(), &rect
, DFC_MENU
, DFCS_MENUCHECK
);
744 #else // Symantec-MicroWin
746 HPEN blackPen
= ::CreatePen(PS_SOLID
, 1, RGB(0, 0, 0));
747 HPEN whiteBrush
= (HPEN
)::GetStockObject(WHITE_BRUSH
);
748 HPEN hPenOld
= (HPEN
)::SelectObject(GetHdc(), blackPen
);
749 HPEN hBrushOld
= (HPEN
)::SelectObject(GetHdc(), whiteBrush
);
750 ::SetROP2(GetHdc(), R2_COPYPEN
);
751 Rectangle(GetHdc(), x1
, y1
, x2
, y2
);
752 MoveToEx(GetHdc(), x1
, y1
, NULL
);
753 LineTo(GetHdc(), x2
, y2
);
754 MoveToEx(GetHdc(), x2
, y1
, NULL
);
755 LineTo(GetHdc(), x1
, y2
);
756 ::SelectObject(GetHdc(), hPenOld
);
757 ::SelectObject(GetHdc(), hBrushOld
);
758 ::DeleteObject(blackPen
);
759 #endif // Win32/Symantec-MicroWin
761 CalcBoundingBox(x1
, y1
);
762 CalcBoundingBox(x2
, y2
);
765 void wxDC::DoDrawPoint(wxCoord x
, wxCoord y
)
769 COLORREF color
= 0x00ffffff;
772 color
= m_pen
.GetColour().GetPixel();
775 SetPixel(GetHdc(), XLOG2DEV(x
), YLOG2DEV(y
), color
);
777 CalcBoundingBox(x
, y
);
780 void wxDC::DoDrawPolygon(int n
,
784 int WXUNUSED_IN_WINCE(fillStyle
))
788 wxColourChanger
cc(*this); // needed for wxSTIPPLE_MASK_OPAQUE handling
790 // Do things less efficiently if we have offsets
791 if (xoffset
!= 0 || yoffset
!= 0)
793 POINT
*cpoints
= new POINT
[n
];
795 for (i
= 0; i
< n
; i
++)
797 cpoints
[i
].x
= (int)(points
[i
].x
+ xoffset
);
798 cpoints
[i
].y
= (int)(points
[i
].y
+ yoffset
);
800 CalcBoundingBox(cpoints
[i
].x
, cpoints
[i
].y
);
803 int prev
= SetPolyFillMode(GetHdc(),fillStyle
==wxODDEVEN_RULE
?ALTERNATE
:WINDING
);
805 (void)Polygon(GetHdc(), cpoints
, n
);
807 SetPolyFillMode(GetHdc(),prev
);
814 for (i
= 0; i
< n
; i
++)
815 CalcBoundingBox(points
[i
].x
, points
[i
].y
);
818 int prev
= SetPolyFillMode(GetHdc(),fillStyle
==wxODDEVEN_RULE
?ALTERNATE
:WINDING
);
820 (void)Polygon(GetHdc(), (POINT
*) points
, n
);
822 SetPolyFillMode(GetHdc(),prev
);
828 wxDC::DoDrawPolyPolygon(int n
,
836 wxDCBase::DoDrawPolyPolygon(n
, count
, points
, xoffset
, yoffset
, fillStyle
);
840 wxColourChanger
cc(*this); // needed for wxSTIPPLE_MASK_OPAQUE handling
842 for (i
= cnt
= 0; i
< n
; i
++)
845 // Do things less efficiently if we have offsets
846 if (xoffset
!= 0 || yoffset
!= 0)
848 POINT
*cpoints
= new POINT
[cnt
];
849 for (i
= 0; i
< cnt
; i
++)
851 cpoints
[i
].x
= (int)(points
[i
].x
+ xoffset
);
852 cpoints
[i
].y
= (int)(points
[i
].y
+ yoffset
);
854 CalcBoundingBox(cpoints
[i
].x
, cpoints
[i
].y
);
857 int prev
= SetPolyFillMode(GetHdc(),fillStyle
==wxODDEVEN_RULE
?ALTERNATE
:WINDING
);
859 (void)PolyPolygon(GetHdc(), cpoints
, count
, n
);
861 SetPolyFillMode(GetHdc(),prev
);
867 for (i
= 0; i
< cnt
; i
++)
868 CalcBoundingBox(points
[i
].x
, points
[i
].y
);
871 int prev
= SetPolyFillMode(GetHdc(),fillStyle
==wxODDEVEN_RULE
?ALTERNATE
:WINDING
);
873 (void)PolyPolygon(GetHdc(), (POINT
*) points
, count
, n
);
875 SetPolyFillMode(GetHdc(),prev
);
882 void wxDC::DoDrawLines(int n
, wxPoint points
[], wxCoord xoffset
, wxCoord yoffset
)
886 // Do things less efficiently if we have offsets
887 if (xoffset
!= 0 || yoffset
!= 0)
889 POINT
*cpoints
= new POINT
[n
];
891 for (i
= 0; i
< n
; i
++)
893 cpoints
[i
].x
= (int)(points
[i
].x
+ xoffset
);
894 cpoints
[i
].y
= (int)(points
[i
].y
+ yoffset
);
896 CalcBoundingBox(cpoints
[i
].x
, cpoints
[i
].y
);
898 (void)Polyline(GetHdc(), cpoints
, n
);
904 for (i
= 0; i
< n
; i
++)
905 CalcBoundingBox(points
[i
].x
, points
[i
].y
);
907 (void)Polyline(GetHdc(), (POINT
*) points
, n
);
911 void wxDC::DoDrawRectangle(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
915 wxColourChanger
cc(*this); // needed for wxSTIPPLE_MASK_OPAQUE handling
917 wxCoord x2
= x
+ width
;
918 wxCoord y2
= y
+ height
;
920 if ((m_logicalFunction
== wxCOPY
) && (m_pen
.GetStyle() == wxTRANSPARENT
))
923 rect
.left
= XLOG2DEV(x
);
924 rect
.top
= YLOG2DEV(y
);
925 rect
.right
= XLOG2DEV(x2
);
926 rect
.bottom
= YLOG2DEV(y2
);
927 (void)FillRect(GetHdc(), &rect
, (HBRUSH
)m_brush
.GetResourceHandle() );
931 // Windows draws the filled rectangles without outline (i.e. drawn with a
932 // transparent pen) one pixel smaller in both directions and we want them
933 // to have the same size regardless of which pen is used - adjust
935 // I wonder if this shouldn´t be done after the LOG2DEV() conversions. RR.
936 if ( m_pen
.GetStyle() == wxTRANSPARENT
)
938 // Apparently not needed for WinCE (see e.g. Life! demo)
945 (void)Rectangle(GetHdc(), XLOG2DEV(x
), YLOG2DEV(y
), XLOG2DEV(x2
), YLOG2DEV(y2
));
949 CalcBoundingBox(x
, y
);
950 CalcBoundingBox(x2
, y2
);
953 void wxDC::DoDrawRoundedRectangle(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
, double radius
)
957 wxColourChanger
cc(*this); // needed for wxSTIPPLE_MASK_OPAQUE handling
959 // Now, a negative radius value is interpreted to mean
960 // 'the proportion of the smallest X or Y dimension'
964 double smallest
= (width
< height
) ? width
: height
;
965 radius
= (- radius
* smallest
);
968 wxCoord x2
= (x
+width
);
969 wxCoord y2
= (y
+height
);
971 // Windows draws the filled rectangles without outline (i.e. drawn with a
972 // transparent pen) one pixel smaller in both directions and we want them
973 // to have the same size regardless of which pen is used - adjust
974 if ( m_pen
.GetStyle() == wxTRANSPARENT
)
980 (void)RoundRect(GetHdc(), XLOG2DEV(x
), YLOG2DEV(y
), XLOG2DEV(x2
),
981 YLOG2DEV(y2
), (int) (2*XLOG2DEV(radius
)), (int)( 2*YLOG2DEV(radius
)));
983 CalcBoundingBox(x
, y
);
984 CalcBoundingBox(x2
, y2
);
987 void wxDC::DoDrawEllipse(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
991 wxColourChanger
cc(*this); // needed for wxSTIPPLE_MASK_OPAQUE handling
993 wxCoord x2
= (x
+width
);
994 wxCoord y2
= (y
+height
);
996 (void)Ellipse(GetHdc(), XLOG2DEV(x
), YLOG2DEV(y
), XLOG2DEV(x2
), YLOG2DEV(y2
));
998 CalcBoundingBox(x
, y
);
999 CalcBoundingBox(x2
, y2
);
1003 void wxDC::DoDrawSpline(wxList
*points
)
1006 // WinCE does not support ::PolyBezier so use generic version
1007 wxDCBase::DoDrawSpline(points
);
1009 // quadratic b-spline to cubic bezier spline conversion
1011 // quadratic spline with control points P0,P1,P2
1012 // P(s) = P0*(1-s)^2 + P1*2*(1-s)*s + P2*s^2
1014 // bezier spline with control points B0,B1,B2,B3
1015 // B(s) = B0*(1-s)^3 + B1*3*(1-s)^2*s + B2*3*(1-s)*s^2 + B3*s^3
1017 // control points of bezier spline calculated from b-spline
1019 // B1 = (2*P1 + P0)/3
1020 // B2 = (2*P1 + P2)/3
1023 WXMICROWIN_CHECK_HDC
1025 wxASSERT_MSG( points
, wxT("NULL pointer to spline points?") );
1027 const size_t n_points
= points
->GetCount();
1028 wxASSERT_MSG( n_points
> 2 , wxT("incomplete list of spline points?") );
1030 const size_t n_bezier_points
= n_points
* 3 + 1;
1031 POINT
*lppt
= (POINT
*)malloc(n_bezier_points
*sizeof(POINT
));
1032 size_t bezier_pos
= 0;
1033 wxCoord x1
, y1
, x2
, y2
, cx1
, cy1
, cx4
, cy4
;
1035 wxList::compatibility_iterator node
= points
->GetFirst();
1036 wxPoint
*p
= (wxPoint
*)node
->GetData();
1037 lppt
[ bezier_pos
].x
= x1
= p
->x
;
1038 lppt
[ bezier_pos
].y
= y1
= p
->y
;
1040 lppt
[ bezier_pos
] = lppt
[ bezier_pos
-1 ];
1043 node
= node
->GetNext();
1044 p
= (wxPoint
*)node
->GetData();
1048 cx1
= ( x1
+ x2
) / 2;
1049 cy1
= ( y1
+ y2
) / 2;
1050 lppt
[ bezier_pos
].x
= XLOG2DEV(cx1
);
1051 lppt
[ bezier_pos
].y
= YLOG2DEV(cy1
);
1053 lppt
[ bezier_pos
] = lppt
[ bezier_pos
-1 ];
1057 while ((node
= node
->GetNext()) != NULL
)
1059 while ((node
= node
->GetNext()))
1060 #endif // !wxUSE_STL
1062 p
= (wxPoint
*)node
->GetData();
1067 cx4
= (x1
+ x2
) / 2;
1068 cy4
= (y1
+ y2
) / 2;
1069 // B0 is B3 of previous segment
1071 lppt
[ bezier_pos
].x
= XLOG2DEV((x1
*2+cx1
)/3);
1072 lppt
[ bezier_pos
].y
= YLOG2DEV((y1
*2+cy1
)/3);
1075 lppt
[ bezier_pos
].x
= XLOG2DEV((x1
*2+cx4
)/3);
1076 lppt
[ bezier_pos
].y
= YLOG2DEV((y1
*2+cy4
)/3);
1079 lppt
[ bezier_pos
].x
= XLOG2DEV(cx4
);
1080 lppt
[ bezier_pos
].y
= YLOG2DEV(cy4
);
1086 lppt
[ bezier_pos
] = lppt
[ bezier_pos
-1 ];
1088 lppt
[ bezier_pos
].x
= XLOG2DEV(x2
);
1089 lppt
[ bezier_pos
].y
= YLOG2DEV(y2
);
1091 lppt
[ bezier_pos
] = lppt
[ bezier_pos
-1 ];
1094 ::PolyBezier( GetHdc(), lppt
, bezier_pos
);
1101 // Chris Breeze 20/5/98: first implementation of DrawEllipticArc on Windows
1102 void wxDC::DoDrawEllipticArc(wxCoord x
,wxCoord y
,wxCoord w
,wxCoord h
,double sa
,double ea
)
1105 DoDrawEllipticArcRot( x
, y
, w
, h
, sa
, ea
);
1108 WXMICROWIN_CHECK_HDC
1110 wxColourChanger
cc(*this); // needed for wxSTIPPLE_MASK_OPAQUE handling
1115 int rx1
= XLOG2DEV(x
+w
/2);
1116 int ry1
= YLOG2DEV(y
+h
/2);
1123 rx1
+= (int)(100.0 * abs(w
) * cos(sa
));
1124 ry1
-= (int)(100.0 * abs(h
) * m_signY
* sin(sa
));
1125 rx2
+= (int)(100.0 * abs(w
) * cos(ea
));
1126 ry2
-= (int)(100.0 * abs(h
) * m_signY
* sin(ea
));
1128 // draw pie with NULL_PEN first and then outline otherwise a line is
1129 // drawn from the start and end points to the centre
1130 HPEN hpenOld
= (HPEN
) ::SelectObject(GetHdc(), (HPEN
) ::GetStockObject(NULL_PEN
));
1133 (void)Pie(GetHdc(), XLOG2DEV(x
), YLOG2DEV(y
), XLOG2DEV(x2
)+1, YLOG2DEV(y2
)+1,
1134 rx1
, ry1
, rx2
, ry2
);
1138 (void)Pie(GetHdc(), XLOG2DEV(x
), YLOG2DEV(y
)-1, XLOG2DEV(x2
)+1, YLOG2DEV(y2
),
1139 rx1
, ry1
-1, rx2
, ry2
-1);
1142 ::SelectObject(GetHdc(), hpenOld
);
1144 (void)Arc(GetHdc(), XLOG2DEV(x
), YLOG2DEV(y
), XLOG2DEV(x2
), YLOG2DEV(y2
),
1145 rx1
, ry1
, rx2
, ry2
);
1147 CalcBoundingBox(x
, y
);
1148 CalcBoundingBox(x2
, y2
);
1152 void wxDC::DoDrawIcon(const wxIcon
& icon
, wxCoord x
, wxCoord y
)
1154 WXMICROWIN_CHECK_HDC
1156 wxCHECK_RET( icon
.Ok(), wxT("invalid icon in DrawIcon") );
1159 ::DrawIconEx(GetHdc(), XLOG2DEV(x
), YLOG2DEV(y
), GetHiconOf(icon
), icon
.GetWidth(), icon
.GetHeight(), 0, NULL
, DI_NORMAL
);
1161 ::DrawIcon(GetHdc(), XLOG2DEV(x
), YLOG2DEV(y
), GetHiconOf(icon
));
1164 CalcBoundingBox(x
, y
);
1165 CalcBoundingBox(x
+ icon
.GetWidth(), y
+ icon
.GetHeight());
1168 void wxDC::DoDrawBitmap( const wxBitmap
&bmp
, wxCoord x
, wxCoord y
, bool useMask
)
1170 WXMICROWIN_CHECK_HDC
1172 wxCHECK_RET( bmp
.Ok(), _T("invalid bitmap in wxDC::DrawBitmap") );
1174 int width
= bmp
.GetWidth(),
1175 height
= bmp
.GetHeight();
1177 HBITMAP hbmpMask
= 0;
1180 HPALETTE oldPal
= 0;
1181 #endif // wxUSE_PALETTE
1183 if ( bmp
.HasAlpha() )
1186 SelectInHDC
select(hdcMem
, GetHbitmapOf(bmp
));
1188 if ( AlphaBlt(GetHdc(), x
, y
, width
, height
, 0, 0, hdcMem
, bmp
) )
1194 wxMask
*mask
= bmp
.GetMask();
1196 hbmpMask
= (HBITMAP
)mask
->GetMaskBitmap();
1200 // don't give assert here because this would break existing
1201 // programs - just silently ignore useMask parameter
1208 // use MaskBlt() with ROP which doesn't do anything to dst in the mask
1210 // On some systems, MaskBlt succeeds yet is much much slower
1211 // than the wxWidgets fall-back implementation. So we need
1212 // to be able to switch this on and off at runtime.
1214 #if wxUSE_SYSTEM_OPTIONS
1215 if (wxSystemOptions::GetOptionInt(wxT("no-maskblt")) == 0)
1219 HDC hdcMem
= ::CreateCompatibleDC(GetHdc());
1220 HGDIOBJ hOldBitmap
= ::SelectObject(hdcMem
, GetHbitmapOf(bmp
));
1222 wxPalette
*pal
= bmp
.GetPalette();
1223 if ( pal
&& ::GetDeviceCaps(cdc
,BITSPIXEL
) <= 8 )
1225 oldPal
= ::SelectPalette(hdcMem
, GetHpaletteOf(*pal
), FALSE
);
1226 ::RealizePalette(hdcMem
);
1228 #endif // wxUSE_PALETTE
1230 ok
= ::MaskBlt(cdc
, x
, y
, width
, height
,
1233 MAKEROP4(SRCCOPY
, DSTCOPY
)) != 0;
1237 ::SelectPalette(hdcMem
, oldPal
, FALSE
);
1238 #endif // wxUSE_PALETTE
1240 ::SelectObject(hdcMem
, hOldBitmap
);
1247 // Rather than reproduce wxDC::Blit, let's do it at the wxWin API
1250 memDC
.SelectObject(bmp
);
1252 Blit(x
, y
, width
, height
, &memDC
, 0, 0, wxCOPY
, useMask
);
1254 memDC
.SelectObject(wxNullBitmap
);
1257 else // no mask, just use BitBlt()
1260 HDC memdc
= ::CreateCompatibleDC( cdc
);
1261 HBITMAP hbitmap
= (HBITMAP
) bmp
.GetHBITMAP( );
1263 wxASSERT_MSG( hbitmap
, wxT("bitmap is ok but HBITMAP is NULL?") );
1265 COLORREF old_textground
= ::GetTextColor(GetHdc());
1266 COLORREF old_background
= ::GetBkColor(GetHdc());
1267 if (m_textForegroundColour
.Ok())
1269 ::SetTextColor(GetHdc(), m_textForegroundColour
.GetPixel() );
1271 if (m_textBackgroundColour
.Ok())
1273 ::SetBkColor(GetHdc(), m_textBackgroundColour
.GetPixel() );
1277 wxPalette
*pal
= bmp
.GetPalette();
1278 if ( pal
&& ::GetDeviceCaps(cdc
,BITSPIXEL
) <= 8 )
1280 oldPal
= ::SelectPalette(memdc
, GetHpaletteOf(*pal
), FALSE
);
1281 ::RealizePalette(memdc
);
1283 #endif // wxUSE_PALETTE
1285 HGDIOBJ hOldBitmap
= ::SelectObject( memdc
, hbitmap
);
1286 ::BitBlt( cdc
, x
, y
, width
, height
, memdc
, 0, 0, SRCCOPY
);
1290 ::SelectPalette(memdc
, oldPal
, FALSE
);
1291 #endif // wxUSE_PALETTE
1293 ::SelectObject( memdc
, hOldBitmap
);
1294 ::DeleteDC( memdc
);
1296 ::SetTextColor(GetHdc(), old_textground
);
1297 ::SetBkColor(GetHdc(), old_background
);
1301 void wxDC::DoDrawText(const wxString
& text
, wxCoord x
, wxCoord y
)
1303 WXMICROWIN_CHECK_HDC
1305 DrawAnyText(text
, x
, y
);
1307 // update the bounding box
1308 CalcBoundingBox(x
, y
);
1311 GetTextExtent(text
, &w
, &h
);
1312 CalcBoundingBox(x
+ w
, y
+ h
);
1315 void wxDC::DrawAnyText(const wxString
& text
, wxCoord x
, wxCoord y
)
1317 WXMICROWIN_CHECK_HDC
1319 // prepare for drawing the text
1320 if ( m_textForegroundColour
.Ok() )
1321 SetTextColor(GetHdc(), m_textForegroundColour
.GetPixel());
1323 DWORD old_background
= 0;
1324 if ( m_textBackgroundColour
.Ok() )
1326 old_background
= SetBkColor(GetHdc(), m_textBackgroundColour
.GetPixel() );
1329 SetBkMode(GetHdc(), m_backgroundMode
== wxTRANSPARENT
? TRANSPARENT
1333 if ( ::ExtTextOut(GetHdc(), XLOG2DEV(x
), YLOG2DEV(y
), 0, NULL
,
1334 text
.c_str(), text
.length(), NULL
) == 0 )
1336 wxLogLastError(wxT("TextOut"));
1339 if ( ::TextOut(GetHdc(), XLOG2DEV(x
), YLOG2DEV(y
),
1340 text
.c_str(), text
.length()) == 0 )
1342 wxLogLastError(wxT("TextOut"));
1346 // restore the old parameters (text foreground colour may be left because
1347 // it never is set to anything else, but background should remain
1348 // transparent even if we just drew an opaque string)
1349 if ( m_textBackgroundColour
.Ok() )
1350 (void)SetBkColor(GetHdc(), old_background
);
1352 SetBkMode(GetHdc(), TRANSPARENT
);
1355 void wxDC::DoDrawRotatedText(const wxString
& text
,
1356 wxCoord x
, wxCoord y
,
1359 WXMICROWIN_CHECK_HDC
1361 // we test that we have some font because otherwise we should still use the
1362 // "else" part below to avoid that DrawRotatedText(angle = 180) and
1363 // DrawRotatedText(angle = 0) use different fonts (we can't use the default
1364 // font for drawing rotated fonts unfortunately)
1365 if ( (angle
== 0.0) && m_font
.Ok() )
1367 DoDrawText(text
, x
, y
);
1369 #ifndef __WXMICROWIN__
1372 // NB: don't take DEFAULT_GUI_FONT (a.k.a. wxSYS_DEFAULT_GUI_FONT)
1373 // because it's not TrueType and so can't have non zero
1374 // orientation/escapement under Win9x
1375 wxFont font
= m_font
.Ok() ? m_font
: *wxSWISS_FONT
;
1376 HFONT hfont
= (HFONT
)font
.GetResourceHandle();
1378 if ( ::GetObject(hfont
, sizeof(lf
), &lf
) == 0 )
1380 wxLogLastError(wxT("GetObject(hfont)"));
1383 // GDI wants the angle in tenth of degree
1384 long angle10
= (long)(angle
* 10);
1385 lf
.lfEscapement
= angle10
;
1386 lf
. lfOrientation
= angle10
;
1388 hfont
= ::CreateFontIndirect(&lf
);
1391 wxLogLastError(wxT("CreateFont"));
1395 HFONT hfontOld
= (HFONT
)::SelectObject(GetHdc(), hfont
);
1397 DrawAnyText(text
, x
, y
);
1399 (void)::SelectObject(GetHdc(), hfontOld
);
1400 (void)::DeleteObject(hfont
);
1403 // call the bounding box by adding all four vertices of the rectangle
1404 // containing the text to it (simpler and probably not slower than
1405 // determining which of them is really topmost/leftmost/...)
1407 GetTextExtent(text
, &w
, &h
);
1409 double rad
= DegToRad(angle
);
1411 // "upper left" and "upper right"
1412 CalcBoundingBox(x
, y
);
1413 CalcBoundingBox(x
+ wxCoord(w
*cos(rad
)), y
- wxCoord(w
*sin(rad
)));
1415 // "bottom left" and "bottom right"
1416 x
+= (wxCoord
)(h
*sin(rad
));
1417 y
+= (wxCoord
)(h
*cos(rad
));
1418 CalcBoundingBox(x
, y
);
1419 CalcBoundingBox(x
+ wxCoord(w
*cos(rad
)), y
- wxCoord(w
*sin(rad
)));
1424 // ---------------------------------------------------------------------------
1426 // ---------------------------------------------------------------------------
1430 void wxDC::DoSelectPalette(bool realize
)
1432 WXMICROWIN_CHECK_HDC
1434 // Set the old object temporarily, in case the assignment deletes an object
1435 // that's not yet selected out.
1438 ::SelectPalette(GetHdc(), (HPALETTE
) m_oldPalette
, FALSE
);
1442 if ( m_palette
.Ok() )
1444 HPALETTE oldPal
= ::SelectPalette(GetHdc(),
1445 GetHpaletteOf(m_palette
),
1448 m_oldPalette
= (WXHPALETTE
) oldPal
;
1451 ::RealizePalette(GetHdc());
1455 void wxDC::SetPalette(const wxPalette
& palette
)
1459 m_palette
= palette
;
1460 DoSelectPalette(true);
1464 void wxDC::InitializePalette()
1466 if ( wxDisplayDepth() <= 8 )
1468 // look for any window or parent that has a custom palette. If any has
1469 // one then we need to use it in drawing operations
1470 wxWindow
*win
= m_canvas
->GetAncestorWithCustomPalette();
1472 m_hasCustomPalette
= win
&& win
->HasCustomPalette();
1473 if ( m_hasCustomPalette
)
1475 m_palette
= win
->GetPalette();
1477 // turn on MSW translation for this palette
1483 #endif // wxUSE_PALETTE
1485 // SetFont/Pen/Brush() really ask to be implemented as a single template
1486 // function... but doing it is not worth breaking OpenWatcom build <sigh>
1488 void wxDC::SetFont(const wxFont
& font
)
1490 WXMICROWIN_CHECK_HDC
1492 if ( font
== m_font
)
1497 HGDIOBJ hfont
= ::SelectObject(GetHdc(), GetHfontOf(font
));
1498 if ( hfont
== HGDI_ERROR
)
1500 wxLogLastError(_T("SelectObject(font)"));
1505 m_oldFont
= (WXHPEN
)hfont
;
1510 else // invalid font, reset the current font
1514 if ( ::SelectObject(GetHdc(), (HPEN
) m_oldFont
) == HGDI_ERROR
)
1516 wxLogLastError(_T("SelectObject(old font)"));
1522 m_font
= wxNullFont
;
1526 void wxDC::SetPen(const wxPen
& pen
)
1528 WXMICROWIN_CHECK_HDC
1535 HGDIOBJ hpen
= ::SelectObject(GetHdc(), GetHpenOf(pen
));
1536 if ( hpen
== HGDI_ERROR
)
1538 wxLogLastError(_T("SelectObject(pen)"));
1543 m_oldPen
= (WXHPEN
)hpen
;
1548 else // invalid pen, reset the current pen
1552 if ( ::SelectObject(GetHdc(), (HPEN
) m_oldPen
) == HGDI_ERROR
)
1554 wxLogLastError(_T("SelectObject(old pen)"));
1564 void wxDC::SetBrush(const wxBrush
& brush
)
1566 WXMICROWIN_CHECK_HDC
1568 if ( brush
== m_brush
)
1573 // we must make sure the brush is aligned with the logical coordinates
1574 // before selecting it
1575 wxBitmap
*stipple
= brush
.GetStipple();
1576 if ( stipple
&& stipple
->Ok() )
1578 if ( !::SetBrushOrgEx
1581 m_deviceOriginX
% stipple
->GetWidth(),
1582 m_deviceOriginY
% stipple
->GetHeight(),
1583 NULL
// [out] previous brush origin
1586 wxLogLastError(_T("SetBrushOrgEx()"));
1590 HGDIOBJ hbrush
= ::SelectObject(GetHdc(), GetHbrushOf(brush
));
1591 if ( hbrush
== HGDI_ERROR
)
1593 wxLogLastError(_T("SelectObject(brush)"));
1598 m_oldBrush
= (WXHPEN
)hbrush
;
1603 else // invalid brush, reset the current brush
1607 if ( ::SelectObject(GetHdc(), (HPEN
) m_oldBrush
) == HGDI_ERROR
)
1609 wxLogLastError(_T("SelectObject(old brush)"));
1615 m_brush
= wxNullBrush
;
1619 void wxDC::SetBackground(const wxBrush
& brush
)
1621 WXMICROWIN_CHECK_HDC
1623 m_backgroundBrush
= brush
;
1625 if ( m_backgroundBrush
.Ok() )
1627 (void)SetBkColor(GetHdc(), m_backgroundBrush
.GetColour().GetPixel());
1631 void wxDC::SetBackgroundMode(int mode
)
1633 WXMICROWIN_CHECK_HDC
1635 m_backgroundMode
= mode
;
1637 // SetBackgroundColour now only refers to text background
1638 // and m_backgroundMode is used there
1641 void wxDC::SetLogicalFunction(int function
)
1643 WXMICROWIN_CHECK_HDC
1645 m_logicalFunction
= function
;
1650 void wxDC::SetRop(WXHDC dc
)
1652 if ( !dc
|| m_logicalFunction
< 0 )
1657 switch (m_logicalFunction
)
1659 case wxCLEAR
: rop
= R2_BLACK
; break;
1660 case wxXOR
: rop
= R2_XORPEN
; break;
1661 case wxINVERT
: rop
= R2_NOT
; break;
1662 case wxOR_REVERSE
: rop
= R2_MERGEPENNOT
; break;
1663 case wxAND_REVERSE
: rop
= R2_MASKPENNOT
; break;
1664 case wxCOPY
: rop
= R2_COPYPEN
; break;
1665 case wxAND
: rop
= R2_MASKPEN
; break;
1666 case wxAND_INVERT
: rop
= R2_MASKNOTPEN
; break;
1667 case wxNO_OP
: rop
= R2_NOP
; break;
1668 case wxNOR
: rop
= R2_NOTMERGEPEN
; break;
1669 case wxEQUIV
: rop
= R2_NOTXORPEN
; break;
1670 case wxSRC_INVERT
: rop
= R2_NOTCOPYPEN
; break;
1671 case wxOR_INVERT
: rop
= R2_MERGENOTPEN
; break;
1672 case wxNAND
: rop
= R2_NOTMASKPEN
; break;
1673 case wxOR
: rop
= R2_MERGEPEN
; break;
1674 case wxSET
: rop
= R2_WHITE
; break;
1677 wxFAIL_MSG( wxT("unsupported logical function") );
1681 SetROP2(GetHdc(), rop
);
1684 bool wxDC::StartDoc(const wxString
& WXUNUSED(message
))
1686 // We might be previewing, so return true to let it continue.
1694 void wxDC::StartPage()
1698 void wxDC::EndPage()
1702 // ---------------------------------------------------------------------------
1704 // ---------------------------------------------------------------------------
1706 wxCoord
wxDC::GetCharHeight() const
1708 WXMICROWIN_CHECK_HDC_RET(0)
1710 TEXTMETRIC lpTextMetric
;
1712 GetTextMetrics(GetHdc(), &lpTextMetric
);
1714 return lpTextMetric
.tmHeight
;
1717 wxCoord
wxDC::GetCharWidth() const
1719 WXMICROWIN_CHECK_HDC_RET(0)
1721 TEXTMETRIC lpTextMetric
;
1723 GetTextMetrics(GetHdc(), &lpTextMetric
);
1725 return lpTextMetric
.tmAveCharWidth
;
1728 void wxDC::DoGetTextExtent(const wxString
& string
, wxCoord
*x
, wxCoord
*y
,
1729 wxCoord
*descent
, wxCoord
*externalLeading
,
1732 #ifdef __WXMICROWIN__
1737 if (descent
) *descent
= 0;
1738 if (externalLeading
) *externalLeading
= 0;
1741 #endif // __WXMICROWIN__
1746 wxASSERT_MSG( font
->Ok(), _T("invalid font in wxDC::GetTextExtent") );
1748 hfontOld
= (HFONT
)::SelectObject(GetHdc(), GetHfontOf(*font
));
1750 else // don't change the font
1758 ::GetTextExtentPoint32(GetHdc(), string
, string
.length(), &sizeRect
);
1759 GetTextMetrics(GetHdc(), &tm
);
1766 *descent
= tm
.tmDescent
;
1767 if (externalLeading
)
1768 *externalLeading
= tm
.tmExternalLeading
;
1772 ::SelectObject(GetHdc(), hfontOld
);
1777 // Each element of the array will be the width of the string up to and
1778 // including the coresoponding character in text.
1780 bool wxDC::DoGetPartialTextExtents(const wxString
& text
, wxArrayInt
& widths
) const
1782 static int maxLenText
= -1;
1783 static int maxWidth
= -1;
1786 int stlen
= text
.length();
1788 if (maxLenText
== -1)
1790 // Win9x and WinNT+ have different limits
1791 int version
= wxGetOsVersion();
1792 maxLenText
= version
== wxWINDOWS_NT
? 65535 : 8192;
1793 maxWidth
= version
== wxWINDOWS_NT
? INT_MAX
: 32767;
1797 widths
.Add(0, stlen
); // fill the array with zeros
1801 if (!::GetTextExtentExPoint(GetHdc(),
1802 text
.c_str(), // string to check
1803 wxMin(stlen
, maxLenText
),
1805 &fit
, // [out] count of chars
1807 &widths
[0], // array to fill
1811 wxLogLastError(wxT("GetTextExtentExPoint"));
1821 void wxDC::SetMapMode(int mode
)
1823 WXMICROWIN_CHECK_HDC
1825 m_mappingMode
= mode
;
1827 if ( mode
== wxMM_TEXT
)
1830 m_logicalScaleY
= 1.0;
1832 else // need to do some calculations
1834 int pixel_width
= ::GetDeviceCaps(GetHdc(), HORZRES
),
1835 pixel_height
= ::GetDeviceCaps(GetHdc(), VERTRES
),
1836 mm_width
= ::GetDeviceCaps(GetHdc(), HORZSIZE
),
1837 mm_height
= ::GetDeviceCaps(GetHdc(), VERTSIZE
);
1839 if ( (mm_width
== 0) || (mm_height
== 0) )
1841 // we can't calculate mm2pixels[XY] then!
1845 double mm2pixelsX
= (double)pixel_width
/ mm_width
,
1846 mm2pixelsY
= (double)pixel_height
/ mm_height
;
1851 m_logicalScaleX
= twips2mm
* mm2pixelsX
;
1852 m_logicalScaleY
= twips2mm
* mm2pixelsY
;
1856 m_logicalScaleX
= pt2mm
* mm2pixelsX
;
1857 m_logicalScaleY
= pt2mm
* mm2pixelsY
;
1861 m_logicalScaleX
= mm2pixelsX
;
1862 m_logicalScaleY
= mm2pixelsY
;
1866 m_logicalScaleX
= mm2pixelsX
/ 10.0;
1867 m_logicalScaleY
= mm2pixelsY
/ 10.0;
1871 wxFAIL_MSG( _T("unknown mapping mode in SetMapMode") );
1875 // VZ: it seems very wasteful to always use MM_ANISOTROPIC when in 99% of
1876 // cases we could do with MM_TEXT and in the remaining 0.9% with
1877 // MM_ISOTROPIC (TODO!)
1879 ::SetMapMode(GetHdc(), MM_ANISOTROPIC
);
1881 int width
= DeviceToLogicalXRel(VIEWPORT_EXTENT
)*m_signX
,
1882 height
= DeviceToLogicalYRel(VIEWPORT_EXTENT
)*m_signY
;
1884 ::SetViewportExtEx(GetHdc(), VIEWPORT_EXTENT
, VIEWPORT_EXTENT
, NULL
);
1885 ::SetWindowExtEx(GetHdc(), width
, height
, NULL
);
1887 ::SetViewportOrgEx(GetHdc(), m_deviceOriginX
, m_deviceOriginY
, NULL
);
1888 ::SetWindowOrgEx(GetHdc(), m_logicalOriginX
, m_logicalOriginY
, NULL
);
1892 void wxDC::SetUserScale(double x
, double y
)
1894 WXMICROWIN_CHECK_HDC
1896 if ( x
== m_userScaleX
&& y
== m_userScaleY
)
1902 this->SetMapMode(m_mappingMode
);
1905 void wxDC::SetAxisOrientation(bool WXUNUSED_IN_WINCE(xLeftRight
),
1906 bool WXUNUSED_IN_WINCE(yBottomUp
))
1908 WXMICROWIN_CHECK_HDC
1911 int signX
= xLeftRight
? 1 : -1,
1912 signY
= yBottomUp
? -1 : 1;
1914 if ( signX
!= m_signX
|| signY
!= m_signY
)
1919 SetMapMode(m_mappingMode
);
1924 void wxDC::SetSystemScale(double x
, double y
)
1926 WXMICROWIN_CHECK_HDC
1928 if ( x
== m_scaleX
&& y
== m_scaleY
)
1935 SetMapMode(m_mappingMode
);
1939 void wxDC::SetLogicalOrigin(wxCoord x
, wxCoord y
)
1941 WXMICROWIN_CHECK_HDC
1943 if ( x
== m_logicalOriginX
&& y
== m_logicalOriginY
)
1946 m_logicalOriginX
= x
;
1947 m_logicalOriginY
= y
;
1950 ::SetWindowOrgEx(GetHdc(), (int)m_logicalOriginX
, (int)m_logicalOriginY
, NULL
);
1954 void wxDC::SetDeviceOrigin(wxCoord x
, wxCoord y
)
1956 WXMICROWIN_CHECK_HDC
1958 if ( x
== m_deviceOriginX
&& y
== m_deviceOriginY
)
1961 m_deviceOriginX
= x
;
1962 m_deviceOriginY
= y
;
1964 ::SetViewportOrgEx(GetHdc(), (int)m_deviceOriginX
, (int)m_deviceOriginY
, NULL
);
1967 // ---------------------------------------------------------------------------
1968 // coordinates transformations
1969 // ---------------------------------------------------------------------------
1971 wxCoord
wxDCBase::DeviceToLogicalX(wxCoord x
) const
1973 return DeviceToLogicalXRel(x
- m_deviceOriginX
)*m_signX
+ m_logicalOriginX
;
1976 wxCoord
wxDCBase::DeviceToLogicalXRel(wxCoord x
) const
1978 // axis orientation is not taken into account for conversion of a distance
1979 return (wxCoord
)(x
/ (m_logicalScaleX
*m_userScaleX
*m_scaleX
));
1982 wxCoord
wxDCBase::DeviceToLogicalY(wxCoord y
) const
1984 return DeviceToLogicalYRel(y
- m_deviceOriginY
)*m_signY
+ m_logicalOriginY
;
1987 wxCoord
wxDCBase::DeviceToLogicalYRel(wxCoord y
) const
1989 // axis orientation is not taken into account for conversion of a distance
1990 return (wxCoord
)( y
/ (m_logicalScaleY
*m_userScaleY
*m_scaleY
));
1993 wxCoord
wxDCBase::LogicalToDeviceX(wxCoord x
) const
1995 return LogicalToDeviceXRel(x
- m_logicalOriginX
)*m_signX
+ m_deviceOriginX
;
1998 wxCoord
wxDCBase::LogicalToDeviceXRel(wxCoord x
) const
2000 // axis orientation is not taken into account for conversion of a distance
2001 return (wxCoord
) (x
*m_logicalScaleX
*m_userScaleX
*m_scaleX
);
2004 wxCoord
wxDCBase::LogicalToDeviceY(wxCoord y
) const
2006 return LogicalToDeviceYRel(y
- m_logicalOriginY
)*m_signY
+ m_deviceOriginY
;
2009 wxCoord
wxDCBase::LogicalToDeviceYRel(wxCoord y
) const
2011 // axis orientation is not taken into account for conversion of a distance
2012 return (wxCoord
) (y
*m_logicalScaleY
*m_userScaleY
*m_scaleY
);
2015 // ---------------------------------------------------------------------------
2017 // ---------------------------------------------------------------------------
2019 bool wxDC::DoBlit(wxCoord xdest
, wxCoord ydest
,
2020 wxCoord width
, wxCoord height
,
2021 wxDC
*source
, wxCoord xsrc
, wxCoord ysrc
,
2022 int rop
, bool useMask
,
2023 wxCoord xsrcMask
, wxCoord ysrcMask
)
2025 wxCHECK_MSG( source
, false, _T("wxDC::Blit(): NULL wxDC pointer") );
2027 WXMICROWIN_CHECK_HDC_RET(false)
2029 // if either the source or destination has alpha channel, we must use
2030 // AlphaBlt() as other function don't handle it correctly
2031 const wxBitmap
& bmpSrc
= source
->m_selectedBitmap
;
2032 if ( bmpSrc
.Ok() && (bmpSrc
.HasAlpha() ||
2033 (m_selectedBitmap
.Ok() && m_selectedBitmap
.HasAlpha())) )
2035 if ( AlphaBlt(GetHdc(), xdest
, ydest
, width
, height
,
2036 xsrc
, ysrc
, GetHdcOf(*source
), bmpSrc
) )
2040 wxMask
*mask
= NULL
;
2043 mask
= bmpSrc
.GetMask();
2045 if ( !(bmpSrc
.Ok() && mask
&& mask
->GetMaskBitmap()) )
2047 // don't give assert here because this would break existing
2048 // programs - just silently ignore useMask parameter
2053 if (xsrcMask
== -1 && ysrcMask
== -1)
2055 xsrcMask
= xsrc
; ysrcMask
= ysrc
;
2058 COLORREF old_textground
= ::GetTextColor(GetHdc());
2059 COLORREF old_background
= ::GetBkColor(GetHdc());
2060 if (m_textForegroundColour
.Ok())
2062 ::SetTextColor(GetHdc(), m_textForegroundColour
.GetPixel() );
2064 if (m_textBackgroundColour
.Ok())
2066 ::SetBkColor(GetHdc(), m_textBackgroundColour
.GetPixel() );
2072 case wxXOR
: dwRop
= SRCINVERT
; break;
2073 case wxINVERT
: dwRop
= DSTINVERT
; break;
2074 case wxOR_REVERSE
: dwRop
= 0x00DD0228; break;
2075 case wxAND_REVERSE
: dwRop
= SRCERASE
; break;
2076 case wxCLEAR
: dwRop
= BLACKNESS
; break;
2077 case wxSET
: dwRop
= WHITENESS
; break;
2078 case wxOR_INVERT
: dwRop
= MERGEPAINT
; break;
2079 case wxAND
: dwRop
= SRCAND
; break;
2080 case wxOR
: dwRop
= SRCPAINT
; break;
2081 case wxEQUIV
: dwRop
= 0x00990066; break;
2082 case wxNAND
: dwRop
= 0x007700E6; break;
2083 case wxAND_INVERT
: dwRop
= 0x00220326; break;
2084 case wxCOPY
: dwRop
= SRCCOPY
; break;
2085 case wxNO_OP
: dwRop
= DSTCOPY
; break;
2086 case wxSRC_INVERT
: dwRop
= NOTSRCCOPY
; break;
2087 case wxNOR
: dwRop
= NOTSRCCOPY
; break;
2089 wxFAIL_MSG( wxT("unsupported logical function") );
2093 bool success
= false;
2098 // we want the part of the image corresponding to the mask to be
2099 // transparent, so use "DSTCOPY" ROP for the mask points (the usual
2100 // meaning of fg and bg is inverted which corresponds to wxWin notion
2101 // of the mask which is also contrary to the Windows one)
2103 // On some systems, MaskBlt succeeds yet is much much slower
2104 // than the wxWidgets fall-back implementation. So we need
2105 // to be able to switch this on and off at runtime.
2106 #if wxUSE_SYSTEM_OPTIONS
2107 if (wxSystemOptions::GetOptionInt(wxT("no-maskblt")) == 0)
2113 xdest
, ydest
, width
, height
,
2116 (HBITMAP
)mask
->GetMaskBitmap(),
2118 MAKEROP4(dwRop
, DSTCOPY
)
2125 // Blit bitmap with mask
2128 HBITMAP buffer_bmap
;
2130 #if wxUSE_DC_CACHEING
2131 // create a temp buffer bitmap and DCs to access it and the mask
2132 wxDCCacheEntry
* dcCacheEntry1
= FindDCInCache(NULL
, source
->GetHDC());
2133 dc_mask
= (HDC
) dcCacheEntry1
->m_dc
;
2135 wxDCCacheEntry
* dcCacheEntry2
= FindDCInCache(dcCacheEntry1
, GetHDC());
2136 dc_buffer
= (HDC
) dcCacheEntry2
->m_dc
;
2138 wxDCCacheEntry
* bitmapCacheEntry
= FindBitmapInCache(GetHDC(),
2141 buffer_bmap
= (HBITMAP
) bitmapCacheEntry
->m_bitmap
;
2142 #else // !wxUSE_DC_CACHEING
2143 // create a temp buffer bitmap and DCs to access it and the mask
2144 dc_mask
= ::CreateCompatibleDC(GetHdcOf(*source
));
2145 dc_buffer
= ::CreateCompatibleDC(GetHdc());
2146 buffer_bmap
= ::CreateCompatibleBitmap(GetHdc(), width
, height
);
2147 #endif // wxUSE_DC_CACHEING/!wxUSE_DC_CACHEING
2148 HGDIOBJ hOldMaskBitmap
= ::SelectObject(dc_mask
, (HBITMAP
) mask
->GetMaskBitmap());
2149 HGDIOBJ hOldBufferBitmap
= ::SelectObject(dc_buffer
, buffer_bmap
);
2151 // copy dest to buffer
2152 if ( !::BitBlt(dc_buffer
, 0, 0, (int)width
, (int)height
,
2153 GetHdc(), xdest
, ydest
, SRCCOPY
) )
2155 wxLogLastError(wxT("BitBlt"));
2158 // copy src to buffer using selected raster op
2159 if ( !::BitBlt(dc_buffer
, 0, 0, (int)width
, (int)height
,
2160 GetHdcOf(*source
), xsrc
, ysrc
, dwRop
) )
2162 wxLogLastError(wxT("BitBlt"));
2165 // set masked area in buffer to BLACK (pixel value 0)
2166 COLORREF prevBkCol
= ::SetBkColor(GetHdc(), RGB(255, 255, 255));
2167 COLORREF prevCol
= ::SetTextColor(GetHdc(), RGB(0, 0, 0));
2168 if ( !::BitBlt(dc_buffer
, 0, 0, (int)width
, (int)height
,
2169 dc_mask
, xsrcMask
, ysrcMask
, SRCAND
) )
2171 wxLogLastError(wxT("BitBlt"));
2174 // set unmasked area in dest to BLACK
2175 ::SetBkColor(GetHdc(), RGB(0, 0, 0));
2176 ::SetTextColor(GetHdc(), RGB(255, 255, 255));
2177 if ( !::BitBlt(GetHdc(), xdest
, ydest
, (int)width
, (int)height
,
2178 dc_mask
, xsrcMask
, ysrcMask
, SRCAND
) )
2180 wxLogLastError(wxT("BitBlt"));
2182 ::SetBkColor(GetHdc(), prevBkCol
); // restore colours to original values
2183 ::SetTextColor(GetHdc(), prevCol
);
2185 // OR buffer to dest
2186 success
= ::BitBlt(GetHdc(), xdest
, ydest
,
2187 (int)width
, (int)height
,
2188 dc_buffer
, 0, 0, SRCPAINT
) != 0;
2191 wxLogLastError(wxT("BitBlt"));
2194 // tidy up temporary DCs and bitmap
2195 ::SelectObject(dc_mask
, hOldMaskBitmap
);
2196 ::SelectObject(dc_buffer
, hOldBufferBitmap
);
2198 #if !wxUSE_DC_CACHEING
2200 ::DeleteDC(dc_mask
);
2201 ::DeleteDC(dc_buffer
);
2202 ::DeleteObject(buffer_bmap
);
2207 else // no mask, just BitBlt() it
2209 // if we already have a DIB, draw it using StretchDIBits(), otherwise
2210 // use StretchBlt() if available and finally fall back to BitBlt()
2212 // FIXME: use appropriate WinCE functions
2214 const int caps
= ::GetDeviceCaps(GetHdc(), RASTERCAPS
);
2215 if ( bmpSrc
.Ok() && (caps
& RC_STRETCHDIB
) )
2220 if ( ::GetObject(GetHbitmapOf(bmpSrc
),
2222 &ds
) == sizeof(ds
) )
2224 StretchBltModeChanger
changeMode(GetHdc(), COLORONCOLOR
);
2226 // Figure out what co-ordinate system we're supposed to specify
2228 const LONG hDIB
= ds
.dsBmih
.biHeight
;
2232 ysrc
= hDIB
- (ysrc
+ height
);
2235 if ( ::StretchDIBits(GetHdc(),
2241 (LPBITMAPINFO
)&ds
.dsBmih
,
2244 ) == (int)GDI_ERROR
)
2246 // On Win9x this API fails most (all?) of the time, so
2247 // logging it becomes quite distracting. Since it falls
2248 // back to the code below this is not really serious, so
2250 //wxLogLastError(wxT("StretchDIBits"));
2259 if ( !success
&& (caps
& RC_STRETCHBLT
) )
2264 StretchBltModeChanger
changeMode(GetHdc(), COLORONCOLOR
);
2270 xdest
, ydest
, width
, height
,
2272 xsrc
, ysrc
, width
, height
,
2276 wxLogLastError(_T("StretchBlt"));
2290 (int)width
, (int)height
,
2296 wxLogLastError(_T("BitBlt"));
2305 ::SetTextColor(GetHdc(), old_textground
);
2306 ::SetBkColor(GetHdc(), old_background
);
2311 void wxDC::GetDeviceSize(int *width
, int *height
) const
2313 WXMICROWIN_CHECK_HDC
2316 *width
= ::GetDeviceCaps(GetHdc(), HORZRES
);
2318 *height
= ::GetDeviceCaps(GetHdc(), VERTRES
);
2321 void wxDC::DoGetSizeMM(int *w
, int *h
) const
2323 WXMICROWIN_CHECK_HDC
2325 // if we implement it in terms of DoGetSize() instead of directly using the
2326 // results returned by GetDeviceCaps(HORZ/VERTSIZE) as was done before, it
2327 // will also work for wxWindowDC and wxClientDC even though their size is
2328 // not the same as the total size of the screen
2329 int wPixels
, hPixels
;
2330 DoGetSize(&wPixels
, &hPixels
);
2334 int wTotal
= ::GetDeviceCaps(GetHdc(), HORZRES
);
2336 wxCHECK_RET( wTotal
, _T("0 width device?") );
2338 *w
= (wPixels
* ::GetDeviceCaps(GetHdc(), HORZSIZE
)) / wTotal
;
2343 int hTotal
= ::GetDeviceCaps(GetHdc(), VERTRES
);
2345 wxCHECK_RET( hTotal
, _T("0 height device?") );
2347 *h
= (hPixels
* ::GetDeviceCaps(GetHdc(), VERTSIZE
)) / hTotal
;
2351 wxSize
wxDC::GetPPI() const
2353 WXMICROWIN_CHECK_HDC_RET(wxSize(0,0))
2355 int x
= ::GetDeviceCaps(GetHdc(), LOGPIXELSX
);
2356 int y
= ::GetDeviceCaps(GetHdc(), LOGPIXELSY
);
2358 return wxSize(x
, y
);
2361 // For use by wxWidgets only, unless custom units are required.
2362 void wxDC::SetLogicalScale(double x
, double y
)
2364 WXMICROWIN_CHECK_HDC
2366 m_logicalScaleX
= x
;
2367 m_logicalScaleY
= y
;
2370 // ----------------------------------------------------------------------------
2372 // ----------------------------------------------------------------------------
2374 #if wxUSE_DC_CACHEING
2377 * This implementation is a bit ugly and uses the old-fashioned wxList class, so I will
2378 * improve it in due course, either using arrays, or simply storing pointers to one
2379 * entry for the bitmap, and two for the DCs. -- JACS
2382 wxList
wxDC::sm_bitmapCache
;
2383 wxList
wxDC::sm_dcCache
;
2385 wxDCCacheEntry::wxDCCacheEntry(WXHBITMAP hBitmap
, int w
, int h
, int depth
)
2394 wxDCCacheEntry::wxDCCacheEntry(WXHDC hDC
, int depth
)
2403 wxDCCacheEntry::~wxDCCacheEntry()
2406 ::DeleteObject((HBITMAP
) m_bitmap
);
2408 ::DeleteDC((HDC
) m_dc
);
2411 wxDCCacheEntry
* wxDC::FindBitmapInCache(WXHDC dc
, int w
, int h
)
2413 int depth
= ::GetDeviceCaps((HDC
) dc
, PLANES
) * ::GetDeviceCaps((HDC
) dc
, BITSPIXEL
);
2414 wxList::compatibility_iterator node
= sm_bitmapCache
.GetFirst();
2417 wxDCCacheEntry
* entry
= (wxDCCacheEntry
*) node
->GetData();
2419 if (entry
->m_depth
== depth
)
2421 if (entry
->m_width
< w
|| entry
->m_height
< h
)
2423 ::DeleteObject((HBITMAP
) entry
->m_bitmap
);
2424 entry
->m_bitmap
= (WXHBITMAP
) ::CreateCompatibleBitmap((HDC
) dc
, w
, h
);
2425 if ( !entry
->m_bitmap
)
2427 wxLogLastError(wxT("CreateCompatibleBitmap"));
2429 entry
->m_width
= w
; entry
->m_height
= h
;
2435 node
= node
->GetNext();
2437 WXHBITMAP hBitmap
= (WXHBITMAP
) ::CreateCompatibleBitmap((HDC
) dc
, w
, h
);
2440 wxLogLastError(wxT("CreateCompatibleBitmap"));
2442 wxDCCacheEntry
* entry
= new wxDCCacheEntry(hBitmap
, w
, h
, depth
);
2443 AddToBitmapCache(entry
);
2447 wxDCCacheEntry
* wxDC::FindDCInCache(wxDCCacheEntry
* notThis
, WXHDC dc
)
2449 int depth
= ::GetDeviceCaps((HDC
) dc
, PLANES
) * ::GetDeviceCaps((HDC
) dc
, BITSPIXEL
);
2450 wxList::compatibility_iterator node
= sm_dcCache
.GetFirst();
2453 wxDCCacheEntry
* entry
= (wxDCCacheEntry
*) node
->GetData();
2455 // Don't return the same one as we already have
2456 if (!notThis
|| (notThis
!= entry
))
2458 if (entry
->m_depth
== depth
)
2464 node
= node
->GetNext();
2466 WXHDC hDC
= (WXHDC
) ::CreateCompatibleDC((HDC
) dc
);
2469 wxLogLastError(wxT("CreateCompatibleDC"));
2471 wxDCCacheEntry
* entry
= new wxDCCacheEntry(hDC
, depth
);
2472 AddToDCCache(entry
);
2476 void wxDC::AddToBitmapCache(wxDCCacheEntry
* entry
)
2478 sm_bitmapCache
.Append(entry
);
2481 void wxDC::AddToDCCache(wxDCCacheEntry
* entry
)
2483 sm_dcCache
.Append(entry
);
2486 void wxDC::ClearCache()
2488 WX_CLEAR_LIST(wxList
, sm_dcCache
);
2489 WX_CLEAR_LIST(wxList
, sm_bitmapCache
);
2492 // Clean up cache at app exit
2493 class wxDCModule
: public wxModule
2496 virtual bool OnInit() { return true; }
2497 virtual void OnExit() { wxDC::ClearCache(); }
2500 DECLARE_DYNAMIC_CLASS(wxDCModule
)
2503 IMPLEMENT_DYNAMIC_CLASS(wxDCModule
, wxModule
)
2505 #endif // wxUSE_DC_CACHEING
2507 // ----------------------------------------------------------------------------
2508 // alpha channel support
2509 // ----------------------------------------------------------------------------
2511 static bool AlphaBlt(HDC hdcDst
,
2512 int x
, int y
, int width
, int height
,
2513 int srcX
, int srcY
, HDC hdcSrc
,
2514 const wxBitmap
& bmp
)
2516 wxASSERT_MSG( bmp
.Ok() && bmp
.HasAlpha(), _T("AlphaBlt(): invalid bitmap") );
2517 wxASSERT_MSG( hdcDst
&& hdcSrc
, _T("AlphaBlt(): invalid HDC") );
2519 // do we have AlphaBlend() and company in the headers?
2520 #if defined(AC_SRC_OVER) && wxUSE_DYNLIB_CLASS
2521 // yes, now try to see if we have it during run-time
2522 typedef BOOL (WINAPI
*AlphaBlend_t
)(HDC
,int,int,int,int,
2523 HDC
,int,int,int,int,
2526 static AlphaBlend_t pfnAlphaBlend
= wxMSIMG32_SYMBOL(AlphaBlend
);
2527 if ( pfnAlphaBlend
)
2530 bf
.BlendOp
= AC_SRC_OVER
;
2532 bf
.SourceConstantAlpha
= 0xff;
2533 bf
.AlphaFormat
= AC_SRC_ALPHA
;
2535 if ( pfnAlphaBlend(hdcDst
, x
, y
, width
, height
,
2536 hdcSrc
, srcX
, srcY
, width
, height
,
2539 // skip wxAlphaBlend() call below
2543 wxLogLastError(_T("AlphaBlend"));
2546 wxUnusedVar(hdcSrc
);
2547 #endif // defined(AC_SRC_OVER)
2549 // AlphaBlend() unavailable of failed: use our own (probably much slower)
2551 #ifdef wxHAVE_RAW_BITMAP
2552 wxAlphaBlend(hdcDst
, x
, y
, width
, height
, srcX
, srcY
, bmp
);
2555 #else // !wxHAVE_RAW_BITMAP
2556 // no wxAlphaBlend() neither, fall back to using simple BitBlt() (we lose
2557 // alpha but at least something will be shown like this)
2560 #endif // wxHAVE_RAW_BITMAP
2564 // wxAlphaBlend: our fallback if ::AlphaBlend() is unavailable
2565 #ifdef wxHAVE_RAW_BITMAP
2568 wxAlphaBlend(HDC hdcDst
, int xDst
, int yDst
,
2570 int srcX
, int srcY
, const wxBitmap
& bmpSrc
)
2572 // get the destination DC pixels
2573 wxBitmap
bmpDst(w
, h
, 32 /* force creating RGBA DIB */);
2575 SelectInHDC
select(hdcMem
, GetHbitmapOf(bmpDst
));
2577 if ( !::BitBlt(hdcMem
, 0, 0, w
, h
, hdcDst
, xDst
, yDst
, SRCCOPY
) )
2579 wxLogLastError(_T("BitBlt"));
2582 // combine them with the source bitmap using alpha
2583 wxAlphaPixelData
dataDst(bmpDst
),
2584 dataSrc((wxBitmap
&)bmpSrc
);
2586 wxCHECK_RET( dataDst
&& dataSrc
,
2587 _T("failed to get raw data in wxAlphaBlend") );
2589 wxAlphaPixelData::Iterator
pDst(dataDst
),
2592 pSrc
.Offset(dataSrc
, srcX
, srcY
);
2594 for ( int y
= 0; y
< h
; y
++ )
2596 wxAlphaPixelData::Iterator pDstRowStart
= pDst
,
2597 pSrcRowStart
= pSrc
;
2599 for ( int x
= 0; x
< w
; x
++ )
2601 // note that source bitmap uses premultiplied alpha (as required by
2602 // the real AlphaBlend)
2603 const unsigned beta
= 255 - pSrc
.Alpha();
2605 pDst
.Red() = pSrc
.Red() + (beta
* pDst
.Red() + 127) / 255;
2606 pDst
.Blue() = pSrc
.Blue() + (beta
* pDst
.Blue() + 127) / 255;
2607 pDst
.Green() = pSrc
.Green() + (beta
* pDst
.Green() + 127) / 255;
2613 pDst
= pDstRowStart
;
2614 pSrc
= pSrcRowStart
;
2615 pDst
.OffsetY(dataDst
, 1);
2616 pSrc
.OffsetY(dataSrc
, 1);
2619 // and finally blit them back to the destination DC
2620 if ( !::BitBlt(hdcDst
, xDst
, yDst
, w
, h
, hdcMem
, 0, 0, SRCCOPY
) )
2622 wxLogLastError(_T("BitBlt"));
2626 #endif // #ifdef wxHAVE_RAW_BITMAP
2628 void wxDC::DoGradientFillLinear (const wxRect
& rect
,
2629 const wxColour
& initialColour
,
2630 const wxColour
& destColour
,
2631 wxDirection nDirection
)
2633 // use native function if we have compile-time support it and can load it
2634 // during run-time (linking to it statically would make the program
2635 // unusable on earlier Windows versions)
2636 #if defined(GRADIENT_FILL_RECT_H) && wxUSE_DYNLIB_CLASS
2638 (WINAPI
*GradientFill_t
)(HDC
, PTRIVERTEX
, ULONG
, PVOID
, ULONG
, ULONG
);
2639 static GradientFill_t pfnGradientFill
= wxMSIMG32_SYMBOL(GradientFill
);
2641 if ( pfnGradientFill
)
2643 GRADIENT_RECT grect
;
2644 grect
.UpperLeft
= 0;
2645 grect
.LowerRight
= 1;
2647 // invert colours direction if not filling from left-to-right or
2649 int firstVertex
= nDirection
== wxNORTH
|| nDirection
== wxWEST
? 1 : 0;
2651 // one vertex for upper left and one for upper-right
2652 TRIVERTEX vertices
[2];
2654 vertices
[0].x
= rect
.GetLeft();
2655 vertices
[0].y
= rect
.GetTop();
2656 vertices
[1].x
= rect
.GetRight();
2657 vertices
[1].y
= rect
.GetBottom();
2659 vertices
[firstVertex
].Red
= (COLOR16
)(initialColour
.Red() << 8);
2660 vertices
[firstVertex
].Green
= (COLOR16
)(initialColour
.Green() << 8);
2661 vertices
[firstVertex
].Blue
= (COLOR16
)(initialColour
.Blue() << 8);
2662 vertices
[firstVertex
].Alpha
= 0;
2663 vertices
[1 - firstVertex
].Red
= (COLOR16
)(destColour
.Red() << 8);
2664 vertices
[1 - firstVertex
].Green
= (COLOR16
)(destColour
.Green() << 8);
2665 vertices
[1 - firstVertex
].Blue
= (COLOR16
)(destColour
.Blue() << 8);
2666 vertices
[1 - firstVertex
].Alpha
= 0;
2668 if (nDirection
== wxWEST
||
2669 nDirection
== wxEAST
)
2670 if ( (*pfnGradientFill
)
2677 nDirection
== wxWEST
|| nDirection
== wxEAST
2678 ? GRADIENT_FILL_RECT_H
2679 : GRADIENT_FILL_RECT_V
2682 // skip call of the base class version below
2686 wxLogLastError(_T("GradientFill"));
2688 #endif // wxUSE_DYNLIB_CLASS
2690 wxDCBase::DoGradientFillLinear(rect
, initialColour
, destColour
, nDirection
);