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