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,
66 GtkWidget **widget_ptr;
68 path = g_build_filename (g_getenv ("EMPATHY_SRCDIR"), "libempathy-gtk",
70 if (!g_file_test (path, G_FILE_TEST_EXISTS)) {
72 path = g_build_filename (DATADIR, "empathy", filename, NULL);
74 empathy_debug (DEBUG_DOMAIN, "Loading glade file %s", path);
76 gui = glade_xml_new (path, root, domain);
80 g_warning ("Couldn't find necessary glade file '%s'", filename);
83 for (name = first_required_widget; name; name = va_arg (args, char *)) {
84 widget_ptr = va_arg (args, void *);
86 *widget_ptr = glade_xml_get_widget (gui, name);
89 g_warning ("Glade file '%s' is missing widget '%s'.",
99 empathy_glade_get_file_simple (const gchar *filename,
102 const gchar *first_required_widget, ...)
107 va_start (args, first_required_widget);
109 gui = get_glade_file (filename,
112 first_required_widget,
118 g_object_unref (gui);
123 empathy_glade_get_file (const gchar *filename,
126 const gchar *first_required_widget, ...)
131 va_start (args, first_required_widget);
133 gui = get_glade_file (filename,
136 first_required_widget,
149 empathy_glade_connect (GladeXML *gui,
151 gchar *first_widget, ...)
159 va_start (args, first_widget);
161 for (name = first_widget; name; name = va_arg (args, char *)) {
162 signal = va_arg (args, void *);
163 callback = va_arg (args, void *);
165 widget = glade_xml_get_widget (gui, name);
167 g_warning ("Glade file is missing widget '%s', aborting",
172 g_signal_connect (widget,
174 G_CALLBACK (callback),
182 empathy_glade_setup_size_group (GladeXML *gui,
183 GtkSizeGroupMode mode,
184 gchar *first_widget, ...)
188 GtkSizeGroup *size_group;
191 va_start (args, first_widget);
193 size_group = gtk_size_group_new (mode);
195 for (name = first_widget; name; name = va_arg (args, char *)) {
196 widget = glade_xml_get_widget (gui, name);
198 g_warning ("Glade file is missing widget '%s'", name);
202 gtk_size_group_add_widget (size_group, widget);
205 g_object_unref (size_group);
211 empathy_icon_name_from_account (McAccount *account)
215 profile = mc_account_get_profile (account);
217 return mc_profile_get_icon_name (profile);
221 empathy_icon_name_for_presence (McPresence presence)
224 case MC_PRESENCE_AVAILABLE:
225 return EMPATHY_IMAGE_AVAILABLE;
226 case MC_PRESENCE_DO_NOT_DISTURB:
227 return EMPATHY_IMAGE_BUSY;
228 case MC_PRESENCE_AWAY:
229 return EMPATHY_IMAGE_AWAY;
230 case MC_PRESENCE_EXTENDED_AWAY:
231 return EMPATHY_IMAGE_EXT_AWAY;
232 case MC_PRESENCE_HIDDEN:
233 return EMPATHY_IMAGE_HIDDEN;
234 case MC_PRESENCE_OFFLINE:
235 case MC_PRESENCE_UNSET:
236 return EMPATHY_IMAGE_OFFLINE;
238 g_assert_not_reached ();
245 empathy_icon_name_for_contact (EmpathyContact *contact)
249 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact),
250 EMPATHY_IMAGE_OFFLINE);
252 presence = empathy_contact_get_presence (contact);
253 return empathy_icon_name_for_presence (presence);
257 empathy_pixbuf_from_data (gchar *data,
260 GdkPixbufLoader *loader;
261 GdkPixbuf *pixbuf = NULL;
262 GError *error = NULL;
268 loader = gdk_pixbuf_loader_new ();
269 if (!gdk_pixbuf_loader_write (loader, data, data_size, &error)) {
270 empathy_debug (DEBUG_DOMAIN, "Failed to write to pixbuf loader: %s",
271 error ? error->message : "No error given");
272 g_clear_error (&error);
273 g_object_unref (loader);
276 if (!gdk_pixbuf_loader_close (loader, &error)) {
277 empathy_debug (DEBUG_DOMAIN, "Failed to close pixbuf loader: %s",
278 error ? error->message : "No error given");
279 g_clear_error (&error);
280 g_object_unref (loader);
284 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
286 g_object_ref (pixbuf);
289 g_object_unref (loader);
295 pixbuf_from_avatar_size_prepared_cb (GdkPixbufLoader *loader,
298 struct SizeData *data)
300 g_return_if_fail (width > 0 && height > 0);
302 if (data->preserve_aspect_ratio && (data->width > 0 || data->height > 0)) {
303 if (width < data->width && height < data->height) {
308 if (data->width < 0) {
309 width = width * (double) data->height / (gdouble) height;
310 height = data->height;
311 } else if (data->height < 0) {
312 height = height * (double) data->width / (double) width;
314 } else if ((double) height * (double) data->width >
315 (double) width * (double) data->height) {
316 width = 0.5 + (double) width * (double) data->height / (double) height;
317 height = data->height;
319 height = 0.5 + (double) height * (double) data->width / (double) width;
323 if (data->width > 0) {
327 if (data->height > 0) {
328 height = data->height;
332 gdk_pixbuf_loader_set_size (loader, width, height);
336 empathy_avatar_pixbuf_roundify (GdkPixbuf *pixbuf)
338 gint width, height, rowstride;
341 width = gdk_pixbuf_get_width (pixbuf);
342 height = gdk_pixbuf_get_height (pixbuf);
343 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
344 pixels = gdk_pixbuf_get_pixels (pixbuf);
346 if (width < 6 || height < 6) {
354 pixels[rowstride + 3] = 0x80;
355 pixels[rowstride * 2 + 3] = 0xC0;
358 pixels[width * 4 - 1] = 0;
359 pixels[width * 4 - 5] = 0x80;
360 pixels[width * 4 - 9] = 0xC0;
361 pixels[rowstride + (width * 4) - 1] = 0x80;
362 pixels[(2 * rowstride) + (width * 4) - 1] = 0xC0;
365 pixels[(height - 1) * rowstride + 3] = 0;
366 pixels[(height - 1) * rowstride + 7] = 0x80;
367 pixels[(height - 1) * rowstride + 11] = 0xC0;
368 pixels[(height - 2) * rowstride + 3] = 0x80;
369 pixels[(height - 3) * rowstride + 3] = 0xC0;
372 pixels[height * rowstride - 1] = 0;
373 pixels[(height - 1) * rowstride - 1] = 0x80;
374 pixels[(height - 2) * rowstride - 1] = 0xC0;
375 pixels[height * rowstride - 5] = 0x80;
376 pixels[height * rowstride - 9] = 0xC0;
380 empathy_gdk_pixbuf_is_opaque (GdkPixbuf *pixbuf)
382 gint width, height, rowstride, i;
386 width = gdk_pixbuf_get_width (pixbuf);
387 height = gdk_pixbuf_get_height (pixbuf);
388 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
389 pixels = gdk_pixbuf_get_pixels (pixbuf);
392 for (i = 3; i < rowstride; i+=4) {
398 for (i = 1; i < height - 1; i++) {
399 row = pixels + (i*rowstride);
400 if (row[3] < 0xfe || row[rowstride-1] < 0xfe) {
405 row = pixels + ((height-1) * rowstride);
406 for (i = 3; i < rowstride; i+=4) {
416 empathy_pixbuf_from_avatar_scaled (EmpathyAvatar *avatar,
421 GdkPixbufLoader *loader;
422 struct SizeData data;
423 GError *error = NULL;
430 data.height = height;
431 data.preserve_aspect_ratio = TRUE;
433 loader = gdk_pixbuf_loader_new ();
435 g_signal_connect (loader, "size-prepared",
436 G_CALLBACK (pixbuf_from_avatar_size_prepared_cb),
439 if (!gdk_pixbuf_loader_write (loader, avatar->data, avatar->len, &error)) {
440 g_warning ("Couldn't write avatar image:%p with "
441 "length:%" G_GSIZE_FORMAT " to pixbuf loader: %s",
442 avatar->data, avatar->len, error->message);
443 g_error_free (error);
447 gdk_pixbuf_loader_close (loader, NULL);
449 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
450 if (!gdk_pixbuf_get_has_alpha (pixbuf)) {
451 GdkPixbuf *rounded_pixbuf;
453 rounded_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8,
454 gdk_pixbuf_get_width (pixbuf),
455 gdk_pixbuf_get_height (pixbuf));
456 gdk_pixbuf_copy_area (pixbuf, 0, 0,
457 gdk_pixbuf_get_width (pixbuf),
458 gdk_pixbuf_get_height (pixbuf),
461 pixbuf = rounded_pixbuf;
463 g_object_ref (pixbuf);
466 if (empathy_gdk_pixbuf_is_opaque (pixbuf)) {
467 empathy_avatar_pixbuf_roundify (pixbuf);
470 g_object_unref (loader);
476 empathy_pixbuf_avatar_from_contact_scaled (EmpathyContact *contact,
480 EmpathyAvatar *avatar;
482 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
484 avatar = empathy_contact_get_avatar (contact);
486 return empathy_pixbuf_from_avatar_scaled (avatar, width, height);
490 empathy_pixbuf_scale_down_if_necessary (GdkPixbuf *pixbuf, gint max_size)
495 width = gdk_pixbuf_get_width (pixbuf);
496 height = gdk_pixbuf_get_height (pixbuf);
498 if (width > max_size || height > max_size) {
499 factor = (gdouble) max_size / MAX (width, height);
501 width = width * factor;
502 height = height * factor;
504 return gdk_pixbuf_scale_simple (pixbuf,
509 return g_object_ref (pixbuf);
513 empathy_pixbuf_from_icon_name (const gchar *icon_name,
514 GtkIconSize icon_size)
517 GdkPixbuf *pixbuf = NULL;
518 GError *error = NULL;
526 theme = gtk_icon_theme_get_default ();
528 if (gtk_icon_size_lookup (icon_size, &w, &h)) {
532 pixbuf = gtk_icon_theme_load_icon (theme,
538 empathy_debug (DEBUG_DOMAIN, "Error loading icon: %s", error->message);
539 g_clear_error (&error);
545 /* Stolen from GtkSourceView, hence the weird intendation. Please keep it like
546 * that to make it easier to apply changes from the original code.
548 #define GTK_TEXT_UNKNOWN_CHAR 0xFFFC
550 /* this function acts like g_utf8_offset_to_pointer() except that if it finds a
551 * decomposable character it consumes the decomposition length from the given
552 * offset. So it's useful when the offset was calculated for the normalized
553 * version of str, but we need a pointer to str itself. */
555 pointer_from_offset_skipping_decomp (const gchar *str, gint offset)
557 gchar *casefold, *normal;
563 q = g_utf8_next_char (p);
564 casefold = g_utf8_casefold (p, q - p);
565 normal = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
566 offset -= g_utf8_strlen (normal, -1);
575 g_utf8_strcasestr (const gchar *haystack, const gchar *needle)
579 const gchar *ret = NULL;
582 gchar *caseless_haystack;
585 g_return_val_if_fail (haystack != NULL, NULL);
586 g_return_val_if_fail (needle != NULL, NULL);
588 casefold = g_utf8_casefold (haystack, -1);
589 caseless_haystack = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
592 needle_len = g_utf8_strlen (needle, -1);
593 haystack_len = g_utf8_strlen (caseless_haystack, -1);
597 ret = (gchar *)haystack;
601 if (haystack_len < needle_len)
607 p = (gchar*)caseless_haystack;
608 needle_len = strlen (needle);
613 if ((strncmp (p, needle, needle_len) == 0))
615 ret = pointer_from_offset_skipping_decomp (haystack, i);
619 p = g_utf8_next_char (p);
624 g_free (caseless_haystack);
630 g_utf8_caselessnmatch (const char *s1, const char *s2,
631 gssize n1, gssize n2)
634 gchar *normalized_s1;
635 gchar *normalized_s2;
638 gboolean ret = FALSE;
640 g_return_val_if_fail (s1 != NULL, FALSE);
641 g_return_val_if_fail (s2 != NULL, FALSE);
642 g_return_val_if_fail (n1 > 0, FALSE);
643 g_return_val_if_fail (n2 > 0, FALSE);
645 casefold = g_utf8_casefold (s1, n1);
646 normalized_s1 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
649 casefold = g_utf8_casefold (s2, n2);
650 normalized_s2 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
653 len_s1 = strlen (normalized_s1);
654 len_s2 = strlen (normalized_s2);
659 ret = (strncmp (normalized_s1, normalized_s2, len_s2) == 0);
662 g_free (normalized_s1);
663 g_free (normalized_s2);
669 forward_chars_with_skipping (GtkTextIter *iter,
671 gboolean skip_invisible,
672 gboolean skip_nontext,
673 gboolean skip_decomp)
677 g_return_if_fail (count >= 0);
683 gboolean ignored = FALSE;
685 /* minimal workaround to avoid the infinite loop of bug #168247.
686 * It doesn't fix the problemjust the symptom...
688 if (gtk_text_iter_is_end (iter))
691 if (skip_nontext && gtk_text_iter_get_char (iter) == GTK_TEXT_UNKNOWN_CHAR)
694 if (!ignored && skip_invisible &&
695 /* _gtk_text_btree_char_is_invisible (iter)*/ FALSE)
698 if (!ignored && skip_decomp)
700 /* being UTF8 correct sucks; this accounts for extra
701 offsets coming from canonical decompositions of
702 UTF8 characters (e.g. accented characters) which
703 g_utf8_normalize() performs */
708 buffer_len = g_unichar_to_utf8 (gtk_text_iter_get_char (iter), buffer);
709 normal = g_utf8_normalize (buffer, buffer_len, G_NORMALIZE_NFD);
710 i -= (g_utf8_strlen (normal, -1) - 1);
714 gtk_text_iter_forward_char (iter);
722 lines_match (const GtkTextIter *start,
724 gboolean visible_only,
726 GtkTextIter *match_start,
727 GtkTextIter *match_end)
734 if (*lines == NULL || **lines == '\0')
737 *match_start = *start;
744 gtk_text_iter_forward_line (&next);
746 /* No more text in buffer, but *lines is nonempty */
747 if (gtk_text_iter_equal (start, &next))
753 line_text = gtk_text_iter_get_visible_slice (start, &next);
755 line_text = gtk_text_iter_get_slice (start, &next);
760 line_text = gtk_text_iter_get_visible_text (start, &next);
762 line_text = gtk_text_iter_get_text (start, &next);
765 if (match_start) /* if this is the first line we're matching */
767 found = g_utf8_strcasestr (line_text, *lines);
771 /* If it's not the first line, we have to match from the
774 if (g_utf8_caselessnmatch (line_text, *lines, strlen (line_text),
787 /* Get offset to start of search string */
788 offset = g_utf8_strlen (line_text, found - line_text);
792 /* If match start needs to be returned, set it to the
793 * start of the search string.
795 forward_chars_with_skipping (&next, offset, visible_only, !slice, FALSE);
801 /* Go to end of search string */
802 forward_chars_with_skipping (&next, g_utf8_strlen (*lines, -1), visible_only, !slice, TRUE);
811 /* pass NULL for match_start, since we don't need to find the
814 return lines_match (&next, lines, visible_only, slice, NULL, match_end);
817 /* strsplit () that retains the delimiter as part of the string. */
819 strbreakup (const char *string,
820 const char *delimiter,
823 GSList *string_list = NULL, *slist;
824 gchar **str_array, *s, *casefold, *new_string;
827 g_return_val_if_fail (string != NULL, NULL);
828 g_return_val_if_fail (delimiter != NULL, NULL);
831 max_tokens = G_MAXINT;
833 s = strstr (string, delimiter);
836 guint delimiter_len = strlen (delimiter);
842 len = s - string + delimiter_len;
843 new_string = g_new (gchar, len + 1);
844 strncpy (new_string, string, len);
846 casefold = g_utf8_casefold (new_string, -1);
848 new_string = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
850 string_list = g_slist_prepend (string_list, new_string);
852 string = s + delimiter_len;
853 s = strstr (string, delimiter);
854 } while (--max_tokens && s);
860 casefold = g_utf8_casefold (string, -1);
861 new_string = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
863 string_list = g_slist_prepend (string_list, new_string);
866 str_array = g_new (gchar*, n);
870 str_array[i--] = NULL;
871 for (slist = string_list; slist; slist = slist->next)
872 str_array[i--] = slist->data;
874 g_slist_free (string_list);
880 empathy_text_iter_forward_search (const GtkTextIter *iter,
882 GtkTextIter *match_start,
883 GtkTextIter *match_end,
884 const GtkTextIter *limit)
886 gchar **lines = NULL;
888 gboolean retval = FALSE;
890 gboolean visible_only;
893 g_return_val_if_fail (iter != NULL, FALSE);
894 g_return_val_if_fail (str != NULL, FALSE);
896 if (limit && gtk_text_iter_compare (iter, limit) >= 0)
900 /* If we can move one char, return the empty string there */
903 if (gtk_text_iter_forward_char (&match)) {
904 if (limit && gtk_text_iter_equal (&match, limit)) {
909 *match_start = match;
923 /* locate all lines */
924 lines = strbreakup (str, "\n", -1);
929 /* This loop has an inefficient worst-case, where
930 * gtk_text_iter_get_text () is called repeatedly on
935 if (limit && gtk_text_iter_compare (&search, limit) >= 0) {
939 if (lines_match (&search, (const gchar**)lines,
940 visible_only, slice, &match, &end)) {
942 (limit && gtk_text_iter_compare (&end, limit) <= 0)) {
946 *match_start = match;
954 } while (gtk_text_iter_forward_line (&search));
956 g_strfreev ((gchar**)lines);
962 g_utf8_strrcasestr (const gchar *haystack, const gchar *needle)
966 const gchar *ret = NULL;
969 gchar *caseless_haystack;
972 g_return_val_if_fail (haystack != NULL, NULL);
973 g_return_val_if_fail (needle != NULL, NULL);
975 casefold = g_utf8_casefold (haystack, -1);
976 caseless_haystack = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
979 needle_len = g_utf8_strlen (needle, -1);
980 haystack_len = g_utf8_strlen (caseless_haystack, -1);
984 ret = (gchar *)haystack;
988 if (haystack_len < needle_len)
994 i = haystack_len - needle_len;
995 p = g_utf8_offset_to_pointer (caseless_haystack, i);
996 needle_len = strlen (needle);
998 while (p >= caseless_haystack)
1000 if (strncmp (p, needle, needle_len) == 0)
1002 ret = pointer_from_offset_skipping_decomp (haystack, i);
1006 p = g_utf8_prev_char (p);
1011 g_free (caseless_haystack);
1017 backward_lines_match (const GtkTextIter *start,
1018 const gchar **lines,
1019 gboolean visible_only,
1021 GtkTextIter *match_start,
1022 GtkTextIter *match_end)
1024 GtkTextIter line, next;
1029 if (*lines == NULL || **lines == '\0')
1032 *match_start = *start;
1034 *match_end = *start;
1038 line = next = *start;
1039 if (gtk_text_iter_get_line_offset (&next) == 0)
1041 if (!gtk_text_iter_backward_line (&next))
1045 gtk_text_iter_set_line_offset (&next, 0);
1050 line_text = gtk_text_iter_get_visible_slice (&next, &line);
1052 line_text = gtk_text_iter_get_slice (&next, &line);
1057 line_text = gtk_text_iter_get_visible_text (&next, &line);
1059 line_text = gtk_text_iter_get_text (&next, &line);
1062 if (match_start) /* if this is the first line we're matching */
1064 found = g_utf8_strrcasestr (line_text, *lines);
1068 /* If it's not the first line, we have to match from the
1069 * start of the line.
1071 if (g_utf8_caselessnmatch (line_text, *lines, strlen (line_text),
1084 /* Get offset to start of search string */
1085 offset = g_utf8_strlen (line_text, found - line_text);
1087 forward_chars_with_skipping (&next, offset, visible_only, !slice, FALSE);
1089 /* If match start needs to be returned, set it to the
1090 * start of the search string.
1094 *match_start = next;
1097 /* Go to end of search string */
1098 forward_chars_with_skipping (&next, g_utf8_strlen (*lines, -1), visible_only, !slice, TRUE);
1107 /* try to match the rest of the lines forward, passing NULL
1108 * for match_start so lines_match will try to match the entire
1110 return lines_match (&next, lines, visible_only,
1111 slice, NULL, match_end);
1115 empathy_text_iter_backward_search (const GtkTextIter *iter,
1117 GtkTextIter *match_start,
1118 GtkTextIter *match_end,
1119 const GtkTextIter *limit)
1121 gchar **lines = NULL;
1123 gboolean retval = FALSE;
1125 gboolean visible_only;
1128 g_return_val_if_fail (iter != NULL, FALSE);
1129 g_return_val_if_fail (str != NULL, FALSE);
1131 if (limit && gtk_text_iter_compare (iter, limit) <= 0)
1136 /* If we can move one char, return the empty string there */
1139 if (gtk_text_iter_backward_char (&match))
1141 if (limit && gtk_text_iter_equal (&match, limit))
1145 *match_start = match;
1156 visible_only = TRUE;
1159 /* locate all lines */
1160 lines = strbreakup (str, "\n", -1);
1166 /* This loop has an inefficient worst-case, where
1167 * gtk_text_iter_get_text () is called repeatedly on
1172 if (limit && gtk_text_iter_compare (&search, limit) <= 0)
1175 if (backward_lines_match (&search, (const gchar**)lines,
1176 visible_only, slice, &match, &end))
1178 if (limit == NULL || (limit &&
1179 gtk_text_iter_compare (&end, limit) > 0))
1184 *match_start = match;
1191 if (gtk_text_iter_get_line_offset (&search) == 0)
1193 if (!gtk_text_iter_backward_line (&search))
1198 gtk_text_iter_set_line_offset (&search, 0);
1202 g_strfreev ((gchar**)lines);
1208 empathy_window_get_is_visible (GtkWindow *window)
1210 GdkWindowState state;
1211 GdkWindow *gdk_window;
1213 g_return_val_if_fail (GTK_IS_WINDOW (window), FALSE);
1215 gdk_window = GTK_WIDGET (window)->window;
1220 state = gdk_window_get_state (gdk_window);
1221 if (state & (GDK_WINDOW_STATE_WITHDRAWN | GDK_WINDOW_STATE_ICONIFIED)) {
1229 empathy_window_iconify (GtkWindow *window, GtkStatusIcon *status_icon)
1231 GdkRectangle icon_location;
1234 GdkWindow *gdk_window;
1236 gtk_status_icon_get_geometry (status_icon, NULL, &icon_location, NULL);
1237 gdk_window = GTK_WIDGET (window)->window;
1238 dpy = gdk_x11_drawable_get_xdisplay (gdk_window);
1240 data[0] = icon_location.x;
1241 data[1] = icon_location.y;
1242 data[2] = icon_location.width;
1243 data[3] = icon_location.height;
1245 XChangeProperty (dpy,
1246 GDK_WINDOW_XID (gdk_window),
1247 gdk_x11_get_xatom_by_name_for_display (gdk_drawable_get_display (gdk_window),
1248 "_NET_WM_ICON_GEOMETRY"),
1249 XA_CARDINAL, 32, PropModeReplace,
1250 (guchar *)&data, 4);
1252 gtk_window_set_skip_taskbar_hint (window, TRUE);
1253 gtk_window_iconify (window);
1256 /* Takes care of moving the window to the current workspace. */
1258 empathy_window_present (GtkWindow *window,
1259 gboolean steal_focus)
1263 g_return_if_fail (GTK_IS_WINDOW (window));
1265 /* There are three cases: hidden, visible, visible on another
1269 if (!empathy_window_get_is_visible (window)) {
1270 /* Hide it so present brings it to the current workspace. */
1271 gtk_widget_hide (GTK_WIDGET (window));
1274 timestamp = gtk_get_current_event_time ();
1275 gtk_window_set_skip_taskbar_hint (window, FALSE);
1276 gtk_window_present_with_time (window, timestamp);
1277 /* FIXME: This shouldn't be required as gtk_window_present's doc says
1278 * it deiconify automatically. */
1279 gtk_window_deiconify (window);
1283 empathy_get_toplevel_window (GtkWidget *widget)
1285 GtkWidget *toplevel;
1287 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1289 toplevel = gtk_widget_get_toplevel (widget);
1290 if (GTK_IS_WINDOW (toplevel) &&
1291 GTK_WIDGET_TOPLEVEL (toplevel)) {
1292 return GTK_WINDOW (toplevel);
1298 /* The URL opening code can't handle schemeless strings, so we try to be
1299 * smart and add http if there is no scheme or doesn't look like a mail
1300 * address. This should work in most cases, and let us click on strings
1301 * like "www.gnome.org".
1304 fixup_url (const gchar *url)
1306 if (!g_str_has_prefix (url, "http://") &&
1307 !strstr (url, ":/") &&
1308 !strstr (url, "@")) {
1309 return g_strdup_printf ("http://%s", url);
1316 empathy_url_show (const char *url)
1320 GError *error = NULL;
1322 real_url = fixup_url (url);
1326 /* FIXME: this does not work for multihead, we should use
1327 * GdkAppLaunchContext for that, when we can depend on GTK+ trunk
1329 res = g_app_info_launch_default_for_uri (url, NULL, &error);
1331 empathy_debug (DEBUG_DOMAIN, "Couldn't show URL %s: %s",
1332 url, error->message);
1333 g_clear_error (&error);
1340 link_button_hook (GtkLinkButton *button,
1344 empathy_url_show (link);
1348 empathy_link_button_new (const gchar *url,
1351 static gboolean hook = FALSE;
1355 gtk_link_button_set_uri_hook (link_button_hook, NULL, NULL);
1358 return gtk_link_button_new_with_label (url, title);
1362 empathy_toggle_button_set_state_quietly (GtkWidget *widget,
1367 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (widget));
1369 g_signal_handlers_block_by_func (widget, callback, user_data);
1370 g_object_set (widget, "active", active, NULL);
1371 g_signal_handlers_unblock_by_func (widget, callback, user_data);
1375 empathy_text_buffer_tag_set (GtkTextBuffer *buffer,
1376 const gchar *tag_name,
1377 const gchar *first_property_name,
1380 GtkTextTagTable *table;
1383 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
1384 g_return_val_if_fail (tag_name != NULL, NULL);
1386 table = gtk_text_buffer_get_tag_table (buffer);
1387 tag = gtk_text_tag_table_lookup (table, tag_name);
1390 tag = gtk_text_tag_new (tag_name);
1391 gtk_text_tag_table_add (table, tag);
1392 g_object_unref (tag);
1394 /* Clear the old values so that we don't affect the new theme. */
1396 "background-set", FALSE,
1397 "foreground-set", FALSE,
1398 "invisible-set", FALSE,
1399 "justification-set", FALSE,
1400 "paragraph-background-set", FALSE,
1401 "pixels-above-lines-set", FALSE,
1402 "pixels-below-lines-set", FALSE,
1407 "weight-set", FALSE,
1411 if (first_property_name) {
1414 va_start (list, first_property_name);
1415 g_object_set_valist (G_OBJECT (tag), first_property_name, list);