]> Pileus Git - ~andy/gtk/blobdiff - demos/gtk-demo/images.c
gtk: remove "gboolean homogeneous" from gtk_box_new()
[~andy/gtk] / demos / gtk-demo / images.c
index 7faeb53ccaeb18f39dcaa730166dec361743841c..8ff6b1490e726b6b8d99cd5c413ac372b65e7635 100644 (file)
  */
 
 #include <gtk/gtk.h>
+#include <glib/gstdio.h>
 #include <stdio.h>
 #include <errno.h>
+#include "demo-common.h"
 
 static GtkWidget *window = NULL;
 static GdkPixbufLoader *pixbuf_loader = NULL;
@@ -22,30 +24,34 @@ static guint load_timeout = 0;
 static FILE* image_stream = NULL;
 
 static void
-progressive_prepared_callback (GdkPixbufLoader* loader, gpointer data)
+progressive_prepared_callback (GdkPixbufLoader *loader,
+                              gpointer         data)
 {
-  GdkPixbufpixbuf;
-  GtkWidgetimage;
+  GdkPixbuf *pixbuf;
+  GtkWidget *image;
 
   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);
-  
+
   gtk_image_set_from_pixbuf (GTK_IMAGE (image), pixbuf);
 }
 
 static void
-progressive_updated_callback (GdkPixbufLoader* loader,
-                              gint x, gint y, gint width, gint height,
-                              gpointer data)
+progressive_updated_callback (GdkPixbufLoader *loader,
+                              gint                x,
+                              gint                y,
+                              gint                width,
+                              gint                height,
+                              gpointer    data)
 {
-  GtkWidgetimage;
-  
+  GtkWidget *image;
+
   image = GTK_WIDGET (data);
 
   /* We know the pixbuf inside the GtkImage has changed, but the image
@@ -55,7 +61,7 @@ progressive_updated_callback (GdkPixbufLoader* loader,
    * the pixbuf on the display, then we could queue a draw for only
    * the updated area of the image.
    */
-  
+
   gtk_widget_queue_draw (image);
 }
 
@@ -65,173 +71,174 @@ progressive_timeout (gpointer data)
   GtkWidget *image;
 
   image = GTK_WIDGET (data);
-  
+
   /* 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 (image_stream)
     {
       size_t bytes_read;
       guchar buf[256];
       GError *error = NULL;
-      
+
       bytes_read = fread (buf, 1, 256, image_stream);
 
       if (ferror (image_stream))
-        {
-          GtkWidget *dialog;
-          
-          dialog = gtk_message_dialog_new (GTK_WINDOW (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 (image_stream);
-          image_stream = NULL;
-
-          gtk_widget_show (dialog);
-          
-          load_timeout = 0;
-
-          return FALSE; /* uninstall the timeout */
-        }
+       {
+         GtkWidget *dialog;
+
+         dialog = gtk_message_dialog_new (GTK_WINDOW (window),
+                                          GTK_DIALOG_DESTROY_WITH_PARENT,
+                                          GTK_MESSAGE_ERROR,
+                                          GTK_BUTTONS_CLOSE,
+                                          "Failure reading image file 'alphatest.png': %s",
+                                          g_strerror (errno));
+
+         g_signal_connect (dialog, "response",
+                           G_CALLBACK (gtk_widget_destroy), NULL);
+
+         fclose (image_stream);
+         image_stream = NULL;
+
+         gtk_widget_show (dialog);
+
+         load_timeout = 0;
+
+         return FALSE; /* uninstall the timeout */
+       }
 
       if (!gdk_pixbuf_loader_write (pixbuf_loader,
-                                    buf, bytes_read,
-                                    &error))
-        {
-          GtkWidget *dialog;
-          
-          dialog = gtk_message_dialog_new (GTK_WINDOW (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);
-
-          fclose (image_stream);
-          image_stream = NULL;
-          
-          gtk_widget_show (dialog);
-
-          load_timeout = 0;
-
-          return FALSE; /* uninstall the timeout */
-        }
+                                   buf, bytes_read,
+                                   &error))
+       {
+         GtkWidget *dialog;
+
+         dialog = gtk_message_dialog_new (GTK_WINDOW (window),
+                                          GTK_DIALOG_DESTROY_WITH_PARENT,
+                                          GTK_MESSAGE_ERROR,
+                                          GTK_BUTTONS_CLOSE,
+                                          "Failed to load image: %s",
+                                          error->message);
+
+         g_error_free (error);
+
+         g_signal_connect (dialog, "response",
+                           G_CALLBACK (gtk_widget_destroy), NULL);
+
+         fclose (image_stream);
+         image_stream = NULL;
+
+         gtk_widget_show (dialog);
+
+         load_timeout = 0;
+
+         return FALSE; /* uninstall the timeout */
+       }
 
       if (feof (image_stream))
