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>
47 #include <libempathy/empathy-conf.h>
49 #include "empathy-chat-view.h"
50 #include "empathy-chat.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;
71 gpointer theme_context;
73 time_t last_timestamp;
74 BlockType last_block_type;
76 gboolean allow_scrolling;
79 gboolean is_group_chat;
81 GtkTextMark *find_mark_previous;
82 GtkTextMark *find_mark_next;
83 gboolean find_wrapped;
84 gboolean find_last_direction;
86 /* This is for the group chat so we know if the "other" last contact
87 * changed, so we know whether to insert a header or not.
89 EmpathyContact *last_contact;
91 guint notify_system_fonts_id;
92 guint notify_show_avatars_id;
95 static void empathy_chat_view_class_init (EmpathyChatViewClass *klass);
96 static void empathy_chat_view_init (EmpathyChatView *view);
97 static void chat_view_finalize (GObject *object);
98 static gboolean chat_view_drag_motion (GtkWidget *widget,
99 GdkDragContext *context,
103 static void chat_view_size_allocate (GtkWidget *widget,
104 GtkAllocation *alloc);
105 static void chat_view_setup_tags (EmpathyChatView *view);
106 static void chat_view_system_font_update (EmpathyChatView *view);
107 static void chat_view_notify_system_font_cb (EmpathyConf *conf,
110 static void chat_view_notify_show_avatars_cb (EmpathyConf *conf,
113 static void chat_view_populate_popup (EmpathyChatView *view,
116 static gboolean chat_view_event_cb (EmpathyChatView *view,
117 GdkEventMotion *event,
119 static gboolean chat_view_url_event_cb (GtkTextTag *tag,
123 GtkTextBuffer *buffer);
124 static void chat_view_open_address_cb (GtkMenuItem *menuitem,
126 static void chat_view_copy_address_cb (GtkMenuItem *menuitem,
128 static void chat_view_clear_view_cb (GtkMenuItem *menuitem,
129 EmpathyChatView *view);
130 static gboolean chat_view_is_scrolled_down (EmpathyChatView *view);
131 static void chat_view_theme_changed_cb (EmpathyThemeManager *manager,
132 EmpathyChatView *view);
133 static void chat_view_theme_updated_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 = BLOCK_TYPE_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_updated_cb,
239 empathy_theme_detach_from_view (priv->theme, priv->theme_context,
242 g_object_unref (priv->theme);
245 G_OBJECT_CLASS (empathy_chat_view_parent_class)->finalize (object);
249 chat_view_drag_motion (GtkWidget *widget,
250 GdkDragContext *context,
255 /* Don't handle drag motion, since we don't want the view to scroll as
256 * the result of dragging something across it.
263 chat_view_size_allocate (GtkWidget *widget,
264 GtkAllocation *alloc)
268 down = chat_view_is_scrolled_down (EMPATHY_CHAT_VIEW (widget));
270 GTK_WIDGET_CLASS (empathy_chat_view_parent_class)->size_allocate (widget, alloc);
275 adj = GTK_TEXT_VIEW (widget)->vadjustment;
276 gtk_adjustment_set_value (adj, adj->upper - adj->page_size);
281 chat_view_setup_tags (EmpathyChatView *view)
283 EmpathyChatViewPriv *priv;
286 priv = GET_PRIV (view);
288 gtk_text_buffer_create_tag (priv->buffer,
292 /* FIXME: Move to the theme and come up with something that looks a bit
295 gtk_text_buffer_create_tag (priv->buffer,
297 "background", "yellow",
300 tag = gtk_text_buffer_create_tag (priv->buffer,
304 g_signal_connect (tag,
306 G_CALLBACK (chat_view_url_event_cb),
309 g_signal_connect (view,
310 "motion-notify-event",
311 G_CALLBACK (chat_view_event_cb),
316 chat_view_system_font_update (EmpathyChatView *view)
318 PangoFontDescription *font_description = NULL;
321 if (empathy_conf_get_string (empathy_conf_get (),
322 "/desktop/gnome/interface/document_font_name",
323 &font_name) && font_name) {
324 font_description = pango_font_description_from_string (font_name);
327 font_description = NULL;
330 gtk_widget_modify_font (GTK_WIDGET (view), font_description);
332 if (font_description) {
333 pango_font_description_free (font_description);
338 chat_view_notify_system_font_cb (EmpathyConf *conf,
342 EmpathyChatView *view;
343 EmpathyChatViewPriv *priv;
344 gboolean show_avatars = FALSE;
347 priv = GET_PRIV (view);
349 chat_view_system_font_update (view);
351 /* Ugly, again, to adjust the vertical position of the nick... Will fix
352 * this when reworking the theme manager so that view register
353 * themselves with it instead of the other way around.
355 empathy_conf_get_bool (conf,
356 EMPATHY_PREFS_UI_SHOW_AVATARS,
359 empathy_theme_set_show_avatars (priv->theme, show_avatars);
363 chat_view_notify_show_avatars_cb (EmpathyConf *conf,
367 EmpathyChatView *view;
368 EmpathyChatViewPriv *priv;
369 gboolean show_avatars = FALSE;
372 priv = GET_PRIV (view);
374 empathy_conf_get_bool (conf, key, &show_avatars);
376 empathy_theme_set_show_avatars (priv->theme, show_avatars);
380 chat_view_populate_popup (EmpathyChatView *view,
384 EmpathyChatViewPriv *priv;
385 GtkTextTagTable *table;
388 GtkTextIter iter, start, end;
392 priv = GET_PRIV (view);
394 /* Clear menu item */
395 if (gtk_text_buffer_get_char_count (priv->buffer) > 0) {
396 item = gtk_menu_item_new ();
397 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
398 gtk_widget_show (item);
400 item = gtk_image_menu_item_new_from_stock (GTK_STOCK_CLEAR, NULL);
401 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
402 gtk_widget_show (item);
404 g_signal_connect (item,
406 G_CALLBACK (chat_view_clear_view_cb),
410 /* Link context menu items */
411 table = gtk_text_buffer_get_tag_table (priv->buffer);
412 tag = gtk_text_tag_table_lookup (table, "link");
414 gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
416 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
417 GTK_TEXT_WINDOW_WIDGET,
421 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &iter, x, y);
425 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
426 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
427 str = gtk_text_buffer_get_text (priv->buffer,
428 &start, &end, FALSE);
431 if (G_STR_EMPTY (str)) {
436 /* NOTE: Set data just to get the string freed when not needed. */
437 g_object_set_data_full (G_OBJECT (menu),
439 (GDestroyNotify) g_free);
441 item = gtk_menu_item_new ();
442 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
443 gtk_widget_show (item);
445 item = gtk_menu_item_new_with_mnemonic (_("_Copy Link Address"));
446 g_signal_connect (item,
448 G_CALLBACK (chat_view_copy_address_cb),
450 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
451 gtk_widget_show (item);
453 item = gtk_menu_item_new_with_mnemonic (_("_Open Link"));
454 g_signal_connect (item,
456 G_CALLBACK (chat_view_open_address_cb),
458 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
459 gtk_widget_show (item);
463 chat_view_event_cb (EmpathyChatView *view,
464 GdkEventMotion *event,
467 static GdkCursor *hand = NULL;
468 static GdkCursor *beam = NULL;
469 GtkTextWindowType type;
472 gint x, y, buf_x, buf_y;
474 type = gtk_text_view_get_window_type (GTK_TEXT_VIEW (view),
477 if (type != GTK_TEXT_WINDOW_TEXT) {
481 /* Get where the pointer really is. */
482 win = gtk_text_view_get_window (GTK_TEXT_VIEW (view), type);
487 gdk_window_get_pointer (win, &x, &y, NULL);
489 /* Get the iter where the cursor is at */
490 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view), type,
494 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view),
498 if (gtk_text_iter_has_tag (&iter, tag)) {
500 hand = gdk_cursor_new (GDK_HAND2);
501 beam = gdk_cursor_new (GDK_XTERM);
503 gdk_window_set_cursor (win, hand);
506 beam = gdk_cursor_new (GDK_XTERM);
508 gdk_window_set_cursor (win, beam);
515 chat_view_url_event_cb (GtkTextTag *tag,
519 GtkTextBuffer *buffer)
521 GtkTextIter start, end;
524 /* If the link is being selected, don't do anything. */
525 gtk_text_buffer_get_selection_bounds (buffer, &start, &end);
526 if (gtk_text_iter_get_offset (&start) != gtk_text_iter_get_offset (&end)) {
530 if (event->type == GDK_BUTTON_RELEASE && event->button.button == 1) {
533 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
534 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
535 str = gtk_text_buffer_get_text (buffer,
540 empathy_url_show (str);
549 chat_view_open_address_cb (GtkMenuItem *menuitem, const gchar *url)
551 empathy_url_show (url);
555 chat_view_copy_address_cb (GtkMenuItem *menuitem, const gchar *url)
557 GtkClipboard *clipboard;
559 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
560 gtk_clipboard_set_text (clipboard, url, -1);
562 clipboard = gtk_clipboard_get (GDK_SELECTION_PRIMARY);
563 gtk_clipboard_set_text (clipboard, url, -1);
567 chat_view_clear_view_cb (GtkMenuItem *menuitem, EmpathyChatView *view)
569 empathy_chat_view_clear (view);
573 chat_view_is_scrolled_down (EmpathyChatView *view)
577 sw = gtk_widget_get_parent (GTK_WIDGET (view));
578 if (GTK_IS_SCROLLED_WINDOW (sw)) {
581 vadj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (sw));
583 if (vadj->value + vadj->page_size / 2 < vadj->upper - vadj->page_size) {
592 chat_view_maybe_trim_buffer (EmpathyChatView *view)
594 EmpathyChatViewPriv *priv;
595 GtkTextIter top, bottom;
598 GtkTextTagTable *table;
601 priv = GET_PRIV (view);
603 gtk_text_buffer_get_end_iter (priv->buffer, &bottom);
604 line = gtk_text_iter_get_line (&bottom);
605 if (line < MAX_LINES) {
609 remove = line - MAX_LINES;
610 gtk_text_buffer_get_start_iter (priv->buffer, &top);
613 if (!gtk_text_iter_forward_lines (&bottom, remove)) {
617 /* Track backwords to a place where we can safely cut, we don't do it in
618 * the middle of a tag.
620 table = gtk_text_buffer_get_tag_table (priv->buffer);
621 tag = gtk_text_tag_table_lookup (table, "cut");
626 if (!gtk_text_iter_forward_to_tag_toggle (&bottom, tag)) {
630 if (!gtk_text_iter_equal (&top, &bottom)) {
631 gtk_text_buffer_delete (priv->buffer, &top, &bottom);
636 chat_view_theme_changed_cb (EmpathyThemeManager *manager,
637 EmpathyChatView *view)
639 EmpathyChatViewPriv *priv;
640 gboolean show_avatars = FALSE;
641 gboolean theme_rooms = FALSE;
643 priv = GET_PRIV (view);
645 priv->last_block_type = BLOCK_TYPE_NONE;
647 empathy_conf_get_bool (empathy_conf_get (),
648 EMPATHY_PREFS_CHAT_THEME_CHAT_ROOM,
650 if (!theme_rooms && priv->is_group_chat) {
651 empathy_theme_manager_apply (manager, view, NULL);
653 empathy_theme_manager_apply_saved (manager, view);
656 /* Needed for now to update the "rise" property of the names to get it
657 * vertically centered.
659 empathy_conf_get_bool (empathy_conf_get (),
660 EMPATHY_PREFS_UI_SHOW_AVATARS,
662 empathy_theme_set_show_avatars (priv->theme, show_avatars);
665 /* Pads a pixbuf to the specified size, by centering it in a larger transparent
666 * pixbuf. Returns a new ref.
669 chat_view_pad_to_size (GdkPixbuf *pixbuf,
672 gint extra_padding_right)
674 gint src_width, src_height;
676 gint x_offset, y_offset;
678 src_width = gdk_pixbuf_get_width (pixbuf);
679 src_height = gdk_pixbuf_get_height (pixbuf);
681 x_offset = (width - src_width) / 2;
682 y_offset = (height - src_height) / 2;
684 padded = gdk_pixbuf_new (gdk_pixbuf_get_colorspace (pixbuf),
686 gdk_pixbuf_get_bits_per_sample (pixbuf),
687 width + extra_padding_right,
690 gdk_pixbuf_fill (padded, 0);
692 gdk_pixbuf_copy_area (pixbuf,
693 0, /* source coords */
698 x_offset, /* dest coords */
710 chat_view_avatar_cache_data_free (gpointer ptr)
712 AvatarData *data = ptr;
714 g_object_unref (data->pixbuf);
715 g_free (data->token);
716 g_slice_free (AvatarData, data);
720 empathy_chat_view_get_avatar_pixbuf_with_cache (EmpathyContact *contact)
722 static GHashTable *avatar_cache = NULL;
724 EmpathyAvatar *avatar;
725 GdkPixbuf *tmp_pixbuf;
726 GdkPixbuf *pixbuf = NULL;
728 /* Init avatar cache */
730 avatar_cache = g_hash_table_new_full (empathy_contact_hash,
731 empathy_contact_equal,
733 chat_view_avatar_cache_data_free);
736 /* Check if avatar is in cache and if it's up to date */
737 avatar = empathy_contact_get_avatar (contact);
738 data = g_hash_table_lookup (avatar_cache, contact);
740 if (avatar && !tp_strdiff (avatar->token, data->token)) {
741 /* We have the avatar in cache */
745 /* The cache is outdate */
746 g_hash_table_remove (avatar_cache, contact);
749 /* Avatar not in cache, create pixbuf */
750 tmp_pixbuf = empathy_pixbuf_avatar_from_contact_scaled (contact, 32, 32);
752 pixbuf = chat_view_pad_to_size (tmp_pixbuf, 32, 32, 6);
753 g_object_unref (tmp_pixbuf);
759 /* Insert new pixbuf in cache */
760 data = g_slice_new0 (AvatarData);
761 data->token = g_strdup (avatar->token);
762 data->pixbuf = pixbuf;
764 g_hash_table_insert (avatar_cache,
765 g_object_ref (contact),
772 empathy_chat_view_new (void)
774 return g_object_new (EMPATHY_TYPE_CHAT_VIEW, NULL);
778 empathy_chat_view_append_message (EmpathyChatView *view,
781 EmpathyChatViewPriv *priv = GET_PRIV (view);
782 EmpathyContact *sender;
786 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
787 g_return_if_fail (EMPATHY_IS_MESSAGE (msg));
789 if (!empathy_message_get_body (msg)) {
793 bottom = chat_view_is_scrolled_down (view);
794 sender = empathy_message_get_sender (msg);
795 from_self = empathy_contact_is_user (sender);
797 chat_view_maybe_trim_buffer (view);
799 empathy_theme_append_message (priv->theme, priv->theme_context,
803 empathy_chat_view_scroll_down (view);
808 empathy_chat_view_append_event (EmpathyChatView *view,
811 EmpathyChatViewPriv *priv;
814 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
815 g_return_if_fail (!G_STR_EMPTY (str));
817 priv = GET_PRIV (view);
819 bottom = chat_view_is_scrolled_down (view);
821 chat_view_maybe_trim_buffer (view);
823 empathy_theme_append_event (priv->theme,
828 empathy_chat_view_scroll_down (view);
831 priv->last_block_type = BLOCK_TYPE_EVENT;
835 empathy_chat_view_append_button (EmpathyChatView *view,
836 const gchar *message,
840 EmpathyChatViewPriv *priv;
841 GtkTextChildAnchor *anchor;
846 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
847 g_return_if_fail (button1 != NULL);
849 priv = GET_PRIV (view);
853 bottom = chat_view_is_scrolled_down (view);
855 empathy_theme_append_timestamp (priv->theme, priv->theme_context,
860 empathy_theme_append_text (priv->theme, priv->theme_context,
861 view, message, tag, NULL);
864 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
866 anchor = gtk_text_buffer_create_child_anchor (priv->buffer, &iter);
867 gtk_text_view_add_child_at_anchor (GTK_TEXT_VIEW (view), button1, anchor);
868 gtk_widget_show (button1);
870 gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
878 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
880 anchor = gtk_text_buffer_create_child_anchor (priv->buffer, &iter);
881 gtk_text_view_add_child_at_anchor (GTK_TEXT_VIEW (view), button2, anchor);
882 gtk_widget_show (button2);
884 gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
892 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
893 gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
901 empathy_chat_view_scroll_down (view);
904 priv->last_block_type = BLOCK_TYPE_INVITE;
908 empathy_chat_view_scroll (EmpathyChatView *view,
909 gboolean allow_scrolling)
911 EmpathyChatViewPriv *priv;
913 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
915 priv = GET_PRIV (view);
917 priv->allow_scrolling = allow_scrolling;
919 empathy_debug (DEBUG_DOMAIN, "Scrolling %s",
920 allow_scrolling ? "enabled" : "disabled");
923 /* Code stolen from pidgin/gtkimhtml.c */
925 chat_view_scroll_cb (EmpathyChatView *view)
927 EmpathyChatViewPriv *priv;
931 priv = GET_PRIV (view);
932 adj = GTK_TEXT_VIEW (view)->vadjustment;
933 max_val = adj->upper - adj->page_size;
935 g_return_val_if_fail (priv->scroll_time != NULL, FALSE);
937 if (g_timer_elapsed (priv->scroll_time, NULL) > MAX_SCROLL_TIME) {
938 /* time's up. jump to the end and kill the timer */
939 gtk_adjustment_set_value (adj, max_val);
940 g_timer_destroy (priv->scroll_time);
941 priv->scroll_time = NULL;
942 priv->scroll_timeout = 0;
946 /* scroll by 1/3rd the remaining distance */
947 gtk_adjustment_set_value (adj, gtk_adjustment_get_value (adj) + ((max_val - gtk_adjustment_get_value (adj)) / 3));
952 empathy_chat_view_scroll_down (EmpathyChatView *view)
954 EmpathyChatViewPriv *priv;
956 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
958 priv = GET_PRIV (view);
960 if (!priv->allow_scrolling) {
964 empathy_debug (DEBUG_DOMAIN, "Scrolling down");
966 if (priv->scroll_time) {
967 g_timer_reset (priv->scroll_time);
969 priv->scroll_time = g_timer_new();
971 if (!priv->scroll_timeout) {
972 priv->scroll_timeout = g_timeout_add (SCROLL_DELAY,
973 (GSourceFunc) chat_view_scroll_cb,
979 empathy_chat_view_get_selection_bounds (EmpathyChatView *view,
983 GtkTextBuffer *buffer;
985 g_return_val_if_fail (EMPATHY_IS_CHAT_VIEW (view), FALSE);
987 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
989 return gtk_text_buffer_get_selection_bounds (buffer, start, end);
993 empathy_chat_view_clear (EmpathyChatView *view)
995 GtkTextBuffer *buffer;
996 EmpathyChatViewPriv *priv;
998 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
1000 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
1001 gtk_text_buffer_set_text (buffer, "", -1);
1003 /* We set these back to the initial values so we get
1004 * timestamps when clearing the window to know when
1005 * conversations start.
1007 priv = GET_PRIV (view);
1009 empathy_theme_view_cleared (priv->theme, priv->theme_context, view);
1011 priv->last_block_type = BLOCK_TYPE_NONE;
1012 priv->last_timestamp = 0;
1016 empathy_chat_view_find_previous (EmpathyChatView *view,
1017 const gchar *search_criteria,
1018 gboolean new_search)
1020 EmpathyChatViewPriv *priv;
1021 GtkTextBuffer *buffer;
1022 GtkTextIter iter_at_mark;
1023 GtkTextIter iter_match_start;
1024 GtkTextIter iter_match_end;
1026 gboolean from_start = FALSE;
1028 g_return_val_if_fail (EMPATHY_IS_CHAT_VIEW (view), FALSE);
1029 g_return_val_if_fail (search_criteria != NULL, FALSE);
1031 priv = GET_PRIV (view);
1033 buffer = priv->buffer;
1035 if (G_STR_EMPTY (search_criteria)) {
1036 if (priv->find_mark_previous) {
1037 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1039 gtk_text_buffer_move_mark (buffer,
1040 priv->find_mark_previous,
1042 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
1043 priv->find_mark_previous,
1048 gtk_text_buffer_select_range (buffer,
1060 if (priv->find_mark_previous) {
1061 gtk_text_buffer_get_iter_at_mark (buffer,
1063 priv->find_mark_previous);
1065 gtk_text_buffer_get_end_iter (buffer, &iter_at_mark);
1069 priv->find_last_direction = FALSE;
1071 found = empathy_text_iter_backward_search (&iter_at_mark,
1078 gboolean result = FALSE;
1084 /* Here we wrap around. */
1085 if (!new_search && !priv->find_wrapped) {
1086 priv->find_wrapped = TRUE;
1087 result = empathy_chat_view_find_previous (view,
1090 priv->find_wrapped = FALSE;
1096 /* Set new mark and show on screen */
1097 if (!priv->find_mark_previous) {
1098 priv->find_mark_previous = gtk_text_buffer_create_mark (buffer, NULL,
1102 gtk_text_buffer_move_mark (buffer,
1103 priv->find_mark_previous,
1107 if (!priv->find_mark_next) {
1108 priv->find_mark_next = gtk_text_buffer_create_mark (buffer, NULL,
1112 gtk_text_buffer_move_mark (buffer,
1113 priv->find_mark_next,
1117 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
1118 priv->find_mark_previous,
1124 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &iter_match_start);
1125 gtk_text_buffer_move_mark_by_name (buffer, "insert", &iter_match_end);
1131 empathy_chat_view_find_next (EmpathyChatView *view,
1132 const gchar *search_criteria,
1133 gboolean new_search)
1135 EmpathyChatViewPriv *priv;
1136 GtkTextBuffer *buffer;
1137 GtkTextIter iter_at_mark;
1138 GtkTextIter iter_match_start;
1139 GtkTextIter iter_match_end;
1141 gboolean from_start = FALSE;
1143 g_return_val_if_fail (EMPATHY_IS_CHAT_VIEW (view), FALSE);
1144 g_return_val_if_fail (search_criteria != NULL, FALSE);
1146 priv = GET_PRIV (view);
1148 buffer = priv->buffer;
1150 if (G_STR_EMPTY (search_criteria)) {
1151 if (priv->find_mark_next) {
1152 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1154 gtk_text_buffer_move_mark (buffer,
1155 priv->find_mark_next,
1157 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
1158 priv->find_mark_next,
1163 gtk_text_buffer_select_range (buffer,
1175 if (priv->find_mark_next) {
1176 gtk_text_buffer_get_iter_at_mark (buffer,
1178 priv->find_mark_next);
1180 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1184 priv->find_last_direction = TRUE;
1186 found = empathy_text_iter_forward_search (&iter_at_mark,
1193 gboolean result = FALSE;
1199 /* Here we wrap around. */
1200 if (!new_search && !priv->find_wrapped) {
1201 priv->find_wrapped = TRUE;
1202 result = empathy_chat_view_find_next (view,
1205 priv->find_wrapped = FALSE;
1211 /* Set new mark and show on screen */
1212 if (!priv->find_mark_next) {
1213 priv->find_mark_next = gtk_text_buffer_create_mark (buffer, NULL,
1217 gtk_text_buffer_move_mark (buffer,
1218 priv->find_mark_next,
1222 if (!priv->find_mark_previous) {
1223 priv->find_mark_previous = gtk_text_buffer_create_mark (buffer, NULL,
1227 gtk_text_buffer_move_mark (buffer,
1228 priv->find_mark_previous,
1232 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
1233 priv->find_mark_next,
1239 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &iter_match_start);
1240 gtk_text_buffer_move_mark_by_name (buffer, "insert", &iter_match_end);
1247 empathy_chat_view_find_abilities (EmpathyChatView *view,
1248 const gchar *search_criteria,
1249 gboolean *can_do_previous,
1250 gboolean *can_do_next)
1252 EmpathyChatViewPriv *priv;
1253 GtkTextBuffer *buffer;
1254 GtkTextIter iter_at_mark;
1255 GtkTextIter iter_match_start;
1256 GtkTextIter iter_match_end;
1258 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
1259 g_return_if_fail (search_criteria != NULL);
1260 g_return_if_fail (can_do_previous != NULL && can_do_next != NULL);
1262 priv = GET_PRIV (view);
1264 buffer = priv->buffer;
1266 if (can_do_previous) {
1267 if (priv->find_mark_previous) {
1268 gtk_text_buffer_get_iter_at_mark (buffer,
1270 priv->find_mark_previous);
1272 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1275 *can_do_previous = empathy_text_iter_backward_search (&iter_at_mark,
1283 if (priv->find_mark_next) {
1284 gtk_text_buffer_get_iter_at_mark (buffer,
1286 priv->find_mark_next);
1288 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1291 *can_do_next = empathy_text_iter_forward_search (&iter_at_mark,
1300 empathy_chat_view_highlight (EmpathyChatView *view,
1303 GtkTextBuffer *buffer;
1305 GtkTextIter iter_start;
1306 GtkTextIter iter_end;
1307 GtkTextIter iter_match_start;
1308 GtkTextIter iter_match_end;
1311 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
1313 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
1315 gtk_text_buffer_get_start_iter (buffer, &iter);
1317 gtk_text_buffer_get_bounds (buffer, &iter_start, &iter_end);
1318 gtk_text_buffer_remove_tag_by_name (buffer, "highlight",
1322 if (G_STR_EMPTY (text)) {
1327 found = empathy_text_iter_forward_search (&iter,
1337 gtk_text_buffer_apply_tag_by_name (buffer, "highlight",
1341 iter = iter_match_end;
1342 gtk_text_iter_forward_char (&iter);
1347 empathy_chat_view_copy_clipboard (EmpathyChatView *view)
1349 GtkTextBuffer *buffer;
1350 GtkClipboard *clipboard;
1352 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
1354 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
1355 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1357 gtk_text_buffer_copy_clipboard (buffer, clipboard);
1361 empathy_chat_view_get_theme (EmpathyChatView *view)
1363 EmpathyChatViewPriv *priv;
1365 g_return_val_if_fail (EMPATHY_IS_CHAT_VIEW (view), NULL);
1367 priv = GET_PRIV (view);
1373 chat_view_theme_updated_cb (EmpathyTheme *theme, EmpathyChatView *view)
1375 EmpathyChatViewPriv *priv;
1377 priv = GET_PRIV (view);
1379 empathy_theme_detach_from_view (priv->theme, priv->theme_context,
1382 priv->theme_context = empathy_theme_setup_with_view (theme, view);
1386 empathy_chat_view_set_theme (EmpathyChatView *view, EmpathyTheme *theme)
1388 EmpathyChatViewPriv *priv;
1390 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
1391 g_return_if_fail (EMPATHY_IS_THEME (theme));
1393 priv = GET_PRIV (view);
1396 g_signal_handlers_disconnect_by_func (priv->theme,
1397 chat_view_theme_updated_cb,
1399 empathy_theme_detach_from_view (priv->theme, priv->theme_context,
1402 g_object_unref (priv->theme);
1405 priv->theme = g_object_ref (theme);
1407 g_signal_connect (priv->theme,
1409 G_CALLBACK (chat_view_theme_updated_cb),
1412 priv->theme_context = empathy_theme_setup_with_view (theme, view);
1414 /* FIXME: Possibly redraw the function and make it a property */
1418 empathy_chat_view_set_margin (EmpathyChatView *view,
1421 EmpathyChatViewPriv *priv;
1423 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
1425 priv = GET_PRIV (view);
1428 "left-margin", margin,
1429 "right-margin", margin,
1434 empathy_chat_view_get_smiley_menu (GCallback callback,
1437 EmpathySmileyManager *smiley_manager;
1438 GSList *smileys, *l;
1443 g_return_val_if_fail (callback != NULL, NULL);
1445 menu = gtk_menu_new ();
1447 smiley_manager = empathy_smiley_manager_new ();
1448 smileys = empathy_smiley_manager_get_all (smiley_manager);
1449 for (l = smileys; l; l = l->next) {
1450 EmpathySmiley *smiley;
1455 image = gtk_image_new_from_pixbuf (smiley->pixbuf);
1457 item = gtk_image_menu_item_new_with_label ("");
1458 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1460 gtk_menu_attach (GTK_MENU (menu), item,
1461 x, x + 1, y, y + 1);
1463 gtk_widget_set_tooltip_text (item, smiley->str);
1465 g_object_set_data (G_OBJECT (item), "smiley_text", smiley->str);
1466 g_signal_connect (item, "activate", callback, user_data);
1475 g_object_unref (smiley_manager);
1477 gtk_widget_show_all (menu);
1482 /* FIXME: Do we really need this? Better to do it internally only at setup time,
1483 * we will never change it on the fly.
1486 empathy_chat_view_set_is_group_chat (EmpathyChatView *view,
1487 gboolean is_group_chat)
1489 EmpathyChatViewPriv *priv;
1490 gboolean theme_rooms = FALSE;
1492 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
1494 priv = GET_PRIV (view);
1496 priv->is_group_chat = is_group_chat;
1498 empathy_conf_get_bool (empathy_conf_get (),
1499 EMPATHY_PREFS_CHAT_THEME_CHAT_ROOM,
1502 if (!theme_rooms && is_group_chat) {
1503 empathy_theme_manager_apply (empathy_theme_manager_get (),
1507 empathy_theme_manager_apply_saved (empathy_theme_manager_get (),
1513 empathy_chat_view_get_last_timestamp (EmpathyChatView *view)
1515 EmpathyChatViewPriv *priv;
1517 g_return_val_if_fail (EMPATHY_IS_CHAT_VIEW (view), 0);
1519 priv = GET_PRIV (view);
1521 return priv->last_timestamp;
1525 empathy_chat_view_set_last_timestamp (EmpathyChatView *view,
1528 EmpathyChatViewPriv *priv;
1530 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
1532 priv = GET_PRIV (view);
1534 priv->last_timestamp = timestamp;
1538 empathy_chat_view_get_last_block_type (EmpathyChatView *view)
1540 EmpathyChatViewPriv *priv;
1542 g_return_val_if_fail (EMPATHY_IS_CHAT_VIEW (view), 0);
1544 priv = GET_PRIV (view);
1546 return priv->last_block_type;
1550 empathy_chat_view_set_last_block_type (EmpathyChatView *view,
1551 BlockType block_type)
1553 EmpathyChatViewPriv *priv;
1555 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
1557 priv = GET_PRIV (view);
1559 priv->last_block_type = block_type;
1563 empathy_chat_view_get_last_contact (EmpathyChatView *view)
1565 EmpathyChatViewPriv *priv;
1567 g_return_val_if_fail (EMPATHY_IS_CHAT_VIEW (view), NULL);
1569 priv = GET_PRIV (view);
1571 return priv->last_contact;
1575 empathy_chat_view_set_last_contact (EmpathyChatView *view, EmpathyContact *contact)
1577 EmpathyChatViewPriv *priv;
1579 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
1581 priv = GET_PRIV (view);
1583 if (priv->last_contact) {
1584 g_object_unref (priv->last_contact);
1585 priv->last_contact = NULL;
1589 priv->last_contact = g_object_ref (contact);