Ignore generated files and input file
[cascardo/movie.git] / gdk.c
1 /*
2  *  Copyright (C) 2008  Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License along
15  *  with this program; if not, write to the Free Software Foundation, Inc.,
16  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18
19
20 #include <gtk/gtk.h>
21 #include <gdk-pixbuf/gdk-pixbuf.h>
22 #include "point.h"
23
24 struct ctx
25 {
26   GtkWidget *draw;
27   GdkPixbuf *picture;
28   GArray *points;
29   int i;
30   gboolean move;
31 };
32
33 #define FILENAME "/home/cascardo/fotos/debconf.jpg"
34 #define WIDTH 800
35 #define HEIGHT 600
36
37 gboolean
38 queue (gpointer data)
39 {
40   struct ctx *ctx;
41   ctx = (struct ctx *) data;
42   gtk_widget_queue_draw (GTK_WIDGET (ctx->draw));
43   ctx->move = TRUE;
44   return FALSE;
45 }
46
47 gboolean
48 expose (GtkWidget *widget, GdkEventExpose *event, gpointer data)
49 {
50   GdkPixbuf *screen;
51   struct ctx *ctx;
52   Point point;
53   int w, h;
54   ctx = (struct ctx *) data;
55   gdk_drawable_get_size (GDK_DRAWABLE (event->window), &w, &h);
56   if (ctx->move)
57     ctx->i = (ctx->i >= ctx->points->len) ? 0 : ctx->i + 1;
58   point = g_array_index (ctx->points, Point, ctx->i);
59   screen = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8,
60                            event->area.width, event->area.height);
61   gdk_pixbuf_scale (ctx->picture, screen, 0, 0,
62                     event->area.width, event->area.height,
63                     -point.x + w/2 - event->area.x,
64                     -point.y + h/2 - event->area.y,
65                     point.rx, point.ry, GDK_INTERP_BILINEAR);
66   gdk_draw_pixbuf (widget->window, NULL, screen, 0, 0,
67                    event->area.x, event->area.y,
68                    event->area.width, event->area.height,
69                    GDK_RGB_DITHER_NONE, 0, 0);
70   gdk_pixbuf_unref (screen);
71   if (point.name)
72     {
73       if (ctx->move)
74         g_timeout_add (3000, queue, ctx);
75     }
76   else
77     {
78       if (ctx->move)
79         g_timeout_add (10, queue, ctx);
80     }
81   ctx->move = FALSE;
82   return FALSE;
83 }
84
85 #define FPF 40
86
87 int
88 main (int argc, char **argv)
89 {
90   char *filename;
91   GdkColorspace colorspace;
92   gboolean has_alpha;
93   int bits_per_sample;
94   int width, height;
95   GtkWidget *window;
96   struct ctx ctx;
97   gtk_init (&argc, &argv);
98   if (argc < 2)
99     filename = FILENAME;
100   else
101     filename = argv[1];
102   ctx.points = ReadPoints ("pro-gnu");
103   ctx.points = drop_dup_frames (ctx.points, FPF);
104   rescale_points (ctx.points, get_scales (FPF));
105   ctx.picture = gdk_pixbuf_new_from_file (filename, NULL);
106   ctx.i = ctx.points->len;
107   colorspace = gdk_pixbuf_get_colorspace (ctx.picture);
108   has_alpha = gdk_pixbuf_get_has_alpha (ctx.picture);
109   bits_per_sample = gdk_pixbuf_get_bits_per_sample (ctx.picture);
110   width = WIDTH;
111   height = HEIGHT;
112   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
113   g_signal_connect (G_OBJECT (window), "destroy",
114                     G_CALLBACK (gtk_main_quit), NULL);
115   ctx.draw = gtk_drawing_area_new ();
116   gtk_widget_set_size_request (ctx.draw, width, height);
117   gtk_container_add (GTK_CONTAINER (window), ctx.draw);
118   gtk_widget_show_all (window);
119   g_signal_connect (G_OBJECT (ctx.draw), "expose_event",
120                     G_CALLBACK (expose), &ctx);
121   g_timeout_add (10, queue, &ctx);
122   gtk_main ();
123   gdk_pixbuf_unref (ctx.picture);
124   return 0;
125 }