-        {
-          fclose (image_stream);
-          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 (pixbuf_loader,
-                                        &error))
-            {
-              GtkWidget *dialog;
-              
-              dialog = gtk_message_dialog_new (GTK_WINDOW (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 (pixbuf_loader));
-              pixbuf_loader = NULL;
-              
-              load_timeout = 0;
-              
-              return FALSE; /* uninstall the timeout */
-            }
-          
-          g_object_unref (G_OBJECT (pixbuf_loader));
-          pixbuf_loader = NULL;
-        }
+       {
+         fclose (image_stream);
+         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 (pixbuf_loader,
+                                       &error))
+           {
+             GtkWidget *dialog;
+
+             dialog = gtk_message_dialog_new (GTK_WINDOW (window),
+                                              GTK_DIALOG_DESTROY_WITH_PARENT,
+                                              GTK_MESSAGE_ERROR,
+                                              GTK_BUTTONS_CLOSE,
+                                              "Failed to load image: %s",
+                                              error->message);
+
+             g_error_free (error);
+
+             g_signal_connect (dialog, "response",
+                               G_CALLBACK (gtk_widget_destroy), NULL);
+
+             gtk_widget_show (dialog);
+
+             g_object_unref (pixbuf_loader);
+             pixbuf_loader = NULL;
+
+             load_timeout = 0;
+
+             return FALSE; /* uninstall the timeout */
+           }
+
+         g_object_unref (pixbuf_loader);
+         pixbuf_loader = NULL;
+       }
     }
   else
     {
-      const gchar *filename;
+      gchar *filename;
+      gchar *error_message = NULL;
+      GError *error = NULL;
 
-      if (g_file_test ("./alphatest.png", G_FILE_TEST_EXISTS))
-        filename = "./alphatest.png";
+      /* demo_find_file() looks in the current directory first,
+       * so you can run gtk-demo without installing GTK, then looks
+       * in the location where the file is installed.
+       */
+      filename = demo_find_file ("alphatest.png", &error);
+      if (error)
+       {
+         error_message = g_strdup (error->message);
+         g_error_free (error);
+       }
       else
-        filename = DEMOCODEDIR"/alphatest.png";
-      
-      image_stream = fopen (filename, "r");
+       {
+         image_stream = g_fopen (filename, "rb");
+         g_free (filename);
+
+         if (!image_stream)
+           error_message = g_strdup_printf ("Unable to open image file 'alphatest.png': %s",
+                                            g_strerror (errno));
+       }
 
       if (image_stream == NULL)
-        {
-          GtkWidget *dialog;
-          
-          dialog = gtk_message_dialog_new (GTK_WINDOW (window),
-                                           GTK_DIALOG_DESTROY_WITH_PARENT,
-                                           GTK_MESSAGE_ERROR,
-                                           GTK_BUTTONS_CLOSE,
-                                           "Unable to open image file 'alphatest.png': %s",
-                                           g_strerror (errno));
-
-          gtk_signal_connect (GTK_OBJECT (dialog),
-                              "response",
-                              GTK_SIGNAL_FUNC (gtk_widget_destroy),
-                              NULL);
-          
-          gtk_widget_show (dialog);
-
-          load_timeout = 0;
-
-          return FALSE; /* uninstall the timeout */
-        }
+       {
+         GtkWidget *dialog;
+
+         dialog = gtk_message_dialog_new (GTK_WINDOW (window),
+                                          GTK_DIALOG_DESTROY_WITH_PARENT,
+                                          GTK_MESSAGE_ERROR,
+                                          GTK_BUTTONS_CLOSE,
+                                          "%s", error_message);
+         g_free (error_message);
+
+         g_signal_connect (dialog, "response",
+                           G_CALLBACK (gtk_widget_destroy), NULL);
+
+         gtk_widget_show (dialog);
+
+         load_timeout = 0;
+
+         return FALSE; /* uninstall the timeout */
+       }
 
       if (pixbuf_loader)
-        {
-          gdk_pixbuf_loader_close (pixbuf_loader, NULL);
-          g_object_unref (G_OBJECT (pixbuf_loader));
-          pixbuf_loader = NULL;
-        }
-      
+       {
+         gdk_pixbuf_loader_close (pixbuf_loader, NULL);
+         g_object_unref (pixbuf_loader);
+         pixbuf_loader = NULL;
+       }
+
       pixbuf_loader = gdk_pixbuf_loader_new ();
-      
-      g_signal_connect_data (G_OBJECT (pixbuf_loader),
-                             "area_prepared",
-                             G_CALLBACK (progressive_prepared_callback),
-                             image,
-                             NULL, FALSE, FALSE);
-      
-      g_signal_connect_data (G_OBJECT (pixbuf_loader),
-                             "area_updated",
-                             G_CALLBACK (progressive_updated_callback),
-                             image,
-                             NULL, FALSE, FALSE);
+
+      g_signal_connect (pixbuf_loader, "area-prepared",
+                       G_CALLBACK (progressive_prepared_callback), image);
+
+      g_signal_connect (pixbuf_loader, "area-updated",
+                       G_CALLBACK (progressive_updated_callback), image);
     }
 
   /* leave timeout installed */
