]> git.saurik.com Git - wxWidgets.git/blob - src/msw/dc.cpp
Ok() should be called on image, not bitmap
[wxWidgets.git] / src / msw / dc.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: dc.cpp
3 // Purpose: wxDC class
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 01/02/97
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ===========================================================================
13 // declarations
14 // ===========================================================================
15
16 // ---------------------------------------------------------------------------
17 // headers
18 // ---------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "dc.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #ifndef WX_PRECOMP
32 #include "wx/window.h"
33 #include "wx/dc.h"
34 #include "wx/utils.h"
35 #include "wx/dialog.h"
36 #include "wx/app.h"
37 #include "wx/bitmap.h"
38 #include "wx/dcmemory.h"
39 #include "wx/log.h"
40 #include "wx/icon.h"
41 #endif
42
43 #include "wx/dcprint.h"
44
45 #include <string.h>
46 #include <math.h>
47
48 #include "wx/msw/private.h" // needs to be before #include <commdlg.h>
49
50 #if wxUSE_COMMON_DIALOGS
51 #include <commdlg.h>
52 #endif
53
54 #ifndef __WIN32__
55 #include <print.h>
56 #endif
57
58 IMPLEMENT_ABSTRACT_CLASS(wxDC, wxDCBase)
59
60 // ---------------------------------------------------------------------------
61 // constants
62 // ---------------------------------------------------------------------------
63
64 static const int VIEWPORT_EXTENT = 1000;
65
66 static const int MM_POINTS = 9;
67 static const int MM_METRIC = 10;
68
69 // usually this is defined in math.h
70 #ifndef M_PI
71 static const double M_PI = 3.14159265358979323846;
72 #endif // M_PI
73
74 // ROPs which don't have standard names (see "Ternary Raster Operations" in the
75 // MSDN docs for how this and other numbers in wxDC::Blit() are obtained)
76 #define DSTCOPY 0x00AA0029 // a.k.a. NOP operation
77
78 // ---------------------------------------------------------------------------
79 // private functions
80 // ---------------------------------------------------------------------------
81
82 // convert degrees to radians
83 static inline double DegToRad(double deg) { return (deg * M_PI) / 180.0; }
84
85 // ----------------------------------------------------------------------------
86 // private classes
87 // ----------------------------------------------------------------------------
88
89 // instead of duplicating the same code which sets and then restores text
90 // colours in each wxDC method working with wxSTIPPLE_MASK_OPAQUE brushes,
91 // encapsulate this in a small helper class
92
93 // wxColourChanger: changes the text colours in the ctor if required and
94 // restores them in the dtor
95 class wxColourChanger
96 {
97 public:
98 wxColourChanger(wxDC& dc);
99 ~wxColourChanger();
100
101 private:
102 wxDC& m_dc;
103
104 COLORREF m_colFgOld, m_colBgOld;
105
106 bool m_changed;
107 };
108
109 // ===========================================================================
110 // implementation
111 // ===========================================================================
112
113 // ----------------------------------------------------------------------------
114 // wxColourChanger
115 // ----------------------------------------------------------------------------
116
117 wxColourChanger::wxColourChanger(wxDC& dc) : m_dc(dc)
118 {
119 if ( dc.GetBrush().GetStyle() == wxSTIPPLE_MASK_OPAQUE )
120 {
121 HDC hdc = GetHdcOf(dc);
122 m_colFgOld = ::GetTextColor(hdc);
123 m_colBgOld = ::GetBkColor(hdc);
124
125 // note that Windows convention is opposite to wxWindows one, this is
126 // why text colour becomes the background one and vice versa
127 const wxColour& colFg = dc.GetTextForeground();
128 if ( colFg.Ok() )
129 {
130 ::SetBkColor(hdc, colFg.GetPixel());
131 }
132
133 const wxColour& colBg = dc.GetTextBackground();
134 if ( colBg.Ok() )
135 {
136 ::SetTextColor(hdc, colBg.GetPixel());
137 }
138
139 SetBkMode(hdc,
140 dc.GetBackgroundMode() == wxTRANSPARENT ? TRANSPARENT
141 : OPAQUE);
142
143 // flag which telsl us to undo changes in the dtor
144 m_changed = TRUE;
145 }
146 else
147 {
148 // nothing done, nothing to undo
149 m_changed = FALSE;
150 }
151 }
152
153 wxColourChanger::~wxColourChanger()
154 {
155 if ( m_changed )
156 {
157 // restore the colours we changed
158 HDC hdc = GetHdcOf(m_dc);
159
160 ::SetBkMode(hdc, TRANSPARENT);
161 ::SetTextColor(hdc, m_colFgOld);
162 ::SetBkColor(hdc, m_colBgOld);
163 }
164 }
165
166 // ---------------------------------------------------------------------------
167 // wxDC
168 // ---------------------------------------------------------------------------
169
170 // Default constructor
171 wxDC::wxDC()
172 {
173 m_canvas = NULL;
174
175 m_oldBitmap = 0;
176 m_oldPen = 0;
177 m_oldBrush = 0;
178 m_oldFont = 0;
179 m_oldPalette = 0;
180
181 m_bOwnsDC = FALSE;
182 m_hDC = 0;
183
184 m_windowExtX = VIEWPORT_EXTENT;
185 m_windowExtY = VIEWPORT_EXTENT;
186 }
187
188
189 wxDC::~wxDC()
190 {
191 if ( m_hDC != 0 )
192 {
193 SelectOldObjects(m_hDC);
194
195 // if we own the HDC, we delete it, otherwise we just release it
196
197 if ( m_bOwnsDC )
198 {
199 ::DeleteDC(GetHdc());
200 }
201 else // we don't own our HDC
202 {
203 // this is not supposed to happen as we can't free the HDC then
204 wxCHECK_RET( m_canvas, _T("no canvas in not owning ~wxDC?") );
205
206 ::ReleaseDC(GetHwndOf(m_canvas), GetHdc());
207 }
208 }
209 }
210
211 // This will select current objects out of the DC,
212 // which is what you have to do before deleting the
213 // DC.
214 void wxDC::SelectOldObjects(WXHDC dc)
215 {
216 if (dc)
217 {
218 if (m_oldBitmap)
219 {
220 ::SelectObject((HDC) dc, (HBITMAP) m_oldBitmap);
221 if (m_selectedBitmap.Ok())
222 {
223 m_selectedBitmap.SetSelectedInto(NULL);
224 }
225 }
226 m_oldBitmap = 0;
227 if (m_oldPen)
228 {
229 ::SelectObject((HDC) dc, (HPEN) m_oldPen);
230 }
231 m_oldPen = 0;
232 if (m_oldBrush)
233 {
234 ::SelectObject((HDC) dc, (HBRUSH) m_oldBrush);
235 }
236 m_oldBrush = 0;
237 if (m_oldFont)
238 {
239 ::SelectObject((HDC) dc, (HFONT) m_oldFont);
240 }
241 m_oldFont = 0;
242 if (m_oldPalette)
243 {
244 ::SelectPalette((HDC) dc, (HPALETTE) m_oldPalette, TRUE);
245 }
246 m_oldPalette = 0;
247 }
248
249 m_brush = wxNullBrush;
250 m_pen = wxNullPen;
251 m_palette = wxNullPalette;
252 m_font = wxNullFont;
253 m_backgroundBrush = wxNullBrush;
254 m_selectedBitmap = wxNullBitmap;
255 }
256
257 // ---------------------------------------------------------------------------
258 // clipping
259 // ---------------------------------------------------------------------------
260
261 #define DO_SET_CLIPPING_BOX() \
262 { \
263 RECT rect; \
264 \
265 GetClipBox(GetHdc(), &rect); \
266 \
267 m_clipX1 = (wxCoord) XDEV2LOG(rect.left); \
268 m_clipY1 = (wxCoord) YDEV2LOG(rect.top); \
269 m_clipX2 = (wxCoord) XDEV2LOG(rect.right); \
270 m_clipY2 = (wxCoord) YDEV2LOG(rect.bottom); \
271 }
272
273 void wxDC::DoSetClippingRegion(wxCoord cx, wxCoord cy, wxCoord cw, wxCoord ch)
274 {
275 m_clipping = TRUE;
276 IntersectClipRect(GetHdc(), XLOG2DEV(cx), YLOG2DEV(cy),
277 XLOG2DEV(cx + cw), YLOG2DEV(cy + ch));
278 DO_SET_CLIPPING_BOX()
279 }
280
281 void wxDC::DoSetClippingRegionAsRegion(const wxRegion& region)
282 {
283 wxCHECK_RET( region.GetHRGN(), wxT("invalid clipping region") );
284
285 m_clipping = TRUE;
286
287 #ifdef __WIN16__
288 SelectClipRgn(GetHdc(), (HRGN) region.GetHRGN());
289 #else
290 ExtSelectClipRgn(GetHdc(), (HRGN) region.GetHRGN(), RGN_AND);
291 #endif
292
293 DO_SET_CLIPPING_BOX()
294 }
295
296 void wxDC::DestroyClippingRegion()
297 {
298 if (m_clipping && m_hDC)
299 {
300 // TODO: this should restore the previous clipping region,
301 // so that OnPaint processing works correctly, and the update clipping region
302 // doesn't get destroyed after the first DestroyClippingRegion.
303 HRGN rgn = CreateRectRgn(0, 0, 32000, 32000);
304 SelectClipRgn(GetHdc(), rgn);
305 DeleteObject(rgn);
306 }
307 m_clipping = FALSE;
308 }
309
310 // ---------------------------------------------------------------------------
311 // query capabilities
312 // ---------------------------------------------------------------------------
313
314 bool wxDC::CanDrawBitmap() const
315 {
316 return TRUE;
317 }
318
319 bool wxDC::CanGetTextExtent() const
320 {
321 // What sort of display is it?
322 int technology = ::GetDeviceCaps(GetHdc(), TECHNOLOGY);
323
324 return (technology == DT_RASDISPLAY) || (technology == DT_RASPRINTER);
325 }
326
327 int wxDC::GetDepth() const
328 {
329 return (int)::GetDeviceCaps(GetHdc(), BITSPIXEL);
330 }
331
332 // ---------------------------------------------------------------------------
333 // drawing
334 // ---------------------------------------------------------------------------
335
336 void wxDC::Clear()
337 {
338 RECT rect;
339 if ( m_canvas )
340 {
341 GetClientRect((HWND) m_canvas->GetHWND(), &rect);
342 }
343 else
344 {
345 // No, I think we should simply ignore this if printing on e.g.
346 // a printer DC.
347 // wxCHECK_RET( m_selectedBitmap.Ok(), wxT("this DC can't be cleared") );
348 if (!m_selectedBitmap.Ok())
349 return;
350
351 rect.left = 0; rect.top = 0;
352 rect.right = m_selectedBitmap.GetWidth();
353 rect.bottom = m_selectedBitmap.GetHeight();
354 }
355
356 (void) ::SetMapMode(GetHdc(), MM_TEXT);
357
358 DWORD colour = GetBkColor(GetHdc());
359 HBRUSH brush = CreateSolidBrush(colour);
360 FillRect(GetHdc(), &rect, brush);
361 DeleteObject(brush);
362
363 ::SetMapMode(GetHdc(), MM_ANISOTROPIC);
364 ::SetViewportExtEx(GetHdc(), VIEWPORT_EXTENT, VIEWPORT_EXTENT, NULL);
365 ::SetWindowExtEx(GetHdc(), m_windowExtX, m_windowExtY, NULL);
366 ::SetViewportOrgEx(GetHdc(), (int)m_deviceOriginX, (int)m_deviceOriginY, NULL);
367 ::SetWindowOrgEx(GetHdc(), (int)m_logicalOriginX, (int)m_logicalOriginY, NULL);
368 }
369
370 void wxDC::DoFloodFill(wxCoord x, wxCoord y, const wxColour& col, int style)
371 {
372 if ( !::ExtFloodFill(GetHdc(), XLOG2DEV(x), YLOG2DEV(y),
373 col.GetPixel(),
374 style == wxFLOOD_SURFACE ? FLOODFILLSURFACE
375 : FLOODFILLBORDER) )
376 {
377 // quoting from the MSDN docs:
378 //
379 // Following are some of the reasons this function might fail:
380 //
381 // * The filling could not be completed.
382 // * The specified point has the boundary color specified by the
383 // crColor parameter (if FLOODFILLBORDER was requested).
384 // * The specified point does not have the color specified by
385 // crColor (if FLOODFILLSURFACE was requested)
386 // * The point is outside the clipping region that is, it is not
387 // visible on the device.
388 //
389 wxLogLastError(wxT("ExtFloodFill"));
390 }
391
392 CalcBoundingBox(x, y);
393 }
394
395 bool wxDC::DoGetPixel(wxCoord x, wxCoord y, wxColour *col) const
396 {
397 wxCHECK_MSG( col, FALSE, _T("NULL colour parameter in wxDC::GetPixel") );
398
399 // get the color of the pixel
400 COLORREF pixelcolor = ::GetPixel(GetHdc(), XLOG2DEV(x), YLOG2DEV(y));
401
402 wxRGBToColour(*col, pixelcolor);
403
404 return TRUE;
405 }
406
407 void wxDC::DoCrossHair(wxCoord x, wxCoord y)
408 {
409 wxCoord x1 = x-VIEWPORT_EXTENT;
410 wxCoord y1 = y-VIEWPORT_EXTENT;
411 wxCoord x2 = x+VIEWPORT_EXTENT;
412 wxCoord y2 = y+VIEWPORT_EXTENT;
413
414 (void)MoveToEx(GetHdc(), XLOG2DEV(x1), YLOG2DEV(y), NULL);
415 (void)LineTo(GetHdc(), XLOG2DEV(x2), YLOG2DEV(y));
416
417 (void)MoveToEx(GetHdc(), XLOG2DEV(x), YLOG2DEV(y1), NULL);
418 (void)LineTo(GetHdc(), XLOG2DEV(x), YLOG2DEV(y2));
419
420 CalcBoundingBox(x1, y1);
421 CalcBoundingBox(x2, y2);
422 }
423
424 void wxDC::DoDrawLine(wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2)
425 {
426 (void)MoveToEx(GetHdc(), XLOG2DEV(x1), YLOG2DEV(y1), NULL);
427 (void)LineTo(GetHdc(), XLOG2DEV(x2), YLOG2DEV(y2));
428
429 // Normalization: Windows doesn't draw the last point of the line.
430 // But apparently neither does GTK+, so we take it out again.
431 // (void)LineTo(GetHdc(), XLOG2DEV(x2) + 1, YLOG2DEV(y2));
432
433 CalcBoundingBox(x1, y1);
434 CalcBoundingBox(x2, y2);
435 }
436
437 // Draws an arc of a circle, centred on (xc, yc), with starting point (x1, y1)
438 // and ending at (x2, y2)
439 void wxDC::DoDrawArc(wxCoord x1, wxCoord y1,
440 wxCoord x2, wxCoord y2,
441 wxCoord xc, wxCoord yc)
442 {
443 wxColourChanger cc(*this); // needed for wxSTIPPLE_MASK_OPAQUE handling
444
445 double dx = xc - x1;
446 double dy = yc - y1;
447 double radius = (double)sqrt(dx*dx+dy*dy);
448 wxCoord r = (wxCoord)radius;
449
450 // treat the special case of full circle separately
451 if ( x1 == x2 && y1 == y2 )
452 {
453 DrawEllipse(xc - r, yc - r, 2*r, 2*r);
454 return;
455 }
456
457 wxCoord xx1 = XLOG2DEV(x1);
458 wxCoord yy1 = YLOG2DEV(y1);
459 wxCoord xx2 = XLOG2DEV(x2);
460 wxCoord yy2 = YLOG2DEV(y2);
461 wxCoord xxc = XLOG2DEV(xc);
462 wxCoord yyc = YLOG2DEV(yc);
463 wxCoord ray = (wxCoord) sqrt(double((xxc-xx1)*(xxc-xx1)+(yyc-yy1)*(yyc-yy1)));
464
465 wxCoord xxx1 = (wxCoord) (xxc-ray);
466 wxCoord yyy1 = (wxCoord) (yyc-ray);
467 wxCoord xxx2 = (wxCoord) (xxc+ray);
468 wxCoord yyy2 = (wxCoord) (yyc+ray);
469
470 if ( m_brush.Ok() && m_brush.GetStyle() != wxTRANSPARENT )
471 {
472 // Have to add 1 to bottom-right corner of rectangle
473 // to make semi-circles look right (crooked line otherwise).
474 // Unfortunately this is not a reliable method, depends
475 // on the size of shape.
476 // TODO: figure out why this happens!
477 Pie(GetHdc(),xxx1,yyy1,xxx2+1,yyy2+1, xx1,yy1,xx2,yy2);
478 }
479 else
480 {
481 Arc(GetHdc(),xxx1,yyy1,xxx2,yyy2, xx1,yy1,xx2,yy2);
482 }
483
484 CalcBoundingBox(xc - r, yc - r);
485 CalcBoundingBox(xc + r, yc + r);
486 }
487
488 void wxDC::DoDrawCheckMark(wxCoord x1, wxCoord y1,
489 wxCoord width, wxCoord height)
490 {
491 wxCoord x2 = x1 + width,
492 y2 = y1 + height;
493
494 #if defined(__WIN32__) && !defined(__SC__)
495 RECT rect;
496 rect.left = x1;
497 rect.top = y1;
498 rect.right = x2;
499 rect.bottom = y2;
500
501 DrawFrameControl(GetHdc(), &rect, DFC_MENU, DFCS_MENUCHECK);
502 #else // Win16
503 // In WIN16, draw a cross
504 HPEN blackPen = ::CreatePen(PS_SOLID, 1, RGB(0, 0, 0));
505 HPEN whiteBrush = (HPEN)::GetStockObject(WHITE_BRUSH);
506 HPEN hPenOld = (HPEN)::SelectObject(GetHdc(), blackPen);
507 HPEN hBrushOld = (HPEN)::SelectObject(GetHdc(), whiteBrush);
508 ::SetROP2(GetHdc(), R2_COPYPEN);
509 Rectangle(GetHdc(), x1, y1, x2, y2);
510 MoveTo(GetHdc(), x1, y1);
511 LineTo(GetHdc(), x2, y2);
512 MoveTo(GetHdc(), x2, y1);
513 LineTo(GetHdc(), x1, y2);
514 ::SelectObject(GetHdc(), hPenOld);
515 ::SelectObject(GetHdc(), hBrushOld);
516 ::DeleteObject(blackPen);
517 #endif // Win32/16
518
519 CalcBoundingBox(x1, y1);
520 CalcBoundingBox(x2, y2);
521 }
522
523 void wxDC::DoDrawPoint(wxCoord x, wxCoord y)
524 {
525 COLORREF color = 0x00ffffff;
526 if (m_pen.Ok())
527 {
528 color = m_pen.GetColour().GetPixel();
529 }
530
531 SetPixel(GetHdc(), XLOG2DEV(x), YLOG2DEV(y), color);
532
533 CalcBoundingBox(x, y);
534 }
535
536 void wxDC::DoDrawPolygon(int n, wxPoint points[], wxCoord xoffset, wxCoord yoffset,int fillStyle)
537 {
538 wxColourChanger cc(*this); // needed for wxSTIPPLE_MASK_OPAQUE handling
539
540 // Do things less efficiently if we have offsets
541 if (xoffset != 0 || yoffset != 0)
542 {
543 POINT *cpoints = new POINT[n];
544 int i;
545 for (i = 0; i < n; i++)
546 {
547 cpoints[i].x = (int)(points[i].x + xoffset);
548 cpoints[i].y = (int)(points[i].y + yoffset);
549
550 CalcBoundingBox(cpoints[i].x, cpoints[i].y);
551 }
552 int prev = SetPolyFillMode(GetHdc(),fillStyle==wxODDEVEN_RULE?ALTERNATE:WINDING);
553 (void)Polygon(GetHdc(), cpoints, n);
554 SetPolyFillMode(GetHdc(),prev);
555 delete[] cpoints;
556 }
557 else
558 {
559 int i;
560 for (i = 0; i < n; i++)
561 CalcBoundingBox(points[i].x, points[i].y);
562
563 int prev = SetPolyFillMode(GetHdc(),fillStyle==wxODDEVEN_RULE?ALTERNATE:WINDING);
564 (void)Polygon(GetHdc(), (POINT*) points, n);
565 SetPolyFillMode(GetHdc(),prev);
566 }
567 }
568
569 void wxDC::DoDrawLines(int n, wxPoint points[], wxCoord xoffset, wxCoord yoffset)
570 {
571 // Do things less efficiently if we have offsets
572 if (xoffset != 0 || yoffset != 0)
573 {
574 POINT *cpoints = new POINT[n];
575 int i;
576 for (i = 0; i < n; i++)
577 {
578 cpoints[i].x = (int)(points[i].x + xoffset);
579 cpoints[i].y = (int)(points[i].y + yoffset);
580
581 CalcBoundingBox(cpoints[i].x, cpoints[i].y);
582 }
583 (void)Polyline(GetHdc(), cpoints, n);
584 delete[] cpoints;
585 }
586 else
587 {
588 int i;
589 for (i = 0; i < n; i++)
590 CalcBoundingBox(points[i].x, points[i].y);
591
592 (void)Polyline(GetHdc(), (POINT*) points, n);
593 }
594 }
595
596 void wxDC::DoDrawRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
597 {
598 wxColourChanger cc(*this); // needed for wxSTIPPLE_MASK_OPAQUE handling
599
600 wxCoord x2 = x + width;
601 wxCoord y2 = y + height;
602
603 if ((m_logicalFunction == wxCOPY) && (m_pen.GetStyle() == wxTRANSPARENT))
604 {
605 RECT rect;
606 rect.left = XLOG2DEV(x);
607 rect.top = YLOG2DEV(y);
608 rect.right = XLOG2DEV(x2);
609 rect.bottom = YLOG2DEV(y2);
610 (void)FillRect(GetHdc(), &rect, (HBRUSH)m_brush.GetResourceHandle() );
611 }
612 else
613 {
614 // Windows draws the filled rectangles without outline (i.e. drawn with a
615 // transparent pen) one pixel smaller in both directions and we want them
616 // to have the same size regardless of which pen is used - adjust
617
618 // I wonder if this shouldn´t be done after the LOG2DEV() conversions. RR.
619 if ( m_pen.GetStyle() == wxTRANSPARENT )
620 {
621 x2++;
622 y2++;
623 }
624
625 (void)Rectangle(GetHdc(), XLOG2DEV(x), YLOG2DEV(y), XLOG2DEV(x2), YLOG2DEV(y2));
626 }
627
628
629 CalcBoundingBox(x, y);
630 CalcBoundingBox(x2, y2);
631 }
632
633 void wxDC::DoDrawRoundedRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height, double radius)
634 {
635 wxColourChanger cc(*this); // needed for wxSTIPPLE_MASK_OPAQUE handling
636
637 // Now, a negative radius value is interpreted to mean
638 // 'the proportion of the smallest X or Y dimension'
639
640 if (radius < 0.0)
641 {
642 double smallest = 0.0;
643 if (width < height)
644 smallest = width;
645 else
646 smallest = height;
647 radius = (- radius * smallest);
648 }
649
650 wxCoord x2 = (x+width);
651 wxCoord y2 = (y+height);
652
653 // Windows draws the filled rectangles without outline (i.e. drawn with a
654 // transparent pen) one pixel smaller in both directions and we want them
655 // to have the same size regardless of which pen is used - adjust
656 if ( m_pen.GetStyle() == wxTRANSPARENT )
657 {
658 x2++;
659 y2++;
660 }
661
662 (void)RoundRect(GetHdc(), XLOG2DEV(x), YLOG2DEV(y), XLOG2DEV(x2),
663 YLOG2DEV(y2), (int) (2*XLOG2DEV(radius)), (int)( 2*YLOG2DEV(radius)));
664
665 CalcBoundingBox(x, y);
666 CalcBoundingBox(x2, y2);
667 }
668
669 void wxDC::DoDrawEllipse(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
670 {
671 wxColourChanger cc(*this); // needed for wxSTIPPLE_MASK_OPAQUE handling
672
673 wxCoord x2 = (x+width);
674 wxCoord y2 = (y+height);
675
676 (void)Ellipse(GetHdc(), XLOG2DEV(x), YLOG2DEV(y), XLOG2DEV(x2), YLOG2DEV(y2));
677
678 CalcBoundingBox(x, y);
679 CalcBoundingBox(x2, y2);
680 }
681
682 // Chris Breeze 20/5/98: first implementation of DrawEllipticArc on Windows
683 void wxDC::DoDrawEllipticArc(wxCoord x,wxCoord y,wxCoord w,wxCoord h,double sa,double ea)
684 {
685 wxColourChanger cc(*this); // needed for wxSTIPPLE_MASK_OPAQUE handling
686
687 wxCoord x2 = x + w;
688 wxCoord y2 = y + h;
689
690 int rx1 = XLOG2DEV(x+w/2);
691 int ry1 = YLOG2DEV(y+h/2);
692 int rx2 = rx1;
693 int ry2 = ry1;
694
695 sa = DegToRad(sa);
696 ea = DegToRad(ea);
697
698 rx1 += (int)(100.0 * abs(w) * cos(sa));
699 ry1 -= (int)(100.0 * abs(h) * m_signY * sin(sa));
700 rx2 += (int)(100.0 * abs(w) * cos(ea));
701 ry2 -= (int)(100.0 * abs(h) * m_signY * sin(ea));
702
703 // draw pie with NULL_PEN first and then outline otherwise a line is
704 // drawn from the start and end points to the centre
705 HPEN hpenOld = (HPEN) ::SelectObject(GetHdc(), (HPEN) ::GetStockObject(NULL_PEN));
706 if (m_signY > 0)
707 {
708 (void)Pie(GetHdc(), XLOG2DEV(x), YLOG2DEV(y), XLOG2DEV(x2)+1, YLOG2DEV(y2)+1,
709 rx1, ry1, rx2, ry2);
710 }
711 else
712 {
713 (void)Pie(GetHdc(), XLOG2DEV(x), YLOG2DEV(y)-1, XLOG2DEV(x2)+1, YLOG2DEV(y2),
714 rx1, ry1-1, rx2, ry2-1);
715 }
716
717 ::SelectObject(GetHdc(), hpenOld);
718
719 (void)Arc(GetHdc(), XLOG2DEV(x), YLOG2DEV(y), XLOG2DEV(x2), YLOG2DEV(y2),
720 rx1, ry1, rx2, ry2);
721
722 CalcBoundingBox(x, y);
723 CalcBoundingBox(x2, y2);
724 }
725
726 void wxDC::DoDrawIcon(const wxIcon& icon, wxCoord x, wxCoord y)
727 {
728 wxCHECK_RET( icon.Ok(), wxT("invalid icon in DrawIcon") );
729
730 #ifdef __WIN32__
731 ::DrawIconEx(GetHdc(), XLOG2DEV(x), YLOG2DEV(y), GetHiconOf(icon), icon.GetWidth(), icon.GetHeight(), 0, NULL, DI_NORMAL);
732 #else
733 ::DrawIcon(GetHdc(), XLOG2DEV(x), YLOG2DEV(y), GetHiconOf(icon));
734 #endif
735
736 CalcBoundingBox(x, y);
737 CalcBoundingBox(x + icon.GetWidth(), y + icon.GetHeight());
738 }
739
740 void wxDC::DoDrawBitmap( const wxBitmap &bmp, wxCoord x, wxCoord y, bool useMask )
741 {
742 wxCHECK_RET( bmp.Ok(), _T("invalid bitmap in wxDC::DrawBitmap") );
743
744 int width = bmp.GetWidth(),
745 height = bmp.GetHeight();
746
747 HBITMAP hbmpMask = 0;
748
749 if ( useMask )
750 {
751 wxMask *mask = bmp.GetMask();
752 if ( mask )
753 hbmpMask = (HBITMAP)mask->GetMaskBitmap();
754
755 if ( !hbmpMask )
756 {
757 // don't give assert here because this would break existing
758 // programs - just silently ignore useMask parameter
759 useMask = FALSE;
760 }
761 }
762
763 if ( useMask )
764 {
765 #ifdef __WIN32__
766 HDC hdcMem = ::CreateCompatibleDC(GetHdc());
767 ::SelectObject(hdcMem, GetHbitmapOf(bmp));
768
769 // use MaskBlt() with ROP which doesn't do anything to dst in the mask
770 // points
771 bool ok = ::MaskBlt(GetHdc(), x, y, width, height,
772 hdcMem, 0, 0,
773 hbmpMask, 0, 0,
774 MAKEROP4(SRCCOPY, DSTCOPY)) != 0;
775 ::DeleteDC(hdcMem);
776
777 if ( !ok )
778 #endif // Win32
779 {
780 // Rather than reproduce wxDC::Blit, let's do it at the wxWin API
781 // level
782 wxMemoryDC memDC;
783 memDC.SelectObject(bmp);
784
785 Blit(x, y, width, height, &memDC, 0, 0, wxCOPY, useMask);
786
787 memDC.SelectObject(wxNullBitmap);
788 }
789 }
790 else // no mask, just use BitBlt()
791 {
792 HDC cdc = GetHdc();
793 HDC memdc = ::CreateCompatibleDC( cdc );
794 HBITMAP hbitmap = (HBITMAP) bmp.GetHBITMAP( );
795
796 wxASSERT_MSG( hbitmap, wxT("bitmap is ok but HBITMAP is NULL?") );
797
798 COLORREF old_textground = ::GetTextColor(GetHdc());
799 COLORREF old_background = ::GetBkColor(GetHdc());
800 if (m_textForegroundColour.Ok())
801 {
802 ::SetTextColor(GetHdc(), m_textForegroundColour.GetPixel() );
803 }
804 if (m_textBackgroundColour.Ok())
805 {
806 ::SetBkColor(GetHdc(), m_textBackgroundColour.GetPixel() );
807 }
808
809 ::SelectObject( memdc, hbitmap );
810 ::BitBlt( cdc, x, y, width, height, memdc, 0, 0, SRCCOPY);
811 ::DeleteDC( memdc );
812
813 ::SetTextColor(GetHdc(), old_textground);
814 ::SetBkColor(GetHdc(), old_background);
815 }
816 }
817
818 void wxDC::DoDrawText(const wxString& text, wxCoord x, wxCoord y)
819 {
820 DrawAnyText(text, x, y);
821
822 // update the bounding box
823 CalcBoundingBox(x, y);
824
825 wxCoord w, h;
826 GetTextExtent(text, &w, &h);
827 CalcBoundingBox(x + w, y + h);
828 }
829
830 void wxDC::DrawAnyText(const wxString& text, wxCoord x, wxCoord y)
831 {
832 // prepare for drawing the text
833 if ( m_textForegroundColour.Ok() )
834 SetTextColor(GetHdc(), m_textForegroundColour.GetPixel());
835
836 DWORD old_background = 0;
837 if ( m_textBackgroundColour.Ok() )
838 {
839 old_background = SetBkColor(GetHdc(), m_textBackgroundColour.GetPixel() );
840 }
841
842 SetBkMode(GetHdc(), m_backgroundMode == wxTRANSPARENT ? TRANSPARENT
843 : OPAQUE);
844
845 if ( ::TextOut(GetHdc(), XLOG2DEV(x), YLOG2DEV(y),
846 text.c_str(), text.length()) == 0 )
847 {
848 wxLogLastError(wxT("TextOut"));
849 }
850
851 // restore the old parameters (text foreground colour may be left because
852 // it never is set to anything else, but background should remain
853 // transparent even if we just drew an opaque string)
854 if ( m_textBackgroundColour.Ok() )
855 (void)SetBkColor(GetHdc(), old_background);
856
857 SetBkMode(GetHdc(), TRANSPARENT);
858 }
859
860 void wxDC::DoDrawRotatedText(const wxString& text,
861 wxCoord x, wxCoord y,
862 double angle)
863 {
864 // we test that we have some font because otherwise we should still use the
865 // "else" part below to avoid that DrawRotatedText(angle = 180) and
866 // DrawRotatedText(angle = 0) use different fonts (we can't use the default
867 // font for drawing rotated fonts unfortunately)
868 if ( (angle == 0.0) && m_font.Ok() )
869 {
870 DoDrawText(text, x, y);
871 }
872 else
873 {
874 // NB: don't take DEFAULT_GUI_FONT because it's not TrueType and so
875 // can't have non zero orientation/escapement
876 wxFont font = m_font.Ok() ? m_font : *wxNORMAL_FONT;
877 HFONT hfont = (HFONT)font.GetResourceHandle();
878 LOGFONT lf;
879 if ( ::GetObject(hfont, sizeof(lf), &lf) == 0 )
880 {
881 wxLogLastError(wxT("GetObject(hfont)"));
882 }
883
884 // GDI wants the angle in tenth of degree
885 long angle10 = (long)(angle * 10);
886 lf.lfEscapement = angle10;
887 lf. lfOrientation = angle10;
888
889 hfont = ::CreateFontIndirect(&lf);
890 if ( !hfont )
891 {
892 wxLogLastError(wxT("CreateFont"));
893 }
894 else
895 {
896 HFONT hfontOld = (HFONT)::SelectObject(GetHdc(), hfont);
897
898 DrawAnyText(text, x, y);
899
900 (void)::SelectObject(GetHdc(), hfontOld);
901 (void)::DeleteObject(hfont);
902 }
903
904 // call the bounding box by adding all four vertices of the rectangle
905 // containing the text to it (simpler and probably not slower than
906 // determining which of them is really topmost/leftmost/...)
907 wxCoord w, h;
908 GetTextExtent(text, &w, &h);
909
910 double rad = DegToRad(angle);
911
912 // "upper left" and "upper right"
913 CalcBoundingBox(x, y);
914 CalcBoundingBox(x + w*cos(rad), y - h*sin(rad));
915
916 // "bottom left" and "bottom right"
917 x += (wxCoord)(h*sin(rad));
918 y += (wxCoord)(h*cos(rad));
919 CalcBoundingBox(x, y);
920 CalcBoundingBox(x + h*sin(rad), y + h*cos(rad));
921 }
922 }
923
924 // ---------------------------------------------------------------------------
925 // set GDI objects
926 // ---------------------------------------------------------------------------
927
928 void wxDC::SetPalette(const wxPalette& palette)
929 {
930 // Set the old object temporarily, in case the assignment deletes an object
931 // that's not yet selected out.
932 if (m_oldPalette)
933 {
934 ::SelectPalette(GetHdc(), (HPALETTE) m_oldPalette, TRUE);
935 m_oldPalette = 0;
936 }
937
938 m_palette = palette;
939
940 if (!m_palette.Ok())
941 {
942 // Setting a NULL colourmap is a way of restoring
943 // the original colourmap
944 if (m_oldPalette)
945 {
946 ::SelectPalette(GetHdc(), (HPALETTE) m_oldPalette, TRUE);
947 m_oldPalette = 0;
948 }
949
950 return;
951 }
952
953 if (m_palette.Ok() && m_palette.GetHPALETTE())
954 {
955 HPALETTE oldPal = ::SelectPalette(GetHdc(), (HPALETTE) m_palette.GetHPALETTE(), TRUE);
956 if (!m_oldPalette)
957 m_oldPalette = (WXHPALETTE) oldPal;
958
959 ::RealizePalette(GetHdc());
960 }
961 }
962
963 void wxDC::SetFont(const wxFont& the_font)
964 {
965 // Set the old object temporarily, in case the assignment deletes an object
966 // that's not yet selected out.
967 if (m_oldFont)
968 {
969 ::SelectObject(GetHdc(), (HFONT) m_oldFont);
970 m_oldFont = 0;
971 }
972
973 m_font = the_font;
974
975 if (!the_font.Ok())
976 {
977 if (m_oldFont)
978 ::SelectObject(GetHdc(), (HFONT) m_oldFont);
979 m_oldFont = 0;
980 }
981
982 if (m_font.Ok() && m_font.GetResourceHandle())
983 {
984 HFONT f = (HFONT) ::SelectObject(GetHdc(), (HFONT) m_font.GetResourceHandle());
985 if (f == (HFONT) NULL)
986 {
987 wxLogDebug(wxT("::SelectObject failed in wxDC::SetFont."));
988 }
989 if (!m_oldFont)
990 m_oldFont = (WXHFONT) f;
991 }
992 }
993
994 void wxDC::SetPen(const wxPen& pen)
995 {
996 // Set the old object temporarily, in case the assignment deletes an object
997 // that's not yet selected out.
998 if (m_oldPen)
999 {
1000 ::SelectObject(GetHdc(), (HPEN) m_oldPen);
1001 m_oldPen = 0;
1002 }
1003
1004 m_pen = pen;
1005
1006 if (!m_pen.Ok())
1007 {
1008 if (m_oldPen)
1009 ::SelectObject(GetHdc(), (HPEN) m_oldPen);
1010 m_oldPen = 0;
1011 }
1012
1013 if (m_pen.Ok())
1014 {
1015 if (m_pen.GetResourceHandle())
1016 {
1017 HPEN p = (HPEN) ::SelectObject(GetHdc(), (HPEN)m_pen.GetResourceHandle());
1018 if (!m_oldPen)
1019 m_oldPen = (WXHPEN) p;
1020 }
1021 }
1022 }
1023
1024 void wxDC::SetBrush(const wxBrush& brush)
1025 {
1026 // Set the old object temporarily, in case the assignment deletes an object
1027 // that's not yet selected out.
1028 if (m_oldBrush)
1029 {
1030 ::SelectObject(GetHdc(), (HBRUSH) m_oldBrush);
1031 m_oldBrush = 0;
1032 }
1033
1034 m_brush = brush;
1035
1036 if (!m_brush.Ok())
1037 {
1038 if (m_oldBrush)
1039 ::SelectObject(GetHdc(), (HBRUSH) m_oldBrush);
1040 m_oldBrush = 0;
1041 }
1042
1043 if (m_brush.Ok())
1044 {
1045 // to make sure the brush is alligned with the logical coordinates
1046 wxBitmap *stipple = m_brush.GetStipple();
1047 if ( stipple && stipple->Ok() )
1048 {
1049 #ifdef __WIN32__
1050 ::SetBrushOrgEx(GetHdc(),
1051 m_deviceOriginX % stipple->GetWidth(),
1052 m_deviceOriginY % stipple->GetHeight(),
1053 NULL); // don't need previous brush origin
1054 #else
1055 ::SetBrushOrg(GetHdc(),
1056 m_deviceOriginX % stipple->GetWidth(),
1057 m_deviceOriginY % stipple->GetHeight());
1058 #endif
1059 }
1060
1061 if ( m_brush.GetResourceHandle() )
1062 {
1063 HBRUSH b = 0;
1064 b = (HBRUSH) ::SelectObject(GetHdc(), (HBRUSH)m_brush.GetResourceHandle());
1065 if (!m_oldBrush)
1066 m_oldBrush = (WXHBRUSH) b;
1067 }
1068 }
1069 }
1070
1071 void wxDC::SetBackground(const wxBrush& brush)
1072 {
1073 m_backgroundBrush = brush;
1074
1075 if (!m_backgroundBrush.Ok())
1076 return;
1077
1078 if (m_canvas)
1079 {
1080 bool customColours = TRUE;
1081 // If we haven't specified wxUSER_COLOURS, don't allow the panel/dialog box to
1082 // change background colours from the control-panel specified colours.
1083 if (m_canvas->IsKindOf(CLASSINFO(wxWindow)) && ((m_canvas->GetWindowStyleFlag() & wxUSER_COLOURS) != wxUSER_COLOURS))
1084 customColours = FALSE;
1085
1086 if (customColours)
1087 {
1088 if (m_backgroundBrush.GetStyle()==wxTRANSPARENT)
1089 {
1090 m_canvas->SetTransparent(TRUE);
1091 }
1092 else
1093 {
1094 // New behaviour, 10/2/99: setting the background brush of a DC
1095 // doesn't affect the window background colour. However,
1096 // I'm leaving in the transparency setting because it's needed by
1097 // various controls (e.g. wxStaticText) to determine whether to draw
1098 // transparently or not. TODO: maybe this should be a new function
1099 // wxWindow::SetTransparency(). Should that apply to the child itself, or the
1100 // parent?
1101 // m_canvas->SetBackgroundColour(m_backgroundBrush.GetColour());
1102 m_canvas->SetTransparent(FALSE);
1103 }
1104 }
1105 }
1106 COLORREF new_color = m_backgroundBrush.GetColour().GetPixel();
1107 {
1108 (void)SetBkColor(GetHdc(), new_color);
1109 }
1110 }
1111
1112 void wxDC::SetBackgroundMode(int mode)
1113 {
1114 m_backgroundMode = mode;
1115
1116 // SetBackgroundColour now only refers to text background
1117 // and m_backgroundMode is used there
1118
1119 /*
1120 if (m_backgroundMode == wxTRANSPARENT)
1121 ::SetBkMode(GetHdc(), TRANSPARENT);
1122 else
1123 ::SetBkMode(GetHdc(), OPAQUE);
1124 Last change: AC 29 Jan 101 8:54 pm
1125 */
1126 }
1127
1128 void wxDC::SetLogicalFunction(int function)
1129 {
1130 m_logicalFunction = function;
1131
1132 SetRop(m_hDC);
1133 }
1134
1135 void wxDC::SetRop(WXHDC dc)
1136 {
1137 if ( !dc || m_logicalFunction < 0 )
1138 return;
1139
1140 int rop;
1141
1142 switch (m_logicalFunction)
1143 {
1144 case wxCLEAR: rop = R2_BLACK; break;
1145 case wxXOR: rop = R2_XORPEN; break;
1146 case wxINVERT: rop = R2_NOT; break;
1147 case wxOR_REVERSE: rop = R2_MERGEPENNOT; break;
1148 case wxAND_REVERSE: rop = R2_MASKPENNOT; break;
1149 case wxCOPY: rop = R2_COPYPEN; break;
1150 case wxAND: rop = R2_MASKPEN; break;
1151 case wxAND_INVERT: rop = R2_MASKNOTPEN; break;
1152 case wxNO_OP: rop = R2_NOP; break;
1153 case wxNOR: rop = R2_NOTMERGEPEN; break;
1154 case wxEQUIV: rop = R2_NOTXORPEN; break;
1155 case wxSRC_INVERT: rop = R2_NOTCOPYPEN; break;
1156 case wxOR_INVERT: rop = R2_MERGENOTPEN; break;
1157 case wxNAND: rop = R2_NOTMASKPEN; break;
1158 case wxOR: rop = R2_MERGEPEN; break;
1159 case wxSET: rop = R2_WHITE; break;
1160
1161 default:
1162 wxFAIL_MSG( wxT("unsupported logical function") );
1163 return;
1164 }
1165
1166 SetROP2(GetHdc(), rop);
1167 }
1168
1169 bool wxDC::StartDoc(const wxString& message)
1170 {
1171 // We might be previewing, so return TRUE to let it continue.
1172 return TRUE;
1173 }
1174
1175 void wxDC::EndDoc()
1176 {
1177 }
1178
1179 void wxDC::StartPage()
1180 {
1181 }
1182
1183 void wxDC::EndPage()
1184 {
1185 }
1186
1187 // ---------------------------------------------------------------------------
1188 // text metrics
1189 // ---------------------------------------------------------------------------
1190
1191 wxCoord wxDC::GetCharHeight() const
1192 {
1193 TEXTMETRIC lpTextMetric;
1194
1195 GetTextMetrics(GetHdc(), &lpTextMetric);
1196
1197 return YDEV2LOGREL(lpTextMetric.tmHeight);
1198 }
1199
1200 wxCoord wxDC::GetCharWidth() const
1201 {
1202 TEXTMETRIC lpTextMetric;
1203
1204 GetTextMetrics(GetHdc(), &lpTextMetric);
1205
1206 return XDEV2LOGREL(lpTextMetric.tmAveCharWidth);
1207 }
1208
1209 void wxDC::DoGetTextExtent(const wxString& string, wxCoord *x, wxCoord *y,
1210 wxCoord *descent, wxCoord *externalLeading,
1211 wxFont *theFont) const
1212 {
1213 wxFont *fontToUse = (wxFont*) theFont;
1214 if (!fontToUse)
1215 fontToUse = (wxFont*) &m_font;
1216
1217 SIZE sizeRect;
1218 TEXTMETRIC tm;
1219
1220 GetTextExtentPoint(GetHdc(), WXSTRINGCAST string, wxStrlen(WXSTRINGCAST string), &sizeRect);
1221 GetTextMetrics(GetHdc(), &tm);
1222
1223 if (x) *x = XDEV2LOGREL(sizeRect.cx);
1224 if (y) *y = YDEV2LOGREL(sizeRect.cy);
1225 if (descent) *descent = tm.tmDescent;
1226 if (externalLeading) *externalLeading = tm.tmExternalLeading;
1227 }
1228
1229 void wxDC::SetMapMode(int mode)
1230 {
1231 m_mappingMode = mode;
1232
1233 int pixel_width = 0;
1234 int pixel_height = 0;
1235 int mm_width = 0;
1236 int mm_height = 0;
1237
1238 pixel_width = GetDeviceCaps(GetHdc(), HORZRES);
1239 pixel_height = GetDeviceCaps(GetHdc(), VERTRES);
1240 mm_width = GetDeviceCaps(GetHdc(), HORZSIZE);
1241 mm_height = GetDeviceCaps(GetHdc(), VERTSIZE);
1242
1243 if ((pixel_width == 0) || (pixel_height == 0) || (mm_width == 0) || (mm_height == 0))
1244 {
1245 return;
1246 }
1247
1248 double mm2pixelsX = pixel_width/mm_width;
1249 double mm2pixelsY = pixel_height/mm_height;
1250
1251 switch (mode)
1252 {
1253 case wxMM_TWIPS:
1254 {
1255 m_logicalScaleX = (twips2mm * mm2pixelsX);
1256 m_logicalScaleY = (twips2mm * mm2pixelsY);
1257 break;
1258 }
1259 case wxMM_POINTS:
1260 {
1261 m_logicalScaleX = (pt2mm * mm2pixelsX);
1262 m_logicalScaleY = (pt2mm * mm2pixelsY);
1263 break;
1264 }
1265 case wxMM_METRIC:
1266 {
1267 m_logicalScaleX = mm2pixelsX;
1268 m_logicalScaleY = mm2pixelsY;
1269 break;
1270 }
1271 case wxMM_LOMETRIC:
1272 {
1273 m_logicalScaleX = (mm2pixelsX/10.0);
1274 m_logicalScaleY = (mm2pixelsY/10.0);
1275 break;
1276 }
1277 default:
1278 case wxMM_TEXT:
1279 {
1280 m_logicalScaleX = 1.0;
1281 m_logicalScaleY = 1.0;
1282 break;
1283 }
1284 }
1285
1286 if (::GetMapMode(GetHdc()) != MM_ANISOTROPIC)
1287 ::SetMapMode(GetHdc(), MM_ANISOTROPIC);
1288
1289 SetViewportExtEx(GetHdc(), VIEWPORT_EXTENT, VIEWPORT_EXTENT, NULL);
1290 m_windowExtX = (int)MS_XDEV2LOGREL(VIEWPORT_EXTENT);
1291 m_windowExtY = (int)MS_YDEV2LOGREL(VIEWPORT_EXTENT);
1292 ::SetWindowExtEx(GetHdc(), m_windowExtX, m_windowExtY, NULL);
1293 ::SetViewportOrgEx(GetHdc(), (int)m_deviceOriginX, (int)m_deviceOriginY, NULL);
1294 ::SetWindowOrgEx(GetHdc(), (int)m_logicalOriginX, (int)m_logicalOriginY, NULL);
1295 }
1296
1297 void wxDC::SetUserScale(double x, double y)
1298 {
1299 m_userScaleX = x;
1300 m_userScaleY = y;
1301
1302 SetMapMode(m_mappingMode);
1303 }
1304
1305 void wxDC::SetAxisOrientation(bool xLeftRight, bool yBottomUp)
1306 {
1307 m_signX = xLeftRight ? 1 : -1;
1308 m_signY = yBottomUp ? -1 : 1;
1309
1310 SetMapMode(m_mappingMode);
1311 }
1312
1313 void wxDC::SetSystemScale(double x, double y)
1314 {
1315 m_scaleX = x;
1316 m_scaleY = y;
1317
1318 SetMapMode(m_mappingMode);
1319 }
1320
1321 void wxDC::SetLogicalOrigin(wxCoord x, wxCoord y)
1322 {
1323 m_logicalOriginX = x;
1324 m_logicalOriginY = y;
1325
1326 ::SetWindowOrgEx(GetHdc(), (int)m_logicalOriginX, (int)m_logicalOriginY, NULL);
1327 }
1328
1329 void wxDC::SetDeviceOrigin(wxCoord x, wxCoord y)
1330 {
1331 m_deviceOriginX = x;
1332 m_deviceOriginY = y;
1333
1334 ::SetViewportOrgEx(GetHdc(), (int)m_deviceOriginX, (int)m_deviceOriginY, NULL);
1335 }
1336
1337 // ---------------------------------------------------------------------------
1338 // coordinates transformations
1339 // ---------------------------------------------------------------------------
1340
1341 wxCoord wxDCBase::DeviceToLogicalX(wxCoord x) const
1342 {
1343 double xRel = x - m_deviceOriginX;
1344 xRel /= m_logicalScaleX*m_userScaleX*m_signX*m_scaleX;
1345 return (wxCoord)(xRel + m_logicalOriginX);
1346 }
1347
1348 wxCoord wxDCBase::DeviceToLogicalXRel(wxCoord x) const
1349 {
1350 return (wxCoord) ((x)/(m_logicalScaleX*m_userScaleX*m_signX*m_scaleX));
1351 }
1352
1353 wxCoord wxDCBase::DeviceToLogicalY(wxCoord y) const
1354 {
1355 double yRel = y - m_deviceOriginY;
1356 yRel /= m_logicalScaleY*m_userScaleY*m_signY*m_scaleY;
1357 return (wxCoord)(yRel + m_logicalOriginY);
1358 }
1359
1360 wxCoord wxDCBase::DeviceToLogicalYRel(wxCoord y) const
1361 {
1362 return (wxCoord) ((y)/(m_logicalScaleY*m_userScaleY*m_signY*m_scaleY));
1363 }
1364
1365 wxCoord wxDCBase::LogicalToDeviceX(wxCoord x) const
1366 {
1367 return (wxCoord) ((x - m_logicalOriginX)*m_logicalScaleX*m_userScaleX*m_signX*m_scaleX + m_deviceOriginX);
1368 }
1369
1370 wxCoord wxDCBase::LogicalToDeviceXRel(wxCoord x) const
1371 {
1372 return (wxCoord) (x*m_logicalScaleX*m_userScaleX*m_signX*m_scaleX);
1373 }
1374
1375 wxCoord wxDCBase::LogicalToDeviceY(wxCoord y) const
1376 {
1377 return (wxCoord) ((y - m_logicalOriginY)*m_logicalScaleY*m_userScaleY*m_signY*m_scaleY + m_deviceOriginY);
1378 }
1379
1380 wxCoord wxDCBase::LogicalToDeviceYRel(wxCoord y) const
1381 {
1382 return (wxCoord) (y*m_logicalScaleY*m_userScaleY*m_signY*m_scaleY);
1383 }
1384
1385 // ---------------------------------------------------------------------------
1386 // bit blit
1387 // ---------------------------------------------------------------------------
1388
1389 bool wxDC::DoBlit(wxCoord xdest, wxCoord ydest,
1390 wxCoord width, wxCoord height,
1391 wxDC *source, wxCoord xsrc, wxCoord ysrc,
1392 int rop, bool useMask)
1393 {
1394 wxMask *mask = NULL;
1395 if ( useMask )
1396 {
1397 const wxBitmap& bmp = source->m_selectedBitmap;
1398 mask = bmp.GetMask();
1399
1400 if ( !(bmp.Ok() && mask && mask->GetMaskBitmap()) )
1401 {
1402 // don't give assert here because this would break existing
1403 // programs - just silently ignore useMask parameter
1404 useMask = FALSE;
1405 }
1406 }
1407
1408 COLORREF old_textground = ::GetTextColor(GetHdc());
1409 COLORREF old_background = ::GetBkColor(GetHdc());
1410 if (m_textForegroundColour.Ok())
1411 {
1412 ::SetTextColor(GetHdc(), m_textForegroundColour.GetPixel() );
1413 }
1414 if (m_textBackgroundColour.Ok())
1415 {
1416 ::SetBkColor(GetHdc(), m_textBackgroundColour.GetPixel() );
1417 }
1418
1419 DWORD dwRop = SRCCOPY;
1420 switch (rop)
1421 {
1422 case wxXOR: dwRop = SRCINVERT; break;
1423 case wxINVERT: dwRop = DSTINVERT; break;
1424 case wxOR_REVERSE: dwRop = 0x00DD0228; break;
1425 case wxAND_REVERSE: dwRop = SRCERASE; break;
1426 case wxCLEAR: dwRop = BLACKNESS; break;
1427 case wxSET: dwRop = WHITENESS; break;
1428 case wxOR_INVERT: dwRop = MERGEPAINT; break;
1429 case wxAND: dwRop = SRCAND; break;
1430 case wxOR: dwRop = SRCPAINT; break;
1431 case wxEQUIV: dwRop = 0x00990066; break;
1432 case wxNAND: dwRop = 0x007700E6; break;
1433 case wxAND_INVERT: dwRop = 0x00220326; break;
1434 case wxCOPY: dwRop = SRCCOPY; break;
1435 case wxNO_OP: dwRop = DSTCOPY; break;
1436 case wxSRC_INVERT: dwRop = NOTSRCCOPY; break;
1437 case wxNOR: dwRop = NOTSRCCOPY; break;
1438 default:
1439 wxFAIL_MSG( wxT("unsupported logical function") );
1440 return FALSE;
1441 }
1442
1443 bool success;
1444
1445 if (useMask)
1446 {
1447 #ifdef __WIN32__
1448 // we want the part of the image corresponding to the mask to be
1449 // transparent, so use "DSTCOPY" ROP for the mask points (the usual
1450 // meaning of fg and bg is inverted which corresponds to wxWin notion
1451 // of the mask which is also contrary to the Windows one)
1452 success = ::MaskBlt(GetHdc(), xdest, ydest, width, height,
1453 GetHdcOf(*source), xsrc, ysrc,
1454 (HBITMAP)mask->GetMaskBitmap(), xsrc, ysrc,
1455 MAKEROP4(dwRop, DSTCOPY)) != 0;
1456
1457 if ( !success )
1458 #endif // Win32
1459 {
1460 // Blit bitmap with mask
1461
1462 // create a temp buffer bitmap and DCs to access it and the mask
1463 HDC dc_mask = ::CreateCompatibleDC(GetHdcOf(*source));
1464 HDC dc_buffer = ::CreateCompatibleDC(GetHdc());
1465 HBITMAP buffer_bmap = ::CreateCompatibleBitmap(GetHdc(), width, height);
1466 ::SelectObject(dc_mask, (HBITMAP) mask->GetMaskBitmap());
1467 ::SelectObject(dc_buffer, buffer_bmap);
1468
1469 // copy dest to buffer
1470 if ( !::BitBlt(dc_buffer, 0, 0, (int)width, (int)height,
1471 GetHdc(), xdest, ydest, SRCCOPY) )
1472 {
1473 wxLogLastError(wxT("BitBlt"));
1474 }
1475
1476 // copy src to buffer using selected raster op
1477 if ( !::BitBlt(dc_buffer, 0, 0, (int)width, (int)height,
1478 GetHdcOf(*source), xsrc, ysrc, dwRop) )
1479 {
1480 wxLogLastError(wxT("BitBlt"));
1481 }
1482
1483 // set masked area in buffer to BLACK (pixel value 0)
1484 COLORREF prevBkCol = ::SetBkColor(GetHdc(), RGB(255, 255, 255));
1485 COLORREF prevCol = ::SetTextColor(GetHdc(), RGB(0, 0, 0));
1486 if ( !::BitBlt(dc_buffer, 0, 0, (int)width, (int)height,
1487 dc_mask, xsrc, ysrc, SRCAND) )
1488 {
1489 wxLogLastError(wxT("BitBlt"));
1490 }
1491
1492 // set unmasked area in dest to BLACK
1493 ::SetBkColor(GetHdc(), RGB(0, 0, 0));
1494 ::SetTextColor(GetHdc(), RGB(255, 255, 255));
1495 if ( !::BitBlt(GetHdc(), xdest, ydest, (int)width, (int)height,
1496 dc_mask, xsrc, ysrc, SRCAND) )
1497 {
1498 wxLogLastError(wxT("BitBlt"));
1499 }
1500 ::SetBkColor(GetHdc(), prevBkCol); // restore colours to original values
1501 ::SetTextColor(GetHdc(), prevCol);
1502
1503 // OR buffer to dest
1504 success = ::BitBlt(GetHdc(), xdest, ydest,
1505 (int)width, (int)height,
1506 dc_buffer, 0, 0, SRCPAINT) != 0;
1507 if ( !success )
1508 {
1509 wxLogLastError(wxT("BitBlt"));
1510 }
1511
1512 // tidy up temporary DCs and bitmap
1513 ::SelectObject(dc_mask, 0);
1514 ::DeleteDC(dc_mask);
1515 ::SelectObject(dc_buffer, 0);
1516 ::DeleteDC(dc_buffer);
1517 ::DeleteObject(buffer_bmap);
1518 }
1519 }
1520 else // no mask, just BitBlt() it
1521 {
1522 success = ::BitBlt(GetHdc(), xdest, ydest,
1523 (int)width, (int)height,
1524 GetHdcOf(*source), xsrc, ysrc, dwRop) != 0;
1525 if ( !success )
1526 {
1527 wxLogLastError(wxT("BitBlt"));
1528 }
1529 }
1530 ::SetTextColor(GetHdc(), old_textground);
1531 ::SetBkColor(GetHdc(), old_background);
1532
1533 return success;
1534 }
1535
1536 void wxDC::DoGetSize(int *w, int *h) const
1537 {
1538 if ( w ) *w = ::GetDeviceCaps(GetHdc(), HORZRES);
1539 if ( h ) *h = ::GetDeviceCaps(GetHdc(), VERTRES);
1540 }
1541
1542 void wxDC::DoGetSizeMM(int *w, int *h) const
1543 {
1544 if ( w ) *w = ::GetDeviceCaps(GetHdc(), HORZSIZE);
1545 if ( h ) *h = ::GetDeviceCaps(GetHdc(), VERTSIZE);
1546 }
1547
1548 wxSize wxDC::GetPPI() const
1549 {
1550 int x = ::GetDeviceCaps(GetHdc(), LOGPIXELSX);
1551 int y = ::GetDeviceCaps(GetHdc(), LOGPIXELSY);
1552
1553 return wxSize(x, y);
1554 }
1555
1556 // For use by wxWindows only, unless custom units are required.
1557 void wxDC::SetLogicalScale(double x, double y)
1558 {
1559 m_logicalScaleX = x;
1560 m_logicalScaleY = y;
1561 }
1562
1563 #if WXWIN_COMPATIBILITY
1564 void wxDC::DoGetTextExtent(const wxString& string, float *x, float *y,
1565 float *descent, float *externalLeading,
1566 wxFont *theFont, bool use16bit) const
1567 {
1568 wxCoord x1, y1, descent1, externalLeading1;
1569 GetTextExtent(string, & x1, & y1, & descent1, & externalLeading1, theFont, use16bit);
1570 *x = x1; *y = y1;
1571 if (descent)
1572 *descent = descent1;
1573 if (externalLeading)
1574 *externalLeading = externalLeading1;
1575 }
1576 #endif
1577
1578