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