@@ -248,25 +255,25 @@ start_progressive_loading (GtkWidget *image)
    * The timeout simply simulates a slow data source by inserting
    * pauses in the reading process.
    */
-  load_timeout = g_timeout_add (150,
-                                progressive_timeout,
-                                image);
+  load_timeout = gdk_threads_add_timeout (150,
+                               progressive_timeout,
+                               image);
 }
 
 static void
-cleanup_callback (GtkObject *object,
-                  gpointer data)
+cleanup_callback (GObject   *object,
+                 gpointer   data)
 {
   if (load_timeout)
     {
       g_source_remove (load_timeout);
       load_timeout = 0;
     }
-  
+
   if (pixbuf_loader)
     {
       gdk_pixbuf_loader_close (pixbuf_loader, NULL);
-      g_object_unref (G_OBJECT (pixbuf_loader));
+      g_object_unref (pixbuf_loader);
       pixbuf_loader = NULL;
     }
 
@@ -275,34 +282,68 @@ cleanup_callback (GtkObject *object,
   image_stream = NULL;
 }
 
+static void
+toggle_sensitivity_callback (GtkWidget *togglebutton,
+                             gpointer   user_data)
+{
+  GtkContainer *container = user_data;
+  GList *list;
+  GList *tmp;
+
+  list = gtk_container_get_children (container);
+
+  tmp = list;
+  while (tmp != NULL)
+    {
+      /* don't disable our toggle */
+      if (GTK_WIDGET (tmp->data) != togglebutton)
+        gtk_widget_set_sensitive (GTK_WIDGET (tmp->data),
+                                  !gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (togglebutton)));
+
+      tmp = tmp->next;
+    }
+
+  g_list_free (list);
+}
+
+
 GtkWidget *
