StyleContextProtocol
public protocol StyleContextProtocol : ObjectProtocol
GtkStyleContext stores styling information affecting a widget.
In order to construct the final style information, GtkStyleContext
queries information from all attached GtkStyleProviders. Style
providers can be either attached explicitly to the context through
[methodGtk.StyleContext.add_provider], or to the display through
[funcGtk.StyleContext.add_provider_for_display]. The resulting
style is a combination of all providers’ information in priority order.
For GTK widgets, any GtkStyleContext returned by
[methodGtk.Widget.get_style_context] will already have a GdkDisplay
and RTL/LTR information set. The style context will also be updated
automatically if any of these settings change on the widget.
Style Classes
Widgets can add style classes to their context, which can be used to associate different styles by class. The documentation for individual widgets lists which style classes it uses itself, and which style classes may be added by applications to affect their appearance.
Custom styling in UI libraries and applications
If you are developing a library with custom widgets that render differently
than standard components, you may need to add a GtkStyleProvider yourself
with the GTK_STYLE_PROVIDER_PRIORITY_FALLBACK priority, either a
GtkCssProvider or a custom object implementing the GtkStyleProvider
interface. This way themes may still attempt to style your UI elements in
a different way if needed so.
If you are using custom styling on an applications, you probably want then
to make your style information prevail to the theme’s, so you must use
a GtkStyleProvider with the GTK_STYLE_PROVIDER_PRIORITY_APPLICATION
priority, keep in mind that the user settings in
XDG_CONFIG_HOME/gtk-4.0/gtk.css will
still take precedence over your changes, as it uses the
GTK_STYLE_PROVIDER_PRIORITY_USER priority.
The StyleContextProtocol protocol exposes the methods and properties of an underlying GtkStyleContext instance.
The default implementation of these can be found in the protocol extension below.
For a concrete class that implements these methods and properties, see StyleContext.
Alternatively, use StyleContextRef as a lighweight, unowned reference if you already have an instance you just want to use.
-
Untyped pointer to the underlying
GtkStyleContextinstance.Declaration
Swift
var ptr: UnsafeMutableRawPointer! { get } -
style_context_ptrDefault implementationTyped pointer to the underlying
GtkStyleContextinstance.Default Implementation
Return the stored, untyped pointer as a typed pointer to the
GtkStyleContextinstance.Declaration
Swift
var style_context_ptr: UnsafeMutablePointer<GtkStyleContext>! { get } -
Required Initialiser for types conforming to
StyleContextProtocolDeclaration
Swift
init(raw: UnsafeMutableRawPointer)
-
bind(property:Extension methodto: _: flags: transformFrom: transformTo: ) Bind a
StyleContextPropertyNamesource property to a given target object.Declaration
Swift
@discardableResult @inlinable func bind<Q, T>(property source_property: StyleContextPropertyName, to target: T, _ target_property: Q, flags f: BindingFlags = .default, transformFrom transform_from: @escaping GLibObject.ValueTransformer = { $0.transform(destValue: $1) }, transformTo transform_to: @escaping GLibObject.ValueTransformer = { $0.transform(destValue: $1) }) -> BindingRef! where Q : PropertyNameProtocol, T : ObjectProtocolParameters
source_propertythe source property to bind
targetthe target object to bind to
target_propertythe target property to bind to
flagsthe flags to pass to the
Bindingtransform_fromValueTransformerto use for forward transformationtransform_toValueTransformerto use for backwards transformationReturn Value
binding reference or
nilin case of an error -
get(property:Extension method) Get the value of a StyleContext property
Declaration
Swift
@inlinable func get(property: StyleContextPropertyName) -> GLibObject.ValueParameters
propertythe property to get the value for
Return Value
the value of the named property
-
set(property:Extension methodvalue: ) Set the value of a StyleContext property. Note that this will only have an effect on properties that are writable and not construct-only!
Declaration
Swift
@inlinable func set(property: StyleContextPropertyName, value v: GLibObject.Value)Parameters
propertythe property to get the value for
Return Value
the value of the named property
-
addClass(className:Extension method) Adds a style class to
context, so later uses of the style context will make use of this new class for styling.In the CSS file format, a
GtkEntrydefining a “search” class, would be matched by:entry.search { ... }While any widget defining a “search” class would be matched by:
.search { ... }Declaration
Swift
@inlinable func addClass(className: UnsafePointer<CChar>!) -
add(provider:Extension methodpriority: ) Adds a style provider to
context, to be used in style construction.Note that a style provider added by this function only affects the style of the widget to which
contextbelongs. If you want to affect the style of all widgets, use [funcGtk.StyleContext.add_provider_for_display].Note: If both priorities are the same, a
GtkStyleProvideradded through this function takes precedence over another added through [funcGtk.StyleContext.add_provider_for_display].Declaration
Swift
@inlinable func add<StyleProviderT>(provider: StyleProviderT, priority: Int) where StyleProviderT : StyleProviderProtocol -
get(border:Extension method) Gets the border for a given state as a
GtkBorder.Declaration
Swift
@inlinable func get<BorderT>(border: BorderT) where BorderT : BorderProtocol -
get(color:Extension method) Gets the foreground color for a given state.
Declaration
Swift
@inlinable func get<RGBAT>(color: RGBAT) where RGBAT : RGBAProtocol -
getDisplay()Extension methodReturns the
GdkDisplayto whichcontextis attached.Declaration
Swift
@inlinable func getDisplay() -> Gdk.DisplayRef! -
get(margin:Extension method) Gets the margin for a given state as a
GtkBorder.Declaration
Swift
@inlinable func get<BorderT>(margin: BorderT) where BorderT : BorderProtocol -
get(padding:Extension method) Gets the padding for a given state as a
GtkBorder.Declaration
Swift
@inlinable func get<BorderT>(padding: BorderT) where BorderT : BorderProtocol -
getScale()Extension methodReturns the scale used for assets.
Declaration
Swift
@inlinable func getScale() -> Int -
getState()Extension methodReturns the state used for style matching.
This method should only be used to retrieve the
GtkStateFlagsto pass toGtkStyleContextmethods, like [methodGtk.StyleContext.get_padding]. If you need to retrieve the current state of aGtkWidget, use [methodGtk.Widget.get_state_flags].Declaration
Swift
@inlinable func getState() -> StateFlags -
hasClass(className:Extension method) Returns
trueifcontextcurrently has defined the given class name.Declaration
Swift
@inlinable func hasClass(className: UnsafePointer<CChar>!) -> Bool -
lookupColor(colorName:Extension methodcolor: ) Looks up and resolves a color name in the
contextcolor map.Declaration
Swift
@inlinable func lookupColor<RGBAT>(colorName: UnsafePointer<CChar>!, color: RGBAT) -> Bool where RGBAT : RGBAProtocol -
removeClass(className:Extension method) Removes
class_namefromcontext.Declaration
Swift
@inlinable func removeClass(className: UnsafePointer<CChar>!) -
remove(provider:Extension method) Removes
providerfrom the style providers list incontext.Declaration
Swift
@inlinable func remove<StyleProviderT>(provider: StyleProviderT) where StyleProviderT : StyleProviderProtocol -
restore()Extension methodRestores
contextstate to a previous stage.See [method
Gtk.StyleContext.save].Declaration
Swift
@inlinable func restore() -
save()Extension methodSaves the
contextstate.This allows temporary modifications done through [method
Gtk.StyleContext.add_class], [methodGtk.StyleContext.remove_class], [methodGtk.StyleContext.set_state] to be quickly reverted in one go through [methodGtk.StyleContext.restore].The matching call to [method
Gtk.StyleContext.restore] must be done before GTK returns to the main loop.Declaration
Swift
@inlinable func save() -
set(display:Extension method) Attaches
contextto the given display.The display is used to add style information from “global” style providers, such as the display’s
GtkSettingsinstance.If you are using a
GtkStyleContextreturned from [methodGtk.Widget.get_style_context], you do not need to call this yourself.Declaration
Swift
@inlinable func set<DisplayT>(display: DisplayT) where DisplayT : DisplayProtocol -
set(scale:Extension method) Sets the scale to use when getting image assets for the style.
Declaration
Swift
@inlinable func set(scale: Int) -
setState(flags:Extension method) Sets the state to be used for style matching.
Declaration
Swift
@inlinable func setState(flags: StateFlags) -
toString(flags:Extension method) Converts the style context into a string representation.
The string representation always includes information about the name, state, id, visibility and style classes of the CSS node that is backing
context. Depending on the flags, more information may be included.This function is intended for testing and debugging of the CSS implementation in GTK. There are no guarantees about the format of the returned string, it may change.
Declaration
Swift
@inlinable func toString(flags: StyleContextPrintFlags) -> String! -
renderActivity(cr:Extension methodx: y: width: height: ) Renders an activity indicator (such as in
GtkSpinner). The stateGTK_STATE_FLAG_CHECKEDdetermines whether there is activity going on.Declaration
Swift
@inlinable func renderActivity<ContextT>(cr: ContextT, x: CDouble, y: CDouble, width: CDouble, height: CDouble) where ContextT : ContextProtocol -
renderArrow(cr:Extension methodangle: x: y: size: ) Renders an arrow pointing to
angle.Typical arrow rendering at 0, 1⁄2 π;, π; and 3⁄2 π:

