forked from AuroraMiddleware/gtk
tests: Convert to GtkDragSource
Some tests, such as testimage did not have anything particularly worth keeping, so were removed instead of fixed.
This commit is contained in:
parent
78a0913f0f
commit
b4c689ecd6
@ -53,7 +53,6 @@ gtk_tests = [
|
||||
['testiconview-keynav'],
|
||||
['testicontheme'],
|
||||
['testinfobar'],
|
||||
['testimage'],
|
||||
['testkineticscrolling'],
|
||||
['testlist'],
|
||||
['testlist2'],
|
||||
|
@ -313,8 +313,6 @@ target_drag_motion (GtkWidget *widget,
|
||||
gint x,
|
||||
gint y)
|
||||
{
|
||||
GtkWidget *source_widget;
|
||||
GdkDrag *drag;
|
||||
char *s;
|
||||
|
||||
if (!have_drag)
|
||||
@ -323,12 +321,6 @@ target_drag_motion (GtkWidget *widget,
|
||||
gtk_image_set_from_pixbuf (GTK_IMAGE (widget), trashcan_open);
|
||||
}
|
||||
|
||||
drag = gdk_drop_get_drag (drop);
|
||||
source_widget = drag ? gtk_drag_get_source_widget (drag) : NULL;
|
||||
g_print ("motion, source %s\n", source_widget ?
|
||||
G_OBJECT_TYPE_NAME (source_widget) :
|
||||
"NULL");
|
||||
|
||||
s = gdk_content_formats_to_string (gdk_drop_get_formats (drop));
|
||||
g_print ("%s\n", s);
|
||||
|
||||
@ -416,20 +408,6 @@ label_drag_data_received (GtkWidget *widget,
|
||||
gdk_drop_finish (drop, 0);
|
||||
}
|
||||
|
||||
void
|
||||
source_drag_data_get (GtkWidget *widget,
|
||||
GdkDrag *drag,
|
||||
GtkSelectionData *selection_data,
|
||||
gpointer data)
|
||||
{
|
||||
if (gtk_selection_data_get_target (selection_data) == g_intern_static_string ("application/x-rootwindow-drop"))
|
||||
g_print ("I was dropped on the rootwin\n");
|
||||
else
|
||||
gtk_selection_data_set (selection_data,
|
||||
gtk_selection_data_get_target (selection_data),
|
||||
8, (guchar *) "I'm Data!", 9);
|
||||
}
|
||||
|
||||
/* The following is a rather elaborate example demonstrating/testing
|
||||
* changing of the window hierarchy during a drag - in this case,
|
||||
* via a "spring-loaded" popup window.
|
||||
@ -564,7 +542,6 @@ popsite_leave (GtkWidget *widget,
|
||||
|
||||
void
|
||||
source_drag_data_delete (GtkWidget *widget,
|
||||
GdkDrag *drag,
|
||||
gpointer data)
|
||||
{
|
||||
g_print ("Delete the data!\n");
|
||||
@ -587,6 +564,9 @@ main (int argc, char **argv)
|
||||
GtkWidget *button;
|
||||
GdkPixbuf *drag_icon;
|
||||
GdkTexture *texture;
|
||||
GdkContentProvider *content;
|
||||
GValue value = G_VALUE_INIT;
|
||||
GtkDragSource *source;
|
||||
GdkContentFormats *targets;
|
||||
|
||||
test_init ();
|
||||
@ -661,12 +641,13 @@ main (int argc, char **argv)
|
||||
|
||||
button = gtk_button_new_with_label ("Drag Here\n");
|
||||
|
||||
targets = gdk_content_formats_new (target_table, n_targets);
|
||||
gtk_drag_source_set (button, GDK_BUTTON1_MASK | GDK_BUTTON3_MASK,
|
||||
targets,
|
||||
GDK_ACTION_COPY | GDK_ACTION_MOVE);
|
||||
gtk_drag_source_set_icon_paintable (button, GDK_PAINTABLE (texture));
|
||||
gdk_content_formats_unref (targets);
|
||||
g_value_init (&value, G_TYPE_STRING);
|
||||
g_value_set_string (&value, "I'm data!");
|
||||
content = gdk_content_provider_new_for_value (&value);
|
||||
g_value_unset (&value);
|
||||
source = gtk_drag_source_new (content, GDK_ACTION_COPY | GDK_ACTION_MOVE);
|
||||
gtk_drag_source_attach (source, button, GDK_BUTTON1_MASK | GDK_BUTTON3_MASK);
|
||||
gtk_drag_source_set_icon (source, GDK_PAINTABLE (texture), 0, 0);
|
||||
|
||||
g_object_unref (texture);
|
||||
|
||||
@ -674,9 +655,7 @@ main (int argc, char **argv)
|
||||
gtk_widget_set_vexpand (button, TRUE);
|
||||
gtk_grid_attach (GTK_GRID (grid), button, 0, 1, 1, 1);
|
||||
|
||||
g_signal_connect (button, "drag-data-get",
|
||||
G_CALLBACK (source_drag_data_get), NULL);
|
||||
g_signal_connect (button, "drag-data-delete",
|
||||
g_signal_connect (source, "drag-data-delete",
|
||||
G_CALLBACK (source_drag_data_delete), NULL);
|
||||
|
||||
gtk_widget_show (window);
|
||||
|
344
tests/testdnd2.c
344
tests/testdnd2.c
@ -37,114 +37,12 @@ enum {
|
||||
BOTTOM_RIGHT
|
||||
};
|
||||
|
||||
static void
|
||||
image_drag_begin (GtkWidget *widget,
|
||||
GdkDrag *drag,
|
||||
gpointer data)
|
||||
{
|
||||
GdkPaintable *paintable;
|
||||
gint hotspot;
|
||||
gint hot_x, hot_y;
|
||||
gint size;
|
||||
|
||||
paintable = get_image_paintable (GTK_IMAGE (data), &size);
|
||||
hotspot = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (data), "hotspot"));
|
||||
switch (hotspot)
|
||||
{
|
||||
default:
|
||||
case TOP_LEFT:
|
||||
hot_x = 0;
|
||||
hot_y = 0;
|
||||
break;
|
||||
case CENTER:
|
||||
hot_x = size / 2;
|
||||
hot_y = size / 2;
|
||||
break;
|
||||
case BOTTOM_RIGHT:
|
||||
hot_x = size;
|
||||
hot_y = size;
|
||||
break;
|
||||
}
|
||||
gtk_drag_set_icon_paintable (drag, paintable, hot_x, hot_y);
|
||||
g_object_unref (paintable);
|
||||
}
|
||||
|
||||
static void
|
||||
drag_widget_destroyed (GtkWidget *image, gpointer data)
|
||||
{
|
||||
GtkWidget *widget = data;
|
||||
|
||||
g_print ("drag widget destroyed\n");
|
||||
g_object_unref (image);
|
||||
g_object_set_data (G_OBJECT (widget), "drag widget", NULL);
|
||||
}
|
||||
|
||||
static void
|
||||
window_drag_end (GtkWidget *widget,
|
||||
GdkDrag *drag,
|
||||
gpointer data)
|
||||
{
|
||||
GtkWidget *window = data;
|
||||
|
||||
gtk_widget_destroy (window);
|
||||
g_signal_handlers_disconnect_by_func (widget, window_drag_end, data);
|
||||
}
|
||||
|
||||
static void
|
||||
window_drag_begin (GtkWidget *widget,
|
||||
GdkDrag *drag,
|
||||
gpointer data)
|
||||
{
|
||||
GdkPaintable *paintable;
|
||||
GtkWidget *image;
|
||||
int hotspot;
|
||||
int size;
|
||||
|
||||
hotspot = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (data), "hotspot"));
|
||||
|
||||
image = g_object_get_data (G_OBJECT (widget), "drag widget");
|
||||
if (image == NULL)
|
||||
{
|
||||
g_print ("creating new drag widget\n");
|
||||
paintable = get_image_paintable (GTK_IMAGE (data), &size);
|
||||
image = gtk_image_new_from_paintable (paintable);
|
||||
g_object_unref (paintable);
|
||||
g_object_ref (image);
|
||||
g_object_set_data (G_OBJECT (widget), "drag widget", image);
|
||||
g_signal_connect (image, "destroy", G_CALLBACK (drag_widget_destroyed), widget);
|
||||
}
|
||||
else
|
||||
g_print ("reusing drag widget\n");
|
||||
|
||||
gtk_drag_set_icon_widget (drag, image, 0, 0);
|
||||
|
||||
if (hotspot == CENTER)
|
||||
g_signal_connect (widget, "drag-end", G_CALLBACK (window_drag_end), image);
|
||||
}
|
||||
|
||||
static void
|
||||
update_source_target_list (GtkWidget *image)
|
||||
{
|
||||
GdkContentFormats *target_list;
|
||||
|
||||
target_list = gdk_content_formats_new (NULL, 0);
|
||||
|
||||
target_list = gtk_content_formats_add_image_targets (target_list, FALSE);
|
||||
if (gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_ICON_NAME)
|
||||
target_list = gtk_content_formats_add_text_targets (target_list);
|
||||
|
||||
gtk_drag_source_set_target_list (image, target_list);
|
||||
|
||||
gdk_content_formats_unref (target_list);
|
||||
}
|
||||
|
||||
static void
|
||||
update_dest_target_list (GtkWidget *image)
|
||||
{
|
||||
GdkContentFormats *target_list;
|
||||
|
||||
target_list = gdk_content_formats_new (NULL, 0);
|
||||
|
||||
target_list = gtk_content_formats_add_image_targets (target_list, FALSE);
|
||||
target_list = gtk_content_formats_add_text_targets (target_list);
|
||||
|
||||
@ -191,149 +89,191 @@ image_drag_data_received (GtkWidget *widget,
|
||||
GtkSelectionData *selection_data,
|
||||
gpointer data)
|
||||
{
|
||||
GdkTexture *texture;
|
||||
gchar *text;
|
||||
|
||||
if (gtk_selection_data_get_length (selection_data) == 0)
|
||||
return;
|
||||
|
||||
if (gtk_selection_data_targets_include_image (selection_data, FALSE))
|
||||
texture = gtk_selection_data_get_texture (selection_data);
|
||||
if (texture)
|
||||
{
|
||||
GdkTexture *texture;
|
||||
|
||||
texture = gtk_selection_data_get_texture (selection_data);
|
||||
gtk_image_set_from_paintable (GTK_IMAGE (data), GDK_PAINTABLE (texture));
|
||||
|
||||
gtk_image_set_from_paintable (GTK_IMAGE (widget), GDK_PAINTABLE (texture));
|
||||
g_object_unref (texture);
|
||||
return;
|
||||
}
|
||||
else if (gtk_selection_data_targets_include_text (selection_data))
|
||||
|
||||
text = (gchar *)gtk_selection_data_get_text (selection_data);
|
||||
if (text)
|
||||
{
|
||||
text = (gchar *)gtk_selection_data_get_text (selection_data);
|
||||
gtk_image_set_from_icon_name (GTK_IMAGE (data), text);
|
||||
gtk_image_set_from_icon_name (GTK_IMAGE (widget), text);
|
||||
g_free (text);
|
||||
}
|
||||
else
|
||||
{
|
||||
g_assert_not_reached ();
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
update_source_icon (GtkDragSource *source,
|
||||
const char *icon_name,
|
||||
int hotspot)
|
||||
{
|
||||
GdkPaintable *paintable;
|
||||
int hot_x, hot_y;
|
||||
int size = 48;
|
||||
|
||||
paintable = gtk_icon_theme_load_icon (gtk_icon_theme_get_default (),
|
||||
icon_name, size, 0, NULL);
|
||||
switch (hotspot)
|
||||
{
|
||||
default:
|
||||
case TOP_LEFT:
|
||||
hot_x = 0;
|
||||
hot_y = 0;
|
||||
break;
|
||||
case CENTER:
|
||||
hot_x = size / 2;
|
||||
hot_y = size / 2;
|
||||
break;
|
||||
case BOTTOM_RIGHT:
|
||||
hot_x = size;
|
||||
hot_y = size;
|
||||
break;
|
||||
}
|
||||
gtk_drag_source_set_icon (source, paintable, hot_x, hot_y);
|
||||
g_object_unref (paintable);
|
||||
}
|
||||
|
||||
static GBytes *
|
||||
get_data (const char *mimetype,
|
||||
gpointer data)
|
||||
{
|
||||
GtkWidget *image = data;
|
||||
GdkContentFormats *formats;
|
||||
gboolean want_text;
|
||||
|
||||
formats = gdk_content_formats_new (NULL, 0);
|
||||
formats = gtk_content_formats_add_text_targets (formats);
|
||||
want_text = gdk_content_formats_contain_mime_type (formats, mimetype);
|
||||
gdk_content_formats_unref (formats);
|
||||
|
||||
g_print ("get data called for %s\n", mimetype);
|
||||
if (want_text)
|
||||
{
|
||||
const char *text = gtk_image_get_icon_name (GTK_IMAGE (image));
|
||||
|
||||
return g_bytes_new (text, strlen (text) + 1);
|
||||
}
|
||||
else if (strcmp (mimetype, "image/png") == 0)
|
||||
{
|
||||
int size;
|
||||
GdkPaintable *paintable = get_image_paintable (GTK_IMAGE (image), &size);
|
||||
if (GDK_IS_TEXTURE (paintable))
|
||||
{
|
||||
char *name = g_strdup ("drag-data-XXXXXX");
|
||||
int fd;
|
||||
char *data;
|
||||
gsize size;
|
||||
|
||||
// FIXME: this is horrible
|
||||
|
||||
fd = g_mkstemp (name);
|
||||
close (fd);
|
||||
|
||||
gdk_texture_save_to_png (GDK_TEXTURE (paintable), name);
|
||||
|
||||
g_file_get_contents (name, &data, &size, NULL);
|
||||
g_free (name);
|
||||
|
||||
return g_bytes_new_take (data, size);
|
||||
}
|
||||
|
||||
g_clear_object (&paintable);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void
|
||||
drag_begin (GtkDragSource *source)
|
||||
{
|
||||
g_print ("drag begin\n");
|
||||
}
|
||||
|
||||
static void
|
||||
drag_end (GtkDragSource *source)
|
||||
{
|
||||
g_print ("drag end\n");
|
||||
}
|
||||
|
||||
static gboolean
|
||||
drag_failed (GtkDragSource *source, GdkDragCancelReason reason)
|
||||
{
|
||||
g_print ("drag failed: %d\n", reason);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
GtkWidget *
|
||||
make_image (const gchar *icon_name, int hotspot)
|
||||
{
|
||||
GtkWidget *image;
|
||||
GtkDragSource *source;
|
||||
GdkContentFormats *formats;
|
||||
GdkContentProvider *content;
|
||||
|
||||
image = gtk_image_new_from_icon_name (icon_name);
|
||||
gtk_image_set_icon_size (GTK_IMAGE (image), GTK_ICON_SIZE_LARGE);
|
||||
|
||||
gtk_drag_source_set (image, GDK_BUTTON1_MASK, NULL, GDK_ACTION_COPY);
|
||||
update_source_target_list (image);
|
||||
formats = gdk_content_formats_new (NULL, 0);
|
||||
formats = gtk_content_formats_add_image_targets (formats, FALSE);
|
||||
formats = gtk_content_formats_add_text_targets (formats);
|
||||
|
||||
g_object_set_data (G_OBJECT (image), "hotspot", GINT_TO_POINTER (hotspot));
|
||||
content = gdk_content_provider_new_with_formats (formats, get_data, image);
|
||||
source = gtk_drag_source_new (content, GDK_ACTION_COPY);
|
||||
g_object_unref (content);
|
||||
gdk_content_formats_unref (formats);
|
||||
update_source_icon (source, icon_name, hotspot);
|
||||
|
||||
g_signal_connect (image, "drag-begin", G_CALLBACK (image_drag_begin), image);
|
||||
g_signal_connect (image, "drag-data-get", G_CALLBACK (image_drag_data_get), image);
|
||||
g_signal_connect (source, "drag-begin", G_CALLBACK (drag_begin), NULL);
|
||||
g_signal_connect (source, "drag-end", G_CALLBACK (drag_end), NULL);
|
||||
g_signal_connect (source, "drag-failed", G_CALLBACK (drag_failed), NULL);
|
||||
gtk_drag_source_attach (source, image, GDK_BUTTON1_MASK);
|
||||
|
||||
gtk_drag_dest_set (image, GTK_DEST_DEFAULT_ALL, NULL, GDK_ACTION_COPY);
|
||||
g_signal_connect (image, "drag-data-received", G_CALLBACK (image_drag_data_received), image);
|
||||
update_dest_target_list (image);
|
||||
|
||||
return image;
|
||||
}
|
||||
|
||||
GtkWidget *
|
||||
make_image2 (const gchar *icon_name, int hotspot)
|
||||
{
|
||||
GtkWidget *image;
|
||||
|
||||
image = gtk_image_new_from_icon_name (icon_name);
|
||||
gtk_image_set_icon_size (GTK_IMAGE (image), GTK_ICON_SIZE_LARGE);
|
||||
|
||||
gtk_drag_source_set (image, GDK_BUTTON1_MASK, NULL, GDK_ACTION_COPY);
|
||||
update_source_target_list (image);
|
||||
|
||||
g_object_set_data (G_OBJECT (image), "hotspot", GINT_TO_POINTER (hotspot));
|
||||
|
||||
g_signal_connect (image, "drag-begin", G_CALLBACK (window_drag_begin), image);
|
||||
g_signal_connect (image, "drag-data-get", G_CALLBACK (image_drag_data_get), image);
|
||||
|
||||
gtk_drag_dest_set (image, GTK_DEST_DEFAULT_ALL, NULL, GDK_ACTION_COPY);
|
||||
g_signal_connect (image, "drag-data-received", G_CALLBACK (image_drag_data_received), image);
|
||||
g_signal_connect (image, "drag-data-received", G_CALLBACK (image_drag_data_received), NULL);
|
||||
update_dest_target_list (image);
|
||||
|
||||
return image;
|
||||
}
|
||||
|
||||
static void
|
||||
spinner_drag_begin (GtkWidget *widget,
|
||||
GdkDrag *drag,
|
||||
gpointer data)
|
||||
spinner_drag_begin (GtkDragSource *source,
|
||||
GtkWidget *widget)
|
||||
{
|
||||
GtkWidget *spinner;
|
||||
GdkPaintable *paintable;
|
||||
|
||||
g_print ("GtkWidget::drag-begin\n");
|
||||
spinner = g_object_new (GTK_TYPE_SPINNER,
|
||||
"visible", TRUE,
|
||||
"active", TRUE,
|
||||
NULL);
|
||||
gtk_drag_set_icon_widget (drag, spinner, 0, 0);
|
||||
g_object_set_data (G_OBJECT (drag), "spinner", spinner);
|
||||
}
|
||||
|
||||
static void
|
||||
spinner_drag_end (GtkWidget *widget,
|
||||
GdkDrag *drag,
|
||||
gpointer data)
|
||||
{
|
||||
GtkWidget *spinner;
|
||||
|
||||
g_print ("GtkWidget::drag-end\n");
|
||||
spinner = g_object_get_data (G_OBJECT (drag), "spinner");
|
||||
gtk_widget_destroy (spinner);
|
||||
}
|
||||
|
||||
static gboolean
|
||||
spinner_drag_failed (GtkWidget *widget,
|
||||
GdkDrag *drag,
|
||||
GtkDragResult result,
|
||||
gpointer data)
|
||||
{
|
||||
GTypeClass *class;
|
||||
GEnumValue *value;
|
||||
|
||||
class = g_type_class_ref (GTK_TYPE_DRAG_RESULT);
|
||||
value = g_enum_get_value (G_ENUM_CLASS (class), result);
|
||||
g_print ("GtkWidget::drag-failed %s\n", value->value_nick);
|
||||
g_type_class_unref (class);
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
void
|
||||
spinner_drag_data_get (GtkWidget *widget,
|
||||
GdkDrag *drag,
|
||||
GtkSelectionData *selection_data,
|
||||
gpointer data)
|
||||
{
|
||||
g_print ("GtkWidget::drag-data-get\n");
|
||||
gtk_selection_data_set_text (selection_data, "ACTIVE", -1);
|
||||
paintable = gtk_widget_paintable_new (widget);
|
||||
gtk_drag_source_set_icon (source, paintable, 0, 0);
|
||||
g_object_unref (paintable);
|
||||
}
|
||||
|
||||
static GtkWidget *
|
||||
make_spinner (void)
|
||||
{
|
||||
GtkWidget *spinner;
|
||||
GtkDragSource *source;
|
||||
GdkContentProvider *content;
|
||||
GValue value = G_VALUE_INIT;
|
||||
|
||||
spinner = gtk_spinner_new ();
|
||||
gtk_spinner_start (GTK_SPINNER (spinner));
|
||||
|
||||
gtk_drag_source_set (spinner, GDK_BUTTON1_MASK, NULL, GDK_ACTION_COPY);
|
||||
gtk_drag_source_add_text_targets (spinner);
|
||||
g_value_init (&value, G_TYPE_STRING);
|
||||
g_value_set_string (&value, "ACTIVE");
|
||||
content = gdk_content_provider_new_for_value (&value);
|
||||
source = gtk_drag_source_new (content, GDK_ACTION_COPY);
|
||||
g_signal_connect (source, "drag-begin", G_CALLBACK (spinner_drag_begin), spinner);
|
||||
gtk_drag_source_attach (source, spinner, GDK_BUTTON1_MASK);
|
||||
|
||||
g_signal_connect (spinner, "drag-begin", G_CALLBACK (spinner_drag_begin), spinner);
|
||||
g_signal_connect (spinner, "drag-end", G_CALLBACK (spinner_drag_end), spinner);
|
||||
g_signal_connect (spinner, "drag-failed", G_CALLBACK (spinner_drag_failed), spinner);
|
||||
g_signal_connect (spinner, "drag-data-get", G_CALLBACK (spinner_drag_data_get), spinner);
|
||||
g_object_unref (content);
|
||||
g_value_unset (&value);
|
||||
|
||||
return spinner;
|
||||
}
|
||||
@ -367,9 +307,9 @@ main (int argc, char *Argv[])
|
||||
gtk_grid_attach (GTK_GRID (grid), make_spinner (), 0, 2, 1, 1);
|
||||
gtk_grid_attach (GTK_GRID (grid), make_image ("weather-clear", CENTER), 1, 2, 1, 1);
|
||||
|
||||
gtk_grid_attach (GTK_GRID (grid), make_image2 ("dialog-question", TOP_LEFT), 0, 3, 1, 1);
|
||||
gtk_grid_attach (GTK_GRID (grid), make_image ("dialog-question", TOP_LEFT), 0, 3, 1, 1);
|
||||
|
||||
gtk_grid_attach (GTK_GRID (grid), make_image2 ("dialog-information", CENTER), 1, 3, 1, 1);
|
||||
gtk_grid_attach (GTK_GRID (grid), make_image ("dialog-information", CENTER), 1, 3, 1, 1);
|
||||
|
||||
gtk_widget_show (window);
|
||||
gtk_main ();
|
||||
|
@ -8,18 +8,6 @@ clear_pressed (GtkEntry *entry, gint icon, gpointer data)
|
||||
gtk_editable_set_text (GTK_EDITABLE (entry), "");
|
||||
}
|
||||
|
||||
static void
|
||||
drag_begin_cb (GtkWidget *widget,
|
||||
GdkDrag *drag,
|
||||
gpointer user_data)
|
||||
{
|
||||
gint pos;
|
||||
|
||||
pos = gtk_entry_get_current_icon_drag_source (GTK_ENTRY (widget));
|
||||
if (pos != -1)
|
||||
gtk_drag_set_icon_name (drag, "dialog-information", 2, 2);
|
||||
}
|
||||
|
||||
static void
|
||||
set_blank (GtkWidget *button,
|
||||
GtkEntry *entry)
|
||||
@ -172,8 +160,6 @@ main (int argc, char **argv)
|
||||
gtk_entry_set_icon_drag_source (GTK_ENTRY (entry),
|
||||
GTK_ENTRY_ICON_PRIMARY,
|
||||
content, GDK_ACTION_COPY);
|
||||
g_signal_connect_after (entry, "drag-begin",
|
||||
G_CALLBACK (drag_begin_cb), NULL);
|
||||
g_object_unref (content);
|
||||
|
||||
/*
|
||||
|
@ -1,178 +0,0 @@
|
||||
/* 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
|
||||
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <gtk/gtk.h>
|
||||
#include <gio/gio.h>
|
||||
|
||||
static void
|
||||
drag_begin (GtkWidget *widget,
|
||||
GdkDrag *drag,
|
||||
gpointer data)
|
||||
{
|
||||
GtkWidget *image = GTK_WIDGET (data);
|
||||
GdkPaintable *paintable;
|
||||
|
||||
paintable = gtk_image_get_paintable (GTK_IMAGE (image));
|
||||
gtk_drag_set_icon_paintable (drag, paintable, -2, -2);
|
||||
}
|
||||
|
||||
void
|
||||
drag_data_get (GtkWidget *widget,
|
||||
GdkDrag *drag,
|
||||
GtkSelectionData *selection_data,
|
||||
gpointer data)
|
||||
{
|
||||
GtkWidget *image = GTK_WIDGET (data);
|
||||
GdkPaintable *paintable;
|
||||
|
||||
paintable = gtk_image_get_paintable (GTK_IMAGE (image));
|
||||
if (GDK_IS_TEXTURE (paintable))
|
||||
gtk_selection_data_set_texture (selection_data, GDK_TEXTURE (paintable));
|
||||
}
|
||||
|
||||
static void
|
||||
drag_data_received (GtkWidget *widget,
|
||||
GdkDrag *drag,
|
||||
GtkSelectionData *selection_data,
|
||||
guint info,
|
||||
guint32 time,
|
||||
gpointer data)
|
||||
{
|
||||
GtkWidget *image = GTK_WIDGET (data);
|
||||
GdkTexture *texture;
|
||||
|
||||
if (gtk_selection_data_get_length (selection_data) < 0)
|
||||
return;
|
||||
|
||||
texture = gtk_selection_data_get_texture (selection_data);
|
||||
gtk_image_set_from_paintable (GTK_IMAGE (image), GDK_PAINTABLE (texture));
|
||||
|
||||
g_object_unref (texture);
|
||||
}
|
||||
|
||||
static gboolean
|
||||
idle_func (gpointer data)
|
||||
{
|
||||
g_print ("keep me busy\n");
|
||||
|
||||
return G_SOURCE_CONTINUE;
|
||||
}
|
||||
|
||||
int
|
||||
main (int argc, char **argv)
|
||||
{
|
||||
GtkWidget *window, *grid;
|
||||
GtkWidget *label, *image;
|
||||
GtkIconTheme *theme;
|
||||
GdkPaintable *paintable;
|
||||
gchar *icon_name = "help-browser";
|
||||
gchar *anim_filename = NULL;
|
||||
GtkIconInfo *icon_info;
|
||||
GIcon *icon;
|
||||
GFile *file;
|
||||
|
||||
gtk_init ();
|
||||
|
||||
if (argc > 1)
|
||||
icon_name = argv[1];
|
||||
|
||||
if (argc > 2)
|
||||
anim_filename = argv[2];
|
||||
|
||||
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
|
||||
|
||||
grid = gtk_grid_new ();
|
||||
gtk_grid_set_row_spacing (GTK_GRID (grid), 10);
|
||||
gtk_grid_set_column_spacing (GTK_GRID (grid), 10);
|
||||
gtk_container_add (GTK_CONTAINER (window), grid);
|
||||
|
||||
label = gtk_label_new ("symbolic size");
|
||||
gtk_grid_attach (GTK_GRID (grid), label, 1, 0, 1, 1);
|
||||
label = gtk_label_new ("fixed size");
|
||||
gtk_grid_attach (GTK_GRID (grid), label, 2, 0, 1, 1);
|
||||
|
||||
label = gtk_label_new ("GTK_IMAGE_PIXBUF");
|
||||
gtk_grid_attach (GTK_GRID (grid), label, 0, 1, 1, 1);
|
||||
|
||||
theme = gtk_icon_theme_get_default ();
|
||||
icon_info = gtk_icon_theme_lookup_icon_for_scale (theme, icon_name, 48, gtk_widget_get_scale_factor (window), GTK_ICON_LOOKUP_GENERIC_FALLBACK);
|
||||
paintable = gtk_icon_info_load_icon (icon_info, NULL);
|
||||
g_object_unref (icon_info);
|
||||
image = gtk_image_new_from_paintable (paintable);
|
||||
g_object_unref (paintable);
|
||||
gtk_grid_attach (GTK_GRID (grid), image, 2, 1, 1, 1);
|
||||
|
||||
gtk_drag_source_set (image, GDK_BUTTON1_MASK,
|
||||
NULL,
|
||||
GDK_ACTION_COPY);
|
||||
gtk_drag_source_add_image_targets (image);
|
||||
g_signal_connect (image, "drag_begin", G_CALLBACK (drag_begin), image);
|
||||
g_signal_connect (image, "drag_data_get", G_CALLBACK (drag_data_get), image);
|
||||
|
||||
gtk_drag_dest_set (image,
|
||||
GTK_DEST_DEFAULT_MOTION |
|
||||
GTK_DEST_DEFAULT_HIGHLIGHT |
|
||||
GTK_DEST_DEFAULT_DROP,
|
||||
NULL, GDK_ACTION_COPY);
|
||||
gtk_drag_dest_add_image_targets (image);
|
||||
g_signal_connect (image, "drag_data_received",
|
||||
G_CALLBACK (drag_data_received), image);
|
||||
|
||||
label = gtk_label_new ("GTK_IMAGE_ICON_NAME");
|
||||
gtk_grid_attach (GTK_GRID (grid), label, 0, 4, 1, 1);
|
||||
image = gtk_image_new_from_icon_name (icon_name);
|
||||
gtk_image_set_icon_size (GTK_IMAGE (image), GTK_ICON_SIZE_LARGE);
|
||||
gtk_grid_attach (GTK_GRID (grid), image, 1, 4, 1, 1);
|
||||
image = gtk_image_new_from_icon_name (icon_name);
|
||||
gtk_image_set_icon_size (GTK_IMAGE (image), GTK_ICON_SIZE_LARGE);
|
||||
gtk_image_set_pixel_size (GTK_IMAGE (image), 30);
|
||||
gtk_grid_attach (GTK_GRID (grid), image, 2, 4, 1, 1);
|
||||
|
||||
label = gtk_label_new ("GTK_IMAGE_GICON");
|
||||
gtk_grid_attach (GTK_GRID (grid), label, 0, 5, 1, 1);
|
||||
icon = g_themed_icon_new_with_default_fallbacks ("folder-remote");
|
||||
image = gtk_image_new_from_gicon (icon);
|
||||
gtk_image_set_icon_size (GTK_IMAGE (image), GTK_ICON_SIZE_LARGE);
|
||||
g_object_unref (icon);
|
||||
gtk_grid_attach (GTK_GRID (grid), image, 1, 5, 1, 1);
|
||||
file = g_file_new_for_path ("apple-red.png");
|
||||
icon = g_file_icon_new (file);
|
||||
image = gtk_image_new_from_gicon (icon);
|
||||
gtk_image_set_icon_size (GTK_IMAGE (image), GTK_ICON_SIZE_LARGE);
|
||||
g_object_unref (icon);
|
||||
gtk_image_set_pixel_size (GTK_IMAGE (image), 30);
|
||||
gtk_grid_attach (GTK_GRID (grid), image, 2, 5, 1, 1);
|
||||
|
||||
if (anim_filename)
|
||||
{
|
||||
label = gtk_label_new ("GTK_IMAGE_ANIMATION (from file)");
|
||||
gtk_grid_attach (GTK_GRID (grid), label, 0, 6, 1, 1);
|
||||
image = gtk_image_new_from_file (anim_filename);
|
||||
gtk_image_set_pixel_size (GTK_IMAGE (image), 30);
|
||||
gtk_grid_attach (GTK_GRID (grid), image, 2, 6, 1, 1);
|
||||
|
||||
/* produce high load */
|
||||
g_idle_add_full (G_PRIORITY_DEFAULT,
|
||||
idle_func, NULL, NULL);
|
||||
}
|
||||
|
||||
gtk_widget_show (window);
|
||||
|
||||
gtk_main ();
|
||||
|
||||
return 0;
|
||||
}
|
@ -5,9 +5,8 @@ static const char *entries[] = {
|
||||
};
|
||||
|
||||
static void
|
||||
drag_begin (GtkWidget *widget,
|
||||
GdkDrag *drag,
|
||||
gpointer data)
|
||||
drag_begin (GtkDragSource *source,
|
||||
GtkWidget *widget)
|
||||
{
|
||||
GtkWidget *row;
|
||||
GtkAllocation alloc;
|
||||
@ -19,26 +18,11 @@ drag_begin (GtkWidget *widget,
|
||||
|
||||
paintable = gtk_widget_paintable_new (row);
|
||||
gtk_widget_translate_coordinates (widget, row, 0, 0, &x, &y);
|
||||
gtk_drag_set_icon_paintable (drag, paintable, -x, -y);
|
||||
gtk_drag_source_set_icon (source, paintable, -x, -y);
|
||||
|
||||
g_object_unref (paintable);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
drag_data_get (GtkWidget *widget,
|
||||
GdkDrag *drag,
|
||||
GtkSelectionData *selection_data,
|
||||
gpointer data)
|
||||
{
|
||||
gtk_selection_data_set (selection_data,
|
||||
g_intern_static_string ("GTK_LIST_BOX_ROW"),
|
||||
32,
|
||||
(const guchar *)&widget,
|
||||
sizeof (gpointer));
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
drag_data_received (GtkWidget *widget,
|
||||
GdkDrop *drop,
|
||||
@ -69,7 +53,10 @@ static GtkWidget *
|
||||
create_row (const gchar *text)
|
||||
{
|
||||
GtkWidget *row, *box, *label, *image;
|
||||
GBytes *bytes;
|
||||
GdkContentProvider *content;
|
||||
GdkContentFormats *targets;
|
||||
GtkDragSource *source;
|
||||
|
||||
row = gtk_list_box_row_new ();
|
||||
image = gtk_image_new_from_icon_name ("open-menu-symbolic");
|
||||
@ -81,11 +68,13 @@ create_row (const gchar *text)
|
||||
gtk_container_add (GTK_CONTAINER (box), label);
|
||||
gtk_container_add (GTK_CONTAINER (box), image);
|
||||
|
||||
targets = gdk_content_formats_new (entries, 1);
|
||||
bytes = g_bytes_new (&row, sizeof (gpointer));
|
||||
content = gdk_content_provider_new_for_bytes ("GTK_LIST_BOX_ROW", bytes);
|
||||
source = gtk_drag_source_new (content, GDK_ACTION_MOVE);
|
||||
g_signal_connect (source, "drag-begin", G_CALLBACK (drag_begin), image);
|
||||
gtk_drag_source_attach (source, image, GDK_BUTTON1_MASK);
|
||||
|
||||
gtk_drag_source_set (image, GDK_BUTTON1_MASK, targets, GDK_ACTION_MOVE);
|
||||
g_signal_connect (image, "drag-begin", G_CALLBACK (drag_begin), NULL);
|
||||
g_signal_connect (image, "drag-data-get", G_CALLBACK (drag_data_get), NULL);
|
||||
targets = gdk_content_formats_new (entries, 1);
|
||||
|
||||
gtk_drag_dest_set (row, GTK_DEST_DEFAULT_ALL, targets, GDK_ACTION_MOVE);
|
||||
g_signal_connect (row, "drag-data-received", G_CALLBACK (drag_data_received), NULL);
|
||||
|
@ -90,21 +90,6 @@ on_page_reordered (GtkNotebook *notebook, GtkWidget *child, guint page_num, gpoi
|
||||
g_print ("page %d reordered\n", page_num);
|
||||
}
|
||||
|
||||
static void
|
||||
on_notebook_drag_begin (GtkWidget *widget,
|
||||
GdkDrag *drag,
|
||||
gpointer data)
|
||||
{
|
||||
guint page_num;
|
||||
|
||||
page_num = gtk_notebook_get_current_page (GTK_NOTEBOOK (widget));
|
||||
|
||||
if (page_num > 2)
|
||||
gtk_drag_set_icon_name (drag,
|
||||
(page_num % 2) ? "help-browser" : "process-stop",
|
||||
0, 0);
|
||||
}
|
||||
|
||||
static gboolean
|
||||
remove_in_idle (gpointer data)
|
||||
{
|
||||
@ -186,8 +171,6 @@ create_notebook (gchar **labels,
|
||||
|
||||
g_signal_connect (GTK_NOTEBOOK (notebook), "page-reordered",
|
||||
G_CALLBACK (on_page_reordered), NULL);
|
||||
g_signal_connect_after (G_OBJECT (notebook), "drag-begin",
|
||||
G_CALLBACK (on_notebook_drag_begin), NULL);
|
||||
return notebook;
|
||||
}
|
||||
|
||||
@ -233,8 +216,6 @@ create_notebook_non_dragable_content (gchar **labels,
|
||||
|
||||
g_signal_connect (GTK_NOTEBOOK (notebook), "page-reordered",
|
||||
G_CALLBACK (on_page_reordered), NULL);
|
||||
g_signal_connect_after (G_OBJECT (notebook), "drag-begin",
|
||||
G_CALLBACK (on_notebook_drag_begin), NULL);
|
||||
return notebook;
|
||||
}
|
||||
|
||||
@ -271,8 +252,6 @@ create_notebook_with_notebooks (gchar **labels,
|
||||
|
||||
g_signal_connect (GTK_NOTEBOOK (notebook), "page-reordered",
|
||||
G_CALLBACK (on_page_reordered), NULL);
|
||||
g_signal_connect_after (G_OBJECT (notebook), "drag-begin",
|
||||
G_CALLBACK (on_notebook_drag_begin), NULL);
|
||||
return notebook;
|
||||
}
|
||||
|
||||
|
@ -389,6 +389,8 @@ main (gint argc, gchar **argv)
|
||||
GtkWidget *hbox, *hbox1, *hbox2, *checkbox, *option_menu, *menu;
|
||||
gint i;
|
||||
GdkContentFormats *targets;
|
||||
GdkContentProvider *content;
|
||||
GtkDragSource *source;
|
||||
static const gchar *toolbar_styles[] = { "icons", "text", "both (vertical)",
|
||||
"both (horizontal)" };
|
||||
GtkToolItem *item;
|
||||
@ -616,9 +618,10 @@ main (gint argc, gchar **argv)
|
||||
gtk_container_add (GTK_CONTAINER (hbox), checkbox);
|
||||
|
||||
targets = gdk_content_formats_new (target_table, G_N_ELEMENTS (target_table));
|
||||
gtk_drag_source_set (button, GDK_BUTTON1_MASK,
|
||||
targets,
|
||||
GDK_ACTION_MOVE);
|
||||
content = gdk_content_provider_new_for_bytes (target_table[0], g_bytes_new ("", 1));
|
||||
source = gtk_drag_source_new (content, GDK_ACTION_MOVE);
|
||||
g_object_unref (content);
|
||||
gtk_drag_source_attach (source, button, GDK_BUTTON1_MASK);
|
||||
gtk_drag_dest_set (toolbar, GTK_DEST_DEFAULT_DROP,
|
||||
targets,
|
||||
GDK_ACTION_MOVE);
|
||||
|
Loading…
Reference in New Issue
Block a user