| 1 |
/* Pixbufs |
| 2 |
* |
| 3 |
* A GdkPixbuf represents an image, normally in RGB or RGBA format. |
| 4 |
* Pixbufs are normally used to load files from disk and perform |
| 5 |
* image scaling. |
| 6 |
* |
| 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. |
| 10 |
* |
| 11 |
* Look at the Image demo for additional pixbuf usage examples. |
| 12 |
* |
| 13 |
*/ |
| 14 |
|
| 15 |
#include <stdlib.h> |
| 16 |
#include <gtk/gtk.h> |
| 17 |
#include <math.h> |
| 18 |
|
| 19 |
#define BACKGROUND_NAME "/pixbufs/background.jpg" |
| 20 |
|
| 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" |
| 30 |
}; |
| 31 |
|
| 32 |
#define N_IMAGES G_N_ELEMENTS (image_names) |
| 33 |
|
| 34 |
/* demo window */ |
| 35 |
static GtkWidget *window = NULL; |
| 36 |
|
| 37 |
/* Current frame */ |
| 38 |
static GdkPixbuf *frame; |
| 39 |
|
| 40 |
/* Background image */ |
| 41 |
static GdkPixbuf *background; |
| 42 |
static gint back_width, back_height; |
| 43 |
|
| 44 |
/* Images */ |
| 45 |
static GdkPixbuf *images[N_IMAGES]; |
| 46 |
|
| 47 |
/* Widgets */ |
| 48 |
static GtkWidget *da; |
| 49 |
|
| 50 |
/* Loads the images for the demo and returns whether the operation succeeded */ |
| 51 |
static gboolean |
| 52 |
load_pixbufs (GError **error) |
| 53 |
{ |
| 54 |
gint i; |
| 55 |
|
| 56 |
if (background) |
| 57 |
return TRUE; /* already loaded earlier */ |
| 58 |
|
| 59 |
background = gdk_pixbuf_new_from_resource (BACKGROUND_NAME, error); |
| 60 |
if (!background) |
| 61 |
return FALSE; /* Note that "error" was filled with a GError */ |
| 62 |
|
| 63 |
back_width = gdk_pixbuf_get_width (background); |
| 64 |
back_height = gdk_pixbuf_get_height (background); |
| 65 |
|
| 66 |
for (i = 0; i < N_IMAGES; i++) |
| 67 |
{ |
| 68 |
images[i] = gdk_pixbuf_new_from_resource (image_names[i], error); |
| 69 |
|
| 70 |
if (!images[i]) |
| 71 |
return FALSE; /* Note that "error" was filled with a GError */ |
| 72 |
} |
| 73 |
|
| 74 |
return TRUE; |
| 75 |
} |
| 76 |
|
| 77 |
/* Expose callback for the drawing area */ |
| 78 |
static gint |
| 79 |
draw_cb (GtkWidget *widget, |
| 80 |
cairo_t *cr, |
| 81 |
gpointer data) |
| 82 |
{ |
| 83 |
gdk_cairo_set_source_pixbuf (cr, frame, 0, 0); |
| 84 |
cairo_paint (cr); |
| 85 |
|
| 86 |
return TRUE; |
| 87 |
} |
| 88 |
|
| 89 |
#define CYCLE_TIME 3000000 /* 3 seconds */ |
| 90 |
|
| 91 |
static gint64 start_time; |
| 92 |
|
| 93 |
/* Handler to regenerate the frame */ |
| 94 |
static gboolean |
| 95 |
on_tick (GtkWidget *widget, |
| 96 |
GdkFrameClock *frame_clock, |
| 97 |
gpointer data) |
| 98 |
{ |
| 99 |
gint64 current_time; |
| 100 |
double f; |
| 101 |
int i; |
| 102 |
double xmid, ymid; |
| 103 |
double radius; |
| 104 |
|
| 105 |
gdk_pixbuf_copy_area (background, 0, 0, back_width, back_height, |
| 106 |
frame, 0, 0); |
| 107 |
|
| 108 |
if (start_time == 0) |
| 109 |
start_time = gdk_frame_clock_get_frame_time (frame_clock); |
| 110 |
|
| 111 |
current_time = gdk_frame_clock_get_frame_time (frame_clock); |
| 112 |
f = ((current_time - start_time) % CYCLE_TIME) / (double)CYCLE_TIME; |
| 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 |
{ |
| 121 |
double ang; |
| 122 |
int xpos, ypos; |
| 123 |
int iw, ih; |
| 124 |
double r; |
| 125 |
GdkRectangle r1, r2, dest; |
| 126 |
double k; |
| 127 |
|
| 128 |
ang = 2.0 * G_PI * (double) i / N_IMAGES - f * 2.0 * G_PI; |
| 129 |
|
| 130 |
iw = gdk_pixbuf_get_width (images[i]); |
| 131 |
ih = gdk_pixbuf_get_height (images[i]); |
| 132 |
|
| 133 |
r = radius + (radius / 3.0) * sin (f * 2.0 * G_PI); |
| 134 |
|
| 135 |
xpos = floor (xmid + r * cos (ang) - iw / 2.0 + 0.5); |
| 136 |
ypos = floor (ymid + r * sin (ang) - ih / 2.0 + 0.5); |
| 137 |
|
| 138 |
k = (i & 1) ? sin (f * 2.0 * G_PI) : cos (f * 2.0 * G_PI); |
| 139 |
k = 2.0 * k * k; |
| 140 |
k = MAX (0.25, k); |
| 141 |
|
| 142 |
r1.x = xpos; |
| 143 |
r1.y = ypos; |
| 144 |
r1.width = iw * k; |
| 145 |
r1.height = ih * k; |
| 146 |
|
| 147 |
r2.x = 0; |
| 148 |
r2.y = 0; |
| 149 |
r2.width = back_width; |
| 150 |
r2.height = back_height; |
| 151 |
|
| 152 |
if (gdk_rectangle_intersect (&r1, &r2, &dest)) |
| 153 |
gdk_pixbuf_composite (images[i], |
| 154 |
frame, |
| 155 |
dest.x, dest.y, |
| 156 |
dest.width, dest.height, |
| 157 |
xpos, ypos, |
| 158 |
k, k, |
| 159 |
GDK_INTERP_NEAREST, |
| 160 |
((i & 1) |
| 161 |
? MAX (127, fabs (255 * sin (f * 2.0 * G_PI))) |
| 162 |
: MAX (127, fabs (255 * cos (f * 2.0 * G_PI))))); |
| 163 |
} |
| 164 |
|
| 165 |
gtk_widget_queue_draw (da); |
| 166 |
|
| 167 |
return G_SOURCE_CONTINUE; |
| 168 |
} |
| 169 |
|
| 170 |
GtkWidget * |
| 171 |
do_pixbufs (GtkWidget *do_widget) |
| 172 |
{ |
| 173 |
if (!window) |
| 174 |
{ |
| 175 |
GError *error; |
| 176 |
|
| 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); |
| 182 |
|
| 183 |
g_signal_connect (window, "destroy", |
| 184 |
G_CALLBACK (gtk_widget_destroyed), &window); |
| 185 |
|
| 186 |
error = NULL; |
| 187 |
if (!load_pixbufs (&error)) |
| 188 |
{ |
| 189 |
GtkWidget *dialog; |
| 190 |
|
| 191 |
dialog = gtk_message_dialog_new (GTK_WINDOW (window), |
| 192 |
GTK_DIALOG_DESTROY_WITH_PARENT, |
| 193 |
GTK_MESSAGE_ERROR, |
| 194 |
GTK_BUTTONS_CLOSE, |
| 195 |
"Failed to load an image: %s", |
| 196 |
error->message); |
| 197 |
|
| 198 |
g_error_free (error); |
| 199 |
|
| 200 |
g_signal_connect (dialog, "response", |
| 201 |
G_CALLBACK (gtk_widget_destroy), NULL); |
| 202 |
|
| 203 |
gtk_widget_show (dialog); |
| 204 |
} |
| 205 |
else |
| 206 |
{ |
| 207 |
gtk_widget_set_size_request (window, back_width, back_height); |
| 208 |
|
| 209 |
frame = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, back_width, back_height); |
| 210 |
|
| 211 |
da = gtk_drawing_area_new (); |
| 212 |
|
| 213 |
g_signal_connect (da, "draw", |
| 214 |
G_CALLBACK (draw_cb), NULL); |
| 215 |
|
| 216 |
gtk_container_add (GTK_CONTAINER (window), da); |
| 217 |
|
| 218 |
gtk_widget_add_tick_callback (da, on_tick, NULL, NULL); |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
if (!gtk_widget_get_visible (window)) |
| 223 |
gtk_widget_show_all (window); |
| 224 |
else |
| 225 |
{ |
| 226 |
gtk_widget_destroy (window); |
| 227 |
g_object_unref (frame); |
| 228 |
} |
| 229 |
|
| 230 |
return window; |
| 231 |
} |