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