]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/dc.cpp
1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "dc.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
27 #include "wx/dialog.h"
31 #include "wx/dcprint.h"
32 #include "wx/msw/private.h"
38 #if USE_COMMON_DIALOGS
58 #if !USE_SHARED_LIBRARY
59 IMPLEMENT_ABSTRACT_CLASS(wxDC
, wxObject
)
62 // Declarations local to this file
64 #define YSCALE(y) (yorigin - (y))
66 // #define wx_round(a) (int)((a)+.5)
68 // Default constructor
71 // Stop internal GDI objects being found and pointers kept
72 // after these objects have been deleted.
73 // No - it's OK; these lists won't return them
74 // because their 'visible' status will be FALSE.
76 wxTheFontList->RemoveFont(&m_font);
77 wxThePenList->RemovePen(&m_pen);
78 wxTheBrushList->RemoveBrush(&m_brush);
79 wxTheBrushList->RemoveBrush(&m_backgroundBrush);
82 m_minX
= 0; m_minY
= 0; m_maxX
= 0; m_maxY
= 0;
84 m_autoSetting
= TRUE
;
88 // m_selectedBitmap = NULL;
101 m_minX
= 0; m_minY
= 0; m_maxX
= 0; m_maxY
= 0;
103 m_logicalOriginX
= 0;
104 m_logicalOriginY
= 0;
107 m_logicalScaleX
= 1.0;
108 m_logicalScaleY
= 1.0;
113 m_systemScaleX
= 1.0;
114 m_systemScaleY
= 1.0;
115 m_mappingMode
= MM_TEXT
;
116 // m_dontDelete = FALSE;
121 m_windowExtX
= VIEWPORT_EXTENT
;
122 m_windowExtY
= VIEWPORT_EXTENT
;
123 m_logicalFunction
= -1;
125 m_backgroundBrush
= *wxWHITE_BRUSH
;
126 // m_backgroundBrush.UseResource();
128 m_textForegroundColour
= *wxBLACK
;
129 m_textBackgroundColour
= *wxWHITE
;
131 m_colour
= wxColourDisplay();
140 SelectOldObjects(m_hDC
);
142 if ( m_canvas
== NULL
)
143 ::DeleteDC((HDC
)m_hDC
);
145 ::ReleaseDC((HWND
)m_canvas
->GetHWND(), (HDC
)m_hDC
);
152 SelectOldObjects(m_hDC);
153 ::DeleteDC((HDC) m_hDC);
158 // This will select current objects out of the DC,
159 // which is what you have to do before deleting the
161 void wxDC::SelectOldObjects(WXHDC dc
)
164 wxDebugMsg("wxDC::SelectOldObjects %X\n", this);
171 wxDebugMsg("wxDC::SelectOldObjects: Selecting old HBITMAP %X\n", m_oldBitmap
);
173 ::SelectObject((HDC
) dc
, (HBITMAP
) m_oldBitmap
);
174 if (m_selectedBitmap
.Ok())
176 m_selectedBitmap
.SetSelectedInto(NULL
);
183 wxDebugMsg("wxDC::SelectOldObjects: Selecting old HPEN %X\n", m_oldPen
);
185 ::SelectObject((HDC
) dc
, (HPEN
) m_oldPen
);
191 wxDebugMsg("wxDC::SelectOldObjects: Selecting old HBRUSH %X\n", m_oldBrush
);
193 ::SelectObject((HDC
) dc
, (HBRUSH
) m_oldBrush
);
199 wxDebugMsg("wxDC::SelectOldObjects: Selecting old HFONT %X\n", m_oldFont
);
201 ::SelectObject((HDC
) dc
, (HFONT
) m_oldFont
);
207 wxDebugMsg("wxDC::SelectOldObjects: Selecting old HPALETTE %X\n", m_oldPalette
);
209 ::SelectPalette((HDC
) dc
, (HPALETTE
) m_oldPalette
, TRUE
);
212 wxDebugMsg("wxDC::SelectOldObjects: Done.\n");
218 m_font.ReleaseResource();
220 m_pen.ReleaseResource();
222 m_brush.ReleaseResource();
223 if (m_backgroundBrush.Ok())
224 m_backgroundBrush.ReleaseResource();
227 m_brush
= wxNullBrush
;
229 m_palette
= wxNullPalette
;
231 m_backgroundBrush
= wxNullBrush
;
232 m_selectedBitmap
= wxNullBitmap
;
235 void wxDC::SetClippingRegion(long cx
, long cy
, long cw
, long ch
)
240 m_clipX2
= (int)(cx
+ cw
);
241 m_clipY2
= (int)(cy
+ ch
);
245 DoClipping((WXHDC
) m_hDC
);
250 void wxDC::DoClipping(WXHDC dc
)
252 if (m_clipping
&& dc
)
254 IntersectClipRect((HDC
) dc
, XLOG2DEV(m_clipX1
), YLOG2DEV(m_clipY1
),
255 XLOG2DEV(m_clipX2
), YLOG2DEV(m_clipY2
));
261 m_canvas
->CalcScrolledPosition(0, 0, &x_off
, &y_off
);
263 // HRGN rgn = CreateRectRgn(XLOG2DEV(m_clipX1 + x_off), YLOG2DEV(m_clipY1 + y_off),
264 // XLOG2DEV(m_clipX2 + x_off), YLOG2DEV(m_clipY2 + y_off));
266 // SelectClipRgn(dc, rgn);
267 // DeleteObject(rgn);
268 IntersectClipRect((HDC
) dc
, XLOG2DEV(m_clipX1
+ x_off
), YLOG2DEV(m_clipY1
+ y_off
),
269 XLOG2DEV(m_clipX2
+ x_off
), YLOG2DEV(m_clipY2
+ y_off
));
274 void wxDC::DestroyClippingRegion(void)
278 if (m_clipping
&& m_hDC
)
280 // SelectClipRgn(dc, NULL);
281 HRGN rgn
= CreateRectRgn(0, 0, 32000, 32000);
283 wxDebugMsg("wxDC::DestroyClippingRegion: Selecting HRGN %X\n", rgn
);
285 SelectClipRgn((HDC
) m_hDC
, rgn
);
287 wxDebugMsg("wxDC::DestroyClippingRegion: Deleting HRGN %X\n", rgn
);
296 bool wxDC::CanDrawBitmap(void) const
301 bool wxDC::CanGetTextExtent(void) const
303 // What sort of display is it?
304 int technology
= ::GetDeviceCaps((HDC
) m_hDC
, TECHNOLOGY
);
308 if (technology
!= DT_RASDISPLAY
&& technology
!= DT_RASPRINTER
)
315 void wxDC::SetPalette(const wxPalette
& palette
)
319 m_palette
= m_palette
;
323 // Setting a NULL colourmap is a way of restoring
324 // the original colourmap
327 ::SelectPalette((HDC
) m_hDC
, (HPALETTE
) m_oldPalette
, TRUE
);
329 wxDebugMsg("wxDC::SetPalette: set old palette %X\n", m_oldPalette
);
337 if (m_palette
.Ok() && m_palette
.GetHPALETTE())
339 HPALETTE oldPal
= ::SelectPalette((HDC
) m_hDC
, (HPALETTE
) m_palette
.GetHPALETTE(), TRUE
);
341 m_oldPalette
= (WXHPALETTE
) oldPal
;
344 wxDebugMsg("wxDC::SetPalette %X: selected palette %X\n", this, m_palette
.GetHPALETTE());
346 wxDebugMsg("wxDC::SetPalette: oldPal was palette %X\n", oldPal
);
348 wxDebugMsg("wxDC::SetPalette: m_oldPalette is palette %X\n", m_oldPalette
);
350 ::RealizePalette((HDC
) m_hDC
);
356 void wxDC::Clear(void)
362 GetClientRect((HWND
) m_canvas
->GetHWND(), &rect
);
363 else if (m_selectedBitmap
.Ok())
365 rect
.left
= 0; rect
.top
= 0;
366 rect
.right
= m_selectedBitmap
.GetWidth();
367 rect
.bottom
= m_selectedBitmap
.GetHeight();
369 (void) ::SetMapMode((HDC
) m_hDC
, MM_TEXT
);
371 DWORD colour
= GetBkColor((HDC
) m_hDC
);
372 HBRUSH brush
= CreateSolidBrush(colour
);
373 FillRect((HDC
) m_hDC
, &rect
, brush
);
376 ::SetMapMode((HDC
) m_hDC
, MM_ANISOTROPIC
);
377 ::SetViewportExtEx((HDC
) m_hDC
, VIEWPORT_EXTENT
, VIEWPORT_EXTENT
, NULL
);
378 ::SetWindowExtEx((HDC
) m_hDC
, m_windowExtX
, m_windowExtY
, NULL
);
379 ::SetViewportOrgEx((HDC
) m_hDC
, (int)m_deviceOriginX
, (int)m_deviceOriginY
, NULL
);
380 ::SetWindowOrgEx((HDC
) m_hDC
, (int)m_logicalOriginX
, (int)m_logicalOriginY
, NULL
);
385 void wxDC::FloodFill(long x
, long y
, wxColour
*col
, int style
)
390 if (m_brush
.Ok() && m_autoSetting
)
394 // m_canvas->CalcScrolledPosition((int)x, (int)y, &xx, &yy);
398 (void)ExtFloodFill((HDC
) m_hDC
, XLOG2DEV(x
), YLOG2DEV(y
),
400 style
==wxFLOOD_SURFACE
?
401 FLOODFILLSURFACE
:FLOODFILLBORDER
406 CalcBoundingBox(x
, y
);
409 bool wxDC::GetPixel(long x
, long y
, wxColour
*col
) const
411 // added by steve 29.12.94 (copied from DrawPoint)
412 // returns TRUE for pixels in the color of the current pen
413 // and FALSE for all other pixels colors
414 // if col is non-NULL return the color of the pixel
416 // get the color of the pixel
417 COLORREF pixelcolor
= ::GetPixel((HDC
) m_hDC
, XLOG2DEV(x
), YLOG2DEV(y
));
418 // get the color of the pen
419 COLORREF pencolor
= 0x00ffffff;
422 pencolor
= m_pen
.GetColour().GetPixel() ;
425 // return the color of the pixel
427 col
->Set(GetRValue(pixelcolor
),GetGValue(pixelcolor
),GetBValue(pixelcolor
));
429 // check, if color of the pixels is the same as the color
430 // of the current pen
431 return(pixelcolor
==pencolor
);
434 void wxDC::CrossHair(long x
, long y
)
436 if (m_pen
.Ok() && m_autoSetting
)
439 // We suppose that our screen is 2000x2000 max.
447 (void)MoveToEx((HDC
) m_hDC
, XLOG2DEV(x1
), YLOG2DEV(y
), NULL
);
448 (void)LineTo((HDC
) m_hDC
, XLOG2DEV(x2
), YLOG2DEV(y
));
450 (void)MoveToEx((HDC
) m_hDC
, XLOG2DEV(x
), YLOG2DEV(y1
), NULL
);
451 (void)LineTo((HDC
) m_hDC
, XLOG2DEV(x
), YLOG2DEV(y2
));
455 CalcBoundingBox(x1
, y1
);
456 CalcBoundingBox(x2
, y2
);
459 void wxDC::DrawLine(long x1
, long y1
, long x2
, long y2
)
461 // BUGBUG - is this necessary? YES YES YES YEs Yes yes ye....
462 if (m_pen
.Ok() && m_autoSetting
)
467 (void)MoveToEx((HDC
) m_hDC
, XLOG2DEV(x1
), YLOG2DEV(y1
), NULL
);
468 (void)LineTo((HDC
) m_hDC
, XLOG2DEV(x2
), YLOG2DEV(y2
));
470 /* MATTHEW: [6] New normalization */
471 #if WX_STANDARD_GRAPHICS
472 (void)LineTo((HDC
) m_hDC
, XLOG2DEV(x2
) + 1, YLOG2DEV(y2
));
477 CalcBoundingBox(x1
, y1
);
478 CalcBoundingBox(x2
, y2
);
481 void wxDC::DrawArc(long x1
,long y1
,long x2
,long y2
,double xc
,double yc
)
485 double radius
= (double)sqrt(dx
*dx
+dy
*dy
) ;;
486 if (x1
==x2
&& x2
==y2
)
488 DrawEllipse(xc
,yc
,(double)(radius
*2.0),(double)(radius
*2)) ;
492 // BUGBUG - is this necessary?
493 if (m_pen
.Ok() && m_autoSetting
)
498 long xx1
= XLOG2DEV(x1
) ;
499 long yy1
= YLOG2DEV(y1
) ;
500 long xx2
= XLOG2DEV(x2
) ;
501 long yy2
= YLOG2DEV(y2
) ;
502 long xxc
= XLOG2DEV(xc
) ;
503 long yyc
= YLOG2DEV(yc
) ;
504 long ray
= (long) sqrt(double((xxc
-xx1
)*(xxc
-xx1
)+(yyc
-yy1
)*(yyc
-yy1
))) ;
506 (void)MoveToEx((HDC
) m_hDC
, (int) xx1
, (int) yy1
, NULL
);
507 long xxx1
= (long) (xxc
-ray
);
508 long yyy1
= (long) (yyc
-ray
);
509 long xxx2
= (long) (xxc
+ray
);
510 long yyy2
= (long) (yyc
+ray
);
511 if (m_brush
.Ok() && m_brush
.GetStyle() !=wxTRANSPARENT
)
513 // BUGBUG - is this necessary?
514 if (m_brush
.GetStyle()!=wxTRANSPARENT
&&m_autoSetting
)
516 Pie((HDC
) m_hDC
,xxx1
,yyy1
,xxx2
,yyy2
,
520 Arc((HDC
) m_hDC
,xxx1
,yyy1
,xxx2
,yyy2
,
525 CalcBoundingBox((xc
-radius
), (yc
-radius
));
526 CalcBoundingBox((xc
+radius
), (yc
+radius
));
529 void wxDC::DrawPoint(long x
, long y
)
531 // BUGBUG - is this necessary?
532 if (m_pen
.Ok() && m_autoSetting
)
537 COLORREF color
= 0x00ffffff;
540 // m_pen.RealizeResource();
541 color
= m_pen
.GetColour().GetPixel() ;
544 SetPixel((HDC
) m_hDC
, XLOG2DEV(x
), YLOG2DEV(y
), color
);
548 CalcBoundingBox(x
, y
);
551 void wxDC::DrawPolygon(int n
, wxPoint points
[], long xoffset
, long yoffset
,int fillStyle
)
553 // BUGBUG - is this necessary?
554 if (m_pen
.Ok() && m_autoSetting
)
559 POINT
*cpoints
= new POINT
[n
];
561 for (i
= 0; i
< n
; i
++)
563 cpoints
[i
].x
= (int)(XLOG2DEV(points
[i
].x
+ xoffset
));
564 cpoints
[i
].y
= (int)(YLOG2DEV(points
[i
].y
+ yoffset
));
566 CalcBoundingBox(points
[i
].x
+ xoffset
, points
[i
].y
+ yoffset
);
569 int prev
= SetPolyFillMode((HDC
) m_hDC
,fillStyle
==wxODDEVEN_RULE
?ALTERNATE
:WINDING
) ;
570 (void)Polygon((HDC
) m_hDC
, cpoints
, n
);
571 SetPolyFillMode((HDC
) m_hDC
,prev
) ;
578 void wxDC::DrawLines(int n
, wxPoint points
[], long xoffset
, long yoffset
)
580 // BUGBUG - is this necessary?
581 if (m_pen
.Ok() && m_autoSetting
)
586 POINT
*cpoints
= new POINT
[n
];
588 for (i
= 0; i
< n
; i
++)
590 cpoints
[i
].x
= (int)(XLOG2DEV(points
[i
].x
+ xoffset
));
591 cpoints
[i
].y
= (int)(YLOG2DEV(points
[i
].y
+ yoffset
));
593 CalcBoundingBox(points
[i
].x
+ xoffset
, points
[i
].y
+ yoffset
);
596 (void)Polyline((HDC
) m_hDC
, cpoints
, n
);
603 void wxDC::DrawRectangle(long x
, long y
, long width
, long height
)
605 // BUGBUG - is this necessary?
606 if (m_pen
.Ok() && m_autoSetting
)
610 long y2
= y
+ height
;
614 /* MATTHEW: [6] new normalization */
615 #if WX_STANDARD_GRAPHICS
616 bool do_brush
, do_pen
;
618 do_brush
= m_brush
.Ok() && m_brush
.GetStyle() != wxTRANSPARENT
;
619 do_pen
= m_pen
.Ok() && m_pen
.GetStyle() != wxTRANSPARENT
;
622 HPEN orig_pen
= NULL
;
624 if (do_pen
|| !m_pen
.Ok())
625 orig_pen
= ::SelectObject((HDC
) m_hDC
, ::GetStockObject(NULL_PEN
));
627 (void)Rectangle((HDC
) m_hDC
, XLOG2DEV(x
), YLOG2DEV(y
),
628 XLOG2DEV(x2
) + 1, YLOG2DEV(y2
) + 1);
630 if (do_pen
|| !m_pen
.Ok())
631 ::SelectObject((HDC
) m_hDC
, orig_pen
);
634 HBRUSH orig_brush
= NULL
;
636 if (do_brush
|| !m_brush
.Ok())
637 orig_brush
= ::SelectObject((HDC
) m_hDC
, ::GetStockObject(NULL_BRUSH
));
639 (void)Rectangle((HDC
) m_hDC
, XLOG2DEV(x
), YLOG2DEV(y
),
640 XLOG2DEV(x2
), YLOG2DEV(y2
));
642 if (do_brush
|| !m_brush
.Ok())
643 ::SelectObject((HDC
) m_hDC
, orig_brush
);
646 (void)Rectangle((HDC
) m_hDC
, XLOG2DEV(x
), YLOG2DEV(y
), XLOG2DEV(x2
), YLOG2DEV(y2
));
649 CalcBoundingBox(x
, y
);
650 CalcBoundingBox(x2
, y2
);
655 void wxDC::DrawRoundedRectangle(long x
, long y
, long width
, long height
, double radius
)
657 // BUGBUG - is this necessary?
658 if (m_pen
.Ok() && m_autoSetting
)
661 // Now, a negative radius value is interpreted to mean
662 // 'the proportion of the smallest X or Y dimension'
666 double smallest
= 0.0;
671 radius
= (- radius
* smallest
);
675 long y2
= (y
+height
);
679 (void)RoundRect((HDC
) m_hDC
, XLOG2DEV(x
), YLOG2DEV(y
), XLOG2DEV(x2
),
680 YLOG2DEV(y2
), 2*XLOG2DEV(radius
), 2*YLOG2DEV(radius
));
682 CalcBoundingBox(x
, y
);
683 CalcBoundingBox(x2
, y2
);
688 void wxDC::DrawEllipse(long x
, long y
, long width
, long height
)
690 // BUGBUG - is this necessary?
691 if (m_pen
.Ok() && m_autoSetting
)
695 long y2
= (y
+height
);
699 (void)Ellipse((HDC
) m_hDC
, XLOG2DEV(x
), YLOG2DEV(y
), XLOG2DEV(x2
), YLOG2DEV(y2
));
703 CalcBoundingBox(x
, y
);
704 CalcBoundingBox(x2
, y2
);
707 // Chris Breeze 20/5/98: first implementation of DrawEllipticArc on Windows
708 void wxDC::DrawEllipticArc(long x
,long y
,long w
,long h
,double sa
,double ea
)
710 // BUGBUG - is this necessary?
711 if (m_pen
.Ok() && m_autoSetting
)
719 const double deg2rad
= 3.14159265359 / 180.0;
720 int rx1
= XLOG2DEV(x
+w
/2);
721 int ry1
= YLOG2DEV(y
+h
/2);
724 rx1
+= (int)(100.0 * abs(w
) * cos(sa
* deg2rad
));
725 ry1
-= (int)(100.0 * abs(h
) * m_signY
* sin(sa
* deg2rad
));
726 rx2
+= (int)(100.0 * abs(w
) * cos(ea
* deg2rad
));
727 ry2
-= (int)(100.0 * abs(h
) * m_signY
* sin(ea
* deg2rad
));
729 // draw pie with NULL_PEN first and then outline otherwise a line is
730 // drawn from the start and end points to the centre
731 HPEN orig_pen
= ::SelectObject((HDC
) m_hDC
, ::GetStockObject(NULL_PEN
));
734 (void)Pie((HDC
) m_hDC
, XLOG2DEV(x
), YLOG2DEV(y
), XLOG2DEV(x2
)+1, YLOG2DEV(y2
)+1,
739 (void)Pie((HDC
) m_hDC
, XLOG2DEV(x
), YLOG2DEV(y
)-1, XLOG2DEV(x2
)+1, YLOG2DEV(y2
),
740 rx1
, ry1
-1, rx2
, ry2
-1);
742 ::SelectObject((HDC
) m_hDC
, orig_pen
);
743 (void)Arc((HDC
) m_hDC
, XLOG2DEV(x
), YLOG2DEV(y
), XLOG2DEV(x2
), YLOG2DEV(y2
),
748 CalcBoundingBox(x
, y
);
749 CalcBoundingBox(x2
, y2
);
752 void wxDC::DrawIcon(const wxIcon
& icon
, long x
, long y
)
756 ::DrawIcon((HDC
) m_hDC
, XLOG2DEV(x
), YLOG2DEV(y
), (HICON
) icon
.GetHICON());
757 CalcBoundingBox(x
, y
);
758 CalcBoundingBox(x
+icon
.GetWidth(), y
+icon
.GetHeight());
763 void wxDC::SetFont(const wxFont
& the_font
)
765 // Release the current font from servitude (decrements the usage count)
767 // m_font.ReleaseResource();
776 ::SelectObject((HDC
) m_hDC
, (HFONT
) m_oldFont
);
780 if (m_font
.Ok() && m_font
.GetResourceHandle())
783 wxDebugMsg("wxDC::SetFont: Selecting HFONT %X\n", m_font
.GetResourceHandle());
785 HFONT f
= (HFONT
) ::SelectObject((HDC
) m_hDC
, (HFONT
) m_font
.GetResourceHandle());
787 m_oldFont
= (WXHFONT
) f
;
792 void wxDC::SetPen(const wxPen
& pen
)
797 // m_pen.ReleaseResource();
804 ::SelectObject((HDC
) m_hDC
, (HPEN
) m_oldPen
);
810 // m_pen.UseResource();
811 // m_pen.RealizeResource();
812 if (m_pen
.GetResourceHandle())
814 HPEN p
= (HPEN
) ::SelectObject((HDC
) m_hDC
, (HPEN
)m_pen
.GetResourceHandle()) ;
816 m_oldPen
= (WXHPEN
) p
;
823 void wxDC::SetBrush(const wxBrush
& brush
)
828 // m_brush.ReleaseResource();
835 ::SelectObject((HDC
) m_hDC
, (HBRUSH
) m_oldBrush
);
841 // m_brush.UseResource();
842 // m_brush.RealizeResource();
844 if (m_brush
.GetResourceHandle())
847 b
= ::SelectObject((HDC
) m_hDC
, (HBRUSH
)m_brush
.GetResourceHandle()) ;
849 m_oldBrush
= (WXHBRUSH
) b
;
855 void wxDC::DrawText(const wxString
& text
, long x
, long y
, bool use16bit
)
859 if (m_font
.Ok() && m_font
.GetResourceHandle())
862 wxDebugMsg("wxDC::DrawText: Selecting HFONT %X\n", m_font
.GetResourceHandle());
864 HFONT f
= ::SelectObject((HDC
) m_hDC
, (HFONT
) m_font
.GetResourceHandle());
866 m_oldFont
= (WXHFONT
) f
;
869 if (m_textForegroundColour
.Ok())
870 SetTextColor((HDC
) m_hDC
, m_textForegroundColour
.GetPixel() ) ;
872 DWORD old_background
;
873 if (m_textBackgroundColour
.Ok())
875 old_background
= SetBkColor((HDC
) m_hDC
, m_textBackgroundColour
.GetPixel() ) ;
878 if (m_backgroundMode
== wxTRANSPARENT
)
879 SetBkMode((HDC
) m_hDC
, TRANSPARENT
);
881 SetBkMode((HDC
) m_hDC
, OPAQUE
);
883 (void)TextOut((HDC
) m_hDC
, XLOG2DEV(x
), YLOG2DEV(y
), (char *) (const char *)text
, strlen((const char *)text
));
885 if (m_textBackgroundColour
.Ok())
886 (void)SetBkColor((HDC
) m_hDC
, old_background
);
888 CalcBoundingBox(x
, y
);
891 GetTextExtent(text
, &w
, &h
);
892 CalcBoundingBox((x
+ w
), (y
+ h
));
897 void wxDC::SetBackground(const wxBrush
& brush
)
899 // if (m_backgroundBrush.Ok())
900 // m_backgroundBrush.ReleaseResource();
902 m_backgroundBrush
= brush
;
904 if (!m_backgroundBrush
.Ok())
907 // m_backgroundBrush.UseResource();
908 // m_backgroundBrush.RealizeResource() ;
912 bool customColours
= TRUE
;
913 // If we haven't specified wxUSER_COLOURS, don't allow the panel/dialog box to
914 // change background colours from the control-panel specified colours.
915 if (m_canvas
->IsKindOf(CLASSINFO(wxWindow
)) && ((m_canvas
->GetWindowStyleFlag() & wxUSER_COLOURS
) != wxUSER_COLOURS
))
916 customColours
= FALSE
;
920 // HBRUSH br = (m_backgroundBrush.GetStyle()==wxTRANSPARENT) ?
921 // GetStockObject(NULL_BRUSH) : (HBRUSH) m_backgroundBrush.GetResourceHandle();
922 if (m_backgroundBrush
.GetStyle()==wxTRANSPARENT
)
924 m_canvas
->m_backgroundTransparent
= TRUE
;
928 m_canvas
->SetBackgroundColour(m_backgroundBrush
.GetColour());
929 m_canvas
->m_backgroundTransparent
= FALSE
;
935 COLORREF new_color
= m_backgroundBrush
.GetColour().GetPixel() ;
937 (void)SetBkColor((HDC
) m_hDC
, new_color
);
943 void wxDC::SetBackgroundMode(int mode
)
945 m_backgroundMode
= mode
;
949 if (m_backgroundMode
== wxTRANSPARENT
)
950 ::SetBkMode((HDC
) m_hDC
, TRANSPARENT
);
952 ::SetBkMode((HDC
) m_hDC
, OPAQUE
);
957 void wxDC::SetLogicalFunction(int function
)
959 m_logicalFunction
= function
;
963 SetRop((WXHDC
) m_hDC
);
968 void wxDC::SetRop(WXHDC dc
)
970 if (!dc
|| m_logicalFunction
< 0)
974 // These may be wrong
975 switch (m_logicalFunction
)
977 // case wxXOR: c_rop = R2_XORPEN; break;
978 case wxXOR
: c_rop
= R2_NOTXORPEN
; break;
979 case wxINVERT
: c_rop
= R2_NOT
; break;
980 case wxOR_REVERSE
: c_rop
= R2_MERGEPENNOT
; break;
981 case wxAND_REVERSE
: c_rop
= R2_MASKPENNOT
; break;
982 case wxCLEAR
: c_rop
= R2_WHITE
; break;
983 case wxSET
: c_rop
= R2_BLACK
; break;
984 case wxSRC_INVERT
: c_rop
= R2_NOTCOPYPEN
; break;
985 case wxOR_INVERT
: c_rop
= R2_MERGENOTPEN
; break;
986 case wxAND
: c_rop
= R2_MASKPEN
; break;
987 case wxOR
: c_rop
= R2_MERGEPEN
; break;
988 case wxAND_INVERT
: c_rop
= R2_MASKNOTPEN
; break;
993 c_rop
= R2_COPYPEN
; break;
995 SetROP2((HDC
) dc
, c_rop
);
998 bool wxDC::StartDoc(const wxString
& message
)
1000 if (!this->IsKindOf(CLASSINFO(wxPrinterDC
)))
1006 docinfo
.cbSize
= sizeof(DOCINFO
);
1007 docinfo
.lpszDocName
= (const char *)message
;
1008 docinfo
.lpszOutput
= (const char *)m_filename
;
1009 #if defined(__WIN95__)
1010 docinfo
.lpszDatatype
= NULL
;
1014 if (m_hDC
) flag
= (SP_ERROR
!=
1016 ::StartDoc((HDC
) m_hDC
, &docinfo
));
1019 ::StartDocW((HDC
) m_hDC
, &docinfo
));
1021 ::StartDocA((HDC
) m_hDC
, &docinfo
));
1030 void wxDC::EndDoc(void)
1032 if (!this->IsKindOf(CLASSINFO(wxPrinterDC
)))
1034 if (m_hDC
) ::EndDoc((HDC
) m_hDC
);
1037 void wxDC::StartPage(void)
1039 if (!this->IsKindOf(CLASSINFO(wxPrinterDC
)))
1042 ::StartPage((HDC
) m_hDC
);
1045 void wxDC::EndPage(void)
1047 if (!this->IsKindOf(CLASSINFO(wxPrinterDC
)))
1050 ::EndPage((HDC
) m_hDC
);
1053 long wxDC::GetCharHeight(void) const
1055 TEXTMETRIC lpTextMetric
;
1057 GetTextMetrics((HDC
) m_hDC
, &lpTextMetric
);
1059 return YDEV2LOGREL(lpTextMetric
.tmHeight
);
1062 long wxDC::GetCharWidth(void) const
1064 TEXTMETRIC lpTextMetric
;
1066 GetTextMetrics((HDC
) m_hDC
, &lpTextMetric
);
1068 return XDEV2LOGREL(lpTextMetric
.tmAveCharWidth
);
1071 void wxDC::GetTextExtent(const wxString
& string
, long *x
, long *y
,
1072 long *descent
, long *externalLeading
, wxFont
*theFont
, bool use16bit
) const
1074 wxFont
*fontToUse
= (wxFont
*) theFont
;
1076 fontToUse
= (wxFont
*) &m_font
;
1081 GetTextExtentPoint((HDC
) m_hDC
, (char *)(const char *) string
, strlen((char *)(const char *) string
), &sizeRect
);
1082 GetTextMetrics((HDC
) m_hDC
, &tm
);
1084 *x
= XDEV2LOGREL(sizeRect
.cx
);
1085 *y
= YDEV2LOGREL(sizeRect
.cy
);
1086 if (descent
) *descent
= tm
.tmDescent
;
1087 if (externalLeading
) *externalLeading
= tm
.tmExternalLeading
;
1090 void wxDC::SetMapMode(int mode
)
1092 m_mappingMode
= mode
;
1094 int pixel_width
= 0;
1095 int pixel_height
= 0;
1101 pixel_width
= GetDeviceCaps((HDC
) m_hDC
, HORZRES
);
1102 pixel_height
= GetDeviceCaps((HDC
) m_hDC
, VERTRES
);
1103 mm_width
= GetDeviceCaps((HDC
) m_hDC
, HORZSIZE
);
1104 mm_height
= GetDeviceCaps((HDC
) m_hDC
, VERTSIZE
);
1106 if ((pixel_width
== 0) || (pixel_height
== 0) || (mm_width
== 0) || (mm_height
== 0))
1108 // if (!m_hDC && m_canvas)
1109 // m_canvas->ReleaseHDC() ;
1113 double mm2pixelsX
= pixel_width
/mm_width
;
1114 double mm2pixelsY
= pixel_height
/mm_height
;
1120 m_logicalScaleX
= (twips2mm
* mm2pixelsX
);
1121 m_logicalScaleY
= (twips2mm
* mm2pixelsY
);
1126 m_logicalScaleX
= (pt2mm
* mm2pixelsX
);
1127 m_logicalScaleY
= (pt2mm
* mm2pixelsY
);
1132 m_logicalScaleX
= mm2pixelsX
;
1133 m_logicalScaleY
= mm2pixelsY
;
1138 m_logicalScaleX
= (mm2pixelsX
/10.0);
1139 m_logicalScaleY
= (mm2pixelsY
/10.0);
1145 m_logicalScaleX
= 1.0;
1146 m_logicalScaleY
= 1.0;
1151 if (::GetMapMode((HDC
) m_hDC
) != MM_ANISOTROPIC
)
1152 ::SetMapMode((HDC
) m_hDC
, MM_ANISOTROPIC
);
1154 SetViewportExtEx((HDC
) m_hDC
, VIEWPORT_EXTENT
, VIEWPORT_EXTENT
, NULL
);
1155 m_windowExtX
= (int)MS_XDEV2LOGREL(VIEWPORT_EXTENT
);
1156 m_windowExtY
= (int)MS_YDEV2LOGREL(VIEWPORT_EXTENT
);
1157 ::SetWindowExtEx((HDC
) m_hDC
, m_windowExtX
, m_windowExtY
, NULL
);
1158 ::SetViewportOrgEx((HDC
) m_hDC
, (int)m_deviceOriginX
, (int)m_deviceOriginY
, NULL
);
1159 ::SetWindowOrgEx((HDC
) m_hDC
, (int)m_logicalOriginX
, (int)m_logicalOriginY
, NULL
);
1164 void wxDC::SetUserScale(double x
, double y
)
1169 SetMapMode(m_mappingMode
);
1172 void wxDC::SetAxisOrientation(bool xLeftRight
, bool yBottomUp
)
1174 m_signX
= xLeftRight
? 1 : -1;
1175 m_signY
= yBottomUp
? -1 : 1;
1177 SetMapMode(m_mappingMode
);
1180 void wxDC::SetSystemScale(double x
, double y
)
1185 SetMapMode(m_mappingMode
);
1188 void wxDC::SetLogicalOrigin(long x
, long y
)
1190 m_logicalOriginX
= x
;
1191 m_logicalOriginY
= y
;
1195 ::SetWindowOrgEx((HDC
) m_hDC
, (int)m_logicalOriginX
, (int)m_logicalOriginY
, NULL
);
1200 void wxDC::SetDeviceOrigin(long x
, long y
)
1202 m_deviceOriginX
= x
;
1203 m_deviceOriginY
= y
;
1207 ::SetViewportOrgEx((HDC
) m_hDC
, (int)m_deviceOriginX
, (int)m_deviceOriginY
, NULL
);
1212 long wxDC::DeviceToLogicalX(long x
) const
1214 return (long) (((x
) - m_deviceOriginX
)/(m_logicalScaleX
*m_userScaleX
*m_signX
*m_systemScaleX
) - m_logicalOriginX
) ;
1217 long wxDC::DeviceToLogicalXRel(long x
) const
1219 return (long) ((x
)/(m_logicalScaleX
*m_userScaleX
*m_signX
*m_systemScaleX
)) ;
1222 long wxDC::DeviceToLogicalY(long y
) const
1224 return (long) (((y
) - m_deviceOriginY
)/(m_logicalScaleY
*m_userScaleY
*m_signY
*m_systemScaleY
) - m_logicalOriginY
) ;
1227 long wxDC::DeviceToLogicalYRel(long y
) const
1229 return (long) ((y
)/(m_logicalScaleY
*m_userScaleY
*m_signY
*m_systemScaleY
)) ;
1232 long wxDC::LogicalToDeviceX(long x
) const
1234 return (long) (floor((x
) - m_logicalOriginX
)*m_logicalScaleX
*m_userScaleX
*m_signX
*m_systemScaleX
+ m_deviceOriginX
) ;
1237 long wxDC::LogicalToDeviceXRel(long x
) const
1239 return (long) (floor(x
)*m_logicalScaleX
*m_userScaleX
*m_signX
*m_systemScaleX
) ;
1242 long wxDC::LogicalToDeviceY(long y
) const
1244 return (long) (floor((y
) - m_logicalOriginY
)*m_logicalScaleY
*m_userScaleY
*m_signY
*m_systemScaleY
+ m_deviceOriginY
);
1247 long wxDC::LogicalToDeviceYRel(long y
) const
1249 return (long) (floor(y
)*m_logicalScaleY
*m_userScaleY
*m_signY
*m_systemScaleY
) ;
1252 // This group of functions may not do any conversion
1253 // if m_scaleGDI is TRUE, since the HDC does the
1254 // conversion automatically.
1256 long wxDC::ImplDeviceToLogicalX(long x
) const
1258 // return (m_scaleGDI ? x : DeviceToLogicalX(x));
1262 long wxDC::ImplDeviceToLogicalY(long y
) const
1264 // return (m_scaleGDI ? y : DeviceToLogicalY(y));
1268 long wxDC::ImplDeviceToLogicalXRel(long x
) const
1270 // return (m_scaleGDI ? x : DeviceToLogicalXRel(x));
1274 long wxDC::ImplDeviceToLogicalYRel(long y
) const
1276 // return (m_scaleGDI ? y : DeviceToLogicalYRel(y));
1280 long wxDC::ImplLogicalToDeviceX(long x
) const
1282 // return (m_scaleGDI ? (floor(double(x))) : LogicalToDeviceX(x));
1286 long wxDC::ImplLogicalToDeviceY(long y
) const
1288 // return (m_scaleGDI ? (floor(double(y))) : LogicalToDeviceY(y));
1292 long wxDC::ImplLogicalToDeviceXRel(long x
) const
1294 // return (m_scaleGDI ? (floor(double(x))) : LogicalToDeviceXRel(x));
1298 long wxDC::ImplLogicalToDeviceYRel(long y
) const
1300 // return (m_scaleGDI ? (floor(double(y))) : LogicalToDeviceYRel(y));
1304 bool wxDC::Blit(long xdest
, long ydest
, long width
, long height
,
1305 wxDC
*source
, long xsrc
, long ysrc
, int rop
, bool useMask
)
1308 source
->BeginDrawing();
1310 long xdest1
= xdest
;
1311 long ydest1
= ydest
;
1315 // Chris Breeze 18/5/98: use text foreground/background colours
1316 // when blitting from 1-bit bitmaps
1317 COLORREF old_textground
= ::GetTextColor((HDC
)m_hDC
);
1318 COLORREF old_background
= ::GetBkColor((HDC
)m_hDC
);
1319 if (m_textForegroundColour
.Ok())
1321 ::SetTextColor((HDC
) m_hDC
, m_textForegroundColour
.GetPixel() ) ;
1323 if (m_textBackgroundColour
.Ok())
1325 ::SetBkColor((HDC
) m_hDC
, m_textBackgroundColour
.GetPixel() ) ;
1328 DWORD dwRop
= rop
== wxCOPY
? SRCCOPY
:
1329 rop
== wxCLEAR
? WHITENESS
:
1330 rop
== wxSET
? BLACKNESS
:
1331 rop
== wxINVERT
? DSTINVERT
:
1332 rop
== wxAND
? MERGECOPY
:
1333 rop
== wxOR
? MERGEPAINT
:
1334 rop
== wxSRC_INVERT
? NOTSRCCOPY
:
1335 rop
== wxXOR
? SRCINVERT
:
1336 rop
== wxOR_REVERSE
? MERGEPAINT
:
1337 rop
== wxAND_REVERSE
? SRCERASE
:
1338 rop
== wxSRC_OR
? SRCPAINT
:
1339 rop
== wxSRC_AND
? SRCAND
:
1343 if (useMask
&& source
->m_selectedBitmap
.Ok() && source
->m_selectedBitmap
.GetMask())
1347 // Not implemented under Win95 (or maybe a specific device?)
1348 if (MaskBlt((HDC
) m_hDC
, xdest1
, ydest1
, (int)width
, (int)height
,
1349 (HDC
) source
->m_hDC
, xsrc1
, ysrc1
, (HBITMAP
) source
->m_selectedBitmap
.GetMask()->GetMaskBitmap(),
1359 HDC dc_mask
= CreateCompatibleDC((HDC
) source
->m_hDC
);
1360 ::SelectObject(dc_mask
, (HBITMAP
) source
->m_selectedBitmap
.GetMask()->GetMaskBitmap());
1361 success
= (BitBlt((HDC
) m_hDC
, xdest1
, ydest1
, (int)width
, (int)height
,
1362 dc_mask
, xsrc1
, ysrc1
, 0x00220326 /* NOTSRCAND */) != 0);
1363 success
= (BitBlt((HDC
) m_hDC
, xdest1
, ydest1
, (int)width
, (int)height
,
1364 (HDC
) source
->m_hDC
, xsrc1
, ysrc1
, SRCPAINT
) != 0);
1365 ::SelectObject(dc_mask
, 0);
1366 ::DeleteDC(dc_mask
);
1368 // New code from Chris Breeze, 8/5/98
1370 // create a temp buffer bitmap and DCs to access it and the mask
1371 HDC dc_mask
= ::CreateCompatibleDC((HDC
) source
->m_hDC
);
1372 HDC dc_buffer
= ::CreateCompatibleDC((HDC
) m_hDC
);
1373 HBITMAP buffer_bmap
= ::CreateCompatibleBitmap((HDC
) m_hDC
, width
, height
);
1374 ::SelectObject(dc_mask
, (HBITMAP
) source
->m_selectedBitmap
.GetMask()->GetMaskBitmap());
1375 ::SelectObject(dc_buffer
, buffer_bmap
);
1377 // copy dest to buffer
1378 ::BitBlt(dc_buffer
, 0, 0, (int)width
, (int)height
,
1379 (HDC
) m_hDC
, xdest1
, ydest1
, SRCCOPY
);
1381 // copy src to buffer using selected raster op
1382 ::BitBlt(dc_buffer
, 0, 0, (int)width
, (int)height
,
1383 (HDC
) source
->m_hDC
, xsrc1
, ysrc1
, dwRop
);
1385 // set masked area in buffer to BLACK (pixel value 0)
1386 COLORREF prevBkCol
= ::SetBkColor((HDC
) m_hDC
, RGB(255, 255, 255));
1387 COLORREF prevCol
= ::SetTextColor((HDC
) m_hDC
, RGB(0, 0, 0));
1388 ::BitBlt(dc_buffer
, 0, 0, (int)width
, (int)height
,
1389 dc_mask
, xsrc1
, ysrc1
, SRCAND
);
1391 // set unmasked area in dest to BLACK
1392 ::SetBkColor((HDC
) m_hDC
, RGB(0, 0, 0));
1393 ::SetTextColor((HDC
) m_hDC
, RGB(255, 255, 255));
1394 ::BitBlt((HDC
) m_hDC
, xdest1
, ydest1
, (int)width
, (int)height
,
1395 dc_mask
, xsrc1
, ysrc1
, SRCAND
);
1396 ::SetBkColor((HDC
) m_hDC
, prevBkCol
); // restore colours to original values
1397 ::SetTextColor((HDC
) m_hDC
, prevCol
);
1399 // OR buffer to dest
1400 success
= (::BitBlt((HDC
) m_hDC
, xdest1
, ydest1
, (int)width
, (int)height
,
1401 dc_buffer
, 0, 0, SRCPAINT
) != 0);
1403 // tidy up temporary DCs and bitmap
1404 ::SelectObject(dc_mask
, 0);
1405 ::DeleteDC(dc_mask
);
1406 ::SelectObject(dc_buffer
, 0);
1407 ::DeleteDC(dc_buffer
);
1408 ::DeleteObject(buffer_bmap
);
1413 success
= (BitBlt((HDC
) m_hDC
, xdest1
, ydest1
, (int)width
, (int)height
, (HDC
) source
->m_hDC
,
1414 xsrc1
, ysrc1
, dwRop
) != 0);
1416 ::SetTextColor((HDC
)m_hDC
, old_textground
);
1417 ::SetBkColor((HDC
)m_hDC
, old_background
);
1418 source
->EndDrawing();
1424 void wxDC::GetSize(int* width
, int* height
) const
1426 long w
=::GetDeviceCaps((HDC
) m_hDC
,HORZRES
);
1427 long h
=::GetDeviceCaps((HDC
) m_hDC
,VERTRES
);
1432 void wxDC::GetSizeMM(long *width
, long *height
) const
1434 long w
=::GetDeviceCaps((HDC
) m_hDC
,HORZSIZE
);
1435 long h
=::GetDeviceCaps((HDC
) m_hDC
,VERTSIZE
);
1442 # if USE_XFIG_SPLINE_CODE
1443 # include "../common/xfspline.inc"
1445 # include "../common/wxspline.inc"
1447 #endif // USE_SPLINES
1451 #include "xfspline.inc"
1452 #endif // USE_SPLINES
1454 void wxDC::DrawPolygon(wxList
*list
, long xoffset
, long yoffset
,int fillStyle
)
1456 int n
= list
->Number();
1457 wxPoint
*points
= new wxPoint
[n
];
1460 for(wxNode
*node
= list
->First(); node
; node
= node
->Next()) {
1461 wxPoint
*point
= (wxPoint
*)node
->Data();
1462 points
[i
].x
= point
->x
;
1463 points
[i
++].y
= point
->y
;
1465 DrawPolygon(n
, points
, xoffset
, yoffset
,fillStyle
);
1469 void wxDC::DrawLines(wxList
*list
, long xoffset
, long yoffset
)
1471 int n
= list
->Number();
1472 wxPoint
*points
= new wxPoint
[n
];
1475 for(wxNode
*node
= list
->First(); node
; node
= node
->Next()) {
1476 wxPoint
*point
= (wxPoint
*)node
->Data();
1477 points
[i
].x
= point
->x
;
1478 points
[i
++].y
= point
->y
;
1480 DrawLines(n
, points
, xoffset
, yoffset
);
1484 void wxDC::SetTextForeground(const wxColour
& colour
)
1486 m_textForegroundColour
= colour
;
1489 void wxDC::SetTextBackground(const wxColour
& colour
)
1491 m_textBackgroundColour
= colour
;
1495 // Make a 3-point spline
1496 void wxDC::DrawSpline(long x1
, long y1
, long x2
, long y2
, long x3
, long y3
)
1498 wxList
*point_list
= new wxList
;
1500 wxPoint
*point1
= new wxPoint
;
1501 point1
->x
= x1
; point1
->y
= y1
;
1502 point_list
->Append((wxObject
*)point1
);
1504 wxPoint
*point2
= new wxPoint
;
1505 point2
->x
= x2
; point2
->y
= y2
;
1506 point_list
->Append((wxObject
*)point2
);
1508 wxPoint
*point3
= new wxPoint
;
1509 point3
->x
= x3
; point3
->y
= y3
;
1510 point_list
->Append((wxObject
*)point3
);
1512 DrawSpline(point_list
);
1514 for(wxNode
*node
= point_list
->First(); node
; node
= node
->Next()) {
1515 wxPoint
*p
= (wxPoint
*)node
->Data();
1520 wxSpline spline(point_list);
1522 wx_draw_open_spline(this, &spline);
1523 spline.DeletePoints();
1528 // For use by wxWindows only, unless custom units are required.
1529 void wxDC::SetLogicalScale(double x
, double y
)
1531 m_logicalScaleX
= x
;
1532 m_logicalScaleY
= y
;
1535 void wxDC::CalcBoundingBox(long x
, long y
)
1537 if (x
< m_minX
) m_minX
= x
;
1538 if (y
< m_minY
) m_minY
= y
;
1539 if (x
> m_maxX
) m_maxX
= x
;
1540 if (y
> m_maxY
) m_maxY
= y
;
1543 void wxDC::GetClippingBox(long *x
,long *y
,long *w
,long *h
) const
1549 *w
= (m_clipX2
- m_clipX1
) ;
1550 *h
= (m_clipY2
- m_clipY1
) ;
1553 *x
= *y
= *w
= *h
= 0 ;
1556 #if WXWIN_COMPATIBILITY
1557 void wxDC::GetTextExtent(const wxString
& string
, float *x
, float *y
,
1558 float *descent
, float *externalLeading
,
1559 wxFont
*theFont
, bool use16bit
) const
1561 long x1
, y1
, descent1
, externalLeading1
;
1562 GetTextExtent(string
, & x1
, & y1
, & descent1
, & externalLeading1
, theFont
, use16bit
);
1565 *descent
= descent1
;
1566 if (externalLeading
)
1567 *externalLeading
= externalLeading1
;