aboutsummaryrefslogtreecommitdiff
path: root/src/lib/animations.c
blob: b3f6bf5ed9484792a96eb49bc99523032f80d5ed (plain) (blame)
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
/*
     This file is part of GNUnet.
     (C) 2012 Christian Grothoff (and other contributing authors)

     GNUnet is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published
     by the Free Software Foundation; either version 2, or (at your
     option) any later version.

     GNUnet 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
     General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with GNUnet; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
     Boston, MA 02111-1307, USA.
*/

/**
 * @file src/lib/animations.c
 * @brief drawing animations in gnunet-gtk
 * @author Christian Grothoff
 * @author LRN
 */
#include "gnunet_gtk.h"

/**
 * How often do we update animations?  Should be small enough
 * to fit with the animations used, but not so tiny that we just
 * wake up the CPU for nothing all the time.
 */
#define TICKER_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 100)


/**
 * Handle for an animation.  Each animation file should only
 * be loaded once and can then be used in multiple tree views.
 */
struct GNUNET_FS_AnimationContext
{
  /**
   * This is a doublye-linked list.
   */
  struct GNUNET_FS_AnimationContext *next;
 
  /**
   * This is a doublye-linked list.
   */
  struct GNUNET_FS_AnimationContext *prev;

  /**
   * Handle to the animation.
   */
  GdkPixbufAnimation *ani;

  /**
   * Iterator of the animation.
   */
  GdkPixbufAnimationIter *iter;

  /**
   * Pixbuf with the current image from the animation.
   */
  GdkPixbuf *pixbuf;

};


/**
 * Handle to a tree view (and column) that contains animations.
 * Whenever an animation is added to a tree view, this module
 * must be told about the tree view and column for the animations
 * to work.
 */
struct GNUNET_FS_AnimationTreeViewHandle
{
  /**
   * This is a doublye-linked list.
   */
  struct GNUNET_FS_AnimationTreeViewHandle *next;

  /**
   * This is a doublye-linked list.
   */
  struct GNUNET_FS_AnimationTreeViewHandle *prev;

  /**
   * Tree view to watch.
   */
  GtkTreeView *tv;

  /**
   * Column that might contain animations.
   */
  GtkTreeViewColumn *image_col;
};


/**
 * Head of linked list of animations.
 */
static struct GNUNET_FS_AnimationContext *animation_head;

/**
 * Tail of linked list of animations.
 */
static struct GNUNET_FS_AnimationContext *animation_tail;

/**
 * Head of linked list of tree views with animations.
 */
static struct GNUNET_FS_AnimationTreeViewHandle *atv_head;

/**
 * Tail of linked list of tree views with animations.
 */
static struct GNUNET_FS_AnimationTreeViewHandle *atv_tail;

/**
 * Task run to update animations.
 */
static GNUNET_SCHEDULER_TaskIdentifier ticker_task;


/**
 * Create an animation context.  Usually animation contexts are
 * created during the startup of the application and kept around
 * until shutdown.
 *
 * @param filename name of the file with the animation to show
 * @return context to use to get the animated pixbuf, NULL on error
 */
struct GNUNET_FS_AnimationContext *
GNUNET_GTK_animation_context_create (const char *filename)
{
  GError *err = NULL;
  struct GNUNET_FS_AnimationContext *ac;

  ac = GNUNET_malloc (sizeof (struct GNUNET_FS_AnimationContext));
  ac->ani = gdk_pixbuf_animation_new_from_file (filename, &err);
  if (NULL == ac->ani)
    {
      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
		  _("Failed to load animation from file `%s'\n"),
		  filename);
      GNUNET_free (ac);
      return NULL;
    }
  ac->iter = gdk_pixbuf_animation_get_iter (ac->ani, NULL);
  ac->pixbuf = gdk_pixbuf_copy (gdk_pixbuf_animation_iter_get_pixbuf (ac->iter));
  GNUNET_CONTAINER_DLL_insert (animation_head,
			       animation_tail,
			       ac);
  return ac;
}


/**
 * Destroy an animation context.  Should only be called after the
 * respective pixbufs are no longer needed.
 *
 * @param ac animation context to destroy.
 */
void
GNUNET_GTK_animation_context_destroy (struct GNUNET_FS_AnimationContext *ac)
{
  if (NULL == ac)
    return;
  g_object_unref (ac->pixbuf);
  g_object_unref (ac->iter); 
  g_object_unref (ac->ani);
  GNUNET_CONTAINER_DLL_remove (animation_head,
			       animation_tail,
			       ac);
  GNUNET_free (ac);
}


/**
 * Obtain the animated pixbuf from an animation context.  Note
 * that the pixbuf will only properly work within GtkTreeViews
 * where the column with the image has been registered using
 * GNUNET_GTK_animation_tree_view_register.
 *
 * @param ac animation context to query
 * @return pixbuf of the AC, NULL on error loading the pixbuf
 */