-do_images (void)
+do_images (GtkWidget *do_widget)
 {
   GtkWidget *frame;
   GtkWidget *vbox;
   GtkWidget *image;
   GtkWidget *label;
   GtkWidget *align;
-  
+  GtkWidget *button;
+  GdkPixbuf *pixbuf;
+  GIcon     *gicon;
+  GError *error = NULL;
+  char *filename;
+
   if (!window)
     {
       window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
+      gtk_window_set_screen (GTK_WINDOW (window),
+                            gtk_widget_get_screen (do_widget));
       gtk_window_set_title (GTK_WINDOW (window), "Images");
 
-      gtk_signal_connect (GTK_OBJECT (window), "destroy", GTK_SIGNAL_FUNC (gtk_widget_destroyed), &window);
-      gtk_signal_connect (GTK_OBJECT (window), "destroy", GTK_SIGNAL_FUNC (cleanup_callback), NULL);
+      g_signal_connect (window, "destroy",
+                       G_CALLBACK (gtk_widget_destroyed), &window);
+      g_signal_connect (window, "destroy",
+                       G_CALLBACK (cleanup_callback), NULL);
 
       gtk_container_set_border_width (GTK_CONTAINER (window), 8);
 
-      vbox = gtk_vbox_new (FALSE, 8);
+      vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 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_markup (GTK_LABEL (label),
-                            "<u>Image loaded from a file</u>");
+                           "<u>Image loaded from a file</u>");
       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
@@ -312,52 +353,43 @@ do_images (void)
       gtk_container_add (GTK_CONTAINER (align), frame);
       gtk_box_pack_start (GTK_BOX (vbox), align, FALSE, FALSE, 0);
 
-      /* We look for the image in the current directory first,
-       * so you can run gtk-demo without installing GTK
+      /* demo_find_file() looks in the current directory first,
+       * so you can run gtk-demo without installing GTK, then looks
+       * in the location where the file is installed.
        */
-      if (g_file_test ("./gtk-logo-rgb.gif", G_FILE_TEST_EXISTS))
-        {
-          /* This code shows off error handling. You can just use
-           * gtk_image_new_from_file() instead if you don't want to report
-           * errors to the user. If the file doesn't load when using
-           * gtk_image_new_from_file(), a "missing image" icon will
-           * be displayed instead.
-           */
-          GdkPixbuf *pixbuf;
-          GError *error = NULL;
-          
-          pixbuf = gdk_pixbuf_new_from_file ("./gtk-logo-rgb.gif",
-                                             &error);
-          if (error)
-            {
-              GtkWidget *dialog;
-
-              dialog = gtk_message_dialog_new (GTK_WINDOW (window),
-                                               GTK_DIALOG_DESTROY_WITH_PARENT,
-                                               GTK_MESSAGE_ERROR,
-                                               GTK_BUTTONS_CLOSE,
-                                               "Unable to open image file 'gtk-logo-rgb.gif': %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);
-            }
-          
-          image = gtk_image_new_from_pixbuf (pixbuf);
-        }
-      else
-        {
-          /* This is the simpler code, with no error handling.
-           * Here we're loading the installed gtk-logo-rgb.gif instead
-           * of the one in the current directory.
-           */
-          image = gtk_image_new_from_file (DEMOCODEDIR"/gtk-logo-rgb.gif");
-        }
+      pixbuf = NULL;
+      filename = demo_find_file ("gtk-logo-rgb.gif", &error);
+      if (filename)
+       {
+         pixbuf = gdk_pixbuf_new_from_file (filename, &error);
+         g_free (filename);
+       }
+
+      if (error)
+       {
+         /* This code shows off error handling. You can just use
+          * gtk_image_new_from_file() instead if you don't want to report
+          * errors to the user. If the file doesn't load when using
+          * gtk_image_new_from_file(), a "missing image" icon will
+          * be displayed instead.
+          */
+         GtkWidget *dialog;
+
+         dialog = gtk_message_dialog_new (GTK_WINDOW (window),
+                                          GTK_DIALOG_DESTROY_WITH_PARENT,
+                                          GTK_MESSAGE_ERROR,
+                                          GTK_BUTTONS_CLOSE,
+                                          "Unable to open image file 'gtk-logo-rgb.gif': %s",
+                                          error->message);
+         g_error_free (error);
+
+         g_signal_connect (dialog, "response",
+                           G_CALLBACK (gtk_widget_destroy), NULL);
+
+         gtk_widget_show (dialog);
+       }
+
+      image = gtk_image_new_from_pixbuf (pixbuf);
 
       gtk_container_add (GTK_CONTAINER (frame), image);
 
@@ -366,9 +398,9 @@ do_images (void)
 
       label = gtk_label_new (NULL);
       gtk_label_set_markup (GTK_LABEL (label),
-                            "<u>Animation loaded from a file</u>");
+                           "<u>Animation loaded from a file</u>");
       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
@@ -378,25 +410,41 @@ do_images (void)
       gtk_container_add (GTK_CONTAINER (align), frame);
       gtk_box_pack_start (GTK_BOX (vbox), align, FALSE, FALSE, 0);
 
-      /* We look for the image in the current directory first,
-       * so you can run gtk-demo without installing GTK
+      filename = demo_find_file ("floppybuddy.gif", NULL);
+      image = gtk_image_new_from_file (filename);
+      g_free (filename);
+
+      gtk_container_add (GTK_CONTAINER (frame), image);
+
+      /* Symbolic icon */
+
+      label = gtk_label_new (NULL);
+      gtk_label_set_markup (GTK_LABEL (label),
+                           "<u>Symbolic themed icon</u>");
+      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
        */
-      if (g_file_test ("./floppybuddy.gif", G_FILE_TEST_EXISTS))
-        image = gtk_image_new_from_file ("./floppybuddy.gif");
-      else
-        image = gtk_image_new_from_file (DEMOCODEDIR"/floppybuddy.gif");
+      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);
+
+      gicon = g_themed_icon_new_with_default_fallbacks ("battery-critical-charging-symbolic");
+      image = gtk_image_new_from_gicon (gicon, GTK_ICON_SIZE_DIALOG);
 
       gtk_container_add (GTK_CONTAINER (frame), image);
-      
 
       /* Progressive */
-      
-      
+
+
       label = gtk_label_new (NULL);
       gtk_label_set_markup (GTK_LABEL (label),
-                            "<u>Progressive image loading</u>");
+                           "<u>Progressive image loading</u>");
       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
@@ -413,9 +461,17 @@ do_images (void)
       gtk_container_add (GTK_CONTAINER (frame), image);
 
       start_progressive_loading (image);
+
+      /* Sensitivity control */
+      button = gtk_toggle_button_new_with_mnemonic ("_Insensitive");
+      gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
+
+      g_signal_connect (button, "toggled",
+                        G_CALLBACK (toggle_sensitivity_callback),
+                        vbox);
     }
 
-  if (!GTK_WIDGET_VISIBLE (window))
+  if (!gtk_widget_get_visible (window))
     {
       gtk_widget_show_all (window);
     }