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