]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-avatar-image.c
Updated Spanish translation
[empathy.git] / libempathy-gtk / empathy-avatar-image.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2006-2007 Imendio AB
4  * Copyright (C) 2007-2008 Collabora Ltd.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of the
9  * License, or (at your option) any later version.
10  *
11  * This program 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  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public
17  * License along with this program; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19  * Boston, MA  02110-1301  USA
20  *
21  * Authors: Xavier Claessens <xclaesse@gmail.com>
22  */
23
24 #include "config.h"
25 #include "empathy-avatar-image.h"
26
27 #include <glib/gi18n-lib.h>
28 #include <gdk/gdkx.h>
29 #include <tp-account-widgets/tpaw-pixbuf-utils.h>
30
31 #include "empathy-ui-utils.h"
32 #include "empathy-utils.h"
33
34 /**
35  * SECTION:empathy-avatar-image
36  * @title: EmpathyAvatarImage
37  * @short_description: A widget to display an avatar
38  * @include: libempathy-gtk/empathy-avatar-image.h
39  *
40  * #EmpathyAvatarImage is a widget which displays an avatar.
41  */
42
43 /**
44  * EmpathyAvatarImage:
45  * @parent: parent object
46  *
47  * Widget which displays an avatar.
48  */
49
50 #define MAX_SMALL 64
51 #define MAX_LARGE 400
52
53 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyAvatarImage)
54 typedef struct {
55         GtkWidget   *image;
56         GtkWidget   *popup;
57         GdkPixbuf   *pixbuf;
58 } EmpathyAvatarImagePriv;
59
60 static void     avatar_image_finalize                (GObject           *object);
61 static void     avatar_image_add_filter              (EmpathyAvatarImage *avatar_image);
62 static void     avatar_image_remove_filter           (EmpathyAvatarImage *avatar_image);
63 static gboolean avatar_image_button_press_event      (GtkWidget         *widget,
64                                                       GdkEventButton    *event);
65 static gboolean avatar_image_button_release_event    (GtkWidget         *widget,
66                                                       GdkEventButton    *event);
67
68 G_DEFINE_TYPE (EmpathyAvatarImage, empathy_avatar_image, GTK_TYPE_EVENT_BOX);
69
70 static void
71 empathy_avatar_image_class_init (EmpathyAvatarImageClass *klass)
72 {
73         GObjectClass   *object_class;
74         GtkWidgetClass *widget_class;
75
76         object_class = G_OBJECT_CLASS (klass);
77         widget_class = GTK_WIDGET_CLASS (klass);
78
79         object_class->finalize = avatar_image_finalize;
80
81         widget_class->button_press_event   = avatar_image_button_press_event;
82         widget_class->button_release_event = avatar_image_button_release_event;
83
84         g_type_class_add_private (object_class, sizeof (EmpathyAvatarImagePriv));
85 }
86
87 static void
88 empathy_avatar_image_init (EmpathyAvatarImage *avatar_image)
89 {
90         EmpathyAvatarImagePriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (avatar_image,
91                 EMPATHY_TYPE_AVATAR_IMAGE, EmpathyAvatarImagePriv);
92
93         avatar_image->priv = priv;
94         priv->image = gtk_image_new ();
95         gtk_container_add (GTK_CONTAINER (avatar_image), priv->image);
96         empathy_avatar_image_set (avatar_image, NULL);
97         gtk_widget_show (priv->image);
98
99         avatar_image_add_filter (avatar_image);
100 }
101
102 static void
103 avatar_image_finalize (GObject *object)
104 {
105         EmpathyAvatarImagePriv *priv;
106
107         priv = GET_PRIV (object);
108
109         avatar_image_remove_filter (EMPATHY_AVATAR_IMAGE (object));
110
111         if (priv->popup) {
112                 gtk_widget_destroy (priv->popup);
113         }
114
115         if (priv->pixbuf) {
116                 g_object_unref (priv->pixbuf);
117         }
118
119         G_OBJECT_CLASS (empathy_avatar_image_parent_class)->finalize (object);
120 }
121
122 static GdkFilterReturn
123 avatar_image_filter_func (GdkXEvent  *gdkxevent,
124                           GdkEvent   *event,
125                           gpointer    data)
126 {
127         XEvent                *xevent = gdkxevent;
128         Atom                   atom;
129         EmpathyAvatarImagePriv *priv;
130
131         priv = GET_PRIV (data);
132
133         if (xevent->type == PropertyNotify) {
134                 atom = gdk_x11_get_xatom_by_name ("_NET_CURRENT_DESKTOP");
135                 if (xevent->xproperty.atom == atom) {
136                         if (priv->popup) {
137                                 gtk_widget_destroy (priv->popup);
138                                 priv->popup = NULL;
139                         }
140                 }
141         }
142
143         return GDK_FILTER_CONTINUE;
144 }
145
146 static void
147 avatar_image_add_filter (EmpathyAvatarImage *avatar_image)
148 {
149         Display    *display;
150         Window     window;
151         gint       mask;
152         XWindowAttributes attrs;
153
154         mask = PropertyChangeMask;
155
156         window = gdk_x11_get_default_root_xwindow ();
157         display = gdk_x11_get_default_xdisplay ();
158
159         gdk_error_trap_push ();
160
161         XGetWindowAttributes (display, window, &attrs);
162         mask |= attrs.your_event_mask;
163
164         XSelectInput (display, window, mask);
165
166         gdk_error_trap_pop_ignored ();
167
168         gdk_window_add_filter (NULL, avatar_image_filter_func, avatar_image);
169 }
170
171 static void
172 avatar_image_remove_filter (EmpathyAvatarImage *avatar_image)
173 {
174         gdk_window_remove_filter (NULL, avatar_image_filter_func, avatar_image);
175 }
176
177 static gboolean
178 avatar_image_button_press_event (GtkWidget *widget, GdkEventButton *event)
179 {
180         EmpathyAvatarImagePriv *priv;
181         GtkWidget             *popup;
182         GtkWidget             *frame;
183         GtkWidget             *image;
184         gint                   x, y;
185         gint                   popup_width, popup_height;
186         gint                   width, height;
187         GdkPixbuf             *pixbuf;
188         GtkAllocation          allocation;
189
190         priv = GET_PRIV (widget);
191
192         if (priv->popup) {
193                 gtk_widget_destroy (priv->popup);
194                 priv->popup = NULL;
195         }
196
197         if (event->button != 1 || event->type != GDK_BUTTON_PRESS || !priv->pixbuf) {
198                 return FALSE;
199         }
200
201         popup_width = gdk_pixbuf_get_width (priv->pixbuf);
202         popup_height = gdk_pixbuf_get_height (priv->pixbuf);
203
204         gtk_widget_get_allocation (priv->image, &allocation);
205         width = allocation.width;
206         height = allocation.height;
207
208         /* Don't show a popup if the popup is smaller then the currently avatar
209          * image.
210          */
211         if (popup_height <= height && popup_width <= width) {
212                 return TRUE;
213         }
214
215         pixbuf = tpaw_pixbuf_scale_down_if_necessary (priv->pixbuf, MAX_LARGE);
216         popup_width = gdk_pixbuf_get_width (pixbuf);
217         popup_height = gdk_pixbuf_get_height (pixbuf);
218
219         popup = gtk_window_new (GTK_WINDOW_POPUP);
220
221         frame = gtk_frame_new (NULL);
222         gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_OUT);
223
224         gtk_container_add (GTK_CONTAINER (popup), frame);
225
226         image = gtk_image_new ();
227         gtk_container_add (GTK_CONTAINER (frame), image);
228
229         gtk_image_set_from_pixbuf (GTK_IMAGE (image), pixbuf);
230         g_object_unref (pixbuf);
231
232         gdk_window_get_origin (gtk_widget_get_window (priv->image), &x, &y);
233
234         x = x - (popup_width - width) / 2;
235         y = y - (popup_height - height) / 2;
236
237         gtk_window_move (GTK_WINDOW (popup), x, y);
238
239         priv->popup = popup;
240
241         gtk_widget_show_all (popup);
242
243         return TRUE;
244 }
245
246 static gboolean
247 avatar_image_button_release_event (GtkWidget *widget, GdkEventButton *event)
248 {
249         EmpathyAvatarImagePriv *priv;
250
251         priv = GET_PRIV (widget);
252
253         if (event->button != 1 || event->type != GDK_BUTTON_RELEASE) {
254                 return FALSE;
255         }
256
257         if (!priv->popup) {
258                 return TRUE;
259         }
260
261         gtk_widget_destroy (priv->popup);
262         priv->popup = NULL;
263
264         return TRUE;
265 }
266
267 /**
268  * empathy_avatar_image_new:
269  *
270  * Creates a new #EmpathyAvatarImage.
271  *
272  * Return value: a new #EmpathyAvatarImage
273  */
274 GtkWidget *
275 empathy_avatar_image_new (void)
276 {
277         EmpathyAvatarImage *avatar_image;
278
279         avatar_image = g_object_new (EMPATHY_TYPE_AVATAR_IMAGE, NULL);
280
281         return GTK_WIDGET (avatar_image);
282 }
283
284 /**
285  * empathy_avatar_image_set:
286  * @avatar_image: an #EmpathyAvatarImage
287  * @avatar: the #EmpathyAvatar to set @avatar_image to
288  *
289  * Sets @avatar_image to display the avatar indicated by @avatar.
290  */
291 void
292 empathy_avatar_image_set (EmpathyAvatarImage *avatar_image,
293                           EmpathyAvatar      *avatar)
294 {
295         EmpathyAvatarImagePriv *priv = GET_PRIV (avatar_image);
296         GdkPixbuf              *scaled_pixbuf;
297
298         g_return_if_fail (EMPATHY_IS_AVATAR_IMAGE (avatar_image));
299
300         if (priv->pixbuf) {
301                 g_object_unref (priv->pixbuf);
302                 priv->pixbuf = NULL;
303         }
304
305         if (avatar) {
306                 priv->pixbuf = tpaw_pixbuf_from_data ((gchar *) avatar->data,
307                                 avatar->len);
308         }
309
310         if (!priv->pixbuf) {
311                 gtk_image_clear (GTK_IMAGE (priv->image));
312                 return;
313         }
314
315         scaled_pixbuf = tpaw_pixbuf_scale_down_if_necessary (priv->pixbuf, MAX_SMALL);
316         gtk_image_set_from_pixbuf (GTK_IMAGE (priv->image), scaled_pixbuf);
317
318         if (scaled_pixbuf != priv->pixbuf) {
319                 gtk_widget_set_tooltip_text (GTK_WIDGET (avatar_image),
320                                              _("Click to enlarge"));
321         } else {
322                 gtk_widget_set_tooltip_text (GTK_WIDGET (avatar_image),
323                                              NULL);
324         }
325
326         g_object_unref (scaled_pixbuf);
327 }
328