]> git.saurik.com Git - wxWidgets.git/blob - src/msw/dc.cpp
allow multiple extensions in tge generic wxFileDialog (patch 457580)
[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 (a.k.a. wxSYS_DEFAULT_GUI_FONT)
1013 // because it's not TrueType and so can't have non zero
1014 // orientation/escapement under Win9x
1015 wxFont font = m_font.Ok() ? m_font : *wxSWISS_FONT;
1016 HFONT hfont = (HFONT)font.GetResourceHandle();
1017 LOGFONT lf;
1018 if ( ::GetObject(hfont, sizeof(lf), &lf) == 0 )
1019 {
1020 wxLogLastError(wxT("GetObject(hfont)"));
1021 }
1022
1023 // GDI wants the angle in tenth of degree
1024 long angle10 = (long)(angle * 10);
1025 lf.lfEscapement = angle10;
1026 lf. lfOrientation = angle10;
1027
1028 hfont = ::CreateFontIndirect(&lf);
1029 if ( !hfont )
1030 {
1031 wxLogLastError(wxT("CreateFont"));
1032 }
1033 else
1034 {
1035 HFONT hfontOld = (HFONT)::SelectObject(GetHdc(), hfont);
1036
1037 DrawAnyText(text, x, y);
1038
1039 (void)::SelectObject(GetHdc(), hfontOld);
1040 (void)::DeleteObject(hfont);
1041 }
1042
1043 // call the bounding box by adding all four vertices of the rectangle
1044 // containing the text to it (simpler and probably not slower than
1045 // determining which of them is really topmost/leftmost/...)
1046 wxCoord w, h;
1047 GetTextExtent(text, &w, &h);
1048
1049 double rad = DegToRad(angle);
1050
1051 // "upper left" and "upper right"
1052 CalcBoundingBox(x, y);
1053 CalcBoundingBox(x + w*cos(rad), y - h*sin(rad));
1054
1055 // "bottom left" and "bottom right"
1056 x += (wxCoord)(h*sin(rad));
1057 y += (wxCoord)(h*cos(rad));
1058 CalcBoundingBox(x, y);
1059 CalcBoundingBox(x + h*sin(rad), y + h*cos(rad));
1060 }
1061 #endif
1062 }
1063
1064 // ---------------------------------------------------------------------------
1065 // set GDI objects
1066 // ---------------------------------------------------------------------------
1067
1068 void wxDC::SetPalette(const wxPalette& palette)
1069 {
1070 #ifdef __WXMICROWIN__
1071 if (!GetHDC()) return;
1072 #endif
1073
1074 // Set the old object temporarily, in case the assignment deletes an object
1075 // that's not yet selected out.
1076 if (m_oldPalette)
1077 {
1078 ::SelectPalette(GetHdc(), (HPALETTE) m_oldPalette, TRUE);
1079 m_oldPalette = 0;
1080 }
1081
1082 m_palette = palette;
1083
1084 if (!m_palette.Ok())
1085 {
1086 // Setting a NULL colourmap is a way of restoring
1087 // the original colourmap
1088 if (m_oldPalette)
1089 {
1090 ::SelectPalette(GetHdc(), (HPALETTE) m_oldPalette, TRUE);
1091 m_oldPalette = 0;
1092 }
1093
1094 return;
1095 }
1096
1097 if (m_palette.Ok() && m_palette.GetHPALETTE())
1098 {
1099 HPALETTE oldPal = ::SelectPalette(GetHdc(), (HPALETTE) m_palette.GetHPALETTE(), TRUE);
1100 if (!m_oldPalette)
1101 m_oldPalette = (WXHPALETTE) oldPal;
1102
1103 ::RealizePalette(GetHdc());
1104 }
1105 }
1106
1107 void wxDC::SetFont(const wxFont& the_font)
1108 {
1109 #ifdef __WXMICROWIN__
1110 if (!GetHDC()) return;
1111 #endif
1112
1113 // Set the old object temporarily, in case the assignment deletes an object
1114 // that's not yet selected out.
1115 if (m_oldFont)
1116 {
1117 ::SelectObject(GetHdc(), (HFONT) m_oldFont);
1118 m_oldFont = 0;
1119 }
1120
1121 m_font = the_font;
1122
1123 if (!the_font.Ok())
1124 {
1125 if (m_oldFont)
1126 ::SelectObject(GetHdc(), (HFONT) m_oldFont);
1127 m_oldFont = 0;
1128 }
1129
1130 if (m_font.Ok() && m_font.GetResourceHandle())
1131 {
1132 HFONT f = (HFONT) ::SelectObject(GetHdc(), (HFONT) m_font.GetResourceHandle());
1133 if (f == (HFONT) NULL)
1134 {
1135 wxLogDebug(wxT("::SelectObject failed in wxDC::SetFont."));
1136 }
1137 if (!m_oldFont)
1138 m_oldFont = (WXHFONT) f;
1139 }
1140 }
1141
1142 void wxDC::SetPen(const wxPen& pen)
1143 {
1144 #ifdef __WXMICROWIN__
1145 if (!GetHDC()) return;
1146 #endif
1147
1148 // Set the old object temporarily, in case the assignment deletes an object
1149 // that's not yet selected out.
1150 if (m_oldPen)
1151 {
1152 ::SelectObject(GetHdc(), (HPEN) m_oldPen);
1153 m_oldPen = 0;
1154 }
1155
1156 m_pen = pen;
1157
1158 if (!m_pen.Ok())
1159 {
1160 if (m_oldPen)
1161 ::SelectObject(GetHdc(), (HPEN) m_oldPen);
1162 m_oldPen = 0;
1163 }
1164
1165 if (m_pen.Ok())
1166 {
1167 if (m_pen.GetResourceHandle())
1168 {
1169 HPEN p = (HPEN) ::SelectObject(GetHdc(), (HPEN)m_pen.GetResourceHandle());
1170 if (!m_oldPen)
1171 m_oldPen = (WXHPEN) p;
1172 }
1173 }
1174 }
1175
1176 void wxDC::SetBrush(const wxBrush& brush)
1177 {
1178 #ifdef __WXMICROWIN__
1179 if (!GetHDC()) return;
1180 #endif
1181
1182 // Set the old object temporarily, in case the assignment deletes an object
1183 // that's not yet selected out.
1184 if (m_oldBrush)
1185 {
1186 ::SelectObject(GetHdc(), (HBRUSH) m_oldBrush);
1187 m_oldBrush = 0;
1188 }
1189
1190 m_brush = brush;
1191
1192 if (!m_brush.Ok())
1193 {
1194 if (m_oldBrush)
1195 ::SelectObject(GetHdc(), (HBRUSH) m_oldBrush);
1196 m_oldBrush = 0;
1197 }
1198
1199 if (m_brush.Ok())
1200 {
1201 // to make sure the brush is alligned with the logical coordinates
1202 wxBitmap *stipple = m_brush.GetStipple();
1203 if ( stipple && stipple->Ok() )
1204 {
1205 #ifdef __WIN32__
1206 ::SetBrushOrgEx(GetHdc(),
1207 m_deviceOriginX % stipple->GetWidth(),
1208 m_deviceOriginY % stipple->GetHeight(),
1209 NULL); // don't need previous brush origin
1210 #else
1211 ::SetBrushOrg(GetHdc(),
1212 m_deviceOriginX % stipple->GetWidth(),
1213 m_deviceOriginY % stipple->GetHeight());
1214 #endif
1215 }
1216
1217 if ( m_brush.GetResourceHandle() )
1218 {
1219 HBRUSH b = 0;
1220 b = (HBRUSH) ::SelectObject(GetHdc(), (HBRUSH)m_brush.GetResourceHandle());
1221 if (!m_oldBrush)
1222 m_oldBrush = (WXHBRUSH) b;
1223 }
1224 }
1225 }
1226
1227 void wxDC::SetBackground(const wxBrush& brush)
1228 {
1229 #ifdef __WXMICROWIN__
1230 if (!GetHDC()) return;
1231 #endif
1232
1233 m_backgroundBrush = brush;
1234
1235 if (!m_backgroundBrush.Ok())
1236 return;
1237
1238 if (m_canvas)
1239 {
1240 bool customColours = TRUE;
1241 // If we haven't specified wxUSER_COLOURS, don't allow the panel/dialog box to
1242 // change background colours from the control-panel specified colours.
1243 if (m_canvas->IsKindOf(CLASSINFO(wxWindow)) && ((m_canvas->GetWindowStyleFlag() & wxUSER_COLOURS) != wxUSER_COLOURS))
1244 customColours = FALSE;
1245
1246 if (customColours)
1247 {
1248 if (m_backgroundBrush.GetStyle()==wxTRANSPARENT)
1249 {
1250 m_canvas->SetTransparent(TRUE);
1251 }
1252 else
1253 {
1254 // New behaviour, 10/2/99: setting the background brush of a DC
1255 // doesn't affect the window background colour. However,
1256 // I'm leaving in the transparency setting because it's needed by
1257 // various controls (e.g. wxStaticText) to determine whether to draw
1258 // transparently or not. TODO: maybe this should be a new function
1259 // wxWindow::SetTransparency(). Should that apply to the child itself, or the
1260 // parent?
1261 // m_canvas->SetBackgroundColour(m_backgroundBrush.GetColour());
1262 m_canvas->SetTransparent(FALSE);
1263 }
1264 }
1265 }
1266 COLORREF new_color = m_backgroundBrush.GetColour().GetPixel();
1267 {
1268 (void)SetBkColor(GetHdc(), new_color);
1269 }
1270 }
1271
1272 void wxDC::SetBackgroundMode(int mode)
1273 {
1274 #ifdef __WXMICROWIN__
1275 if (!GetHDC()) return;
1276 #endif
1277
1278 m_backgroundMode = mode;
1279
1280 // SetBackgroundColour now only refers to text background
1281 // and m_backgroundMode is used there
1282
1283 /*
1284 if (m_backgroundMode == wxTRANSPARENT)
1285 ::SetBkMode(GetHdc(), TRANSPARENT);
1286 else
1287 ::SetBkMode(GetHdc(), OPAQUE);
1288 Last change: AC 29 Jan 101 8:54 pm
1289 */
1290 }
1291
1292 void wxDC::SetLogicalFunction(int function)
1293 {
1294 #ifdef __WXMICROWIN__
1295 if (!GetHDC()) return;
1296 #endif
1297
1298 m_logicalFunction = function;
1299
1300 SetRop(m_hDC);
1301 }
1302
1303 void wxDC::SetRop(WXHDC dc)
1304 {
1305 if ( !dc || m_logicalFunction < 0 )
1306 return;
1307
1308 int rop;
1309
1310 switch (m_logicalFunction)
1311 {
1312 case wxCLEAR: rop = R2_BLACK; break;
1313 case wxXOR: rop = R2_XORPEN; break;
1314 case wxINVERT: rop = R2_NOT; break;
1315 case wxOR_REVERSE: rop = R2_MERGEPENNOT; break;
1316 case wxAND_REVERSE: rop = R2_MASKPENNOT; break;
1317 case wxCOPY: rop = R2_COPYPEN; break;
1318 case wxAND: rop = R2_MASKPEN; break;
1319 case wxAND_INVERT: rop = R2_MASKNOTPEN; break;
1320 case wxNO_OP: rop = R2_NOP; break;
1321 case wxNOR: rop = R2_NOTMERGEPEN; break;
1322 case wxEQUIV: rop = R2_NOTXORPEN; break;
1323 case wxSRC_INVERT: rop = R2_NOTCOPYPEN; break;
1324 case wxOR_INVERT: rop = R2_MERGENOTPEN; break;
1325 case wxNAND: rop = R2_NOTMASKPEN; break;
1326 case wxOR: rop = R2_MERGEPEN; break;
1327 case wxSET: rop = R2_WHITE; break;
1328
1329 default:
1330 wxFAIL_MSG( wxT("unsupported logical function") );
1331 return;
1332 }
1333
1334 SetROP2(GetHdc(), rop);
1335 }
1336
1337 bool wxDC::StartDoc(const wxString& WXUNUSED(message))
1338 {
1339 // We might be previewing, so return TRUE to let it continue.
1340 return TRUE;
1341 }
1342
1343 void wxDC::EndDoc()
1344 {
1345 }
1346
1347 void wxDC::StartPage()
1348 {
1349 }
1350
1351 void wxDC::EndPage()
1352 {
1353 }
1354
1355 // ---------------------------------------------------------------------------
1356 // text metrics
1357 // ---------------------------------------------------------------------------
1358
1359 wxCoord wxDC::GetCharHeight() const
1360 {
1361 #ifdef __WXMICROWIN__
1362 if (!GetHDC()) return 0;
1363 #endif
1364
1365 TEXTMETRIC lpTextMetric;
1366
1367 GetTextMetrics(GetHdc(), &lpTextMetric);
1368
1369 return YDEV2LOGREL(lpTextMetric.tmHeight);
1370 }
1371
1372 wxCoord wxDC::GetCharWidth() const
1373 {
1374 #ifdef __WXMICROWIN__
1375 if (!GetHDC()) return 0;
1376 #endif
1377
1378 TEXTMETRIC lpTextMetric;
1379
1380 GetTextMetrics(GetHdc(), &lpTextMetric);
1381
1382 return XDEV2LOGREL(lpTextMetric.tmAveCharWidth);
1383 }
1384
1385 void wxDC::DoGetTextExtent(const wxString& string, wxCoord *x, wxCoord *y,
1386 wxCoord *descent, wxCoord *externalLeading,
1387 wxFont *font) const
1388 {
1389 #ifdef __WXMICROWIN__
1390 if (!GetHDC())
1391 {
1392 if (x) *x = 0;
1393 if (y) *y = 0;
1394 if (descent) *descent = 0;
1395 if (externalLeading) *externalLeading = 0;
1396 return;
1397 }
1398 #endif
1399
1400 HFONT hfontOld;
1401 if ( font )
1402 {
1403 wxASSERT_MSG( font->Ok(), _T("invalid font in wxDC::GetTextExtent") );
1404
1405 hfontOld = (HFONT)::SelectObject(GetHdc(), GetHfontOf(*font));
1406 }
1407 else // don't change the font
1408 {
1409 hfontOld = 0;
1410 }
1411
1412 SIZE sizeRect;
1413 TEXTMETRIC tm;
1414
1415 GetTextExtentPoint(GetHdc(), string, string.length(), &sizeRect);
1416 GetTextMetrics(GetHdc(), &tm);
1417
1418 if (x) *x = XDEV2LOGREL(sizeRect.cx);
1419 if (y) *y = YDEV2LOGREL(sizeRect.cy);
1420 if (descent) *descent = tm.tmDescent;
1421 if (externalLeading) *externalLeading = tm.tmExternalLeading;
1422
1423 if ( hfontOld )
1424 {
1425 ::SelectObject(GetHdc(), hfontOld);
1426 }
1427 }
1428
1429 void wxDC::SetMapMode(int mode)
1430 {
1431 #ifdef __WXMICROWIN__
1432 if (!GetHDC()) return;
1433 #endif
1434
1435 m_mappingMode = mode;
1436
1437 int pixel_width = 0;
1438 int pixel_height = 0;
1439 int mm_width = 0;
1440 int mm_height = 0;
1441
1442 pixel_width = GetDeviceCaps(GetHdc(), HORZRES);
1443 pixel_height = GetDeviceCaps(GetHdc(), VERTRES);
1444 mm_width = GetDeviceCaps(GetHdc(), HORZSIZE);
1445 mm_height = GetDeviceCaps(GetHdc(), VERTSIZE);
1446
1447 if ((pixel_width == 0) || (pixel_height == 0) || (mm_width == 0) || (mm_height == 0))
1448 {
1449 return;
1450 }
1451
1452 double mm2pixelsX = pixel_width/mm_width;
1453 double mm2pixelsY = pixel_height/mm_height;
1454
1455 switch (mode)
1456 {
1457 case wxMM_TWIPS:
1458 {
1459 m_logicalScaleX = (twips2mm * mm2pixelsX);
1460 m_logicalScaleY = (twips2mm * mm2pixelsY);
1461 break;
1462 }
1463 case wxMM_POINTS:
1464 {
1465 m_logicalScaleX = (pt2mm * mm2pixelsX);
1466 m_logicalScaleY = (pt2mm * mm2pixelsY);
1467 break;
1468 }
1469 case wxMM_METRIC:
1470 {
1471 m_logicalScaleX = mm2pixelsX;
1472 m_logicalScaleY = mm2pixelsY;
1473 break;
1474 }
1475 case wxMM_LOMETRIC:
1476 {
1477 m_logicalScaleX = (mm2pixelsX/10.0);
1478 m_logicalScaleY = (mm2pixelsY/10.0);
1479 break;
1480 }
1481 default:
1482 case wxMM_TEXT:
1483 {
1484 m_logicalScaleX = 1.0;
1485 m_logicalScaleY = 1.0;
1486 break;
1487 }
1488 }
1489
1490 if (::GetMapMode(GetHdc()) != MM_ANISOTROPIC)
1491 ::SetMapMode(GetHdc(), MM_ANISOTROPIC);
1492
1493 SetViewportExtEx(GetHdc(), VIEWPORT_EXTENT, VIEWPORT_EXTENT, NULL);
1494 m_windowExtX = (int)MS_XDEV2LOGREL(VIEWPORT_EXTENT);
1495 m_windowExtY = (int)MS_YDEV2LOGREL(VIEWPORT_EXTENT);
1496 ::SetWindowExtEx(GetHdc(), m_windowExtX, m_windowExtY, NULL);
1497 ::SetViewportOrgEx(GetHdc(), (int)m_deviceOriginX, (int)m_deviceOriginY, NULL);
1498 ::SetWindowOrgEx(GetHdc(), (int)m_logicalOriginX, (int)m_logicalOriginY, NULL);
1499 }
1500
1501 void wxDC::SetUserScale(double x, double y)
1502 {
1503 #ifdef __WXMICROWIN__
1504 if (!GetHDC()) return;
1505 #endif
1506
1507 m_userScaleX = x;
1508 m_userScaleY = y;
1509
1510 SetMapMode(m_mappingMode);
1511 }
1512
1513 void wxDC::SetAxisOrientation(bool xLeftRight, bool yBottomUp)
1514 {
1515 #ifdef __WXMICROWIN__
1516 if (!GetHDC()) return;
1517 #endif
1518
1519 m_signX = xLeftRight ? 1 : -1;
1520 m_signY = yBottomUp ? -1 : 1;
1521
1522 SetMapMode(m_mappingMode);
1523 }
1524
1525 void wxDC::SetSystemScale(double x, double y)
1526 {
1527 #ifdef __WXMICROWIN__
1528 if (!GetHDC()) return;
1529 #endif
1530
1531 m_scaleX = x;
1532 m_scaleY = y;
1533
1534 SetMapMode(m_mappingMode);
1535 }
1536
1537 void wxDC::SetLogicalOrigin(wxCoord x, wxCoord y)
1538 {
1539 #ifdef __WXMICROWIN__
1540 if (!GetHDC()) return;
1541 #endif
1542
1543 m_logicalOriginX = x;
1544 m_logicalOriginY = y;
1545
1546 ::SetWindowOrgEx(GetHdc(), (int)m_logicalOriginX, (int)m_logicalOriginY, NULL);
1547 }
1548
1549 void wxDC::SetDeviceOrigin(wxCoord x, wxCoord y)
1550 {
1551 #ifdef __WXMICROWIN__
1552 if (!GetHDC()) return;
1553 #endif
1554
1555 m_deviceOriginX = x;
1556 m_deviceOriginY = y;
1557
1558 ::SetViewportOrgEx(GetHdc(), (int)m_deviceOriginX, (int)m_deviceOriginY, NULL);
1559 }
1560
1561 // ---------------------------------------------------------------------------
1562 // coordinates transformations
1563 // ---------------------------------------------------------------------------
1564
1565 wxCoord wxDCBase::DeviceToLogicalX(wxCoord x) const
1566 {
1567 double xRel = x - m_deviceOriginX;
1568 xRel /= m_logicalScaleX*m_userScaleX*m_signX*m_scaleX;
1569 return (wxCoord)(xRel + m_logicalOriginX);
1570 }
1571
1572 wxCoord wxDCBase::DeviceToLogicalXRel(wxCoord x) const
1573 {
1574 return (wxCoord) ((x)/(m_logicalScaleX*m_userScaleX*m_signX*m_scaleX));
1575 }
1576
1577 wxCoord wxDCBase::DeviceToLogicalY(wxCoord y) const
1578 {
1579 double yRel = y - m_deviceOriginY;
1580 yRel /= m_logicalScaleY*m_userScaleY*m_signY*m_scaleY;
1581 return (wxCoord)(yRel + m_logicalOriginY);
1582 }
1583
1584 wxCoord wxDCBase::DeviceToLogicalYRel(wxCoord y) const
1585 {
1586 return (wxCoord) ((y)/(m_logicalScaleY*m_userScaleY*m_signY*m_scaleY));
1587 }
1588
1589 wxCoord wxDCBase::LogicalToDeviceX(wxCoord x) const
1590 {
1591 return (wxCoord) ((x - m_logicalOriginX)*m_logicalScaleX*m_userScaleX*m_signX*m_scaleX + m_deviceOriginX);
1592 }
1593
1594 wxCoord wxDCBase::LogicalToDeviceXRel(wxCoord x) const
1595 {
1596 return (wxCoord) (x*m_logicalScaleX*m_userScaleX*m_signX*m_scaleX);
1597 }
1598
1599 wxCoord wxDCBase::LogicalToDeviceY(wxCoord y) const
1600 {
1601 return (wxCoord) ((y - m_logicalOriginY)*m_logicalScaleY*m_userScaleY*m_signY*m_scaleY + m_deviceOriginY);
1602 }
1603
1604 wxCoord wxDCBase::LogicalToDeviceYRel(wxCoord y) const
1605 {
1606 return (wxCoord) (y*m_logicalScaleY*m_userScaleY*m_signY*m_scaleY);
1607 }
1608
1609 // ---------------------------------------------------------------------------
1610 // bit blit
1611 // ---------------------------------------------------------------------------
1612
1613 bool wxDC::DoBlit(wxCoord xdest, wxCoord ydest,
1614 wxCoord width, wxCoord height,
1615 wxDC *source, wxCoord xsrc, wxCoord ysrc,
1616 int rop, bool useMask,
1617 wxCoord xsrcMask, wxCoord ysrcMask)
1618 {
1619 #ifdef __WXMICROWIN__
1620 if (!GetHDC()) return FALSE;
1621 #endif
1622
1623 wxMask *mask = NULL;
1624 if ( useMask )
1625 {
1626 const wxBitmap& bmp = source->m_selectedBitmap;
1627 mask = bmp.GetMask();
1628
1629 if ( !(bmp.Ok() && mask && mask->GetMaskBitmap()) )
1630 {
1631 // don't give assert here because this would break existing
1632 // programs - just silently ignore useMask parameter
1633 useMask = FALSE;
1634 }
1635 }
1636
1637 if (xsrcMask == -1 && ysrcMask == -1)
1638 {
1639 xsrcMask = xsrc; ysrcMask = ysrc;
1640 }
1641
1642 COLORREF old_textground = ::GetTextColor(GetHdc());
1643 COLORREF old_background = ::GetBkColor(GetHdc());
1644 if (m_textForegroundColour.Ok())
1645 {
1646 ::SetTextColor(GetHdc(), m_textForegroundColour.GetPixel() );
1647 }
1648 if (m_textBackgroundColour.Ok())
1649 {
1650 ::SetBkColor(GetHdc(), m_textBackgroundColour.GetPixel() );
1651 }
1652
1653 DWORD dwRop = SRCCOPY;
1654 switch (rop)
1655 {
1656 case wxXOR: dwRop = SRCINVERT; break;
1657 case wxINVERT: dwRop = DSTINVERT; break;
1658 case wxOR_REVERSE: dwRop = 0x00DD0228; break;
1659 case wxAND_REVERSE: dwRop = SRCERASE; break;
1660 case wxCLEAR: dwRop = BLACKNESS; break;
1661 case wxSET: dwRop = WHITENESS; break;
1662 case wxOR_INVERT: dwRop = MERGEPAINT; break;
1663 case wxAND: dwRop = SRCAND; break;
1664 case wxOR: dwRop = SRCPAINT; break;
1665 case wxEQUIV: dwRop = 0x00990066; break;
1666 case wxNAND: dwRop = 0x007700E6; break;
1667 case wxAND_INVERT: dwRop = 0x00220326; break;
1668 case wxCOPY: dwRop = SRCCOPY; break;
1669 case wxNO_OP: dwRop = DSTCOPY; break;
1670 case wxSRC_INVERT: dwRop = NOTSRCCOPY; break;
1671 case wxNOR: dwRop = NOTSRCCOPY; break;
1672 default:
1673 wxFAIL_MSG( wxT("unsupported logical function") );
1674 return FALSE;
1675 }
1676
1677 bool success = FALSE;
1678
1679 if (useMask)
1680 {
1681 #ifdef __WIN32__
1682 // we want the part of the image corresponding to the mask to be
1683 // transparent, so use "DSTCOPY" ROP for the mask points (the usual
1684 // meaning of fg and bg is inverted which corresponds to wxWin notion
1685 // of the mask which is also contrary to the Windows one)
1686
1687 // On some systems, MaskBlt succeeds yet is much much slower
1688 // than the wxWindows fall-back implementation. So we need
1689 // to be able to switch this on and off at runtime.
1690 #if wxUSE_SYSTEM_OPTIONS
1691 if (wxSystemOptions::GetOptionInt(wxT("no-maskblt")) == 0)
1692 #endif
1693 {
1694 success = ::MaskBlt(GetHdc(), xdest, ydest, width, height,
1695 GetHdcOf(*source), xsrc, ysrc,
1696 (HBITMAP)mask->GetMaskBitmap(), xsrcMask, ysrcMask,
1697 MAKEROP4(dwRop, DSTCOPY)) != 0;
1698 }
1699
1700 if ( !success )
1701 #endif // Win32
1702 {
1703 // Blit bitmap with mask
1704 HDC dc_mask ;
1705 HDC dc_buffer ;
1706 HBITMAP buffer_bmap ;
1707
1708 #if wxUSE_DC_CACHEING
1709 // create a temp buffer bitmap and DCs to access it and the mask
1710 wxDCCacheEntry* dcCacheEntry1 = FindDCInCache(NULL, source->GetHDC());
1711 dc_mask = (HDC) dcCacheEntry1->m_dc;
1712
1713 wxDCCacheEntry* dcCacheEntry2 = FindDCInCache(dcCacheEntry1, GetHDC());
1714 dc_buffer = (HDC) dcCacheEntry2->m_dc;
1715
1716 wxDCCacheEntry* bitmapCacheEntry = FindBitmapInCache(GetHDC(),
1717 width, height);
1718
1719 buffer_bmap = (HBITMAP) bitmapCacheEntry->m_bitmap;
1720 #else // !wxUSE_DC_CACHEING
1721 // create a temp buffer bitmap and DCs to access it and the mask
1722 dc_mask = ::CreateCompatibleDC(GetHdcOf(*source));
1723 dc_buffer = ::CreateCompatibleDC(GetHdc());
1724 buffer_bmap = ::CreateCompatibleBitmap(GetHdc(), width, height);
1725 #endif // wxUSE_DC_CACHEING/!wxUSE_DC_CACHEING
1726 ::SelectObject(dc_mask, (HBITMAP) mask->GetMaskBitmap());
1727 ::SelectObject(dc_buffer, buffer_bmap);
1728
1729 // copy dest to buffer
1730 if ( !::BitBlt(dc_buffer, 0, 0, (int)width, (int)height,
1731 GetHdc(), xdest, ydest, SRCCOPY) )
1732 {
1733 wxLogLastError(wxT("BitBlt"));
1734 }
1735
1736 // copy src to buffer using selected raster op
1737 if ( !::BitBlt(dc_buffer, 0, 0, (int)width, (int)height,
1738 GetHdcOf(*source), xsrc, ysrc, dwRop) )
1739 {
1740 wxLogLastError(wxT("BitBlt"));
1741 }
1742
1743 // set masked area in buffer to BLACK (pixel value 0)
1744 COLORREF prevBkCol = ::SetBkColor(GetHdc(), RGB(255, 255, 255));
1745 COLORREF prevCol = ::SetTextColor(GetHdc(), RGB(0, 0, 0));
1746 if ( !::BitBlt(dc_buffer, 0, 0, (int)width, (int)height,
1747 dc_mask, xsrcMask, ysrcMask, SRCAND) )
1748 {
1749 wxLogLastError(wxT("BitBlt"));
1750 }
1751
1752 // set unmasked area in dest to BLACK
1753 ::SetBkColor(GetHdc(), RGB(0, 0, 0));
1754 ::SetTextColor(GetHdc(), RGB(255, 255, 255));
1755 if ( !::BitBlt(GetHdc(), xdest, ydest, (int)width, (int)height,
1756 dc_mask, xsrcMask, ysrcMask, SRCAND) )
1757 {
1758 wxLogLastError(wxT("BitBlt"));
1759 }
1760 ::SetBkColor(GetHdc(), prevBkCol); // restore colours to original values
1761 ::SetTextColor(GetHdc(), prevCol);
1762
1763 // OR buffer to dest
1764 success = ::BitBlt(GetHdc(), xdest, ydest,
1765 (int)width, (int)height,
1766 dc_buffer, 0, 0, SRCPAINT) != 0;
1767 if ( !success )
1768 {
1769 wxLogLastError(wxT("BitBlt"));
1770 }
1771
1772 // tidy up temporary DCs and bitmap
1773 ::SelectObject(dc_mask, 0);
1774 ::SelectObject(dc_buffer, 0);
1775
1776 #if !wxUSE_DC_CACHEING
1777 {
1778 ::DeleteDC(dc_mask);
1779 ::DeleteDC(dc_buffer);
1780 ::DeleteObject(buffer_bmap);
1781 }
1782 #endif
1783 }
1784 }
1785 else // no mask, just BitBlt() it
1786 {
1787 success = ::BitBlt(GetHdc(), xdest, ydest,
1788 (int)width, (int)height,
1789 GetHdcOf(*source), xsrc, ysrc, dwRop) != 0;
1790 if ( !success )
1791 {
1792 wxLogLastError(wxT("BitBlt"));
1793 }
1794 }
1795 ::SetTextColor(GetHdc(), old_textground);
1796 ::SetBkColor(GetHdc(), old_background);
1797
1798 return success;
1799 }
1800
1801 void wxDC::DoGetSize(int *w, int *h) const
1802 {
1803 #ifdef __WXMICROWIN__
1804 if (!GetHDC()) return;
1805 #endif
1806
1807 if ( w ) *w = ::GetDeviceCaps(GetHdc(), HORZRES);
1808 if ( h ) *h = ::GetDeviceCaps(GetHdc(), VERTRES);
1809 }
1810
1811 void wxDC::DoGetSizeMM(int *w, int *h) const
1812 {
1813 #ifdef __WXMICROWIN__
1814 if (!GetHDC()) return;
1815 #endif
1816
1817 if ( w ) *w = ::GetDeviceCaps(GetHdc(), HORZSIZE);
1818 if ( h ) *h = ::GetDeviceCaps(GetHdc(), VERTSIZE);
1819 }
1820
1821 wxSize wxDC::GetPPI() const
1822 {
1823 #ifdef __WXMICROWIN__
1824 if (!GetHDC()) return wxSize();
1825 #endif
1826
1827 int x = ::GetDeviceCaps(GetHdc(), LOGPIXELSX);
1828 int y = ::GetDeviceCaps(GetHdc(), LOGPIXELSY);
1829
1830 return wxSize(x, y);
1831 }
1832
1833 // For use by wxWindows only, unless custom units are required.
1834 void wxDC::SetLogicalScale(double x, double y)
1835 {
1836 #ifdef __WXMICROWIN__
1837 if (!GetHDC()) return;
1838 #endif
1839
1840 m_logicalScaleX = x;
1841 m_logicalScaleY = y;
1842 }
1843
1844 #if WXWIN_COMPATIBILITY
1845 void wxDC::DoGetTextExtent(const wxString& string, float *x, float *y,
1846 float *descent, float *externalLeading,
1847 wxFont *theFont, bool use16bit) const
1848 {
1849 #ifdef __WXMICROWIN__
1850 if (!GetHDC()) return;
1851 #endif
1852
1853 wxCoord x1, y1, descent1, externalLeading1;
1854 GetTextExtent(string, & x1, & y1, & descent1, & externalLeading1, theFont, use16bit);
1855 *x = x1; *y = y1;
1856 if (descent)
1857 *descent = descent1;
1858 if (externalLeading)
1859 *externalLeading = externalLeading1;
1860 }
1861 #endif
1862
1863 #if wxUSE_DC_CACHEING
1864
1865 /*
1866 * This implementation is a bit ugly and uses the old-fashioned wxList class, so I will
1867 * improve it in due course, either using arrays, or simply storing pointers to one
1868 * entry for the bitmap, and two for the DCs. -- JACS
1869 */
1870
1871 wxList wxDC::sm_bitmapCache;
1872 wxList wxDC::sm_dcCache;
1873
1874 wxDCCacheEntry::wxDCCacheEntry(WXHBITMAP hBitmap, int w, int h, int depth)
1875 {
1876 m_bitmap = hBitmap;
1877 m_dc = 0;
1878 m_width = w;
1879 m_height = h;
1880 m_depth = depth;
1881 }
1882
1883 wxDCCacheEntry::wxDCCacheEntry(WXHDC hDC, int depth)
1884 {
1885 m_bitmap = 0;
1886 m_dc = hDC;
1887 m_width = 0;
1888 m_height = 0;
1889 m_depth = depth;
1890 }
1891
1892 wxDCCacheEntry::~wxDCCacheEntry()
1893 {
1894 if (m_bitmap)
1895 ::DeleteObject((HBITMAP) m_bitmap);
1896 if (m_dc)
1897 ::DeleteDC((HDC) m_dc);
1898 }
1899
1900 wxDCCacheEntry* wxDC::FindBitmapInCache(WXHDC dc, int w, int h)
1901 {
1902 int depth = ::GetDeviceCaps((HDC) dc, PLANES) * ::GetDeviceCaps((HDC) dc, BITSPIXEL);
1903 wxNode* node = sm_bitmapCache.First();
1904 while (node)
1905 {
1906 wxDCCacheEntry* entry = (wxDCCacheEntry*) node->Data();
1907
1908 if (entry->m_depth == depth)
1909 {
1910 if (entry->m_width < w || entry->m_height < h)
1911 {
1912 ::DeleteObject((HBITMAP) entry->m_bitmap);
1913 entry->m_bitmap = (WXHBITMAP) ::CreateCompatibleBitmap((HDC) dc, w, h);
1914 if ( !entry->m_bitmap)
1915 {
1916 wxLogLastError(wxT("CreateCompatibleBitmap"));
1917 }
1918 entry->m_width = w; entry->m_height = h;
1919 return entry;
1920 }
1921 return entry;
1922 }
1923
1924 node = node->Next();
1925 }
1926 WXHBITMAP hBitmap = (WXHBITMAP) ::CreateCompatibleBitmap((HDC) dc, w, h);
1927 if ( !hBitmap)
1928 {
1929 wxLogLastError(wxT("CreateCompatibleBitmap"));
1930 }
1931 wxDCCacheEntry* entry = new wxDCCacheEntry(hBitmap, w, h, depth);
1932 AddToBitmapCache(entry);
1933 return entry;
1934 }
1935
1936 wxDCCacheEntry* wxDC::FindDCInCache(wxDCCacheEntry* notThis, WXHDC dc)
1937 {
1938 int depth = ::GetDeviceCaps((HDC) dc, PLANES) * ::GetDeviceCaps((HDC) dc, BITSPIXEL);
1939 wxNode* node = sm_dcCache.First();
1940 while (node)
1941 {
1942 wxDCCacheEntry* entry = (wxDCCacheEntry*) node->Data();
1943
1944 // Don't return the same one as we already have
1945 if (!notThis || (notThis != entry))
1946 {
1947 if (entry->m_depth == depth)
1948 {
1949 return entry;
1950 }
1951 }
1952
1953 node = node->Next();
1954 }
1955 WXHDC hDC = (WXHDC) ::CreateCompatibleDC((HDC) dc);
1956 if ( !hDC)
1957 {
1958 wxLogLastError(wxT("CreateCompatibleDC"));
1959 }
1960 wxDCCacheEntry* entry = new wxDCCacheEntry(hDC, depth);
1961 AddToDCCache(entry);
1962 return entry;
1963 }
1964
1965 void wxDC::AddToBitmapCache(wxDCCacheEntry* entry)
1966 {
1967 sm_bitmapCache.Append(entry);
1968 }
1969
1970 void wxDC::AddToDCCache(wxDCCacheEntry* entry)
1971 {
1972 sm_dcCache.Append(entry);
1973 }
1974
1975 void wxDC::ClearCache()
1976 {
1977 sm_bitmapCache.DeleteContents(TRUE);
1978 sm_bitmapCache.Clear();
1979 sm_bitmapCache.DeleteContents(FALSE);
1980 sm_dcCache.DeleteContents(TRUE);
1981 sm_dcCache.Clear();
1982 sm_dcCache.DeleteContents(FALSE);
1983 }
1984
1985 // Clean up cache at app exit
1986 class wxDCModule : public wxModule
1987 {
1988 public:
1989 virtual bool OnInit() { return TRUE; }
1990 virtual void OnExit() { wxDC::ClearCache(); }
1991
1992 private:
1993 DECLARE_DYNAMIC_CLASS(wxDCModule)
1994 };
1995
1996 IMPLEMENT_DYNAMIC_CLASS(wxDCModule, wxModule)
1997
1998 #endif
1999 // wxUSE_DC_CACHEING
2000