Load Gif into Gtk 3 Window C\C++

1.5k views Asked by At

I'm trying to create a c application with code::block that use Gtk 3 as a graphics library This version of gtk is a bit different from gtk 2 and not so much tutorials or exampes are avaiable online. I want to use a type named GdkPixmap that permit to load gifs into your windows, after googled it i have found that the type is deprecated and have been eliminated from gtk new version (3). From official documentation (migrate from gtk2 to gtk3) i have found that is possible to use cairo functions to make the works made by GdkPixmap but i havent found how change and migrate.

This code founded on stackoverflow is an example of loading gif into window using gtk2 that doesnt work on gtk3 compiler said : error unknown type name 'GdkPixmap'

#include <gtk/gtk.h>
#include <stdio.h>
#include <stdlib.h>

GdkPixbuf *load_pixbuf_from_file (const char *filename)
{
    GError *error = NULL;
    GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file (filename, &error);

    if (pixbuf == NULL)
    {
        g_print ("Error loading file: %d : %s\n", error->code, error->message);
        g_error_free (error);
        exit (1);
    }
    return pixbuf;
}

GdkPixbufAnimation *load_pixbuf_animation_from_file (const char *filename)
{
    GError *error = NULL;
    GdkPixbufAnimation *pixbuf = gdk_pixbuf_animation_new_from_file (filename, &error);

    if (pixbuf == NULL)
    {
        g_print ("Error loading file: %d : %s\n", error->code, error->message);
        g_error_free (error);
        exit (1);
    }
    return pixbuf;
}

int main (int argc, char **argv)
{
    GtkWidget *window = NULL;
    GdkPixbuf *image = NULL;
    GdkPixbufAnimation * anim = NULL;
    GtkWidget *widget = NULL;
    GdkPixmap *background = NULL;
    GtkStyle *style = NULL;

    gtk_init (&argc, &argv);
    /* Load a non animated gif */
    image = load_pixbuf_from_file ("C://Users//Pcc//Downloads//66.gif");
    //  widget = gtk_image_new_from_pixbuf (image);
    gdk_pixbuf_render_pixmap_and_mask (image, &background, NULL, 0);
    style = gtk_style_new ();
    style->bg_pixmap [0] = background;
    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title (GTK_WINDOW(window), "Load Image");
    gtk_window_set_default_size (GTK_WINDOW (window), 400, 300);
    gtk_widget_set_style (GTK_WIDGET(window), GTK_STYLE (style));
    gtk_window_set_transient_for (GTK_WINDOW (window), NULL);

    GtkWidget *hbox = NULL;
    hbox = gtk_hbox_new (0, FALSE);
    gtk_container_add (GTK_CONTAINER(window), hbox);

    GtkWidget *button = NULL;
    button = gtk_button_new_with_label ("Sonic");
    gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);

    gtk_widget_show_all (window);
    gtk_main ();
    return 0;
}

Use and install a obsolete gtk i dont think is good Please to post examples of code that works on gtk3 that load gifs

1

There are 1 answers

2
liberforce On

There's a specific GdkPixmap to Cairo migration guide right inside the official GTK 3 documentation.