Class StyleContext
- All Implemented Interfaces:
Proxy
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
addProvider(org.gnome.gtk.StyleProvider, int)
, or to the display through
addProviderForDisplay(org.gnome.gdk.Display, org.gnome.gtk.StyleProvider, int)
. The resulting
style is a combination of all providers’ information in priority order.
For GTK widgets, any GtkStyleContext
returned by
Widget.getStyleContext()
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.
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
StyleContext.Builder<B extends StyleContext.Builder<B>>
Deprecated.Inner class implementing a builder pattern to construct a GObject with properties.static class
Deprecated.Nested classes/interfaces inherited from class org.gnome.gobject.GObject
GObject.NotifyCallback, GObject.ObjectClass
-
Constructor Summary
ConstructorDescriptionStyleContext
(MemorySegment address) Deprecated.Create a StyleContext proxy instance for the provided memory address. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Deprecated.void
addProvider
(StyleProvider provider, int priority) Deprecated.Use style classes insteadstatic void
addProviderForDisplay
(Display display, StyleProvider provider, int priority) Deprecated.protected StyleContext
asParent()
Deprecated.Returns this instance as if it were its parent type.static StyleContext.Builder
<? extends StyleContext.Builder> builder()
Deprecated.AStyleContext.Builder
object constructs aStyleContext
with the specified properties.protected void
changed()
Deprecated.void
Deprecated.This api will be removed in GTK 5void
Deprecated.UseWidget.getColor(org.gnome.gdk.RGBA)
insteadDeprecated.UseWidget.getDisplay()
insteadvoid
Deprecated.This api will be removed in GTK 5static MemoryLayout
Deprecated.The memory layout of the native struct.void
getPadding
(Border padding) Deprecated.This api will be removed in GTK 5int
getScale()
Deprecated.UseWidget.getScaleFactor()
insteadgetState()
Deprecated.UseWidget.getStateFlags()
insteadstatic Type
getType()
Deprecated.Get the GType of the StyleContext classboolean
Deprecated.UseWidget.hasCssClass(java.lang.String)
insteadboolean
lookupColor
(String colorName, RGBA color) Deprecated.This api will be removed in GTK 5void
removeClass
(String className) Deprecated.UseWidget.removeCssClass(java.lang.String)
insteadvoid
removeProvider
(StyleProvider provider) Deprecated.static void
removeProviderForDisplay
(Display display, StyleProvider provider) Deprecated.void
restore()
Deprecated.This API will be removed in GTK 5void
save()
Deprecated.This API will be removed in GTK 5void
setDisplay
(Display display) Deprecated.You should not use this apivoid
setScale
(int scale) Deprecated.You should not use this apivoid
setState
(Set<StateFlags> flags) Deprecated.You should not use this apivoid
setState
(StateFlags... flags) Deprecated.You should not use this apitoString
(Set<StyleContextPrintFlags> flags) Deprecated.This api will be removed in GTK 5toString
(StyleContextPrintFlags... flags) Deprecated.This api will be removed in GTK 5Methods inherited from class org.gnome.gobject.GObject
addToggleRef, addWeakPointer, bindProperty, bindProperty, bindProperty, bindPropertyFull, bindPropertyFull, bindPropertyWithClosures, bindPropertyWithClosures, compatControl, connect, connect, connect, constructed, disconnect, dispatchPropertiesChanged, dispose, dupData, dupQdata, emit, emitNotify, finalize_, forceFloating, freezeNotify, get, getData, getProperty, getProperty, getProperty, getQdata, getv, interfaceFindProperty, interfaceInstallProperty, interfaceListProperties, isFloating, newInstance, newInstance, newInstance, newInstance, newv, notify_, notify_, notifyByPspec, onNotify, ref, refSink, removeToggleRef, removeWeakPointer, replaceData, replaceQdata, runDispose, set, setData, setDataFull, setProperty, setProperty, setProperty, setQdata, setQdataFull, setv, stealData, stealQdata, takeRef, thawNotify, unref, watchClosure, weakRef, weakUnref, withProperties
Methods inherited from class org.gnome.gobject.TypeInstance
callParent, callParent, getPrivate, readGClass, writeGClass
Methods inherited from class io.github.jwharm.javagi.base.ProxyInstance
equals, handle, hashCode
-
Constructor Details
-
StyleContext
Deprecated.Create a StyleContext proxy instance for the provided memory address.- Parameters:
address
- the memory address of the native object
-
-
Method Details
-
getType
-
getMemoryLayout
Deprecated.The memory layout of the native struct.- Returns:
- the memory layout
-
asParent
Deprecated.Returns this instance as if it were its parent type. This is mostly synonymous to the Javasuper
keyword, but will set the native typeclass function pointers to the parent type. When overriding a native virtual method in Java, "chaining up" withsuper.methodName()
doesn't work, because it invokes the overridden function pointer again. To chain up, callasParent().methodName()
. This will call the native function pointer of this virtual method in the typeclass of the parent type. -
addProviderForDisplay
@Deprecated public static void addProviderForDisplay(Display display, StyleProvider provider, int priority) Deprecated.Adds a global style provider todisplay
, which will be used in style construction for allGtkStyleContexts
underdisplay
.GTK uses this to make styling information from
GtkSettings
available.Note: If both priorities are the same, A
GtkStyleProvider
added throughaddProvider(org.gnome.gtk.StyleProvider, int)
takes precedence over another added through this function.- Parameters:
display
- aGdkDisplay
provider
- aGtkStyleProvider
priority
- the priority of the style provider. The lower it is, the earlier it will be used in the style construction. Typically this will be in the range betweenGTK_STYLE_PROVIDER_PRIORITY_FALLBACK
andGTK_STYLE_PROVIDER_PRIORITY_USER
-
removeProviderForDisplay
Deprecated.Removesprovider
from the global style providers list indisplay
.- Parameters:
display
- aGdkDisplay
provider
- aGtkStyleProvider
-
addClass
Deprecated.UseWidget.addCssClass(java.lang.String)
insteadAdds a style class to this StyleContext, 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 { ... }
- Parameters:
className
- class name to use in styling
-
addProvider
Deprecated.Use style classes insteadAdds a style provider to this StyleContext, to be used in style construction.Note that a style provider added by this function only affects the style of the widget to which this StyleContext belongs. If you want to affect the style of all widgets, use
addProviderForDisplay(org.gnome.gdk.Display, org.gnome.gtk.StyleProvider, int)
.Note: If both priorities are the same, a
GtkStyleProvider
added through this function takes precedence over another added throughaddProviderForDisplay(org.gnome.gdk.Display, org.gnome.gtk.StyleProvider, int)
.- Parameters:
provider
- aGtkStyleProvider
priority
- the priority of the style provider. The lower it is, the earlier it will be used in the style construction. Typically this will be in the range betweenGTK_STYLE_PROVIDER_PRIORITY_FALLBACK
andGTK_STYLE_PROVIDER_PRIORITY_USER
-
getBorder
Deprecated.This api will be removed in GTK 5Gets the border for a given state as aGtkBorder
.- Parameters:
border
- return value for the border settings
-
getColor
Deprecated.UseWidget.getColor(org.gnome.gdk.RGBA)
insteadGets the foreground color for a given state.- Parameters:
color
- return value for the foreground color
-
getDisplay
Deprecated.UseWidget.getDisplay()
insteadReturns theGdkDisplay
to which this StyleContext is attached.- Returns:
- a
GdkDisplay
.
-
getMargin
Deprecated.This api will be removed in GTK 5Gets the margin for a given state as aGtkBorder
.- Parameters:
margin
- return value for the margin settings
-
getPadding
Deprecated.This api will be removed in GTK 5Gets the padding for a given state as aGtkBorder
.- Parameters:
padding
- return value for the padding settings
-
getScale
Deprecated.UseWidget.getScaleFactor()
insteadReturns the scale used for assets.- Returns:
- the scale
-
getState
Deprecated.UseWidget.getStateFlags()
insteadReturns the state used for style matching.This method should only be used to retrieve the
GtkStateFlags
to pass toGtkStyleContext
methods, likegetPadding(org.gnome.gtk.Border)
. If you need to retrieve the current state of aGtkWidget
, useWidget.getStateFlags()
.- Returns:
- the state flags
-
hasClass
Deprecated.UseWidget.hasCssClass(java.lang.String)
insteadReturnstrue
if this StyleContext currently has defined the given class name.- Parameters:
className
- a class name- Returns:
true
if this StyleContext hasclassName
defined
-
lookupColor
Deprecated.This api will be removed in GTK 5Looks up and resolves a color name in the this StyleContext color map.- Parameters:
colorName
- color name to lookupcolor
- Return location for the looked up color- Returns:
true
ifcolorName
was found and resolved,false
otherwise
-
removeClass
Deprecated.UseWidget.removeCssClass(java.lang.String)
insteadRemovesclassName
from this StyleContext.- Parameters:
className
- class name to remove
-
removeProvider
Deprecated.Removesprovider
from the style providers list in this StyleContext.- Parameters:
provider
- aGtkStyleProvider
-
restore
Deprecated.This API will be removed in GTK 5Restores this StyleContext state to a previous stage.See
save()
. -
save
Deprecated.This API will be removed in GTK 5Saves the this StyleContext state.This allows temporary modifications done through
addClass(java.lang.String)
,removeClass(java.lang.String)
,setState(java.util.Set<org.gnome.gtk.StateFlags>)
to be quickly reverted in one go throughrestore()
.The matching call to
restore()
must be done before GTK returns to the main loop. -
setDisplay
Deprecated.You should not use this apiAttaches this StyleContext 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 fromWidget.getStyleContext()
, you do not need to call this yourself.- Parameters:
display
- aGdkDisplay
-
setScale
Deprecated.You should not use this apiSets the scale to use when getting image assets for the style.- Parameters:
scale
- scale
-
setState
Deprecated.You should not use this apiSets the state to be used for style matching.- Parameters:
flags
- state to represent
-
setState
Deprecated.You should not use this apiSets the state to be used for style matching.- Parameters:
flags
- state to represent
-
toString
Deprecated.This api will be removed in GTK 5Converts 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 this StyleContext. 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.
- Parameters:
flags
- Flags that determine what to print- Returns:
- a newly allocated string representing this StyleContext
-
toString
Deprecated.This api will be removed in GTK 5Converts 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 this StyleContext. 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.
- Parameters:
flags
- Flags that determine what to print- Returns:
- a newly allocated string representing this StyleContext
-
changed
protected void changed()Deprecated. -
builder
Deprecated.AStyleContext.Builder
object constructs aStyleContext
with the specified properties. Use the variousset...()
methods to set properties, and finish construction withStyleContext.Builder.build()
.
-
Widget.addCssClass(java.lang.String)
instead