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 G_DEFINE_TYPE_WITH_CODE (EmpathyChatTextView, empathy_chat_text_view,
78 G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CHAT_VIEW,
79 chat_text_view_iface_init));
88 chat_text_view_url_event_cb (GtkTextTag *tag,
92 EmpathyChatTextView *view)
94 EmpathyChatTextViewPriv *priv;
95 GtkTextIter start, end;
98 priv = GET_PRIV (view);
100 /* If the link is being selected, don't do anything. */
101 gtk_text_buffer_get_selection_bounds (priv->buffer, &start, &end);
102 if (gtk_text_iter_get_offset (&start) != gtk_text_iter_get_offset (&end)) {
106 if (event->type == GDK_BUTTON_RELEASE && event->button.button == 1) {
109 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
110 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
111 str = gtk_text_buffer_get_text (priv->buffer,
116 empathy_url_show (GTK_WIDGET (view), str);
125 chat_text_view_event_cb (EmpathyChatTextView *view,
126 GdkEventMotion *event,
129 static GdkCursor *hand = NULL;
130 static GdkCursor *beam = NULL;
131 GtkTextWindowType type;
134 gint x, y, buf_x, buf_y;
136 type = gtk_text_view_get_window_type (GTK_TEXT_VIEW (view),
139 if (type != GTK_TEXT_WINDOW_TEXT) {
143 /* Get where the pointer really is. */
144 win = gtk_text_view_get_window (GTK_TEXT_VIEW (view), type);
149 gdk_window_get_pointer (win, &x, &y, NULL);
151 /* Get the iter where the cursor is at */
152 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view), type,
156 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view),
160 if (gtk_text_iter_has_tag (&iter, tag)) {
162 hand = gdk_cursor_new (GDK_HAND2);
163 beam = gdk_cursor_new (GDK_XTERM);
165 gdk_window_set_cursor (win, hand);
168 beam = gdk_cursor_new (GDK_XTERM);
170 gdk_window_set_cursor (win, beam);
177 chat_text_view_create_tags (EmpathyChatTextView *view)
179 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
182 gtk_text_buffer_create_tag (priv->buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_CUT, NULL);
183 gtk_text_buffer_create_tag (priv->buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_HIGHLIGHT, NULL);
184 gtk_text_buffer_create_tag (priv->buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_SPACING, NULL);
185 gtk_text_buffer_create_tag (priv->buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_TIME, NULL);
186 gtk_text_buffer_create_tag (priv->buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_ACTION, NULL);
187 gtk_text_buffer_create_tag (priv->buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_BODY, NULL);
188 gtk_text_buffer_create_tag (priv->buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_EVENT, NULL);
190 tag = gtk_text_buffer_create_tag (priv->buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_LINK, NULL);
191 g_signal_connect (tag, "event",
192 G_CALLBACK (chat_text_view_url_event_cb),
195 g_signal_connect (view, "motion-notify-event",
196 G_CALLBACK (chat_text_view_event_cb),
201 chat_text_view_system_font_update (EmpathyChatTextView *view)
203 PangoFontDescription *font_description = NULL;
206 if (empathy_conf_get_string (empathy_conf_get (),
207 "/desktop/gnome/interface/document_font_name",
208 &font_name) && font_name) {
209 font_description = pango_font_description_from_string (font_name);
212 font_description = NULL;
215 gtk_widget_modify_font (GTK_WIDGET (view), font_description);
217 if (font_description) {
218 pango_font_description_free (font_description);
223 chat_text_view_notify_system_font_cb (EmpathyConf *conf,
227 EmpathyChatTextView *view = user_data;
229 chat_text_view_system_font_update (view);
233 chat_text_view_open_address_cb (GtkMenuItem *menuitem, const gchar *url)
235 empathy_url_show (GTK_WIDGET (menuitem), url);
239 chat_text_view_copy_address_cb (GtkMenuItem *menuitem, const gchar *url)
241 GtkClipboard *clipboard;
243 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
244 gtk_clipboard_set_text (clipboard, url, -1);
246 clipboard = gtk_clipboard_get (GDK_SELECTION_PRIMARY);
247 gtk_clipboard_set_text (clipboard, url, -1);
251 chat_text_view_populate_popup (EmpathyChatTextView *view,
255 EmpathyChatTextViewPriv *priv;
256 GtkTextTagTable *table;
259 GtkTextIter iter, start, end;
263 priv = GET_PRIV (view);
265 /* Clear menu item */
266 if (gtk_text_buffer_get_char_count (priv->buffer) > 0) {
267 item = gtk_separator_menu_item_new ();
268 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
269 gtk_widget_show (item);
271 item = gtk_image_menu_item_new_from_stock (GTK_STOCK_CLEAR, NULL);
272 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
273 gtk_widget_show (item);
275 g_signal_connect_swapped (item, "activate",
276 G_CALLBACK (empathy_chat_view_clear),
280 /* Link context menu items */
281 table = gtk_text_buffer_get_tag_table (priv->buffer);
282 tag = gtk_text_tag_table_lookup (table, EMPATHY_CHAT_TEXT_VIEW_TAG_LINK);
284 gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
286 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
287 GTK_TEXT_WINDOW_WIDGET,
291 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &iter, x, y);
295 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
296 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
297 str = gtk_text_buffer_get_text (priv->buffer,
298 &start, &end, FALSE);
301 if (EMP_STR_EMPTY (str)) {
306 /* NOTE: Set data just to get the string freed when not needed. */
307 g_object_set_data_full (G_OBJECT (menu),
309 (GDestroyNotify) g_free);
311 item = gtk_separator_menu_item_new ();
312 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
313 gtk_widget_show (item);
315 item = gtk_menu_item_new_with_mnemonic (_("_Copy Link Address"));
316 g_signal_connect (item, "activate",
317 G_CALLBACK (chat_text_view_copy_address_cb),
319 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
320 gtk_widget_show (item);
322 item = gtk_menu_item_new_with_mnemonic (_("_Open Link"));
323 g_signal_connect (item, "activate",
324 G_CALLBACK (chat_text_view_open_address_cb),
326 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
327 gtk_widget_show (item);
331 chat_text_view_is_scrolled_down (EmpathyChatTextView *view)
335 sw = gtk_widget_get_parent (GTK_WIDGET (view));
336 if (GTK_IS_SCROLLED_WINDOW (sw)) {
342 vadj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (sw));
343 value = gtk_adjustment_get_value (vadj);
344 upper = gtk_adjustment_get_upper (vadj);
345 page_size = gtk_adjustment_get_page_size (vadj);
347 if (value < upper - page_size) {
356 chat_text_view_maybe_trim_buffer (EmpathyChatTextView *view)
358 EmpathyChatTextViewPriv *priv;
359 GtkTextIter top, bottom;
362 GtkTextTagTable *table;
365 priv = GET_PRIV (view);
367 gtk_text_buffer_get_end_iter (priv->buffer, &bottom);
368 line = gtk_text_iter_get_line (&bottom);
369 if (line < MAX_LINES) {
373 remove_ = line - MAX_LINES;
374 gtk_text_buffer_get_start_iter (priv->buffer, &top);
377 if (!gtk_text_iter_forward_lines (&bottom, remove_)) {
381 /* Track backwords to a place where we can safely cut, we don't do it in
382 * the middle of a tag.
384 table = gtk_text_buffer_get_tag_table (priv->buffer);
385 tag = gtk_text_tag_table_lookup (table, EMPATHY_CHAT_TEXT_VIEW_TAG_CUT);
390 if (!gtk_text_iter_forward_to_tag_toggle (&bottom, tag)) {
394 if (!gtk_text_iter_equal (&top, &bottom)) {
395 gtk_text_buffer_delete (priv->buffer, &top, &bottom);
400 chat_text_view_append_timestamp (EmpathyChatTextView *view,
404 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
409 str = g_string_new ("- ");
411 /* Append date if needed */
416 date = g_date_new ();
417 g_date_set_time_t (date, timestamp);
418 /* Translators: timestamp displayed between conversations in
419 * chat windows (strftime format string) */
420 g_date_strftime (buf, 256, _("%A %B %d %Y"), date);
421 g_string_append (str, buf);
422 g_string_append (str, ", ");
427 tmp = empathy_time_to_string_local (timestamp, EMPATHY_TIME_FORMAT_DISPLAY_SHORT);
428 g_string_append (str, tmp);
431 g_string_append (str, " -\n");
433 /* Insert the string in the buffer */
434 empathy_chat_text_view_append_spacing (view);
435 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
436 gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
439 EMPATHY_CHAT_TEXT_VIEW_TAG_TIME,
442 priv->last_timestamp = timestamp;
444 g_string_free (str, TRUE);
448 chat_text_maybe_append_date_and_time (EmpathyChatTextView *view,
451 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
452 GDate *date, *last_date;
453 gboolean append_date = FALSE;
454 gboolean append_time = FALSE;
456 /* Get the date from last message */
457 last_date = g_date_new ();
458 g_date_set_time_t (last_date, priv->last_timestamp);
460 /* Get the date of the message we are appending */
461 date = g_date_new ();
462 g_date_set_time_t (date, timestamp);
464 /* If last message was from another day we append date and time */
465 if (g_date_compare (date, last_date) > 0) {
470 g_date_free (last_date);
473 /* If last message is 'old' append the time */
474 if (timestamp - priv->last_timestamp >= TIMESTAMP_INTERVAL) {
478 if (append_date || (!priv->only_if_date && append_time)) {
479 chat_text_view_append_timestamp (view, timestamp, append_date);
484 chat_text_view_size_allocate (GtkWidget *widget,
485 GtkAllocation *alloc)
489 down = chat_text_view_is_scrolled_down (EMPATHY_CHAT_TEXT_VIEW (widget));
491 GTK_WIDGET_CLASS (empathy_chat_text_view_parent_class)->size_allocate (widget, alloc);
496 adj = GTK_TEXT_VIEW (widget)->vadjustment;
497 gtk_adjustment_set_value (adj,
498 gtk_adjustment_get_upper (adj) -
499 gtk_adjustment_get_page_size (adj));
504 chat_text_view_drag_motion (GtkWidget *widget,
505 GdkDragContext *context,
510 /* Don't handle drag motion, since we don't want the view to scroll as
511 * the result of dragging something across it. */
517 chat_text_view_get_property (GObject *object,
522 EmpathyChatTextViewPriv *priv = GET_PRIV (object);
525 case PROP_LAST_CONTACT:
526 g_value_set_object (value, priv->last_contact);
528 case PROP_ONLY_IF_DATE:
529 g_value_set_boolean (value, priv->only_if_date);
532 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
538 chat_text_view_set_property (GObject *object,
543 EmpathyChatTextViewPriv *priv = GET_PRIV (object);
546 case PROP_ONLY_IF_DATE:
547 priv->only_if_date = g_value_get_boolean (value);
550 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
556 chat_text_view_finalize (GObject *object)
558 EmpathyChatTextView *view;
559 EmpathyChatTextViewPriv *priv;
561 view = EMPATHY_CHAT_TEXT_VIEW (object);
562 priv = GET_PRIV (view);
564 DEBUG ("%p", object);
566 empathy_conf_notify_remove (empathy_conf_get (), priv->notify_system_fonts_id);
568 if (priv->last_contact) {
569 g_object_unref (priv->last_contact);
571 if (priv->scroll_time) {
572 g_timer_destroy (priv->scroll_time);
574 if (priv->scroll_timeout) {
575 g_source_remove (priv->scroll_timeout);
577 g_object_unref (priv->smiley_manager);
579 G_OBJECT_CLASS (empathy_chat_text_view_parent_class)->finalize (object);
583 empathy_chat_text_view_class_init (EmpathyChatTextViewClass *klass)
585 GObjectClass *object_class = G_OBJECT_CLASS (klass);
586 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
588 object_class->finalize = chat_text_view_finalize;
589 object_class->get_property = chat_text_view_get_property;
590 object_class->set_property = chat_text_view_set_property;
592 widget_class->size_allocate = chat_text_view_size_allocate;
593 widget_class->drag_motion = chat_text_view_drag_motion;
595 g_object_class_install_property (object_class,
597 g_param_spec_object ("last-contact",
599 "The sender of the last received message",
600 EMPATHY_TYPE_CONTACT,
602 g_object_class_install_property (object_class,
604 g_param_spec_boolean ("only-if-date",
606 "Display timestamp only if the date changes",
611 g_type_class_add_private (object_class, sizeof (EmpathyChatTextViewPriv));
615 empathy_chat_text_view_init (EmpathyChatTextView *view)
617 EmpathyChatTextViewPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (view,
618 EMPATHY_TYPE_CHAT_TEXT_VIEW, EmpathyChatTextViewPriv);
621 priv->buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
622 priv->last_timestamp = 0;
623 priv->allow_scrolling = TRUE;
624 priv->smiley_manager = empathy_smiley_manager_dup_singleton ();
627 "wrap-mode", GTK_WRAP_WORD_CHAR,
629 "cursor-visible", FALSE,
632 priv->notify_system_fonts_id =
633 empathy_conf_notify_add (empathy_conf_get (),
634 "/desktop/gnome/interface/document_font_name",
635 chat_text_view_notify_system_font_cb,
637 chat_text_view_system_font_update (view);
638 chat_text_view_create_tags (view);
640 g_signal_connect (view,
642 G_CALLBACK (chat_text_view_populate_popup),
646 /* Code stolen from pidgin/gtkimhtml.c */
648 chat_text_view_scroll_cb (EmpathyChatTextView *view)
650 EmpathyChatTextViewPriv *priv;
654 priv = GET_PRIV (view);
655 adj = GTK_TEXT_VIEW (view)->vadjustment;
656 max_val = gtk_adjustment_get_upper (adj) - gtk_adjustment_get_page_size (adj);
658 g_return_val_if_fail (priv->scroll_time != NULL, FALSE);
660 if (g_timer_elapsed (priv->scroll_time, NULL) > MAX_SCROLL_TIME) {
661 /* time's up. jump to the end and kill the timer */
662 gtk_adjustment_set_value (adj, max_val);
663 g_timer_destroy (priv->scroll_time);
664 priv->scroll_time = NULL;
665 priv->scroll_timeout = 0;
669 /* scroll by 1/3rd the remaining distance */
670 gtk_adjustment_set_value (adj, gtk_adjustment_get_value (adj) + ((max_val - gtk_adjustment_get_value (adj)) / 3));
675 chat_text_view_scroll_down (EmpathyChatView *view)
677 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
679 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
681 if (!priv->allow_scrolling) {
685 DEBUG ("Scrolling down");
687 if (priv->scroll_time) {
688 g_timer_reset (priv->scroll_time);
690 priv->scroll_time = g_timer_new ();
692 if (!priv->scroll_timeout) {
693 priv->scroll_timeout = g_timeout_add (SCROLL_DELAY,
694 (GSourceFunc) chat_text_view_scroll_cb,
700 chat_text_view_append_message (EmpathyChatView *view,
703 EmpathyChatTextView *text_view = EMPATHY_CHAT_TEXT_VIEW (view);
704 EmpathyChatTextViewPriv *priv = GET_PRIV (text_view);
708 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
709 g_return_if_fail (EMPATHY_IS_MESSAGE (msg));
711 if (!empathy_message_get_body (msg)) {
715 bottom = chat_text_view_is_scrolled_down (text_view);
717 chat_text_view_maybe_trim_buffer (EMPATHY_CHAT_TEXT_VIEW (view));
719 timestamp = empathy_message_get_timestamp (msg);
720 chat_text_maybe_append_date_and_time (text_view, timestamp);
721 if (EMPATHY_CHAT_TEXT_VIEW_GET_CLASS (view)->append_message) {
722 EMPATHY_CHAT_TEXT_VIEW_GET_CLASS (view)->append_message (text_view,
727 chat_text_view_scroll_down (view);
730 if (priv->last_contact) {
731 g_object_unref (priv->last_contact);
733 priv->last_contact = g_object_ref (empathy_message_get_sender (msg));
734 g_object_notify (G_OBJECT (view), "last-contact");
738 chat_text_view_append_event (EmpathyChatView *view,
741 EmpathyChatTextView *text_view = EMPATHY_CHAT_TEXT_VIEW (view);
742 EmpathyChatTextViewPriv *priv = GET_PRIV (text_view);
748 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
749 g_return_if_fail (!EMP_STR_EMPTY (str));
751 bottom = chat_text_view_is_scrolled_down (text_view);
752 chat_text_view_maybe_trim_buffer (EMPATHY_CHAT_TEXT_VIEW (view));
753 chat_text_maybe_append_date_and_time (text_view,
754 empathy_time_get_current ());
756 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
757 msg = g_strdup_printf (" - %s\n", str);
758 gtk_text_buffer_insert_with_tags_by_name (priv->buffer, &iter,
760 EMPATHY_CHAT_TEXT_VIEW_TAG_EVENT,
765 chat_text_view_scroll_down (view);
768 if (priv->last_contact) {
769 g_object_unref (priv->last_contact);
770 priv->last_contact = NULL;
771 g_object_notify (G_OBJECT (view), "last-contact");
776 chat_text_view_scroll (EmpathyChatView *view,
777 gboolean allow_scrolling)
779 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
781 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
783 DEBUG ("Scrolling %s", allow_scrolling ? "enabled" : "disabled");
785 priv->allow_scrolling = allow_scrolling;
786 if (allow_scrolling) {
787 empathy_chat_view_scroll_down (view);
792 chat_text_view_get_has_selection (EmpathyChatView *view)
794 GtkTextBuffer *buffer;
796 g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), FALSE);
798 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
800 return gtk_text_buffer_get_has_selection (buffer);
804 chat_text_view_clear (EmpathyChatView *view)
806 GtkTextBuffer *buffer;
807 EmpathyChatTextViewPriv *priv;
809 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
811 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
812 gtk_text_buffer_set_text (buffer, "", -1);
814 /* We set these back to the initial values so we get
815 * timestamps when clearing the window to know when
816 * conversations start.
818 priv = GET_PRIV (view);
820 priv->last_timestamp = 0;
821 if (priv->last_contact) {
822 g_object_unref (priv->last_contact);
823 priv->last_contact = NULL;
828 chat_text_view_find_previous (EmpathyChatView *view,
829 const gchar *search_criteria,
832 EmpathyChatTextViewPriv *priv;
833 GtkTextBuffer *buffer;
834 GtkTextIter iter_at_mark;
835 GtkTextIter iter_match_start;
836 GtkTextIter iter_match_end;
838 gboolean from_start = FALSE;
840 g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), FALSE);
841 g_return_val_if_fail (search_criteria != NULL, FALSE);
843 priv = GET_PRIV (view);
845 buffer = priv->buffer;
847 if (EMP_STR_EMPTY (search_criteria)) {
848 if (priv->find_mark_previous) {
849 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
851 gtk_text_buffer_move_mark (buffer,
852 priv->find_mark_previous,
854 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
855 priv->find_mark_previous,
860 gtk_text_buffer_select_range (buffer,
872 if (priv->find_mark_previous) {
873 gtk_text_buffer_get_iter_at_mark (buffer,
875 priv->find_mark_previous);
877 gtk_text_buffer_get_end_iter (buffer, &iter_at_mark);
881 priv->find_last_direction = FALSE;
883 found = empathy_text_iter_backward_search (&iter_at_mark,
890 gboolean result = FALSE;
896 /* Here we wrap around. */
897 if (!new_search && !priv->find_wrapped) {
898 priv->find_wrapped = TRUE;
899 result = chat_text_view_find_previous (view,
902 priv->find_wrapped = FALSE;
908 /* Set new mark and show on screen */
909 if (!priv->find_mark_previous) {
910 priv->find_mark_previous = gtk_text_buffer_create_mark (buffer, NULL,
914 gtk_text_buffer_move_mark (buffer,
915 priv->find_mark_previous,
919 if (!priv->find_mark_next) {
920 priv->find_mark_next = gtk_text_buffer_create_mark (buffer, NULL,
924 gtk_text_buffer_move_mark (buffer,
925 priv->find_mark_next,
929 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
930 priv->find_mark_previous,
936 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &iter_match_start);
937 gtk_text_buffer_move_mark_by_name (buffer, "insert", &iter_match_end);
943 chat_text_view_find_next (EmpathyChatView *view,
944 const gchar *search_criteria,
947 EmpathyChatTextViewPriv *priv;
948 GtkTextBuffer *buffer;
949 GtkTextIter iter_at_mark;
950 GtkTextIter iter_match_start;
951 GtkTextIter iter_match_end;
953 gboolean from_start = FALSE;
955 g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), FALSE);
956 g_return_val_if_fail (search_criteria != NULL, FALSE);
958 priv = GET_PRIV (view);
960 buffer = priv->buffer;
962 if (EMP_STR_EMPTY (search_criteria)) {
963 if (priv->find_mark_next) {
964 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
966 gtk_text_buffer_move_mark (buffer,
967 priv->find_mark_next,
969 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
970 priv->find_mark_next,
975 gtk_text_buffer_select_range (buffer,
987 if (priv->find_mark_next) {
988 gtk_text_buffer_get_iter_at_mark (buffer,
990 priv->find_mark_next);
992 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
996 priv->find_last_direction = TRUE;
998 found = empathy_text_iter_forward_search (&iter_at_mark,
1005 gboolean result = FALSE;
1011 /* Here we wrap around. */
1012 if (!new_search && !priv->find_wrapped) {
1013 priv->find_wrapped = TRUE;
1014 result = chat_text_view_find_next (view,
1017 priv->find_wrapped = FALSE;
1023 /* Set new mark and show on screen */
1024 if (!priv->find_mark_next) {
1025 priv->find_mark_next = gtk_text_buffer_create_mark (buffer, NULL,
1029 gtk_text_buffer_move_mark (buffer,
1030 priv->find_mark_next,
1034 if (!priv->find_mark_previous) {
1035 priv->find_mark_previous = gtk_text_buffer_create_mark (buffer, NULL,
1039 gtk_text_buffer_move_mark (buffer,
1040 priv->find_mark_previous,
1044 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
1045 priv->find_mark_next,
1051 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &iter_match_start);
1052 gtk_text_buffer_move_mark_by_name (buffer, "insert", &iter_match_end);
1058 chat_text_view_find_abilities (EmpathyChatView *view,
1059 const gchar *search_criteria,
1060 gboolean *can_do_previous,
1061 gboolean *can_do_next)
1063 EmpathyChatTextViewPriv *priv;
1064 GtkTextBuffer *buffer;
1065 GtkTextIter iter_at_mark;
1066 GtkTextIter iter_match_start;
1067 GtkTextIter iter_match_end;
1069 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
1070 g_return_if_fail (search_criteria != NULL);
1071 g_return_if_fail (can_do_previous != NULL && can_do_next != NULL);
1073 priv = GET_PRIV (view);
1075 buffer = priv->buffer;
1077 if (can_do_previous) {
1078 if (priv->find_mark_previous) {
1079 gtk_text_buffer_get_iter_at_mark (buffer,
1081 priv->find_mark_previous);
1083 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1086 *can_do_previous = empathy_text_iter_backward_search (&iter_at_mark,
1094 if (priv->find_mark_next) {
1095 gtk_text_buffer_get_iter_at_mark (buffer,
1097 priv->find_mark_next);
1099 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1102 *can_do_next = empathy_text_iter_forward_search (&iter_at_mark,
1111 chat_text_view_highlight (EmpathyChatView *view,
1114 GtkTextBuffer *buffer;
1116 GtkTextIter iter_start;
1117 GtkTextIter iter_end;
1118 GtkTextIter iter_match_start;
1119 GtkTextIter iter_match_end;
1122 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
1124 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
1126 gtk_text_buffer_get_start_iter (buffer, &iter);
1128 gtk_text_buffer_get_bounds (buffer, &iter_start, &iter_end);
1129 gtk_text_buffer_remove_tag_by_name (buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_HIGHLIGHT,
1133 if (EMP_STR_EMPTY (text)) {
1138 found = empathy_text_iter_forward_search (&iter,
1148 gtk_text_buffer_apply_tag_by_name (buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_HIGHLIGHT,
1152 iter = iter_match_end;
1153 gtk_text_iter_forward_char (&iter);
1158 chat_text_view_copy_clipboard (EmpathyChatView *view)
1160 GtkTextBuffer *buffer;
1161 GtkTextIter start, iter, end;
1162 GtkClipboard *clipboard;
1165 GtkTextChildAnchor *anchor = NULL;
1168 gboolean ignore_newlines = FALSE;
1170 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
1172 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
1173 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1175 if (!gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
1178 str = g_string_new ("");
1180 for (iter = start; !gtk_text_iter_equal (&iter, &end); gtk_text_iter_forward_char (&iter)) {
1181 c = gtk_text_iter_get_char (&iter);
1182 /* 0xFFFC is the 'object replacement' unicode character,
1183 * it indicates the presence of a pixbuf or a widget. */
1185 ignore_newlines = FALSE;
1186 if ((pixbuf = gtk_text_iter_get_pixbuf (&iter))) {
1188 text = g_object_get_data (G_OBJECT(pixbuf),
1191 str = g_string_append (str, text);
1192 } else if ((anchor = gtk_text_iter_get_child_anchor (&iter))) {
1194 list = gtk_text_child_anchor_get_widgets (anchor);
1196 text = g_object_get_data (G_OBJECT(list->data),
1199 str = g_string_append (str, text);
1203 } else if (c == '\n') {
1204 if (!ignore_newlines) {
1205 ignore_newlines = TRUE;
1206 str = g_string_append_unichar (str, c);
1209 ignore_newlines = FALSE;
1210 str = g_string_append_unichar (str, c);
1214 gtk_clipboard_set_text (clipboard, str->str, str->len);
1215 g_string_free (str, TRUE);
1219 chat_text_view_iface_init (EmpathyChatViewIface *iface)
1221 iface->append_message = chat_text_view_append_message;
1222 iface->append_event = chat_text_view_append_event;
1223 iface->scroll = chat_text_view_scroll;
1224 iface->scroll_down = chat_text_view_scroll_down;
1225 iface->get_has_selection = chat_text_view_get_has_selection;
1226 iface->clear = chat_text_view_clear;
1227 iface->find_previous = chat_text_view_find_previous;
1228 iface->find_next = chat_text_view_find_next;
1229 iface->find_abilities = chat_text_view_find_abilities;
1230 iface->highlight = chat_text_view_highlight;
1231 iface->copy_clipboard = chat_text_view_copy_clipboard;
1235 empathy_chat_text_view_get_last_contact (EmpathyChatTextView *view)
1237 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1239 g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), NULL);
1241 return priv->last_contact;
1245 empathy_chat_text_view_set_only_if_date (EmpathyChatTextView *view,
1246 gboolean only_if_date)
1248 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1250 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
1252 if (only_if_date != priv->only_if_date) {
1253 priv->only_if_date = only_if_date;
1254 g_object_notify (G_OBJECT (view), "only-if-date");
1259 chat_text_view_insert_text_with_emoticons (EmpathyChatTextView *view,
1263 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1264 gboolean use_smileys = FALSE;
1268 empathy_conf_get_bool (empathy_conf_get (),
1269 EMPATHY_PREFS_CHAT_SHOW_SMILEYS,
1273 gtk_text_buffer_insert (priv->buffer, iter, str, -1);
1277 hits = empathy_smiley_manager_parse_len (priv->smiley_manager, str, -1);
1278 for (l = hits; l; l = l->next) {
1279 EmpathySmileyHit *hit = l->data;
1281 if (hit->start > last) {
1282 /* Append the text between last smiley (or the
1283 * start of the message) and this smiley */
1284 gtk_text_buffer_insert (priv->buffer, iter, str + last,
1287 gtk_text_buffer_insert_pixbuf (priv->buffer, iter, hit->pixbuf);
1291 empathy_smiley_hit_free (hit);
1293 g_slist_free (hits);
1295 gtk_text_buffer_insert (priv->buffer, iter, str + last, -1);
1299 empathy_chat_text_view_append_body (EmpathyChatTextView *view,
1303 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1304 GtkTextIter start_iter, end_iter;
1308 GMatchInfo *match_info;
1314 priv = GET_PRIV (view);
1316 gtk_text_buffer_get_end_iter (priv->buffer, &start_iter);
1317 mark = gtk_text_buffer_create_mark (priv->buffer, NULL, &start_iter, TRUE);
1319 uri_regex = empathy_uri_regex_dup_singleton ();
1320 for (match = g_regex_match (uri_regex, body, 0, &match_info); match;
1321 match = g_match_info_next (match_info, NULL)) {
1322 if (!g_match_info_fetch_pos (match_info, 0, &s, &e))
1326 tmp = empathy_substring (body, last, s);
1328 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1329 chat_text_view_insert_text_with_emoticons (view,
1335 tmp = empathy_substring (body, s, e);
1337 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1338 gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
1342 EMPATHY_CHAT_TEXT_VIEW_TAG_LINK,
1348 g_match_info_free (match_info);
1349 g_regex_unref (uri_regex);
1351 if (last < (gint) strlen (body)) {
1352 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1353 chat_text_view_insert_text_with_emoticons (view,
1358 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1359 gtk_text_buffer_insert (priv->buffer, &iter, "\n", 1);
1361 /* Apply the style to the inserted text. */
1362 gtk_text_buffer_get_iter_at_mark (priv->buffer, &start_iter, mark);
1363 gtk_text_buffer_get_end_iter (priv->buffer, &end_iter);
1365 gtk_text_buffer_apply_tag_by_name (priv->buffer,
1370 gtk_text_buffer_delete_mark (priv->buffer, mark);
1374 empathy_chat_text_view_append_spacing (EmpathyChatTextView *view)
1376 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1379 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1380 gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
1384 EMPATHY_CHAT_TEXT_VIEW_TAG_CUT,
1385 EMPATHY_CHAT_TEXT_VIEW_TAG_SPACING,
1390 empathy_chat_text_view_tag_set (EmpathyChatTextView *view,
1391 const gchar *tag_name,
1392 const gchar *first_property_name,
1395 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1397 GtkTextTagTable *table;
1400 g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), NULL);
1401 g_return_val_if_fail (tag_name != NULL, NULL);
1403 table = gtk_text_buffer_get_tag_table (priv->buffer);
1404 tag = gtk_text_tag_table_lookup (table, tag_name);
1406 if (tag && first_property_name) {
1407 va_start (list, first_property_name);
1408 g_object_set_valist (G_OBJECT (tag), first_property_name, list);