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 priv->last_timestamp = timestamp;
447 g_string_free (str, TRUE);
451 chat_text_maybe_append_date_and_time (EmpathyChatTextView *view,
454 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
455 GDate *date, *last_date;
456 gboolean append_date = FALSE;
457 gboolean append_time = FALSE;
459 /* Get the date from last message */
460 last_date = g_date_new ();
461 g_date_set_time_t (last_date, priv->last_timestamp);
463 /* Get the date of the message we are appending */
464 date = g_date_new ();
465 g_date_set_time_t (date, timestamp);
467 /* If last message was from another day we append date and time */
468 if (g_date_compare (date, last_date) > 0) {
473 g_date_free (last_date);
476 /* If last message is 'old' append the time */
477 if (timestamp - priv->last_timestamp >= TIMESTAMP_INTERVAL) {
481 if (append_date || (!priv->only_if_date && append_time)) {
482 chat_text_view_append_timestamp (view, timestamp, append_date);
487 chat_text_view_size_allocate (GtkWidget *widget,
488 GtkAllocation *alloc)
492 down = chat_text_view_is_scrolled_down (EMPATHY_CHAT_TEXT_VIEW (widget));
494 GTK_WIDGET_CLASS (empathy_chat_text_view_parent_class)->size_allocate (widget, alloc);
499 adj = GTK_TEXT_VIEW (widget)->vadjustment;
500 gtk_adjustment_set_value (adj,
501 gtk_adjustment_get_upper (adj) -
502 gtk_adjustment_get_page_size (adj));
507 chat_text_view_drag_motion (GtkWidget *widget,
508 GdkDragContext *context,
513 /* Don't handle drag motion, since we don't want the view to scroll as
514 * the result of dragging something across it. */
520 chat_text_view_get_property (GObject *object,
525 EmpathyChatTextViewPriv *priv = GET_PRIV (object);
528 case PROP_LAST_CONTACT:
529 g_value_set_object (value, priv->last_contact);
531 case PROP_ONLY_IF_DATE:
532 g_value_set_boolean (value, priv->only_if_date);
535 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
541 chat_text_view_set_property (GObject *object,
546 EmpathyChatTextViewPriv *priv = GET_PRIV (object);
549 case PROP_ONLY_IF_DATE:
550 priv->only_if_date = g_value_get_boolean (value);
553 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
559 chat_text_view_finalize (GObject *object)
561 EmpathyChatTextView *view;
562 EmpathyChatTextViewPriv *priv;
564 view = EMPATHY_CHAT_TEXT_VIEW (object);
565 priv = GET_PRIV (view);
567 DEBUG ("%p", object);
569 empathy_conf_notify_remove (empathy_conf_get (), priv->notify_system_fonts_id);
571 if (priv->last_contact) {
572 g_object_unref (priv->last_contact);
574 if (priv->scroll_time) {
575 g_timer_destroy (priv->scroll_time);
577 if (priv->scroll_timeout) {
578 g_source_remove (priv->scroll_timeout);
580 g_object_unref (priv->smiley_manager);
582 G_OBJECT_CLASS (empathy_chat_text_view_parent_class)->finalize (object);
586 text_view_copy_clipboard (GtkTextView *text_view)
588 chat_text_view_copy_clipboard (EMPATHY_CHAT_VIEW (text_view));
592 empathy_chat_text_view_class_init (EmpathyChatTextViewClass *klass)
594 GObjectClass *object_class = G_OBJECT_CLASS (klass);
595 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
596 GtkTextViewClass *text_view_class = GTK_TEXT_VIEW_CLASS (klass);
598 object_class->finalize = chat_text_view_finalize;
599 object_class->get_property = chat_text_view_get_property;
600 object_class->set_property = chat_text_view_set_property;
602 widget_class->size_allocate = chat_text_view_size_allocate;
603 widget_class->drag_motion = chat_text_view_drag_motion;
605 text_view_class->copy_clipboard = text_view_copy_clipboard;
607 g_object_class_install_property (object_class,
609 g_param_spec_object ("last-contact",
611 "The sender of the last received message",
612 EMPATHY_TYPE_CONTACT,
614 g_object_class_install_property (object_class,
616 g_param_spec_boolean ("only-if-date",
618 "Display timestamp only if the date changes",
623 g_type_class_add_private (object_class, sizeof (EmpathyChatTextViewPriv));
627 empathy_chat_text_view_init (EmpathyChatTextView *view)
629 EmpathyChatTextViewPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (view,
630 EMPATHY_TYPE_CHAT_TEXT_VIEW, EmpathyChatTextViewPriv);
633 priv->buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
634 priv->last_timestamp = 0;
635 priv->allow_scrolling = TRUE;
636 priv->smiley_manager = empathy_smiley_manager_dup_singleton ();
639 "wrap-mode", GTK_WRAP_WORD_CHAR,
641 "cursor-visible", FALSE,
644 priv->notify_system_fonts_id =
645 empathy_conf_notify_add (empathy_conf_get (),
646 "/desktop/gnome/interface/document_font_name",
647 chat_text_view_notify_system_font_cb,
649 chat_text_view_system_font_update (view);
650 chat_text_view_create_tags (view);
652 g_signal_connect (view,
654 G_CALLBACK (chat_text_view_populate_popup),
658 /* Code stolen from pidgin/gtkimhtml.c */
660 chat_text_view_scroll_cb (EmpathyChatTextView *view)
662 EmpathyChatTextViewPriv *priv;
666 priv = GET_PRIV (view);
667 adj = GTK_TEXT_VIEW (view)->vadjustment;
668 max_val = gtk_adjustment_get_upper (adj) - gtk_adjustment_get_page_size (adj);
670 g_return_val_if_fail (priv->scroll_time != NULL, FALSE);
672 if (g_timer_elapsed (priv->scroll_time, NULL) > MAX_SCROLL_TIME) {
673 /* time's up. jump to the end and kill the timer */
674 gtk_adjustment_set_value (adj, max_val);
675 g_timer_destroy (priv->scroll_time);
676 priv->scroll_time = NULL;
677 priv->scroll_timeout = 0;
681 /* scroll by 1/3rd the remaining distance */
682 gtk_adjustment_set_value (adj, gtk_adjustment_get_value (adj) + ((max_val - gtk_adjustment_get_value (adj)) / 3));
687 chat_text_view_scroll_down (EmpathyChatView *view)
689 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
691 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
693 if (!priv->allow_scrolling) {
697 DEBUG ("Scrolling down");
699 if (priv->scroll_time) {
700 g_timer_reset (priv->scroll_time);
702 priv->scroll_time = g_timer_new ();
704 if (!priv->scroll_timeout) {
705 priv->scroll_timeout = g_timeout_add (SCROLL_DELAY,
706 (GSourceFunc) chat_text_view_scroll_cb,
712 chat_text_view_append_message (EmpathyChatView *view,
715 EmpathyChatTextView *text_view = EMPATHY_CHAT_TEXT_VIEW (view);
716 EmpathyChatTextViewPriv *priv = GET_PRIV (text_view);
720 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
721 g_return_if_fail (EMPATHY_IS_MESSAGE (msg));
723 if (!empathy_message_get_body (msg)) {
727 bottom = chat_text_view_is_scrolled_down (text_view);
729 chat_text_view_maybe_trim_buffer (EMPATHY_CHAT_TEXT_VIEW (view));
731 timestamp = empathy_message_get_timestamp (msg);
732 chat_text_maybe_append_date_and_time (text_view, timestamp);
733 if (EMPATHY_CHAT_TEXT_VIEW_GET_CLASS (view)->append_message) {
734 EMPATHY_CHAT_TEXT_VIEW_GET_CLASS (view)->append_message (text_view,
739 chat_text_view_scroll_down (view);
742 if (priv->last_contact) {
743 g_object_unref (priv->last_contact);
745 priv->last_contact = g_object_ref (empathy_message_get_sender (msg));
746 g_object_notify (G_OBJECT (view), "last-contact");
750 chat_text_view_append_event (EmpathyChatView *view,
753 EmpathyChatTextView *text_view = EMPATHY_CHAT_TEXT_VIEW (view);
754 EmpathyChatTextViewPriv *priv = GET_PRIV (text_view);
760 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
761 g_return_if_fail (!EMP_STR_EMPTY (str));
763 bottom = chat_text_view_is_scrolled_down (text_view);
764 chat_text_view_maybe_trim_buffer (EMPATHY_CHAT_TEXT_VIEW (view));
765 chat_text_maybe_append_date_and_time (text_view,
766 empathy_time_get_current ());
768 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
769 msg = g_strdup_printf (" - %s\n", str);
770 gtk_text_buffer_insert_with_tags_by_name (priv->buffer, &iter,
772 EMPATHY_CHAT_TEXT_VIEW_TAG_EVENT,
777 chat_text_view_scroll_down (view);
780 if (priv->last_contact) {
781 g_object_unref (priv->last_contact);
782 priv->last_contact = NULL;
783 g_object_notify (G_OBJECT (view), "last-contact");
788 chat_text_view_scroll (EmpathyChatView *view,
789 gboolean allow_scrolling)
791 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
793 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
795 DEBUG ("Scrolling %s", allow_scrolling ? "enabled" : "disabled");
797 priv->allow_scrolling = allow_scrolling;
798 if (allow_scrolling) {
799 empathy_chat_view_scroll_down (view);
804 chat_text_view_get_has_selection (EmpathyChatView *view)
806 GtkTextBuffer *buffer;
808 g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), FALSE);
810 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
812 return gtk_text_buffer_get_has_selection (buffer);
816 chat_text_view_clear (EmpathyChatView *view)
818 GtkTextBuffer *buffer;
819 EmpathyChatTextViewPriv *priv;
821 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
823 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
824 gtk_text_buffer_set_text (buffer, "", -1);
826 /* We set these back to the initial values so we get
827 * timestamps when clearing the window to know when
828 * conversations start.
830 priv = GET_PRIV (view);
832 priv->last_timestamp = 0;
833 if (priv->last_contact) {
834 g_object_unref (priv->last_contact);
835 priv->last_contact = NULL;
840 chat_text_view_find_previous (EmpathyChatView *view,
841 const gchar *search_criteria,
845 EmpathyChatTextViewPriv *priv;
846 GtkTextBuffer *buffer;
847 GtkTextIter iter_at_mark;
848 GtkTextIter iter_match_start;
849 GtkTextIter iter_match_end;
851 gboolean from_start = FALSE;
853 g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), FALSE);
854 g_return_val_if_fail (search_criteria != NULL, FALSE);
856 priv = GET_PRIV (view);
858 buffer = priv->buffer;
860 if (EMP_STR_EMPTY (search_criteria)) {
861 if (priv->find_mark_previous) {
862 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
864 gtk_text_buffer_move_mark (buffer,
865 priv->find_mark_previous,
867 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
868 priv->find_mark_previous,
873 gtk_text_buffer_select_range (buffer,
885 if (!new_search && priv->find_mark_previous) {
886 gtk_text_buffer_get_iter_at_mark (buffer,
888 priv->find_mark_previous);
890 gtk_text_buffer_get_end_iter (buffer, &iter_at_mark);
894 priv->find_last_direction = FALSE;
896 /* Use the standard GTK+ method for case sensitive searches. It can't do
897 * case insensitive searches (see bug #61852), so keep the custom method
898 * around for case insensitive searches. */
900 found = gtk_text_iter_backward_search (&iter_at_mark,
902 0, /* no text search flags, we want exact matches */
907 found = empathy_text_iter_backward_search (&iter_at_mark,
915 gboolean result = FALSE;
921 /* Here we wrap around. */
922 if (!new_search && !priv->find_wrapped) {
923 priv->find_wrapped = TRUE;
924 result = chat_text_view_find_previous (view,
928 priv->find_wrapped = FALSE;
934 /* Set new mark and show on screen */
935 if (!priv->find_mark_previous) {
936 priv->find_mark_previous = gtk_text_buffer_create_mark (buffer, NULL,
940 gtk_text_buffer_move_mark (buffer,
941 priv->find_mark_previous,
945 if (!priv->find_mark_next) {
946 priv->find_mark_next = gtk_text_buffer_create_mark (buffer, NULL,
950 gtk_text_buffer_move_mark (buffer,
951 priv->find_mark_next,
955 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
956 priv->find_mark_previous,
962 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &iter_match_start);
963 gtk_text_buffer_move_mark_by_name (buffer, "insert", &iter_match_end);
969 chat_text_view_find_next (EmpathyChatView *view,
970 const gchar *search_criteria,
974 EmpathyChatTextViewPriv *priv;
975 GtkTextBuffer *buffer;
976 GtkTextIter iter_at_mark;
977 GtkTextIter iter_match_start;
978 GtkTextIter iter_match_end;
980 gboolean from_start = FALSE;
982 g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), FALSE);
983 g_return_val_if_fail (search_criteria != NULL, FALSE);
985 priv = GET_PRIV (view);
987 buffer = priv->buffer;
989 if (EMP_STR_EMPTY (search_criteria)) {
990 if (priv->find_mark_next) {
991 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
993 gtk_text_buffer_move_mark (buffer,
994 priv->find_mark_next,
996 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
997 priv->find_mark_next,
1002 gtk_text_buffer_select_range (buffer,
1014 if (!new_search && priv->find_mark_next) {
1015 gtk_text_buffer_get_iter_at_mark (buffer,
1017 priv->find_mark_next);
1019 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1023 priv->find_last_direction = TRUE;
1025 /* Use the standard GTK+ method for case sensitive searches. It can't do
1026 * case insensitive searches (see bug #61852), so keep the custom method
1027 * around for case insensitive searches. */
1029 found = gtk_text_iter_forward_search (&iter_at_mark,
1036 found = empathy_text_iter_forward_search (&iter_at_mark,
1044 gboolean result = FALSE;
1050 /* Here we wrap around. */
1051 if (!new_search && !priv->find_wrapped) {
1052 priv->find_wrapped = TRUE;
1053 result = chat_text_view_find_next (view,
1057 priv->find_wrapped = FALSE;
1063 /* Set new mark and show on screen */
1064 if (!priv->find_mark_next) {
1065 priv->find_mark_next = gtk_text_buffer_create_mark (buffer, NULL,
1069 gtk_text_buffer_move_mark (buffer,
1070 priv->find_mark_next,
1074 if (!priv->find_mark_previous) {
1075 priv->find_mark_previous = gtk_text_buffer_create_mark (buffer, NULL,
1079 gtk_text_buffer_move_mark (buffer,
1080 priv->find_mark_previous,
1084 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
1085 priv->find_mark_next,
1091 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &iter_match_start);
1092 gtk_text_buffer_move_mark_by_name (buffer, "insert", &iter_match_end);
1098 chat_text_view_find_abilities (EmpathyChatView *view,
1099 const gchar *search_criteria,
1100 gboolean match_case,
1101 gboolean *can_do_previous,
1102 gboolean *can_do_next)
1104 EmpathyChatTextViewPriv *priv;
1105 GtkTextBuffer *buffer;
1106 GtkTextIter iter_at_mark;
1107 GtkTextIter iter_match_start;
1108 GtkTextIter iter_match_end;
1110 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
1111 g_return_if_fail (search_criteria != NULL);
1112 g_return_if_fail (can_do_previous != NULL && can_do_next != NULL);
1114 priv = GET_PRIV (view);
1116 buffer = priv->buffer;
1118 if (can_do_previous) {
1119 if (priv->find_mark_previous) {
1120 gtk_text_buffer_get_iter_at_mark (buffer,
1122 priv->find_mark_previous);
1124 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1128 *can_do_previous = gtk_text_iter_backward_search (&iter_at_mark,
1135 *can_do_previous = empathy_text_iter_backward_search (&iter_at_mark,
1144 if (priv->find_mark_next) {
1145 gtk_text_buffer_get_iter_at_mark (buffer,
1147 priv->find_mark_next);
1149 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1153 *can_do_next = gtk_text_iter_forward_search (&iter_at_mark,
1160 *can_do_next = empathy_text_iter_forward_search (&iter_at_mark,
1170 chat_text_view_highlight (EmpathyChatView *view,
1172 gboolean match_case)
1174 GtkTextBuffer *buffer;
1176 GtkTextIter iter_start;
1177 GtkTextIter iter_end;
1178 GtkTextIter iter_match_start;
1179 GtkTextIter iter_match_end;
1182 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
1184 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
1186 gtk_text_buffer_get_start_iter (buffer, &iter);
1188 gtk_text_buffer_get_bounds (buffer, &iter_start, &iter_end);
1189 gtk_text_buffer_remove_tag_by_name (buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_HIGHLIGHT,
1193 if (EMP_STR_EMPTY (text)) {
1199 found = gtk_text_iter_forward_search (&iter,
1206 found = empathy_text_iter_forward_search (&iter,
1216 gtk_text_buffer_apply_tag_by_name (buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_HIGHLIGHT,
1220 iter = iter_match_end;
1225 chat_text_view_copy_clipboard (EmpathyChatView *view)
1227 GtkTextBuffer *buffer;
1228 GtkTextIter start, iter, end;
1229 GtkClipboard *clipboard;
1232 GtkTextChildAnchor *anchor = NULL;
1235 gboolean ignore_newlines = FALSE;
1237 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
1239 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
1240 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1242 if (!gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
1245 str = g_string_new ("");
1247 for (iter = start; !gtk_text_iter_equal (&iter, &end); gtk_text_iter_forward_char (&iter)) {
1248 c = gtk_text_iter_get_char (&iter);
1249 /* 0xFFFC is the 'object replacement' unicode character,
1250 * it indicates the presence of a pixbuf or a widget. */
1252 ignore_newlines = FALSE;
1253 if ((pixbuf = gtk_text_iter_get_pixbuf (&iter))) {
1255 text = g_object_get_data (G_OBJECT(pixbuf),
1258 str = g_string_append (str, text);
1259 } else if ((anchor = gtk_text_iter_get_child_anchor (&iter))) {
1261 list = gtk_text_child_anchor_get_widgets (anchor);
1263 text = g_object_get_data (G_OBJECT(list->data),
1266 str = g_string_append (str, text);
1270 } else if (c == '\n') {
1271 if (!ignore_newlines) {
1272 ignore_newlines = TRUE;
1273 str = g_string_append_unichar (str, c);
1276 ignore_newlines = FALSE;
1277 str = g_string_append_unichar (str, c);
1281 gtk_clipboard_set_text (clipboard, str->str, str->len);
1282 g_string_free (str, TRUE);
1286 chat_text_view_iface_init (EmpathyChatViewIface *iface)
1288 iface->append_message = chat_text_view_append_message;
1289 iface->append_event = chat_text_view_append_event;
1290 iface->scroll = chat_text_view_scroll;
1291 iface->scroll_down = chat_text_view_scroll_down;
1292 iface->get_has_selection = chat_text_view_get_has_selection;
1293 iface->clear = chat_text_view_clear;
1294 iface->find_previous = chat_text_view_find_previous;
1295 iface->find_next = chat_text_view_find_next;
1296 iface->find_abilities = chat_text_view_find_abilities;
1297 iface->highlight = chat_text_view_highlight;
1298 iface->copy_clipboard = chat_text_view_copy_clipboard;
1302 empathy_chat_text_view_get_last_contact (EmpathyChatTextView *view)
1304 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1306 g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), NULL);
1308 return priv->last_contact;
1312 empathy_chat_text_view_set_only_if_date (EmpathyChatTextView *view,
1313 gboolean only_if_date)
1315 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1317 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
1319 if (only_if_date != priv->only_if_date) {
1320 priv->only_if_date = only_if_date;
1321 g_object_notify (G_OBJECT (view), "only-if-date");
1326 chat_text_view_replace_link (const gchar *text,
1328 gpointer match_data,
1331 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (user_data);
1334 gtk_text_buffer_get_end_iter (buffer, &iter);
1335 gtk_text_buffer_insert_with_tags_by_name (buffer, &iter,
1337 EMPATHY_CHAT_TEXT_VIEW_TAG_LINK,
1342 chat_text_view_replace_smiley (const gchar *text,
1344 gpointer match_data,
1347 EmpathySmileyHit *hit = match_data;
1348 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (user_data);
1351 gtk_text_buffer_get_end_iter (buffer, &iter);
1352 gtk_text_buffer_insert_pixbuf (buffer, &iter, hit->pixbuf);
1356 chat_text_view_replace_verbatim (const gchar *text,
1358 gpointer match_data,
1361 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (user_data);
1364 gtk_text_buffer_get_end_iter (buffer, &iter);
1365 gtk_text_buffer_insert (buffer, &iter, text, len);
1368 static EmpathyStringParser string_parsers[] = {
1369 {empathy_string_match_link, chat_text_view_replace_link},
1370 {empathy_string_match_all, chat_text_view_replace_verbatim},
1374 static EmpathyStringParser string_parsers_with_smiley[] = {
1375 {empathy_string_match_link, chat_text_view_replace_link},
1376 {empathy_string_match_smiley, chat_text_view_replace_smiley},
1377 {empathy_string_match_all, chat_text_view_replace_verbatim},
1382 empathy_chat_text_view_append_body (EmpathyChatTextView *view,
1386 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1387 EmpathyStringParser *parsers;
1388 gboolean use_smileys;
1389 GtkTextIter start_iter;
1393 /* Check if we have to parse smileys */
1394 empathy_conf_get_bool (empathy_conf_get (),
1395 EMPATHY_PREFS_CHAT_SHOW_SMILEYS,
1398 parsers = string_parsers_with_smiley;
1400 parsers = string_parsers;
1402 /* Create a mark at the place we'll start inserting */
1403 gtk_text_buffer_get_end_iter (priv->buffer, &start_iter);
1404 mark = gtk_text_buffer_create_mark (priv->buffer, NULL, &start_iter, TRUE);
1406 /* Parse text for links/smileys and insert in the buffer */
1407 empathy_string_parser_substr (body, -1, parsers, priv->buffer);
1409 /* Insert a newline after the text inserted */
1410 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1411 gtk_text_buffer_insert (priv->buffer, &iter, "\n", 1);
1413 /* Apply the style to the inserted text. */
1414 gtk_text_buffer_get_iter_at_mark (priv->buffer, &start_iter, mark);
1415 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1416 gtk_text_buffer_apply_tag_by_name (priv->buffer, tag,
1420 gtk_text_buffer_delete_mark (priv->buffer, mark);
1424 empathy_chat_text_view_append_spacing (EmpathyChatTextView *view)
1426 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1429 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1430 gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
1434 EMPATHY_CHAT_TEXT_VIEW_TAG_CUT,
1435 EMPATHY_CHAT_TEXT_VIEW_TAG_SPACING,
1440 empathy_chat_text_view_tag_set (EmpathyChatTextView *view,
1441 const gchar *tag_name,
1442 const gchar *first_property_name,
1445 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1447 GtkTextTagTable *table;
1450 g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), NULL);
1451 g_return_val_if_fail (tag_name != NULL, NULL);
1453 table = gtk_text_buffer_get_tag_table (priv->buffer);
1454 tag = gtk_text_tag_table_lookup (table, tag_name);
1456 if (tag && first_property_name) {
1457 va_start (list, first_property_name);
1458 g_object_set_valist (G_OBJECT (tag), first_property_name, list);