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