1 /* ev-transition-animation.c
2 * this file is part of evince, a gnome document viewer
4 * Copyright (C) 2007 Carlos Garnacho <carlos@imendio.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
24 #include "ev-transition-animation.h"
25 #include "ev-timeline.h"
27 #define EV_TRANSITION_ANIMATION_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), EV_TYPE_TRANSITION_ANIMATION, EvTransitionAnimationPriv))
30 typedef struct EvTransitionAnimationPriv EvTransitionAnimationPriv;
32 struct EvTransitionAnimationPriv {
33 EvTransitionEffect *effect;
34 cairo_surface_t *origin_surface;
35 cairo_surface_t *dest_surface;
46 G_DEFINE_TYPE (EvTransitionAnimation, ev_transition_animation, EV_TYPE_TIMELINE)
50 ev_transition_animation_init (EvTransitionAnimation *animation)
55 ev_transition_animation_set_property (GObject *object,
60 EvTransitionAnimationPriv *priv;
62 priv = EV_TRANSITION_ANIMATION_GET_PRIVATE (object);
67 g_object_unref (priv->effect);
69 priv->effect = g_value_dup_object (value);
71 case PROP_ORIGIN_SURFACE:
72 ev_transition_animation_set_origin_surface (EV_TRANSITION_ANIMATION (object),
73 g_value_get_pointer (value));
75 case PROP_DEST_SURFACE:
76 ev_transition_animation_set_dest_surface (EV_TRANSITION_ANIMATION (object),
77 g_value_get_pointer (value));
80 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
85 ev_transition_animation_get_property (GObject *object,
90 EvTransitionAnimationPriv *priv;
92 priv = EV_TRANSITION_ANIMATION_GET_PRIVATE (object);
96 g_value_set_object (value, priv->effect);
98 case PROP_ORIGIN_SURFACE:
99 g_value_set_pointer (value, priv->origin_surface);
101 case PROP_DEST_SURFACE:
102 g_value_set_pointer (value, priv->dest_surface);
105 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
110 ev_transition_animation_finalize (GObject *object)
112 EvTransitionAnimationPriv *priv;
114 priv = EV_TRANSITION_ANIMATION_GET_PRIVATE (object);
117 g_object_unref (priv->effect);
119 if (priv->origin_surface)
120 cairo_surface_destroy (priv->origin_surface);
122 if (priv->dest_surface)
123 cairo_surface_destroy (priv->dest_surface);
125 G_OBJECT_CLASS (ev_transition_animation_parent_class)->finalize (object);
129 ev_transition_animation_constructor (GType type,
130 guint n_construct_properties,
131 GObjectConstructParam *construct_params)
134 EvTransitionAnimationPriv *priv;
135 EvTransitionEffect *effect;
138 object = G_OBJECT_CLASS (ev_transition_animation_parent_class)->constructor (type,
139 n_construct_properties,
142 priv = EV_TRANSITION_ANIMATION_GET_PRIVATE (object);
143 effect = priv->effect;
145 g_object_get (effect, "duration", &duration, NULL);
146 ev_timeline_set_duration (EV_TIMELINE (object), duration * 1000);
152 ev_transition_animation_class_init (EvTransitionAnimationClass *klass)
154 GObjectClass *object_class = G_OBJECT_CLASS (klass);
156 object_class->set_property = ev_transition_animation_set_property;
157 object_class->get_property = ev_transition_animation_get_property;
158 object_class->finalize = ev_transition_animation_finalize;
159 object_class->constructor = ev_transition_animation_constructor;
161 g_object_class_install_property (object_class,
163 g_param_spec_object ("effect",
165 "Transition effect description",
166 EV_TYPE_TRANSITION_EFFECT,
167 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE));
168 g_object_class_install_property (object_class,
170 g_param_spec_pointer ("origin-surface",
172 "Cairo surface from which the animation will happen",
174 g_object_class_install_property (object_class,
176 g_param_spec_pointer ("dest-surface",
177 "Destination surface",
178 "Cairo surface to which the animation will happen",
181 g_type_class_add_private (klass, sizeof (EvTransitionAnimationPriv));
185 paint_surface (cairo_t *cr,
186 cairo_surface_t *surface,
190 GdkRectangle page_area)
194 gdk_cairo_rectangle (cr, &page_area);
196 cairo_surface_set_device_offset (surface, x_offset, y_offset);
197 cairo_set_source_surface (cr, surface, 0, 0);
202 cairo_paint_with_alpha (cr, alpha);
209 ev_transition_animation_split (cairo_t *cr,
210 EvTransitionAnimation *animation,
211 EvTransitionEffect *effect,
213 GdkRectangle page_area)
215 EvTransitionAnimationPriv *priv;
216 EvTransitionEffectAlignment alignment;
217 EvTransitionEffectDirection direction;
220 priv = EV_TRANSITION_ANIMATION_GET_PRIVATE (animation);
221 width = page_area.width;
222 height = page_area.height;
224 g_object_get (effect,
225 "alignment", &alignment,
226 "direction", &direction,
229 if (direction == EV_TRANSITION_DIRECTION_INWARD) {
230 paint_surface (cr, priv->dest_surface, 0, 0, 1., page_area);
232 if (alignment == EV_TRANSITION_ALIGNMENT_HORIZONTAL) {
235 height * progress / 2,
237 height * (1 - progress));
240 width * progress / 2,
242 width * (1 - progress),
248 paint_surface (cr, priv->origin_surface, 0, 0, 1., page_area);
250 paint_surface (cr, priv->origin_surface, 0, 0, 1., page_area);
252 if (alignment == EV_TRANSITION_ALIGNMENT_HORIZONTAL) {
255 (height / 2) - (height * progress / 2),
260 (width / 2) - (width * progress / 2),
268 paint_surface (cr, priv->dest_surface, 0, 0, 1., page_area);
273 ev_transition_animation_blinds (cairo_t *cr,
274 EvTransitionAnimation *animation,
275 EvTransitionEffect *effect,
277 GdkRectangle page_area)
279 EvTransitionAnimationPriv *priv;
280 EvTransitionEffectAlignment alignment;
281 gint width, height, i;
283 priv = EV_TRANSITION_ANIMATION_GET_PRIVATE (animation);
284 width = page_area.width;
285 height = page_area.height;
287 g_object_get (effect,
288 "alignment", &alignment,
291 paint_surface (cr, priv->origin_surface, 0, 0, 1., page_area);
293 for (i = 0; i < N_BLINDS; i++) {
296 if (alignment == EV_TRANSITION_ALIGNMENT_HORIZONTAL) {
299 height / N_BLINDS * i,
301 height / N_BLINDS * progress);
304 width / N_BLINDS * i,
306 width / N_BLINDS * progress,
311 paint_surface (cr, priv->dest_surface, 0, 0, 1., page_area);
317 ev_transition_animation_box (cairo_t *cr,
318 EvTransitionAnimation *animation,
319 EvTransitionEffect *effect,
321 GdkRectangle page_area)
323 EvTransitionAnimationPriv *priv;
324 EvTransitionEffectDirection direction;
327 priv = EV_TRANSITION_ANIMATION_GET_PRIVATE (animation);
328 width = page_area.width;
329 height = page_area.height;
331 g_object_get (effect,
332 "direction", &direction,
335 if (direction == EV_TRANSITION_DIRECTION_INWARD) {
336 paint_surface (cr, priv->dest_surface, 0, 0, 1., page_area);
339 width * progress / 2,
340 height * progress / 2,
341 width * (1 - progress),
342 height * (1 - progress));
345 paint_surface (cr, priv->origin_surface, 0, 0, 1., page_area);
347 paint_surface (cr, priv->origin_surface, 0, 0, 1., page_area);
350 (width / 2) - (width * progress / 2),
351 (height / 2) - (height * progress / 2),
356 paint_surface (cr, priv->dest_surface, 0, 0, 1., page_area);
361 ev_transition_animation_wipe (cairo_t *cr,
362 EvTransitionAnimation *animation,
363 EvTransitionEffect *effect,
365 GdkRectangle page_area)
367 EvTransitionAnimationPriv *priv;
371 priv = EV_TRANSITION_ANIMATION_GET_PRIVATE (animation);
372 width = page_area.width;
373 height = page_area.height;
375 g_object_get (effect,
379 paint_surface (cr, priv->origin_surface, 0, 0, 1., page_area);
387 } else if (angle <= 90) {
391 height * (1 - progress),
394 } else if (angle <= 180) {
397 width * (1 - progress),
401 } else if (angle <= 270) {
411 paint_surface (cr, priv->dest_surface, 0, 0, 1., page_area);
415 ev_transition_animation_dissolve (cairo_t *cr,
416 EvTransitionAnimation *animation,
417 EvTransitionEffect *effect,
419 GdkRectangle page_area)
421 EvTransitionAnimationPriv *priv;
423 priv = EV_TRANSITION_ANIMATION_GET_PRIVATE (animation);
425 paint_surface (cr, priv->dest_surface, 0, 0, 1., page_area);
426 paint_surface (cr, priv->origin_surface, 0, 0, 1 - progress, page_area);
430 ev_transition_animation_push (cairo_t *cr,
431 EvTransitionAnimation *animation,
432 EvTransitionEffect *effect,
434 GdkRectangle page_area)
436 EvTransitionAnimationPriv *priv;
440 priv = EV_TRANSITION_ANIMATION_GET_PRIVATE (animation);
441 width = page_area.width;
442 height = page_area.height;
444 g_object_get (effect,
450 paint_surface (cr, priv->origin_surface, - (width * progress), 0, 1., page_area);
451 paint_surface (cr, priv->dest_surface, width * (1 - progress), 0, 1., page_area);
454 paint_surface (cr, priv->origin_surface, 0, - (height * progress), 1., page_area);
455 paint_surface (cr, priv->dest_surface, 0, height * (1 - progress), 1., page_area);
460 ev_transition_animation_cover (cairo_t *cr,
461 EvTransitionAnimation *animation,
462 EvTransitionEffect *effect,
464 GdkRectangle page_area)
466 EvTransitionAnimationPriv *priv;
470 priv = EV_TRANSITION_ANIMATION_GET_PRIVATE (animation);
471 width = page_area.width;
472 height = page_area.height;
474 g_object_get (effect,
478 paint_surface (cr, priv->origin_surface, 0, 0, 1., page_area);
482 paint_surface (cr, priv->dest_surface, width * (1 - progress), 0, 1., page_area);
485 paint_surface (cr, priv->dest_surface, 0, height * (1 - progress), 1., page_area);
490 ev_transition_animation_uncover (cairo_t *cr,
491 EvTransitionAnimation *animation,
492 EvTransitionEffect *effect,
494 GdkRectangle page_area)
496 EvTransitionAnimationPriv *priv;
500 priv = EV_TRANSITION_ANIMATION_GET_PRIVATE (animation);
501 width = page_area.width;
502 height = page_area.height;
504 g_object_get (effect,
508 paint_surface (cr, priv->dest_surface, 0, 0, 1., page_area);
512 paint_surface (cr, priv->origin_surface, - (width * progress), 0, 1., page_area);
515 paint_surface (cr, priv->origin_surface, 0, - (height * progress), 1., page_area);
520 ev_transition_animation_fade (cairo_t *cr,
521 EvTransitionAnimation *animation,
522 EvTransitionEffect *effect,
524 GdkRectangle page_area)
526 EvTransitionAnimationPriv *priv;
528 priv = EV_TRANSITION_ANIMATION_GET_PRIVATE (animation);
530 paint_surface (cr, priv->origin_surface, 0, 0, 1., page_area);
531 paint_surface (cr, priv->dest_surface, 0, 0, progress, page_area);
535 ev_transition_animation_paint (EvTransitionAnimation *animation,
537 GdkRectangle page_area)
539 EvTransitionAnimationPriv *priv;
540 EvTransitionEffectType type;
543 g_return_if_fail (EV_IS_TRANSITION_ANIMATION (animation));
545 priv = EV_TRANSITION_ANIMATION_GET_PRIVATE (animation);
547 if (!priv->dest_surface) {
548 /* animation is still not ready, paint the origin surface */
549 paint_surface (cr, priv->origin_surface, 0, 0, 1., page_area);
553 g_object_get (priv->effect, "type", &type, NULL);
554 progress = ev_timeline_get_progress (EV_TIMELINE (animation));
557 case EV_TRANSITION_EFFECT_REPLACE:
558 /* just paint the destination slide */
559 paint_surface (cr, priv->dest_surface, 0, 0, 1., page_area);
561 case EV_TRANSITION_EFFECT_SPLIT:
562 ev_transition_animation_split (cr, animation, priv->effect, progress, page_area);
564 case EV_TRANSITION_EFFECT_BLINDS:
565 ev_transition_animation_blinds (cr, animation, priv->effect, progress, page_area);
567 case EV_TRANSITION_EFFECT_BOX:
568 ev_transition_animation_box (cr, animation, priv->effect, progress, page_area);
570 case EV_TRANSITION_EFFECT_WIPE:
571 ev_transition_animation_wipe (cr, animation, priv->effect, progress, page_area);
573 case EV_TRANSITION_EFFECT_DISSOLVE:
574 ev_transition_animation_dissolve (cr, animation, priv->effect, progress, page_area);
576 case EV_TRANSITION_EFFECT_PUSH:
577 ev_transition_animation_push (cr, animation, priv->effect, progress, page_area);
579 case EV_TRANSITION_EFFECT_COVER:
580 ev_transition_animation_cover (cr, animation, priv->effect, progress, page_area);
582 case EV_TRANSITION_EFFECT_UNCOVER:
583 ev_transition_animation_uncover (cr, animation, priv->effect, progress, page_area);
585 case EV_TRANSITION_EFFECT_FADE:
586 ev_transition_animation_fade (cr, animation, priv->effect, progress, page_area);
589 GEnumValue *enum_value;
591 enum_value = g_enum_get_value (g_type_class_peek (EV_TYPE_TRANSITION_EFFECT_TYPE), type);
593 g_warning ("Unimplemented transition animation: '%s', "
594 "please post a bug report in Evince bugzilla "
595 "(http://bugzilla.gnome.org) with a testcase.",
596 enum_value->value_nick);
598 /* just paint the destination slide */
599 paint_surface (cr, priv->dest_surface, 0, 0, 1., page_area);
604 EvTransitionAnimation *
605 ev_transition_animation_new (EvTransitionEffect *effect)
607 g_return_val_if_fail (EV_IS_TRANSITION_EFFECT (effect), NULL);
609 return g_object_new (EV_TYPE_TRANSITION_ANIMATION,
615 ev_transition_animation_set_origin_surface (EvTransitionAnimation *animation,
616 cairo_surface_t *origin_surface)
618 EvTransitionAnimationPriv *priv;
619 cairo_surface_t *surface;
621 g_return_if_fail (EV_IS_TRANSITION_ANIMATION (animation));
623 priv = EV_TRANSITION_ANIMATION_GET_PRIVATE (animation);
625 if (priv->origin_surface == origin_surface)
628 surface = cairo_surface_reference (origin_surface);
630 if (priv->origin_surface)
631 cairo_surface_destroy (priv->origin_surface);
633 priv->origin_surface = surface;
634 g_object_notify (G_OBJECT (animation), "origin-surface");
636 if (priv->origin_surface && priv->dest_surface)
637 ev_timeline_start (EV_TIMELINE (animation));
641 ev_transition_animation_set_dest_surface (EvTransitionAnimation *animation,
642 cairo_surface_t *dest_surface)
644 EvTransitionAnimationPriv *priv;
645 cairo_surface_t *surface;
647 g_return_if_fail (EV_IS_TRANSITION_ANIMATION (animation));
649 priv = EV_TRANSITION_ANIMATION_GET_PRIVATE (animation);
651 if (priv->dest_surface == dest_surface)
654 surface = cairo_surface_reference (dest_surface);
656 if (priv->dest_surface)
657 cairo_surface_destroy (priv->dest_surface);
659 priv->dest_surface = surface;
660 g_object_notify (G_OBJECT (animation), "dest-surface");
662 if (priv->origin_surface && priv->dest_surface)
663 ev_timeline_start (EV_TIMELINE (animation));
667 ev_transition_animation_ready (EvTransitionAnimation *animation)
669 EvTransitionAnimationPriv *priv;
671 g_return_val_if_fail (EV_IS_TRANSITION_ANIMATION (animation), FALSE);
673 priv = EV_TRANSITION_ANIMATION_GET_PRIVATE (animation);
675 return (priv->origin_surface != NULL);