Common Graphics : Using graphics contexts : Creating graphics contexts

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.
Note:
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
C:\Users\documentation\Documents\vastePublisher\stable\VAS Documentation Word\images\sp006015.gif
C:\Users\documentation\Documents\vastePublisher\stable\VAS Documentation Word\images\sp006020.gif
C:\Users\documentation\Documents\vastePublisher\stable\VAS Documentation Word\images\sp006025.gif
C:\Users\documentation\Documents\vastePublisher\stable\VAS Documentation Word\images\sp006030.gif
C:\Users\documentation\Documents\vastePublisher\stable\VAS Documentation Word\images\sp006035.gif
C:\Users\documentation\Documents\vastePublisher\stable\VAS Documentation Word\images\sp006040.gif
C:\Users\documentation\Documents\vastePublisher\stable\VAS Documentation Word\images\sp006045.gifC:\Users\documentation\Documents\vastePublisher\stable\VAS Documentation Word\images\sp006045.gif
C:\Users\documentation\Documents\vastePublisher\stable\VAS Documentation Word\images\sp006050.gifC:\Users\documentation\Documents\vastePublisher\stable\VAS Documentation Word\images\sp006050.gif
C:\Users\documentation\Documents\vastePublisher\stable\VAS Documentation Word\images\sp006055.gif
Tip:
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.
gc
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