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