413cb49010b16953ea374e7e337708c093064b16
[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   GdkGC *gc;
32   GdkGC *gc2;
33   PangoAttrList *list;
34 };
35
36 #define FILENAME "/home/cascardo/fotos/debconf.jpg"
37 #define WIDTH 800
38 #define HEIGHT 600
39
40 gboolean
41 queue (gpointer data)
42 {
43   struct ctx *ctx;
44   ctx = (struct ctx *) data;
45   gtk_widget_queue_draw (GTK_WIDGET (ctx->draw));
46   ctx->move = TRUE;
47   return FALSE;
48 }
49
50 gboolean
51 expose (GtkWidget *widget, GdkEventExpose *event, gpointer data)
52 {
53   GdkPixbuf *screen;
54   struct ctx *ctx;
55   Point point;
56   int w, h;
57   ctx = (struct ctx *) data;
58   gdk_drawable_get_size (GDK_DRAWABLE (event->window), &w, &h);
59   if (ctx->move)
60     ctx->i = (ctx->i >= ctx->points->len) ? 0 : ctx->i + 1;
61   point = g_array_index (ctx->points, Point, ctx->i);
62   screen = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8,
63                            event->area.width, event->area.height);
64   gdk_pixbuf_scale (ctx->picture, screen, 0, 0,
65                     event->area.width, event->area.height,
66                     -point.x + w/2 - event->area.x,
67                     -point.y + h/2 - event->area.y,
68                     point.rx, point.ry, GDK_INTERP_BILINEAR);
69   gdk_draw_pixbuf (widget->window, NULL, screen, 0, 0,
70                    event->area.x, event->area.y,
71                    event->area.width, event->area.height,
72                    GDK_RGB_DITHER_NONE, 0, 0);
73   gdk_pixbuf_unref (screen);
74   if (point.name)
75     {
76       PangoLayout *layout;
77       int pw, ph;
78       layout = gtk_widget_create_pango_layout (ctx->draw, point.name);
79       pango_layout_set_attributes (layout, ctx->list);
80       pango_layout_get_pixel_size (layout, &pw, &ph);
81       gdk_draw_rectangle (widget->window, ctx->gc2, TRUE,
82                           (WIDTH - pw - 8) / 2, HEIGHT - ph - 20, pw + 8, ph);
83       gdk_draw_layout (widget->window, ctx->gc, (WIDTH - pw) / 2,
84                        HEIGHT - ph - 20, layout);
85       g_object_unref (layout);
86       if (ctx->move)
87         g_timeout_add (3000, queue, ctx);
88     }
89   else
90     {
91       if (ctx->move)
92         g_timeout_add (10, queue, ctx);
93     }
94   ctx->move = FALSE;
95   return FALSE;
96 }
97
98 #define FPF 40
99
100 int
101 main (int argc, char **argv)
102 {
103   char *filename;
104   GdkColorspace colorspace;
105   gboolean has_alpha;
106   int bits_per_sample;
107   int width, height;
108   GtkWidget *window;
109   struct ctx ctx;
110   GdkColor Yellow;
111   GdkColor Black;
112   PangoAttribute *attr;
113   gtk_init (&argc, &argv);
114   if (argc < 2)
115     filename = FILENAME;
116   else
117     filename = argv[1];
118   ctx.points = ReadPoints ("pro-gnu");
119   ctx.points = drop_dup_frames (ctx.points, FPF);
120   rescale_points (ctx.points, get_scales (FPF));
121   ctx.picture = gdk_pixbuf_new_from_file (filename, NULL);
122   ctx.i = ctx.points->len;
123   colorspace = gdk_pixbuf_get_colorspace (ctx.picture);
124   has_alpha = gdk_pixbuf_get_has_alpha (ctx.picture);
125   bits_per_sample = gdk_pixbuf_get_bits_per_sample (ctx.picture);
126   width = WIDTH;
127   height = HEIGHT;
128   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
129   g_signal_connect (G_OBJECT (window), "destroy",
130                     G_CALLBACK (gtk_main_quit), NULL);
131   ctx.draw = gtk_drawing_area_new ();
132   gtk_widget_set_size_request (ctx.draw, width, height);
133   gtk_container_add (GTK_CONTAINER (window), ctx.draw);
134   gtk_widget_show_all (window);
135   g_signal_connect (G_OBJECT (ctx.draw), "expose_event",
136                     G_CALLBACK (expose), &ctx);
137   Yellow.red = 0xFFFF;
138   Yellow.green = 0xFFFF;
139   Yellow.blue = 0;
140   Black.red = 0;
141   Black.green = 0;
142   Black.blue = 0;
143   ctx.gc = gdk_gc_new (ctx.draw->window);
144   gdk_gc_set_rgb_fg_color (ctx.gc, &Yellow);
145   ctx.gc2 = gdk_gc_new (ctx.draw->window);
146   gdk_gc_set_rgb_fg_color (ctx.gc2, &Black);
147   ctx.list = pango_attr_list_new ();
148   attr = pango_attr_size_new (32 * PANGO_SCALE);
149   pango_attr_list_insert (ctx.list, attr);
150   attr = pango_attr_weight_new (PANGO_WEIGHT_SEMIBOLD);
151   pango_attr_list_insert (ctx.list, attr);
152   g_timeout_add (10, queue, &ctx);
153   gtk_main ();
154   gdk_pixbuf_unref (ctx.picture);
155   return 0;
156 }