Added GTK+ widgets and shows a piece of the scaled image
[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
23 #define FILENAME "/home/cascardo/fotos/debconf.jpg"
24 #define WIDTH 800
25 #define HEIGHT 600
26
27 gboolean
28 queue (gpointer data)
29 {
30   gtk_widget_queue_draw (GTK_WIDGET (data));
31   return TRUE;
32 }
33
34 gboolean
35 expose (GtkWidget *widget, GdkEventExpose *event, gpointer data)
36 {
37   GdkPixbuf *picture;
38   GdkPixbuf *screen;
39   picture = (GdkPixbuf *) data;
40   screen = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8,
41                            event->area.width, event->area.height);
42   gdk_pixbuf_scale (picture, screen, 0, 0,
43                     event->area.width, event->area.height,
44                     -event->area.x, -event->area.y,
45                     4.0, 4.0, GDK_INTERP_HYPER);
46   gdk_draw_pixbuf (widget->window, NULL, screen, 0, 0, 0, 0, -1, -1,
47                    GDK_RGB_DITHER_NONE, 0, 0);
48   gdk_pixbuf_unref (screen);
49   return FALSE;
50 }
51
52 int
53 main (int argc, char **argv)
54 {
55   char *filename;
56   GdkPixbuf *picture;
57   GdkColorspace colorspace;
58   gboolean has_alpha;
59   int bits_per_sample;
60   int width, height;
61   GtkWidget *window;
62   GtkWidget *draw;
63   gtk_init (&argc, &argv);
64   if (argc < 2)
65     filename = FILENAME;
66   else
67     filename = argv[1];
68   picture = gdk_pixbuf_new_from_file (filename, NULL);
69   colorspace = gdk_pixbuf_get_colorspace (picture);
70   has_alpha = gdk_pixbuf_get_has_alpha (picture);
71   bits_per_sample = gdk_pixbuf_get_bits_per_sample (picture);
72   width = WIDTH;
73   height = HEIGHT;
74   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
75   g_signal_connect (G_OBJECT (window), "destroy",
76                     G_CALLBACK (gtk_main_quit), NULL);
77   draw = gtk_drawing_area_new ();
78   gtk_widget_set_size_request (draw, width, height);
79   gtk_container_add (GTK_CONTAINER (window), draw);
80   gtk_widget_show_all (window);
81   g_signal_connect (G_OBJECT (draw), "expose_event",
82                     G_CALLBACK (expose), picture);
83   g_timeout_add (10, queue, draw);
84   gtk_main ();
85   gdk_pixbuf_unref (picture);
86   return 0;
87 }