1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2002-2007 Imendio AB
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public
16 * License along with this program; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Authors: Mikael Hallendal <micke@imendio.com>
21 * Richard Hult <richard@imendio.com>
22 * Martyn Russell <martyn@imendio.com>
27 #include <sys/types.h>
31 #include <glib/gi18n.h>
32 #include <gtk/gtkbutton.h>
33 #include <gtk/gtkimage.h>
34 #include <gtk/gtkmenu.h>
35 #include <gtk/gtkmenuitem.h>
36 #include <gtk/gtkimagemenuitem.h>
37 #include <gtk/gtkstock.h>
38 #include <gtk/gtkscrolledwindow.h>
39 #include <gtk/gtksizegroup.h>
40 #include <glade/glade.h>
42 #include <telepathy-glib/util.h>
43 #include <libmissioncontrol/mc-account.h>
45 #include <libempathy/empathy-utils.h>
46 #include <libempathy/empathy-debug.h>
48 #include "empathy-chat-view.h"
49 #include "empathy-chat.h"
50 #include "empathy-conf.h"
51 #include "empathy-preferences.h"
52 #include "empathy-theme-manager.h"
53 #include "empathy-ui-utils.h"
54 #include "empathy-smiley-manager.h"
56 #define DEBUG_DOMAIN "ChatView"
58 /* Number of seconds between timestamps when using normal mode, 5 minutes. */
59 #define TIMESTAMP_INTERVAL 300
62 #define MAX_SCROLL_TIME 0.4 /* seconds */
63 #define SCROLL_DELAY 33 /* milliseconds */
65 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), EMPATHY_TYPE_CHAT_VIEW, EmpathyChatViewPriv))
67 struct _EmpathyChatViewPriv {
68 GtkTextBuffer *buffer;
72 time_t last_timestamp;
73 EmpathyChatViewBlock last_block_type;
75 gboolean allow_scrolling;
78 gboolean is_group_chat;
80 GtkTextMark *find_mark_previous;
81 GtkTextMark *find_mark_next;
82 gboolean find_wrapped;
83 gboolean find_last_direction;
85 /* This is for the group chat so we know if the "other" last contact
86 * changed, so we know whether to insert a header or not.
88 EmpathyContact *last_contact;
90 guint notify_system_fonts_id;
91 guint notify_show_avatars_id;
94 static void empathy_chat_view_class_init (EmpathyChatViewClass *klass);
95 static void empathy_chat_view_init (EmpathyChatView *view);
96 static void chat_view_finalize (GObject *object);
97 static gboolean chat_view_drag_motion (GtkWidget *widget,
98 GdkDragContext *context,
102 static void chat_view_size_allocate (GtkWidget *widget,
103 GtkAllocation *alloc);
104 static void chat_view_setup_tags (EmpathyChatView *view);
105 static void chat_view_system_font_update (EmpathyChatView *view);
106 static void chat_view_notify_system_font_cb (EmpathyConf *conf,
109 static void chat_view_notify_show_avatars_cb (EmpathyConf *conf,
112 static void chat_view_populate_popup (EmpathyChatView *view,
115 static gboolean chat_view_event_cb (EmpathyChatView *view,
116 GdkEventMotion *event,
118 static gboolean chat_view_url_event_cb (GtkTextTag *tag,
122 GtkTextBuffer *buffer);
123 static void chat_view_open_address_cb (GtkMenuItem *menuitem,
125 static void chat_view_copy_address_cb (GtkMenuItem *menuitem,
127 static void chat_view_clear_view_cb (GtkMenuItem *menuitem,
128 EmpathyChatView *view);
129 static gboolean chat_view_is_scrolled_down (EmpathyChatView *view);
130 static void chat_view_theme_changed_cb (EmpathyThemeManager *manager,
131 EmpathyChatView *view);
132 static void chat_view_theme_notify_cb (EmpathyTheme *theme,
134 EmpathyChatView *view);
136 G_DEFINE_TYPE (EmpathyChatView, empathy_chat_view, GTK_TYPE_TEXT_VIEW);
139 empathy_chat_view_class_init (EmpathyChatViewClass *klass)
141 GObjectClass *object_class = G_OBJECT_CLASS (klass);
142 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
144 object_class->finalize = chat_view_finalize;
145 widget_class->size_allocate = chat_view_size_allocate;
146 widget_class->drag_motion = chat_view_drag_motion;
148 g_type_class_add_private (object_class, sizeof (EmpathyChatViewPriv));
152 empathy_chat_view_init (EmpathyChatView *view)
154 EmpathyChatViewPriv *priv;
155 gboolean show_avatars;
157 priv = GET_PRIV (view);
159 priv->buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
161 priv->last_block_type = EMPATHY_CHAT_VIEW_BLOCK_NONE;
162 priv->last_timestamp = 0;
164 priv->allow_scrolling = TRUE;
166 priv->is_group_chat = FALSE;
169 "wrap-mode", GTK_WRAP_WORD_CHAR,
171 "cursor-visible", FALSE,
174 priv->notify_system_fonts_id =
175 empathy_conf_notify_add (empathy_conf_get (),
176 "/desktop/gnome/interface/document_font_name",
177 chat_view_notify_system_font_cb,
179 chat_view_system_font_update (view);
181 priv->notify_show_avatars_id =
182 empathy_conf_notify_add (empathy_conf_get (),
183 EMPATHY_PREFS_UI_SHOW_AVATARS,
184 chat_view_notify_show_avatars_cb,
187 chat_view_setup_tags (view);
189 empathy_theme_manager_apply_saved (empathy_theme_manager_get (), view);
191 show_avatars = FALSE;
192 empathy_conf_get_bool (empathy_conf_get (),
193 EMPATHY_PREFS_UI_SHOW_AVATARS,
196 empathy_theme_set_show_avatars (priv->theme, show_avatars);
198 g_signal_connect (view,
200 G_CALLBACK (chat_view_populate_popup),
203 g_signal_connect_object (empathy_theme_manager_get (),
205 G_CALLBACK (chat_view_theme_changed_cb),
211 chat_view_finalize (GObject *object)
213 EmpathyChatView *view;
214 EmpathyChatViewPriv *priv;
216 view = EMPATHY_CHAT_VIEW (object);
217 priv = GET_PRIV (view);
219 empathy_debug (DEBUG_DOMAIN, "finalize: %p", object);
221 empathy_conf_notify_remove (empathy_conf_get (), priv->notify_system_fonts_id);
222 empathy_conf_notify_remove (empathy_conf_get (), priv->notify_show_avatars_id);
224 if (priv->last_contact) {
225 g_object_unref (priv->last_contact);
227 if (priv->scroll_time) {
228 g_timer_destroy (priv->scroll_time);
230 if (priv->scroll_timeout) {
231 g_source_remove (priv->scroll_timeout);
235 g_signal_handlers_disconnect_by_func (priv->theme,
236 chat_view_theme_notify_cb,
238 g_object_unref (priv->theme);
241 G_OBJECT_CLASS (empathy_chat_view_parent_class)->finalize (object);
245 chat_view_drag_motion (GtkWidget *widget,
246 GdkDragContext *context,
251 /* Don't handle drag motion, since we don't want the view to scroll as
252 * the result of dragging something across it.
259 chat_view_size_allocate (GtkWidget *widget,
260 GtkAllocation *alloc)
264 down = chat_view_is_scrolled_down (EMPATHY_CHAT_VIEW (widget));
266 GTK_WIDGET_CLASS (empathy_chat_view_parent_class)->size_allocate (widget, alloc);
271 adj = GTK_TEXT_VIEW (widget)->vadjustment;
272 gtk_adjustment_set_value (adj, adj->upper - adj->page_size);
277 chat_view_setup_tags (EmpathyChatView *view)
279 EmpathyChatViewPriv *priv;
282 priv = GET_PRIV (view);
284 gtk_text_buffer_create_tag (priv->buffer,
288 /* FIXME: Move to the theme and come up with something that looks a bit
291 gtk_text_buffer_create_tag (priv->buffer,
293 "background", "yellow",
296 tag = gtk_text_buffer_create_tag (priv->buffer,
300 g_signal_connect (tag,
302 G_CALLBACK (chat_view_url_event_cb),
305 g_signal_connect (view,
306 "motion-notify-event",
307 G_CALLBACK (chat_view_event_cb),
312 chat_view_system_font_update (EmpathyChatView *view)
314 PangoFontDescription *font_description = NULL;
317 if (empathy_conf_get_string (empathy_conf_get (),
318 "/desktop/gnome/interface/document_font_name",
319 &font_name) && font_name) {
320 font_description = pango_font_description_from_string (font_name);
323 font_description = NULL;
326 gtk_widget_modify_font (GTK_WIDGET (view), font_description);
328 if (font_description) {
329 pango_font_description_free (font_description);
334 chat_view_notify_system_font_cb (EmpathyConf *conf,
338 EmpathyChatView *view;
339 EmpathyChatViewPriv *priv;
340 gboolean show_avatars = FALSE;
343 priv = GET_PRIV (view);
345 chat_view_system_font_update (view);
347 /* Ugly, again, to adjust the vertical position of the nick... Will fix
348 * this when reworking the theme manager so that view register
349 * themselves with it instead of the other way around.
351 empathy_conf_get_bool (conf,
352 EMPATHY_PREFS_UI_SHOW_AVATARS,
355 empathy_theme_set_show_avatars (priv->theme, show_avatars);
359 chat_view_notify_show_avatars_cb (EmpathyConf *conf,
363 EmpathyChatView *view;
364 EmpathyChatViewPriv *priv;
365 gboolean show_avatars = FALSE;
368 priv = GET_PRIV (view);
370 empathy_conf_get_bool (conf, key, &show_avatars);
372 empathy_theme_set_show_avatars (priv->theme, show_avatars);
376 chat_view_populate_popup (EmpathyChatView *view,
380 EmpathyChatViewPriv *priv;
381 GtkTextTagTable *table;
384 GtkTextIter iter, start, end;
388 priv = GET_PRIV (view);
390 /* Clear menu item */
391 if (gtk_text_buffer_get_char_count (priv->buffer) > 0) {
392 item = gtk_menu_item_new ();
393 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
394 gtk_widget_show (item);
396 item = gtk_image_menu_item_new_from_stock (GTK_STOCK_CLEAR, NULL);
397 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
398 gtk_widget_show (item);
400 g_signal_connect (item,
402 G_CALLBACK (chat_view_clear_view_cb),
406 /* Link context menu items */
407 table = gtk_text_buffer_get_tag_table (priv->buffer);
408 tag = gtk_text_tag_table_lookup (table, "link");
410 gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
412 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
413 GTK_TEXT_WINDOW_WIDGET,
417 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &iter, x, y);
421 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
422 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
423 str = gtk_text_buffer_get_text (priv->buffer,
424 &start, &end, FALSE);
427 if (G_STR_EMPTY (str)) {
432 /* NOTE: Set data just to get the string freed when not needed. */
433 g_object_set_data_full (G_OBJECT (menu),
435 (GDestroyNotify) g_free);
437 item = gtk_menu_item_new ();
438 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
439 gtk_widget_show (item);
441 item = gtk_menu_item_new_with_mnemonic (_("_Copy Link Address"));
442 g_signal_connect (item,
444 G_CALLBACK (chat_view_copy_address_cb),
446 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
447 gtk_widget_show (item);
449 item = gtk_menu_item_new_with_mnemonic (_("_Open Link"));
450 g_signal_connect (item,
452 G_CALLBACK (chat_view_open_address_cb),
454 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
455 gtk_widget_show (item);
459 chat_view_event_cb (EmpathyChatView *view,
460 GdkEventMotion *event,
463 static GdkCursor *hand = NULL;
464 static GdkCursor *beam = NULL;
465 GtkTextWindowType type;
468 gint x, y, buf_x, buf_y;
470 type = gtk_text_view_get_window_type (GTK_TEXT_VIEW (view),
473 if (type != GTK_TEXT_WINDOW_TEXT) {
477 /* Get where the pointer really is. */
478 win = gtk_text_view_get_window (GTK_TEXT_VIEW (view), type);
483 gdk_window_get_pointer (win, &x, &y, NULL);
485 /* Get the iter where the cursor is at */
486 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view), type,
490 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view),
494 if (gtk_text_iter_has_tag (&iter, tag)) {
496 hand = gdk_cursor_new (GDK_HAND2);
497 beam = gdk_cursor_new (GDK_XTERM);
499 gdk_window_set_cursor (win, hand);
502 beam = gdk_cursor_new (GDK_XTERM);
504 gdk_window_set_cursor (win, beam);
511 chat_view_url_event_cb (GtkTextTag *tag,
515 GtkTextBuffer *buffer)
517 GtkTextIter start, end;
520 /* If the link is being selected, don't do anything. */
521 gtk_text_buffer_get_selection_bounds (buffer, &start, &end);
522 if (gtk_text_iter_get_offset (&start) != gtk_text_iter_get_offset (&end)) {
526 if (event->type == GDK_BUTTON_RELEASE && event->button.button == 1) {
529 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
530 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
531 str = gtk_text_buffer_get_text (buffer,
536 empathy_url_show (str);
545 chat_view_open_address_cb (GtkMenuItem *menuitem, const gchar *url)
547 empathy_url_show (url);
551 chat_view_copy_address_cb (GtkMenuItem *menuitem, const gchar *url)
553 GtkClipboard *clipboard;
555 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
556 gtk_clipboard_set_text (clipboard, url, -1);
558 clipboard = gtk_clipboard_get (GDK_SELECTION_PRIMARY);
559 gtk_clipboard_set_text (clipboard, url, -1);
563 chat_view_clear_view_cb (GtkMenuItem *menuitem, EmpathyChatView *view)
565 empathy_chat_view_clear (view);
569 chat_view_is_scrolled_down (EmpathyChatView *view)
573 sw = gtk_widget_get_parent (GTK_WIDGET (view));
574 if (GTK_IS_SCROLLED_WINDOW (sw)) {
577 vadj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (sw));
579 if (vadj->value + vadj->page_size / 2 < vadj->upper - vadj->page_size) {
588 chat_view_maybe_trim_buffer (EmpathyChatView *view)
590 EmpathyChatViewPriv *priv;
591 GtkTextIter top, bottom;
594 GtkTextTagTable *table;
597 priv = GET_PRIV (view);
599 gtk_text_buffer_get_end_iter (priv->buffer, &bottom);
600 line = gtk_text_iter_get_line (&bottom);
601 if (line < MAX_LINES) {
605 remove = line - MAX_LINES;
606 gtk_text_buffer_get_start_iter (priv->buffer, &top);
609 if (!gtk_text_iter_forward_lines (&bottom, remove)) {
613 /* Track backwords to a place where we can safely cut, we don't do it in
614 * the middle of a tag.
616 table = gtk_text_buffer_get_tag_table (priv->buffer);
617 tag = gtk_text_tag_table_lookup (table, "cut");
622 if (!gtk_text_iter_forward_to_tag_toggle (&bottom, tag)) {
626 if (!gtk_text_iter_equal (&top, &bottom)) {
627 gtk_text_buffer_delete (priv->buffer, &top, &bottom);
632 chat_view_theme_changed_cb (EmpathyThemeManager *manager,
633 EmpathyChatView *view)
635 EmpathyChatViewPriv *priv;
636 gboolean show_avatars = FALSE;
637 gboolean theme_rooms = FALSE;
639 priv = GET_PRIV (view);
641 priv->last_block_type = EMPATHY_CHAT_VIEW_BLOCK_NONE;
643 empathy_conf_get_bool (empathy_conf_get (),
644 EMPATHY_PREFS_CHAT_THEME_CHAT_ROOM,
646 if (!theme_rooms && priv->is_group_chat) {
647 empathy_theme_manager_apply (manager, view, NULL);
649 empathy_theme_manager_apply_saved (manager, view);
652 /* Needed for now to update the "rise" property of the names to get it
653 * vertically centered.
655 empathy_conf_get_bool (empathy_conf_get (),
656 EMPATHY_PREFS_UI_SHOW_AVATARS,
658 empathy_theme_set_show_avatars (priv->theme, show_avatars);
661 /* Pads a pixbuf to the specified size, by centering it in a larger transparent
662 * pixbuf. Returns a new ref.
665 chat_view_pad_to_size (GdkPixbuf *pixbuf,
668 gint extra_padding_right)
670 gint src_width, src_height;
672 gint x_offset, y_offset;
674 src_width = gdk_pixbuf_get_width (pixbuf);
675 src_height = gdk_pixbuf_get_height (pixbuf);
677 x_offset = (width - src_width) / 2;
678 y_offset = (height - src_height) / 2;
680 padded = gdk_pixbuf_new (gdk_pixbuf_get_colorspace (pixbuf),
682 gdk_pixbuf_get_bits_per_sample (pixbuf),
683 width + extra_padding_right,
686 gdk_pixbuf_fill (padded, 0);
688 gdk_pixbuf_copy_area (pixbuf,
689 0, /* source coords */
694 x_offset, /* dest coords */
706 chat_view_avatar_cache_data_free (gpointer ptr)
708 AvatarData *data = ptr;
710 g_object_unref (data->pixbuf);
711 g_free (data->token);
712 g_slice_free (AvatarData, data);
716 empathy_chat_view_get_avatar_pixbuf_with_cache (EmpathyContact *contact)
718 static GHashTable *avatar_cache = NULL;
720 EmpathyAvatar *avatar;
721 GdkPixbuf *tmp_pixbuf;
722 GdkPixbuf *pixbuf = NULL;
724 /* Init avatar cache */
726 avatar_cache = g_hash_table_new_full (empathy_contact_hash,
727 empathy_contact_equal,
729 chat_view_avatar_cache_data_free);
732 /* Check if avatar is in cache and if it's up to date */
733 avatar = empathy_contact_get_avatar (contact);
734 data = g_hash_table_lookup (avatar_cache, contact);
736 if (avatar && !tp_strdiff (avatar->token, data->token)) {
737 /* We have the avatar in cache */
741 /* The cache is outdate */
742 g_hash_table_remove (avatar_cache, contact);
745 /* Avatar not in cache, create pixbuf */
746 tmp_pixbuf = empathy_pixbuf_avatar_from_contact_scaled (contact, 32, 32);
748 pixbuf = chat_view_pad_to_size (tmp_pixbuf, 32, 32, 6);
749 g_object_unref (tmp_pixbuf);
755 /* Insert new pixbuf in cache */
756 data = g_slice_new0 (AvatarData);
757 data->token = g_strdup (avatar->token);
758 data->pixbuf = pixbuf;
760 g_hash_table_insert (avatar_cache,
761 g_object_ref (contact),
768 empathy_chat_view_new (void)
770 return g_object_new (EMPATHY_TYPE_CHAT_VIEW, NULL);
774 empathy_chat_view_append_message (EmpathyChatView *view,
777 EmpathyChatViewPriv *priv = GET_PRIV (view);
778 EmpathyContact *sender;
782 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
783 g_return_if_fail (EMPATHY_IS_MESSAGE (msg));
785 if (!empathy_message_get_body (msg)) {
789 bottom = chat_view_is_scrolled_down (view);
790 sender = empathy_message_get_sender (msg);
791 from_self = empathy_contact_is_user (sender);
793 chat_view_maybe_trim_buffer (view);
795 empathy_theme_append_message (priv->theme, view, msg);
798 empathy_chat_view_scroll_down (view);
803 empathy_chat_view_append_event (EmpathyChatView *view,
806 EmpathyChatViewPriv *priv;
809 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
810 g_return_if_fail (!G_STR_EMPTY (str));
812 priv = GET_PRIV (view);
814 bottom = chat_view_is_scrolled_down (view);
816 chat_view_maybe_trim_buffer (view);
818 empathy_theme_append_event (priv->theme, view, str);
821 empathy_chat_view_scroll_down (view);
824 priv->last_block_type = EMPATHY_CHAT_VIEW_BLOCK_EVENT;
828 empathy_chat_view_append_button (EmpathyChatView *view,
829 const gchar *message,
833 EmpathyChatViewPriv *priv;
834 GtkTextChildAnchor *anchor;
839 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
840 g_return_if_fail (button1 != NULL);
842 priv = GET_PRIV (view);
846 bottom = chat_view_is_scrolled_down (view);
848 empathy_theme_append_timestamp (priv->theme, view, NULL, TRUE, TRUE);
851 empathy_theme_append_text (priv->theme, view, message, tag, NULL);
854 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
856 anchor = gtk_text_buffer_create_child_anchor (priv->buffer, &iter);
857 gtk_text_view_add_child_at_anchor (GTK_TEXT_VIEW (view), button1, anchor);
858 gtk_widget_show (button1);
860 gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
868 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
870 anchor = gtk_text_buffer_create_child_anchor (priv->buffer, &iter);
871 gtk_text_view_add_child_at_anchor (GTK_TEXT_VIEW (view), button2, anchor);
872 gtk_widget_show (button2);
874 gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
882 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
883 gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
891 empathy_chat_view_scroll_down (view);
894 priv->last_block_type = EMPATHY_CHAT_VIEW_BLOCK_INVITE;
898 empathy_chat_view_scroll (EmpathyChatView *view,
899 gboolean allow_scrolling)
901 EmpathyChatViewPriv *priv;
903 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
905 priv = GET_PRIV (view);
907 priv->allow_scrolling = allow_scrolling;
909 empathy_debug (DEBUG_DOMAIN, "Scrolling %s",
910 allow_scrolling ? "enabled" : "disabled");
913 /* Code stolen from pidgin/gtkimhtml.c */
915 chat_view_scroll_cb (EmpathyChatView *view)
917 EmpathyChatViewPriv *priv;
921 priv = GET_PRIV (view);
922 adj = GTK_TEXT_VIEW (view)->vadjustment;
923 max_val = adj->upper - adj->page_size;
925 g_return_val_if_fail (priv->scroll_time != NULL, FALSE);
927 if (g_timer_elapsed (priv->scroll_time, NULL) > MAX_SCROLL_TIME) {
928 /* time's up. jump to the end and kill the timer */
929 gtk_adjustment_set_value (adj, max_val);
930 g_timer_destroy (priv->scroll_time);
931 priv->scroll_time = NULL;
932 priv->scroll_timeout = 0;
936 /* scroll by 1/3rd the remaining distance */
937 gtk_adjustment_set_value (adj, gtk_adjustment_get_value (adj) + ((max_val - gtk_adjustment_get_value (adj)) / 3));
942 empathy_chat_view_scroll_down (EmpathyChatView *view)
944 EmpathyChatViewPriv *priv;
946 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
948 priv = GET_PRIV (view);
950 if (!priv->allow_scrolling) {
954 empathy_debug (DEBUG_DOMAIN, "Scrolling down");
956 if (priv->scroll_time) {
957 g_timer_reset (priv->scroll_time);
959 priv->scroll_time = g_timer_new();
961 if (!priv->scroll_timeout) {
962 priv->scroll_timeout = g_timeout_add (SCROLL_DELAY,
963 (GSourceFunc) chat_view_scroll_cb,
969 empathy_chat_view_get_selection_bounds (EmpathyChatView *view,
973 GtkTextBuffer *buffer;
975 g_return_val_if_fail (EMPATHY_IS_CHAT_VIEW (view), FALSE);
977 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
979 return gtk_text_buffer_get_selection_bounds (buffer, start, end);
983 empathy_chat_view_clear (EmpathyChatView *view)
985 GtkTextBuffer *buffer;
986 EmpathyChatViewPriv *priv;
988 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
990 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
991 gtk_text_buffer_set_text (buffer, "", -1);
993 /* We set these back to the initial values so we get
994 * timestamps when clearing the window to know when
995 * conversations start.
997 priv = GET_PRIV (view);
999 priv->last_block_type = EMPATHY_CHAT_VIEW_BLOCK_NONE;
1000 priv->last_timestamp = 0;
1004 empathy_chat_view_find_previous (EmpathyChatView *view,
1005 const gchar *search_criteria,
1006 gboolean new_search)
1008 EmpathyChatViewPriv *priv;
1009 GtkTextBuffer *buffer;
1010 GtkTextIter iter_at_mark;
1011 GtkTextIter iter_match_start;
1012 GtkTextIter iter_match_end;
1014 gboolean from_start = FALSE;
1016 g_return_val_if_fail (EMPATHY_IS_CHAT_VIEW (view), FALSE);
1017 g_return_val_if_fail (search_criteria != NULL, FALSE);
1019 priv = GET_PRIV (view);
1021 buffer = priv->buffer;
1023 if (G_STR_EMPTY (search_criteria)) {
1024 if (priv->find_mark_previous) {
1025 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1027 gtk_text_buffer_move_mark (buffer,
1028 priv->find_mark_previous,
1030 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
1031 priv->find_mark_previous,
1036 gtk_text_buffer_select_range (buffer,
1048 if (priv->find_mark_previous) {
1049 gtk_text_buffer_get_iter_at_mark (buffer,
1051 priv->find_mark_previous);
1053 gtk_text_buffer_get_end_iter (buffer, &iter_at_mark);
1057 priv->find_last_direction = FALSE;
1059 found = empathy_text_iter_backward_search (&iter_at_mark,
1066 gboolean result = FALSE;
1072 /* Here we wrap around. */
1073 if (!new_search && !priv->find_wrapped) {
1074 priv->find_wrapped = TRUE;
1075 result = empathy_chat_view_find_previous (view,
1078 priv->find_wrapped = FALSE;
1084 /* Set new mark and show on screen */
1085 if (!priv->find_mark_previous) {
1086 priv->find_mark_previous = gtk_text_buffer_create_mark (buffer, NULL,
1090 gtk_text_buffer_move_mark (buffer,
1091 priv->find_mark_previous,
1095 if (!priv->find_mark_next) {
1096 priv->find_mark_next = gtk_text_buffer_create_mark (buffer, NULL,
1100 gtk_text_buffer_move_mark (buffer,
1101 priv->find_mark_next,
1105 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
1106 priv->find_mark_previous,
1112 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &iter_match_start);
1113 gtk_text_buffer_move_mark_by_name (buffer, "insert", &iter_match_end);
1119 empathy_chat_view_find_next (EmpathyChatView *view,
1120 const gchar *search_criteria,
1121 gboolean new_search)
1123 EmpathyChatViewPriv *priv;
1124 GtkTextBuffer *buffer;
1125 GtkTextIter iter_at_mark;
1126 GtkTextIter iter_match_start;
1127 GtkTextIter iter_match_end;
1129 gboolean from_start = FALSE;
1131 g_return_val_if_fail (EMPATHY_IS_CHAT_VIEW (view), FALSE);
1132 g_return_val_if_fail (search_criteria != NULL, FALSE);
1134 priv = GET_PRIV (view);
1136 buffer = priv->buffer;
1138 if (G_STR_EMPTY (search_criteria)) {
1139 if (priv->find_mark_next) {
1140 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1142 gtk_text_buffer_move_mark (buffer,
1143 priv->find_mark_next,
1145 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
1146 priv->find_mark_next,
1151 gtk_text_buffer_select_range (buffer,
1163 if (priv->find_mark_next) {
1164 gtk_text_buffer_get_iter_at_mark (buffer,
1166 priv->find_mark_next);
1168 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1172 priv->find_last_direction = TRUE;
1174 found = empathy_text_iter_forward_search (&iter_at_mark,
1181 gboolean result = FALSE;
1187 /* Here we wrap around. */
1188 if (!new_search && !priv->find_wrapped) {
1189 priv->find_wrapped = TRUE;
1190 result = empathy_chat_view_find_next (view,
1193 priv->find_wrapped = FALSE;
1199 /* Set new mark and show on screen */
1200 if (!priv->find_mark_next) {
1201 priv->find_mark_next = gtk_text_buffer_create_mark (buffer, NULL,
1205 gtk_text_buffer_move_mark (buffer,
1206 priv->find_mark_next,
1210 if (!priv->find_mark_previous) {
1211 priv->find_mark_previous = gtk_text_buffer_create_mark (buffer, NULL,
1215 gtk_text_buffer_move_mark (buffer,
1216 priv->find_mark_previous,
1220 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
1221 priv->find_mark_next,
1227 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &iter_match_start);
1228 gtk_text_buffer_move_mark_by_name (buffer, "insert", &iter_match_end);
1235 empathy_chat_view_find_abilities (EmpathyChatView *view,
1236 const gchar *search_criteria,
1237 gboolean *can_do_previous,
1238 gboolean *can_do_next)
1240 EmpathyChatViewPriv *priv;
1241 GtkTextBuffer *buffer;
1242 GtkTextIter iter_at_mark;
1243 GtkTextIter iter_match_start;
1244 GtkTextIter iter_match_end;
1246 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
1247 g_return_if_fail (search_criteria != NULL);
1248 g_return_if_fail (can_do_previous != NULL && can_do_next != NULL);
1250 priv = GET_PRIV (view);
1252 buffer = priv->buffer;
1254 if (can_do_previous) {
1255 if (priv->find_mark_previous) {
1256 gtk_text_buffer_get_iter_at_mark (buffer,
1258 priv->find_mark_previous);
1260 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1263 *can_do_previous = empathy_text_iter_backward_search (&iter_at_mark,
1271 if (priv->find_mark_next) {
1272 gtk_text_buffer_get_iter_at_mark (buffer,
1274 priv->find_mark_next);
1276 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1279 *can_do_next = empathy_text_iter_forward_search (&iter_at_mark,
1288 empathy_chat_view_highlight (EmpathyChatView *view,
1291 GtkTextBuffer *buffer;
1293 GtkTextIter iter_start;
1294 GtkTextIter iter_end;
1295 GtkTextIter iter_match_start;
1296 GtkTextIter iter_match_end;
1299 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
1301 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
1303 gtk_text_buffer_get_start_iter (buffer, &iter);
1305 gtk_text_buffer_get_bounds (buffer, &iter_start, &iter_end);
1306 gtk_text_buffer_remove_tag_by_name (buffer, "highlight",
1310 if (G_STR_EMPTY (text)) {
1315 found = empathy_text_iter_forward_search (&iter,
1325 gtk_text_buffer_apply_tag_by_name (buffer, "highlight",
1329 iter = iter_match_end;
1330 gtk_text_iter_forward_char (&iter);
1335 empathy_chat_view_copy_clipboard (EmpathyChatView *view)
1337 GtkTextBuffer *buffer;
1338 GtkClipboard *clipboard;
1340 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
1342 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
1343 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1345 gtk_text_buffer_copy_clipboard (buffer, clipboard);
1349 empathy_chat_view_get_theme (EmpathyChatView *view)
1351 EmpathyChatViewPriv *priv;
1353 g_return_val_if_fail (EMPATHY_IS_CHAT_VIEW (view), NULL);
1355 priv = GET_PRIV (view);
1361 chat_view_theme_notify_cb (EmpathyTheme *theme,
1363 EmpathyChatView *view)
1365 empathy_theme_update_view (theme, view);
1369 empathy_chat_view_set_theme (EmpathyChatView *view, EmpathyTheme *theme)
1371 EmpathyChatViewPriv *priv;
1373 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
1374 g_return_if_fail (EMPATHY_IS_THEME (theme));
1376 priv = GET_PRIV (view);
1379 g_signal_handlers_disconnect_by_func (priv->theme,
1380 chat_view_theme_notify_cb,
1382 g_object_unref (priv->theme);
1385 priv->theme = g_object_ref (theme);
1387 empathy_theme_update_view (theme, view);
1388 g_signal_connect (priv->theme, "notify",
1389 G_CALLBACK (chat_view_theme_notify_cb),
1392 /* FIXME: Redraw all messages using the new theme */
1396 empathy_chat_view_set_margin (EmpathyChatView *view,
1399 EmpathyChatViewPriv *priv;
1401 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
1403 priv = GET_PRIV (view);
1406 "left-margin", margin,
1407 "right-margin", margin,
1412 empathy_chat_view_get_smiley_menu (GCallback callback,
1415 EmpathySmileyManager *smiley_manager;
1416 GSList *smileys, *l;
1421 g_return_val_if_fail (callback != NULL, NULL);
1423 menu = gtk_menu_new ();
1425 smiley_manager = empathy_smiley_manager_new ();
1426 smileys = empathy_smiley_manager_get_all (smiley_manager);
1427 for (l = smileys; l; l = l->next) {
1428 EmpathySmiley *smiley;
1433 image = gtk_image_new_from_pixbuf (smiley->pixbuf);
1435 item = gtk_image_menu_item_new_with_label ("");
1436 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1438 gtk_menu_attach (GTK_MENU (menu), item,
1439 x, x + 1, y, y + 1);
1441 gtk_widget_set_tooltip_text (item, smiley->str);
1443 g_object_set_data (G_OBJECT (item), "smiley_text", smiley->str);
1444 g_signal_connect (item, "activate", callback, user_data);
1453 g_object_unref (smiley_manager);
1455 gtk_widget_show_all (menu);
1460 /* FIXME: Do we really need this? Better to do it internally only at setup time,
1461 * we will never change it on the fly.
1464 empathy_chat_view_set_is_group_chat (EmpathyChatView *view,
1465 gboolean is_group_chat)
1467 EmpathyChatViewPriv *priv;
1468 gboolean theme_rooms = FALSE;
1470 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
1472 priv = GET_PRIV (view);
1474 priv->is_group_chat = is_group_chat;
1476 empathy_conf_get_bool (empathy_conf_get (),
1477 EMPATHY_PREFS_CHAT_THEME_CHAT_ROOM,
1480 if (!theme_rooms && is_group_chat) {
1481 empathy_theme_manager_apply (empathy_theme_manager_get (),
1485 empathy_theme_manager_apply_saved (empathy_theme_manager_get (),
1491 empathy_chat_view_get_last_timestamp (EmpathyChatView *view)
1493 EmpathyChatViewPriv *priv;
1495 g_return_val_if_fail (EMPATHY_IS_CHAT_VIEW (view), 0);
1497 priv = GET_PRIV (view);
1499 return priv->last_timestamp;
1503 empathy_chat_view_set_last_timestamp (EmpathyChatView *view,
1506 EmpathyChatViewPriv *priv;
1508 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
1510 priv = GET_PRIV (view);
1512 priv->last_timestamp = timestamp;
1515 EmpathyChatViewBlock
1516 empathy_chat_view_get_last_block_type (EmpathyChatView *view)
1518 EmpathyChatViewPriv *priv;
1520 g_return_val_if_fail (EMPATHY_IS_CHAT_VIEW (view), 0);
1522 priv = GET_PRIV (view);
1524 return priv->last_block_type;
1528 empathy_chat_view_set_last_block_type (EmpathyChatView *view,
1529 EmpathyChatViewBlock block_type)
1531 EmpathyChatViewPriv *priv;
1533 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
1535 priv = GET_PRIV (view);
1537 priv->last_block_type = block_type;
1541 empathy_chat_view_get_last_contact (EmpathyChatView *view)
1543 EmpathyChatViewPriv *priv;
1545 g_return_val_if_fail (EMPATHY_IS_CHAT_VIEW (view), NULL);
1547 priv = GET_PRIV (view);
1549 return priv->last_contact;
1553 empathy_chat_view_set_last_contact (EmpathyChatView *view, EmpathyContact *contact)
1555 EmpathyChatViewPriv *priv;
1557 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
1559 priv = GET_PRIV (view);
1561 if (priv->last_contact) {
1562 g_object_unref (priv->last_contact);
1563 priv->last_contact = NULL;
1567 priv->last_contact = g_object_ref (contact);