1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2002-2007 Imendio AB
4 * Copyright (C) 2008 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., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301 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>
29 #include <sys/types.h>
33 #include <glib/gi18n-lib.h>
36 #include <telepathy-glib/util.h>
38 #include <libempathy/empathy-utils.h>
40 #include "empathy-chat-text-view.h"
41 #include "empathy-chat.h"
42 #include "empathy-conf.h"
43 #include "empathy-ui-utils.h"
44 #include "empathy-smiley-manager.h"
46 #define DEBUG_FLAG EMPATHY_DEBUG_CHAT
47 #include <libempathy/empathy-debug.h>
49 /* Number of seconds between timestamps when using normal mode, 5 minutes. */
50 #define TIMESTAMP_INTERVAL 300
53 #define MAX_SCROLL_TIME 0.4 /* seconds */
54 #define SCROLL_DELAY 33 /* milliseconds */
56 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyChatTextView)
59 GtkTextBuffer *buffer;
62 GtkTextMark *find_mark_previous;
63 GtkTextMark *find_mark_next;
64 gboolean find_wrapped;
65 gboolean find_last_direction;
66 EmpathyContact *last_contact;
67 time_t last_timestamp;
68 gboolean allow_scrolling;
69 guint notify_system_fonts_id;
70 EmpathySmileyManager *smiley_manager;
71 gboolean only_if_date;
72 } EmpathyChatTextViewPriv;
74 static void chat_text_view_iface_init (EmpathyChatViewIface *iface);
76 static void chat_text_view_copy_clipboard (EmpathyChatView *view);
78 G_DEFINE_TYPE_WITH_CODE (EmpathyChatTextView, empathy_chat_text_view,
80 G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CHAT_VIEW,
81 chat_text_view_iface_init));
90 chat_text_view_url_event_cb (GtkTextTag *tag,
94 EmpathyChatTextView *view)
96 EmpathyChatTextViewPriv *priv;
97 GtkTextIter start, end;
100 priv = GET_PRIV (view);
102 /* If the link is being selected, don't do anything. */
103 gtk_text_buffer_get_selection_bounds (priv->buffer, &start, &end);
104 if (gtk_text_iter_get_offset (&start) != gtk_text_iter_get_offset (&end)) {
108 if (event->type == GDK_BUTTON_RELEASE && event->button.button == 1) {
111 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
112 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
113 str = gtk_text_buffer_get_text (priv->buffer,
118 empathy_url_show (GTK_WIDGET (view), str);
127 chat_text_view_event_cb (EmpathyChatTextView *view,
128 GdkEventMotion *event,
131 static GdkCursor *hand = NULL;
132 static GdkCursor *beam = NULL;
133 GtkTextWindowType type;
136 gint x, y, buf_x, buf_y;
138 type = gtk_text_view_get_window_type (GTK_TEXT_VIEW (view),
141 if (type != GTK_TEXT_WINDOW_TEXT) {
145 /* Get where the pointer really is. */
146 win = gtk_text_view_get_window (GTK_TEXT_VIEW (view), type);
151 gdk_window_get_pointer (win, &x, &y, NULL);
153 /* Get the iter where the cursor is at */
154 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view), type,
158 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view),
162 if (gtk_text_iter_has_tag (&iter, tag)) {
164 hand = gdk_cursor_new (GDK_HAND2);
165 beam = gdk_cursor_new (GDK_XTERM);
167 gdk_window_set_cursor (win, hand);
170 beam = gdk_cursor_new (GDK_XTERM);
172 gdk_window_set_cursor (win, beam);
179 chat_text_view_create_tags (EmpathyChatTextView *view)
181 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
184 gtk_text_buffer_create_tag (priv->buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_CUT, NULL);
185 gtk_text_buffer_create_tag (priv->buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_HIGHLIGHT, NULL);
186 gtk_text_buffer_create_tag (priv->buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_SPACING, NULL);
187 gtk_text_buffer_create_tag (priv->buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_TIME, NULL);
188 gtk_text_buffer_create_tag (priv->buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_ACTION, NULL);
189 gtk_text_buffer_create_tag (priv->buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_BODY, NULL);
190 gtk_text_buffer_create_tag (priv->buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_EVENT, NULL);
192 tag = gtk_text_buffer_create_tag (priv->buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_LINK, NULL);
193 g_signal_connect (tag, "event",
194 G_CALLBACK (chat_text_view_url_event_cb),
197 g_signal_connect (view, "motion-notify-event",
198 G_CALLBACK (chat_text_view_event_cb),
203 chat_text_view_system_font_update (EmpathyChatTextView *view)
205 PangoFontDescription *font_description = NULL;
208 if (empathy_conf_get_string (empathy_conf_get (),
209 "/desktop/gnome/interface/document_font_name",
210 &font_name) && font_name) {
211 font_description = pango_font_description_from_string (font_name);
214 font_description = NULL;
217 gtk_widget_modify_font (GTK_WIDGET (view), font_description);
219 if (font_description) {
220 pango_font_description_free (font_description);
225 chat_text_view_notify_system_font_cb (EmpathyConf *conf,
229 EmpathyChatTextView *view = user_data;
231 chat_text_view_system_font_update (view);
235 chat_text_view_open_address_cb (GtkMenuItem *menuitem, const gchar *url)
237 empathy_url_show (GTK_WIDGET (menuitem), url);
241 chat_text_view_copy_address_cb (GtkMenuItem *menuitem, const gchar *url)
243 GtkClipboard *clipboard;
245 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
246 gtk_clipboard_set_text (clipboard, url, -1);
248 clipboard = gtk_clipboard_get (GDK_SELECTION_PRIMARY);
249 gtk_clipboard_set_text (clipboard, url, -1);
253 chat_text_view_populate_popup (EmpathyChatTextView *view,
257 EmpathyChatTextViewPriv *priv;
258 GtkTextTagTable *table;
261 GtkTextIter iter, start, end;
265 priv = GET_PRIV (view);
267 /* Clear menu item */
268 if (gtk_text_buffer_get_char_count (priv->buffer) > 0) {
269 item = gtk_separator_menu_item_new ();
270 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
271 gtk_widget_show (item);
273 item = gtk_image_menu_item_new_from_stock (GTK_STOCK_CLEAR, NULL);
274 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
275 gtk_widget_show (item);
277 g_signal_connect_swapped (item, "activate",
278 G_CALLBACK (empathy_chat_view_clear),
282 /* Link context menu items */
283 table = gtk_text_buffer_get_tag_table (priv->buffer);
284 tag = gtk_text_tag_table_lookup (table, EMPATHY_CHAT_TEXT_VIEW_TAG_LINK);
286 gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
288 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
289 GTK_TEXT_WINDOW_WIDGET,
293 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &iter, x, y);
297 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
298 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
299 str = gtk_text_buffer_get_text (priv->buffer,
300 &start, &end, FALSE);
303 if (EMP_STR_EMPTY (str)) {
308 /* NOTE: Set data just to get the string freed when not needed. */
309 g_object_set_data_full (G_OBJECT (menu),
311 (GDestroyNotify) g_free);
313 item = gtk_separator_menu_item_new ();
314 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
315 gtk_widget_show (item);
317 item = gtk_menu_item_new_with_mnemonic (_("_Copy Link Address"));
318 g_signal_connect (item, "activate",
319 G_CALLBACK (chat_text_view_copy_address_cb),
321 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
322 gtk_widget_show (item);
324 item = gtk_menu_item_new_with_mnemonic (_("_Open Link"));
325 g_signal_connect (item, "activate",
326 G_CALLBACK (chat_text_view_open_address_cb),
328 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
329 gtk_widget_show (item);
333 chat_text_view_is_scrolled_down (EmpathyChatTextView *view)
337 sw = gtk_widget_get_parent (GTK_WIDGET (view));
338 if (GTK_IS_SCROLLED_WINDOW (sw)) {
344 vadj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (sw));
345 value = gtk_adjustment_get_value (vadj);
346 upper = gtk_adjustment_get_upper (vadj);
347 page_size = gtk_adjustment_get_page_size (vadj);
349 if (value < upper - page_size) {
358 chat_text_view_maybe_trim_buffer (EmpathyChatTextView *view)
360 EmpathyChatTextViewPriv *priv;
361 GtkTextIter top, bottom;
364 GtkTextTagTable *table;
367 priv = GET_PRIV (view);
369 gtk_text_buffer_get_end_iter (priv->buffer, &bottom);
370 line = gtk_text_iter_get_line (&bottom);
371 if (line < MAX_LINES) {
375 remove_ = line - MAX_LINES;
376 gtk_text_buffer_get_start_iter (priv->buffer, &top);
379 if (!gtk_text_iter_forward_lines (&bottom, remove_)) {
383 /* Track backwords to a place where we can safely cut, we don't do it in
384 * the middle of a tag.
386 table = gtk_text_buffer_get_tag_table (priv->buffer);
387 tag = gtk_text_tag_table_lookup (table, EMPATHY_CHAT_TEXT_VIEW_TAG_CUT);
392 if (!gtk_text_iter_forward_to_tag_toggle (&bottom, tag)) {
396 if (!gtk_text_iter_equal (&top, &bottom)) {
397 gtk_text_buffer_delete (priv->buffer, &top, &bottom);
402 chat_text_view_append_timestamp (EmpathyChatTextView *view,
406 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
411 str = g_string_new ("- ");
413 /* Append date if needed */
418 date = g_date_new ();
419 g_date_set_time_t (date, timestamp);
420 /* Translators: timestamp displayed between conversations in
421 * chat windows (strftime format string) */
422 g_date_strftime (buf, 256, _("%A %B %d %Y"), date);
423 g_string_append (str, buf);
424 g_string_append (str, ", ");
429 tmp = empathy_time_to_string_local (timestamp, EMPATHY_TIME_FORMAT_DISPLAY_SHORT);
430 g_string_append (str, tmp);
433 g_string_append (str, " -\n");
435 /* Insert the string in the buffer */
436 empathy_chat_text_view_append_spacing (view);
437 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
438 gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
441 EMPATHY_CHAT_TEXT_VIEW_TAG_TIME,
444 priv->last_timestamp = timestamp;
446 g_string_free (str, TRUE);
450 chat_text_maybe_append_date_and_time (EmpathyChatTextView *view,
453 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
454 GDate *date, *last_date;
455 gboolean append_date = FALSE;
456 gboolean append_time = FALSE;
458 /* Get the date from last message */
459 last_date = g_date_new ();
460 g_date_set_time_t (last_date, priv->last_timestamp);
462 /* Get the date of the message we are appending */
463 date = g_date_new ();
464 g_date_set_time_t (date, timestamp);
466 /* If last message was from another day we append date and time */
467 if (g_date_compare (date, last_date) > 0) {
472 g_date_free (last_date);
475 /* If last message is 'old' append the time */
476 if (timestamp - priv->last_timestamp >= TIMESTAMP_INTERVAL) {
480 if (append_date || (!priv->only_if_date && append_time)) {
481 chat_text_view_append_timestamp (view, timestamp, append_date);
486 chat_text_view_size_allocate (GtkWidget *widget,
487 GtkAllocation *alloc)
491 down = chat_text_view_is_scrolled_down (EMPATHY_CHAT_TEXT_VIEW (widget));
493 GTK_WIDGET_CLASS (empathy_chat_text_view_parent_class)->size_allocate (widget, alloc);
498 adj = GTK_TEXT_VIEW (widget)->vadjustment;
499 gtk_adjustment_set_value (adj,
500 gtk_adjustment_get_upper (adj) -
501 gtk_adjustment_get_page_size (adj));
506 chat_text_view_drag_motion (GtkWidget *widget,
507 GdkDragContext *context,
512 /* Don't handle drag motion, since we don't want the view to scroll as
513 * the result of dragging something across it. */
519 chat_text_view_get_property (GObject *object,
524 EmpathyChatTextViewPriv *priv = GET_PRIV (object);
527 case PROP_LAST_CONTACT:
528 g_value_set_object (value, priv->last_contact);
530 case PROP_ONLY_IF_DATE:
531 g_value_set_boolean (value, priv->only_if_date);
534 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
540 chat_text_view_set_property (GObject *object,
545 EmpathyChatTextViewPriv *priv = GET_PRIV (object);
548 case PROP_ONLY_IF_DATE:
549 priv->only_if_date = g_value_get_boolean (value);
552 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
558 chat_text_view_finalize (GObject *object)
560 EmpathyChatTextView *view;
561 EmpathyChatTextViewPriv *priv;
563 view = EMPATHY_CHAT_TEXT_VIEW (object);
564 priv = GET_PRIV (view);
566 DEBUG ("%p", object);
568 empathy_conf_notify_remove (empathy_conf_get (), priv->notify_system_fonts_id);
570 if (priv->last_contact) {
571 g_object_unref (priv->last_contact);
573 if (priv->scroll_time) {
574 g_timer_destroy (priv->scroll_time);
576 if (priv->scroll_timeout) {
577 g_source_remove (priv->scroll_timeout);
579 g_object_unref (priv->smiley_manager);
581 G_OBJECT_CLASS (empathy_chat_text_view_parent_class)->finalize (object);
585 text_view_copy_clipboard (GtkTextView *text_view)
587 chat_text_view_copy_clipboard (EMPATHY_CHAT_VIEW (text_view));
591 empathy_chat_text_view_class_init (EmpathyChatTextViewClass *klass)
593 GObjectClass *object_class = G_OBJECT_CLASS (klass);
594 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
595 GtkTextViewClass *text_view_class = GTK_TEXT_VIEW_CLASS (klass);
597 object_class->finalize = chat_text_view_finalize;
598 object_class->get_property = chat_text_view_get_property;
599 object_class->set_property = chat_text_view_set_property;
601 widget_class->size_allocate = chat_text_view_size_allocate;
602 widget_class->drag_motion = chat_text_view_drag_motion;
604 text_view_class->copy_clipboard = text_view_copy_clipboard;
606 g_object_class_install_property (object_class,
608 g_param_spec_object ("last-contact",
610 "The sender of the last received message",
611 EMPATHY_TYPE_CONTACT,
613 g_object_class_install_property (object_class,
615 g_param_spec_boolean ("only-if-date",
617 "Display timestamp only if the date changes",
622 g_type_class_add_private (object_class, sizeof (EmpathyChatTextViewPriv));
626 empathy_chat_text_view_init (EmpathyChatTextView *view)
628 EmpathyChatTextViewPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (view,
629 EMPATHY_TYPE_CHAT_TEXT_VIEW, EmpathyChatTextViewPriv);
632 priv->buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
633 priv->last_timestamp = 0;
634 priv->allow_scrolling = TRUE;
635 priv->smiley_manager = empathy_smiley_manager_dup_singleton ();
638 "wrap-mode", GTK_WRAP_WORD_CHAR,
640 "cursor-visible", FALSE,
643 priv->notify_system_fonts_id =
644 empathy_conf_notify_add (empathy_conf_get (),
645 "/desktop/gnome/interface/document_font_name",
646 chat_text_view_notify_system_font_cb,
648 chat_text_view_system_font_update (view);
649 chat_text_view_create_tags (view);
651 g_signal_connect (view,
653 G_CALLBACK (chat_text_view_populate_popup),
657 /* Code stolen from pidgin/gtkimhtml.c */
659 chat_text_view_scroll_cb (EmpathyChatTextView *view)
661 EmpathyChatTextViewPriv *priv;
665 priv = GET_PRIV (view);
666 adj = GTK_TEXT_VIEW (view)->vadjustment;
667 max_val = gtk_adjustment_get_upper (adj) - gtk_adjustment_get_page_size (adj);
669 g_return_val_if_fail (priv->scroll_time != NULL, FALSE);
671 if (g_timer_elapsed (priv->scroll_time, NULL) > MAX_SCROLL_TIME) {
672 /* time's up. jump to the end and kill the timer */
673 gtk_adjustment_set_value (adj, max_val);
674 g_timer_destroy (priv->scroll_time);
675 priv->scroll_time = NULL;
676 priv->scroll_timeout = 0;
680 /* scroll by 1/3rd the remaining distance */
681 gtk_adjustment_set_value (adj, gtk_adjustment_get_value (adj) + ((max_val - gtk_adjustment_get_value (adj)) / 3));
686 chat_text_view_scroll_down (EmpathyChatView *view)
688 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
690 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
692 if (!priv->allow_scrolling) {
696 DEBUG ("Scrolling down");
698 if (priv->scroll_time) {
699 g_timer_reset (priv->scroll_time);
701 priv->scroll_time = g_timer_new ();
703 if (!priv->scroll_timeout) {
704 priv->scroll_timeout = g_timeout_add (SCROLL_DELAY,
705 (GSourceFunc) chat_text_view_scroll_cb,
711 chat_text_view_append_message (EmpathyChatView *view,
714 EmpathyChatTextView *text_view = EMPATHY_CHAT_TEXT_VIEW (view);
715 EmpathyChatTextViewPriv *priv = GET_PRIV (text_view);
719 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
720 g_return_if_fail (EMPATHY_IS_MESSAGE (msg));
722 if (!empathy_message_get_body (msg)) {
726 bottom = chat_text_view_is_scrolled_down (text_view);
728 chat_text_view_maybe_trim_buffer (EMPATHY_CHAT_TEXT_VIEW (view));
730 timestamp = empathy_message_get_timestamp (msg);
731 chat_text_maybe_append_date_and_time (text_view, timestamp);
732 if (EMPATHY_CHAT_TEXT_VIEW_GET_CLASS (view)->append_message) {
733 EMPATHY_CHAT_TEXT_VIEW_GET_CLASS (view)->append_message (text_view,
738 chat_text_view_scroll_down (view);
741 if (priv->last_contact) {
742 g_object_unref (priv->last_contact);
744 priv->last_contact = g_object_ref (empathy_message_get_sender (msg));
745 g_object_notify (G_OBJECT (view), "last-contact");
749 chat_text_view_append_event (EmpathyChatView *view,
752 EmpathyChatTextView *text_view = EMPATHY_CHAT_TEXT_VIEW (view);
753 EmpathyChatTextViewPriv *priv = GET_PRIV (text_view);
759 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
760 g_return_if_fail (!EMP_STR_EMPTY (str));
762 bottom = chat_text_view_is_scrolled_down (text_view);
763 chat_text_view_maybe_trim_buffer (EMPATHY_CHAT_TEXT_VIEW (view));
764 chat_text_maybe_append_date_and_time (text_view,
765 empathy_time_get_current ());
767 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
768 msg = g_strdup_printf (" - %s\n", str);
769 gtk_text_buffer_insert_with_tags_by_name (priv->buffer, &iter,
771 EMPATHY_CHAT_TEXT_VIEW_TAG_EVENT,
776 chat_text_view_scroll_down (view);
779 if (priv->last_contact) {
780 g_object_unref (priv->last_contact);
781 priv->last_contact = NULL;
782 g_object_notify (G_OBJECT (view), "last-contact");
787 chat_text_view_scroll (EmpathyChatView *view,
788 gboolean allow_scrolling)
790 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
792 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
794 DEBUG ("Scrolling %s", allow_scrolling ? "enabled" : "disabled");
796 priv->allow_scrolling = allow_scrolling;
797 if (allow_scrolling) {
798 empathy_chat_view_scroll_down (view);
803 chat_text_view_get_has_selection (EmpathyChatView *view)
805 GtkTextBuffer *buffer;
807 g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), FALSE);
809 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
811 return gtk_text_buffer_get_has_selection (buffer);
815 chat_text_view_clear (EmpathyChatView *view)
817 GtkTextBuffer *buffer;
818 EmpathyChatTextViewPriv *priv;
820 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
822 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
823 gtk_text_buffer_set_text (buffer, "", -1);
825 /* We set these back to the initial values so we get
826 * timestamps when clearing the window to know when
827 * conversations start.
829 priv = GET_PRIV (view);
831 priv->last_timestamp = 0;
832 if (priv->last_contact) {
833 g_object_unref (priv->last_contact);
834 priv->last_contact = NULL;
839 chat_text_view_find_previous (EmpathyChatView *view,
840 const gchar *search_criteria,
844 EmpathyChatTextViewPriv *priv;
845 GtkTextBuffer *buffer;
846 GtkTextIter iter_at_mark;
847 GtkTextIter iter_match_start;
848 GtkTextIter iter_match_end;
850 gboolean from_start = FALSE;
852 g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), FALSE);
853 g_return_val_if_fail (search_criteria != NULL, FALSE);
855 priv = GET_PRIV (view);
857 buffer = priv->buffer;
859 if (EMP_STR_EMPTY (search_criteria)) {
860 if (priv->find_mark_previous) {
861 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
863 gtk_text_buffer_move_mark (buffer,
864 priv->find_mark_previous,
866 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
867 priv->find_mark_previous,
872 gtk_text_buffer_select_range (buffer,
884 if (!new_search && priv->find_mark_previous) {
885 gtk_text_buffer_get_iter_at_mark (buffer,
887 priv->find_mark_previous);
889 gtk_text_buffer_get_end_iter (buffer, &iter_at_mark);
893 priv->find_last_direction = FALSE;
895 /* Use the standard GTK+ method for case sensitive searches. It can't do
896 * case insensitive searches (see bug #61852), so keep the custom method
897 * around for case insensitive searches. */
899 found = gtk_text_iter_backward_search (&iter_at_mark,
901 0, /* no text search flags, we want exact matches */
906 found = empathy_text_iter_backward_search (&iter_at_mark,
914 gboolean result = FALSE;
920 /* Here we wrap around. */
921 if (!new_search && !priv->find_wrapped) {
922 priv->find_wrapped = TRUE;
923 result = chat_text_view_find_previous (view,
927 priv->find_wrapped = FALSE;
933 /* Set new mark and show on screen */
934 if (!priv->find_mark_previous) {
935 priv->find_mark_previous = gtk_text_buffer_create_mark (buffer, NULL,
939 gtk_text_buffer_move_mark (buffer,
940 priv->find_mark_previous,
944 if (!priv->find_mark_next) {
945 priv->find_mark_next = gtk_text_buffer_create_mark (buffer, NULL,
949 gtk_text_buffer_move_mark (buffer,
950 priv->find_mark_next,
954 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
955 priv->find_mark_previous,
961 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &iter_match_start);
962 gtk_text_buffer_move_mark_by_name (buffer, "insert", &iter_match_end);
968 chat_text_view_find_next (EmpathyChatView *view,
969 const gchar *search_criteria,
973 EmpathyChatTextViewPriv *priv;
974 GtkTextBuffer *buffer;
975 GtkTextIter iter_at_mark;
976 GtkTextIter iter_match_start;
977 GtkTextIter iter_match_end;
979 gboolean from_start = FALSE;
981 g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), FALSE);
982 g_return_val_if_fail (search_criteria != NULL, FALSE);
984 priv = GET_PRIV (view);
986 buffer = priv->buffer;
988 if (EMP_STR_EMPTY (search_criteria)) {
989 if (priv->find_mark_next) {
990 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
992 gtk_text_buffer_move_mark (buffer,
993 priv->find_mark_next,
995 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
996 priv->find_mark_next,
1001 gtk_text_buffer_select_range (buffer,
1013 if (!new_search && priv->find_mark_next) {
1014 gtk_text_buffer_get_iter_at_mark (buffer,
1016 priv->find_mark_next);
1018 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1022 priv->find_last_direction = TRUE;
1024 /* Use the standard GTK+ method for case sensitive searches. It can't do
1025 * case insensitive searches (see bug #61852), so keep the custom method
1026 * around for case insensitive searches. */
1028 found = gtk_text_iter_forward_search (&iter_at_mark,
1035 found = empathy_text_iter_forward_search (&iter_at_mark,
1043 gboolean result = FALSE;
1049 /* Here we wrap around. */
1050 if (!new_search && !priv->find_wrapped) {
1051 priv->find_wrapped = TRUE;
1052 result = chat_text_view_find_next (view,
1056 priv->find_wrapped = FALSE;
1062 /* Set new mark and show on screen */
1063 if (!priv->find_mark_next) {
1064 priv->find_mark_next = gtk_text_buffer_create_mark (buffer, NULL,
1068 gtk_text_buffer_move_mark (buffer,
1069 priv->find_mark_next,
1073 if (!priv->find_mark_previous) {
1074 priv->find_mark_previous = gtk_text_buffer_create_mark (buffer, NULL,
1078 gtk_text_buffer_move_mark (buffer,
1079 priv->find_mark_previous,
1083 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
1084 priv->find_mark_next,
1090 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &iter_match_start);
1091 gtk_text_buffer_move_mark_by_name (buffer, "insert", &iter_match_end);
1097 chat_text_view_find_abilities (EmpathyChatView *view,
1098 const gchar *search_criteria,
1099 gboolean match_case,
1100 gboolean *can_do_previous,
1101 gboolean *can_do_next)
1103 EmpathyChatTextViewPriv *priv;
1104 GtkTextBuffer *buffer;
1105 GtkTextIter iter_at_mark;
1106 GtkTextIter iter_match_start;
1107 GtkTextIter iter_match_end;
1109 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
1110 g_return_if_fail (search_criteria != NULL);
1111 g_return_if_fail (can_do_previous != NULL && can_do_next != NULL);
1113 priv = GET_PRIV (view);
1115 buffer = priv->buffer;
1117 if (can_do_previous) {
1118 if (priv->find_mark_previous) {
1119 gtk_text_buffer_get_iter_at_mark (buffer,
1121 priv->find_mark_previous);
1123 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1127 *can_do_previous = gtk_text_iter_backward_search (&iter_at_mark,
1134 *can_do_previous = empathy_text_iter_backward_search (&iter_at_mark,
1143 if (priv->find_mark_next) {
1144 gtk_text_buffer_get_iter_at_mark (buffer,
1146 priv->find_mark_next);
1148 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1152 *can_do_next = gtk_text_iter_forward_search (&iter_at_mark,
1159 *can_do_next = empathy_text_iter_forward_search (&iter_at_mark,
1169 chat_text_view_highlight (EmpathyChatView *view,
1171 gboolean match_case)
1173 GtkTextBuffer *buffer;
1175 GtkTextIter iter_start;
1176 GtkTextIter iter_end;
1177 GtkTextIter iter_match_start;
1178 GtkTextIter iter_match_end;
1181 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
1183 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
1185 gtk_text_buffer_get_start_iter (buffer, &iter);
1187 gtk_text_buffer_get_bounds (buffer, &iter_start, &iter_end);
1188 gtk_text_buffer_remove_tag_by_name (buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_HIGHLIGHT,
1192 if (EMP_STR_EMPTY (text)) {
1198 found = gtk_text_iter_forward_search (&iter,
1205 found = empathy_text_iter_forward_search (&iter,
1215 gtk_text_buffer_apply_tag_by_name (buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_HIGHLIGHT,
1219 iter = iter_match_end;
1224 chat_text_view_copy_clipboard (EmpathyChatView *view)
1226 GtkTextBuffer *buffer;
1227 GtkTextIter start, iter, end;
1228 GtkClipboard *clipboard;
1231 GtkTextChildAnchor *anchor = NULL;
1234 gboolean ignore_newlines = FALSE;
1236 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
1238 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
1239 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1241 if (!gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
1244 str = g_string_new ("");
1246 for (iter = start; !gtk_text_iter_equal (&iter, &end); gtk_text_iter_forward_char (&iter)) {
1247 c = gtk_text_iter_get_char (&iter);
1248 /* 0xFFFC is the 'object replacement' unicode character,
1249 * it indicates the presence of a pixbuf or a widget. */
1251 ignore_newlines = FALSE;
1252 if ((pixbuf = gtk_text_iter_get_pixbuf (&iter))) {
1254 text = g_object_get_data (G_OBJECT(pixbuf),
1257 str = g_string_append (str, text);
1258 } else if ((anchor = gtk_text_iter_get_child_anchor (&iter))) {
1260 list = gtk_text_child_anchor_get_widgets (anchor);
1262 text = g_object_get_data (G_OBJECT(list->data),
1265 str = g_string_append (str, text);
1269 } else if (c == '\n') {
1270 if (!ignore_newlines) {
1271 ignore_newlines = TRUE;
1272 str = g_string_append_unichar (str, c);
1275 ignore_newlines = FALSE;
1276 str = g_string_append_unichar (str, c);
1280 gtk_clipboard_set_text (clipboard, str->str, str->len);
1281 g_string_free (str, TRUE);
1285 chat_text_view_iface_init (EmpathyChatViewIface *iface)
1287 iface->append_message = chat_text_view_append_message;
1288 iface->append_event = chat_text_view_append_event;
1289 iface->scroll = chat_text_view_scroll;
1290 iface->scroll_down = chat_text_view_scroll_down;
1291 iface->get_has_selection = chat_text_view_get_has_selection;
1292 iface->clear = chat_text_view_clear;
1293 iface->find_previous = chat_text_view_find_previous;
1294 iface->find_next = chat_text_view_find_next;
1295 iface->find_abilities = chat_text_view_find_abilities;
1296 iface->highlight = chat_text_view_highlight;
1297 iface->copy_clipboard = chat_text_view_copy_clipboard;
1301 empathy_chat_text_view_get_last_contact (EmpathyChatTextView *view)
1303 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1305 g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), NULL);
1307 return priv->last_contact;
1311 empathy_chat_text_view_set_only_if_date (EmpathyChatTextView *view,
1312 gboolean only_if_date)
1314 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1316 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
1318 if (only_if_date != priv->only_if_date) {
1319 priv->only_if_date = only_if_date;
1320 g_object_notify (G_OBJECT (view), "only-if-date");
1325 chat_text_view_replace_link (const gchar *text,
1327 gpointer match_data,
1330 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (user_data);
1333 gtk_text_buffer_get_end_iter (buffer, &iter);
1334 gtk_text_buffer_insert_with_tags_by_name (buffer, &iter,
1336 EMPATHY_CHAT_TEXT_VIEW_TAG_LINK,
1341 chat_text_view_replace_smiley (const gchar *text,
1343 gpointer match_data,
1346 EmpathySmileyHit *hit = match_data;
1347 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (user_data);
1350 gtk_text_buffer_get_end_iter (buffer, &iter);
1351 gtk_text_buffer_insert_pixbuf (buffer, &iter, hit->pixbuf);
1355 chat_text_view_replace_verbatim (const gchar *text,
1357 gpointer match_data,
1360 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (user_data);
1363 gtk_text_buffer_get_end_iter (buffer, &iter);
1364 gtk_text_buffer_insert (buffer, &iter, text, len);
1367 static EmpathyStringParser string_parsers[] = {
1368 {empathy_string_match_link, chat_text_view_replace_link},
1369 {empathy_string_match_all, chat_text_view_replace_verbatim},
1373 static EmpathyStringParser string_parsers_with_smiley[] = {
1374 {empathy_string_match_link, chat_text_view_replace_link},
1375 {empathy_string_match_smiley, chat_text_view_replace_smiley},
1376 {empathy_string_match_all, chat_text_view_replace_verbatim},
1381 empathy_chat_text_view_append_body (EmpathyChatTextView *view,
1385 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1386 EmpathyStringParser *parsers;
1387 gboolean use_smileys;
1388 GtkTextIter start_iter;
1392 /* Check if we have to parse smileys */
1393 empathy_conf_get_bool (empathy_conf_get (),
1394 EMPATHY_PREFS_CHAT_SHOW_SMILEYS,
1397 parsers = string_parsers_with_smiley;
1399 parsers = string_parsers;
1401 /* Create a mark at the place we'll start inserting */
1402 gtk_text_buffer_get_end_iter (priv->buffer, &start_iter);
1403 mark = gtk_text_buffer_create_mark (priv->buffer, NULL, &start_iter, TRUE);
1405 /* Parse text for links/smileys and insert in the buffer */
1406 empathy_string_parser_substr (body, -1, parsers, priv->buffer);
1408 /* Insert a newline after the text inserted */
1409 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1410 gtk_text_buffer_insert (priv->buffer, &iter, "\n", 1);
1412 /* Apply the style to the inserted text. */
1413 gtk_text_buffer_get_iter_at_mark (priv->buffer, &start_iter, mark);
1414 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1415 gtk_text_buffer_apply_tag_by_name (priv->buffer, tag,
1419 gtk_text_buffer_delete_mark (priv->buffer, mark);
1423 empathy_chat_text_view_append_spacing (EmpathyChatTextView *view)
1425 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1428 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1429 gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
1433 EMPATHY_CHAT_TEXT_VIEW_TAG_CUT,
1434 EMPATHY_CHAT_TEXT_VIEW_TAG_SPACING,
1439 empathy_chat_text_view_tag_set (EmpathyChatTextView *view,
1440 const gchar *tag_name,
1441 const gchar *first_property_name,
1444 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1446 GtkTextTagTable *table;
1449 g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), NULL);
1450 g_return_val_if_fail (tag_name != NULL, NULL);
1452 table = gtk_text_buffer_get_tag_table (priv->buffer);
1453 tag = gtk_text_tag_table_lookup (table, tag_name);
1455 if (tag && first_property_name) {
1456 va_start (list, first_property_name);
1457 g_object_set_valist (G_OBJECT (tag), first_property_name, list);