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