]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/msw/dc.cpp
remove the #warning for VisualAge
[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, wxObject)
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// implementation
87// ===========================================================================
88
89// ---------------------------------------------------------------------------
90// wxDC
91// ---------------------------------------------------------------------------
92
93// Default constructor
94wxDC::wxDC()
95{
96 m_canvas = NULL;
97
98 m_oldBitmap = 0;
99 m_oldPen = 0;
100 m_oldBrush = 0;
101 m_oldFont = 0;
102 m_oldPalette = 0;
103
104 m_bOwnsDC = FALSE;
105 m_hDC = 0;
106
107 m_windowExtX = VIEWPORT_EXTENT;
108 m_windowExtY = VIEWPORT_EXTENT;
109
110 m_hDCCount = 0;
111}
112
113
114wxDC::~wxDC()
115{
116 if ( m_hDC != 0 ) {
117 SelectOldObjects(m_hDC);
118 if ( m_bOwnsDC ) {
119 if ( m_canvas == NULL )
120 ::DeleteDC(GetHdc());
121 else
122 ::ReleaseDC((HWND)m_canvas->GetHWND(), GetHdc());
123 }
124 }
125
126}
127
128// This will select current objects out of the DC,
129// which is what you have to do before deleting the
130// DC.
131void wxDC::SelectOldObjects(WXHDC dc)
132{
133 if (dc)
134 {
135 if (m_oldBitmap)
136 {
137 ::SelectObject((HDC) dc, (HBITMAP) m_oldBitmap);
138 if (m_selectedBitmap.Ok())
139 {
140 m_selectedBitmap.SetSelectedInto(NULL);
141 }
142 }
143 m_oldBitmap = 0;
144 if (m_oldPen)
145 {
146 ::SelectObject((HDC) dc, (HPEN) m_oldPen);
147 }
148 m_oldPen = 0;
149 if (m_oldBrush)
150 {
151 ::SelectObject((HDC) dc, (HBRUSH) m_oldBrush);
152 }
153 m_oldBrush = 0;
154 if (m_oldFont)
155 {
156 ::SelectObject((HDC) dc, (HFONT) m_oldFont);
157 }
158 m_oldFont = 0;
159 if (m_oldPalette)
160 {
161 ::SelectPalette((HDC) dc, (HPALETTE) m_oldPalette, TRUE);
162 }
163 m_oldPalette = 0;
164 }
165
166 m_brush = wxNullBrush;
167 m_pen = wxNullPen;
168 m_palette = wxNullPalette;
169 m_font = wxNullFont;
170 m_backgroundBrush = wxNullBrush;
171 m_selectedBitmap = wxNullBitmap;
172}
173
174// ---------------------------------------------------------------------------
175// clipping
176// ---------------------------------------------------------------------------
177
178#define DO_SET_CLIPPING_BOX() \
179{ \
180 RECT rect; \
181 \
182 GetClipBox(GetHdc(), &rect); \
183 \
184 m_clipX1 = (wxCoord) XDEV2LOG(rect.left); \
185 m_clipY1 = (wxCoord) YDEV2LOG(rect.top); \
186 m_clipX2 = (wxCoord) XDEV2LOG(rect.right); \
187 m_clipY2 = (wxCoord) YDEV2LOG(rect.bottom); \
188}
189
190void wxDC::DoSetClippingRegion(wxCoord cx, wxCoord cy, wxCoord cw, wxCoord ch)
191{
192 m_clipping = TRUE;
193 IntersectClipRect(GetHdc(), XLOG2DEV(cx), YLOG2DEV(cy),
194 XLOG2DEV(cx + cw), YLOG2DEV(cy + ch));
195 DO_SET_CLIPPING_BOX()
196}
197
198void wxDC::DoSetClippingRegionAsRegion(const wxRegion& region)
199{
200 wxCHECK_RET( region.GetHRGN(), wxT("invalid clipping region") );
201
202 m_clipping = TRUE;
203
204#ifdef __WIN16__
205 SelectClipRgn(GetHdc(), (HRGN) region.GetHRGN());
206#else
207 ExtSelectClipRgn(GetHdc(), (HRGN) region.GetHRGN(), RGN_AND);
208#endif
209
210 DO_SET_CLIPPING_BOX()
211}
212
213void wxDC::DestroyClippingRegion()
214{
215 if (m_clipping && m_hDC)
216 {
217 // TODO: this should restore the previous clipping region,
218 // so that OnPaint processing works correctly, and the update clipping region
219 // doesn't get destroyed after the first DestroyClippingRegion.
220 HRGN rgn = CreateRectRgn(0, 0, 32000, 32000);
221 SelectClipRgn(GetHdc(), rgn);
222 DeleteObject(rgn);
223 }
224 m_clipping = FALSE;
225}
226
227// ---------------------------------------------------------------------------
228// query capabilities
229// ---------------------------------------------------------------------------
230
231bool wxDC::CanDrawBitmap() const
232{
233 return TRUE;
234}
235
236bool wxDC::CanGetTextExtent() const
237{
238 // What sort of display is it?
239 int technology = ::GetDeviceCaps(GetHdc(), TECHNOLOGY);
240
241 return (technology == DT_RASDISPLAY) || (technology == DT_RASPRINTER);
242}
243
244int wxDC::GetDepth() const
245{
246 return (int)::GetDeviceCaps(GetHdc(), BITSPIXEL);
247}
248
249// ---------------------------------------------------------------------------
250// drawing
251// ---------------------------------------------------------------------------
252
253void wxDC::Clear()
254{
255 RECT rect;
256 if ( m_canvas )
257 {
258 GetClientRect((HWND) m_canvas->GetHWND(), &rect);
259 }
260 else
261 {
262 // No, I think we should simply ignore this if printing on e.g.
263 // a printer DC.
264 // wxCHECK_RET( m_selectedBitmap.Ok(), wxT("this DC can't be cleared") );
265 if (!m_selectedBitmap.Ok())
266 return;
267
268 rect.left = 0; rect.top = 0;
269 rect.right = m_selectedBitmap.GetWidth();
270 rect.bottom = m_selectedBitmap.GetHeight();
271 }
272
273 (void) ::SetMapMode(GetHdc(), MM_TEXT);
274
275 DWORD colour = GetBkColor(GetHdc());
276 HBRUSH brush = CreateSolidBrush(colour);
277 FillRect(GetHdc(), &rect, brush);
278 DeleteObject(brush);
279
280 ::SetMapMode(GetHdc(), MM_ANISOTROPIC);
281 ::SetViewportExtEx(GetHdc(), VIEWPORT_EXTENT, VIEWPORT_EXTENT, NULL);
282 ::SetWindowExtEx(GetHdc(), m_windowExtX, m_windowExtY, NULL);
283 ::SetViewportOrgEx(GetHdc(), (int)m_deviceOriginX, (int)m_deviceOriginY, NULL);
284 ::SetWindowOrgEx(GetHdc(), (int)m_logicalOriginX, (int)m_logicalOriginY, NULL);
285}
286
287void wxDC::DoFloodFill(wxCoord x, wxCoord y, const wxColour& col, int style)
288{
289 if ( !::ExtFloodFill(GetHdc(), XLOG2DEV(x), YLOG2DEV(y),
290 col.GetPixel(),
291 style == wxFLOOD_SURFACE ? FLOODFILLSURFACE
292 : FLOODFILLBORDER) )
293 {
294 // quoting from the MSDN docs:
295 //
296 // Following are some of the reasons this function might fail:
297 //
298 // * The filling could not be completed.
299 // * The specified point has the boundary color specified by the
300 // crColor parameter (if FLOODFILLBORDER was requested).
301 // * The specified point does not have the color specified by
302 // crColor (if FLOODFILLSURFACE was requested)
303 // * The point is outside the clipping region that is, it is not
304 // visible on the device.
305 //
306 wxLogLastError("ExtFloodFill");
307 }
308
309 CalcBoundingBox(x, y);
310}
311
312bool wxDC::DoGetPixel(wxCoord x, wxCoord y, wxColour *col) const
313{
314 // get the color of the pixel
315 COLORREF pixelcolor = ::GetPixel(GetHdc(), XLOG2DEV(x), YLOG2DEV(y));
316
317 // JACS: what was this for?
318#if 0
319 // get the color of the pen
320 COLORREF pencolor = 0x00ffffff;
321 if (m_pen.Ok())
322 {
323 pencolor = m_pen.GetColour().GetPixel();
324 }
325#endif
326
327 // return the color of the pixel
328 if( col )
329 {
330 col->Set(GetRValue(pixelcolor),
331 GetGValue(pixelcolor),
332 GetBValue(pixelcolor));
333 }
334
335 // check, if color of the pixels is the same as the color of the current
336 // pen and return TRUE if it is, FALSE otherwise
337 // JACS, 24/02/2000: can't understand the reason for this, so returning TRUE instead.
338 // return pixelcolor == pencolor;
339
340 return TRUE;
341}
342
343void wxDC::DoCrossHair(wxCoord x, wxCoord y)
344{
345 wxCoord x1 = x-VIEWPORT_EXTENT;
346 wxCoord y1 = y-VIEWPORT_EXTENT;
347 wxCoord x2 = x+VIEWPORT_EXTENT;
348 wxCoord y2 = y+VIEWPORT_EXTENT;
349
350 (void)MoveToEx(GetHdc(), XLOG2DEV(x1), YLOG2DEV(y), NULL);
351 (void)LineTo(GetHdc(), XLOG2DEV(x2), YLOG2DEV(y));
352
353 (void)MoveToEx(GetHdc(), XLOG2DEV(x), YLOG2DEV(y1), NULL);
354 (void)LineTo(GetHdc(), XLOG2DEV(x), YLOG2DEV(y2));
355
356 CalcBoundingBox(x1, y1);
357 CalcBoundingBox(x2, y2);
358}
359
360void wxDC::DoDrawLine(wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2)
361{
362 (void)MoveToEx(GetHdc(), XLOG2DEV(x1), YLOG2DEV(y1), NULL);
363 (void)LineTo(GetHdc(), XLOG2DEV(x2), YLOG2DEV(y2));
364
365 // Normalization: Windows doesn't draw the last point of the line.
366 // But apparently neither does GTK+, so we take it out again.
367// (void)LineTo(GetHdc(), XLOG2DEV(x2) + 1, YLOG2DEV(y2));
368
369 CalcBoundingBox(x1, y1);
370 CalcBoundingBox(x2, y2);
371}
372
373void wxDC::DoDrawArc(wxCoord x1,wxCoord y1,wxCoord x2,wxCoord y2, wxCoord xc, wxCoord yc)
374{
375 double dx = xc-x1;
376 double dy = yc-y1;
377 double radius = (double)sqrt(dx*dx+dy*dy) ;;
378 if (x1==x2 && x2==y2)
379 {
380 DrawEllipse(xc,yc,(wxCoord)(radius*2.0),(wxCoord)(radius*2.0));
381 return;
382 }
383
384 wxCoord xx1 = XLOG2DEV(x1);
385 wxCoord yy1 = YLOG2DEV(y1);
386 wxCoord xx2 = XLOG2DEV(x2);
387 wxCoord yy2 = YLOG2DEV(y2);
388 wxCoord xxc = XLOG2DEV(xc);
389 wxCoord yyc = YLOG2DEV(yc);
390 wxCoord ray = (wxCoord) sqrt(double((xxc-xx1)*(xxc-xx1)+(yyc-yy1)*(yyc-yy1)));
391
392 (void)MoveToEx(GetHdc(), (int) xx1, (int) yy1, NULL);
393 wxCoord xxx1 = (wxCoord) (xxc-ray);
394 wxCoord yyy1 = (wxCoord) (yyc-ray);
395 wxCoord xxx2 = (wxCoord) (xxc+ray);
396 wxCoord yyy2 = (wxCoord) (yyc+ray);
397 if (m_brush.Ok() && m_brush.GetStyle() !=wxTRANSPARENT)
398 {
399 // Have to add 1 to bottom-right corner of rectangle
400 // to make semi-circles look right (crooked line otherwise).
401 // Unfortunately this is not a reliable method, depends
402 // on the size of shape.
403 // TODO: figure out why this happens!
404 Pie(GetHdc(),xxx1,yyy1,xxx2+1,yyy2+1,
405 xx1,yy1,xx2,yy2);
406 }
407 else
408 Arc(GetHdc(),xxx1,yyy1,xxx2,yyy2,
409 xx1,yy1,xx2,yy2);
410
411 CalcBoundingBox((wxCoord)(xc-radius), (wxCoord)(yc-radius));
412 CalcBoundingBox((wxCoord)(xc+radius), (wxCoord)(yc+radius));
413}
414
415void wxDC::DoDrawCheckMark(wxCoord x1, wxCoord y1,
416 wxCoord width, wxCoord height)
417{
418 wxCoord x2 = x1 + width,
419 y2 = y1 + height;
420
421#if defined(__WIN32__) && !defined(__SC__)
422 RECT rect;
423 rect.left = x1;
424 rect.top = y1;
425 rect.right = x2;
426 rect.bottom = y2;
427
428 DrawFrameControl(GetHdc(), &rect, DFC_MENU, DFCS_MENUCHECK);
429#else // Win16
430 // In WIN16, draw a cross
431 HPEN blackPen = ::CreatePen(PS_SOLID, 1, RGB(0, 0, 0));
432 HPEN whiteBrush = (HPEN)::GetStockObject(WHITE_BRUSH);
433 HPEN hPenOld = (HPEN)::SelectObject(GetHdc(), blackPen);
434 HPEN hBrushOld = (HPEN)::SelectObject(GetHdc(), whiteBrush);
435 ::SetROP2(GetHdc(), R2_COPYPEN);
436 Rectangle(GetHdc(), x1, y1, x2, y2);
437 MoveTo(GetHdc(), x1, y1);
438 LineTo(GetHdc(), x2, y2);
439 MoveTo(GetHdc(), x2, y1);
440 LineTo(GetHdc(), x1, y2);
441 ::SelectObject(GetHdc(), hPenOld);
442 ::SelectObject(GetHdc(), hBrushOld);
443 ::DeleteObject(blackPen);
444#endif // Win32/16
445
446 CalcBoundingBox(x1, y1);
447 CalcBoundingBox(x2, y2);
448}
449
450void wxDC::DoDrawPoint(wxCoord x, wxCoord y)
451{
452 COLORREF color = 0x00ffffff;
453 if (m_pen.Ok())
454 {
455 color = m_pen.GetColour().GetPixel();
456 }
457
458 SetPixel(GetHdc(), XLOG2DEV(x), YLOG2DEV(y), color);
459
460 CalcBoundingBox(x, y);
461}
462
463void wxDC::DoDrawPolygon(int n, wxPoint points[], wxCoord xoffset, wxCoord yoffset,int fillStyle)
464{
465 COLORREF old_textground = ::GetTextColor(GetHdc());
466 COLORREF old_background = ::GetBkColor(GetHdc());
467 if (m_brush.GetStyle() == wxSTIPPLE_MASK_OPAQUE)
468 {
469
470 if (m_textForegroundColour.Ok())
471 { //just the oposite from what is expected see help on pattern brush
472 // 1 in mask becomes bk color
473 ::SetBkColor(GetHdc(), m_textForegroundColour.GetPixel() );
474 }
475 if (m_textBackgroundColour.Ok())
476 { //just the oposite from what is expected
477 // 0 in mask becomes text color
478 ::SetTextColor(GetHdc(), m_textBackgroundColour.GetPixel() );
479 }
480
481 if (m_backgroundMode == wxTRANSPARENT)
482 SetBkMode(GetHdc(), TRANSPARENT);
483 else
484 SetBkMode(GetHdc(), OPAQUE);
485 }
486
487 // Do things less efficiently if we have offsets
488 if (xoffset != 0 || yoffset != 0)
489 {
490 POINT *cpoints = new POINT[n];
491 int i;
492 for (i = 0; i < n; i++)
493 {
494 cpoints[i].x = (int)(points[i].x + xoffset);
495 cpoints[i].y = (int)(points[i].y + yoffset);
496
497 CalcBoundingBox(cpoints[i].x, cpoints[i].y);
498 }
499 int prev = SetPolyFillMode(GetHdc(),fillStyle==wxODDEVEN_RULE?ALTERNATE:WINDING);
500 (void)Polygon(GetHdc(), cpoints, n);
501 SetPolyFillMode(GetHdc(),prev);
502 delete[] cpoints;
503 }
504 else
505 {
506 int i;
507 for (i = 0; i < n; i++)
508 CalcBoundingBox(points[i].x, points[i].y);
509
510 int prev = SetPolyFillMode(GetHdc(),fillStyle==wxODDEVEN_RULE?ALTERNATE:WINDING);
511 (void)Polygon(GetHdc(), (POINT*) points, n);
512 SetPolyFillMode(GetHdc(),prev);
513 }
514
515 if (m_brush.GetStyle() == wxSTIPPLE_MASK_OPAQUE)
516 {
517 ::SetBkMode(GetHdc(), TRANSPARENT);
518 ::SetTextColor(GetHdc(), old_textground);
519 ::SetBkColor(GetHdc(), old_background);
520 }
521}
522
523void wxDC::DoDrawLines(int n, wxPoint points[], wxCoord xoffset, wxCoord yoffset)
524{
525 // Do things less efficiently if we have offsets
526 if (xoffset != 0 || yoffset != 0)
527 {
528 POINT *cpoints = new POINT[n];
529 int i;
530 for (i = 0; i < n; i++)
531 {
532 cpoints[i].x = (int)(points[i].x + xoffset);
533 cpoints[i].y = (int)(points[i].y + yoffset);
534
535 CalcBoundingBox(cpoints[i].x, cpoints[i].y);
536 }
537 (void)Polyline(GetHdc(), cpoints, n);
538 delete[] cpoints;
539 }
540 else
541 {
542 int i;
543 for (i = 0; i < n; i++)
544 CalcBoundingBox(points[i].x, points[i].y);
545
546 (void)Polyline(GetHdc(), (POINT*) points, n);
547 }
548}
549
550void wxDC::DoDrawRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
551{
552 COLORREF colFgOld = 0,
553 colBgOld = 0;
554
555 if ( m_brush.GetStyle() == wxSTIPPLE_MASK_OPAQUE )
556 {
557 colFgOld = ::GetTextColor(GetHdc());
558 colBgOld = ::GetBkColor(GetHdc());
559
560 if ( m_textForegroundColour.Ok() )
561 {
562 // just the oposite from what is expected see help on pattern brush
563 // 1 in mask becomes bk color
564 ::SetBkColor(GetHdc(), m_textForegroundColour.GetPixel());
565 }
566
567 if ( m_textBackgroundColour.Ok() )
568 {
569 // 0 in mask becomes text color
570 ::SetTextColor(GetHdc(), m_textBackgroundColour.GetPixel());
571 }
572
573 // VZ: IMHO this does strictly nothing here
574 SetBkMode(GetHdc(), m_backgroundMode == wxTRANSPARENT ? TRANSPARENT
575 : OPAQUE);
576 }
577
578 wxCoord x2 = x + width;
579 wxCoord y2 = y + height;
580
581 if ((m_logicalFunction == wxCOPY) && (m_pen.GetStyle() == wxTRANSPARENT))
582 {
583 RECT rect;
584 rect.left = XLOG2DEV(x);
585 rect.top = YLOG2DEV(y);
586 rect.right = XLOG2DEV(x2);
587 rect.bottom = YLOG2DEV(y2);
588 (void)FillRect(GetHdc(), &rect, (HBRUSH)m_brush.GetResourceHandle() );
589 }
590 else
591 {
592 // Windows draws the filled rectangles without outline (i.e. drawn with a
593 // transparent pen) one pixel smaller in both directions and we want them
594 // to have the same size regardless of which pen is used - adjust
595
596