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