]> git.saurik.com Git - wxWidgets.git/blob - src/msw/dc.cpp
db.cpp
[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 */
1119 }
1120
1121 void wxDC::SetLogicalFunction(int function)
1122 {
1123 m_logicalFunction = function;
1124
1125 SetRop(m_hDC);
1126 }
1127
1128 void wxDC::SetRop(WXHDC dc)
1129 {
1130 if ( !dc || m_logicalFunction < 0 )
1131 return;
1132
1133 int rop;
1134
1135 switch (m_logicalFunction)
1136 {
1137 case wxCLEAR: rop = R2_BLACK; break;
1138 case wxXOR: rop = R2_XORPEN; break;
1139 case wxINVERT: rop = R2_NOT; break;
1140 case wxOR_REVERSE: rop = R2_MERGEPENNOT; break;
1141 case wxAND_REVERSE: rop = R2_MASKPENNOT; break;
1142 case wxCOPY: rop = R2_COPYPEN; break;
1143 case wxAND: rop = R2_MASKPEN; break;
1144 case wxAND_INVERT: rop = R2_MASKNOTPEN; break;
1145 case wxNO_OP: rop = R2_NOP; break;
1146 case wxNOR: rop = R2_NOTMERGEPEN; break;
1147 case wxEQUIV: rop = R2_NOTXORPEN; break;
1148 case wxSRC_INVERT: rop = R2_NOTCOPYPEN; break;
1149 case wxOR_INVERT: rop = R2_MERGENOTPEN; break;
1150 case wxNAND: rop = R2_NOTMASKPEN; break;
1151 case wxOR: rop = R2_MERGEPEN; break;
1152 case wxSET: rop = R2_WHITE; break;
1153
1154 default:
1155 wxFAIL_MSG( wxT("unsupported logical function") );
1156 return;
1157 }
1158
1159 SetROP2(GetHdc(), rop);
1160 }
1161
1162 bool wxDC::StartDoc(const wxString& message)
1163 {
1164 // We might be previewing, so return TRUE to let it continue.
1165 return TRUE;
1166 }
1167
1168 void wxDC::EndDoc()
1169 {
1170 }
1171
1172 void wxDC::StartPage()
1173 {
1174 }
1175
1176 void wxDC::EndPage()
1177 {
1178 }
1179
1180 // ---------------------------------------------------------------------------
1181 // text metrics
1182 // ---------------------------------------------------------------------------
1183
1184 wxCoord wxDC::GetCharHeight() const
1185 {
1186 TEXTMETRIC lpTextMetric;
1187
1188 GetTextMetrics(GetHdc(), &lpTextMetric);
1189
1190 return YDEV2LOGREL(lpTextMetric.tmHeight);
1191 }
1192
1193 wxCoord wxDC::GetCharWidth() const
1194 {
1195 TEXTMETRIC lpTextMetric;
1196
1197 GetTextMetrics(GetHdc(), &lpTextMetric);
1198
1199 return XDEV2LOGREL(lpTextMetric.tmAveCharWidth);
1200 }
1201
1202 void wxDC::DoGetTextExtent(const wxString& string, wxCoord *x, wxCoord *y,
1203 wxCoord *descent, wxCoord *externalLeading,
1204 wxFont *theFont) const
1205 {
1206 wxFont *fontToUse = (wxFont*) theFont;
1207 if (!fontToUse)
1208 fontToUse = (wxFont*) &m_font;
1209
1210 SIZE sizeRect;
1211 TEXTMETRIC tm;
1212
1213 GetTextExtentPoint(GetHdc(), WXSTRINGCAST string, wxStrlen(WXSTRINGCAST string), &sizeRect);
1214 GetTextMetrics(GetHdc(), &tm);
1215
1216 if (x) *x = XDEV2LOGREL(sizeRect.cx);
1217 if (y) *y = YDEV2LOGREL(sizeRect.cy);
1218 if (descent) *descent = tm.tmDescent;
1219 if (externalLeading) *externalLeading = tm.tmExternalLeading;
1220 }
1221
1222 void wxDC::SetMapMode(int mode)
1223 {
1224 m_mappingMode = mode;
1225
1226 int pixel_width = 0;
1227 int pixel_height = 0;
1228 int mm_width = 0;
1229 int mm_height = 0;
1230
1231 pixel_width = GetDeviceCaps(GetHdc(), HORZRES);
1232 pixel_height = GetDeviceCaps(GetHdc(), VERTRES);
1233 mm_width = GetDeviceCaps(GetHdc(), HORZSIZE);
1234 mm_height = GetDeviceCaps(GetHdc(), VERTSIZE);
1235
1236 if ((pixel_width == 0) || (pixel_height == 0) || (mm_width == 0) || (mm_height == 0))
1237 {
1238 return;
1239 }
1240
1241 double mm2pixelsX = pixel_width/mm_width;
1242 double mm2pixelsY = pixel_height/mm_height;
1243
1244 switch (mode)
1245 {
1246 case wxMM_TWIPS:
1247 {
1248 m_logicalScaleX = (twips2mm * mm2pixelsX);
1249 m_logicalScaleY = (twips2mm * mm2pixelsY);
1250 break;
1251 }
1252 case wxMM_POINTS:
1253 {
1254 m_logicalScaleX = (pt2mm * mm2pixelsX);
1255 m_logicalScaleY = (pt2mm * mm2pixelsY);
1256 break;
1257 }
1258 case wxMM_METRIC:
1259 {
1260 m_logicalScaleX = mm2pixelsX;
1261 m_logicalScaleY = mm2pixelsY;
1262 break;
1263 }
1264 case wxMM_LOMETRIC:
1265 {
1266 m_logicalScaleX = (mm2pixelsX/10.0);
1267 m_logicalScaleY = (mm2pixelsY/10.0);
1268 break;
1269 }
1270 default:
1271 case wxMM_TEXT:
1272 {
1273 m_logicalScaleX = 1.0;
1274 m_logicalScaleY = 1.0;
1275 break;
1276 }
1277 }
1278
1279 if (::GetMapMode(GetHdc()) != MM_ANISOTROPIC)
1280 ::SetMapMode(GetHdc(), MM_ANISOTROPIC);
1281
1282 SetViewportExtEx(GetHdc(), VIEWPORT_EXTENT, VIEWPORT_EXTENT, NULL);
1283 m_windowExtX = (int)MS_XDEV2LOGREL(VIEWPORT_EXTENT);
1284 m_windowExtY = (int)MS_YDEV2LOGREL(VIEWPORT_EXTENT);
1285 ::SetWindowExtEx(GetHdc(), m_windowExtX, m_windowExtY, NULL);
1286 ::SetViewportOrgEx(GetHdc(), (int)m_deviceOriginX, (int)m_deviceOriginY, NULL);
1287 ::SetWindowOrgEx(GetHdc(), (int)m_logicalOriginX, (int)m_logicalOriginY, NULL);
1288 }
1289
1290 void wxDC::SetUserScale(double x, double y)
1291 {
1292 m_userScaleX = x;
1293 m_userScaleY = y;
1294
1295 SetMapMode(m_mappingMode);
1296 }
1297
1298 void wxDC::SetAxisOrientation(bool xLeftRight, bool yBottomUp)
1299 {
1300 m_signX = xLeftRight ? 1 : -1;
1301 m_signY = yBottomUp ? -1 : 1;
1302
1303 SetMapMode(m_mappingMode);
1304 }
1305
1306 void wxDC::SetSystemScale(double x, double y)
1307 {
1308 m_scaleX = x;
1309 m_scaleY = y;
1310
1311 SetMapMode(m_mappingMode);
1312 }
1313
1314 void wxDC::SetLogicalOrigin(wxCoord x, wxCoord y)
1315 {
1316 m_logicalOriginX = x;
1317 m_logicalOriginY = y;
1318
1319 ::SetWindowOrgEx(GetHdc(), (int)m_logicalOriginX, (int)m_logicalOriginY, NULL);
1320 }
1321
1322 void wxDC::SetDeviceOrigin(wxCoord x, wxCoord y)
1323 {
1324 m_deviceOriginX = x;
1325 m_deviceOriginY = y;
1326
1327 ::SetViewportOrgEx(GetHdc(), (int)m_deviceOriginX, (int)m_deviceOriginY, NULL);
1328 }
1329
1330 // ---------------------------------------------------------------------------
1331 // coordinates transformations
1332 // ---------------------------------------------------------------------------
1333
1334 wxCoord wxDCBase::DeviceToLogicalX(wxCoord x) const
1335 {
1336 return (wxCoord) (((x) - m_deviceOriginX)/(m_logicalScaleX*m_userScaleX*m_signX*m_scaleX) - m_logicalOriginX);
1337 }
1338
1339 wxCoord wxDCBase::DeviceToLogicalXRel(wxCoord x) const
1340 {
1341 return (wxCoord) ((x)/(m_logicalScaleX*m_userScaleX*m_signX*m_scaleX));
1342 }
1343
1344 wxCoord wxDCBase::DeviceToLogicalY(wxCoord y) const
1345 {
1346 return (wxCoord) (((y) - m_deviceOriginY)/(m_logicalScaleY*m_userScaleY*m_signY*m_scaleY) - m_logicalOriginY);
1347 }
1348
1349 wxCoord wxDCBase::DeviceToLogicalYRel(wxCoord y) const
1350 {
1351 return (wxCoord) ((y)/(m_logicalScaleY*m_userScaleY*m_signY*m_scaleY));
1352 }
1353
1354 wxCoord wxDCBase::LogicalToDeviceX(wxCoord x) const
1355 {
1356 return (wxCoord) ((x - m_logicalOriginX)*m_logicalScaleX*m_userScaleX*m_signX*m_scaleX + m_deviceOriginX);
1357 }
1358
1359 wxCoord wxDCBase::LogicalToDeviceXRel(wxCoord x) const
1360 {
1361 return (wxCoord) (x*m_logicalScaleX*m_userScaleX*m_signX*m_scaleX);
1362 }
1363
1364 wxCoord wxDCBase::LogicalToDeviceY(wxCoord y) const
1365 {
1366 return (wxCoord) ((y - m_logicalOriginY)*m_logicalScaleY*m_userScaleY*m_signY*m_scaleY + m_deviceOriginY);
1367 }
1368
1369 wxCoord wxDCBase::LogicalToDeviceYRel(wxCoord y) const
1370 {
1371 return (wxCoord) (y*m_logicalScaleY*m_userScaleY*m_signY*m_scaleY);
1372 }
1373
1374 // ---------------------------------------------------------------------------
1375 // bit blit
1376 // ---------------------------------------------------------------------------
1377
1378 bool wxDC::DoBlit(wxCoord xdest, wxCoord ydest,
1379 wxCoord width, wxCoord height,
1380 wxDC *source, wxCoord xsrc, wxCoord ysrc,
1381 int rop, bool useMask)
1382 {
1383 wxMask *mask = NULL;
1384 if ( useMask )
1385 {
1386 const wxBitmap& bmp = source->m_selectedBitmap;
1387 mask = bmp.GetMask();
1388
1389 if ( !(bmp.Ok() && mask && mask->GetMaskBitmap()) )
1390 {
1391 // don't give assert here because this would break existing
1392 // programs - just silently ignore useMask parameter
1393 useMask = FALSE;
1394 }
1395 }
1396
1397 COLORREF old_textground = ::GetTextColor(GetHdc());
1398 COLORREF old_background = ::GetBkColor(GetHdc());
1399 if (m_textForegroundColour.Ok())
1400 {
1401 ::SetTextColor(GetHdc(), m_textForegroundColour.GetPixel() );
1402 }
1403 if (m_textBackgroundColour.Ok())
1404 {
1405 ::SetBkColor(GetHdc(), m_textBackgroundColour.GetPixel() );
1406 }
1407
1408 DWORD dwRop = SRCCOPY;
1409 switch (rop)
1410 {
1411 case wxXOR: dwRop = SRCINVERT; break;
1412 case wxINVERT: dwRop = DSTINVERT; break;
1413 case wxOR_REVERSE: dwRop = 0x00DD0228; break;
1414 case wxAND_REVERSE: dwRop = SRCERASE; break;
1415 case wxCLEAR: dwRop = BLACKNESS; break;
1416 case wxSET: dwRop = WHITENESS; break;
1417 case wxOR_INVERT: dwRop = MERGEPAINT; break;
1418 case wxAND: dwRop = SRCAND; break;
1419 case wxOR: dwRop = SRCPAINT; break;
1420 case wxEQUIV: dwRop = 0x00990066; break;
1421 case wxNAND: dwRop = 0x007700E6; break;
1422 case wxAND_INVERT: dwRop = 0x00220326; break;
1423 case wxCOPY: dwRop = SRCCOPY; break;
1424 case wxNO_OP: dwRop = DSTCOPY; break;
1425 case wxSRC_INVERT: dwRop = NOTSRCCOPY; break;
1426 case wxNOR: dwRop = NOTSRCCOPY; break;
1427 default:
1428 wxFAIL_MSG( wxT("unsupported logical function") );
1429 return FALSE;
1430 }
1431
1432 bool success;
1433
1434 if (useMask)
1435 {
1436 #ifdef __WIN32__
1437 // we want the part of the image corresponding to the mask to be
1438 // transparent, so use "DSTCOPY" ROP for the mask points (the usual
1439 // meaning of fg and bg is inverted which corresponds to wxWin notion
1440 // of the mask which is also contrary to the Windows one)
1441 success = ::MaskBlt(GetHdc(), xdest, ydest, width, height,
1442 GetHdcOf(*source), xsrc, ysrc,
1443 (HBITMAP)mask->GetMaskBitmap(), 0, 0,
1444 MAKEROP4(dwRop, DSTCOPY)) != 0;
1445
1446 if ( !success )
1447 #endif // Win32
1448 {
1449 // Blit bitmap with mask
1450
1451 // create a temp buffer bitmap and DCs to access it and the mask
1452 HDC dc_mask = ::CreateCompatibleDC(GetHdcOf(*source));
1453 HDC dc_buffer = ::CreateCompatibleDC(GetHdc());
1454 HBITMAP buffer_bmap = ::CreateCompatibleBitmap(GetHdc(), width, height);
1455 ::SelectObject(dc_mask, (HBITMAP) mask->GetMaskBitmap());
1456 ::SelectObject(dc_buffer, buffer_bmap);
1457
1458 // copy dest to buffer
1459 if ( !::BitBlt(dc_buffer, 0, 0, (int)width, (int)height,
1460 GetHdc(), xdest, ydest, SRCCOPY) )
1461 {
1462 wxLogLastError(wxT("BitBlt"));
1463 }
1464
1465 // copy src to buffer using selected raster op
1466 if ( !::BitBlt(dc_buffer, 0, 0, (int)width, (int)height,
1467 GetHdcOf(*source), xsrc, ysrc, dwRop) )
1468 {
1469 wxLogLastError(wxT("BitBlt"));
1470 }
1471
1472 // set masked area in buffer to BLACK (pixel value 0)
1473 COLORREF prevBkCol = ::SetBkColor(GetHdc(), RGB(255, 255, 255));
1474 COLORREF prevCol = ::SetTextColor(GetHdc(), RGB(0, 0, 0));
1475 if ( !::BitBlt(dc_buffer, 0, 0, (int)width, (int)height,
1476 dc_mask, xsrc, ysrc, SRCAND) )
1477 {
1478 wxLogLastError(wxT("BitBlt"));
1479 }
1480
1481 // set unmasked area in dest to BLACK
1482 ::SetBkColor(GetHdc(), RGB(0, 0, 0));
1483 ::SetTextColor(GetHdc(), RGB(255, 255, 255));
1484 if ( !::BitBlt(GetHdc(), xdest, ydest, (int)width, (int)height,
1485 dc_mask, xsrc, ysrc, SRCAND) )
1486 {
1487 wxLogLastError(wxT("BitBlt"));
1488 }
1489 ::SetBkColor(GetHdc(), prevBkCol); // restore colours to original values
1490 ::SetTextColor(GetHdc(), prevCol);
1491
1492 // OR buffer to dest
1493 success = ::BitBlt(GetHdc(), xdest, ydest,
1494 (int)width, (int)height,
1495 dc_buffer, 0, 0, SRCPAINT) != 0;
1496 if ( !success )
1497 {
1498 wxLogLastError(wxT("BitBlt"));
1499 }
1500
1501 // tidy up temporary DCs and bitmap
1502 ::SelectObject(dc_mask, 0);
1503 ::DeleteDC(dc_mask);
1504 ::SelectObject(dc_buffer, 0);
1505 ::DeleteDC(dc_buffer);
1506 ::DeleteObject(buffer_bmap);
1507 }
1508 }
1509 else // no mask, just BitBlt() it
1510 {
1511 success = ::BitBlt(GetHdc(), xdest, ydest,
1512 (int)width, (int)height,
1513 GetHdcOf(*source), xsrc, ysrc, dwRop) != 0;
1514 if ( !success )
1515 {
1516 wxLogLastError(wxT("BitBlt"));
1517 }
1518 }
1519 ::SetTextColor(GetHdc(), old_textground);
1520 ::SetBkColor(GetHdc(), old_background);
1521
1522 return success;
1523 }
1524
1525 void wxDC::DoGetSize(int *w, int *h) const
1526 {
1527 if ( w ) *w = ::GetDeviceCaps(GetHdc(), HORZRES);
1528 if ( h ) *h = ::GetDeviceCaps(GetHdc(), VERTRES);
1529 }
1530
1531 void wxDC::DoGetSizeMM(int *w, int *h) const
1532 {
1533 if ( w ) *w = ::GetDeviceCaps(GetHdc(), HORZSIZE);
1534 if ( h ) *h = ::GetDeviceCaps(GetHdc(), VERTSIZE);
1535 }
1536
1537 wxSize wxDC::GetPPI() const
1538 {
1539 int x = ::GetDeviceCaps(GetHdc(), LOGPIXELSX);
1540 int y = ::GetDeviceCaps(GetHdc(), LOGPIXELSY);
1541
1542 return wxSize(x, y);
1543 }
1544
1545 // For use by wxWindows only, unless custom units are required.
1546 void wxDC::SetLogicalScale(double x, double y)
1547 {
1548 m_logicalScaleX = x;
1549 m_logicalScaleY = y;
1550 }
1551
1552 #if WXWIN_COMPATIBILITY
1553 void wxDC::DoGetTextExtent(const wxString& string, float *x, float *y,
1554 float *descent, float *externalLeading,
1555 wxFont *theFont, bool use16bit) const
1556 {
1557 wxCoord x1, y1, descent1, externalLeading1;
1558 GetTextExtent(string, & x1, & y1, & descent1, & externalLeading1, theFont, use16bit);
1559 *x = x1; *y = y1;
1560 if (descent)
1561 *descent = descent1;
1562 if (externalLeading)
1563 *externalLeading = externalLeading1;
1564 }
1565 #endif
1566
1567 // ---------------------------------------------------------------------------
1568 // spline drawing code
1569 // ---------------------------------------------------------------------------
1570
1571 #if wxUSE_SPLINES
1572
1573 class wxSpline: public wxObject
1574 {
1575 public:
1576 int type;
1577 wxList *points;
1578
1579 wxSpline(wxList *list);
1580 void DeletePoints();
1581
1582 // Doesn't delete points
1583 ~wxSpline();
1584 };
1585
1586 void wx_draw_open_spline(wxDC *dc, wxSpline *spline);
1587
1588 void wx_quadratic_spline(double a1, double b1, double a2, double b2,
1589 double a3, double b3, double a4, double b4);
1590 void wx_clear_stack();
1591 int wx_spline_pop(double *x1, double *y1, double *x2, double *y2, double *x3,
1592 double *y3, double *x4, double *y4);
1593 void wx_spline_push(double x1, double y1, double x2, double y2, double x3, double y3,
1594 double x4, double y4);
1595 static bool wx_spline_add_point(double x, double y);
1596 static void wx_spline_draw_point_array(wxDC *dc);
1597 wxSpline *wx_make_spline(int x1, int y1, int x2, int y2, int x3, int y3);
1598
1599 void wxDC::DoDrawSpline(wxList *list)
1600 {
1601 wxSpline spline(list);
1602
1603 wx_draw_open_spline(this, &spline);
1604 }
1605
1606 wxList wx_spline_point_list;
1607
1608 void wx_draw_open_spline(wxDC *dc, wxSpline *spline)
1609 {
1610 wxPoint *p;
1611 double cx1, cy1, cx2, cy2, cx3, cy3, cx4, cy4;
1612 double x1, y1, x2, y2;
1613
1614 wxNode *node = spline->points->First();
1615 p = (wxPoint *)node->Data();
1616
1617 x1 = p->x;
1618 y1 = p->y;
1619
1620 node = node->Next();
1621 p = (wxPoint *)node->Data();
1622
1623 x2 = p->x;
1624 y2 = p->y;
1625 cx1 = (double)((x1 + x2) / 2);
1626 cy1 = (double)((y1 + y2) / 2);
1627 cx2 = (double)((cx1 + x2) / 2);
1628 cy2 = (double)((cy1 + y2) / 2);
1629
1630 wx_spline_add_point(x1, y1);
1631
1632 while ((node = node->Next()) != NULL)
1633 {
1634 p = (wxPoint *)node->Data();
1635 x1 = x2;
1636 y1 = y2;
1637 x2 = p->x;
1638 y2 = p->y;
1639 cx4 = (double)(x1 + x2) / 2;
1640 cy4 = (double)(y1 + y2) / 2;
1641 cx3 = (double)(x1 + cx4) / 2;
1642 cy3 = (double)(y1 + cy4) / 2;
1643
1644 wx_quadratic_spline(cx1, cy1, cx2, cy2, cx3, cy3, cx4, cy4);
1645
1646 cx1 = cx4;
1647 cy1 = cy4;
1648 cx2 = (double)(cx1 + x2) / 2;
1649 cy2 = (double)(cy1 + y2) / 2;
1650 }
1651
1652 wx_spline_add_point((double)wx_round(cx1), (double)wx_round(cy1));
1653 wx_spline_add_point(x2, y2);
1654
1655 wx_spline_draw_point_array(dc);
1656
1657 }
1658
1659 /********************* CURVES FOR SPLINES *****************************
1660
1661 The following spline drawing routine is from
1662
1663 "An Algorithm for High-Speed Curve Generation"
1664 by George Merrill Chaikin,
1665 Computer Graphics and Image Processing, 3, Academic Press,
1666 1974, 346-349.
1667
1668 and
1669
1670 "On Chaikin's Algorithm" by R. F. Riesenfeld,
1671 Computer Graphics and Image Processing, 4, Academic Press,
1672 1975, 304-310.
1673
1674 ***********************************************************************/
1675
1676 #define half(z1, z2) ((z1+z2)/2.0)
1677 #define THRESHOLD 5
1678
1679 /* iterative version */
1680
1681 void wx_quadratic_spline(double a1, double b1, double a2, double b2, double a3, double b3, double a4,
1682 double b4)
1683 {
1684 register double xmid, ymid;
1685 double x1, y1, x2, y2, x3, y3, x4, y4;
1686
1687 wx_clear_stack();
1688 wx_spline_push(a1, b1, a2, b2, a3, b3, a4, b4);
1689
1690 while (wx_spline_pop(&x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4)) {
1691 xmid = (double)half(x2, x3);
1692 ymid = (double)half(y2, y3);
1693 if (fabs(x1 - xmid) < THRESHOLD && fabs(y1 - ymid) < THRESHOLD &&
1694 fabs(xmid - x4) < THRESHOLD && fabs(ymid - y4) < THRESHOLD) {
1695 wx_spline_add_point((double)wx_round(x1), (double)wx_round(y1));
1696 wx_spline_add_point((double)wx_round(xmid), (double)wx_round(ymid));
1697 } else {
1698 wx_spline_push(xmid, ymid, (double)half(xmid, x3), (double)half(ymid, y3),
1699 (double)half(x3, x4), (double)half(y3, y4), x4, y4);
1700 wx_spline_push(x1, y1, (double)half(x1, x2), (double)half(y1, y2),
1701 (double)half(x2, xmid), (double)half(y2, ymid), xmid, ymid);
1702 }
1703 }
1704 }
1705
1706
1707 /* utilities used by spline drawing routines */
1708
1709
1710 typedef struct wx_spline_stack_struct {
1711 double x1, y1, x2, y2, x3, y3, x4, y4;
1712 }
1713 Stack;
1714
1715 #define SPLINE_STACK_DEPTH 20
1716 static Stack wx_spline_stack[SPLINE_STACK_DEPTH];
1717 static Stack *wx_stack_top;
1718 static int wx_stack_count;
1719
1720 void wx_clear_stack()
1721 {
1722 wx_stack_top = wx_spline_stack;
1723 wx_stack_count = 0;
1724 }
1725
1726 void wx_spline_push(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4)
1727 {
1728 wx_stack_top->x1 = x1;
1729 wx_stack_top->y1 = y1;
1730 wx_stack_top->x2 = x2;
1731 wx_stack_top->y2 = y2;
1732 wx_stack_top->x3 = x3;
1733 wx_stack_top->y3 = y3;
1734 wx_stack_top->x4 = x4;
1735 wx_stack_top->y4 = y4;
1736 wx_stack_top++;
1737 wx_stack_count++;
1738 }
1739
1740 int wx_spline_pop(double *x1, double *y1, double *x2, double *y2,
1741 double *x3, double *y3, double *x4, double *y4)
1742 {
1743 if (wx_stack_count == 0)
1744 return (0);
1745 wx_stack_top--;
1746 wx_stack_count--;
1747 *x1 = wx_stack_top->x1;
1748 *y1 = wx_stack_top->y1;
1749 *x2 = wx_stack_top->x2;
1750 *y2 = wx_stack_top->y2;
1751 *x3 = wx_stack_top->x3;
1752 *y3 = wx_stack_top->y3;
1753 *x4 = wx_stack_top->x4;
1754 *y4 = wx_stack_top->y4;
1755 return (1);
1756 }
1757
1758 static bool wx_spline_add_point(double x, double y)
1759 {
1760 wxPoint *point = new wxPoint;
1761 point->x = (int) x;
1762 point->y = (int) y;
1763 wx_spline_point_list.Append((wxObject*)point);
1764 return TRUE;
1765 }
1766
1767 static void wx_spline_draw_point_array(wxDC *dc)
1768 {
1769 dc->DrawLines(&wx_spline_point_list, 0, 0);
1770 wxNode *node = wx_spline_point_list.First();
1771 while (node)
1772 {
1773 wxPoint *point = (wxPoint *)node->Data();
1774 delete point;
1775 delete node;
1776 node = wx_spline_point_list.First();
1777 }
1778 }
1779
1780 wxSpline::wxSpline(wxList *list)
1781 {
1782 points = list;
1783 }
1784
1785 wxSpline::~wxSpline()
1786 {
1787 }
1788
1789 void wxSpline::DeletePoints()
1790 {
1791 for(wxNode *node = points->First(); node; node = points->First())
1792 {
1793 wxPoint *point = (wxPoint *)node->Data();
1794 delete point;
1795 delete node;
1796 }
1797 delete points;
1798 }
1799
1800
1801 #endif // wxUSE_SPLINES
1802