1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2002-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., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
21 * Authors: Mikael Hallendal <micke@imendio.com>
22 * Richard Hult <richard@imendio.com>
23 * Martyn Russell <martyn@imendio.com>
24 * Xavier Claessens <xclaesse@gmail.com>
26 * Part of this file is copied from GtkSourceView (gtksourceiter.c):
34 #include <X11/Xatom.h>
36 #include <glib/gi18n.h>
39 #include <glade/glade.h>
41 #include <libmissioncontrol/mc-profile.h>
43 #include <libempathy/empathy-debug.h>
45 #include "empathy-ui-utils.h"
46 #include "empathy-images.h"
48 #define DEBUG_DOMAIN "UiUtils"
53 gboolean preserve_aspect_ratio;
57 get_glade_file (const gchar *filename,
60 const gchar *first_required_widget,
65 GtkWidget **widget_ptr;
67 empathy_debug (DEBUG_DOMAIN, "Loading glade file %s", filename);
69 gui = glade_xml_new (filename, root, domain);
72 g_warning ("Couldn't find necessary glade file '%s'", filename);
75 for (name = first_required_widget; name; name = va_arg (args, char *)) {
76 widget_ptr = va_arg (args, void *);
78 *widget_ptr = glade_xml_get_widget (gui, name);
81 g_warning ("Glade file '%s' is missing widget '%s'.",
91 empathy_glade_get_file_simple (const gchar *filename,
94 const gchar *first_required_widget, ...)
99 va_start (args, first_required_widget);
101 gui = get_glade_file (filename,
104 first_required_widget,
110 g_object_unref (gui);
115 empathy_glade_get_file (const gchar *filename,
118 const gchar *first_required_widget, ...)
123 va_start (args, first_required_widget);
125 gui = get_glade_file (filename,
128 first_required_widget,
141 empathy_glade_connect (GladeXML *gui,
143 gchar *first_widget, ...)
151 va_start (args, first_widget);
153 for (name = first_widget; name; name = va_arg (args, char *)) {
154 signal = va_arg (args, void *);
155 callback = va_arg (args, void *);
157 widget = glade_xml_get_widget (gui, name);
159 g_warning ("Glade file is missing widget '%s', aborting",
164 g_signal_connect (widget,
166 G_CALLBACK (callback),
174 empathy_glade_setup_size_group (GladeXML *gui,
175 GtkSizeGroupMode mode,
176 gchar *first_widget, ...)
180 GtkSizeGroup *size_group;
183 va_start (args, first_widget);
185 size_group = gtk_size_group_new (mode);
187 for (name = first_widget; name; name = va_arg (args, char *)) {
188 widget = glade_xml_get_widget (gui, name);
190 g_warning ("Glade file is missing widget '%s'", name);
194 gtk_size_group_add_widget (size_group, widget);
197 g_object_unref (size_group);
203 empathy_icon_name_from_account (McAccount *account)
207 profile = mc_account_get_profile (account);
209 return mc_profile_get_icon_name (profile);
213 empathy_icon_name_for_presence (McPresence presence)
216 case MC_PRESENCE_AVAILABLE:
217 return EMPATHY_IMAGE_AVAILABLE;
218 case MC_PRESENCE_DO_NOT_DISTURB:
219 return EMPATHY_IMAGE_BUSY;
220 case MC_PRESENCE_AWAY:
221 return EMPATHY_IMAGE_AWAY;
222 case MC_PRESENCE_EXTENDED_AWAY:
223 return EMPATHY_IMAGE_EXT_AWAY;
224 case MC_PRESENCE_HIDDEN:
225 return EMPATHY_IMAGE_HIDDEN;
226 case MC_PRESENCE_OFFLINE:
227 case MC_PRESENCE_UNSET:
228 return EMPATHY_IMAGE_OFFLINE;
230 g_assert_not_reached ();
237 empathy_icon_name_for_contact (EmpathyContact *contact)
241 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact),
242 EMPATHY_IMAGE_OFFLINE);
244 presence = empathy_contact_get_presence (contact);
245 return empathy_icon_name_for_presence (presence);
249 empathy_pixbuf_from_data (gchar *data,
252 GdkPixbufLoader *loader;
253 GdkPixbuf *pixbuf = NULL;
254 GError *error = NULL;
260 loader = gdk_pixbuf_loader_new ();
261 if (!gdk_pixbuf_loader_write (loader, data, data_size, &error)) {
262 empathy_debug (DEBUG_DOMAIN, "Failed to write to pixbuf loader: %s",
263 error ? error->message : "No error given");
264 g_clear_error (&error);
265 g_object_unref (loader);
268 if (!gdk_pixbuf_loader_close (loader, &error)) {
269 empathy_debug (DEBUG_DOMAIN, "Failed to close pixbuf loader: %s",
270 error ? error->message : "No error given");
271 g_clear_error (&error);
272 g_object_unref (loader);
276 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
278 g_object_ref (pixbuf);
281 g_object_unref (loader);
287 pixbuf_from_avatar_size_prepared_cb (GdkPixbufLoader *loader,
290 struct SizeData *data)
292 g_return_if_fail (width > 0 && height > 0);
294 if (data->preserve_aspect_ratio && (data->width > 0 || data->height > 0)) {
295 if (width < data->width && height < data->height) {
300 if (data->width < 0) {
301 width = width * (double) data->height / (gdouble) height;
302 height = data->height;
303 } else if (data->height < 0) {
304 height = height * (double) data->width / (double) width;
306 } else if ((double) height * (double) data->width >
307 (double) width * (double) data->height) {
308 width = 0.5 + (double) width * (double) data->height / (double) height;
309 height = data->height;
311 height = 0.5 + (double) height * (double) data->width / (double) width;
315 if (data->width > 0) {
319 if (data->height > 0) {
320 height = data->height;
324 gdk_pixbuf_loader_set_size (loader, width, height);
328 empathy_avatar_pixbuf_roundify (GdkPixbuf *pixbuf)
330 gint width, height, rowstride;
333 width = gdk_pixbuf_get_width (pixbuf);
334 height = gdk_pixbuf_get_height (pixbuf);
335 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
336 pixels = gdk_pixbuf_get_pixels (pixbuf);
338 if (width < 6 || height < 6) {
346 pixels[rowstride + 3] = 0x80;
347 pixels[rowstride * 2 + 3] = 0xC0;
350 pixels[width * 4 - 1] = 0;
351 pixels[width * 4 - 5] = 0x80;
352 pixels[width * 4 - 9] = 0xC0;
353 pixels[rowstride + (width * 4) - 1] = 0x80;
354 pixels[(2 * rowstride) + (width * 4) - 1] = 0xC0;
357 pixels[(height - 1) * rowstride + 3] = 0;
358 pixels[(height - 1) * rowstride + 7] = 0x80;
359 pixels[(height - 1) * rowstride + 11] = 0xC0;
360 pixels[(height - 2) * rowstride + 3] = 0x80;
361 pixels[(height - 3) * rowstride + 3] = 0xC0;
364 pixels[height * rowstride - 1] = 0;
365 pixels[(height - 1) * rowstride - 1] = 0x80;
366 pixels[(height - 2) * rowstride - 1] = 0xC0;
367 pixels[height * rowstride - 5] = 0x80;
368 pixels[height * rowstride - 9] = 0xC0;
372 empathy_gdk_pixbuf_is_opaque (GdkPixbuf *pixbuf)
374 gint width, height, rowstride, i;
378 width = gdk_pixbuf_get_width (pixbuf);
379 height = gdk_pixbuf_get_height (pixbuf);
380 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
381 pixels = gdk_pixbuf_get_pixels (pixbuf);
384 for (i = 3; i < rowstride; i+=4) {
390 for (i = 1; i < height - 1; i++) {
391 row = pixels + (i*rowstride);
392 if (row[3] < 0xfe || row[rowstride-1] < 0xfe) {
397 row = pixels + ((height-1) * rowstride);
398 for (i = 3; i < rowstride; i+=4) {
408 empathy_pixbuf_from_avatar_scaled (EmpathyAvatar *avatar,
413 GdkPixbufLoader *loader;
414 struct SizeData data;
415 GError *error = NULL;
422 data.height = height;
423 data.preserve_aspect_ratio = TRUE;
425 loader = gdk_pixbuf_loader_new ();
427 g_signal_connect (loader, "size-prepared",
428 G_CALLBACK (pixbuf_from_avatar_size_prepared_cb),
431 if (!gdk_pixbuf_loader_write (loader, avatar->data, avatar->len, &error)) {
432 g_warning ("Couldn't write avatar image:%p with "
433 "length:%" G_GSIZE_FORMAT " to pixbuf loader: %s",
434 avatar->data, avatar->len, error->message);
435 g_error_free (error);
439 gdk_pixbuf_loader_close (loader, NULL);
441 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
442 if (!gdk_pixbuf_get_has_alpha (pixbuf)) {
443 GdkPixbuf *rounded_pixbuf;
445 rounded_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8,
446 gdk_pixbuf_get_width (pixbuf),
447 gdk_pixbuf_get_height (pixbuf));
448 gdk_pixbuf_copy_area (pixbuf, 0, 0,
449 gdk_pixbuf_get_width (pixbuf),
450 gdk_pixbuf_get_height (pixbuf),
453 pixbuf = rounded_pixbuf;
455 g_object_ref (pixbuf);
458 if (empathy_gdk_pixbuf_is_opaque (pixbuf)) {
459 empathy_avatar_pixbuf_roundify (pixbuf);
462 g_object_unref (loader);
468 empathy_pixbuf_avatar_from_contact_scaled (EmpathyContact *contact,
472 EmpathyAvatar *avatar;
474 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
476 avatar = empathy_contact_get_avatar (contact);
478 return empathy_pixbuf_from_avatar_scaled (avatar, width, height);
482 empathy_pixbuf_scale_down_if_necessary (GdkPixbuf *pixbuf, gint max_size)
487 width = gdk_pixbuf_get_width (pixbuf);
488 height = gdk_pixbuf_get_height (pixbuf);
490 if (width > max_size || height > max_size) {
491 factor = (gdouble) max_size / MAX (width, height);
493 width = width * factor;
494 height = height * factor;
496 return gdk_pixbuf_scale_simple (pixbuf,
501 return g_object_ref (pixbuf);
505 empathy_pixbuf_from_icon_name (const gchar *icon_name,
506 GtkIconSize icon_size)
509 GdkPixbuf *pixbuf = NULL;
510 GError *error = NULL;
518 theme = gtk_icon_theme_get_default ();
520 if (gtk_icon_size_lookup (icon_size, &w, &h)) {
524 pixbuf = gtk_icon_theme_load_icon (theme,
530 empathy_debug (DEBUG_DOMAIN, "Error loading icon: %s", error->message);
531 g_clear_error (&error);
537 /* Stolen from GtkSourceView, hence the weird intendation. Please keep it like
538 * that to make it easier to apply changes from the original code.
540 #define GTK_TEXT_UNKNOWN_CHAR 0xFFFC
542 /* this function acts like g_utf8_offset_to_pointer() except that if it finds a
543 * decomposable character it consumes the decomposition length from the given
544 * offset. So it's useful when the offset was calculated for the normalized
545 * version of str, but we need a pointer to str itself. */
547 pointer_from_offset_skipping_decomp (const gchar *str, gint offset)
549 gchar *casefold, *normal;
555 q = g_utf8_next_char (p);
556 casefold = g_utf8_casefold (p, q - p);
557 normal = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
558 offset -= g_utf8_strlen (normal, -1);
567 g_utf8_strcasestr (const gchar *haystack, const gchar *needle)
571 const gchar *ret = NULL;
574 gchar *caseless_haystack;
577 g_return_val_if_fail (haystack != NULL, NULL);
578 g_return_val_if_fail (needle != NULL, NULL);
580 casefold = g_utf8_casefold (haystack, -1);
581 caseless_haystack = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
584 needle_len = g_utf8_strlen (needle, -1);
585 haystack_len = g_utf8_strlen (caseless_haystack, -1);
589 ret = (gchar *)haystack;
593 if (haystack_len < needle_len)
599 p = (gchar*)caseless_haystack;
600 needle_len = strlen (needle);
605 if ((strncmp (p, needle, needle_len) == 0))
607 ret = pointer_from_offset_skipping_decomp (haystack, i);
611 p = g_utf8_next_char (p);
616 g_free (caseless_haystack);
622 g_utf8_caselessnmatch (const char *s1, const char *s2,
623 gssize n1, gssize n2)
626 gchar *normalized_s1;
627 gchar *normalized_s2;
630 gboolean ret = FALSE;
632 g_return_val_if_fail (s1 != NULL, FALSE);
633 g_return_val_if_fail (s2 != NULL, FALSE);
634 g_return_val_if_fail (n1 > 0, FALSE);
635 g_return_val_if_fail (n2 > 0, FALSE);
637 casefold = g_utf8_casefold (s1, n1);
638 normalized_s1 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
641 casefold = g_utf8_casefold (s2, n2);
642 normalized_s2 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
645 len_s1 = strlen (normalized_s1);
646 len_s2 = strlen (normalized_s2);
651 ret = (strncmp (normalized_s1, normalized_s2, len_s2) == 0);
654 g_free (normalized_s1);
655 g_free (normalized_s2);
661 forward_chars_with_skipping (GtkTextIter *iter,
663 gboolean skip_invisible,
664 gboolean skip_nontext,
665 gboolean skip_decomp)
669 g_return_if_fail (count >= 0);
675 gboolean ignored = FALSE;
677 /* minimal workaround to avoid the infinite loop of bug #168247.
678 * It doesn't fix the problemjust the symptom...
680 if (gtk_text_iter_is_end (iter))
683 if (skip_nontext && gtk_text_iter_get_char (iter) == GTK_TEXT_UNKNOWN_CHAR)
686 if (!ignored && skip_invisible &&
687 /* _gtk_text_btree_char_is_invisible (iter)*/ FALSE)
690 if (!ignored && skip_decomp)
692 /* being UTF8 correct sucks; this accounts for extra
693 offsets coming from canonical decompositions of
694 UTF8 characters (e.g. accented characters) which
695 g_utf8_normalize() performs */
700 buffer_len = g_unichar_to_utf8 (gtk_text_iter_get_char (iter), buffer);
701 normal = g_utf8_normalize (buffer, buffer_len, G_NORMALIZE_NFD);
702 i -= (g_utf8_strlen (normal, -1) - 1);
706 gtk_text_iter_forward_char (iter);
714 lines_match (const GtkTextIter *start,
716 gboolean visible_only,
718 GtkTextIter *match_start,
719 GtkTextIter *match_end)
726 if (*lines == NULL || **lines == '\0')
729 *match_start = *start;
736 gtk_text_iter_forward_line (&next);
738 /* No more text in buffer, but *lines is nonempty */
739 if (gtk_text_iter_equal (start, &next))
745 line_text = gtk_text_iter_get_visible_slice (start, &next);
747 line_text = gtk_text_iter_get_slice (start, &next);
752 line_text = gtk_text_iter_get_visible_text (start, &next);
754 line_text = gtk_text_iter_get_text (start, &next);
757 if (match_start) /* if this is the first line we're matching */
759 found = g_utf8_strcasestr (line_text, *lines);
763 /* If it's not the first line, we have to match from the
766 if (g_utf8_caselessnmatch (line_text, *lines, strlen (line_text),
779 /* Get offset to start of search string */
780 offset = g_utf8_strlen (line_text, found - line_text);
784 /* If match start needs to be returned, set it to the
785 * start of the search string.
787 forward_chars_with_skipping (&next, offset, visible_only, !slice, FALSE);
793 /* Go to end of search string */
794 forward_chars_with_skipping (&next, g_utf8_strlen (*lines, -1), visible_only, !slice, TRUE);
803 /* pass NULL for match_start, since we don't need to find the
806 return lines_match (&next, lines, visible_only, slice, NULL, match_end);
809 /* strsplit () that retains the delimiter as part of the string. */
811 strbreakup (const char *string,
812 const char *delimiter,
815 GSList *string_list = NULL, *slist;
816 gchar **str_array, *s, *casefold, *new_string;
819 g_return_val_if_fail (string != NULL, NULL);
820 g_return_val_if_fail (delimiter != NULL, NULL);
823 max_tokens = G_MAXINT;
825 s = strstr (string, delimiter);
828 guint delimiter_len = strlen (delimiter);
834 len = s - string + delimiter_len;
835 new_string = g_new (gchar, len + 1);
836 strncpy (new_string, string, len);
838 casefold = g_utf8_casefold (new_string, -1);
840 new_string = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
842 string_list = g_slist_prepend (string_list, new_string);
844 string = s + delimiter_len;
845 s = strstr (string, delimiter);
846 } while (--max_tokens && s);
852 casefold = g_utf8_casefold (string, -1);
853 new_string = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
855 string_list = g_slist_prepend (string_list, new_string);
858 str_array = g_new (gchar*, n);
862 str_array[i--] = NULL;
863 for (slist = string_list; slist; slist = slist->next)
864 str_array[i--] = slist->data;
866 g_slist_free (string_list);
872 empathy_text_iter_forward_search (const GtkTextIter *iter,
874 GtkTextIter *match_start,
875 GtkTextIter *match_end,
876 const GtkTextIter *limit)
878 gchar **lines = NULL;
880 gboolean retval = FALSE;
882 gboolean visible_only;
885 g_return_val_if_fail (iter != NULL, FALSE);
886 g_return_val_if_fail (str != NULL, FALSE);
888 if (limit && gtk_text_iter_compare (iter, limit) >= 0)
892 /* If we can move one char, return the empty string there */
895 if (gtk_text_iter_forward_char (&match)) {
896 if (limit && gtk_text_iter_equal (&match, limit)) {
901 *match_start = match;
915 /* locate all lines */
916 lines = strbreakup (str, "\n", -1);
921 /* This loop has an inefficient worst-case, where
922 * gtk_text_iter_get_text () is called repeatedly on
927 if (limit && gtk_text_iter_compare (&search, limit) >= 0) {
931 if (lines_match (&search, (const gchar**)lines,
932 visible_only, slice, &match, &end)) {
934 (limit && gtk_text_iter_compare (&end, limit) <= 0)) {
938 *match_start = match;
946 } while (gtk_text_iter_forward_line (&search));
948 g_strfreev ((gchar**)lines);
954 g_utf8_strrcasestr (const gchar *haystack, const gchar *needle)
958 const gchar *ret = NULL;
961 gchar *caseless_haystack;
964 g_return_val_if_fail (haystack != NULL, NULL);
965 g_return_val_if_fail (needle != NULL, NULL);
967 casefold = g_utf8_casefold (haystack, -1);
968 caseless_haystack = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
971 needle_len = g_utf8_strlen (needle, -1);
972 haystack_len = g_utf8_strlen (caseless_haystack, -1);
976 ret = (gchar *)haystack;
980 if (haystack_len < needle_len)
986 i = haystack_len - needle_len;
987 p = g_utf8_offset_to_pointer (caseless_haystack, i);
988 needle_len = strlen (needle);
990 while (p >= caseless_haystack)
992 if (strncmp (p, needle, needle_len) == 0)
994 ret = pointer_from_offset_skipping_decomp (haystack, i);
998 p = g_utf8_prev_char (p);
1003 g_free (caseless_haystack);
1009 backward_lines_match (const GtkTextIter *start,
1010 const gchar **lines,
1011 gboolean visible_only,
1013 GtkTextIter *match_start,
1014 GtkTextIter *match_end)
1016 GtkTextIter line, next;
1021 if (*lines == NULL || **lines == '\0')
1024 *match_start = *start;
1026 *match_end = *start;
1030 line = next = *start;
1031 if (gtk_text_iter_get_line_offset (&next) == 0)
1033 if (!gtk_text_iter_backward_line (&next))
1037 gtk_text_iter_set_line_offset (&next, 0);
1042 line_text = gtk_text_iter_get_visible_slice (&next, &line);
1044 line_text = gtk_text_iter_get_slice (&next, &line);
1049 line_text = gtk_text_iter_get_visible_text (&next, &line);
1051 line_text = gtk_text_iter_get_text (&next, &line);
1054 if (match_start) /* if this is the first line we're matching */
1056 found = g_utf8_strrcasestr (line_text, *lines);
1060 /* If it's not the first line, we have to match from the
1061 * start of the line.
1063 if (g_utf8_caselessnmatch (line_text, *lines, strlen (line_text),
1076 /* Get offset to start of search string */
1077 offset = g_utf8_strlen (line_text, found - line_text);
1079 forward_chars_with_skipping (&next, offset, visible_only, !slice, FALSE);
1081 /* If match start needs to be returned, set it to the
1082 * start of the search string.
1086 *match_start = next;
1089 /* Go to end of search string */
1090 forward_chars_with_skipping (&next, g_utf8_strlen (*lines, -1), visible_only, !slice, TRUE);
1099 /* try to match the rest of the lines forward, passing NULL
1100 * for match_start so lines_match will try to match the entire
1102 return lines_match (&next, lines, visible_only,
1103 slice, NULL, match_end);
1107 empathy_text_iter_backward_search (const GtkTextIter *iter,
1109 GtkTextIter *match_start,
1110 GtkTextIter *match_end,
1111 const GtkTextIter *limit)
1113 gchar **lines = NULL;
1115 gboolean retval = FALSE;
1117 gboolean visible_only;
1120 g_return_val_if_fail (iter != NULL, FALSE);
1121 g_return_val_if_fail (str != NULL, FALSE);
1123 if (limit && gtk_text_iter_compare (iter, limit) <= 0)
1128 /* If we can move one char, return the empty string there */
1131 if (gtk_text_iter_backward_char (&match))
1133 if (limit && gtk_text_iter_equal (&match, limit))
1137 *match_start = match;
1148 visible_only = TRUE;
1151 /* locate all lines */
1152 lines = strbreakup (str, "\n", -1);
1158 /* This loop has an inefficient worst-case, where
1159 * gtk_text_iter_get_text () is called repeatedly on
1164 if (limit && gtk_text_iter_compare (&search, limit) <= 0)
1167 if (backward_lines_match (&search, (const gchar**)lines,
1168 visible_only, slice, &match, &end))
1170 if (limit == NULL || (limit &&
1171 gtk_text_iter_compare (&end, limit) > 0))
1176 *match_start = match;
1183 if (gtk_text_iter_get_line_offset (&search) == 0)
1185 if (!gtk_text_iter_backward_line (&search))
1190 gtk_text_iter_set_line_offset (&search, 0);
1194 g_strfreev ((gchar**)lines);
1200 empathy_window_get_is_visible (GtkWindow *window)
1202 GdkWindowState state;
1203 GdkWindow *gdk_window;
1205 g_return_val_if_fail (GTK_IS_WINDOW (window), FALSE);
1207 gdk_window = GTK_WIDGET (window)->window;
1212 state = gdk_window_get_state (gdk_window);
1213 if (state & (GDK_WINDOW_STATE_WITHDRAWN | GDK_WINDOW_STATE_ICONIFIED)) {
1221 empathy_window_iconify (GtkWindow *window, GtkStatusIcon *status_icon)
1223 GdkRectangle icon_location;
1226 GdkWindow *gdk_window;
1228 gtk_status_icon_get_geometry (status_icon, NULL, &icon_location, NULL);
1229 gdk_window = GTK_WIDGET (window)->window;
1230 dpy = gdk_x11_drawable_get_xdisplay (gdk_window);
1232 data[0] = icon_location.x;
1233 data[1] = icon_location.y;
1234 data[2] = icon_location.width;
1235 data[3] = icon_location.height;
1237 XChangeProperty (dpy,
1238 GDK_WINDOW_XID (gdk_window),
1239 gdk_x11_get_xatom_by_name_for_display (gdk_drawable_get_display (gdk_window),
1240 "_NET_WM_ICON_GEOMETRY"),
1241 XA_CARDINAL, 32, PropModeReplace,
1242 (guchar *)&data, 4);
1244 gtk_window_set_skip_taskbar_hint (window, TRUE);
1245 gtk_window_iconify (window);
1248 /* Takes care of moving the window to the current workspace. */
1250 empathy_window_present (GtkWindow *window,
1251 gboolean steal_focus)
1255 g_return_if_fail (GTK_IS_WINDOW (window));
1257 /* There are three cases: hidden, visible, visible on another
1261 if (!empathy_window_get_is_visible (window)) {
1262 /* Hide it so present brings it to the current workspace. */
1263 gtk_widget_hide (GTK_WIDGET (window));
1266 timestamp = gtk_get_current_event_time ();
1267 gtk_window_set_skip_taskbar_hint (window, FALSE);
1268 gtk_window_present_with_time (window, timestamp);
1269 /* FIXME: This shouldn't be required as gtk_window_present's doc says
1270 * it deiconify automatically. */
1271 gtk_window_deiconify (window);
1275 empathy_get_toplevel_window (GtkWidget *widget)
1277 GtkWidget *toplevel;
1279 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1281 toplevel = gtk_widget_get_toplevel (widget);
1282 if (GTK_IS_WINDOW (toplevel) &&
1283 GTK_WIDGET_TOPLEVEL (toplevel)) {
1284 return GTK_WINDOW (toplevel);
1290 /* The URL opening code can't handle schemeless strings, so we try to be
1291 * smart and add http if there is no scheme or doesn't look like a mail
1292 * address. This should work in most cases, and let us click on strings
1293 * like "www.gnome.org".
1296 fixup_url (const gchar *url)
1298 if (!g_str_has_prefix (url, "ghelp:") &&
1299 !strstr (url, ":/") &&
1300 !strstr (url, "@")) {
1301 return g_strdup_printf ("http://%s", url);
1308 empathy_url_show (const char *url)
1311 GError *error = NULL;
1313 real_url = fixup_url (url);
1318 /* FIXME: this does not work for multihead, we should use
1319 * GdkAppLaunchContext or gtk_show_url, see bug #514396.
1321 g_app_info_launch_default_for_uri (url, NULL, &error);
1325 dialog = gtk_message_dialog_new (NULL, 0,
1326 GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
1327 _("Unable to open URI"));
1328 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
1331 g_signal_connect (dialog, "response",
1332 G_CALLBACK (gtk_widget_destroy),
1334 gtk_window_present (GTK_WINDOW (dialog));
1336 g_clear_error (&error);
1343 link_button_hook (GtkLinkButton *button,
1347 empathy_url_show (link);
1351 empathy_link_button_new (const gchar *url,
1354 static gboolean hook = FALSE;
1358 gtk_link_button_set_uri_hook (link_button_hook, NULL, NULL);
1361 return gtk_link_button_new_with_label (url, title);
1365 empathy_toggle_button_set_state_quietly (GtkWidget *widget,
1370 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (widget));
1372 g_signal_handlers_block_by_func (widget, callback, user_data);
1373 g_object_set (widget, "active", active, NULL);
1374 g_signal_handlers_unblock_by_func (widget, callback, user_data);
1378 empathy_text_buffer_tag_set (GtkTextBuffer *buffer,
1379 const gchar *tag_name,
1380 const gchar *first_property_name,
1383 GtkTextTagTable *table;
1386 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
1387 g_return_val_if_fail (tag_name != NULL, NULL);
1389 table = gtk_text_buffer_get_tag_table (buffer);
1390 tag = gtk_text_tag_table_lookup (table, tag_name);
1393 tag = gtk_text_tag_new (tag_name);
1394 gtk_text_tag_table_add (table, tag);
1395 g_object_unref (tag);
1397 /* Clear the old values so that we don't affect the new theme. */
1399 "background-set", FALSE,
1400 "foreground-set", FALSE,
1401 "invisible-set", FALSE,
1402 "justification-set", FALSE,
1403 "paragraph-background-set", FALSE,
1404 "pixels-above-lines-set", FALSE,
1405 "pixels-below-lines-set", FALSE,
1410 "weight-set", FALSE,
1414 if (first_property_name) {
1417 va_start (list, first_property_name);
1418 g_object_set_valist (G_OBJECT (tag), first_property_name, list);