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