4

我有一个加载动画文件矩阵的GTK+应用程序。它们在加载时自动运行动画,然后在动画完成时停止。我将如何重新启动每个包含和 动画完成时生成的信号的动画?CGIFGtkImagesGtkImageGIF

谢谢你。

编辑:这里描述
的使用是否可以做到这一点?gdk_pixbuf_animation_get_iter()

下面提供了完整的代码。

/*
 * Compile me with:
 *   gcc -o reels reels.c $(pkg-config --cflags --libs gtk+-2.0 gmodule-2.0)
 */

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <time.h>

/* GTK */
#include <gdk/gdkx.h>
#include <gtk/gtk.h>

/**** prototypes ****/

static void destroy (GtkWidget*, gpointer);
GdkPixbuf *create_pixbuf(const gchar * filename);
GtkWidget *SetupWindow(gchar *data, const gchar *filename);
static void destroy (GtkWidget *window, gpointer data);
void btnSpin_clicked(GtkWidget *button, gpointer data);
void btnExit_clicked(GtkWidget *button, gpointer data);

/********************/

GtkWidget   *images[3][5];

static void destroy (GtkWidget *window, gpointer data)
{
  gtk_main_quit ();
}

void btnSpin_clicked(GtkWidget *button, gpointer data)
{
    printf("Spin Button pressed.\n");

    return;
}

void btnExit_clicked(GtkWidget *button, gpointer data)
{
    gtk_main_quit();

    return;
}


GtkWidget *SetupWindow(gchar *data, const gchar *filename)
{
    GdkPixmap *background;
    GdkPixbuf *pixbuf;
    GdkScreen *ourscreen;
    GdkColormap *colormap;
    GtkStyle *style;
    GdkColor fg;
    GdkColor bg;
    GError *error = NULL;
    GdkRectangle *rect;
    GtkWidget *window;

    pixbuf = gdk_pixbuf_new_from_file (filename,&error);
    if (error != NULL) {
        if (error->domain == GDK_PIXBUF_ERROR) {
            g_print ("Pixbuf Related Error:\n");
        }
        if (error->domain == G_FILE_ERROR) {
            g_print ("File Error: Check file permissions and state:\n");
        }

        g_printerr ("%s\n", error[0].message);
    }

    gdk_pixbuf_render_pixmap_and_mask (pixbuf, &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), data);
    // gtk_window_maximize(GTK_WINDOW(window));
    gtk_window_set_modal (GTK_WINDOW (window),TRUE);
    gtk_window_set_default_size(GTK_WINDOW(window),628,530);
    gtk_widget_set_style (GTK_WIDGET(window), GTK_STYLE(style));
    gtk_window_set_position(GTK_WINDOW(window),GTK_WIN_POS_CENTER_ALWAYS);
    gtk_container_set_border_width(GTK_CONTAINER(window), 0);
    //gtk_window_set_resizable(GTK_WINDOW(window), (gboolean) FALSE);
    gtk_window_set_decorated( GTK_WINDOW(window), FALSE );    

    return(window);
}

int main (int argc, char *argv[])
{
    GdkPixbufAnimation *animation;
    GtkWidget *image;
    int x,y;    
    GdkPixbuf *pixBuf;
    GdkPixmap *pixMap;
    gchar filename[20];
    GtkWidget *btnSpin, *btnExit;

    GtkWidget *frame;       /* for absolute positionining of widgets */
    GtkWidget *window;
    int posx, posy;

    gtk_init (&argc, &argv);

    window = SetupWindow("Demo", "background.gif");
    g_signal_connect (G_OBJECT (window), "destroy", G_CALLBACK (destroy), NULL);

    frame = gtk_fixed_new();
    gtk_container_add(GTK_CONTAINER(window), frame);

    btnSpin = gtk_button_new_with_label("Spin");
    gtk_widget_set_size_request(btnSpin, 80, 35);
    gtk_fixed_put(GTK_FIXED(frame), btnSpin, 229, 485);
    g_signal_connect(G_OBJECT( btnSpin ), "clicked", G_CALLBACK(btnSpin_clicked), NULL );

    btnExit = gtk_button_new_with_label("Exit");
    gtk_widget_set_size_request(btnExit, 80, 35);
    gtk_fixed_put(GTK_FIXED(frame), btnExit, 320, 485);
    g_signal_connect(G_OBJECT( btnExit ), "clicked", G_CALLBACK(btnExit_clicked), NULL );

    /* setup animated gifs */
    for( y = 0; y < 3; y++ )
    {
        posy = (y*120) + 20;
        for( x = 0; x < 5; x++ )
        {
            posx = (x*120) + 20;
            /* set each Image widget to spin GIF */
            sprintf( filename,"%d-%d.gif", y+1,x+1 );
            images[y][x] = gtk_image_new_from_file(filename);
            gtk_fixed_put(GTK_FIXED(frame), images[y][x], posx, posy);
        }
    }

    gtk_widget_show_all(window);

    gtk_main ();

    return 0;
}
4

2 回答 2

4

看了GtkImage 源码后,恐怕动画完成时没有产生信号。但是,您应该能够通过调用gtk_image_set_from_file()或重新启动动画gtk_image_set_from_animation()

为了彻底解决你最初的问题,我建议你创建一个 GtkImage 的子类。它的行为应该与 GtkImage 完全一样,除了应该在第 1315 行附近animation_timeout发送信号。delay < 0

有关创建 GObject 子类的信息(请注意,GtkImage 是 GObject)可以在此处找到。

于 2012-06-04T17:19:02.007 回答
0

根据 user1202136 的回答,以下代码片段显示了解决方案。我发布这个以防其他人发现它有用。

基本思想是使用GdkPixbufAnimation,gdk_pixbuf_animation_new_from_filegtk_image_set_from_animation

以下代码片段显示了如何执行此操作。

GtkWidget *images[3][5];
GdkPixbufAnimation *animations[3][5];

/* Initial setup of animated gifs */
for( y = 0; y < 3; y++ )
{
    for( x = 0; x < 5; x++ )
    {
        /* set each Image widget to spin GIF */
        sprintf( filename,"%d-%d.gif", y+1,x+1 );
        images[y][x] = gtk_image_new();
        animations[y][x] = gdk_pixbuf_animation_new_from_file ( filename , &error);
        gtk_image_set_from_animation (GTK_IMAGE(images[y][x]), animations[y][x]);
        gtk_fixed_put(GTK_FIXED(frame), images[y][x], (x*120) + 20, (y*120) + 20);
    }
}


/*  now to restart the animations use the images and animations array already stored in memory,
    no need to re-read the animations from disk so this happens quickly
*/

/* restart animated gifs */
for( y = 0; y < 3; y++ )
{
    for( x = 0; x < 5; x++ )
    {
        gtk_image_set_from_animation(GTK_IMAGE(images[y][x]), animations[y][x]);
    }
}
于 2012-06-07T18:49:58.957 回答