testanimation.c 12.1 KB
Newer Older
Jonathan Blandford's avatar
Jonathan Blandford committed
1 2 3 4 5

/* testpixbuf -- test program for gdk-pixbuf code
 * Copyright (C) 1999 Mark Crichton, Larry Ewing
 *
 * This library is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU Lesser General Public
Jonathan Blandford's avatar
Jonathan Blandford committed
7 8 9 10 11 12
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 * Lesser General Public License for more details.
Jonathan Blandford's avatar
Jonathan Blandford committed
14
 *
15
 * You should have received a copy of the GNU Lesser General Public
Javier Jardón's avatar
Javier Jardón committed
16
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
Jonathan Blandford's avatar
Jonathan Blandford committed
17 18
 */

19
#include "config.h"
20
#include <stdio.h>
Jonathan Blandford's avatar
Jonathan Blandford committed
21 22 23
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
Havoc Pennington's avatar
Havoc Pennington committed
24
#include <errno.h>
Jonathan Blandford's avatar
Jonathan Blandford committed
25
#include <gtk/gtk.h>
Havoc Pennington's avatar
Havoc Pennington committed
26 27 28 29 30 31 32 33 34 35

typedef struct _LoadContext LoadContext;

struct _LoadContext
{
  gchar *filename;
  GtkWidget *window;
  GdkPixbufLoader *pixbuf_loader;
  guint load_timeout;
  FILE* image_stream;
Jonathan Blandford's avatar
Jonathan Blandford committed
36 37 38
};

static void
Havoc Pennington's avatar
Havoc Pennington committed
39
destroy_context (gpointer data)
Jonathan Blandford's avatar
Jonathan Blandford committed
40
{
Havoc Pennington's avatar
Havoc Pennington committed
41 42 43 44 45 46 47 48 49 50 51 52 53
  LoadContext *lc = data;

  g_free (lc->filename);
  
  if (lc->load_timeout)
    g_source_remove (lc->load_timeout);

  if (lc->image_stream)
    fclose (lc->image_stream);

  if (lc->pixbuf_loader)
    {
      gdk_pixbuf_loader_close (lc->pixbuf_loader, NULL);
54
      g_object_unref (lc->pixbuf_loader);
Havoc Pennington's avatar
Havoc Pennington committed
55 56 57
    }
  
  g_free (lc);
Jonathan Blandford's avatar
Jonathan Blandford committed
58 59
}

Havoc Pennington's avatar
Havoc Pennington committed
60 61
static LoadContext*
get_load_context (GtkWidget *image)
Jonathan Blandford's avatar
Jonathan Blandford committed
62
{
Havoc Pennington's avatar
Havoc Pennington committed
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
  LoadContext *lc;

  lc = g_object_get_data (G_OBJECT (image), "lc");

  if (lc == NULL)
    {
      lc = g_new0 (LoadContext, 1);

      g_object_set_data_full (G_OBJECT (image),        
                              "lc",
                              lc,
                              destroy_context);
    }

  return lc;
Jonathan Blandford's avatar
Jonathan Blandford committed
78 79 80
}

static void
Havoc Pennington's avatar
Havoc Pennington committed
81 82
progressive_prepared_callback (GdkPixbufLoader* loader,
                               gpointer         data)
Jonathan Blandford's avatar
Jonathan Blandford committed
83
{
Havoc Pennington's avatar
Havoc Pennington committed
84 85
  GdkPixbuf* pixbuf;
  GtkWidget* image;
Jonathan Blandford's avatar
Jonathan Blandford committed
86

Havoc Pennington's avatar
Havoc Pennington committed
87 88 89 90 91 92 93 94 95 96 97 98 99 100
  image = GTK_WIDGET (data);
    
  pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);

  /* Avoid displaying random memory contents, since the pixbuf
   * isn't filled in yet.
   */
  gdk_pixbuf_fill (pixbuf, 0xaaaaaaff);

  /* Could set the pixbuf instead, if we only wanted to display
   * static images.
   */
  gtk_image_set_from_animation (GTK_IMAGE (image),
                                gdk_pixbuf_loader_get_animation (loader));
Jonathan Blandford's avatar
Jonathan Blandford committed
101 102
}

