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