summaryrefslogtreecommitdiff
path: root/gtk/gtkanimationdescription.c
blob: 785018ad37a1af73654f36b816e2610235583ef4 (plain)
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
/* GTK - The GIMP Toolkit
 * Copyright (C) 2010 Carlos Garnacho <carlosg@gnome.org>
 *
 * 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.
 */

#include "config.h"
#include "gtkanimationdescription.h"
#include "gtkintl.h"

struct GtkAnimationDescription
{
  GtkTimelineProgressType progress_type;
  gdouble duration;
  guint loop : 1;
  guint ref_count;
};

GtkAnimationDescription *
gtk_animation_description_new (gdouble                 duration,
                               GtkTimelineProgressType progress_type,
                               gboolean                loop)
{
  GtkAnimationDescription *desc;

  desc = g_slice_new (GtkAnimationDescription);
  desc->duration = duration;
  desc->progress_type = progress_type;
  desc->loop = loop;
  desc->ref_count = 1;

  return desc;
}

gdouble
gtk_animation_description_get_duration (GtkAnimationDescription *desc)
{
  return desc->duration;
}

GtkTimelineProgressType
gtk_animation_description_get_progress_type (GtkAnimationDescription *desc)
{
  return desc->progress_type;
}

gboolean
gtk_animation_description_get_loop (GtkAnimationDescription *desc)
{
  return (desc->loop != 0);
}

GtkAnimationDescription *
gtk_animation_description_ref (GtkAnimationDescription *desc)
{
  desc->ref_count++;
  return desc;
}

void
gtk_animation_description_unref (GtkAnimationDescription *desc)
{
  desc->ref_count--;

  if (desc->ref_count == 0)
    g_slice_free (GtkAnimationDescription, desc);
}

GtkAnimationDescription *
gtk_animation_description_from_string (const gchar *str)
{
  gchar timing_function[16] = { 0, };
  gchar duration_unit[3] = { 0, };
  gchar loop_str[5] = { 0, };
  GtkTimelineProgressType progress_type;
  guint duration = 0;
  gboolean loop;

  if (sscanf (str, "%d%2s %15s %5s", &duration, duration_unit, timing_function, loop_str) == 4)
    loop = TRUE;
  else if (sscanf (str, "%d%2s %15s", &duration, duration_unit, timing_function) == 3)
    loop = FALSE;
  else
    return NULL;

  if (strcmp (duration_unit, "s") == 0)
    duration *= 1000;
  else if (strcmp (duration_unit, "ms") != 0)
    {
      g_warning ("Unknown duration unit: %s\n", duration_unit);
      return NULL;
    }

  if (strcmp (timing_function, "linear") == 0)
    progress_type = GTK_TIMELINE_PROGRESS_LINEAR;
  else if (strcmp (timing_function, "ease") == 0)
    progress_type = GTK_TIMELINE_PROGRESS_EASE;
  else if (strcmp (timing_function, "ease-in") == 0)
    progress_type = GTK_TIMELINE_PROGRESS_EASE_IN;
  else if (strcmp (timing_function, "ease-out") == 0)
    progress_type = GTK_TIMELINE_PROGRESS_EASE_OUT;
  else if (strcmp (timing_function, "ease-in-out") == 0)
    progress_type = GTK_TIMELINE_PROGRESS_EASE_IN_OUT;
  else
    {
      g_warning ("Unknown timing function: %s\n", timing_function);
      return NULL;
    }

  return gtk_animation_description_new ((gdouble) duration, progress_type, loop);
}

GType
gtk_animation_description_get_type (void)
{
  static GType type = 0;

  if (G_UNLIKELY (!type))
    type = g_boxed_type_register_static (I_("GtkAnimationDescription"),
					 (GBoxedCopyFunc) gtk_animation_description_ref,
					 (GBoxedFreeFunc) gtk_animation_description_unref);

  return type;
}