# wxGraphicsContext Class Reference
[Graphics Device Interface (GDI), Device Contexts]

`#include </home/work/projects/wx/2.9/wx/interface/wx/graphics.h>`

Inheritance diagram for wxGraphicsContext:
[legend]

## Detailed Description

A wxGraphicsContext instance is the object that is drawn upon.

It is created by a renderer using wxGraphicsRenderer::CreateContext(). This can be either directly using a renderer instance, or indirectly using the static convenience Create() functions of wxGraphicsContext that always delegate the task to the default renderer.

        void MyCanvas::OnPaint(wxPaintEvent &event)
        {
            // Create paint DC
            wxPaintDC dc(this);

            // Create graphics context from it
            wxGraphicsContext *gc = wxGraphicsContext::Create( dc );

            if (gc)
            {
                // make a path that contains a circle and some lines
                gc->SetPen( *wxRED_PEN );
                wxGraphicsPath path = gc->CreatePath();
                path.AddCircle( 50.0, 50.0, 50.0 );
                path.MoveToPoint(0.0, 50.0);
                path.AddLineToPoint(100.0, 50.0);
                path.MoveToPoint(50.0, 0.0);
                path.AddLineToPoint(50.0, 100.0 );
                path.CloseSubpath();
                path.AddRectangle(25.0, 25.0, 50.0, 50.0);

                gc->StrokePath(path);

                delete gc;
            }
        }

##

Library: wxCore
Category: Graphics Device Interface (GDI), Device Contexts

See also:
  wxGraphicsRenderer::CreateContext(), wxGCDC, wxDC

## Public Member Functions

