testclipboard.c 3.84 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * Copyright (C) 2011  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 Jardon's avatar
Javier Jardon committed
15
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16 17 18 19 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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
 */

#include <gtk/gtk.h>

GtkClipboard *clipboard;
GtkWidget *image;
GtkWidget *label;

#define SIZE 256.

static void
image_request_cb (GtkClipboard *clipboard,
                  GdkPixbuf *pixbuf,
                  gpointer data)
{
  GdkPixbuf *copy;
  int height;
  int width;
  gdouble factor;
  char *str;

  if (pixbuf != NULL)
    {
      height = gdk_pixbuf_get_height (pixbuf);
      width = gdk_pixbuf_get_width (pixbuf);

      factor = MAX ((SIZE / height), (SIZE / width));

      copy = gdk_pixbuf_scale_simple (pixbuf, width * factor, height * factor, GDK_INTERP_BILINEAR);
      gtk_image_set_from_pixbuf (GTK_IMAGE (image), copy);
      g_object_unref (copy);
      str = g_strdup_printf ("<b>Image</b> %d \342\234\225 %d", width, height);
    }
  else
    {
      str = g_strdup ("<b>No image data</b>");
    }
  gtk_label_set_markup (GTK_LABEL (label), str);
  g_free (str);
}

static void
update_display (void)
{
  gtk_clipboard_request_image (clipboard, image_request_cb, NULL);
}

static void
on_owner_change (GtkClipboard *clipboard,
                 GdkEvent     *event,
                 gpointer      user_data)
{
  update_display ();
}

static void
on_response (GtkDialog *dialog,
             gint       response_id,
             gpointer   user_data)
{
  switch (response_id)
    {
    case 0:
      /* copy large */
      {
        GtkIconTheme *theme;
        GdkPixbuf *pixbuf;
        theme = gtk_icon_theme_get_default ();
84 85
        pixbuf = gtk_icon_theme_load_icon (theme, "utilities-terminal", 1600, 0, NULL);
        g_assert_nonnull (pixbuf);
86 87 88 89 90 91 92 93 94
        gtk_clipboard_set_image (clipboard, pixbuf);
      }
      break;
    case 1:
      /* copy small */
      {
        GtkIconTheme *theme;
        GdkPixbuf *pixbuf;
        theme = gtk_icon_theme_get_default ();
95 96
        pixbuf = gtk_icon_theme_load_icon (theme, "utilities-terminal", 48, 0, NULL);
        g_assert_nonnull (pixbuf);
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
        gtk_clipboard_set_image (clipboard, pixbuf);
      }
      break;
    case GTK_RESPONSE_CLOSE:
    default:
      gtk_main_quit ();
      break;
    }
}

int
main (int argc, char **argv)
{
  GtkWidget *window;

  gtk_init (&argc, &argv);

  window = gtk_dialog_new_with_buttons ("Clipboard",
                                        NULL,
                                        0,
                                        "Copy Large", 0,
                                        "Copy Small", 1,
119
                                        "_Close", GTK_RESPONSE_CLOSE,
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
                                        NULL);

  image = gtk_image_new ();
  gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (window))), image, FALSE, FALSE, 0);
  label = gtk_label_new ("No data found");
  gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (window))), label, FALSE, FALSE, 0);

  g_signal_connect (window, "response", G_CALLBACK (on_response), NULL);

  clipboard = gtk_clipboard_get_for_display (gtk_widget_get_display (window),
                                             GDK_SELECTION_CLIPBOARD);
  g_signal_connect (clipboard, "owner-change", G_CALLBACK (on_owner_change), NULL);

  update_display ();

  gtk_widget_show_all (window);

  gtk_main ();

  return 0;
}