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