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