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