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
StyleContext#addProviderForDisplay. 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
Nested ClassesModifier and TypeClassDescriptionstatic classStyleContext.Builder<B extends StyleContext.Builder<B>>Deprecated.Inner class implementing a builder pattern to construct a GObject with properties.static classDeprecated.Nested classes/interfaces inherited from class org.gnome.gobject.GObject
GObject.NotifyCallback, GObject.ObjectClass -
Constructor Summary
ConstructorsConstructorDescriptionDeprecated.Creates a new StyleContext.StyleContext(MemorySegment address) Deprecated.Create a StyleContext proxy instance for the provided memory address. -
Method Summary
Modifier and TypeMethodDescriptionvoidDeprecated.UseWidget.addCssClass(java.lang.String)insteadvoidaddProvider(StyleProvider provider, int priority) Deprecated.Use style classes insteadprotected StyleContextasParent()Deprecated.Returns this instance as if it were its parent type.static StyleContext.Builder<? extends StyleContext.Builder> builder()Deprecated.AStyleContext.Builderobject constructs aStyleContextwith the specified properties.protected voidchanged()Deprecated.voidDeprecated.This api will be removed in GTK 5voidDeprecated.UseWidget.getColor(org.gnome.gdk.RGBA)insteadDeprecated.UseWidget.getDisplay()insteadvoidDeprecated.This api will be removed in GTK 5static MemoryLayoutDeprecated.The memory layout of the native struct.voidgetPadding(Border padding) Deprecated.This api will be removed in GTK 5intgetScale()Deprecated.UseWidget.getScaleFactor()insteadgetState()Deprecated.UseWidget.getStateFlags()insteadstatic @Nullable TypegetType()Deprecated.Get the GType of the StyleContext classbooleanDeprecated.UseWidget.hasCssClass(java.lang.String)insteadbooleanlookupColor(String colorName, RGBA color) Deprecated.This api will be removed in GTK 5voidremoveClass(String className) Deprecated.UseWidget.removeCssClass(java.lang.String)insteadvoidremoveProvider(StyleProvider provider) Deprecated.voidrestore()Deprecated.This API will be removed in GTK 5voidsave()Deprecated.This API will be removed in GTK 5voidsetDisplay(Display display) Deprecated.You should not use this apivoidsetScale(int scale) Deprecated.You should not use this apivoidsetState(Set<StateFlags> flags) Deprecated.You should not use this apivoidsetState(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, 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, withPropertiesMethods inherited from class org.gnome.gobject.TypeInstance
callParent, callParent, cast, getPrivate, readGClass, writeGClassMethods inherited from class org.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
-
StyleContext
public StyleContext()Deprecated.Creates a new StyleContext.
-
-
Method Details
-
getType
Deprecated.Get the GType of the StyleContext class- Returns:
- the GType
-
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 Javasuperkeyword, 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. -
addClass
Deprecated.UseWidget.addCssClass(java.lang.String)insteadAdds a style class tocontext,so later uses of the style context will make use of this new class for styling.In the CSS file format, a
GtkEntrydefining 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 tocontext,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
StyleContext#addProviderForDisplay.Note: If both priorities are the same, a
GtkStyleProvideradded through this function takes precedence over another added throughStyleContext#addProviderForDisplay.- Parameters:
provider- aGtkStyleProviderpriority- 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_FALLBACKandGTK_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 theGdkDisplayto 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
GtkStateFlagsto pass toGtkStyleContextmethods, 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)insteadReturnstrueif this StyleContext currently has defined the given class name.- Parameters:
className- a class name- Returns:
trueif this StyleContext hasclassNamedefined
-
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:
trueifcolorNamewas found and resolved,falseotherwise
-
removeClass
Deprecated.UseWidget.removeCssClass(java.lang.String)insteadRemovesclassNamefromcontext.- Parameters:
className- class name to remove
-
removeProvider
Deprecated.Removesproviderfrom the style providers list incontext.- 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
GtkSettingsinstance.If you are using a
GtkStyleContextreturned 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
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.
- 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
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.
- Parameters:
flags- Flags that determine what to print- Returns:
- a newly allocated string representing this StyleContext
-
changed
protected void changed()Deprecated. -
builder
Deprecated.AStyleContext.Builderobject constructs aStyleContextwith the specified properties. Use the variousset...()methods to set properties, and finish construction withStyleContext.Builder.build().- Returns:
- the builder object
-
Widgetwhere applicable; otherwise, there is no replacement for querying the style machinery. Stylable UI elements should use widgets.