]> Pileus Git - ~andy/gtk/blob - demos/gtk-demo/images.c
acd382f75795e8f88cccfa1b75fcb9d937d68e46
[~andy/gtk] / demos / gtk-demo / images.c
1 /* Images
2  *
3  * GtkImage is used to display an image; the image can be in a number of formats.
4  * Typically, you load an image into a GdkPixbuf, then display the pixbuf.
5  *
6  * This demo code shows some of the more obscure cases, in the simple
7  * case a call to gtk_image_new_from_file() is all you need.
8  *
9  * If you want to put image data in your program as a C variable,
10  * use the make-inline-pixbuf program that comes with GTK+.
11  * This way you won't need to depend on loading external files, your
12  * application binary can be self-contained.
13  */
14
15 #include <gtk/gtk.h>
16 #include <glib/gstdio.h>
17 #include <stdio.h>
18 #include <errno.h>
19 #include "demo-common.h"
20
21 static GtkWidget *window = NULL;
22 static GdkPixbufLoader *pixbuf_loader = NULL;
23 static guint load_timeout = 0;
24 static FILE* image_stream = NULL;
25
26 static void
27 progressive_prepared_callback (GdkPixbufLoader *loader,
28                                gpointer         data)
29 {
30   GdkPixbuf *pixbuf;
31   GtkWidget *image;
32
33   image = GTK_WIDGET (data);
34
35   pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
36
37   /* Avoid displaying random memory contents, since the pixbuf
38    * isn't filled in yet.
39    */
40   gdk_pixbuf_fill (pixbuf, 0xaaaaaaff);
41
42   gtk_image_set_from_pixbuf (GTK_IMAGE (image), pixbuf);
43 }
44
45 static void
46 progressive_updated_callback (GdkPixbufLoader *loader,
47                               gint                 x,
48                               gint                 y,
49                               gint                 width,
50                               gint                 height,
51                               gpointer     data)
52 {
53   GtkWidget *image;
54
55   image = GTK_WIDGET (data);
56
57   /* We know the pixbuf inside the GtkImage has changed, but the image
58    * itself doesn't know this; so queue a redraw.  If we wanted to be
59    * really efficient, we could use a drawing area or something
60    * instead of a GtkImage, so we could control the exact position of
61    * the pixbuf on the display, then we could queue a draw for only
62    * the updated area of the image.
63    */
64
65   gtk_widget_queue_draw (image);
66 }
67
68 static gint
69 progressive_timeout (gpointer data)
70 {
71   GtkWidget *image;
72
73   image = GTK_WIDGET (data);
74
75   /* This shows off fully-paranoid error handling, so looks scary.
76    * You could factor out the error handling code into a nice separate
77    * function to make things nicer.
78    */
79
80   if (image_stream)
81     {
82       size_t bytes_read;
83       guchar buf[256];
84       GError *error = NULL;
85
86       bytes_read = fread (buf, 1, 256, image_stream);
87
88       if (ferror (image_stream))
89         {
90           GtkWidget *dialog;
91
92           dialog = gtk_message_dialog_new (GTK_WINDOW (window),
93                                            GTK_DIALOG_DESTROY_WITH_PARENT,
94                                            GTK_MESSAGE_ERROR,
95                                            GTK_BUTTONS_CLOSE,
96                                            "Failure reading image file 'alphatest.png': %s",
97                                            g_strerror (errno));
98
99           g_signal_connect (dialog, "response",
100                             G_CALLBACK (gtk_widget_destroy), NULL);
101
102           fclose (image_stream);
103           image_stream = NULL;
104
105           gtk_widget_show (dialog);
106
107           load_timeout = 0;
108
109           return FALSE; /* uninstall the timeout */
110         }
111
112       if (!gdk_pixbuf_loader_write (pixbuf_loader,
113                                     buf, bytes_read,
114                                     &error))
115         {
116           GtkWidget *dialog;
117
118           dialog = gtk_message_dialog_new (GTK_WINDOW (window),
119                                            GTK_DIALOG_DESTROY_WITH_PARENT,
120                                            GTK_MESSAGE_ERROR,
121                                            GTK_BUTTONS_CLOSE,
122                                            "Failed to load image: %s",
123                                            error->message);
124
125           g_error_free (error);
126
127           g_signal_connect (dialog, "response",
128                             G_CALLBACK (gtk_widget_destroy), NULL);
129
130           fclose (image_stream);
131           image_stream = NULL;
132
133           gtk_widget_show (dialog);
134
135           load_timeout = 0;
136
137           return FALSE; /* uninstall the timeout */
138         }
139
140       if (feof (image_stream))
141         {
142           fclose (image_stream);
143           image_stream = NULL;
144
145           /* Errors can happen on close, e.g. if the image
146            * file was truncated we'll know on close that
147            * it was incomplete.
148            */
149           error = NULL;
150           if (!gdk_pixbuf_loader_close (pixbuf_loader,
151                                         &error))
152             {
153               GtkWidget *dialog;
154
155               dialog = gtk_message_dialog_new (GTK_WINDOW (window),
156                                                GTK_DIALOG_DESTROY_WITH_PARENT,
157                                                GTK_MESSAGE_ERROR,
158                                                GTK_BUTTONS_CLOSE,
159                                                "Failed to load image: %s",
160                                                error->message);
161
162               g_error_free (error);
163
164               g_signal_connect (dialog, "response",
165                                 G_CALLBACK (gtk_widget_destroy), NULL);
166
167               gtk_widget_show (dialog);
168
169               g_object_unref (pixbuf_loader);
170               pixbuf_loader = NULL;
171
172               load_timeout = 0;
173
174               return FALSE; /* uninstall the timeout */
175             }
176
177           g_object_unref (pixbuf_loader);
178           pixbuf_loader = NULL;
179         }
180     }
181   else
182     {
183       gchar *filename;
184       gchar *error_message = NULL;
185       GError *error = NULL;
186
187       /* demo_find_file() looks in the current directory first,
188        * so you can run gtk-demo without installing GTK, then looks
189        * in the location where the file is installed.
190        */
191       filename = demo_find_file ("alphatest.png", &error);
192       if (error)
193         {
194           error_message = g_strdup (error->message);
195           g_error_free (error);
196         }
197       else
198         {
199           image_stream = g_fopen (filename, "rb");
200           g_free (filename);
201
202           if (!image_stream)
203             error_message = g_strdup_printf ("Unable to open image file 'alphatest.png': %s",
204                                              g_strerror (errno));
205         }
206
207       if (image_stream == NULL)
208         {
209           GtkWidget *dialog;
210
211           dialog = gtk_message_dialog_new (GTK_WINDOW (window),
212                                            GTK_DIALOG_DESTROY_WITH_PARENT,
213                                            GTK_MESSAGE_ERROR,
214                                            GTK_BUTTONS_CLOSE,
215                                            "%s", error_message);
216           g_free (error_message);
217
218           g_signal_connect (dialog, "response",
219                             G_CALLBACK (gtk_widget_destroy), NULL);
220
221           gtk_widget_show (dialog);
222
223           load_timeout = 0;
224
225           return FALSE; /* uninstall the timeout */
226         }
227
228       if (pixbuf_loader)
229         {
230           gdk_pixbuf_loader_close (pixbuf_loader, NULL);
231           g_object_unref (pixbuf_loader);
232           pixbuf_loader = NULL;
233         }
234
235       pixbuf_loader = gdk_pixbuf_loader_new ();
236
237       g_signal_connect (pixbuf_loader, "area-prepared",
238                         G_CALLBACK (progressive_prepared_callback), image);
239
240       g_signal_connect (pixbuf_loader, "area-updated",
241                         G_CALLBACK (progressive_updated_callback), image);
242     }
243
244   /* leave timeout installed */
245   return TRUE;
246 }
247
248 static void
249 start_progressive_loading (GtkWidget *image)
250 {
251   /* This is obviously totally contrived (we slow down loading
252    * on purpose to show how incremental loading works).
253    * The real purpose of incremental loading is the case where
254    * you are reading data from a slow source such as the network.
255    * The timeout simply simulates a slow data source by inserting
256    * pauses in the reading process.
257    */
258   load_timeout = gdk_threads_add_timeout (150,
259                                 progressive_timeout,
260                                 image);
261 }
262
263 static void
264 cleanup_callback (GObject   *object,
265                   gpointer   data)
266 {
267   if (load_timeout)
268     {
269       g_source_remove (load_timeout);
270       load_timeout = 0;
271     }
272
273   if (pixbuf_loader)
274     {
275       gdk_pixbuf_loader_close (pixbuf_loader, NULL);
276       g_object_unref (pixbuf_loader);
277       pixbuf_loader = NULL;
278     }
279
280   if (image_stream)
281     fclose (image_stream);
282   image_stream = NULL;
283 }
284
285 static void
286 toggle_sensitivity_callback (GtkWidget *togglebutton,
287                              gpointer   user_data)
288 {
289   GtkContainer *container = user_data;
290   GList *list;
291   GList *tmp;
292
293   list = gtk_container_get_children (container);
294
295   tmp = list;
296   while (tmp != NULL)
297     {
298       /* don't disable our toggle */
299       if (GTK_WIDGET (tmp->data) != togglebutton)
300         gtk_widget_set_sensitive (GTK_WIDGET (tmp->data),
301                                   !gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (togglebutton)));
302
303       tmp = tmp->next;
304     }
305
306   g_list_free (list);
307 }
308
309
310 GtkWidget *
311 do_images (GtkWidget *do_widget)
312 {
313   GtkWidget *frame;
314   GtkWidget *vbox;
315   GtkWidget *image;
316   GtkWidget *label;
317   GtkWidget *align;
318   GtkWidget *button;
319   GdkPixbuf *pixbuf;
320   GIcon     *gicon;
321   GError *error = NULL;
322   char *filename;
323
324   if (!window)
325     {
326       window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
327       gtk_window_set_screen (GTK_WINDOW (window),
328                              gtk_widget_get_screen (do_widget));
329       gtk_window_set_title (GTK_WINDOW (window), "Images");
330
331       g_signal_connect (window, "destroy",
332                         G_CALLBACK (gtk_widget_destroyed), &window);
333       g_signal_connect (window, "destroy",
334                         G_CALLBACK (cleanup_callback), NULL);
335
336       gtk_container_set_border_width (GTK_CONTAINER (window), 8);
337
338       vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, FALSE, 8);
339       gtk_container_set_border_width (GTK_CONTAINER (vbox), 8);
340       gtk_container_add (GTK_CONTAINER (window), vbox);
341
342       label = gtk_label_new (NULL);
343       gtk_label_set_markup (GTK_LABEL (label),
344                             "<u>Image loaded from a file</u>");
345       gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
346
347       frame = gtk_frame_new (NULL);
348       gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
349       /* The alignment keeps the frame from growing when users resize
350        * the window
351        */
352       align = gtk_alignment_new (0.5, 0.5, 0, 0);
353       gtk_container_add (GTK_CONTAINER (align), frame);
354       gtk_box_pack_start (GTK_BOX (vbox), align, FALSE, FALSE, 0);
355
356       /* demo_find_file() looks in the current directory first,
357        * so you can run gtk-demo without installing GTK, then looks
358        * in the location where the file is installed.
359        */
360       pixbuf = NULL;
361       filename = demo_find_file ("gtk-logo-rgb.gif", &error);
362       if (filename)
363         {
364           pixbuf = gdk_pixbuf_new_from_file (filename, &error);
365           g_free (filename);
366         }
367
368       if (error)
369         {
370           /* This code shows off error handling. You can just use
371            * gtk_image_new_from_file() instead if you don't want to report
372            * errors to the user. If the file doesn't load when using
373            * gtk_image_new_from_file(), a "missing image" icon will
374            * be displayed instead.
375            */
376           GtkWidget *dialog;
377
378           dialog = gtk_message_dialog_new (GTK_WINDOW (window),
379                                            GTK_DIALOG_DESTROY_WITH_PARENT,
380                                            GTK_MESSAGE_ERROR,
381                                            GTK_BUTTONS_CLOSE,
382                                            "Unable to open image file 'gtk-logo-rgb.gif': %s",
383                                            error->message);
384           g_error_free (error);
385
386           g_signal_connect (dialog, "response",
387                             G_CALLBACK (gtk_widget_destroy), NULL);
388
389           gtk_widget_show (dialog);
390         }
391
392       image = gtk_image_new_from_pixbuf (pixbuf);
393
394       gtk_container_add (GTK_CONTAINER (frame), image);
395
396
397       /* Animation */
398
399       label = gtk_label_new (NULL);
400       gtk_label_set_markup (GTK_LABEL (label),
401                             "<u>Animation loaded from a file</u>");
402       gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
403
404       frame = gtk_frame_new (NULL);
405       gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
406       /* The alignment keeps the frame from growing when users resize
407        * the window
408        */
409       align = gtk_alignment_new (0.5, 0.5, 0, 0);
410       gtk_container_add (GTK_CONTAINER (align), frame);
411       gtk_box_pack_start (GTK_BOX (vbox), align, FALSE, FALSE, 0);
412
413       filename = demo_find_file ("floppybuddy.gif", NULL);
414       image = gtk_image_new_from_file (filename);
415       g_free (filename);
416
417       gtk_container_add (GTK_CONTAINER (frame), image);
418
419       /* Symbolic icon */
420
421       label = gtk_label_new (NULL);
422       gtk_label_set_markup (GTK_LABEL (label),
423                             "<u>Symbolic themed icon</u>");
424       gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
425
426       frame = gtk_frame_new (NULL);
427       gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
428       /* The alignment keeps the frame from growing when users resize
429        * the window
430        */
431       align = gtk_alignment_new (0.5, 0.5, 0, 0);
432       gtk_container_add (GTK_CONTAINER (align), frame);
433       gtk_box_pack_start (GTK_BOX (vbox), align, FALSE, FALSE, 0);
434
435       gicon = g_themed_icon_new_with_default_fallbacks ("battery-critical-charging-symbolic");
436       image = gtk_image_new_from_gicon (gicon, GTK_ICON_SIZE_DIALOG);
437
438       gtk_container_add (GTK_CONTAINER (frame), image);
439
440       /* Progressive */
441
442
443       label = gtk_label_new (NULL);
444       gtk_label_set_markup (GTK_LABEL (label),
445                             "<u>Progressive image loading</u>");
446       gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
447
448       frame = gtk_frame_new (NULL);
449       gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
450       /* The alignment keeps the frame from growing when users resize
451        * the window
452        */
453       align = gtk_alignment_new (0.5, 0.5, 0, 0);
454       gtk_container_add (GTK_CONTAINER (align), frame);
455       gtk_box_pack_start (GTK_BOX (vbox), align, FALSE, FALSE, 0);
456
457       /* Create an empty image for now; the progressive loader
458        * will create the pixbuf and fill it in.
459        */
460       image = gtk_image_new_from_pixbuf (NULL);
461       gtk_container_add (GTK_CONTAINER (frame), image);
462
463       start_progressive_loading (image);
464
465       /* Sensitivity control */
466       button = gtk_toggle_button_new_with_mnemonic ("_Insensitive");
467       gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
468
469       g_signal_connect (button, "toggled",
470                         G_CALLBACK (toggle_sensitivity_callback),
471                         vbox);
472     }
473
474   if (!gtk_widget_get_visible (window))
475     {
476       gtk_widget_show_all (window);
477     }
478   else
479     {
480       gtk_widget_destroy (window);
481       window = NULL;
482     }
483
484   return window;
485 }