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