]> Pileus Git - ~andy/gtk/blob - demos/testpixbuf-scale.c
notebook: Draw tabs and arrows with Cairo
[~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   GtkAllocation allocation;
38   GdkPixbuf *dest;
39   cairo_t *cr;
40
41   dest = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, event->area.width, event->area.height);
42
43   gtk_widget_get_allocation (widget, &allocation);
44   gdk_pixbuf_composite_color (pixbuf, dest,
45                               0, 0, event->area.width, event->area.height,
46                               -event->area.x, -event->area.y,
47                               (double) allocation.width / gdk_pixbuf_get_width (pixbuf),
48                               (double) allocation.height / gdk_pixbuf_get_height (pixbuf),
49                               interp_type, overall_alpha,
50                               event->area.x, event->area.y, 16, 0xaaaaaa, 0x555555);
51
52   cr = gdk_cairo_create (event->window);
53
54   gdk_cairo_set_source_pixbuf (cr, dest, 0, 0);
55   gdk_cairo_rectangle (cr, &event->area);
56   cairo_fill (cr);
57
58   cairo_destroy (cr);
59   g_object_unref (dest);
60   
61   return TRUE;
62 }
63
64 extern void pixbuf_init();
65
66 int
67 main(int argc, char **argv)
68 {
69         GtkWidget *window, *vbox;
70         GtkWidget *combo_box;
71         GtkWidget *alignment;
72         GtkWidget *hbox, *label, *hscale;
73         GtkAdjustment *adjustment;
74         GtkRequisition scratch_requisition;
75         const gchar *creator;
76         GError *error;
77         
78         pixbuf_init ();
79
80         gtk_init (&argc, &argv);
81
82         if (argc != 2) {
83                 fprintf (stderr, "Usage: testpixbuf-scale FILE\n");
84                 exit (1);
85         }
86
87         error = NULL;
88         pixbuf = gdk_pixbuf_new_from_file (argv[1], &error);
89         if (!pixbuf) {
90                 fprintf (stderr, "Cannot load image: %s\n",
91                          error->message);
92                 g_error_free (error);
93                 exit(1);
94         }
95
96         creator = gdk_pixbuf_get_option (pixbuf, "tEXt::Software");
97         if (creator)
98                 g_print ("%s was created by '%s'\n", argv[1], creator);
99
100         window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
101         g_signal_connect (window, "destroy",
102                           G_CALLBACK (gtk_main_quit), NULL);
103         
104         vbox = gtk_vbox_new (FALSE, 0);
105         gtk_container_add (GTK_CONTAINER (window), vbox);
106
107         combo_box = gtk_combo_box_new_text ();
108
109         gtk_combo_box_append_text (GTK_COMBO_BOX (combo_box), "NEAREST");
110         gtk_combo_box_append_text (GTK_COMBO_BOX (combo_box), "BILINEAR");
111         gtk_combo_box_append_text (GTK_COMBO_BOX (combo_box), "TILES");
112         gtk_combo_box_append_text (GTK_COMBO_BOX (combo_box), "HYPER");
113
114         gtk_combo_box_set_active (GTK_COMBO_BOX (combo_box), 1);
115
116         g_signal_connect (combo_box, "changed",
117                           G_CALLBACK (set_interp_type),
118                           NULL);
119         
120         alignment = gtk_alignment_new (0.0, 0.0, 0.0, 0.5);
121         gtk_box_pack_start (GTK_BOX (vbox), alignment, FALSE, FALSE, 0);
122
123         hbox = gtk_hbox_new (FALSE, 4);
124         gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
125
126         label = gtk_label_new ("Overall Alpha:");
127         gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
128
129         adjustment = GTK_ADJUSTMENT (gtk_adjustment_new (overall_alpha, 0, 255, 1, 10, 0));
130         g_signal_connect (adjustment, "value_changed",
131                           G_CALLBACK (overall_changed_cb), NULL);
132         
133         hscale = gtk_hscale_new (adjustment);
134         gtk_scale_set_digits (GTK_SCALE (hscale), 0);
135         gtk_box_pack_start (GTK_BOX (hbox), hscale, TRUE, TRUE, 0);
136
137         gtk_container_add (GTK_CONTAINER (alignment), combo_box);
138         gtk_widget_show_all (vbox);
139
140         /* Compute the size without the drawing area, so we know how big to make the default size */
141         gtk_size_request_get_size (GTK_SIZE_REQUEST (vbox),
142                                    &scratch_requisition, NULL);
143
144         darea = gtk_drawing_area_new ();
145         gtk_box_pack_start (GTK_BOX (vbox), darea, TRUE, TRUE, 0);
146
147         g_signal_connect (darea, "expose_event",
148                           G_CALLBACK (expose_cb), NULL);
149
150         gtk_window_set_default_size (GTK_WINDOW (window),
151                                      gdk_pixbuf_get_width (pixbuf),
152                                      scratch_requisition.height + gdk_pixbuf_get_height (pixbuf));
153         
154         gtk_widget_show_all (window);
155
156         gtk_main ();
157
158         return 0;
159 }