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