virtual void | Clip (const wxRegion &region)=0
| Clips drawings to the specified region.
virtual void | Clip (wxDouble x, wxDouble y, wxDouble w, wxDouble h)=0
| Clips drawings to the specified rectangle.
virtual void | ConcatTransform (const wxGraphicsMatrix &matrix)=0
| Concatenates the passed in transform with the current transform of this context.
virtual wxGraphicsBrush | CreateBrush (const wxBrush &brush) const
| Creates a native brush from a wxBrush.
virtual wxGraphicsFont | CreateFont (const wxFont &font, const wxColour &col=*wxBLACK) const
| Creates a native graphics font from a wxFont and a text colour.
virtual wxGraphicsMatrix | CreateMatrix (wxDouble a=1.0, wxDouble b=0.0, wxDouble c=0.0, wxDouble d=1.0, wxDouble tx=0.0, wxDouble ty=0.0) const
| Creates a native affine transformation matrix from the passed in values.
wxGraphicsPath | CreatePath () const
| Creates a native graphics path which is initially empty.
virtual wxGraphicsPen | CreatePen (const wxPen &pen) const
| Creates a native pen from a wxPen.
virtual void | DrawBitmap (const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h)=0
| Draws the bitmap.
virtual void | DrawEllipse (wxDouble x, wxDouble y, wxDouble w, wxDouble h)
| Draws an ellipse.
virtual void | DrawIcon (const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h)=0
| Draws the icon.
virtual void | DrawLines (size_t n, const wxPoint2DDouble *points, wxPolygonFillMode fillStyle=wxODDEVEN_RULE)
| Draws a polygon.
virtual void | DrawPath (const wxGraphicsPath &path, wxPolygonFillMode fillStyle=wxODDEVEN_RULE)
| Draws the path by first filling and then stroking.
virtual void | DrawRectangle (wxDouble x, wxDouble y, wxDouble w, wxDouble h)
| Draws a rectangle.
virtual void | DrawRoundedRectangle (wxDouble x, wxDouble y, wxDouble w, wxDouble h, wxDouble radius)
| Draws a rounded rectangle.
void | DrawText (const wxString &str, wxDouble x, wxDouble y)
| Draws text at the defined position.
void | DrawText (const wxString &str, wxDouble x, wxDouble y, wxDouble angle)
| Draws text at the defined position.
void | DrawText (const wxString &str, wxDouble x, wxDouble y, const wxGraphicsBrush &backgroundBrush)
| Draws text at the defined position.
void | DrawText (const wxString &str, wxDouble x, wxDouble y, wxDouble angle, const wxGraphicsBrush &backgroundBrush)
| Draws text at the defined position.
virtual void | FillPath (const wxGraphicsPath &path, wxPolygonFillMode fillStyle=wxODDEVEN_RULE)=0
| Fills the path with the current brush.
virtual void * | GetNativeContext ()=0
| Returns the native context (CGContextRef for Core Graphics, Graphics pointer for GDIPlus and cairo_t pointer for cairo).
virtual void | GetPartialTextExtents (const wxString &text, wxArrayDouble &widths) const =0
| Fills the widths array with the widths from the beginning of text to the corresponding character of text.
virtual void | GetTextExtent (const wxString &text, wxDouble *width, wxDouble *height, wxDouble *descent, wxDouble *externalLeading) const =0
| Gets the dimensions of the string using the currently selected font.
virtual wxGraphicsMatrix | GetTransform () const =0
| Gets the current transformation matrix of this context.
virtual void | ResetClip ()=0
| Resets the clipping to original shape.
virtual void | Rotate (wxDouble angle)=0
| Rotates the current transformation matrix (in radians).
virtual void | Scale (wxDouble xScale, wxDouble yScale)=0
| Scales the current transformation matrix.
void | SetBrush (const wxBrush &brush)
| Sets the brush for filling paths.
virtual void | SetBrush (const wxGraphicsBrush &brush)
| Sets the brush for filling paths.
void | SetFont (const wxFont &font, const wxColour &colour)
| Sets the font for drawing text.
virtual void | SetFont (const wxGraphicsFont &font)
| Sets the font for drawing text.
void | SetPen (const wxPen &pen)
| Sets the pen used for stroking.
virtual void | SetPen (const wxGraphicsPen &pen)
| Sets the pen used for stroking.
virtual void | SetTransform (const wxGraphicsMatrix &matrix)=0
| Sets the current transformation matrix of this context.
virtual void | StrokeLine (wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2)
| Strokes a single line.
virtual void | StrokeLines (size_t n, const wxPoint2DDouble *beginPoints, const wxPoint2DDouble *endPoints)
| Stroke disconnected lines from begin to end points, fastest method available for this purpose.
virtual void | StrokeLines (size_t n, const wxPoint2DDouble *points)
| Stroke lines connecting all the points.
virtual void | StrokePath (const wxGraphicsPath &path)=0
| Strokes along a path with the current pen.
virtual void | Translate (wxDouble dx, wxDouble dy)=0
| Translates the current transformation matrix.
virtual void | BeginLayer (wxDouble opacity)=0
| Redirects all rendering is done into a fully transparent temporary context.
virtual void | EndLayer ()=0
| Composites back the drawings into the context with the opacity given at the BeginLayer call.
virtual bool | SetAntialiasMode (wxAntialiasMode antialias)=0
| Sets the antialiasing mode, returns true if it supported.
virtual wxAntialiasMode | GetAntialiasMode () const
| Returns the current shape antialiasing mode.
virtual bool | SetInterpolationQuality (wxInterpolationQuality interpolation)=0
| Sets the interpolation quality, returns true if it supported.
virtual wxInterpolationQuality | GetInterpolationQuality () const
| Returns the current interpolation quality.
virtual bool | SetCompositionMode (wxCompositionMode op)=0
| Sets the compositing operator, returns true if it supported.
virtual wxCompositionMode | GetCompositionMode () const
| Returns the current compositing operator.
wxGraphicsBrush | CreateLinearGradientBrush (wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2, const wxColour &c1, const wxColour &c2) const
| Creates a native brush with a linear gradient.
wxGraphicsBrush | CreateLinearGradientBrush (wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2, const wxGraphicsGradientStops &stops) const
| Creates a native brush with a linear gradient.
virtual wxGraphicsBrush | CreateRadialGradientBrush (wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius, const wxColour &oColor, const wxColour &cColor) const
| Creates a native brush with a radial gradient.
virtual wxGraphicsBrush | CreateRadialGradientBrush (wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius, const wxGraphicsGradientStops &stops)=0
| Creates a native brush with a radial gradient.