Havoc Pennington's avatar
Havoc Pennington committed
103 104 105 106
static void
progressive_updated_callback (GdkPixbufLoader* loader,
                              gint x, gint y, gint width, gint height,
                              gpointer data)
Jonathan Blandford's avatar
Jonathan Blandford committed
107
{
Havoc Pennington's avatar
Havoc Pennington committed
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
  GtkWidget* image;
  
  image = GTK_WIDGET (data);

  /* We know the pixbuf inside the GtkImage has changed, but the image
   * itself doesn't know this; so queue a redraw.  If we wanted to be
   * really efficient, we could use a drawing area or something
   * instead of a GtkImage, so we could control the exact position of
   * the pixbuf on the display, then we could queue a draw for only
   * the updated area of the image.
   */

  /* We only really need to redraw if the image's animation iterator
   * is gdk_pixbuf_animation_iter_on_currently_loading_frame(), but
   * who cares.
   */
  
  gtk_widget_queue_draw (image);
Jonathan Blandford's avatar
Jonathan Blandford committed
126 127 128
}

static gint
Havoc Pennington's avatar
Havoc Pennington committed
129
progressive_timeout (gpointer data)
Jonathan Blandford's avatar
Jonathan Blandford committed
130
{
Havoc Pennington's avatar
Havoc Pennington committed
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
  GtkWidget *image;
  LoadContext *lc;
  
  image = GTK_WIDGET (data);
  lc = get_load_context (image);
  
  /* This shows off fully-paranoid error handling, so looks scary.
   * You could factor out the error handling code into a nice separate
   * function to make things nicer.
   */
  
  if (lc->image_stream)
    {
      size_t bytes_read;
      guchar buf[256];
      GError *error = NULL;
      
      bytes_read = fread (buf, 1, 256, lc->image_stream);

      if (ferror (lc->image_stream))
        {
          GtkWidget *dialog;
          
          dialog = gtk_message_dialog_new (GTK_WINDOW (lc->window),
                                           GTK_DIALOG_DESTROY_WITH_PARENT,
                                           GTK_MESSAGE_ERROR,
                                           GTK_BUTTONS_CLOSE,
                                           "Failure reading image file 'alphatest.png': %s",
                                           g_strerror (errno));

161 162
          g_signal_connect (dialog, "response",
			    G_CALLBACK (gtk_widget_destroy), NULL);
Havoc Pennington's avatar
Havoc Pennington committed
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185

          fclose (lc->image_stream);
          lc->image_stream = NULL;

          gtk_widget_show (dialog);
          
          lc->load_timeout = 0;

          return FALSE; /* uninstall the timeout */
        }

      if (!gdk_pixbuf_loader_write (lc->pixbuf_loader,
                                    buf, bytes_read,
                                    &error))
        {
          GtkWidget *dialog;
          
          dialog = gtk_message_dialog_new (GTK_WINDOW (lc->window),
                                           GTK_DIALOG_DESTROY_WITH_PARENT,
                                           GTK_MESSAGE_ERROR,
                                           GTK_BUTTONS_CLOSE,
                                           "Failed to load image: %s",
                                           error->message);
Jonathan Blandford's avatar
Jonathan Blandford committed
186

Havoc Pennington's avatar
Havoc Pennington committed
187 188
          g_error_free (error);
          
189 190
          g_signal_connect (dialog, "response",
			    G_CALLBACK (gtk_widget_destroy), NULL);
Havoc Pennington's avatar
Havoc Pennington committed
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225

          fclose (lc->image_stream);
          lc->image_stream = NULL;
          
          gtk_widget_show (dialog);

          lc->load_timeout = 0;

          return FALSE; /* uninstall the timeout */
        }

      if (feof (lc->image_stream))
        {
          fclose (lc->image_stream);
          lc->image_stream = NULL;

          /* Errors can happen on close, e.g. if the image
           * file was truncated we'll know on close that
           * it was incomplete.
           */
          error = NULL;
          if (!gdk_pixbuf_loader_close (lc->pixbuf_loader,
                                        &error))
            {
              GtkWidget *dialog;
              
              dialog = gtk_message_dialog_new (GTK_WINDOW (lc->window),
                                               GTK_DIALOG_DESTROY_WITH_PARENT,
                                               GTK_MESSAGE_ERROR,
                                               GTK_BUTTONS_CLOSE,
                                               "Failed to load image: %s",
                                               error->message);
              
              g_error_free (error);
              
226 227
              g_signal_connect (dialog, "response",
				G_CALLBACK (gtk_widget_destroy), NULL);
Havoc Pennington's avatar
Havoc Pennington committed
228 229 230
              
              gtk_widget_show (dialog);

231
              g_object_unref (lc->pixbuf_loader);
Havoc Pennington's avatar
Havoc Pennington committed
232 233 234 235 236 237 238
              lc->pixbuf_loader = NULL;
              
              lc->load_timeout = 0;
              
              return FALSE; /* uninstall the timeout */
            }
          
239
          g_object_unref (lc->pixbuf_loader);
Havoc Pennington's avatar
Havoc Pennington committed
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
          lc->pixbuf_loader = NULL;
        }
    }
  else
    {
      lc->image_stream = fopen (lc->filename, "r");

      if (lc->image_stream == NULL)
        {
          GtkWidget *dialog;
          
          dialog = gtk_message_dialog_new (GTK_WINDOW (lc->window),
                                           GTK_DIALOG_DESTROY_WITH_PARENT,
                                           GTK_MESSAGE_ERROR,
                                           GTK_BUTTONS_CLOSE,
                                           "Unable to open image file '%s': %s",
                                           lc->filename,
                                           g_strerror (errno));

259 260
          g_signal_connect (dialog, "response",
			    G_CALLBACK (gtk_widget_destroy), NULL);
Havoc Pennington's avatar
Havoc Pennington committed
261 262 263 264 265 266 267 268 269 270 271
          
          gtk_widget_show (dialog);

          lc->load_timeout = 0;

          return FALSE; /* uninstall the timeout */
        }

      if (lc->pixbuf_loader)
        {
          gdk_pixbuf_loader_close (lc->pixbuf_loader, NULL);
272
          g_object_unref (lc->pixbuf_loader);
Havoc Pennington's avatar
Havoc Pennington committed
273 274 275 276 277
          lc->pixbuf_loader = NULL;
        }
      
      lc->pixbuf_loader = gdk_pixbuf_loader_new ();
      
278
      g_signal_connect (lc->pixbuf_loader, "area_prepared",
279
			G_CALLBACK (progressive_prepared_callback), image);
280
      g_signal_connect (lc->pixbuf_loader, "area_updated",
281
			G_CALLBACK (progressive_updated_callback), image);
Havoc Pennington's avatar
Havoc Pennington committed
282 283 284 285
    }

  /* leave timeout installed */
  return TRUE;
Jonathan Blandford's avatar
Jonathan Blandford committed
286 287 288
}

