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