## Static Public Member Functions

static wxGraphicsContext * | Create (wxWindow *window)
| Creates a wxGraphicsContext from a wxWindow.
static wxGraphicsContext * | Create (const wxWindowDC &dc)
| Creates a wxGraphicsContext from a wxWindowDC.
static wxGraphicsContext * | Create (const wxMemoryDC &dc)
| Creates a wxGraphicsContext from a wxMemoryDC.
static wxGraphicsContext * | Create (const wxPrinterDC &dc)
| Creates a wxGraphicsContext from a wxPrinterDC.
static wxGraphicsContext * | CreateFromNative (void *context)
| Creates a wxGraphicsContext from a native context.
static wxGraphicsContext * | CreateFromNativeWindow (void *window)
| Creates a wxGraphicsContext from a native window.

List of all members.

## Member Function Documentation

virtual void wxGraphicsContext::BeginLayer | ( | wxDouble | opacity | ) | `[pure virtual]`

Redirects all rendering is done into a fully transparent temporary context.

virtual void wxGraphicsContext::Clip | ( | const wxRegion & | region | ) | `[pure virtual]`

Clips drawings to the specified region.

virtual void wxGraphicsContext::Clip | ( | wxDouble | x,
|  | wxDouble | y,
|  | wxDouble | w,
|  | wxDouble | h |
| ) |  |  | `[pure virtual]`

Clips drawings to the specified rectangle.

virtual void wxGraphicsContext::ConcatTransform | ( | const wxGraphicsMatrix & | matrix | ) | `[pure virtual]`

Concatenates the passed in transform with the current transform of this context.

static wxGraphicsContext* wxGraphicsContext::Create | ( | wxWindow * | window | ) | `[static]`

Creates a wxGraphicsContext from a wxWindow.

See also:
  wxGraphicsRenderer::CreateContext()

static wxGraphicsContext* wxGraphicsContext::Create | ( | const wxPrinterDC & | dc | ) | `[static]`

Creates a wxGraphicsContext from a wxPrinterDC.

Under GTK+, this will only work when using the GtkPrint printing backend which is available since GTK+ 2.10.

See also:
  wxGraphicsRenderer::CreateContext(), Printing Under Unix (GTK+)

static wxGraphicsContext* wxGraphicsContext::Create | ( | const wxWindowDC & | dc | ) | `[static]`

Creates a wxGraphicsContext from a wxWindowDC.

See also:
  wxGraphicsRenderer::CreateContext()

static wxGraphicsContext* wxGraphicsContext::Create | ( | const wxMemoryDC & | dc | ) | `[static]`

Creates a wxGraphicsContext from a wxMemoryDC.

See also:
  wxGraphicsRenderer::CreateContext()

virtual wxGraphicsBrush wxGraphicsContext::CreateBrush | ( | const wxBrush & | brush | ) | const`[virtual]`

Creates a native brush from a wxBrush.

virtual wxGraphicsFont wxGraphicsContext::CreateFont | ( | const wxFont & | font,
|  | const wxColour & | col = `*wxBLACK` |
| ) |  |  | const`[virtual]`

Creates a native graphics font from a wxFont and a text colour.

static wxGraphicsContext* wxGraphicsContext::CreateFromNative | ( | void * | context | ) | `[static]`

Creates a wxGraphicsContext from a native context.

This native context must be a CGContextRef for Core Graphics, a Graphics pointer for GDIPlus, or a cairo_t pointer for cairo.

See also:
  wxGraphicsRenderer::CreateContextFromNativeContext()

static wxGraphicsContext* wxGraphicsContext::CreateFromNativeWindow | ( | void * | window | ) | `[static]`

Creates a wxGraphicsContext from a native window.

See also:
  wxGraphicsRenderer::CreateContextFromNativeWindow()

