[StScrollView] Add support for GtkPolicyType for scrollbars

Previously we were hacking out the vertical scrollbar, this patch
allow us to sanely say in which directions we want to scroll.

Note this patch intentionally changes St to depend on GTK+ in the
API.  I believe this is a correct long term change where we should
view St as co-evolving with GTK+ rather than replacing or paralleling.

https://bugzilla.gnome.org/show_bug.cgi?id=609015
This commit is contained in:
Colin Walters 2010-02-16 11:04:07 -05:00
parent cfd63c7d4c
commit 18c5405b79
5 changed files with 151 additions and 7 deletions

View File

@ -217,6 +217,7 @@ St-1.0.gir: $(mutter) $(G_IR_SCANNER) libgnome-shell.la libst-1.0.la Makefile
--namespace=St \ --namespace=St \
--nsversion=1.0 \ --nsversion=1.0 \
--include=Clutter-1.0 \ --include=Clutter-1.0 \
--include=Gtk-2.0 \
--add-include-path=$(builddir) \ --add-include-path=$(builddir) \
--libtool="$(LIBTOOL)" \ --libtool="$(LIBTOOL)" \
--program=mutter \ --program=mutter \

View File

@ -64,6 +64,9 @@ struct _StScrollViewPrivate
ClutterActor *hscroll; ClutterActor *hscroll;
ClutterActor *vscroll; ClutterActor *vscroll;
GtkPolicyType hscroll_policy;
GtkPolicyType vscroll_policy;
gfloat row_size; gfloat row_size;
gfloat column_size; gfloat column_size;
@ -77,6 +80,8 @@ enum {
PROP_HSCROLL, PROP_HSCROLL,
PROP_VSCROLL, PROP_VSCROLL,
PROP_HSCROLLBAR_POLICY,
PROP_VSCROLLBAR_POLICY,
PROP_MOUSE_SCROLL PROP_MOUSE_SCROLL
}; };
@ -96,6 +101,12 @@ st_scroll_view_get_property (GObject *object,
case PROP_VSCROLL: case PROP_VSCROLL:
g_value_set_object (value, priv->vscroll); g_value_set_object (value, priv->vscroll);
break; break;
case PROP_HSCROLLBAR_POLICY:
g_value_set_enum (value, priv->hscroll_policy);
break;
case PROP_VSCROLLBAR_POLICY:
g_value_set_enum (value, priv->vscroll_policy);
break;
case PROP_MOUSE_SCROLL: case PROP_MOUSE_SCROLL:
g_value_set_boolean (value, priv->mouse_scroll); g_value_set_boolean (value, priv->mouse_scroll);
break; break;
@ -110,11 +121,25 @@ st_scroll_view_set_property (GObject *object,
const GValue *value, const GValue *value,
GParamSpec *pspec) GParamSpec *pspec)
{ {
StScrollView *self = ST_SCROLL_VIEW (object);
StScrollViewPrivate *priv = self->priv;
switch (property_id) switch (property_id)
{ {
case PROP_MOUSE_SCROLL: case PROP_MOUSE_SCROLL:
st_scroll_view_set_mouse_scrolling ((StScrollView *) object, st_scroll_view_set_mouse_scrolling (self,
g_value_get_boolean (value)); g_value_get_boolean (value));
break;
case PROP_HSCROLLBAR_POLICY:
st_scroll_view_set_policy (self,
g_value_get_enum (value),
priv->vscroll_policy);
break;
case PROP_VSCROLLBAR_POLICY:
st_scroll_view_set_policy (self,
priv->hscroll_policy,
g_value_get_enum (value));
break;
default: default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
} }
@ -474,6 +499,23 @@ st_scroll_view_class_init (StScrollViewClass *klass)
ST_TYPE_SCROLL_BAR, ST_TYPE_SCROLL_BAR,
G_PARAM_READABLE)); G_PARAM_READABLE));
pspec = g_param_spec_enum ("vscrollbar-policy",
"Vertical Scrollbar Policy",
"When the vertical scrollbar is displayed",
GTK_TYPE_POLICY_TYPE,
GTK_POLICY_ALWAYS,
G_PARAM_READWRITE);
g_object_class_install_property (object_class, PROP_VSCROLLBAR_POLICY, pspec);
pspec = g_param_spec_enum ("hscrollbar-policy",
"Horizontal Scrollbar Policy",
"When the horizontal scrollbar is displayed",
GTK_TYPE_POLICY_TYPE,
GTK_POLICY_ALWAYS,
G_PARAM_READWRITE);
g_object_class_install_property (object_class, PROP_HSCROLLBAR_POLICY, pspec);
pspec = g_param_spec_boolean ("enable-mouse-scrolling", pspec = g_param_spec_boolean ("enable-mouse-scrolling",
"Enable Mouse Scrolling", "Enable Mouse Scrolling",
"Enable automatic mouse wheel scrolling", "Enable automatic mouse wheel scrolling",
@ -525,6 +567,9 @@ child_hadjustment_notify_cb (GObject *gobject,
child_adjustment_changed_cb, child_adjustment_changed_cb,
priv->hscroll); priv->hscroll);
if (priv->hscroll_policy == GTK_POLICY_NEVER)
return;
st_scrollable_get_adjustments (ST_SCROLLABLE (actor), &hadjust, NULL); st_scrollable_get_adjustments (ST_SCROLLABLE (actor), &hadjust, NULL);
if (hadjust) if (hadjust)
{ {
@ -537,11 +582,15 @@ child_hadjustment_notify_cb (GObject *gobject,
} }
st_scroll_bar_set_adjustment (ST_SCROLL_BAR(priv->hscroll), hadjust); st_scroll_bar_set_adjustment (ST_SCROLL_BAR(priv->hscroll), hadjust);
if (priv->hscroll_policy == GTK_POLICY_AUTOMATIC)
{
g_signal_connect (hadjust, "changed", G_CALLBACK ( g_signal_connect (hadjust, "changed", G_CALLBACK (
child_adjustment_changed_cb), priv->hscroll); child_adjustment_changed_cb), priv->hscroll);
child_adjustment_changed_cb (hadjust, priv->hscroll); child_adjustment_changed_cb (hadjust, priv->hscroll);
} }
} }
}
static void static void
child_vadjustment_notify_cb (GObject *gobject, child_vadjustment_notify_cb (GObject *gobject,
@ -559,6 +608,9 @@ child_vadjustment_notify_cb (GObject *gobject,
child_adjustment_changed_cb, child_adjustment_changed_cb,
priv->vscroll); priv->vscroll);
if (priv->vscroll_policy == GTK_POLICY_NEVER)
return;
st_scrollable_get_adjustments (ST_SCROLLABLE(actor), NULL, &vadjust); st_scrollable_get_adjustments (ST_SCROLLABLE(actor), NULL, &vadjust);
if (vadjust) if (vadjust)
{ {
@ -571,17 +623,23 @@ child_vadjustment_notify_cb (GObject *gobject,
} }
st_scroll_bar_set_adjustment (ST_SCROLL_BAR(priv->vscroll), vadjust); st_scroll_bar_set_adjustment (ST_SCROLL_BAR(priv->vscroll), vadjust);
if (priv->vscroll_policy == GTK_POLICY_AUTOMATIC)
{
g_signal_connect (vadjust, "changed", G_CALLBACK ( g_signal_connect (vadjust, "changed", G_CALLBACK (
child_adjustment_changed_cb), priv->vscroll); child_adjustment_changed_cb), priv->vscroll);
child_adjustment_changed_cb (vadjust, priv->vscroll); child_adjustment_changed_cb (vadjust, priv->vscroll);
} }
} }
}
static void static void
st_scroll_view_init (StScrollView *self) st_scroll_view_init (StScrollView *self)
{ {
StScrollViewPrivate *priv = self->priv = SCROLL_VIEW_PRIVATE (self); StScrollViewPrivate *priv = self->priv = SCROLL_VIEW_PRIVATE (self);
priv->hscroll_policy = GTK_POLICY_AUTOMATIC;
priv->vscroll_policy = GTK_POLICY_AUTOMATIC;
priv->hscroll = CLUTTER_ACTOR (st_scroll_bar_new (NULL)); priv->hscroll = CLUTTER_ACTOR (st_scroll_bar_new (NULL));
priv->vscroll = g_object_new (ST_TYPE_SCROLL_BAR, "vertical", TRUE, NULL); priv->vscroll = g_object_new (ST_TYPE_SCROLL_BAR, "vertical", TRUE, NULL);
@ -842,3 +900,73 @@ st_scroll_view_get_mouse_scrolling (StScrollView *scroll)
return priv->mouse_scroll; return priv->mouse_scroll;
} }
/**
* st_scroll_view_set_policy:
* @scroll: A #StScrollView
* @hscroll: Whether to enable horizontal scrolling
* @vscroll: Whether to enable vertical scrolling
*
* Set the scroll policy.
*/
void
st_scroll_view_set_policy (StScrollView *scroll,
GtkPolicyType hscroll,
GtkPolicyType vscroll)
{
StScrollViewPrivate *priv;
StAdjustment *hadjust, *vadjust;
g_return_if_fail (ST_IS_SCROLL_VIEW (scroll));
g_return_if_fail (hscroll == GTK_POLICY_ALWAYS || vscroll == GTK_POLICY_ALWAYS);
priv = ST_SCROLL_VIEW (scroll)->priv;
if (priv->hscroll_policy == hscroll && priv->vscroll_policy == vscroll)
return;
g_object_freeze_notify ((GObject *) scroll);
if (priv->hscroll_policy != hscroll)
{
priv->hscroll_policy = hscroll;
g_object_notify ((GObject *) scroll, "hscrollbar-policy");
}
if (priv->vscroll_policy != vscroll)
{
priv->vscroll_policy = vscroll;
g_object_notify ((GObject *) scroll, "vscrollbar-policy");
}
st_scrollable_get_adjustments (ST_SCROLLABLE (priv->child), &hadjust, &vadjust);
if (priv->hscroll_policy == GTK_POLICY_NEVER)
{
hadjust = NULL;
clutter_actor_hide (priv->hscroll);
}
else if (priv->hscroll_policy == GTK_POLICY_AUTOMATIC)
{
/* We call this function because we need to set up the adjustment
* notification hooks, and the _show will queue a reallocate, from which we'll figure
* out if we need to really show or hide the scrollbar.
*/
child_hadjustment_notify_cb (G_OBJECT (priv->child), NULL, scroll);
clutter_actor_show (priv->hscroll);
}
if (priv->vscroll_policy == GTK_POLICY_NEVER)
{
vadjust = NULL;
clutter_actor_hide (priv->vscroll);
}
else
{
child_vadjustment_notify_cb (G_OBJECT (priv->child), NULL, scroll);
clutter_actor_show (priv->vscroll);
}
st_scrollable_set_adjustments (ST_SCROLLABLE (priv->child), hadjust, vadjust);
g_object_thaw_notify ((GObject *) scroll);
}

