StyleContext

open class StyleContext(pointer: <Error class: unknown class><<Error class: unknown class>>)

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 method@Gtk.StyleContext.add_provider, or to the display through func@Gtk.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 method@Gtk.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.

Skipped during bindings generation

  • method to_string: C function gtk_style_context_to_string is ignored

Constructors

Link copied to clipboard
constructor(pointer: <Error class: unknown class><<Error class: unknown class>>)

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard
open var display: <Error class: unknown class>

The display of the style context.

Link copied to clipboard
val gtkStyleContextPointer: <Error class: unknown class><<Error class: unknown class>>

Functions

Link copied to clipboard
open fun addClass(className: String)

Adds a style class to @context, so later uses of the style context will make use of this new class for styling.

Link copied to clipboard
open fun addProvider(provider: StyleProvider, priority: <Error class: unknown class>)

Adds a style provider to @context, to be used in style construction.

Link copied to clipboard
open fun getBorder(border: Border)

Gets the border for a given state as a GtkBorder.

Link copied to clipboard
open fun getColor(color: <Error class: unknown class>)

Gets the foreground color for a given state.

Link copied to clipboard
open fun getDisplay(): <Error class: unknown class>

Returns the GdkDisplay to which @context is attached.

Link copied to clipboard
open fun getMargin(margin: Border)

Gets the margin for a given state as a GtkBorder.

Link copied to clipboard
open fun getPadding(padding: Border)

Gets the padding for a given state as a GtkBorder.

Link copied to clipboard
open fun getScale(): Int

Returns the scale used for assets.

Link copied to clipboard
open fun getState(): StateFlags

Returns the state used for style matching.

Link copied to clipboard
open fun hasClass(className: String): Boolean

Returns true if @context currently has defined the given class name.

Link copied to clipboard
open fun lookupColor(colorName: String, color: <Error class: unknown class>): Boolean

Looks up and resolves a color name in the @context color map.

Link copied to clipboard
open fun removeClass(className: String)

Removes @class_name from @context.

Link copied to clipboard
open fun removeProvider(provider: StyleProvider)

Removes @provider from the style providers list in @context.

Link copied to clipboard
open fun restore()

Restores @context state to a previous stage.

Link copied to clipboard
open fun save()

Saves the @context state.

Link copied to clipboard
open fun setDisplay(display: <Error class: unknown class>)

Attaches @context to the given display.

Link copied to clipboard
open fun setScale(scale: Int)

Sets the scale to use when getting image assets for the style.

Link copied to clipboard
open fun setState(flags: StateFlags)

Sets the state to be used for style matching.