]> Pileus Git - ~andy/gtk/blob - demos/pixbuf-demo/pixbuf-demo.c
Change FSF Address
[~andy/gtk] / demos / pixbuf-demo / pixbuf-demo.c
1 /* GdkPixbuf library - Scaling and compositing demo
2  *
3  * Copyright (C) 1999 The Free Software Foundation
4  *
5  * Authors: Federico Mena-Quintero <federico@gimp.org>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include "config.h"
22 #include <stdlib.h>
23 #include <gtk/gtk.h>
24 #include <math.h>
25
26 \f
27
28 #define FRAME_DELAY 50
29
30 #define BACKGROUND_NAME "background.jpg"
31
32 static const char *image_names[] = {
33         "apple-red.png",
34         "gnome-applets.png",
35         "gnome-calendar.png",
36         "gnome-foot.png",
37         "gnome-gmush.png",
38         "gnome-gimp.png",
39         "gnome-gsame.png",
40         "gnu-keys.png"
41 };
42
43 #define N_IMAGES (sizeof (image_names) / sizeof (image_names[0]))
44
45 /* Current frame */
46 static GdkPixbuf *frame;
47
48 /* Background image */
49 static GdkPixbuf *background;
50 static int back_width, back_height;
51
52 /* Images */
53 static GdkPixbuf *images[N_IMAGES];
54
55 /* Widgets */
56 static GtkWidget *da;
57
58 \f
59
60 /* Loads the images for the demo and returns whether the operation succeeded */
61 static gboolean
62 load_pixbufs (void)
63 {
64         int i;
65
66         /* We pass NULL for the error return location, we don't care
67          * about the error message.
68          */
69         
70         background = gdk_pixbuf_new_from_file (BACKGROUND_NAME, NULL);
71         if (!background)
72                 return FALSE;
73
74         back_width = gdk_pixbuf_get_width (background);
75         back_height = gdk_pixbuf_get_height (background);
76
77         for (i = 0; i < N_IMAGES; i++) {
78                 images[i] = gdk_pixbuf_new_from_file (image_names[i], NULL);
79                 if (!images[i])
80                         return FALSE;
81         }
82
83         return TRUE;
84 }
85
86 /* Expose callback for the drawing area */
87 static gboolean
88 draw_cb (GtkWidget *widget, cairo_t *cr, gpointer data)
89 {
90         gdk_cairo_set_source_pixbuf (cr, frame, 0, 0);
91         cairo_paint (cr);
92
93         return TRUE;
94 }
95
96 #define CYCLE_LEN 60
97
98 static int frame_num;
99
100 /* Timeout handler to regenerate the frame */
101 static gint
102 timeout (gpointer data)
103 {
104         double f;
105         int i;
106         double xmid, ymid;
107         double radius;
108
109         gdk_pixbuf_copy_area (background, 0, 0, back_width, back_height,
110                               frame, 0, 0);
111
112         f = (double) (frame_num % CYCLE_LEN) / CYCLE_LEN;
113
114         xmid = back_width / 2.0;
115         ymid = back_height / 2.0;
116
117         radius = MIN (xmid, ymid) / 2.0;
118
119         for (i = 0; i < N_IMAGES; i++) {
120                 double ang;
121                 int xpos, ypos;
122                 int iw, ih;
123                 double r;
124                 GdkRectangle r1, r2, dest;
125                 double k;
126
127                 ang = 2.0 * G_PI * (double) i / N_IMAGES - f * 2.0 * G_PI;
128
129                 iw = gdk_pixbuf_get_width (images[i]);
130                 ih = gdk_pixbuf_get_height (images[i]);
131
132                 r = radius + (radius / 3.0) * sin (f * 2.0 * G_PI);
133
134                 xpos = floor (xmid + r * cos (ang) - iw / 2.0 + 0.5);
135                 ypos = floor (ymid + r * sin (ang) - ih / 2.0 + 0.5);
136
137                 k = (i & 1) ? sin (f * 2.0 * G_PI) : cos (f * 2.0 * G_PI);
138                 k = 2.0 * k * k;
139                 k = MAX (0.25, k);
140
141                 r1.x = xpos;
142                 r1.y = ypos;
143                 r1.width = iw * k;
144                 r1.height = ih * k;
145
146                 r2.x = 0;
147                 r2.y = 0;
148                 r2.width = back_width;
149                 r2.height = back_height;
150
151                 if (gdk_rectangle_intersect (&r1, &r2, &dest))
152                         gdk_pixbuf_composite (images[i],
153                                               frame,
154                                               dest.x, dest.y,
155                                               dest.width, dest.height,
156                                               xpos, ypos,
157                                               k, k,
158                                               GDK_INTERP_NEAREST,
159                                               ((i & 1)
160                                                ? MAX (127, fabs (255 * sin (f * 2.0 * G_PI)))
161                                                : MAX (127, fabs (255 * cos (f * 2.0 * G_PI)))));
162         }
163
164         gtk_widget_queue_draw (da);
165
166         frame_num++;
167         return TRUE;
168 }
169
170 static guint timeout_id;
171
172 /* Destroy handler for the window */
173 static void
174 destroy_cb (GObject *object, gpointer data)
175 {
176         g_source_remove (timeout_id);
177         timeout_id = 0;
178
179         gtk_main_quit ();
180 }
181
182 int
183 main (int argc, char **argv)
184 {
185         GtkWidget *window;
186
187         gtk_init (&argc, &argv);
188
189         if (!load_pixbufs ()) {
190                 g_message ("main(): Could not load all the pixbufs!");
191                 exit (EXIT_FAILURE);
192         }
193
194         frame = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, back_width, back_height);
195
196         window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
197
198         gtk_widget_set_size_request (window, back_width, back_height);
199         gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
200
201         g_signal_connect (window, "destroy",
202                           G_CALLBACK (destroy_cb), NULL);
203
204         da = gtk_drawing_area_new ();
205
206         g_signal_connect (da, "draw",
207                           G_CALLBACK (draw_cb), NULL);
208
209         gtk_container_add (GTK_CONTAINER (window), da);
210
211         timeout_id = gdk_threads_add_timeout (FRAME_DELAY, timeout, NULL);
212
213         gtk_widget_show_all (window);
214         gtk_main ();
215
216         return 0;
217 }