wxGraphicsBrush wxGraphicsContext::CreateLinearGradientBrush | ( | wxDouble | x1,
|  | wxDouble | y1,
|  | wxDouble | x2,
|  | wxDouble | y2,
|  | const wxColour & | c1,
|  | const wxColour & | c2 |
| ) |  |  | const

Creates a native brush with a linear gradient.

The brush starts at (x1, y1) and ends at (x2, y2). Either just the start and end gradient colours (c1 and c2) or full set of gradient stops can be specified.

The version taking wxGraphicsGradientStops is new in wxWidgets 2.9.1.

wxGraphicsBrush wxGraphicsContext::CreateLinearGradientBrush | ( | wxDouble | x1,
|  | wxDouble | y1,
|  | wxDouble | x2,
|  | wxDouble | y2,
|  | const wxGraphicsGradientStops & | stops |
| ) |  |  | const

Creates a native brush with a linear gradient.

The brush starts at (x1, y1) and ends at (x2, y2). Either just the start and end gradient colours (c1 and c2) or full set of gradient stops can be specified.

The version taking wxGraphicsGradientStops is new in wxWidgets 2.9.1.

virtual wxGraphicsMatrix wxGraphicsContext::CreateMatrix | ( | wxDouble | a = `1.0`,
|  | wxDouble | b = `0.0`,
|  | wxDouble | c = `0.0`,
|  | wxDouble | d = `1.0`,
|  | wxDouble | tx = `0.0`,
|  | wxDouble | ty = `0.0` |
| ) |  |  | const`[virtual]`

Creates a native affine transformation matrix from the passed in values.

The default parameters result in an identity matrix.

wxGraphicsPath wxGraphicsContext::CreatePath | ( |  | ) | const

Creates a native graphics path which is initially empty.

virtual wxGraphicsPen wxGraphicsContext::CreatePen | ( | const wxPen & | pen | ) | const`[virtual]`

Creates a native pen from a wxPen.

virtual wxGraphicsBrush wxGraphicsContext::CreateRadialGradientBrush | ( | wxDouble | xo,
|  | wxDouble | yo,
|  | wxDouble | xc,
|  | wxDouble | yc,
|  | wxDouble | radius,
|  | const wxColour & | oColor,
|  | const wxColour & | cColor |
| ) |  |  | const`[virtual]`

Creates a native brush with a radial gradient.

The brush originates at (xo, yc) and ends on a circle around (xc, yc) with the given radius.

The gradient may be specified either by its start and end colours oColor and cColor or by a full set of gradient stops.

The version taking wxGraphicsGradientStops is new in wxWidgets 2.9.1.

virtual wxGraphicsBrush wxGraphicsContext::CreateRadialGradientBrush | ( | wxDouble | xo,
|  | wxDouble | yo,
|  | wxDouble | xc,
|  | wxDouble | yc,
|  | wxDouble | radius,
|  | const wxGraphicsGradientStops & | stops |
| ) |  |  | `[pure virtual]`

Creates a native brush with a radial gradient.

The brush originates at (xo, yc) and ends on a circle around (xc, yc) with the given radius.

The gradient may be specified either by its start and end colours oColor and cColor or by a full set of gradient stops.

The version taking wxGraphicsGradientStops is new in wxWidgets 2.9.1.

virtual void wxGraphicsContext::DrawBitmap | ( | const wxBitmap & | bmp,
|  | wxDouble | x,
|  | wxDouble | y,
|  | wxDouble | w,
|  | wxDouble | h |
| ) |  |  | `[pure virtual]`

Draws the bitmap.

In case of a mono bitmap, this is treated as a mask and the current brushed is used for filling.

virtual void wxGraphicsContext::DrawEllipse | ( | wxDouble | x,
|  | wxDouble | y,
|  | wxDouble | w,
|  | wxDouble | h |
| ) |  |  | `[virtual]`

Draws an ellipse.

virtual void wxGraphicsContext::DrawIcon | ( | const wxIcon & | icon,
|  | wxDouble | x,
|  | wxDouble | y,
|  | wxDouble | w,
|  | wxDouble | h |
| ) |  |  | `[pure virtual]`

Draws the icon.

