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