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