]> Pileus Git - ~andy/gtk/blob - gtk/gtkanimationdescription.c
Merge branch 'master' into treeview-refactor
[~andy/gtk] / gtk / gtkanimationdescription.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 2010 Carlos Garnacho <carlosg@gnome.org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #include "config.h"
21 #include "gtkanimationdescription.h"
22 #include "gtkintl.h"
23
24 struct GtkAnimationDescription
25 {
26   GtkTimelineProgressType progress_type;
27   gdouble duration;
28   guint loop : 1;
29   guint ref_count;
30 };
31
32 GtkAnimationDescription *
33 _gtk_animation_description_new (gdouble                 duration,
34                                 GtkTimelineProgressType progress_type,
35                                 gboolean                loop)
36 {
37   GtkAnimationDescription *desc;
38
39   desc = g_slice_new (GtkAnimationDescription);
40   desc->duration = duration;
41   desc->progress_type = progress_type;
42   desc->loop = loop;
43   desc->ref_count = 1;
44
45   return desc;
46 }
47
48 gdouble
49 _gtk_animation_description_get_duration (GtkAnimationDescription *desc)
50 {
51   return desc->duration;
52 }
53
54 GtkTimelineProgressType
55 _gtk_animation_description_get_progress_type (GtkAnimationDescription *desc)
56 {
57   return desc->progress_type;
58 }
59
60 gboolean
61 _gtk_animation_description_get_loop (GtkAnimationDescription *desc)
62 {
63   return (desc->loop != 0);
64 }
65
66 GtkAnimationDescription *
67 _gtk_animation_description_ref (GtkAnimationDescription *desc)
68 {
69   desc->ref_count++;
70   return desc;
71 }
72
73 void
74 _gtk_animation_description_unref (GtkAnimationDescription *desc)
75 {
76   desc->ref_count--;
77
78   if (desc->ref_count == 0)
79     g_slice_free (GtkAnimationDescription, desc);
80 }
81
82 GtkAnimationDescription *
83 _gtk_animation_description_from_string (const gchar *str)
84 {
85   gchar timing_function[16] = { 0, };
86   gchar duration_unit[3] = { 0, };
87   gchar loop_str[5] = { 0, };
88   GtkTimelineProgressType progress_type;
89   guint duration = 0;
90   gboolean loop;
91
92   if (sscanf (str, "%d%2s %15s %5s", &duration, duration_unit, timing_function, loop_str) == 4)
93     loop = TRUE;
94   else if (sscanf (str, "%d%2s %15s", &duration, duration_unit, timing_function) == 3)
95     loop = FALSE;
96   else
97     return NULL;
98
99   if (strcmp (duration_unit, "s") == 0)
100     duration *= 1000;
101   else if (strcmp (duration_unit, "ms") != 0)
102     {
103       g_warning ("Unknown duration unit: %s\n", duration_unit);
104       return NULL;
105     }
106
107   if (strcmp (timing_function, "linear") == 0)
108     progress_type = GTK_TIMELINE_PROGRESS_LINEAR;
109   else if (strcmp (timing_function, "ease") == 0)
110     progress_type = GTK_TIMELINE_PROGRESS_EASE;
111   else if (strcmp (timing_function, "ease-in") == 0)
112     progress_type = GTK_TIMELINE_PROGRESS_EASE_IN;
113   else if (strcmp (timing_function, "ease-out") == 0)
114     progress_type = GTK_TIMELINE_PROGRESS_EASE_OUT;
115   else if (strcmp (timing_function, "ease-in-out") == 0)
116     progress_type = GTK_TIMELINE_PROGRESS_EASE_IN_OUT;
117   else
118     {
119       g_warning ("Unknown timing function: %s\n", timing_function);
120       return NULL;
121     }
122
123   return _gtk_animation_description_new ((gdouble) duration, progress_type, loop);
124 }
125
126 GType
127 _gtk_animation_description_get_type (void)
128 {
129   static GType type = 0;
130
131   if (G_UNLIKELY (!type))
132     type = g_boxed_type_register_static (I_("GtkAnimationDescription"),
133                                          (GBoxedCopyFunc) _gtk_animation_description_ref,
134                                          (GBoxedFreeFunc) _gtk_animation_description_unref);
135
136   return type;
137 }