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