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):
32 #include <X11/Xatom.h>
34 #include <glib/gi18n.h>
36 #include <glade/glade.h>
37 #include <libgnomevfs/gnome-vfs-utils.h>
39 #include <libmissioncontrol/mc-profile.h>
41 #include <libempathy/empathy-debug.h>
43 #include "empathy-ui-utils.h"
44 #include "empathy-images.h"
46 #define DEBUG_DOMAIN "UiUtils"
51 gboolean preserve_aspect_ratio;
55 get_glade_file (const gchar *filename,
58 const gchar *first_required_widget,
64 GtkWidget **widget_ptr;
66 path = g_build_filename (DATADIR, "empathy", filename, NULL);
67 gui = glade_xml_new (path, root, domain);
71 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,
113 g_object_unref (gui);
117 empathy_glade_get_file (const gchar *filename,
120 const gchar *first_required_widget, ...)
125 va_start (args, first_required_widget);
127 gui = get_glade_file (filename,
130 first_required_widget,
143 empathy_glade_connect (GladeXML *gui,
145 gchar *first_widget, ...)
153 va_start (args, first_widget);
155 for (name = first_widget; name; name = va_arg (args, char *)) {
156 signal = va_arg (args, void *);
157 callback = va_arg (args, void *);
159 widget = glade_xml_get_widget (gui, name);
161 g_warning ("Glade file is missing widget '%s', aborting",
166 g_signal_connect (widget,
168 G_CALLBACK (callback),
176 empathy_glade_setup_size_group (GladeXML *gui,
177 GtkSizeGroupMode mode,
178 gchar *first_widget, ...)
182 GtkSizeGroup *size_group;
185 va_start (args, first_widget);
187 size_group = gtk_size_group_new (mode);
189 for (name = first_widget; name; name = va_arg (args, char *)) {
190 widget = glade_xml_get_widget (gui, name);
192 g_warning ("Glade file is missing widget '%s'", name);
196 gtk_size_group_add_widget (size_group, widget);
199 g_object_unref (size_group);
205 empathy_icon_name_from_account (McAccount *account)
209 profile = mc_account_get_profile (account);
211 return mc_profile_get_icon_name (profile);
215 empathy_icon_name_for_presence_state (McPresence state)
218 case MC_PRESENCE_AVAILABLE:
219 return EMPATHY_IMAGE_AVAILABLE;
220 case MC_PRESENCE_DO_NOT_DISTURB:
221 return EMPATHY_IMAGE_BUSY;
222 case MC_PRESENCE_AWAY:
223 return EMPATHY_IMAGE_AWAY;
224 case MC_PRESENCE_EXTENDED_AWAY:
225 return EMPATHY_IMAGE_EXT_AWAY;
226 case MC_PRESENCE_HIDDEN:
227 return EMPATHY_IMAGE_HIDDEN;
228 case MC_PRESENCE_OFFLINE:
229 case MC_PRESENCE_UNSET:
230 return EMPATHY_IMAGE_OFFLINE;
232 g_assert_not_reached ();
239 empathy_icon_name_for_presence (EmpathyPresence *presence)
243 g_return_val_if_fail (EMPATHY_IS_PRESENCE (presence),
244 EMPATHY_IMAGE_OFFLINE);
246 state = empathy_presence_get_state (presence);
248 return empathy_icon_name_for_presence_state (state);
252 empathy_icon_name_for_contact (EmpathyContact *contact)
254 EmpathyPresence *presence;
256 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact),
257 EMPATHY_IMAGE_OFFLINE);
259 presence = empathy_contact_get_presence (contact);
261 return empathy_icon_name_for_presence (presence);
264 return EMPATHY_IMAGE_UNKNOWN;
268 empathy_pixbuf_from_data (gchar *data,
271 GdkPixbufLoader *loader;
272 GdkPixbuf *pixbuf = NULL;
273 GError *error = NULL;
279 loader = gdk_pixbuf_loader_new ();
280 if (!gdk_pixbuf_loader_write (loader, data, data_size, &error)) {
281 empathy_debug (DEBUG_DOMAIN, "Failed to write to pixbuf loader: %s",
282 error ? error->message : "No error given");
283 g_clear_error (&error);
284 g_object_unref (loader);
287 if (!gdk_pixbuf_loader_close (loader, &error)) {
288 empathy_debug (DEBUG_DOMAIN, "Failed to close pixbuf loader: %s",
289 error ? error->message : "No error given");
290 g_clear_error (&error);
291 g_object_unref (loader);
295 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
297 g_object_ref (pixbuf);
300 g_object_unref (loader);
306 pixbuf_from_avatar_size_prepared_cb (GdkPixbufLoader *loader,
309 struct SizeData *data)
311 g_return_if_fail (width > 0 && height > 0);
313 if (data->preserve_aspect_ratio && (data->width > 0 || data->height > 0)) {
314 if (width < data->width && height < data->height) {
319 if (data->width < 0) {
320 width = width * (double) data->height / (gdouble) height;
321 height = data->height;
322 } else if (data->height < 0) {
323 height = height * (double) data->width / (double) width;
325 } else if ((double) height * (double) data->width >
326 (double) width * (double) data->height) {
327 width = 0.5 + (double) width * (double) data->height / (double) height;
328 height = data->height;
330 height = 0.5 + (double) height * (double) data->width / (double) width;
334 if (data->width > 0) {
338 if (data->height > 0) {
339 height = data->height;
343 gdk_pixbuf_loader_set_size (loader, width, height);
347 empathy_avatar_pixbuf_roundify (GdkPixbuf *pixbuf)
349 gint width, height, rowstride;
352 width = gdk_pixbuf_get_width (pixbuf);
353 height = gdk_pixbuf_get_height (pixbuf);
354 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
355 pixels = gdk_pixbuf_get_pixels (pixbuf);
357 if (width < 6 || height < 6) {
365 pixels[rowstride + 3] = 0x80;
366 pixels[rowstride * 2 + 3] = 0xC0;
369 pixels[width * 4 - 1] = 0;
370 pixels[width * 4 - 5] = 0x80;
371 pixels[width * 4 - 9] = 0xC0;
372 pixels[rowstride + (width * 4) - 1] = 0x80;
373 pixels[(2 * rowstride) + (width * 4) - 1] = 0xC0;
376 pixels[(height - 1) * rowstride + 3] = 0;
377 pixels[(height - 1) * rowstride + 7] = 0x80;
378 pixels[(height - 1) * rowstride + 11] = 0xC0;
379 pixels[(height - 2) * rowstride + 3] = 0x80;
380 pixels[(height - 3) * rowstride + 3] = 0xC0;
383 pixels[height * rowstride - 1] = 0;
384 pixels[(height - 1) * rowstride - 1] = 0x80;
385 pixels[(height - 2) * rowstride - 1] = 0xC0;
386 pixels[height * rowstride - 5] = 0x80;
387 pixels[height * rowstride - 9] = 0xC0;
391 empathy_gdk_pixbuf_is_opaque (GdkPixbuf *pixbuf)
393 gint width, height, rowstride, i;
397 width = gdk_pixbuf_get_width (pixbuf);
398 height = gdk_pixbuf_get_height (pixbuf);
399 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
400 pixels = gdk_pixbuf_get_pixels (pixbuf);
403 for (i = 3; i < rowstride; i+=4) {
409 for (i = 1; i < height - 1; i++) {
410 row = pixels + (i*rowstride);
411 if (row[3] < 0xfe || row[rowstride-1] < 0xfe) {
416 row = pixels + ((height-1) * rowstride);
417 for (i = 3; i < rowstride; i+=4) {
427 empathy_pixbuf_from_avatar_scaled (EmpathyAvatar *avatar,
432 GdkPixbufLoader *loader;
433 struct SizeData data;
434 GError *error = NULL;
441 data.height = height;
442 data.preserve_aspect_ratio = TRUE;
444 loader = gdk_pixbuf_loader_new ();
446 g_signal_connect (loader, "size-prepared",
447 G_CALLBACK (pixbuf_from_avatar_size_prepared_cb),
450 if (!gdk_pixbuf_loader_write (loader, avatar->data, avatar->len, &error)) {
451 g_warning ("Couldn't write avatar image:%p with "
452 "length:%" G_GSIZE_FORMAT " to pixbuf loader: %s",
453 avatar->data, avatar->len, error->message);
454 g_error_free (error);
458 gdk_pixbuf_loader_close (loader, NULL);
460 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
461 if (!gdk_pixbuf_get_has_alpha (pixbuf)) {
462 GdkPixbuf *rounded_pixbuf;
464 rounded_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8,
465 gdk_pixbuf_get_width (pixbuf),
466 gdk_pixbuf_get_height (pixbuf));
467 gdk_pixbuf_copy_area (pixbuf, 0, 0,
468 gdk_pixbuf_get_width (pixbuf),
469 gdk_pixbuf_get_height (pixbuf),
472 pixbuf = rounded_pixbuf;
474 g_object_ref (pixbuf);
477 if (empathy_gdk_pixbuf_is_opaque (pixbuf)) {
478 empathy_avatar_pixbuf_roundify (pixbuf);
481 g_object_unref (loader);
487 empathy_pixbuf_avatar_from_contact_scaled (EmpathyContact *contact,
491 EmpathyAvatar *avatar;
493 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
495 avatar = empathy_contact_get_avatar (contact);
497 return empathy_pixbuf_from_avatar_scaled (avatar, width, height);
501 empathy_pixbuf_scale_down_if_necessary (GdkPixbuf *pixbuf, gint max_size)
506 width = gdk_pixbuf_get_width (pixbuf);
507 height = gdk_pixbuf_get_height (pixbuf);
509 if (width > max_size || height > max_size) {
510 factor = (gdouble) max_size / MAX (width, height);
512 width = width * factor;
513 height = height * factor;
515 return gdk_pixbuf_scale_simple (pixbuf,
520 return g_object_ref (pixbuf);
524 empathy_pixbuf_from_icon_name (const gchar *icon_name,
525 GtkIconSize icon_size)
528 GdkPixbuf *pixbuf = NULL;
529 GError *error = NULL;
533 theme = gtk_icon_theme_get_default ();
535 if (gtk_icon_size_lookup (icon_size, &w, &h)) {
539 pixbuf = gtk_icon_theme_load_icon (theme,
545 empathy_debug (DEBUG_DOMAIN, "Error loading icon: %s", error->message);
546 g_clear_error (&error);
552 /* Stolen from GtkSourceView, hence the weird intendation. Please keep it like
553 * that to make it easier to apply changes from the original code.
555 #define GTK_TEXT_UNKNOWN_CHAR 0xFFFC
557 /* this function acts like g_utf8_offset_to_pointer() except that if it finds a
558 * decomposable character it consumes the decomposition length from the given
559 * offset. So it's useful when the offset was calculated for the normalized
560 * version of str, but we need a pointer to str itself. */
562 pointer_from_offset_skipping_decomp (const gchar *str, gint offset)
564 gchar *casefold, *normal;
570 q = g_utf8_next_char (p);
571 casefold = g_utf8_casefold (p, q - p);
572 normal = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
573 offset -= g_utf8_strlen (normal, -1);
582 g_utf8_strcasestr (const gchar *haystack, const gchar *needle)
586 const gchar *ret = NULL;
589 gchar *caseless_haystack;
592 g_return_val_if_fail (haystack != NULL, NULL);
593 g_return_val_if_fail (needle != NULL, NULL);
595 casefold = g_utf8_casefold (haystack, -1);
596 caseless_haystack = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
599 needle_len = g_utf8_strlen (needle, -1);
600 haystack_len = g_utf8_strlen (caseless_haystack, -1);
604 ret = (gchar *)haystack;
608 if (haystack_len < needle_len)
614 p = (gchar*)caseless_haystack;
615 needle_len = strlen (needle);
620 if ((strncmp (p, needle, needle_len) == 0))
622 ret = pointer_from_offset_skipping_decomp (haystack, i);
626 p = g_utf8_next_char (p);
631 g_free (caseless_haystack);
637 g_utf8_caselessnmatch (const char *s1, const char *s2,
638 gssize n1, gssize n2)
641 gchar *normalized_s1;
642 gchar *normalized_s2;
645 gboolean ret = FALSE;
647 g_return_val_if_fail (s1 != NULL, FALSE);
648 g_return_val_if_fail (s2 != NULL, FALSE);
649 g_return_val_if_fail (n1 > 0, FALSE);
650 g_return_val_if_fail (n2 > 0, FALSE);
652 casefold = g_utf8_casefold (s1, n1);
653 normalized_s1 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
656 casefold = g_utf8_casefold (s2, n2);
657 normalized_s2 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
660 len_s1 = strlen (normalized_s1);
661 len_s2 = strlen (normalized_s2);
666 ret = (strncmp (normalized_s1, normalized_s2, len_s2) == 0);
669 g_free (normalized_s1);
670 g_free (normalized_s2);
676 forward_chars_with_skipping (GtkTextIter *iter,
678 gboolean skip_invisible,
679 gboolean skip_nontext,
680 gboolean skip_decomp)
684 g_return_if_fail (count >= 0);
690 gboolean ignored = FALSE;
692 /* minimal workaround to avoid the infinite loop of bug #168247.
693 * It doesn't fix the problemjust the symptom...
695 if (gtk_text_iter_is_end (iter))
698 if (skip_nontext && gtk_text_iter_get_char (iter) == GTK_TEXT_UNKNOWN_CHAR)
701 if (!ignored && skip_invisible &&
702 /* _gtk_text_btree_char_is_invisible (iter)*/ FALSE)
705 if (!ignored && skip_decomp)
707 /* being UTF8 correct sucks; this accounts for extra
708 offsets coming from canonical decompositions of
709 UTF8 characters (e.g. accented characters) which
710 g_utf8_normalize() performs */
715 buffer_len = g_unichar_to_utf8 (gtk_text_iter_get_char (iter), buffer);
716 normal = g_utf8_normalize (buffer, buffer_len, G_NORMALIZE_NFD);
717 i -= (g_utf8_strlen (normal, -1) - 1);
721 gtk_text_iter_forward_char (iter);
729 lines_match (const GtkTextIter *start,
731 gboolean visible_only,
733 GtkTextIter *match_start,
734 GtkTextIter *match_end)
741 if (*lines == NULL || **lines == '\0')
744 *match_start = *start;
751 gtk_text_iter_forward_line (&next);
753 /* No more text in buffer, but *lines is nonempty */
754 if (gtk_text_iter_equal (start, &next))
760 line_text = gtk_text_iter_get_visible_slice (start, &next);
762 line_text = gtk_text_iter_get_slice (start, &next);
767 line_text = gtk_text_iter_get_visible_text (start, &next);
769 line_text = gtk_text_iter_get_text (start, &next);
772 if (match_start) /* if this is the first line we're matching */
774 found = g_utf8_strcasestr (line_text, *lines);
778 /* If it's not the first line, we have to match from the
781 if (g_utf8_caselessnmatch (line_text, *lines, strlen (line_text),
794 /* Get offset to start of search string */
795 offset = g_utf8_strlen (line_text, found - line_text);
799 /* If match start needs to be returned, set it to the
800 * start of the search string.
802 forward_chars_with_skipping (&next, offset, visible_only, !slice, FALSE);
808 /* Go to end of search string */
809 forward_chars_with_skipping (&next, g_utf8_strlen (*lines, -1), visible_only, !slice, TRUE);
818 /* pass NULL for match_start, since we don't need to find the
821 return lines_match (&next, lines, visible_only, slice, NULL, match_end);
824 /* strsplit () that retains the delimiter as part of the string. */
826 strbreakup (const char *string,
827 const char *delimiter,
830 GSList *string_list = NULL, *slist;
831 gchar **str_array, *s, *casefold, *new_string;
834 g_return_val_if_fail (string != NULL, NULL);
835 g_return_val_if_fail (delimiter != NULL, NULL);
838 max_tokens = G_MAXINT;
840 s = strstr (string, delimiter);
843 guint delimiter_len = strlen (delimiter);
849 len = s - string + delimiter_len;
850 new_string = g_new (gchar, len + 1);
851 strncpy (new_string, string, len);
853 casefold = g_utf8_casefold (new_string, -1);
855 new_string = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
857 string_list = g_slist_prepend (string_list, new_string);
859 string = s + delimiter_len;
860 s = strstr (string, delimiter);
861 } while (--max_tokens && s);
867 casefold = g_utf8_casefold (string, -1);
868 new_string = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
870 string_list = g_slist_prepend (string_list, new_string);
873 str_array = g_new (gchar*, n);
877 str_array[i--] = NULL;
878 for (slist = string_list; slist; slist = slist->next)
879 str_array[i--] = slist->data;
881 g_slist_free (string_list);
887 empathy_text_iter_forward_search (const GtkTextIter *iter,
889 GtkTextIter *match_start,
890 GtkTextIter *match_end,
891 const GtkTextIter *limit)
893 gchar **lines = NULL;
895 gboolean retval = FALSE;
897 gboolean visible_only;
900 g_return_val_if_fail (iter != NULL, FALSE);
901 g_return_val_if_fail (str != NULL, FALSE);
903 if (limit && gtk_text_iter_compare (iter, limit) >= 0)
907 /* If we can move one char, return the empty string there */
910 if (gtk_text_iter_forward_char (&match)) {
911 if (limit && gtk_text_iter_equal (&match, limit)) {
916 *match_start = match;
930 /* locate all lines */
931 lines = strbreakup (str, "\n", -1);
936 /* This loop has an inefficient worst-case, where
937 * gtk_text_iter_get_text () is called repeatedly on
942 if (limit && gtk_text_iter_compare (&search, limit) >= 0) {
946 if (lines_match (&search, (const gchar**)lines,
947 visible_only, slice, &match, &end)) {
949 (limit && gtk_text_iter_compare (&end, limit) <= 0)) {
953 *match_start = match;
961 } while (gtk_text_iter_forward_line (&search));
963 g_strfreev ((gchar**)lines);
969 g_utf8_strrcasestr (const gchar *haystack, const gchar *needle)
973 const gchar *ret = NULL;
976 gchar *caseless_haystack;
979 g_return_val_if_fail (haystack != NULL, NULL);
980 g_return_val_if_fail (needle != NULL, NULL);
982 casefold = g_utf8_casefold (haystack, -1);
983 caseless_haystack = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
986 needle_len = g_utf8_strlen (needle, -1);
987 haystack_len = g_utf8_strlen (caseless_haystack, -1);
991 ret = (gchar *)haystack;
995 if (haystack_len < needle_len)
1001 i = haystack_len - needle_len;
1002 p = g_utf8_offset_to_pointer (caseless_haystack, i);
1003 needle_len = strlen (needle);
1005 while (p >= caseless_haystack)
1007 if (strncmp (p, needle, needle_len) == 0)
1009 ret = pointer_from_offset_skipping_decomp (haystack, i);
1013 p = g_utf8_prev_char (p);
1018 g_free (caseless_haystack);
1024 backward_lines_match (const GtkTextIter *start,
1025 const gchar **lines,
1026 gboolean visible_only,
1028 GtkTextIter *match_start,
1029 GtkTextIter *match_end)
1031 GtkTextIter line, next;
1036 if (*lines == NULL || **lines == '\0')
1039 *match_start = *start;
1041 *match_end = *start;
1045 line = next = *start;
1046 if (gtk_text_iter_get_line_offset (&next) == 0)
1048 if (!gtk_text_iter_backward_line (&next))
1052 gtk_text_iter_set_line_offset (&next, 0);
1057 line_text = gtk_text_iter_get_visible_slice (&next, &line);
1059 line_text = gtk_text_iter_get_slice (&next, &line);
1064 line_text = gtk_text_iter_get_visible_text (&next, &line);
1066 line_text = gtk_text_iter_get_text (&next, &line);
1069 if (match_start) /* if this is the first line we're matching */
1071 found = g_utf8_strrcasestr (line_text, *lines);
1075 /* If it's not the first line, we have to match from the
1076 * start of the line.
1078 if (g_utf8_caselessnmatch (line_text, *lines, strlen (line_text),
1091 /* Get offset to start of search string */
1092 offset = g_utf8_strlen (line_text, found - line_text);
1094 forward_chars_with_skipping (&next, offset, visible_only, !slice, FALSE);
1096 /* If match start needs to be returned, set it to the
1097 * start of the search string.
1101 *match_start = next;
1104 /* Go to end of search string */
1105 forward_chars_with_skipping (&next, g_utf8_strlen (*lines, -1), visible_only, !slice, TRUE);
1114 /* try to match the rest of the lines forward, passing NULL
1115 * for match_start so lines_match will try to match the entire
1117 return lines_match (&next, lines, visible_only,
1118 slice, NULL, match_end);
1122 empathy_text_iter_backward_search (const GtkTextIter *iter,
1124 GtkTextIter *match_start,
1125 GtkTextIter *match_end,
1126 const GtkTextIter *limit)
1128 gchar **lines = NULL;
1130 gboolean retval = FALSE;
1132 gboolean visible_only;
1135 g_return_val_if_fail (iter != NULL, FALSE);
1136 g_return_val_if_fail (str != NULL, FALSE);
1138 if (limit && gtk_text_iter_compare (iter, limit) <= 0)
1143 /* If we can move one char, return the empty string there */
1146 if (gtk_text_iter_backward_char (&match))
1148 if (limit && gtk_text_iter_equal (&match, limit))
1152 *match_start = match;
1163 visible_only = TRUE;
1166 /* locate all lines */
1167 lines = strbreakup (str, "\n", -1);
1173 /* This loop has an inefficient worst-case, where
1174 * gtk_text_iter_get_text () is called repeatedly on
1179 if (limit && gtk_text_iter_compare (&search, limit) <= 0)
1182 if (backward_lines_match (&search, (const gchar**)lines,
1183 visible_only, slice, &match, &end))
1185 if (limit == NULL || (limit &&
1186 gtk_text_iter_compare (&end, limit) > 0))
1191 *match_start = match;
1198 if (gtk_text_iter_get_line_offset (&search) == 0)
1200 if (!gtk_text_iter_backward_line (&search))
1205 gtk_text_iter_set_line_offset (&search, 0);
1209 g_strfreev ((gchar**)lines);
1215 window_get_is_on_current_workspace (GtkWindow *window)
1217 GdkWindow *gdk_window;
1219 gdk_window = GTK_WIDGET (window)->window;
1221 return !(gdk_window_get_state (gdk_window) &
1222 GDK_WINDOW_STATE_ICONIFIED);
1228 /* Checks if the window is visible as in visible on the current workspace. */
1230 empathy_window_get_is_visible (GtkWindow *window)
1232 g_return_val_if_fail (GTK_IS_WINDOW (window), FALSE);
1234 return GTK_WIDGET_VISIBLE (GTK_WIDGET (window)) &&
1235 window_get_is_on_current_workspace (window);
1239 empathy_window_iconify (GtkWindow *window, GtkStatusIcon *status_icon)
1241 GdkRectangle icon_location;
1244 GdkWindow *gdk_window;
1246 gtk_status_icon_get_geometry (status_icon, NULL, &icon_location, NULL);
1247 gdk_window = GTK_WIDGET (window)->window;
1248 dpy = gdk_x11_drawable_get_xdisplay (gdk_window);
1250 data[0] = icon_location.x;
1251 data[1] = icon_location.y;
1252 data[2] = icon_location.width;
1253 data[3] = icon_location.height;
1255 XChangeProperty (dpy,
1256 GDK_WINDOW_XID (gdk_window),
1257 gdk_x11_get_xatom_by_name_for_display (gdk_drawable_get_display (gdk_window),
1258 "_NET_WM_ICON_GEOMETRY"),
1259 XA_CARDINAL, 32, PropModeReplace,
1260 (guchar *)&data, 4);
1262 gtk_window_set_skip_taskbar_hint (window, TRUE);
1263 gtk_window_iconify (window);
1267 /* Takes care of moving the window to the current workspace. */
1269 empathy_window_present (GtkWindow *window,
1270 gboolean steal_focus)
1272 gboolean on_current;
1275 g_return_if_fail (window != NULL);
1277 /* There are three cases: hidden, visible, visible on another
1281 on_current = window_get_is_on_current_workspace (window);
1283 if ( GTK_WIDGET_VISIBLE (GTK_WIDGET (window)) && !on_current) {
1284 /* Hide it so present brings it to the current workspace. */
1285 gtk_widget_hide (GTK_WIDGET (window));
1288 gtk_window_set_skip_taskbar_hint (window, FALSE);
1290 timestamp = gtk_get_current_event_time ();
1291 if (steal_focus && timestamp != GDK_CURRENT_TIME) {
1292 gtk_window_present_with_time (window, timestamp);
1294 gtk_window_present (window);
1299 empathy_get_toplevel_window (GtkWidget *widget)
1301 GtkWidget *toplevel;
1303 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1305 toplevel = gtk_widget_get_toplevel (widget);
1306 if (GTK_IS_WINDOW (toplevel) &&
1307 GTK_WIDGET_TOPLEVEL (toplevel)) {
1308 return GTK_WINDOW (toplevel);
1314 /* The URL opening code can't handle schemeless strings, so we try to be
1315 * smart and add http if there is no scheme or doesn't look like a mail
1316 * address. This should work in most cases, and let us click on strings
1317 * like "www.gnome.org".
1320 fixup_url (const gchar *url)
1324 if (!g_str_has_prefix (url, "http://") &&
1325 !strstr (url, ":/") &&
1326 !strstr (url, "@")) {
1327 real_url = g_strdup_printf ("http://%s", url);
1329 real_url = g_strdup (url);
1336 empathy_url_show (const char *url)
1341 real_url = fixup_url (url);
1342 res = gnome_vfs_url_show (real_url);
1343 if (res != GNOME_VFS_OK) {
1344 empathy_debug (DEBUG_DOMAIN, "Couldn't show URL %s: %s",
1346 gnome_vfs_result_to_string (res));
1353 link_button_hook (GtkLinkButton *button,
1357 empathy_url_show (link);
1361 empathy_link_button_new (const gchar *url,
1364 static gboolean hook = FALSE;
1368 gtk_link_button_set_uri_hook (link_button_hook, NULL, NULL);
1371 return gtk_link_button_new_with_label (url, title);
1375 empathy_toggle_button_set_state_quietly (GtkWidget *widget,
1380 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (widget));
1382 g_signal_handlers_block_by_func (widget, callback, user_data);
1383 g_object_set (widget, "active", active, NULL);
1384 g_signal_handlers_unblock_by_func (widget, callback, user_data);