pixbufs.c 6.45 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/* 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.
12
 *
13 14 15 16 17 18
 */

#include <stdlib.h>
#include <gtk/gtk.h>
#include <math.h>

19 20
#include "demo-common.h"

21 22
#define FRAME_DELAY 50

23
#define BACKGROUND_NAME "background.jpg"
24

25
static const char *image_names[] = {
26 27 28 29 30 31 32 33 34 35
  "apple-red.png",
  "gnome-applets.png",
  "gnome-calendar.png",
  "gnome-foot.png",
  "gnome-gmush.png",
  "gnome-gimp.png",
  "gnome-gsame.png",
  "gnu-keys.png"
};

36
#define N_IMAGES G_N_ELEMENTS (image_names)
37 38

/* demo window */
39
static GtkWidget *window = NULL;
40 41 42 43 44 45

/* Current frame */
static GdkPixbuf *frame;

/* Background image */
static GdkPixbuf *background;
46
static gint back_width, back_height;
47 48 49 50 51 52 53 54 55 56 57

/* 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)
{
58
  gint i;
59
  char *filename;
60 61 62

  if (background)
    return TRUE; /* already loaded earlier */
63

Matthias Clasen's avatar
Matthias Clasen committed
64
  /* demo_find_file() looks in the current directory first,
65 66 67 68 69 70
   * so you can run gtk-demo without installing GTK, then looks
   * in the location where the file is installed.
   */
  filename = demo_find_file (BACKGROUND_NAME, error);
  if (!filename)
    return FALSE; /* note that "error" was filled in and returned */
71

72 73
  background = gdk_pixbuf_new_from_file (filename, error);
  g_free (filename);
Matthias Clasen's avatar
Matthias Clasen committed
74

75
  if (!background)
76
    return FALSE; /* Note that "error" was filled with a GError */
77

78 79 80 81 82
  back_width = gdk_pixbuf_get_width (background);
  back_height = gdk_pixbuf_get_height (background);

  for (i = 0; i < N_IMAGES; i++)
    {
83 84
      filename = demo_find_file (image_names[i], error);
      if (!filename)
Matthias Clasen's avatar
Matthias Clasen committed
85
        return FALSE; /* Note that "error" was filled with a GError */
Matthias Clasen's avatar
Matthias Clasen committed
86

87 88
      images[i] = gdk_pixbuf_new_from_file (filename, error);
      g_free (filename);
Matthias Clasen's avatar
Matthias Clasen committed
89

90
      if (!images[i])
Matthias Clasen's avatar
Matthias Clasen committed
91
        return FALSE; /* Note that "error" was filled with a GError */
92 93 94 95 96 97 98
    }

  return TRUE;
}

/* Expose callback for the drawing area */
static gint
99 100 101
draw_cb (GtkWidget *widget,
         cairo_t   *cr,
         gpointer   data)
102
{
103
  gdk_cairo_set_source_pixbuf (cr, frame, 0, 0);
104
  cairo_paint (cr);
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122

  return TRUE;
}

#define CYCLE_LEN 60

static int frame_num;

/* Timeout handler to regenerate the frame */
static gint
timeout (gpointer data)
{
  double f;
  int i;
  double xmid, ymid;
  double radius;

  gdk_pixbuf_copy_area (background, 0, 0, back_width, back_height,
Matthias Clasen's avatar
Matthias Clasen committed
123
                        frame, 0, 0);
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140

  f = (double) (frame_num % CYCLE_LEN) / CYCLE_LEN;

  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;

141
      ang = 2.0 * G_PI * (double) i / N_IMAGES - f * 2.0 * G_PI;
142 143 144 145

      iw = gdk_pixbuf_get_width (images[i]);
      ih = gdk_pixbuf_get_height (images[i]);

146
      r = radius + (radius / 3.0) * sin (f * 2.0 * G_PI);
147 148 149 150

      xpos = floor (xmid + r * cos (ang) - iw / 2.0 + 0.5);
      ypos = floor (ymid + r * sin (ang) - ih / 2.0 + 0.5);

151
      k = (i & 1) ? sin (f * 2.0 * G_PI) : cos (f * 2.0 * G_PI);
152 153 154 155 156 157 158 159 160 161 162 163 164 165
      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))
Matthias Clasen's avatar
Matthias Clasen committed
166 167 168 169 170 171 172 173 174 175
        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)))));
176 177
    }

178
  GDK_THREADS_ENTER ();
179
  gtk_widget_queue_draw (da);
180
  GDK_THREADS_LEAVE ();
181 182 183 184 185 186 187 188

  frame_num++;
  return TRUE;
}

static guint timeout_id;

static void
189
cleanup_callback (GObject   *object,
Matthias Clasen's avatar
Matthias Clasen committed
190
                  gpointer   data)
191 192 193 194 195 196
{
  g_source_remove (timeout_id);
  timeout_id = 0;
}

GtkWidget *
197
do_pixbufs (GtkWidget *do_widget)
198 199 200 201 202 203
{
  if (!window)
    {
      GError *error;

      window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
204
      gtk_window_set_screen (GTK_WINDOW (window),
Matthias Clasen's avatar
Matthias Clasen committed
205
                             gtk_widget_get_screen (do_widget));
206
      gtk_window_set_title (GTK_WINDOW (window), "Pixbufs");
207
      gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
208

209 210 211 212
      g_signal_connect (window, "destroy",
                        G_CALLBACK (gtk_widget_destroyed), &window);
      g_signal_connect (window, "destroy",
                        G_CALLBACK (cleanup_callback), NULL);
213

214

215 216
      error = NULL;
      if (!load_pixbufs (&error))
Matthias Clasen's avatar
Matthias Clasen committed
217 218
        {
          GtkWidget *dialog;
219

Matthias Clasen's avatar
Matthias Clasen committed
220 221 222 223 224 225
          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);
226

Matthias Clasen's avatar
Matthias Clasen committed
227
          g_error_free (error);
228

Matthias Clasen's avatar
Matthias Clasen committed
229 230
          g_signal_connect (dialog, "response",
                            G_CALLBACK (gtk_widget_destroy), NULL);
231

Matthias Clasen's avatar
Matthias Clasen committed
232 233
          gtk_widget_show (dialog);
        }
234
      else
Matthias Clasen's avatar
Matthias Clasen committed
235 236
        {
          gtk_widget_set_size_request (window, back_width, back_height);
237

Matthias Clasen's avatar
Matthias Clasen committed
238
          frame = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, back_width, back_height);
239

Matthias Clasen's avatar
Matthias Clasen committed
240
          da = gtk_drawing_area_new ();
241

242 243
          g_signal_connect (da, "draw",
                            G_CALLBACK (draw_cb), NULL);
244

Matthias Clasen's avatar
Matthias Clasen committed
245
          gtk_container_add (GTK_CONTAINER (window), da);
246

Matthias Clasen's avatar
Matthias Clasen committed
247 248
          timeout_id = g_timeout_add (FRAME_DELAY, timeout, NULL);
        }
249
    }
250

251
  if (!gtk_widget_get_visible (window))
252 253 254 255 256 257 258
    {
      gtk_widget_show_all (window);
    }
  else
    {
      gtk_widget_destroy (window);
      window = NULL;
259
      g_object_unref (frame);
260 261 262 263
    }

  return window;
}