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