Class Picture

All Implemented Interfaces:
PointerInterface

public class Picture extends Widget
The `GtkPicture` widget displays a `GdkPaintable`.

![An example GtkPicture](picture.png)

Many convenience functions are provided to make pictures simple to use.
For example, if you want to load an image from a file, and then display
it, there’s a convenience function to do this:

```c
GtkWidget *widget = gtk_picture_new_for_filename ("myfile.png");
```

If the file isn’t loaded successfully, the picture will contain a
“broken image” icon similar to that used in many web browsers.
If you want to handle errors in loading the file yourself,
for example by displaying an error message, then load the image with
[ctor@Gdk.Texture.new_from_file], then create the `GtkPicture` with
[ctor@Gtk.Picture.new_for_paintable].

Sometimes an application will want to avoid depending on external data
files, such as image files. See the documentation of `GResource` for details.
In this case, [ctor@Gtk.Picture.new_for_resource] and
[method@Gtk.Picture.set_resource] should be used.

`GtkPicture` displays an image at its natural size. See [class@Gtk.Image]
if you want to display a fixed-size image, such as an icon.

## Sizing the paintable

You can influence how the paintable is displayed inside the `GtkPicture`
by changing [property@Gtk.Picture:content-fit]. See [enum@Gtk.ContentFit]
for details. [property@Gtk.Picture:can-shrink] can be unset to make sure
that paintables are never made smaller than their ideal size - but
be careful if you do not know the size of the paintable in use (like
when displaying user-loaded images). This can easily cause the picture to
grow larger than the screen. And [property@GtkWidget:halign] and
[property@GtkWidget:valign] can be used to make sure the paintable doesn't
fill all available space but is instead displayed at its original size.

## CSS nodes

`GtkPicture` has a single CSS node with the name `picture`.

## Accessibility

`GtkPicture` uses the `GTK_ACCESSIBLE_ROLE_IMG` role.

