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);
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,
121 g_object_unref (gui);
125 empathy_glade_get_file (const gchar *filename,
128 const gchar *first_required_widget, ...)
133 va_start (args, first_required_widget);
135 gui = get_glade_file (filename,
138 first_required_widget,
151 empathy_glade_connect (GladeXML *gui,
153 gchar *first_widget, ...)
161 va_start (args, first_widget);
163 for (name = first_widget; name; name = va_arg (args, char *)) {
164 signal = va_arg (args, void *);
165 callback = va_arg (args, void *);
167 widget = glade_xml_get_widget (gui, name);
169 g_warning ("Glade file is missing widget '%s', aborting",
174 g_signal_connect (widget,
176 G_CALLBACK (callback),
184 empathy_glade_setup_size_group (GladeXML *gui,
185 GtkSizeGroupMode mode,
186 gchar *first_widget, ...)
190 GtkSizeGroup *size_group;
193 va_start (args, first_widget);
195 size_group = gtk_size_group_new (mode);
197 for (name = first_widget; name; name = va_arg (args, char *)) {
198 widget = glade_xml_get_widget (gui, name);
200 g_warning ("Glade file is missing widget '%s'", name);
204 gtk_size_group_add_widget (size_group, widget);
207 g_object_unref (size_group);
213 empathy_icon_name_from_account (McAccount *account)
217 profile = mc_account_get_profile (account);
219 return mc_profile_get_icon_name (profile);
223 empathy_icon_name_for_presence (McPresence presence)
226 case MC_PRESENCE_AVAILABLE:
227 return EMPATHY_IMAGE_AVAILABLE;
228 case MC_PRESENCE_DO_NOT_DISTURB:
229 return EMPATHY_IMAGE_BUSY;
230 case MC_PRESENCE_AWAY:
231 return EMPATHY_IMAGE_AWAY;
232 case MC_PRESENCE_EXTENDED_AWAY:
233 return EMPATHY_IMAGE_EXT_AWAY;
234 case MC_PRESENCE_HIDDEN:
235 return EMPATHY_IMAGE_HIDDEN;
236 case MC_PRESENCE_OFFLINE:
237 case MC_PRESENCE_UNSET:
238 return EMPATHY_IMAGE_OFFLINE;
240 g_assert_not_reached ();
247 empathy_icon_name_for_contact (EmpathyContact *contact)
251 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact),
252 EMPATHY_IMAGE_OFFLINE);
254 presence = empathy_contact_get_presence (contact);
255 return empathy_icon_name_for_presence (presence);
259 empathy_pixbuf_from_data (gchar *data,
262 GdkPixbufLoader *loader;
263 GdkPixbuf *pixbuf = NULL;
264 GError *error = NULL;
270 loader = gdk_pixbuf_loader_new ();
271 if (!gdk_pixbuf_loader_write (loader, data, data_size, &error)) {
272 empathy_debug (DEBUG_DOMAIN, "Failed to write to pixbuf loader: %s",
273 error ? error->message : "No error given");
274 g_clear_error (&error);
275 g_object_unref (loader);
278 if (!gdk_pixbuf_loader_close (loader, &error)) {
279 empathy_debug (DEBUG_DOMAIN, "Failed to close pixbuf loader: %s",
280 error ? error->message : "No error given");
281 g_clear_error (&error);
282 g_object_unref (loader);
286 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
288 g_object_ref (pixbuf);
291 g_object_unref (loader);
297 pixbuf_from_avatar_size_prepared_cb (GdkPixbufLoader *loader,
300 struct SizeData *data)
302 g_return_if_fail (width > 0 && height > 0);
304 if (data->preserve_aspect_ratio && (data->width > 0 || data->height > 0)) {
305 if (width < data->width && height < data->height) {
310 if (data->width < 0) {
311 width = width * (double) data->height / (gdouble) height;
312 height = data->height;
313 } else if (data->height < 0) {
314 height = height * (double) data->width / (double) width;
316 } else if ((double) height * (double) data->width >
317 (double) width * (double) data->height) {
318 width = 0.5 + (double) width * (double) data->height / (double) height;
319 height = data->height;
321 height = 0.5 + (double) height * (double) data->width / (double) width;
325 if (data->width > 0) {
329 if (data->height > 0) {
330 height = data->height;
334 gdk_pixbuf_loader_set_size (loader, width, height);
338 empathy_avatar_pixbuf_roundify (GdkPixbuf *pixbuf)
340 gint width, height, rowstride;
343 width = gdk_pixbuf_get_width (pixbuf);
344 height = gdk_pixbuf_get_height (pixbuf);
345 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
346 pixels = gdk_pixbuf_get_pixels (pixbuf);
348 if (width < 6 || height < 6) {
356 pixels[rowstride + 3] = 0x80;
357 pixels[rowstride * 2 + 3] = 0xC0;
360 pixels[width * 4 - 1] = 0;
361 pixels[width * 4 - 5] = 0x80;
362 pixels[width * 4 - 9] = 0xC0;
363 pixels[rowstride + (width * 4) - 1] = 0x80;
364 pixels[(2 * rowstride) + (width * 4) - 1] = 0xC0;
367 pixels[(height - 1) * rowstride + 3] = 0;
368 pixels[(height - 1) * rowstride + 7] = 0x80;
369 pixels[(height - 1) * rowstride + 11] = 0xC0;
370 pixels[(height - 2) * rowstride + 3] = 0x80;
371 pixels[(height - 3) * rowstride + 3] = 0xC0;
374 pixels[height * rowstride - 1] = 0;
375 pixels[(height - 1) * rowstride - 1] = 0x80;
376 pixels[(height - 2) * rowstride - 1] = 0xC0;
377 pixels[height * rowstride - 5] = 0x80;
378 pixels[height * rowstride - 9] = 0xC0;
382 empathy_gdk_pixbuf_is_opaque (GdkPixbuf *pixbuf)
384 gint width, height, rowstride, i;
388 width = gdk_pixbuf_get_width (pixbuf);
389 height = gdk_pixbuf_get_height (pixbuf);
390 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
391 pixels = gdk_pixbuf_get_pixels (pixbuf);
394 for (i = 3; i < rowstride; i+=4) {
400 for (i = 1; i < height - 1; i++) {
401 row = pixels + (i*rowstride);
402 if (row[3] < 0xfe || row[rowstride-1] < 0xfe) {
407 row = pixels + ((height-1) * rowstride);
408 for (i = 3; i < rowstride; i+=4) {
418 empathy_pixbuf_from_avatar_scaled (EmpathyAvatar *avatar,
423 GdkPixbufLoader *loader;
424 struct SizeData data;
425 GError *error = NULL;
432 data.height = height;
433 data.preserve_aspect_ratio = TRUE;
435 loader = gdk_pixbuf_loader_new ();
437 g_signal_connect (loader, "size-prepared",
438 G_CALLBACK (pixbuf_from_avatar_size_prepared_cb),
441 if (!gdk_pixbuf_loader_write (loader, avatar->data, avatar->len, &error)) {
442 g_warning ("Couldn't write avatar image:%p with "
443 "length:%" G_GSIZE_FORMAT " to pixbuf loader: %s",
444 avatar->data, avatar->len, error->message);
445 g_error_free (error);
449 gdk_pixbuf_loader_close (loader, NULL);
451 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
452 if (!gdk_pixbuf_get_has_alpha (pixbuf)) {
453 GdkPixbuf *rounded_pixbuf;
455 rounded_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8,
456 gdk_pixbuf_get_width (pixbuf),
457 gdk_pixbuf_get_height (pixbuf));
458 gdk_pixbuf_copy_area (pixbuf, 0, 0,
459 gdk_pixbuf_get_width (pixbuf),
460 gdk_pixbuf_get_height (pixbuf),
463 pixbuf = rounded_pixbuf;
465 g_object_ref (pixbuf);
468 if (empathy_gdk_pixbuf_is_opaque (pixbuf)) {
469 empathy_avatar_pixbuf_roundify (pixbuf);
472 g_object_unref (loader);
478 empathy_pixbuf_avatar_from_contact_scaled (EmpathyContact *contact,
482 EmpathyAvatar *avatar;
484 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
486 avatar = empathy_contact_get_avatar (contact);
488 return empathy_pixbuf_from_avatar_scaled (avatar, width, height);
492 empathy_pixbuf_scale_down_if_necessary (GdkPixbuf *pixbuf, gint max_size)
497 width = gdk_pixbuf_get_width (pixbuf);
498 height = gdk_pixbuf_get_height (pixbuf);
500 if (width > max_size || height > max_size) {
501 factor = (gdouble) max_size / MAX (width, height);
503 width = width * factor;
504 height = height * factor;
506 return gdk_pixbuf_scale_simple (pixbuf,
511 return g_object_ref (pixbuf);
515 empathy_pixbuf_from_icon_name (const gchar *icon_name,
516 GtkIconSize icon_size)
519 GdkPixbuf *pixbuf = NULL;
520 GError *error = NULL;
528 theme = gtk_icon_theme_get_default ();
530 if (gtk_icon_size_lookup (icon_size, &w, &h)) {
534 pixbuf = gtk_icon_theme_load_icon (theme,
540 empathy_debug (DEBUG_DOMAIN, "Error loading icon: %s", error->message);
541 g_clear_error (&error);
547 /* Stolen from GtkSourceView, hence the weird intendation. Please keep it like
548 * that to make it easier to apply changes from the original code.
550 #define GTK_TEXT_UNKNOWN_CHAR 0xFFFC
552 /* this function acts like g_utf8_offset_to_pointer() except that if it finds a
553 * decomposable character it consumes the decomposition length from the given
554 * offset. So it's useful when the offset was calculated for the normalized
555 * version of str, but we need a pointer to str itself. */
557 pointer_from_offset_skipping_decomp (const gchar *str, gint offset)
559 gchar *casefold, *normal;
565 q = g_utf8_next_char (p);
566 casefold = g_utf8_casefold (p, q - p);
567 normal = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
568 offset -= g_utf8_strlen (normal, -1);
577 g_utf8_strcasestr (const gchar *haystack, const gchar *needle)
581 const gchar *ret = NULL;
584 gchar *caseless_haystack;
587 g_return_val_if_fail (haystack != NULL, NULL);
588 g_return_val_if_fail (needle != NULL, NULL);
590 casefold = g_utf8_casefold (haystack, -1);
591 caseless_haystack = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
594 needle_len = g_utf8_strlen (needle, -1);
595 haystack_len = g_utf8_strlen (caseless_haystack, -1);
599 ret = (gchar *)haystack;
603 if (haystack_len < needle_len)
609 p = (gchar*)caseless_haystack;
610 needle_len = strlen (needle);
615 if ((strncmp (p, needle, needle_len) == 0))
617 ret = pointer_from_offset_skipping_decomp (haystack, i);
621 p = g_utf8_next_char (p);
626 g_free (caseless_haystack);
632 g_utf8_caselessnmatch (const char *s1, const char *s2,
633 gssize n1, gssize n2)
636 gchar *normalized_s1;
637 gchar *normalized_s2;
640 gboolean ret = FALSE;
642 g_return_val_if_fail (s1 != NULL, FALSE);
643 g_return_val_if_fail (s2 != NULL, FALSE);
644 g_return_val_if_fail (n1 > 0, FALSE);
645 g_return_val_if_fail (n2 > 0, FALSE);
647 casefold = g_utf8_casefold (s1, n1);
648 normalized_s1 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
651 casefold = g_utf8_casefold (s2, n2);
652 normalized_s2 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
655 len_s1 = strlen (normalized_s1);
656 len_s2 = strlen (normalized_s2);
661 ret = (strncmp (normalized_s1, normalized_s2, len_s2) == 0);
664 g_free (normalized_s1);
665 g_free (normalized_s2);
671 forward_chars_with_skipping (GtkTextIter *iter,
673 gboolean skip_invisible,
674 gboolean skip_nontext,
675 gboolean skip_decomp)
679 g_return_if_fail (count >= 0);
685 gboolean ignored = FALSE;
687 /* minimal workaround to avoid the infinite loop of bug #168247.
688 * It doesn't fix the problemjust the symptom...
690 if (gtk_text_iter_is_end (iter))
693 if (skip_nontext && gtk_text_iter_get_char (iter) == GTK_TEXT_UNKNOWN_CHAR)
696 if (!ignored && skip_invisible &&
697 /* _gtk_text_btree_char_is_invisible (iter)*/ FALSE)
700 if (!ignored && skip_decomp)
702 /* being UTF8 correct sucks; this accounts for extra
703 offsets coming from canonical decompositions of
704 UTF8 characters (e.g. accented characters) which
705 g_utf8_normalize() performs */
710 buffer_len = g_unichar_to_utf8 (gtk_text_iter_get_char (iter), buffer);
711 normal = g_utf8_normalize (buffer, buffer_len, G_NORMALIZE_NFD);
712 i -= (g_utf8_strlen (normal, -1) - 1);
716 gtk_text_iter_forward_char (iter);
724 lines_match (const GtkTextIter *start,
726 gboolean visible_only,
728 GtkTextIter *match_start,
729 GtkTextIter *match_end)
736 if (*lines == NULL || **lines == '\0')
739 *match_start = *start;
746 gtk_text_iter_forward_line (&next);
748 /* No more text in buffer, but *lines is nonempty */
749 if (gtk_text_iter_equal (start, &next))
755 line_text = gtk_text_iter_get_visible_slice (start, &next);
757 line_text = gtk_text_iter_get_slice (start, &next);
762 line_text = gtk_text_iter_get_visible_text (start, &next);
764 line_text = gtk_text_iter_get_text (start, &next);
767 if (match_start) /* if this is the first line we're matching */
769 found = g_utf8_strcasestr (line_text, *lines);
773 /* If it's not the first line, we have to match from the
776 if (g_utf8_caselessnmatch (line_text, *lines, strlen (line_text),
789 /* Get offset to start of search string */
790 offset = g_utf8_strlen (line_text, found - line_text);
794 /* If match start needs to be returned, set it to the
795 * start of the search string.
797 forward_chars_with_skipping (&next, offset, visible_only, !slice, FALSE);
803 /* Go to end of search string */
804 forward_chars_with_skipping (&next, g_utf8_strlen (*lines, -1), visible_only, !slice, TRUE);
813 /* pass NULL for match_start, since we don't need to find the
816 return lines_match (&next, lines, visible_only, slice, NULL, match_end);
819 /* strsplit () that retains the delimiter as part of the string. */
821 strbreakup (const char *string,
822 const char *delimiter,
825 GSList *string_list = NULL, *slist;
826 gchar **str_array, *s, *casefold, *new_string;
829 g_return_val_if_fail (string != NULL, NULL);
830 g_return_val_if_fail (delimiter != NULL, NULL);
833 max_tokens = G_MAXINT;
835 s = strstr (string, delimiter);
838 guint delimiter_len = strlen (delimiter);
844 len = s - string + delimiter_len;
845 new_string = g_new (gchar, len + 1);
846 strncpy (new_string, string, len);
848 casefold = g_utf8_casefold (new_string, -1);
850 new_string = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
852 string_list = g_slist_prepend (string_list, new_string);
854 string = s + delimiter_len;
855 s = strstr (string, delimiter);
856 } while (--max_tokens && s);
862 casefold = g_utf8_casefold (string, -1);
863 new_string = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
865 string_list = g_slist_prepend (string_list, new_string);
868 str_array = g_new (gchar*, n);
872 str_array[i--] = NULL;
873 for (slist = string_list; slist; slist = slist->next)
874 str_array[i--] = slist->data;
876 g_slist_free (string_list);
882 empathy_text_iter_forward_search (const GtkTextIter *iter,
884 GtkTextIter *match_start,
885 GtkTextIter *match_end,
886 const GtkTextIter *limit)
888 gchar **lines = NULL;
890 gboolean retval = FALSE;
892 gboolean visible_only;
895 g_return_val_if_fail (iter != NULL, FALSE);
896 g_return_val_if_fail (str != NULL, FALSE);
898 if (limit && gtk_text_iter_compare (iter, limit) >= 0)
902 /* If we can move one char, return the empty string there */
905 if (gtk_text_iter_forward_char (&match)) {
906 if (limit && gtk_text_iter_equal (&match, limit)) {
911 *match_start = match;
925 /* locate all lines */
926 lines = strbreakup (str, "\n", -1);
931 /* This loop has an inefficient worst-case, where
932 * gtk_text_iter_get_text () is called repeatedly on
937 if (limit && gtk_text_iter_compare (&search, limit) >= 0) {
941 if (lines_match (&search, (const gchar**)lines,
942 visible_only, slice, &match, &end)) {
944 (limit && gtk_text_iter_compare (&end, limit) <= 0)) {
948 *match_start = match;
956 } while (gtk_text_iter_forward_line (&search));
958 g_strfreev ((gchar**)lines);
964 g_utf8_strrcasestr (const gchar *haystack, const gchar *needle)
968 const gchar *ret = NULL;
971 gchar *caseless_haystack;
974 g_return_val_if_fail (haystack != NULL, NULL);
975 g_return_val_if_fail (needle != NULL, NULL);
977 casefold = g_utf8_casefold (haystack, -1);
978 caseless_haystack = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
981 needle_len = g_utf8_strlen (needle, -1);
982 haystack_len = g_utf8_strlen (caseless_haystack, -1);
986 ret = (gchar *)haystack;
990 if (haystack_len < needle_len)
996 i = haystack_len - needle_len;
997 p = g_utf8_offset_to_pointer (caseless_haystack, i);
998 needle_len = strlen (needle);
1000 while (p >= caseless_haystack)
1002 if (strncmp (p, needle, needle_len) == 0)
1004 ret = pointer_from_offset_skipping_decomp (haystack, i);
1008 p = g_utf8_prev_char (p);
1013 g_free (caseless_haystack);
1019 backward_lines_match (const GtkTextIter *start,
1020 const gchar **lines,
1021 gboolean visible_only,
1023 GtkTextIter *match_start,
1024 GtkTextIter *match_end)
1026 GtkTextIter line, next;
1031 if (*lines == NULL || **lines == '\0')
1034 *match_start = *start;
1036 *match_end = *start;
1040 line = next = *start;
1041 if (gtk_text_iter_get_line_offset (&next) == 0)
1043 if (!gtk_text_iter_backward_line (&next))
1047 gtk_text_iter_set_line_offset (&next, 0);
1052 line_text = gtk_text_iter_get_visible_slice (&next, &line);
1054 line_text = gtk_text_iter_get_slice (&next, &line);
1059 line_text = gtk_text_iter_get_visible_text (&next, &line);
1061 line_text = gtk_text_iter_get_text (&next, &line);
1064 if (match_start) /* if this is the first line we're matching */
1066 found = g_utf8_strrcasestr (line_text, *lines);
1070 /* If it's not the first line, we have to match from the
1071 * start of the line.
1073 if (g_utf8_caselessnmatch (line_text, *lines, strlen (line_text),
1086 /* Get offset to start of search string */
1087 offset = g_utf8_strlen (line_text, found - line_text);
1089 forward_chars_with_skipping (&next, offset, visible_only, !slice, FALSE);
1091 /* If match start needs to be returned, set it to the
1092 * start of the search string.
1096 *match_start = next;
1099 /* Go to end of search string */
1100 forward_chars_with_skipping (&next, g_utf8_strlen (*lines, -1), visible_only, !slice, TRUE);
1109 /* try to match the rest of the lines forward, passing NULL
1110 * for match_start so lines_match will try to match the entire
1112 return lines_match (&next, lines, visible_only,
1113 slice, NULL, match_end);
1117 empathy_text_iter_backward_search (const GtkTextIter *iter,
1119 GtkTextIter *match_start,
1120 GtkTextIter *match_end,
1121 const GtkTextIter *limit)
1123 gchar **lines = NULL;
1125 gboolean retval = FALSE;
1127 gboolean visible_only;
1130 g_return_val_if_fail (iter != NULL, FALSE);
1131 g_return_val_if_fail (str != NULL, FALSE);
1133 if (limit && gtk_text_iter_compare (iter, limit) <= 0)
1138 /* If we can move one char, return the empty string there */
1141 if (gtk_text_iter_backward_char (&match))
1143 if (limit && gtk_text_iter_equal (&match, limit))
1147 *match_start = match;
1158 visible_only = TRUE;
1161 /* locate all lines */
1162 lines = strbreakup (str, "\n", -1);
1168 /* This loop has an inefficient worst-case, where
1169 * gtk_text_iter_get_text () is called repeatedly on
1174 if (limit && gtk_text_iter_compare (&search, limit) <= 0)
1177 if (backward_lines_match (&search, (const gchar**)lines,
1178 visible_only, slice, &match, &end))
1180 if (limit == NULL || (limit &&
1181 gtk_text_iter_compare (&end, limit) > 0))
1186 *match_start = match;
1193 if (gtk_text_iter_get_line_offset (&search) == 0)
1195 if (!gtk_text_iter_backward_line (&search))
1200 gtk_text_iter_set_line_offset (&search, 0);
1204 g_strfreev ((gchar**)lines);
1210 empathy_window_get_is_visible (GtkWindow *window)
1212 GdkWindowState state;
1213 GdkWindow *gdk_window;
1215 g_return_val_if_fail (GTK_IS_WINDOW (window), FALSE);
1217 gdk_window = GTK_WIDGET (window)->window;
1222 state = gdk_window_get_state (gdk_window);
1223 if (state & (GDK_WINDOW_STATE_WITHDRAWN | GDK_WINDOW_STATE_ICONIFIED)) {
1231 empathy_window_iconify (GtkWindow *window, GtkStatusIcon *status_icon)
1233 GdkRectangle icon_location;
1236 GdkWindow *gdk_window;
1238 gtk_status_icon_get_geometry (status_icon, NULL, &icon_location, NULL);
1239 gdk_window = GTK_WIDGET (window)->window;
1240 dpy = gdk_x11_drawable_get_xdisplay (gdk_window);
1242 data[0] = icon_location.x;
1243 data[1] = icon_location.y;
1244 data[2] = icon_location.width;
1245 data[3] = icon_location.height;
1247 XChangeProperty (dpy,
1248 GDK_WINDOW_XID (gdk_window),
1249 gdk_x11_get_xatom_by_name_for_display (gdk_drawable_get_display (gdk_window),
1250 "_NET_WM_ICON_GEOMETRY"),
1251 XA_CARDINAL, 32, PropModeReplace,
1252 (guchar *)&data, 4);
1254 gtk_window_set_skip_taskbar_hint (window, TRUE);
1255 gtk_window_iconify (window);
1258 /* Takes care of moving the window to the current workspace. */
1260 empathy_window_present (GtkWindow *window,
1261 gboolean steal_focus)
1265 g_return_if_fail (GTK_IS_WINDOW (window));
1267 /* There are three cases: hidden, visible, visible on another
1271 if (!empathy_window_get_is_visible (window)) {
1272 /* Hide it so present brings it to the current workspace. */
1273 gtk_widget_hide (GTK_WIDGET (window));
1276 timestamp = gtk_get_current_event_time ();
1277 gtk_window_set_skip_taskbar_hint (window, FALSE);
1278 gtk_window_present_with_time (window, timestamp);
1282 empathy_get_toplevel_window (GtkWidget *widget)
1284 GtkWidget *toplevel;
1286 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1288 toplevel = gtk_widget_get_toplevel (widget);
1289 if (GTK_IS_WINDOW (toplevel) &&
1290 GTK_WIDGET_TOPLEVEL (toplevel)) {
1291 return GTK_WINDOW (toplevel);
1297 /* The URL opening code can't handle schemeless strings, so we try to be
1298 * smart and add http if there is no scheme or doesn't look like a mail
1299 * address. This should work in most cases, and let us click on strings
1300 * like "www.gnome.org".
1303 fixup_url (const gchar *url)
1307 if (!g_str_has_prefix (url, "http://") &&
1308 !strstr (url, ":/") &&
1309 !strstr (url, "@")) {
1310 real_url = g_strdup_printf ("http://%s", url);
1312 real_url = g_strdup (url);
1319 empathy_url_show (const char *url)
1324 real_url = fixup_url (url);
1325 res = gnome_vfs_url_show (real_url);
1326 if (res != GNOME_VFS_OK) {
1327 empathy_debug (DEBUG_DOMAIN, "Couldn't show URL %s: %s",
1329 gnome_vfs_result_to_string (res));
1336 link_button_hook (GtkLinkButton *button,
1340 empathy_url_show (link);
1344 empathy_link_button_new (const gchar *url,
1347 static gboolean hook = FALSE;
1351 gtk_link_button_set_uri_hook (link_button_hook, NULL, NULL);
1354 return gtk_link_button_new_with_label (url, title);
1358 empathy_toggle_button_set_state_quietly (GtkWidget *widget,
1363 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (widget));
1365 g_signal_handlers_block_by_func (widget, callback, user_data);
1366 g_object_set (widget, "active", active, NULL);
1367 g_signal_handlers_unblock_by_func (widget, callback, user_data);
1371 empathy_text_buffer_tag_set (GtkTextBuffer *buffer,
1372 const gchar *tag_name,
1373 const gchar *first_property_name,
1376 GtkTextTagTable *table;
1379 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
1380 g_return_val_if_fail (tag_name != NULL, NULL);
1382 table = gtk_text_buffer_get_tag_table (buffer);
1383 tag = gtk_text_tag_table_lookup (table, tag_name);
1386 tag = gtk_text_tag_new (tag_name);
1387 gtk_text_tag_table_add (table, tag);
1388 g_object_unref (tag);
1390 /* Clear the old values so that we don't affect the new theme. */
1392 "background-set", FALSE,
1393 "foreground-set", FALSE,
1394 "invisible-set", FALSE,
1395 "justification-set", FALSE,
1396 "paragraph-background-set", FALSE,
1397 "pixels-above-lines-set", FALSE,
1398 "pixels-below-lines-set", FALSE,
1403 "weight-set", FALSE,
1407 if (first_property_name) {
1410 va_start (list, first_property_name);
1411 g_object_set_valist (G_OBJECT (tag), first_property_name, list);