]> git.saurik.com Git - wxWidgets.git/blob - src/msw/dc.cpp
683d74d73b0ea1d8ac4e5989a44e855042116d0c
[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 #if wxUSE_NORLANDER_HEADERS
52 #include <windows.h>
53 #endif
54 #include <commdlg.h>
55 #endif
56
57 #ifndef __WIN32__
58 #include <print.h>
59 #endif
60
61 IMPLEMENT_ABSTRACT_CLASS(wxDC, wxObject)
62
63 // ---------------------------------------------------------------------------
64 // constants
65 // ---------------------------------------------------------------------------
66
67 static const int VIEWPORT_EXTENT = 1000;
68
69 static const int MM_POINTS = 9;
70 static const int MM_METRIC = 10;
71
72 // usually this is defined in math.h
73 #ifndef M_PI
74 static const double M_PI = 3.14159265358979323846;
75 #endif // M_PI
76
77 // ---------------------------------------------------------------------------
78 // private functions
79 // ---------------------------------------------------------------------------
80
81 // convert degrees to radians
82 static inline double DegToRad(double deg) { return (deg * M_PI) / 180.0; }
83
84 // ===========================================================================
85 // implementation
86 // ===========================================================================
87
88 // ---------------------------------------------------------------------------
89 // wxDC
90 // ---------------------------------------------------------------------------
91
92 // Default constructor
93 wxDC::wxDC()
94 {
95 m_canvas = NULL;
96
97 m_oldBitmap = 0;
98 m_oldPen = 0;
99 m_oldBrush = 0;
100 m_oldFont = 0;
101 m_oldPalette = 0;
102
103 m_bOwnsDC = FALSE;
104 m_hDC = 0;
105
106 m_windowExtX = VIEWPORT_EXTENT;
107 m_windowExtY = VIEWPORT_EXTENT;
108
109 m_hDCCount = 0;
110 }
111
112
113 wxDC::~wxDC()
114 {
115 if ( m_hDC != 0 ) {
116 SelectOldObjects(m_hDC);
117 if ( m_bOwnsDC ) {
118 if ( m_canvas == NULL )
119 ::DeleteDC(GetHdc());
120 else
121 ::ReleaseDC((HWND)m_canvas->GetHWND(), GetHdc());
122 }
123 }
124
125 }
126
127 // This will select current objects out of the DC,
128 // which is what you have to do before deleting the
129 // DC.
130 void wxDC::SelectOldObjects(WXHDC dc)
131 {
132 if (dc)
133 {
134 if (m_oldBitmap)
135 {
136 ::SelectObject((HDC) dc, (HBITMAP) m_oldBitmap);
137 if (m_selectedBitmap.Ok())
138 {
139 m_selectedBitmap.SetSelectedInto(NULL);
140 }
141 }
142 m_oldBitmap = 0;
143 if (m_oldPen)
144 {
145 ::SelectObject((HDC) dc, (HPEN) m_oldPen);
146 }
147 m_oldPen = 0;
148 if (m_oldBrush)
149 {
150 ::SelectObject((HDC) dc, (HBRUSH) m_oldBrush);
151 }
152 m_oldBrush = 0;
153 if (m_oldFont)
154 {
155 ::SelectObject((HDC) dc, (HFONT) m_oldFont);
156 }
157 m_oldFont = 0;
158 if (m_oldPalette)
159 {
160 ::SelectPalette((HDC) dc, (HPALETTE) m_oldPalette, TRUE);
161 }
162 m_oldPalette = 0;
163 }
164
165 m_brush = wxNullBrush;
166 m_pen = wxNullPen;
167 m_palette = wxNullPalette;
168 m_font = wxNullFont;
169 m_backgroundBrush = wxNullBrush;
170 m_selectedBitmap = wxNullBitmap;
171 }
172
173 // ---------------------------------------------------------------------------
174 // clipping
175 // ---------------------------------------------------------------------------
176
177 void wxDC::DoSetClippingRegion(wxCoord cx, wxCoord cy, wxCoord cw, wxCoord ch)
178 {
179 m_clipping = TRUE;
180 m_clipX1 = (int)cx;
181 m_clipY1 = (int)cy;
182 m_clipX2 = (int)(cx + cw);
183 m_clipY2 = (int)(cy + ch);
184
185 DoClipping((WXHDC) m_hDC);
186 }
187
188 void wxDC::DoSetClippingRegionAsRegion(const wxRegion& region)
189 {
190 wxCHECK_RET( region.GetHRGN(), wxT("invalid clipping region") );
191
192 wxRect box = region.GetBox();
193
194 m_clipping = TRUE;
195 m_clipX1 = box.x;
196 m_clipY1 = box.y;
197 m_clipX2 = box.x + box.width;
198 m_clipY2 = box.y + box.height;
199
200 #ifdef __WIN16__
201 SelectClipRgn(GetHdc(), (HRGN) region.GetHRGN());
202 #else
203 ExtSelectClipRgn(GetHdc(), (HRGN) region.GetHRGN(), RGN_AND);
204 #endif
205 }
206
207 void wxDC::DoClipping(WXHDC dc)
208 {
209 if (m_clipping && dc)
210 {
211 IntersectClipRect((HDC) dc, XLOG2DEV(m_clipX1), YLOG2DEV(m_clipY1),
212 XLOG2DEV(m_clipX2), YLOG2DEV(m_clipY2));
213 }
214 }
215
216 void wxDC::DestroyClippingRegion()
217 {
218 if (m_clipping && m_hDC)
219 {
220 // TODO: this should restore the previous clipping region,
221 // so that OnPaint processing works correctly, and the update clipping region
222 // doesn't get destroyed after the first DestroyClippingRegion.
223 HRGN rgn = CreateRectRgn(0, 0, 32000, 32000);
224 SelectClipRgn(GetHdc(), rgn);
225 DeleteObject(rgn);
226 }
227 m_clipping = FALSE;
228 }
229
230 // ---------------------------------------------------------------------------
231 // query capabilities
232 // ---------------------------------------------------------------------------
233
234 bool wxDC::CanDrawBitmap() const
235 {
236 return TRUE;
237 }
238
239 bool wxDC::CanGetTextExtent() const
240 {
241 // What sort of display is it?
242 int technology = ::GetDeviceCaps(GetHdc(), TECHNOLOGY);
243
244 return (technology == DT_RASDISPLAY) || (technology == DT_RASPRINTER);
245 }
246
247 int wxDC::GetDepth() const
248 {
249 return (int)::GetDeviceCaps(GetHdc(), BITSPIXEL);
250 }
251
252 // ---------------------------------------------------------------------------
253 // drawing
254 // ---------------------------------------------------------------------------
255
256 void wxDC::Clear()
257 {
258 RECT rect;
259 if ( m_canvas )
260 {
261 GetClientRect((HWND) m_canvas->GetHWND(), &rect);
262 }
263 else
264 {
265 // No, I think we should simply ignore this if printing on e.g.
266 // a printer DC.
267 // wxCHECK_RET( m_selectedBitmap.Ok(), wxT("this DC can't be cleared") );
268 if (!m_selectedBitmap.Ok())
269 return;
270
271 rect.left = 0; rect.top = 0;
272 rect.right = m_selectedBitmap.GetWidth();
273 rect.bottom = m_selectedBitmap.GetHeight();
274 }
275
276 (void) ::SetMapMode(GetHdc(), MM_TEXT);
277
278 DWORD colour = GetBkColor(GetHdc());
279 HBRUSH brush = CreateSolidBrush(colour);
280 FillRect(GetHdc(), &rect, brush);
281 DeleteObject(brush);
282
283 ::SetMapMode(GetHdc(), MM_ANISOTROPIC);
284 ::SetViewportExtEx(GetHdc(), VIEWPORT_EXTENT, VIEWPORT_EXTENT, NULL);
285 ::SetWindowExtEx(GetHdc(), m_windowExtX, m_windowExtY, NULL);
286 ::SetViewportOrgEx(GetHdc(), (int)m_deviceOriginX, (int)m_deviceOriginY, NULL);
287 ::SetWindowOrgEx(GetHdc(), (int)m_logicalOriginX, (int)m_logicalOriginY, NULL);
288 }
289
290 void wxDC::DoFloodFill(wxCoord x, wxCoord y, const wxColour& col, int style)
291 {
292 if ( !::ExtFloodFill(GetHdc(), XLOG2DEV(x), YLOG2DEV(y),
293 col.GetPixel(),
294 style == wxFLOOD_SURFACE ? FLOODFILLSURFACE
295 : FLOODFILLBORDER) )
296 {
297 // quoting from the MSDN docs:
298 //
299 // Following are some of the reasons this function might fail:
300 //
301 // * The filling could not be completed.
302 // * The specified point has the boundary color specified by the
303 // crColor parameter (if FLOODFILLBORDER was requested).
304 // * The specified point does not have the color specified by
305 // crColor (if FLOODFILLSURFACE was requested)
306 // * The point is outside the clipping region that is, it is not
307 // visible on the device.
308 //
309 wxLogLastError("ExtFloodFill");
310 }
311
312 CalcBoundingBox(x, y);
313 }
314
315 bool wxDC::DoGetPixel(wxCoord x, wxCoord y, wxColour *col) const
316 {
317 // get the color of the pixel
318 COLORREF pixelcolor = ::GetPixel(GetHdc(), XLOG2DEV(x), YLOG2DEV(y));
319
320 // get the color of the pen
321 COLORREF pencolor = 0x00ffffff;
322 if (m_pen.Ok())
323 {
324 pencolor = m_pen.GetColour().GetPixel();
325 }
326
327 // return the color of the pixel
328 if( col )
329 {
330 col->Set(GetRValue(pixelcolor),
331 GetGValue(pixelcolor),
332 GetBValue(pixelcolor));
333 }
334
335 // check, if color of the pixels is the same as the color of the current
336 // pen and return TRUE if it is, FALSE otherwise
337 return pixelcolor == pencolor;
338 }
339
340 void wxDC::DoCrossHair(wxCoord x, wxCoord y)
341 {
342 wxCoord x1 = x-VIEWPORT_EXTENT;
343 wxCoord y1 = y-VIEWPORT_EXTENT;
344 wxCoord x2 = x+VIEWPORT_EXTENT;
345 wxCoord y2 = y+VIEWPORT_EXTENT;
346
347 (void)MoveToEx(GetHdc(), XLOG2DEV(x1), YLOG2DEV(y), NULL);
348 (void)LineTo(GetHdc(), XLOG2DEV(x2), YLOG2DEV(y));
349
350 (void)MoveToEx(GetHdc(), XLOG2DEV(x), YLOG2DEV(y1), NULL);
351 (void)LineTo(GetHdc(), XLOG2DEV(x), YLOG2DEV(y2));
352
353 CalcBoundingBox(x1, y1);
354 CalcBoundingBox(x2, y2);
355 }
356
357 void wxDC::DoDrawLine(wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2)
358 {
359 (void)MoveToEx(GetHdc(), XLOG2DEV(x1), YLOG2DEV(y1), NULL);
360 (void)LineTo(GetHdc(), XLOG2DEV(x2), YLOG2DEV(y2));
361
362 /* MATTHEW: [6] New normalization */
363 #if WX_STANDARD_GRAPHICS
364 (void)LineTo(GetHdc(), XLOG2DEV(x2) + 1, YLOG2DEV(y2));
365 #endif
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::DoDrawPoint(wxCoord x, wxCoord y)
414 {
415 COLORREF color = 0x00ffffff;
416 if (m_pen.Ok())
417 {
418 color = m_pen.GetColour().GetPixel();
419 }
420
421 SetPixel(GetHdc(), XLOG2DEV(x), YLOG2DEV(y), color);
422
423 CalcBoundingBox(x, y);
424 }
425
426 void wxDC::DoDrawPolygon(int n, wxPoint points[], wxCoord xoffset, wxCoord yoffset,int fillStyle)
427 {
428 COLORREF old_textground = ::GetTextColor(GetHdc());
429 COLORREF old_background = ::GetBkColor(GetHdc());
430 if (m_brush.GetStyle() == wxSTIPPLE_MASK_OPAQUE)
431 {
432
433 if (m_textForegroundColour.Ok())
434 { //just the oposite from what is expected see help on pattern brush
435 // 1 in mask becomes bk color
436 ::SetBkColor(GetHdc(), m_textForegroundColour.GetPixel() );
437 }
438 if (m_textBackgroundColour.Ok())
439 { //just the oposite from what is expected
440 // 0 in mask becomes text color
441 ::SetTextColor(GetHdc(), m_textBackgroundColour.GetPixel() );
442 }
443
444 if (m_backgroundMode == wxTRANSPARENT)
445 SetBkMode(GetHdc(), TRANSPARENT);
446 else
447 SetBkMode(GetHdc(), OPAQUE);
448 }
449
450 // Do things less efficiently if we have offsets
451 if (xoffset != 0 || yoffset != 0)
452 {
453 POINT *cpoints = new POINT[n];
454 int i;
455 for (i = 0; i < n; i++)
456 {
457 cpoints[i].x = (int)(points[i].x + xoffset);
458 cpoints[i].y = (int)(points[i].y + yoffset);
459
460 CalcBoundingBox(cpoints[i].x, cpoints[i].y);
461 }
462 int prev = SetPolyFillMode(GetHdc(),fillStyle==wxODDEVEN_RULE?ALTERNATE:WINDING);
463 (void)Polygon(GetHdc(), cpoints, n);
464 SetPolyFillMode(GetHdc(),prev);
465 delete[] cpoints;
466 }
467 else
468 {
469 int i;
470 for (i = 0; i < n; i++)
471 CalcBoundingBox(points[i].x, points[i].y);
472
473 int prev = SetPolyFillMode(GetHdc(),fillStyle==wxODDEVEN_RULE?ALTERNATE:WINDING);
474 (void)Polygon(GetHdc(), (POINT*) points, n);
475 SetPolyFillMode(GetHdc(),prev);
476 }
477
478 if (m_brush.GetStyle() == wxSTIPPLE_MASK_OPAQUE)
479 {
480 ::SetBkMode(GetHdc(), TRANSPARENT);
481 ::SetTextColor(GetHdc(), old_textground);
482 ::SetBkColor(GetHdc(), old_background);
483 }
484 }
485
486 void wxDC::DoDrawLines(int n, wxPoint points[], wxCoord xoffset, wxCoord yoffset)
487 {
488 // Do things less efficiently if we have offsets
489 if (xoffset != 0 || yoffset != 0)
490 {
491 POINT *cpoints = new POINT[n];
492 int i;
493 for (i = 0; i < n; i++)
494 {
495 cpoints[i].x = (int)(points[i].x + xoffset);
496 cpoints[i].y = (int)(points[i].y + yoffset);
497
498 CalcBoundingBox(cpoints[i].x, cpoints[i].y);
499 }
500 (void)Polyline(GetHdc(), cpoints, n);
501 delete[] cpoints;
502 }
503 else
504 {
505 int i;
506 for (i = 0; i < n; i++)
507 CalcBoundingBox(points[i].x, points[i].y);
508
509 (void)Polyline(GetHdc(), (POINT*) points, n);
510 }
511 }
512
513 void wxDC::DoDrawRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
514 {
515 COLORREF colFgOld = 0,
516 colBgOld = 0;
517
518 if ( m_brush.GetStyle() == wxSTIPPLE_MASK_OPAQUE )
519 {
520 colFgOld = ::GetTextColor(GetHdc());
521 colBgOld = ::GetBkColor(GetHdc());
522
523 if ( m_textForegroundColour.Ok() )
524 {
525 // just the oposite from what is expected see help on pattern brush
526 // 1 in mask becomes bk color
527 ::SetBkColor(GetHdc(), m_textForegroundColour.GetPixel());
528 }
529
530 if ( m_textBackgroundColour.Ok() )
531 {
532 // 0 in mask becomes text color
533 ::SetTextColor(GetHdc(), m_textBackgroundColour.GetPixel());
534 }
535
536 // VZ: IMHO this does strictly nothing here
537 SetBkMode(GetHdc(), m_backgroundMode == wxTRANSPARENT ? TRANSPARENT
538 : OPAQUE);
539 }
540
541 wxCoord x2 = x + width;
542 wxCoord y2 = y + height;
543
544 // Windows draws the filled rectangles without outline (i.e. drawn with a
545 // transparent pen) one pixel smaller in both directions and we want them
546 // to have the same size regardless of which pen is used - adjust
547 if ( m_pen.GetStyle() == wxTRANSPARENT )
548 {
549 x2++;
550 y2++;
551 }
552
553 (void)Rectangle(GetHdc(), XLOG2DEV(x), YLOG2DEV(y), XLOG2DEV(x2), YLOG2DEV(y2));
554
555 CalcBoundingBox(x, y);
556 CalcBoundingBox(x2, y2);
557
558 if ( m_brush.GetStyle() == wxSTIPPLE_MASK_OPAQUE )
559 {
560 // restore the colours we changed
561 ::SetBkMode(GetHdc(), TRANSPARENT);
562 ::SetTextColor(GetHdc(), colFgOld);
563 ::SetBkColor(GetHdc(), colBgOld);
564 }
565 }
566
567 void wxDC::DoDrawRoundedRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height, double radius)
568 {
569 // Now, a negative radius value is interpreted to mean
570 // 'the proportion of the smallest X or Y dimension'
571
572 if (radius < 0.0)
573 {
574 double smallest = 0.0;
575 if (width < height)
576 smallest = width;
577 else
578 smallest = height;
579 radius = (- radius * smallest);
580 }
581
582 wxCoord x2 = (x+width);
583 wxCoord y2 = (y+height);
584
585 (void)RoundRect(GetHdc(), XLOG2DEV(x), YLOG2DEV(y), XLOG2DEV(x2),
586 YLOG2DEV(y2), (int) (2*XLOG2DEV(radius)), (int)( 2*YLOG2DEV(radius)));
587
588 CalcBoundingBox(x, y);
589 CalcBoundingBox(x2, y2);
590 }
591
592 void wxDC::DoDrawEllipse(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
593 {
594 wxCoord x2 = (x+width);
595 wxCoord y2 = (y+height);
596
597 (void)Ellipse(GetHdc(), XLOG2DEV(x), YLOG2DEV(y), XLOG2DEV(x2), YLOG2DEV(y2));
598
599 CalcBoundingBox(x, y);
600 CalcBoundingBox(x2, y2);
601 }
602
603 // Chris Breeze 20/5/98: first implementation of DrawEllipticArc on Windows
604 void wxDC::DoDrawEllipticArc(wxCoord x,wxCoord y,wxCoord w,wxCoord h,double sa,double ea)
605 {
606 wxCoord x2 = (x+w);
607 wxCoord y2 = (y+h);
608
609 int rx1 = XLOG2DEV(x+w/2);
610 int ry1 = YLOG2DEV(y+h/2);
611 int rx2 = rx1;
612 int ry2 = ry1;
613
614 sa = DegToRad(sa);
615 ea = DegToRad(ea);
616
617 rx1 += (int)(100.0 * abs(w) * cos(sa));
618 ry1 -= (int)(100.0 * abs(h) * m_signY * sin(sa));
619 rx2 += (int)(100.0 * abs(w) * cos(ea));
620 ry2 -= (int)(100.0 * abs(h) * m_signY * sin(ea));
621
622 // draw pie with NULL_PEN first and then outline otherwise a line is
623 // drawn from the start and end points to the centre
624 HPEN orig_pen = (HPEN) ::SelectObject(GetHdc(), (HPEN) ::GetStockObject(NULL_PEN));
625 if (m_signY > 0)
626 {
627 (void)Pie(GetHdc(), XLOG2DEV(x), YLOG2DEV(y), XLOG2DEV(x2)+1, YLOG2DEV(y2)+1,
628 rx1, ry1, rx2, ry2);
629 }
630 else
631 {
632 (void)Pie(GetHdc(), XLOG2DEV(x), YLOG2DEV(y)-1, XLOG2DEV(x2)+1, YLOG2DEV(y2),
633 rx1, ry1-1, rx2, ry2-1);
634 }
635 ::SelectObject(GetHdc(), orig_pen);
636 (void)Arc(GetHdc(), XLOG2DEV(x), YLOG2DEV(y), XLOG2DEV(x2), YLOG2DEV(y2),
637 rx1, ry1, rx2, ry2);
638
639 CalcBoundingBox(x, y);
640 CalcBoundingBox(x2, y2);
641 }
642
643 void wxDC::DoDrawIcon(const wxIcon& icon, wxCoord x, wxCoord y)
644 {
645 wxCHECK_RET( icon.Ok(), wxT("invalid icon in DrawIcon") );
646
647 ::DrawIcon(GetHdc(), XLOG2DEV(x), YLOG2DEV(y), GetHiconOf(icon));
648
649 CalcBoundingBox(x, y);
650 CalcBoundingBox(x + icon.GetWidth(), y + icon.GetHeight());
651 }
652
653 void wxDC::DoDrawBitmap( const wxBitmap &bmp, wxCoord x, wxCoord y, bool useMask )
654 {
655 wxCHECK_RET( bmp.Ok(), _T("invalid bitmap in wxDC::DrawBitmap") );
656
657 int width = bmp.GetWidth(),
658 height = bmp.GetHeight();
659
660 HBITMAP hbmpMask = 0;
661
662 if ( useMask )
663 {
664 wxMask *mask = bmp.GetMask();
665 if ( mask )
666 hbmpMask = (HBITMAP)mask->GetMaskBitmap();
667
668 if ( !hbmpMask )
669 {
670 // don't give assert here because this would break existing
671 // programs - just silently ignore useMask parameter
672 useMask = FALSE;
673 }
674 }
675
676 if ( useMask )
677 {
678 #ifdef __WIN32__
679 HDC hdcMem = ::CreateCompatibleDC(GetHdc());
680 ::SelectObject(hdcMem, GetHbitmapOf(bmp));
681
682 // this will only work if the transparent part of our bitmap is black
683 // because it is combined with the destination rectangle using OR, so
684 // it won't be really transparent otherwise - I don't know what to do
685 // about it, may be use MAKEROP4(SRCCOPY, DSTINVERT) twice? Or create a
686 // copy of the bitmap with the transparent part replaced with black
687 // pixels?
688 bool ok = ::MaskBlt(GetHdc(), x, y, width, height,
689 hdcMem, 0, 0,
690 hbmpMask, 0, 0,
691 MAKEROP4(SRCCOPY, 0x00AA0029)) != 0;
692 ::DeleteDC(hdcMem);
693
694 if ( !ok )
695 #endif // Win32
696 {
697 // VZ: this is incorrect, Blit() doesn't (and can't) draw
698 // transparently, but it's still better than nothing at all
699
700 // GRG: Blit() *should* draw transparently when there is a mask.
701
702 // Rather than reproduce wxDC::Blit, let's do it at the wxWin API 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 wxXOR: rop = R2_XORPEN; break;
1049 case wxINVERT: rop = R2_NOT; break;
1050 case wxOR_REVERSE: rop = R2_MERGEPENNOT; break;
1051 case wxAND_REVERSE: rop = R2_MASKPENNOT; break;
1052 case wxCLEAR: rop = R2_WHITE; break;
1053 case wxSET: rop = R2_BLACK; break;
1054 case wxOR_INVERT: rop = R2_MERGENOTPEN; break;
1055 case wxAND: rop = R2_MASKPEN; break;
1056 case wxOR: rop = R2_MERGEPEN; break;
1057 case wxEQUIV: rop = R2_NOTXORPEN; break;
1058 case wxNAND: rop = R2_NOTMASKPEN; break;
1059 case wxAND_INVERT: rop = R2_MASKNOTPEN; break;
1060 case wxCOPY: rop = R2_COPYPEN; break;
1061 case wxNO_OP: rop = R2_NOP; break;
1062 case wxSRC_INVERT: rop = R2_NOTCOPYPEN; break;
1063 case wxNOR: rop = R2_NOTMERGEPEN; break;
1064 default:
1065 wxFAIL_MSG( wxT("unsupported logical function") );
1066 return;
1067 }
1068
1069 SetROP2(GetHdc(), rop);
1070 }
1071
1072 bool wxDC::StartDoc(const wxString& message)
1073 {
1074 // We might be previewing, so return TRUE to let it continue.
1075 return TRUE;
1076 }
1077
1078 void wxDC::EndDoc()
1079 {
1080 }
1081
1082 void wxDC::StartPage()
1083 {
1084 }
1085
1086 void wxDC::EndPage()
1087 {
1088 }
1089
1090 // ---------------------------------------------------------------------------
1091 // text metrics
1092 // ---------------------------------------------------------------------------
1093
1094 wxCoord wxDC::GetCharHeight() const
1095 {
1096 TEXTMETRIC lpTextMetric;
1097
1098 GetTextMetrics(GetHdc(), &lpTextMetric);
1099
1100 return YDEV2LOGREL(lpTextMetric.tmHeight);
1101 }
1102
1103 wxCoord wxDC::GetCharWidth() const
1104 {
1105 TEXTMETRIC lpTextMetric;
1106
1107 GetTextMetrics(GetHdc(), &lpTextMetric);
1108
1109 return XDEV2LOGREL(lpTextMetric.tmAveCharWidth);
1110 }
1111
1112 void wxDC::DoGetTextExtent(const wxString& string, wxCoord *x, wxCoord *y,
1113 wxCoord *descent, wxCoord *externalLeading,
1114 wxFont *theFont) const
1115 {
1116 wxFont *fontToUse = (wxFont*) theFont;
1117 if (!fontToUse)
1118 fontToUse = (wxFont*) &m_font;
1119
1120 SIZE sizeRect;
1121 TEXTMETRIC tm;
1122
1123 GetTextExtentPoint(GetHdc(), WXSTRINGCAST string, wxStrlen(WXSTRINGCAST string), &sizeRect);
1124 GetTextMetrics(GetHdc(), &tm);
1125
1126 if (x) *x = XDEV2LOGREL(sizeRect.cx);
1127 if (y) *y = YDEV2LOGREL(sizeRect.cy);
1128 if (descent) *descent = tm.tmDescent;
1129 if (externalLeading) *externalLeading = tm.tmExternalLeading;
1130 }
1131
1132 void wxDC::SetMapMode(int mode)
1133 {
1134 m_mappingMode = mode;
1135
1136 int pixel_width = 0;
1137 int pixel_height = 0;
1138 int mm_width = 0;
1139 int mm_height = 0;
1140
1141 pixel_width = GetDeviceCaps(GetHdc(), HORZRES);
1142 pixel_height = GetDeviceCaps(GetHdc(), VERTRES);
1143 mm_width = GetDeviceCaps(GetHdc(), HORZSIZE);
1144 mm_height = GetDeviceCaps(GetHdc(), VERTSIZE);
1145
1146 if ((pixel_width == 0) || (pixel_height == 0) || (mm_width == 0) || (mm_height == 0))
1147 {
1148 return;
1149 }
1150
1151 double mm2pixelsX = pixel_width/mm_width;
1152 double mm2pixelsY = pixel_height/mm_height;
1153
1154 switch (mode)
1155 {
1156 case wxMM_TWIPS:
1157 {
1158 m_logicalScaleX = (twips2mm * mm2pixelsX);
1159 m_logicalScaleY = (twips2mm * mm2pixelsY);
1160 break;
1161 }
1162 case wxMM_POINTS:
1163 {
1164 m_logicalScaleX = (pt2mm * mm2pixelsX);
1165 m_logicalScaleY = (pt2mm * mm2pixelsY);
1166 break;
1167 }
1168 case wxMM_METRIC:
1169 {
1170 m_logicalScaleX = mm2pixelsX;
1171 m_logicalScaleY = mm2pixelsY;
1172 break;
1173 }
1174 case wxMM_LOMETRIC:
1175 {
1176 m_logicalScaleX = (mm2pixelsX/10.0);
1177 m_logicalScaleY = (mm2pixelsY/10.0);
1178 break;
1179 }
1180 default:
1181 case wxMM_TEXT:
1182 {
1183 m_logicalScaleX = 1.0;
1184 m_logicalScaleY = 1.0;
1185 break;
1186 }
1187 }
1188
1189 if (::GetMapMode(GetHdc()) != MM_ANISOTROPIC)
1190 ::SetMapMode(GetHdc(), MM_ANISOTROPIC);
1191
1192 SetViewportExtEx(GetHdc(), VIEWPORT_EXTENT, VIEWPORT_EXTENT, NULL);
1193 m_windowExtX = (int)MS_XDEV2LOGREL(VIEWPORT_EXTENT);
1194 m_windowExtY = (int)MS_YDEV2LOGREL(VIEWPORT_EXTENT);
1195 ::SetWindowExtEx(GetHdc(), m_windowExtX, m_windowExtY, NULL);
1196 ::SetViewportOrgEx(GetHdc(), (int)m_deviceOriginX, (int)m_deviceOriginY, NULL);
1197 ::SetWindowOrgEx(GetHdc(), (int)m_logicalOriginX, (int)m_logicalOriginY, NULL);
1198 }
1199
1200 void wxDC::SetUserScale(double x, double y)
1201 {
1202 m_userScaleX = x;
1203 m_userScaleY = y;
1204
1205 SetMapMode(m_mappingMode);
1206 }
1207
1208 void wxDC::SetAxisOrientation(bool xLeftRight, bool yBottomUp)
1209 {
1210 m_signX = xLeftRight ? 1 : -1;
1211 m_signY = yBottomUp ? -1 : 1;
1212
1213 SetMapMode(m_mappingMode);
1214 }
1215
1216 void wxDC::SetSystemScale(double x, double y)
1217 {
1218 m_scaleX = x;
1219 m_scaleY = y;
1220
1221 SetMapMode(m_mappingMode);
1222 }
1223
1224 void wxDC::SetLogicalOrigin(wxCoord x, wxCoord y)
1225 {
1226 m_logicalOriginX = x;
1227 m_logicalOriginY = y;
1228
1229 ::SetWindowOrgEx(GetHdc(), (int)m_logicalOriginX, (int)m_logicalOriginY, NULL);
1230 }
1231
1232 void wxDC::SetDeviceOrigin(wxCoord x, wxCoord y)
1233 {
1234 m_deviceOriginX = x;
1235 m_deviceOriginY = y;
1236
1237 ::SetViewportOrgEx(GetHdc(), (int)m_deviceOriginX, (int)m_deviceOriginY, NULL);
1238 }
1239
1240 // ---------------------------------------------------------------------------
1241 // coordinates transformations
1242 // ---------------------------------------------------------------------------
1243
1244 wxCoord wxDCBase::DeviceToLogicalX(wxCoord x) const
1245 {
1246 return (wxCoord) (((x) - m_deviceOriginX)/(m_logicalScaleX*m_userScaleX*m_signX*m_scaleX) - m_logicalOriginX);
1247 }
1248
1249 wxCoord wxDCBase::DeviceToLogicalXRel(wxCoord x) const
1250 {
1251 return (wxCoord) ((x)/(m_logicalScaleX*m_userScaleX*m_signX*m_scaleX));
1252 }
1253
1254 wxCoord wxDCBase::DeviceToLogicalY(wxCoord y) const
1255 {
1256 return (wxCoord) (((y) - m_deviceOriginY)/(m_logicalScaleY*m_userScaleY*m_signY*m_scaleY) - m_logicalOriginY);
1257 }
1258
1259 wxCoord wxDCBase::DeviceToLogicalYRel(wxCoord y) const
1260 {
1261 return (wxCoord) ((y)/(m_logicalScaleY*m_userScaleY*m_signY*m_scaleY));
1262 }
1263
1264 wxCoord wxDCBase::LogicalToDeviceX(wxCoord x) const
1265 {
1266 return (wxCoord) ((x - m_logicalOriginX)*m_logicalScaleX*m_userScaleX*m_signX*m_scaleX + m_deviceOriginX);
1267 }
1268
1269 wxCoord wxDCBase::LogicalToDeviceXRel(wxCoord x) const
1270 {
1271 return (wxCoord) (x*m_logicalScaleX*m_userScaleX*m_signX*m_scaleX);
1272 }
1273
1274 wxCoord wxDCBase::LogicalToDeviceY(wxCoord y) const
1275 {
1276 return (wxCoord) ((y - m_logicalOriginY)*m_logicalScaleY*m_userScaleY*m_signY*m_scaleY + m_deviceOriginY);
1277 }
1278
1279 wxCoord wxDCBase::LogicalToDeviceYRel(wxCoord y) const
1280 {
1281 return (wxCoord) (y*m_logicalScaleY*m_userScaleY*m_signY*m_scaleY);
1282 }
1283
1284 // ---------------------------------------------------------------------------
1285 // bit blit
1286 // ---------------------------------------------------------------------------
1287
1288 bool wxDC::DoBlit(wxCoord xdest, wxCoord ydest,
1289 wxCoord width, wxCoord height,
1290 wxDC *source, wxCoord xsrc, wxCoord ysrc,
1291 int rop, bool useMask)
1292 {
1293 wxMask *mask = NULL;
1294 if ( useMask )
1295 {
1296 const wxBitmap& bmp = source->m_selectedBitmap;
1297 mask = bmp.GetMask();
1298
1299 if ( !(bmp.Ok() && mask && mask->GetMaskBitmap()) )
1300 {
1301 // don't give assert here because this would break existing
1302 // programs - just silently ignore useMask parameter
1303 useMask = FALSE;
1304 }
1305 }
1306
1307 COLORREF old_textground = ::GetTextColor(GetHdc());
1308 COLORREF old_background = ::GetBkColor(GetHdc());
1309 if (m_textForegroundColour.Ok())
1310 {
1311 ::SetTextColor(GetHdc(), m_textForegroundColour.GetPixel() );
1312 }
1313 if (m_textBackgroundColour.Ok())
1314 {
1315 ::SetBkColor(GetHdc(), m_textBackgroundColour.GetPixel() );
1316 }
1317
1318 DWORD dwRop = SRCCOPY;
1319 switch (rop)
1320 {
1321 case wxXOR: dwRop = SRCINVERT; break;
1322 case wxINVERT: dwRop = DSTINVERT; break;
1323 case wxOR_REVERSE: dwRop = 0x00DD0228; break;
1324 case wxAND_REVERSE: dwRop = SRCERASE; break;
1325 case wxCLEAR: dwRop = BLACKNESS; break;
1326 case wxSET: dwRop = WHITENESS; break;
1327 case wxOR_INVERT: dwRop = MERGEPAINT; break;
1328 case wxAND: dwRop = SRCAND; break;
1329 case wxOR: dwRop = SRCPAINT; break;
1330 case wxEQUIV: dwRop = 0x00990066; break;
1331 case wxNAND: dwRop = 0x007700E6; break;
1332 case wxAND_INVERT: dwRop = 0x00220326; break;
1333 case wxCOPY: dwRop = SRCCOPY; break;
1334 case wxNO_OP: dwRop = 0x00AA0029; break;
1335 case wxSRC_INVERT: dwRop = NOTSRCCOPY; break;
1336 case wxNOR: dwRop = NOTSRCCOPY; break;
1337 default:
1338 wxFAIL_MSG( wxT("unsupported logical function") );
1339 return FALSE;
1340 }
1341
1342
1343 bool success;
1344
1345 if (useMask)
1346 {
1347 #ifdef __WIN32__
1348 // prepare the mask bitmap
1349 HBITMAP hbmpMask = wxInvertMask((HBITMAP)mask->GetMaskBitmap());
1350
1351 // select the correct brush: the current one by default, background one
1352 // if none
1353 HBRUSH hbrNew;
1354 if ( m_brush.Ok() )
1355 {
1356 hbrNew = (HBRUSH)m_brush.GetResourceHandle();
1357 }
1358 else if ( m_backgroundBrush.Ok() )
1359 {
1360 hbrNew = (HBRUSH)m_backgroundBrush.GetResourceHandle();
1361 }
1362 else
1363 {
1364 hbrNew = 0;
1365 }
1366
1367 HGDIOBJ hbrOld = hbrNew ? ::SelectObject(GetHdc(), hbrNew) : 0;
1368
1369 // we want the part of the image corresponding to the mask to be
1370 // transparent, i.e. do PATCOPY there and apply dwRop elsewhere
1371
1372 success = ::MaskBlt(GetHdc(), xdest, ydest, width, height,
1373 GetHdcOf(*source), xsrc, ysrc,
1374 hbmpMask, 0, 0,
1375 MAKEROP4(0x00AA0029, dwRop)) != 0;
1376
1377 if ( hbrNew )
1378 {
1379 (void)::SelectObject(GetHdc(), hbrOld);
1380 }
1381
1382 ::DeleteObject(hbmpMask);
1383
1384 if ( !success )
1385 #endif // Win32
1386 {
1387 // Blit bitmap with mask
1388
1389 // create a temp buffer bitmap and DCs to access it and the mask
1390 HDC dc_mask = ::CreateCompatibleDC(GetHdcOf(*source));
1391 HDC dc_buffer = ::CreateCompatibleDC(GetHdc());
1392 HBITMAP buffer_bmap = ::CreateCompatibleBitmap(GetHdc(), width, height);
1393 ::SelectObject(dc_mask, (HBITMAP) mask->GetMaskBitmap());
1394 ::SelectObject(dc_buffer, buffer_bmap);
1395
1396 // copy dest to buffer
1397 if ( !::BitBlt(dc_buffer, 0, 0, (int)width, (int)height,
1398 GetHdc(), xdest, ydest, SRCCOPY) )
1399 {
1400 wxLogLastError("BitBlt");
1401 }
1402
1403 // copy src to buffer using selected raster op
1404 if ( !::BitBlt(dc_buffer, 0, 0, (int)width, (int)height,
1405 GetHdcOf(*source), xsrc, ysrc, dwRop) )
1406 {
1407 wxLogLastError("BitBlt");
1408 }
1409
1410 // set masked area in buffer to BLACK (pixel value 0)
1411 COLORREF prevBkCol = ::SetBkColor(GetHdc(), RGB(255, 255, 255));
1412 COLORREF prevCol = ::SetTextColor(GetHdc(), RGB(0, 0, 0));
1413 if ( !::BitBlt(dc_buffer, 0, 0, (int)width, (int)height,
1414 dc_mask, xsrc, ysrc, SRCAND) )
1415 {
1416 wxLogLastError("BitBlt");
1417 }
1418
1419 // set unmasked area in dest to BLACK
1420 ::SetBkColor(GetHdc(), RGB(0, 0, 0));
1421 ::SetTextColor(GetHdc(), RGB(255, 255, 255));
1422 if ( !::BitBlt(GetHdc(), xdest, ydest, (int)width, (int)height,
1423 dc_mask, xsrc, ysrc, SRCAND) )
1424 {
1425 wxLogLastError("BitBlt");
1426 }
1427 ::SetBkColor(GetHdc(), prevBkCol); // restore colours to original values
1428 ::SetTextColor(GetHdc(), prevCol);
1429
1430 // OR buffer to dest
1431 success = ::BitBlt(GetHdc(), xdest, ydest,
1432 (int)width, (int)height,
1433 dc_buffer, 0, 0, SRCPAINT) != 0;
1434 if ( !success )
1435 {
1436 wxLogLastError("BitBlt");
1437 }
1438
1439 // tidy up temporary DCs and bitmap
1440 ::SelectObject(dc_mask, 0);
1441 ::DeleteDC(dc_mask);
1442 ::SelectObject(dc_buffer, 0);
1443 ::DeleteDC(dc_buffer);
1444 ::DeleteObject(buffer_bmap);
1445 }
1446 }
1447 else // no mask, just BitBlt() it
1448 {
1449 success = ::BitBlt(GetHdc(), xdest, ydest,
1450 (int)width, (int)height,
1451 GetHdcOf(*source), xsrc, ysrc, dwRop) != 0;
1452 if ( !success )
1453 {
1454 wxLogLastError("BitBlt");
1455 }
1456 }
1457 ::SetTextColor(GetHdc(), old_textground);
1458 ::SetBkColor(GetHdc(), old_background);
1459
1460 return success;
1461 }
1462
1463 void wxDC::DoGetSize(int *w, int *h) const
1464 {
1465 if ( w ) *w = ::GetDeviceCaps(GetHdc(), HORZRES);
1466 if ( h ) *h = ::GetDeviceCaps(GetHdc(), VERTRES);
1467 }
1468
1469 void wxDC::DoGetSizeMM(int *w, int *h) const
1470 {
1471 if ( w ) *w = ::GetDeviceCaps(GetHdc(), HORZSIZE);
1472 if ( h ) *h = ::GetDeviceCaps(GetHdc(), VERTSIZE);
1473 }
1474
1475 wxSize wxDC::GetPPI() const
1476 {
1477 int x = ::GetDeviceCaps(GetHdc(), LOGPIXELSX);
1478 int y = ::GetDeviceCaps(GetHdc(), LOGPIXELSY);
1479
1480 return wxSize(x, y);
1481 }
1482
1483 // For use by wxWindows only, unless custom units are required.
1484 void wxDC::SetLogicalScale(double x, double y)
1485 {
1486 m_logicalScaleX = x;
1487 m_logicalScaleY = y;
1488 }
1489
1490 #if WXWIN_COMPATIBILITY
1491 void wxDC::DoGetTextExtent(const wxString& string, float *x, float *y,
1492 float *descent, float *externalLeading,
1493 wxFont *theFont, bool use16bit) const
1494 {
1495 wxCoord x1, y1, descent1, externalLeading1;
1496 GetTextExtent(string, & x1, & y1, & descent1, & externalLeading1, theFont, use16bit);
1497 *x = x1; *y = y1;
1498 if (descent)
1499 *descent = descent1;
1500 if (externalLeading)
1501 *externalLeading = externalLeading1;
1502 }
1503 #endif
1504
1505 // ---------------------------------------------------------------------------
1506 // spline drawing code
1507 // ---------------------------------------------------------------------------
1508
1509 #if wxUSE_SPLINES
1510
1511 class wxSpline: public wxObject
1512 {
1513 public:
1514 int type;
1515 wxList *points;
1516
1517 wxSpline(wxList *list);
1518 void DeletePoints();
1519
1520 // Doesn't delete points
1521 ~wxSpline();
1522 };
1523
1524 void wx_draw_open_spline(wxDC *dc, wxSpline *spline);
1525
1526 void wx_quadratic_spline(double a1, double b1, double a2, double b2,
1527 double a3, double b3, double a4, double b4);
1528 void wx_clear_stack();
1529 int wx_spline_pop(double *x1, double *y1, double *x2, double *y2, double *x3,
1530 double *y3, double *x4, double *y4);
1531 void wx_spline_push(double x1, double y1, double x2, double y2, double x3, double y3,
1532 double x4, double y4);
1533 static bool wx_spline_add_point(double x, double y);
1534 static void wx_spline_draw_point_array(wxDC *dc);
1535 wxSpline *wx_make_spline(int x1, int y1, int x2, int y2, int x3, int y3);
1536
1537 void wxDC::DoDrawSpline(wxList *list)
1538 {
1539 wxSpline spline(list);
1540
1541 wx_draw_open_spline(this, &spline);
1542 }
1543
1544 wxList wx_spline_point_list;
1545
1546 void wx_draw_open_spline(wxDC *dc, wxSpline *spline)
1547 {
1548 wxPoint *p;
1549 double cx1, cy1, cx2, cy2, cx3, cy3, cx4, cy4;
1550 double x1, y1, x2, y2;
1551
1552 wxNode *node = spline->points->First();
1553 p = (wxPoint *)node->Data();
1554
1555 x1 = p->x;
1556 y1 = p->y;
1557
1558 node = node->Next();
1559 p = (wxPoint *)node->Data();
1560
1561 x2 = p->x;
1562 y2 = p->y;
1563 cx1 = (double)((x1 + x2) / 2);
1564 cy1 = (double)((y1 + y2) / 2);
1565 cx2 = (double)((cx1 + x2) / 2);
1566 cy2 = (double)((cy1 + y2) / 2);
1567
1568 wx_spline_add_point(x1, y1);
1569
1570 while ((node = node->Next()) != NULL)
1571 {
1572 p = (wxPoint *)node->Data();
1573 x1 = x2;
1574 y1 = y2;
1575 x2 = p->x;
1576 y2 = p->y;
1577 cx4 = (double)(x1 + x2) / 2;
1578 cy4 = (double)(y1 + y2) / 2;
1579 cx3 = (double)(x1 + cx4) / 2;
1580 cy3 = (double)(y1 + cy4) / 2;
1581
1582 wx_quadratic_spline(cx1, cy1, cx2, cy2, cx3, cy3, cx4, cy4);
1583
1584 cx1 = cx4;
1585 cy1 = cy4;
1586 cx2 = (double)(cx1 + x2) / 2;
1587 cy2 = (double)(cy1 + y2) / 2;
1588 }
1589
1590 wx_spline_add_point((double)wx_round(cx1), (double)wx_round(cy1));
1591 wx_spline_add_point(x2, y2);
1592
1593 wx_spline_draw_point_array(dc);
1594
1595 }
1596
1597 /********************* CURVES FOR SPLINES *****************************
1598
1599 The following spline drawing routine is from
1600
1601 "An Algorithm for High-Speed Curve Generation"
1602 by George Merrill Chaikin,
1603 Computer Graphics and Image Processing, 3, Academic Press,
1604 1974, 346-349.
1605
1606 and
1607
1608 "On Chaikin's Algorithm" by R. F. Riesenfeld,
1609 Computer Graphics and Image Processing, 4, Academic Press,
1610 1975, 304-310.
1611
1612 ***********************************************************************/
1613
1614 #define half(z1, z2) ((z1+z2)/2.0)
1615 #define THRESHOLD 5
1616
1617 /* iterative version */
1618
1619 void wx_quadratic_spline(double a1, double b1, double a2, double b2, double a3, double b3, double a4,
1620 double b4)
1621 {
1622 register double xmid, ymid;
1623 double x1, y1, x2, y2, x3, y3, x4, y4;
1624
1625 wx_clear_stack();
1626 wx_spline_push(a1, b1, a2, b2, a3, b3, a4, b4);
1627
1628 while (wx_spline_pop(&x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4)) {
1629 xmid = (double)half(x2, x3);
1630 ymid = (double)half(y2, y3);
1631 if (fabs(x1 - xmid) < THRESHOLD && fabs(y1 - ymid) < THRESHOLD &&
1632 fabs(xmid - x4) < THRESHOLD && fabs(ymid - y4) < THRESHOLD) {
1633 wx_spline_add_point((double)wx_round(x1), (double)wx_round(y1));
1634 wx_spline_add_point((double)wx_round(xmid), (double)wx_round(ymid));
1635 } else {
1636 wx_spline_push(xmid, ymid, (double)half(xmid, x3), (double)half(ymid, y3),
1637 (double)half(x3, x4), (double)half(y3, y4), x4, y4);
1638 wx_spline_push(x1, y1, (double)half(x1, x2), (double)half(y1, y2),
1639 (double)half(x2, xmid), (double)half(y2, ymid), xmid, ymid);
1640 }
1641 }
1642 }
1643
1644
1645 /* utilities used by spline drawing routines */
1646
1647
1648 typedef struct wx_spline_stack_struct {
1649 double x1, y1, x2, y2, x3, y3, x4, y4;
1650 }
1651 Stack;
1652
1653 #define SPLINE_STACK_DEPTH 20
1654 static Stack wx_spline_stack[SPLINE_STACK_DEPTH];
1655 static Stack *wx_stack_top;
1656 static int wx_stack_count;
1657
1658 void wx_clear_stack()
1659 {
1660 wx_stack_top = wx_spline_stack;
1661 wx_stack_count = 0;
1662 }
1663
1664 void wx_spline_push(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4)
1665 {
1666 wx_stack_top->x1 = x1;
1667 wx_stack_top->y1 = y1;
1668 wx_stack_top->x2 = x2;
1669 wx_stack_top->y2 = y2;
1670 wx_stack_top->x3 = x3;
1671 wx_stack_top->y3 = y3;
1672 wx_stack_top->x4 = x4;
1673 wx_stack_top->y4 = y4;
1674 wx_stack_top++;
1675 wx_stack_count++;
1676 }
1677
1678 int wx_spline_pop(double *x1, double *y1, double *x2, double *y2,
1679 double *x3, double *y3, double *x4, double *y4)
1680 {
1681 if (wx_stack_count == 0)
1682 return (0);
1683 wx_stack_top--;
1684 wx_stack_count--;
1685 *x1 = wx_stack_top->x1;
1686 *y1 = wx_stack_top->y1;
1687 *x2 = wx_stack_top->x2;
1688 *y2 = wx_stack_top->y2;
1689 *x3 = wx_stack_top->x3;
1690 *y3 = wx_stack_top->y3;
1691 *x4 = wx_stack_top->x4;
1692 *y4 = wx_stack_top->y4;
1693 return (1);
1694 }
1695
1696 static bool wx_spline_add_point(double x, double y)
1697 {
1698 wxPoint *point = new wxPoint;
1699 point->x = (int) x;
1700 point->y = (int) y;
1701 wx_spline_point_list.Append((wxObject*)point);
1702 return TRUE;
1703 }
1704
1705 static void wx_spline_draw_point_array(wxDC *dc)
1706 {
1707 dc->DrawLines(&wx_spline_point_list, 0, 0);
1708 wxNode *node = wx_spline_point_list.First();
1709 while (node)
1710 {
1711 wxPoint *point = (wxPoint *)node->Data();
1712 delete point;
1713 delete node;
1714 node = wx_spline_point_list.First();
1715 }
1716 }
1717
1718 wxSpline::wxSpline(wxList *list)
1719 {
1720 points = list;
1721 }
1722
1723 wxSpline::~wxSpline()
1724 {
1725 }
1726
1727 void wxSpline::DeletePoints()
1728 {
1729 for(wxNode *node = points->First(); node; node = points->First())
1730 {
1731 wxPoint *point = (wxPoint *)node->Data();
1732 delete point;
1733 delete node;
1734 }
1735 delete points;
1736 }
1737
1738
1739 #endif // wxUSE_SPLINES
1740