https://docs.gtk.org/gtk4/class.Picture.html

  • Constructor Details

    • Picture

      public Picture(PointerContainer pointer)
    • Picture

      public Picture()
      Creates a new empty `GtkPicture` widget.
  • Method Details

    • getClassHandler

      public static ClassHandler getClassHandler()
    • newForFilePicture

      public static Picture newForFilePicture(@Nullable File file)
      Creates a new `GtkPicture` displaying the given @file.

      If the file isn’t found or can’t be loaded, the resulting
      `GtkPicture` is empty.

      If you need to detect failures to load the file, use
      [ctor@Gdk.Texture.new_from_file] to load the file yourself,
      then create the `GtkPicture` from the texture.
      Parameters:
      file - a `GFile`
      Returns:
      a new `GtkPicture`
    • newForFilenamePicture

      public static Picture newForFilenamePicture(@Nullable Str filename)
      Creates a new `GtkPicture` displaying the file @filename.

      This is a utility function that calls [ctor@Gtk.Picture.new_for_file].
      See that function for details.
      Parameters:
      filename - a filename
      Returns:
      a new `GtkPicture`
    • newForFilenamePicture

      public static Picture newForFilenamePicture(String filename)
      Creates a new `GtkPicture` displaying the file @filename.

      This is a utility function that calls [ctor@Gtk.Picture.new_for_file].
      See that function for details.
      Parameters:
      filename - a filename
      Returns:
      a new `GtkPicture`
    • newForPaintablePicture

      public static Picture newForPaintablePicture(@Nullable Paintable paintable)
      Creates a new `GtkPicture` displaying @paintable.

      The `GtkPicture` will track changes to the @paintable and update
      its size and contents in response to it.
      Parameters:
      paintable - a `GdkPaintable`
      Returns:
      a new `GtkPicture`
    • newForPixbufPicture

      public static Picture newForPixbufPicture(@Nullable Pixbuf pixbuf)
      Creates a new `GtkPicture` displaying @pixbuf.

      This is a utility function that calls [ctor@Gtk.Picture.new_for_paintable],
      See that function for details.

      The pixbuf must not be modified after passing it to this function.
      Parameters:
      pixbuf - a `GdkPixbuf`
      Returns:
      a new `GtkPicture`
    • newForResourcePicture

      public static Picture newForResourcePicture(@Nullable Str resource_path)
      Creates a new `GtkPicture` displaying the resource at @resource_path.

      This is a utility function that calls [ctor@Gtk.Picture.new_for_file].
      See that function for details.
      Parameters:
      resource_path - resource path to play back
      Returns:
      a new `GtkPicture`
    • newForResourcePicture

      public static Picture newForResourcePicture(String resource_path)
      Creates a new `GtkPicture` displaying the resource at @resource_path.

      This is a utility function that calls [ctor@Gtk.Picture.new_for_file].
      See that function for details.
      Parameters:
      resource_path - resource path to play back
      Returns:
      a new `GtkPicture`
    • getAlternativeText

      public Str getAlternativeText()
      Gets the alternative textual description of the picture.

      The returned string will be %NULL if the picture cannot be described textually.
      Returns:
      the alternative textual description of @self.
    • getCanShrink

      public boolean getCanShrink()
      Returns whether the `GtkPicture` respects its contents size.
      Returns:
      %TRUE if the picture can be made smaller than its contents
    • getContentFit

      public int getContentFit()
      Returns the fit mode for the content of the `GtkPicture`.

      See [enum@Gtk.ContentFit] for details.
      Returns:
      the content fit mode
    • getFile

      public File getFile()
      Gets the `GFile` currently displayed if @self is displaying a file.

      If @self is not displaying a file, for example when
      [method@Gtk.Picture.set_paintable] was used, then %NULL is returned.
      Returns:
      The `GFile` displayed by @self.
    • getPaintable

      public Paintable getPaintable()
      Gets the `GdkPaintable` being displayed by the `GtkPicture`.
      Returns:
      the displayed paintable
    • setAlternativeText

      public void setAlternativeText(@Nullable Str alternative_text)
      Sets an alternative textual description for the picture contents.

      It is equivalent to the "alt" attribute for images on websites.

      This text will be made available to accessibility tools.

      If the picture cannot be described textually, set this property to %NULL.
      Parameters:
      alternative_text - a textual description of the contents
    • setAlternativeText

      public void setAlternativeText(String alternative_text)
      Sets an alternative textual description for the picture contents.

      It is equivalent to the "alt" attribute for images on websites.

      This text will be made available to accessibility tools.

      If the picture cannot be described textually, set this property to %NULL.
      Parameters:
      alternative_text - a textual description of the contents
    • setCanShrink

      public void setCanShrink(boolean can_shrink)
      If set to %TRUE, the @self can be made smaller than its contents.

      The contents will then be scaled down when rendering.

      If you want to still force a minimum size manually, consider using
      [method@Gtk.Widget.set_size_request].

      Also of note is that a similar function for growing does not exist
      because the grow behavior can be controlled via
      [method@Gtk.Widget.set_halign] and [method@Gtk.Widget.set_valign].
      Parameters:
      can_shrink - if @self can be made smaller than its contents
    • setContentFit

      public void setContentFit(int content_fit)
      Sets how the content should be resized to fit the `GtkPicture`.

      See [enum@Gtk.ContentFit] for details.
      Parameters:
      content_fit - the content fit mode
    • setFile

      public void setFile(@Nullable File file)
      Makes @self load and display @file.

      See [ctor@Gtk.Picture.new_for_file] for details.
      Parameters:
      file - a `GFile`
    • setFilename

      public void setFilename(@Nullable Str filename)
      Makes @self load and display the given @filename.

      This is a utility function that calls [method@Gtk.Picture.set_file].
      Parameters:
      filename - the filename to play
    • setFilename

      public void setFilename(String filename)
      Makes @self load and display the given @filename.

      This is a utility function that calls [method@Gtk.Picture.set_file].
      Parameters:
      filename - the filename to play
    • setPaintable

      public void setPaintable(@Nullable Paintable paintable)
      Makes @self display the given @paintable.

      If @paintable is %NULL, nothing will be displayed.

      See [ctor@Gtk.Picture.new_for_paintable] for details.
      Parameters:
      paintable - a `GdkPaintable`
    • setPixbuf

      public void setPixbuf(@Nullable Pixbuf pixbuf)
      Sets a `GtkPicture` to show a `GdkPixbuf`.

      See [ctor@Gtk.Picture.new_for_pixbuf] for details.

      This is a utility function that calls [method@Gtk.Picture.set_paintable].
      Parameters:
      pixbuf - a `GdkPixbuf`
    • setResource

      public void setResource(@Nullable Str resource_path)
      Makes @self load and display the resource at the given
      @resource_path.

      This is a utility function that calls [method@Gtk.Picture.set_file].
      Parameters:
      resource_path - the resource to set
    • setResource

      public void setResource(String resource_path)
      Makes @self load and display the resource at the given
      @resource_path.

      This is a utility function that calls [method@Gtk.Picture.set_file].
      Parameters:
      resource_path - the resource to set
    • asAccessible

      public Accessible asAccessible()
      Implements interface Accessible. Call this to get access to interface functions.
      Overrides:
      asAccessible in class Widget
      Returns:
      Accessible
    • asBuildable

      public Buildable asBuildable()
      Implements interface Buildable. Call this to get access to interface functions.
      Overrides:
      asBuildable in class Widget
      Returns:
      Buildable
    • asConstraintTarget

      public ConstraintTarget asConstraintTarget()
      Implements interface ConstraintTarget. Call this to get access to interface functions.
      Overrides:
      asConstraintTarget in class Widget
      Returns:
      ConstraintTarget
    • getTypeID

      public static long getTypeID()
    • getParentTypeID

      public static long getParentTypeID()
    • getTypeSize

      public static TypeSystem.TypeSize getTypeSize()
    • getParentTypeSize

      public static TypeSystem.TypeSize getParentTypeSize()
    • getInstanceSize

      public static int getInstanceSize()