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