Class CssProvider

All Implemented Interfaces:
StyleProvider, Proxy

@Generated("org.javagi.JavaGI") public class CssProvider extends GObject implements StyleProvider

A style provider for CSS.

It is able to parse CSS-like input in order to style widgets.

An application can make GTK parse a specific CSS style sheet by calling loadFromFile(File) or loadFromResource(String) and adding the provider with StyleContext.addProvider(StyleProvider, int) or StyleContext.addProviderForDisplay.

In addition, certain files will be read when GTK is initialized. First, the file $XDG_CONFIG_HOME/gtk-4.0/gtk.css is loaded if it exists. Then, GTK loads the first existing file among XDG_DATA_HOME/themes/THEME/gtk-VERSION/gtk-VARIANT.css, $HOME/.themes/THEME/gtk-VERSION/gtk-VARIANT.css, $XDG_DATA_DIRS/themes/THEME/gtk-VERSION/gtk-VARIANT.css and DATADIR/share/themes/THEME/gtk-VERSION/gtk-VARIANT.css, where THEME is the name of the current theme (see the Gtk.Settings:gtk-theme-name setting), VARIANT is the variant to load (see the Gtk.Settings:gtk-application-prefer-dark-theme setting), DATADIR is the prefix configured when GTK was compiled (unless overridden by the GTK_DATA_PREFIX environment variable), and VERSION is the GTK version number. If no file is found for the current version, GTK tries older versions all the way back to 4.0.

To track errors while loading CSS, connect to the Gtk.CssProvider::parsing-error signal.

  • Constructor Details

    • CssProvider

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

      public CssProvider()
      Create a new CssProvider.
  • Method Details

    • getType

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

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

      protected CssProvider asParent()
      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
    • loadFromBytes

      public void loadFromBytes(byte[] data)

      Loads data into cssProvider.

      This clears any previously loaded information.

      Parameters:
      data - GBytes containing the data to load
      Since:
      4.12
    • loadFromData

      @Deprecated public void loadFromData(String data, long length)
      Deprecated.

      Loads data into cssProvider.

      This clears any previously loaded information.

      Parameters:
      data - CSS data to be parsed
      length - the length of data in bytes, or -1 for NUL terminated strings
    • loadFromFile

      public void loadFromFile(File file)

      Loads the data contained in file into cssProvider.

      This clears any previously loaded information.

      Parameters:
      file - GFile pointing to a file to load
    • loadFromPath

      public void loadFromPath(String path)

      Loads the data contained in path into cssProvider.

      This clears any previously loaded information.

      Parameters:
      path - the path of a filename to load, in the GLib filename encoding
    • loadFromResource

      public void loadFromResource(String resourcePath)

      Loads the data contained in the resource at resourcePath into the cssProvider.

      This clears any previously loaded information.

      Parameters:
      resourcePath - a GResource resource path
    • loadFromString

      public void loadFromString(String string)

      Loads string into cssProvider.

      This clears any previously loaded information.

      Parameters:
      string - the CSS to load
      Since:
      4.12
    • loadNamed

      @Deprecated public void loadNamed(String name, @Nullable String variant)
      Deprecated.
      Using any of the other theme loaders, combine with media queries.

      Loads a theme from the usual theme paths.

      The actual process of finding the theme might change between releases, but it is guaranteed that this function uses the same mechanism to load the theme that GTK uses for loading its own theme.

      Parameters:
      name - A theme name
      variant - variant to load, for example, "dark", or null for the default
    • toString

      public String toString()

      Converts the this CssProvider into a string representation in CSS format.

      Using loadFromString(String) with the return value from this function on a new provider created with CssProvider() will basically create a duplicate of this provider.

      Overrides:
      toString in class Object
      Returns:
      a new string representing the provider.
    • onParsingError

      Signals that a parsing error occurred.

      The expected error values are in the Gtk.CssParserError and Gtk.CssParserWarning enumerations.

      The path, line and position describe the actual location of the error as accurately as possible.

      Parsing errors are never fatal, so the parsing will resume after the error. Errors may however cause parts of the given data or even all of it to not be parsed at all. So it is a useful idea to check that the parsing succeeds by connecting to this signal.

      Errors in the Gtk.CssParserWarning enumeration should not be treated as fatal errors.

      Note that this signal may be emitted at any time as the css provider may opt to defer parsing parts or all of the input to a later time than when a loading function was called.

      Parameters:
      handler - the signal handler
      Returns:
      a signal handler ID to keep track of the signal connection
      See Also:
    • emitParsingError

      public void emitParsingError(@Nullable CssSection section, @Nullable GError error)
      Emits the "parsing-error" signal. See onParsingError(CssProvider.ParsingErrorCallback).
    • builder

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