Declaration
Swift
@inlinable func renderArrow<ContextT>(cr: ContextT, angle: CDouble, x: CDouble, y: CDouble, size: CDouble) where ContextT : ContextProtocol -
renderBackground(cr:Extension methodx: y: width: height: ) Renders the background of an element.
Typical background rendering, showing the effect of
background-image,border-widthandborder-radius:
Declaration
Swift
@inlinable func renderBackground<ContextT>(cr: ContextT, x: CDouble, y: CDouble, width: CDouble, height: CDouble) where ContextT : ContextProtocol -
renderCheck(cr:Extension methodx: y: width: height: ) Renders a checkmark (as in a
GtkCheckButton).The
GTK_STATE_FLAG_CHECKEDstate determines whether the check is on or off, andGTK_STATE_FLAG_INCONSISTENTdetermines whether it should be marked as undefined.Typical checkmark rendering:

Declaration
Swift
@inlinable func renderCheck<ContextT>(cr: ContextT, x: CDouble, y: CDouble, width: CDouble, height: CDouble) where ContextT : ContextProtocol -
renderExpander(cr:Extension methodx: y: width: height: ) Renders an expander (as used in
GtkTreeViewandGtkExpander) in the area defined byx,y,width,height. The stateGTK_STATE_FLAG_CHECKEDdetermines whether the expander is collapsed or expanded.Typical expander rendering:

