testanimation.c 12.8 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
Jonathan Blandford's avatar
Jonathan Blandford committed
16 17 18 19 20 21
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

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

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
38 39 40
};

static void
Havoc Pennington's avatar
Havoc Pennington committed
41
destroy_context (gpointer data)
Jonathan Blandford's avatar
Jonathan Blandford committed
42
{
Havoc Pennington's avatar
Havoc Pennington committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
  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);
      g_object_unref (G_OBJECT (lc->pixbuf_loader));
    }
  
  g_free (lc);
Jonathan Blandford's avatar
Jonathan Blandford committed
60 61
}

Havoc Pennington's avatar
Havoc Pennington committed
62 63
static LoadContext*
get_load_context (GtkWidget *image)
Jonathan Blandford's avatar
Jonathan Blandford committed
64
{
Havoc Pennington's avatar
Havoc Pennington committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
  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
80 81 82
}

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

Havoc Pennington's avatar
Havoc Pennington committed
89 90 91 92 93 94 95 96 97 98 99 100 101 102
  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
103 104
}

Havoc Pennington's avatar
Havoc Pennington committed
105 106 107 108
static void
progressive_updated_callback (GdkPixbufLoader* loader,
                              gint x, gint y, gint width, gint height,
                              gpointer data)
Jonathan Blandford's avatar
Jonathan Blandford committed
109
{
Havoc Pennington's avatar
Havoc Pennington committed
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
  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
128 129 130
}

static gint
Havoc Pennington's avatar
Havoc Pennington committed
131
progressive_timeout (gpointer data)
Jonathan Blandford's avatar
Jonathan Blandford committed
132
{
Havoc Pennington's avatar
Havoc Pennington committed
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 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
  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));

          gtk_signal_connect (GTK_OBJECT (dialog),
                              "response",
                              GTK_SIGNAL_FUNC (gtk_widget_destroy),
                              NULL);

          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
190

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 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
          g_error_free (error);
          
          gtk_signal_connect (GTK_OBJECT (dialog),
                              "response",
                              GTK_SIGNAL_FUNC (gtk_widget_destroy),
                              NULL);

          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);
              
              gtk_signal_connect (GTK_OBJECT (dialog),
                                  "response",
                                  GTK_SIGNAL_FUNC (gtk_widget_destroy),
                                  NULL);
              
              gtk_widget_show (dialog);

              g_object_unref (G_OBJECT (lc->pixbuf_loader));
              lc->pixbuf_loader = NULL;
              
              lc->load_timeout = 0;
              
              return FALSE; /* uninstall the timeout */
            }
          
          g_object_unref (G_OBJECT (lc->pixbuf_loader));
          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));

          gtk_signal_connect (GTK_OBJECT (dialog),
                              "response",
                              GTK_SIGNAL_FUNC (gtk_widget_destroy),
                              NULL);
          
          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);
          g_object_unref (G_OBJECT (lc->pixbuf_loader));
          lc->pixbuf_loader = NULL;
        }
      
      lc->pixbuf_loader = gdk_pixbuf_loader_new ();
      
      g_signal_connect_data (G_OBJECT (lc->pixbuf_loader),
                             "area_prepared",
                             G_CALLBACK (progressive_prepared_callback),
                             image,
                             NULL, FALSE, FALSE);
      
      g_signal_connect_data (G_OBJECT (lc->pixbuf_loader),
                             "area_updated",
                             G_CALLBACK (progressive_updated_callback),
                             image,
                             NULL, FALSE, FALSE);
    }

  /* leave timeout installed */
  return TRUE;
Jonathan Blandford's avatar
Jonathan Blandford committed
303 304 305
}

static void
Havoc Pennington's avatar
Havoc Pennington committed
306
start_progressive_loading (GtkWidget *image)
Jonathan Blandford's avatar
Jonathan Blandford committed
307
{
Havoc Pennington's avatar
Havoc Pennington committed
308 309 310 311 312 313 314 315 316 317 318 319 320 321
  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.
   */
  lc->load_timeout = g_timeout_add (100,
                                    progressive_timeout,
                                    image);
Jonathan Blandford's avatar
Jonathan Blandford committed
322 323
}

Havoc Pennington's avatar
Havoc Pennington committed
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
static GtkWidget *
do_image (const char *filename)
{
  GtkWidget *frame;
  GtkWidget *vbox;
  GtkWidget *image;
  GtkWidget *label;
  GtkWidget *align;
  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);

  vbox = gtk_vbox_new (FALSE, 8);
  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);
  /* The alignment keeps the frame from growing when users resize
   * the window
   */
  align = gtk_alignment_new (0.5, 0.5, 0, 0);
  gtk_container_add (GTK_CONTAINER (align), frame);
  gtk_box_pack_start (GTK_BOX (vbox), align, FALSE, FALSE, 0);      

  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);

  gtk_widget_show_all (window);

  return window;
}
Jonathan Blandford's avatar
Jonathan Blandford committed
379 380

static void
Havoc Pennington's avatar
Havoc Pennington committed
381
do_nonprogressive (const gchar *filename)
Jonathan Blandford's avatar
Jonathan Blandford committed
382
{
Havoc Pennington's avatar
Havoc Pennington committed
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
  GtkWidget *frame;
  GtkWidget *vbox;
  GtkWidget *image;
  GtkWidget *label;
  GtkWidget *align;
  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);

  vbox = gtk_vbox_new (FALSE, 8);
  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);
  /* The alignment keeps the frame from growing when users resize
   * the window
   */
  align = gtk_alignment_new (0.5, 0.5, 0, 0);
  gtk_container_add (GTK_CONTAINER (align), frame);
  gtk_box_pack_start (GTK_BOX (vbox), align, FALSE, FALSE, 0);      

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

  gtk_widget_show_all (window);
Jonathan Blandford's avatar
Jonathan Blandford committed
424 425 426
}

int
Havoc Pennington's avatar
Havoc Pennington committed
427 428
main (int    argc,
      char **argv)
Jonathan Blandford's avatar
Jonathan Blandford committed
429
{
Havoc Pennington's avatar
Havoc Pennington committed
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
  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
447 448