mirror of
https://github.com/brl/mutter.git
synced 2024-11-13 01:36:10 -05:00
359 lines
9.7 KiB
C
359 lines
9.7 KiB
C
|
#ifdef HAVE_CONFIG_H
|
||
|
#include "config.h"
|
||
|
#endif
|
||
|
|
||
|
#include "clutter-drop-action.h"
|
||
|
|
||
|
#include "clutter-actor-meta-private.h"
|
||
|
#include "clutter-actor-private.h"
|
||
|
#include "clutter-drag-action.h"
|
||
|
#include "clutter-main.h"
|
||
|
#include "clutter-marshal.h"
|
||
|
#include "clutter-stage-private.h"
|
||
|
|
||
|
struct _ClutterDropActionPrivate
|
||
|
{
|
||
|
ClutterActor *actor;
|
||
|
ClutterActor *stage;
|
||
|
|
||
|
gulong mapped_id;
|
||
|
|
||
|
guint is_inside : 1;
|
||
|
};
|
||
|
|
||
|
typedef struct _DropTarget {
|
||
|
ClutterActor *stage;
|
||
|
|
||
|
gulong capture_id;
|
||
|
|
||
|
GHashTable *actions;
|
||
|
|
||
|
ClutterDropAction *last_action;
|
||
|
} DropTarget;
|
||
|
|
||
|
enum
|
||
|
{
|
||
|
CAN_DROP,
|
||
|
OVER_IN,
|
||
|
OVER_OUT,
|
||
|
DROP,
|
||
|
|
||
|
LAST_SIGNAL
|
||
|
};
|
||
|
|
||
|
static guint drop_signals[LAST_SIGNAL] = { 0, };
|
||
|
|
||
|
G_DEFINE_TYPE (ClutterDropAction, clutter_drop_action, CLUTTER_TYPE_ACTION)
|
||
|
|
||
|
static void
|
||
|
drop_target_free (gpointer _data)
|
||
|
{
|
||
|
DropTarget *data = _data;
|
||
|
|
||
|
g_signal_handler_disconnect (data->stage, data->capture_id);
|
||
|
g_hash_table_destroy (data->actions);
|
||
|
g_free (data);
|
||
|
}
|
||
|
|
||
|
static gboolean
|
||
|
on_stage_capture (ClutterStage *stage,
|
||
|
ClutterEvent *event,
|
||
|
gpointer user_data)
|
||
|
{
|
||
|
DropTarget *data = user_data;
|
||
|
gfloat event_x, event_y;
|
||
|
ClutterInputDevice *device;
|
||
|
ClutterActor *actor, *drag_actor;
|
||
|
ClutterDropAction *drop_action;
|
||
|
gboolean was_reactive;
|
||
|
|
||
|
if (!(clutter_event_type (event) == CLUTTER_MOTION ||
|
||
|
clutter_event_type (event) == CLUTTER_BUTTON_RELEASE))
|
||
|
return FALSE;
|
||
|
|
||
|
if (!(clutter_event_get_state (event) & CLUTTER_BUTTON1_MASK))
|
||
|
return FALSE;
|
||
|
|
||
|
clutter_event_get_coords (event, &event_x, &event_y);
|
||
|
device = clutter_event_get_device (event);
|
||
|
|
||
|
drag_actor = _clutter_stage_get_drag_actor (stage, device);
|
||
|
if (drag_actor == NULL)
|
||
|
return FALSE;
|
||
|
|
||
|
/* get the actor under the cursor, excluding the dragged actor; we
|
||
|
* use reactivity because it won't cause any scene invalidation
|
||
|
*/
|
||
|
was_reactive = clutter_actor_get_reactive (drag_actor);
|
||
|
clutter_actor_set_reactive (drag_actor, FALSE);
|
||
|
|
||
|
actor = clutter_stage_get_actor_at_pos (stage, CLUTTER_PICK_REACTIVE,
|
||
|
event_x,
|
||
|
event_y);
|
||
|
if (actor == NULL || actor == CLUTTER_ACTOR (stage))
|
||
|
{
|
||
|
if (data->last_action != NULL)
|
||
|
{
|
||
|
ClutterActorMeta *meta = CLUTTER_ACTOR_META (data->last_action);
|
||
|
|
||
|
g_signal_emit (data->last_action, drop_signals[OVER_OUT], 0,
|
||
|
clutter_actor_meta_get_actor (meta));
|
||
|
|
||
|
data->last_action = NULL;
|
||
|
}
|
||
|
|
||
|
goto out;
|
||
|
}
|
||
|
|
||
|
drop_action = g_hash_table_lookup (data->actions, actor);
|
||
|
|
||
|
if (drop_action == NULL)
|
||
|
{
|
||
|
if (data->last_action != NULL)
|
||
|
{
|
||
|
ClutterActorMeta *meta = CLUTTER_ACTOR_META (data->last_action);
|
||
|
|
||
|
g_signal_emit (data->last_action, drop_signals[OVER_OUT], 0,
|
||
|
clutter_actor_meta_get_actor (meta));
|
||
|
|
||
|
data->last_action = NULL;
|
||
|
}
|
||
|
|
||
|
goto out;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
if (data->last_action != drop_action)
|
||
|
{
|
||
|
ClutterActorMeta *meta;
|
||
|
|
||
|
if (data->last_action != NULL)
|
||
|
{
|
||
|
meta = CLUTTER_ACTOR_META (data->last_action);
|
||
|
|
||
|
g_signal_emit (data->last_action, drop_signals[OVER_OUT], 0,
|
||
|
clutter_actor_meta_get_actor (meta));
|
||
|
}
|
||
|
|
||
|
meta = CLUTTER_ACTOR_META (drop_action);
|
||
|
|
||
|
g_signal_emit (drop_action, drop_signals[OVER_IN], 0,
|
||
|
clutter_actor_meta_get_actor (meta));
|
||
|
}
|
||
|
|
||
|
data->last_action = drop_action;
|
||
|
}
|
||
|
|
||
|
out:
|
||
|
if (clutter_event_type (event) == CLUTTER_BUTTON_RELEASE)
|
||
|
{
|
||
|
if (data->last_action != NULL)
|
||
|
{
|
||
|
ClutterActorMeta *meta = CLUTTER_ACTOR_META (data->last_action);
|
||
|
gboolean can_drop = FALSE;
|
||
|
|
||
|
g_signal_emit (data->last_action, drop_signals[CAN_DROP], 0,
|
||
|
clutter_actor_meta_get_actor (meta),
|
||
|
event_x, event_y,
|
||
|
&can_drop);
|
||
|
|
||
|
if (can_drop)
|
||
|
{
|
||
|
g_signal_emit (data->last_action, drop_signals[DROP], 0,
|
||
|
clutter_actor_meta_get_actor (meta),
|
||
|
event_x, event_y);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
data->last_action = NULL;
|
||
|
}
|
||
|
|
||
|
if (drag_actor != NULL)
|
||
|
clutter_actor_set_reactive (drag_actor, was_reactive);
|
||
|
|
||
|
return FALSE;
|
||
|
}
|
||
|
|
||
|
static void
|
||
|
drop_action_register (ClutterDropAction *self)
|
||
|
{
|
||
|
ClutterDropActionPrivate *priv = self->priv;
|
||
|
DropTarget *data;
|
||
|
|
||
|
g_assert (priv->stage != NULL);
|
||
|
|
||
|
data = g_object_get_data (G_OBJECT (priv->stage), "__clutter_drop_targets");
|
||
|
if (data == NULL)
|
||
|
{
|
||
|
data = g_new0 (DropTarget, 1);
|
||
|
|
||
|
data->stage = priv->stage;
|
||
|
data->actions = g_hash_table_new (NULL, NULL);
|
||
|
data->capture_id = g_signal_connect (priv->stage, "captured-event",
|
||
|
G_CALLBACK (on_stage_capture),
|
||
|
data);
|
||
|
g_object_set_data_full (G_OBJECT (priv->stage), "__clutter_drop_targets",
|
||
|
data,
|
||
|
drop_target_free);
|
||
|
}
|
||
|
|
||
|
g_hash_table_replace (data->actions, priv->actor, self);
|
||
|
}
|
||
|
|
||
|
static void
|
||
|
drop_action_unregister (ClutterDropAction *self)
|
||
|
{
|
||
|
ClutterDropActionPrivate *priv = self->priv;
|
||
|
DropTarget *data;
|
||
|
|
||
|
data = g_object_get_data (G_OBJECT (priv->stage), "__clutter_drop_targets");
|
||
|
if (data == NULL)
|
||
|
return;
|
||
|
|
||
|
g_hash_table_remove (data->actions, priv->actor);
|
||
|
if (g_hash_table_size (data->actions) == 0)
|
||
|
g_object_set_data (G_OBJECT (data->stage), "__clutter_drop_targets", NULL);
|
||
|
}
|
||
|
|
||
|
static void
|
||
|
on_actor_mapped (ClutterActor *actor,
|
||
|
GParamSpec *pspec,
|
||
|
ClutterDropAction *self)
|
||
|
{
|
||
|
if (CLUTTER_ACTOR_IS_MAPPED (actor))
|
||
|
{
|
||
|
if (self->priv->stage == NULL)
|
||
|
self->priv->stage = clutter_actor_get_stage (actor);
|
||
|
|
||
|
drop_action_register (self);
|
||
|
}
|
||
|
else
|
||
|
drop_action_unregister (self);
|
||
|
}
|
||
|
|
||
|
static void
|
||
|
clutter_drop_action_set_actor (ClutterActorMeta *meta,
|
||
|
ClutterActor *actor)
|
||
|
{
|
||
|
ClutterDropActionPrivate *priv = CLUTTER_DROP_ACTION (meta)->priv;
|
||
|
|
||
|
if (priv->actor != NULL)
|
||
|
{
|
||
|
drop_action_unregister (CLUTTER_DROP_ACTION (meta));
|
||
|
|
||
|
if (priv->mapped_id != 0)
|
||
|
g_signal_handler_disconnect (priv->actor, priv->mapped_id);
|
||
|
|
||
|
priv->stage = NULL;
|
||
|
priv->actor = NULL;
|
||
|
priv->mapped_id = 0;
|
||
|
}
|
||
|
|
||
|
priv->actor = actor;
|
||
|
|
||
|
if (priv->actor != NULL)
|
||
|
{
|
||
|
priv->stage = clutter_actor_get_stage (actor);
|
||
|
priv->mapped_id = g_signal_connect (actor, "notify::mapped",
|
||
|
G_CALLBACK (on_actor_mapped),
|
||
|
meta);
|
||
|
|
||
|
if (priv->stage != NULL)
|
||
|
drop_action_register (CLUTTER_DROP_ACTION (meta));
|
||
|
}
|
||
|
|
||
|
CLUTTER_ACTOR_META_CLASS (clutter_drop_action_parent_class)->set_actor (meta, actor);
|
||
|
}
|
||
|
|
||
|
static gboolean
|
||
|
signal_accumulator (GSignalInvocationHint *ihint,
|
||
|
GValue *return_accu,
|
||
|
const GValue *handler_return,
|
||
|
gpointer user_data)
|
||
|
{
|
||
|
gboolean continue_emission;
|
||
|
|
||
|
continue_emission = g_value_get_boolean (handler_return);
|
||
|
g_value_set_boolean (return_accu, continue_emission);
|
||
|
|
||
|
return continue_emission;
|
||
|
}
|
||
|
|
||
|
static gboolean
|
||
|
clutter_drop_action_real_can_drop (ClutterDropAction *action,
|
||
|
ClutterActor *actor,
|
||
|
gfloat event_x,
|
||
|
gfloat event_y)
|
||
|
{
|
||
|
return TRUE;
|
||
|
}
|
||
|
|
||
|
static void
|
||
|
clutter_drop_action_class_init (ClutterDropActionClass *klass)
|
||
|
{
|
||
|
ClutterActorMetaClass *meta_class = CLUTTER_ACTOR_META_CLASS (klass);
|
||
|
|
||
|
g_type_class_add_private (klass, sizeof (ClutterDropActionPrivate));
|
||
|
|
||
|
meta_class->set_actor = clutter_drop_action_set_actor;
|
||
|
|
||
|
klass->can_drop = clutter_drop_action_real_can_drop;
|
||
|
|
||
|
drop_signals[CAN_DROP] =
|
||
|
g_signal_new (I_("can-drop"),
|
||
|
G_TYPE_FROM_CLASS (klass),
|
||
|
G_SIGNAL_RUN_LAST,
|
||
|
G_STRUCT_OFFSET (ClutterDropActionClass, can_drop),
|
||
|
signal_accumulator, NULL,
|
||
|
_clutter_marshal_BOOLEAN__OBJECT_FLOAT_FLOAT,
|
||
|
G_TYPE_BOOLEAN, 3,
|
||
|
CLUTTER_TYPE_ACTOR,
|
||
|
G_TYPE_FLOAT,
|
||
|
G_TYPE_FLOAT);
|
||
|
|
||
|
drop_signals[OVER_IN] =
|
||
|
g_signal_new (I_("over-in"),
|
||
|
G_TYPE_FROM_CLASS (klass),
|
||
|
G_SIGNAL_RUN_LAST,
|
||
|
G_STRUCT_OFFSET (ClutterDropActionClass, over_in),
|
||
|
NULL, NULL,
|
||
|
_clutter_marshal_VOID__OBJECT,
|
||
|
G_TYPE_NONE, 1,
|
||
|
CLUTTER_TYPE_ACTOR);
|
||
|
|
||
|
drop_signals[OVER_OUT] =
|
||
|
g_signal_new (I_("over-out"),
|
||
|
G_TYPE_FROM_CLASS (klass),
|
||
|
G_SIGNAL_RUN_LAST,
|
||
|
G_STRUCT_OFFSET (ClutterDropActionClass, over_out),
|
||
|
NULL, NULL,
|
||
|
_clutter_marshal_VOID__OBJECT,
|
||
|
G_TYPE_NONE, 1,
|
||
|
CLUTTER_TYPE_ACTOR);
|
||
|
|
||
|
drop_signals[DROP] =
|
||
|
g_signal_new (I_("drop"),
|
||
|
G_TYPE_FROM_CLASS (klass),
|
||
|
G_SIGNAL_RUN_LAST,
|
||
|
G_STRUCT_OFFSET (ClutterDropActionClass, drop),
|
||
|
NULL, NULL,
|
||
|
_clutter_marshal_VOID__OBJECT_FLOAT_FLOAT,
|
||
|
G_TYPE_NONE, 3,
|
||
|
CLUTTER_TYPE_ACTOR,
|
||
|
G_TYPE_FLOAT,
|
||
|
G_TYPE_FLOAT);
|
||
|
}
|
||
|
|
||
|
static void
|
||
|
clutter_drop_action_init (ClutterDropAction *self)
|
||
|
{
|
||
|
self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, CLUTTER_TYPE_DROP_ACTION,
|
||
|
ClutterDropActionPrivate);
|
||
|
}
|
||
|
|
||
|
ClutterAction *
|
||
|
clutter_drop_action_new (void)
|
||
|
{
|
||
|
return g_object_new (CLUTTER_TYPE_DROP_ACTION, NULL);
|
||
|
}
|