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