GdkPixbuf *
GNUNET_GTK_animation_context_get_pixbuf (struct GNUNET_FS_AnimationContext *ac)
{
  if (NULL == ac)
    return NULL;
  return ac->pixbuf;
}


/**
 * Advance the given animation by a frame, if the time is ripe.
 *
 * @param ac animation to advance
 * @return 0 if nothing needed to be done
 */
static int
tick_animation (struct GNUNET_FS_AnimationContext *ac)
{
  GdkPixbuf *pixbuf;
  gint width;
  gint height;

  if (!gdk_pixbuf_animation_iter_advance (ac->iter, NULL))
    return 0; 
  pixbuf = gdk_pixbuf_animation_iter_get_pixbuf (ac->iter);
  width = gdk_pixbuf_get_width (pixbuf);
  height = gdk_pixbuf_get_height (pixbuf);
  gdk_pixbuf_copy_area (pixbuf, 0, 0, width, height, ac->pixbuf, 0, 0);
  return 1;
}


/**
 * Redraw the visible portion of the tree view column (the animations
 * there might have changed).
 *
 * @param atv handle to the tree view to redraw
 */
static void
redraw_tree_view (struct GNUNET_FS_AnimationTreeViewHandle *atv)
{  
  GdkWindow *gw;
  GdkRectangle r;
  GdkRectangle tr;
  gint x;
  gint width;
  
  gw = gtk_widget_get_window (GTK_WIDGET (atv->tv));
  if (! gtk_widget_get_realized (GTK_WIDGET (atv->tv)))
    return;
  /* Get column x and width in bin window coordinates */
  gtk_tree_view_get_cell_area (atv->tv, NULL, atv->image_col, &r);
  /* Convert x and width to window coordinates */
  x = r.x;
  width = r.width;
  gtk_tree_view_convert_bin_window_to_widget_coords (atv->tv, x, 0, &r.x, NULL);
  gtk_tree_view_convert_bin_window_to_widget_coords (atv->tv, x + width, 0, &r.width, NULL);
  
  /* Get visible area of the treeview, in tree coordinates */
  gtk_tree_view_get_visible_rect (atv->tv, &tr);
  
  /* Convert y and height of the visible area to widget coordinates */
  gtk_tree_view_convert_tree_to_widget_coords (atv->tv, 0, tr.y, NULL, &r.y);
  gtk_tree_view_convert_tree_to_widget_coords (atv->tv, 0, tr.y + tr.height, NULL, &r.height);
  
  /* r now encloses only column image_col, redraw it */
  gdk_window_invalidate_rect (gw, &r, TRUE);
}


/**
 * Task run periodically to advance all of our animations.
 *
 * @param cls closure, unused
 * @param tc scheduler context, unused
 */
static void
ticker (void *cls,
	const struct GNUNET_SCHEDULER_TaskContext *tc)
{
  struct GNUNET_FS_AnimationContext *pos;
  struct GNUNET_FS_AnimationTreeViewHandle *atv;
  unsigned int counter;

  ticker_task = GNUNET_SCHEDULER_add_delayed (TICKER_DELAY,
					      &ticker,
					      NULL);
  counter = 0;
  for (pos = animation_head; NULL != pos; pos = pos->next)
    counter += tick_animation (pos);
  if (0 == counter)
    return; /* nothing to be done */
  for (atv = atv_head; NULL != atv; atv = atv->next)
    redraw_tree_view (atv);
}


/**
 * Register a tree view column with the animation subsystem.  This
 * column may contain animated pixbufs and thus needs to be periodically
 * redrawn.
 *
 * @param tv tree view to register
 * @param image_col column in the tree view with the animated pixbufs
 * @return handle to unregister the tree view later
 */
struct GNUNET_FS_AnimationTreeViewHandle *
GNUNET_GTK_animation_tree_view_register (GtkTreeView *tv,
					 GtkTreeViewColumn *image_col)
{
  struct GNUNET_FS_AnimationTreeViewHandle *atv;

  atv = GNUNET_malloc (sizeof (struct GNUNET_FS_AnimationTreeViewHandle));
  atv->tv = tv;
  atv->image_col = image_col;
  GNUNET_CONTAINER_DLL_insert (atv_head,
			       atv_tail,
			       atv);
  if (GNUNET_SCHEDULER_NO_TASK == ticker_task)
    ticker_task = GNUNET_SCHEDULER_add_delayed (TICKER_DELAY,
						&ticker,
						NULL);
  return atv;
}


/**
 * Unregister a tree view, it no longer needs to be updated.
 *
 * @param atv tree view to unregister
 */
void
GNUNET_GTK_animation_tree_view_unregister (struct GNUNET_FS_AnimationTreeViewHandle *atv)
{
  GNUNET_CONTAINER_DLL_remove (atv_head,
			       atv_tail,
			       atv);
  GNUNET_free (atv);
  if (NULL != atv_head)
    return;
  GNUNET_SCHEDULER_cancel (ticker_task);
  ticker_task = GNUNET_SCHEDULER_NO_TASK;
}


/* end of animations.c */