Creating graphics contexts
Graphics contexts are created by sending the createGC:values: message to a CgDrawable. The GC attributes are initialized to preset values, listed in Table 16. The two arguments of the message specify modifications to the preset values. The first argument, valuemask, specifies which components of the second argument, values, should override the preset values.
Creating a graphics context with default values
In the following example, the temporary variable gc is assigned a graphics context. By passing the CgConstants constant None as the valuemask and nil as the values, the graphics context is initialized using default values.
| gc |
gc := CgWindow default
createGC: None
values: nil.
Creating a graphics context using the CgGCValues object
Graphics contexts can be configured using a CgGCValues object. The CgGCValues object is a convenient way of storing graphics attributes without having to store the graphics context and its associated overhead. The CgGCValues object is used to configure a GC or to query its attributes.
In the following example, a CgGCValues object is created, the function: message sets the desired raster operation (GXhighlight), and the lineWidth: message sets a line width value (2 pixels) in the CgGCValues object. The CgGCValues object is then passed to the drawable using the createGC:values: message. The valuemasks GCFunction and GCLineWidth are ORed together to create a mask that identifies which attributes are being modified. The gcValues variable contains the new values for the attributes.
| gc gcValues |
gcValues := CgGCValues new
function: GXhighlight;
lineWidth: 2.
gc := CgWindow default
createGC: GCFunction | GCLineWidth
values: gcValues.
Retrieving several values from a graphics context using the CgGCValues object
You can also use a CgGCValues object to retrieve attribute values from a graphics context. In the following example, the GCLineWidth valuemask is used to retrieve the current line width. The information is returned in a new CgGCValues object.
| gcValues lineWidth |
CgGC default
getGCValues: GCLineWidth
valuesReturn: (gcValues := CgGCValues new).
lineWidth := gcValues lineWidth.
The GCClipMask and GCDashList components of a GC cannot be retrieved.
Table 16 contains the complete list of attributes that can be initialized or modified. In most cases, the attribute is either an integer value or a constant from the CgConstants pool dictionary. For a detailed explanation, consult the method comment for the createGC:values: method of CgDrawable.
Table 16. Graphics context attributes
Value mask
Value expected
Default value
GCForeground specifies the foreground drawing color
Integer pixel value (index of color in colormap)
GCBackground specifies the background drawing color
Integer pixel value (index of color in colormap)
GCLineWidth specifies the thickness of the drawing line
Integer value of line width in pixels
1 pixel
GCLineStyle specifies how a line should be drawn. Dash pattern is specified using a dash list (see "dashes" below)
LineSolid (foreground color)
LineOnOffDash (foreground color)
LineDoubleDash (fore- and background color)
C:\Users\documentation\Documents\vastePublisher\stable\VAS Documentation Word\images\sp006015.gif
GCCapStyle specifies how a line should be ended
C:\Users\documentation\Documents\vastePublisher\stable\VAS Documentation Word\images\sp006020.gif
CGJoinStyle specifies how two lines should be joined


C:\Users\documentation\Documents\vastePublisher\stable\VAS Documentation Word\images\sp006025.gif
GCFillStyle specifies how to fill an object such as an arc, rectangle, or polygon
(foreground color)
(foreground color)
(fore- and background
(a multicolored pixmap)
C:\Users\documentation\Documents\vastePublisher\stable\VAS Documentation Word\images\sp006030.gif
GCFillRule specifies how a polygon should be drawn when it intersects itself
EvenOddRule (hollow where areas intersect)
WindingRule (solid where areas intersect)
C:\Users\documentation\Documents\vastePublisher\stable\VAS Documentation Word\images\sp006035.gif
GCArcMode specifies how an arc should be drawn. To fill an arc, the GCFillStyle would also have to be set correctly.
ArcChord (connects start and end points directly)
ArcPieslice (connects start and end points through the arc center point)
C:\Users\documentation\Documents\vastePublisher\stable\VAS Documentation Word\images\sp006040.gif
GCFunction specifies the raster operation to be used
(draws shape directly from source)
(draws shape, replacing foreground with
background and vice-versa. Drawing
the same shape again will restore
drawable to previous state)
GCFont specifies the font to be used in text drawing operations
A CgFont
Standard system font
GCSubwindowMode specifies whether drawing in a parent window is clipped by or draws over child windows
(allows drawing only in the parent window; child
windows are not affected)
(child boundaries are ignored; drawing in the
parent window will draw over child windows)
GCTileStripXOrigin specifies the x offset of the tile or stipple pattern allowing you to position the pattern evenly
Integer value
C:\Users\documentation\Documents\vastePublisher\stable\VAS Documentation Word\images\sp006045.gifC:\Users\documentation\Documents\vastePublisher\stable\VAS Documentation Word\images\sp006045.gif
GCTileStipYOrigin specifies the y offset of the stipple or tile pattern allowing you to position the pattern evenly
Integer value
C:\Users\documentation\Documents\vastePublisher\stable\VAS Documentation Word\images\sp006050.gifC:\Users\documentation\Documents\vastePublisher\stable\VAS Documentation Word\images\sp006050.gif
x integer coordinate for clipping
y integer coordinate for clipping
GCClipMask specifies the CgPixmap used as a clipping mask
A CgPixmap of depth 1, or nil
GCDashOffset specifies in pixels where to begin within a dash pattern
Integer value
tile specifies a CgPixmap to be used as a multiplane, multicolor fill pattern
A CgPixmap of the same depth as the drawable
A tile CgPixmap containing all zeros
stipple specifies a CgPixmap to be used as a 1-plane, 2-color fill pattern
A CgPixmap of depth 1
A stipple CgPixmap containing all ones
dashes specifies the lengths of segments in the dash list
Array of integers specifying the length of each dash in number of pixels
C:\Users\documentation\Documents\vastePublisher\stable\VAS Documentation Word\images\sp006055.gif
#(4 4)
Some platforms impose constraints on the implementation of the graphics context attributes. These limitations are described in Appendix D, "Common graphics platform differences".
Configuring a graphics context using convenience methods
Convenience (set) methods have been provided for setting graphics context attributes. These methods are sent directly to the graphics context and are simpler to use than specifying CgGCValues and valuemasks.
In the following example, a GC is created using default values, then immediately modified using two convenience methods.
| gc |
gc := CgWindow default
createGC: None
values: nil.
gc setFunction: GXcopy.
setLineAttributes: 2
lineStyle: LineSolid
capStyle: CapButt
joinStyle: JoinMiter.
The following table lists frequently used convenience methods that can be sent to a graphics context.
Table 17. Graphics context convenience (set) methods
Values modified
Background color
gc setBackground:
CgWindow default whitePixel
Dash offset and pattern
gc setDashes: 0 dashList: #(100 20)
Foreground color
gc setForeground:
CgWindow default blackPixel
Raster operation
gc setFunction: GXhighlight
Width, style, endcap style, and join style of a line, all at once
setLineAttributes: 2
lineStyle: LineOnOffDash
capStyle: CapButt
joinStyle: JoinMiter
Foreground and background colors, the raster operation function, and the planeMask all at once
setState: CgWindow default blackPixel
background: CgWindow default whitePixel
function: GXcopy
planeMask: nil
Last modified date: 06/09/2018