Class StyleContext

All Implemented Interfaces:
Proxy

@Generated("org.javagi.JavaGI") @Deprecated public class StyleContext extends GObject
Deprecated.
The relevant API has been moved to Widget where applicable; otherwise, there is no replacement for querying the style machinery. Stylable UI elements should use widgets.

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(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.

  • Constructor Details

    • StyleContext

      public StyleContext(MemorySegment address)
      Deprecated.
      Create a StyleContext instance for the provided memory address.
      Parameters:
      address - the memory address of the native object
    • StyleContext

      public StyleContext()
      Deprecated.
      Create a new StyleContext.
  • Method Details

    • getType

      public static @Nullable Type getType()
      Deprecated.
      Get the GType of the StyleContext class.
      Returns:
      the GType
    • getMemoryLayout

      public static MemoryLayout getMemoryLayout()
      Deprecated.
      The memory layout of the native struct.
      Returns:
      the memory layout
    • asParent

      protected StyleContext asParent()
      Deprecated.
      Return this instance as if it were its parent type. Comparable to the Java super keyword, but ensures the parent typeclass is also used in native code.
      Overrides:
      asParent in class GObject
      Returns:
      the instance as if it were its parent type
    • addClass

      @Deprecated public void addClass(String className)
      Deprecated.

      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 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 public void addProvider(StyleProvider provider, int priority)
      Deprecated.
      Use style classes instead

      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 this StyleContext belongs. If you want to affect the style of all widgets, use StyleContext.addProviderForDisplay.

      Note: If both priorities are the same, a GtkStyleProvider added through this function takes precedence over another added through StyleContext.addProviderForDisplay.

      Parameters:
      provider - a GtkStyleProvider
      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 between GTK_STYLE_PROVIDER_PRIORITY_FALLBACK and GTK_STYLE_PROVIDER_PRIORITY_USER
    • getBorder

      @Deprecated public void getBorder(Border border)
      Deprecated.
      This api will be removed in GTK 5
      Gets the border for a given state as a GtkBorder.
      Parameters:
      border - return value for the border settings
    • getColor

      @Deprecated public void getColor(RGBA color)
      Deprecated.
      Gets the foreground color for a given state.
      Parameters:
      color - return value for the foreground color
    • getDisplay

      @Deprecated public Display getDisplay()
      Deprecated.
      Returns the GdkDisplay to which this StyleContext is attached.
      Returns:
      a GdkDisplay.
    • getMargin

      @Deprecated public void getMargin(Border margin)
      Deprecated.
      This api will be removed in GTK 5
      Gets the margin for a given state as a GtkBorder.
      Parameters:
      margin - return value for the margin settings
    • getPadding

      @Deprecated public void getPadding(Border padding)
      Deprecated.
      This api will be removed in GTK 5
      Gets the padding for a given state as a GtkBorder.
      Parameters:
      padding - return value for the padding settings
    • getScale

      @Deprecated public int getScale()
      Deprecated.
      Returns the scale used for assets.
      Returns:
      the scale
    • getState

      @Deprecated public Set<StateFlags> getState()
      Deprecated.

      Returns the state used for style matching.

      This method should only be used to retrieve the GtkStateFlags to pass to GtkStyleContext methods, like getPadding(Border). If you need to retrieve the current state of a GtkWidget, use Widget.getStateFlags().

      Returns:
      the state flags
    • hasClass

      @Deprecated public boolean hasClass(String className)
      Deprecated.
      Returns true if this StyleContext currently has defined the given class name.
      Parameters:
      className - a class name
      Returns:
      true if this StyleContext has className defined
    • lookupColor

      @Deprecated public boolean lookupColor(String colorName, RGBA color)
      Deprecated.
      This api will be removed in GTK 5
      Looks up and resolves a color name in the this StyleContext color map.
      Parameters:
      colorName - color name to lookup
      color - Return location for the looked up color
      Returns:
      true if colorName was found and resolved, false otherwise
    • removeClass

      @Deprecated public void removeClass(String className)
      Deprecated.
      Removes className from context.
      Parameters:
      className - class name to remove
    • removeProvider

      @Deprecated public void removeProvider(StyleProvider provider)
      Deprecated.
      Removes provider from the style providers list in context.
      Parameters:
      provider - a GtkStyleProvider
    • restore

      @Deprecated public void restore()
      Deprecated.
      This API will be removed in GTK 5

      Restores this StyleContext state to a previous stage.

      See save().

    • save

      @Deprecated public void save()
      Deprecated.
      This API will be removed in GTK 5

      Saves the this StyleContext state.

      This allows temporary modifications done through addClass(String), removeClass(String), setState(Set) to be quickly reverted in one go through restore().

      The matching call to restore() must be done before GTK returns to the main loop.

    • setDisplay

      @Deprecated public void setDisplay(Display display)
      Deprecated.
      You should not use this api

      Attaches 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 from Widget.getStyleContext(), you do not need to call this yourself.

      Parameters:
      display - a GdkDisplay
    • setScale

      @Deprecated public void setScale(int scale)
      Deprecated.
      You should not use this api
      Sets the scale to use when getting image assets for the style.
      Parameters:
      scale - scale
    • setState

      @Deprecated public void setState(Set<StateFlags> flags)
      Deprecated.
      You should not use this api
      Sets the state to be used for style matching.
      Parameters:
      flags - state to represent
    • setState

      @Deprecated public void setState(StateFlags... flags)
      Deprecated.
      You should not use this api
      Sets the state to be used for style matching.
      Parameters:
      flags - state to represent
    • toString

      @Deprecated public String toString(Set<StyleContextPrintFlags> flags)
      Deprecated.
      This api will be removed in GTK 5

      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.

      Parameters:
      flags - Flags that determine what to print
      Returns:
      a newly allocated string representing this StyleContext
    • toString

      @Deprecated public String toString(StyleContextPrintFlags... flags)
      Deprecated.
      This api will be removed in GTK 5

      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.

      Parameters:
      flags - Flags that determine what to print
      Returns:
      a newly allocated string representing this StyleContext
    • changed

      protected void changed()
      Deprecated.
    • builder

      public static StyleContext.Builder<? extends StyleContext.Builder> builder()
      Deprecated.
      A StyleContext.Builder object constructs a StyleContext with the specified properties. Use the various set...() methods to set properties, and finish construction with StyleContext.Builder.build().
      Returns:
      the builder object