]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-avatar-image.c
de.po: Updated German 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., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  *
21  * Authors: Xavier Claessens <xclaesse@gmail.com>
22  */
23
24 #include "config.h"
25
26 #include <glib/gi18n-lib.h>
27 #include <gdk/gdkkeysyms.h>
28 #include <gdk/gdk.h>
29 #include <gtk/gtk.h>
30 #include <gdk/gdkx.h>
31
32 #include <libempathy/empathy-utils.h>
33 #include "empathy-avatar-image.h"
34 #include "empathy-ui-utils.h"
35
36 #define MAX_SMALL 64
37 #define MAX_LARGE 400
38
39 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyAvatarImage)
40 typedef struct {
41         GtkWidget   *image;
42         GtkWidget   *popup;
43         GdkPixbuf   *pixbuf;
44 } EmpathyAvatarImagePriv;
45
46 static void     avatar_image_finalize                (GObject           *object);
47 static void     avatar_image_add_filter              (EmpathyAvatarImage *avatar_image);
48 static void     avatar_image_remove_filter           (EmpathyAvatarImage *avatar_image);
49 static gboolean avatar_image_button_press_event      (GtkWidget         *widget,
50                                                       GdkEventButton    *event);
51 static gboolean avatar_image_button_release_event    (GtkWidget         *widget,
52                                                       GdkEventButton    *event);
53
54 G_DEFINE_TYPE (EmpathyAvatarImage, empathy_avatar_image, GTK_TYPE_EVENT_BOX);
55
56 static void
57 empathy_avatar_image_class_init (EmpathyAvatarImageClass *klass)
58 {
59         GObjectClass   *object_class;
60         GtkWidgetClass *widget_class;
61
62         object_class = G_OBJECT_CLASS (klass);
63         widget_class = GTK_WIDGET_CLASS (klass);
64
65         object_class->finalize = avatar_image_finalize;
66
67         widget_class->button_press_event   = avatar_image_button_press_event;
68         widget_class->button_release_event = avatar_image_button_release_event;
69
70         g_type_class_add_private (object_class, sizeof (EmpathyAvatarImagePriv));
71 }
72
73 static void
74 empathy_avatar_image_init (EmpathyAvatarImage *avatar_image)
75 {
76         EmpathyAvatarImagePriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (avatar_image,
77                 EMPATHY_TYPE_AVATAR_IMAGE, EmpathyAvatarImagePriv);
78
79         avatar_image->priv = priv;
80         priv->image = gtk_image_new ();
81         gtk_container_add (GTK_CONTAINER (avatar_image), priv->image);
82         empathy_avatar_image_set (avatar_image, NULL);
83         gtk_widget_show (priv->image);
84
85         avatar_image_add_filter (avatar_image);
86 }
87
88 static void
89 avatar_image_finalize (GObject *object)
90 {
91         EmpathyAvatarImagePriv *priv;
92
93         priv = GET_PRIV (object);
94
95         avatar_image_remove_filter (EMPATHY_AVATAR_IMAGE (object));
96
97         if (priv->popup) {
98                 gtk_widget_destroy (priv->popup);
99         }
100
101         if (priv->pixbuf) {
102                 g_object_unref (priv->pixbuf);
103         }
104
105         G_OBJECT_CLASS (empathy_avatar_image_parent_class)->finalize (object);
106 }
107
108 static GdkFilterReturn
109 avatar_image_filter_func (GdkXEvent  *gdkxevent,
110                           GdkEvent   *event,
111                           gpointer    data)
112 {
113         XEvent                *xevent = gdkxevent;
114         Atom                   atom;
115         EmpathyAvatarImagePriv *priv;
116
117         priv = GET_PRIV (data);
118
119         switch (xevent->type) {
120         case PropertyNotify:
121                 atom = gdk_x11_get_xatom_by_name ("_NET_CURRENT_DESKTOP");
122                 if (xevent->xproperty.atom == atom) {
123                         if (priv->popup) {
124                                 gtk_widget_destroy (priv->popup);
125                                 priv->popup = NULL;
126                         }
127                 }
128                 break;
129         }
130
131         return GDK_FILTER_CONTINUE;
132 }
133
134 static void
135 avatar_image_add_filter (EmpathyAvatarImage *avatar_image)
136 {
137         Window     window;
138         GdkWindow *gdkwindow;
139         gint       mask;
140
141         mask = PropertyChangeMask;
142
143         window = GDK_ROOT_WINDOW ();
144         gdkwindow = gdk_xid_table_lookup (window);
145
146         gdk_error_trap_push ();
147         if (gdkwindow) {
148                 XWindowAttributes attrs;
149                 XGetWindowAttributes (gdk_display, window, &attrs);
150                 mask |= attrs.your_event_mask;
151         }
152
153         XSelectInput (gdk_display, window, mask);
154
155         gdk_error_trap_pop ();
156
157         gdk_window_add_filter (NULL, avatar_image_filter_func, avatar_image);
158 }
159
160 static void
161 avatar_image_remove_filter (EmpathyAvatarImage *avatar_image)
162 {
163         gdk_window_remove_filter (NULL, avatar_image_filter_func, avatar_image);
164 }
165
166 static gboolean
167 avatar_image_button_press_event (GtkWidget *widget, GdkEventButton *event)
168 {
169         EmpathyAvatarImagePriv *priv;
170         GtkWidget             *popup;
171         GtkWidget             *frame;
172         GtkWidget             *image;
173         gint                   x, y;
174         gint                   popup_width, popup_height;
175         gint                   width, height;
176         GdkPixbuf             *pixbuf;
177
178         priv = GET_PRIV (widget);
179
180         if (priv->popup) {
181                 gtk_widget_destroy (priv->popup);
182                 priv->popup = NULL;
183         }
184
185         if (event->button != 1 || event->type != GDK_BUTTON_PRESS || !priv->pixbuf) {
186                 return FALSE;
187         }
188
189         popup_width = gdk_pixbuf_get_width (priv->pixbuf);
190         popup_height = gdk_pixbuf_get_height (priv->pixbuf);
191
192         width = priv->image->allocation.width;
193         height = priv->image->allocation.height;
194
195         /* Don't show a popup if the popup is smaller then the currently avatar
196          * image.
197          */
198         if (popup_height <= height && popup_width <= width) {
199                 return TRUE;
200         }
201
202         pixbuf = empathy_pixbuf_scale_down_if_necessary (priv->pixbuf, MAX_LARGE);
203         popup_width = gdk_pixbuf_get_width (pixbuf);
204         popup_height = gdk_pixbuf_get_height (pixbuf);
205
206         popup = gtk_window_new (GTK_WINDOW_POPUP);
207
208         frame = gtk_frame_new (NULL);
209         gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_OUT);
210
211         gtk_container_add (GTK_CONTAINER (popup), frame);
212
213         image = gtk_image_new ();
214         gtk_container_add (GTK_CONTAINER (frame), image);
215
216         gtk_image_set_from_pixbuf (GTK_IMAGE (image), pixbuf);
217         g_object_unref (pixbuf);
218
219         gdk_window_get_origin (priv->image->window, &x, &y);
220
221         x = x - (popup_width - width) / 2;
222         y = y - (popup_height - height) / 2;
223
224         gtk_window_move (GTK_WINDOW (popup), x, y);
225
226         priv->popup = popup;
227
228         gtk_widget_show_all (popup);
229
230         return TRUE;
231 }
232
233 static gboolean
234 avatar_image_button_release_event (GtkWidget *widget, GdkEventButton *event)
235 {
236         EmpathyAvatarImagePriv *priv;
237
238         priv = GET_PRIV (widget);
239
240         if (event->button != 1 || event->type != GDK_BUTTON_RELEASE) {
241                 return FALSE;
242         }
243
244         if (!priv->popup) {
245                 return TRUE;
246         }
247
248         gtk_widget_destroy (priv->popup);
249         priv->popup = NULL;
250
251         return TRUE;
252 }
253
254 GtkWidget *
255 empathy_avatar_image_new (void)
256 {
257         EmpathyAvatarImage *avatar_image;
258
259         avatar_image = g_object_new (EMPATHY_TYPE_AVATAR_IMAGE, NULL);
260
261         return GTK_WIDGET (avatar_image);
262 }
263
264 void
265 empathy_avatar_image_set (EmpathyAvatarImage *avatar_image,
266                           EmpathyAvatar      *avatar)
267 {
268         EmpathyAvatarImagePriv *priv = GET_PRIV (avatar_image);
269         GdkPixbuf              *scaled_pixbuf;
270
271         g_return_if_fail (EMPATHY_IS_AVATAR_IMAGE (avatar_image));
272
273         if (priv->pixbuf) {
274                 g_object_unref (priv->pixbuf);
275                 priv->pixbuf = NULL;
276         }
277
278         if (avatar) {
279                 priv->pixbuf = empathy_pixbuf_from_data (avatar->data, avatar->len);
280         }
281
282         if (!priv->pixbuf) {
283                 gtk_image_set_from_icon_name (GTK_IMAGE (priv->image),
284                                               "stock_person",
285                                               GTK_ICON_SIZE_DIALOG);
286                 return;
287         }
288
289         scaled_pixbuf = empathy_pixbuf_scale_down_if_necessary (priv->pixbuf, MAX_SMALL);
290         gtk_image_set_from_pixbuf (GTK_IMAGE (priv->image), scaled_pixbuf);
291
292         if (scaled_pixbuf != priv->pixbuf) {
293                 gtk_widget_set_tooltip_text (GTK_WIDGET (avatar_image),
294                                              _("Click to enlarge"));
295         } else {
296                 gtk_widget_set_tooltip_text (GTK_WIDGET (avatar_image),
297                                              NULL);
298         }
299
300         g_object_unref (scaled_pixbuf);
301 }
302