3 * A GdkPixbuf represents an image, normally in RGB or RGBA format.
4 * Pixbufs are normally used to load files from disk and perform
7 * This demo is not all that educational, but looks cool. It was written
8 * by Extreme Pixbuf Hacker Federico Mena Quintero. It also shows
9 * off how to use GtkDrawingArea to do a simple animation.
11 * Look at the Image demo for additional pixbuf usage examples.
19 #include "demo-common.h"
21 #define FRAME_DELAY 50
23 #define BACKGROUND_NAME "background.jpg"
25 static const char *image_names[] = {
36 #define N_IMAGES G_N_ELEMENTS (image_names)
39 static GtkWidget *window = NULL;
42 static GdkPixbuf *frame;
44 /* Background image */
45 static GdkPixbuf *background;
46 static gint back_width, back_height;
49 static GdkPixbuf *images[N_IMAGES];
54 /* Loads the images for the demo and returns whether the operation succeeded */
56 load_pixbufs (GError **error)
62 return TRUE; /* already loaded earlier */
64 /* demo_find_file() looks in the current directory first,
65 * so you can run gtk-demo without installing GTK, then looks
66 * in the location where the file is installed.
68 filename = demo_find_file (BACKGROUND_NAME, error);
70 return FALSE; /* note that "error" was filled in and returned */
72 background = gdk_pixbuf_new_from_file (filename, error);
76 return FALSE; /* Note that "error" was filled with a GError */
78 back_width = gdk_pixbuf_get_width (background);
79 back_height = gdk_pixbuf_get_height (background);
81 for (i = 0; i < N_IMAGES; i++)
83 filename = demo_find_file (image_names[i], error);
85 return FALSE; /* Note that "error" was filled with a GError */
87 images[i] = gdk_pixbuf_new_from_file (filename, error);
91 return FALSE; /* Note that "error" was filled with a GError */
97 /* Expose callback for the drawing area */
99 expose_cb (GtkWidget *widget,
100 GdkEventExpose *event,
106 rowstride = gdk_pixbuf_get_rowstride (frame);
108 pixels = gdk_pixbuf_get_pixels (frame) + rowstride * event->area.y + event->area.x * 3;
110 gdk_draw_rgb_image_dithalign (widget->window,
111 widget->style->black_gc,
112 event->area.x, event->area.y,
113 event->area.width, event->area.height,
114 GDK_RGB_DITHER_NORMAL,
116 event->area.x, event->area.y);
123 static int frame_num;
125 /* Timeout handler to regenerate the frame */
127 timeout (gpointer data)
134 gdk_pixbuf_copy_area (background, 0, 0, back_width, back_height,
137 f = (double) (frame_num % CYCLE_LEN) / CYCLE_LEN;
139 xmid = back_width / 2.0;
140 ymid = back_height / 2.0;
142 radius = MIN (xmid, ymid) / 2.0;
144 for (i = 0; i < N_IMAGES; i++)
150 GdkRectangle r1, r2, dest;
153 ang = 2.0 * G_PI * (double) i / N_IMAGES - f * 2.0 * G_PI;
155 iw = gdk_pixbuf_get_width (images[i]);
156 ih = gdk_pixbuf_get_height (images[i]);
158 r = radius + (radius / 3.0) * sin (f * 2.0 * G_PI);
160 xpos = floor (xmid + r * cos (ang) - iw / 2.0 + 0.5);
161 ypos = floor (ymid + r * sin (ang) - ih / 2.0 + 0.5);
163 k = (i & 1) ? sin (f * 2.0 * G_PI) : cos (f * 2.0 * G_PI);
174 r2.width = back_width;
175 r2.height = back_height;
177 if (gdk_rectangle_intersect (&r1, &r2, &dest))
178 gdk_pixbuf_composite (images[i],
181 dest.width, dest.height,
186 ? MAX (127, fabs (255 * sin (f * 2.0 * G_PI)))
187 : MAX (127, fabs (255 * cos (f * 2.0 * G_PI)))));
190 GDK_THREADS_ENTER ();
191 gtk_widget_queue_draw (da);
192 GDK_THREADS_LEAVE ();
198 static guint timeout_id;
201 cleanup_callback (GtkObject *object,
204 g_source_remove (timeout_id);
209 do_pixbufs (GtkWidget *do_widget)
215 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
216 gtk_window_set_screen (GTK_WINDOW (window),
217 gtk_widget_get_screen (do_widget));
218 gtk_window_set_title (GTK_WINDOW (window), "Pixbufs");
219 gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
221 g_signal_connect (window, "destroy",
222 G_CALLBACK (gtk_widget_destroyed), &window);
223 g_signal_connect (window, "destroy",
224 G_CALLBACK (cleanup_callback), NULL);
228 if (!load_pixbufs (&error))
232 dialog = gtk_message_dialog_new (GTK_WINDOW (window),
233 GTK_DIALOG_DESTROY_WITH_PARENT,
236 "Failed to load an image: %s",
239 g_error_free (error);
241 g_signal_connect (dialog, "response",
242 G_CALLBACK (gtk_widget_destroy), NULL);
244 gtk_widget_show (dialog);
248 gtk_widget_set_size_request (window, back_width, back_height);
250 frame = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, back_width, back_height);
252 da = gtk_drawing_area_new ();
254 g_signal_connect (da, "expose-event",
255 G_CALLBACK (expose_cb), NULL);
257 gtk_container_add (GTK_CONTAINER (window), da);
259 timeout_id = g_timeout_add (FRAME_DELAY, timeout, NULL);
263 if (!GTK_WIDGET_VISIBLE (window))
265 gtk_widget_show_all (window);
269 gtk_widget_destroy (window);
271 g_object_unref (frame);