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., 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>
29 #include <sys/types.h>
33 #include <glib/gi18n-lib.h>
36 #include <telepathy-glib/util.h>
37 #include <libmissioncontrol/mc-account.h>
39 #include <libempathy/empathy-utils.h>
41 #include "empathy-chat-text-view.h"
42 #include "empathy-chat.h"
43 #include "empathy-conf.h"
44 #include "empathy-ui-utils.h"
45 #include "empathy-smiley-manager.h"
47 #define DEBUG_FLAG EMPATHY_DEBUG_CHAT
48 #include <libempathy/empathy-debug.h>
50 /* Number of seconds between timestamps when using normal mode, 5 minutes. */
51 #define TIMESTAMP_INTERVAL 300
54 #define MAX_SCROLL_TIME 0.4 /* seconds */
55 #define SCROLL_DELAY 33 /* milliseconds */
57 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyChatTextView)
60 GtkTextBuffer *buffer;
63 GtkTextMark *find_mark_previous;
64 GtkTextMark *find_mark_next;
65 gboolean find_wrapped;
66 gboolean find_last_direction;
67 EmpathyContact *last_contact;
68 time_t last_timestamp;
69 gboolean allow_scrolling;
70 guint notify_system_fonts_id;
71 EmpathySmileyManager *smiley_manager;
72 gboolean only_if_date;
73 } EmpathyChatTextViewPriv;
75 static void chat_text_view_iface_init (EmpathyChatViewIface *iface);
77 G_DEFINE_TYPE_WITH_CODE (EmpathyChatTextView, empathy_chat_text_view,
79 G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CHAT_VIEW,
80 chat_text_view_iface_init));
89 chat_text_view_url_event_cb (GtkTextTag *tag,
93 EmpathyChatTextView *view)
95 EmpathyChatTextViewPriv *priv;
96 GtkTextIter start, end;
99 priv = GET_PRIV (view);
101 /* If the link is being selected, don't do anything. */
102 gtk_text_buffer_get_selection_bounds (priv->buffer, &start, &end);
103 if (gtk_text_iter_get_offset (&start) != gtk_text_iter_get_offset (&end)) {
107 if (event->type == GDK_BUTTON_RELEASE && event->button.button == 1) {
110 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
111 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
112 str = gtk_text_buffer_get_text (priv->buffer,
117 empathy_url_show (GTK_WIDGET (view), str);
126 chat_text_view_event_cb (EmpathyChatTextView *view,
127 GdkEventMotion *event,
130 static GdkCursor *hand = NULL;
131 static GdkCursor *beam = NULL;
132 GtkTextWindowType type;
135 gint x, y, buf_x, buf_y;
137 type = gtk_text_view_get_window_type (GTK_TEXT_VIEW (view),
140 if (type != GTK_TEXT_WINDOW_TEXT) {
144 /* Get where the pointer really is. */
145 win = gtk_text_view_get_window (GTK_TEXT_VIEW (view), type);
150 gdk_window_get_pointer (win, &x, &y, NULL);
152 /* Get the iter where the cursor is at */
153 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view), type,
157 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view),
161 if (gtk_text_iter_has_tag (&iter, tag)) {
163 hand = gdk_cursor_new (GDK_HAND2);
164 beam = gdk_cursor_new (GDK_XTERM);
166 gdk_window_set_cursor (win, hand);
169 beam = gdk_cursor_new (GDK_XTERM);
171 gdk_window_set_cursor (win, beam);
178 chat_text_view_create_tags (EmpathyChatTextView *view)
180 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
183 gtk_text_buffer_create_tag (priv->buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_CUT, NULL);
184 gtk_text_buffer_create_tag (priv->buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_HIGHLIGHT, NULL);
185 gtk_text_buffer_create_tag (priv->buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_SPACING, NULL);
186 gtk_text_buffer_create_tag (priv->buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_TIME, NULL);
187 gtk_text_buffer_create_tag (priv->buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_ACTION, NULL);
188 gtk_text_buffer_create_tag (priv->buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_BODY, NULL);
189 gtk_text_buffer_create_tag (priv->buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_EVENT, NULL);
191 tag = gtk_text_buffer_create_tag (priv->buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_LINK, NULL);
192 g_signal_connect (tag, "event",
193 G_CALLBACK (chat_text_view_url_event_cb),
196 g_signal_connect (view, "motion-notify-event",
197 G_CALLBACK (chat_text_view_event_cb),
202 chat_text_view_system_font_update (EmpathyChatTextView *view)
204 PangoFontDescription *font_description = NULL;
207 if (empathy_conf_get_string (empathy_conf_get (),
208 "/desktop/gnome/interface/document_font_name",
209 &font_name) && font_name) {
210 font_description = pango_font_description_from_string (font_name);
213 font_description = NULL;
216 gtk_widget_modify_font (GTK_WIDGET (view), font_description);
218 if (font_description) {
219 pango_font_description_free (font_description);
224 chat_text_view_notify_system_font_cb (EmpathyConf *conf,
228 EmpathyChatTextView *view = user_data;
230 chat_text_view_system_font_update (view);
234 chat_text_view_clear_view_cb (GtkMenuItem *menuitem, EmpathyChatTextView *view)
236 empathy_chat_view_clear (EMPATHY_CHAT_VIEW (view));
240 chat_text_view_open_address_cb (GtkMenuItem *menuitem, const gchar *url)
242 empathy_url_show (GTK_WIDGET (menuitem), url);
246 chat_text_view_copy_address_cb (GtkMenuItem *menuitem, const gchar *url)
248 GtkClipboard *clipboard;
250 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
251 gtk_clipboard_set_text (clipboard, url, -1);
253 clipboard = gtk_clipboard_get (GDK_SELECTION_PRIMARY);
254 gtk_clipboard_set_text (clipboard, url, -1);
258 chat_text_view_populate_popup (EmpathyChatTextView *view,
262 EmpathyChatTextViewPriv *priv;
263 GtkTextTagTable *table;
266 GtkTextIter iter, start, end;
270 priv = GET_PRIV (view);
272 /* Clear menu item */
273 if (gtk_text_buffer_get_char_count (priv->buffer) > 0) {
274 item = gtk_menu_item_new ();
275 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
276 gtk_widget_show (item);
278 item = gtk_image_menu_item_new_from_stock (GTK_STOCK_CLEAR, NULL);
279 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
280 gtk_widget_show (item);
282 g_signal_connect (item,
284 G_CALLBACK (chat_text_view_clear_view_cb),
288 /* Link context menu items */
289 table = gtk_text_buffer_get_tag_table (priv->buffer);
290 tag = gtk_text_tag_table_lookup (table, EMPATHY_CHAT_TEXT_VIEW_TAG_LINK);
292 gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
294 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
295 GTK_TEXT_WINDOW_WIDGET,
299 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &iter, x, y);
303 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
304 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
305 str = gtk_text_buffer_get_text (priv->buffer,
306 &start, &end, FALSE);
309 if (EMP_STR_EMPTY (str)) {
314 /* NOTE: Set data just to get the string freed when not needed. */
315 g_object_set_data_full (G_OBJECT (menu),
317 (GDestroyNotify) g_free);
319 item = gtk_menu_item_new ();
320 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
321 gtk_widget_show (item);
323 item = gtk_menu_item_new_with_mnemonic (_("_Copy Link Address"));
324 g_signal_connect (item,
326 G_CALLBACK (chat_text_view_copy_address_cb),
328 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
329 gtk_widget_show (item);
331 item = gtk_menu_item_new_with_mnemonic (_("_Open Link"));
332 g_signal_connect (item,
334 G_CALLBACK (chat_text_view_open_address_cb),
336 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
337 gtk_widget_show (item);
341 chat_text_view_is_scrolled_down (EmpathyChatTextView *view)
345 sw = gtk_widget_get_parent (GTK_WIDGET (view));
346 if (GTK_IS_SCROLLED_WINDOW (sw)) {
349 vadj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (sw));
351 if (vadj->value + vadj->page_size / 2 < vadj->upper - vadj->page_size) {
360 chat_text_view_maybe_trim_buffer (EmpathyChatTextView *view)
362 EmpathyChatTextViewPriv *priv;
363 GtkTextIter top, bottom;
366 GtkTextTagTable *table;
369 priv = GET_PRIV (view);
371 gtk_text_buffer_get_end_iter (priv->buffer, &bottom);
372 line = gtk_text_iter_get_line (&bottom);
373 if (line < MAX_LINES) {
377 remove = line - MAX_LINES;
378 gtk_text_buffer_get_start_iter (priv->buffer, &top);
381 if (!gtk_text_iter_forward_lines (&bottom, remove)) {
385 /* Track backwords to a place where we can safely cut, we don't do it in
386 * the middle of a tag.
388 table = gtk_text_buffer_get_tag_table (priv->buffer);
389 tag = gtk_text_tag_table_lookup (table, EMPATHY_CHAT_TEXT_VIEW_TAG_CUT);
394 if (!gtk_text_iter_forward_to_tag_toggle (&bottom, tag)) {
398 if (!gtk_text_iter_equal (&top, &bottom)) {
399 gtk_text_buffer_delete (priv->buffer, &top, &bottom);
404 chat_text_view_append_timestamp (EmpathyChatTextView *view,
408 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
413 str = g_string_new ("- ");
415 /* Append date if needed */
420 date = g_date_new ();
421 g_date_set_time_t (date, timestamp);
422 /* Translators: timestamp displayed between conversations in
423 * chat windows (strftime format string) */
424 g_date_strftime (buf, 256, _("%A %B %d %Y"), date);
425 g_string_append (str, buf);
426 g_string_append (str, ", ");
431 tmp = empathy_time_to_string_local (timestamp, EMPATHY_TIME_FORMAT_DISPLAY_SHORT);
432 g_string_append (str, tmp);
435 g_string_append (str, " -\n");
437 /* Insert the string in the buffer */
438 empathy_chat_text_view_append_spacing (view);
439 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
440 gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
443 EMPATHY_CHAT_TEXT_VIEW_TAG_TIME,
446 priv->last_timestamp = timestamp;
448 g_string_free (str, TRUE);
452 chat_text_maybe_append_date_and_time (EmpathyChatTextView *view,
455 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
456 GDate *date, *last_date;
457 gboolean append_date = FALSE;
458 gboolean append_time = FALSE;
460 /* Get the date from last message */
461 last_date = g_date_new ();
462 g_date_set_time_t (last_date, priv->last_timestamp);
464 /* Get the date of the message we are appending */
465 date = g_date_new ();
466 g_date_set_time_t (date, timestamp);
468 /* If last message was from another day we append date and time */
469 if (g_date_compare (date, last_date) > 0) {
474 g_date_free (last_date);
477 /* If last message is 'old' append the time */
478 if (timestamp - priv->last_timestamp >= TIMESTAMP_INTERVAL) {
482 if (append_date || (!priv->only_if_date && append_time)) {
483 chat_text_view_append_timestamp (view, timestamp, append_date);
488 chat_text_view_size_allocate (GtkWidget *widget,
489 GtkAllocation *alloc)
493 down = chat_text_view_is_scrolled_down (EMPATHY_CHAT_TEXT_VIEW (widget));
495 GTK_WIDGET_CLASS (empathy_chat_text_view_parent_class)->size_allocate (widget, alloc);
500 adj = GTK_TEXT_VIEW (widget)->vadjustment;
501 gtk_adjustment_set_value (adj, adj->upper - adj->page_size);
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);
580 G_OBJECT_CLASS (empathy_chat_text_view_parent_class)->finalize (object);
584 empathy_chat_text_view_class_init (EmpathyChatTextViewClass *klass)
586 GObjectClass *object_class = G_OBJECT_CLASS (klass);
587 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
589 object_class->finalize = chat_text_view_finalize;
590 object_class->get_property = chat_text_view_get_property;
591 object_class->set_property = chat_text_view_set_property;
593 widget_class->size_allocate = chat_text_view_size_allocate;
594 widget_class->drag_motion = chat_text_view_drag_motion;
596 g_object_class_install_property (object_class,
598 g_param_spec_object ("last-contact",
600 "The sender of the last received message",
601 EMPATHY_TYPE_CONTACT,
603 g_object_class_install_property (object_class,
605 g_param_spec_boolean ("only-if-date",
607 "Display timestamp only if the date changes",
612 g_type_class_add_private (object_class, sizeof (EmpathyChatTextViewPriv));
616 empathy_chat_text_view_init (EmpathyChatTextView *view)
618 EmpathyChatTextViewPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (view,
619 EMPATHY_TYPE_CHAT_TEXT_VIEW, EmpathyChatTextViewPriv);
622 priv->buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
623 priv->last_timestamp = 0;
624 priv->allow_scrolling = TRUE;
625 priv->smiley_manager = empathy_smiley_manager_dup_singleton ();
628 "wrap-mode", GTK_WRAP_WORD_CHAR,
630 "cursor-visible", FALSE,
633 priv->notify_system_fonts_id =
634 empathy_conf_notify_add (empathy_conf_get (),
635 "/desktop/gnome/interface/document_font_name",
636 chat_text_view_notify_system_font_cb,
638 chat_text_view_system_font_update (view);
639 chat_text_view_create_tags (view);
641 g_signal_connect (view,
643 G_CALLBACK (chat_text_view_populate_popup),
647 /* Code stolen from pidgin/gtkimhtml.c */
649 chat_text_view_scroll_cb (EmpathyChatTextView *view)
651 EmpathyChatTextViewPriv *priv;
655 priv = GET_PRIV (view);
656 adj = GTK_TEXT_VIEW (view)->vadjustment;
657 max_val = adj->upper - adj->page_size;
659 g_return_val_if_fail (priv->scroll_time != NULL, FALSE);
661 if (g_timer_elapsed (priv->scroll_time, NULL) > MAX_SCROLL_TIME) {
662 /* time's up. jump to the end and kill the timer */
663 gtk_adjustment_set_value (adj, max_val);
664 g_timer_destroy (priv->scroll_time);
665 priv->scroll_time = NULL;
666 priv->scroll_timeout = 0;
670 /* scroll by 1/3rd the remaining distance */
671 gtk_adjustment_set_value (adj, gtk_adjustment_get_value (adj) + ((max_val - gtk_adjustment_get_value (adj)) / 3));
676 chat_text_view_scroll_down (EmpathyChatView *view)
678 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
680 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
682 if (!priv->allow_scrolling) {
686 DEBUG ("Scrolling down");
688 if (priv->scroll_time) {
689 g_timer_reset (priv->scroll_time);
691 priv->scroll_time = g_timer_new();
693 if (!priv->scroll_timeout) {
694 priv->scroll_timeout = g_timeout_add (SCROLL_DELAY,
695 (GSourceFunc) chat_text_view_scroll_cb,
701 chat_text_view_append_message (EmpathyChatView *view,
704 EmpathyChatTextView *text_view = EMPATHY_CHAT_TEXT_VIEW (view);
705 EmpathyChatTextViewPriv *priv = GET_PRIV (text_view);
709 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
710 g_return_if_fail (EMPATHY_IS_MESSAGE (msg));
712 if (!empathy_message_get_body (msg)) {
716 bottom = chat_text_view_is_scrolled_down (text_view);
718 chat_text_view_maybe_trim_buffer (EMPATHY_CHAT_TEXT_VIEW (view));
720 timestamp = empathy_message_get_timestamp (msg);
721 chat_text_maybe_append_date_and_time (text_view, timestamp);
722 if (EMPATHY_CHAT_TEXT_VIEW_GET_CLASS (view)->append_message) {
723 EMPATHY_CHAT_TEXT_VIEW_GET_CLASS (view)->append_message (text_view,
728 chat_text_view_scroll_down (view);
731 if (priv->last_contact) {
732 g_object_unref (priv->last_contact);
734 priv->last_contact = g_object_ref (empathy_message_get_sender (msg));
735 g_object_notify (G_OBJECT (view), "last-contact");
739 chat_text_view_append_event (EmpathyChatView *view,
742 EmpathyChatTextView *text_view = EMPATHY_CHAT_TEXT_VIEW (view);
743 EmpathyChatTextViewPriv *priv = GET_PRIV (text_view);
749 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
750 g_return_if_fail (!EMP_STR_EMPTY (str));
752 bottom = chat_text_view_is_scrolled_down (text_view);
753 chat_text_view_maybe_trim_buffer (EMPATHY_CHAT_TEXT_VIEW (view));
754 chat_text_maybe_append_date_and_time (text_view,
755 empathy_time_get_current ());
757 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
758 msg = g_strdup_printf (" - %s\n", str);
759 gtk_text_buffer_insert_with_tags_by_name (priv->buffer, &iter,
761 EMPATHY_CHAT_TEXT_VIEW_TAG_EVENT,
766 chat_text_view_scroll_down (view);
769 if (priv->last_contact) {
770 g_object_unref (priv->last_contact);
771 priv->last_contact = NULL;
772 g_object_notify (G_OBJECT (view), "last-contact");
777 chat_text_view_scroll (EmpathyChatView *view,
778 gboolean allow_scrolling)
780 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
782 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
784 DEBUG ("Scrolling %s", allow_scrolling ? "enabled" : "disabled");
786 priv->allow_scrolling = allow_scrolling;
787 if (allow_scrolling) {
788 empathy_chat_view_scroll_down (view);
793 chat_text_view_get_has_selection (EmpathyChatView *view)
795 GtkTextBuffer *buffer;
797 g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), FALSE);
799 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
801 return gtk_text_buffer_get_has_selection (buffer);
805 chat_text_view_clear (EmpathyChatView *view)
807 GtkTextBuffer *buffer;
808 EmpathyChatTextViewPriv *priv;
810 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
812 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
813 gtk_text_buffer_set_text (buffer, "", -1);
815 /* We set these back to the initial values so we get
816 * timestamps when clearing the window to know when
817 * conversations start.
819 priv = GET_PRIV (view);
821 priv->last_timestamp = 0;
825 chat_text_view_find_previous (EmpathyChatView *view,
826 const gchar *search_criteria,
829 EmpathyChatTextViewPriv *priv;
830 GtkTextBuffer *buffer;
831 GtkTextIter iter_at_mark;
832 GtkTextIter iter_match_start;
833 GtkTextIter iter_match_end;
835 gboolean from_start = FALSE;
837 g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), FALSE);
838 g_return_val_if_fail (search_criteria != NULL, FALSE);
840 priv = GET_PRIV (view);
842 buffer = priv->buffer;
844 if (EMP_STR_EMPTY (search_criteria)) {
845 if (priv->find_mark_previous) {
846 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
848 gtk_text_buffer_move_mark (buffer,
849 priv->find_mark_previous,
851 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
852 priv->find_mark_previous,
857 gtk_text_buffer_select_range (buffer,
869 if (priv->find_mark_previous) {
870 gtk_text_buffer_get_iter_at_mark (buffer,
872 priv->find_mark_previous);
874 gtk_text_buffer_get_end_iter (buffer, &iter_at_mark);
878 priv->find_last_direction = FALSE;
880 found = empathy_text_iter_backward_search (&iter_at_mark,
887 gboolean result = FALSE;
893 /* Here we wrap around. */
894 if (!new_search && !priv->find_wrapped) {
895 priv->find_wrapped = TRUE;
896 result = chat_text_view_find_previous (view,
899 priv->find_wrapped = FALSE;
905 /* Set new mark and show on screen */
906 if (!priv->find_mark_previous) {
907 priv->find_mark_previous = gtk_text_buffer_create_mark (buffer, NULL,
911 gtk_text_buffer_move_mark (buffer,
912 priv->find_mark_previous,
916 if (!priv->find_mark_next) {
917 priv->find_mark_next = gtk_text_buffer_create_mark (buffer, NULL,
921 gtk_text_buffer_move_mark (buffer,
922 priv->find_mark_next,
926 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
927 priv->find_mark_previous,
933 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &iter_match_start);
934 gtk_text_buffer_move_mark_by_name (buffer, "insert", &iter_match_end);
940 chat_text_view_find_next (EmpathyChatView *view,
941 const gchar *search_criteria,
944 EmpathyChatTextViewPriv *priv;
945 GtkTextBuffer *buffer;
946 GtkTextIter iter_at_mark;
947 GtkTextIter iter_match_start;
948 GtkTextIter iter_match_end;
950 gboolean from_start = FALSE;
952 g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), FALSE);
953 g_return_val_if_fail (search_criteria != NULL, FALSE);
955 priv = GET_PRIV (view);
957 buffer = priv->buffer;
959 if (EMP_STR_EMPTY (search_criteria)) {
960 if (priv->find_mark_next) {
961 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
963 gtk_text_buffer_move_mark (buffer,
964 priv->find_mark_next,
966 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
967 priv->find_mark_next,
972 gtk_text_buffer_select_range (buffer,
984 if (priv->find_mark_next) {
985 gtk_text_buffer_get_iter_at_mark (buffer,
987 priv->find_mark_next);
989 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
993 priv->find_last_direction = TRUE;
995 found = empathy_text_iter_forward_search (&iter_at_mark,
1002 gboolean result = FALSE;
1008 /* Here we wrap around. */
1009 if (!new_search && !priv->find_wrapped) {
1010 priv->find_wrapped = TRUE;
1011 result = chat_text_view_find_next (view,
1014 priv->find_wrapped = FALSE;
1020 /* Set new mark and show on screen */
1021 if (!priv->find_mark_next) {
1022 priv->find_mark_next = gtk_text_buffer_create_mark (buffer, NULL,
1026 gtk_text_buffer_move_mark (buffer,
1027 priv->find_mark_next,
1031 if (!priv->find_mark_previous) {
1032 priv->find_mark_previous = gtk_text_buffer_create_mark (buffer, NULL,
1036 gtk_text_buffer_move_mark (buffer,
1037 priv->find_mark_previous,
1041 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
1042 priv->find_mark_next,
1048 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &iter_match_start);
1049 gtk_text_buffer_move_mark_by_name (buffer, "insert", &iter_match_end);
1055 chat_text_view_find_abilities (EmpathyChatView *view,
1056 const gchar *search_criteria,
1057 gboolean *can_do_previous,
1058 gboolean *can_do_next)
1060 EmpathyChatTextViewPriv *priv;
1061 GtkTextBuffer *buffer;
1062 GtkTextIter iter_at_mark;
1063 GtkTextIter iter_match_start;
1064 GtkTextIter iter_match_end;
1066 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
1067 g_return_if_fail (search_criteria != NULL);
1068 g_return_if_fail (can_do_previous != NULL && can_do_next != NULL);
1070 priv = GET_PRIV (view);
1072 buffer = priv->buffer;
1074 if (can_do_previous) {
1075 if (priv->find_mark_previous) {
1076 gtk_text_buffer_get_iter_at_mark (buffer,
1078 priv->find_mark_previous);
1080 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1083 *can_do_previous = empathy_text_iter_backward_search (&iter_at_mark,
1091 if (priv->find_mark_next) {
1092 gtk_text_buffer_get_iter_at_mark (buffer,
1094 priv->find_mark_next);
1096 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1099 *can_do_next = empathy_text_iter_forward_search (&iter_at_mark,
1108 chat_text_view_highlight (EmpathyChatView *view,
1111 GtkTextBuffer *buffer;
1113 GtkTextIter iter_start;
1114 GtkTextIter iter_end;
1115 GtkTextIter iter_match_start;
1116 GtkTextIter iter_match_end;
1119 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
1121 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
1123 gtk_text_buffer_get_start_iter (buffer, &iter);
1125 gtk_text_buffer_get_bounds (buffer, &iter_start, &iter_end);
1126 gtk_text_buffer_remove_tag_by_name (buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_HIGHLIGHT,
1130 if (EMP_STR_EMPTY (text)) {
1135 found = empathy_text_iter_forward_search (&iter,
1145 gtk_text_buffer_apply_tag_by_name (buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_HIGHLIGHT,
1149 iter = iter_match_end;
1150 gtk_text_iter_forward_char (&iter);
1155 chat_text_view_copy_clipboard (EmpathyChatView *view)
1157 GtkTextBuffer *buffer;
1158 GtkClipboard *clipboard;
1160 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
1162 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
1163 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1165 gtk_text_buffer_copy_clipboard (buffer, clipboard);
1169 chat_text_view_iface_init (EmpathyChatViewIface *iface)
1171 iface->append_message = chat_text_view_append_message;
1172 iface->append_event = chat_text_view_append_event;
1173 iface->scroll = chat_text_view_scroll;
1174 iface->scroll_down = chat_text_view_scroll_down;
1175 iface->get_has_selection = chat_text_view_get_has_selection;
1176 iface->clear = chat_text_view_clear;
1177 iface->find_previous = chat_text_view_find_previous;
1178 iface->find_next = chat_text_view_find_next;
1179 iface->find_abilities = chat_text_view_find_abilities;
1180 iface->highlight = chat_text_view_highlight;
1181 iface->copy_clipboard = chat_text_view_copy_clipboard;
1185 empathy_chat_text_view_get_last_contact (EmpathyChatTextView *view)
1187 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1189 g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), NULL);
1191 return priv->last_contact;
1195 empathy_chat_text_view_set_only_if_date (EmpathyChatTextView *view,
1196 gboolean only_if_date)
1198 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1200 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
1202 if (only_if_date != priv->only_if_date) {
1203 priv->only_if_date = only_if_date;
1204 g_object_notify (G_OBJECT (view), "only-if-date");
1209 chat_text_view_insert_text_with_emoticons (EmpathyChatTextView *view,
1213 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1214 gboolean use_smileys = FALSE;
1215 GSList *smileys, *l;
1217 empathy_conf_get_bool (empathy_conf_get (),
1218 EMPATHY_PREFS_CHAT_SHOW_SMILEYS,
1222 gtk_text_buffer_insert (priv->buffer, iter, str, -1);
1226 smileys = empathy_smiley_manager_parse (priv->smiley_manager, str);
1227 for (l = smileys; l; l = l->next) {
1228 EmpathySmiley *smiley;
1231 if (smiley->pixbuf) {
1232 gtk_text_buffer_insert_pixbuf (priv->buffer, iter, smiley->pixbuf);
1234 gtk_text_buffer_insert (priv->buffer, iter, smiley->str, -1);
1236 empathy_smiley_free (smiley);
1238 g_slist_free (smileys);
1242 empathy_chat_text_view_append_body (EmpathyChatTextView *view,
1246 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1247 GtkTextIter start_iter, end_iter;
1251 GMatchInfo *match_info;
1257 priv = GET_PRIV (view);
1259 gtk_text_buffer_get_end_iter (priv->buffer, &start_iter);
1260 mark = gtk_text_buffer_create_mark (priv->buffer, NULL, &start_iter, TRUE);
1262 uri_regex = empathy_uri_regex_dup_singleton ();
1263 for (match = g_regex_match (uri_regex, body, 0, &match_info); match;
1264 match = g_match_info_next (match_info, NULL)) {
1265 if (!g_match_info_fetch_pos (match_info, 0, &s, &e))
1269 tmp = empathy_substring (body, last, s);
1271 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1272 chat_text_view_insert_text_with_emoticons (view,
1278 tmp = empathy_substring (body, s, e);
1280 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1281 gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
1285 EMPATHY_CHAT_TEXT_VIEW_TAG_LINK,
1291 g_match_info_free (match_info);
1292 g_regex_unref (uri_regex);
1294 if (last < strlen (body)) {
1295 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1296 chat_text_view_insert_text_with_emoticons (view,
1301 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1302 gtk_text_buffer_insert (priv->buffer, &iter, "\n", 1);
1304 /* Apply the style to the inserted text. */
1305 gtk_text_buffer_get_iter_at_mark (priv->buffer, &start_iter, mark);
1306 gtk_text_buffer_get_end_iter (priv->buffer, &end_iter);
1308 gtk_text_buffer_apply_tag_by_name (priv->buffer,
1313 gtk_text_buffer_delete_mark (priv->buffer, mark);
1317 empathy_chat_text_view_append_spacing (EmpathyChatTextView *view)
1319 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1322 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1323 gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
1327 EMPATHY_CHAT_TEXT_VIEW_TAG_CUT,
1328 EMPATHY_CHAT_TEXT_VIEW_TAG_SPACING,
1333 empathy_chat_text_view_tag_set (EmpathyChatTextView *view,
1334 const gchar *tag_name,
1335 const gchar *first_property_name,
1338 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1340 GtkTextTagTable *table;
1343 g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), NULL);
1344 g_return_val_if_fail (tag_name != NULL, NULL);
1346 table = gtk_text_buffer_get_tag_table (priv->buffer);
1347 tag = gtk_text_tag_table_lookup (table, tag_name);
1349 if (tag && first_property_name) {
1350 va_start (list, first_property_name);
1351 g_object_set_valist (G_OBJECT (tag), first_property_name, list);