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 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>
38 #include <glade/glade.h>
39 #include <libgnomevfs/gnome-vfs-utils.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 (UNINSTALLED_GLADE_DIR, filename, NULL);
69 if (!g_file_test (path, G_FILE_TEST_EXISTS)) {
71 path = g_build_filename (DATADIR, "empathy", filename, NULL);
73 empathy_debug (DEBUG_DOMAIN, "Loading glade file %s", path);
75 gui = glade_xml_new (path, root, domain);
79 g_warning ("Couldn't find necessary glade file '%s'", filename);
82 for (name = first_required_widget; name; name = va_arg (args, char *)) {
83 widget_ptr = va_arg (args, void *);
85 *widget_ptr = glade_xml_get_widget (gui, name);
88 g_warning ("Glade file '%s' is missing widget '%s'.",
98 empathy_glade_get_file_simple (const gchar *filename,
101 const gchar *first_required_widget, ...)
106 va_start (args, first_required_widget);
108 gui = get_glade_file (filename,
111 first_required_widget,
117 g_object_unref (gui);
122 empathy_glade_get_file (const gchar *filename,
125 const gchar *first_required_widget, ...)
130 va_start (args, first_required_widget);
132 gui = get_glade_file (filename,
135 first_required_widget,
148 empathy_glade_connect (GladeXML *gui,
150 gchar *first_widget, ...)
158 va_start (args, first_widget);
160 for (name = first_widget; name; name = va_arg (args, char *)) {
161 signal = va_arg (args, void *);
162 callback = va_arg (args, void *);
164 widget = glade_xml_get_widget (gui, name);
166 g_warning ("Glade file is missing widget '%s', aborting",
171 g_signal_connect (widget,
173 G_CALLBACK (callback),
181 empathy_glade_setup_size_group (GladeXML *gui,
182 GtkSizeGroupMode mode,
183 gchar *first_widget, ...)
187 GtkSizeGroup *size_group;
190 va_start (args, first_widget);
192 size_group = gtk_size_group_new (mode);
194 for (name = first_widget; name; name = va_arg (args, char *)) {
195 widget = glade_xml_get_widget (gui, name);
197 g_warning ("Glade file is missing widget '%s'", name);
201 gtk_size_group_add_widget (size_group, widget);
204 g_object_unref (size_group);
210 empathy_icon_name_from_account (McAccount *account)
214 profile = mc_account_get_profile (account);
216 return mc_profile_get_icon_name (profile);
220 empathy_icon_name_for_presence (McPresence presence)
223 case MC_PRESENCE_AVAILABLE:
224 return EMPATHY_IMAGE_AVAILABLE;
225 case MC_PRESENCE_DO_NOT_DISTURB:
226 return EMPATHY_IMAGE_BUSY;
227 case MC_PRESENCE_AWAY:
228 return EMPATHY_IMAGE_AWAY;
229 case MC_PRESENCE_EXTENDED_AWAY:
230 return EMPATHY_IMAGE_EXT_AWAY;
231 case MC_PRESENCE_HIDDEN:
232 return EMPATHY_IMAGE_HIDDEN;
233 case MC_PRESENCE_OFFLINE:
234 case MC_PRESENCE_UNSET:
235 return EMPATHY_IMAGE_OFFLINE;
237 g_assert_not_reached ();
244 empathy_icon_name_for_contact (EmpathyContact *contact)
248 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact),
249 EMPATHY_IMAGE_OFFLINE);
251 presence = empathy_contact_get_presence (contact);
252 return empathy_icon_name_for_presence (presence);
256 empathy_pixbuf_from_data (gchar *data,
259 GdkPixbufLoader *loader;
260 GdkPixbuf *pixbuf = NULL;
261 GError *error = NULL;
267 loader = gdk_pixbuf_loader_new ();
268 if (!gdk_pixbuf_loader_write (loader, data, data_size, &error)) {
269 empathy_debug (DEBUG_DOMAIN, "Failed to write to pixbuf loader: %s",
270 error ? error->message : "No error given");
271 g_clear_error (&error);
272 g_object_unref (loader);
275 if (!gdk_pixbuf_loader_close (loader, &error)) {
276 empathy_debug (DEBUG_DOMAIN, "Failed to close pixbuf loader: %s",
277 error ? error->message : "No error given");
278 g_clear_error (&error);
279 g_object_unref (loader);
283 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
285 g_object_ref (pixbuf);
288 g_object_unref (loader);
294 pixbuf_from_avatar_size_prepared_cb (GdkPixbufLoader *loader,
297 struct SizeData *data)
299 g_return_if_fail (width > 0 && height > 0);
301 if (data->preserve_aspect_ratio && (data->width > 0 || data->height > 0)) {
302 if (width < data->width && height < data->height) {
307 if (data->width < 0) {
308 width = width * (double) data->height / (gdouble) height;
309 height = data->height;
310 } else if (data->height < 0) {
311 height = height * (double) data->width / (double) width;
313 } else if ((double) height * (double) data->width >
314 (double) width * (double) data->height) {
315 width = 0.5 + (double) width * (double) data->height / (double) height;
316 height = data->height;
318 height = 0.5 + (double) height * (double) data->width / (double) width;
322 if (data->width > 0) {
326 if (data->height > 0) {
327 height = data->height;
331 gdk_pixbuf_loader_set_size (loader, width, height);
335 empathy_avatar_pixbuf_roundify (GdkPixbuf *pixbuf)
337 gint width, height, rowstride;
340 width = gdk_pixbuf_get_width (pixbuf);
341 height = gdk_pixbuf_get_height (pixbuf);
342 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
343 pixels = gdk_pixbuf_get_pixels (pixbuf);
345 if (width < 6 || height < 6) {
353 pixels[rowstride + 3] = 0x80;
354 pixels[rowstride * 2 + 3] = 0xC0;
357 pixels[width * 4 - 1] = 0;
358 pixels[width * 4 - 5] = 0x80;
359 pixels[width * 4 - 9] = 0xC0;
360 pixels[rowstride + (width * 4) - 1] = 0x80;
361 pixels[(2 * rowstride) + (width * 4) - 1] = 0xC0;
364 pixels[(height - 1) * rowstride + 3] = 0;
365 pixels[(height - 1) * rowstride + 7] = 0x80;
366 pixels[(height - 1) * rowstride + 11] = 0xC0;
367 pixels[(height - 2) * rowstride + 3] = 0x80;
368 pixels[(height - 3) * rowstride + 3] = 0xC0;
371 pixels[height * rowstride - 1] = 0;
372 pixels[(height - 1) * rowstride - 1] = 0x80;
373 pixels[(height - 2) * rowstride - 1] = 0xC0;
374 pixels[height * rowstride - 5] = 0x80;
375 pixels[height * rowstride - 9] = 0xC0;
379 empathy_gdk_pixbuf_is_opaque (GdkPixbuf *pixbuf)
381 gint width, height, rowstride, i;
385 width = gdk_pixbuf_get_width (pixbuf);
386 height = gdk_pixbuf_get_height (pixbuf);
387 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
388 pixels = gdk_pixbuf_get_pixels (pixbuf);
391 for (i = 3; i < rowstride; i+=4) {
397 for (i = 1; i < height - 1; i++) {
398 row = pixels + (i*rowstride);
399 if (row[3] < 0xfe || row[rowstride-1] < 0xfe) {
404 row = pixels + ((height-1) * rowstride);
405 for (i = 3; i < rowstride; i+=4) {
415 empathy_pixbuf_from_avatar_scaled (EmpathyAvatar *avatar,
420 GdkPixbufLoader *loader;
421 struct SizeData data;
422 GError *error = NULL;
429 data.height = height;
430 data.preserve_aspect_ratio = TRUE;
432 loader = gdk_pixbuf_loader_new ();
434 g_signal_connect (loader, "size-prepared",
435 G_CALLBACK (pixbuf_from_avatar_size_prepared_cb),
438 if (!gdk_pixbuf_loader_write (loader, avatar->data, avatar->len, &error)) {
439 g_warning ("Couldn't write avatar image:%p with "
440 "length:%" G_GSIZE_FORMAT " to pixbuf loader: %s",
441 avatar->data, avatar->len, error->message);
442 g_error_free (error);
446 gdk_pixbuf_loader_close (loader, NULL);
448 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
449 if (!gdk_pixbuf_get_has_alpha (pixbuf)) {
450 GdkPixbuf *rounded_pixbuf;
452 rounded_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8,
453 gdk_pixbuf_get_width (pixbuf),
454 gdk_pixbuf_get_height (pixbuf));
455 gdk_pixbuf_copy_area (pixbuf, 0, 0,
456 gdk_pixbuf_get_width (pixbuf),
457 gdk_pixbuf_get_height (pixbuf),
460 pixbuf = rounded_pixbuf;
462 g_object_ref (pixbuf);
465 if (empathy_gdk_pixbuf_is_opaque (pixbuf)) {
466 empathy_avatar_pixbuf_roundify (pixbuf);
469 g_object_unref (loader);
475 empathy_pixbuf_avatar_from_contact_scaled (EmpathyContact *contact,
479 EmpathyAvatar *avatar;
481 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
483 avatar = empathy_contact_get_avatar (contact);
485 return empathy_pixbuf_from_avatar_scaled (avatar, width, height);
489 empathy_pixbuf_scale_down_if_necessary (GdkPixbuf *pixbuf, gint max_size)
494 width = gdk_pixbuf_get_width (pixbuf);
495 height = gdk_pixbuf_get_height (pixbuf);
497 if (width > max_size || height > max_size) {
498 factor = (gdouble) max_size / MAX (width, height);
500 width = width * factor;
501 height = height * factor;
503 return gdk_pixbuf_scale_simple (pixbuf,
508 return g_object_ref (pixbuf);
512 empathy_pixbuf_from_icon_name (const gchar *icon_name,
513 GtkIconSize icon_size)
516 GdkPixbuf *pixbuf = NULL;
517 GError *error = NULL;
525 theme = gtk_icon_theme_get_default ();
527 if (gtk_icon_size_lookup (icon_size, &w, &h)) {
531 pixbuf = gtk_icon_theme_load_icon (theme,
537 empathy_debug (DEBUG_DOMAIN, "Error loading icon: %s", error->message);
538 g_clear_error (&error);
544 /* Stolen from GtkSourceView, hence the weird intendation. Please keep it like
545 * that to make it easier to apply changes from the original code.
547 #define GTK_TEXT_UNKNOWN_CHAR 0xFFFC
549 /* this function acts like g_utf8_offset_to_pointer() except that if it finds a
550 * decomposable character it consumes the decomposition length from the given
551 * offset. So it's useful when the offset was calculated for the normalized
552 * version of str, but we need a pointer to str itself. */
554 pointer_from_offset_skipping_decomp (const gchar *str, gint offset)
556 gchar *casefold, *normal;
562 q = g_utf8_next_char (p);
563 casefold = g_utf8_casefold (p, q - p);
564 normal = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
565 offset -= g_utf8_strlen (normal, -1);
574 g_utf8_strcasestr (const gchar *haystack, const gchar *needle)
578 const gchar *ret = NULL;
581 gchar *caseless_haystack;
584 g_return_val_if_fail (haystack != NULL, NULL);
585 g_return_val_if_fail (needle != NULL, NULL);
587 casefold = g_utf8_casefold (haystack, -1);
588 caseless_haystack = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
591 needle_len = g_utf8_strlen (needle, -1);
592 haystack_len = g_utf8_strlen (caseless_haystack, -1);
596 ret = (gchar *)haystack;
600 if (haystack_len < needle_len)
606 p = (gchar*)caseless_haystack;
607 needle_len = strlen (needle);
612 if ((strncmp (p, needle, needle_len) == 0))
614 ret = pointer_from_offset_skipping_decomp (haystack, i);
618 p = g_utf8_next_char (p);
623 g_free (caseless_haystack);
629 g_utf8_caselessnmatch (const char *s1, const char *s2,
630 gssize n1, gssize n2)
633 gchar *normalized_s1;
634 gchar *normalized_s2;
637 gboolean ret = FALSE;
639 g_return_val_if_fail (s1 != NULL, FALSE);
640 g_return_val_if_fail (s2 != NULL, FALSE);
641 g_return_val_if_fail (n1 > 0, FALSE);
642 g_return_val_if_fail (n2 > 0, FALSE);
644 casefold = g_utf8_casefold (s1, n1);
645 normalized_s1 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
648 casefold = g_utf8_casefold (s2, n2);
649 normalized_s2 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
652 len_s1 = strlen (normalized_s1);
653 len_s2 = strlen (normalized_s2);
658 ret = (strncmp (normalized_s1, normalized_s2, len_s2) == 0);
661 g_free (normalized_s1);
662 g_free (normalized_s2);
668 forward_chars_with_skipping (GtkTextIter *iter,
670 gboolean skip_invisible,
671 gboolean skip_nontext,
672 gboolean skip_decomp)
676 g_return_if_fail (count >= 0);
682 gboolean ignored = FALSE;
684 /* minimal workaround to avoid the infinite loop of bug #168247.
685 * It doesn't fix the problemjust the symptom...
687 if (gtk_text_iter_is_end (iter))
690 if (skip_nontext && gtk_text_iter_get_char (iter) == GTK_TEXT_UNKNOWN_CHAR)
693 if (!ignored && skip_invisible &&
694 /* _gtk_text_btree_char_is_invisible (iter)*/ FALSE)
697 if (!ignored && skip_decomp)
699 /* being UTF8 correct sucks; this accounts for extra
700 offsets coming from canonical decompositions of
701 UTF8 characters (e.g. accented characters) which
702 g_utf8_normalize() performs */
707 buffer_len = g_unichar_to_utf8 (gtk_text_iter_get_char (iter), buffer);
708 normal = g_utf8_normalize (buffer, buffer_len, G_NORMALIZE_NFD);
709 i -= (g_utf8_strlen (normal, -1) - 1);
713 gtk_text_iter_forward_char (iter);
721 lines_match (const GtkTextIter *start,
723 gboolean visible_only,
725 GtkTextIter *match_start,
726 GtkTextIter *match_end)
733 if (*lines == NULL || **lines == '\0')
736 *match_start = *start;
743 gtk_text_iter_forward_line (&next);
745 /* No more text in buffer, but *lines is nonempty */
746 if (gtk_text_iter_equal (start, &next))
752 line_text = gtk_text_iter_get_visible_slice (start, &next);
754 line_text = gtk_text_iter_get_slice (start, &next);
759 line_text = gtk_text_iter_get_visible_text (start, &next);
761 line_text = gtk_text_iter_get_text (start, &next);
764 if (match_start) /* if this is the first line we're matching */
766 found = g_utf8_strcasestr (line_text, *lines);
770 /* If it's not the first line, we have to match from the
773 if (g_utf8_caselessnmatch (line_text, *lines, strlen (line_text),
786 /* Get offset to start of search string */
787 offset = g_utf8_strlen (line_text, found - line_text);
791 /* If match start needs to be returned, set it to the
792 * start of the search string.
794 forward_chars_with_skipping (&next, offset, visible_only, !slice, FALSE);
800 /* Go to end of search string */
801 forward_chars_with_skipping (&next, g_utf8_strlen (*lines, -1), visible_only, !slice, TRUE);
810 /* pass NULL for match_start, since we don't need to find the
813 return lines_match (&next, lines, visible_only, slice, NULL, match_end);
816 /* strsplit () that retains the delimiter as part of the string. */
818 strbreakup (const char *string,
819 const char *delimiter,
822 GSList *string_list = NULL, *slist;
823 gchar **str_array, *s, *casefold, *new_string;
826 g_return_val_if_fail (string != NULL, NULL);
827 g_return_val_if_fail (delimiter != NULL, NULL);
830 max_tokens = G_MAXINT;
832 s = strstr (string, delimiter);
835 guint delimiter_len = strlen (delimiter);
841 len = s - string + delimiter_len;
842 new_string = g_new (gchar, len + 1);
843 strncpy (new_string, string, len);
845 casefold = g_utf8_casefold (new_string, -1);
847 new_string = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
849 string_list = g_slist_prepend (string_list, new_string);
851 string = s + delimiter_len;
852 s = strstr (string, delimiter);
853 } while (--max_tokens && s);
859 casefold = g_utf8_casefold (string, -1);
860 new_string = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
862 string_list = g_slist_prepend (string_list, new_string);
865 str_array = g_new (gchar*, n);
869 str_array[i--] = NULL;
870 for (slist = string_list; slist; slist = slist->next)
871 str_array[i--] = slist->data;
873 g_slist_free (string_list);
879 empathy_text_iter_forward_search (const GtkTextIter *iter,
881 GtkTextIter *match_start,
882 GtkTextIter *match_end,
883 const GtkTextIter *limit)
885 gchar **lines = NULL;
887 gboolean retval = FALSE;
889 gboolean visible_only;
892 g_return_val_if_fail (iter != NULL, FALSE);
893 g_return_val_if_fail (str != NULL, FALSE);
895 if (limit && gtk_text_iter_compare (iter, limit) >= 0)
899 /* If we can move one char, return the empty string there */
902 if (gtk_text_iter_forward_char (&match)) {
903 if (limit && gtk_text_iter_equal (&match, limit)) {
908 *match_start = match;
922 /* locate all lines */
923 lines = strbreakup (str, "\n", -1);
928 /* This loop has an inefficient worst-case, where
929 * gtk_text_iter_get_text () is called repeatedly on
934 if (limit && gtk_text_iter_compare (&search, limit) >= 0) {
938 if (lines_match (&search, (const gchar**)lines,
939 visible_only, slice, &match, &end)) {
941 (limit && gtk_text_iter_compare (&end, limit) <= 0)) {
945 *match_start = match;
953 } while (gtk_text_iter_forward_line (&search));
955 g_strfreev ((gchar**)lines);
961 g_utf8_strrcasestr (const gchar *haystack, const gchar *needle)
965 const gchar *ret = NULL;
968 gchar *caseless_haystack;
971 g_return_val_if_fail (haystack != NULL, NULL);
972 g_return_val_if_fail (needle != NULL, NULL);
974 casefold = g_utf8_casefold (haystack, -1);
975 caseless_haystack = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
978 needle_len = g_utf8_strlen (needle, -1);
979 haystack_len = g_utf8_strlen (caseless_haystack, -1);
983 ret = (gchar *)haystack;
987 if (haystack_len < needle_len)
993 i = haystack_len - needle_len;
994 p = g_utf8_offset_to_pointer (caseless_haystack, i);
995 needle_len = strlen (needle);
997 while (p >= caseless_haystack)
999 if (strncmp (p, needle, needle_len) == 0)
1001 ret = pointer_from_offset_skipping_decomp (haystack, i);
1005 p = g_utf8_prev_char (p);
1010 g_free (caseless_haystack);
1016 backward_lines_match (const GtkTextIter *start,
1017 const gchar **lines,
1018 gboolean visible_only,
1020 GtkTextIter *match_start,
1021 GtkTextIter *match_end)
1023 GtkTextIter line, next;
1028 if (*lines == NULL || **lines == '\0')
1031 *match_start = *start;
1033 *match_end = *start;
1037 line = next = *start;
1038 if (gtk_text_iter_get_line_offset (&next) == 0)
1040 if (!gtk_text_iter_backward_line (&next))
1044 gtk_text_iter_set_line_offset (&next, 0);
1049 line_text = gtk_text_iter_get_visible_slice (&next, &line);
1051 line_text = gtk_text_iter_get_slice (&next, &line);
1056 line_text = gtk_text_iter_get_visible_text (&next, &line);
1058 line_text = gtk_text_iter_get_text (&next, &line);
1061 if (match_start) /* if this is the first line we're matching */
1063 found = g_utf8_strrcasestr (line_text, *lines);
1067 /* If it's not the first line, we have to match from the
1068 * start of the line.
1070 if (g_utf8_caselessnmatch (line_text, *lines, strlen (line_text),
1083 /* Get offset to start of search string */
1084 offset = g_utf8_strlen (line_text, found - line_text);
1086 forward_chars_with_skipping (&next, offset, visible_only, !slice, FALSE);
1088 /* If match start needs to be returned, set it to the
1089 * start of the search string.
1093 *match_start = next;
1096 /* Go to end of search string */
1097 forward_chars_with_skipping (&next, g_utf8_strlen (*lines, -1), visible_only, !slice, TRUE);
1106 /* try to match the rest of the lines forward, passing NULL
1107 * for match_start so lines_match will try to match the entire
1109 return lines_match (&next, lines, visible_only,
1110 slice, NULL, match_end);
1114 empathy_text_iter_backward_search (const GtkTextIter *iter,
1116 GtkTextIter *match_start,
1117 GtkTextIter *match_end,
1118 const GtkTextIter *limit)
1120 gchar **lines = NULL;
1122 gboolean retval = FALSE;
1124 gboolean visible_only;
1127 g_return_val_if_fail (iter != NULL, FALSE);
1128 g_return_val_if_fail (str != NULL, FALSE);
1130 if (limit && gtk_text_iter_compare (iter, limit) <= 0)
1135 /* If we can move one char, return the empty string there */
1138 if (gtk_text_iter_backward_char (&match))
1140 if (limit && gtk_text_iter_equal (&match, limit))
1144 *match_start = match;
1155 visible_only = TRUE;
1158 /* locate all lines */
1159 lines = strbreakup (str, "\n", -1);
1165 /* This loop has an inefficient worst-case, where
1166 * gtk_text_iter_get_text () is called repeatedly on
1171 if (limit && gtk_text_iter_compare (&search, limit) <= 0)
1174 if (backward_lines_match (&search, (const gchar**)lines,
1175 visible_only, slice, &match, &end))
1177 if (limit == NULL || (limit &&
1178 gtk_text_iter_compare (&end, limit) > 0))
1183 *match_start = match;
1190 if (gtk_text_iter_get_line_offset (&search) == 0)
1192 if (!gtk_text_iter_backward_line (&search))
1197 gtk_text_iter_set_line_offset (&search, 0);
1201 g_strfreev ((gchar**)lines);
1207 empathy_window_get_is_visible (GtkWindow *window)
1209 GdkWindowState state;
1210 GdkWindow *gdk_window;
1212 g_return_val_if_fail (GTK_IS_WINDOW (window), FALSE);
1214 gdk_window = GTK_WIDGET (window)->window;
1219 state = gdk_window_get_state (gdk_window);
1220 if (state & (GDK_WINDOW_STATE_WITHDRAWN | GDK_WINDOW_STATE_ICONIFIED)) {
1228 empathy_window_iconify (GtkWindow *window, GtkStatusIcon *status_icon)
1230 GdkRectangle icon_location;
1233 GdkWindow *gdk_window;
1235 gtk_status_icon_get_geometry (status_icon, NULL, &icon_location, NULL);
1236 gdk_window = GTK_WIDGET (window)->window;
1237 dpy = gdk_x11_drawable_get_xdisplay (gdk_window);
1239 data[0] = icon_location.x;
1240 data[1] = icon_location.y;
1241 data[2] = icon_location.width;
1242 data[3] = icon_location.height;
1244 XChangeProperty (dpy,
1245 GDK_WINDOW_XID (gdk_window),
1246 gdk_x11_get_xatom_by_name_for_display (gdk_drawable_get_display (gdk_window),
1247 "_NET_WM_ICON_GEOMETRY"),
1248 XA_CARDINAL, 32, PropModeReplace,
1249 (guchar *)&data, 4);
1251 gtk_window_set_skip_taskbar_hint (window, TRUE);
1252 gtk_window_iconify (window);
1255 /* Takes care of moving the window to the current workspace. */
1257 empathy_window_present (GtkWindow *window,
1258 gboolean steal_focus)
1262 g_return_if_fail (GTK_IS_WINDOW (window));
1264 /* There are three cases: hidden, visible, visible on another
1268 if (!empathy_window_get_is_visible (window)) {
1269 /* Hide it so present brings it to the current workspace. */
1270 gtk_widget_hide (GTK_WIDGET (window));
1273 timestamp = gtk_get_current_event_time ();
1274 gtk_window_set_skip_taskbar_hint (window, FALSE);
1275 gtk_window_present_with_time (window, timestamp);
1279 empathy_get_toplevel_window (GtkWidget *widget)
1281 GtkWidget *toplevel;
1283 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1285 toplevel = gtk_widget_get_toplevel (widget);
1286 if (GTK_IS_WINDOW (toplevel) &&
1287 GTK_WIDGET_TOPLEVEL (toplevel)) {
1288 return GTK_WINDOW (toplevel);
1294 /* The URL opening code can't handle schemeless strings, so we try to be
1295 * smart and add http if there is no scheme or doesn't look like a mail
1296 * address. This should work in most cases, and let us click on strings
1297 * like "www.gnome.org".
1300 fixup_url (const gchar *url)
1304 if (!g_str_has_prefix (url, "http://") &&
1305 !strstr (url, ":/") &&
1306 !strstr (url, "@")) {
1307 real_url = g_strdup_printf ("http://%s", url);
1309 real_url = g_strdup (url);
1316 empathy_url_show (const char *url)
1321 real_url = fixup_url (url);
1322 res = gnome_vfs_url_show (real_url);
1323 if (res != GNOME_VFS_OK) {
1324 empathy_debug (DEBUG_DOMAIN, "Couldn't show URL %s: %s",
1326 gnome_vfs_result_to_string (res));
1333 link_button_hook (GtkLinkButton *button,
1337 empathy_url_show (link);
1341 empathy_link_button_new (const gchar *url,
1344 static gboolean hook = FALSE;
1348 gtk_link_button_set_uri_hook (link_button_hook, NULL, NULL);
1351 return gtk_link_button_new_with_label (url, title);
1355 empathy_toggle_button_set_state_quietly (GtkWidget *widget,
1360 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (widget));
1362 g_signal_handlers_block_by_func (widget, callback, user_data);
1363 g_object_set (widget, "active", active, NULL);
1364 g_signal_handlers_unblock_by_func (widget, callback, user_data);
1368 empathy_text_buffer_tag_set (GtkTextBuffer *buffer,
1369 const gchar *tag_name,
1370 const gchar *first_property_name,
1373 GtkTextTagTable *table;
1376 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
1377 g_return_val_if_fail (tag_name != NULL, NULL);
1379 table = gtk_text_buffer_get_tag_table (buffer);
1380 tag = gtk_text_tag_table_lookup (table, tag_name);
1383 tag = gtk_text_tag_new (tag_name);
1384 gtk_text_tag_table_add (table, tag);
1385 g_object_unref (tag);
1387 /* Clear the old values so that we don't affect the new theme. */
1389 "background-set", FALSE,
1390 "foreground-set", FALSE,
1391 "invisible-set", FALSE,
1392 "justification-set", FALSE,
1393 "paragraph-background-set", FALSE,
1394 "pixels-above-lines-set", FALSE,
1395 "pixels-below-lines-set", FALSE,
1400 "weight-set", FALSE,
1404 if (first_property_name) {
1407 va_start (list, first_property_name);
1408 g_object_set_valist (G_OBJECT (tag), first_property_name, list);