summaryrefslogtreecommitdiff
path: root/packages/gtk2/examples/gtk_demo/pixbufs.inc
blob: dadac903349eb9671838580e7d53cdcfc747af32 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
(* 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.
 *
 *)


const
  FRAME_DELAY = 50;

  BACKGROUND_NAME = 'background.jpg';

  image_names : array [1..8] of pchar = (
    'apple-red.png',
    'gnome-applets.png',
    'gnome-calendar.png',
    'gnome-foot.png',
    'gnome-gmush.png',
    'gnome-gimp.png',
    'gnome-gsame.png',
    'gnu-keys.png');

  N_IMAGES = high(image_names);

(* demo window *)
var
  pixbufs_window      : PGtkWidget;

(* Current frame *)
  pixbufs_frame       : PGdkPixbuf;

(* Background image *)
  pixbufs_background  : PGdkPixbuf;

  pixbufs_back_width,
  pixbufs_back_height : gint;


(* Images *)
  images              : array [1..N_IMAGES] of PGdkPixbuf;

(* Widgets *)
  pixbufs_da          : PGtkWidget;

(* Loads the images for the demo and returns whether the operation succeeded *)
function load_pixbufs (error : PPGError): gboolean;
var
  i        :   gint;
  filename :   pgchar;

begin

  if pixbufs_background <> NULL then  begin
    load_pixbufs := TRUE;  (* already loaded earlier *)
    exit;
  end;

  (* demo_find_file() looks in the the current directory first,
   * 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 = NULL then begin
    load_pixbufs := FALSE;     (* note that "error" was filled in and returned *)
    exit;
  end;

  pixbufs_background := gdk_pixbuf_new_from_file (filename, error);
  g_free (filename);

  if pixbufs_background = NULL then begin
    load_pixbufs := FALSE; (* Note that "error" was filled with a GError *)
    exit;
  end;

  pixbufs_back_width  := gdk_pixbuf_get_width (pixbufs_background);
  pixbufs_back_height := gdk_pixbuf_get_height (pixbufs_background);

  for i := 1 to N_IMAGES do
  begin
      filename := demo_find_file (image_names[i], error);

      if filename = NULL then begin
        load_pixbufs := FALSE; (* Note that "error" was filled with a GError *)
        exit
      end;

      images[i] := gdk_pixbuf_new_from_file (filename, error);
      g_free (filename);

      if images[i] = NULL then begin
        load_pixbufs := FALSE;      (* Note that "error" was filled with a GError *)
        exit;
      end;

  end;

  load_pixbufs := TRUE;
end;

(* Expose callback for the drawing area *)
function expose_cb (widget : PGtkWidget;
                    event  : PGdkEventExpose;
                    data   : gpointer): gint; cdecl;
var
  pixels     : pguchar;
  rowstride  : gint;

begin
  rowstride := gdk_pixbuf_get_rowstride (pixbufs_frame);

  pixels := gdk_pixbuf_get_pixels (pixbufs_frame) + rowstride * event^.area.y + event^.area.x * 3;

  gdk_draw_rgb_image_dithalign (widget^.window,
                                widget^.style^.black_gc,
                                event^.area.x, event^.area.y,
                                event^.area.width, event^.area.height,
                                GDK_RGB_DITHER_NORMAL,
                                pixels, rowstride,
                                event^.area.x, event^.area.y);

  expose_cb := 1;
end;

const
  CYCLE_LEN = 60;

var
  pixbufs_frame_num : integer;


(* Timeout handler to regenerate the frame *)
function timeout (data : gpointer): gboolean;  cdecl;
var
  f      : double;
  i      : integer;
  xmid,
  ymid,
  radius : double;

  ang, r, k  : double;

  alpha,
  xpos, ypos,
  iw, ih     : integer;

  r1, r2,
  dest       : TGdkRectangle;


begin
  gdk_pixbuf_copy_area (pixbufs_background, 0, 0, pixbufs_back_width,
                        pixbufs_back_height, pixbufs_frame, 0, 0);

  f := double(pixbufs_frame_num mod CYCLE_LEN) / CYCLE_LEN;

  xmid := pixbufs_back_width / 2.0;
  ymid := pixbufs_back_height / 2.0;


  radius := min (ymid, xmid) / 2.0;

  for i := 1 to N_IMAGES do
  begin
    ang := 2.0 * G_PI * double (i / N_IMAGES) - f * 2.0 * G_PI;

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

    r := radius + (radius / 3.0) * sin (f * 2.0 * G_PI);

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

    if (i and 1) <> 0 then k:= sin (f * 2.0 * G_PI)
    else k:= cos (f * 2.0 * G_PI);

    k := 2.0 * k * k;
    k := MAX (0.25, k);

    r1.x := xpos;
    r1.y := ypos;
    r1.width := round(iw * k);
    r1.height := round(ih * k);

    r2.x := 0;
    r2.y := 0;
    r2.width  := pixbufs_back_width;
    r2.height := pixbufs_back_height;

    if gdk_rectangle_intersect (@r1, @r2, @dest) then begin
      if (i and 1) <> 0 then
        alpha := round (MAX (127, abs (255 * sin (f * 2.0 * G_PI))))
      else
        alpha := round (MAX (127, abs (255 * cos (f * 2.0 * G_PI))));

      gdk_pixbuf_composite (images[i],
                              pixbufs_frame,
                              dest.x, dest.y,
                              dest.width, dest.height,
                              xpos, ypos,
                              k, k,
                              GDK_INTERP_NEAREST,
                  alpha);
    end;
  end;

  gtk_widget_queue_draw (pixbufs_da);

  inc(pixbufs_frame_num);
  exit (TRUE);
end;

var
  pixbufs_timeout_id : guint;

procedure pixbufs_cleanup_callback (_object : PGtkObject;
                            data    : gpointer);    cdecl;
begin
  g_source_remove (pixbufs_timeout_id);
  pixbufs_timeout_id := 0;
end;



function do_pixbufs        : PGtkWidget;
var
  error  : PGError;
  dialog : PGtkWidget;

begin
  if pixbufs_window = NULL then
  begin

    pixbufs_window := gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title (GTK_WINDOW (pixbufs_window), 'Pixbufs');
    gtk_window_set_resizable (GTK_WINDOW (pixbufs_window), FALSE);

    g_signal_connect (pixbufs_window, 'destroy', G_CALLBACK (@gtk_widget_destroyed), @pixbufs_window);
    g_signal_connect (pixbufs_window, 'destroy', G_CALLBACK (@pixbufs_cleanup_callback), NULL);


    error := NULL;
    if not load_pixbufs (@error) then
    begin
          dialog := gtk_message_dialog_new (GTK_WINDOW (pixbufs_window),
                                           GTK_DIALOG_DESTROY_WITH_PARENT,
                                           GTK_MESSAGE_ERROR,
                                           GTK_BUTTONS_CLOSE,
                                           'Failed to load an image: %s',
                                           [error^.message]);

          g_error_free (error);

          g_signal_connect (dialog, 'response',
                            G_CALLBACK (@gtk_widget_destroy), NULL);

          gtk_widget_show (dialog);
    end else
    begin
          gtk_widget_set_size_request (pixbufs_window, pixbufs_back_width, pixbufs_back_height);

          pixbufs_frame := gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, pixbufs_back_width, pixbufs_back_height);

          pixbufs_da := gtk_drawing_area_new ();

          g_signal_connect (pixbufs_da, 'expose_event',
                            G_CALLBACK (@expose_cb), NULL);

          gtk_container_add (GTK_CONTAINER (pixbufs_window), pixbufs_da);

          pixbufs_timeout_id := gtk_timeout_add (FRAME_DELAY, @timeout, NULL);
    end;
  end;

  if not GTK_WIDGET_VISIBLE (pixbufs_window) then
    gtk_widget_show_all (pixbufs_window)
  else begin
    gtk_widget_destroy (pixbufs_window);
    pixbufs_window := NULL;
  end;

  do_pixbufs := pixbufs_window;
end;