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