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_pixbuf_from_icon_name (const gchar *icon_name,
206 GtkIconSize icon_size)
209 GdkPixbuf *pixbuf = NULL;
210 GError *error = NULL;
214 theme = gtk_icon_theme_get_default ();
216 if (gtk_icon_size_lookup (icon_size, &w, &h)) {
220 pixbuf = gtk_icon_theme_load_icon (theme,
226 empathy_debug (DEBUG_DOMAIN, "Error loading icon: %s", error->message);
227 g_clear_error (&error);
234 empathy_pixbuf_from_smiley (EmpathySmiley type,
235 GtkIconSize icon_size)
237 const gchar *icon_id;
240 case EMPATHY_SMILEY_NORMAL: /* :) */
241 icon_id = "stock_smiley-1";
243 case EMPATHY_SMILEY_WINK: /* ;) */
244 icon_id = "stock_smiley-3";
246 case EMPATHY_SMILEY_BIGEYE: /* =) */
247 icon_id = "stock_smiley-2";
249 case EMPATHY_SMILEY_NOSE: /* :-) */
250 icon_id = "stock_smiley-7";
252 case EMPATHY_SMILEY_CRY: /* :'( */
253 icon_id = "stock_smiley-11";
255 case EMPATHY_SMILEY_SAD: /* :( */
256 icon_id = "stock_smiley-4";
258 case EMPATHY_SMILEY_SCEPTICAL: /* :/ */
259 icon_id = "stock_smiley-9";
261 case EMPATHY_SMILEY_BIGSMILE: /* :D */
262 icon_id = "stock_smiley-6";
264 case EMPATHY_SMILEY_INDIFFERENT: /* :| */
265 icon_id = "stock_smiley-8";
267 case EMPATHY_SMILEY_TOUNGE: /* :p */
268 icon_id = "stock_smiley-10";
270 case EMPATHY_SMILEY_SHOCKED: /* :o */
271 icon_id = "stock_smiley-5";
273 case EMPATHY_SMILEY_COOL: /* 8) */
274 icon_id = "stock_smiley-15";
276 case EMPATHY_SMILEY_SORRY: /* *| */
277 icon_id = "stock_smiley-12";
279 case EMPATHY_SMILEY_KISS: /* :* */
280 icon_id = "stock_smiley-13";
282 case EMPATHY_SMILEY_SHUTUP: /* :# */
283 icon_id = "stock_smiley-14";
285 case EMPATHY_SMILEY_YAWN: /* |O */
288 case EMPATHY_SMILEY_CONFUSED: /* :$ */
289 icon_id = "stock_smiley-17";
291 case EMPATHY_SMILEY_ANGEL: /* O) */
292 icon_id = "stock_smiley-18";
294 case EMPATHY_SMILEY_OOOH: /* :x */
295 icon_id = "stock_smiley-19";
297 case EMPATHY_SMILEY_LOOKAWAY: /* *) */
298 icon_id = "stock_smiley-20";
300 case EMPATHY_SMILEY_BLUSH: /* *S */
301 icon_id = "stock_smiley-23";
303 case EMPATHY_SMILEY_COOLBIGSMILE: /* 8D */
304 icon_id = "stock_smiley-25";
306 case EMPATHY_SMILEY_ANGRY: /* :@ */
307 icon_id = "stock_smiley-16";
309 case EMPATHY_SMILEY_BOSS: /* @) */
310 icon_id = "stock_smiley-21";
312 case EMPATHY_SMILEY_MONKEY: /* #) */
313 icon_id = "stock_smiley-22";
315 case EMPATHY_SMILEY_SILLY: /* O) */
316 icon_id = "stock_smiley-24";
318 case EMPATHY_SMILEY_SICK: /* +o( */
319 icon_id = "stock_smiley-26";
323 g_assert_not_reached ();
328 return empathy_pixbuf_from_icon_name (icon_id, icon_size);
332 empathy_icon_name_from_account (McAccount *account)
336 profile = mc_account_get_profile (account);
338 return mc_profile_get_icon_name (profile);
342 empathy_icon_name_for_presence_state (McPresence state)
345 case MC_PRESENCE_AVAILABLE:
346 return EMPATHY_IMAGE_AVAILABLE;
347 case MC_PRESENCE_DO_NOT_DISTURB:
348 return EMPATHY_IMAGE_BUSY;
349 case MC_PRESENCE_AWAY:
350 return EMPATHY_IMAGE_AWAY;
351 case MC_PRESENCE_EXTENDED_AWAY:
352 return EMPATHY_IMAGE_EXT_AWAY;
353 case MC_PRESENCE_HIDDEN:
354 return EMPATHY_IMAGE_HIDDEN;
355 case MC_PRESENCE_OFFLINE:
356 case MC_PRESENCE_UNSET:
357 return EMPATHY_IMAGE_OFFLINE;
359 g_assert_not_reached ();
366 empathy_icon_name_for_presence (EmpathyPresence *presence)
370 g_return_val_if_fail (EMPATHY_IS_PRESENCE (presence),
371 EMPATHY_IMAGE_OFFLINE);
373 state = empathy_presence_get_state (presence);
375 return empathy_icon_name_for_presence_state (state);
379 empathy_icon_name_for_contact (EmpathyContact *contact)
381 EmpathyPresence *presence;
383 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact),
384 EMPATHY_IMAGE_OFFLINE);
386 presence = empathy_contact_get_presence (contact);
388 return empathy_icon_name_for_presence (presence);
391 return EMPATHY_IMAGE_UNKNOWN;
395 pixbuf_from_avatar_size_prepared_cb (GdkPixbufLoader *loader,
398 struct SizeData *data)
400 g_return_if_fail (width > 0 && height > 0);
402 if (data->preserve_aspect_ratio && (data->width > 0 || data->height > 0)) {
403 if (width < data->width && height < data->height) {
408 if (data->width < 0) {
409 width = width * (double) data->height / (gdouble) height;
410 height = data->height;
411 } else if (data->height < 0) {
412 height = height * (double) data->width / (double) width;
414 } else if ((double) height * (double) data->width >
415 (double) width * (double) data->height) {
416 width = 0.5 + (double) width * (double) data->height / (double) height;
417 height = data->height;
419 height = 0.5 + (double) height * (double) data->width / (double) width;
423 if (data->width > 0) {
427 if (data->height > 0) {
428 height = data->height;
432 gdk_pixbuf_loader_set_size (loader, width, height);
436 empathy_avatar_pixbuf_roundify (GdkPixbuf *pixbuf)
438 gint width, height, rowstride;
441 width = gdk_pixbuf_get_width (pixbuf);
442 height = gdk_pixbuf_get_height (pixbuf);
443 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
444 pixels = gdk_pixbuf_get_pixels (pixbuf);
446 if (width < 6 || height < 6) {
454 pixels[rowstride + 3] = 0x80;
455 pixels[rowstride * 2 + 3] = 0xC0;
458 pixels[width * 4 - 1] = 0;
459 pixels[width * 4 - 5] = 0x80;
460 pixels[width * 4 - 9] = 0xC0;
461 pixels[rowstride + (width * 4) - 1] = 0x80;
462 pixels[(2 * rowstride) + (width * 4) - 1] = 0xC0;
465 pixels[(height - 1) * rowstride + 3] = 0;
466 pixels[(height - 1) * rowstride + 7] = 0x80;
467 pixels[(height - 1) * rowstride + 11] = 0xC0;
468 pixels[(height - 2) * rowstride + 3] = 0x80;
469 pixels[(height - 3) * rowstride + 3] = 0xC0;
472 pixels[height * rowstride - 1] = 0;
473 pixels[(height - 1) * rowstride - 1] = 0x80;
474 pixels[(height - 2) * rowstride - 1] = 0xC0;
475 pixels[height * rowstride - 5] = 0x80;
476 pixels[height * rowstride - 9] = 0xC0;
480 empathy_gdk_pixbuf_is_opaque (GdkPixbuf *pixbuf)
482 gint width, height, rowstride, i;
486 width = gdk_pixbuf_get_width (pixbuf);
487 height = gdk_pixbuf_get_height (pixbuf);
488 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
489 pixels = gdk_pixbuf_get_pixels (pixbuf);
492 for (i = 3; i < rowstride; i+=4) {
498 for (i = 1; i < height - 1; i++) {
499 row = pixels + (i*rowstride);
500 if (row[3] < 0xfe || row[rowstride-1] < 0xfe) {
505 row = pixels + ((height-1) * rowstride);
506 for (i = 3; i < rowstride; i+=4) {
516 empathy_pixbuf_from_avatar_scaled (EmpathyAvatar *avatar,
521 GdkPixbufLoader *loader;
522 struct SizeData data;
523 GError *error = NULL;
530 data.height = height;
531 data.preserve_aspect_ratio = TRUE;
533 loader = gdk_pixbuf_loader_new ();
535 g_signal_connect (loader, "size-prepared",
536 G_CALLBACK (pixbuf_from_avatar_size_prepared_cb),
539 if (!gdk_pixbuf_loader_write (loader, avatar->data, avatar->len, &error)) {
540 g_warning ("Couldn't write avatar image:%p with "
541 "length:%" G_GSIZE_FORMAT " to pixbuf loader: %s",
542 avatar->data, avatar->len, error->message);
543 g_error_free (error);
547 gdk_pixbuf_loader_close (loader, NULL);
549 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
550 if (!gdk_pixbuf_get_has_alpha (pixbuf)) {
551 GdkPixbuf *rounded_pixbuf;
553 rounded_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8,
554 gdk_pixbuf_get_width (pixbuf),
555 gdk_pixbuf_get_height (pixbuf));
556 gdk_pixbuf_copy_area (pixbuf, 0, 0,
557 gdk_pixbuf_get_width (pixbuf),
558 gdk_pixbuf_get_height (pixbuf),
561 pixbuf = rounded_pixbuf;
563 g_object_ref (pixbuf);
566 if (empathy_gdk_pixbuf_is_opaque (pixbuf)) {
567 empathy_avatar_pixbuf_roundify (pixbuf);
570 g_object_unref (loader);
576 empathy_pixbuf_avatar_from_contact_scaled (EmpathyContact *contact,
580 EmpathyAvatar *avatar;
582 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
584 avatar = empathy_contact_get_avatar (contact);
586 return empathy_pixbuf_from_avatar_scaled (avatar, width, height);
589 /* Stolen from GtkSourceView, hence the weird intendation. Please keep it like
590 * that to make it easier to apply changes from the original code.
592 #define GTK_TEXT_UNKNOWN_CHAR 0xFFFC
594 /* this function acts like g_utf8_offset_to_pointer() except that if it finds a
595 * decomposable character it consumes the decomposition length from the given
596 * offset. So it's useful when the offset was calculated for the normalized
597 * version of str, but we need a pointer to str itself. */
599 pointer_from_offset_skipping_decomp (const gchar *str, gint offset)
601 gchar *casefold, *normal;
607 q = g_utf8_next_char (p);
608 casefold = g_utf8_casefold (p, q - p);
609 normal = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
610 offset -= g_utf8_strlen (normal, -1);
619 g_utf8_strcasestr (const gchar *haystack, const gchar *needle)
623 const gchar *ret = NULL;
626 gchar *caseless_haystack;
629 g_return_val_if_fail (haystack != NULL, NULL);
630 g_return_val_if_fail (needle != NULL, NULL);
632 casefold = g_utf8_casefold (haystack, -1);
633 caseless_haystack = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
636 needle_len = g_utf8_strlen (needle, -1);
637 haystack_len = g_utf8_strlen (caseless_haystack, -1);
641 ret = (gchar *)haystack;
645 if (haystack_len < needle_len)
651 p = (gchar*)caseless_haystack;
652 needle_len = strlen (needle);
657 if ((strncmp (p, needle, needle_len) == 0))
659 ret = pointer_from_offset_skipping_decomp (haystack, i);
663 p = g_utf8_next_char (p);
668 g_free (caseless_haystack);
674 g_utf8_caselessnmatch (const char *s1, const char *s2,
675 gssize n1, gssize n2)
678 gchar *normalized_s1;
679 gchar *normalized_s2;
682 gboolean ret = FALSE;
684 g_return_val_if_fail (s1 != NULL, FALSE);
685 g_return_val_if_fail (s2 != NULL, FALSE);
686 g_return_val_if_fail (n1 > 0, FALSE);
687 g_return_val_if_fail (n2 > 0, FALSE);
689 casefold = g_utf8_casefold (s1, n1);
690 normalized_s1 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
693 casefold = g_utf8_casefold (s2, n2);
694 normalized_s2 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
697 len_s1 = strlen (normalized_s1);
698 len_s2 = strlen (normalized_s2);
703 ret = (strncmp (normalized_s1, normalized_s2, len_s2) == 0);
706 g_free (normalized_s1);
707 g_free (normalized_s2);
713 forward_chars_with_skipping (GtkTextIter *iter,
715 gboolean skip_invisible,
716 gboolean skip_nontext,
717 gboolean skip_decomp)
721 g_return_if_fail (count >= 0);
727 gboolean ignored = FALSE;
729 /* minimal workaround to avoid the infinite loop of bug #168247.
730 * It doesn't fix the problemjust the symptom...
732 if (gtk_text_iter_is_end (iter))
735 if (skip_nontext && gtk_text_iter_get_char (iter) == GTK_TEXT_UNKNOWN_CHAR)
738 if (!ignored && skip_invisible &&
739 /* _gtk_text_btree_char_is_invisible (iter)*/ FALSE)
742 if (!ignored && skip_decomp)
744 /* being UTF8 correct sucks; this accounts for extra
745 offsets coming from canonical decompositions of
746 UTF8 characters (e.g. accented characters) which
747 g_utf8_normalize() performs */
752 buffer_len = g_unichar_to_utf8 (gtk_text_iter_get_char (iter), buffer);
753 normal = g_utf8_normalize (buffer, buffer_len, G_NORMALIZE_NFD);
754 i -= (g_utf8_strlen (normal, -1) - 1);
758 gtk_text_iter_forward_char (iter);
766 lines_match (const GtkTextIter *start,
768 gboolean visible_only,
770 GtkTextIter *match_start,
771 GtkTextIter *match_end)
778 if (*lines == NULL || **lines == '\0')
781 *match_start = *start;
788 gtk_text_iter_forward_line (&next);
790 /* No more text in buffer, but *lines is nonempty */
791 if (gtk_text_iter_equal (start, &next))
797 line_text = gtk_text_iter_get_visible_slice (start, &next);
799 line_text = gtk_text_iter_get_slice (start, &next);
804 line_text = gtk_text_iter_get_visible_text (start, &next);
806 line_text = gtk_text_iter_get_text (start, &next);
809 if (match_start) /* if this is the first line we're matching */
811 found = g_utf8_strcasestr (line_text, *lines);
815 /* If it's not the first line, we have to match from the
818 if (g_utf8_caselessnmatch (line_text, *lines, strlen (line_text),
831 /* Get offset to start of search string */
832 offset = g_utf8_strlen (line_text, found - line_text);
836 /* If match start needs to be returned, set it to the
837 * start of the search string.
839 forward_chars_with_skipping (&next, offset, visible_only, !slice, FALSE);
845 /* Go to end of search string */
846 forward_chars_with_skipping (&next, g_utf8_strlen (*lines, -1), visible_only, !slice, TRUE);
855 /* pass NULL for match_start, since we don't need to find the
858 return lines_match (&next, lines, visible_only, slice, NULL, match_end);
861 /* strsplit () that retains the delimiter as part of the string. */
863 strbreakup (const char *string,
864 const char *delimiter,
867 GSList *string_list = NULL, *slist;
868 gchar **str_array, *s, *casefold, *new_string;
871 g_return_val_if_fail (string != NULL, NULL);
872 g_return_val_if_fail (delimiter != NULL, NULL);
875 max_tokens = G_MAXINT;
877 s = strstr (string, delimiter);
880 guint delimiter_len = strlen (delimiter);
886 len = s - string + delimiter_len;
887 new_string = g_new (gchar, len + 1);
888 strncpy (new_string, string, len);
890 casefold = g_utf8_casefold (new_string, -1);
892 new_string = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
894 string_list = g_slist_prepend (string_list, new_string);
896 string = s + delimiter_len;
897 s = strstr (string, delimiter);
898 } while (--max_tokens && s);
904 casefold = g_utf8_casefold (string, -1);
905 new_string = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
907 string_list = g_slist_prepend (string_list, new_string);
910 str_array = g_new (gchar*, n);
914 str_array[i--] = NULL;
915 for (slist = string_list; slist; slist = slist->next)
916 str_array[i--] = slist->data;
918 g_slist_free (string_list);
924 empathy_text_iter_forward_search (const GtkTextIter *iter,
926 GtkTextIter *match_start,
927 GtkTextIter *match_end,
928 const GtkTextIter *limit)
930 gchar **lines = NULL;
932 gboolean retval = FALSE;
934 gboolean visible_only;
937 g_return_val_if_fail (iter != NULL, FALSE);
938 g_return_val_if_fail (str != NULL, FALSE);
940 if (limit && gtk_text_iter_compare (iter, limit) >= 0)
944 /* If we can move one char, return the empty string there */
947 if (gtk_text_iter_forward_char (&match)) {
948 if (limit && gtk_text_iter_equal (&match, limit)) {
953 *match_start = match;
967 /* locate all lines */
968 lines = strbreakup (str, "\n", -1);
973 /* This loop has an inefficient worst-case, where
974 * gtk_text_iter_get_text () is called repeatedly on
979 if (limit && gtk_text_iter_compare (&search, limit) >= 0) {
983 if (lines_match (&search, (const gchar**)lines,
984 visible_only, slice, &match, &end)) {
986 (limit && gtk_text_iter_compare (&end, limit) <= 0)) {
990 *match_start = match;
998 } while (gtk_text_iter_forward_line (&search));
1000 g_strfreev ((gchar**)lines);
1005 static const gchar *
1006 g_utf8_strrcasestr (const gchar *haystack, const gchar *needle)
1010 const gchar *ret = NULL;
1013 gchar *caseless_haystack;
1016 g_return_val_if_fail (haystack != NULL, NULL);
1017 g_return_val_if_fail (needle != NULL, NULL);
1019 casefold = g_utf8_casefold (haystack, -1);
1020 caseless_haystack = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
1023 needle_len = g_utf8_strlen (needle, -1);
1024 haystack_len = g_utf8_strlen (caseless_haystack, -1);
1026 if (needle_len == 0)
1028 ret = (gchar *)haystack;
1032 if (haystack_len < needle_len)
1038 i = haystack_len - needle_len;
1039 p = g_utf8_offset_to_pointer (caseless_haystack, i);
1040 needle_len = strlen (needle);
1042 while (p >= caseless_haystack)
1044 if (strncmp (p, needle, needle_len) == 0)
1046 ret = pointer_from_offset_skipping_decomp (haystack, i);
1050 p = g_utf8_prev_char (p);
1055 g_free (caseless_haystack);
1061 backward_lines_match (const GtkTextIter *start,
1062 const gchar **lines,
1063 gboolean visible_only,
1065 GtkTextIter *match_start,
1066 GtkTextIter *match_end)
1068 GtkTextIter line, next;
1073 if (*lines == NULL || **lines == '\0')
1076 *match_start = *start;
1078 *match_end = *start;
1082 line = next = *start;
1083 if (gtk_text_iter_get_line_offset (&next) == 0)
1085 if (!gtk_text_iter_backward_line (&next))
1089 gtk_text_iter_set_line_offset (&next, 0);
1094 line_text = gtk_text_iter_get_visible_slice (&next, &line);
1096 line_text = gtk_text_iter_get_slice (&next, &line);
1101 line_text = gtk_text_iter_get_visible_text (&next, &line);
1103 line_text = gtk_text_iter_get_text (&next, &line);
1106 if (match_start) /* if this is the first line we're matching */
1108 found = g_utf8_strrcasestr (line_text, *lines);
1112 /* If it's not the first line, we have to match from the
1113 * start of the line.
1115 if (g_utf8_caselessnmatch (line_text, *lines, strlen (line_text),
1128 /* Get offset to start of search string */
1129 offset = g_utf8_strlen (line_text, found - line_text);
1131 forward_chars_with_skipping (&next, offset, visible_only, !slice, FALSE);
1133 /* If match start needs to be returned, set it to the
1134 * start of the search string.
1138 *match_start = next;
1141 /* Go to end of search string */
1142 forward_chars_with_skipping (&next, g_utf8_strlen (*lines, -1), visible_only, !slice, TRUE);
1151 /* try to match the rest of the lines forward, passing NULL
1152 * for match_start so lines_match will try to match the entire
1154 return lines_match (&next, lines, visible_only,
1155 slice, NULL, match_end);
1159 empathy_text_iter_backward_search (const GtkTextIter *iter,
1161 GtkTextIter *match_start,
1162 GtkTextIter *match_end,
1163 const GtkTextIter *limit)
1165 gchar **lines = NULL;
1167 gboolean retval = FALSE;
1169 gboolean visible_only;
1172 g_return_val_if_fail (iter != NULL, FALSE);
1173 g_return_val_if_fail (str != NULL, FALSE);
1175 if (limit && gtk_text_iter_compare (iter, limit) <= 0)
1180 /* If we can move one char, return the empty string there */
1183 if (gtk_text_iter_backward_char (&match))
1185 if (limit && gtk_text_iter_equal (&match, limit))
1189 *match_start = match;
1200 visible_only = TRUE;
1203 /* locate all lines */
1204 lines = strbreakup (str, "\n", -1);
1210 /* This loop has an inefficient worst-case, where
1211 * gtk_text_iter_get_text () is called repeatedly on
1216 if (limit && gtk_text_iter_compare (&search, limit) <= 0)
1219 if (backward_lines_match (&search, (const gchar**)lines,
1220 visible_only, slice, &match, &end))
1222 if (limit == NULL || (limit &&
1223 gtk_text_iter_compare (&end, limit) > 0))
1228 *match_start = match;
1235 if (gtk_text_iter_get_line_offset (&search) == 0)
1237 if (!gtk_text_iter_backward_line (&search))
1242 gtk_text_iter_set_line_offset (&search, 0);
1246 g_strfreev ((gchar**)lines);
1252 window_get_is_on_current_workspace (GtkWindow *window)
1254 GdkWindow *gdk_window;
1256 gdk_window = GTK_WIDGET (window)->window;
1258 return !(gdk_window_get_state (gdk_window) &
1259 GDK_WINDOW_STATE_ICONIFIED);
1265 /* Checks if the window is visible as in visible on the current workspace. */
1267 empathy_window_get_is_visible (GtkWindow *window)
1269 g_return_val_if_fail (GTK_IS_WINDOW (window), FALSE);
1271 return GTK_WIDGET_VISIBLE (GTK_WIDGET (window)) &&
1272 window_get_is_on_current_workspace (window);
1276 empathy_window_iconify (GtkWindow *window, GtkStatusIcon *status_icon)
1278 GdkRectangle icon_location;
1281 GdkWindow *gdk_window;
1283 gtk_status_icon_get_geometry (status_icon, NULL, &icon_location, NULL);
1284 gdk_window = GTK_WIDGET (window)->window;
1285 dpy = gdk_x11_drawable_get_xdisplay (gdk_window);
1287 data[0] = icon_location.x;
1288 data[1] = icon_location.y;
1289 data[2] = icon_location.width;
1290 data[3] = icon_location.height;
1292 XChangeProperty (dpy,
1293 GDK_WINDOW_XID (gdk_window),
1294 gdk_x11_get_xatom_by_name_for_display (gdk_drawable_get_display (gdk_window),
1295 "_NET_WM_ICON_GEOMETRY"),
1296 XA_CARDINAL, 32, PropModeReplace,
1297 (guchar *)&data, 4);
1299 gtk_window_set_skip_taskbar_hint (window, TRUE);
1300 gtk_window_iconify (window);
1304 /* Takes care of moving the window to the current workspace. */
1306 empathy_window_present (GtkWindow *window,
1307 gboolean steal_focus)
1309 gboolean on_current;
1312 g_return_if_fail (window != NULL);
1314 /* There are three cases: hidden, visible, visible on another
1318 on_current = window_get_is_on_current_workspace (window);
1320 if ( GTK_WIDGET_VISIBLE (GTK_WIDGET (window)) && !on_current) {
1321 /* Hide it so present brings it to the current workspace. */
1322 gtk_widget_hide (GTK_WIDGET (window));
1325 gtk_window_set_skip_taskbar_hint (window, FALSE);
1327 timestamp = gtk_get_current_event_time ();
1328 if (steal_focus && timestamp != GDK_CURRENT_TIME) {
1329 gtk_window_present_with_time (window, timestamp);
1331 gtk_window_present (window);
1336 empathy_get_toplevel_window (GtkWidget *widget)
1338 GtkWidget *toplevel;
1340 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1342 toplevel = gtk_widget_get_toplevel (widget);
1343 if (GTK_IS_WINDOW (toplevel) &&
1344 GTK_WIDGET_TOPLEVEL (toplevel)) {
1345 return GTK_WINDOW (toplevel);
1351 /* The URL opening code can't handle schemeless strings, so we try to be
1352 * smart and add http if there is no scheme or doesn't look like a mail
1353 * address. This should work in most cases, and let us click on strings
1354 * like "www.gnome.org".
1357 fixup_url (const gchar *url)
1361 if (!g_str_has_prefix (url, "http://") &&
1362 !strstr (url, ":/") &&
1363 !strstr (url, "@")) {
1364 real_url = g_strdup_printf ("http://%s", url);
1366 real_url = g_strdup (url);
1373 empathy_url_show (const char *url)
1378 real_url = fixup_url (url);
1379 res = gnome_vfs_url_show (real_url);
1380 if (res != GNOME_VFS_OK) {
1381 empathy_debug (DEBUG_DOMAIN, "Couldn't show URL %s: %s",
1383 gnome_vfs_result_to_string (res));
1390 link_button_hook (GtkLinkButton *button,
1394 empathy_url_show (link);
1398 empathy_link_button_new (const gchar *url,
1401 static gboolean hook = FALSE;
1405 gtk_link_button_set_uri_hook (link_button_hook, NULL, NULL);
1408 return gtk_link_button_new_with_label (url, title);
1412 empathy_toggle_button_set_state_quietly (GtkWidget *widget,
1417 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (widget));
1419 g_signal_handlers_block_by_func (widget, callback, user_data);
1420 g_object_set (widget, "active", active, NULL);
1421 g_signal_handlers_unblock_by_func (widget, callback, user_data);