]> Pileus Git - ~andy/gtk/blob - demos/pixbuf-demo.c
Replace a lot of idle and timeout calls by the new gdk_threads api.
[~andy/gtk] / demos / 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, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #include <config.h>
24 #include <stdlib.h>
25 #include <gtk/gtk.h>
26 #include <math.h>
27
28 \f
29
30 #define FRAME_DELAY 50
31
32 #define BACKGROUND_NAME "background.jpg"
33
34 static const char *image_names[] = {
35         "apple-red.png",
36         "gnome-applets.png",
37         "gnome-calendar.png",
38         "gnome-foot.png",
39         "gnome-gmush.png",
40         "gnome-gimp.png",
41         "gnome-gsame.png",
42         "gnu-keys.png"
43 };
44
45 #define N_IMAGES (sizeof (image_names) / sizeof (image_names[0]))
46
47 /* Current frame */
48 static GdkPixbuf *frame;
49
50 /* Background image */
51 static GdkPixbuf *background;
52 static int back_width, back_height;
53
54 /* Images */
55 static GdkPixbuf *images[N_IMAGES];
56
57 /* Widgets */
58 static GtkWidget *da;
59
60 \f
61
62 /* Loads the images for the demo and returns whether the operation succeeded */
63 static gboolean
64 load_pixbufs (void)
65 {
66         int i;
67
68         /* We pass NULL for the error return location, we don't care
69          * about the error message.
70          */
71         
72         background = gdk_pixbuf_new_from_file (BACKGROUND_NAME, NULL);
73         if (!background)
74                 return FALSE;
75
76         back_width = gdk_pixbuf_get_width (background);
77         back_height = gdk_pixbuf_get_height (background);
78
79         for (i = 0; i < N_IMAGES; i++) {
80                 images[i] = gdk_pixbuf_new_from_file (image_names[i], NULL);
81                 if (!images[i])
82                         return FALSE;
83         }
84
85         return TRUE;
86 }
87
88 /* Expose callback for the drawing area */
89 static gint
90 expose_cb (GtkWidget *widget, GdkEventExpose *event, gpointer data)
91 {
92         guchar *pixels;
93         int rowstride;
94
95         rowstride = gdk_pixbuf_get_rowstride (frame);
96
97         pixels = gdk_pixbuf_get_pixels (frame) + rowstride * event->area.y + event->area.x * 3;
98                   
99         gdk_draw_rgb_image_dithalign (widget->window,
100                                       widget->style->black_gc,
101                                       event->area.x, event->area.y,
102                                       event->area.width, event->area.height,
103                                       GDK_RGB_DITHER_NORMAL,
104                                       pixels, rowstride,
105                                       event->area.x, event->area.y);
106
107         return TRUE;
108 }
109
110 #define CYCLE_LEN 60
111
112 static int frame_num;
113
114 /* Timeout handler to regenerate the frame */
115 static gint
116 timeout (gpointer data)
117 {
118         double f;
119         int i;
120         double xmid, ymid;
121         double radius;
122
123         gdk_pixbuf_copy_area (background, 0, 0, back_width, back_height,
124                               frame, 0, 0);
125
126         f = (double) (frame_num % CYCLE_LEN) / CYCLE_LEN;
127
128         xmid = back_width / 2.0;
129         ymid = back_height / 2.0;
130
131         radius = MIN (xmid, ymid) / 2.0;
132
133         for (i = 0; i < N_IMAGES; i++) {
134                 double ang;
135                 int xpos, ypos;
136                 int iw, ih;
137                 double r;
138                 GdkRectangle r1, r2, dest;
139                 double k;
140
141                 ang = 2.0 * G_PI * (double) i / N_IMAGES - f * 2.0 * G_PI;
142
143                 iw = gdk_pixbuf_get_width (images[i]);
144                 ih = gdk_pixbuf_get_height (images[i]);
145
146                 r = radius + (radius / 3.0) * sin (f * 2.0 * G_PI);
147
148                 xpos = floor (xmid + r * cos (ang) - iw / 2.0 + 0.5);
149                 ypos = floor (ymid + r * sin (ang) - ih / 2.0 + 0.5);
150
151                 k = (i & 1) ? sin (f * 2.0 * G_PI) : cos (f * 2.0 * G_PI);
152                 k = 2.0 * k * k;
153                 k = MAX (0.25, k);
154
155                 r1.x = xpos;
156                 r1.y = ypos;
157                 r1.width = iw * k;
158                 r1.height = ih * k;
159
160                 r2.x = 0;
161                 r2.y = 0;
162                 r2.width = back_width;
163                 r2.height = back_height;
164
165                 if (gdk_rectangle_intersect (&r1, &r2, &dest))
166                         gdk_pixbuf_composite (images[i],
167                                               frame,
168                                               dest.x, dest.y,
169                                               dest.width, dest.height,
170                                               xpos, ypos,
171                                               k, k,
172                                               GDK_INTERP_NEAREST,
173                                               ((i & 1)
174                                                ? MAX (127, fabs (255 * sin (f * 2.0 * G_PI)))
175                                                : MAX (127, fabs (255 * cos (f * 2.0 * G_PI)))));
176         }
177
178         gtk_widget_queue_draw (da);
179
180         frame_num++;
181         return TRUE;
182 }
183
184 static guint timeout_id;
185
186 /* Destroy handler for the window */
187 static void
188 destroy_cb (GtkObject *object, gpointer data)
189 {
190         g_source_remove (timeout_id);
191         timeout_id = 0;
192
193         gtk_main_quit ();
194 }
195
196 extern void pixbuf_init();
197
198 int
199 main (int argc, char **argv)
200 {
201         GtkWidget *window;
202
203         pixbuf_init ();
204         gtk_init (&argc, &argv);
205
206         if (!load_pixbufs ()) {
207                 g_message ("main(): Could not load all the pixbufs!");
208                 exit (EXIT_FAILURE);
209         }
210
211         frame = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, back_width, back_height);
212
213         window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
214
215         gtk_widget_set_size_request (window, back_width, back_height);
216         gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
217
218         g_signal_connect (window, "destroy",
219                           G_CALLBACK (destroy_cb), NULL);
220
221         da = gtk_drawing_area_new ();
222
223         g_signal_connect (da, "expose_event",
224                           G_CALLBACK (expose_cb), NULL);
225
226         gtk_container_add (GTK_CONTAINER (window), da);
227
228         timeout_id = gdk_threads_add_timeout (FRAME_DELAY, timeout, NULL);
229
230         gtk_widget_show_all (window);
231         gtk_main ();
232
233         return 0;
234 }