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