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