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