class Gtk::StyleContext

Overview

Gtk::StyleContext stores styling information affecting a widget.

In order to construct the final style information, Gtk::StyleContext queries information from all attached Gtk::StyleProviders. Style providers can be either attached explicitly to the context through Gtk::StyleContext#add_provider, or to the display through Gtk::StyleContext#add_provider_for_display. The resulting style is a combination of all providers’ information in priority order.

For GTK widgets, any Gtk::StyleContext returned by Gtk::Widget#style_context will already have a Gdk::Display 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 Gtk::StyleProvider yourself with the %GTK_STYLE_PROVIDER_PRIORITY_FALLBACK priority, either a Gtk::CssProvider or a custom object implementing the Gtk::StyleProvider 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 Gtk::StyleProvider 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.

Defined in:

lib/gi-crystal/src/auto/gtk-4.0/style_context.cr

Constructors

Class Method Summary

Instance Method Summary

Instance methods inherited from class GObject::Object

==(other : self) ==, bind_property(source_property : String, target : GObject::Object, target_property : String, flags : GObject::BindingFlags) : GObject::Binding bind_property, bind_property_full(source_property : String, target : GObject::Object, target_property : String, flags : GObject::BindingFlags, transform_to : GObject::Closure, transform_from : GObject::Closure) : GObject::Binding bind_property_full, data(key : String) : Pointer(Void) | Nil data, finalize finalize, freeze_notify : Nil freeze_notify, getv(names : Enumerable(String), values : Enumerable(_)) : Nil getv, hash(hasher) hash, notify(property_name : String) : Nil notify, notify_by_pspec(pspec : GObject::ParamSpec) : Nil notify_by_pspec, notify_signal notify_signal, property(property_name : String, value : _) : Nil property, qdata(quark : UInt32) : Pointer(Void) | Nil qdata, ref_count : UInt32 ref_count, run_dispose : Nil run_dispose, set_data(key : String, data : Pointer(Void) | Nil) : Nil set_data, set_property(property_name : String, value : _) : Nil set_property, steal_data(key : String) : Pointer(Void) | Nil steal_data, steal_qdata(quark : UInt32) : Pointer(Void) | Nil steal_qdata, thaw_notify : Nil thaw_notify, to_unsafe : Pointer(Void) to_unsafe, watch_closure(closure : GObject::Closure) : Nil watch_closure

Constructor methods inherited from class GObject::Object

cast(obj : GObject::Object) : self cast, new(pointer : Pointer(Void), transfer : GICrystal::Transfer)
new
new
, newv(object_type : UInt64, parameters : Enumerable(GObject::Parameter)) : self newv

Class methods inherited from class GObject::Object

cast?(obj : GObject::Object) : self | Nil cast?, compat_control(what : UInt64, data : Pointer(Void) | Nil) : UInt64 compat_control, g_type : UInt64 g_type, interface_find_property(g_iface : GObject::TypeInterface, property_name : String) : GObject::ParamSpec interface_find_property, interface_list_properties(g_iface : GObject::TypeInterface) : Enumerable(GObject::ParamSpec) interface_list_properties

Macros inherited from class GObject::Object

previous_vfunc(*args) previous_vfunc, previous_vfunc!(*args) previous_vfunc!, signal(signature) signal

Constructor Detail

def self.new #

Initialize a new StyleContext.


[View source]
def self.new(*, display : Gdk::Display | Nil = nil) #

[View source]

Class Method Detail

def self.add_provider_for_display(display : Gdk::Display, provider : Gtk::StyleProvider, priority : UInt32) : Nil #

Adds a global style provider to display, which will be used in style construction for all Gtk::StyleContexts under display.

GTK uses this to make styling information from Gtk::Settings available.

Note: If both priorities are the same, A Gtk::StyleProvider added through Gtk::StyleContext#add_provider takes precedence over another added through this function.


[View source]
def self.g_type : UInt64 #

Returns the type id (GType) registered in GLib type system.


[View source]
def self.remove_provider_for_display(display : Gdk::Display, provider : Gtk::StyleProvider) : Nil #

Removes provider from the global style providers list in display.


[View source]

Instance Method Detail

def ==(other : self) #
Description copied from class Reference

Returns true if this reference is the same as other. Invokes same?.


def add_class(class_name : String) : Nil #

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 Gtk::Entry defining a “search” class, would be matched by:

WARNING ⚠️ The following code is in css ⚠️

entry.search { ... }

While any widget defining a “search” class would be matched by:

WARNING ⚠️ The following code is in css ⚠️

.search { ... }

[View source]
def add_provider(provider : Gtk::StyleProvider, priority : UInt32) : Nil #

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 Gtk::StyleContext#add_provider_for_display.

Note: If both priorities are the same, a Gtk::StyleProvider added through this function takes precedence over another added through Gtk::StyleContext#add_provider_for_display.


[View source]
def border : Gtk::Border #

Gets the border for a given state as a Gtk::Border.


[View source]
def color : Gdk::RGBA #

Gets the foreground color for a given state.


[View source]
def display : Gdk::Display #

Returns the Gdk::Display to which context is attached.


[View source]
def display=(display : Gdk::Display) : Nil #

Attaches context to the given display.

The display is used to add style information from “global” style providers, such as the display's Gtk::Settings instance.

If you are using a Gtk::StyleContext returned from Gtk::Widget#style_context, you do not need to call this yourself.


[View source]
def display=(value : Gdk::Display | Nil) : Gdk::Display | Nil #

[View source]
def has_class(class_name : String) : Bool #

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


[View source]
def hash(hasher) #
Description copied from class Reference

See Object#hash(hasher)


def lookup_color(color_name : String) : Gdk::RGBA #

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


[View source]
def margin : Gtk::Border #

Gets the margin for a given state as a Gtk::Border.


[View source]
def padding : Gtk::Border #

Gets the padding for a given state as a Gtk::Border.


[View source]
def remove_class(class_name : String) : Nil #

Removes class_name from context.


[View source]
def remove_provider(provider : Gtk::StyleProvider) : Nil #

Removes provider from the style providers list in context.


[View source]
def restore : Nil #

Restores context state to a previous stage.

See Gtk::StyleContext#save.


[View source]
def save : Nil #

Saves the context state.

This allows temporary modifications done through Gtk::StyleContext#add_class, Gtk::StyleContext#remove_class, Gtk::StyleContext#state= to be quickly reverted in one go through Gtk::StyleContext#restore.

The matching call to Gtk::StyleContext#restore must be done before GTK returns to the main loop.


[View source]
def scale : Int32 #

Returns the scale used for assets.


[View source]
def scale=(scale : Int32) : Nil #

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


[View source]
def state : Gtk::StateFlags #

Returns the state used for style matching.

This method should only be used to retrieve the Gtk::StateFlags to pass to Gtk::StyleContext methods, like Gtk::StyleContext#padding. If you need to retrieve the current state of a Gtk::Widget, use Gtk::Widget#state_flags.


[View source]
def state=(flags : Gtk::StateFlags) : Nil #

Sets the state to be used for style matching.


[View source]
def to_string(flags : Gtk::StyleContextPrintFlags) : String #

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.


[View source]