]> Pileus Git - ~andy/gtk/blob - demos/testpixbuf-scale.c
Fixes #136082 and #135265, patch by Morten Welinder.
[~andy/gtk] / demos / testpixbuf-scale.c
1 #include <config.h>
2 #include <gtk/gtk.h>
3
4 #include <stdio.h>
5 #include <stdlib.h>
6
7 GdkInterpType interp_type = GDK_INTERP_BILINEAR;
8 int overall_alpha = 255;
9 GdkPixbuf *pixbuf;
10 GtkWidget *darea;
11   
12 void
13 set_interp_type (GtkWidget *widget, gpointer data)
14 {
15   guint types[] = { GDK_INTERP_NEAREST,
16                     GDK_INTERP_BILINEAR,
17                     GDK_INTERP_TILES,
18                     GDK_INTERP_HYPER };
19
20   interp_type = types[gtk_combo_box_get_active (GTK_COMBO_BOX (widget))];
21   gtk_widget_queue_draw (darea);
22 }
23
24 void
25 overall_changed_cb (GtkAdjustment *adjustment, gpointer data)
26 {
27   if (adjustment->value != overall_alpha)
28     {
29       overall_alpha = adjustment->value;
30       gtk_widget_queue_draw (darea);
31     }
32 }
33
34 gboolean
35 expose_cb (GtkWidget *widget, GdkEventExpose *event, gpointer data)
36 {
37   GdkPixbuf *dest;
38
39   gdk_window_set_back_pixmap (widget->window, NULL, FALSE);
40   
41   dest = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, event->area.width, event->area.height);
42
43   gdk_pixbuf_composite_color (pixbuf, dest,
44                               0, 0, event->area.width, event->area.height,
45                               -event->area.x, -event->area.y,
46                               (double) widget->allocation.width / gdk_pixbuf_get_width (pixbuf),
47                               (double) widget->allocation.height / gdk_pixbuf_get_height (pixbuf),
48                               interp_type, overall_alpha,
49                               event->area.x, event->area.y, 16, 0xaaaaaa, 0x555555);
50
51   gdk_draw_pixbuf (widget->window, widget->style->fg_gc[GTK_STATE_NORMAL], dest,
52                    0, 0, event->area.x, event->area.y,
53                    event->area.width, event->area.height,
54                    GDK_RGB_DITHER_NORMAL, event->area.x, event->area.y);
55   
56   g_object_unref (dest);
57   
58   return TRUE;
59 }
60
61 extern void pixbuf_init();
62
63 int
64 main(int argc, char **argv)
65 {
66         GtkWidget *window, *vbox;
67         GtkWidget *combo_box;
68         GtkWidget *alignment;
69         GtkWidget *hbox, *label, *hscale;
70         GtkAdjustment *adjustment;
71         GtkRequisition scratch_requisition;
72         const gchar *creator;
73         GError *error;
74         
75         pixbuf_init ();
76
77         gtk_init (&argc, &argv);
78
79         if (argc != 2) {
80                 fprintf (stderr, "Usage: testpixbuf-scale FILE\n");
81                 exit (1);
82         }
83
84         error = NULL;
85         pixbuf = gdk_pixbuf_new_from_file (argv[1], &error);
86         if (!pixbuf) {
87                 fprintf (stderr, "Cannot load image: %s\n",
88                          error->message);
89                 g_error_free (error);
90                 exit(1);
91         }
92
93         creator = gdk_pixbuf_get_option (pixbuf, "tEXt::Software");
94         if (creator)
95                 g_print ("%s was created by '%s'\n", argv[1], creator);
96
97         window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
98         g_signal_connect (window, "destroy",
99                           G_CALLBACK (gtk_main_quit), NULL);
100         
101         vbox = gtk_vbox_new (FALSE, 0);
102         gtk_container_add (GTK_CONTAINER (window), vbox);
103
104         combo_box = gtk_combo_box_new_text ();
105
106         gtk_combo_box_append_text (GTK_COMBO_BOX (combo_box), "NEAREST");
107         gtk_combo_box_append_text (GTK_COMBO_BOX (combo_box), "BILINEAR");
108         gtk_combo_box_append_text (GTK_COMBO_BOX (combo_box), "TILES");
109         gtk_combo_box_append_text (GTK_COMBO_BOX (combo_box), "HYPER");
110
111         gtk_combo_box_set_active (GTK_COMBO_BOX (combo_box), 1);
112
113         g_signal_connect (combo_box, "changed",
114                           G_CALLBACK (set_interp_type),
115                           NULL);
116         
117         alignment = gtk_alignment_new (0.0, 0.0, 0.0, 0.5);
118         gtk_box_pack_start (GTK_BOX (vbox), alignment, FALSE, FALSE, 0);
119
120         hbox = gtk_hbox_new (FALSE, 4);
121         gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
122
123         label = gtk_label_new ("Overall Alpha:");
124         gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
125
126         adjustment = GTK_ADJUSTMENT (gtk_adjustment_new (overall_alpha, 0, 255, 1, 10, 0));
127         g_signal_connect (adjustment, "value_changed",
128                           G_CALLBACK (overall_changed_cb), NULL);
129         
130         hscale = gtk_hscale_new (adjustment);
131         gtk_scale_set_digits (GTK_SCALE (hscale), 0);
132         gtk_box_pack_start (GTK_BOX (hbox), hscale, TRUE, TRUE, 0);
133
134         gtk_container_add (GTK_CONTAINER (alignment), combo_box);
135         gtk_widget_show_all (vbox);
136
137         /* Compute the size without the drawing area, so we know how big to make the default size */
138         gtk_widget_size_request (vbox, &scratch_requisition);
139
140         darea = gtk_drawing_area_new ();
141         gtk_box_pack_start (GTK_BOX (vbox), darea, TRUE, TRUE, 0);
142
143         g_signal_connect (darea, "expose_event",
144                           G_CALLBACK (expose_cb), NULL);
145
146         gtk_window_set_default_size (GTK_WINDOW (window),
147                                      gdk_pixbuf_get_width (pixbuf),
148                                      scratch_requisition.height + gdk_pixbuf_get_height (pixbuf));
149         
150         gtk_widget_show_all (window);
151
152         gtk_main ();
153
154         return 0;
155 }