]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/msw/dc.cpp
Commited updated list of files to compile.
[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
27#ifdef __BORLANDC__
28 #pragma hdrstop
29#endif
30
31#ifndef WX_PRECOMP
32 #include "wx/window.h"
33 #include "wx/dc.h"
34 #include "wx/utils.h"
35 #include "wx/dialog.h"
36 #include "wx/app.h"
37 #include "wx/bitmap.h"
38 #include "wx/dcmemory.h"
39 #include "wx/log.h"
40 #include "wx/icon.h"
41#endif
42
43#include "wx/dcprint.h"
44
45#include <string.h>
46#include <math.h>
47
48#include "wx/msw/private.h" // needs to be before #include <commdlg.h>
49
50#if wxUSE_COMMON_DIALOGS
51 #include <commdlg.h>
52#endif
53
54#ifndef __WIN32__
55 #include <print.h>
56#endif
57
58IMPLEMENT_ABSTRACT_CLASS(wxDC, wxDCBase)
59
60// ---------------------------------------------------------------------------
61// constants
62// ---------------------------------------------------------------------------
63
64static const int VIEWPORT_EXTENT = 1000;
65
66static const int MM_POINTS = 9;
67static const int MM_METRIC = 10;
68
69// usually this is defined in math.h
70#ifndef M_PI
71 static const double M_PI = 3.14159265358979323846;
72#endif // M_PI
73
74// ROPs which don't have standard names (see "Ternary Raster Operations" in the
75// MSDN docs for how this and other numbers in wxDC::Blit() are obtained)
76#define DSTCOPY 0x00AA0029 // a.k.a. NOP operation
77
78// ---------------------------------------------------------------------------
79// private functions
80// ---------------------------------------------------------------------------
81
82// convert degrees to radians
83static inline double DegToRad(double deg) { return (deg * M_PI) / 180.0; }
84
85// ----------------------------------------------------------------------------
86// private classes
87// ----------------------------------------------------------------------------
88
89// instead of duplicating the same code which sets and then restores text
90// colours in each wxDC method working with wxSTIPPLE_MASK_OPAQUE brushes,
91// encapsulate this in a small helper class
92
93// wxColourChanger: changes the text colours in the ctor if required and
94// restores them in the dtor
95class wxColourChanger
96{
97public:
98 wxColourChanger(wxDC& dc);
99 ~wxColourChanger();
100
101private:
102 wxDC& m_dc;
103
104 COLORREF m_colFgOld, m_colBgOld;
105
106 bool m_changed;
107};
108
109// ===========================================================================
110// implementation
111// ===========================================================================
112
113// ----------------------------------------------------------------------------
114// wxColourChanger
115// ----------------------------------------------------------------------------
116
117wxColourChanger::wxColourChanger(wxDC& dc) : m_dc(dc)
118{
119 if ( dc.GetBrush().GetStyle() == wxSTIPPLE_MASK_OPAQUE )
120 {
121 HDC hdc = GetHdcOf(dc);
122 m_colFgOld = ::GetTextColor(hdc);
123 m_colBgOld = ::GetBkColor(hdc);
124
125 // note that Windows convention is opposite to wxWindows one, this is
126 // why text colour becomes the background one and vice versa
127 const wxColour& colFg = dc.GetTextForeground();
128 if ( colFg.Ok() )
129 {
130 ::SetBkColor(hdc, colFg.GetPixel());
131 }
132
133 const wxColour& colBg = dc.GetTextBackground();
134 if ( colBg.Ok() )
135 {
136 ::SetTextColor(hdc, colBg.GetPixel());
137 }
138
139 SetBkMode(hdc,
140 dc.GetBackgroundMode() == wxTRANSPARENT ? TRANSPARENT
141 : OPAQUE);
142
143 // flag which telsl us to undo changes in the dtor
144 m_changed = TRUE;
145 }
146 else
147 {
148 // nothing done, nothing to undo
149 m_changed = FALSE;
150 }
151}
152
153wxColourChanger::~wxColourChanger()
154{
155 if ( m_changed )
156 {
157 // restore the colours we changed
158 HDC hdc = GetHdcOf(m_dc);
159
160 ::SetBkMode(hdc, TRANSPARENT);
161 ::SetTextColor(hdc, m_colFgOld);
162 ::SetBkColor(hdc, m_colBgOld);
163 }
164}
165
166// ---------------------------------------------------------------------------
167// wxDC
168// ---------------------------------------------------------------------------
169
170// Default constructor
171wxDC::wxDC()
172{
173 m_canvas = NULL;
174
175 m_oldBitmap = 0;
176 m_oldPen = 0;
177 m_oldBrush = 0;
178 m_oldFont = 0;
179 m_oldPalette = 0;
180
181 m_bOwnsDC = FALSE;
182 m_hDC = 0;
183
184 m_windowExtX = VIEWPORT_EXTENT;
185 m_windowExtY = VIEWPORT_EXTENT;
186}
187
188
189wxDC::~wxDC()
190{
191 if ( m_hDC != 0 )
192 {
193 SelectOldObjects(m_hDC);
194
195 // if we own the HDC, we delete it, otherwise we just release it
196
197 if ( m_bOwnsDC )
198 {
199 ::DeleteDC(GetHdc());
200 }
201 else // we don't own our HDC
202 {
203 if (m_canvas)
204 {
205 ::ReleaseDC(GetHwndOf(m_canvas), GetHdc());
206 }
207 else
208 {
209 // Must have been a wxScreenDC
210 ::ReleaseDC((HWND) NULL, GetHdc());
211 }
212 }
213 }
214}
215
216// This will select current objects out of the DC,
217// which is what you have to do before deleting the
218// DC.
219void wxDC::SelectOldObjects(WXHDC dc)
220{
221 if (dc)
222 {
223 if (m_oldBitmap)
224 {
225 ::SelectObject((HDC) dc, (HBITMAP) m_oldBitmap);
226 if (m_selectedBitmap.Ok())
227 {
228 m_selectedBitmap.SetSelectedInto(NULL);
229 }
230 }
231 m_oldBitmap = 0;
232 if (m_oldPen)
233 {
234 ::SelectObject((HDC) dc, (HPEN) m_oldPen);
235 }
236 m_oldPen = 0;
237 if (m_oldBrush)
238 {
239 ::SelectObject((HDC) dc, (HBRUSH) m_oldBrush);
240 }
241 m_oldBrush = 0;
242 if (m_oldFont)
243 {
244 ::SelectObject((HDC) dc, (HFONT) m_oldFont);
245 }
246 m_oldFont = 0;
247 if (m_oldPalette)
248 {
249 ::SelectPalette((HDC) dc, (HPALETTE) m_oldPalette, TRUE);
250 }
251 m_oldPalette = 0;
252 }
253
254 m_brush = wxNullBrush;
255 m_pen = wxNullPen;
256 m_palette = wxNullPalette;
257 m_font = wxNullFont;
258 m_backgroundBrush = wxNullBrush;
259 m_selectedBitmap = wxNullBitmap;
260}
261
262// ---------------------------------------------------------------------------
263// clipping
264// ---------------------------------------------------------------------------
265
266#define DO_SET_CLIPPING_BOX() \
267{ \
268 RECT rect; \
269 \
270 GetClipBox(GetHdc(), &rect); \
271 \
272 m_clipX1 = (wxCoord) XDEV2LOG(rect.left); \
273 m_clipY1 = (wxCoord) YDEV2LOG(rect.top); \
274 m_clipX2 = (wxCoord) XDEV2LOG(rect.right); \
275 m_clipY2 = (wxCoord) YDEV2LOG(rect.bottom); \
276}
277
278void wxDC::DoSetClippingRegion(wxCoord cx, wxCoord cy, wxCoord cw, wxCoord ch)
279{
280 m_clipping = TRUE;
281 IntersectClipRect(GetHdc(), XLOG2DEV(cx), YLOG2DEV(cy),
282 XLOG2DEV(cx + cw), YLOG2DEV(cy + ch));
283 DO_SET_CLIPPING_BOX()
284}
285
286void wxDC::DoSetClippingRegionAsRegion(const wxRegion& region)
287{
288 wxCHECK_RET( region.GetHRGN(), wxT("invalid clipping region") );
289
290 m_clipping = TRUE;
291
292#ifdef __WIN16__
293 SelectClipRgn(GetHdc(), (HRGN) region.GetHRGN());
294#else
295 ExtSelectClipRgn(GetHdc(), (HRGN) region.GetHRGN(), RGN_AND);
296#endif
297
298 DO_SET_CLIPPING_BOX()
299}
300
301void wxDC::DestroyClippingRegion()
302{
303 if (m_clipping && m_hDC)
304 {
305 // TODO: this should restore the previous clipping region,
306 // so that OnPaint processing works correctly, and the update clipping region
307 // doesn't get destroyed after the first DestroyClippingRegion.
308 HRGN rgn = CreateRectRgn(0, 0, 32000, 32000);
309 SelectClipRgn(GetHdc(), rgn);
310 DeleteObject(rgn);
311 }
312 m_clipping = FALSE;
313}
314
315// ---------------------------------------------------------------------------
316// query capabilities
317// ---------------------------------------------------------------------------
318
319bool wxDC::CanDrawBitmap() const
320{
321 return TRUE;
322}
323
324bool wxDC::CanGetTextExtent() const
325{
326 // What sort of display is it?
327 int technology = ::GetDeviceCaps(GetHdc(), TECHNOLOGY);
328
329 return (technology == DT_RASDISPLAY) || (technology == DT_RASPRINTER);
330}
331
332int wxDC::GetDepth() const
333{
334 return (int)::GetDeviceCaps(GetHdc(), BITSPIXEL);
335}
336
337// ---------------------------------------------------------------------------
338// drawing
339// ---------------------------------------------------------------------------
340
341void wxDC::Clear()
342{
343 RECT rect;
344 if ( m_canvas )
345 {
346 GetClientRect((HWND) m_canvas->GetHWND(), &rect);
347 }
348 else
349 {
350 // No, I think we should simply ignore this if printing on e.g.
351 // a printer DC.
352 // wxCHECK_RET( m_selectedBitmap.Ok(), wxT("this DC can't be cleared") );
353 if (!m_selectedBitmap.Ok())
354 return;
355
356 rect.left = 0; rect.top = 0;
357 rect.right = m_selectedBitmap.GetWidth();
358 rect.bottom = m_selectedBitmap.GetHeight();
359 }
360
361 (void) ::SetMapMode(GetHdc(), MM_TEXT);
362
363 DWORD colour = GetBkColor(GetHdc());
364 HBRUSH brush = CreateSolidBrush(colour);
365 FillRect(GetHdc(), &rect, brush);
366 DeleteObject(brush);
367
368 ::SetMapMode(GetHdc(), MM_ANISOTROPIC);
369 ::SetViewportExtEx(GetHdc(), VIEWPORT_EXTENT, VIEWPORT_EXTENT, NULL);
370 ::SetWindowExtEx(GetHdc(), m_windowExtX, m_windowExtY, NULL);
371 ::SetViewportOrgEx(GetHdc(), (int)m_deviceOriginX, (int)m_deviceOriginY, NULL);
372 ::SetWindowOrgEx(GetHdc(), (int)m_logicalOriginX, (int)m_logicalOriginY, NULL);
373}
374
375void wxDC::DoFloodFill(wxCoord x, wxCoord y, const wxColour& col, int style)
376{
377 if ( !::ExtFloodFill(GetHdc(), XLOG2DEV(x), YLOG2DEV(y),
378 col.GetPixel(),
379 style == wxFLOOD_SURFACE ? FLOODFILLSURFACE
380 : FLOODFILLBORDER) )
381 {
382 // quoting from the MSDN docs:
383 //
384 // Following are some of the reasons this function might fail:
385 //
386 // * The filling could not be completed.
387 // * The specified point has the boundary color specified by the
388 // crColor parameter (if FLOODFILLBORDER was requested).
389 // * The specified point does not have the color specified by
390 // crColor (if FLOODFILLSURFACE was requested)
391 // * The point is outside the clipping region that is, it is not
392 // visible on the device.
393 //
394 wxLogLastError(wxT("ExtFloodFill"));
395 }
396
397 CalcBoundingBox(x, y);
398}
399
400bool wxDC::DoGetPixel(wxCoord x, wxCoord y, wxColour *col) const
401{
402 wxCHECK_MSG( col, FALSE, _T("NULL colour parameter in wxDC::GetPixel") );
403
404 // get the color of the pixel
405 COLORREF pixelcolor = ::GetPixel(GetHdc(), XLOG2DEV(x), YLOG2DEV(y));
406
407 wxRGBToColour(*col, pixelcolor);
408
409 return TRUE;
410}
411
412void wxDC::DoCrossHair(wxCoord x, wxCoord y)
413{
414 wxCoord x1 = x-VIEWPORT_EXTENT;
415 wxCoord y1 = y-VIEWPORT_EXTENT;
416 wxCoord x2 = x+VIEWPORT_EXTENT;
417 wxCoord y2 = y+VIEWPORT_EXTENT;
418
419 (void)MoveToEx(GetHdc(), XLOG2DEV(x1), YLOG2DEV(y), NULL);
420 (void)LineTo(GetHdc(), XLOG2DEV(x2), YLOG2DEV(y));
421
422 (void)MoveToEx(GetHdc(), XLOG2DEV(x), YLOG2DEV(y1), NULL);
423 (void)LineTo(GetHdc(), XLOG2DEV(x), YLOG2DEV(y2));
424
425 CalcBoundingBox(x1, y1);
426 CalcBoundingBox(x2, y2);
427}
428
429void wxDC::DoDrawLine(wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2)
430{
431 (void)MoveToEx(GetHdc(), XLOG2DEV(x1), YLOG2DEV(y1), NULL);
432 (void)LineTo(GetHdc(), XLOG2DEV(x2), YLOG2DEV(y2));
433
434 // Normalization: Windows doesn't draw the last point of the line.
435 // But apparently neither does GTK+, so we take it out again.
436// (void)LineTo(GetHdc(), XLOG2DEV(x2) + 1, YLOG2DEV(y2));
437
438 CalcBoundingBox(x1, y1);
439 CalcBoundingBox(x2, y2);
440}
441
442// Draws an arc of a circle, centred on (xc, yc), with starting point (x1, y1)
443// and ending at (x2, y2)
444void wxDC::DoDrawArc(wxCoord x1, wxCoord y1,
445 wxCoord x2, wxCoord y2,
446 wxCoord xc, wxCoord yc)
447{
448 wxColourChanger cc(*this); // needed for wxSTIPPLE_MASK_OPAQUE handling
449
450 double dx = xc - x1;
451 double dy = yc - y1;
452 double radius = (double)sqrt(dx*dx+dy*dy);
453 wxCoord r = (wxCoord)radius;
454
455 // treat the special case of full circle separately
456 if ( x1 == x2 && y1 == y2 )
457 {
458 DrawEllipse(xc - r, yc - r, 2*r, 2*r);
459 return;
460 }
461
462 wxCoord xx1 = XLOG2DEV(x1);
463 wxCoord yy1 = YLOG2DEV(y1);
464 wxCoord xx2 = XLOG2DEV(x2);
465 wxCoord yy2 = YLOG2DEV(y2);
466 wxCoord xxc = XLOG2DEV(xc);
467 wxCoord yyc = YLOG2DEV(yc);
468 wxCoord ray = (wxCoord) sqrt(double((xxc-xx1)*(xxc-xx1)+(yyc-yy1)*(yyc-yy1)));
469
470 wxCoord xxx1 = (wxCoord) (xxc-ray);
471 wxCoord yyy1 = (wxCoord) (yyc-ray);
472 wxCoord xxx2 = (wxCoord) (xxc+ray);
473 wxCoord yyy2 = (wxCoord) (yyc+ray);
474
475 if ( m_brush.Ok() && m_brush.GetStyle() != wxTRANSPARENT )
476 {
477 // Have to add 1 to bottom-right corner of rectangle
478 // to make semi-circles look right (crooked line otherwise).
479 // Unfortunately this is not a reliable method, depends
480 // on the size of shape.
481 // TODO: figure out why this happens!
482 Pie(GetHdc(),xxx1,yyy1,xxx2+1,yyy2+1, xx1,yy1,xx2,yy2);
483 }
484 else
485 {
486 Arc(GetHdc(),xxx1,yyy1,xxx2,yyy2, xx1,yy1,xx2,yy2);
487 }
488
489 CalcBoundingBox(xc - r, yc - r);
490 CalcBoundingBox(xc + r, yc + r);
491}
492
493void wxDC::DoDrawCheckMark(wxCoord x1, wxCoord y1,
494 wxCoord width, wxCoord height)
495{
496 wxCoord x2 = x1 + width,
497 y2 = y1 + height;
498
499#if defined(__WIN32__) && !defined(__SC__)
500 RECT rect;
501 rect.left = x1;
502 rect.top = y1;
503 rect.right = x2;
504 rect.bottom = y2;
505
506 DrawFrameControl(GetHdc(), &rect, DFC_MENU, DFCS_MENUCHECK);
507#else // Win16
508 // In WIN16, draw a cross
509 HPEN blackPen = ::CreatePen(PS_SOLID, 1, RGB(0, 0, 0));
510 HPEN whiteBrush = (HPEN)::GetStockObject(WHITE_BRUSH);
511 HPEN hPenOld = (HPEN)::SelectObject(GetHdc(), blackPen);
512 HPEN hBrushOld = (HPEN)::SelectObject(GetHdc(), whiteBrush);
513 ::SetROP2(GetHdc(), R2_COPYPEN);
514 Rectangle(GetHdc(), x1, y1, x2, y2);
515 MoveTo(GetHdc(), x1, y1);
516 LineTo(GetHdc(), x2, y2);
517 MoveTo(GetHdc(), x2, y1);
518 LineTo(GetHdc(), x1, y2);
519 ::SelectObject(GetHdc(), hPenOld);
520 ::SelectObject(GetHdc(), hBrushOld);
521 ::DeleteObject(blackPen);
522#endif // Win32/16
523
524 CalcBoundingBox(x1, y1);
525 CalcBoundingBox(x2, y2);
526}
527
528void wxDC::DoDrawPoint(wxCoord x, wxCoord y)
529{
530 COLORREF color = 0x00ffffff;
531 if (m_pen.Ok())
532 {
533 color = m_pen.GetColour().GetPixel();
534 }
535
536 SetPixel(GetHdc(), XLOG2DEV(x), YLOG2DEV(y), color);
537
538 CalcBoundingBox(x, y);
539}
540
541void wxDC::DoDrawPolygon(int n, wxPoint points[], wxCoord xoffset, wxCoord yoffset,int fillStyle)
542{
543 wxColourChanger cc(*this); // needed for wxSTIPPLE_MASK_OPAQUE handling
544
545 // Do things less efficiently if we have offsets
546 if (xoffset != 0 || yoffset != 0)
547 {
548 POINT *cpoints = new POINT[n];
549 int i;
550 for (i = 0; i < n; i++)
551 {
552 cpoints[i].x = (int)(points[i].x + xoffset);
553 cpoints[i].y = (int)(points[i].y + yoffset);
554
555 CalcBoundingBox(cpoints[i].x, cpoints[i].y);
556 }
557 int prev = SetPolyFillMode(GetHdc(),fillStyle==wxODDEVEN_RULE?ALTERNATE:WINDING);
558 (void)Polygon(GetHdc(), cpoints, n);
559 SetPolyFillMode(GetHdc(),prev);
560 delete[] cpoints;
561 }
562 else
563 {
564 int i;
565 for (i = 0; i < n; i++)
566 CalcBoundingBox(points[i].x, points[i].y);
567
568 int prev = SetPolyFillMode(GetHdc(),fillStyle==wxODDEVEN_RULE?ALTERNATE:WINDING);
569 (void)Polygon(GetHdc(), (POINT*) points, n);
570 SetPolyFillMode(GetHdc(),prev);
571 }
572}
573
574void wxDC::DoDrawLines(int n, wxPoint points[], wxCoord xoffset, wxCoord yoffset)
575{
576 // Do things less efficiently if we have offsets
577 if (xoffset != 0 || yoffset != 0)
578 {
579 POINT *cpoints = new POINT[n];
580 int i;
581 for (i = 0; i < n; i++)
582 {
583 cpoints[i].x = (int)(points[i].x + xoffset);
584 cpoints[i].y = (int)(points[i].y + yoffset);
585
586 CalcBoundingBox(cpoints[i].x, cpoints[i].y);
587 }
588 (void)Polyline(GetHdc(), cpoints, n);
589 delete[] cpoints;
590 }
591 else
592 {
593 int i;
594 for (i = 0; i < n; i++)
595 CalcBoundingBox(points[i].x, points[i].y);
596
597 (void)Polyline(GetHdc(), (POINT*) points, n);
598 }
599}
600
601void wxDC::DoDrawRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
602{
603 wxColourChanger cc(*this); // needed for wxSTIPPLE_MASK_OPAQUE handling
604
605 wxCoord x2 = x + width;
606 wxCoord y2 = y + height;
607
608 if ((m_logicalFunction == wxCOPY) && (m_pen.GetStyle() == wxTRANSPARENT))
609 {
610 RECT rect;
611 rect.left = XLOG2DEV(x);
612 rect.top = YLOG2DEV(y);
613 rect.right = XLOG2DEV(x2);
614 rect.bottom = YLOG2DEV(y2);
615 (void)FillRect(GetHdc(), &rect, (HBRUSH)m_brush.GetResourceHandle() );
616 }
617 else
618 {
619 // Windows draws the filled rectangles without outline (i.e. drawn with a
620 // transparent pen) one pixel smaller in both directions and we want them
621 // to have the same size regardless of which pen is used - adjust
622
623