Gtk drag and drop
by CliveMcCarthy from LinuxQuestions.org on (#5N8S5)
I use Kernel 5.4 with Linux Mint 20.2 and the Mate (Gnome) 1.24 desktop.
I'm writing a simple Gtk application -- it's not my first -- it's basically a film strip image selector.
The program needs to drag and drop images. My drop code works fine :). I can successfully drop images from Caja (the File Manager for the Mate desktop) into my program.
However, I can't drag and drop images within my own program :(. Since the drop code works I must have something wrong with my drag code. My drag code looks like this:
Code:void set_drag_image
(
GtkWidget *widget,
__attribute__((unused))GdkDragContext *context,
GtkSelectionData *selection_data,
__attribute__((unused))guint info,
__attribute__((unused))guint time,
__attribute__((unused))gpointer data
)
{
char widget_name[10];
int strip_location;
guchar *dropped_text;
strcpy(widget_name, gtk_widget_get_name(widget));
strip_location = atoi(widget_name);
dropped_text = (guchar *)film_strip_data[strip_location].dropped_text;
printf
(
"source_drag_image called %s\n",
dropped_text
);
gtk_selection_data_set_text
(
selection_data, (gchar *)dropped_text, sizeof(dropped_text)
);
gtk_selection_data_set
(
selection_data,
gtk_selection_data_get_target(selection_data),
8, // 8 bits
dropped_text, sizeof(dropped_text)
);
}
...
for(int k = 0; k < STRIP_COUNT; k++) // iterate through the film strip's widgets
{
g_signal_connect
(
image_strip[k], "drag_data_get",
G_CALLBACK(set_drag_image),
NULL
);
gtk_drag_source_set
(
GTK_WIDGET(image_strip[k]),
GDK_BUTTON1_MASK,
drag_and_drop_type_table,
G_N_ELEMENTS(drag_and_drop_type_table),
GDK_ACTION_COPY
);
}The printf() shows nothing so the drop receiver isn't getting anything. The Gtk documentation is not at all helpful and I haven't found a good on-line code example.
I have been banging away for a few days and am getting nowhere. Thoughts please ?
I'm writing a simple Gtk application -- it's not my first -- it's basically a film strip image selector.
The program needs to drag and drop images. My drop code works fine :). I can successfully drop images from Caja (the File Manager for the Mate desktop) into my program.
However, I can't drag and drop images within my own program :(. Since the drop code works I must have something wrong with my drag code. My drag code looks like this:
Code:void set_drag_image
(
GtkWidget *widget,
__attribute__((unused))GdkDragContext *context,
GtkSelectionData *selection_data,
__attribute__((unused))guint info,
__attribute__((unused))guint time,
__attribute__((unused))gpointer data
)
{
char widget_name[10];
int strip_location;
guchar *dropped_text;
strcpy(widget_name, gtk_widget_get_name(widget));
strip_location = atoi(widget_name);
dropped_text = (guchar *)film_strip_data[strip_location].dropped_text;
printf
(
"source_drag_image called %s\n",
dropped_text
);
gtk_selection_data_set_text
(
selection_data, (gchar *)dropped_text, sizeof(dropped_text)
);
gtk_selection_data_set
(
selection_data,
gtk_selection_data_get_target(selection_data),
8, // 8 bits
dropped_text, sizeof(dropped_text)
);
}
...
for(int k = 0; k < STRIP_COUNT; k++) // iterate through the film strip's widgets
{
g_signal_connect
(
image_strip[k], "drag_data_get",
G_CALLBACK(set_drag_image),
NULL
);
gtk_drag_source_set
(
GTK_WIDGET(image_strip[k]),
GDK_BUTTON1_MASK,
drag_and_drop_type_table,
G_N_ELEMENTS(drag_and_drop_type_table),
GDK_ACTION_COPY
);
}The printf() shows nothing so the drop receiver isn't getting anything. The Gtk documentation is not at all helpful and I haven't found a good on-line code example.
I have been banging away for a few days and am getting nowhere. Thoughts please ?