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