1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393 | #include <ctk/ctk.h>
static GdkPixbuf *
get_image_pixbuf (CtkImage *image)
{
const gchar *icon_name;
CtkIconSize size;
CtkIconTheme *icon_theme;
int width;
switch (ctk_image_get_storage_type (image))
{
case CTK_IMAGE_PIXBUF:
return g_object_ref (ctk_image_get_pixbuf (image));
case CTK_IMAGE_ICON_NAME:
ctk_image_get_icon_name (image, &icon_name, &size);
icon_theme = ctk_icon_theme_get_for_screen (ctk_widget_get_screen (CTK_WIDGET (image)));
ctk_icon_size_lookup (size, &width, NULL);
return ctk_icon_theme_load_icon (icon_theme,
icon_name,
width,
CTK_ICON_LOOKUP_GENERIC_FALLBACK,
NULL);
default:
g_warning ("Image storage type %d not handled",
ctk_image_get_storage_type (image));
return NULL;
}
}
enum {
TARGET_IMAGE,
TARGET_TEXT
};
enum {
TOP_LEFT,
CENTER,
BOTTOM_RIGHT
};
static void
image_drag_begin (CtkWidget *widget G_GNUC_UNUSED,
CdkDragContext *context,
gpointer data)
{
GdkPixbuf *pixbuf;
gint hotspot;
gint hot_x, hot_y;
pixbuf = get_image_pixbuf (CTK_IMAGE (data));
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 = gdk_pixbuf_get_width (pixbuf) / 2;
hot_y = gdk_pixbuf_get_height (pixbuf) / 2;
break;
case BOTTOM_RIGHT:
hot_x = gdk_pixbuf_get_width (pixbuf);
hot_y = gdk_pixbuf_get_height (pixbuf);
break;
}
ctk_drag_set_icon_pixbuf (context, pixbuf, hot_x, hot_y);
g_object_unref (pixbuf);
}
static void
window_destroyed (CtkWidget *window, gpointer data)
{
CtkWidget *widget = data;
g_print ("drag widget destroyed\n");
g_object_unref (window);
g_object_set_data (G_OBJECT (widget), "drag window", NULL);
}
static void
window_drag_end (CtkWidget *ebox,
CdkDragContext *context G_GNUC_UNUSED,
gpointer data)
{
CtkWidget *window = data;
ctk_widget_destroy (window);
g_signal_handlers_disconnect_by_func (ebox, window_drag_end, data);
}
static void
window_drag_begin (CtkWidget *widget,
CdkDragContext *context,
gpointer data)
{
CtkWidget *window;
int hotspot;
hotspot = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (data), "hotspot"));
window = g_object_get_data (G_OBJECT (widget), "drag window");
if (window == NULL)
{
GdkPixbuf *pixbuf;
CtkWidget *image;
window = ctk_window_new (CTK_WINDOW_POPUP);
g_print ("creating new drag widget\n");
pixbuf = get_image_pixbuf (CTK_IMAGE (data));
image = ctk_image_new_from_pixbuf (pixbuf);
g_object_unref (pixbuf);
ctk_widget_show (image);
ctk_container_add (CTK_CONTAINER (window), image);
g_object_ref (window);
g_object_set_data (G_OBJECT (widget), "drag window", window);
g_signal_connect (window, "destroy", G_CALLBACK (window_destroyed), widget);
}
else
g_print ("reusing drag widget\n");
ctk_drag_set_icon_widget (context, window, 0, 0);
if (hotspot == CENTER)
g_signal_connect (widget, "drag-end", G_CALLBACK (window_drag_end), window);
}
static void
update_source_target_list (CtkWidget *ebox, CtkWidget *image)
{
CtkTargetList *target_list;
target_list = ctk_target_list_new (NULL, 0);
ctk_target_list_add_image_targets (target_list, TARGET_IMAGE, FALSE);
if (ctk_image_get_storage_type (CTK_IMAGE (image)) == CTK_IMAGE_ICON_NAME)
ctk_target_list_add_text_targets (target_list, TARGET_TEXT);
ctk_drag_source_set_target_list (ebox, target_list);
ctk_target_list_unref (target_list);
}
static void
update_dest_target_list (CtkWidget *ebox)
{
CtkTargetList *target_list;
target_list = ctk_target_list_new (NULL, 0);
ctk_target_list_add_image_targets (target_list, TARGET_IMAGE, FALSE);
ctk_target_list_add_text_targets (target_list, TARGET_TEXT);
ctk_drag_dest_set_target_list (ebox, target_list);
ctk_target_list_unref (target_list);
}
void
image_drag_data_get (CtkWidget *widget G_GNUC_UNUSED,
CdkDragContext *context G_GNUC_UNUSED,
CtkSelectionData *selection_data,
guint info,
guint time G_GNUC_UNUSED,
gpointer data)
{
GdkPixbuf *pixbuf;
const gchar *name;
switch (info)
{
case TARGET_IMAGE:
pixbuf = get_image_pixbuf (CTK_IMAGE (data));
ctk_selection_data_set_pixbuf (selection_data, pixbuf);
g_object_unref (pixbuf);
break;
case TARGET_TEXT:
if (ctk_image_get_storage_type (CTK_IMAGE (data)) == CTK_IMAGE_ICON_NAME)
ctk_image_get_icon_name (CTK_IMAGE (data), &name, NULL);
else
name = "Boo!";
ctk_selection_data_set_text (selection_data, name, -1);
break;
default:
g_assert_not_reached ();
}
}
static void
image_drag_data_received (CtkWidget *widget G_GNUC_UNUSED,
CdkDragContext *context G_GNUC_UNUSED,
gint x G_GNUC_UNUSED,
gint y G_GNUC_UNUSED,
CtkSelectionData *selection_data,
guint info,
guint32 time G_GNUC_UNUSED,
gpointer data)
{
GdkPixbuf *pixbuf;
gchar *text;
if (ctk_selection_data_get_length (selection_data) == 0)
return;
switch (info)
{
case TARGET_IMAGE:
pixbuf = ctk_selection_data_get_pixbuf (selection_data);
ctk_image_set_from_pixbuf (CTK_IMAGE (data), pixbuf);
g_object_unref (pixbuf);
break;
case TARGET_TEXT:
text = (gchar *)ctk_selection_data_get_text (selection_data);
ctk_image_set_from_icon_name (CTK_IMAGE (data), text, CTK_ICON_SIZE_DIALOG);
g_free (text);
break;
default:
g_assert_not_reached ();
}
}
CtkWidget *
make_image (const gchar *icon_name, int hotspot)
{
CtkWidget *image, *ebox;
image = ctk_image_new_from_icon_name (icon_name, CTK_ICON_SIZE_DIALOG);
ebox = ctk_event_box_new ();
ctk_drag_source_set (ebox, CDK_BUTTON1_MASK, NULL, 0, CDK_ACTION_COPY);
update_source_target_list (ebox, image);
g_object_set_data (G_OBJECT (image), "hotspot", GINT_TO_POINTER (hotspot));
g_signal_connect (ebox, "drag-begin", G_CALLBACK (image_drag_begin), image);
g_signal_connect (ebox, "drag-data-get", G_CALLBACK (image_drag_data_get), image);
ctk_drag_dest_set (ebox, CTK_DEST_DEFAULT_ALL, NULL, 0, CDK_ACTION_COPY);
g_signal_connect (ebox, "drag-data-received", G_CALLBACK (image_drag_data_received), image);
update_dest_target_list (ebox);
ctk_container_add (CTK_CONTAINER (ebox), image);
return ebox;
}
CtkWidget *
make_image2 (const gchar *icon_name, int hotspot)
{
CtkWidget *image, *ebox;
image = ctk_image_new_from_icon_name (icon_name, CTK_ICON_SIZE_DIALOG);
ebox = ctk_event_box_new ();
ctk_drag_source_set (ebox, CDK_BUTTON1_MASK, NULL, 0, CDK_ACTION_COPY);
update_source_target_list (ebox, image);
g_object_set_data (G_OBJECT (image), "hotspot", GINT_TO_POINTER (hotspot));
g_signal_connect (ebox, "drag-begin", G_CALLBACK (window_drag_begin), image);
g_signal_connect (ebox, "drag-data-get", G_CALLBACK (image_drag_data_get), image);
ctk_drag_dest_set (ebox, CTK_DEST_DEFAULT_ALL, NULL, 0, CDK_ACTION_COPY);
g_signal_connect (ebox, "drag-data-received", G_CALLBACK (image_drag_data_received), image);
update_dest_target_list (ebox);
ctk_container_add (CTK_CONTAINER (ebox), image);
return ebox;
}
static void
spinner_drag_begin (CtkWidget *widget G_GNUC_UNUSED,
CdkDragContext *context,
gpointer data G_GNUC_UNUSED)
{
CtkWidget *spinner;
g_print ("CtkWidget::drag-begin\n");
spinner = g_object_new (CTK_TYPE_SPINNER,
"visible", TRUE,
"active", TRUE,
NULL);
ctk_drag_set_icon_widget (context, spinner, 0, 0);
g_object_set_data (G_OBJECT (context), "spinner", spinner);
}
static void
spinner_drag_end (CtkWidget *widget G_GNUC_UNUSED,
CdkDragContext *context,
gpointer data G_GNUC_UNUSED)
{
CtkWidget *spinner;
g_print ("CtkWidget::drag-end\n");
spinner = g_object_get_data (G_OBJECT (context), "spinner");
ctk_widget_destroy (spinner);
}
static gboolean
spinner_drag_failed (CtkWidget *widget G_GNUC_UNUSED,
CdkDragContext *context G_GNUC_UNUSED,
CtkDragResult result,
gpointer data G_GNUC_UNUSED)
{
GTypeClass *class;
GEnumValue *value;
class = g_type_class_ref (CTK_TYPE_DRAG_RESULT);
value = g_enum_get_value (G_ENUM_CLASS (class), result);
g_print ("CtkWidget::drag-failed %s\n", value->value_nick);
g_type_class_unref (class);
return FALSE;
}
void
spinner_drag_data_get (CtkWidget *widget G_GNUC_UNUSED,
CdkDragContext *context G_GNUC_UNUSED,
CtkSelectionData *selection_data,
guint info G_GNUC_UNUSED,
guint time G_GNUC_UNUSED,
gpointer data G_GNUC_UNUSED)
{
g_print ("CtkWidget::drag-data-get\n");
ctk_selection_data_set_text (selection_data, "ACTIVE", -1);
}
static CtkWidget *
make_spinner (void)
{
CtkWidget *spinner, *ebox;
spinner = ctk_spinner_new ();
ctk_spinner_start (CTK_SPINNER (spinner));
ebox = ctk_event_box_new ();
ctk_drag_source_set (ebox, CDK_BUTTON1_MASK, NULL, 0, CDK_ACTION_COPY);
ctk_drag_source_add_text_targets (ebox);
g_signal_connect (ebox, "drag-begin", G_CALLBACK (spinner_drag_begin), spinner);
g_signal_connect (ebox, "drag-end", G_CALLBACK (spinner_drag_end), spinner);
g_signal_connect (ebox, "drag-failed", G_CALLBACK (spinner_drag_failed), spinner);
g_signal_connect (ebox, "drag-data-get", G_CALLBACK (spinner_drag_data_get), spinner);
ctk_container_add (CTK_CONTAINER (ebox), spinner);
return ebox;
}
int
main (int argc G_GNUC_UNUSED,
char *Argv[] G_GNUC_UNUSED)<--- There is an unknown macro here somewhere. Configuration is required. If G_GNUC_UNUSED is a macro then please configure it.
{
CtkWidget *window;
CtkWidget *grid;
CtkWidget *entry;
ctk_init (NULL, NULL);
window = ctk_window_new (CTK_WINDOW_TOPLEVEL);
ctk_window_set_title (CTK_WINDOW (window), "Drag And Drop");
ctk_window_set_resizable (CTK_WINDOW (window), FALSE);
grid = ctk_grid_new ();
g_object_set (grid,
"margin", 20,
"row-spacing", 20,
"column-spacing", 20,
NULL);
ctk_container_add (CTK_CONTAINER (window), grid);
ctk_grid_attach (CTK_GRID (grid), make_image ("dialog-warning", TOP_LEFT), 0, 0, 1, 1);
ctk_grid_attach (CTK_GRID (grid), make_image ("process-stop", BOTTOM_RIGHT), 1, 0, 1, 1);
entry = ctk_entry_new ();
ctk_grid_attach (CTK_GRID (grid), entry, 0, 1, 2, 1);
ctk_grid_attach (CTK_GRID (grid), make_spinner (), 0, 2, 1, 1);
ctk_grid_attach (CTK_GRID (grid), make_image ("weather-clear", CENTER), 1, 2, 1, 1);
ctk_grid_attach (CTK_GRID (grid), make_image2 ("dialog-question", TOP_LEFT), 0, 3, 1, 1);
ctk_grid_attach (CTK_GRID (grid), make_image2 ("dialog-information", CENTER), 1, 3, 1, 1);
ctk_widget_show_all (window);
ctk_main ();
return 0;
}
|