From 93f3412f70fb3cd00c7df04975d5b3ae875a8bb3 Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Mon, 16 Nov 2009 13:28:51 -0500 Subject: [PATCH] Import ShellButtonBox as StClickable Now a StBin, and add hover/active style properties. Also, add the event to the CLICKED signal. Otherwise a straightforward namespace transformation. https://bugzilla.gnome.org/show_bug.cgi?id=602131 --- src/Makefile-st.am | 2 + src/st/st-clickable.c | 330 ++++++++++++++++++++++++++++++++++++++++++ src/st/st-clickable.h | 35 +++++ 3 files changed, 367 insertions(+) create mode 100644 src/st/st-clickable.c create mode 100644 src/st/st-clickable.h diff --git a/src/Makefile-st.am b/src/Makefile-st.am index 618e334f5..a6a85f12e 100644 --- a/src/Makefile-st.am +++ b/src/Makefile-st.am @@ -72,6 +72,7 @@ st_source_h = \ st/st-box-layout.h \ st/st-box-layout-child.h \ st/st-button.h \ + st/st-clickable.h \ st/st-clipboard.h \ st/st-drawing-area.h \ st/st-entry.h \ @@ -109,6 +110,7 @@ st_source_c = \ st/st-box-layout.c \ st/st-box-layout-child.c \ st/st-button.c \ + st/st-clickable.c \ st/st-clipboard.c \ st/st-drawing-area.c \ st/st-entry.c \ diff --git a/src/st/st-clickable.c b/src/st/st-clickable.c new file mode 100644 index 000000000..bc5ad4994 --- /dev/null +++ b/src/st/st-clickable.c @@ -0,0 +1,330 @@ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */ + +/** + * SECTION:st-clickable + * @short_description: A bin with methods and properties useful for implementing buttons + * + * A #StBin subclass which translates lower-level Clutter button events + * into higher level properties which are useful for implementing "button-like" + * actors. + */ + +#include "st-clickable.h" + +G_DEFINE_TYPE (StClickable, st_clickable, ST_TYPE_BIN); + +struct _StClickablePrivate { + gboolean active; + gboolean held; + gboolean hover; + gboolean pressed; + + guint initiating_button; +}; + +/* Signals */ +enum +{ + CLICKED, + LAST_SIGNAL +}; + +enum { + PROP_0, + + PROP_ACTIVE, + PROP_HOVER, + PROP_PRESSED, +}; + +static guint st_clickable_signals [LAST_SIGNAL] = { 0 }; + +static void +sync_pseudo_class (StClickable *self) +{ + st_widget_set_style_pseudo_class (ST_WIDGET (self), + (self->priv->pressed || self->priv->active) ? "pressed" : + (self->priv->hover ? "hover" : NULL)); +} + +static void +set_active (StClickable *self, + gboolean active) +{ + if (self->priv->active == active) + return; + self->priv->active = active; + sync_pseudo_class (self); + g_object_notify (G_OBJECT (self), "active"); +} + +static void +set_hover (StClickable *self, + gboolean hover) +{ + if (self->priv->hover == hover) + return; + self->priv->hover = hover; + sync_pseudo_class (self); + g_object_notify (G_OBJECT (self), "hover"); +} + +static void +set_pressed (StClickable *self, + gboolean pressed) +{ + if (self->priv->pressed == pressed) + return; + self->priv->pressed = pressed; + sync_pseudo_class (self); + g_object_notify (G_OBJECT (self), "pressed"); +} + +static gboolean +st_clickable_contains (StClickable *self, + ClutterActor *actor) +{ + while (actor != NULL && actor != (ClutterActor*)self) + { + actor = clutter_actor_get_parent (actor); + } + return actor != NULL; +} + +static gboolean +st_clickable_enter_event (ClutterActor *actor, + ClutterCrossingEvent *event) +{ + StClickable *self = ST_CLICKABLE (actor); + + if (st_clickable_contains (self, event->related)) + return TRUE; + if (!st_clickable_contains (self, event->source)) + return TRUE; + + g_object_freeze_notify (G_OBJECT (actor)); + + if (self->priv->held) + set_pressed (self, TRUE); + set_hover (self, TRUE); + + g_object_thaw_notify (G_OBJECT (actor)); + + return TRUE; +} + +static gboolean +st_clickable_leave_event (ClutterActor *actor, + ClutterCrossingEvent *event) +{ + StClickable *self = ST_CLICKABLE (actor); + + if (st_clickable_contains (self, event->related)) + return TRUE; + + set_hover (self, FALSE); + set_pressed (self, FALSE); + + return TRUE; +} + +static gboolean +st_clickable_button_press_event (ClutterActor *actor, + ClutterButtonEvent *event) +{ + StClickable *self = ST_CLICKABLE (actor); + + if (event->click_count != 1) + return FALSE; + + if (self->priv->held) + return TRUE; + + if (!st_clickable_contains (self, event->source)) + return FALSE; + + self->priv->held = TRUE; + self->priv->initiating_button = event->button; + clutter_grab_pointer (CLUTTER_ACTOR (self)); + + set_pressed (self, TRUE); + + return TRUE; +} + +static gboolean +st_clickable_button_release_event (ClutterActor *actor, + ClutterButtonEvent *event) +{ + StClickable *self = ST_CLICKABLE (actor); + + if (event->button != self->priv->initiating_button || event->click_count != 1) + return FALSE; + + if (!self->priv->held) + return TRUE; + + self->priv->held = FALSE; + clutter_ungrab_pointer (); + + if (!st_clickable_contains (self, event->source)) + return FALSE; + + set_pressed (self, FALSE); + + g_signal_emit (G_OBJECT (self), st_clickable_signals[CLICKED], 0, event); + + return TRUE; +} + +/** + * st_clickable_fake_release: + * @box: + * + * If this widget is holding a pointer grab, this function will + * will ungrab it, and reset the pressed state. The effect is + * similar to if the user had released the mouse button, but without + * emitting the clicked signal. + * + * This function is useful if for example you want to do something after the user + * is holding the mouse button for a given period of time, breaking the + * grab. + */ +void +st_clickable_fake_release (StClickable *self) +{ + if (!self->priv->held) + return; + + self->priv->held = FALSE; + clutter_ungrab_pointer (); + + set_pressed (self, FALSE); +} + +static void +st_clickable_set_property (GObject *object, + guint prop_id, + const GValue *value, + GParamSpec *pspec) +{ + StClickable *self = ST_CLICKABLE (object); + + switch (prop_id) + { + case PROP_ACTIVE: + set_active (self, g_value_get_boolean (value)); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +st_clickable_get_property (GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec) +{ + StClickable *self = ST_CLICKABLE (object); + + switch (prop_id) + { + case PROP_ACTIVE: + g_value_set_boolean (value, self->priv->active); + break; + case PROP_PRESSED: + g_value_set_boolean (value, self->priv->pressed); + break; + case PROP_HOVER: + g_value_set_boolean (value, self->priv->hover); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +st_clickable_class_init (StClickableClass *klass) +{ + GObjectClass *gobject_class = G_OBJECT_CLASS (klass); + ClutterActorClass *actor_class = CLUTTER_ACTOR_CLASS (klass); + + gobject_class->get_property = st_clickable_get_property; + gobject_class->set_property = st_clickable_set_property; + + actor_class->enter_event = st_clickable_enter_event; + actor_class->leave_event = st_clickable_leave_event; + actor_class->button_press_event = st_clickable_button_press_event; + actor_class->button_release_event = st_clickable_button_release_event; + + /** + * StClickable::clicked + * @box: The #StClickable + * + * This signal is emitted when the button should take the action + * associated with button click+release. + */ + st_clickable_signals[CLICKED] = + g_signal_new ("clicked", + G_TYPE_FROM_CLASS (klass), + G_SIGNAL_RUN_LAST, + 0, + NULL, NULL, + g_cclosure_marshal_VOID__BOXED, + G_TYPE_NONE, 1, CLUTTER_TYPE_EVENT); + + /** + * StClickable:active + * + * The property allows the button to be used as a "toggle button"; it's up to the + * application to update the active property in response to the activate signal; + * it doesn't happen automatically. + */ + g_object_class_install_property (gobject_class, + PROP_ACTIVE, + g_param_spec_boolean ("active", + "Active", + "Whether the button persistently active", + FALSE, + G_PARAM_READWRITE)); + + /** + * StClickable:hover + * + * This property tracks whether the mouse is over the button; note this + * state is independent of whether the button is pressed. + */ + g_object_class_install_property (gobject_class, + PROP_HOVER, + g_param_spec_boolean ("hover", + "Hovering state", + "Whether the mouse is over the button", + FALSE, + G_PARAM_READABLE)); + + /** + * StClickable:pressed + * + * This property tracks whether the button should have a "pressed in" + * effect. + */ + g_object_class_install_property (gobject_class, + PROP_PRESSED, + g_param_spec_boolean ("pressed", + "Pressed state", + "Whether the button is currently pressed", + FALSE, + G_PARAM_READABLE)); + + g_type_class_add_private (gobject_class, sizeof (StClickablePrivate)); +} + +static void +st_clickable_init (StClickable *self) +{ + self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, ST_TYPE_CLICKABLE, + StClickablePrivate); +} diff --git a/src/st/st-clickable.h b/src/st/st-clickable.h new file mode 100644 index 000000000..1c59447d7 --- /dev/null +++ b/src/st/st-clickable.h @@ -0,0 +1,35 @@ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */ +#ifndef __ST_CLICKABLE_H__ +#define __ST_CLICKABLE_H__ + +#include "st-bin.h" + +#define ST_TYPE_CLICKABLE (st_clickable_get_type ()) +#define ST_CLICKABLE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), ST_TYPE_CLICKABLE, StClickable)) +#define ST_CLICKABLE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), ST_TYPE_CLICKABLE, StClickableClass)) +#define ST_IS_CLICKABLE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), ST_TYPE_CLICKABLE)) +#define ST_IS_CLICKABLE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), ST_TYPE_CLICKABLE)) +#define ST_CLICKABLE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), ST_TYPE_CLICKABLE, StClickableClass)) + +typedef struct _StClickable StClickable; +typedef struct _StClickableClass StClickableClass; + +typedef struct _StClickablePrivate StClickablePrivate; + +struct _StClickable +{ + StBin parent; + + StClickablePrivate *priv; +}; + +struct _StClickableClass +{ + StBinClass parent_class; +}; + +GType st_clickable_get_type (void) G_GNUC_CONST; + +void st_clickable_fake_release (StClickable *box); + +#endif /* __ST_CLICKABLE_H__ */