Define a style class for the tab close button
[empathy-mirror.git] / libempathy-gtk / empathy-avatar-image.c
blob3fb79692c333405f94361e80156dfa08b4263240
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.
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.
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.
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
21 * Authors: Xavier Claessens <xclaesse@gmail.com>
24 #include "config.h"
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>
32 #include <libempathy/empathy-utils.h>
33 #include "empathy-avatar-image.h"
34 #include "empathy-ui-utils.h"
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
42 * #EmpathyAvatarImage is a widget which displays an avatar.
45 /**
46 * EmpathyAvatarImage:
47 * @parent: parent object
49 * Widget which displays an avatar.
52 #define MAX_SMALL 64
53 #define MAX_LARGE 400
55 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyAvatarImage)
56 typedef struct {
57 GtkWidget *image;
58 GtkWidget *popup;
59 GdkPixbuf *pixbuf;
60 } EmpathyAvatarImagePriv;
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);
70 G_DEFINE_TYPE (EmpathyAvatarImage, empathy_avatar_image, GTK_TYPE_EVENT_BOX);
72 static void
73 empathy_avatar_image_class_init (EmpathyAvatarImageClass *klass)
75 GObjectClass *object_class;
76 GtkWidgetClass *widget_class;
78 object_class = G_OBJECT_CLASS (klass);
79 widget_class = GTK_WIDGET_CLASS (klass);
81 object_class->finalize = avatar_image_finalize;
83 widget_class->button_press_event = avatar_image_button_press_event;
84 widget_class->button_release_event = avatar_image_button_release_event;
86 g_type_class_add_private (object_class, sizeof (EmpathyAvatarImagePriv));
89 static void
90 empathy_avatar_image_init (EmpathyAvatarImage *avatar_image)
92 EmpathyAvatarImagePriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (avatar_image,
93 EMPATHY_TYPE_AVATAR_IMAGE, EmpathyAvatarImagePriv);
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);
101 avatar_image_add_filter (avatar_image);
104 static void
105 avatar_image_finalize (GObject *object)
107 EmpathyAvatarImagePriv *priv;
109 priv = GET_PRIV (object);
111 avatar_image_remove_filter (EMPATHY_AVATAR_IMAGE (object));
113 if (priv->popup) {
114 gtk_widget_destroy (priv->popup);
117 if (priv->pixbuf) {
118 g_object_unref (priv->pixbuf);
121 G_OBJECT_CLASS (empathy_avatar_image_parent_class)->finalize (object);
124 static GdkFilterReturn
125 avatar_image_filter_func (GdkXEvent *gdkxevent,
126 GdkEvent *event,
127 gpointer data)
129 XEvent *xevent = gdkxevent;
130 Atom atom;
131 EmpathyAvatarImagePriv *priv;
133 priv = GET_PRIV (data);
135 if (xevent->type == PropertyNotify) {
136 atom = gdk_x11_get_xatom_by_name ("_NET_CURRENT_DESKTOP");
137 if (xevent->xproperty.atom == atom) {
138 if (priv->popup) {
139 gtk_widget_destroy (priv->popup);
140 priv->popup = NULL;
145 return GDK_FILTER_CONTINUE;
148 static void
149 avatar_image_add_filter (EmpathyAvatarImage *avatar_image)
151 Display *display;
152 Window window;
153 gint mask;
154 XWindowAttributes attrs;
156 mask = PropertyChangeMask;
158 window = gdk_x11_get_default_root_xwindow ();
159 display = gdk_x11_get_default_xdisplay ();
161 gdk_error_trap_push ();
163 XGetWindowAttributes (display, window, &attrs);
164 mask |= attrs.your_event_mask;
166 XSelectInput (display, window, mask);
168 gdk_error_trap_pop_ignored ();
170 gdk_window_add_filter (NULL, avatar_image_filter_func, avatar_image);
173 static void
174 avatar_image_remove_filter (EmpathyAvatarImage *avatar_image)
176 gdk_window_remove_filter (NULL, avatar_image_filter_func, avatar_image);
179 static gboolean
180 avatar_image_button_press_event (GtkWidget *widget, GdkEventButton *event)
182 EmpathyAvatarImagePriv *priv;
183 GtkWidget *popup;
184 GtkWidget *frame;
185 GtkWidget *image;
186 gint x, y;
187 gint popup_width, popup_height;
188 gint width, height;
189 GdkPixbuf *pixbuf;
190 GtkAllocation allocation;
192 priv = GET_PRIV (widget);
194 if (priv->popup) {
195 gtk_widget_destroy (priv->popup);
196 priv->popup = NULL;
199 if (event->button != 1 || event->type != GDK_BUTTON_PRESS || !priv->pixbuf) {
200 return FALSE;
203 popup_width = gdk_pixbuf_get_width (priv->pixbuf);
204 popup_height = gdk_pixbuf_get_height (priv->pixbuf);
206 gtk_widget_get_allocation (priv->image, &allocation);
207 width = allocation.width;
208 height = allocation.height;
210 /* Don't show a popup if the popup is smaller then the currently avatar
211 * image.
213 if (popup_height <= height && popup_width <= width) {
214 return TRUE;
217 pixbuf = empathy_pixbuf_scale_down_if_necessary (priv->pixbuf, MAX_LARGE);
218 popup_width = gdk_pixbuf_get_width (pixbuf);
219 popup_height = gdk_pixbuf_get_height (pixbuf);
221 popup = gtk_window_new (GTK_WINDOW_POPUP);
223 frame = gtk_frame_new (NULL);
224 gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_OUT);
226 gtk_container_add (GTK_CONTAINER (popup), frame);
228 image = gtk_image_new ();
229 gtk_container_add (GTK_CONTAINER (frame), image);
231 gtk_image_set_from_pixbuf (GTK_IMAGE (image), pixbuf);
232 g_object_unref (pixbuf);
234 gdk_window_get_origin (gtk_widget_get_window (priv->image), &x, &y);
236 x = x - (popup_width - width) / 2;
237 y = y - (popup_height - height) / 2;
239 gtk_window_move (GTK_WINDOW (popup), x, y);
241 priv->popup = popup;
243 gtk_widget_show_all (popup);
245 return TRUE;
248 static gboolean
249 avatar_image_button_release_event (GtkWidget *widget, GdkEventButton *event)
251 EmpathyAvatarImagePriv *priv;
253 priv = GET_PRIV (widget);
255 if (event->button != 1 || event->type != GDK_BUTTON_RELEASE) {
256 return FALSE;
259 if (!priv->popup) {
260 return TRUE;
263 gtk_widget_destroy (priv->popup);
264 priv->popup = NULL;
266 return TRUE;
270 * empathy_avatar_image_new:
272 * Creates a new #EmpathyAvatarImage.
274 * Return value: a new #EmpathyAvatarImage
276 GtkWidget *
277 empathy_avatar_image_new (void)
279 EmpathyAvatarImage *avatar_image;
281 avatar_image = g_object_new (EMPATHY_TYPE_AVATAR_IMAGE, NULL);
283 return GTK_WIDGET (avatar_image);
287 * empathy_avatar_image_set:
288 * @avatar_image: an #EmpathyAvatarImage
289 * @avatar: the #EmpathyAvatar to set @avatar_image to
291 * Sets @avatar_image to display the avatar indicated by @avatar.
293 void
294 empathy_avatar_image_set (EmpathyAvatarImage *avatar_image,
295 EmpathyAvatar *avatar)
297 EmpathyAvatarImagePriv *priv = GET_PRIV (avatar_image);
298 GdkPixbuf *scaled_pixbuf;
300 g_return_if_fail (EMPATHY_IS_AVATAR_IMAGE (avatar_image));
302 if (priv->pixbuf) {
303 g_object_unref (priv->pixbuf);
304 priv->pixbuf = NULL;
307 if (avatar) {
308 priv->pixbuf = empathy_pixbuf_from_data ((gchar *) avatar->data,
309 avatar->len);
312 if (!priv->pixbuf) {
313 gtk_image_clear (GTK_IMAGE (priv->image));
314 return;
317 scaled_pixbuf = empathy_pixbuf_scale_down_if_necessary (priv->pixbuf, MAX_SMALL);
318 gtk_image_set_from_pixbuf (GTK_IMAGE (priv->image), scaled_pixbuf);
320 if (scaled_pixbuf != priv->pixbuf) {
321 gtk_widget_set_tooltip_text (GTK_WIDGET (avatar_image),
322 _("Click to enlarge"));
323 } else {
324 gtk_widget_set_tooltip_text (GTK_WIDGET (avatar_image),
325 NULL);
328 g_object_unref (scaled_pixbuf);