Shows the name associated with the face
[cascardo/movie.git] / gdk.c
diff --git a/gdk.c b/gdk.c
index 73e11d9..98981a8 100644 (file)
--- a/gdk.c
+++ b/gdk.c
  */
 
 
+#include <gtk/gtk.h>
 #include <gdk-pixbuf/gdk-pixbuf.h>
+#include "point.h"
+
+struct ctx
+{
+  GtkWidget *draw;
+  GdkPixbuf *picture;
+  GArray *points;
+  int i;
+  gboolean move;
+  GdkGC *gc;
+  PangoAttrList *list;
+};
 
 #define FILENAME "/home/cascardo/fotos/debconf.jpg"
 #define WIDTH 800
 #define HEIGHT 600
 
+gboolean
+queue (gpointer data)
+{
+  struct ctx *ctx;
+  ctx = (struct ctx *) data;
+  gtk_widget_queue_draw (GTK_WIDGET (ctx->draw));
+  ctx->move = TRUE;
+  return FALSE;
+}
+
+gboolean
+expose (GtkWidget *widget, GdkEventExpose *event, gpointer data)
+{
+  GdkPixbuf *screen;
+  struct ctx *ctx;
+  Point point;
+  int w, h;
+  ctx = (struct ctx *) data;
+  gdk_drawable_get_size (GDK_DRAWABLE (event->window), &w, &h);
+  if (ctx->move)
+    ctx->i = (ctx->i >= ctx->points->len) ? 0 : ctx->i + 1;
+  point = g_array_index (ctx->points, Point, ctx->i);
+  screen = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8,
+                           event->area.width, event->area.height);
+  gdk_pixbuf_scale (ctx->picture, screen, 0, 0,
+                    event->area.width, event->area.height,
+                   -point.x + w/2 - event->area.x,
+                   -point.y + h/2 - event->area.y,
+                   point.rx, point.ry, GDK_INTERP_BILINEAR);
+  gdk_draw_pixbuf (widget->window, NULL, screen, 0, 0,
+                   event->area.x, event->area.y,
+                  event->area.width, event->area.height,
+                   GDK_RGB_DITHER_NONE, 0, 0);
+  gdk_pixbuf_unref (screen);
+  if (point.name)
+    {
+      PangoLayout *layout;
+      int pw, ph;
+      layout = gtk_widget_create_pango_layout (ctx->draw, point.name);
+      pango_layout_set_attributes (layout, ctx->list);
+      pango_layout_get_pixel_size (layout, &pw, &ph);
+      gdk_draw_layout (widget->window, ctx->gc, (WIDTH - pw) / 2,
+                       HEIGHT - ph - 20, layout);
+      g_object_unref (layout);
+      if (ctx->move)
+        g_timeout_add (3000, queue, ctx);
+    }
+  else
+    {
+      if (ctx->move)
+        g_timeout_add (10, queue, ctx);
+    }
+  ctx->move = FALSE;
+  return FALSE;
+}
+
+#define FPF 40
+
 int
 main (int argc, char **argv)
 {
   char *filename;
-  GdkPixbuf *picture;
-  GdkPixbuf *screen;
   GdkColorspace colorspace;
   gboolean has_alpha;
   int bits_per_sample;
   int width, height;
-  g_type_init ();
+  GtkWidget *window;
+  struct ctx ctx;
+  GdkColor Yellow;
+  GdkColor Black;
+  PangoAttribute *attr;
+  gtk_init (&argc, &argv);
   if (argc < 2)
     filename = FILENAME;
   else
     filename = argv[1];
-  picture = gdk_pixbuf_new_from_file (filename, NULL);
-  colorspace = gdk_pixbuf_get_colorspace (picture);
-  has_alpha = gdk_pixbuf_get_has_alpha (picture);
-  bits_per_sample = gdk_pixbuf_get_bits_per_sample (picture);
+  ctx.points = ReadPoints ("pro-gnu");
+  ctx.points = drop_dup_frames (ctx.points, FPF);
+  rescale_points (ctx.points, get_scales (FPF));
+  ctx.picture = gdk_pixbuf_new_from_file (filename, NULL);
+  ctx.i = ctx.points->len;
+  colorspace = gdk_pixbuf_get_colorspace (ctx.picture);
+  has_alpha = gdk_pixbuf_get_has_alpha (ctx.picture);
+  bits_per_sample = gdk_pixbuf_get_bits_per_sample (ctx.picture);
   width = WIDTH;
   height = HEIGHT;
-  screen = gdk_pixbuf_new (colorspace, has_alpha, bits_per_sample,
-                           width, height);
-  gdk_pixbuf_scale (picture, screen, 0, 0, width, height, 0, 0, 2, 2,
-                    GDK_INTERP_BILINEAR);
-  gdk_pixbuf_unref (picture);
-  gdk_pixbuf_unref (screen);
+  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
+  g_signal_connect (G_OBJECT (window), "destroy",
+                    G_CALLBACK (gtk_main_quit), NULL);
+  ctx.draw = gtk_drawing_area_new ();
+  gtk_widget_set_size_request (ctx.draw, width, height);
+  gtk_container_add (GTK_CONTAINER (window), ctx.draw);
+  gtk_widget_show_all (window);
+  g_signal_connect (G_OBJECT (ctx.draw), "expose_event",
+                    G_CALLBACK (expose), &ctx);
+  Yellow.red = 0xFFFF;
+  Yellow.green = 0xFFFF;
+  Yellow.blue = 0;
+  Black.red = 0;
+  Black.green = 0;
+  Black.blue = 0;
+  ctx.gc = gdk_gc_new (ctx.draw->window);
+  gdk_gc_set_rgb_fg_color (ctx.gc, &Yellow);
+  gdk_gc_set_rgb_bg_color (ctx.gc, &Black);
+  ctx.list = pango_attr_list_new ();
+  attr = pango_attr_size_new (32 * PANGO_SCALE);
+  pango_attr_list_insert (ctx.list, attr);
+  attr = pango_attr_weight_new (PANGO_WEIGHT_SEMIBOLD);
+  pango_attr_list_insert (ctx.list, attr);
+  g_timeout_add (10, queue, &ctx);
+  gtk_main ();
+  gdk_pixbuf_unref (ctx.picture);
   return 0;
 }