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