static void
Havoc Pennington's avatar
Havoc Pennington committed
289
start_progressive_loading (GtkWidget *image)
Jonathan Blandford's avatar
Jonathan Blandford committed
290
{
Havoc Pennington's avatar
Havoc Pennington committed
291 292 293 294 295 296 297 298 299 300 301
  LoadContext *lc;

  lc = get_load_context (image);
  
  /* This is obviously totally contrived (we slow down loading
   * on purpose to show how incremental loading works).
   * The real purpose of incremental loading is the case where
   * you are reading data from a slow source such as the network.
   * The timeout simply simulates a slow data source by inserting
   * pauses in the reading process.
   */
302
  lc->load_timeout = gdk_threads_add_timeout (100,
Havoc Pennington's avatar
Havoc Pennington committed
303 304
                                    progressive_timeout,
                                    image);
Jonathan Blandford's avatar
Jonathan Blandford committed
305 306
}

Havoc Pennington's avatar
Havoc Pennington committed
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
static GtkWidget *
do_image (const char *filename)
{
  GtkWidget *frame;
  GtkWidget *vbox;
  GtkWidget *image;
  GtkWidget *label;
  GtkWidget *window;
  gchar *str, *escaped;
  LoadContext *lc;
  
  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title (GTK_WINDOW (window), "Image Loading");

  gtk_container_set_border_width (GTK_CONTAINER (window), 8);

323
  vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 8);
