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 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141
|
/* GTK - The GIMP Toolkit
* Copyright (C) 1995-1999 Peter Mattis, Spencer Kimball and Josh MacDonald
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
* file for a list of people on the GTK+ Team. See the ChangeLog
* files for a list of changes. These files are distributed with
* GTK+ at ftp://ftp.gtk.org/pub/gtk/.
*/
#include "config.h"
#include "gtkdroptarget.h"
#include "gtkdropprivate.h"
#include "gtkeventcontrollerprivate.h"
#include "gtkmarshalers.h"
#include "gdk/gdkmarshalers.h"
#include "gtknative.h"
#include "gtkprivate.h"
#include "gtktypebuiltins.h"
/**
* GtkDropTarget:
*
* An event controller to receive Drag-and-Drop operations.
*
* The most basic way to use a `GtkDropTarget` to receive drops on a
* widget is to create it via [ctor@Gtk.DropTarget.new], passing in the
* `GType` of the data you want to receive and connect to the
* [signal@Gtk.DropTarget::drop] signal to receive the data:
*
* ```c
* static gboolean
* on_drop (GtkDropTarget *target,
* const GValue *value,
* double x,
* double y,
* gpointer data)
* {
* MyWidget *self = data;
*
* // Call the appropriate setter depending on the type of data
* // that we received
* if (G_VALUE_HOLDS (value, G_TYPE_FILE))
* my_widget_set_file (self, g_value_get_object (value));
* else if (G_VALUE_HOLDS (value, GDK_TYPE_PIXBUF))
* my_widget_set_pixbuf (self, g_value_get_object (value));
* else
* return FALSE;
*
* return TRUE;
* }
*
* static void
* my_widget_init (MyWidget *self)
* {
* GtkDropTarget *target =
* gtk_drop_target_new (G_TYPE_INVALID, GDK_ACTION_COPY);
*
* // This widget accepts two types of drop types: GFile objects
* // and GdkPixbuf objects
* gtk_drop_target_set_gtypes (target, (GType [2]) {
* G_TYPE_FILE,
* GDK_TYPE_PIXBUF,
* }, 2);
*
* g_signal_connect (target, "drop", G_CALLBACK (on_drop), self);
* gtk_widget_add_controller (GTK_WIDGET (self), GTK_EVENT_CONTROLLER (target));
* }
* ```
*
* `GtkDropTarget` supports more options, such as:
*
* * rejecting potential drops via the [signal@Gtk.DropTarget::accept] signal
* and the [method@Gtk.DropTarget.reject] function to let other drop
* targets handle the drop
* * tracking an ongoing drag operation before the drop via the
* [signal@Gtk.DropTarget::enter], [signal@Gtk.DropTarget::motion] and
* [signal@Gtk.DropTarget::leave] signals
* * configuring how to receive data by setting the
* [property@Gtk.DropTarget:preload] property and listening for its
* availability via the [property@Gtk.DropTarget:value] property
*
* However, `GtkDropTarget` is ultimately modeled in a synchronous way
* and only supports data transferred via `GType`. If you want full control
* over an ongoing drop, the [class@Gtk.DropTargetAsync] object gives you
* this ability.
*
* While a pointer is dragged over the drop target's widget and the drop
* has not been rejected, that widget will receive the
* %GTK_STATE_FLAG_DROP_ACTIVE state, which can be used to style the widget.
*
* If you are not interested in receiving the drop, but just want to update
* UI state during a Drag-and-Drop operation (e.g. switching tabs), you can
* use [class@Gtk.DropControllerMotion].
*/
struct _GtkDropTarget
{
GtkEventController parent_object;
GdkContentFormats *formats;
GdkDragAction actions;
guint preload : 1;
guint dropping : 1;
graphene_point_t coords;
GdkDrop *drop;
GCancellable *cancellable; /* NULL unless doing a read of value */
GValue value;
};
struct _GtkDropTargetClass
{
GtkEventControllerClass parent_class;
gboolean (* accept) (GtkDropTarget *self,
GdkDrop *drop);
GdkDragAction (* enter) (GtkDropTarget *self,
double x,
double y);
GdkDragAction (* motion) (GtkDropTarget *self,
double x,
double y);
void (* leave) (GtkDropTarget *self,
GdkDrop *drop);
gboolean (* drop) (GtkDropTarget *self,
const GValue *value,
double x,
double y);
};
enum {
PROP_0,
PROP_ACTIONS,
PROP_CURRENT_DROP,
PROP_DROP,
PROP_FORMATS,
PROP_PRELOAD,
PROP_VALUE,
NUM_PROPERTIES
};
static GParamSpec *properties[NUM_PROPERTIES];
enum {
ACCEPT,
ENTER,
MOTION,
LEAVE,
DROP,
NUM_SIGNALS
};
static guint signals[NUM_SIGNALS];
G_DEFINE_TYPE (GtkDropTarget, gtk_drop_target, GTK_TYPE_EVENT_CONTROLLER);
static void
gtk_drop_target_end_drop (GtkDropTarget *self)
{
if (self->drop == NULL)
return;
g_object_freeze_notify (G_OBJECT (self));
if (self->dropping)
{
gdk_drop_finish (self->drop, GDK_ACTION_NONE);
self->dropping = FALSE;
}
g_clear_object (&self->drop);
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_DROP]);
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_CURRENT_DROP]);
if (G_IS_VALUE (&self->value))
{
g_value_unset (&self->value);
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_VALUE]);
}
if (self->cancellable)
{
g_cancellable_cancel (self->cancellable);
g_clear_object (&self->cancellable);
}
gtk_widget_unset_state_flags (gtk_event_controller_get_widget (GTK_EVENT_CONTROLLER (self)),
GTK_STATE_FLAG_DROP_ACTIVE);
g_object_thaw_notify (G_OBJECT (self));
}
static GdkDragAction
make_action_unique (GdkDragAction actions)
{
if (actions & GDK_ACTION_COPY)
return GDK_ACTION_COPY;
if (actions & GDK_ACTION_MOVE)
return GDK_ACTION_MOVE;
if (actions & GDK_ACTION_LINK)
return GDK_ACTION_LINK;
return GDK_ACTION_NONE;
}
static void
gtk_drop_target_do_drop (GtkDropTarget *self)
{
gboolean success;
g_assert (self->dropping);
g_assert (G_IS_VALUE (&self->value));
g_signal_emit (self, signals[DROP], 0, &self->value, self->coords.x, self->coords.y, &success);
if (success)
gdk_drop_finish (self->drop, make_action_unique (self->actions & gdk_drop_get_actions (self->drop)));
else
gdk_drop_finish (self->drop, GDK_ACTION_NONE);
self->dropping = FALSE;
gtk_drop_target_end_drop (self);
}
static void
gtk_drop_target_load_done (GObject *source,
GAsyncResult *res,
gpointer data)
{
GtkDropTarget *self = data;
const GValue *value;
GError *error = NULL;
value = gdk_drop_read_value_finish (GDK_DROP (source), res, &error);
if (value == NULL)
{
/* If this happens, data/self is invalid */
if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
{
g_clear_error (&error);
return;
}
g_clear_object (&self->cancellable);
/* XXX: Should this be a warning? */
g_warning ("Failed to receive drop data: %s", error->message);
g_clear_error (&error);
gtk_drop_target_end_drop (self);
return;
}
g_clear_object (&self->cancellable);
g_value_init (&self->value, G_VALUE_TYPE (value));
g_value_copy (value, &self->value);
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_VALUE]);
if (self->dropping)
gtk_drop_target_do_drop (self);
}
static gboolean
gtk_drop_target_load_local (GtkDropTarget *self,
GType type)
{
GdkDrag *drag;
drag = gdk_drop_get_drag (self->drop);
if (drag == NULL)
return FALSE;
g_value_init (&self->value, type);
if (gdk_content_provider_get_value (gdk_drag_get_content (drag),
&self->value,
NULL))
{
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_VALUE]);
return TRUE;
}
g_value_unset (&self->value);
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_VALUE]);
return FALSE;
}
static gboolean
gtk_drop_target_load (GtkDropTarget *self)
{
GType type;
g_assert (self->drop);
if (G_IS_VALUE (&self->value))
return TRUE;
if (self->cancellable)
return FALSE;
type = gdk_content_formats_match_gtype (self->formats, gdk_drop_get_formats (self->drop));
if (gtk_drop_target_load_local (self, type))
return TRUE;
self->cancellable = g_cancellable_new ();
gdk_drop_read_value_async (self->drop,
type,
G_PRIORITY_DEFAULT,
self->cancellable,
gtk_drop_target_load_done,
g_object_ref (self));
return FALSE;
}
static void
gtk_drop_target_start_drop (GtkDropTarget *self,
GdkDrop *drop)
{
g_object_freeze_notify (G_OBJECT (self));
gtk_drop_target_end_drop (self);
self->drop = g_object_ref (drop);
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_DROP]);
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_CURRENT_DROP]);
if (self->preload)
gtk_drop_target_load (self);
gtk_widget_set_state_flags (gtk_event_controller_get_widget (GTK_EVENT_CONTROLLER (self)),
GTK_STATE_FLAG_DROP_ACTIVE,
FALSE);
g_object_thaw_notify (G_OBJECT (self));
}
static gboolean
gtk_drop_target_accept (GtkDropTarget *self,
GdkDrop *drop)
{
if ((gdk_drop_get_actions (drop) & gtk_drop_target_get_actions (self)) == GDK_ACTION_NONE)
return FALSE;
if (self->formats == NULL)
return TRUE;
return gdk_content_formats_match_gtype (self->formats, gdk_drop_get_formats (drop)) != G_TYPE_INVALID;
}
static GdkDragAction
gtk_drop_target_enter (GtkDropTarget *self,
double x,
double y)
{
return make_action_unique (self->actions & gdk_drop_get_actions (self->drop));
}
static GdkDragAction
gtk_drop_target_motion (GtkDropTarget *self,
double x,
double y)
{
return make_action_unique (self->actions & gdk_drop_get_actions (self->drop));
}
static gboolean
gtk_drop_target_drop (GtkDropTarget *self,
const GValue *value,
double x,
double y)
{
return FALSE;
}
static gboolean
gtk_drop_target_filter_event (GtkEventController *controller,
GdkEvent *event)
{
switch ((int) gdk_event_get_event_type (event))
{
case GDK_DRAG_ENTER:
case GDK_DRAG_LEAVE:
case GDK_DRAG_MOTION:
case GDK_DROP_START:
return GTK_EVENT_CONTROLLER_CLASS (gtk_drop_target_parent_class)->filter_event (controller, event);
default:;
}
return TRUE;
}
static gboolean
gtk_drop_target_handle_event (GtkEventController *controller,
GdkEvent *event,
double x,
double y)
{
GtkDropTarget *self = GTK_DROP_TARGET (controller);
/* All drops have been rejected. New drops only arrive via crossing
* events, so we can: */
if (self->drop == NULL)
return FALSE;
switch ((int) gdk_event_get_event_type (event))
{
case GDK_DRAG_MOTION:
{
GtkWidget *widget = gtk_event_controller_get_widget (controller);
GdkDragAction preferred;
/* sanity check */
g_return_val_if_fail (self->drop == gdk_dnd_event_get_drop (event), FALSE);
graphene_point_init (&self->coords, x, y);
g_signal_emit (self, signals[MOTION], 0, x, y, &preferred);
if (!gdk_drag_action_is_unique (preferred))
{
g_critical ("Handler for GtkDropTarget::motion on %s %p did not return a unique preferred action",
G_OBJECT_TYPE_NAME (widget), widget);
preferred = make_action_unique (preferred);
}
if (preferred &&
gtk_drop_status (self->drop, self->actions, preferred))
{
gtk_widget_set_state_flags (widget, GTK_STATE_FLAG_DROP_ACTIVE, FALSE);
}
else
{
gtk_widget_unset_state_flags (widget, GTK_STATE_FLAG_DROP_ACTIVE);
}
}
return FALSE;
case GDK_DROP_START:
{
/* sanity check */
g_return_val_if_fail (self->drop == gdk_dnd_event_get_drop (event), FALSE);
graphene_point_init (&self->coords, x, y);
self->dropping = TRUE;
if (gtk_drop_target_load (self))
gtk_drop_target_do_drop (self);
return TRUE;
}
default:
return FALSE;
}
}
static void
gtk_drop_target_handle_crossing (GtkEventController *controller,
const GtkCrossingData *crossing,
double x,
double y)
{
GtkDropTarget *self = GTK_DROP_TARGET (controller);
GtkWidget *widget = gtk_event_controller_get_widget (controller);
if (crossing->type != GTK_CROSSING_DROP)
return;
/* sanity check */
g_warn_if_fail (self->drop == NULL || self->drop == crossing->drop);
if (crossing->direction == GTK_CROSSING_IN)
{
gboolean accept = FALSE;
GdkDragAction preferred;
if (self->drop != NULL)
return;
/* if we were a target already but self->drop == NULL, the drop
* was rejected already */
if (crossing->old_descendent != NULL ||
crossing->old_target == widget)
return;
g_signal_emit (self, signals[ACCEPT], 0, crossing->drop, &accept);
if (!accept)
return;
graphene_point_init (&self->coords, x, y);
gtk_drop_target_start_drop (self, crossing->drop);
/* start_drop ends w/ thaw_notify, where handler may reject, so recheck */
if (self->drop != NULL)
g_signal_emit (self, signals[ENTER], 0, x, y, &preferred);
else
preferred = 0;
if (!gdk_drag_action_is_unique (preferred))
{
g_critical ("Handler for GtkDropTarget::enter on %s %p did not return a unique preferred action",
G_OBJECT_TYPE_NAME (widget), widget);
preferred = make_action_unique (preferred);
}
if (preferred &&
gtk_drop_status (self->drop, self->actions, preferred))
{
gtk_widget_set_state_flags (widget, GTK_STATE_FLAG_DROP_ACTIVE, FALSE);
}
else
{
gtk_widget_unset_state_flags (widget, GTK_STATE_FLAG_DROP_ACTIVE);
}
}
else
{
if (crossing->new_descendent != NULL ||
crossing->new_target == widget)
return;
g_signal_emit (self, signals[LEAVE], 0);
if (!self->dropping)
gtk_drop_target_end_drop (self);
gtk_widget_unset_state_flags (widget, GTK_STATE_FLAG_DROP_ACTIVE);
}
}
static void
gtk_drop_target_finalize (GObject *object)
{
GtkDropTarget *self = GTK_DROP_TARGET (object);
g_clear_pointer (&self->formats, gdk_content_formats_unref);
G_OBJECT_CLASS (gtk_drop_target_parent_class)->finalize (object);
}
static void
gtk_drop_target_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
GtkDropTarget *self = GTK_DROP_TARGET (object);
switch (prop_id)
{
case PROP_ACTIONS:
gtk_drop_target_set_actions (self, g_value_get_flags (value));
break;
case PROP_FORMATS:
self->formats = g_value_dup_boxed (value);
if (self->formats == NULL)
self->formats = gdk_content_formats_new (NULL, 0);
break;
case PROP_PRELOAD:
gtk_drop_target_set_preload (self, g_value_get_boolean (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
gtk_drop_target_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
GtkDropTarget *self = GTK_DROP_TARGET (object);
switch (prop_id)
{
case PROP_ACTIONS:
g_value_set_flags (value, self->actions);
break;
case PROP_DROP:
case PROP_CURRENT_DROP:
g_value_set_object (value, self->drop);
break;
case PROP_FORMATS:
g_value_set_boxed (value, self->formats);
break;
case PROP_PRELOAD:
g_value_set_boolean (value, self->preload);
break;
case PROP_VALUE:
if (G_IS_VALUE (&self->value))
g_value_set_boxed (value, &self->value);
else
g_value_set_boxed (value, NULL);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
gtk_drop_target_class_init (GtkDropTargetClass *class)
{
GObjectClass *object_class = G_OBJECT_CLASS (class);
GtkEventControllerClass *controller_class = GTK_EVENT_CONTROLLER_CLASS (class);
object_class->finalize = gtk_drop_target_finalize;
object_class->set_property = gtk_drop_target_set_property;
object_class->get_property = gtk_drop_target_get_property;
controller_class->handle_event = gtk_drop_target_handle_event;
controller_class->filter_event = gtk_drop_target_filter_event;
controller_class->handle_crossing = gtk_drop_target_handle_crossing;
class->accept = gtk_drop_target_accept;
class->enter = gtk_drop_target_enter;
class->motion = gtk_drop_target_motion;
class->drop = gtk_drop_target_drop;
/**
* GtkDropTarget:actions:
*
* The `GdkDragActions` that this drop target supports.
*/
properties[PROP_ACTIONS] =
g_param_spec_flags ("actions", NULL, NULL,
GDK_TYPE_DRAG_ACTION, GDK_ACTION_NONE,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);
/**
* GtkDropTarget:drop:
*
* The `GdkDrop` that is currently being performed.
*
* Deprecated: 4.4: Use [property@Gtk.DropTarget:current-drop] instead
*/
properties[PROP_DROP] =
g_param_spec_object ("drop", NULL, NULL,
GDK_TYPE_DROP,
GTK_PARAM_READABLE | G_PARAM_DEPRECATED);
/**
* GtkDropTarget:current-drop:
*
* The `GdkDrop` that is currently being performed.
*
* Since: 4.4
*/
properties[PROP_CURRENT_DROP] =
g_param_spec_object ("current-drop", NULL, NULL,
GDK_TYPE_DROP,
GTK_PARAM_READABLE);
/**
* GtkDropTarget:formats:
*
* The `GdkContentFormats` that determine the supported data formats.
*/
properties[PROP_FORMATS] =
g_param_spec_boxed ("formats", NULL, NULL,
GDK_TYPE_CONTENT_FORMATS,
GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);
/**
* GtkDropTarget:preload:
*
* Whether the drop data should be preloaded when the pointer is only
* hovering over the widget but has not been released.
*
* Setting this property allows finer grained reaction to an ongoing
* drop at the cost of loading more data.
*
* The default value for this property is %FALSE to avoid downloading
* huge amounts of data by accident.
*
* For example, if somebody drags a full document of gigabytes of text
* from a text editor across a widget with a preloading drop target,
* this data will be downloaded, even if the data is ultimately dropped
* elsewhere.
*
* For a lot of data formats, the amount of data is very small (like
* %GDK_TYPE_RGBA), so enabling this property does not hurt at all.
* And for local-only Drag-and-Drop operations, no data transfer is done,
* so enabling it there is free.
*/
properties[PROP_PRELOAD] =
g_param_spec_boolean ("preload", NULL, NULL,
FALSE,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);
/**
* GtkDropTarget:value:
*
* The value for this drop operation.
*
* This is %NULL if the data has not been loaded yet or no drop
* operation is going on.
*
* Data may be available before the [signal@Gtk.DropTarget::drop]
* signal gets emitted - for example when the [property@Gtk.DropTarget:preload]
* property is set. You can use the ::notify signal to be notified
* of available data.
*/
properties[PROP_VALUE] =
g_param_spec_boxed ("value", NULL, NULL,
G_TYPE_VALUE,
GTK_PARAM_READABLE);
g_object_class_install_properties (object_class, NUM_PROPERTIES, properties);
/**
* GtkDropTarget::accept:
* @self: the `GtkDropTarget`
* @drop: the `GdkDrop`
*
* Emitted on the drop site when a drop operation is about to begin.
*
* If the drop is not accepted, %FALSE will be returned and the drop target
* will ignore the drop. If %TRUE is returned, the drop is accepted for now
* but may be rejected later via a call to [method@Gtk.DropTarget.reject]
* or ultimately by returning %FALSE from a [signal@Gtk.DropTarget::drop]
* handler.
*
* The default handler for this signal decides whether to accept the drop
* based on the formats provided by the @drop.
*
* If the decision whether the drop will be accepted or rejected depends
* on the data, this function should return %TRUE, the
* [property@Gtk.DropTarget:preload] property should be set and the value
* should be inspected via the ::notify:value signal, calling
* [method@Gtk.DropTarget.reject] if required.
*
* Returns: %TRUE if @drop is accepted
*/
signals[ACCEPT] =
g_signal_new (I_("accept"),
G_TYPE_FROM_CLASS (class),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (GtkDropTargetClass, accept),
g_signal_accumulator_first_wins, NULL,
_gdk_marshal_BOOLEAN__OBJECT,
G_TYPE_BOOLEAN, 1,
GDK_TYPE_DROP);
g_signal_set_va_marshaller (signals[ACCEPT],
GTK_TYPE_DROP_TARGET,
_gdk_marshal_BOOLEAN__OBJECTv);
/**
* GtkDropTarget::enter:
* @self: the `GtkDropTarget`
* @x: the x coordinate of the current pointer position
* @y: the y coordinate of the current pointer position
*
* Emitted on the drop site when the pointer enters the widget.
*
* It can be used to set up custom highlighting.
*
* Returns: Preferred action for this drag operation or `GDK_ACTION_NONE` if
* dropping is not supported at the current @x,@y location.
*/
signals[ENTER] =
g_signal_new (I_("enter"),
G_TYPE_FROM_CLASS (class),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (GtkDropTargetClass, enter),
g_signal_accumulator_first_wins, NULL,
_gtk_marshal_FLAGS__DOUBLE_DOUBLE,
GDK_TYPE_DRAG_ACTION, 2,
G_TYPE_DOUBLE, G_TYPE_DOUBLE);
g_signal_set_va_marshaller (signals[ENTER],
GTK_TYPE_DROP_TARGET,
_gtk_marshal_FLAGS__DOUBLE_DOUBLEv);
/**
* GtkDropTarget::motion:
* @self: the `GtkDropTarget`
* @x: the x coordinate of the current pointer position
* @y: the y coordinate of the current pointer position
*
* Emitted while the pointer is moving over the drop target.
*
* Returns: Preferred action for this drag operation or `GDK_ACTION_NONE` if
* dropping is not supported at the current @x,@y location.
*/
signals[MOTION] =
g_signal_new (I_("motion"),
G_TYPE_FROM_CLASS (class),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (GtkDropTargetClass, motion),
g_signal_accumulator_first_wins, NULL,
_gtk_marshal_FLAGS__DOUBLE_DOUBLE,
GDK_TYPE_DRAG_ACTION, 2,
G_TYPE_DOUBLE, G_TYPE_DOUBLE);
g_signal_set_va_marshaller (signals[MOTION],
GTK_TYPE_DROP_TARGET,
_gtk_marshal_FLAGS__DOUBLE_DOUBLEv);
/**
* GtkDropTarget::leave:
* @self: the `GtkDropTarget`
*
* Emitted on the drop site when the pointer leaves the widget.
*
* Its main purpose it to undo things done in
* [signal@Gtk.DropTarget::enter].
*/
signals[LEAVE] =
g_signal_new (I_("leave"),
G_TYPE_FROM_CLASS (class),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (GtkDropTargetClass, leave),
NULL, NULL,
NULL,
G_TYPE_NONE, 0);
/**
* GtkDropTarget::drop:
* @self: the `GtkDropTarget`
* @value: the `GValue` being dropped
* @x: the x coordinate of the current pointer position
* @y: the y coordinate of the current pointer position
*
* Emitted on the drop site when the user drops the data onto the widget.
*
* The signal handler must determine whether the pointer position is in
* a drop zone or not. If it is not in a drop zone, it returns %FALSE
* and no further processing is necessary.
*
* Otherwise, the handler returns %TRUE. In this case, this handler will
* accept the drop. The handler is responsible for using the given @value
* and performing the drop operation.
*
* Returns: whether the drop was accepted at the given pointer position
*/
signals[DROP] =
g_signal_new (I_("drop"),
G_TYPE_FROM_CLASS (class),
G_SIGNAL_RUN_LAST,
0,
g_signal_accumulator_first_wins, NULL,
_gtk_marshal_BOOLEAN__BOXED_DOUBLE_DOUBLE,
G_TYPE_BOOLEAN, 3,
G_TYPE_VALUE, G_TYPE_DOUBLE, G_TYPE_DOUBLE);
g_signal_set_va_marshaller (signals[DROP],
GTK_TYPE_DROP_TARGET,
_gtk_marshal_BOOLEAN__BOXED_DOUBLE_DOUBLEv);
}
static void
gtk_drop_target_init (GtkDropTarget *self)
{
}
/**
* gtk_drop_target_new:
* @type: The supported type or %G_TYPE_INVALID
* @actions: the supported actions
*
* Creates a new `GtkDropTarget` object.
*
* If the drop target should support more than 1 type, pass
* %G_TYPE_INVALID for @type and then call
* [method@Gtk.DropTarget.set_gtypes].
*
* Returns: the new `GtkDropTarget`
*/
GtkDropTarget *
gtk_drop_target_new (GType type,
GdkDragAction actions)
{
GtkDropTarget *result;
GdkContentFormats *formats;
if (type != G_TYPE_INVALID)
formats = gdk_content_formats_new_for_gtype (type);
else
formats = NULL;
result = g_object_new (GTK_TYPE_DROP_TARGET,
"formats", formats,
"actions", actions,
NULL);
g_clear_pointer (&formats, gdk_content_formats_unref);
return result;
}
/**
* gtk_drop_target_get_formats:
* @self: a `GtkDropTarget`
*
* Gets the data formats that this drop target accepts.
*
* If the result is %NULL, all formats are expected to be supported.
*
* Returns: (nullable) (transfer none): the supported data formats
*/
GdkContentFormats *
gtk_drop_target_get_formats (GtkDropTarget *self)
{
g_return_val_if_fail (GTK_IS_DROP_TARGET (self), NULL);
return self->formats;
}
/**
* gtk_drop_target_set_gtypes:
* @self: a `GtkDropTarget`
* @types: (nullable) (transfer none) (array length=n_types): all supported `GType`s
* that can be dropped on the target
* @n_types: number of @types
*
* Sets the supported `GType`s for this drop target.
*/
void
gtk_drop_target_set_gtypes (GtkDropTarget *self,
GType *types,
gsize n_types)
{
GdkContentFormatsBuilder *builder;
gsize i;
g_return_if_fail (GTK_IS_DROP_TARGET (self));
g_return_if_fail (n_types == 0 || types != NULL);
gdk_content_formats_unref (self->formats);
builder = gdk_content_formats_builder_new ();
for (i = 0; i < n_types; i++)
gdk_content_formats_builder_add_gtype (builder, types[i]);
self->formats = gdk_content_formats_builder_free_to_formats (builder);
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_FORMATS]);
}
/**
* gtk_drop_target_get_gtypes:
* @self: a `GtkDropTarget`
* @n_types: (out) (optional): the number of `GType`s contained in the
* return value
*
* Gets the list of supported `GType`s that can be dropped on the target.
*
* If no types have been set, `NULL` will be returned.
*
* Returns: (transfer none) (nullable) (array length=n_types):
* the `G_TYPE_INVALID`-terminated array of types included in
* formats
*/
const GType *
gtk_drop_target_get_gtypes (GtkDropTarget *self,
gsize *n_types)
{
g_return_val_if_fail (GTK_IS_DROP_TARGET (self), NULL);
return gdk_content_formats_get_gtypes (self->formats, n_types);
}
/**
* gtk_drop_target_set_actions:
* @self: a `GtkDropTarget`
* @actions: the supported actions
*
* Sets the actions that this drop target supports.
*/
void
gtk_drop_target_set_actions (GtkDropTarget *self,
GdkDragAction actions)
{
g_return_if_fail (GTK_IS_DROP_TARGET (self));
if (self->actions == actions)
return;
self->actions = actions;
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_ACTIONS]);
}
/**
* gtk_drop_target_get_actions:
* @self: a `GtkDropTarget`
*
* Gets the actions that this drop target supports.
*
* Returns: the actions that this drop target supports
*/
GdkDragAction
gtk_drop_target_get_actions (GtkDropTarget *self)
{
g_return_val_if_fail (GTK_IS_DROP_TARGET (self), GDK_ACTION_NONE);
return self->actions;
}
/**
* gtk_drop_target_set_preload:
* @self: a `GtkDropTarget`
* @preload: %TRUE to preload drop data
*
* Sets whether data should be preloaded on hover.
*/
void
gtk_drop_target_set_preload (GtkDropTarget *self,
gboolean preload)
{
g_return_if_fail (GTK_IS_DROP_TARGET (self));
if (self->preload == preload)
return;
self->preload = preload;
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_PRELOAD]);
}
/**
* gtk_drop_target_get_preload:
* @self: a `GtkDropTarget`
*
* Gets whether data should be preloaded on hover.
*
* Returns: %TRUE if drop data should be preloaded
*/
gboolean
gtk_drop_target_get_preload (GtkDropTarget *self)
{
g_return_val_if_fail (GTK_IS_DROP_TARGET (self), 0);
return self->preload;
}
/**
* gtk_drop_target_get_drop:
* @self: a `GtkDropTarget`
*
* Gets the currently handled drop operation.
*
* If no drop operation is going on, %NULL is returned.
*
* Returns: (nullable) (transfer none): The current drop
*
* Deprecated: 4.4: Use [method@Gtk.DropTarget.get_current_drop] instead
*/
GdkDrop *
gtk_drop_target_get_drop (GtkDropTarget *self)
{
g_return_val_if_fail (GTK_IS_DROP_TARGET (self), NULL);
return self->drop;
}
/**
* gtk_drop_target_get_current_drop:
* @self: a `GtkDropTarget`
*
* Gets the currently handled drop operation.
*
* If no drop operation is going on, %NULL is returned.
*
* Returns: (nullable) (transfer none): The current drop
*
* Since: 4.4
*/
GdkDrop *
gtk_drop_target_get_current_drop (GtkDropTarget *self)
{
g_return_val_if_fail (GTK_IS_DROP_TARGET (self), NULL);
return self->drop;
}
/**
* gtk_drop_target_get_value:
* @self: a `GtkDropTarget`
*
* Gets the current drop data, as a `GValue`.
*
* Returns: (nullable) (transfer none): The current drop data
*/
const GValue *
gtk_drop_target_get_value (GtkDropTarget *self)
{
g_return_val_if_fail (GTK_IS_DROP_TARGET (self), NULL);
if (!G_IS_VALUE (&self->value))
return NULL;
return &self->value;
}
/**
* gtk_drop_target_reject:
* @self: a `GtkDropTarget`
*
* Rejects the ongoing drop operation.
*
* If no drop operation is ongoing, i.e when [property@Gtk.DropTarget:current-drop]
* is %NULL, this function does nothing.
*
* This function should be used when delaying the decision
* on whether to accept a drag or not until after reading
* the data.
*/
void
gtk_drop_target_reject (GtkDropTarget *self)
{
g_return_if_fail (GTK_IS_DROP_TARGET (self));
if (self->drop == NULL)
return;
gtk_drop_target_end_drop (self);
}
|