virtual void wxGraphicsContext::DrawLines | ( | size_t | n,
|  | const wxPoint2DDouble * | points,
|  | wxPolygonFillMode | fillStyle = `wxODDEVEN_RULE` |
| ) |  |  | `[virtual]`

Draws a polygon.

virtual void wxGraphicsContext::DrawPath | ( | const wxGraphicsPath & | path,
|  | wxPolygonFillMode | fillStyle = `wxODDEVEN_RULE` |
| ) |  |  | `[virtual]`

Draws the path by first filling and then stroking.

virtual void wxGraphicsContext::DrawRectangle | ( | wxDouble | x,
|  | wxDouble | y,
|  | wxDouble | w,
|  | wxDouble | h |
| ) |  |  | `[virtual]`

Draws a rectangle.

virtual void wxGraphicsContext::DrawRoundedRectangle | ( | wxDouble | x,
|  | wxDouble | y,
|  | wxDouble | w,
|  | wxDouble | h,
|  | wxDouble | radius |
| ) |  |  | `[virtual]`

Draws a rounded rectangle.

void wxGraphicsContext::DrawText | ( | const wxString & | str,
|  | wxDouble | x,
|  | wxDouble | y |
| ) |  |  |

Draws text at the defined position.

void wxGraphicsContext::DrawText | ( | const wxString & | str,
|  | wxDouble | x,
|  | wxDouble | y,
|  | wxDouble | angle |
| ) |  |  |

Draws text at the defined position.

Parameters:

  | str | The text to draw.
  | x | The x coordinate position to draw the text at.
  | y | The y coordinate position to draw the text at.
  | angle | The angle relative to the (default) horizontal direction to draw the string.

void wxGraphicsContext::DrawText | ( | const wxString & | str,
|  | wxDouble | x,
|  | wxDouble | y,
|  | const wxGraphicsBrush & | backgroundBrush |
| ) |  |  |

Draws text at the defined position.

Parameters:

  | str | The text to draw.
  | x | The x coordinate position to draw the text at.
  | y | The y coordinate position to draw the text at.
  | backgroundBrush | Brush to fill the text with.

void wxGraphicsContext::DrawText | ( | const wxString & | str,
|  | wxDouble | x,
|  | wxDouble | y,
|  | wxDouble | angle,
|  | const wxGraphicsBrush & | backgroundBrush |
| ) |  |  |

Draws text at the defined position.

Parameters:

  | str | The text to draw.
  | x | The x coordinate position to draw the text at.
  | y | The y coordinate position to draw the text at.
  | angle | The angle relative to the (default) horizontal direction to draw the string.
  | backgroundBrush | Brush to fill the text with.

virtual void wxGraphicsContext::EndLayer | ( |  | ) | `[pure virtual]`

Composites back the drawings into the context with the opacity given at the BeginLayer call.

virtual void wxGraphicsContext::FillPath | ( | const wxGraphicsPath & | path,
|  | wxPolygonFillMode | fillStyle = `wxODDEVEN_RULE` |
| ) |  |  | `[pure virtual]`

Fills the path with the current brush.

virtual wxAntialiasMode wxGraphicsContext::GetAntialiasMode | ( |  | ) | const`[virtual]`

Returns the current shape antialiasing mode.

virtual wxCompositionMode wxGraphicsContext::GetCompositionMode | ( |  | ) | const`[virtual]`

Returns the current compositing operator.

virtual wxInterpolationQuality wxGraphicsContext::GetInterpolationQuality | ( |  | ) | const`[virtual]`

Returns the current interpolation quality.

virtual void* wxGraphicsContext::GetNativeContext | ( |  | ) | `[pure virtual]`

Returns the native context (CGContextRef for Core Graphics, Graphics pointer for GDIPlus and cairo_t pointer for cairo).

virtual void wxGraphicsContext::GetPartialTextExtents | ( | const wxString & | text,
|  | wxArrayDouble & | widths |
| ) |  |  | const`[pure virtual]`

Fills the widths array with the widths from the beginning of text to the corresponding character of text.

