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