1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
|
/* tidy-scroll-view.h: Container with scroll-bars
*
* Copyright (C) 2008 OpenedHand
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that 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 along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
* Written by: Chris Lord <chris@openedhand.com>
*/
#include "tidy-scroll-view.h"
#include "tidy-marshal.h"
#include "tidy-scrollable.h"
#include <clutter/clutter.h>
static void clutter_container_iface_init (ClutterContainerIface *iface);
G_DEFINE_TYPE_WITH_CODE (TidyScrollView, tidy_scroll_view, CLUTTER_TYPE_ACTOR,
G_IMPLEMENT_INTERFACE (CLUTTER_TYPE_CONTAINER,
clutter_container_iface_init))
#define SCROLL_VIEW_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), \
TIDY_TYPE_SCROLL_VIEW, \
TidyScrollViewPrivate))
struct _TidyScrollViewPrivate
{
ClutterActor *child;
};
enum {
PROP_0,
PROP_CHILD,
};
static void
tidy_scroll_view_get_property (GObject *object, guint property_id,
GValue *value, GParamSpec *pspec)
{
TidyScrollViewPrivate *priv = ((TidyScrollView *)object)->priv;
switch (property_id)
{
case PROP_CHILD :
g_value_set_object (value, priv->child);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}
}
static void
tidy_scroll_view_set_property (GObject *object, guint property_id,
const GValue *value, GParamSpec *pspec)
{
switch (property_id)
{
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}
}
static void
tidy_scroll_view_dispose (GObject *object)
{
TidyScrollViewPrivate *priv = TIDY_SCROLL_VIEW (object)->priv;
if (priv->child)
clutter_container_remove_actor (CLUTTER_CONTAINER (object), priv->child);
G_OBJECT_CLASS (tidy_scroll_view_parent_class)->dispose (object);
}
static void
tidy_scroll_view_finalize (GObject *object)
{
G_OBJECT_CLASS (tidy_scroll_view_parent_class)->finalize (object);
}
static void
tidy_scroll_view_paint (ClutterActor *actor)
{
TidyScrollViewPrivate *priv = TIDY_SCROLL_VIEW (actor)->priv;
if (priv->child && CLUTTER_ACTOR_IS_VISIBLE (priv->child))
clutter_actor_paint (priv->child);
}
static void
tidy_scroll_view_pick (ClutterActor *actor, const ClutterColor *color)
{
/* Chain up so we get a bounding box pained (if we are reactive) */
CLUTTER_ACTOR_CLASS (tidy_scroll_view_parent_class)->pick (actor, color);
/* Trigger pick on children */
tidy_scroll_view_paint (actor);
}
static void
tidy_scroll_view_get_preferred_width (ClutterActor *actor,
gfloat for_height,
gfloat *min_width_p,
gfloat *natural_width_p)
{
TidyScrollViewPrivate *priv = TIDY_SCROLL_VIEW (actor)->priv;
if (!priv->child)
return;
/* Our natural width is the natural width of the child */
clutter_actor_get_preferred_width (priv->child,
for_height,
NULL,
natural_width_p);
}
static void
tidy_scroll_view_get_preferred_height (ClutterActor *actor,
gfloat for_width,
gfloat *min_height_p,
gfloat *natural_height_p)
{
TidyScrollViewPrivate *priv = TIDY_SCROLL_VIEW (actor)->priv;
if (!priv->child)
return;
/* Our natural height is the natural height of the child */
clutter_actor_get_preferred_height (priv->child,
for_width,
NULL,
natural_height_p);
}
static void
tidy_scroll_view_allocate (ClutterActor *actor,
const ClutterActorBox *box,
ClutterAllocationFlags flags)
{
ClutterActorBox child_box;
TidyScrollViewPrivate *priv = TIDY_SCROLL_VIEW (actor)->priv;
/* Chain up */
CLUTTER_ACTOR_CLASS (tidy_scroll_view_parent_class)->
allocate (actor, box, flags);
/* Child */
child_box.x1 = 0;
child_box.x2 = box->x2 - box->x1;
child_box.y1 = 0;
child_box.y2 = box->y2 - box->y1;
if (priv->child)
{
clutter_actor_allocate (priv->child, &child_box, flags);
clutter_actor_set_clip (priv->child,
child_box.x1,
child_box.y1,
child_box.x2 - child_box.x1,
child_box.y2 - child_box.y1);
}
}
static void
tidy_scroll_view_class_init (TidyScrollViewClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
ClutterActorClass *actor_class = CLUTTER_ACTOR_CLASS (klass);
g_type_class_add_private (klass, sizeof (TidyScrollViewPrivate));
object_class->get_property = tidy_scroll_view_get_property;
object_class->set_property = tidy_scroll_view_set_property;
object_class->dispose= tidy_scroll_view_dispose;
object_class->finalize = tidy_scroll_view_finalize;
actor_class->paint = tidy_scroll_view_paint;
actor_class->pick = tidy_scroll_view_pick;
actor_class->get_preferred_width = tidy_scroll_view_get_preferred_width;
actor_class->get_preferred_height = tidy_scroll_view_get_preferred_height;
actor_class->allocate = tidy_scroll_view_allocate;
g_object_class_install_property (object_class,
PROP_CHILD,
g_param_spec_object ("child",
"ClutterActor",
"Child actor",
CLUTTER_TYPE_ACTOR,
G_PARAM_READABLE));
}
static void
tidy_scroll_view_init (TidyScrollView *self)
{
TidyScrollViewPrivate *priv = self->priv = SCROLL_VIEW_PRIVATE (self);
}
static void
tidy_scroll_view_add_actor (ClutterContainer *container,
ClutterActor *actor)
{
TidyScrollView *self = TIDY_SCROLL_VIEW (container);
TidyScrollViewPrivate *priv = self->priv;
if (priv->child)
{
g_warning ("Attempting to add an actor of type %s to "
"a TidyScrollView that already contains "
"an actor of type %s.",
g_type_name (G_OBJECT_TYPE (actor)),
g_type_name (G_OBJECT_TYPE (priv->child)));
}
else
{
if (TIDY_IS_SCROLLABLE(actor))
{
priv->child = actor;
clutter_actor_set_parent (actor, CLUTTER_ACTOR (container));
/* Notify that child has been set */
g_signal_emit_by_name (container, "actor-added", priv->child);
g_object_notify (G_OBJECT (container), "child");
clutter_actor_queue_relayout (CLUTTER_ACTOR (container));
}
else
{
g_warning ("Attempting to add an actor of type %s to "
"a TidyScrollView, but the actor does "
"not implement TidyScrollable.",
g_type_name (G_OBJECT_TYPE (actor)));
}
}
}
static void
tidy_scroll_view_remove_actor (ClutterContainer *container,
ClutterActor *actor)
{
TidyScrollViewPrivate *priv = TIDY_SCROLL_VIEW (container)->priv;
if (actor == priv->child)
{
g_object_ref (priv->child);
clutter_actor_unparent (priv->child);
g_signal_emit_by_name (container, "actor-removed", priv->child);
g_object_unref (priv->child);
priv->child = NULL;
g_object_notify (G_OBJECT (container), "child");
if (CLUTTER_ACTOR_IS_VISIBLE (container))
clutter_actor_queue_relayout (CLUTTER_ACTOR (container));
}
}
static void
tidy_scroll_view_foreach (ClutterContainer *container,
ClutterCallback callback,
gpointer callback_data)
{
TidyScrollViewPrivate *priv = TIDY_SCROLL_VIEW (container)->priv;
if (priv->child)
callback (priv->child, callback_data);
}
static void
tidy_scroll_view_lower (ClutterContainer *container,
ClutterActor *actor,
ClutterActor *sibling)
{
/* single child */
}
static void
tidy_scroll_view_raise (ClutterContainer *container,
ClutterActor *actor,
ClutterActor *sibling)
{
/* single child */
}
static void
tidy_scroll_view_sort_depth_order (ClutterContainer *container)
{
/* single child */
}
static void
clutter_container_iface_init (ClutterContainerIface *iface)
{
iface->add = tidy_scroll_view_add_actor;
iface->remove = tidy_scroll_view_remove_actor;
iface->foreach = tidy_scroll_view_foreach;
iface->lower = tidy_scroll_view_lower;
iface->raise = tidy_scroll_view_raise;
iface->sort_depth_order = tidy_scroll_view_sort_depth_order;
}
ClutterActor *
tidy_scroll_view_new (void)
{
return CLUTTER_ACTOR (g_object_new (TIDY_TYPE_SCROLL_VIEW, NULL));
}
ClutterActor *
tidy_scroll_view_get_child (TidyScrollView *scroll)
{
TidyScrollViewPrivate *priv;
g_return_val_if_fail (TIDY_IS_SCROLL_VIEW (scroll), NULL);
priv = scroll->priv;
return priv->child;
}
|