2009-09-04 01:05:16 +00:00
|
|
|
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SECTION:shell-menu
|
|
|
|
* @short_description: A box which acts like a popup menu
|
|
|
|
*
|
2010-03-31 18:57:07 +00:00
|
|
|
* A #StBoxLayout subclass which adds methods and signals useful for
|
|
|
|
* implementing popup-menu like actors.
|
2009-09-04 01:05:16 +00:00
|
|
|
*/
|
|
|
|
|
2009-11-24 14:07:40 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
2009-09-04 01:05:16 +00:00
|
|
|
#include "shell-menu.h"
|
|
|
|
|
2010-03-31 18:57:07 +00:00
|
|
|
G_DEFINE_TYPE (ShellMenu, shell_menu, ST_TYPE_BOX_LAYOUT);
|
2009-09-04 01:05:16 +00:00
|
|
|
|
|
|
|
struct _ShellMenuPrivate {
|
2009-09-11 21:16:49 +00:00
|
|
|
gboolean popped_up;
|
2009-09-04 01:05:16 +00:00
|
|
|
gboolean have_grab;
|
2009-09-25 20:44:28 +00:00
|
|
|
guint activating_button;
|
2009-09-04 01:05:16 +00:00
|
|
|
|
2009-09-10 01:14:31 +00:00
|
|
|
gboolean released_on_source;
|
|
|
|
ClutterActor *source_actor;
|
|
|
|
|
2009-09-04 01:05:16 +00:00
|
|
|
ClutterActor *selected;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Signals */
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
UNSELECTED,
|
|
|
|
SELECTED,
|
|
|
|
ACTIVATE,
|
2009-09-11 21:16:49 +00:00
|
|
|
CANCELLED,
|
2009-09-04 01:05:16 +00:00
|
|
|
LAST_SIGNAL
|
|
|
|
};
|
|
|
|
|
|
|
|
static guint shell_menu_signals [LAST_SIGNAL] = { 0 };
|
|
|
|
|
|
|
|
static gboolean
|
2009-09-10 01:14:31 +00:00
|
|
|
container_contains (ClutterContainer *container,
|
|
|
|
ClutterActor *actor)
|
2009-09-04 01:05:16 +00:00
|
|
|
{
|
2010-03-31 18:57:07 +00:00
|
|
|
ClutterActor *container_actor = CLUTTER_ACTOR (container);
|
|
|
|
|
|
|
|
while (actor != NULL && actor != container_actor)
|
2009-09-04 01:05:16 +00:00
|
|
|
{
|
|
|
|
actor = clutter_actor_get_parent (actor);
|
|
|
|
}
|
|
|
|
return actor != NULL;
|
|
|
|
}
|
|
|
|
|
2009-09-11 21:16:49 +00:00
|
|
|
static void
|
2010-03-31 18:57:07 +00:00
|
|
|
shell_menu_popdown_nosignal (ShellMenu *menu)
|
2009-09-11 21:16:49 +00:00
|
|
|
{
|
2010-03-31 18:57:07 +00:00
|
|
|
menu->priv->popped_up = FALSE;
|
|
|
|
if (menu->priv->have_grab)
|
2009-09-11 21:16:49 +00:00
|
|
|
clutter_ungrab_pointer ();
|
2010-03-31 18:57:07 +00:00
|
|
|
clutter_actor_hide (CLUTTER_ACTOR (menu));
|
2009-09-11 21:16:49 +00:00
|
|
|
}
|
|
|
|
|
2009-09-04 01:05:16 +00:00
|
|
|
static void
|
|
|
|
on_selected_destroy (ClutterActor *actor,
|
2010-03-31 18:57:07 +00:00
|
|
|
ShellMenu *menu)
|
2009-09-04 01:05:16 +00:00
|
|
|
{
|
2010-03-31 18:57:07 +00:00
|
|
|
menu->priv->selected = NULL;
|
2009-09-04 01:05:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2010-03-31 18:57:07 +00:00
|
|
|
set_selected (ShellMenu *menu,
|
2009-09-04 01:05:16 +00:00
|
|
|
ClutterActor *actor)
|
|
|
|
{
|
2010-03-31 18:57:07 +00:00
|
|
|
if (actor == menu->priv->selected)
|
2009-09-04 01:05:16 +00:00
|
|
|
return;
|
2010-03-31 18:57:07 +00:00
|
|
|
if (menu->priv->selected)
|
2009-09-04 01:05:16 +00:00
|
|
|
{
|
2010-03-31 18:57:07 +00:00
|
|
|
g_signal_handlers_disconnect_by_func (menu->priv->selected,
|
|
|
|
G_CALLBACK (on_selected_destroy),
|
|
|
|
menu);
|
|
|
|
g_signal_emit (G_OBJECT (menu), shell_menu_signals[UNSELECTED], 0,
|
|
|
|
menu->priv->selected);
|
2009-09-04 01:05:16 +00:00
|
|
|
}
|
2010-03-31 18:57:07 +00:00
|
|
|
menu->priv->selected = actor;
|
|
|
|
if (menu->priv->selected)
|
2009-09-04 01:05:16 +00:00
|
|
|
{
|
2010-03-31 18:57:07 +00:00
|
|
|
g_signal_connect (menu->priv->selected, "destroy",
|
|
|
|
G_CALLBACK (on_selected_destroy), menu);
|
|
|
|
g_signal_emit (G_OBJECT (menu), shell_menu_signals[SELECTED], 0,
|
|
|
|
menu->priv->selected);
|
2009-09-04 01:05:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
shell_menu_enter_event (ClutterActor *actor,
|
|
|
|
ClutterCrossingEvent *event)
|
|
|
|
{
|
2010-03-31 18:57:07 +00:00
|
|
|
ShellMenu *menu = SHELL_MENU (actor);
|
2009-09-04 01:05:16 +00:00
|
|
|
|
2010-03-31 18:57:07 +00:00
|
|
|
if (container_contains (CLUTTER_CONTAINER (menu), event->source) &&
|
|
|
|
event->source != CLUTTER_ACTOR (menu))
|
|
|
|
set_selected (menu, event->source);
|
2009-09-04 01:05:16 +00:00
|
|
|
|
2010-03-31 18:57:07 +00:00
|
|
|
return CLUTTER_ACTOR_CLASS (shell_menu_parent_class)->enter_event (actor, event);
|
2009-09-04 01:05:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
shell_menu_leave_event (ClutterActor *actor,
|
|
|
|
ClutterCrossingEvent *event)
|
|
|
|
{
|
2010-03-31 18:57:07 +00:00
|
|
|
ShellMenu *menu = SHELL_MENU (actor);
|
2009-09-04 01:05:16 +00:00
|
|
|
|
2010-03-31 18:57:07 +00:00
|
|
|
set_selected (menu, NULL);
|
2009-09-04 01:05:16 +00:00
|
|
|
|
2010-03-31 18:57:07 +00:00
|
|
|
return CLUTTER_ACTOR_CLASS (shell_menu_parent_class)->leave_event (actor, event);
|
2009-09-04 01:05:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
shell_menu_button_release_event (ClutterActor *actor,
|
|
|
|
ClutterButtonEvent *event)
|
|
|
|
{
|
2010-03-31 18:57:07 +00:00
|
|
|
ShellMenu *menu = SHELL_MENU (actor);
|
2009-09-04 01:05:16 +00:00
|
|
|
|
2009-09-25 20:44:28 +00:00
|
|
|
/* Until the user releases the button that brought up the menu, we just
|
|
|
|
* ignore other button press/releass.
|
|
|
|
* See https://bugzilla.gnome.org/show_bug.cgi?id=596371
|
|
|
|
*/
|
2010-03-31 18:57:07 +00:00
|
|
|
if (menu->priv->activating_button > 0 &&
|
|
|
|
menu->priv->activating_button != event->button)
|
2009-09-04 01:05:16 +00:00
|
|
|
return FALSE;
|
|
|
|
|
2010-03-31 18:57:07 +00:00
|
|
|
menu->priv->activating_button = 0;
|
2009-09-25 20:44:28 +00:00
|
|
|
|
2010-03-31 18:57:07 +00:00
|
|
|
if (menu->priv->source_actor && !menu->priv->released_on_source)
|
2009-09-10 01:14:31 +00:00
|
|
|
{
|
2010-03-31 18:57:07 +00:00
|
|
|
if (menu->priv->source_actor == event->source ||
|
|
|
|
(CLUTTER_IS_CONTAINER (menu->priv->source_actor) &&
|
|
|
|
container_contains (CLUTTER_CONTAINER (menu->priv->source_actor), event->source)))
|
2009-09-10 01:14:31 +00:00
|
|
|
{
|
|
|
|
/* On the next release, we want to pop down the menu regardless */
|
2010-03-31 18:57:07 +00:00
|
|
|
menu->priv->released_on_source = TRUE;
|
2009-09-10 01:14:31 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-31 18:57:07 +00:00
|
|
|
shell_menu_popdown_nosignal (menu);
|
2009-09-04 01:05:16 +00:00
|
|
|
|
2010-03-31 18:57:07 +00:00
|
|
|
if (!container_contains (CLUTTER_CONTAINER (menu), event->source) ||
|
|
|
|
menu->priv->selected == NULL)
|
2009-09-11 21:16:49 +00:00
|
|
|
{
|
2010-03-31 18:57:07 +00:00
|
|
|
g_signal_emit (G_OBJECT (menu), shell_menu_signals[CANCELLED], 0);
|
2009-09-11 21:16:49 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2009-09-04 01:05:16 +00:00
|
|
|
|
2010-03-31 18:57:07 +00:00
|
|
|
g_signal_emit (G_OBJECT (menu), shell_menu_signals[ACTIVATE], 0,
|
|
|
|
menu->priv->selected);
|
2009-09-04 01:05:16 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-03-31 18:57:07 +00:00
|
|
|
shell_menu_popup (ShellMenu *menu,
|
2009-09-04 01:05:16 +00:00
|
|
|
guint button,
|
|
|
|
guint32 activate_time)
|
|
|
|
{
|
2010-03-31 18:57:07 +00:00
|
|
|
if (menu->priv->popped_up)
|
2009-09-11 21:16:49 +00:00
|
|
|
return;
|
2010-03-31 18:57:07 +00:00
|
|
|
menu->priv->activating_button = button;
|
|
|
|
menu->priv->popped_up = TRUE;
|
|
|
|
menu->priv->have_grab = TRUE;
|
|
|
|
menu->priv->released_on_source = FALSE;
|
|
|
|
clutter_grab_pointer (CLUTTER_ACTOR (menu));
|
2009-09-04 01:05:16 +00:00
|
|
|
}
|
|
|
|
|
2009-09-11 21:16:49 +00:00
|
|
|
/**
|
|
|
|
* shell_menu_popdown:
|
2010-03-31 18:57:07 +00:00
|
|
|
* @menu:
|
2009-09-11 21:16:49 +00:00
|
|
|
*
|
|
|
|
* If the menu is currently active, hide it, emitting the 'cancelled' signal.
|
|
|
|
*/
|
2009-09-04 01:05:16 +00:00
|
|
|
void
|
2010-03-31 18:57:07 +00:00
|
|
|
shell_menu_popdown (ShellMenu *menu)
|
2009-09-04 01:05:16 +00:00
|
|
|
{
|
2010-03-31 18:57:07 +00:00
|
|
|
if (!menu->priv->popped_up)
|
2009-09-11 21:16:49 +00:00
|
|
|
return;
|
2010-03-31 18:57:07 +00:00
|
|
|
shell_menu_popdown_nosignal (menu);
|
|
|
|
g_signal_emit (G_OBJECT (menu), shell_menu_signals[CANCELLED], 0);
|
2009-09-04 01:05:16 +00:00
|
|
|
}
|
|
|
|
|
2009-09-10 01:14:31 +00:00
|
|
|
static void
|
|
|
|
on_source_destroyed (ClutterActor *actor,
|
2010-03-31 18:57:07 +00:00
|
|
|
ShellMenu *menu)
|
2009-09-10 01:14:31 +00:00
|
|
|
{
|
2010-03-31 18:57:07 +00:00
|
|
|
menu->priv->source_actor = NULL;
|
2009-09-10 01:14:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* shell_menu_set_persistent_source:
|
2010-03-31 18:57:07 +00:00
|
|
|
* @menu:
|
2009-09-10 01:14:31 +00:00
|
|
|
* @source: Actor to use as menu origin
|
|
|
|
*
|
|
|
|
* This function changes the menu behavior on button release. Normally
|
|
|
|
* when the mouse is released anywhere, the menu "pops down"; when this
|
|
|
|
* function is called, if the mouse is released over the source actor,
|
|
|
|
* the menu stays.
|
|
|
|
*
|
|
|
|
* The given @source actor must be reactive for this function to work.
|
|
|
|
*/
|
|
|
|
void
|
2010-03-31 18:57:07 +00:00
|
|
|
shell_menu_set_persistent_source (ShellMenu *menu,
|
2009-09-10 01:14:31 +00:00
|
|
|
ClutterActor *source)
|
|
|
|
{
|
2010-03-31 18:57:07 +00:00
|
|
|
if (menu->priv->source_actor)
|
2009-09-10 01:14:31 +00:00
|
|
|
{
|
2010-03-31 18:57:07 +00:00
|
|
|
g_signal_handlers_disconnect_by_func (G_OBJECT (menu->priv->source_actor),
|
2009-09-10 01:14:31 +00:00
|
|
|
G_CALLBACK (on_source_destroyed),
|
2010-03-31 18:57:07 +00:00
|
|
|
menu);
|
2009-09-10 01:14:31 +00:00
|
|
|
}
|
2010-03-31 18:57:07 +00:00
|
|
|
menu->priv->source_actor = source;
|
|
|
|
if (menu->priv->source_actor)
|
2009-09-10 01:14:31 +00:00
|
|
|
{
|
2010-03-31 18:57:07 +00:00
|
|
|
g_signal_connect (G_OBJECT (menu->priv->source_actor),
|
2009-09-10 01:14:31 +00:00
|
|
|
"destroy",
|
|
|
|
G_CALLBACK (on_source_destroyed),
|
2010-03-31 18:57:07 +00:00
|
|
|
menu);
|
2009-09-10 01:14:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
shell_menu_dispose (GObject *gobject)
|
|
|
|
{
|
|
|
|
ShellMenu *self = SHELL_MENU (gobject);
|
|
|
|
|
|
|
|
shell_menu_set_persistent_source (self, NULL);
|
|
|
|
|
|
|
|
G_OBJECT_CLASS (shell_menu_parent_class)->dispose (gobject);
|
|
|
|
}
|
|
|
|
|
2009-09-04 01:05:16 +00:00
|
|
|
static void
|
|
|
|
shell_menu_class_init (ShellMenuClass *klass)
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
|
|
|
|
ClutterActorClass *actor_class = CLUTTER_ACTOR_CLASS (klass);
|
|
|
|
|
2009-09-10 01:14:31 +00:00
|
|
|
gobject_class->dispose = shell_menu_dispose;
|
|
|
|
|
2009-09-04 01:05:16 +00:00
|
|
|
actor_class->enter_event = shell_menu_enter_event;
|
|
|
|
actor_class->leave_event = shell_menu_leave_event;
|
|
|
|
actor_class->button_release_event = shell_menu_button_release_event;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ShellMenu::unselected
|
2010-03-31 18:57:07 +00:00
|
|
|
* @menu: The #ShellMenu
|
2009-09-04 01:05:16 +00:00
|
|
|
* @actor: The previously hovered-over menu item
|
|
|
|
*
|
|
|
|
* This signal is emitted when a menu item transitions to
|
|
|
|
* an unselected state.
|
|
|
|
*/
|
|
|
|
shell_menu_signals[UNSELECTED] =
|
|
|
|
g_signal_new ("unselected",
|
|
|
|
G_TYPE_FROM_CLASS (klass),
|
|
|
|
G_SIGNAL_RUN_LAST,
|
|
|
|
0,
|
|
|
|
NULL, NULL,
|
|
|
|
g_cclosure_marshal_VOID__OBJECT,
|
|
|
|
G_TYPE_NONE, 1, CLUTTER_TYPE_ACTOR);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ShellMenu::selected
|
2010-03-31 18:57:07 +00:00
|
|
|
* @menu: The #ShellMenu
|
2009-09-04 01:05:16 +00:00
|
|
|
* @actor: The hovered-over menu item
|
|
|
|
*
|
|
|
|
* This signal is emitted when a menu item is in a selected state.
|
|
|
|
*/
|
|
|
|
shell_menu_signals[SELECTED] =
|
|
|
|
g_signal_new ("selected",
|
|
|
|
G_TYPE_FROM_CLASS (klass),
|
|
|
|
G_SIGNAL_RUN_LAST,
|
|
|
|
0,
|
|
|
|
NULL, NULL,
|
|
|
|
g_cclosure_marshal_VOID__OBJECT,
|
|
|
|
G_TYPE_NONE, 1, CLUTTER_TYPE_ACTOR);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ShellMenu::activate
|
2010-03-31 18:57:07 +00:00
|
|
|
* @menu: The #ShellMenu
|
2009-09-04 01:05:16 +00:00
|
|
|
* @actor: The clicked menu item
|
|
|
|
*
|
|
|
|
* This signal is emitted when a menu item is selected.
|
|
|
|
*/
|
|
|
|
shell_menu_signals[ACTIVATE] =
|
|
|
|
g_signal_new ("activate",
|
|
|
|
G_TYPE_FROM_CLASS (klass),
|
|
|
|
G_SIGNAL_RUN_LAST,
|
|
|
|
0,
|
|
|
|
NULL, NULL,
|
|
|
|
g_cclosure_marshal_VOID__OBJECT,
|
|
|
|
G_TYPE_NONE, 1, CLUTTER_TYPE_ACTOR);
|
|
|
|
|
|
|
|
/**
|
2009-09-11 21:16:49 +00:00
|
|
|
* ShellMenu::cancelled
|
2010-03-31 18:57:07 +00:00
|
|
|
* @menu: The #ShellMenu
|
2009-09-04 01:05:16 +00:00
|
|
|
*
|
2009-09-11 21:16:49 +00:00
|
|
|
* This signal is emitted when the menu is closed without an option selected.
|
2009-09-04 01:05:16 +00:00
|
|
|
*/
|
2009-09-11 21:16:49 +00:00
|
|
|
shell_menu_signals[CANCELLED] =
|
|
|
|
g_signal_new ("cancelled",
|
2009-09-04 01:05:16 +00:00
|
|
|
G_TYPE_FROM_CLASS (klass),
|
|
|
|
G_SIGNAL_RUN_LAST,
|
|
|
|
0,
|
|
|
|
NULL, NULL,
|
|
|
|
g_cclosure_marshal_VOID__OBJECT,
|
|
|
|
G_TYPE_NONE, 0);
|
|
|
|
|
|
|
|
g_type_class_add_private (gobject_class, sizeof (ShellMenuPrivate));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
shell_menu_init (ShellMenu *self)
|
|
|
|
{
|
|
|
|
self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, SHELL_TYPE_MENU,
|
|
|
|
ShellMenuPrivate);
|
|
|
|
}
|