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