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-gsettings.h>
39 #include <libempathy/empathy-utils.h>
41 #include "empathy-chat-text-view.h"
42 #include "empathy-chat.h"
43 #include "empathy-ui-utils.h"
44 #include "empathy-smiley-manager.h"
45 #include "empathy-string-parser.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 (5 * G_TIME_SPAN_MINUTE)
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 gint64 last_timestamp;
69 gboolean allow_scrolling;
70 guint notify_system_fonts_id;
71 GSettings *gsettings_desktop;
72 GSettings *gsettings_chat;
73 EmpathySmileyManager *smiley_manager;
74 gboolean only_if_date;
75 } EmpathyChatTextViewPriv;
77 static void chat_text_view_iface_init (EmpathyChatViewIface *iface);
79 static void chat_text_view_copy_clipboard (EmpathyChatView *view);
81 G_DEFINE_TYPE_WITH_CODE (EmpathyChatTextView, empathy_chat_text_view,
83 G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CHAT_VIEW,
84 chat_text_view_iface_init));
93 chat_text_view_url_event_cb (GtkTextTag *tag,
97 EmpathyChatTextView *view)
99 EmpathyChatTextViewPriv *priv;
100 GtkTextIter start, end;
103 priv = GET_PRIV (view);
105 /* If the link is being selected, don't do anything. */
106 gtk_text_buffer_get_selection_bounds (priv->buffer, &start, &end);
107 if (gtk_text_iter_get_offset (&start) != gtk_text_iter_get_offset (&end)) {
111 if (event->type == GDK_BUTTON_RELEASE && event->button.button == 1) {
114 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
115 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
116 str = gtk_text_buffer_get_text (priv->buffer,
121 empathy_url_show (GTK_WIDGET (view), str);
130 chat_text_view_event_cb (EmpathyChatTextView *view,
131 GdkEventMotion *event,
134 static GdkCursor *hand = NULL;
135 static GdkCursor *beam = NULL;
136 GtkTextWindowType type;
139 gint x, y, buf_x, buf_y;
141 type = gtk_text_view_get_window_type (GTK_TEXT_VIEW (view),
144 if (type != GTK_TEXT_WINDOW_TEXT) {
148 /* Get where the pointer really is. */
149 win = gtk_text_view_get_window (GTK_TEXT_VIEW (view), type);
154 gdk_window_get_device_position (win, event->device, &x, &y, NULL);
156 /* Get the iter where the cursor is at */
157 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view), type,
161 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view),
165 if (gtk_text_iter_has_tag (&iter, tag)) {
167 hand = gdk_cursor_new (GDK_HAND2);
168 beam = gdk_cursor_new (GDK_XTERM);
170 gdk_window_set_cursor (win, hand);
173 beam = gdk_cursor_new (GDK_XTERM);
175 gdk_window_set_cursor (win, beam);
182 chat_text_view_create_tags (EmpathyChatTextView *view)
184 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
187 gtk_text_buffer_create_tag (priv->buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_CUT, NULL);
188 gtk_text_buffer_create_tag (priv->buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_HIGHLIGHT, NULL);
189 gtk_text_buffer_create_tag (priv->buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_SPACING, NULL);
190 gtk_text_buffer_create_tag (priv->buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_TIME, NULL);
191 gtk_text_buffer_create_tag (priv->buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_ACTION, NULL);
192 gtk_text_buffer_create_tag (priv->buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_BODY, NULL);
193 gtk_text_buffer_create_tag (priv->buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_EVENT, NULL);
195 tag = gtk_text_buffer_create_tag (priv->buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_LINK, NULL);
196 g_signal_connect (tag, "event",
197 G_CALLBACK (chat_text_view_url_event_cb),
200 g_signal_connect (view, "motion-notify-event",
201 G_CALLBACK (chat_text_view_event_cb),
206 chat_text_view_system_font_update (EmpathyChatTextView *view)
208 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
209 PangoFontDescription *font_description = NULL;
212 font_name = g_settings_get_string (priv->gsettings_desktop,
213 EMPATHY_PREFS_DESKTOP_INTERFACE_DOCUMENT_FONT_NAME);
215 if (font_name != NULL) {
216 font_description = pango_font_description_from_string (font_name);
219 font_description = NULL;
222 gtk_widget_override_font (GTK_WIDGET (view), font_description);
224 if (font_description) {
225 pango_font_description_free (font_description);
230 chat_text_view_notify_system_font_cb (GSettings *gsettings,
232 EmpathyChatTextView *self)
234 chat_text_view_system_font_update (self);
238 chat_text_view_open_address_cb (GtkMenuItem *menuitem, const gchar *url)
240 empathy_url_show (GTK_WIDGET (menuitem), url);
244 chat_text_view_copy_address_cb (GtkMenuItem *menuitem, const gchar *url)
246 GtkClipboard *clipboard;
248 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
249 gtk_clipboard_set_text (clipboard, url, -1);
251 clipboard = gtk_clipboard_get (GDK_SELECTION_PRIMARY);
252 gtk_clipboard_set_text (clipboard, url, -1);
256 chat_text_view_populate_popup (EmpathyChatTextView *view,
260 EmpathyChatTextViewPriv *priv;
261 GtkTextTagTable *table;
264 GtkTextIter iter, start, end;
268 priv = GET_PRIV (view);
270 /* Clear menu item */
271 if (gtk_text_buffer_get_char_count (priv->buffer) > 0) {
272 item = gtk_separator_menu_item_new ();
273 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
274 gtk_widget_show (item);
276 item = gtk_image_menu_item_new_from_stock (GTK_STOCK_CLEAR, NULL);
277 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
278 gtk_widget_show (item);
280 g_signal_connect_swapped (item, "activate",
281 G_CALLBACK (empathy_chat_view_clear),
285 /* Link context menu items */
286 table = gtk_text_buffer_get_tag_table (priv->buffer);
287 tag = gtk_text_tag_table_lookup (table, EMPATHY_CHAT_TEXT_VIEW_TAG_LINK);
289 gdk_window_get_device_position (gtk_widget_get_window (GTK_WIDGET (view)),
290 gdk_device_manager_get_client_pointer (gdk_display_get_device_manager (
291 gtk_widget_get_display (GTK_WIDGET (view)))), &x, &y, NULL);
293 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
294 GTK_TEXT_WINDOW_WIDGET,
298 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &iter, x, y);
302 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
303 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
304 str = gtk_text_buffer_get_text (priv->buffer,
305 &start, &end, FALSE);
308 if (EMP_STR_EMPTY (str)) {
313 /* NOTE: Set data just to get the string freed when not needed. */
314 g_object_set_data_full (G_OBJECT (menu),
316 (GDestroyNotify) g_free);
318 item = gtk_separator_menu_item_new ();
319 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
320 gtk_widget_show (item);
322 item = gtk_menu_item_new_with_mnemonic (_("_Copy Link Address"));
323 g_signal_connect (item, "activate",
324 G_CALLBACK (chat_text_view_copy_address_cb),
326 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
327 gtk_widget_show (item);
329 item = gtk_menu_item_new_with_mnemonic (_("_Open Link"));
330 g_signal_connect (item, "activate",
331 G_CALLBACK (chat_text_view_open_address_cb),
333 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
334 gtk_widget_show (item);
338 chat_text_view_is_scrolled_down (EmpathyChatTextView *view)
345 vadj = gtk_scrollable_get_vadjustment (GTK_SCROLLABLE (view));
346 value = gtk_adjustment_get_value (vadj);
347 upper = gtk_adjustment_get_upper (vadj);
348 page_size = gtk_adjustment_get_page_size (vadj);
350 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 */
415 /* Translators: timestamp displayed between conversations in
416 * chat windows (strftime format string) */
417 tmp = empathy_time_to_string_utc (timestamp, _("%A %B %d %Y"));
418 g_string_append (str, tmp);
419 g_string_append (str, ", ");
424 tmp = empathy_time_to_string_local (timestamp, EMPATHY_TIME_FORMAT_DISPLAY_SHORT);
425 g_string_append (str, tmp);
428 g_string_append (str, " -\n");
430 /* Insert the string in the buffer */
431 empathy_chat_text_view_append_spacing (view);
432 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
433 gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
436 EMPATHY_CHAT_TEXT_VIEW_TAG_TIME,
439 g_string_free (str, TRUE);
443 chat_text_maybe_append_date_and_time (EmpathyChatTextView *view,
446 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
447 GDateTime *date, *last_date;
448 gboolean append_date = FALSE;
449 gboolean append_time = FALSE;
452 /* Get the date from last message */
453 last_date = g_date_time_new_from_unix_utc (priv->last_timestamp);
455 /* Get the date of the message we are appending */
456 date = g_date_time_new_from_unix_utc (timestamp);
458 delta = g_date_time_difference (date, last_date);
459 /* If last message was from another day we append date and time */
460 if (delta >= G_TIME_SPAN_DAY) {
465 g_date_time_unref (last_date);
466 g_date_time_unref (date);
468 /* If last message is 'old' append the time */
469 if (delta >= TIMESTAMP_INTERVAL) {
473 if (append_date || (!priv->only_if_date && append_time)) {
474 chat_text_view_append_timestamp (view, timestamp, append_date);
479 chat_text_view_size_allocate (GtkWidget *widget,
480 GtkAllocation *alloc)
484 down = chat_text_view_is_scrolled_down (EMPATHY_CHAT_TEXT_VIEW (widget));
486 GTK_WIDGET_CLASS (empathy_chat_text_view_parent_class)->size_allocate (widget, alloc);
491 adj = gtk_scrollable_get_vadjustment (GTK_SCROLLABLE (widget));
492 gtk_adjustment_set_value (adj,
493 gtk_adjustment_get_upper (adj) -
494 gtk_adjustment_get_page_size (adj));
499 chat_text_view_drag_motion (GtkWidget *widget,
500 GdkDragContext *context,
505 /* Don't handle drag motion, since we don't want the view to scroll as
506 * the result of dragging something across it. */
512 chat_text_view_get_property (GObject *object,
517 EmpathyChatTextViewPriv *priv = GET_PRIV (object);
520 case PROP_LAST_CONTACT:
521 g_value_set_object (value, priv->last_contact);
523 case PROP_ONLY_IF_DATE:
524 g_value_set_boolean (value, priv->only_if_date);
527 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
533 chat_text_view_set_property (GObject *object,
538 EmpathyChatTextViewPriv *priv = GET_PRIV (object);
541 case PROP_ONLY_IF_DATE:
542 priv->only_if_date = g_value_get_boolean (value);
545 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
551 chat_text_view_finalize (GObject *object)
553 EmpathyChatTextView *view;
554 EmpathyChatTextViewPriv *priv;
556 view = EMPATHY_CHAT_TEXT_VIEW (object);
557 priv = GET_PRIV (view);
559 DEBUG ("%p", object);
561 g_object_unref (priv->gsettings_chat);
562 g_object_unref (priv->gsettings_desktop);
564 if (priv->last_contact) {
565 g_object_unref (priv->last_contact);
567 if (priv->scroll_time) {
568 g_timer_destroy (priv->scroll_time);
570 if (priv->scroll_timeout) {
571 g_source_remove (priv->scroll_timeout);
573 g_object_unref (priv->smiley_manager);
575 G_OBJECT_CLASS (empathy_chat_text_view_parent_class)->finalize (object);
579 text_view_copy_clipboard (GtkTextView *text_view)
581 chat_text_view_copy_clipboard (EMPATHY_CHAT_VIEW (text_view));
585 empathy_chat_text_view_class_init (EmpathyChatTextViewClass *klass)
587 GObjectClass *object_class = G_OBJECT_CLASS (klass);
588 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
589 GtkTextViewClass *text_view_class = GTK_TEXT_VIEW_CLASS (klass);
591 object_class->finalize = chat_text_view_finalize;
592 object_class->get_property = chat_text_view_get_property;
593 object_class->set_property = chat_text_view_set_property;
595 widget_class->size_allocate = chat_text_view_size_allocate;
596 widget_class->drag_motion = chat_text_view_drag_motion;
598 text_view_class->copy_clipboard = text_view_copy_clipboard;
600 g_object_class_install_property (object_class,
602 g_param_spec_object ("last-contact",
604 "The sender of the last received message",
605 EMPATHY_TYPE_CONTACT,
607 g_object_class_install_property (object_class,
609 g_param_spec_boolean ("only-if-date",
611 "Display timestamp only if the date changes",
616 g_type_class_add_private (object_class, sizeof (EmpathyChatTextViewPriv));
620 empathy_chat_text_view_init (EmpathyChatTextView *view)
622 EmpathyChatTextViewPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (view,
623 EMPATHY_TYPE_CHAT_TEXT_VIEW, EmpathyChatTextViewPriv);
626 priv->buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
627 priv->last_timestamp = 0;
628 priv->allow_scrolling = TRUE;
629 priv->smiley_manager = empathy_smiley_manager_dup_singleton ();
632 "wrap-mode", GTK_WRAP_WORD_CHAR,
634 "cursor-visible", FALSE,
637 priv->gsettings_chat = g_settings_new (EMPATHY_PREFS_CHAT_SCHEMA);
639 priv->gsettings_desktop = g_settings_new (
640 EMPATHY_PREFS_DESKTOP_INTERFACE_SCHEMA);
641 g_signal_connect (priv->gsettings_desktop,
642 "changed::" EMPATHY_PREFS_DESKTOP_INTERFACE_DOCUMENT_FONT_NAME,
643 G_CALLBACK (chat_text_view_notify_system_font_cb),
645 chat_text_view_system_font_update (view);
646 chat_text_view_create_tags (view);
648 g_signal_connect (view,
650 G_CALLBACK (chat_text_view_populate_popup),
655 chat_text_view_scroll_stop (EmpathyChatTextView *view)
657 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
659 g_timer_destroy (priv->scroll_time);
660 priv->scroll_time = NULL;
662 g_source_remove (priv->scroll_timeout);
663 priv->scroll_timeout = 0;
666 /* Code stolen from pidgin/gtkimhtml.c */
668 chat_text_view_scroll_cb (EmpathyChatTextView *view)
670 EmpathyChatTextViewPriv *priv;
674 priv = GET_PRIV (view);
676 adj = gtk_scrollable_get_vadjustment (GTK_SCROLLABLE (view));
677 max_val = gtk_adjustment_get_upper (adj) - gtk_adjustment_get_page_size (adj);
679 g_return_val_if_fail (priv->scroll_time != NULL, FALSE);
681 if (g_timer_elapsed (priv->scroll_time, NULL) > MAX_SCROLL_TIME) {
682 /* time's up. jump to the end and kill the timer */
683 gtk_adjustment_set_value (adj, max_val);
684 chat_text_view_scroll_stop (view);
688 /* scroll by 1/3rd the remaining distance */
689 gtk_adjustment_set_value (adj, gtk_adjustment_get_value (adj) + ((max_val - gtk_adjustment_get_value (adj)) / 3));
694 chat_text_view_scroll_down (EmpathyChatView *view)
696 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
698 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
700 if (!priv->allow_scrolling) {
704 DEBUG ("Scrolling down");
706 if (priv->scroll_time) {
707 g_timer_reset (priv->scroll_time);
709 priv->scroll_time = g_timer_new ();
711 if (!priv->scroll_timeout) {
712 priv->scroll_timeout = g_timeout_add (SCROLL_DELAY,
713 (GSourceFunc) chat_text_view_scroll_cb,
719 chat_text_view_append_message (EmpathyChatView *view,
721 gboolean should_highlight)
723 EmpathyChatTextView *text_view = EMPATHY_CHAT_TEXT_VIEW (view);
724 EmpathyChatTextViewPriv *priv = GET_PRIV (text_view);
728 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
729 g_return_if_fail (EMPATHY_IS_MESSAGE (msg));
731 if (!empathy_message_get_body (msg)) {
735 bottom = chat_text_view_is_scrolled_down (text_view);
737 chat_text_view_maybe_trim_buffer (EMPATHY_CHAT_TEXT_VIEW (view));
739 timestamp = empathy_message_get_timestamp (msg);
740 chat_text_maybe_append_date_and_time (text_view, timestamp);
741 if (EMPATHY_CHAT_TEXT_VIEW_GET_CLASS (view)->append_message) {
742 EMPATHY_CHAT_TEXT_VIEW_GET_CLASS (view)->append_message (text_view,
748 chat_text_view_scroll_down (view);
751 if (priv->last_contact) {
752 g_object_unref (priv->last_contact);
754 priv->last_contact = g_object_ref (empathy_message_get_sender (msg));
755 g_object_notify (G_OBJECT (view), "last-contact");
757 priv->last_timestamp = timestamp;
761 chat_text_view_append_event (EmpathyChatView *view,
764 EmpathyChatTextView *text_view = EMPATHY_CHAT_TEXT_VIEW (view);
765 EmpathyChatTextViewPriv *priv = GET_PRIV (text_view);
771 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
772 g_return_if_fail (!EMP_STR_EMPTY (str));
774 bottom = chat_text_view_is_scrolled_down (text_view);
775 chat_text_view_maybe_trim_buffer (EMPATHY_CHAT_TEXT_VIEW (view));
776 chat_text_maybe_append_date_and_time (text_view,
777 empathy_time_get_current ());
779 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
780 msg = g_strdup_printf (" - %s\n", str);
781 gtk_text_buffer_insert_with_tags_by_name (priv->buffer, &iter,
783 EMPATHY_CHAT_TEXT_VIEW_TAG_EVENT,
788 chat_text_view_scroll_down (view);
791 if (priv->last_contact) {
792 g_object_unref (priv->last_contact);
793 priv->last_contact = NULL;
794 g_object_notify (G_OBJECT (view), "last-contact");
799 chat_text_view_scroll (EmpathyChatView *view,
800 gboolean allow_scrolling)
802 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
804 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
806 DEBUG ("Scrolling %s", allow_scrolling ? "enabled" : "disabled");
808 priv->allow_scrolling = allow_scrolling;
809 if (allow_scrolling) {
810 empathy_chat_view_scroll_down (view);
815 chat_text_view_get_has_selection (EmpathyChatView *view)
817 GtkTextBuffer *buffer;
819 g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), FALSE);
821 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
823 return gtk_text_buffer_get_has_selection (buffer);
827 chat_text_view_clear (EmpathyChatView *view)
829 GtkTextBuffer *buffer;
830 EmpathyChatTextViewPriv *priv;
832 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
834 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
835 gtk_text_buffer_set_text (buffer, "", -1);
837 /* We set these back to the initial values so we get
838 * timestamps when clearing the window to know when
839 * conversations start.
841 priv = GET_PRIV (view);
843 priv->last_timestamp = 0;
844 if (priv->last_contact) {
845 g_object_unref (priv->last_contact);
846 priv->last_contact = NULL;
851 chat_text_view_find_previous (EmpathyChatView *view,
852 const gchar *search_criteria,
856 EmpathyChatTextViewPriv *priv;
857 GtkTextBuffer *buffer;
858 GtkTextIter iter_at_mark;
859 GtkTextIter iter_match_start;
860 GtkTextIter iter_match_end;
862 gboolean from_start = FALSE;
864 g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), FALSE);
865 g_return_val_if_fail (search_criteria != NULL, FALSE);
867 priv = GET_PRIV (view);
869 buffer = priv->buffer;
871 if (EMP_STR_EMPTY (search_criteria)) {
872 if (priv->find_mark_previous) {
873 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
875 gtk_text_buffer_move_mark (buffer,
876 priv->find_mark_previous,
878 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
879 priv->find_mark_previous,
884 gtk_text_buffer_select_range (buffer,
896 if (!new_search && priv->find_mark_previous) {
897 gtk_text_buffer_get_iter_at_mark (buffer,
899 priv->find_mark_previous);
901 gtk_text_buffer_get_end_iter (buffer, &iter_at_mark);
905 priv->find_last_direction = FALSE;
907 /* Use the standard GTK+ method for case sensitive searches. It can't do
908 * case insensitive searches (see bug #61852), so keep the custom method
909 * around for case insensitive searches. */
911 found = gtk_text_iter_backward_search (&iter_at_mark,
913 0, /* no text search flags, we want exact matches */
918 found = empathy_text_iter_backward_search (&iter_at_mark,
926 gboolean result = FALSE;
932 /* Here we wrap around. */
933 if (!new_search && !priv->find_wrapped) {
934 priv->find_wrapped = TRUE;
935 result = chat_text_view_find_previous (view,
939 priv->find_wrapped = FALSE;
945 /* Set new mark and show on screen */
946 if (!priv->find_mark_previous) {
947 priv->find_mark_previous = gtk_text_buffer_create_mark (buffer, NULL,
951 gtk_text_buffer_move_mark (buffer,
952 priv->find_mark_previous,
956 if (!priv->find_mark_next) {
957 priv->find_mark_next = gtk_text_buffer_create_mark (buffer, NULL,
961 gtk_text_buffer_move_mark (buffer,
962 priv->find_mark_next,
966 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
967 priv->find_mark_previous,
973 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &iter_match_start);
974 gtk_text_buffer_move_mark_by_name (buffer, "insert", &iter_match_end);
980 chat_text_view_find_next (EmpathyChatView *view,
981 const gchar *search_criteria,
985 EmpathyChatTextViewPriv *priv;
986 GtkTextBuffer *buffer;
987 GtkTextIter iter_at_mark;
988 GtkTextIter iter_match_start;
989 GtkTextIter iter_match_end;
991 gboolean from_start = FALSE;
993 g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), FALSE);
994 g_return_val_if_fail (search_criteria != NULL, FALSE);
996 priv = GET_PRIV (view);
998 buffer = priv->buffer;
1000 if (EMP_STR_EMPTY (search_criteria)) {
1001 if (priv->find_mark_next) {
1002 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1004 gtk_text_buffer_move_mark (buffer,
1005 priv->find_mark_next,
1007 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
1008 priv->find_mark_next,
1013 gtk_text_buffer_select_range (buffer,
1025 if (!new_search && priv->find_mark_next) {
1026 gtk_text_buffer_get_iter_at_mark (buffer,
1028 priv->find_mark_next);
1030 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1034 priv->find_last_direction = TRUE;
1036 /* Use the standard GTK+ method for case sensitive searches. It can't do
1037 * case insensitive searches (see bug #61852), so keep the custom method
1038 * around for case insensitive searches. */
1040 found = gtk_text_iter_forward_search (&iter_at_mark,
1047 found = empathy_text_iter_forward_search (&iter_at_mark,
1055 gboolean result = FALSE;
1061 /* Here we wrap around. */
1062 if (!new_search && !priv->find_wrapped) {
1063 priv->find_wrapped = TRUE;
1064 result = chat_text_view_find_next (view,
1068 priv->find_wrapped = FALSE;
1074 /* Set new mark and show on screen */
1075 if (!priv->find_mark_next) {
1076 priv->find_mark_next = gtk_text_buffer_create_mark (buffer, NULL,
1080 gtk_text_buffer_move_mark (buffer,
1081 priv->find_mark_next,
1085 if (!priv->find_mark_previous) {
1086 priv->find_mark_previous = gtk_text_buffer_create_mark (buffer, NULL,
1090 gtk_text_buffer_move_mark (buffer,
1091 priv->find_mark_previous,
1095 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
1096 priv->find_mark_next,
1102 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &iter_match_start);
1103 gtk_text_buffer_move_mark_by_name (buffer, "insert", &iter_match_end);
1109 chat_text_view_find_abilities (EmpathyChatView *view,
1110 const gchar *search_criteria,
1111 gboolean match_case,
1112 gboolean *can_do_previous,
1113 gboolean *can_do_next)
1115 EmpathyChatTextViewPriv *priv;
1116 GtkTextBuffer *buffer;
1117 GtkTextIter iter_at_mark;
1118 GtkTextIter iter_match_start;
1119 GtkTextIter iter_match_end;
1121 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
1122 g_return_if_fail (search_criteria != NULL);
1123 g_return_if_fail (can_do_previous != NULL && can_do_next != NULL);
1125 priv = GET_PRIV (view);
1127 buffer = priv->buffer;
1129 if (can_do_previous) {
1130 if (priv->find_mark_previous) {
1131 gtk_text_buffer_get_iter_at_mark (buffer,
1133 priv->find_mark_previous);
1135 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1139 *can_do_previous = gtk_text_iter_backward_search (&iter_at_mark,
1146 *can_do_previous = empathy_text_iter_backward_search (&iter_at_mark,
1155 if (priv->find_mark_next) {
1156 gtk_text_buffer_get_iter_at_mark (buffer,
1158 priv->find_mark_next);
1160 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1164 *can_do_next = gtk_text_iter_forward_search (&iter_at_mark,
1171 *can_do_next = empathy_text_iter_forward_search (&iter_at_mark,
1181 chat_text_view_highlight (EmpathyChatView *view,
1183 gboolean match_case)
1185 GtkTextBuffer *buffer;
1187 GtkTextIter iter_start;
1188 GtkTextIter iter_end;
1189 GtkTextIter iter_match_start;
1190 GtkTextIter iter_match_end;
1193 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
1195 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
1197 gtk_text_buffer_get_start_iter (buffer, &iter);
1199 gtk_text_buffer_get_bounds (buffer, &iter_start, &iter_end);
1200 gtk_text_buffer_remove_tag_by_name (buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_HIGHLIGHT,
1204 if (EMP_STR_EMPTY (text)) {
1210 found = gtk_text_iter_forward_search (&iter,
1217 found = empathy_text_iter_forward_search (&iter,
1227 gtk_text_buffer_apply_tag_by_name (buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_HIGHLIGHT,
1231 iter = iter_match_end;
1236 chat_text_view_copy_clipboard (EmpathyChatView *view)
1238 GtkTextBuffer *buffer;
1239 GtkTextIter start, iter, end;
1240 GtkClipboard *clipboard;
1243 GtkTextChildAnchor *anchor = NULL;
1246 gboolean ignore_newlines = FALSE;
1248 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
1250 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
1251 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1253 if (!gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
1256 str = g_string_new ("");
1258 for (iter = start; !gtk_text_iter_equal (&iter, &end); gtk_text_iter_forward_char (&iter)) {
1259 c = gtk_text_iter_get_char (&iter);
1260 /* 0xFFFC is the 'object replacement' unicode character,
1261 * it indicates the presence of a pixbuf or a widget. */
1263 ignore_newlines = FALSE;
1264 if ((pixbuf = gtk_text_iter_get_pixbuf (&iter))) {
1266 text = g_object_get_data (G_OBJECT(pixbuf),
1269 str = g_string_append (str, text);
1270 } else if ((anchor = gtk_text_iter_get_child_anchor (&iter))) {
1272 list = gtk_text_child_anchor_get_widgets (anchor);
1274 text = g_object_get_data (G_OBJECT(list->data),
1277 str = g_string_append (str, text);
1281 } else if (c == '\n') {
1282 if (!ignore_newlines) {
1283 ignore_newlines = TRUE;
1284 str = g_string_append_unichar (str, c);
1287 ignore_newlines = FALSE;
1288 str = g_string_append_unichar (str, c);
1292 gtk_clipboard_set_text (clipboard, str->str, str->len);
1293 g_string_free (str, TRUE);
1297 chat_text_view_iface_init (EmpathyChatViewIface *iface)
1299 iface->append_message = chat_text_view_append_message;
1300 iface->append_event = chat_text_view_append_event;
1301 iface->scroll = chat_text_view_scroll;
1302 iface->scroll_down = chat_text_view_scroll_down;
1303 iface->get_has_selection = chat_text_view_get_has_selection;
1304 iface->clear = chat_text_view_clear;
1305 iface->find_previous = chat_text_view_find_previous;
1306 iface->find_next = chat_text_view_find_next;
1307 iface->find_abilities = chat_text_view_find_abilities;
1308 iface->highlight = chat_text_view_highlight;
1309 iface->copy_clipboard = chat_text_view_copy_clipboard;
1313 empathy_chat_text_view_get_last_contact (EmpathyChatTextView *view)
1315 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1317 g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), NULL);
1319 return priv->last_contact;
1323 empathy_chat_text_view_get_last_timestamp (EmpathyChatTextView *view)
1325 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1327 g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), 0);
1329 return priv->last_timestamp;
1333 empathy_chat_text_view_set_only_if_date (EmpathyChatTextView *view,
1334 gboolean only_if_date)
1336 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1338 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
1340 if (only_if_date != priv->only_if_date) {
1341 priv->only_if_date = only_if_date;
1342 g_object_notify (G_OBJECT (view), "only-if-date");
1347 chat_text_view_replace_link (const gchar *text,
1349 gpointer match_data,
1352 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (user_data);
1355 gtk_text_buffer_get_end_iter (buffer, &iter);
1356 gtk_text_buffer_insert_with_tags_by_name (buffer, &iter,
1358 EMPATHY_CHAT_TEXT_VIEW_TAG_LINK,
1363 chat_text_view_replace_smiley (const gchar *text,
1365 gpointer match_data,
1368 EmpathySmileyHit *hit = match_data;
1369 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (user_data);
1372 gtk_text_buffer_get_end_iter (buffer, &iter);
1373 gtk_text_buffer_insert_pixbuf (buffer, &iter, hit->pixbuf);
1377 chat_text_view_replace_verbatim (const gchar *text,
1379 gpointer match_data,
1382 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (user_data);
1385 gtk_text_buffer_get_end_iter (buffer, &iter);
1386 gtk_text_buffer_insert (buffer, &iter, text, len);
1389 static EmpathyStringParser string_parsers[] = {
1390 {empathy_string_match_link, chat_text_view_replace_link},
1391 {empathy_string_match_all, chat_text_view_replace_verbatim},
1395 static EmpathyStringParser string_parsers_with_smiley[] = {
1396 {empathy_string_match_link, chat_text_view_replace_link},
1397 {empathy_string_match_smiley, chat_text_view_replace_smiley},
1398 {empathy_string_match_all, chat_text_view_replace_verbatim},
1403 empathy_chat_text_view_append_body (EmpathyChatTextView *view,
1407 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1408 EmpathyStringParser *parsers;
1409 gboolean use_smileys;
1410 GtkTextIter start_iter;
1414 /* Check if we have to parse smileys */
1415 use_smileys = g_settings_get_boolean (priv->gsettings_chat,
1416 EMPATHY_PREFS_CHAT_SHOW_SMILEYS);
1419 parsers = string_parsers_with_smiley;
1421 parsers = string_parsers;
1423 /* Create a mark at the place we'll start inserting */
1424 gtk_text_buffer_get_end_iter (priv->buffer, &start_iter);
1425 mark = gtk_text_buffer_create_mark (priv->buffer, NULL, &start_iter, TRUE);
1427 /* Parse text for links/smileys and insert in the buffer */
1428 empathy_string_parser_substr (body, -1, parsers, priv->buffer);
1430 /* Insert a newline after the text inserted */
1431 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1432 gtk_text_buffer_insert (priv->buffer, &iter, "\n", 1);
1434 /* Apply the style to the inserted text. */
1435 gtk_text_buffer_get_iter_at_mark (priv->buffer, &start_iter, mark);
1436 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1437 gtk_text_buffer_apply_tag_by_name (priv->buffer, tag,
1441 gtk_text_buffer_delete_mark (priv->buffer, mark);
1445 empathy_chat_text_view_append_spacing (EmpathyChatTextView *view)
1447 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1450 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1451 gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
1455 EMPATHY_CHAT_TEXT_VIEW_TAG_CUT,
1456 EMPATHY_CHAT_TEXT_VIEW_TAG_SPACING,
1461 empathy_chat_text_view_tag_set (EmpathyChatTextView *view,
1462 const gchar *tag_name,
1463 const gchar *first_property_name,
1466 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1468 GtkTextTagTable *table;
1471 g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), NULL);
1472 g_return_val_if_fail (tag_name != NULL, NULL);
1474 table = gtk_text_buffer_get_tag_table (priv->buffer);
1475 tag = gtk_text_tag_table_lookup (table, tag_name);
1477 if (tag && first_property_name) {
1478 va_start (list, first_property_name);
1479 g_object_set_valist (G_OBJECT (tag), first_property_name, list);