]> Pileus Git - ~andy/gtk/blob - tests/testimage.c
Merge branch 'master' into treeview-refactor
[~andy/gtk] / tests / testimage.c
1 /* testimage.c
2  * Copyright (C) 2004  Red Hat, Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library 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 GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #include <gtk/gtk.h>
21 #include <gio/gio.h>
22
23 static void
24 drag_begin (GtkWidget      *widget,
25             GdkDragContext *context,
26             gpointer        data)
27 {
28   GtkWidget *image = GTK_WIDGET (data);
29
30   GdkPixbuf *pixbuf = gtk_image_get_pixbuf (GTK_IMAGE (image));
31
32   gtk_drag_set_icon_pixbuf (context, pixbuf, -2, -2);
33 }
34
35 void  
36 drag_data_get  (GtkWidget        *widget,
37                 GdkDragContext   *context,
38                 GtkSelectionData *selection_data,
39                 guint             info,
40                 guint             time,
41                 gpointer          data)
42 {
43   GtkWidget *image = GTK_WIDGET (data);
44
45   GdkPixbuf *pixbuf = gtk_image_get_pixbuf (GTK_IMAGE (image));
46
47   gtk_selection_data_set_pixbuf (selection_data, pixbuf);
48 }
49
50 static void
51 drag_data_received (GtkWidget        *widget,
52                     GdkDragContext   *context,
53                     gint              x,
54                     gint              y,
55                     GtkSelectionData *selection_data,
56                     guint             info,
57                     guint32           time,
58                     gpointer          data)
59 {
60   GtkWidget *image = GTK_WIDGET (data);
61
62   GdkPixbuf *pixbuf;
63
64   if (gtk_selection_data_get_length (selection_data) < 0)
65     return;
66
67   pixbuf = gtk_selection_data_get_pixbuf (selection_data);
68
69   gtk_image_set_from_pixbuf (GTK_IMAGE (image), pixbuf);
70 }
71
72 static gboolean
73 idle_func (gpointer data)
74 {
75   g_print ("keep me busy\n");
76
77   return TRUE;
78 }
79
80 static gboolean
81 anim_image_draw (GtkWidget *widget,
82                  cairo_t   *cr,
83                  gpointer   data)
84 {
85   g_print ("start busyness\n");
86
87   g_signal_handlers_disconnect_by_func (widget, anim_image_draw, data);
88
89   /* produce high load */
90   g_idle_add_full (G_PRIORITY_DEFAULT,
91                    idle_func, NULL, NULL);
92
93   return FALSE;
94 }
95
96 int
97 main (int argc, char **argv)
98 {
99   GtkWidget *window, *table;
100   GtkWidget *label, *image, *box;
101   GtkIconTheme *theme;
102   GdkPixbuf *pixbuf;
103   GtkIconSet *iconset;
104   GtkIconSource *iconsource;
105   gchar *icon_name = "gnome-terminal";
106   gchar *anim_filename = NULL;
107   GIcon *icon;
108   GFile *file;
109
110   gtk_init (&argc, &argv);
111
112   if (argc > 1)
113     icon_name = argv[1];
114
115   if (argc > 2)
116     anim_filename = argv[2];
117
118   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
119   table = gtk_table_new (6, 3, FALSE);
120   gtk_container_add (GTK_CONTAINER (window), table);
121
122   label = gtk_label_new ("symbolic size");
123   gtk_table_attach (GTK_TABLE (table), label, 1, 2, 0, 1,
124                     0, 0, 5, 5);
125   label = gtk_label_new ("fixed size");
126   gtk_table_attach (GTK_TABLE (table), label, 2, 3, 0, 1,
127                     0, 0, 5, 5);
128
129   label = gtk_label_new ("GTK_IMAGE_PIXBUF");
130   gtk_table_attach_defaults (GTK_TABLE (table), label, 0, 1, 1, 2);
131
132   theme = gtk_icon_theme_get_default ();
133   pixbuf = gtk_icon_theme_load_icon (theme, icon_name, 48, 0, NULL);
134   image = gtk_image_new_from_pixbuf (pixbuf);
135   box = gtk_event_box_new ();
136   gtk_container_add (GTK_CONTAINER (box), image);
137   gtk_table_attach_defaults (GTK_TABLE (table), box, 2, 3, 1, 2);
138
139   gtk_drag_source_set (box, GDK_BUTTON1_MASK, 
140                        NULL, 0,
141                        GDK_ACTION_COPY);
142   gtk_drag_source_add_image_targets (box);
143   g_signal_connect (box, "drag_begin", G_CALLBACK (drag_begin), image);
144   g_signal_connect (box, "drag_data_get", G_CALLBACK (drag_data_get), image);
145
146   gtk_drag_dest_set (box,
147                      GTK_DEST_DEFAULT_MOTION |
148                      GTK_DEST_DEFAULT_HIGHLIGHT |
149                      GTK_DEST_DEFAULT_DROP,
150                      NULL, 0, GDK_ACTION_COPY);
151   gtk_drag_dest_add_image_targets (box);
152   g_signal_connect (box, "drag_data_received", 
153                     G_CALLBACK (drag_data_received), image);
154
155   label = gtk_label_new ("GTK_IMAGE_STOCK");
156   gtk_table_attach_defaults (GTK_TABLE (table), label, 0, 1, 2, 3);
157
158   image = gtk_image_new_from_stock (GTK_STOCK_REDO, GTK_ICON_SIZE_DIALOG);
159   gtk_table_attach_defaults (GTK_TABLE (table), image, 1, 2, 2, 3);
160
161   label = gtk_label_new ("GTK_IMAGE_ICON_SET");
162   gtk_table_attach_defaults (GTK_TABLE (table), label, 0, 1, 3, 4);
163
164   iconsource = gtk_icon_source_new ();
165   gtk_icon_source_set_icon_name (iconsource, icon_name);
166   iconset = gtk_icon_set_new ();
167   gtk_icon_set_add_source (iconset, iconsource);
168   image = gtk_image_new_from_icon_set (iconset, GTK_ICON_SIZE_DIALOG);
169   gtk_table_attach_defaults (GTK_TABLE (table), image, 1, 2, 3, 4);
170
171   label = gtk_label_new ("GTK_IMAGE_ICON_NAME");
172   gtk_table_attach_defaults (GTK_TABLE (table), label, 0, 1, 4, 5);
173   image = gtk_image_new_from_icon_name (icon_name, GTK_ICON_SIZE_DIALOG);
174   gtk_table_attach_defaults (GTK_TABLE (table), image, 1, 2, 4, 5);
175   image = gtk_image_new_from_icon_name (icon_name, GTK_ICON_SIZE_DIALOG);
176   gtk_image_set_pixel_size (GTK_IMAGE (image), 30);
177   gtk_table_attach_defaults (GTK_TABLE (table), image, 2, 3, 4, 5);
178
179   label = gtk_label_new ("GTK_IMAGE_GICON");
180   gtk_table_attach_defaults (GTK_TABLE (table), label, 0, 1, 5, 6);
181   icon = g_themed_icon_new_with_default_fallbacks ("folder-remote");
182   image = gtk_image_new_from_gicon (icon, GTK_ICON_SIZE_DIALOG);
183   g_object_unref (icon);
184   gtk_table_attach_defaults (GTK_TABLE (table), image, 1, 2, 5, 6);
185   file = g_file_new_for_path ("apple-red.png");
186   icon = g_file_icon_new (file);
187   image = gtk_image_new_from_gicon (icon, GTK_ICON_SIZE_DIALOG);
188   g_object_unref (icon);
189   gtk_image_set_pixel_size (GTK_IMAGE (image), 30);
190   gtk_table_attach_defaults (GTK_TABLE (table), image, 2, 3, 5, 6);
191
192   
193   if (anim_filename)
194     {
195       label = gtk_label_new ("GTK_IMAGE_ANIMATION (from file)");
196       gtk_table_attach_defaults (GTK_TABLE (table), label, 0, 1, 5, 6);
197       image = gtk_image_new_from_file (anim_filename);
198       gtk_image_set_pixel_size (GTK_IMAGE (image), 30);
199       gtk_table_attach_defaults (GTK_TABLE (table), image, 2, 3, 5, 6);
200
201       /* produce high load */
202       g_signal_connect_after (image, "draw",
203                               G_CALLBACK (anim_image_draw),
204                               NULL);
205     }
206
207   gtk_widget_show_all (window);
208
209   gtk_main ();
210
211   return 0;
212 }