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
GtkStyleContext
instance.Declaration
Swift
var ptr: UnsafeMutableRawPointer! { get }
-
style_context_ptr
Default implementationTyped pointer to the underlying
GtkStyleContext
instance.Default Implementation
Return the stored, untyped pointer as a typed pointer to the
GtkStyleContext
instance.Declaration
Swift
var style_context_ptr: UnsafeMutablePointer<GtkStyleContext>! { get }
-
Required Initialiser for types conforming to
StyleContextProtocol
Declaration
Swift
init(raw: UnsafeMutableRawPointer)
-
bind(property:
Extension methodto: _: flags: transformFrom: transformTo: ) Bind a
StyleContextPropertyName
source 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 : ObjectProtocol
Parameters
source_property
the source property to bind
target
the target object to bind to
target_property
the target property to bind to
flags
the flags to pass to the
Binding
transform_from
ValueTransformer
to use for forward transformationtransform_to
ValueTransformer
to use for backwards transformationReturn Value
binding reference or
nil
in case of an error -
get(property:
Extension method) Get the value of a StyleContext property
Declaration
Swift
@inlinable func get(property: StyleContextPropertyName) -> GLibObject.Value
Parameters
property
the 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
property
the 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
GtkEntry
defining 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
context
belongs. 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
GtkStyleProvider
added 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
GdkDisplay
to whichcontext
is 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
GtkStateFlags
to pass toGtkStyleContext
methods, 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
true
ifcontext
currently 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
context
color map.Declaration
Swift
@inlinable func lookupColor<RGBAT>(colorName: UnsafePointer<CChar>!, color: RGBAT) -> Bool where RGBAT : RGBAProtocol
-
removeClass(className:
Extension method) Removes
class_name
fromcontext
.Declaration
Swift
@inlinable func removeClass(className: UnsafePointer<CChar>!)
-
remove(provider:
Extension method) Removes
provider
from the style providers list incontext
.Declaration
Swift
@inlinable func remove<StyleProviderT>(provider: StyleProviderT) where StyleProviderT : StyleProviderProtocol
-
restore()
Extension methodRestores
context
state to a previous stage.See [method
Gtk.StyleContext.save
].Declaration
Swift
@inlinable func restore()
-
save()
Extension methodSaves the
context
state.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
context
to the given display.The display is used to add style information from “global” style providers, such as the display’s
GtkSettings
instance.If you are using a
GtkStyleContext
returned 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_CHECKED
determines 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-width
andborder-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_CHECKED
state determines whether the check is on or off, andGTK_STATE_FLAG_INCONSISTENT
determines 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
GtkTreeView
andGtkExpander
) in the area defined byx
,y
,width
,height
. The stateGTK_STATE_FLAG_CHECKED
determines 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-radius
and 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
GtkPaned
andGtkWindow
’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
texture
at the specifiedx
andy
coordinates.This function will render the icon in
texture
at 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
layout
on the coordinatesx
,y
Declaration
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_CHECKED
state will determine whether the option is on or off, andGTK_STATE_FLAG_INCONSISTENT
whether 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
-
display
Extension methodUndocumented
Declaration
Swift
@inlinable var display: Gdk.DisplayRef! { get nonmutating set }
-
scale
Extension methodReturns the scale used for assets.
Declaration
Swift
@inlinable var scale: Int { get nonmutating set }
-
state
Extension methodReturns the state used for style matching.
This method should only be used to retrieve the
GtkStateFlags
to pass toGtkStyleContext
methods, 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 }
-
parentObject
Extension methodUndocumented
Declaration
Swift
@inlinable var parentObject: GObject { get }