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