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