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