Declaration
Swift
@inlinable func renderExpander<ContextT>(cr: ContextT, x: CDouble, y: CDouble, width: CDouble, height: CDouble) where ContextT : ContextProtocol -
renderFocus(cr:Extension methodx: y: width: height: ) Renders a focus indicator on the rectangle determined by
x,y,width,height.Typical focus rendering:

Declaration
Swift
@inlinable func renderFocus<ContextT>(cr: ContextT, x: CDouble, y: CDouble, width: CDouble, height: CDouble) where ContextT : ContextProtocol -
renderFrame(cr:Extension methodx: y: width: height: ) Renders a frame around the rectangle defined by
x,y,width,height.Examples of frame rendering, showing the effect of
border-image,border-color,border-width,border-radiusand junctions:
Declaration
Swift
@inlinable func renderFrame<ContextT>(cr: ContextT, x: CDouble, y: CDouble, width: CDouble, height: CDouble) where ContextT : ContextProtocol -
renderHandle(cr:Extension methodx: y: width: height: ) Renders a handle (as in
GtkPanedandGtkWindow’s resize grip), in the rectangle determined byx,y,width,height.Handles rendered for the paned and grip classes:

Declaration
Swift
@inlinable func renderHandle<ContextT>(cr: ContextT, x: CDouble, y: CDouble, width: CDouble, height: CDouble) where ContextT : ContextProtocol -
renderIcon(cr:Extension methodtexture: x: y: ) Renders the icon in
textureat the specifiedxandycoordinates.This function will render the icon in
textureat exactly its size, regardless of scaling factors, which may not be appropriate when drawing on displays with high pixel densities.Declaration
Swift
@inlinable func renderIcon<ContextT, TextureT>(cr: ContextT, texture: TextureT, x: CDouble, y: CDouble) where ContextT : ContextProtocol, TextureT : TextureProtocol -
renderLayout(cr:Extension methodx: y: layout: ) Renders
layouton the coordinatesx,yDeclaration
Swift
@inlinable func renderLayout<ContextT, LayoutT>(cr: ContextT, x: CDouble, y: CDouble, layout: LayoutT) where ContextT : ContextProtocol, LayoutT : LayoutProtocol -
renderLine(cr:Extension methodx0: y0: x1: y1: ) Renders a line from (x0, y0) to (x1, y1).
Declaration
Swift
@inlinable func renderLine<ContextT>(cr: ContextT, x0: CDouble, y0: CDouble, x1: CDouble, y1: CDouble) where ContextT : ContextProtocol -
renderOption(cr:Extension methodx: y: width: height: ) Renders an option mark (as in a radio button), the
GTK_STATE_FLAG_CHECKEDstate will determine whether the option is on or off, andGTK_STATE_FLAG_INCONSISTENTwhether it should be marked as undefined.Typical option mark rendering:

Declaration
Swift
@inlinable func renderOption<ContextT>(cr: ContextT, x: CDouble, y: CDouble, width: CDouble, height: CDouble) where ContextT : ContextProtocol -
displayExtension methodUndocumented
Declaration
Swift
@inlinable var display: Gdk.DisplayRef! { get nonmutating set } -
scaleExtension methodReturns the scale used for assets.
Declaration
Swift
@inlinable var scale: Int { get nonmutating set } -
stateExtension methodReturns the state used for style matching.
This method should only be used to retrieve the
GtkStateFlagsto pass toGtkStyleContextmethods, like [methodGtk.StyleContext.get_padding]. If you need to retrieve the current state of aGtkWidget, use [methodGtk.Widget.get_state_flags].Declaration
Swift
@inlinable var state: StateFlags { get nonmutating set } -
parentObjectExtension methodUndocumented
Declaration
Swift
@inlinable var parentObject: GObject { get }
View on GitHub
Install in Dash
StyleContextProtocol Protocol Reference