2009-09-29 19:08:01 +00:00
|
|
|
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
|
2009-09-08 19:47:30 +00:00
|
|
|
/*
|
|
|
|
* st-bin.c: Basic container actor
|
|
|
|
*
|
2010-11-10 22:00:45 +00:00
|
|
|
* Copyright 2009 Intel Corporation.
|
|
|
|
* Copyright 2009, 2010 Red Hat, Inc.
|
2009-09-08 19:47:30 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms and conditions of the GNU Lesser General Public License,
|
|
|
|
* version 2.1, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope 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
|
2010-11-10 22:00:45 +00:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2009-09-08 19:47:30 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SECTION:st-bin
|
|
|
|
* @short_description: a simple container with one actor
|
|
|
|
*
|
|
|
|
* #StBin is a simple container capable of having only one
|
|
|
|
* #ClutterActor as a child.
|
|
|
|
*
|
|
|
|
* #StBin inherits from #StWidget, so it is fully themable.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <clutter/clutter.h>
|
|
|
|
|
|
|
|
#include "st-bin.h"
|
|
|
|
#include "st-enum-types.h"
|
|
|
|
#include "st-private.h"
|
|
|
|
|
2015-09-24 14:08:13 +00:00
|
|
|
typedef struct _StBinPrivate StBinPrivate;
|
2009-09-08 19:47:30 +00:00
|
|
|
struct _StBinPrivate
|
|
|
|
{
|
|
|
|
ClutterActor *child;
|
|
|
|
};
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
PROP_0,
|
|
|
|
|
|
|
|
PROP_CHILD,
|
2019-07-29 15:59:41 +00:00
|
|
|
|
|
|
|
N_PROPS
|
2009-09-08 19:47:30 +00:00
|
|
|
};
|
|
|
|
|
2019-07-29 15:59:41 +00:00
|
|
|
static GParamSpec *props[N_PROPS] = { NULL, };
|
|
|
|
|
2009-09-08 19:47:30 +00:00
|
|
|
static void clutter_container_iface_init (ClutterContainerIface *iface);
|
|
|
|
|
|
|
|
G_DEFINE_TYPE_WITH_CODE (StBin, st_bin, ST_TYPE_WIDGET,
|
2015-09-24 16:04:48 +00:00
|
|
|
G_ADD_PRIVATE (StBin)
|
2009-09-08 19:47:30 +00:00
|
|
|
G_IMPLEMENT_INTERFACE (CLUTTER_TYPE_CONTAINER,
|
|
|
|
clutter_container_iface_init));
|
|
|
|
|
|
|
|
static void
|
|
|
|
st_bin_add (ClutterContainer *container,
|
|
|
|
ClutterActor *actor)
|
|
|
|
{
|
|
|
|
st_bin_set_child (ST_BIN (container), actor);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
st_bin_remove (ClutterContainer *container,
|
|
|
|
ClutterActor *actor)
|
|
|
|
{
|
2015-09-24 14:08:13 +00:00
|
|
|
StBin *bin = ST_BIN (container);
|
|
|
|
StBinPrivate *priv = st_bin_get_instance_private (bin);
|
2009-09-08 19:47:30 +00:00
|
|
|
|
|
|
|
if (priv->child == actor)
|
2015-09-24 14:08:13 +00:00
|
|
|
st_bin_set_child (bin, NULL);
|
2009-09-08 19:47:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
clutter_container_iface_init (ClutterContainerIface *iface)
|
|
|
|
{
|
|
|
|
iface->add = st_bin_add;
|
|
|
|
iface->remove = st_bin_remove;
|
|
|
|
}
|
|
|
|
|
2019-10-17 21:27:27 +00:00
|
|
|
static double
|
|
|
|
get_align_factor (ClutterActorAlign align)
|
|
|
|
{
|
|
|
|
switch (align)
|
|
|
|
{
|
|
|
|
case CLUTTER_ACTOR_ALIGN_CENTER:
|
|
|
|
return 0.5;
|
|
|
|
|
|
|
|
case CLUTTER_ACTOR_ALIGN_START:
|
|
|
|
return 0.0;
|
|
|
|
|
|
|
|
case CLUTTER_ACTOR_ALIGN_END:
|
|
|
|
return 1.0;
|
|
|
|
|
|
|
|
case CLUTTER_ACTOR_ALIGN_FILL:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0.0;
|
|
|
|
}
|
|
|
|
|
2009-09-08 19:47:30 +00:00
|
|
|
static void
|
|
|
|
st_bin_allocate (ClutterActor *self,
|
2020-05-09 19:30:26 +00:00
|
|
|
const ClutterActorBox *box)
|
2009-09-08 19:47:30 +00:00
|
|
|
{
|
2015-09-24 14:08:13 +00:00
|
|
|
StBinPrivate *priv = st_bin_get_instance_private (ST_BIN (self));
|
2009-09-08 19:47:30 +00:00
|
|
|
|
2020-05-09 19:30:26 +00:00
|
|
|
clutter_actor_set_allocation (self, box);
|
2009-09-08 19:47:30 +00:00
|
|
|
|
2015-07-22 13:43:12 +00:00
|
|
|
if (priv->child && clutter_actor_is_visible (priv->child))
|
2009-09-08 19:47:30 +00:00
|
|
|
{
|
2009-09-20 17:41:13 +00:00
|
|
|
StThemeNode *theme_node = st_widget_get_theme_node (ST_WIDGET (self));
|
2019-10-17 21:27:27 +00:00
|
|
|
ClutterActorAlign x_align = clutter_actor_get_x_align (priv->child);
|
|
|
|
ClutterActorAlign y_align = clutter_actor_get_y_align (priv->child);
|
2010-02-13 16:24:25 +00:00
|
|
|
ClutterActorBox childbox;
|
|
|
|
|
|
|
|
st_theme_node_get_content_box (theme_node, box, &childbox);
|
2012-02-14 01:22:10 +00:00
|
|
|
clutter_actor_allocate_align_fill (priv->child, &childbox,
|
2019-10-17 21:27:27 +00:00
|
|
|
get_align_factor (x_align),
|
|
|
|
get_align_factor (y_align),
|
|
|
|
x_align == CLUTTER_ACTOR_ALIGN_FILL,
|
2020-05-09 19:30:26 +00:00
|
|
|
y_align == CLUTTER_ACTOR_ALIGN_FILL);
|
2009-09-08 19:47:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
st_bin_get_preferred_width (ClutterActor *self,
|
|
|
|
gfloat for_height,
|
|
|
|
gfloat *min_width_p,
|
|
|
|
gfloat *natural_width_p)
|
|
|
|
{
|
2015-09-24 14:08:13 +00:00
|
|
|
StBinPrivate *priv = st_bin_get_instance_private (ST_BIN (self));
|
2009-09-20 17:41:13 +00:00
|
|
|
StThemeNode *theme_node = st_widget_get_theme_node (ST_WIDGET (self));
|
2009-09-08 19:47:30 +00:00
|
|
|
|
2009-09-20 17:41:13 +00:00
|
|
|
st_theme_node_adjust_for_height (theme_node, &for_height);
|
2009-09-08 19:47:30 +00:00
|
|
|
|
2015-07-22 13:43:12 +00:00
|
|
|
if (priv->child == NULL || !clutter_actor_is_visible (priv->child))
|
2009-09-08 19:47:30 +00:00
|
|
|
{
|
|
|
|
if (min_width_p)
|
2009-09-20 17:41:13 +00:00
|
|
|
*min_width_p = 0;
|
2009-09-08 19:47:30 +00:00
|
|
|
|
|
|
|
if (natural_width_p)
|
2009-09-20 17:41:13 +00:00
|
|
|
*natural_width_p = 0;
|
2009-09-08 19:47:30 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-10-17 21:27:27 +00:00
|
|
|
ClutterActorAlign y_align = clutter_actor_get_y_align (priv->child);
|
|
|
|
|
|
|
|
_st_actor_get_preferred_width (priv->child, for_height,
|
|
|
|
y_align == CLUTTER_ACTOR_ALIGN_FILL,
|
2010-02-13 16:24:25 +00:00
|
|
|
min_width_p,
|
|
|
|
natural_width_p);
|
2009-09-08 19:47:30 +00:00
|
|
|
}
|
2009-09-20 17:41:13 +00:00
|
|
|
|
|
|
|
st_theme_node_adjust_preferred_width (theme_node, min_width_p, natural_width_p);
|
2009-09-08 19:47:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
st_bin_get_preferred_height (ClutterActor *self,
|
|
|
|
gfloat for_width,
|
|
|
|
gfloat *min_height_p,
|
|
|
|
gfloat *natural_height_p)
|
|
|
|
{
|
2015-09-24 14:08:13 +00:00
|
|
|
StBinPrivate *priv = st_bin_get_instance_private (ST_BIN (self));
|
2009-09-20 17:41:13 +00:00
|
|
|
StThemeNode *theme_node = st_widget_get_theme_node (ST_WIDGET (self));
|
2009-09-08 19:47:30 +00:00
|
|
|
|
2009-09-20 17:41:13 +00:00
|
|
|
st_theme_node_adjust_for_width (theme_node, &for_width);
|
2009-09-08 19:47:30 +00:00
|
|
|
|
2015-07-22 13:43:12 +00:00
|
|
|
if (priv->child == NULL || !clutter_actor_is_visible (priv->child))
|
2009-09-08 19:47:30 +00:00
|
|
|
{
|
|
|
|
if (min_height_p)
|
2009-09-20 17:41:13 +00:00
|
|
|
*min_height_p = 0;
|
2009-09-08 19:47:30 +00:00
|
|
|
|
|
|
|
if (natural_height_p)
|
2009-09-20 17:41:13 +00:00
|
|
|
*natural_height_p = 0;
|
2009-09-08 19:47:30 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-10-17 21:27:27 +00:00
|
|
|
ClutterActorAlign x_align = clutter_actor_get_y_align (priv->child);
|
|
|
|
|
|
|
|
_st_actor_get_preferred_height (priv->child, for_width,
|
|
|
|
x_align == CLUTTER_ACTOR_ALIGN_FILL,
|
2010-02-13 16:24:25 +00:00
|
|
|
min_height_p,
|
|
|
|
natural_height_p);
|
2009-09-08 19:47:30 +00:00
|
|
|
}
|
2009-09-20 17:41:13 +00:00
|
|
|
|
|
|
|
st_theme_node_adjust_preferred_height (theme_node, min_height_p, natural_height_p);
|
2009-09-08 19:47:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2018-08-28 20:29:13 +00:00
|
|
|
st_bin_destroy (ClutterActor *actor)
|
2009-09-08 19:47:30 +00:00
|
|
|
{
|
2018-08-28 20:29:13 +00:00
|
|
|
StBinPrivate *priv = st_bin_get_instance_private (ST_BIN (actor));
|
2009-09-08 19:47:30 +00:00
|
|
|
|
|
|
|
if (priv->child)
|
2009-10-08 18:53:46 +00:00
|
|
|
clutter_actor_destroy (priv->child);
|
|
|
|
g_assert (priv->child == NULL);
|
2009-09-08 19:47:30 +00:00
|
|
|
|
2018-08-28 20:29:13 +00:00
|
|
|
CLUTTER_ACTOR_CLASS (st_bin_parent_class)->destroy (actor);
|
2009-09-08 19:47:30 +00:00
|
|
|
}
|
|
|
|
|
2013-05-07 06:28:13 +00:00
|
|
|
static void
|
|
|
|
st_bin_popup_menu (StWidget *widget)
|
|
|
|
{
|
2015-09-24 14:08:13 +00:00
|
|
|
StBinPrivate *priv = st_bin_get_instance_private (ST_BIN (widget));
|
2013-05-07 06:28:13 +00:00
|
|
|
|
|
|
|
if (priv->child && ST_IS_WIDGET (priv->child))
|
|
|
|
st_widget_popup_menu (ST_WIDGET (priv->child));
|
|
|
|
}
|
|
|
|
|
2010-06-15 16:11:39 +00:00
|
|
|
static gboolean
|
|
|
|
st_bin_navigate_focus (StWidget *widget,
|
|
|
|
ClutterActor *from,
|
2018-11-27 12:58:25 +00:00
|
|
|
StDirectionType direction)
|
2010-06-15 16:11:39 +00:00
|
|
|
{
|
2015-09-24 14:08:13 +00:00
|
|
|
StBinPrivate *priv = st_bin_get_instance_private (ST_BIN (widget));
|
2010-06-15 16:11:39 +00:00
|
|
|
ClutterActor *bin_actor = CLUTTER_ACTOR (widget);
|
|
|
|
|
2012-08-16 16:39:31 +00:00
|
|
|
if (st_widget_get_can_focus (widget))
|
2010-06-15 16:11:39 +00:00
|
|
|
{
|
|
|
|
if (from && clutter_actor_contains (bin_actor, from))
|
|
|
|
return FALSE;
|
|
|
|
|
2015-07-22 13:43:12 +00:00
|
|
|
if (clutter_actor_is_mapped (bin_actor))
|
2012-09-06 20:12:44 +00:00
|
|
|
{
|
|
|
|
clutter_actor_grab_key_focus (bin_actor);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
2010-06-15 16:11:39 +00:00
|
|
|
}
|
|
|
|
else if (priv->child && ST_IS_WIDGET (priv->child))
|
|
|
|
return st_widget_navigate_focus (ST_WIDGET (priv->child), from, direction, FALSE);
|
|
|
|
else
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2009-09-08 19:47:30 +00:00
|
|
|
static void
|
|
|
|
st_bin_set_property (GObject *gobject,
|
|
|
|
guint prop_id,
|
|
|
|
const GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
StBin *bin = ST_BIN (gobject);
|
|
|
|
|
|
|
|
switch (prop_id)
|
|
|
|
{
|
|
|
|
case PROP_CHILD:
|
|
|
|
st_bin_set_child (bin, g_value_get_object (value));
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
st_bin_get_property (GObject *gobject,
|
|
|
|
guint prop_id,
|
|
|
|
GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
2015-09-24 14:08:13 +00:00
|
|
|
StBinPrivate *priv = st_bin_get_instance_private (ST_BIN (gobject));
|
2009-09-08 19:47:30 +00:00
|
|
|
|
|
|
|
switch (prop_id)
|
|
|
|
{
|
|
|
|
case PROP_CHILD:
|
|
|
|
g_value_set_object (value, priv->child);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
st_bin_class_init (StBinClass *klass)
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
|
|
|
|
ClutterActorClass *actor_class = CLUTTER_ACTOR_CLASS (klass);
|
2010-06-15 16:11:39 +00:00
|
|
|
StWidgetClass *widget_class = ST_WIDGET_CLASS (klass);
|
2009-09-08 19:47:30 +00:00
|
|
|
|
|
|
|
gobject_class->set_property = st_bin_set_property;
|
|
|
|
gobject_class->get_property = st_bin_get_property;
|
|
|
|
|
|
|
|
actor_class->get_preferred_width = st_bin_get_preferred_width;
|
|
|
|
actor_class->get_preferred_height = st_bin_get_preferred_height;
|
|
|
|
actor_class->allocate = st_bin_allocate;
|
2018-08-28 20:29:13 +00:00
|
|
|
actor_class->destroy = st_bin_destroy;
|
2009-09-08 19:47:30 +00:00
|
|
|
|
2013-05-07 06:28:13 +00:00
|
|
|
widget_class->popup_menu = st_bin_popup_menu;
|
2010-06-15 16:11:39 +00:00
|
|
|
widget_class->navigate_focus = st_bin_navigate_focus;
|
|
|
|
|
2009-09-08 19:47:30 +00:00
|
|
|
/**
|
|
|
|
* StBin:child:
|
|
|
|
*
|
|
|
|
* The child #ClutterActor of the #StBin container.
|
|
|
|
*/
|
2019-07-29 15:59:41 +00:00
|
|
|
props[PROP_CHILD] =
|
|
|
|
g_param_spec_object ("child",
|
|
|
|
"Child",
|
|
|
|
"The child of the Bin",
|
|
|
|
CLUTTER_TYPE_ACTOR,
|
|
|
|
ST_PARAM_READWRITE);
|
2009-09-08 19:47:30 +00:00
|
|
|
|
2019-07-29 15:59:41 +00:00
|
|
|
g_object_class_install_properties (gobject_class, N_PROPS, props);
|
2009-09-08 19:47:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
st_bin_init (StBin *bin)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* st_bin_new:
|
|
|
|
*
|
|
|
|
* Creates a new #StBin, a simple container for one child.
|
|
|
|
*
|
2020-05-20 23:50:09 +00:00
|
|
|
* Returns: the newly created #StBin actor
|
2009-09-08 19:47:30 +00:00
|
|
|
*/
|
|
|
|
StWidget *
|
|
|
|
st_bin_new (void)
|
|
|
|
{
|
|
|
|
return g_object_new (ST_TYPE_BIN, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* st_bin_set_child:
|
|
|
|
* @bin: a #StBin
|
2014-05-28 19:54:02 +00:00
|
|
|
* @child: (nullable): a #ClutterActor, or %NULL
|
2009-09-08 19:47:30 +00:00
|
|
|
*
|
|
|
|
* Sets @child as the child of @bin.
|
|
|
|
*
|
|
|
|
* If @bin already has a child, the previous child is removed.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
st_bin_set_child (StBin *bin,
|
|
|
|
ClutterActor *child)
|
|
|
|
{
|
|
|
|
StBinPrivate *priv;
|
|
|
|
|
|
|
|
g_return_if_fail (ST_IS_BIN (bin));
|
|
|
|
g_return_if_fail (child == NULL || CLUTTER_IS_ACTOR (child));
|
|
|
|
|
2015-09-24 14:08:13 +00:00
|
|
|
priv = st_bin_get_instance_private (bin);
|
2009-09-08 19:47:30 +00:00
|
|
|
|
|
|
|
if (priv->child == child)
|
|
|
|
return;
|
|
|
|
|
2020-11-24 09:34:08 +00:00
|
|
|
if (child)
|
|
|
|
{
|
|
|
|
ClutterActor *parent = clutter_actor_get_parent (child);
|
|
|
|
|
|
|
|
if (parent)
|
|
|
|
{
|
|
|
|
g_warning ("%s: The provided 'child' actor %p already has a "
|
|
|
|
"(different) parent %p and can't be made a child of %p.",
|
|
|
|
G_STRFUNC, child, parent, bin);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-08 19:47:30 +00:00
|
|
|
if (priv->child)
|
2012-02-13 20:22:53 +00:00
|
|
|
clutter_actor_remove_child (CLUTTER_ACTOR (bin), priv->child);
|
2009-09-08 19:47:30 +00:00
|
|
|
|
2012-02-13 20:22:53 +00:00
|
|
|
priv->child = NULL;
|
2009-09-08 19:47:30 +00:00
|
|
|
|
|
|
|
if (child)
|
|
|
|
{
|
|
|
|
priv->child = child;
|
2012-02-13 20:22:53 +00:00
|
|
|
clutter_actor_add_child (CLUTTER_ACTOR (bin), child);
|
2009-09-08 19:47:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
clutter_actor_queue_relayout (CLUTTER_ACTOR (bin));
|
|
|
|
|
2019-07-29 15:59:41 +00:00
|
|
|
g_object_notify_by_pspec (G_OBJECT (bin), props[PROP_CHILD]);
|
2009-09-08 19:47:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* st_bin_get_child:
|
|
|
|
* @bin: a #StBin
|
|
|
|
*
|
2020-05-20 23:50:09 +00:00
|
|
|
* Gets the #ClutterActor child for @bin.
|
2009-09-08 19:47:30 +00:00
|
|
|
*
|
2020-05-20 23:50:09 +00:00
|
|
|
* Returns: (transfer none) (nullable): a #ClutterActor, or %NULL
|
2009-09-08 19:47:30 +00:00
|
|
|
*/
|
|
|
|
ClutterActor *
|
|
|
|
st_bin_get_child (StBin *bin)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (ST_IS_BIN (bin), NULL);
|
|
|
|
|
2015-09-24 14:08:13 +00:00
|
|
|
return ((StBinPrivate *)st_bin_get_instance_private (bin))->child;
|
2009-09-08 19:47:30 +00:00
|
|
|
}
|