class Gtk::StyleContext
- Gtk::StyleContext
- GObject::Object
- Reference
- Object
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.crConstructors
-
.new
Initialize a new
StyleContext
. - .new(*, display : Gdk::Display? = nil)
Class Method Summary
-
.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. -
.g_type : UInt64
Returns the type id (GType) registered in GLib type system.
-
.remove_provider_for_display(display : Gdk::Display, provider : Gtk::StyleProvider) : Nil
Removes @provider from the global style providers list in @display.
Instance Method Summary
-
#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.
-
#add_provider(provider : Gtk::StyleProvider, priority : UInt32) : Nil
Adds a style provider to @context, to be used in style construction.
-
#border : Gtk::Border
Gets the border for a given state as a
Gtk::Border
. -
#color : Gdk::RGBA
Gets the foreground color for a given state.
-
#display : Gdk::Display
Returns the
Gdk::Display
to which @context is attached. -
#display=(display : Gdk::Display) : Nil
Attaches @context to the given display.
- #display=(value : Gdk::Display?) : Gdk::Display?
-
#has_class(class_name : String) : Bool
Returns
true
if @context currently has defined the given class name. -
#lookup_color(color_name : String) : Gdk::RGBA
Looks up and resolves a color name in the @context color map.
-
#margin : Gtk::Border
Gets the margin for a given state as a
Gtk::Border
. -
#padding : Gtk::Border
Gets the padding for a given state as a
Gtk::Border
. -
#remove_class(class_name : String) : Nil
Removes @class_name from @context.
-
#remove_provider(provider : Gtk::StyleProvider) : Nil
Removes @provider from the style providers list in @context.
-
#restore : Nil
Restores @context state to a previous stage.
-
#save : Nil
Saves the @context state.
-
#scale : Int32
Returns the scale used for assets.
-
#scale=(scale : Int32) : Nil
Sets the scale to use when getting image assets for the style.
-
#state : Gtk::StateFlags
Returns the state used for style matching.
-
#state=(flags : Gtk::StateFlags) : Nil
Sets the state to be used for style matching.
-
#to_string(flags : Gtk::StyleContextPrintFlags) : String
Converts the style context into a string representation.
Instance methods inherited from class GObject::Object
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)?
data,
finalize
finalize,
freeze_notify : Nil
freeze_notify,
getv(names : Enumerable(String), values : Enumerable(_)) : Nil
getv,
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)?
qdata,
ref_count : UInt32
ref_count,
run_dispose : Nil
run_dispose,
set_data(key : String, data : Pointer(Void)?) : Nil
set_data,
set_property(property_name : String, value : _) : Nil
set_property,
steal_data(key : String) : Pointer(Void)?
steal_data,
steal_qdata(quark : UInt32) : Pointer(Void)?
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,
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
compat_control(what : UInt64, data : Pointer(Void)?) : 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
Constructor Detail
Class Method Detail
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.
Removes @provider from the global style providers list in @display.
Instance Method Detail
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 { ... }
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
.
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.
Returns true
if @context currently has defined the
given class name.
Looks up and resolves a color name in the @context color map.
Removes @provider from the style providers list in @context.
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.
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
.
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.