]> git.saurik.com Git - wxWidgets.git/blob - src/msw/dc.cpp
CW Win32 support
[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 ::DrawIcon((HDC) m_hDC, XLOG2DEV(x), YLOG2DEV(y), (HICON) icon.GetHICON());
607 CalcBoundingBox(x, y);
608 CalcBoundingBox(x+icon.GetWidth(), y+icon.GetHeight());
609 }
610
611 void wxDC::DrawBitmap( const wxBitmap &bmp, long x, long y, bool useMask )
612 {
613 if (!bmp.Ok())
614 return;
615
616 // If we're not drawing transparently, and not drawing to a printer,
617 // optimize this function to use Windows functions.
618 if (!useMask && !IsKindOf(CLASSINFO(wxPrinterDC)))
619 {
620 HDC cdc = (HDC)m_hDC;
621 HDC memdc = ::CreateCompatibleDC( cdc );
622 HBITMAP hbitmap = (HBITMAP) bmp.GetHBITMAP( );
623 ::SelectObject( memdc, hbitmap );
624 ::BitBlt( cdc, x, y, bmp.GetWidth(), bmp.GetHeight(), memdc, 0, 0, SRCCOPY);
625 ::SelectObject( memdc, 0 );
626 ::DeleteDC( memdc );
627 }
628 else
629 {
630 // Rather than reproduce wxDC::Blit, let's do it at the wxWin API level
631 wxMemoryDC memDC;
632 memDC.SelectObject(bmp);
633
634 /* Not sure if we need this. The mask should leave the
635 * masked areas as per the original background of this DC.
636 */
637 /*
638 // There might be transparent areas, so make these
639 // the same colour as this DC
640 memDC.SetBackground(* GetBackground());
641 memDC.Clear();
642 */
643
644 Blit(x, y, bmp.GetWidth(), bmp.GetHeight(), & memDC, 0, 0, wxCOPY, useMask);
645
646 memDC.SelectObject(wxNullBitmap);
647 }
648 }
649
650 void wxDC::SetFont(const wxFont& the_font)
651 {
652 // Set the old object temporarily, in case the assignment deletes an object
653 // that's not yet selected out.
654 if (m_oldFont)
655 {
656 ::SelectObject((HDC) m_hDC, (HFONT) m_oldFont);
657 m_oldFont = 0;
658 }
659
660 m_font = the_font;
661
662 if (!the_font.Ok())
663 {
664 if (m_oldFont)
665 ::SelectObject((HDC) m_hDC, (HFONT) m_oldFont);
666 m_oldFont = 0 ;
667 }
668
669 if (m_font.Ok() && m_font.GetResourceHandle())
670 {
671 HFONT f = (HFONT) ::SelectObject((HDC) m_hDC, (HFONT) m_font.GetResourceHandle());
672 if (f == NULL)
673 {
674 wxDebugMsg("::SelectObject failed in wxDC::SetFont.");
675 }
676 if (!m_oldFont)
677 m_oldFont = (WXHFONT) f;
678 }
679 }
680
681 void wxDC::SetPen(const wxPen& pen)
682 {
683 // Set the old object temporarily, in case the assignment deletes an object
684 // that's not yet selected out.
685 if (m_oldPen)
686 {
687 ::SelectObject((HDC) m_hDC, (HPEN) m_oldPen);
688 m_oldPen = 0;
689 }
690
691 m_pen = pen;
692
693 if (!m_pen.Ok())
694 {
695 if (m_oldPen)
696 ::SelectObject((HDC) m_hDC, (HPEN) m_oldPen);
697 m_oldPen = 0 ;
698 }
699
700 if (m_pen.Ok())
701 {
702 if (m_pen.GetResourceHandle())
703 {
704 HPEN p = (HPEN) ::SelectObject((HDC) m_hDC, (HPEN)m_pen.GetResourceHandle()) ;
705 if (!m_oldPen)
706 m_oldPen = (WXHPEN) p;
707 }
708 }
709 }
710
711 void wxDC::SetBrush(const wxBrush& brush)
712 {
713 // Set the old object temporarily, in case the assignment deletes an object
714 // that's not yet selected out.
715 if (m_oldBrush)
716 {
717 ::SelectObject((HDC) m_hDC, (HBRUSH) m_oldBrush);
718 m_oldBrush = 0;
719 }
720
721 m_brush = brush;
722
723 if (!m_brush.Ok())
724 {
725 if (m_oldBrush)
726 ::SelectObject((HDC) m_hDC, (HBRUSH) m_oldBrush);
727 m_oldBrush = 0 ;
728 }
729
730 if (m_brush.Ok())
731 {
732 if (m_brush.GetResourceHandle())
733 {
734 HBRUSH b = 0;
735 b = (HBRUSH) ::SelectObject((HDC) m_hDC, (HBRUSH)m_brush.GetResourceHandle()) ;
736 if (!m_oldBrush)
737 m_oldBrush = (WXHBRUSH) b;
738 }
739 }
740 }
741
742 void wxDC::DrawText(const wxString& text, long x, long y, bool use16bit)
743 {
744 // Should be unnecessary: SetFont should have done this already.
745 #if 0
746 if (m_font.Ok() && m_font.GetResourceHandle())
747 {
748 HFONT f = (HFONT) ::SelectObject((HDC) m_hDC, (HFONT) m_font.GetResourceHandle());
749 if (!m_oldFont)
750 m_oldFont = (WXHFONT) f;
751 }
752 #endif
753
754 if (m_textForegroundColour.Ok())
755 SetTextColor((HDC) m_hDC, m_textForegroundColour.GetPixel() ) ;
756
757 DWORD old_background = 0;
758 if (m_textBackgroundColour.Ok())
759 {
760 old_background = SetBkColor((HDC) m_hDC, m_textBackgroundColour.GetPixel() ) ;
761 }
762
763 if (m_backgroundMode == wxTRANSPARENT)
764 SetBkMode((HDC) m_hDC, TRANSPARENT);
765 else
766 SetBkMode((HDC) m_hDC, OPAQUE);
767
768 (void)TextOut((HDC) m_hDC, XLOG2DEV(x), YLOG2DEV(y), (char *) (const char *)text, strlen((const char *)text));
769
770 if (m_textBackgroundColour.Ok())
771 (void)SetBkColor((HDC) m_hDC, old_background);
772
773 CalcBoundingBox(x, y);
774
775 long w, h;
776 GetTextExtent(text, &w, &h);
777 CalcBoundingBox((x + w), (y + h));
778 }
779
780 void wxDC::SetBackground(const wxBrush& brush)
781 {
782 m_backgroundBrush = brush;
783
784 if (!m_backgroundBrush.Ok())
785 return;
786
787 if (m_canvas)
788 {
789 bool customColours = TRUE;
790 // If we haven't specified wxUSER_COLOURS, don't allow the panel/dialog box to
791 // change background colours from the control-panel specified colours.
792 if (m_canvas->IsKindOf(CLASSINFO(wxWindow)) && ((m_canvas->GetWindowStyleFlag() & wxUSER_COLOURS) != wxUSER_COLOURS))
793 customColours = FALSE;
794
795 if (customColours)
796 {
797 if (m_backgroundBrush.GetStyle()==wxTRANSPARENT)
798 {
799 m_canvas->m_backgroundTransparent = TRUE;
800 }
801 else
802 {
803 m_canvas->SetBackgroundColour(m_backgroundBrush.GetColour());
804 m_canvas->m_backgroundTransparent = FALSE;
805 }
806 }
807 }
808 COLORREF new_color = m_backgroundBrush.GetColour().GetPixel() ;
809 {
810 (void)SetBkColor((HDC) m_hDC, new_color);
811 }
812 }
813
814 void wxDC::SetBackgroundMode(int mode)
815 {
816 m_backgroundMode = mode;
817
818 if (m_backgroundMode == wxTRANSPARENT)
819 ::SetBkMode((HDC) m_hDC, TRANSPARENT);
820 else
821 ::SetBkMode((HDC) m_hDC, OPAQUE);
822 }
823
824 void wxDC::SetLogicalFunction(int function)
825 {
826 m_logicalFunction = function;
827
828 SetRop((WXHDC) m_hDC);
829 }
830
831 void wxDC::SetRop(WXHDC dc)
832 {
833 if (!dc || m_logicalFunction < 0)
834 return;
835
836 int c_rop;
837 // These may be wrong
838 switch (m_logicalFunction)
839 {
840 // case wxXOR: c_rop = R2_XORPEN; break;
841 case wxXOR: c_rop = R2_NOTXORPEN; break;
842 case wxINVERT: c_rop = R2_NOT; break;
843 case wxOR_REVERSE: c_rop = R2_MERGEPENNOT; break;
844 case wxAND_REVERSE: c_rop = R2_MASKPENNOT; break;
845 case wxCLEAR: c_rop = R2_WHITE; break;
846 case wxSET: c_rop = R2_BLACK; break;
847 case wxSRC_INVERT: c_rop = R2_NOTCOPYPEN; break;
848 case wxOR_INVERT: c_rop = R2_MERGENOTPEN; break;
849 case wxAND: c_rop = R2_MASKPEN; break;
850 case wxOR: c_rop = R2_MERGEPEN; break;
851 case wxAND_INVERT: c_rop = R2_MASKNOTPEN; break;
852 case wxEQUIV:
853 case wxNAND:
854 case wxCOPY:
855 default:
856 c_rop = R2_COPYPEN; break;
857 }
858 SetROP2((HDC) dc, c_rop);
859 }
860
861 bool wxDC::StartDoc(const wxString& message)
862 {
863 if (!this->IsKindOf(CLASSINFO(wxPrinterDC)))
864 return TRUE;
865
866 DOCINFO docinfo;
867 docinfo.cbSize = sizeof(DOCINFO);
868 docinfo.lpszDocName = (const char *)message;
869
870 if (m_filename.IsEmpty())
871 docinfo.lpszOutput = NULL;
872 else
873 docinfo.lpszOutput = (const char *)m_filename;
874
875 #if defined(__WIN95__)
876 docinfo.lpszDatatype = NULL;
877 docinfo.fwType = 0;
878 #endif
879
880 if (!m_hDC)
881 return FALSE;
882
883 int ret =
884 #ifndef __WIN32__
885 ::StartDoc((HDC) m_hDC, &docinfo);
886 #else
887 #ifdef UNICODE
888 ::StartDocW((HDC) m_hDC, &docinfo);
889 #else
890 ::StartDocA((HDC) m_hDC, &docinfo);
891 #endif
892 #endif
893
894 #ifndef __WIN16__
895 if (ret <= 0)
896 {
897 DWORD lastError = GetLastError();
898 wxDebugMsg("wxDC::StartDoc failed with error: %d\n", lastError);
899 }
900 #endif
901
902 return (ret > 0);
903 }
904
905 void wxDC::EndDoc(void)
906 {
907 if (!this->IsKindOf(CLASSINFO(wxPrinterDC)))
908 return;
909 if (m_hDC) ::EndDoc((HDC) m_hDC);
910 }
911
912 void wxDC::StartPage(void)
913 {
914 if (!this->IsKindOf(CLASSINFO(wxPrinterDC)))
915 return;
916 if (m_hDC)
917 ::StartPage((HDC) m_hDC);
918 }
919
920 void wxDC::EndPage(void)
921 {
922 if (!this->IsKindOf(CLASSINFO(wxPrinterDC)))
923 return;
924 if (m_hDC)
925 ::EndPage((HDC) m_hDC);
926 }
927
928 long wxDC::GetCharHeight(void) const
929 {
930 TEXTMETRIC lpTextMetric;
931
932 GetTextMetrics((HDC) m_hDC, &lpTextMetric);
933
934 return YDEV2LOGREL(lpTextMetric.tmHeight);
935 }
936
937 long wxDC::GetCharWidth(void) const
938 {
939 TEXTMETRIC lpTextMetric;
940
941 GetTextMetrics((HDC) m_hDC, &lpTextMetric);
942
943 return XDEV2LOGREL(lpTextMetric.tmAveCharWidth);
944 }
945
946 void wxDC::GetTextExtent(const wxString& string, long *x, long *y,
947 long *descent, long *externalLeading, wxFont *theFont, bool use16bit) const
948 {
949 wxFont *fontToUse = (wxFont*) theFont;
950 if (!fontToUse)
951 fontToUse = (wxFont*) &m_font;
952
953 SIZE sizeRect;
954 TEXTMETRIC tm;
955
956 GetTextExtentPoint((HDC) m_hDC, (char *)(const char *) string, strlen((char *)(const char *) string), &sizeRect);
957 GetTextMetrics((HDC) m_hDC, &tm);
958
959 if (x) *x = XDEV2LOGREL(sizeRect.cx);
960 if (y) *y = YDEV2LOGREL(sizeRect.cy);
961 if (descent) *descent = tm.tmDescent;
962 if (externalLeading) *externalLeading = tm.tmExternalLeading;
963 }
964
965 void wxDC::SetMapMode(int mode)
966 {
967 m_mappingMode = mode;
968
969 int pixel_width = 0;
970 int pixel_height = 0;
971 int mm_width = 0;
972 int mm_height = 0;
973
974 pixel_width = GetDeviceCaps((HDC) m_hDC, HORZRES);
975 pixel_height = GetDeviceCaps((HDC) m_hDC, VERTRES);
976 mm_width = GetDeviceCaps((HDC) m_hDC, HORZSIZE);
977 mm_height = GetDeviceCaps((HDC) m_hDC, VERTSIZE);
978
979 if ((pixel_width == 0) || (pixel_height == 0) || (mm_width == 0) || (mm_height == 0))
980 {
981 return;
982 }
983
984 double mm2pixelsX = pixel_width/mm_width;
985 double mm2pixelsY = pixel_height/mm_height;
986
987 switch (mode)
988 {
989 case MM_TWIPS:
990 {
991 m_logicalScaleX = (twips2mm * mm2pixelsX);
992 m_logicalScaleY = (twips2mm * mm2pixelsY);
993 break;
994 }
995 case MM_POINTS:
996 {
997 m_logicalScaleX = (pt2mm * mm2pixelsX);
998 m_logicalScaleY = (pt2mm * mm2pixelsY);
999 break;
1000 }
1001 case MM_METRIC:
1002 {
1003 m_logicalScaleX = mm2pixelsX;
1004 m_logicalScaleY = mm2pixelsY;
1005 break;
1006 }
1007 case MM_LOMETRIC:
1008 {
1009 m_logicalScaleX = (mm2pixelsX/10.0);
1010 m_logicalScaleY = (mm2pixelsY/10.0);
1011 break;
1012 }
1013 default:
1014 case MM_TEXT:
1015 {
1016 m_logicalScaleX = 1.0;
1017 m_logicalScaleY = 1.0;
1018 break;
1019 }
1020 }
1021
1022 if (::GetMapMode((HDC) m_hDC) != MM_ANISOTROPIC)
1023 ::SetMapMode((HDC) m_hDC, MM_ANISOTROPIC);
1024
1025 SetViewportExtEx((HDC) m_hDC, VIEWPORT_EXTENT, VIEWPORT_EXTENT, NULL);
1026 m_windowExtX = (int)MS_XDEV2LOGREL(VIEWPORT_EXTENT);
1027 m_windowExtY = (int)MS_YDEV2LOGREL(VIEWPORT_EXTENT);
1028 ::SetWindowExtEx((HDC) m_hDC, m_windowExtX, m_windowExtY, NULL);
1029 ::SetViewportOrgEx((HDC) m_hDC, (int)m_deviceOriginX, (int)m_deviceOriginY, NULL);
1030 ::SetWindowOrgEx((HDC) m_hDC, (int)m_logicalOriginX, (int)m_logicalOriginY, NULL);
1031 }
1032
1033 void wxDC::SetUserScale(double x, double y)
1034 {
1035 m_userScaleX = x;
1036 m_userScaleY = y;
1037
1038 SetMapMode(m_mappingMode);
1039 }
1040
1041 void wxDC::SetAxisOrientation(bool xLeftRight, bool yBottomUp)
1042 {
1043 m_signX = xLeftRight ? 1 : -1;
1044 m_signY = yBottomUp ? -1 : 1;
1045
1046 SetMapMode(m_mappingMode);
1047 }
1048
1049 void wxDC::SetSystemScale(double x, double y)
1050 {
1051 m_systemScaleX = x;
1052 m_systemScaleY = y;
1053
1054 SetMapMode(m_mappingMode);
1055 }
1056
1057 void wxDC::SetLogicalOrigin(long x, long y)
1058 {
1059 m_logicalOriginX = x;
1060 m_logicalOriginY = y;
1061
1062 ::SetWindowOrgEx((HDC) m_hDC, (int)m_logicalOriginX, (int)m_logicalOriginY, NULL);
1063 }
1064
1065 void wxDC::SetDeviceOrigin(long x, long y)
1066 {
1067 m_deviceOriginX = x;
1068 m_deviceOriginY = y;
1069
1070 ::SetViewportOrgEx((HDC) m_hDC, (int)m_deviceOriginX, (int)m_deviceOriginY, NULL);
1071 }
1072
1073 long wxDC::DeviceToLogicalX(long x) const
1074 {
1075 return (long) (((x) - m_deviceOriginX)/(m_logicalScaleX*m_userScaleX*m_signX*m_systemScaleX) - m_logicalOriginX) ;
1076 }
1077
1078 long wxDC::DeviceToLogicalXRel(long x) const
1079 {
1080 return (long) ((x)/(m_logicalScaleX*m_userScaleX*m_signX*m_systemScaleX)) ;
1081 }
1082
1083 long wxDC::DeviceToLogicalY(long y) const
1084 {
1085 return (long) (((y) - m_deviceOriginY)/(m_logicalScaleY*m_userScaleY*m_signY*m_systemScaleY) - m_logicalOriginY) ;
1086 }
1087
1088 long wxDC::DeviceToLogicalYRel(long y) const
1089 {
1090 return (long) ((y)/(m_logicalScaleY*m_userScaleY*m_signY*m_systemScaleY)) ;
1091 }
1092
1093 long wxDC::LogicalToDeviceX(long x) const
1094 {
1095 return (long) (floor((x) - m_logicalOriginX)*m_logicalScaleX*m_userScaleX*m_signX*m_systemScaleX + m_deviceOriginX) ;
1096 }
1097
1098 long wxDC::LogicalToDeviceXRel(long x) const
1099 {
1100 return (long) (floor(x)*m_logicalScaleX*m_userScaleX*m_signX*m_systemScaleX) ;
1101 }
1102
1103 long wxDC::LogicalToDeviceY(long y) const
1104 {
1105 return (long) (floor((y) - m_logicalOriginY)*m_logicalScaleY*m_userScaleY*m_signY*m_systemScaleY + m_deviceOriginY);
1106 }
1107
1108 long wxDC::LogicalToDeviceYRel(long y) const
1109 {
1110 return (long) (floor(y)*m_logicalScaleY*m_userScaleY*m_signY*m_systemScaleY) ;
1111 }
1112
1113 // This group of functions may not do any conversion
1114 // if m_scaleGDI is TRUE, since the HDC does the
1115 // conversion automatically.
1116
1117 long wxDC::ImplDeviceToLogicalX(long x) const
1118 {
1119 // return (m_scaleGDI ? x : DeviceToLogicalX(x));
1120 return x;
1121 }
1122
1123 long wxDC::ImplDeviceToLogicalY(long y) const
1124 {
1125 // return (m_scaleGDI ? y : DeviceToLogicalY(y));
1126 return y;
1127 }
1128
1129 long wxDC::ImplDeviceToLogicalXRel(long x) const
1130 {
1131 // return (m_scaleGDI ? x : DeviceToLogicalXRel(x));
1132 return x;
1133 }
1134
1135 long wxDC::ImplDeviceToLogicalYRel(long y) const
1136 {
1137 // return (m_scaleGDI ? y : DeviceToLogicalYRel(y));
1138 return y;
1139 }
1140
1141 long wxDC::ImplLogicalToDeviceX(long x) const
1142 {
1143 // return (m_scaleGDI ? (floor(double(x))) : LogicalToDeviceX(x));
1144 return x;
1145 }
1146
1147 long wxDC::ImplLogicalToDeviceY(long y) const
1148 {
1149 // return (m_scaleGDI ? (floor(double(y))) : LogicalToDeviceY(y));
1150 return y;
1151 }
1152
1153 long wxDC::ImplLogicalToDeviceXRel(long x) const
1154 {
1155 // return (m_scaleGDI ? (floor(double(x))) : LogicalToDeviceXRel(x));
1156 return x;
1157 }
1158
1159 long wxDC::ImplLogicalToDeviceYRel(long y) const
1160 {
1161 // return (m_scaleGDI ? (floor(double(y))) : LogicalToDeviceYRel(y));
1162 return y;
1163 }
1164
1165 bool wxDC::Blit(long xdest, long ydest, long width, long height,
1166 wxDC *source, long xsrc, long ysrc, int rop, bool useMask)
1167 {
1168 long xdest1 = xdest;
1169 long ydest1 = ydest;
1170 long xsrc1 = xsrc;
1171 long ysrc1 = ysrc;
1172
1173 // Chris Breeze 18/5/98: use text foreground/background colours
1174 // when blitting from 1-bit bitmaps
1175 COLORREF old_textground = ::GetTextColor((HDC)m_hDC);
1176 COLORREF old_background = ::GetBkColor((HDC)m_hDC);
1177 if (m_textForegroundColour.Ok())
1178 {
1179 ::SetTextColor((HDC) m_hDC, m_textForegroundColour.GetPixel() ) ;
1180 }
1181 if (m_textBackgroundColour.Ok())
1182 {
1183 ::SetBkColor((HDC) m_hDC, m_textBackgroundColour.GetPixel() ) ;
1184 }
1185
1186 DWORD dwRop = rop == wxCOPY ? SRCCOPY :
1187 rop == wxCLEAR ? WHITENESS :
1188 rop == wxSET ? BLACKNESS :
1189 rop == wxINVERT ? DSTINVERT :
1190 rop == wxAND ? MERGECOPY :
1191 rop == wxOR ? MERGEPAINT :
1192 rop == wxSRC_INVERT ? NOTSRCCOPY :
1193 rop == wxXOR ? SRCINVERT :
1194 rop == wxOR_REVERSE ? MERGEPAINT :
1195 rop == wxAND_REVERSE ? SRCERASE :
1196 rop == wxSRC_OR ? SRCPAINT :
1197 rop == wxSRC_AND ? SRCAND :
1198 SRCCOPY;
1199
1200 bool success = TRUE;
1201 if (useMask && source->m_selectedBitmap.Ok() && source->m_selectedBitmap.GetMask())
1202 {
1203
1204 #if 0 // __WIN32__
1205 // Not implemented under Win95 (or maybe a specific device?)
1206 if (MaskBlt((HDC) m_hDC, xdest1, ydest1, (int)width, (int)height,
1207 (HDC) source->m_hDC, xsrc1, ysrc1, (HBITMAP) source->m_selectedBitmap.GetMask()->GetMaskBitmap(),
1208 0, 0, 0xAACC0020))
1209 {
1210 // Success
1211 }
1212 else
1213 #endif
1214 {
1215 // Old code
1216 #if 0
1217 HDC dc_mask = CreateCompatibleDC((HDC) source->m_hDC);
1218 ::SelectObject(dc_mask, (HBITMAP) source->m_selectedBitmap.GetMask()->GetMaskBitmap());
1219 success = (BitBlt((HDC) m_hDC, xdest1, ydest1, (int)width, (int)height,
1220 dc_mask, xsrc1, ysrc1, 0x00220326 /* NOTSRCAND */) != 0);
1221 success = (BitBlt((HDC) m_hDC, xdest1, ydest1, (int)width, (int)height,
1222 (HDC) source->m_hDC, xsrc1, ysrc1, SRCPAINT) != 0);
1223 ::SelectObject(dc_mask, 0);
1224 ::DeleteDC(dc_mask);
1225 #endif
1226 // New code from Chris Breeze, 15/7/98
1227 // Blit bitmap with mask
1228
1229 if (IsKindOf(CLASSINFO(wxPrinterDC)))
1230 {
1231 // If we are printing source colours are screen colours
1232 // not printer colours and so we need copy the bitmap
1233 // pixel by pixel.
1234 RECT rect;
1235 HDC dc_mask = ::CreateCompatibleDC((HDC) source->m_hDC);
1236 HDC dc_src = (HDC) source->m_hDC;
1237
1238 ::SelectObject(dc_mask, (HBITMAP) source->m_selectedBitmap.GetMask()->GetMaskBitmap());
1239 for (int x = 0; x < width; x++)
1240 {
1241 for (int y = 0; y < height; y++)
1242 {
1243 COLORREF cref = ::GetPixel(dc_mask, x, y);
1244 if (cref)
1245 {
1246 HBRUSH brush = ::CreateSolidBrush(::GetPixel(dc_src, x, y));
1247 rect.left = xdest1 + x; rect.right = rect.left + 1;
1248 rect.top = ydest1 + y; rect.bottom = rect.top + 1;
1249 ::FillRect((HDC) m_hDC, &rect, brush);
1250 ::DeleteObject(brush);
1251 }
1252 }
1253 }
1254 ::SelectObject(dc_mask, 0);
1255 ::DeleteDC(dc_mask);
1256 }
1257 else
1258 {
1259 // create a temp buffer bitmap and DCs to access it and the mask
1260 HDC dc_mask = ::CreateCompatibleDC((HDC) source->m_hDC);
1261 HDC dc_buffer = ::CreateCompatibleDC((HDC) m_hDC);
1262 HBITMAP buffer_bmap = ::CreateCompatibleBitmap((HDC) m_hDC, width, height);
1263 ::SelectObject(dc_mask, (HBITMAP) source->m_selectedBitmap.GetMask()->GetMaskBitmap());
1264 ::SelectObject(dc_buffer, buffer_bmap);
1265
1266 // copy dest to buffer
1267 ::BitBlt(dc_buffer, 0, 0, (int)width, (int)height,
1268 (HDC) m_hDC, xdest1, ydest1, SRCCOPY);
1269
1270 // copy src to buffer using selected raster op
1271 ::BitBlt(dc_buffer, 0, 0, (int)width, (int)height,
1272 (HDC) source->m_hDC, xsrc1, ysrc1, dwRop);
1273
1274 // set masked area in buffer to BLACK (pixel value 0)
1275 COLORREF prevBkCol = ::SetBkColor((HDC) m_hDC, RGB(255, 255, 255));
1276 COLORREF prevCol = ::SetTextColor((HDC) m_hDC, RGB(0, 0, 0));
1277 ::BitBlt(dc_buffer, 0, 0, (int)width, (int)height,
1278 dc_mask, xsrc1, ysrc1, SRCAND);
1279
1280 // set unmasked area in dest to BLACK
1281 ::SetBkColor((HDC) m_hDC, RGB(0, 0, 0));
1282 ::SetTextColor((HDC) m_hDC, RGB(255, 255, 255));
1283 ::BitBlt((HDC) m_hDC, xdest1, ydest1, (int)width, (int)height,
1284 dc_mask, xsrc1, ysrc1, SRCAND);
1285 ::SetBkColor((HDC) m_hDC, prevBkCol); // restore colours to original values
1286 ::SetTextColor((HDC) m_hDC, prevCol);
1287
1288 // OR buffer to dest
1289 success = (::BitBlt((HDC) m_hDC, xdest1, ydest1, (int)width, (int)height,
1290 dc_buffer, 0, 0, SRCPAINT) != 0);
1291
1292 // tidy up temporary DCs and bitmap
1293 ::SelectObject(dc_mask, 0);
1294 ::DeleteDC(dc_mask);
1295 ::SelectObject(dc_buffer, 0);
1296 ::DeleteDC(dc_buffer);
1297 ::DeleteObject(buffer_bmap);
1298 }
1299 }
1300 }
1301 else
1302 {
1303 if (IsKindOf(CLASSINFO(wxPrinterDC)))
1304 {
1305 // If we are printing, source colours are screen colours
1306 // not printer colours and so we need copy the bitmap
1307 // pixel by pixel.
1308 HDC dc_src = (HDC) source->m_hDC;
1309 RECT rect;
1310 for (int x = 0; x < width; x++)
1311 {
1312 for (int y = 0; y < height; y++)
1313 {
1314 HBRUSH brush = ::CreateSolidBrush(::GetPixel(dc_src, x, y));
1315 rect.left = xdest1 + x; rect.right = rect.left + 1;
1316 rect.top = ydest1 + y; rect.bottom = rect.top + 1;
1317 ::FillRect((HDC) m_hDC, &rect, brush);
1318 ::DeleteObject(brush);
1319 }
1320 }
1321 }
1322 else
1323 {
1324 success = (BitBlt((HDC) m_hDC, xdest1, ydest1, (int)width, (int)height, (HDC) source->m_hDC,
1325 xsrc1, ysrc1, dwRop) != 0);
1326 }
1327 }
1328 ::SetTextColor((HDC)m_hDC, old_textground);
1329 ::SetBkColor((HDC)m_hDC, old_background);
1330
1331 return success;
1332 }
1333
1334 void wxDC::GetSize(int* width, int* height) const
1335 {
1336 long w=::GetDeviceCaps((HDC) m_hDC,HORZRES);
1337 long h=::GetDeviceCaps((HDC) m_hDC,VERTRES);
1338 *width = w;
1339 *height = h;
1340 }
1341
1342 void wxDC::GetSizeMM(long *width, long *height) const
1343 {
1344 long w=::GetDeviceCaps((HDC) m_hDC,HORZSIZE);
1345 long h=::GetDeviceCaps((HDC) m_hDC,VERTSIZE);
1346 *width = w;
1347 *height = h;
1348 }
1349
1350 void wxDC::DrawPolygon(wxList *list, long xoffset, long yoffset,int fillStyle)
1351 {
1352 int n = list->Number();
1353 wxPoint *points = new wxPoint[n];
1354
1355 int i = 0;
1356 for(wxNode *node = list->First(); node; node = node->Next()) {
1357 wxPoint *point = (wxPoint *)node->Data();
1358 points[i].x = point->x;
1359 points[i++].y = point->y;
1360 }
1361 DrawPolygon(n, points, xoffset, yoffset,fillStyle);
1362 delete[] points;
1363 }
1364
1365 void wxDC::DrawLines(wxList *list, long xoffset, long yoffset)
1366 {
1367 int n = list->Number();
1368 wxPoint *points = new wxPoint[n];
1369
1370 int i = 0;
1371 for(wxNode *node = list->First(); node; node = node->Next()) {
1372 wxPoint *point = (wxPoint *)node->Data();
1373 points[i].x = point->x;
1374 points[i++].y = point->y;
1375 }
1376 DrawLines(n, points, xoffset, yoffset);
1377 delete []points;
1378 }
1379
1380 void wxDC::SetTextForeground(const wxColour& colour)
1381 {
1382 m_textForegroundColour = colour;
1383 }
1384
1385 void wxDC::SetTextBackground(const wxColour& colour)
1386 {
1387 m_textBackgroundColour = colour;
1388 }
1389
1390 // For use by wxWindows only, unless custom units are required.
1391 void wxDC::SetLogicalScale(double x, double y)
1392 {
1393 m_logicalScaleX = x;
1394 m_logicalScaleY = y;
1395 }
1396
1397 void wxDC::CalcBoundingBox(long x, long y)
1398 {
1399 if (x < m_minX) m_minX = x;
1400 if (y < m_minY) m_minY = y;
1401 if (x > m_maxX) m_maxX = x;
1402 if (y > m_maxY) m_maxY = y;
1403 }
1404
1405 void wxDC::GetClippingBox(long *x,long *y,long *w,long *h) const
1406 {
1407 if (m_clipping)
1408 {
1409 *x = m_clipX1 ;
1410 *y = m_clipY1 ;
1411 *w = (m_clipX2 - m_clipX1) ;
1412 *h = (m_clipY2 - m_clipY1) ;
1413 }
1414 else
1415 *x = *y = *w = *h = 0 ;
1416 }
1417
1418 #if WXWIN_COMPATIBILITY
1419 void wxDC::GetTextExtent(const wxString& string, float *x, float *y,
1420 float *descent, float *externalLeading,
1421 wxFont *theFont, bool use16bit) const
1422 {
1423 long x1, y1, descent1, externalLeading1;
1424 GetTextExtent(string, & x1, & y1, & descent1, & externalLeading1, theFont, use16bit);
1425 *x = x1; *y = y1;
1426 if (descent)
1427 *descent = descent1;
1428 if (externalLeading)
1429 *externalLeading = externalLeading1;
1430 }
1431 #endif
1432
1433 int wxDC::GetDepth(void) const
1434 {
1435 return (int) ::GetDeviceCaps((HDC) m_hDC,BITSPIXEL);
1436 }
1437
1438 #if wxUSE_SPLINES
1439
1440 // Make a 3-point spline
1441 void wxDC::DrawSpline(long x1, long y1, long x2, long y2, long x3, long y3)
1442 {
1443 wxList *point_list = new wxList;
1444
1445 wxPoint *point1 = new wxPoint;
1446 point1->x = x1; point1->y = y1;
1447 point_list->Append((wxObject*)point1);
1448
1449 wxPoint *point2 = new wxPoint;
1450 point2->x = x2; point2->y = y2;
1451 point_list->Append((wxObject*)point2);
1452
1453 wxPoint *point3 = new wxPoint;
1454 point3->x = x3; point3->y = y3;
1455 point_list->Append((wxObject*)point3);
1456
1457 DrawSpline(point_list);
1458
1459 for(wxNode *node = point_list->First(); node; node = node->Next()) {
1460 wxPoint *p = (wxPoint *)node->Data();
1461 delete p;
1462 }
1463 delete point_list;
1464 }
1465
1466 ////#define wx_round(a) (int)((a)+.5)
1467 //#define wx_round(a) (a)
1468
1469 class wxSpline: public wxObject
1470 {
1471 public:
1472 int type;
1473 wxList *points;
1474
1475 wxSpline(wxList *list);
1476 void DeletePoints(void);
1477
1478 // Doesn't delete points
1479 ~wxSpline(void);
1480 };
1481
1482 void wxDC::DrawSpline(int n, wxPoint points[])
1483 {
1484 wxList list;
1485 int i;
1486 for (i =0; i < n; i++)
1487 list.Append((wxObject*)&points[i]);
1488 DrawSpline((wxList *)&list);
1489 }
1490
1491 void wx_draw_open_spline(wxDC *dc, wxSpline *spline);
1492
1493 void wx_quadratic_spline(double a1, double b1, double a2, double b2,
1494 double a3, double b3, double a4, double b4);
1495 void wx_clear_stack(void);
1496 int wx_spline_pop(double *x1, double *y1, double *x2, double *y2, double *x3,
1497 double *y3, double *x4, double *y4);
1498 void wx_spline_push(double x1, double y1, double x2, double y2, double x3, double y3,
1499 double x4, double y4);
1500 static bool wx_spline_add_point(double x, double y);
1501 static void wx_spline_draw_point_array(wxDC *dc);
1502 wxSpline *wx_make_spline(int x1, int y1, int x2, int y2, int x3, int y3);
1503
1504 void wxDC::DrawSpline(wxList *list)
1505 {
1506 wxSpline spline(list);
1507
1508 wx_draw_open_spline(this, &spline);
1509 }
1510
1511
1512 wxList wx_spline_point_list;
1513
1514 void wx_draw_open_spline(wxDC *dc, wxSpline *spline)
1515 {
1516 wxPoint *p;
1517 double cx1, cy1, cx2, cy2, cx3, cy3, cx4, cy4;
1518 double x1, y1, x2, y2;
1519
1520 wxNode *node = spline->points->First();
1521 p = (wxPoint *)node->Data();
1522
1523 x1 = p->x;
1524 y1 = p->y;
1525
1526 node = node->Next();
1527 p = (wxPoint *)node->Data();
1528
1529 x2 = p->x;
1530 y2 = p->y;
1531 cx1 = (double)((x1 + x2) / 2);
1532 cy1 = (double)((y1 + y2) / 2);
1533 cx2 = (double)((cx1 + x2) / 2);
1534 cy2 = (double)((cy1 + y2) / 2);
1535
1536 wx_spline_add_point(x1, y1);
1537
1538 while ((node = node->Next()) != NULL)
1539 {
1540 p = (wxPoint *)node->Data();
1541 x1 = x2;
1542 y1 = y2;
1543 x2 = p->x;
1544 y2 = p->y;
1545 cx4 = (double)(x1 + x2) / 2;
1546 cy4 = (double)(y1 + y2) / 2;
1547 cx3 = (double)(x1 + cx4) / 2;
1548 cy3 = (double)(y1 + cy4) / 2;
1549
1550 wx_quadratic_spline(cx1, cy1, cx2, cy2, cx3, cy3, cx4, cy4);
1551
1552 cx1 = cx4;
1553 cy1 = cy4;
1554 cx2 = (double)(cx1 + x2) / 2;
1555 cy2 = (double)(cy1 + y2) / 2;
1556 }
1557
1558 wx_spline_add_point((double)wx_round(cx1), (double)wx_round(cy1));
1559 wx_spline_add_point(x2, y2);
1560
1561 wx_spline_draw_point_array(dc);
1562
1563 }
1564
1565 /********************* CURVES FOR SPLINES *****************************
1566
1567 The following spline drawing routine is from
1568
1569 "An Algorithm for High-Speed Curve Generation"
1570 by George Merrill Chaikin,
1571 Computer Graphics and Image Processing, 3, Academic Press,
1572 1974, 346-349.
1573
1574 and
1575
1576 "On Chaikin's Algorithm" by R. F. Riesenfeld,
1577 Computer Graphics and Image Processing, 4, Academic Press,
1578 1975, 304-310.
1579
1580 ***********************************************************************/
1581
1582 #define half(z1, z2) ((z1+z2)/2.0)
1583 #define THRESHOLD 5
1584
1585 /* iterative version */
1586
1587 void wx_quadratic_spline(double a1, double b1, double a2, double b2, double a3, double b3, double a4,
1588 double b4)
1589 {
1590 register double xmid, ymid;
1591 double x1, y1, x2, y2, x3, y3, x4, y4;
1592
1593 wx_clear_stack();
1594 wx_spline_push(a1, b1, a2, b2, a3, b3, a4, b4);
1595
1596 while (wx_spline_pop(&x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4)) {
1597 xmid = (double)half(x2, x3);
1598 ymid = (double)half(y2, y3);
1599 if (fabs(x1 - xmid) < THRESHOLD && fabs(y1 - ymid) < THRESHOLD &&
1600 fabs(xmid - x4) < THRESHOLD && fabs(ymid - y4) < THRESHOLD) {
1601 wx_spline_add_point((double)wx_round(x1), (double)wx_round(y1));
1602 wx_spline_add_point((double)wx_round(xmid), (double)wx_round(ymid));
1603 } else {
1604 wx_spline_push(xmid, ymid, (double)half(xmid, x3), (double)half(ymid, y3),
1605 (double)half(x3, x4), (double)half(y3, y4), x4, y4);
1606 wx_spline_push(x1, y1, (double)half(x1, x2), (double)half(y1, y2),
1607 (double)half(x2, xmid), (double)half(y2, ymid), xmid, ymid);
1608 }
1609 }
1610 }
1611
1612
1613 /* utilities used by spline drawing routines */
1614
1615
1616 typedef struct wx_spline_stack_struct {
1617 double x1, y1, x2, y2, x3, y3, x4, y4;
1618 }
1619 Stack;
1620
1621 #define SPLINE_STACK_DEPTH 20
1622 static Stack wx_spline_stack[SPLINE_STACK_DEPTH];
1623 static Stack *wx_stack_top;
1624 static int wx_stack_count;
1625
1626 void wx_clear_stack(void)
1627 {
1628 wx_stack_top = wx_spline_stack;
1629 wx_stack_count = 0;
1630 }
1631
1632 void wx_spline_push(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4)
1633 {
1634 wx_stack_top->x1 = x1;
1635 wx_stack_top->y1 = y1;
1636 wx_stack_top->x2 = x2;
1637 wx_stack_top->y2 = y2;
1638 wx_stack_top->x3 = x3;
1639 wx_stack_top->y3 = y3;
1640 wx_stack_top->x4 = x4;
1641 wx_stack_top->y4 = y4;
1642 wx_stack_top++;
1643 wx_stack_count++;
1644 }
1645
1646 int wx_spline_pop(double *x1, double *y1, double *x2, double *y2,
1647 double *x3, double *y3, double *x4, double *y4)
1648 {
1649 if (wx_stack_count == 0)
1650 return (0);
1651 wx_stack_top--;
1652 wx_stack_count--;
1653 *x1 = wx_stack_top->x1;
1654 *y1 = wx_stack_top->y1;
1655 *x2 = wx_stack_top->x2;
1656 *y2 = wx_stack_top->y2;
1657 *x3 = wx_stack_top->x3;
1658 *y3 = wx_stack_top->y3;
1659 *x4 = wx_stack_top->x4;
1660 *y4 = wx_stack_top->y4;
1661 return (1);
1662 }
1663
1664 static bool wx_spline_add_point(double x, double y)
1665 {
1666 wxPoint *point = new wxPoint ;
1667 point->x = (int) x;
1668 point->y = (int) y;
1669 wx_spline_point_list.Append((wxObject*)point);
1670 return TRUE;
1671 }
1672
1673 static void wx_spline_draw_point_array(wxDC *dc)
1674 {
1675 dc->DrawLines(&wx_spline_point_list, (double)0.0, (double)0.0);
1676 wxNode *node = wx_spline_point_list.First();
1677 while (node)
1678 {
1679 wxPoint *point = (wxPoint *)node->Data();
1680 delete point;
1681 delete node;
1682 node = wx_spline_point_list.First();
1683 }
1684 }
1685
1686 wxSpline::wxSpline(wxList *list)
1687 {
1688 points = list;
1689 }
1690
1691 wxSpline::~wxSpline(void)
1692 {
1693 }
1694
1695 void wxSpline::DeletePoints(void)
1696 {
1697 for(wxNode *node = points->First(); node; node = points->First())
1698 {
1699 wxPoint *point = (wxPoint *)node->Data();
1700 delete point;
1701 delete node;
1702 }
1703 delete points;
1704 }
1705
1706
1707 #endif // wxUSE_SPLINES
1708