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