Havoc Pennington's avatar
Havoc Pennington committed
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
  gtk_container_set_border_width (GTK_CONTAINER (vbox), 8);
  gtk_container_add (GTK_CONTAINER (window), vbox);

  label = gtk_label_new (NULL);
  gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
  escaped = g_markup_escape_text (filename, -1);
  str = g_strdup_printf ("Progressively loading: <b>%s</b>", escaped);
  gtk_label_set_markup (GTK_LABEL (label),
                        str);
  g_free (escaped);
  g_free (str);
  
  gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
      
  frame = gtk_frame_new (NULL);
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
340 341 342
  gtk_widget_set_halign (frame, GTK_ALIGN_CENTER);
  gtk_widget_set_valign (frame, GTK_ALIGN_CENTER);
  gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);
Havoc Pennington's avatar
Havoc Pennington committed
343 344 345 346 347 348 349 350 351 352 353

  image = gtk_image_new_from_pixbuf (NULL);
  gtk_container_add (GTK_CONTAINER (frame), image);

  lc = get_load_context (image);

  lc->window = window;
  lc->filename = g_strdup (filename);
  
  start_progressive_loading (image);

354
  g_signal_connect (window, "destroy",
Matthias Clasen's avatar
Matthias Clasen committed
355 356
		    G_CALLBACK (gtk_main_quit), NULL);
  
357
  g_signal_connect (window, "delete_event",
Matthias Clasen's avatar
Matthias Clasen committed
358 359
		    G_CALLBACK (gtk_main_quit), NULL);

Havoc Pennington's avatar
Havoc Pennington committed
360 361 362 363
  gtk_widget_show_all (window);

  return window;
}
Jonathan Blandford's avatar
Jonathan Blandford committed
364 365

static void
Havoc Pennington's avatar
Havoc Pennington committed
366
do_nonprogressive (const gchar *filename)
Jonathan Blandford's avatar
Jonathan Blandford committed
367
{
Havoc Pennington's avatar
Havoc Pennington committed
368 369 370 371 372 373 374 375 376 377 378 379
  GtkWidget *frame;
  GtkWidget *vbox;
  GtkWidget *image;
  GtkWidget *label;
  GtkWidget *window;
  gchar *str, *escaped;
  
  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title (GTK_WINDOW (window), "Animation");

  gtk_container_set_border_width (GTK_CONTAINER (window), 8);

380
  vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 8);
Havoc Pennington's avatar
Havoc Pennington committed
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
  gtk_container_set_border_width (GTK_CONTAINER (vbox), 8);
  gtk_container_add (GTK_CONTAINER (window), vbox);

  label = gtk_label_new (NULL);
  gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
  escaped = g_markup_escape_text (filename, -1);
  str = g_strdup_printf ("Loaded from file: <b>%s</b>", escaped);
  gtk_label_set_markup (GTK_LABEL (label),
                        str);
  g_free (escaped);
  g_free (str);
  
  gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
      
  frame = gtk_frame_new (NULL);
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
397 398 399
  gtk_widget_set_halign (frame, GTK_ALIGN_CENTER);
  gtk_widget_set_valign (frame, GTK_ALIGN_CENTER);
  gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);
Havoc Pennington's avatar
Havoc Pennington committed
400 401 402 403

  image = gtk_image_new_from_file (filename);
  gtk_container_add (GTK_CONTAINER (frame), image);

404
  g_signal_connect (window, "destroy",
Matthias Clasen's avatar
Matthias Clasen committed
405 406
		    G_CALLBACK (gtk_main_quit), NULL);
  
407
  g_signal_connect (window, "delete_event",
Matthias Clasen's avatar
Matthias Clasen committed
408 409
		    G_CALLBACK (gtk_main_quit), NULL);

Havoc Pennington's avatar
Havoc Pennington committed
410
  gtk_widget_show_all (window);
Jonathan Blandford's avatar
Jonathan Blandford committed
411 412 413
}

int
Havoc Pennington's avatar
Havoc Pennington committed
414 415
main (int    argc,
      char **argv)
Jonathan Blandford's avatar
Jonathan Blandford committed
416
{
Havoc Pennington's avatar
Havoc Pennington committed
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
  gint i;
  
  gtk_init (&argc, &argv);

  i = 1;
  while (i < argc)
    {
      do_image (argv[i]);
      do_nonprogressive (argv[i]);
      
      ++i;
    }

  gtk_main ();
  
  return 0;
}
Havoc Pennington's avatar
Havoc Pennington committed
434