virtual void wxGraphicsContext::GetTextExtent | ( | const wxString & | text,
|  | wxDouble * | width,
|  | wxDouble * | height,
|  | wxDouble * | descent,
|  | wxDouble * | externalLeading |
| ) |  |  | const`[pure virtual]`

Gets the dimensions of the string using the currently selected font.

Parameters:

  | text | The text string to measure.
  | width | Variable to store the total calculated width of the text.
  | height | Variable to store the total calculated height of the text.
  | descent | Variable to store the dimension from the baseline of the font to the bottom of the descender.
  | externalLeading | Any extra vertical space added to the font by the font designer (usually is zero).

virtual wxGraphicsMatrix wxGraphicsContext::GetTransform | ( |  | ) | const`[pure virtual]`

Gets the current transformation matrix of this context.

virtual void wxGraphicsContext::ResetClip | ( |  | ) | `[pure virtual]`

Resets the clipping to original shape.

virtual void wxGraphicsContext::Rotate | ( | wxDouble | angle | ) | `[pure virtual]`

Rotates the current transformation matrix (in radians).

virtual void wxGraphicsContext::Scale | ( | wxDouble | xScale,
|  | wxDouble | yScale |
| ) |  |  | `[pure virtual]`

Scales the current transformation matrix.

virtual bool wxGraphicsContext::SetAntialiasMode | ( | wxAntialiasMode | antialias | ) | `[pure virtual]`

Sets the antialiasing mode, returns true if it supported.

void wxGraphicsContext::SetBrush | ( | const wxBrush & | brush | ) |

Sets the brush for filling paths.

virtual void wxGraphicsContext::SetBrush | ( | const wxGraphicsBrush & | brush | ) | `[virtual]`

Sets the brush for filling paths.

virtual bool wxGraphicsContext::SetCompositionMode | ( | wxCompositionMode | op | ) | `[pure virtual]`

Sets the compositing operator, returns true if it supported.

virtual void wxGraphicsContext::SetFont | ( | const wxGraphicsFont & | font | ) | `[virtual]`

Sets the font for drawing text.

void wxGraphicsContext::SetFont | ( | const wxFont & | font,
|  | const wxColour & | colour |
| ) |  |  |

Sets the font for drawing text.

virtual bool wxGraphicsContext::SetInterpolationQuality | ( | wxInterpolationQuality | interpolation | ) | `[pure virtual]`

Sets the interpolation quality, returns true if it supported.

virtual void wxGraphicsContext::SetPen | ( | const wxGraphicsPen & | pen | ) | `[virtual]`

Sets the pen used for stroking.

void wxGraphicsContext::SetPen | ( | const wxPen & | pen | ) |

Sets the pen used for stroking.

virtual void wxGraphicsContext::SetTransform | ( | const wxGraphicsMatrix & | matrix | ) | `[pure virtual]`

Sets the current transformation matrix of this context.

virtual void wxGraphicsContext::StrokeLine | ( | wxDouble | x1,
|  | wxDouble | y1,
|  | wxDouble | x2,
|  | wxDouble | y2 |
| ) |  |  | `[virtual]`

Strokes a single line.

virtual void wxGraphicsContext::StrokeLines | ( | size_t | n,
|  | const wxPoint2DDouble * | beginPoints,
|  | const wxPoint2DDouble * | endPoints |
| ) |  |  | `[virtual]`

Stroke disconnected lines from begin to end points, fastest method available for this purpose.

virtual void wxGraphicsContext::StrokeLines | ( | size_t | n,
|  | const wxPoint2DDouble * | points |
| ) |  |  | `[virtual]`

Stroke lines connecting all the points.

Unlike the other overload of this function, this method draws a single polyline and not a number of disconnected lines.

virtual void wxGraphicsContext::StrokePath | ( | const wxGraphicsPath & | path | ) | `[pure virtual]`

Strokes along a path with the current pen.

virtual void wxGraphicsContext::Translate | ( | wxDouble | dx,
|  | wxDouble | dy |
| ) |  |  | `[pure virtual]`

Translates the current transformation matrix.

|  | [ top ]