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):
33 #include <glib/gi18n.h>
35 #include <glade/glade.h>
36 #include <libgnome/libgnome.h>
38 #include <libmissioncontrol/mc-profile.h>
40 #include <libempathy/gossip-paths.h>
41 #include <libempathy/gossip-debug.h>
43 #include "gossip-ui-utils.h"
44 #include "gossip-stock.h"
49 gboolean preserve_aspect_ratio;
53 get_glade_file (const gchar *filename,
56 const gchar *first_required_widget,
62 GtkWidget **widget_ptr;
64 path = gossip_paths_get_glade_path (filename);
65 gui = glade_xml_new (path, root, domain);
69 g_warning ("Couldn't find necessary glade file '%s'", filename);
73 for (name = first_required_widget; name; name = va_arg (args, char *)) {
74 widget_ptr = va_arg (args, void *);
76 *widget_ptr = glade_xml_get_widget (gui, name);
79 g_warning ("Glade file '%s' is missing widget '%s'.",
89 gossip_glade_get_file_simple (const gchar *filename,
92 const gchar *first_required_widget, ...)
97 va_start (args, first_required_widget);
99 gui = get_glade_file (filename,
102 first_required_widget,
111 g_object_unref (gui);
115 gossip_glade_get_file (const gchar *filename,
118 const gchar *first_required_widget, ...)
123 va_start (args, first_required_widget);
125 gui = get_glade_file (filename,
128 first_required_widget,
141 gossip_glade_connect (GladeXML *gui,
143 gchar *first_widget, ...)
151 va_start (args, first_widget);
153 for (name = first_widget; name; name = va_arg (args, char *)) {
154 signal = va_arg (args, void *);
155 callback = va_arg (args, void *);
157 widget = glade_xml_get_widget (gui, name);
159 g_warning ("Glade file is missing widget '%s', aborting",
164 g_signal_connect (widget,
166 G_CALLBACK (callback),
174 gossip_glade_setup_size_group (GladeXML *gui,
175 GtkSizeGroupMode mode,
176 gchar *first_widget, ...)
180 GtkSizeGroup *size_group;
183 va_start (args, first_widget);
185 size_group = gtk_size_group_new (mode);
187 for (name = first_widget; name; name = va_arg (args, char *)) {
188 widget = glade_xml_get_widget (gui, name);
190 g_warning ("Glade file is missing widget '%s'", name);
194 gtk_size_group_add_widget (size_group, widget);
197 g_object_unref (size_group);
203 gossip_pixbuf_from_smiley (GossipSmiley type,
204 GtkIconSize icon_size)
207 GdkPixbuf *pixbuf = NULL;
208 GError *error = NULL;
211 const gchar *icon_id;
213 theme = gtk_icon_theme_get_default ();
215 if (!gtk_icon_size_lookup (icon_size, &w, &h)) {
222 case GOSSIP_SMILEY_NORMAL: /* :) */
223 icon_id = "stock_smiley-1";
225 case GOSSIP_SMILEY_WINK: /* ;) */
226 icon_id = "stock_smiley-3";
228 case GOSSIP_SMILEY_BIGEYE: /* =) */
229 icon_id = "stock_smiley-2";
231 case GOSSIP_SMILEY_NOSE: /* :-) */
232 icon_id = "stock_smiley-7";
234 case GOSSIP_SMILEY_CRY: /* :'( */
235 icon_id = "stock_smiley-11";
237 case GOSSIP_SMILEY_SAD: /* :( */
238 icon_id = "stock_smiley-4";
240 case GOSSIP_SMILEY_SCEPTICAL: /* :/ */
241 icon_id = "stock_smiley-9";
243 case GOSSIP_SMILEY_BIGSMILE: /* :D */
244 icon_id = "stock_smiley-6";
246 case GOSSIP_SMILEY_INDIFFERENT: /* :| */
247 icon_id = "stock_smiley-8";
249 case GOSSIP_SMILEY_TOUNGE: /* :p */
250 icon_id = "stock_smiley-10";
252 case GOSSIP_SMILEY_SHOCKED: /* :o */
253 icon_id = "stock_smiley-5";
255 case GOSSIP_SMILEY_COOL: /* 8) */
256 icon_id = "stock_smiley-15";
258 case GOSSIP_SMILEY_SORRY: /* *| */
259 icon_id = "stock_smiley-12";
261 case GOSSIP_SMILEY_KISS: /* :* */
262 icon_id = "stock_smiley-13";
264 case GOSSIP_SMILEY_SHUTUP: /* :# */
265 icon_id = "stock_smiley-14";
267 case GOSSIP_SMILEY_YAWN: /* |O */
270 case GOSSIP_SMILEY_CONFUSED: /* :$ */
271 icon_id = "stock_smiley-17";
273 case GOSSIP_SMILEY_ANGEL: /* O) */
274 icon_id = "stock_smiley-18";
276 case GOSSIP_SMILEY_OOOH: /* :x */
277 icon_id = "stock_smiley-19";
279 case GOSSIP_SMILEY_LOOKAWAY: /* *) */
280 icon_id = "stock_smiley-20";
282 case GOSSIP_SMILEY_BLUSH: /* *S */
283 icon_id = "stock_smiley-23";
285 case GOSSIP_SMILEY_COOLBIGSMILE: /* 8D */
286 icon_id = "stock_smiley-25";
288 case GOSSIP_SMILEY_ANGRY: /* :@ */
289 icon_id = "stock_smiley-16";
291 case GOSSIP_SMILEY_BOSS: /* @) */
292 icon_id = "stock_smiley-21";
294 case GOSSIP_SMILEY_MONKEY: /* #) */
295 icon_id = "stock_smiley-22";
297 case GOSSIP_SMILEY_SILLY: /* O) */
298 icon_id = "stock_smiley-24";
300 case GOSSIP_SMILEY_SICK: /* +o( */
301 icon_id = "stock_smiley-26";
305 g_assert_not_reached ();
309 pixbuf = gtk_icon_theme_load_icon (theme,
310 icon_id, /* icon name */
319 gossip_pixbuf_from_profile (McProfile *profile,
320 GtkIconSize icon_size)
325 const gchar *icon_id = NULL;
326 GError *error = NULL;
328 theme = gtk_icon_theme_get_default ();
330 if (gtk_icon_size_lookup (icon_size, &w, &h)) {
334 icon_id = mc_profile_get_icon_name (profile);
336 theme = gtk_icon_theme_get_default ();
337 return gtk_icon_theme_load_icon (theme,
338 icon_id, /* Icon name */
345 gossip_pixbuf_from_account (McAccount *account,
346 GtkIconSize icon_size)
350 profile = mc_account_get_profile (account);
352 return gossip_pixbuf_from_profile (profile, icon_size);
356 gossip_pixbuf_for_presence_state (GossipPresenceState state)
358 const gchar *stock = NULL;
361 case GOSSIP_PRESENCE_STATE_AVAILABLE:
362 stock = GOSSIP_STOCK_AVAILABLE;
364 case GOSSIP_PRESENCE_STATE_BUSY:
365 stock = GOSSIP_STOCK_BUSY;
367 case GOSSIP_PRESENCE_STATE_AWAY:
368 stock = GOSSIP_STOCK_AWAY;
370 case GOSSIP_PRESENCE_STATE_EXT_AWAY:
371 stock = GOSSIP_STOCK_EXT_AWAY;
373 case GOSSIP_PRESENCE_STATE_HIDDEN:
374 case GOSSIP_PRESENCE_STATE_UNAVAILABLE:
375 stock = GOSSIP_STOCK_OFFLINE;
379 return gossip_stock_render (stock, GTK_ICON_SIZE_MENU);
383 gossip_pixbuf_for_presence (GossipPresence *presence)
385 GossipPresenceState state;
387 g_return_val_if_fail (GOSSIP_IS_PRESENCE (presence),
388 gossip_pixbuf_offline ());
390 state = gossip_presence_get_state (presence);
392 return gossip_pixbuf_for_presence_state (state);
396 gossip_pixbuf_for_contact (GossipContact *contact)
398 GossipPresence *presence;
399 GossipSubscription subscription;
401 g_return_val_if_fail (GOSSIP_IS_CONTACT (contact),
402 gossip_pixbuf_offline ());
404 presence = gossip_contact_get_presence (contact);
407 return gossip_pixbuf_for_presence (presence);
410 subscription = gossip_contact_get_subscription (contact);
412 if (subscription != GOSSIP_SUBSCRIPTION_BOTH &&
413 subscription != GOSSIP_SUBSCRIPTION_TO) {
414 return gossip_stock_render (GOSSIP_STOCK_PENDING,
418 return gossip_pixbuf_offline ();
422 gossip_pixbuf_offline (void)
424 return gossip_stock_render (GOSSIP_STOCK_OFFLINE,
429 gossip_pixbuf_avatar_from_contact (GossipContact *contact)
432 GdkPixbufLoader *loader;
433 GossipAvatar *avatar;
434 GError *error = NULL;
436 g_return_val_if_fail (GOSSIP_IS_CONTACT (contact), NULL);
438 avatar = gossip_contact_get_avatar (contact);
443 loader = gdk_pixbuf_loader_new ();
445 if (!gdk_pixbuf_loader_write (loader, avatar->data, avatar->len, &error)) {
446 g_warning ("Couldn't write avatar image:%p with "
447 "length:%" G_GSIZE_FORMAT " to pixbuf loader: %s",
448 avatar->data, avatar->len, error->message);
449 g_error_free (error);
453 gdk_pixbuf_loader_close (loader, NULL);
455 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
457 g_object_ref (pixbuf);
458 g_object_unref (loader);
464 pixbuf_from_avatar_size_prepared_cb (GdkPixbufLoader *loader,
467 struct SizeData *data)
469 g_return_if_fail (width > 0 && height > 0);
471 if (data->preserve_aspect_ratio && (data->width > 0 || data->height > 0)) {
472 if (width < data->width && height < data->height) {
477 if (data->width < 0) {
478 width = width * (double) data->height / (gdouble) height;
479 height = data->height;
480 } else if (data->height < 0) {
481 height = height * (double) data->width / (double) width;
483 } else if ((double) height * (double) data->width >
484 (double) width * (double) data->height) {
485 width = 0.5 + (double) width * (double) data->height / (double) height;
486 height = data->height;
488 height = 0.5 + (double) height * (double) data->width / (double) width;
492 if (data->width > 0) {
496 if (data->height > 0) {
497 height = data->height;
501 gdk_pixbuf_loader_set_size (loader, width, height);
505 gossip_pixbuf_from_avatar_scaled (GossipAvatar *avatar,
510 GdkPixbufLoader *loader;
511 struct SizeData data;
512 GError *error = NULL;
519 data.height = height;
520 data.preserve_aspect_ratio = TRUE;
522 loader = gdk_pixbuf_loader_new ();
524 g_signal_connect (loader, "size-prepared",
525 G_CALLBACK (pixbuf_from_avatar_size_prepared_cb),
528 if (!gdk_pixbuf_loader_write (loader, avatar->data, avatar->len, &error)) {
529 g_warning ("Couldn't write avatar image:%p with "
530 "length:%" G_GSIZE_FORMAT " to pixbuf loader: %s",
531 avatar->data, avatar->len, error->message);
532 g_error_free (error);
536 gdk_pixbuf_loader_close (loader, NULL);
538 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
540 g_object_ref (pixbuf);
541 g_object_unref (loader);
547 gossip_pixbuf_avatar_from_contact_scaled (GossipContact *contact,
551 GossipAvatar *avatar;
553 g_return_val_if_fail (GOSSIP_IS_CONTACT (contact), NULL);
555 avatar = gossip_contact_get_avatar (contact);
557 return gossip_pixbuf_from_avatar_scaled (avatar, width, height);
559 /* Stolen from GtkSourceView, hence the weird intendation. Please keep it like
560 * that to make it easier to apply changes from the original code.
562 #define GTK_TEXT_UNKNOWN_CHAR 0xFFFC
564 /* this function acts like g_utf8_offset_to_pointer() except that if it finds a
565 * decomposable character it consumes the decomposition length from the given
566 * offset. So it's useful when the offset was calculated for the normalized
567 * version of str, but we need a pointer to str itself. */
569 pointer_from_offset_skipping_decomp (const gchar *str, gint offset)
571 gchar *casefold, *normal;
577 q = g_utf8_next_char (p);
578 casefold = g_utf8_casefold (p, q - p);
579 normal = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
580 offset -= g_utf8_strlen (normal, -1);
589 g_utf8_strcasestr (const gchar *haystack, const gchar *needle)
593 const gchar *ret = NULL;
596 gchar *caseless_haystack;
599 g_return_val_if_fail (haystack != NULL, NULL);
600 g_return_val_if_fail (needle != NULL, NULL);
602 casefold = g_utf8_casefold (haystack, -1);
603 caseless_haystack = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
606 needle_len = g_utf8_strlen (needle, -1);
607 haystack_len = g_utf8_strlen (caseless_haystack, -1);
611 ret = (gchar *)haystack;
615 if (haystack_len < needle_len)
621 p = (gchar*)caseless_haystack;
622 needle_len = strlen (needle);
627 if ((strncmp (p, needle, needle_len) == 0))
629 ret = pointer_from_offset_skipping_decomp (haystack, i);
633 p = g_utf8_next_char (p);
638 g_free (caseless_haystack);
644 g_utf8_caselessnmatch (const char *s1, const char *s2,
645 gssize n1, gssize n2)
648 gchar *normalized_s1;
649 gchar *normalized_s2;
652 gboolean ret = FALSE;
654 g_return_val_if_fail (s1 != NULL, FALSE);
655 g_return_val_if_fail (s2 != NULL, FALSE);
656 g_return_val_if_fail (n1 > 0, FALSE);
657 g_return_val_if_fail (n2 > 0, FALSE);
659 casefold = g_utf8_casefold (s1, n1);
660 normalized_s1 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
663 casefold = g_utf8_casefold (s2, n2);
664 normalized_s2 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
667 len_s1 = strlen (normalized_s1);
668 len_s2 = strlen (normalized_s2);
673 ret = (strncmp (normalized_s1, normalized_s2, len_s2) == 0);
676 g_free (normalized_s1);
677 g_free (normalized_s2);
683 forward_chars_with_skipping (GtkTextIter *iter,
685 gboolean skip_invisible,
686 gboolean skip_nontext,
687 gboolean skip_decomp)
691 g_return_if_fail (count >= 0);
697 gboolean ignored = FALSE;
699 /* minimal workaround to avoid the infinite loop of bug #168247.
700 * It doesn't fix the problemjust the symptom...
702 if (gtk_text_iter_is_end (iter))
705 if (skip_nontext && gtk_text_iter_get_char (iter) == GTK_TEXT_UNKNOWN_CHAR)
708 if (!ignored && skip_invisible &&
709 /* _gtk_text_btree_char_is_invisible (iter)*/ FALSE)
712 if (!ignored && skip_decomp)
714 /* being UTF8 correct sucks; this accounts for extra
715 offsets coming from canonical decompositions of
716 UTF8 characters (e.g. accented characters) which
717 g_utf8_normalize() performs */
722 buffer_len = g_unichar_to_utf8 (gtk_text_iter_get_char (iter), buffer);
723 normal = g_utf8_normalize (buffer, buffer_len, G_NORMALIZE_NFD);
724 i -= (g_utf8_strlen (normal, -1) - 1);
728 gtk_text_iter_forward_char (iter);
736 lines_match (const GtkTextIter *start,
738 gboolean visible_only,
740 GtkTextIter *match_start,
741 GtkTextIter *match_end)
748 if (*lines == NULL || **lines == '\0')
751 *match_start = *start;
758 gtk_text_iter_forward_line (&next);
760 /* No more text in buffer, but *lines is nonempty */
761 if (gtk_text_iter_equal (start, &next))
767 line_text = gtk_text_iter_get_visible_slice (start, &next);
769 line_text = gtk_text_iter_get_slice (start, &next);
774 line_text = gtk_text_iter_get_visible_text (start, &next);
776 line_text = gtk_text_iter_get_text (start, &next);
779 if (match_start) /* if this is the first line we're matching */
781 found = g_utf8_strcasestr (line_text, *lines);
785 /* If it's not the first line, we have to match from the
788 if (g_utf8_caselessnmatch (line_text, *lines, strlen (line_text),
801 /* Get offset to start of search string */
802 offset = g_utf8_strlen (line_text, found - line_text);
806 /* If match start needs to be returned, set it to the
807 * start of the search string.
809 forward_chars_with_skipping (&next, offset, visible_only, !slice, FALSE);
815 /* Go to end of search string */
816 forward_chars_with_skipping (&next, g_utf8_strlen (*lines, -1), visible_only, !slice, TRUE);
825 /* pass NULL for match_start, since we don't need to find the
828 return lines_match (&next, lines, visible_only, slice, NULL, match_end);
831 /* strsplit () that retains the delimiter as part of the string. */
833 strbreakup (const char *string,
834 const char *delimiter,
837 GSList *string_list = NULL, *slist;
838 gchar **str_array, *s, *casefold, *new_string;
841 g_return_val_if_fail (string != NULL, NULL);
842 g_return_val_if_fail (delimiter != NULL, NULL);
845 max_tokens = G_MAXINT;
847 s = strstr (string, delimiter);
850 guint delimiter_len = strlen (delimiter);
856 len = s - string + delimiter_len;
857 new_string = g_new (gchar, len + 1);
858 strncpy (new_string, string, len);
860 casefold = g_utf8_casefold (new_string, -1);
862 new_string = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
864 string_list = g_slist_prepend (string_list, new_string);
866 string = s + delimiter_len;
867 s = strstr (string, delimiter);
868 } while (--max_tokens && s);
874 casefold = g_utf8_casefold (string, -1);
875 new_string = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
877 string_list = g_slist_prepend (string_list, new_string);
880 str_array = g_new (gchar*, n);
884 str_array[i--] = NULL;
885 for (slist = string_list; slist; slist = slist->next)
886 str_array[i--] = slist->data;
888 g_slist_free (string_list);
894 gossip_text_iter_forward_search (const GtkTextIter *iter,
896 GtkTextIter *match_start,
897 GtkTextIter *match_end,
898 const GtkTextIter *limit)
900 gchar **lines = NULL;
902 gboolean retval = FALSE;
904 gboolean visible_only;
907 g_return_val_if_fail (iter != NULL, FALSE);
908 g_return_val_if_fail (str != NULL, FALSE);
910 if (limit && gtk_text_iter_compare (iter, limit) >= 0)
914 /* If we can move one char, return the empty string there */
917 if (gtk_text_iter_forward_char (&match)) {
918 if (limit && gtk_text_iter_equal (&match, limit)) {
923 *match_start = match;
937 /* locate all lines */
938 lines = strbreakup (str, "\n", -1);
943 /* This loop has an inefficient worst-case, where
944 * gtk_text_iter_get_text () is called repeatedly on
949 if (limit && gtk_text_iter_compare (&search, limit) >= 0) {
953 if (lines_match (&search, (const gchar**)lines,
954 visible_only, slice, &match, &end)) {
956 (limit && gtk_text_iter_compare (&end, limit) <= 0)) {
960 *match_start = match;
968 } while (gtk_text_iter_forward_line (&search));
970 g_strfreev ((gchar**)lines);
976 g_utf8_strrcasestr (const gchar *haystack, const gchar *needle)
980 const gchar *ret = NULL;
983 gchar *caseless_haystack;
986 g_return_val_if_fail (haystack != NULL, NULL);
987 g_return_val_if_fail (needle != NULL, NULL);
989 casefold = g_utf8_casefold (haystack, -1);
990 caseless_haystack = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
993 needle_len = g_utf8_strlen (needle, -1);
994 haystack_len = g_utf8_strlen (caseless_haystack, -1);
998 ret = (gchar *)haystack;
1002 if (haystack_len < needle_len)
1008 i = haystack_len - needle_len;
1009 p = g_utf8_offset_to_pointer (caseless_haystack, i);
1010 needle_len = strlen (needle);
1012 while (p >= caseless_haystack)
1014 if (strncmp (p, needle, needle_len) == 0)
1016 ret = pointer_from_offset_skipping_decomp (haystack, i);
1020 p = g_utf8_prev_char (p);
1025 g_free (caseless_haystack);
1031 backward_lines_match (const GtkTextIter *start,
1032 const gchar **lines,
1033 gboolean visible_only,
1035 GtkTextIter *match_start,
1036 GtkTextIter *match_end)
1038 GtkTextIter line, next;
1043 if (*lines == NULL || **lines == '\0')
1046 *match_start = *start;
1048 *match_end = *start;
1052 line = next = *start;
1053 if (gtk_text_iter_get_line_offset (&next) == 0)
1055 if (!gtk_text_iter_backward_line (&next))
1059 gtk_text_iter_set_line_offset (&next, 0);
1064 line_text = gtk_text_iter_get_visible_slice (&next, &line);
1066 line_text = gtk_text_iter_get_slice (&next, &line);
1071 line_text = gtk_text_iter_get_visible_text (&next, &line);
1073 line_text = gtk_text_iter_get_text (&next, &line);
1076 if (match_start) /* if this is the first line we're matching */
1078 found = g_utf8_strrcasestr (line_text, *lines);
1082 /* If it's not the first line, we have to match from the
1083 * start of the line.
1085 if (g_utf8_caselessnmatch (line_text, *lines, strlen (line_text),
1098 /* Get offset to start of search string */
1099 offset = g_utf8_strlen (line_text, found - line_text);
1101 forward_chars_with_skipping (&next, offset, visible_only, !slice, FALSE);
1103 /* If match start needs to be returned, set it to the
1104 * start of the search string.
1108 *match_start = next;
1111 /* Go to end of search string */
1112 forward_chars_with_skipping (&next, g_utf8_strlen (*lines, -1), visible_only, !slice, TRUE);
1121 /* try to match the rest of the lines forward, passing NULL
1122 * for match_start so lines_match will try to match the entire
1124 return lines_match (&next, lines, visible_only,
1125 slice, NULL, match_end);
1129 gossip_text_iter_backward_search (const GtkTextIter *iter,
1131 GtkTextIter *match_start,
1132 GtkTextIter *match_end,
1133 const GtkTextIter *limit)
1135 gchar **lines = NULL;
1137 gboolean retval = FALSE;
1139 gboolean visible_only;
1142 g_return_val_if_fail (iter != NULL, FALSE);
1143 g_return_val_if_fail (str != NULL, FALSE);
1145 if (limit && gtk_text_iter_compare (iter, limit) <= 0)
1150 /* If we can move one char, return the empty string there */
1153 if (gtk_text_iter_backward_char (&match))
1155 if (limit && gtk_text_iter_equal (&match, limit))
1159 *match_start = match;
1170 visible_only = TRUE;
1173 /* locate all lines */
1174 lines = strbreakup (str, "\n", -1);
1180 /* This loop has an inefficient worst-case, where
1181 * gtk_text_iter_get_text () is called repeatedly on
1186 if (limit && gtk_text_iter_compare (&search, limit) <= 0)
1189 if (backward_lines_match (&search, (const gchar**)lines,
1190 visible_only, slice, &match, &end))
1192 if (limit == NULL || (limit &&
1193 gtk_text_iter_compare (&end, limit) > 0))
1198 *match_start = match;
1205 if (gtk_text_iter_get_line_offset (&search) == 0)
1207 if (!gtk_text_iter_backward_line (&search))
1212 gtk_text_iter_set_line_offset (&search, 0);
1216 g_strfreev ((gchar**)lines);
1222 window_get_is_on_current_workspace (GtkWindow *window)
1224 GdkWindow *gdk_window;
1226 gdk_window = GTK_WIDGET (window)->window;
1228 return !(gdk_window_get_state (gdk_window) &
1229 GDK_WINDOW_STATE_ICONIFIED);
1235 /* Checks if the window is visible as in visible on the current workspace. */
1237 gossip_window_get_is_visible (GtkWindow *window)
1241 g_return_val_if_fail (window != NULL, FALSE);
1243 g_object_get (window,
1244 "visible", &visible,
1247 return visible && window_get_is_on_current_workspace (window);
1250 /* Takes care of moving the window to the current workspace. */
1252 gossip_window_present (GtkWindow *window,
1253 gboolean steal_focus)
1256 gboolean on_current;
1259 g_return_if_fail (window != NULL);
1261 /* There are three cases: hidden, visible, visible on another
1265 g_object_get (window,
1266 "visible", &visible,
1269 on_current = window_get_is_on_current_workspace (window);
1271 if (visible && !on_current) {
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 if (steal_focus && timestamp != GDK_CURRENT_TIME) {
1278 gtk_window_present_with_time (window, timestamp);
1280 gtk_window_present (window);
1284 /* The URL opening code can't handle schemeless strings, so we try to be
1285 * smart and add http if there is no scheme or doesn't look like a mail
1286 * address. This should work in most cases, and let us click on strings
1287 * like "www.gnome.org".
1290 fixup_url (const gchar *url)
1294 if (!g_str_has_prefix (url, "http://") &&
1295 !strstr (url, ":/") &&
1296 !strstr (url, "@")) {
1297 real_url = g_strdup_printf ("http://%s", url);
1299 real_url = g_strdup (url);
1306 gossip_url_show (const char *url)
1309 GError *error = NULL;
1311 real_url = fixup_url (url);
1312 gnome_url_show (real_url, &error);
1314 g_warning ("Couldn't show URL:'%s'", real_url);
1315 g_error_free (error);
1322 link_button_hook (GtkLinkButton *button,
1326 gossip_url_show (link);
1330 gossip_link_button_new (const gchar *url,
1333 static gboolean hook = FALSE;
1337 gtk_link_button_set_uri_hook (link_button_hook, NULL, NULL);
1340 return gtk_link_button_new_with_label (url, title);
1343 /* FIXME: Do this in a proper way at some point, probably in GTK+? */
1345 gossip_window_set_default_icon_name (const gchar *name)
1347 gtk_window_set_default_icon_name (name);
1351 gossip_toggle_button_set_state_quietly (GtkWidget *widget,
1356 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (widget));
1358 g_signal_handlers_block_by_func (widget, callback, user_data);
1359 g_object_set (widget, "active", active, NULL);
1360 g_signal_handlers_unblock_by_func (widget, callback, user_data);