4 * An OpenGL based 'interactive canvas' library.
6 * Copyright © 2011 Intel Corporation.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
22 * Emmanuele Bassi <ebassi@linux.intel.com>
26 * SECTION:clutter-drop-action
27 * @Title: ClutterDropAction
28 * @short_description: An action for drop targets
30 * #ClutterDropAction is a #ClutterAction that allows a #ClutterActor
31 * implementation to control what happens when an actor dragged using
32 * a #ClutterDragAction crosses the target area or when a dragged actor
33 * is released (or "dropped") on the target area.
35 * A trivial use of #ClutterDropAction consists in connecting to the
36 * #ClutterDropAction::drop signal and handling the drop from there,
40 * ClutterAction *action = clutter_drop_action ();
42 * g_signal_connect (action, "drop", G_CALLBACK (on_drop), NULL);
43 * clutter_actor_add_action (an_actor, action);
46 * The #ClutterDropAction::can-drop can be used to control whether the
47 * #ClutterDropAction::drop signal is going to be emitted; returning %FALSE
48 * from a handler connected to the #ClutterDropAction::can-drop signal will
49 * cause the #ClutterDropAction::drop signal to be skipped when the input
50 * device button is released.
52 * <example id="drop-action-example">
53 * <title>Drop targets</title>
55 * <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../tests/interactive/test-drop.c">
56 * <xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback>
61 * It's important to note that #ClutterDropAction will only work with
62 * actors dragged using #ClutterDragAction.
64 * #ClutterDropAction is available since Clutter 1.8
71 #include "clutter-drop-action.h"
73 #include "clutter-actor-meta-private.h"
74 #include "clutter-actor-private.h"
75 #include "clutter-drag-action.h"
76 #include "clutter-main.h"
77 #include "clutter-marshal.h"
78 #include "clutter-stage-private.h"
80 struct _ClutterDropActionPrivate
88 typedef struct _DropTarget {
95 ClutterDropAction *last_action;
108 static guint drop_signals[LAST_SIGNAL] = { 0, };
110 G_DEFINE_TYPE (ClutterDropAction, clutter_drop_action, CLUTTER_TYPE_ACTION)
113 drop_target_free (gpointer _data)
115 DropTarget *data = _data;
117 g_signal_handler_disconnect (data->stage, data->capture_id);
118 g_hash_table_destroy (data->actions);
123 on_stage_capture (ClutterStage *stage,
127 DropTarget *data = user_data;
128 gfloat event_x, event_y;
129 ClutterInputDevice *device;
130 ClutterActor *actor, *drag_actor;
131 ClutterDropAction *drop_action;
132 gboolean was_reactive;
134 if (!(clutter_event_type (event) == CLUTTER_MOTION ||
135 clutter_event_type (event) == CLUTTER_BUTTON_RELEASE))
136 return CLUTTER_EVENT_PROPAGATE;
138 if (!(clutter_event_get_state (event) & CLUTTER_BUTTON1_MASK))
139 return CLUTTER_EVENT_PROPAGATE;
141 clutter_event_get_coords (event, &event_x, &event_y);
142 device = clutter_event_get_device (event);
144 drag_actor = _clutter_stage_get_drag_actor (stage, device);
145 if (drag_actor == NULL)
146 return CLUTTER_EVENT_PROPAGATE;
148 /* get the actor under the cursor, excluding the dragged actor; we
149 * use reactivity because it won't cause any scene invalidation
151 was_reactive = clutter_actor_get_reactive (drag_actor);
152 clutter_actor_set_reactive (drag_actor, FALSE);
154 actor = clutter_stage_get_actor_at_pos (stage, CLUTTER_PICK_REACTIVE,
157 if (actor == NULL || actor == CLUTTER_ACTOR (stage))
159 if (data->last_action != NULL)
161 ClutterActorMeta *meta = CLUTTER_ACTOR_META (data->last_action);
163 g_signal_emit (data->last_action, drop_signals[OVER_OUT], 0,
164 clutter_actor_meta_get_actor (meta));
166 data->last_action = NULL;
172 drop_action = g_hash_table_lookup (data->actions, actor);
174 if (drop_action == NULL)
176 if (data->last_action != NULL)
178 ClutterActorMeta *meta = CLUTTER_ACTOR_META (data->last_action);
180 g_signal_emit (data->last_action, drop_signals[OVER_OUT], 0,
181 clutter_actor_meta_get_actor (meta));
183 data->last_action = NULL;
190 if (data->last_action != drop_action)
192 ClutterActorMeta *meta;
194 if (data->last_action != NULL)
196 meta = CLUTTER_ACTOR_META (data->last_action);
198 g_signal_emit (data->last_action, drop_signals[OVER_OUT], 0,
199 clutter_actor_meta_get_actor (meta));
202 meta = CLUTTER_ACTOR_META (drop_action);
204 g_signal_emit (drop_action, drop_signals[OVER_IN], 0,
205 clutter_actor_meta_get_actor (meta));
208 data->last_action = drop_action;
212 if (clutter_event_type (event) == CLUTTER_BUTTON_RELEASE)
214 if (data->last_action != NULL)
216 ClutterActorMeta *meta = CLUTTER_ACTOR_META (data->last_action);
217 gboolean can_drop = FALSE;
219 g_signal_emit (data->last_action, drop_signals[CAN_DROP], 0,
220 clutter_actor_meta_get_actor (meta),
226 g_signal_emit (data->last_action, drop_signals[DROP], 0,
227 clutter_actor_meta_get_actor (meta),
232 data->last_action = NULL;
235 if (drag_actor != NULL)
236 clutter_actor_set_reactive (drag_actor, was_reactive);
238 return CLUTTER_EVENT_PROPAGATE;
242 drop_action_register (ClutterDropAction *self)
244 ClutterDropActionPrivate *priv = self->priv;
247 g_assert (priv->stage != NULL);
249 data = g_object_get_data (G_OBJECT (priv->stage), "__clutter_drop_targets");
252 data = g_new0 (DropTarget, 1);
254 data->stage = priv->stage;
255 data->actions = g_hash_table_new (NULL, NULL);
256 data->capture_id = g_signal_connect (priv->stage, "captured-event",
257 G_CALLBACK (on_stage_capture),
259 g_object_set_data_full (G_OBJECT (priv->stage), "__clutter_drop_targets",
264 g_hash_table_replace (data->actions, priv->actor, self);
268 drop_action_unregister (ClutterDropAction *self)
270 ClutterDropActionPrivate *priv = self->priv;
273 data = g_object_get_data (G_OBJECT (priv->stage), "__clutter_drop_targets");
277 g_hash_table_remove (data->actions, priv->actor);
278 if (g_hash_table_size (data->actions) == 0)
279 g_object_set_data (G_OBJECT (data->stage), "__clutter_drop_targets", NULL);
283 on_actor_mapped (ClutterActor *actor,
285 ClutterDropAction *self)
287 if (CLUTTER_ACTOR_IS_MAPPED (actor))
289 if (self->priv->stage == NULL)
290 self->priv->stage = clutter_actor_get_stage (actor);
292 drop_action_register (self);
295 drop_action_unregister (self);
299 clutter_drop_action_set_actor (ClutterActorMeta *meta,
302 ClutterDropActionPrivate *priv = CLUTTER_DROP_ACTION (meta)->priv;
304 if (priv->actor != NULL)
306 drop_action_unregister (CLUTTER_DROP_ACTION (meta));
308 if (priv->mapped_id != 0)
309 g_signal_handler_disconnect (priv->actor, priv->mapped_id);
318 if (priv->actor != NULL)
320 priv->stage = clutter_actor_get_stage (actor);
321 priv->mapped_id = g_signal_connect (actor, "notify::mapped",
322 G_CALLBACK (on_actor_mapped),
325 if (priv->stage != NULL)
326 drop_action_register (CLUTTER_DROP_ACTION (meta));
329 CLUTTER_ACTOR_META_CLASS (clutter_drop_action_parent_class)->set_actor (meta, actor);
333 signal_accumulator (GSignalInvocationHint *ihint,
335 const GValue *handler_return,
338 gboolean continue_emission;
340 continue_emission = g_value_get_boolean (handler_return);
341 g_value_set_boolean (return_accu, continue_emission);
343 return continue_emission;
347 clutter_drop_action_real_can_drop (ClutterDropAction *action,
356 clutter_drop_action_class_init (ClutterDropActionClass *klass)
358 ClutterActorMetaClass *meta_class = CLUTTER_ACTOR_META_CLASS (klass);
360 g_type_class_add_private (klass, sizeof (ClutterDropActionPrivate));
362 meta_class->set_actor = clutter_drop_action_set_actor;
364 klass->can_drop = clutter_drop_action_real_can_drop;
367 * ClutterDropAction::can-drop:
368 * @action: the #ClutterDropAction that emitted the signal
369 * @actor: the #ClutterActor attached to the @action
370 * @event_x: the X coordinate (in stage space) of the drop event
371 * @event_y: the Y coordinate (in stage space) of the drop event
373 * The ::can-drop signal is emitted when the dragged actor is dropped
374 * on @actor. The return value of the ::can-drop signal will determine
375 * whether or not the #ClutterDropAction::drop signal is going to be
376 * emitted on @action.
378 * The default implementation of #ClutterDropAction returns %TRUE for
381 * Return value: %TRUE if the drop is accepted, and %FALSE otherwise
385 drop_signals[CAN_DROP] =
386 g_signal_new (I_("can-drop"),
387 G_TYPE_FROM_CLASS (klass),
389 G_STRUCT_OFFSET (ClutterDropActionClass, can_drop),
390 signal_accumulator, NULL,
391 _clutter_marshal_BOOLEAN__OBJECT_FLOAT_FLOAT,
398 * ClutterDropAction::over-in:
399 * @action: the #ClutterDropAction that emitted the signal
400 * @actor: the #ClutterActor attached to the @action
402 * The ::over-in signal is emitted when the dragged actor crosses
407 drop_signals[OVER_IN] =
408 g_signal_new (I_("over-in"),
409 G_TYPE_FROM_CLASS (klass),
411 G_STRUCT_OFFSET (ClutterDropActionClass, over_in),
413 _clutter_marshal_VOID__OBJECT,
418 * ClutterDropAction::over-out:
419 * @action: the #ClutterDropAction that emitted the signal
420 * @actor: the #ClutterActor attached to the @action
422 * The ::over-out signal is emitted when the dragged actor crosses
427 drop_signals[OVER_OUT] =
428 g_signal_new (I_("over-out"),
429 G_TYPE_FROM_CLASS (klass),
431 G_STRUCT_OFFSET (ClutterDropActionClass, over_out),
433 _clutter_marshal_VOID__OBJECT,
438 * ClutterDropAction::drop:
439 * @action: the #ClutterDropAction that emitted the signal
440 * @actor: the #ClutterActor attached to the @action
441 * @event_x: the X coordinate (in stage space) of the drop event
442 * @event_y: the Y coordinate (in stage space) of the drop event
444 * The ::drop signal is emitted when the dragged actor is dropped
445 * on @actor. This signal is only emitted if at least an handler of
446 * #ClutterDropAction::can-drop returns %TRUE.
451 g_signal_new (I_("drop"),
452 G_TYPE_FROM_CLASS (klass),
454 G_STRUCT_OFFSET (ClutterDropActionClass, drop),
456 _clutter_marshal_VOID__OBJECT_FLOAT_FLOAT,
464 clutter_drop_action_init (ClutterDropAction *self)
466 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, CLUTTER_TYPE_DROP_ACTION,
467 ClutterDropActionPrivate);
471 * clutter_drop_action_new:
473 * Creates a new #ClutterDropAction.
475 * Use clutter_actor_add_action() to add the action to a #ClutterActor.
477 * Return value: the newly created #ClutterDropAction
482 clutter_drop_action_new (void)
484 return g_object_new (CLUTTER_TYPE_DROP_ACTION, NULL);