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