2001-04-18 18:09:18 +00:00
|
|
|
/* Pixbufs
|
|
|
|
*
|
|
|
|
* A GdkPixbuf represents an image, normally in RGB or RGBA format.
|
|
|
|
* Pixbufs are normally used to load files from disk and perform
|
|
|
|
* image scaling.
|
|
|
|
*
|
|
|
|
* This demo is not all that educational, but looks cool. It was written
|
|
|
|
* by Extreme Pixbuf Hacker Federico Mena Quintero. It also shows
|
|
|
|
* off how to use GtkDrawingArea to do a simple animation.
|
|
|
|
*
|
|
|
|
* Look at the Image demo for additional pixbuf usage examples.
|
2001-05-18 16:28:30 +00:00
|
|
|
*
|
2001-04-18 18:09:18 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <gtk/gtk.h>
|
|
|
|
#include <math.h>
|
|
|
|
|
2013-01-30 15:57:13 +00:00
|
|
|
#define BACKGROUND_NAME "/pixbufs/background.jpg"
|
2001-04-18 18:09:18 +00:00
|
|
|
|
2001-11-23 21:46:44 +00:00
|
|
|
static const char *image_names[] = {
|
2013-01-30 15:57:13 +00:00
|
|
|
"/pixbufs/apple-red.png",
|
|
|
|
"/pixbufs/gnome-applets.png",
|
|
|
|
"/pixbufs/gnome-calendar.png",
|
|
|
|
"/pixbufs/gnome-foot.png",
|
|
|
|
"/pixbufs/gnome-gmush.png",
|
|
|
|
"/pixbufs/gnome-gimp.png",
|
|
|
|
"/pixbufs/gnome-gsame.png",
|
|
|
|
"/pixbufs/gnu-keys.png"
|
2001-04-18 18:09:18 +00:00
|
|
|
};
|
|
|
|
|
2001-11-23 21:46:44 +00:00
|
|
|
#define N_IMAGES G_N_ELEMENTS (image_names)
|
2001-04-18 18:09:18 +00:00
|
|
|
|
|
|
|
/* demo window */
|
2001-05-18 18:30:59 +00:00
|
|
|
static GtkWidget *window = NULL;
|
2001-04-18 18:09:18 +00:00
|
|
|
|
|
|
|
/* Current frame */
|
|
|
|
static GdkPixbuf *frame;
|
|
|
|
|
|
|
|
/* Background image */
|
|
|
|
static GdkPixbuf *background;
|
2001-05-18 16:28:30 +00:00
|
|
|
static gint back_width, back_height;
|
2001-04-18 18:09:18 +00:00
|
|
|
|
|
|
|
/* Images */
|
|
|
|
static GdkPixbuf *images[N_IMAGES];
|
|
|
|
|
|
|
|
/* Widgets */
|
|
|
|
static GtkWidget *da;
|
|
|
|
|
|
|
|
/* Loads the images for the demo and returns whether the operation succeeded */
|
|
|
|
static gboolean
|
|
|
|
load_pixbufs (GError **error)
|
|
|
|
{
|
2001-05-18 16:28:30 +00:00
|
|
|
gint i;
|
2001-04-18 18:09:18 +00:00
|
|
|
|
|
|
|
if (background)
|
|
|
|
return TRUE; /* already loaded earlier */
|
2001-05-18 16:28:30 +00:00
|
|
|
|
2013-01-30 15:57:13 +00:00
|
|
|
background = gdk_pixbuf_new_from_resource (BACKGROUND_NAME, error);
|
2001-04-18 18:09:18 +00:00
|
|
|
if (!background)
|
2001-11-23 21:46:44 +00:00
|
|
|
return FALSE; /* Note that "error" was filled with a GError */
|
2001-05-18 16:28:30 +00:00
|
|
|
|
2001-04-18 18:09:18 +00:00
|
|
|
back_width = gdk_pixbuf_get_width (background);
|
|
|
|
back_height = gdk_pixbuf_get_height (background);
|
|
|
|
|
|
|
|
for (i = 0; i < N_IMAGES; i++)
|
|
|
|
{
|
2013-01-30 15:57:13 +00:00
|
|
|
images[i] = gdk_pixbuf_new_from_resource (image_names[i], error);
|
2011-09-02 03:55:47 +00:00
|
|
|
|
2001-04-18 18:09:18 +00:00
|
|
|
if (!images[i])
|
2008-07-06 07:24:02 +00:00
|
|
|
return FALSE; /* Note that "error" was filled with a GError */
|
2001-04-18 18:09:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Expose callback for the drawing area */
|
2016-11-18 05:16:38 +00:00
|
|
|
static void
|
|
|
|
draw_func (GtkDrawingArea *area,
|
|
|
|
cairo_t *cr,
|
|
|
|
int width,
|
|
|
|
int height,
|
|
|
|
gpointer data)
|
2001-04-18 18:09:18 +00:00
|
|
|
{
|
2010-07-13 13:57:18 +00:00
|
|
|
gdk_cairo_set_source_pixbuf (cr, frame, 0, 0);
|
2010-09-08 19:53:26 +00:00
|
|
|
cairo_paint (cr);
|
2001-04-18 18:09:18 +00:00
|
|
|
}
|
|
|
|
|
2013-02-20 19:46:11 +00:00
|
|
|
#define CYCLE_TIME 3000000 /* 3 seconds */
|
2001-04-18 18:09:18 +00:00
|
|
|
|
2013-02-20 19:46:11 +00:00
|
|
|
static gint64 start_time;
|
2001-04-18 18:09:18 +00:00
|
|
|
|
2013-02-20 19:46:11 +00:00
|
|
|
/* Handler to regenerate the frame */
|
|
|
|
static gboolean
|
|
|
|
on_tick (GtkWidget *widget,
|
|
|
|
GdkFrameClock *frame_clock,
|
|
|
|
gpointer data)
|
2001-04-18 18:09:18 +00:00
|
|
|
{
|
2013-02-20 19:46:11 +00:00
|
|
|
gint64 current_time;
|
2001-04-18 18:09:18 +00:00
|
|
|
double f;
|
|
|
|
int i;
|
|
|
|
double xmid, ymid;
|
|
|
|
double radius;
|
|
|
|
|
|
|
|
gdk_pixbuf_copy_area (background, 0, 0, back_width, back_height,
|
2008-07-06 07:24:02 +00:00
|
|
|
frame, 0, 0);
|
2001-04-18 18:09:18 +00:00
|
|
|
|
2013-02-20 19:46:11 +00:00
|
|
|
if (start_time == 0)
|
|
|
|
start_time = gdk_frame_clock_get_frame_time (frame_clock);
|
|
|
|
|
|
|
|
current_time = gdk_frame_clock_get_frame_time (frame_clock);
|
|
|
|
f = ((current_time - start_time) % CYCLE_TIME) / (double)CYCLE_TIME;
|
2001-04-18 18:09:18 +00:00
|
|
|
|
|
|
|
xmid = back_width / 2.0;
|
|
|
|
ymid = back_height / 2.0;
|
|
|
|
|
|
|
|
radius = MIN (xmid, ymid) / 2.0;
|
|
|
|
|
|
|
|
for (i = 0; i < N_IMAGES; i++)
|
|
|
|
{
|
|
|
|
double ang;
|
|
|
|
int xpos, ypos;
|
|
|
|
int iw, ih;
|
|
|
|
double r;
|
|
|
|
GdkRectangle r1, r2, dest;
|
|
|
|
double k;
|
|
|
|
|
2001-11-25 23:36:29 +00:00
|
|
|
ang = 2.0 * G_PI * (double) i / N_IMAGES - f * 2.0 * G_PI;
|
2001-04-18 18:09:18 +00:00
|
|
|
|
|
|
|
iw = gdk_pixbuf_get_width (images[i]);
|
|
|
|
ih = gdk_pixbuf_get_height (images[i]);
|
|
|
|
|
2001-11-25 23:36:29 +00:00
|
|
|
r = radius + (radius / 3.0) * sin (f * 2.0 * G_PI);
|
2001-04-18 18:09:18 +00:00
|
|
|
|
|
|
|
xpos = floor (xmid + r * cos (ang) - iw / 2.0 + 0.5);
|
|
|
|
ypos = floor (ymid + r * sin (ang) - ih / 2.0 + 0.5);
|
|
|
|
|
2001-11-25 23:36:29 +00:00
|
|
|
k = (i & 1) ? sin (f * 2.0 * G_PI) : cos (f * 2.0 * G_PI);
|
2001-04-18 18:09:18 +00:00
|
|
|
k = 2.0 * k * k;
|
|
|
|
k = MAX (0.25, k);
|
|
|
|
|
|
|
|
r1.x = xpos;
|
|
|
|
r1.y = ypos;
|
|
|
|
r1.width = iw * k;
|
|
|
|
r1.height = ih * k;
|
|
|
|
|
|
|
|
r2.x = 0;
|
|
|
|
r2.y = 0;
|
|
|
|
r2.width = back_width;
|
|
|
|
r2.height = back_height;
|
|
|
|
|
|
|
|
if (gdk_rectangle_intersect (&r1, &r2, &dest))
|
2008-07-06 07:24:02 +00:00
|
|
|
gdk_pixbuf_composite (images[i],
|
|
|
|
frame,
|
|
|
|
dest.x, dest.y,
|
|
|
|
dest.width, dest.height,
|
|
|
|
xpos, ypos,
|
|
|
|
k, k,
|
|
|
|
GDK_INTERP_NEAREST,
|
|
|
|
((i & 1)
|
|
|
|
? MAX (127, fabs (255 * sin (f * 2.0 * G_PI)))
|
|
|
|
: MAX (127, fabs (255 * cos (f * 2.0 * G_PI)))));
|
2001-04-18 18:09:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gtk_widget_queue_draw (da);
|
|
|
|
|
2012-01-31 00:12:27 +00:00
|
|
|
return G_SOURCE_CONTINUE;
|
2001-04-18 18:09:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GtkWidget *
|
2003-11-08 22:08:05 +00:00
|
|
|
do_pixbufs (GtkWidget *do_widget)
|
2001-04-18 18:09:18 +00:00
|
|
|
{
|
|
|
|
if (!window)
|
|
|
|
{
|
|
|
|
GError *error;
|
|
|
|
|
2020-02-14 19:55:36 +00:00
|
|
|
window = gtk_window_new ();
|
2017-10-31 06:41:15 +00:00
|
|
|
gtk_window_set_display (GTK_WINDOW (window),
|
|
|
|
gtk_widget_get_display (do_widget));
|
2001-04-18 18:09:18 +00:00
|
|
|
gtk_window_set_title (GTK_WINDOW (window), "Pixbufs");
|
2001-06-25 23:48:51 +00:00
|
|
|
gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
|
2001-05-18 16:28:30 +00:00
|
|
|
|
2008-08-11 19:12:58 +00:00
|
|
|
g_signal_connect (window, "destroy",
|
|
|
|
G_CALLBACK (gtk_widget_destroyed), &window);
|
2001-04-18 18:09:18 +00:00
|
|
|
|
|
|
|
error = NULL;
|
|
|
|
if (!load_pixbufs (&error))
|
2008-07-06 07:24:02 +00:00
|
|
|
{
|
|
|
|
GtkWidget *dialog;
|
2001-05-18 16:28:30 +00:00
|
|
|
|
2008-07-06 07:24:02 +00:00
|
|
|
dialog = gtk_message_dialog_new (GTK_WINDOW (window),
|
|
|
|
GTK_DIALOG_DESTROY_WITH_PARENT,
|
|
|
|
GTK_MESSAGE_ERROR,
|
|
|
|
GTK_BUTTONS_CLOSE,
|
|
|
|
"Failed to load an image: %s",
|
|
|
|
error->message);
|
2001-05-18 16:28:30 +00:00
|
|
|
|
2008-07-06 07:24:02 +00:00
|
|
|
g_error_free (error);
|
2001-05-18 16:28:30 +00:00
|
|
|
|
2008-07-06 07:24:02 +00:00
|
|
|
g_signal_connect (dialog, "response",
|
|
|
|
G_CALLBACK (gtk_widget_destroy), NULL);
|
2001-05-18 16:28:30 +00:00
|
|
|
|
2008-07-06 07:24:02 +00:00
|
|
|
gtk_widget_show (dialog);
|
|
|
|
}
|
2001-04-18 18:09:18 +00:00
|
|
|
else
|
2008-07-06 07:24:02 +00:00
|
|
|
{
|
|
|
|
frame = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, back_width, back_height);
|
2001-05-18 16:28:30 +00:00
|
|
|
|
2008-07-06 07:24:02 +00:00
|
|
|
da = gtk_drawing_area_new ();
|
2001-05-18 16:28:30 +00:00
|
|
|
|
2016-11-18 05:16:38 +00:00
|
|
|
gtk_drawing_area_set_content_width (GTK_DRAWING_AREA (da), back_width);
|
|
|
|
gtk_drawing_area_set_content_height (GTK_DRAWING_AREA (da), back_height);
|
|
|
|
gtk_drawing_area_set_draw_func (GTK_DRAWING_AREA (da), draw_func, NULL, NULL);
|
2001-05-18 16:28:30 +00:00
|
|
|
|
2008-07-06 07:24:02 +00:00
|
|
|
gtk_container_add (GTK_CONTAINER (window), da);
|
2001-05-18 16:28:30 +00:00
|
|
|
|
2013-02-20 19:46:11 +00:00
|
|
|
gtk_widget_add_tick_callback (da, on_tick, NULL, NULL);
|
2008-07-06 07:24:02 +00:00
|
|
|
}
|
2001-04-18 18:09:18 +00:00
|
|
|
}
|
2001-05-18 16:28:30 +00:00
|
|
|
|
2010-03-01 06:47:38 +00:00
|
|
|
if (!gtk_widget_get_visible (window))
|
2017-01-19 09:02:04 +00:00
|
|
|
gtk_widget_show (window);
|
2001-04-18 18:09:18 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
gtk_widget_destroy (window);
|
2004-09-22 04:55:19 +00:00
|
|
|
g_object_unref (frame);
|
2001-04-18 18:09:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return window;
|
|
|
|
}
|