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_state (McPresence state)
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_presence (EmpathyPresence *presence)
251 g_return_val_if_fail (EMPATHY_IS_PRESENCE (presence),
252 EMPATHY_IMAGE_OFFLINE);
254 state = empathy_presence_get_state (presence);
256 return empathy_icon_name_for_presence_state (state);
260 empathy_icon_name_for_contact (EmpathyContact *contact)
262 EmpathyPresence *presence;
264 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact),
265 EMPATHY_IMAGE_OFFLINE);
267 presence = empathy_contact_get_presence (contact);
269 return empathy_icon_name_for_presence (presence);
272 return EMPATHY_IMAGE_UNKNOWN;
276 empathy_pixbuf_from_data (gchar *data,
279 GdkPixbufLoader *loader;
280 GdkPixbuf *pixbuf = NULL;
281 GError *error = NULL;
287 loader = gdk_pixbuf_loader_new ();
288 if (!gdk_pixbuf_loader_write (loader, data, data_size, &error)) {
289 empathy_debug (DEBUG_DOMAIN, "Failed to write to pixbuf loader: %s",
290 error ? error->message : "No error given");
291 g_clear_error (&error);
292 g_object_unref (loader);
295 if (!gdk_pixbuf_loader_close (loader, &error)) {
296 empathy_debug (DEBUG_DOMAIN, "Failed to close pixbuf loader: %s",
297 error ? error->message : "No error given");
298 g_clear_error (&error);
299 g_object_unref (loader);
303 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
305 g_object_ref (pixbuf);
308 g_object_unref (loader);
314 pixbuf_from_avatar_size_prepared_cb (GdkPixbufLoader *loader,
317 struct SizeData *data)
319 g_return_if_fail (width > 0 && height > 0);
321 if (data->preserve_aspect_ratio && (data->width > 0 || data->height > 0)) {
322 if (width < data->width && height < data->height) {
327 if (data->width < 0) {
328 width = width * (double) data->height / (gdouble) height;
329 height = data->height;
330 } else if (data->height < 0) {
331 height = height * (double) data->width / (double) width;
333 } else if ((double) height * (double) data->width >
334 (double) width * (double) data->height) {
335 width = 0.5 + (double) width * (double) data->height / (double) height;
336 height = data->height;
338 height = 0.5 + (double) height * (double) data->width / (double) width;
342 if (data->width > 0) {
346 if (data->height > 0) {
347 height = data->height;
351 gdk_pixbuf_loader_set_size (loader, width, height);
355 empathy_avatar_pixbuf_roundify (GdkPixbuf *pixbuf)
357 gint width, height, rowstride;
360 width = gdk_pixbuf_get_width (pixbuf);
361 height = gdk_pixbuf_get_height (pixbuf);
362 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
363 pixels = gdk_pixbuf_get_pixels (pixbuf);
365 if (width < 6 || height < 6) {
373 pixels[rowstride + 3] = 0x80;
374 pixels[rowstride * 2 + 3] = 0xC0;
377 pixels[width * 4 - 1] = 0;
378 pixels[width * 4 - 5] = 0x80;
379 pixels[width * 4 - 9] = 0xC0;
380 pixels[rowstride + (width * 4) - 1] = 0x80;
381 pixels[(2 * rowstride) + (width * 4) - 1] = 0xC0;
384 pixels[(height - 1) * rowstride + 3] = 0;
385 pixels[(height - 1) * rowstride + 7] = 0x80;
386 pixels[(height - 1) * rowstride + 11] = 0xC0;
387 pixels[(height - 2) * rowstride + 3] = 0x80;
388 pixels[(height - 3) * rowstride + 3] = 0xC0;
391 pixels[height * rowstride - 1] = 0;
392 pixels[(height - 1) * rowstride - 1] = 0x80;
393 pixels[(height - 2) * rowstride - 1] = 0xC0;
394 pixels[height * rowstride - 5] = 0x80;
395 pixels[height * rowstride - 9] = 0xC0;
399 empathy_gdk_pixbuf_is_opaque (GdkPixbuf *pixbuf)
401 gint width, height, rowstride, i;
405 width = gdk_pixbuf_get_width (pixbuf);
406 height = gdk_pixbuf_get_height (pixbuf);
407 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
408 pixels = gdk_pixbuf_get_pixels (pixbuf);
411 for (i = 3; i < rowstride; i+=4) {
417 for (i = 1; i < height - 1; i++) {
418 row = pixels + (i*rowstride);
419 if (row[3] < 0xfe || row[rowstride-1] < 0xfe) {
424 row = pixels + ((height-1) * rowstride);
425 for (i = 3; i < rowstride; i+=4) {
435 empathy_pixbuf_from_avatar_scaled (EmpathyAvatar *avatar,
440 GdkPixbufLoader *loader;
441 struct SizeData data;
442 GError *error = NULL;
449 data.height = height;
450 data.preserve_aspect_ratio = TRUE;
452 loader = gdk_pixbuf_loader_new ();
454 g_signal_connect (loader, "size-prepared",
455 G_CALLBACK (pixbuf_from_avatar_size_prepared_cb),
458 if (!gdk_pixbuf_loader_write (loader, avatar->data, avatar->len, &error)) {
459 g_warning ("Couldn't write avatar image:%p with "
460 "length:%" G_GSIZE_FORMAT " to pixbuf loader: %s",
461 avatar->data, avatar->len, error->message);
462 g_error_free (error);
466 gdk_pixbuf_loader_close (loader, NULL);
468 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
469 if (!gdk_pixbuf_get_has_alpha (pixbuf)) {
470 GdkPixbuf *rounded_pixbuf;
472 rounded_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8,
473 gdk_pixbuf_get_width (pixbuf),
474 gdk_pixbuf_get_height (pixbuf));
475 gdk_pixbuf_copy_area (pixbuf, 0, 0,
476 gdk_pixbuf_get_width (pixbuf),
477 gdk_pixbuf_get_height (pixbuf),
480 pixbuf = rounded_pixbuf;
482 g_object_ref (pixbuf);
485 if (empathy_gdk_pixbuf_is_opaque (pixbuf)) {
486 empathy_avatar_pixbuf_roundify (pixbuf);
489 g_object_unref (loader);
495 empathy_pixbuf_avatar_from_contact_scaled (EmpathyContact *contact,
499 EmpathyAvatar *avatar;
501 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
503 avatar = empathy_contact_get_avatar (contact);
505 return empathy_pixbuf_from_avatar_scaled (avatar, width, height);
509 empathy_pixbuf_scale_down_if_necessary (GdkPixbuf *pixbuf, gint max_size)
514 width = gdk_pixbuf_get_width (pixbuf);
515 height = gdk_pixbuf_get_height (pixbuf);
517 if (width > max_size || height > max_size) {
518 factor = (gdouble) max_size / MAX (width, height);
520 width = width * factor;
521 height = height * factor;
523 return gdk_pixbuf_scale_simple (pixbuf,
528 return g_object_ref (pixbuf);
532 empathy_pixbuf_from_icon_name (const gchar *icon_name,
533 GtkIconSize icon_size)
536 GdkPixbuf *pixbuf = NULL;
537 GError *error = NULL;
545 theme = gtk_icon_theme_get_default ();
547 if (gtk_icon_size_lookup (icon_size, &w, &h)) {
551 pixbuf = gtk_icon_theme_load_icon (theme,
557 empathy_debug (DEBUG_DOMAIN, "Error loading icon: %s", error->message);
558 g_clear_error (&error);
564 /* Stolen from GtkSourceView, hence the weird intendation. Please keep it like
565 * that to make it easier to apply changes from the original code.
567 #define GTK_TEXT_UNKNOWN_CHAR 0xFFFC
569 /* this function acts like g_utf8_offset_to_pointer() except that if it finds a
570 * decomposable character it consumes the decomposition length from the given
571 * offset. So it's useful when the offset was calculated for the normalized
572 * version of str, but we need a pointer to str itself. */
574 pointer_from_offset_skipping_decomp (const gchar *str, gint offset)
576 gchar *casefold, *normal;
582 q = g_utf8_next_char (p);
583 casefold = g_utf8_casefold (p, q - p);
584 normal = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
585 offset -= g_utf8_strlen (normal, -1);
594 g_utf8_strcasestr (const gchar *haystack, const gchar *needle)
598 const gchar *ret = NULL;
601 gchar *caseless_haystack;
604 g_return_val_if_fail (haystack != NULL, NULL);
605 g_return_val_if_fail (needle != NULL, NULL);
607 casefold = g_utf8_casefold (haystack, -1);
608 caseless_haystack = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
611 needle_len = g_utf8_strlen (needle, -1);
612 haystack_len = g_utf8_strlen (caseless_haystack, -1);
616 ret = (gchar *)haystack;
620 if (haystack_len < needle_len)
626 p = (gchar*)caseless_haystack;
627 needle_len = strlen (needle);
632 if ((strncmp (p, needle, needle_len) == 0))
634 ret = pointer_from_offset_skipping_decomp (haystack, i);
638 p = g_utf8_next_char (p);
643 g_free (caseless_haystack);
649 g_utf8_caselessnmatch (const char *s1, const char *s2,
650 gssize n1, gssize n2)
653 gchar *normalized_s1;
654 gchar *normalized_s2;
657 gboolean ret = FALSE;
659 g_return_val_if_fail (s1 != NULL, FALSE);
660 g_return_val_if_fail (s2 != NULL, FALSE);
661 g_return_val_if_fail (n1 > 0, FALSE);
662 g_return_val_if_fail (n2 > 0, FALSE);
664 casefold = g_utf8_casefold (s1, n1);
665 normalized_s1 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
668 casefold = g_utf8_casefold (s2, n2);
669 normalized_s2 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
672 len_s1 = strlen (normalized_s1);
673 len_s2 = strlen (normalized_s2);
678 ret = (strncmp (normalized_s1, normalized_s2, len_s2) == 0);
681 g_free (normalized_s1);
682 g_free (normalized_s2);
688 forward_chars_with_skipping (GtkTextIter *iter,
690 gboolean skip_invisible,
691 gboolean skip_nontext,
692 gboolean skip_decomp)
696 g_return_if_fail (count >= 0);
702 gboolean ignored = FALSE;
704 /* minimal workaround to avoid the infinite loop of bug #168247.
705 * It doesn't fix the problemjust the symptom...
707 if (gtk_text_iter_is_end (iter))
710 if (skip_nontext && gtk_text_iter_get_char (iter) == GTK_TEXT_UNKNOWN_CHAR)
713 if (!ignored && skip_invisible &&
714 /* _gtk_text_btree_char_is_invisible (iter)*/ FALSE)
717 if (!ignored && skip_decomp)
719 /* being UTF8 correct sucks; this accounts for extra
720 offsets coming from canonical decompositions of
721 UTF8 characters (e.g. accented characters) which
722 g_utf8_normalize() performs */
727 buffer_len = g_unichar_to_utf8 (gtk_text_iter_get_char (iter), buffer);
728 normal = g_utf8_normalize (buffer, buffer_len, G_NORMALIZE_NFD);
729 i -= (g_utf8_strlen (normal, -1) - 1);
733 gtk_text_iter_forward_char (iter);
741 lines_match (const GtkTextIter *start,
743 gboolean visible_only,
745 GtkTextIter *match_start,
746 GtkTextIter *match_end)
753 if (*lines == NULL || **lines == '\0')
756 *match_start = *start;
763 gtk_text_iter_forward_line (&next);
765 /* No more text in buffer, but *lines is nonempty */
766 if (gtk_text_iter_equal (start, &next))
772 line_text = gtk_text_iter_get_visible_slice (start, &next);
774 line_text = gtk_text_iter_get_slice (start, &next);
779 line_text = gtk_text_iter_get_visible_text (start, &next);
781 line_text = gtk_text_iter_get_text (start, &next);
784 if (match_start) /* if this is the first line we're matching */
786 found = g_utf8_strcasestr (line_text, *lines);
790 /* If it's not the first line, we have to match from the
793 if (g_utf8_caselessnmatch (line_text, *lines, strlen (line_text),
806 /* Get offset to start of search string */
807 offset = g_utf8_strlen (line_text, found - line_text);
811 /* If match start needs to be returned, set it to the
812 * start of the search string.
814 forward_chars_with_skipping (&next, offset, visible_only, !slice, FALSE);
820 /* Go to end of search string */
821 forward_chars_with_skipping (&next, g_utf8_strlen (*lines, -1), visible_only, !slice, TRUE);
830 /* pass NULL for match_start, since we don't need to find the
833 return lines_match (&next, lines, visible_only, slice, NULL, match_end);
836 /* strsplit () that retains the delimiter as part of the string. */
838 strbreakup (const char *string,
839 const char *delimiter,
842 GSList *string_list = NULL, *slist;
843 gchar **str_array, *s, *casefold, *new_string;
846 g_return_val_if_fail (string != NULL, NULL);
847 g_return_val_if_fail (delimiter != NULL, NULL);
850 max_tokens = G_MAXINT;
852 s = strstr (string, delimiter);
855 guint delimiter_len = strlen (delimiter);
861 len = s - string + delimiter_len;
862 new_string = g_new (gchar, len + 1);
863 strncpy (new_string, string, len);
865 casefold = g_utf8_casefold (new_string, -1);
867 new_string = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
869 string_list = g_slist_prepend (string_list, new_string);
871 string = s + delimiter_len;
872 s = strstr (string, delimiter);
873 } while (--max_tokens && s);
879 casefold = g_utf8_casefold (string, -1);
880 new_string = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
882 string_list = g_slist_prepend (string_list, new_string);
885 str_array = g_new (gchar*, n);
889 str_array[i--] = NULL;
890 for (slist = string_list; slist; slist = slist->next)
891 str_array[i--] = slist->data;
893 g_slist_free (string_list);
899 empathy_text_iter_forward_search (const GtkTextIter *iter,
901 GtkTextIter *match_start,
902 GtkTextIter *match_end,
903 const GtkTextIter *limit)
905 gchar **lines = NULL;
907 gboolean retval = FALSE;
909 gboolean visible_only;
912 g_return_val_if_fail (iter != NULL, FALSE);
913 g_return_val_if_fail (str != NULL, FALSE);
915 if (limit && gtk_text_iter_compare (iter, limit) >= 0)
919 /* If we can move one char, return the empty string there */
922 if (gtk_text_iter_forward_char (&match)) {
923 if (limit && gtk_text_iter_equal (&match, limit)) {
928 *match_start = match;
942 /* locate all lines */
943 lines = strbreakup (str, "\n", -1);
948 /* This loop has an inefficient worst-case, where
949 * gtk_text_iter_get_text () is called repeatedly on
954 if (limit && gtk_text_iter_compare (&search, limit) >= 0) {
958 if (lines_match (&search, (const gchar**)lines,
959 visible_only, slice, &match, &end)) {
961 (limit && gtk_text_iter_compare (&end, limit) <= 0)) {
965 *match_start = match;
973 } while (gtk_text_iter_forward_line (&search));
975 g_strfreev ((gchar**)lines);
981 g_utf8_strrcasestr (const gchar *haystack, const gchar *needle)
985 const gchar *ret = NULL;
988 gchar *caseless_haystack;
991 g_return_val_if_fail (haystack != NULL, NULL);
992 g_return_val_if_fail (needle != NULL, NULL);
994 casefold = g_utf8_casefold (haystack, -1);
995 caseless_haystack = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
998 needle_len = g_utf8_strlen (needle, -1);
999 haystack_len = g_utf8_strlen (caseless_haystack, -1);
1001 if (needle_len == 0)
1003 ret = (gchar *)haystack;
1007 if (haystack_len < needle_len)
1013 i = haystack_len - needle_len;
1014 p = g_utf8_offset_to_pointer (caseless_haystack, i);
1015 needle_len = strlen (needle);
1017 while (p >= caseless_haystack)
1019 if (strncmp (p, needle, needle_len) == 0)
1021 ret = pointer_from_offset_skipping_decomp (haystack, i);
1025 p = g_utf8_prev_char (p);
1030 g_free (caseless_haystack);
1036 backward_lines_match (const GtkTextIter *start,
1037 const gchar **lines,
1038 gboolean visible_only,
1040 GtkTextIter *match_start,
1041 GtkTextIter *match_end)
1043 GtkTextIter line, next;
1048 if (*lines == NULL || **lines == '\0')
1051 *match_start = *start;
1053 *match_end = *start;
1057 line = next = *start;
1058 if (gtk_text_iter_get_line_offset (&next) == 0)
1060 if (!gtk_text_iter_backward_line (&next))
1064 gtk_text_iter_set_line_offset (&next, 0);
1069 line_text = gtk_text_iter_get_visible_slice (&next, &line);
1071 line_text = gtk_text_iter_get_slice (&next, &line);
1076 line_text = gtk_text_iter_get_visible_text (&next, &line);
1078 line_text = gtk_text_iter_get_text (&next, &line);
1081 if (match_start) /* if this is the first line we're matching */
1083 found = g_utf8_strrcasestr (line_text, *lines);
1087 /* If it's not the first line, we have to match from the
1088 * start of the line.
1090 if (g_utf8_caselessnmatch (line_text, *lines, strlen (line_text),
1103 /* Get offset to start of search string */
1104 offset = g_utf8_strlen (line_text, found - line_text);
1106 forward_chars_with_skipping (&next, offset, visible_only, !slice, FALSE);
1108 /* If match start needs to be returned, set it to the
1109 * start of the search string.
1113 *match_start = next;
1116 /* Go to end of search string */
1117 forward_chars_with_skipping (&next, g_utf8_strlen (*lines, -1), visible_only, !slice, TRUE);
1126 /* try to match the rest of the lines forward, passing NULL
1127 * for match_start so lines_match will try to match the entire
1129 return lines_match (&next, lines, visible_only,
1130 slice, NULL, match_end);
1134 empathy_text_iter_backward_search (const GtkTextIter *iter,
1136 GtkTextIter *match_start,
1137 GtkTextIter *match_end,
1138 const GtkTextIter *limit)
1140 gchar **lines = NULL;
1142 gboolean retval = FALSE;
1144 gboolean visible_only;
1147 g_return_val_if_fail (iter != NULL, FALSE);
1148 g_return_val_if_fail (str != NULL, FALSE);
1150 if (limit && gtk_text_iter_compare (iter, limit) <= 0)
1155 /* If we can move one char, return the empty string there */
1158 if (gtk_text_iter_backward_char (&match))
1160 if (limit && gtk_text_iter_equal (&match, limit))
1164 *match_start = match;
1175 visible_only = TRUE;
1178 /* locate all lines */
1179 lines = strbreakup (str, "\n", -1);
1185 /* This loop has an inefficient worst-case, where
1186 * gtk_text_iter_get_text () is called repeatedly on
1191 if (limit && gtk_text_iter_compare (&search, limit) <= 0)
1194 if (backward_lines_match (&search, (const gchar**)lines,
1195 visible_only, slice, &match, &end))
1197 if (limit == NULL || (limit &&
1198 gtk_text_iter_compare (&end, limit) > 0))
1203 *match_start = match;
1210 if (gtk_text_iter_get_line_offset (&search) == 0)
1212 if (!gtk_text_iter_backward_line (&search))
1217 gtk_text_iter_set_line_offset (&search, 0);
1221 g_strfreev ((gchar**)lines);
1227 empathy_window_get_is_visible (GtkWindow *window)
1229 GdkWindowState state;
1230 GdkWindow *gdk_window;
1232 g_return_val_if_fail (GTK_IS_WINDOW (window), FALSE);
1234 gdk_window = GTK_WIDGET (window)->window;
1239 state = gdk_window_get_state (gdk_window);
1240 if (state & (GDK_WINDOW_STATE_WITHDRAWN | GDK_WINDOW_STATE_ICONIFIED)) {
1248 empathy_window_iconify (GtkWindow *window, GtkStatusIcon *status_icon)
1250 GdkRectangle icon_location;
1253 GdkWindow *gdk_window;
1255 gtk_status_icon_get_geometry (status_icon, NULL, &icon_location, NULL);
1256 gdk_window = GTK_WIDGET (window)->window;
1257 dpy = gdk_x11_drawable_get_xdisplay (gdk_window);
1259 data[0] = icon_location.x;
1260 data[1] = icon_location.y;
1261 data[2] = icon_location.width;
1262 data[3] = icon_location.height;
1264 XChangeProperty (dpy,
1265 GDK_WINDOW_XID (gdk_window),
1266 gdk_x11_get_xatom_by_name_for_display (gdk_drawable_get_display (gdk_window),
1267 "_NET_WM_ICON_GEOMETRY"),
1268 XA_CARDINAL, 32, PropModeReplace,
1269 (guchar *)&data, 4);
1271 gtk_window_set_skip_taskbar_hint (window, TRUE);
1272 gtk_window_iconify (window);
1275 /* Takes care of moving the window to the current workspace. */
1277 empathy_window_present (GtkWindow *window,
1278 gboolean steal_focus)
1282 g_return_if_fail (GTK_IS_WINDOW (window));
1284 /* There are three cases: hidden, visible, visible on another
1288 if (!empathy_window_get_is_visible (window)) {
1289 /* Hide it so present brings it to the current workspace. */
1290 gtk_widget_hide (GTK_WIDGET (window));
1293 timestamp = gtk_get_current_event_time ();
1294 gtk_window_set_skip_taskbar_hint (window, FALSE);
1295 gtk_window_present_with_time (window, timestamp);
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);
1388 empathy_text_buffer_tag_set (GtkTextBuffer *buffer,
1389 const gchar *tag_name,
1390 const gchar *first_property_name,
1393 GtkTextTagTable *table;
1396 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
1397 g_return_val_if_fail (tag_name != NULL, NULL);
1399 table = gtk_text_buffer_get_tag_table (buffer);
1400 tag = gtk_text_tag_table_lookup (table, tag_name);
1403 tag = gtk_text_tag_new (tag_name);
1404 gtk_text_tag_table_add (table, tag);
1405 g_object_unref (tag);
1407 /* Clear the old values so that we don't affect the new theme. */
1409 "background-set", FALSE,
1410 "foreground-set", FALSE,
1411 "invisible-set", FALSE,
1412 "justification-set", FALSE,
1413 "paragraph-background-set", FALSE,
1414 "pixels-above-lines-set", FALSE,
1415 "pixels-below-lines-set", FALSE,
1420 "weight-set", FALSE,
1424 if (first_property_name) {
1427 va_start (list, first_property_name);
1428 g_object_set_valist (G_OBJECT (tag), first_property_name, list);