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