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