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>
37 #include <libmissioncontrol/mc-account.h>
39 #include <libempathy/empathy-utils.h>
41 #include "empathy-chat-text-view.h"
42 #include "empathy-chat.h"
43 #include "empathy-conf.h"
44 #include "empathy-ui-utils.h"
45 #include "empathy-smiley-manager.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 G_DEFINE_TYPE_WITH_CODE (EmpathyChatTextView, empathy_chat_text_view,
79 G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CHAT_VIEW,
80 chat_text_view_iface_init));
89 chat_text_view_url_event_cb (GtkTextTag *tag,
93 EmpathyChatTextView *view)
95 EmpathyChatTextViewPriv *priv;
96 GtkTextIter start, end;
99 priv = GET_PRIV (view);
101 /* If the link is being selected, don't do anything. */
102 gtk_text_buffer_get_selection_bounds (priv->buffer, &start, &end);
103 if (gtk_text_iter_get_offset (&start) != gtk_text_iter_get_offset (&end)) {
107 if (event->type == GDK_BUTTON_RELEASE && event->button.button == 1) {
110 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
111 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
112 str = gtk_text_buffer_get_text (priv->buffer,
117 empathy_url_show (GTK_WIDGET (view), str);
126 chat_text_view_event_cb (EmpathyChatTextView *view,
127 GdkEventMotion *event,
130 static GdkCursor *hand = NULL;
131 static GdkCursor *beam = NULL;
132 GtkTextWindowType type;
135 gint x, y, buf_x, buf_y;
137 type = gtk_text_view_get_window_type (GTK_TEXT_VIEW (view),
140 if (type != GTK_TEXT_WINDOW_TEXT) {
144 /* Get where the pointer really is. */
145 win = gtk_text_view_get_window (GTK_TEXT_VIEW (view), type);
150 gdk_window_get_pointer (win, &x, &y, NULL);
152 /* Get the iter where the cursor is at */
153 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view), type,
157 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view),
161 if (gtk_text_iter_has_tag (&iter, tag)) {
163 hand = gdk_cursor_new (GDK_HAND2);
164 beam = gdk_cursor_new (GDK_XTERM);
166 gdk_window_set_cursor (win, hand);
169 beam = gdk_cursor_new (GDK_XTERM);
171 gdk_window_set_cursor (win, beam);
178 chat_text_view_create_tags (EmpathyChatTextView *view)
180 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
183 gtk_text_buffer_create_tag (priv->buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_CUT, NULL);
184 gtk_text_buffer_create_tag (priv->buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_HIGHLIGHT, NULL);
185 gtk_text_buffer_create_tag (priv->buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_SPACING, NULL);
186 gtk_text_buffer_create_tag (priv->buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_TIME, NULL);
187 gtk_text_buffer_create_tag (priv->buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_ACTION, NULL);
188 gtk_text_buffer_create_tag (priv->buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_BODY, NULL);
189 gtk_text_buffer_create_tag (priv->buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_EVENT, NULL);
191 tag = gtk_text_buffer_create_tag (priv->buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_LINK, NULL);
192 g_signal_connect (tag, "event",
193 G_CALLBACK (chat_text_view_url_event_cb),
196 g_signal_connect (view, "motion-notify-event",
197 G_CALLBACK (chat_text_view_event_cb),
202 chat_text_view_system_font_update (EmpathyChatTextView *view)
204 PangoFontDescription *font_description = NULL;
207 if (empathy_conf_get_string (empathy_conf_get (),
208 "/desktop/gnome/interface/document_font_name",
209 &font_name) && font_name) {
210 font_description = pango_font_description_from_string (font_name);
213 font_description = NULL;
216 gtk_widget_modify_font (GTK_WIDGET (view), font_description);
218 if (font_description) {
219 pango_font_description_free (font_description);
224 chat_text_view_notify_system_font_cb (EmpathyConf *conf,
228 EmpathyChatTextView *view = user_data;
230 chat_text_view_system_font_update (view);
234 chat_text_view_open_address_cb (GtkMenuItem *menuitem, const gchar *url)
236 empathy_url_show (GTK_WIDGET (menuitem), url);
240 chat_text_view_copy_address_cb (GtkMenuItem *menuitem, const gchar *url)
242 GtkClipboard *clipboard;
244 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
245 gtk_clipboard_set_text (clipboard, url, -1);
247 clipboard = gtk_clipboard_get (GDK_SELECTION_PRIMARY);
248 gtk_clipboard_set_text (clipboard, url, -1);
252 chat_text_view_populate_popup (EmpathyChatTextView *view,
256 EmpathyChatTextViewPriv *priv;
257 GtkTextTagTable *table;
260 GtkTextIter iter, start, end;
264 priv = GET_PRIV (view);
266 /* Clear menu item */
267 if (gtk_text_buffer_get_char_count (priv->buffer) > 0) {
268 item = gtk_separator_menu_item_new ();
269 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
270 gtk_widget_show (item);
272 item = gtk_image_menu_item_new_from_stock (GTK_STOCK_CLEAR, NULL);
273 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
274 gtk_widget_show (item);
276 g_signal_connect_swapped (item, "activate",
277 G_CALLBACK (empathy_chat_view_clear),
281 /* Link context menu items */
282 table = gtk_text_buffer_get_tag_table (priv->buffer);
283 tag = gtk_text_tag_table_lookup (table, EMPATHY_CHAT_TEXT_VIEW_TAG_LINK);
285 gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
287 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
288 GTK_TEXT_WINDOW_WIDGET,
292 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &iter, x, y);
296 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
297 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
298 str = gtk_text_buffer_get_text (priv->buffer,
299 &start, &end, FALSE);
302 if (EMP_STR_EMPTY (str)) {
307 /* NOTE: Set data just to get the string freed when not needed. */
308 g_object_set_data_full (G_OBJECT (menu),
310 (GDestroyNotify) g_free);
312 item = gtk_separator_menu_item_new ();
313 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
314 gtk_widget_show (item);
316 item = gtk_menu_item_new_with_mnemonic (_("_Copy Link Address"));
317 g_signal_connect (item, "activate",
318 G_CALLBACK (chat_text_view_copy_address_cb),
320 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
321 gtk_widget_show (item);
323 item = gtk_menu_item_new_with_mnemonic (_("_Open Link"));
324 g_signal_connect (item, "activate",
325 G_CALLBACK (chat_text_view_open_address_cb),
327 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
328 gtk_widget_show (item);
332 chat_text_view_is_scrolled_down (EmpathyChatTextView *view)
336 sw = gtk_widget_get_parent (GTK_WIDGET (view));
337 if (GTK_IS_SCROLLED_WINDOW (sw)) {
340 vadj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (sw));
342 if (vadj->value + vadj->page_size / 2 < vadj->upper - vadj->page_size) {
351 chat_text_view_maybe_trim_buffer (EmpathyChatTextView *view)
353 EmpathyChatTextViewPriv *priv;
354 GtkTextIter top, bottom;
357 GtkTextTagTable *table;
360 priv = GET_PRIV (view);
362 gtk_text_buffer_get_end_iter (priv->buffer, &bottom);
363 line = gtk_text_iter_get_line (&bottom);
364 if (line < MAX_LINES) {
368 remove = line - MAX_LINES;
369 gtk_text_buffer_get_start_iter (priv->buffer, &top);
372 if (!gtk_text_iter_forward_lines (&bottom, remove)) {
376 /* Track backwords to a place where we can safely cut, we don't do it in
377 * the middle of a tag.
379 table = gtk_text_buffer_get_tag_table (priv->buffer);
380 tag = gtk_text_tag_table_lookup (table, EMPATHY_CHAT_TEXT_VIEW_TAG_CUT);
385 if (!gtk_text_iter_forward_to_tag_toggle (&bottom, tag)) {
389 if (!gtk_text_iter_equal (&top, &bottom)) {
390 gtk_text_buffer_delete (priv->buffer, &top, &bottom);
395 chat_text_view_append_timestamp (EmpathyChatTextView *view,
399 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
404 str = g_string_new ("- ");
406 /* Append date if needed */
411 date = g_date_new ();
412 g_date_set_time_t (date, timestamp);
413 /* Translators: timestamp displayed between conversations in
414 * chat windows (strftime format string) */
415 g_date_strftime (buf, 256, _("%A %B %d %Y"), date);
416 g_string_append (str, buf);
417 g_string_append (str, ", ");
422 tmp = empathy_time_to_string_local (timestamp, EMPATHY_TIME_FORMAT_DISPLAY_SHORT);
423 g_string_append (str, tmp);
426 g_string_append (str, " -\n");
428 /* Insert the string in the buffer */
429 empathy_chat_text_view_append_spacing (view);
430 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
431 gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
434 EMPATHY_CHAT_TEXT_VIEW_TAG_TIME,
437 priv->last_timestamp = timestamp;
439 g_string_free (str, TRUE);
443 chat_text_maybe_append_date_and_time (EmpathyChatTextView *view,
446 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
447 GDate *date, *last_date;
448 gboolean append_date = FALSE;
449 gboolean append_time = FALSE;
451 /* Get the date from last message */
452 last_date = g_date_new ();
453 g_date_set_time_t (last_date, priv->last_timestamp);
455 /* Get the date of the message we are appending */
456 date = g_date_new ();
457 g_date_set_time_t (date, timestamp);
459 /* If last message was from another day we append date and time */
460 if (g_date_compare (date, last_date) > 0) {
465 g_date_free (last_date);
468 /* If last message is 'old' append the time */
469 if (timestamp - priv->last_timestamp >= 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_TEXT_VIEW (widget)->vadjustment;
492 gtk_adjustment_set_value (adj, adj->upper - adj->page_size);
497 chat_text_view_drag_motion (GtkWidget *widget,
498 GdkDragContext *context,
503 /* Don't handle drag motion, since we don't want the view to scroll as
504 * the result of dragging something across it. */
510 chat_text_view_get_property (GObject *object,
515 EmpathyChatTextViewPriv *priv = GET_PRIV (object);
518 case PROP_LAST_CONTACT:
519 g_value_set_object (value, priv->last_contact);
521 case PROP_ONLY_IF_DATE:
522 g_value_set_boolean (value, priv->only_if_date);
525 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
531 chat_text_view_set_property (GObject *object,
536 EmpathyChatTextViewPriv *priv = GET_PRIV (object);
539 case PROP_ONLY_IF_DATE:
540 priv->only_if_date = g_value_get_boolean (value);
543 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
549 chat_text_view_finalize (GObject *object)
551 EmpathyChatTextView *view;
552 EmpathyChatTextViewPriv *priv;
554 view = EMPATHY_CHAT_TEXT_VIEW (object);
555 priv = GET_PRIV (view);
557 DEBUG ("%p", object);
559 empathy_conf_notify_remove (empathy_conf_get (), priv->notify_system_fonts_id);
561 if (priv->last_contact) {
562 g_object_unref (priv->last_contact);
564 if (priv->scroll_time) {
565 g_timer_destroy (priv->scroll_time);
567 if (priv->scroll_timeout) {
568 g_source_remove (priv->scroll_timeout);
570 g_object_unref (priv->smiley_manager);
572 G_OBJECT_CLASS (empathy_chat_text_view_parent_class)->finalize (object);
576 empathy_chat_text_view_class_init (EmpathyChatTextViewClass *klass)
578 GObjectClass *object_class = G_OBJECT_CLASS (klass);
579 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
581 object_class->finalize = chat_text_view_finalize;
582 object_class->get_property = chat_text_view_get_property;
583 object_class->set_property = chat_text_view_set_property;
585 widget_class->size_allocate = chat_text_view_size_allocate;
586 widget_class->drag_motion = chat_text_view_drag_motion;
588 g_object_class_install_property (object_class,
590 g_param_spec_object ("last-contact",
592 "The sender of the last received message",
593 EMPATHY_TYPE_CONTACT,
595 g_object_class_install_property (object_class,
597 g_param_spec_boolean ("only-if-date",
599 "Display timestamp only if the date changes",
604 g_type_class_add_private (object_class, sizeof (EmpathyChatTextViewPriv));
608 empathy_chat_text_view_init (EmpathyChatTextView *view)
610 EmpathyChatTextViewPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (view,
611 EMPATHY_TYPE_CHAT_TEXT_VIEW, EmpathyChatTextViewPriv);
614 priv->buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
615 priv->last_timestamp = 0;
616 priv->allow_scrolling = TRUE;
617 priv->smiley_manager = empathy_smiley_manager_dup_singleton ();
620 "wrap-mode", GTK_WRAP_WORD_CHAR,
622 "cursor-visible", FALSE,
625 priv->notify_system_fonts_id =
626 empathy_conf_notify_add (empathy_conf_get (),
627 "/desktop/gnome/interface/document_font_name",
628 chat_text_view_notify_system_font_cb,
630 chat_text_view_system_font_update (view);
631 chat_text_view_create_tags (view);
633 g_signal_connect (view,
635 G_CALLBACK (chat_text_view_populate_popup),
639 /* Code stolen from pidgin/gtkimhtml.c */
641 chat_text_view_scroll_cb (EmpathyChatTextView *view)
643 EmpathyChatTextViewPriv *priv;
647 priv = GET_PRIV (view);
648 adj = GTK_TEXT_VIEW (view)->vadjustment;
649 max_val = adj->upper - adj->page_size;
651 g_return_val_if_fail (priv->scroll_time != NULL, FALSE);
653 if (g_timer_elapsed (priv->scroll_time, NULL) > MAX_SCROLL_TIME) {
654 /* time's up. jump to the end and kill the timer */
655 gtk_adjustment_set_value (adj, max_val);
656 g_timer_destroy (priv->scroll_time);
657 priv->scroll_time = NULL;
658 priv->scroll_timeout = 0;
662 /* scroll by 1/3rd the remaining distance */
663 gtk_adjustment_set_value (adj, gtk_adjustment_get_value (adj) + ((max_val - gtk_adjustment_get_value (adj)) / 3));
668 chat_text_view_scroll_down (EmpathyChatView *view)
670 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
672 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
674 if (!priv->allow_scrolling) {
678 DEBUG ("Scrolling down");
680 if (priv->scroll_time) {
681 g_timer_reset (priv->scroll_time);
683 priv->scroll_time = g_timer_new ();
685 if (!priv->scroll_timeout) {
686 priv->scroll_timeout = g_timeout_add (SCROLL_DELAY,
687 (GSourceFunc) chat_text_view_scroll_cb,
693 chat_text_view_append_message (EmpathyChatView *view,
696 EmpathyChatTextView *text_view = EMPATHY_CHAT_TEXT_VIEW (view);
697 EmpathyChatTextViewPriv *priv = GET_PRIV (text_view);
701 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
702 g_return_if_fail (EMPATHY_IS_MESSAGE (msg));
704 if (!empathy_message_get_body (msg)) {
708 bottom = chat_text_view_is_scrolled_down (text_view);
710 chat_text_view_maybe_trim_buffer (EMPATHY_CHAT_TEXT_VIEW (view));
712 timestamp = empathy_message_get_timestamp (msg);
713 chat_text_maybe_append_date_and_time (text_view, timestamp);
714 if (EMPATHY_CHAT_TEXT_VIEW_GET_CLASS (view)->append_message) {
715 EMPATHY_CHAT_TEXT_VIEW_GET_CLASS (view)->append_message (text_view,
720 chat_text_view_scroll_down (view);
723 if (priv->last_contact) {
724 g_object_unref (priv->last_contact);
726 priv->last_contact = g_object_ref (empathy_message_get_sender (msg));
727 g_object_notify (G_OBJECT (view), "last-contact");
731 chat_text_view_append_event (EmpathyChatView *view,
734 EmpathyChatTextView *text_view = EMPATHY_CHAT_TEXT_VIEW (view);
735 EmpathyChatTextViewPriv *priv = GET_PRIV (text_view);
741 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
742 g_return_if_fail (!EMP_STR_EMPTY (str));
744 bottom = chat_text_view_is_scrolled_down (text_view);
745 chat_text_view_maybe_trim_buffer (EMPATHY_CHAT_TEXT_VIEW (view));
746 chat_text_maybe_append_date_and_time (text_view,
747 empathy_time_get_current ());
749 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
750 msg = g_strdup_printf (" - %s\n", str);
751 gtk_text_buffer_insert_with_tags_by_name (priv->buffer, &iter,
753 EMPATHY_CHAT_TEXT_VIEW_TAG_EVENT,
758 chat_text_view_scroll_down (view);
761 if (priv->last_contact) {
762 g_object_unref (priv->last_contact);
763 priv->last_contact = NULL;
764 g_object_notify (G_OBJECT (view), "last-contact");
769 chat_text_view_scroll (EmpathyChatView *view,
770 gboolean allow_scrolling)
772 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
774 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
776 DEBUG ("Scrolling %s", allow_scrolling ? "enabled" : "disabled");
778 priv->allow_scrolling = allow_scrolling;
779 if (allow_scrolling) {
780 empathy_chat_view_scroll_down (view);
785 chat_text_view_get_has_selection (EmpathyChatView *view)
787 GtkTextBuffer *buffer;
789 g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), FALSE);
791 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
793 return gtk_text_buffer_get_has_selection (buffer);
797 chat_text_view_clear (EmpathyChatView *view)
799 GtkTextBuffer *buffer;
800 EmpathyChatTextViewPriv *priv;
802 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
804 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
805 gtk_text_buffer_set_text (buffer, "", -1);
807 /* We set these back to the initial values so we get
808 * timestamps when clearing the window to know when
809 * conversations start.
811 priv = GET_PRIV (view);
813 priv->last_timestamp = 0;
817 chat_text_view_find_previous (EmpathyChatView *view,
818 const gchar *search_criteria,
821 EmpathyChatTextViewPriv *priv;
822 GtkTextBuffer *buffer;
823 GtkTextIter iter_at_mark;
824 GtkTextIter iter_match_start;
825 GtkTextIter iter_match_end;
827 gboolean from_start = FALSE;
829 g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), FALSE);
830 g_return_val_if_fail (search_criteria != NULL, FALSE);
832 priv = GET_PRIV (view);
834 buffer = priv->buffer;
836 if (EMP_STR_EMPTY (search_criteria)) {
837 if (priv->find_mark_previous) {
838 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
840 gtk_text_buffer_move_mark (buffer,
841 priv->find_mark_previous,
843 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
844 priv->find_mark_previous,
849 gtk_text_buffer_select_range (buffer,
861 if (priv->find_mark_previous) {
862 gtk_text_buffer_get_iter_at_mark (buffer,
864 priv->find_mark_previous);
866 gtk_text_buffer_get_end_iter (buffer, &iter_at_mark);
870 priv->find_last_direction = FALSE;
872 found = empathy_text_iter_backward_search (&iter_at_mark,
879 gboolean result = FALSE;
885 /* Here we wrap around. */
886 if (!new_search && !priv->find_wrapped) {
887 priv->find_wrapped = TRUE;
888 result = chat_text_view_find_previous (view,
891 priv->find_wrapped = FALSE;
897 /* Set new mark and show on screen */
898 if (!priv->find_mark_previous) {
899 priv->find_mark_previous = gtk_text_buffer_create_mark (buffer, NULL,
903 gtk_text_buffer_move_mark (buffer,
904 priv->find_mark_previous,
908 if (!priv->find_mark_next) {
909 priv->find_mark_next = gtk_text_buffer_create_mark (buffer, NULL,
913 gtk_text_buffer_move_mark (buffer,
914 priv->find_mark_next,
918 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
919 priv->find_mark_previous,
925 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &iter_match_start);
926 gtk_text_buffer_move_mark_by_name (buffer, "insert", &iter_match_end);
932 chat_text_view_find_next (EmpathyChatView *view,
933 const gchar *search_criteria,
936 EmpathyChatTextViewPriv *priv;
937 GtkTextBuffer *buffer;
938 GtkTextIter iter_at_mark;
939 GtkTextIter iter_match_start;
940 GtkTextIter iter_match_end;
942 gboolean from_start = FALSE;
944 g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), FALSE);
945 g_return_val_if_fail (search_criteria != NULL, FALSE);
947 priv = GET_PRIV (view);
949 buffer = priv->buffer;
951 if (EMP_STR_EMPTY (search_criteria)) {
952 if (priv->find_mark_next) {
953 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
955 gtk_text_buffer_move_mark (buffer,
956 priv->find_mark_next,
958 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
959 priv->find_mark_next,
964 gtk_text_buffer_select_range (buffer,
976 if (priv->find_mark_next) {
977 gtk_text_buffer_get_iter_at_mark (buffer,
979 priv->find_mark_next);
981 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
985 priv->find_last_direction = TRUE;
987 found = empathy_text_iter_forward_search (&iter_at_mark,
994 gboolean result = FALSE;
1000 /* Here we wrap around. */
1001 if (!new_search && !priv->find_wrapped) {
1002 priv->find_wrapped = TRUE;
1003 result = chat_text_view_find_next (view,
1006 priv->find_wrapped = FALSE;
1012 /* Set new mark and show on screen */
1013 if (!priv->find_mark_next) {
1014 priv->find_mark_next = gtk_text_buffer_create_mark (buffer, NULL,
1018 gtk_text_buffer_move_mark (buffer,
1019 priv->find_mark_next,
1023 if (!priv->find_mark_previous) {
1024 priv->find_mark_previous = gtk_text_buffer_create_mark (buffer, NULL,
1028 gtk_text_buffer_move_mark (buffer,
1029 priv->find_mark_previous,
1033 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
1034 priv->find_mark_next,
1040 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &iter_match_start);
1041 gtk_text_buffer_move_mark_by_name (buffer, "insert", &iter_match_end);
1047 chat_text_view_find_abilities (EmpathyChatView *view,
1048 const gchar *search_criteria,
1049 gboolean *can_do_previous,
1050 gboolean *can_do_next)
1052 EmpathyChatTextViewPriv *priv;
1053 GtkTextBuffer *buffer;
1054 GtkTextIter iter_at_mark;
1055 GtkTextIter iter_match_start;
1056 GtkTextIter iter_match_end;
1058 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
1059 g_return_if_fail (search_criteria != NULL);
1060 g_return_if_fail (can_do_previous != NULL && can_do_next != NULL);
1062 priv = GET_PRIV (view);
1064 buffer = priv->buffer;
1066 if (can_do_previous) {
1067 if (priv->find_mark_previous) {
1068 gtk_text_buffer_get_iter_at_mark (buffer,
1070 priv->find_mark_previous);
1072 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1075 *can_do_previous = empathy_text_iter_backward_search (&iter_at_mark,
1083 if (priv->find_mark_next) {
1084 gtk_text_buffer_get_iter_at_mark (buffer,
1086 priv->find_mark_next);
1088 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1091 *can_do_next = empathy_text_iter_forward_search (&iter_at_mark,
1100 chat_text_view_highlight (EmpathyChatView *view,
1103 GtkTextBuffer *buffer;
1105 GtkTextIter iter_start;
1106 GtkTextIter iter_end;
1107 GtkTextIter iter_match_start;
1108 GtkTextIter iter_match_end;
1111 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
1113 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
1115 gtk_text_buffer_get_start_iter (buffer, &iter);
1117 gtk_text_buffer_get_bounds (buffer, &iter_start, &iter_end);
1118 gtk_text_buffer_remove_tag_by_name (buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_HIGHLIGHT,
1122 if (EMP_STR_EMPTY (text)) {
1127 found = empathy_text_iter_forward_search (&iter,
1137 gtk_text_buffer_apply_tag_by_name (buffer, EMPATHY_CHAT_TEXT_VIEW_TAG_HIGHLIGHT,
1141 iter = iter_match_end;
1142 gtk_text_iter_forward_char (&iter);
1147 chat_text_view_copy_clipboard (EmpathyChatView *view)
1149 GtkTextBuffer *buffer;
1150 GtkClipboard *clipboard;
1152 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
1154 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
1155 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1157 gtk_text_buffer_copy_clipboard (buffer, clipboard);
1161 chat_text_view_iface_init (EmpathyChatViewIface *iface)
1163 iface->append_message = chat_text_view_append_message;
1164 iface->append_event = chat_text_view_append_event;
1165 iface->scroll = chat_text_view_scroll;
1166 iface->scroll_down = chat_text_view_scroll_down;
1167 iface->get_has_selection = chat_text_view_get_has_selection;
1168 iface->clear = chat_text_view_clear;
1169 iface->find_previous = chat_text_view_find_previous;
1170 iface->find_next = chat_text_view_find_next;
1171 iface->find_abilities = chat_text_view_find_abilities;
1172 iface->highlight = chat_text_view_highlight;
1173 iface->copy_clipboard = chat_text_view_copy_clipboard;
1177 empathy_chat_text_view_get_last_contact (EmpathyChatTextView *view)
1179 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1181 g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), NULL);
1183 return priv->last_contact;
1187 empathy_chat_text_view_set_only_if_date (EmpathyChatTextView *view,
1188 gboolean only_if_date)
1190 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1192 g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
1194 if (only_if_date != priv->only_if_date) {
1195 priv->only_if_date = only_if_date;
1196 g_object_notify (G_OBJECT (view), "only-if-date");
1201 chat_text_view_insert_text_with_emoticons (EmpathyChatTextView *view,
1205 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1206 gboolean use_smileys = FALSE;
1207 GSList *smileys, *l;
1209 empathy_conf_get_bool (empathy_conf_get (),
1210 EMPATHY_PREFS_CHAT_SHOW_SMILEYS,
1214 gtk_text_buffer_insert (priv->buffer, iter, str, -1);
1218 smileys = empathy_smiley_manager_parse (priv->smiley_manager, str);
1219 for (l = smileys; l; l = l->next) {
1220 EmpathySmiley *smiley;
1223 if (smiley->pixbuf) {
1224 gtk_text_buffer_insert_pixbuf (priv->buffer, iter, smiley->pixbuf);
1226 gtk_text_buffer_insert (priv->buffer, iter, smiley->str, -1);
1228 empathy_smiley_free (smiley);
1230 g_slist_free (smileys);
1234 empathy_chat_text_view_append_body (EmpathyChatTextView *view,
1238 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1239 GtkTextIter start_iter, end_iter;
1243 GMatchInfo *match_info;
1249 priv = GET_PRIV (view);
1251 gtk_text_buffer_get_end_iter (priv->buffer, &start_iter);
1252 mark = gtk_text_buffer_create_mark (priv->buffer, NULL, &start_iter, TRUE);
1254 uri_regex = empathy_uri_regex_dup_singleton ();
1255 for (match = g_regex_match (uri_regex, body, 0, &match_info); match;
1256 match = g_match_info_next (match_info, NULL)) {
1257 if (!g_match_info_fetch_pos (match_info, 0, &s, &e))
1261 tmp = empathy_substring (body, last, s);
1263 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1264 chat_text_view_insert_text_with_emoticons (view,
1270 tmp = empathy_substring (body, s, e);
1272 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1273 gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
1277 EMPATHY_CHAT_TEXT_VIEW_TAG_LINK,
1283 g_match_info_free (match_info);
1284 g_regex_unref (uri_regex);
1286 if (last < strlen (body)) {
1287 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1288 chat_text_view_insert_text_with_emoticons (view,
1293 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1294 gtk_text_buffer_insert (priv->buffer, &iter, "\n", 1);
1296 /* Apply the style to the inserted text. */
1297 gtk_text_buffer_get_iter_at_mark (priv->buffer, &start_iter, mark);
1298 gtk_text_buffer_get_end_iter (priv->buffer, &end_iter);
1300 gtk_text_buffer_apply_tag_by_name (priv->buffer,
1305 gtk_text_buffer_delete_mark (priv->buffer, mark);
1309 empathy_chat_text_view_append_spacing (EmpathyChatTextView *view)
1311 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1314 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1315 gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
1319 EMPATHY_CHAT_TEXT_VIEW_TAG_CUT,
1320 EMPATHY_CHAT_TEXT_VIEW_TAG_SPACING,
1325 empathy_chat_text_view_tag_set (EmpathyChatTextView *view,
1326 const gchar *tag_name,
1327 const gchar *first_property_name,
1330 EmpathyChatTextViewPriv *priv = GET_PRIV (view);
1332 GtkTextTagTable *table;
1335 g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), NULL);
1336 g_return_val_if_fail (tag_name != NULL, NULL);
1338 table = gtk_text_buffer_get_tag_table (priv->buffer);
1339 tag = gtk_text_tag_table_lookup (table, tag_name);
1341 if (tag && first_property_name) {
1342 va_start (list, first_property_name);
1343 g_object_set_valist (G_OBJECT (tag), first_property_name, list);