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