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