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
|
/* Clearlooks theme engine
*
* Copyright (C) 2006 Kulyk Nazar <schamane@myeburg.net>
* Copyright (C) 2006 Benjamin Berg <benjamin@sipsolutions.net>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library 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.
*
*/
/* This code is responsible for the clearlooks animation support. The code
* works by forcing a redraw on the animated widget.
*/
#include "animation.h"
#ifdef HAVE_ANIMATION
#include <glib.h>
struct _AnimationInfo {
GTimer *timer;
gdouble start_modifier;
gdouble stop_time;
GtkWidget *widget;
};
typedef struct _AnimationInfo AnimationInfo;
struct _SignalInfo {
GtkWidget *widget;
gulong handler_id;
};
typedef struct _SignalInfo SignalInfo;
static GSList *connected_widgets = NULL;
static GHashTable *animated_widgets = NULL;
static int animation_timer_id = 0;
static gboolean animation_timeout_handler (gpointer data);
/* This forces a redraw on a widget */
static void
force_widget_redraw (GtkWidget *widget)
{
if (GE_IS_PROGRESS_BAR (widget))
gtk_widget_queue_resize (widget);
else
gtk_widget_queue_draw (widget);
}
/* ensures that the timer is running */
static void
start_timer ()
{
if (animation_timer_id == 0)
animation_timer_id = g_timeout_add (ANIMATION_DELAY, animation_timeout_handler, NULL);
}
/* ensures that the timer is stopped */
static void
stop_timer ()
{
if (animation_timer_id != 0)
{
g_source_remove(animation_timer_id);
animation_timer_id = 0;
}
}
/* destroys an AnimationInfo structure including the GTimer */
static void
animation_info_destroy (AnimationInfo *animation_info)
{
g_timer_destroy (animation_info->timer);
g_free (animation_info);
}
/* This function does not unref the weak reference, because the object
* is beeing destroyed currently. */
static void
on_animated_widget_destruction (gpointer data, GObject *object)
{
/* steal the animation info from the hash table (destroying it would
* result in the weak reference to be unrefed, which does not work
* as the widget is already destroyed. */
g_hash_table_steal (animated_widgets, object);
animation_info_destroy ((AnimationInfo*) data);
}
/* This function also needs to unref the weak reference. */
static void
destroy_animation_info_and_weak_unref (gpointer data)
{
AnimationInfo *animation_info = data;
/* force a last redraw. This is so that if the animation is removed,
* the widget is left in a sane state. */
force_widget_redraw (animation_info->widget);
g_object_weak_unref (G_OBJECT (animation_info->widget), on_animated_widget_destruction, data);
animation_info_destroy (animation_info);
}
/* Find and return a pointer to the data linked to this widget, if it exists */
static AnimationInfo*
lookup_animation_info (const GtkWidget *widget)
{
if (animated_widgets)
return g_hash_table_lookup (animated_widgets, widget);
return NULL;
}
/* Create all the relevant information for the animation, and insert it into the hash table. */
static void
add_animation (const GtkWidget *widget, gdouble stop_time)
{
AnimationInfo *value;
/* object already in the list, do not add it twice */
if (lookup_animation_info (widget))
return;
if (animated_widgets == NULL)
animated_widgets = g_hash_table_new_full (g_direct_hash, g_direct_equal,
NULL, destroy_animation_info_and_weak_unref);
value = g_new(AnimationInfo, 1);
value->widget = (GtkWidget*) widget;
value->timer = g_timer_new ();
value->stop_time= stop_time;
value->start_modifier = 0.0;
g_object_weak_ref (G_OBJECT (widget), on_animated_widget_destruction, value);
g_hash_table_insert (animated_widgets, (GtkWidget*) widget, value);
start_timer ();
}
/* update the animation information for each widget. This will also queue a redraw
* and stop the animation if it is done. */
static gboolean
update_animation_info (gpointer key, gpointer value, gpointer user_data)
{
AnimationInfo *animation_info = value;
GtkWidget *widget = key;
g_assert ((widget != NULL) && (animation_info != NULL));
/* remove the widget from the hash table if it is not drawable */
if (!GTK_WIDGET_DRAWABLE (widget))
{
return TRUE;
}
if (GE_IS_PROGRESS_BAR (widget))
{
gfloat fraction = gtk_progress_bar_get_fraction (GTK_PROGRESS_BAR (widget));
/* stop animation for filled/not filled progress bars */
if (fraction <= 0.0 || fraction >= 1.0)
return TRUE;
}
force_widget_redraw (widget);
/* stop at stop_time */
if (animation_info->stop_time != 0 &&
g_timer_elapsed (animation_info->timer, NULL) > animation_info->stop_time)
return TRUE;
return FALSE;
}
/* This gets called by the glib main loop every once in a while. */
static gboolean
animation_timeout_handler (gpointer data)
{
/*g_print("** TICK **\n");*/
/* enter threads as update_animation_info will use gtk/gdk. */
gdk_threads_enter ();
g_hash_table_foreach_remove (animated_widgets, update_animation_info, NULL);
/* leave threads again */
gdk_threads_leave ();
if(g_hash_table_size(animated_widgets)==0)
{
stop_timer ();
return FALSE;
}
return TRUE;
}
static void
on_checkbox_toggle (GtkWidget *widget, gpointer data)
{
AnimationInfo *animation_info = lookup_animation_info (widget);
if (animation_info != NULL)
{
gfloat elapsed = g_timer_elapsed (animation_info->timer, NULL);
animation_info->start_modifier = elapsed - animation_info->start_modifier;
}
else
{
add_animation (widget, CHECK_ANIMATION_TIME);
}
}
static void
on_connected_widget_destruction (gpointer data, GObject *widget)
{
connected_widgets = g_slist_remove (connected_widgets, data);
g_free (data);
}
static void
disconnect_all_signals ()
{
GSList * item = connected_widgets;
while (item != NULL)
{
SignalInfo *signal_info = (SignalInfo*) item->data;
g_signal_handler_disconnect (signal_info->widget, signal_info->handler_id);
g_object_weak_unref (G_OBJECT (signal_info->widget), on_connected_widget_destruction, signal_info);
g_free (signal_info);
item = g_slist_next (item);
}
g_slist_free (connected_widgets);
connected_widgets = NULL;
}
/* helper function for clearlooks_animation_connect_checkbox */
static gint
find_signal_info (gconstpointer signal_info, gconstpointer widget)
{
if (((SignalInfo*)signal_info)->widget == widget)
return 0;
else
return 1;
}
/* external interface */
/* adds a progress bar */
void
clearlooks_animation_progressbar_add (GtkWidget *progressbar)
{
gdouble fraction = gtk_progress_bar_get_fraction (GTK_PROGRESS_BAR (progressbar));
if (fraction < 1.0 && fraction > 0.0)
add_animation ((GtkWidget*) progressbar, 0.0);
}
/* hooks up the signals for check and radio buttons */
void
clearlooks_animation_connect_checkbox (GtkWidget *widget)
{
if (GE_IS_CHECK_BUTTON (widget))
{
if (!g_slist_find_custom (connected_widgets, widget, find_signal_info))
{
SignalInfo * signal_info = g_new (SignalInfo, 1);
signal_info->widget = widget;
signal_info->handler_id = g_signal_connect ((GObject*)widget, "toggled", G_CALLBACK (on_checkbox_toggle), NULL);
connected_widgets = g_slist_append (connected_widgets, signal_info);
g_object_weak_ref (G_OBJECT (widget), on_connected_widget_destruction, signal_info);
}
}
}
/* returns TRUE if the widget is animated, and FALSE otherwise */
gboolean
clearlooks_animation_is_animated (GtkWidget *widget)
{
return lookup_animation_info (widget) != NULL ? TRUE : FALSE;
}
/* returns the elapsed time for the animation */
gdouble
clearlooks_animation_elapsed (gpointer data)
{
AnimationInfo *animation_info = lookup_animation_info (data);
if (animation_info)
return g_timer_elapsed (animation_info->timer, NULL)
- animation_info->start_modifier;
else
return 0.0;
}
/* cleans up all resources of the animation system */
void
clearlooks_animation_cleanup ()
{
disconnect_all_signals ();
if (animated_widgets != NULL)
{
g_hash_table_destroy (animated_widgets);
animated_widgets = NULL;
}
stop_timer ();
}
#else /* !HAVE_ANIMATION */
static void clearlooks_animation_dummy_function_so_wall_shuts_up_when_animations_is_disabled()
{
clearlooks_animation_dummy_function_so_wall_shuts_up_when_animations_is_disabled();
}
#endif /* HAVE_ANIMATION */
|