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