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 #define BACKGROUND_NAME "/pixbufs/background.jpg"
21 static const char *image_names[] = {
22 "/pixbufs/apple-red.png",
23 "/pixbufs/gnome-applets.png",
24 "/pixbufs/gnome-calendar.png",
25 "/pixbufs/gnome-foot.png",
26 "/pixbufs/gnome-gmush.png",
27 "/pixbufs/gnome-gimp.png",
28 "/pixbufs/gnome-gsame.png",
29 "/pixbufs/gnu-keys.png"
32 #define N_IMAGES G_N_ELEMENTS (image_names)
35 static GtkWidget *window = NULL;
38 static GdkPixbuf *frame;
40 /* Background image */
41 static GdkPixbuf *background;
42 static gint back_width, back_height;
45 static GdkPixbuf *images[N_IMAGES];
50 /* Loads the images for the demo and returns whether the operation succeeded */
52 load_pixbufs (GError **error)
57 return TRUE; /* already loaded earlier */
59 background = gdk_pixbuf_new_from_resource (BACKGROUND_NAME, error);
61 return FALSE; /* Note that "error" was filled with a GError */
63 back_width = gdk_pixbuf_get_width (background);
64 back_height = gdk_pixbuf_get_height (background);
66 for (i = 0; i < N_IMAGES; i++)
68 images[i] = gdk_pixbuf_new_from_resource (image_names[i], error);
71 return FALSE; /* Note that "error" was filled with a GError */
77 /* Expose callback for the drawing area */
79 draw_cb (GtkWidget *widget,
83 gdk_cairo_set_source_pixbuf (cr, frame, 0, 0);
89 #define CYCLE_TIME 3000000 /* 3 seconds */
91 static gint64 start_time;
93 /* Handler to regenerate the frame */
95 on_tick (GtkWidget *widget,
96 GdkFrameClock *frame_clock,
105 gdk_pixbuf_copy_area (background, 0, 0, back_width, back_height,
109 start_time = gdk_frame_clock_get_frame_time (frame_clock);
111 current_time = gdk_frame_clock_get_frame_time (frame_clock);
112 f = ((current_time - start_time) % CYCLE_TIME) / (double)CYCLE_TIME;
114 xmid = back_width / 2.0;
115 ymid = back_height / 2.0;
117 radius = MIN (xmid, ymid) / 2.0;
119 for (i = 0; i < N_IMAGES; i++)
125 GdkRectangle r1, r2, dest;
128 ang = 2.0 * G_PI * (double) i / N_IMAGES - f * 2.0 * G_PI;
130 iw = gdk_pixbuf_get_width (images[i]);
131 ih = gdk_pixbuf_get_height (images[i]);
133 r = radius + (radius / 3.0) * sin (f * 2.0 * G_PI);
135 xpos = floor (xmid + r * cos (ang) - iw / 2.0 + 0.5);
136 ypos = floor (ymid + r * sin (ang) - ih / 2.0 + 0.5);
138 k = (i & 1) ? sin (f * 2.0 * G_PI) : cos (f * 2.0 * G_PI);
149 r2.width = back_width;
150 r2.height = back_height;
152 if (gdk_rectangle_intersect (&r1, &r2, &dest))
153 gdk_pixbuf_composite (images[i],
156 dest.width, dest.height,
161 ? MAX (127, fabs (255 * sin (f * 2.0 * G_PI)))
162 : MAX (127, fabs (255 * cos (f * 2.0 * G_PI)))));
165 gtk_widget_queue_draw (da);
167 return G_SOURCE_CONTINUE;
171 do_pixbufs (GtkWidget *do_widget)
177 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
178 gtk_window_set_screen (GTK_WINDOW (window),
179 gtk_widget_get_screen (do_widget));
180 gtk_window_set_title (GTK_WINDOW (window), "Pixbufs");
181 gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
183 g_signal_connect (window, "destroy",
184 G_CALLBACK (gtk_widget_destroyed), &window);
188 if (!load_pixbufs (&error))
192 dialog = gtk_message_dialog_new (GTK_WINDOW (window),
193 GTK_DIALOG_DESTROY_WITH_PARENT,
196 "Failed to load an image: %s",
199 g_error_free (error);
201 g_signal_connect (dialog, "response",
202 G_CALLBACK (gtk_widget_destroy), NULL);
204 gtk_widget_show (dialog);
208 gtk_widget_set_size_request (window, back_width, back_height);
210 frame = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, back_width, back_height);
212 da = gtk_drawing_area_new ();
214 g_signal_connect (da, "draw",
215 G_CALLBACK (draw_cb), NULL);
217 gtk_container_add (GTK_CONTAINER (window), da);
219 gtk_widget_add_tick_callback (da, on_tick, NULL, NULL);
223 if (!gtk_widget_get_visible (window))
225 gtk_widget_show_all (window);
229 gtk_widget_destroy (window);
231 g_object_unref (frame);