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