1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Wrapper definitions for wx.GraphicsPath, wx.GraphicsContext
9 // Copyright: (c) 2006 by Total Control Software
10 // Licence: wxWindows license
11 /////////////////////////////////////////////////////////////////////////////
16 //---------------------------------------------------------------------------
20 #include <wx/graphics.h>
23 // Turn off the aquisition of the Global Interpreter Lock for the classes and
24 // functions in this file
27 //---------------------------------------------------------------------------
31 #if !wxUSE_GRAPHICS_CONTEXT
32 // C++ stub classes for platforms that don't have wxGraphicsContext yet.
34 class wxGraphicsObject : public wxObject
37 wxGraphicsObject( wxGraphicsRenderer* renderer ) {}
38 wxGraphicsObject( const wxGraphicsObject& obj ) {}
39 virtual ~wxGraphicsObject() {}
40 wxGraphicsRenderer* GetRenderer() const {}
43 class wxGraphicsPen : public wxGraphicsObject
45 //wxGraphicsPen(wxGraphicsRenderer* renderer) : wxGraphicsObject(renderer) {}
46 virtual ~wxGraphicsPen() {}
47 virtual void Apply( wxGraphicsContext* context) {}
48 virtual wxDouble GetWidth() {}
51 class wxGraphicsBrush : public wxGraphicsObject
54 //wxGraphicsBrush(wxGraphicsRenderer* renderer) {}
55 virtual ~wxGraphicsBrush() {}
56 virtual void Apply( wxGraphicsContext* context) {}
59 class wxGraphicsFont : public wxGraphicsObject
62 //wxGraphicsFont(wxGraphicsRenderer* renderer) {}
63 virtual ~wxGraphicsFont() {}
64 virtual void Apply( wxGraphicsContext* context) {}
71 wxPyBlock_t blocked = wxPyBeginBlockThreads();
72 PyErr_SetString(PyExc_NotImplementedError,
73 "wxGraphicsPath is not available on this platform.");
74 wxPyEndBlockThreads(blocked);
76 virtual ~wxGraphicsPath() {}
78 void MoveToPoint( wxDouble, wxDouble ) {}
79 void AddLineToPoint( wxDouble, wxDouble ) {}
80 void AddCurveToPoint( wxDouble, wxDouble, wxDouble, wxDouble, wxDouble, wxDouble ) {}
81 void CloseSubpath() {}
82 void GetCurrentPoint( wxDouble&, wxDouble&) {}
83 void AddArc( wxDouble, wxDouble, wxDouble, wxDouble, wxDouble, bool ) {}
85 void AddQuadCurveToPoint( wxDouble, wxDouble, wxDouble, wxDouble ) {}
86 void AddRectangle( wxDouble, wxDouble, wxDouble, wxDouble ) {}
87 void AddCircle( wxDouble, wxDouble, wxDouble ) {}
88 void AddArcToPoint( wxDouble, wxDouble , wxDouble, wxDouble, wxDouble ) {}
90 wxPoint2DDouble GetCurrentPoint() { return wxPoint2DDouble(0,0); }
91 void MoveToPoint( const wxPoint2DDouble& ) {}
92 void AddLineToPoint( const wxPoint2DDouble&) {}
93 void AddCurveToPoint( const wxPoint2DDouble&, const wxPoint2DDouble&, const wxPoint2DDouble&) {}
94 void AddArc( const wxPoint2DDouble&, wxDouble, wxDouble, wxDouble, bool) {}
98 class wxGraphicsContext
101 wxGraphicsContext() {
102 wxPyBlock_t blocked = wxPyBeginBlockThreads();
103 PyErr_SetString(PyExc_NotImplementedError,
104 "wxGraphicsContext is not available on this platform.");
105 wxPyEndBlockThreads(blocked);
107 virtual ~wxGraphicsContext() {}
109 static wxGraphicsContext* Create( const wxWindowDC&) {
110 wxPyBlock_t blocked = wxPyBeginBlockThreads();
111 PyErr_SetString(PyExc_NotImplementedError,
112 "wxGraphicsPath is not available on this platform.");
113 wxPyEndBlockThreads(blocked);
117 static wxGraphicsContext* CreateFromNative( void * ) {
118 wxPyBlock_t blocked = wxPyBeginBlockThreads();
119 PyErr_SetString(PyExc_NotImplementedError,
120 "wxGraphicsContext is not available on this platform.");
121 wxPyEndBlockThreads(blocked);
125 virtual wxGraphicsPen* CreatePen(const wxPen& pen) {}
127 virtual wxGraphicsBrush* CreateBrush(const wxBrush& brush ) {}
129 virtual wxGraphicsBrush* CreateLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2,
130 const wxColour&c1, const wxColour&c2) {}
132 virtual wxGraphicsBrush* CreateRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
133 const wxColour &oColor, const wxColour &cColor) {}
135 virtual wxGraphicsFont* CreateFont( const wxFont &font , const wxColour &col = *wxBLACK ) {}
138 wxGraphicsPath * CreatePath() { return NULL; }
141 void Clip( const wxRegion & ) {}
142 void Clip( wxDouble, wxDouble, wxDouble, wxDouble ) {}
144 void * GetNativeContext() { return NULL; }
145 void Translate( wxDouble , wxDouble ) {}
146 void Scale( wxDouble , wxDouble ) {}
147 void Rotate( wxDouble ) {}
148 void SetFont( const wxFont &, bool ) {}
149 void SetFont( const wxFont &, const wxColour &) {}
150 void StrokePath( const wxGraphicsPath * ) {}
151 void FillPath( const wxGraphicsPath *, int ) {}
152 void DrawPath( const wxGraphicsPath *, int ) {}
153 void DrawText( const wxString &, wxDouble , wxDouble ) {}
154 void DrawText( const wxString &, wxDouble , wxDouble , wxDouble ) {}
155 void GetTextExtent( const wxString &, wxDouble *, wxDouble *,
156 wxDouble *, wxDouble * ) const {}
157 void GetPartialTextExtents(const wxString& , wxArrayDouble& ) const {}
158 void DrawBitmap( const wxBitmap &, wxDouble , wxDouble , wxDouble , wxDouble ) {}
159 void DrawIcon( const wxIcon &, wxDouble , wxDouble , wxDouble , wxDouble ) {}
160 void StrokeLine( wxDouble , wxDouble , wxDouble , wxDouble ) {}
161 void StrokeLines( size_t , const wxPoint2DDouble *) {}
162 void StrokeLines( size_t , const wxPoint2DDouble *, const wxPoint2DDouble *) {}
163 void DrawLines( size_t , const wxPoint2DDouble *, int ) {}
164 void DrawRectangle( wxDouble , wxDouble , wxDouble , wxDouble ) {}
165 void DrawEllipse( wxDouble , wxDouble, wxDouble , wxDouble) {}
166 void DrawRoundedRectangle( wxDouble , wxDouble , wxDouble , wxDouble , wxDouble ) {}
170 class wxGCDC: public wxWindowDC
173 wxGCDC(const wxWindowDC&) {
174 wxPyBlock_t blocked = wxPyBeginBlockThreads();
175 PyErr_SetString(PyExc_NotImplementedError,
176 "wxGCDC is not available on this platform.");
177 wxPyEndBlockThreads(blocked);
181 wxPyBlock_t blocked = wxPyBeginBlockThreads();
182 PyErr_SetString(PyExc_NotImplementedError,
183 "wxGCDC is not available on this platform.");
184 wxPyEndBlockThreads(blocked);
189 wxGraphicsContext* GetGraphicsContext() { return NULL; }
190 void SetGraphicsContext( wxGraphicsContext* ) {}
196 //---------------------------------------------------------------------------
197 //---------------------------------------------------------------------------
200 %typemap(in) (size_t points, wxPoint2D* points_array ) {
201 $2 = wxPoint2D_LIST_helper($input, &$1);
202 if ($2 == NULL) SWIG_fail;
204 %typemap(freearg) (size_t points, wxPoint2D* points_array ) {
205 if ($2) delete [] $2;
210 MustHaveApp(wxGraphicsPath);
211 MustHaveApp(wxGraphicsContext);
214 //#define wxDouble double
215 typedef double wxDouble;
218 // TODO: Decide which of the overloaded methods should use the primary names
220 class wxGraphicsObject : public wxObject
223 wxGraphicsObject( wxGraphicsRenderer* renderer = NULL );
224 wxGraphicsObject( const wxGraphicsObject& obj );
225 virtual ~wxGraphicsObject();
226 wxGraphicsRenderer* GetRenderer() const;
229 class wxGraphicsPen : public wxGraphicsObject
232 //wxGraphicsPen(wxGraphicsRenderer* renderer);
233 virtual ~wxGraphicsPen();
234 virtual void Apply( wxGraphicsContext* context);
235 virtual wxDouble GetWidth();
238 class wxGraphicsBrush : public wxGraphicsObject
241 //wxGraphicsBrush(wxGraphicsRenderer* renderer);
242 virtual ~wxGraphicsBrush();
243 virtual void Apply( wxGraphicsContext* context);
246 class wxGraphicsFont : public wxGraphicsObject
249 //wxGraphicsFont(wxGraphicsRenderer* renderer);
250 virtual ~wxGraphicsFont();
251 virtual void Apply( wxGraphicsContext* context);
257 //wxGraphicsPath(); *** This class is an ABC, so we can't allow instances to be created directly
258 virtual ~wxGraphicsPath();
262 virtual void , MoveToPoint( wxDouble x, wxDouble y ),
263 "Begins a new subpath at (x,y)", "");
264 // void MoveToPoint( const wxPoint2D& p);
268 virtual void , AddLineToPoint( wxDouble x, wxDouble y ),
269 "Adds a straight line from the current point to (x,y) ", "");
270 // void AddLineToPoint( const wxPoint2D& p);
274 virtual void , AddCurveToPoint( wxDouble cx1, wxDouble cy1,
275 wxDouble cx2, wxDouble cy2,
276 wxDouble x, wxDouble y ),
277 "Adds a cubic Bezier curve from the current point, using two control
278 points and an end point", "");
279 // void AddCurveToPoint( const wxPoint2D& c1, const wxPoint2D& c2, const wxPoint2D& e);
283 virtual void , CloseSubpath(),
284 "closes the current sub-path", "");
287 //virtual void , GetCurrentPoint( wxDouble& x, wxDouble&y),
289 wxPoint2D , GetCurrentPoint(),
290 "Gets the last point of the current path, (0,0) if not yet set", "");
294 virtual void , AddArc( wxDouble x, wxDouble y, wxDouble r,
295 wxDouble startAngle, wxDouble endAngle, bool clockwise ),
296 "Adds an arc of a circle centering at (x,y) with radius (r) from
297 startAngle to endAngle", "");
298 // void AddArc( const wxPoint2D& c, wxDouble r, wxDouble startAngle, wxDouble endAngle, bool clockwise);
302 virtual void , AddQuadCurveToPoint( wxDouble cx, wxDouble cy, wxDouble x, wxDouble y ),
303 "Adds a quadratic Bezier curve from the current point, using a control
304 point and an end point", "");
308 virtual void , AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h ),
309 "Appends a rectangle as a new closed subpath", "");
313 virtual void , AddCircle( wxDouble x, wxDouble y, wxDouble r ),
314 "Appends a circle as a new closed subpath with the given radius.", "");
318 virtual void , AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r ) ,
319 "Draws a an arc to two tangents connecting (current) to (x1,y1) and (x1,y1)
320 to (x2,y2), also a straight line from (current) to (x1,y1)", "");
325 //---------------------------------------------------------------------------
328 class wxGraphicsMatrix
331 wxGraphicsMatrix() {}
333 virtual ~wxGraphicsMatrix() {}
335 wxGraphicsMatrix* Concat( const wxGraphicsMatrix *t ) const;
337 // returns the inverse matrix
338 wxGraphicsMatrix* Invert() const;
340 // returns true if the elements of the transformation matrix are equal ?
341 bool operator==(const wxGraphicsMatrix& t) const;
343 // return true if this is the identity matrix
351 virtual void Translate( wxDouble dx , wxDouble dy ) = 0;
354 virtual void Scale( wxDouble xScale , wxDouble yScale ) = 0;
357 virtual void Rotate( wxDouble angle ) = 0;
362 //---------------------------------------------------------------------------
365 class wxGraphicsContext
368 // wxGraphicsContext() This is also an ABC, use Create to make an instance...
369 virtual ~wxGraphicsContext();
374 "val.__dc = args[0] # save a ref so the dc will not be deleted before self";
375 static wxGraphicsContext* Create( const wxWindowDC& dc);
377 static wxGraphicsContext* Create( wxWindow* window ) ;
379 %newobject CreateFromNative;
380 static wxGraphicsContext* CreateFromNative( void * context ) ;
383 %newobject CreatePath;
384 // creates a path instance that corresponds to the type of graphics context, ie GDIPlus, cairo, CoreGraphics ...
386 virtual wxGraphicsPath * , CreatePath(),
392 virtual wxGraphicsPen* CreatePen(const wxPen& pen);
394 %newobject CreateBrush
395 virtual wxGraphicsBrush* CreateBrush(const wxBrush& brush );
397 %newobject CreateLinearGradientBrush
398 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
399 virtual wxGraphicsBrush* CreateLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2,
400 const wxColour&c1, const wxColour&c2);
402 %newobject CreateRadialGradientBrush
403 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
404 // with radius r and color cColor
405 virtual wxGraphicsBrush* CreateRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
406 const wxColour &oColor, const wxColour &cColor);
408 %newobject CreateFont
410 virtual wxGraphicsFont* CreateFont( const wxFont &font , const wxColour &col = *wxBLACK );
412 // create a 'native' matrix corresponding to these values
413 virtual wxGraphicsMatrix* CreateMatrix( wxDouble a=1.0, wxDouble b=0.0,
414 wxDouble c=0.0, wxDouble d=1.0,
415 wxDouble tx=0.0, wxDouble ty=0.0) = 0;
419 // push the current state of the context, ie the transformation matrix on a stack
421 virtual void , PushState(),
425 // pops a stored state from the stack
427 virtual void , PopState(),
431 // clips drawings to the region
433 virtual void , Clip( const wxRegion ®ion ),
437 // clips drawings to the rect
439 virtual void , Clip( wxDouble x, wxDouble y, wxDouble w, wxDouble h ),
443 // resets the clipping to original extent
445 virtual void , ResetClip(),
449 // returns the native context
451 virtual void * , GetNativeContext(),
457 // transformation: changes the current transformation matrix CTM of the context
462 virtual void , Translate( wxDouble dx , wxDouble dy ),
468 virtual void , Scale( wxDouble xScale , wxDouble yScale ),
474 virtual void , Rotate( wxDouble angle ),
484 virtual void , SetPen( const wxPen &pen ),
488 // sets the brush for filling
490 virtual void , SetBrush( const wxBrush &brush ),
496 virtual void , SetFont( const wxFont &font, bool release = true ),
502 void , SetFont( const wxFont &font, const wxColour& colour ),
507 // strokes along a path with the current pen
509 virtual void , StrokePath( const wxGraphicsPath *path ),
513 // fills a path with the current brush
515 virtual void , FillPath( const wxGraphicsPath *path, int fillStyle = wxWINDING_RULE ),
519 // draws a path by first filling and then stroking
521 virtual void , DrawPath( const wxGraphicsPath *path, int fillStyle = wxWINDING_RULE ),
530 virtual void , DrawText( const wxString &str, wxDouble x, wxDouble y ),
535 virtual void , DrawText( const wxString &str, wxDouble x, wxDouble y, wxDouble angle ),
541 virtual void , GetTextExtent( const wxString &text,
542 wxDouble *OUTPUT /*width*/,
543 wxDouble *OUTPUT /*height*/,
544 wxDouble *OUTPUT /*descent*/,
545 wxDouble *OUTPUT /*externalLeading*/ ) const ,
546 "GetFullTextExtent(self, text) --> (width, height, descent, externalLeading)",
551 DocAStr(GetTextExtent,
552 "GetTextExtent(self, text) --> (width, height)",
555 PyObject* GetTextExtent( const wxString &text )
557 wxDouble width = 0.0,
559 self->GetTextExtent(text, &width, &height, NULL, NULL);
560 // thread wrapers are turned off for this .i file, so no need to acquire GIL...
561 PyObject* rv = PyTuple_New(2);
562 PyTuple_SET_ITEM(rv, 0, PyFloat_FromDouble(width));
563 PyTuple_SET_ITEM(rv, 1, PyFloat_FromDouble(height));
570 DocAStr(GetPartialTextExtents,
571 "GetPartialTextExtents(self, text) -> [widths]",
573 wxArrayDouble GetPartialTextExtents(const wxString& text) {
574 wxArrayDouble widths;
575 self->GetPartialTextExtents(text, widths);
586 virtual void , DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h ),
591 virtual void , DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h ),
596 // convenience methods
599 // strokes a single line
601 virtual void , StrokeLine( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2),
605 // stroke lines connecting each of the points
607 virtual void , StrokeLines( size_t points, const wxPoint2D *points_array),
608 "StrokeLines(self, List points)",
612 // // stroke disconnected lines from begin to end points
613 // virtual void StrokeLines( size_t n, const wxPoint2D *beginPoints, const wxPoint2D *endPoints);
616 void StrokeLineSegements(PyObject* beginPoints, PyObject* endPoints)
618 size_t c1, c2, count;
619 wxPoint2D* beginP = wxPoint2D_LIST_helper(beginPoints, &c1);
620 wxPoint2D* endP = wxPoint2D_LIST_helper(endPoints, &c2);
622 if ( beginP != NULL && endP != NULL )
624 count = wxMin(c1, c2);
625 self->StrokeLines(count, beginP, endP);
634 virtual void , DrawLines( size_t points, const wxPoint2D *points_array, int fillStyle = wxWINDING_RULE ),
640 virtual void , DrawRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h),
646 virtual void , DrawEllipse( wxDouble x, wxDouble y, wxDouble w, wxDouble h),
650 // draws a rounded rectangle
652 virtual void , DrawRoundedRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h, wxDouble radius),
658 //---------------------------------------------------------------------------
661 #include "wx/dcgraph.h"
664 class wxGCDC: public wxDC
668 "self.__dc = args[0] # save a ref so the other dc will not be deleted before self";
669 wxGCDC(const wxWindowDC& dc);
673 wxGraphicsContext* GetGraphicsContext();
674 virtual void SetGraphicsContext( wxGraphicsContext* ctx );
676 %property(GraphicsContext, GetGraphicsContext, SetGraphicsContext);
680 //---------------------------------------------------------------------------
682 // Turn GIL acquisition back on.