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