]>
Commit | Line | Data |
---|---|---|
1b89a5cd SC |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: src/common/graphcmn.cpp | |
3 | // Purpose: graphics context methods common to all platforms | |
4 | // Author: Stefan Csomor | |
5 | // Modified by: | |
6c0aace2 | 6 | // Created: |
1b89a5cd SC |
7 | // RCS-ID: $Id$ |
8 | // Copyright: (c) Stefan Csomor | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // For compilers that support precompilation, includes "wx.h". | |
13 | #include "wx/wxprec.h" | |
14 | ||
15 | #if defined(__BORLANDC__) | |
16 | #pragma hdrstop | |
17 | #endif | |
18 | ||
19 | #if wxUSE_GRAPHICS_CONTEXT | |
20 | ||
21 | #include "wx/graphics.h" | |
22 | ||
23 | #ifndef WX_PRECOMP | |
24 | #include "wx/icon.h" | |
25 | #include "wx/bitmap.h" | |
26 | #include "wx/dcmemory.h" | |
27 | #include "wx/region.h" | |
28 | #endif | |
29 | ||
6c0aace2 VZ |
30 | #ifndef wxMAC_USE_CORE_GRAPHICS_BLEND_MODES |
31 | #define wxMAC_USE_CORE_GRAPHICS_BLEND_MODES 0 | |
32 | #endif | |
33 | ||
1b89a5cd SC |
34 | //----------------------------------------------------------------------------- |
35 | // constants | |
36 | //----------------------------------------------------------------------------- | |
37 | ||
38 | static const double RAD2DEG = 180.0 / M_PI; | |
39 | ||
40 | //----------------------------------------------------------------------------- | |
41 | // Local functions | |
42 | //----------------------------------------------------------------------------- | |
43 | ||
44 | static inline double DegToRad(double deg) | |
45 | { | |
46 | return (deg * M_PI) / 180.0; | |
47 | } | |
48 | ||
49 | //----------------------------------------------------------------------------- | |
50 | // wxDC bridge class | |
51 | //----------------------------------------------------------------------------- | |
52 | ||
53 | #ifdef __WXMAC__ | |
54 | IMPLEMENT_DYNAMIC_CLASS(wxGCDC, wxDCBase) | |
55 | #else | |
56 | IMPLEMENT_DYNAMIC_CLASS(wxGCDC, wxDC) | |
57 | #endif | |
58 | ||
59 | wxGCDC::wxGCDC() | |
60 | { | |
61 | Init(); | |
62 | } | |
63 | ||
64 | void wxGCDC::SetGraphicsContext( wxGraphicsContext* ctx ) | |
6c0aace2 | 65 | { |
1b89a5cd SC |
66 | delete m_graphicContext; |
67 | m_graphicContext = ctx; | |
914fd3f1 SC |
68 | if ( m_graphicContext ) |
69 | { | |
70 | m_matrixOriginal = m_graphicContext->GetTransform(); | |
71 | m_ok = true; | |
fd791571 SC |
72 | // apply the stored transformations to the passed in context |
73 | ComputeScaleAndOrigin(); | |
ad667945 SC |
74 | m_graphicContext->SetFont( m_font , m_textForegroundColour ); |
75 | m_graphicContext->SetPen( m_pen ); | |
76 | m_graphicContext->SetBrush( m_brush); | |
914fd3f1 | 77 | } |
1b89a5cd SC |
78 | } |
79 | ||
80 | wxGCDC::wxGCDC(const wxWindowDC& dc) | |
81 | { | |
82 | Init(); | |
914fd3f1 | 83 | SetGraphicsContext( wxGraphicsContext::Create(dc) ); |
1b89a5cd SC |
84 | } |
85 | ||
86 | void wxGCDC::Init() | |
87 | { | |
88 | m_ok = false; | |
89 | m_colour = true; | |
90 | m_mm_to_pix_x = mm2pt; | |
91 | m_mm_to_pix_y = mm2pt; | |
92 | ||
93 | m_pen = *wxBLACK_PEN; | |
94 | m_font = *wxNORMAL_FONT; | |
95 | m_brush = *wxWHITE_BRUSH; | |
96 | ||
97 | m_graphicContext = NULL; | |
98 | } | |
99 | ||
100 | ||
101 | wxGCDC::~wxGCDC() | |
102 | { | |
103 | delete m_graphicContext; | |
104 | } | |
105 | ||
106 | void wxGCDC::DoDrawBitmap( const wxBitmap &bmp, wxCoord x, wxCoord y, bool WXUNUSED(useMask) ) | |
107 | { | |
108 | wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawBitmap - invalid DC") ); | |
109 | wxCHECK_RET( bmp.Ok(), wxT("wxGCDC(cg)::DoDrawBitmap - invalid bitmap") ); | |
110 | ||
f889bdb3 SC |
111 | if ( bmp.GetDepth() == 1 ) |
112 | { | |
113 | m_graphicContext->SetPen(*wxTRANSPARENT_PEN); | |
114 | m_graphicContext->SetBrush( wxBrush( m_textBackgroundColour , wxSOLID ) ); | |
115 | m_graphicContext->DrawRectangle( x , y , bmp.GetWidth() , bmp.GetHeight() ); | |
116 | m_graphicContext->SetBrush( wxBrush( m_textForegroundColour , wxSOLID ) ); | |
117 | m_graphicContext->DrawBitmap( bmp, x , y , bmp.GetWidth() , bmp.GetHeight() ); | |
118 | m_graphicContext->SetBrush( m_graphicContext->CreateBrush(m_brush)); | |
119 | m_graphicContext->SetPen( m_graphicContext->CreatePen(m_pen)); | |
120 | } | |
121 | else | |
122 | m_graphicContext->DrawBitmap( bmp, x , y , bmp.GetWidth() , bmp.GetHeight() ); | |
1b89a5cd SC |
123 | } |
124 | ||
125 | void wxGCDC::DoDrawIcon( const wxIcon &icon, wxCoord x, wxCoord y ) | |
126 | { | |
127 | wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawIcon - invalid DC") ); | |
128 | wxCHECK_RET( icon.Ok(), wxT("wxGCDC(cg)::DoDrawIcon - invalid icon") ); | |
129 | ||
1b89a5cd SC |
130 | wxCoord w = icon.GetWidth(); |
131 | wxCoord h = icon.GetHeight(); | |
1b89a5cd | 132 | |
0ebd9515 | 133 | m_graphicContext->DrawIcon( icon , x, y, w, h ); |
1b89a5cd SC |
134 | } |
135 | ||
0ebd9515 | 136 | void wxGCDC::DoSetClippingRegion( wxCoord x, wxCoord y, wxCoord w, wxCoord h ) |
1b89a5cd SC |
137 | { |
138 | wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoSetClippingRegion - invalid DC") ); | |
139 | ||
0ebd9515 | 140 | m_graphicContext->Clip( x, y, w, h ); |
1b89a5cd SC |
141 | if ( m_clipping ) |
142 | { | |
0ebd9515 SC |
143 | m_clipX1 = wxMax( m_clipX1, x ); |
144 | m_clipY1 = wxMax( m_clipY1, y ); | |
145 | m_clipX2 = wxMin( m_clipX2, (x + w) ); | |
146 | m_clipY2 = wxMin( m_clipY2, (y + h) ); | |
1b89a5cd SC |
147 | } |
148 | else | |
149 | { | |
150 | m_clipping = true; | |
151 | ||
0ebd9515 SC |
152 | m_clipX1 = x; |
153 | m_clipY1 = y; | |
154 | m_clipX2 = x + w; | |
155 | m_clipY2 = y + h; | |
1b89a5cd SC |
156 | } |
157 | } | |
158 | ||
159 | void wxGCDC::DoSetClippingRegionAsRegion( const wxRegion ®ion ) | |
160 | { | |
161 | wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoSetClippingRegionAsRegion - invalid DC") ); | |
162 | ||
163 | if (region.Empty()) | |
164 | { | |
165 | DestroyClippingRegion(); | |
166 | return; | |
167 | } | |
168 | ||
169 | wxCoord x, y, w, h; | |
170 | region.GetBox( x, y, w, h ); | |
0ebd9515 SC |
171 | |
172 | m_graphicContext->Clip( region ); | |
173 | if ( m_clipping ) | |
1b89a5cd | 174 | { |
0ebd9515 SC |
175 | m_clipX1 = wxMax( m_clipX1, x ); |
176 | m_clipY1 = wxMax( m_clipY1, y ); | |
177 | m_clipX2 = wxMin( m_clipX2, (x + w) ); | |
178 | m_clipY2 = wxMin( m_clipY2, (y + h) ); | |
1b89a5cd SC |
179 | } |
180 | else | |
181 | { | |
0ebd9515 | 182 | m_clipping = true; |
1b89a5cd | 183 | |
0ebd9515 SC |
184 | m_clipX1 = x; |
185 | m_clipY1 = y; | |
186 | m_clipX2 = x + w; | |
187 | m_clipY2 = y + h; | |
1b89a5cd SC |
188 | } |
189 | } | |
190 | ||
191 | void wxGCDC::DestroyClippingRegion() | |
192 | { | |
193 | m_graphicContext->ResetClip(); | |
194 | m_graphicContext->SetPen( m_pen ); | |
195 | m_graphicContext->SetBrush( m_brush ); | |
196 | ||
197 | m_clipping = false; | |
198 | } | |
199 | ||
200 | void wxGCDC::DoGetSizeMM( int* width, int* height ) const | |
201 | { | |
202 | int w = 0, h = 0; | |
203 | ||
204 | GetSize( &w, &h ); | |
205 | if (width) | |
206 | *width = long( double(w) / (m_scaleX * m_mm_to_pix_x) ); | |
207 | if (height) | |
208 | *height = long( double(h) / (m_scaleY * m_mm_to_pix_y) ); | |
209 | } | |
210 | ||
211 | void wxGCDC::SetTextForeground( const wxColour &col ) | |
212 | { | |
213 | wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::SetTextForeground - invalid DC") ); | |
214 | ||
215 | if ( col != m_textForegroundColour ) | |
216 | { | |
217 | m_textForegroundColour = col; | |
218 | m_graphicContext->SetFont( m_font, m_textForegroundColour ); | |
219 | } | |
220 | } | |
221 | ||
222 | void wxGCDC::SetTextBackground( const wxColour &col ) | |
223 | { | |
224 | wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::SetTextBackground - invalid DC") ); | |
225 | ||
226 | m_textBackgroundColour = col; | |
227 | } | |
228 | ||
229 | void wxGCDC::SetMapMode( int mode ) | |
230 | { | |
231 | switch (mode) | |
232 | { | |
233 | case wxMM_TWIPS: | |
234 | SetLogicalScale( twips2mm * m_mm_to_pix_x, twips2mm * m_mm_to_pix_y ); | |
235 | break; | |
236 | ||
237 | case wxMM_POINTS: | |
238 | SetLogicalScale( pt2mm * m_mm_to_pix_x, pt2mm * m_mm_to_pix_y ); | |
239 | break; | |
240 | ||
241 | case wxMM_METRIC: | |
242 | SetLogicalScale( m_mm_to_pix_x, m_mm_to_pix_y ); | |
243 | break; | |
244 | ||
245 | case wxMM_LOMETRIC: | |
246 | SetLogicalScale( m_mm_to_pix_x / 10.0, m_mm_to_pix_y / 10.0 ); | |
247 | break; | |
248 | ||
249 | case wxMM_TEXT: | |
250 | default: | |
251 | SetLogicalScale( 1.0, 1.0 ); | |
252 | break; | |
253 | } | |
254 | ||
255 | ComputeScaleAndOrigin(); | |
256 | } | |
257 | ||
258 | void wxGCDC::SetUserScale( double x, double y ) | |
259 | { | |
260 | // allow negative ? -> no | |
261 | ||
262 | m_userScaleX = x; | |
263 | m_userScaleY = y; | |
264 | ComputeScaleAndOrigin(); | |
265 | } | |
266 | ||
267 | void wxGCDC::SetLogicalScale( double x, double y ) | |
268 | { | |
269 | // allow negative ? | |
270 | m_logicalScaleX = x; | |
271 | m_logicalScaleY = y; | |
272 | ComputeScaleAndOrigin(); | |
273 | } | |
274 | ||
275 | void wxGCDC::SetLogicalOrigin( wxCoord x, wxCoord y ) | |
276 | { | |
277 | m_logicalOriginX = x * m_signX; // is this still correct ? | |
278 | m_logicalOriginY = y * m_signY; | |
279 | ComputeScaleAndOrigin(); | |
280 | } | |
281 | ||
282 | void wxGCDC::SetDeviceOrigin( wxCoord x, wxCoord y ) | |
283 | { | |
284 | m_deviceOriginX = x; | |
285 | m_deviceOriginY = y; | |
286 | ComputeScaleAndOrigin(); | |
287 | } | |
288 | ||
289 | void wxGCDC::SetAxisOrientation( bool xLeftRight, bool yBottomUp ) | |
290 | { | |
291 | m_signX = (xLeftRight ? 1 : -1); | |
292 | m_signY = (yBottomUp ? -1 : 1); | |
293 | ComputeScaleAndOrigin(); | |
294 | } | |
295 | ||
296 | wxSize wxGCDC::GetPPI() const | |
297 | { | |
298 | return wxSize(72, 72); | |
299 | } | |
300 | ||
301 | int wxGCDC::GetDepth() const | |
302 | { | |
303 | return 32; | |
304 | } | |
305 | ||
306 | void wxGCDC::ComputeScaleAndOrigin() | |
307 | { | |
1b89a5cd SC |
308 | m_scaleX = m_logicalScaleX * m_userScaleX; |
309 | m_scaleY = m_logicalScaleY * m_userScaleY; | |
1b89a5cd | 310 | |
fd791571 SC |
311 | if ( m_graphicContext ) |
312 | { | |
313 | m_matrixCurrent = m_graphicContext->CreateMatrix(); | |
314 | m_matrixCurrent.Translate( m_deviceOriginX, m_deviceOriginY ); | |
315 | m_matrixCurrent.Scale( m_scaleX, m_scaleY ); | |
316 | // the logical origin sets the origin to have new coordinates | |
317 | m_matrixCurrent.Translate( -m_logicalOriginX, -m_logicalOriginY ); | |
318 | ||
319 | m_graphicContext->SetTransform( m_matrixOriginal ); | |
320 | m_graphicContext->ConcatTransform( m_matrixCurrent ); | |
321 | } | |
1b89a5cd SC |
322 | } |
323 | ||
324 | void wxGCDC::SetPalette( const wxPalette& WXUNUSED(palette) ) | |
325 | { | |
326 | ||
327 | } | |
328 | ||
329 | void wxGCDC::SetBackgroundMode( int mode ) | |
330 | { | |
331 | m_backgroundMode = mode; | |
332 | } | |
333 | ||
334 | void wxGCDC::SetFont( const wxFont &font ) | |
335 | { | |
336 | m_font = font; | |
337 | if ( m_graphicContext ) | |
338 | { | |
339 | wxFont f = font; | |
340 | if ( f.Ok() ) | |
0ebd9515 | 341 | f.SetPointSize( /*LogicalToDeviceYRel*/(font.GetPointSize())); |
1b89a5cd SC |
342 | m_graphicContext->SetFont( f, m_textForegroundColour ); |
343 | } | |
344 | } | |
345 | ||
346 | void wxGCDC::SetPen( const wxPen &pen ) | |
347 | { | |
348 | if ( m_pen == pen ) | |
349 | return; | |
350 | ||
351 | m_pen = pen; | |
352 | if ( m_graphicContext ) | |
353 | { | |
0ebd9515 | 354 | m_graphicContext->SetPen( m_pen ); |
1b89a5cd SC |
355 | } |
356 | } | |
357 | ||
358 | void wxGCDC::SetBrush( const wxBrush &brush ) | |
359 | { | |
360 | if (m_brush == brush) | |
361 | return; | |
362 | ||
363 | m_brush = brush; | |
364 | if ( m_graphicContext ) | |
365 | { | |
0ebd9515 | 366 | m_graphicContext->SetBrush( m_brush ); |
1b89a5cd SC |
367 | } |
368 | } | |
369 | ||
370 | void wxGCDC::SetBackground( const wxBrush &brush ) | |
371 | { | |
372 | if (m_backgroundBrush == brush) | |
373 | return; | |
374 | ||
375 | m_backgroundBrush = brush; | |
376 | if (!m_backgroundBrush.Ok()) | |
377 | return; | |
378 | } | |
379 | ||
380 | void wxGCDC::SetLogicalFunction( int function ) | |
381 | { | |
382 | if (m_logicalFunction == function) | |
383 | return; | |
384 | ||
385 | m_logicalFunction = function; | |
386 | #if wxMAC_USE_CORE_GRAPHICS_BLEND_MODES | |
387 | ||
388 | CGContextRef cgContext = ((wxCairoContext*)(m_graphicContext))->GetNativeContext(); | |
389 | if ( m_logicalFunction == wxCOPY ) | |
390 | CGContextSetBlendMode( cgContext, kCGBlendModeNormal ); | |
391 | else if ( m_logicalFunction == wxINVERT ) | |
392 | CGContextSetBlendMode( cgContext, kCGBlendModeExclusion ); | |
393 | else | |
394 | CGContextSetBlendMode( cgContext, kCGBlendModeNormal ); | |
395 | #endif | |
396 | ||
397 | } | |
398 | ||
399 | bool wxGCDC::DoFloodFill(wxCoord WXUNUSED(x), wxCoord WXUNUSED(y), | |
400 | const wxColour& WXUNUSED(col), int WXUNUSED(style)) | |
401 | { | |
402 | return false; | |
403 | } | |
404 | ||
405 | bool wxGCDC::DoGetPixel( wxCoord WXUNUSED(x), wxCoord WXUNUSED(y), wxColour *WXUNUSED(col) ) const | |
406 | { | |
407 | // wxCHECK_MSG( 0 , false, wxT("wxGCDC(cg)::DoGetPixel - not implemented") ); | |
408 | return false; | |
409 | } | |
410 | ||
411 | void wxGCDC::DoDrawLine( wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2 ) | |
412 | { | |
413 | wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawLine - invalid DC") ); | |
414 | ||
415 | #if !wxMAC_USE_CORE_GRAPHICS_BLEND_MODES | |
416 | ||
417 | if ( m_logicalFunction != wxCOPY ) | |
418 | return; | |
419 | #endif | |
420 | ||
0ebd9515 | 421 | m_graphicContext->StrokeLine(x1,y1,x2,y2); |
1b89a5cd SC |
422 | |
423 | CalcBoundingBox(x1, y1); | |
424 | CalcBoundingBox(x2, y2); | |
425 | } | |
426 | ||
427 | void wxGCDC::DoCrossHair( wxCoord x, wxCoord y ) | |
428 | { | |
429 | wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoCrossHair - invalid DC") ); | |
430 | ||
431 | if ( m_logicalFunction != wxCOPY ) | |
432 | return; | |
433 | ||
434 | int w = 0, h = 0; | |
435 | ||
436 | GetSize( &w, &h ); | |
437 | ||
0ebd9515 SC |
438 | m_graphicContext->StrokeLine(0,y,w,y); |
439 | m_graphicContext->StrokeLine(x,0,x,h); | |
1b89a5cd | 440 | |
0ebd9515 SC |
441 | CalcBoundingBox(0, 0); |
442 | CalcBoundingBox(0+w, 0+h); | |
1b89a5cd SC |
443 | } |
444 | ||
445 | void wxGCDC::DoDrawArc( wxCoord x1, wxCoord y1, | |
446 | wxCoord x2, wxCoord y2, | |
447 | wxCoord xc, wxCoord yc ) | |
448 | { | |
449 | wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawArc - invalid DC") ); | |
450 | ||
451 | if ( m_logicalFunction != wxCOPY ) | |
452 | return; | |
453 | ||
0ebd9515 SC |
454 | double dx = x1 - xc; |
455 | double dy = y1 - yc; | |
1b89a5cd SC |
456 | double radius = sqrt((double)(dx * dx + dy * dy)); |
457 | wxCoord rad = (wxCoord)radius; | |
458 | double sa, ea; | |
0ebd9515 | 459 | if (x1 == x2 && y1 == y2) |
1b89a5cd SC |
460 | { |
461 | sa = 0.0; | |
462 | ea = 360.0; | |
463 | } | |
464 | else if (radius == 0.0) | |
465 | { | |
466 | sa = ea = 0.0; | |
467 | } | |
468 | else | |
469 | { | |
0ebd9515 SC |
470 | sa = (x1 - xc == 0) ? |
471 | (y1 - yc < 0) ? 90.0 : -90.0 : | |
472 | -atan2(double(y1 - yc), double(x1 - xc)) * RAD2DEG; | |
473 | ea = (x2 - xc == 0) ? | |
474 | (y2 - yc < 0) ? 90.0 : -90.0 : | |
475 | -atan2(double(y2 - yc), double(x2 - xc)) * RAD2DEG; | |
1b89a5cd SC |
476 | } |
477 | ||
478 | bool fill = m_brush.GetStyle() != wxTRANSPARENT; | |
479 | ||
a4e73390 | 480 | wxGraphicsPath path = m_graphicContext->CreatePath(); |
1b89a5cd | 481 | if ( fill && ((x1!=x2)||(y1!=y2)) ) |
0ebd9515 SC |
482 | path.MoveToPoint( xc, yc ); |
483 | path.AddArc( xc, yc , rad , DegToRad(sa) , DegToRad(ea), false ); | |
1b89a5cd | 484 | if ( fill && ((x1!=x2)||(y1!=y2)) ) |
0ebd9515 | 485 | path.AddLineToPoint( xc, yc ); |
1b89a5cd | 486 | m_graphicContext->DrawPath(path); |
1b89a5cd SC |
487 | } |
488 | ||
489 | void wxGCDC::DoDrawEllipticArc( wxCoord x, wxCoord y, wxCoord w, wxCoord h, | |
490 | double sa, double ea ) | |
491 | { | |
492 | wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawEllipticArc - invalid DC") ); | |
493 | ||
494 | if ( m_logicalFunction != wxCOPY ) | |
495 | return; | |
496 | ||
1b89a5cd SC |
497 | bool fill = m_brush.GetStyle() != wxTRANSPARENT; |
498 | ||
a4e73390 | 499 | wxGraphicsPath path = m_graphicContext->CreatePath(); |
1b89a5cd | 500 | m_graphicContext->PushState(); |
0ebd9515 SC |
501 | m_graphicContext->Translate(x+w/2,y+h/2); |
502 | wxDouble factor = ((wxDouble) w) / h; | |
1b89a5cd SC |
503 | m_graphicContext->Scale( factor , 1.0); |
504 | if ( fill && (sa!=ea) ) | |
a4e73390 | 505 | path.MoveToPoint(0,0); |
1b89a5cd SC |
506 | // since these angles (ea,sa) are measured counter-clockwise, we invert them to |
507 | // get clockwise angles | |
0ebd9515 | 508 | path.AddArc( 0, 0, h/2 , DegToRad(-sa) , DegToRad(-ea), sa > ea ); |
1b89a5cd | 509 | if ( fill && (sa!=ea) ) |
a4e73390 | 510 | path.AddLineToPoint(0,0); |
1b89a5cd SC |
511 | m_graphicContext->DrawPath( path ); |
512 | m_graphicContext->PopState(); | |
1b89a5cd SC |
513 | } |
514 | ||
515 | void wxGCDC::DoDrawPoint( wxCoord x, wxCoord y ) | |
516 | { | |
517 | wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawPoint - invalid DC") ); | |
518 | ||
519 | DoDrawLine( x , y , x + 1 , y + 1 ); | |
520 | } | |
521 | ||
522 | void wxGCDC::DoDrawLines(int n, wxPoint points[], | |
523 | wxCoord xoffset, wxCoord yoffset) | |
524 | { | |
525 | wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawLines - invalid DC") ); | |
526 | ||
527 | #if !wxMAC_USE_CORE_GRAPHICS_BLEND_MODES | |
528 | ||
529 | if ( m_logicalFunction != wxCOPY ) | |
530 | return; | |
531 | #endif | |
532 | ||
533 | wxPoint2DDouble* pointsD = new wxPoint2DDouble[n]; | |
534 | for( int i = 0; i < n; ++i) | |
535 | { | |
0ebd9515 SC |
536 | pointsD[i].m_x = points[i].x + xoffset; |
537 | pointsD[i].m_y = points[i].y + yoffset; | |
1b89a5cd SC |
538 | } |
539 | ||
540 | m_graphicContext->StrokeLines( n , pointsD); | |
541 | delete[] pointsD; | |
542 | } | |
543 | ||
544 | #if wxUSE_SPLINES | |
545 | void wxGCDC::DoDrawSpline(wxList *points) | |
546 | { | |
547 | wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawSpline - invalid DC") ); | |
548 | ||
549 | if ( m_logicalFunction != wxCOPY ) | |
550 | return; | |
551 | ||
a4e73390 | 552 | wxGraphicsPath path = m_graphicContext->CreatePath(); |
1b89a5cd SC |
553 | |
554 | wxList::compatibility_iterator node = points->GetFirst(); | |
555 | if (node == wxList::compatibility_iterator()) | |
556 | // empty list | |
557 | return; | |
558 | ||
559 | wxPoint *p = (wxPoint *)node->GetData(); | |
560 | ||
561 | wxCoord x1 = p->x; | |
562 | wxCoord y1 = p->y; | |
563 | ||
564 | node = node->GetNext(); | |
565 | p = (wxPoint *)node->GetData(); | |
566 | ||
567 | wxCoord x2 = p->x; | |
568 | wxCoord y2 = p->y; | |
569 | wxCoord cx1 = ( x1 + x2 ) / 2; | |
570 | wxCoord cy1 = ( y1 + y2 ) / 2; | |
571 | ||
0ebd9515 SC |
572 | path.MoveToPoint( x1 , y1 ); |
573 | path.AddLineToPoint( cx1 , cy1 ); | |
1b89a5cd SC |
574 | #if !wxUSE_STL |
575 | ||
576 | while ((node = node->GetNext()) != NULL) | |
577 | #else | |
578 | ||
579 | while ((node = node->GetNext())) | |
580 | #endif // !wxUSE_STL | |
581 | ||
582 | { | |
583 | p = (wxPoint *)node->GetData(); | |
584 | x1 = x2; | |
585 | y1 = y2; | |
586 | x2 = p->x; | |
587 | y2 = p->y; | |
588 | wxCoord cx4 = (x1 + x2) / 2; | |
589 | wxCoord cy4 = (y1 + y2) / 2; | |
590 | ||
0ebd9515 | 591 | path.AddQuadCurveToPoint(x1 , y1 ,cx4 , cy4 ); |
1b89a5cd SC |
592 | |
593 | cx1 = cx4; | |
594 | cy1 = cy4; | |
595 | } | |
596 | ||
0ebd9515 | 597 | path.AddLineToPoint( x2 , y2 ); |
1b89a5cd SC |
598 | |
599 | m_graphicContext->StrokePath( path ); | |
1b89a5cd SC |
600 | } |
601 | #endif // wxUSE_SPLINES | |
602 | ||
603 | void wxGCDC::DoDrawPolygon( int n, wxPoint points[], | |
604 | wxCoord xoffset, wxCoord yoffset, | |
605 | int fillStyle ) | |
606 | { | |
607 | wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawPolygon - invalid DC") ); | |
608 | ||
609 | if ( n <= 0 || (m_brush.GetStyle() == wxTRANSPARENT && m_pen.GetStyle() == wxTRANSPARENT ) ) | |
610 | return; | |
611 | if ( m_logicalFunction != wxCOPY ) | |
612 | return; | |
613 | ||
614 | bool closeIt = false; | |
615 | if (points[n-1] != points[0]) | |
616 | closeIt = true; | |
617 | ||
618 | wxPoint2DDouble* pointsD = new wxPoint2DDouble[n+(closeIt?1:0)]; | |
619 | for( int i = 0; i < n; ++i) | |
620 | { | |
0ebd9515 SC |
621 | pointsD[i].m_x = points[i].x + xoffset; |
622 | pointsD[i].m_y = points[i].y + yoffset; | |
1b89a5cd SC |
623 | } |
624 | if ( closeIt ) | |
625 | pointsD[n] = pointsD[0]; | |
626 | ||
627 | m_graphicContext->DrawLines( n+(closeIt?1:0) , pointsD, fillStyle); | |
628 | delete[] pointsD; | |
629 | } | |
630 | ||
631 | void wxGCDC::DoDrawPolyPolygon(int n, | |
632 | int count[], | |
633 | wxPoint points[], | |
634 | wxCoord xoffset, | |
635 | wxCoord yoffset, | |
636 | int fillStyle) | |
637 | { | |
638 | wxASSERT(n > 1); | |
a4e73390 | 639 | wxGraphicsPath path = m_graphicContext->CreatePath(); |
1b89a5cd SC |
640 | |
641 | int i = 0; | |
642 | for ( int j = 0; j < n; ++j) | |
643 | { | |
644 | wxPoint start = points[i]; | |
0ebd9515 | 645 | path.MoveToPoint( start.x+ xoffset, start.y+ yoffset); |
1b89a5cd SC |
646 | ++i; |
647 | int l = count[j]; | |
648 | for ( int k = 1; k < l; ++k) | |
649 | { | |
0ebd9515 | 650 | path.AddLineToPoint( points[i].x+ xoffset, points[i].y+ yoffset); |
1b89a5cd SC |
651 | ++i; |
652 | } | |
653 | // close the polygon | |
654 | if ( start != points[i-1]) | |
0ebd9515 | 655 | path.AddLineToPoint( start.x+ xoffset, start.y+ yoffset); |
1b89a5cd SC |
656 | } |
657 | m_graphicContext->DrawPath( path , fillStyle); | |
1b89a5cd SC |
658 | } |
659 | ||
0ebd9515 | 660 | void wxGCDC::DoDrawRectangle(wxCoord x, wxCoord y, wxCoord w, wxCoord h) |
1b89a5cd SC |
661 | { |
662 | wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawRectangle - invalid DC") ); | |
663 | ||
664 | if ( m_logicalFunction != wxCOPY ) | |
665 | return; | |
666 | ||
1b89a5cd | 667 | // CMB: draw nothing if transformed w or h is 0 |
0ebd9515 | 668 | if (w == 0 || h == 0) |
1b89a5cd SC |
669 | return; |
670 | ||
5ebfdf41 SC |
671 | if ( m_graphicContext->ShouldOffset() ) |
672 | { | |
673 | // if we are offsetting the entire rectangle is moved 0.5, so the | |
674 | // border line gets off by 1 | |
0ebd9515 SC |
675 | w -= 1; |
676 | h -= 1; | |
5ebfdf41 | 677 | } |
0ebd9515 | 678 | m_graphicContext->DrawRectangle(x,y,w,h); |
1b89a5cd SC |
679 | } |
680 | ||
681 | void wxGCDC::DoDrawRoundedRectangle(wxCoord x, wxCoord y, | |
0ebd9515 | 682 | wxCoord w, wxCoord h, |
1b89a5cd SC |
683 | double radius) |
684 | { | |
685 | wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawRoundedRectangle - invalid DC") ); | |
686 | ||
687 | if ( m_logicalFunction != wxCOPY ) | |
688 | return; | |
689 | ||
690 | if (radius < 0.0) | |
0ebd9515 | 691 | radius = - radius * ((w < h) ? w : h); |
1b89a5cd SC |
692 | |
693 | // CMB: draw nothing if transformed w or h is 0 | |
0ebd9515 | 694 | if (w == 0 || h == 0) |
1b89a5cd SC |
695 | return; |
696 | ||
0ebd9515 | 697 | m_graphicContext->DrawRoundedRectangle( x,y,w,h,radius); |
1b89a5cd SC |
698 | } |
699 | ||
0ebd9515 | 700 | void wxGCDC::DoDrawEllipse(wxCoord x, wxCoord y, wxCoord w, wxCoord h) |
1b89a5cd SC |
701 | { |
702 | wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawEllipse - invalid DC") ); | |
703 | ||
704 | if ( m_logicalFunction != wxCOPY ) | |
705 | return; | |
706 | ||
0ebd9515 | 707 | m_graphicContext->DrawEllipse(x,y,w,h); |
1b89a5cd SC |
708 | } |
709 | ||
710 | bool wxGCDC::CanDrawBitmap() const | |
711 | { | |
712 | return true; | |
713 | } | |
714 | ||
715 | bool wxGCDC::DoBlit( | |
716 | wxCoord xdest, wxCoord ydest, wxCoord width, wxCoord height, | |
717 | wxDC *source, wxCoord xsrc, wxCoord ysrc, int logical_func , bool WXUNUSED(useMask), | |
718 | wxCoord xsrcMask, wxCoord ysrcMask ) | |
719 | { | |
720 | wxCHECK_MSG( Ok(), false, wxT("wxGCDC(cg)::DoBlit - invalid DC") ); | |
721 | wxCHECK_MSG( source->Ok(), false, wxT("wxGCDC(cg)::DoBlit - invalid source DC") ); | |
6c0aace2 | 722 | |
1b89a5cd SC |
723 | if ( logical_func == wxNO_OP ) |
724 | return true; | |
c64c9cd3 KO |
725 | else if ( logical_func != wxCOPY ) |
726 | { | |
727 | wxFAIL_MSG( wxT("Blitting is only supported with wxCOPY logical operation.") ); | |
728 | return false; | |
729 | } | |
1b89a5cd SC |
730 | |
731 | if (xsrcMask == -1 && ysrcMask == -1) | |
732 | { | |
733 | xsrcMask = xsrc; | |
734 | ysrcMask = ysrc; | |
735 | } | |
736 | ||
64c8307c SC |
737 | wxRect subrect(source-> LogicalToDeviceX(xsrc),source-> LogicalToDeviceY(ysrc), |
738 | source-> LogicalToDeviceXRel(width),source-> LogicalToDeviceYRel(height)); | |
6c0aace2 | 739 | |
64c8307c | 740 | wxBitmap blit = source->GetAsBitmap( &subrect ); |
6c0aace2 | 741 | |
c64c9cd3 KO |
742 | if ( blit.Ok() ) |
743 | { | |
0ebd9515 | 744 | m_graphicContext->DrawBitmap( blit, xdest , ydest , width , height ); |
c64c9cd3 | 745 | } |
1b89a5cd SC |
746 | else |
747 | { | |
c64c9cd3 | 748 | wxFAIL_MSG( wxT("Cannot Blit. Unable to get contents of DC as bitmap.") ); |
1b89a5cd SC |
749 | return false; |
750 | } | |
751 | ||
752 | return true; | |
753 | } | |
754 | ||
755 | void wxGCDC::DoDrawRotatedText(const wxString& str, wxCoord x, wxCoord y, | |
756 | double angle) | |
757 | { | |
758 | wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawRotatedText - invalid DC") ); | |
759 | ||
760 | if ( str.length() == 0 ) | |
761 | return; | |
762 | if ( m_logicalFunction != wxCOPY ) | |
763 | return; | |
764 | ||
068eb463 SC |
765 | if ( m_backgroundMode == wxTRANSPARENT ) |
766 | m_graphicContext->DrawText( str, x ,y , DegToRad(angle )); | |
767 | else | |
768 | m_graphicContext->DrawText( str, x ,y , DegToRad(angle ), m_graphicContext->CreateBrush( wxBrush(m_textBackgroundColour,wxSOLID) ) ); | |
1b89a5cd SC |
769 | } |
770 | ||
771 | void wxGCDC::DoDrawText(const wxString& str, wxCoord x, wxCoord y) | |
772 | { | |
773 | wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawRotatedText - invalid DC") ); | |
774 | ||
775 | if ( str.length() == 0 ) | |
776 | return; | |
777 | if ( m_logicalFunction != wxCOPY ) | |
778 | return; | |
779 | ||
068eb463 SC |
780 | if ( m_backgroundMode == wxTRANSPARENT ) |
781 | m_graphicContext->DrawText( str, x ,y); | |
782 | else | |
783 | m_graphicContext->DrawText( str, x ,y , m_graphicContext->CreateBrush( wxBrush(m_textBackgroundColour,wxSOLID) ) ); | |
1b89a5cd SC |
784 | } |
785 | ||
786 | bool wxGCDC::CanGetTextExtent() const | |
787 | { | |
788 | wxCHECK_MSG( Ok(), false, wxT("wxGCDC(cg)::CanGetTextExtent - invalid DC") ); | |
789 | ||
790 | return true; | |
791 | } | |
792 | ||
793 | void wxGCDC::DoGetTextExtent( const wxString &str, wxCoord *width, wxCoord *height, | |
794 | wxCoord *descent, wxCoord *externalLeading , | |
795 | wxFont *theFont ) const | |
796 | { | |
797 | wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoGetTextExtent - invalid DC") ); | |
798 | ||
799 | if ( theFont ) | |
800 | { | |
801 | m_graphicContext->SetFont( *theFont, m_textForegroundColour ); | |
802 | } | |
803 | ||
804 | wxDouble h , d , e , w; | |
805 | ||
806 | m_graphicContext->GetTextExtent( str, &w, &h, &d, &e ); | |
807 | ||
808 | if ( height ) | |
8a438f46 | 809 | *height = (wxCoord)h; |
1b89a5cd | 810 | if ( descent ) |
8a438f46 | 811 | *descent = (wxCoord)d; |
1b89a5cd | 812 | if ( externalLeading ) |
8a438f46 | 813 | *externalLeading = (wxCoord)e; |
1b89a5cd | 814 | if ( width ) |
8a438f46 | 815 | *width = (wxCoord)w; |
1b89a5cd SC |
816 | |
817 | if ( theFont ) | |
818 | { | |
819 | m_graphicContext->SetFont( m_font, m_textForegroundColour ); | |
820 | } | |
821 | } | |
822 | ||
823 | bool wxGCDC::DoGetPartialTextExtents(const wxString& text, wxArrayInt& widths) const | |
824 | { | |
825 | wxCHECK_MSG( Ok(), false, wxT("wxGCDC(cg)::DoGetPartialTextExtents - invalid DC") ); | |
826 | widths.Clear(); | |
827 | widths.Add(0,text.Length()); | |
828 | if ( text.IsEmpty() ) | |
829 | return true; | |
830 | ||
831 | wxArrayDouble widthsD; | |
832 | ||
833 | m_graphicContext->GetPartialTextExtents( text, widthsD ); | |
834 | for ( size_t i = 0; i < widths.GetCount(); ++i ) | |
0ebd9515 | 835 | widths[i] = (wxCoord)(widthsD[i] + 0.5); |
1b89a5cd SC |
836 | |
837 | return true; | |
838 | } | |
839 | ||
840 | wxCoord wxGCDC::GetCharWidth(void) const | |
841 | { | |
842 | wxCoord width; | |
843 | DoGetTextExtent( wxT("g") , &width , NULL , NULL , NULL , NULL ); | |
844 | ||
845 | return width; | |
846 | } | |
847 | ||
848 | wxCoord wxGCDC::GetCharHeight(void) const | |
849 | { | |
850 | wxCoord height; | |
851 | DoGetTextExtent( wxT("g") , NULL , &height , NULL , NULL , NULL ); | |
852 | ||
853 | return height; | |
854 | } | |
855 | ||
856 | void wxGCDC::Clear(void) | |
857 | { | |
858 | wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::Clear - invalid DC") ); | |
859 | // TODO better implementation / incorporate size info into wxGCDC or context | |
860 | m_graphicContext->SetBrush( m_backgroundBrush ); | |
861 | wxPen p = *wxTRANSPARENT_PEN; | |
862 | m_graphicContext->SetPen( p ); | |
863 | DoDrawRectangle( 0, 0, 32000 , 32000 ); | |
6c0aace2 | 864 | m_graphicContext->SetPen( m_pen ); |
1b89a5cd SC |
865 | m_graphicContext->SetBrush( m_brush ); |
866 | } | |
867 | ||
868 | void wxGCDC::DoGetSize(int *width, int *height) const | |
869 | { | |
870 | *width = 1000; | |
871 | *height = 1000; | |
872 | } | |
873 | ||
874 | void wxGCDC::DoGradientFillLinear(const wxRect& rect, | |
875 | const wxColour& initialColour, | |
876 | const wxColour& destColour, | |
877 | wxDirection nDirection ) | |
878 | { | |
879 | wxPoint start; | |
880 | wxPoint end; | |
881 | switch( nDirection) | |
882 | { | |
883 | case wxWEST : | |
884 | start = rect.GetRightBottom(); | |
885 | start.x++; | |
886 | end = rect.GetLeftBottom(); | |
887 | break; | |
888 | case wxEAST : | |
889 | start = rect.GetLeftBottom(); | |
890 | end = rect.GetRightBottom(); | |
891 | end.x++; | |
892 | break; | |
893 | case wxNORTH : | |
894 | start = rect.GetLeftBottom(); | |
895 | start.y++; | |
896 | end = rect.GetLeftTop(); | |
897 | break; | |
898 | case wxSOUTH : | |
899 | start = rect.GetLeftTop(); | |
900 | end = rect.GetLeftBottom(); | |
901 | end.y++; | |
902 | break; | |
903 | default : | |
904 | break; | |
905 | } | |
906 | ||
0ebd9515 | 907 | if (rect.width == 0 || rect.height == 0) |
1b89a5cd SC |
908 | return; |
909 | ||
0ebd9515 SC |
910 | m_graphicContext->SetBrush( m_graphicContext->CreateLinearGradientBrush( |
911 | start.x,start.y,end.x,end.y, initialColour, destColour)); | |
1b89a5cd | 912 | m_graphicContext->SetPen(*wxTRANSPARENT_PEN); |
0ebd9515 | 913 | m_graphicContext->DrawRectangle(rect.x,rect.y,rect.width,rect.height); |
1b89a5cd SC |
914 | m_graphicContext->SetPen(m_pen); |
915 | } | |
916 | ||
917 | void wxGCDC::DoGradientFillConcentric(const wxRect& rect, | |
918 | const wxColour& initialColour, | |
919 | const wxColour& destColour, | |
920 | const wxPoint& circleCenter) | |
921 | { | |
922 | //Radius | |
923 | wxInt32 cx = rect.GetWidth() / 2; | |
924 | wxInt32 cy = rect.GetHeight() / 2; | |
925 | wxInt32 nRadius; | |
926 | if (cx < cy) | |
927 | nRadius = cx; | |
928 | else | |
929 | nRadius = cy; | |
930 | ||
0ebd9515 | 931 | // make sure the background is filled (todo move into specific platform implementation ?) |
1b89a5cd SC |
932 | m_graphicContext->SetPen(*wxTRANSPARENT_PEN); |
933 | m_graphicContext->SetBrush( wxBrush( destColour) ); | |
0ebd9515 | 934 | m_graphicContext->DrawRectangle(rect.x,rect.y,rect.width,rect.height); |
1b89a5cd SC |
935 | |
936 | m_graphicContext->SetBrush( m_graphicContext->CreateRadialGradientBrush( | |
0ebd9515 SC |
937 | rect.x+circleCenter.x,rect.y+circleCenter.y, |
938 | rect.x+circleCenter.x,rect.y+circleCenter.y, | |
939 | nRadius,initialColour,destColour)); | |
1b89a5cd | 940 | |
0ebd9515 | 941 | m_graphicContext->DrawRectangle(rect.x,rect.y,rect.width,rect.height); |
1b89a5cd SC |
942 | m_graphicContext->SetPen(m_pen); |
943 | } | |
944 | ||
945 | void wxGCDC::DoDrawCheckMark(wxCoord x, wxCoord y, | |
946 | wxCoord width, wxCoord height) | |
947 | { | |
948 | wxDCBase::DoDrawCheckMark(x,y,width,height); | |
949 | } | |
950 | ||
951 | #endif // wxUSE_GRAPHICS_CONTEXT |