View File

@ -84,6 +84,10 @@ void st_scroll_view_set_mouse_scrolling (StScrollView *scroll,
gboolean enabled); gboolean enabled);
gboolean st_scroll_view_get_mouse_scrolling (StScrollView *scroll); gboolean st_scroll_view_get_mouse_scrolling (StScrollView *scroll);
void st_scroll_view_set_policy (StScrollView *scroll,
GtkPolicyType hscroll,
GtkPolicyType vscroll);
G_END_DECLS G_END_DECLS
#endif /* __ST_SCROLL_VIEW_H__ */ #endif /* __ST_SCROLL_VIEW_H__ */

View File

@ -34,6 +34,7 @@
#include <glib-object.h> #include <glib-object.h>
#include <clutter/clutter.h> #include <clutter/clutter.h>
#include <gtk/gtk.h>
G_BEGIN_DECLS G_BEGIN_DECLS

View File

@ -14,9 +14,19 @@ let vbox = new St.BoxLayout({ vertical: true,
style: "padding: 10px;" }); style: "padding: 10px;" });
stage.add_actor(vbox); stage.add_actor(vbox);
let toggle = new St.Button({ label: 'Horizontal Scrolling',
toggle_mode: true });
vbox.add(toggle);
let v = new St.ScrollView(); let v = new St.ScrollView();
vbox.add(v, { expand: true }); vbox.add(v, { expand: true });
toggle.connect('notify::checked', function () {
v.set_policy(toggle.checked ? St.ScrollPolicy.AUTOMATIC
: St.ScrollPolicy.NEVER,
St.ScrollPolicy.AUTOMATIC);
});
let b = new St.BoxLayout({ vertical: true, let b = new St.BoxLayout({ vertical: true,
style: "border: 2px solid #880000; border-radius: 10px; padding: 0px 5px;" }); style: "border: 2px solid #880000; border-radius: 10px; padding: 0px 5px;" });
v.add_actor(b); v.add_actor(b);