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, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include <gtk/gtk.h>
19 #include <gio/gio.h>
20
21 static void
drag_begin(GtkWidget * widget,GdkDragContext * context,gpointer data)22 drag_begin (GtkWidget *widget,
23 GdkDragContext *context,
24 gpointer data)
25 {
26 GtkWidget *image = GTK_WIDGET (data);
27
28 GdkPixbuf *pixbuf = gtk_image_get_pixbuf (GTK_IMAGE (image));
29
30 gtk_drag_set_icon_pixbuf (context, pixbuf, -2, -2);
31 }
32
33 void
drag_data_get(GtkWidget * widget,GdkDragContext * context,GtkSelectionData * selection_data,guint info,guint time,gpointer data)34 drag_data_get (GtkWidget *widget,
35 GdkDragContext *context,
36 GtkSelectionData *selection_data,
37 guint info,
38 guint time,
39 gpointer data)
40 {
41 GtkWidget *image = GTK_WIDGET (data);
42
43 GdkPixbuf *pixbuf = gtk_image_get_pixbuf (GTK_IMAGE (image));
44
45 gtk_selection_data_set_pixbuf (selection_data, pixbuf);
46 }
47
48 static void
drag_data_received(GtkWidget * widget,GdkDragContext * context,gint x,gint y,GtkSelectionData * selection_data,guint info,guint32 time,gpointer data)49 drag_data_received (GtkWidget *widget,
50 GdkDragContext *context,
51 gint x,
52 gint y,
53 GtkSelectionData *selection_data,
54 guint info,
55 guint32 time,
56 gpointer data)
57 {
58 GtkWidget *image = GTK_WIDGET (data);
59
60 GdkPixbuf *pixbuf;
61
62 if (gtk_selection_data_get_length (selection_data) < 0)
63 return;
64
65 pixbuf = gtk_selection_data_get_pixbuf (selection_data);
66
67 gtk_image_set_from_pixbuf (GTK_IMAGE (image), pixbuf);
68 }
69
70 static gboolean
idle_func(gpointer data)71 idle_func (gpointer data)
72 {
73 g_print ("keep me busy\n");
74
75 return G_SOURCE_CONTINUE;
76 }
77
78 static gboolean
anim_image_draw(GtkWidget * widget,cairo_t * cr,gpointer data)79 anim_image_draw (GtkWidget *widget,
80 cairo_t *cr,
81 gpointer data)
82 {
83 g_print ("start busyness\n");
84
85 g_signal_handlers_disconnect_by_func (widget, anim_image_draw, data);
86
87 /* produce high load */
88 g_idle_add_full (G_PRIORITY_DEFAULT,
89 idle_func, NULL, NULL);
90
91 return FALSE;
92 }
93
94 int
main(int argc,char ** argv)95 main (int argc, char **argv)
96 {
97 GtkWidget *window, *grid;
98 GtkWidget *label, *image, *box;
99 GtkIconTheme *theme;
100 GdkPixbuf *pixbuf;
101 GtkIconSet *iconset;
102 GtkIconSource *iconsource;
103 gchar *icon_name = "gnome-terminal";
104 gchar *anim_filename = NULL;
105 GIcon *icon;
106 GFile *file;
107 GdkGeometry geo;
108
109 gtk_init (&argc, &argv);
110
111 if (argc > 1)
112 icon_name = argv[1];
113
114 if (argc > 2)
115 anim_filename = argv[2];
116
117 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
118
119 geo.min_width = 400;
120 geo.min_height = 300;
121 geo.max_width = 800;
122 geo.max_height = 600;
123
124 gtk_window_set_geometry_hints (GTK_WINDOW (window), NULL, &geo, GDK_HINT_MIN_SIZE | GDK_HINT_MAX_SIZE);
125
126 grid = gtk_grid_new ();
127 gtk_container_add (GTK_CONTAINER (window), grid);
128
129 label = gtk_label_new ("symbolic size");
130 gtk_grid_attach (GTK_GRID (grid), label, 1, 0, 1, 1);
131 label = gtk_label_new ("fixed size");
132 gtk_grid_attach (GTK_GRID (grid), label, 2, 0, 1, 1);
133
134 label = gtk_label_new ("GTK_IMAGE_PIXBUF");
135 gtk_grid_attach (GTK_GRID (grid), label, 0, 1, 1, 1);
136
137 theme = gtk_icon_theme_get_default ();
138 pixbuf = gtk_icon_theme_load_icon (theme, icon_name, 48, 0, NULL);
139 image = gtk_image_new_from_pixbuf (pixbuf);
140 box = gtk_event_box_new ();
141 gtk_container_add (GTK_CONTAINER (box), image);
142 gtk_grid_attach (GTK_GRID (grid), box, 2, 1, 1, 1);
143
144 gtk_drag_source_set (box, GDK_BUTTON1_MASK,
145 NULL, 0,
146 GDK_ACTION_COPY);
147 gtk_drag_source_add_image_targets (box);
148 g_signal_connect (box, "drag_begin", G_CALLBACK (drag_begin), image);
149 g_signal_connect (box, "drag_data_get", G_CALLBACK (drag_data_get), image);
150
151 gtk_drag_dest_set (box,
152 GTK_DEST_DEFAULT_MOTION |
153 GTK_DEST_DEFAULT_HIGHLIGHT |
154 GTK_DEST_DEFAULT_DROP,
155 NULL, 0, GDK_ACTION_COPY);
156 gtk_drag_dest_add_image_targets (box);
157 g_signal_connect (box, "drag_data_received",
158 G_CALLBACK (drag_data_received), image);
159
160 label = gtk_label_new ("GTK_IMAGE_STOCK");
161 gtk_grid_attach (GTK_GRID (grid), label, 0, 2, 1, 1);
162
163 G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
164 image = gtk_image_new_from_stock (GTK_STOCK_REDO, GTK_ICON_SIZE_DIALOG);
165 gtk_grid_attach (GTK_GRID (grid), image, 1, 2, 1, 1);
166
167 label = gtk_label_new ("GTK_IMAGE_ICON_SET");
168 gtk_grid_attach (GTK_GRID (grid), label, 0, 3, 1, 1);
169
170 iconsource = gtk_icon_source_new ();
171 gtk_icon_source_set_icon_name (iconsource, icon_name);
172 iconset = gtk_icon_set_new ();
173 gtk_icon_set_add_source (iconset, iconsource);
174 image = gtk_image_new_from_icon_set (iconset, GTK_ICON_SIZE_DIALOG);
175 gtk_grid_attach (GTK_GRID (grid), image, 1, 3, 1, 1);
176 G_GNUC_END_IGNORE_DEPRECATIONS;
177
178 label = gtk_label_new ("GTK_IMAGE_ICON_NAME");
179 gtk_grid_attach (GTK_GRID (grid), label, 0, 4, 1, 1);
180 image = gtk_image_new_from_icon_name (icon_name, GTK_ICON_SIZE_DIALOG);
181 gtk_grid_attach (GTK_GRID (grid), image, 1, 4, 1, 1);
182 image = gtk_image_new_from_icon_name (icon_name, GTK_ICON_SIZE_DIALOG);
183 gtk_image_set_pixel_size (GTK_IMAGE (image), 30);
184 gtk_grid_attach (GTK_GRID (grid), image, 2, 4, 1, 1);
185
186 label = gtk_label_new ("GTK_IMAGE_GICON");
187 gtk_grid_attach (GTK_GRID (grid), label, 0, 5, 1, 1);
188 icon = g_themed_icon_new_with_default_fallbacks ("folder-remote");
189 image = gtk_image_new_from_gicon (icon, GTK_ICON_SIZE_DIALOG);
190 g_object_unref (icon);
191 gtk_grid_attach (GTK_GRID (grid), image, 1, 5, 1, 1);
192 file = g_file_new_for_path ("apple-red.png");
193 icon = g_file_icon_new (file);
194 image = gtk_image_new_from_gicon (icon, GTK_ICON_SIZE_DIALOG);
195 g_object_unref (icon);
196 gtk_image_set_pixel_size (GTK_IMAGE (image), 30);
197 gtk_grid_attach (GTK_GRID (grid), image, 2, 5, 1, 1);
198
199
200 if (anim_filename)
201 {
202 label = gtk_label_new ("GTK_IMAGE_ANIMATION (from file)");
203 gtk_grid_attach (GTK_GRID (grid), label, 0, 6, 1, 1);
204 image = gtk_image_new_from_file (anim_filename);
205 gtk_image_set_pixel_size (GTK_IMAGE (image), 30);
206 gtk_grid_attach (GTK_GRID (grid), image, 2, 6, 1, 1);
207
208 /* produce high load */
209 g_signal_connect_after (image, "draw",
210 G_CALLBACK (anim_image_draw),
211 NULL);
212 }
213
214 gtk_widget_show_all (window);
215
216 gtk_main ();
217
218 return 0;
219 }
220