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>
34 #include <gtk/gtkbutton.h>
35 #include <gtk/gtkimage.h>
36 #include <gtk/gtkmenu.h>
37 #include <gtk/gtkmenuitem.h>
38 #include <gtk/gtkimagemenuitem.h>
39 #include <gtk/gtkstock.h>
40 #include <gtk/gtkscrolledwindow.h>
41 #include <gtk/gtksizegroup.h>
42 #include <glade/glade.h>
44 #include <telepathy-glib/util.h>
45 #include <libmissioncontrol/mc-account.h>
47 #include <libempathy/empathy-utils.h>
49 #include "empathy-chat-text-view.h"
50 #include "empathy-chat.h"
51 #include "empathy-conf.h"
52 #include "empathy-ui-utils.h"
53 #include "empathy-smiley-manager.h"
55 #define DEBUG_FLAG EMPATHY_DEBUG_CHAT
56 #include <libempathy/empathy-debug.h>
58 /* Number of seconds between timestamps when using normal mode, 5 minutes. */
59 #define TIMESTAMP_INTERVAL 300
62 #define MAX_SCROLL_TIME 0.4 /* seconds */
63 #define SCROLL_DELAY 33 /* milliseconds */
65 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyChatTextView)
68 GtkTextBuffer *buffer;
71 GtkTextMark *find_mark_previous;
72 GtkTextMark *find_mark_next;
73 gboolean find_wrapped;
74 gboolean find_last_direction;
75 EmpathyContact *last_contact;
76 time_t last_timestamp;
77 gboolean allow_scrolling;
78 guint notify_system_fonts_id;
79 EmpathySmileyManager *smiley_manager;
80 gboolean only_if_date;
81 } EmpathyChatTextViewPriv;
83 static void chat_text_view_iface_init (EmpathyChatViewIface *iface);
85 G_DEFINE_TYPE_WITH_CODE (EmpathyChatTextView, empathy_chat_text_view,
87 G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CHAT_VIEW,
88 chat_text_view_iface_init));
97 chat_text_view_url_event_cb (GtkTextTag *tag,
101 EmpathyChatTextView *view)
103 EmpathyChatTextViewPriv *priv;
104 GtkTextIter start, end;
107 priv = GET_PRIV (view);
109 /* If the link is being selected, don't do anything. */
110 gtk_text_buffer_get_selection_bounds (priv->buffer, &start, &end);
111 if (gtk_text_iter_get_offset (&start) != gtk_text_iter_get_offset (&end)) {
115 if (event->type == GDK_BUTTON_RELEASE && event->button.button == 1) {
118 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
119 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
120 str = gtk_text_buffer_get_text (priv->buffer,
125 empathy_url_show (GTK_WIDGET (view), str);
134 chat_text_view_event_cb (EmpathyChatTextView *view,
135 GdkEventMotion *event,
138 static GdkCursor *hand = NULL;
139 static GdkCursor *beam = NULL;
140 GtkTextWindowType type;
143 gint x, y, buf_x, buf_y;
145 type = gtk_text_view_get_window_type (GTK_TEXT_VIEW (view),
148 if (type != GTK_TEXT_WINDOW_TEXT) {
152 /* Get where the pointer really is. */
153 win = gtk_text_view_get_window (GTK_TEXT_VIEW (view), type);
158 gdk_window_get_pointer (win, &x, &y, NULL);
160 /* Get the iter where the cursor is at */
161 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view), type,
165 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view),
169 if (gtk_text_iter_has_tag (&iter, tag)) {
171 hand = gdk_cursor_new (GDK_HAND2);
172 beam = gdk_cursor_new (GDK_XTERM);
174 gdk_window_set_cursor (win, hand);
177 beam = gdk_cursor_new (GDK_XTERM);
179 gdk_window_set_cursor (win, beam);
186 chat_text_view_create_tags (EmpathyChatTextView *view)
188 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
191 gtk_text_buffer_create_tag (priv->buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_CUT, NULL);
192 gtk_text_buffer_create_tag (priv->buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_HIGHLIGHT, NULL);
193 gtk_text_buffer_create_tag (priv->buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_SPACING, NULL);
194 gtk_text_buffer_create_tag (priv->buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_TIME, NULL);
195 gtk_text_buffer_create_tag (priv->buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_ACTION, NULL);
196 gtk_text_buffer_create_tag (priv->buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_BODY, NULL);
197 gtk_text_buffer_create_tag (priv->buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_EVENT, NULL);
199 tag = gtk_text_buffer_create_tag (priv->buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_LINK, NULL);
200 g_signal_connect (tag, "event",
201 G_CALLBACK (chat_text_view_url_event_cb),
204 g_signal_connect (view, "motion-notify-event",
205 G_CALLBACK (chat_text_view_event_cb),
210 chat_text_view_system_font_update (EmpathyChatTextView *view)
212 PangoFontDescription *font_description = NULL;
215 if (empathy_conf_get_string (empathy_conf_get (),
216 "/desktop/gnome/interface/document_font_name",
217 &font_name) && font_name) {
218 font_description = pango_font_description_from_string (font_name);
221 font_description = NULL;
224 gtk_widget_modify_font (GTK_WIDGET (view), font_description);
226 if (font_description) {
227 pango_font_description_free (font_description);
232 chat_text_view_notify_system_font_cb (EmpathyConf *conf,
236 EmpathyChatTextView *view = user_data;
238 chat_text_view_system_font_update (view);
242 chat_text_view_clear_view_cb (GtkMenuItem *menuitem, EmpathyChatTextView *view)
244 empathy_chat_view_clear (EMPATHY_CHAT_VIEW (view));
248 chat_text_view_open_address_cb (GtkMenuItem *menuitem, const gchar *url)
250 empathy_url_show (GTK_WIDGET (menuitem), url);
254 chat_text_view_copy_address_cb (GtkMenuItem *menuitem, const gchar *url)
256 GtkClipboard *clipboard;
258 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
259 gtk_clipboard_set_text (clipboard, url, -1);
261 clipboard = gtk_clipboard_get (GDK_SELECTION_PRIMARY);
262 gtk_clipboard_set_text (clipboard, url, -1);
266 chat_text_view_populate_popup (EmpathyChatTextView *view,
270 EmpathyChatTextViewPriv *priv;
271 GtkTextTagTable *table;
274 GtkTextIter iter, start, end;
278 priv = GET_PRIV (view);
280 /* Clear menu item */
281 if (gtk_text_buffer_get_char_count (priv->buffer) > 0) {
282 item = gtk_menu_item_new ();
283 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
284 gtk_widget_show (item);
286 item = gtk_image_menu_item_new_from_stock (GTK_STOCK_CLEAR, NULL);
287 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
288 gtk_widget_show (item);
290 g_signal_connect (item,
292 G_CALLBACK (chat_text_view_clear_view_cb),
296 /* Link context menu items */
297 table = gtk_text_buffer_get_tag_table (priv->buffer);
298 tag = gtk_text_tag_table_lookup (table, EMPATHY_CHAT_TEXT_VIEW_TAG_LINK);
300 gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
302 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
303 GTK_TEXT_WINDOW_WIDGET,
307 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &iter, x, y);
311 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
312 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
313 str = gtk_text_buffer_get_text (priv->buffer,
314 &start, &end, FALSE);
317 if (EMP_STR_EMPTY (str)) {
322 /* NOTE: Set data just to get the string freed when not needed. */
323 g_object_set_data_full (G_OBJECT (menu),
325 (GDestroyNotify) g_free);
327 item = gtk_menu_item_new ();
328 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
329 gtk_widget_show (item);
331 item = gtk_menu_item_new_with_mnemonic (_("_Copy Link Address"));
332 g_signal_connect (item,
334 G_CALLBACK (chat_text_view_copy_address_cb),
336 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
337 gtk_widget_show (item);
339 item = gtk_menu_item_new_with_mnemonic (_("_Open Link"));
340 g_signal_connect (item,
342 G_CALLBACK (chat_text_view_open_address_cb),
344 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
345 gtk_widget_show (item);
349 chat_text_view_is_scrolled_down (EmpathyChatTextView *view)
353 sw = gtk_widget_get_parent (GTK_WIDGET (view));
354 if (GTK_IS_SCROLLED_WINDOW (sw)) {
357 vadj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (sw));
359 if (vadj->value + vadj->page_size / 2 < vadj->upper - vadj->page_size) {
368 chat_text_view_maybe_trim_buffer (EmpathyChatTextView *view)
370 EmpathyChatTextViewPriv *priv;
371 GtkTextIter top, bottom;
374 GtkTextTagTable *table;
377 priv = GET_PRIV (view);
379 gtk_text_buffer_get_end_iter (priv->buffer, &bottom);
380 line = gtk_text_iter_get_line (&bottom);
381 if (line < MAX_LINES) {
385 remove = line - MAX_LINES;
386 gtk_text_buffer_get_start_iter (priv->buffer, &top);
389 if (!gtk_text_iter_forward_lines (&bottom, remove)) {
393 /* Track backwords to a place where we can safely cut, we don't do it in
394 * the middle of a tag.
396 table = gtk_text_buffer_get_tag_table (priv->buffer);
397 tag = gtk_text_tag_table_lookup (table, EMPATHY_CHAT_TEXT_VIEW_TAG_CUT);
402 if (!gtk_text_iter_forward_to_tag_toggle (&bottom, tag)) {
406 if (!gtk_text_iter_equal (&top, &bottom)) {
407 gtk_text_buffer_delete (priv->buffer, &top, &bottom);
412 chat_text_view_append_timestamp (EmpathyChatTextView *view,
416 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
421 str = g_string_new ("- ");
423 /* Append date if needed */
428 date = g_date_new ();
429 g_date_set_time_t (date, timestamp);
430 /* Translators: timestamp displayed between conversations in
431 * chat windows (strftime format string) */
432 g_date_strftime (buf, 256, _("%A %B %d %Y"), date);
433 g_string_append (str, buf);
434 g_string_append (str, ", ");
439 tmp = empathy_time_to_string_local (timestamp, EMPATHY_TIME_FORMAT_DISPLAY_SHORT);
440 g_string_append (str, tmp);
443 g_string_append (str, " -\n");
445 /* Insert the string in the buffer */
446 empathy_chat_text_view_append_spacing (view);
447 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
448 gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
451 EMPATHY_CHAT_TEXT_VIEW_TAG_TIME,
454 priv->last_timestamp = timestamp;
456 g_string_free (str, TRUE);
460 chat_text_maybe_append_date_and_time (EmpathyChatTextView *view,
463 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
464 GDate *date, *last_date;
465 gboolean append_date = FALSE;
466 gboolean append_time = FALSE;
468 /* Get the date from last message */
469 last_date = g_date_new ();
470 g_date_set_time_t (last_date, priv->last_timestamp);
472 /* Get the date of the message we are appending */
473 date = g_date_new ();
474 g_date_set_time_t (date, timestamp);
476 /* If last message was from another day we append date and time */
477 if (g_date_compare (date, last_date) > 0) {
482 g_date_free (last_date);
485 /* If last message is 'old' append the time */
486 if (timestamp - priv->last_timestamp >= TIMESTAMP_INTERVAL) {
490 if (append_date || (!priv->only_if_date && append_time)) {
491 chat_text_view_append_timestamp (view, timestamp, append_date);
496 chat_text_view_size_allocate (GtkWidget *widget,
497 GtkAllocation *alloc)
501 down = chat_text_view_is_scrolled_down (EMPATHY_CHAT_TEXT_VIEW (widget));
503 GTK_WIDGET_CLASS (empathy_chat_text_view_parent_class)->size_allocate (widget, alloc);
508 adj = GTK_TEXT_VIEW (widget)->vadjustment;
509 gtk_adjustment_set_value (adj, adj->upper - adj->page_size);
514 chat_text_view_drag_motion (GtkWidget *widget,
515 GdkDragContext *context,
520 /* Don't handle drag motion, since we don't want the view to scroll as
521 * the result of dragging something across it. */
527 chat_text_view_get_property (GObject *object,
532 EmpathyChatTextViewPriv *priv = GET_PRIV (object);
535 case PROP_LAST_CONTACT:
536 g_value_set_object (value, priv->last_contact);
538 case PROP_ONLY_IF_DATE:
539 g_value_set_boolean (value, priv->only_if_date);
542 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
548 chat_text_view_set_property (GObject *object,
553 EmpathyChatTextViewPriv *priv = GET_PRIV (object);
556 case PROP_ONLY_IF_DATE:
557 priv->only_if_date = g_value_get_boolean (value);
560 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
566 chat_text_view_finalize (GObject *object)
568 EmpathyChatTextView *view;
569 EmpathyChatTextViewPriv *priv;
571 view = EMPATHY_CHAT_TEXT_VIEW (object);
572 priv = GET_PRIV (view);
574 DEBUG ("%p", object);
576 empathy_conf_notify_remove (empathy_conf_get (), priv->notify_system_fonts_id);
578 if (priv->last_contact) {
579 g_object_unref (priv->last_contact);
581 if (priv->scroll_time) {
582 g_timer_destroy (priv->scroll_time);
584 if (priv->scroll_timeout) {
585 g_source_remove (priv->scroll_timeout);
588 G_OBJECT_CLASS (empathy_chat_text_view_parent_class)->finalize (object);
592 empathy_chat_text_view_class_init (EmpathyChatTextViewClass *klass)
594 GObjectClass *object_class = G_OBJECT_CLASS (klass);
595 GtkWidgetClass *widget_class = GTK_WIDGET_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 g_object_class_install_property (object_class,
606 g_param_spec_object ("last-contact",
608 "The sender of the last received message",
609 EMPATHY_TYPE_CONTACT,
611 g_object_class_install_property (object_class,
613 g_param_spec_boolean ("only-if-date",
615 "Display timestamp only if the date changes",
620 g_type_class_add_private (object_class, sizeof (EmpathyChatTextViewPriv));
624 empathy_chat_text_view_init (EmpathyChatTextView *view)
626 EmpathyChatTextViewPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (view,
627 EMPATHY_TYPE_CHAT_TEXT_VIEW, EmpathyChatTextViewPriv);
630 priv->buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
631 priv->last_timestamp = 0;
632 priv->allow_scrolling = TRUE;
633 priv->smiley_manager = empathy_smiley_manager_dup_singleton ();
636 "wrap-mode", GTK_WRAP_WORD_CHAR,
638 "cursor-visible", FALSE,
641 priv->notify_system_fonts_id =
642 empathy_conf_notify_add (empathy_conf_get (),
643 "/desktop/gnome/interface/document_font_name",
644 chat_text_view_notify_system_font_cb,
646 chat_text_view_system_font_update (view);
647 chat_text_view_create_tags (view);
649 g_signal_connect (view,
651 G_CALLBACK (chat_text_view_populate_popup),
655 /* Code stolen from pidgin/gtkimhtml.c */
657 chat_text_view_scroll_cb (EmpathyChatTextView *view)
659 EmpathyChatTextViewPriv *priv;
663 priv = GET_PRIV (view);
664 adj = GTK_TEXT_VIEW (view)->vadjustment;
665 max_val = adj->upper - adj->page_size;
667 g_return_val_if_fail (priv->scroll_time != NULL, FALSE);
669 if (g_timer_elapsed (priv->scroll_time, NULL) > MAX_SCROLL_TIME) {
670 /* time's up. jump to the end and kill the timer */
671 gtk_adjustment_set_value (adj, max_val);
672 g_timer_destroy (priv->scroll_time);
673 priv->scroll_time = NULL;
674 priv->scroll_timeout = 0;
678 /* scroll by 1/3rd the remaining distance */
679 gtk_adjustment_set_value (adj, gtk_adjustment_get_value (adj) + ((max_val - gtk_adjustment_get_value (adj)) / 3));
684 chat_text_view_scroll_down (EmpathyChatView *view)
686 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
688 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
690 if (!priv->allow_scrolling) {
694 DEBUG ("Scrolling down");
696 if (priv->scroll_time) {
697 g_timer_reset (priv->scroll_time);
699 priv->scroll_time = g_timer_new();
701 if (!priv->scroll_timeout) {
702 priv->scroll_timeout = g_timeout_add (SCROLL_DELAY,
703 (GSourceFunc) chat_text_view_scroll_cb,
709 chat_text_view_append_message (EmpathyChatView *view,
712 EmpathyChatTextView *text_view = EMPATHY_CHAT_TEXT_VIEW (view);
713 EmpathyChatTextViewPriv *priv = GET_PRIV (text_view);
717 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
718 g_return_if_fail (EMPATHY_IS_MESSAGE (msg));
720 if (!empathy_message_get_body (msg)) {
724 bottom = chat_text_view_is_scrolled_down (text_view);
726 chat_text_view_maybe_trim_buffer (EMPATHY_CHAT_TEXT_VIEW (view));
728 timestamp = empathy_message_get_timestamp (msg);
729 chat_text_maybe_append_date_and_time (text_view, timestamp);
730 if (EMPATHY_CHAT_TEXT_VIEW_GET_CLASS (view)->append_message) {
731 EMPATHY_CHAT_TEXT_VIEW_GET_CLASS (view)->append_message (text_view,
736 chat_text_view_scroll_down (view);
739 if (priv->last_contact) {
740 g_object_unref (priv->last_contact);
742 priv->last_contact = g_object_ref (empathy_message_get_sender (msg));
743 g_object_notify (G_OBJECT (view), "last-contact");
747 chat_text_view_append_event (EmpathyChatView *view,
750 EmpathyChatTextView *text_view = EMPATHY_CHAT_TEXT_VIEW (view);
751 EmpathyChatTextViewPriv *priv = GET_PRIV (text_view);
757 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
758 g_return_if_fail (!EMP_STR_EMPTY (str));
760 bottom = chat_text_view_is_scrolled_down (text_view);
761 chat_text_view_maybe_trim_buffer (EMPATHY_CHAT_TEXT_VIEW (view));
762 chat_text_maybe_append_date_and_time (text_view,
763 empathy_time_get_current ());
765 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
766 msg = g_strdup_printf (" - %s\n", str);
767 gtk_text_buffer_insert_with_tags_by_name (priv->buffer, &iter,
769 EMPATHY_CHAT_TEXT_VIEW_TAG_EVENT,
774 chat_text_view_scroll_down (view);
777 if (priv->last_contact) {
778 g_object_unref (priv->last_contact);
779 priv->last_contact = NULL;
780 g_object_notify (G_OBJECT (view), "last-contact");
785 chat_text_view_scroll (EmpathyChatView *view,
786 gboolean allow_scrolling)
788 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
790 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
792 DEBUG ("Scrolling %s", allow_scrolling ? "enabled" : "disabled");
794 priv->allow_scrolling = allow_scrolling;
795 if (allow_scrolling) {
796 empathy_chat_view_scroll_down (view);
801 chat_text_view_get_has_selection (EmpathyChatView *view)
803 GtkTextBuffer *buffer;
805 g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), FALSE);
807 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
809 return gtk_text_buffer_get_has_selection (buffer);
813 chat_text_view_clear (EmpathyChatView *view)
815 GtkTextBuffer *buffer;
816 EmpathyChatTextViewPriv *priv;
818 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
820 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
821 gtk_text_buffer_set_text (buffer, "", -1);
823 /* We set these back to the initial values so we get
824 * timestamps when clearing the window to know when
825 * conversations start.
827 priv = GET_PRIV (view);
829 priv->last_timestamp = 0;
833 chat_text_view_find_previous (EmpathyChatView *view,
834 const gchar *search_criteria,
837 EmpathyChatTextViewPriv *priv;
838 GtkTextBuffer *buffer;
839 GtkTextIter iter_at_mark;
840 GtkTextIter iter_match_start;
841 GtkTextIter iter_match_end;
843 gboolean from_start = FALSE;
845 g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), FALSE);
846 g_return_val_if_fail (search_criteria != NULL, FALSE);
848 priv = GET_PRIV (view);
850 buffer = priv->buffer;
852 if (EMP_STR_EMPTY (search_criteria)) {
853 if (priv->find_mark_previous) {
854 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
856 gtk_text_buffer_move_mark (buffer,
857 priv->find_mark_previous,
859 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
860 priv->find_mark_previous,
865 gtk_text_buffer_select_range (buffer,
877 if (priv->find_mark_previous) {
878 gtk_text_buffer_get_iter_at_mark (buffer,
880 priv->find_mark_previous);
882 gtk_text_buffer_get_end_iter (buffer, &iter_at_mark);
886 priv->find_last_direction = FALSE;
888 found = empathy_text_iter_backward_search (&iter_at_mark,
895 gboolean result = FALSE;
901 /* Here we wrap around. */
902 if (!new_search && !priv->find_wrapped) {
903 priv->find_wrapped = TRUE;
904 result = chat_text_view_find_previous (view,
907 priv->find_wrapped = FALSE;
913 /* Set new mark and show on screen */
914 if (!priv->find_mark_previous) {
915 priv->find_mark_previous = gtk_text_buffer_create_mark (buffer, NULL,
919 gtk_text_buffer_move_mark (buffer,
920 priv->find_mark_previous,
924 if (!priv->find_mark_next) {
925 priv->find_mark_next = gtk_text_buffer_create_mark (buffer, NULL,
929 gtk_text_buffer_move_mark (buffer,
930 priv->find_mark_next,
934 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
935 priv->find_mark_previous,
941 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &iter_match_start);
942 gtk_text_buffer_move_mark_by_name (buffer, "insert", &iter_match_end);
948 chat_text_view_find_next (EmpathyChatView *view,
949 const gchar *search_criteria,
952 EmpathyChatTextViewPriv *priv;
953 GtkTextBuffer *buffer;
954 GtkTextIter iter_at_mark;
955 GtkTextIter iter_match_start;
956 GtkTextIter iter_match_end;
958 gboolean from_start = FALSE;
960 g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), FALSE);
961 g_return_val_if_fail (search_criteria != NULL, FALSE);
963 priv = GET_PRIV (view);
965 buffer = priv->buffer;
967 if (EMP_STR_EMPTY (search_criteria)) {
968 if (priv->find_mark_next) {
969 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
971 gtk_text_buffer_move_mark (buffer,
972 priv->find_mark_next,
974 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
975 priv->find_mark_next,
980 gtk_text_buffer_select_range (buffer,
992 if (priv->find_mark_next) {
993 gtk_text_buffer_get_iter_at_mark (buffer,
995 priv->find_mark_next);
997 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1001 priv->find_last_direction = TRUE;
1003 found = empathy_text_iter_forward_search (&iter_at_mark,
1010 gboolean result = FALSE;
1016 /* Here we wrap around. */
1017 if (!new_search && !priv->find_wrapped) {
1018 priv->find_wrapped = TRUE;
1019 result = chat_text_view_find_next (view,
1022 priv->find_wrapped = FALSE;
1028 /* Set new mark and show on screen */
1029 if (!priv->find_mark_next) {
1030 priv->find_mark_next = gtk_text_buffer_create_mark (buffer, NULL,
1034 gtk_text_buffer_move_mark (buffer,
1035 priv->find_mark_next,
1039 if (!priv->find_mark_previous) {
1040 priv->find_mark_previous = gtk_text_buffer_create_mark (buffer, NULL,
1044 gtk_text_buffer_move_mark (buffer,
1045 priv->find_mark_previous,
1049 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
1050 priv->find_mark_next,
1056 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &iter_match_start);
1057 gtk_text_buffer_move_mark_by_name (buffer, "insert", &iter_match_end);
1063 chat_text_view_find_abilities (EmpathyChatView *view,
1064 const gchar *search_criteria,
1065 gboolean *can_do_previous,
1066 gboolean *can_do_next)
1068 EmpathyChatTextViewPriv *priv;
1069 GtkTextBuffer *buffer;
1070 GtkTextIter iter_at_mark;
1071 GtkTextIter iter_match_start;
1072 GtkTextIter iter_match_end;
1074 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
1075 g_return_if_fail (search_criteria != NULL);
1076 g_return_if_fail (can_do_previous != NULL && can_do_next != NULL);
1078 priv = GET_PRIV (view);
1080 buffer = priv->buffer;
1082 if (can_do_previous) {
1083 if (priv->find_mark_previous) {
1084 gtk_text_buffer_get_iter_at_mark (buffer,
1086 priv->find_mark_previous);
1088 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1091 *can_do_previous = empathy_text_iter_backward_search (&iter_at_mark,
1099 if (priv->find_mark_next) {
1100 gtk_text_buffer_get_iter_at_mark (buffer,
1102 priv->find_mark_next);
1104 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1107 *can_do_next = empathy_text_iter_forward_search (&iter_at_mark,
1116 chat_text_view_highlight (EmpathyChatView *view,
1119 GtkTextBuffer *buffer;
1121 GtkTextIter iter_start;
1122 GtkTextIter iter_end;
1123 GtkTextIter iter_match_start;
1124 GtkTextIter iter_match_end;
1127 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
1129 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
1131 gtk_text_buffer_get_start_iter (buffer, &iter);
1133 gtk_text_buffer_get_bounds (buffer, &iter_start, &iter_end);
1134 gtk_text_buffer_remove_tag_by_name (buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_HIGHLIGHT,
1138 if (EMP_STR_EMPTY (text)) {
1143 found = empathy_text_iter_forward_search (&iter,
1153 gtk_text_buffer_apply_tag_by_name (buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_HIGHLIGHT,
1157 iter = iter_match_end;
1158 gtk_text_iter_forward_char (&iter);
1163 chat_text_view_copy_clipboard (EmpathyChatView *view)
1165 GtkTextBuffer *buffer;
1166 GtkClipboard *clipboard;
1168 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
1170 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
1171 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1173 gtk_text_buffer_copy_clipboard (buffer, clipboard);
1177 chat_text_view_iface_init (EmpathyChatViewIface *iface)
1179 iface->append_message = chat_text_view_append_message;
1180 iface->append_event = chat_text_view_append_event;
1181 iface->scroll = chat_text_view_scroll;
1182 iface->scroll_down = chat_text_view_scroll_down;
1183 iface->get_has_selection = chat_text_view_get_has_selection;
1184 iface->clear = chat_text_view_clear;
1185 iface->find_previous = chat_text_view_find_previous;
1186 iface->find_next = chat_text_view_find_next;
1187 iface->find_abilities = chat_text_view_find_abilities;
1188 iface->highlight = chat_text_view_highlight;
1189 iface->copy_clipboard = chat_text_view_copy_clipboard;
1193 empathy_chat_text_view_get_last_contact (EmpathyChatTextView *view)
1195 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1197 g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), NULL);
1199 return priv->last_contact;
1203 empathy_chat_text_view_set_only_if_date (EmpathyChatTextView *view,
1204 gboolean only_if_date)
1206 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1208 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
1210 if (only_if_date != priv->only_if_date) {
1211 priv->only_if_date = only_if_date;
1212 g_object_notify (G_OBJECT (view), "only-if-date");
1217 chat_text_view_insert_text_with_emoticons (EmpathyChatTextView *view,
1221 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1222 gboolean use_smileys = FALSE;
1223 GSList *smileys, *l;
1225 empathy_conf_get_bool (empathy_conf_get (),
1226 EMPATHY_PREFS_CHAT_SHOW_SMILEYS,
1230 gtk_text_buffer_insert (priv->buffer, iter, str, -1);
1234 smileys = empathy_smiley_manager_parse (priv->smiley_manager, str);
1235 for (l = smileys; l; l = l->next) {
1236 EmpathySmiley *smiley;
1239 if (smiley->pixbuf) {
1240 gtk_text_buffer_insert_pixbuf (priv->buffer, iter, smiley->pixbuf);
1242 gtk_text_buffer_insert (priv->buffer, iter, smiley->str, -1);
1244 empathy_smiley_free (smiley);
1246 g_slist_free (smileys);
1250 empathy_chat_text_view_append_body (EmpathyChatTextView *view,
1254 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1255 GtkTextIter start_iter, end_iter;
1259 GMatchInfo *match_info;
1265 priv = GET_PRIV (view);
1267 gtk_text_buffer_get_end_iter (priv->buffer, &start_iter);
1268 mark = gtk_text_buffer_create_mark (priv->buffer, NULL, &start_iter, TRUE);
1270 uri_regex = empathy_uri_regex_dup_singleton ();
1271 for (match = g_regex_match (uri_regex, body, 0, &match_info); match;
1272 match = g_match_info_next (match_info, NULL)) {
1273 if (!g_match_info_fetch_pos (match_info, 0, &s, &e))
1277 tmp = empathy_substring (body, last, s);
1279 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1280 chat_text_view_insert_text_with_emoticons (view,
1286 tmp = empathy_substring (body, s, e);
1288 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1289 gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
1293 EMPATHY_CHAT_TEXT_VIEW_TAG_LINK,
1299 g_match_info_free (match_info);
1300 g_regex_unref (uri_regex);
1302 if (last < strlen (body)) {
1303 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1304 chat_text_view_insert_text_with_emoticons (view,
1309 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1310 gtk_text_buffer_insert (priv->buffer, &iter, "\n", 1);
1312 /* Apply the style to the inserted text. */
1313 gtk_text_buffer_get_iter_at_mark (priv->buffer, &start_iter, mark);
1314 gtk_text_buffer_get_end_iter (priv->buffer, &end_iter);
1316 gtk_text_buffer_apply_tag_by_name (priv->buffer,
1321 gtk_text_buffer_delete_mark (priv->buffer, mark);
1325 empathy_chat_text_view_append_spacing (EmpathyChatTextView *view)
1327 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1330 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1331 gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
1335 EMPATHY_CHAT_TEXT_VIEW_TAG_CUT,
1336 EMPATHY_CHAT_TEXT_VIEW_TAG_SPACING,
1341 empathy_chat_text_view_tag_set (EmpathyChatTextView *view,
1342 const gchar *tag_name,
1343 const gchar *first_property_name,
1346 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1348 GtkTextTagTable *table;
1351 g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), NULL);
1352 g_return_val_if_fail (tag_name != NULL, NULL);
1354 table = gtk_text_buffer_get_tag_table (priv->buffer);
1355 tag = gtk_text_tag_table_lookup (table, tag_name);
1357 if (tag && first_property_name) {
1358 va_start (list, first_property_name);
1359 g_object_set_valist (G_OBJECT (tag), first_property_name, list);