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