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"
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 300
54 #define MAX_SCROLL_TIME 0.4 /* seconds */
55 #define SCROLL_DELAY 33 /* milliseconds */
57 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyChatTextView)
60 GtkTextBuffer *buffer;
63 GtkTextMark *find_mark_previous;
64 GtkTextMark *find_mark_next;
65 gboolean find_wrapped;
66 gboolean find_last_direction;
67 EmpathyContact *last_contact;
68 time_t last_timestamp;
69 gboolean allow_scrolling;
70 guint notify_system_fonts_id;
71 EmpathySmileyManager *smiley_manager;
72 gboolean only_if_date;
73 } EmpathyChatTextViewPriv;
75 static void chat_text_view_iface_init (EmpathyChatViewIface *iface);
77 static void chat_text_view_copy_clipboard (EmpathyChatView *view);
79 G_DEFINE_TYPE_WITH_CODE (EmpathyChatTextView, empathy_chat_text_view,
81 G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CHAT_VIEW,
82 chat_text_view_iface_init));
91 chat_text_view_url_event_cb (GtkTextTag *tag,
95 EmpathyChatTextView *view)
97 EmpathyChatTextViewPriv *priv;
98 GtkTextIter start, end;
101 priv = GET_PRIV (view);
103 /* If the link is being selected, don't do anything. */
104 gtk_text_buffer_get_selection_bounds (priv->buffer, &start, &end);
105 if (gtk_text_iter_get_offset (&start) != gtk_text_iter_get_offset (&end)) {
109 if (event->type == GDK_BUTTON_RELEASE && event->button.button == 1) {
112 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
113 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
114 str = gtk_text_buffer_get_text (priv->buffer,
119 empathy_url_show (GTK_WIDGET (view), str);
128 chat_text_view_event_cb (EmpathyChatTextView *view,
129 GdkEventMotion *event,
132 static GdkCursor *hand = NULL;
133 static GdkCursor *beam = NULL;
134 GtkTextWindowType type;
137 gint x, y, buf_x, buf_y;
139 type = gtk_text_view_get_window_type (GTK_TEXT_VIEW (view),
142 if (type != GTK_TEXT_WINDOW_TEXT) {
146 /* Get where the pointer really is. */
147 win = gtk_text_view_get_window (GTK_TEXT_VIEW (view), type);
152 gdk_window_get_pointer (win, &x, &y, NULL);
154 /* Get the iter where the cursor is at */
155 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view), type,
159 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view),
163 if (gtk_text_iter_has_tag (&iter, tag)) {
165 hand = gdk_cursor_new (GDK_HAND2);
166 beam = gdk_cursor_new (GDK_XTERM);
168 gdk_window_set_cursor (win, hand);
171 beam = gdk_cursor_new (GDK_XTERM);
173 gdk_window_set_cursor (win, beam);
180 chat_text_view_create_tags (EmpathyChatTextView *view)
182 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
185 gtk_text_buffer_create_tag (priv->buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_CUT, NULL);
186 gtk_text_buffer_create_tag (priv->buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_HIGHLIGHT, NULL);
187 gtk_text_buffer_create_tag (priv->buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_SPACING, NULL);
188 gtk_text_buffer_create_tag (priv->buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_TIME, NULL);
189 gtk_text_buffer_create_tag (priv->buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_ACTION, NULL);
190 gtk_text_buffer_create_tag (priv->buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_BODY, NULL);
191 gtk_text_buffer_create_tag (priv->buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_EVENT, NULL);
193 tag = gtk_text_buffer_create_tag (priv->buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_LINK, NULL);
194 g_signal_connect (tag, "event",
195 G_CALLBACK (chat_text_view_url_event_cb),
198 g_signal_connect (view, "motion-notify-event",
199 G_CALLBACK (chat_text_view_event_cb),
204 chat_text_view_system_font_update (EmpathyChatTextView *view)
206 PangoFontDescription *font_description = NULL;
209 if (empathy_conf_get_string (empathy_conf_get (),
210 "/desktop/gnome/interface/document_font_name",
211 &font_name) && font_name) {
212 font_description = pango_font_description_from_string (font_name);
215 font_description = NULL;
218 gtk_widget_modify_font (GTK_WIDGET (view), font_description);
220 if (font_description) {
221 pango_font_description_free (font_description);
226 chat_text_view_notify_system_font_cb (EmpathyConf *conf,
230 EmpathyChatTextView *view = user_data;
232 chat_text_view_system_font_update (view);
236 chat_text_view_open_address_cb (GtkMenuItem *menuitem, const gchar *url)
238 empathy_url_show (GTK_WIDGET (menuitem), url);
242 chat_text_view_copy_address_cb (GtkMenuItem *menuitem, const gchar *url)
244 GtkClipboard *clipboard;
246 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
247 gtk_clipboard_set_text (clipboard, url, -1);
249 clipboard = gtk_clipboard_get (GDK_SELECTION_PRIMARY);
250 gtk_clipboard_set_text (clipboard, url, -1);
254 chat_text_view_populate_popup (EmpathyChatTextView *view,
258 EmpathyChatTextViewPriv *priv;
259 GtkTextTagTable *table;
262 GtkTextIter iter, start, end;
266 priv = GET_PRIV (view);
268 /* Clear menu item */
269 if (gtk_text_buffer_get_char_count (priv->buffer) > 0) {
270 item = gtk_separator_menu_item_new ();
271 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
272 gtk_widget_show (item);
274 item = gtk_image_menu_item_new_from_stock (GTK_STOCK_CLEAR, NULL);
275 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
276 gtk_widget_show (item);
278 g_signal_connect_swapped (item, "activate",
279 G_CALLBACK (empathy_chat_view_clear),
283 /* Link context menu items */
284 table = gtk_text_buffer_get_tag_table (priv->buffer);
285 tag = gtk_text_tag_table_lookup (table, EMPATHY_CHAT_TEXT_VIEW_TAG_LINK);
287 gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
289 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
290 GTK_TEXT_WINDOW_WIDGET,
294 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &iter, x, y);
298 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
299 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
300 str = gtk_text_buffer_get_text (priv->buffer,
301 &start, &end, FALSE);
304 if (EMP_STR_EMPTY (str)) {
309 /* NOTE: Set data just to get the string freed when not needed. */
310 g_object_set_data_full (G_OBJECT (menu),
312 (GDestroyNotify) g_free);
314 item = gtk_separator_menu_item_new ();
315 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
316 gtk_widget_show (item);
318 item = gtk_menu_item_new_with_mnemonic (_("_Copy Link Address"));
319 g_signal_connect (item, "activate",
320 G_CALLBACK (chat_text_view_copy_address_cb),
322 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
323 gtk_widget_show (item);
325 item = gtk_menu_item_new_with_mnemonic (_("_Open Link"));
326 g_signal_connect (item, "activate",
327 G_CALLBACK (chat_text_view_open_address_cb),
329 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
330 gtk_widget_show (item);
334 chat_text_view_is_scrolled_down (EmpathyChatTextView *view)
338 sw = gtk_widget_get_parent (GTK_WIDGET (view));
339 if (GTK_IS_SCROLLED_WINDOW (sw)) {
345 vadj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (sw));
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) {
359 chat_text_view_maybe_trim_buffer (EmpathyChatTextView *view)
361 EmpathyChatTextViewPriv *priv;
362 GtkTextIter top, bottom;
365 GtkTextTagTable *table;
368 priv = GET_PRIV (view);
370 gtk_text_buffer_get_end_iter (priv->buffer, &bottom);
371 line = gtk_text_iter_get_line (&bottom);
372 if (line < MAX_LINES) {
376 remove_ = line - MAX_LINES;
377 gtk_text_buffer_get_start_iter (priv->buffer, &top);
380 if (!gtk_text_iter_forward_lines (&bottom, remove_)) {
384 /* Track backwords to a place where we can safely cut, we don't do it in
385 * the middle of a tag.
387 table = gtk_text_buffer_get_tag_table (priv->buffer);
388 tag = gtk_text_tag_table_lookup (table, EMPATHY_CHAT_TEXT_VIEW_TAG_CUT);
393 if (!gtk_text_iter_forward_to_tag_toggle (&bottom, tag)) {
397 if (!gtk_text_iter_equal (&top, &bottom)) {
398 gtk_text_buffer_delete (priv->buffer, &top, &bottom);
403 chat_text_view_append_timestamp (EmpathyChatTextView *view,
407 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
412 str = g_string_new ("- ");
414 /* Append date if needed */
419 date = g_date_new ();
420 g_date_set_time_t (date, timestamp);
421 /* Translators: timestamp displayed between conversations in
422 * chat windows (strftime format string) */
423 g_date_strftime (buf, 256, _("%A %B %d %Y"), date);
424 g_string_append (str, buf);
425 g_string_append (str, ", ");
430 tmp = empathy_time_to_string_local (timestamp, EMPATHY_TIME_FORMAT_DISPLAY_SHORT);
431 g_string_append (str, tmp);
434 g_string_append (str, " -\n");
436 /* Insert the string in the buffer */
437 empathy_chat_text_view_append_spacing (view);
438 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
439 gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
442 EMPATHY_CHAT_TEXT_VIEW_TAG_TIME,
445 g_string_free (str, TRUE);
449 chat_text_maybe_append_date_and_time (EmpathyChatTextView *view,
452 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
453 GDate *date, *last_date;
454 gboolean append_date = FALSE;
455 gboolean append_time = FALSE;
457 /* Get the date from last message */
458 last_date = g_date_new ();
459 g_date_set_time_t (last_date, priv->last_timestamp);
461 /* Get the date of the message we are appending */
462 date = g_date_new ();
463 g_date_set_time_t (date, timestamp);
465 /* If last message was from another day we append date and time */
466 if (g_date_compare (date, last_date) > 0) {
471 g_date_free (last_date);
474 /* If last message is 'old' append the time */
475 if (timestamp - priv->last_timestamp >= TIMESTAMP_INTERVAL) {
479 if (append_date || (!priv->only_if_date && append_time)) {
480 chat_text_view_append_timestamp (view, timestamp, append_date);
485 chat_text_view_size_allocate (GtkWidget *widget,
486 GtkAllocation *alloc)
490 down = chat_text_view_is_scrolled_down (EMPATHY_CHAT_TEXT_VIEW (widget));
492 GTK_WIDGET_CLASS (empathy_chat_text_view_parent_class)->size_allocate (widget, alloc);
498 sw = gtk_widget_get_parent (widget);
499 if (!GTK_IS_SCROLLED_WINDOW (sw))
502 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (sw));
503 gtk_adjustment_set_value (adj,
504 gtk_adjustment_get_upper (adj) -
505 gtk_adjustment_get_page_size (adj));
510 chat_text_view_drag_motion (GtkWidget *widget,
511 GdkDragContext *context,
516 /* Don't handle drag motion, since we don't want the view to scroll as
517 * the result of dragging something across it. */
523 chat_text_view_get_property (GObject *object,
528 EmpathyChatTextViewPriv *priv = GET_PRIV (object);
531 case PROP_LAST_CONTACT:
532 g_value_set_object (value, priv->last_contact);
534 case PROP_ONLY_IF_DATE:
535 g_value_set_boolean (value, priv->only_if_date);
538 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
544 chat_text_view_set_property (GObject *object,
549 EmpathyChatTextViewPriv *priv = GET_PRIV (object);
552 case PROP_ONLY_IF_DATE:
553 priv->only_if_date = g_value_get_boolean (value);
556 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
562 chat_text_view_finalize (GObject *object)
564 EmpathyChatTextView *view;
565 EmpathyChatTextViewPriv *priv;
567 view = EMPATHY_CHAT_TEXT_VIEW (object);
568 priv = GET_PRIV (view);
570 DEBUG ("%p", object);
572 empathy_conf_notify_remove (empathy_conf_get (), priv->notify_system_fonts_id);
574 if (priv->last_contact) {
575 g_object_unref (priv->last_contact);
577 if (priv->scroll_time) {
578 g_timer_destroy (priv->scroll_time);
580 if (priv->scroll_timeout) {
581 g_source_remove (priv->scroll_timeout);
583 g_object_unref (priv->smiley_manager);
585 G_OBJECT_CLASS (empathy_chat_text_view_parent_class)->finalize (object);
589 text_view_copy_clipboard (GtkTextView *text_view)
591 chat_text_view_copy_clipboard (EMPATHY_CHAT_VIEW (text_view));
595 empathy_chat_text_view_class_init (EmpathyChatTextViewClass *klass)
597 GObjectClass *object_class = G_OBJECT_CLASS (klass);
598 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
599 GtkTextViewClass *text_view_class = GTK_TEXT_VIEW_CLASS (klass);
601 object_class->finalize = chat_text_view_finalize;
602 object_class->get_property = chat_text_view_get_property;
603 object_class->set_property = chat_text_view_set_property;
605 widget_class->size_allocate = chat_text_view_size_allocate;
606 widget_class->drag_motion = chat_text_view_drag_motion;
608 text_view_class->copy_clipboard = text_view_copy_clipboard;
610 g_object_class_install_property (object_class,
612 g_param_spec_object ("last-contact",
614 "The sender of the last received message",
615 EMPATHY_TYPE_CONTACT,
617 g_object_class_install_property (object_class,
619 g_param_spec_boolean ("only-if-date",
621 "Display timestamp only if the date changes",
626 g_type_class_add_private (object_class, sizeof (EmpathyChatTextViewPriv));
630 empathy_chat_text_view_init (EmpathyChatTextView *view)
632 EmpathyChatTextViewPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (view,
633 EMPATHY_TYPE_CHAT_TEXT_VIEW, EmpathyChatTextViewPriv);
636 priv->buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
637 priv->last_timestamp = 0;
638 priv->allow_scrolling = TRUE;
639 priv->smiley_manager = empathy_smiley_manager_dup_singleton ();
642 "wrap-mode", GTK_WRAP_WORD_CHAR,
644 "cursor-visible", FALSE,
647 priv->notify_system_fonts_id =
648 empathy_conf_notify_add (empathy_conf_get (),
649 "/desktop/gnome/interface/document_font_name",
650 chat_text_view_notify_system_font_cb,
652 chat_text_view_system_font_update (view);
653 chat_text_view_create_tags (view);
655 g_signal_connect (view,
657 G_CALLBACK (chat_text_view_populate_popup),
662 chat_text_view_scroll_stop (EmpathyChatTextView *view)
664 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
666 g_timer_destroy (priv->scroll_time);
667 priv->scroll_time = NULL;
669 g_source_remove (priv->scroll_timeout);
670 priv->scroll_timeout = 0;
673 /* Code stolen from pidgin/gtkimhtml.c */
675 chat_text_view_scroll_cb (EmpathyChatTextView *view)
677 EmpathyChatTextViewPriv *priv;
682 priv = GET_PRIV (view);
684 sw = gtk_widget_get_parent (GTK_WIDGET (view));
685 if (!GTK_IS_SCROLLED_WINDOW (sw)) {
686 chat_text_view_scroll_stop (view);
690 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (sw));
691 max_val = gtk_adjustment_get_upper (adj) - gtk_adjustment_get_page_size (adj);
693 g_return_val_if_fail (priv->scroll_time != NULL, FALSE);
695 if (g_timer_elapsed (priv->scroll_time, NULL) > MAX_SCROLL_TIME) {
696 /* time's up. jump to the end and kill the timer */
697 gtk_adjustment_set_value (adj, max_val);
698 chat_text_view_scroll_stop (view);
702 /* scroll by 1/3rd the remaining distance */
703 gtk_adjustment_set_value (adj, gtk_adjustment_get_value (adj) + ((max_val - gtk_adjustment_get_value (adj)) / 3));
708 chat_text_view_scroll_down (EmpathyChatView *view)
710 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
712 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
714 if (!priv->allow_scrolling) {
718 DEBUG ("Scrolling down");
720 if (priv->scroll_time) {
721 g_timer_reset (priv->scroll_time);
723 priv->scroll_time = g_timer_new ();
725 if (!priv->scroll_timeout) {
726 priv->scroll_timeout = g_timeout_add (SCROLL_DELAY,
727 (GSourceFunc) chat_text_view_scroll_cb,
733 chat_text_view_append_message (EmpathyChatView *view,
736 EmpathyChatTextView *text_view = EMPATHY_CHAT_TEXT_VIEW (view);
737 EmpathyChatTextViewPriv *priv = GET_PRIV (text_view);
741 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
742 g_return_if_fail (EMPATHY_IS_MESSAGE (msg));
744 if (!empathy_message_get_body (msg)) {
748 bottom = chat_text_view_is_scrolled_down (text_view);
750 chat_text_view_maybe_trim_buffer (EMPATHY_CHAT_TEXT_VIEW (view));
752 timestamp = empathy_message_get_timestamp (msg);
753 chat_text_maybe_append_date_and_time (text_view, timestamp);
754 if (EMPATHY_CHAT_TEXT_VIEW_GET_CLASS (view)->append_message) {
755 EMPATHY_CHAT_TEXT_VIEW_GET_CLASS (view)->append_message (text_view,
760 chat_text_view_scroll_down (view);
763 if (priv->last_contact) {
764 g_object_unref (priv->last_contact);
766 priv->last_contact = g_object_ref (empathy_message_get_sender (msg));
767 g_object_notify (G_OBJECT (view), "last-contact");
769 priv->last_timestamp = timestamp;
773 chat_text_view_append_event (EmpathyChatView *view,
776 EmpathyChatTextView *text_view = EMPATHY_CHAT_TEXT_VIEW (view);
777 EmpathyChatTextViewPriv *priv = GET_PRIV (text_view);
783 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
784 g_return_if_fail (!EMP_STR_EMPTY (str));
786 bottom = chat_text_view_is_scrolled_down (text_view);
787 chat_text_view_maybe_trim_buffer (EMPATHY_CHAT_TEXT_VIEW (view));
788 chat_text_maybe_append_date_and_time (text_view,
789 empathy_time_get_current ());
791 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
792 msg = g_strdup_printf (" - %s\n", str);
793 gtk_text_buffer_insert_with_tags_by_name (priv->buffer, &iter,
795 EMPATHY_CHAT_TEXT_VIEW_TAG_EVENT,
800 chat_text_view_scroll_down (view);
803 if (priv->last_contact) {
804 g_object_unref (priv->last_contact);
805 priv->last_contact = NULL;
806 g_object_notify (G_OBJECT (view), "last-contact");
811 chat_text_view_scroll (EmpathyChatView *view,
812 gboolean allow_scrolling)
814 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
816 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
818 DEBUG ("Scrolling %s", allow_scrolling ? "enabled" : "disabled");
820 priv->allow_scrolling = allow_scrolling;
821 if (allow_scrolling) {
822 empathy_chat_view_scroll_down (view);
827 chat_text_view_get_has_selection (EmpathyChatView *view)
829 GtkTextBuffer *buffer;
831 g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), FALSE);
833 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
835 return gtk_text_buffer_get_has_selection (buffer);
839 chat_text_view_clear (EmpathyChatView *view)
841 GtkTextBuffer *buffer;
842 EmpathyChatTextViewPriv *priv;
844 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
846 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
847 gtk_text_buffer_set_text (buffer, "", -1);
849 /* We set these back to the initial values so we get
850 * timestamps when clearing the window to know when
851 * conversations start.
853 priv = GET_PRIV (view);
855 priv->last_timestamp = 0;
856 if (priv->last_contact) {
857 g_object_unref (priv->last_contact);
858 priv->last_contact = NULL;
863 chat_text_view_find_previous (EmpathyChatView *view,
864 const gchar *search_criteria,
868 EmpathyChatTextViewPriv *priv;
869 GtkTextBuffer *buffer;
870 GtkTextIter iter_at_mark;
871 GtkTextIter iter_match_start;
872 GtkTextIter iter_match_end;
874 gboolean from_start = FALSE;
876 g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), FALSE);
877 g_return_val_if_fail (search_criteria != NULL, FALSE);
879 priv = GET_PRIV (view);
881 buffer = priv->buffer;
883 if (EMP_STR_EMPTY (search_criteria)) {
884 if (priv->find_mark_previous) {
885 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
887 gtk_text_buffer_move_mark (buffer,
888 priv->find_mark_previous,
890 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
891 priv->find_mark_previous,
896 gtk_text_buffer_select_range (buffer,
908 if (!new_search && priv->find_mark_previous) {
909 gtk_text_buffer_get_iter_at_mark (buffer,
911 priv->find_mark_previous);
913 gtk_text_buffer_get_end_iter (buffer, &iter_at_mark);
917 priv->find_last_direction = FALSE;
919 /* Use the standard GTK+ method for case sensitive searches. It can't do
920 * case insensitive searches (see bug #61852), so keep the custom method
921 * around for case insensitive searches. */
923 found = gtk_text_iter_backward_search (&iter_at_mark,
925 0, /* no text search flags, we want exact matches */
930 found = empathy_text_iter_backward_search (&iter_at_mark,
938 gboolean result = FALSE;
944 /* Here we wrap around. */
945 if (!new_search && !priv->find_wrapped) {
946 priv->find_wrapped = TRUE;
947 result = chat_text_view_find_previous (view,
951 priv->find_wrapped = FALSE;
957 /* Set new mark and show on screen */
958 if (!priv->find_mark_previous) {
959 priv->find_mark_previous = gtk_text_buffer_create_mark (buffer, NULL,
963 gtk_text_buffer_move_mark (buffer,
964 priv->find_mark_previous,
968 if (!priv->find_mark_next) {
969 priv->find_mark_next = gtk_text_buffer_create_mark (buffer, NULL,
973 gtk_text_buffer_move_mark (buffer,
974 priv->find_mark_next,
978 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
979 priv->find_mark_previous,
985 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &iter_match_start);
986 gtk_text_buffer_move_mark_by_name (buffer, "insert", &iter_match_end);
992 chat_text_view_find_next (EmpathyChatView *view,
993 const gchar *search_criteria,
997 EmpathyChatTextViewPriv *priv;
998 GtkTextBuffer *buffer;
999 GtkTextIter iter_at_mark;
1000 GtkTextIter iter_match_start;
1001 GtkTextIter iter_match_end;
1003 gboolean from_start = FALSE;
1005 g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), FALSE);
1006 g_return_val_if_fail (search_criteria != NULL, FALSE);
1008 priv = GET_PRIV (view);
1010 buffer = priv->buffer;
1012 if (EMP_STR_EMPTY (search_criteria)) {
1013 if (priv->find_mark_next) {
1014 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1016 gtk_text_buffer_move_mark (buffer,
1017 priv->find_mark_next,
1019 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
1020 priv->find_mark_next,
1025 gtk_text_buffer_select_range (buffer,
1037 if (!new_search && priv->find_mark_next) {
1038 gtk_text_buffer_get_iter_at_mark (buffer,
1040 priv->find_mark_next);
1042 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1046 priv->find_last_direction = TRUE;
1048 /* Use the standard GTK+ method for case sensitive searches. It can't do
1049 * case insensitive searches (see bug #61852), so keep the custom method
1050 * around for case insensitive searches. */
1052 found = gtk_text_iter_forward_search (&iter_at_mark,
1059 found = empathy_text_iter_forward_search (&iter_at_mark,
1067 gboolean result = FALSE;
1073 /* Here we wrap around. */
1074 if (!new_search && !priv->find_wrapped) {
1075 priv->find_wrapped = TRUE;
1076 result = chat_text_view_find_next (view,
1080 priv->find_wrapped = FALSE;
1086 /* Set new mark and show on screen */
1087 if (!priv->find_mark_next) {
1088 priv->find_mark_next = gtk_text_buffer_create_mark (buffer, NULL,
1092 gtk_text_buffer_move_mark (buffer,
1093 priv->find_mark_next,
1097 if (!priv->find_mark_previous) {
1098 priv->find_mark_previous = gtk_text_buffer_create_mark (buffer, NULL,
1102 gtk_text_buffer_move_mark (buffer,
1103 priv->find_mark_previous,
1107 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
1108 priv->find_mark_next,
1114 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &iter_match_start);
1115 gtk_text_buffer_move_mark_by_name (buffer, "insert", &iter_match_end);
1121 chat_text_view_find_abilities (EmpathyChatView *view,
1122 const gchar *search_criteria,
1123 gboolean match_case,
1124 gboolean *can_do_previous,
1125 gboolean *can_do_next)
1127 EmpathyChatTextViewPriv *priv;
1128 GtkTextBuffer *buffer;
1129 GtkTextIter iter_at_mark;
1130 GtkTextIter iter_match_start;
1131 GtkTextIter iter_match_end;
1133 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
1134 g_return_if_fail (search_criteria != NULL);
1135 g_return_if_fail (can_do_previous != NULL && can_do_next != NULL);
1137 priv = GET_PRIV (view);
1139 buffer = priv->buffer;
1141 if (can_do_previous) {
1142 if (priv->find_mark_previous) {
1143 gtk_text_buffer_get_iter_at_mark (buffer,
1145 priv->find_mark_previous);
1147 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1151 *can_do_previous = gtk_text_iter_backward_search (&iter_at_mark,
1158 *can_do_previous = empathy_text_iter_backward_search (&iter_at_mark,
1167 if (priv->find_mark_next) {
1168 gtk_text_buffer_get_iter_at_mark (buffer,
1170 priv->find_mark_next);
1172 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1176 *can_do_next = gtk_text_iter_forward_search (&iter_at_mark,
1183 *can_do_next = empathy_text_iter_forward_search (&iter_at_mark,
1193 chat_text_view_highlight (EmpathyChatView *view,
1195 gboolean match_case)
1197 GtkTextBuffer *buffer;
1199 GtkTextIter iter_start;
1200 GtkTextIter iter_end;
1201 GtkTextIter iter_match_start;
1202 GtkTextIter iter_match_end;
1205 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
1207 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
1209 gtk_text_buffer_get_start_iter (buffer, &iter);
1211 gtk_text_buffer_get_bounds (buffer, &iter_start, &iter_end);
1212 gtk_text_buffer_remove_tag_by_name (buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_HIGHLIGHT,
1216 if (EMP_STR_EMPTY (text)) {
1222 found = gtk_text_iter_forward_search (&iter,
1229 found = empathy_text_iter_forward_search (&iter,
1239 gtk_text_buffer_apply_tag_by_name (buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_HIGHLIGHT,
1243 iter = iter_match_end;
1248 chat_text_view_copy_clipboard (EmpathyChatView *view)
1250 GtkTextBuffer *buffer;
1251 GtkTextIter start, iter, end;
1252 GtkClipboard *clipboard;
1255 GtkTextChildAnchor *anchor = NULL;
1258 gboolean ignore_newlines = FALSE;
1260 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
1262 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
1263 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1265 if (!gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
1268 str = g_string_new ("");
1270 for (iter = start; !gtk_text_iter_equal (&iter, &end); gtk_text_iter_forward_char (&iter)) {
1271 c = gtk_text_iter_get_char (&iter);
1272 /* 0xFFFC is the 'object replacement' unicode character,
1273 * it indicates the presence of a pixbuf or a widget. */
1275 ignore_newlines = FALSE;
1276 if ((pixbuf = gtk_text_iter_get_pixbuf (&iter))) {
1278 text = g_object_get_data (G_OBJECT(pixbuf),
1281 str = g_string_append (str, text);
1282 } else if ((anchor = gtk_text_iter_get_child_anchor (&iter))) {
1284 list = gtk_text_child_anchor_get_widgets (anchor);
1286 text = g_object_get_data (G_OBJECT(list->data),
1289 str = g_string_append (str, text);
1293 } else if (c == '\n') {
1294 if (!ignore_newlines) {
1295 ignore_newlines = TRUE;
1296 str = g_string_append_unichar (str, c);
1299 ignore_newlines = FALSE;
1300 str = g_string_append_unichar (str, c);
1304 gtk_clipboard_set_text (clipboard, str->str, str->len);
1305 g_string_free (str, TRUE);
1309 chat_text_view_iface_init (EmpathyChatViewIface *iface)
1311 iface->append_message = chat_text_view_append_message;
1312 iface->append_event = chat_text_view_append_event;
1313 iface->scroll = chat_text_view_scroll;
1314 iface->scroll_down = chat_text_view_scroll_down;
1315 iface->get_has_selection = chat_text_view_get_has_selection;
1316 iface->clear = chat_text_view_clear;
1317 iface->find_previous = chat_text_view_find_previous;
1318 iface->find_next = chat_text_view_find_next;
1319 iface->find_abilities = chat_text_view_find_abilities;
1320 iface->highlight = chat_text_view_highlight;
1321 iface->copy_clipboard = chat_text_view_copy_clipboard;
1325 empathy_chat_text_view_get_last_contact (EmpathyChatTextView *view)
1327 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1329 g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), NULL);
1331 return priv->last_contact;
1335 empathy_chat_text_view_get_last_timestamp (EmpathyChatTextView *view)
1337 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1339 g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), 0);
1341 return priv->last_timestamp;
1345 empathy_chat_text_view_set_only_if_date (EmpathyChatTextView *view,
1346 gboolean only_if_date)
1348 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1350 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
1352 if (only_if_date != priv->only_if_date) {
1353 priv->only_if_date = only_if_date;
1354 g_object_notify (G_OBJECT (view), "only-if-date");
1359 chat_text_view_replace_link (const gchar *text,
1361 gpointer match_data,
1364 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (user_data);
1367 gtk_text_buffer_get_end_iter (buffer, &iter);
1368 gtk_text_buffer_insert_with_tags_by_name (buffer, &iter,
1370 EMPATHY_CHAT_TEXT_VIEW_TAG_LINK,
1375 chat_text_view_replace_smiley (const gchar *text,
1377 gpointer match_data,
1380 EmpathySmileyHit *hit = match_data;
1381 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (user_data);
1384 gtk_text_buffer_get_end_iter (buffer, &iter);
1385 gtk_text_buffer_insert_pixbuf (buffer, &iter, hit->pixbuf);
1389 chat_text_view_replace_verbatim (const gchar *text,
1391 gpointer match_data,
1394 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (user_data);
1397 gtk_text_buffer_get_end_iter (buffer, &iter);
1398 gtk_text_buffer_insert (buffer, &iter, text, len);
1401 static EmpathyStringParser string_parsers[] = {
1402 {empathy_string_match_link, chat_text_view_replace_link},
1403 {empathy_string_match_all, chat_text_view_replace_verbatim},
1407 static EmpathyStringParser string_parsers_with_smiley[] = {
1408 {empathy_string_match_link, chat_text_view_replace_link},
1409 {empathy_string_match_smiley, chat_text_view_replace_smiley},
1410 {empathy_string_match_all, chat_text_view_replace_verbatim},
1415 empathy_chat_text_view_append_body (EmpathyChatTextView *view,
1419 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1420 EmpathyStringParser *parsers;
1421 gboolean use_smileys;
1422 GtkTextIter start_iter;
1426 /* Check if we have to parse smileys */
1427 empathy_conf_get_bool (empathy_conf_get (),
1428 EMPATHY_PREFS_CHAT_SHOW_SMILEYS,
1431 parsers = string_parsers_with_smiley;
1433 parsers = string_parsers;
1435 /* Create a mark at the place we'll start inserting */
1436 gtk_text_buffer_get_end_iter (priv->buffer, &start_iter);
1437 mark = gtk_text_buffer_create_mark (priv->buffer, NULL, &start_iter, TRUE);
1439 /* Parse text for links/smileys and insert in the buffer */
1440 empathy_string_parser_substr (body, -1, parsers, priv->buffer);
1442 /* Insert a newline after the text inserted */
1443 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1444 gtk_text_buffer_insert (priv->buffer, &iter, "\n", 1);
1446 /* Apply the style to the inserted text. */
1447 gtk_text_buffer_get_iter_at_mark (priv->buffer, &start_iter, mark);
1448 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1449 gtk_text_buffer_apply_tag_by_name (priv->buffer, tag,
1453 gtk_text_buffer_delete_mark (priv->buffer, mark);
1457 empathy_chat_text_view_append_spacing (EmpathyChatTextView *view)
1459 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1462 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1463 gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
1467 EMPATHY_CHAT_TEXT_VIEW_TAG_CUT,
1468 EMPATHY_CHAT_TEXT_VIEW_TAG_SPACING,
1473 empathy_chat_text_view_tag_set (EmpathyChatTextView *view,
1474 const gchar *tag_name,
1475 const gchar *first_property_name,
1478 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1480 GtkTextTagTable *table;
1483 g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), NULL);
1484 g_return_val_if_fail (tag_name != NULL, NULL);
1486 table = gtk_text_buffer_get_tag_table (priv->buffer);
1487 tag = gtk_text_tag_table_lookup (table, tag_name);
1489 if (tag && first_property_name) {
1490 va_start (list, first_property_name);
1491 g_object_set_valist (G_OBJECT (tag), first_property_name, list);