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