testimage.c 6.52 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* testimage.c
 * Copyright (C) 2004  Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
Javier Jardón's avatar
Javier Jardón committed
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 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61

static void
drag_begin (GtkWidget      *widget,
	    GdkDragContext *context,
	    gpointer        data)
{
  GtkWidget *image = GTK_WIDGET (data);

  GdkPixbuf *pixbuf = gtk_image_get_pixbuf (GTK_IMAGE (image));

  gtk_drag_set_icon_pixbuf (context, pixbuf, -2, -2);
}

void  
drag_data_get  (GtkWidget        *widget,
		GdkDragContext   *context,
		GtkSelectionData *selection_data,
		guint             info,
		guint             time,
		gpointer          data)
{
  GtkWidget *image = GTK_WIDGET (data);

  GdkPixbuf *pixbuf = gtk_image_get_pixbuf (GTK_IMAGE (image));

  gtk_selection_data_set_pixbuf (selection_data, pixbuf);
}

static void
drag_data_received (GtkWidget        *widget,
		    GdkDragContext   *context,
		    gint              x,
		    gint              y,
		    GtkSelectionData *selection_data,
		    guint             info,
		    guint32           time,
		    gpointer          data)
{
  GtkWidget *image = GTK_WIDGET (data);

  GdkPixbuf *pixbuf;

62
  if (gtk_selection_data_get_length (selection_data) < 0)
63 64 65 66 67 68 69
    return;

  pixbuf = gtk_selection_data_get_pixbuf (selection_data);

  gtk_image_set_from_pixbuf (GTK_IMAGE (image), pixbuf);
}

70 71 72 73 74
static gboolean
idle_func (gpointer data)
{
  g_print ("keep me busy\n");

75
  return G_SOURCE_CONTINUE;
76 77 78
}

static gboolean
79 80 81
anim_image_draw (GtkWidget *widget,
                 cairo_t   *cr,
                 gpointer   data)
82 83 84
{
  g_print ("start busyness\n");

85
  g_signal_handlers_disconnect_by_func (widget, anim_image_draw, data);
86 87 88 89 90 91 92 93

  /* produce high load */
  g_idle_add_full (G_PRIORITY_DEFAULT,
                   idle_func, NULL, NULL);

  return FALSE;
}

94 95 96
int
main (int argc, char **argv)
{
97
  GtkWidget *window, *grid;
98 99 100 101 102 103
  GtkWidget *label, *image, *box;
  GtkIconTheme *theme;
  GdkPixbuf *pixbuf;
  GtkIconSet *iconset;
  GtkIconSource *iconsource;
  gchar *icon_name = "gnome-terminal";
104
  gchar *anim_filename = NULL;
105 106
  GIcon *icon;
  GFile *file;
Matthias Clasen's avatar
Matthias Clasen committed
107
  GdkGeometry geo;
108

109 110 111 112 113
  gtk_init (&argc, &argv);

  if (argc > 1)
    icon_name = argv[1];

114 115 116
  if (argc > 2)
    anim_filename = argv[2];

117
  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
Matthias Clasen's avatar
Matthias Clasen committed
118 119 120 121 122 123 124 125

  geo.min_width = 400;
  geo.min_height = 300;
  geo.max_width = 800;
  geo.max_height = 600;

  gtk_window_set_geometry_hints (GTK_WINDOW (window), NULL, &geo, GDK_HINT_MIN_SIZE | GDK_HINT_MAX_SIZE);

126 127
  grid = gtk_grid_new ();
  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 138 139 140 141

  theme = gtk_icon_theme_get_default ();
  pixbuf = gtk_icon_theme_load_icon (theme, icon_name, 48, 0, NULL);
  image = gtk_image_new_from_pixbuf (pixbuf);
  box = gtk_event_box_new ();
  gtk_container_add (GTK_CONTAINER (box), image);
142
  gtk_grid_attach (GTK_GRID (grid), box, 2, 1, 1, 1);
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160

  gtk_drag_source_set (box, GDK_BUTTON1_MASK, 
		       NULL, 0,
		       GDK_ACTION_COPY);
  gtk_drag_source_add_image_targets (box);
  g_signal_connect (box, "drag_begin", G_CALLBACK (drag_begin), image);
  g_signal_connect (box, "drag_data_get", G_CALLBACK (drag_data_get), image);

  gtk_drag_dest_set (box,
                     GTK_DEST_DEFAULT_MOTION |
                     GTK_DEST_DEFAULT_HIGHLIGHT |
                     GTK_DEST_DEFAULT_DROP,
                     NULL, 0, GDK_ACTION_COPY);
  gtk_drag_dest_add_image_targets (box);
  g_signal_connect (box, "drag_data_received", 
		    G_CALLBACK (drag_data_received), image);

  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 171 172 173 174

  iconsource = gtk_icon_source_new ();
  gtk_icon_source_set_icon_name (iconsource, icon_name);
  iconset = gtk_icon_set_new ();
  gtk_icon_set_add_source (iconset, iconsource);
  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 183
  image = gtk_image_new_from_icon_name (icon_name, GTK_ICON_SIZE_DIALOG);
  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 189 190
  icon = g_themed_icon_new_with_default_fallbacks ("folder-remote");
  image = gtk_image_new_from_gicon (icon, GTK_ICON_SIZE_DIALOG);
  g_object_unref (icon);
191
  gtk_grid_attach (GTK_GRID (grid), image, 1, 5, 1, 1);
192 193 194 195 196
  file = g_file_new_for_path ("apple-red.png");
  icon = g_file_icon_new (file);
  image = gtk_image_new_from_gicon (icon, GTK_ICON_SIZE_DIALOG);
  g_object_unref (icon);
  gtk_image_set_pixel_size (GTK_IMAGE (image), 30);
197
  gtk_grid_attach (GTK_GRID (grid), image, 2, 5, 1, 1);
198 199

  
200 201 202
  if (anim_filename)
    {
      label = gtk_label_new ("GTK_IMAGE_ANIMATION (from file)");
203
      gtk_grid_attach (GTK_GRID (grid), label, 0, 6, 1, 1);
204 205
      image = gtk_image_new_from_file (anim_filename);
      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 210
      g_signal_connect_after (image, "draw",
                              G_CALLBACK (anim_image_draw),
211 212 213
                              NULL);
    }

214 215 216 217 218 219
  gtk_widget_show_all (window);

  gtk_main ();

  return 0;
}