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-theme-manager.h"
52 #include "empathy-ui-utils.h"
53 #include "empathy-smiley-manager.h"
55 #define DEBUG_DOMAIN "ChatView"
57 /* Number of seconds between timestamps when using normal mode, 5 minutes. */
58 #define TIMESTAMP_INTERVAL 300
61 #define MAX_SCROLL_TIME 0.4 /* seconds */
62 #define SCROLL_DELAY 33 /* milliseconds */
64 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), EMPATHY_TYPE_CHAT_VIEW, EmpathyChatViewPriv))
66 struct _EmpathyChatViewPriv {
67 GtkTextBuffer *buffer;
71 time_t last_timestamp;
73 gboolean allow_scrolling;
76 gboolean is_group_chat;
78 GtkTextMark *find_mark_previous;
79 GtkTextMark *find_mark_next;
80 gboolean find_wrapped;
81 gboolean find_last_direction;
83 /* This is for the group chat so we know if the "other" last contact
84 * changed, so we know whether to insert a header or not.
86 EmpathyContact *last_contact;
88 guint notify_system_fonts_id;
89 guint notify_show_avatars_id;
92 static void empathy_chat_view_class_init (EmpathyChatViewClass *klass);
93 static void empathy_chat_view_init (EmpathyChatView *view);
94 static void chat_view_finalize (GObject *object);
95 static gboolean chat_view_drag_motion (GtkWidget *widget,
96 GdkDragContext *context,
100 static void chat_view_size_allocate (GtkWidget *widget,
101 GtkAllocation *alloc);
102 static void chat_view_setup_tags (EmpathyChatView *view);
103 static void chat_view_system_font_update (EmpathyChatView *view);
104 static void chat_view_notify_system_font_cb (EmpathyConf *conf,
107 static void chat_view_notify_show_avatars_cb (EmpathyConf *conf,
110 static void chat_view_populate_popup (EmpathyChatView *view,
113 static gboolean chat_view_event_cb (EmpathyChatView *view,
114 GdkEventMotion *event,
116 static gboolean chat_view_url_event_cb (GtkTextTag *tag,
120 GtkTextBuffer *buffer);
121 static void chat_view_open_address_cb (GtkMenuItem *menuitem,
123 static void chat_view_copy_address_cb (GtkMenuItem *menuitem,
125 static void chat_view_clear_view_cb (GtkMenuItem *menuitem,
126 EmpathyChatView *view);
127 static gboolean chat_view_is_scrolled_down (EmpathyChatView *view);
128 static void chat_view_theme_changed_cb (EmpathyThemeManager *manager,
129 EmpathyChatView *view);
130 static void chat_view_theme_notify_cb (EmpathyTheme *theme,
132 EmpathyChatView *view);
134 G_DEFINE_TYPE (EmpathyChatView, empathy_chat_view, GTK_TYPE_TEXT_VIEW);
137 empathy_chat_view_class_init (EmpathyChatViewClass *klass)
139 GObjectClass *object_class = G_OBJECT_CLASS (klass);
140 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
142 object_class->finalize = chat_view_finalize;
143 widget_class->size_allocate = chat_view_size_allocate;
144 widget_class->drag_motion = chat_view_drag_motion;
146 g_type_class_add_private (object_class, sizeof (EmpathyChatViewPriv));
150 empathy_chat_view_init (EmpathyChatView *view)
152 EmpathyChatViewPriv *priv;
153 gboolean show_avatars;
155 priv = GET_PRIV (view);
157 priv->buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
158 priv->last_timestamp = 0;
159 priv->allow_scrolling = TRUE;
160 priv->is_group_chat = FALSE;
163 "wrap-mode", GTK_WRAP_WORD_CHAR,
165 "cursor-visible", FALSE,
168 priv->notify_system_fonts_id =
169 empathy_conf_notify_add (empathy_conf_get (),
170 "/desktop/gnome/interface/document_font_name",
171 chat_view_notify_system_font_cb,
173 chat_view_system_font_update (view);
175 priv->notify_show_avatars_id =
176 empathy_conf_notify_add (empathy_conf_get (),
177 EMPATHY_PREFS_UI_SHOW_AVATARS,
178 chat_view_notify_show_avatars_cb,
181 chat_view_setup_tags (view);
183 empathy_theme_manager_apply_saved (empathy_theme_manager_get (), view);
185 show_avatars = FALSE;
186 empathy_conf_get_bool (empathy_conf_get (),
187 EMPATHY_PREFS_UI_SHOW_AVATARS,
190 empathy_theme_set_show_avatars (priv->theme, show_avatars);
192 g_signal_connect (view,
194 G_CALLBACK (chat_view_populate_popup),
197 g_signal_connect_object (empathy_theme_manager_get (),
199 G_CALLBACK (chat_view_theme_changed_cb),
205 chat_view_finalize (GObject *object)
207 EmpathyChatView *view;
208 EmpathyChatViewPriv *priv;
210 view = EMPATHY_CHAT_VIEW (object);
211 priv = GET_PRIV (view);
213 empathy_debug (DEBUG_DOMAIN, "finalize: %p", object);
215 empathy_conf_notify_remove (empathy_conf_get (), priv->notify_system_fonts_id);
216 empathy_conf_notify_remove (empathy_conf_get (), priv->notify_show_avatars_id);
218 if (priv->last_contact) {
219 g_object_unref (priv->last_contact);
221 if (priv->scroll_time) {
222 g_timer_destroy (priv->scroll_time);
224 if (priv->scroll_timeout) {
225 g_source_remove (priv->scroll_timeout);
229 g_signal_handlers_disconnect_by_func (priv->theme,
230 chat_view_theme_notify_cb,
232 g_object_unref (priv->theme);
235 G_OBJECT_CLASS (empathy_chat_view_parent_class)->finalize (object);
239 chat_view_drag_motion (GtkWidget *widget,
240 GdkDragContext *context,
245 /* Don't handle drag motion, since we don't want the view to scroll as
246 * the result of dragging something across it.
253 chat_view_size_allocate (GtkWidget *widget,
254 GtkAllocation *alloc)
258 down = chat_view_is_scrolled_down (EMPATHY_CHAT_VIEW (widget));
260 GTK_WIDGET_CLASS (empathy_chat_view_parent_class)->size_allocate (widget, alloc);
265 adj = GTK_TEXT_VIEW (widget)->vadjustment;
266 gtk_adjustment_set_value (adj, adj->upper - adj->page_size);
271 chat_view_setup_tags (EmpathyChatView *view)
273 EmpathyChatViewPriv *priv;
276 priv = GET_PRIV (view);
278 gtk_text_buffer_create_tag (priv->buffer,
282 /* FIXME: Move to the theme and come up with something that looks a bit
285 gtk_text_buffer_create_tag (priv->buffer,
287 "background", "yellow",
290 tag = gtk_text_buffer_create_tag (priv->buffer,
294 g_signal_connect (tag,
296 G_CALLBACK (chat_view_url_event_cb),
299 g_signal_connect (view,
300 "motion-notify-event",
301 G_CALLBACK (chat_view_event_cb),
306 chat_view_system_font_update (EmpathyChatView *view)
308 PangoFontDescription *font_description = NULL;
311 if (empathy_conf_get_string (empathy_conf_get (),
312 "/desktop/gnome/interface/document_font_name",
313 &font_name) && font_name) {
314 font_description = pango_font_description_from_string (font_name);
317 font_description = NULL;
320 gtk_widget_modify_font (GTK_WIDGET (view), font_description);
322 if (font_description) {
323 pango_font_description_free (font_description);
328 chat_view_notify_system_font_cb (EmpathyConf *conf,
332 EmpathyChatView *view;
333 EmpathyChatViewPriv *priv;
334 gboolean show_avatars = FALSE;
337 priv = GET_PRIV (view);
339 chat_view_system_font_update (view);
341 /* Ugly, again, to adjust the vertical position of the nick... Will fix
342 * this when reworking the theme manager so that view register
343 * themselves with it instead of the other way around.
345 empathy_conf_get_bool (conf,
346 EMPATHY_PREFS_UI_SHOW_AVATARS,
349 empathy_theme_set_show_avatars (priv->theme, show_avatars);
353 chat_view_notify_show_avatars_cb (EmpathyConf *conf,
357 EmpathyChatView *view;
358 EmpathyChatViewPriv *priv;
359 gboolean show_avatars = FALSE;
362 priv = GET_PRIV (view);
364 empathy_conf_get_bool (conf, key, &show_avatars);
366 empathy_theme_set_show_avatars (priv->theme, show_avatars);
370 chat_view_populate_popup (EmpathyChatView *view,
374 EmpathyChatViewPriv *priv;
375 GtkTextTagTable *table;
378 GtkTextIter iter, start, end;
382 priv = GET_PRIV (view);
384 /* Clear menu item */
385 if (gtk_text_buffer_get_char_count (priv->buffer) > 0) {
386 item = gtk_menu_item_new ();
387 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
388 gtk_widget_show (item);
390 item = gtk_image_menu_item_new_from_stock (GTK_STOCK_CLEAR, NULL);
391 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
392 gtk_widget_show (item);
394 g_signal_connect (item,
396 G_CALLBACK (chat_view_clear_view_cb),
400 /* Link context menu items */
401 table = gtk_text_buffer_get_tag_table (priv->buffer);
402 tag = gtk_text_tag_table_lookup (table, "link");
404 gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
406 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
407 GTK_TEXT_WINDOW_WIDGET,
411 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &iter, x, y);
415 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
416 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
417 str = gtk_text_buffer_get_text (priv->buffer,
418 &start, &end, FALSE);
421 if (G_STR_EMPTY (str)) {
426 /* NOTE: Set data just to get the string freed when not needed. */
427 g_object_set_data_full (G_OBJECT (menu),
429 (GDestroyNotify) g_free);
431 item = gtk_menu_item_new ();
432 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
433 gtk_widget_show (item);
435 item = gtk_menu_item_new_with_mnemonic (_("_Copy Link Address"));
436 g_signal_connect (item,
438 G_CALLBACK (chat_view_copy_address_cb),
440 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
441 gtk_widget_show (item);
443 item = gtk_menu_item_new_with_mnemonic (_("_Open Link"));
444 g_signal_connect (item,
446 G_CALLBACK (chat_view_open_address_cb),
448 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
449 gtk_widget_show (item);
453 chat_view_event_cb (EmpathyChatView *view,
454 GdkEventMotion *event,
457 static GdkCursor *hand = NULL;
458 static GdkCursor *beam = NULL;
459 GtkTextWindowType type;
462 gint x, y, buf_x, buf_y;
464 type = gtk_text_view_get_window_type (GTK_TEXT_VIEW (view),
467 if (type != GTK_TEXT_WINDOW_TEXT) {
471 /* Get where the pointer really is. */
472 win = gtk_text_view_get_window (GTK_TEXT_VIEW (view), type);
477 gdk_window_get_pointer (win, &x, &y, NULL);
479 /* Get the iter where the cursor is at */
480 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view), type,
484 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view),
488 if (gtk_text_iter_has_tag (&iter, tag)) {
490 hand = gdk_cursor_new (GDK_HAND2);
491 beam = gdk_cursor_new (GDK_XTERM);
493 gdk_window_set_cursor (win, hand);
496 beam = gdk_cursor_new (GDK_XTERM);
498 gdk_window_set_cursor (win, beam);
505 chat_view_url_event_cb (GtkTextTag *tag,
509 GtkTextBuffer *buffer)
511 GtkTextIter start, end;
514 /* If the link is being selected, don't do anything. */
515 gtk_text_buffer_get_selection_bounds (buffer, &start, &end);
516 if (gtk_text_iter_get_offset (&start) != gtk_text_iter_get_offset (&end)) {
520 if (event->type == GDK_BUTTON_RELEASE && event->button.button == 1) {
523 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
524 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
525 str = gtk_text_buffer_get_text (buffer,
530 empathy_url_show (str);
539 chat_view_open_address_cb (GtkMenuItem *menuitem, const gchar *url)
541 empathy_url_show (url);
545 chat_view_copy_address_cb (GtkMenuItem *menuitem, const gchar *url)
547 GtkClipboard *clipboard;
549 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
550 gtk_clipboard_set_text (clipboard, url, -1);
552 clipboard = gtk_clipboard_get (GDK_SELECTION_PRIMARY);
553 gtk_clipboard_set_text (clipboard, url, -1);
557 chat_view_clear_view_cb (GtkMenuItem *menuitem, EmpathyChatView *view)
559 empathy_chat_view_clear (view);
563 chat_view_is_scrolled_down (EmpathyChatView *view)
567 sw = gtk_widget_get_parent (GTK_WIDGET (view));
568 if (GTK_IS_SCROLLED_WINDOW (sw)) {
571 vadj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (sw));
573 if (vadj->value + vadj->page_size / 2 < vadj->upper - vadj->page_size) {
582 chat_view_maybe_trim_buffer (EmpathyChatView *view)
584 EmpathyChatViewPriv *priv;
585 GtkTextIter top, bottom;
588 GtkTextTagTable *table;
591 priv = GET_PRIV (view);
593 gtk_text_buffer_get_end_iter (priv->buffer, &bottom);
594 line = gtk_text_iter_get_line (&bottom);
595 if (line < MAX_LINES) {
599 remove = line - MAX_LINES;
600 gtk_text_buffer_get_start_iter (priv->buffer, &top);
603 if (!gtk_text_iter_forward_lines (&bottom, remove)) {
607 /* Track backwords to a place where we can safely cut, we don't do it in
608 * the middle of a tag.
610 table = gtk_text_buffer_get_tag_table (priv->buffer);
611 tag = gtk_text_tag_table_lookup (table, "cut");
616 if (!gtk_text_iter_forward_to_tag_toggle (&bottom, tag)) {
620 if (!gtk_text_iter_equal (&top, &bottom)) {
621 gtk_text_buffer_delete (priv->buffer, &top, &bottom);
626 chat_view_theme_changed_cb (EmpathyThemeManager *manager,
627 EmpathyChatView *view)
629 EmpathyChatViewPriv *priv;
630 gboolean show_avatars = FALSE;
631 gboolean theme_rooms = FALSE;
633 priv = GET_PRIV (view);
635 empathy_conf_get_bool (empathy_conf_get (),
636 EMPATHY_PREFS_CHAT_THEME_CHAT_ROOM,
638 if (!theme_rooms && priv->is_group_chat) {
639 empathy_theme_manager_apply (manager, view, NULL);
641 empathy_theme_manager_apply_saved (manager, view);
644 /* Needed for now to update the "rise" property of the names to get it
645 * vertically centered.
647 empathy_conf_get_bool (empathy_conf_get (),
648 EMPATHY_PREFS_UI_SHOW_AVATARS,
650 empathy_theme_set_show_avatars (priv->theme, show_avatars);
653 /* Pads a pixbuf to the specified size, by centering it in a larger transparent
654 * pixbuf. Returns a new ref.
657 chat_view_pad_to_size (GdkPixbuf *pixbuf,
660 gint extra_padding_right)
662 gint src_width, src_height;
664 gint x_offset, y_offset;
666 src_width = gdk_pixbuf_get_width (pixbuf);
667 src_height = gdk_pixbuf_get_height (pixbuf);
669 x_offset = (width - src_width) / 2;
670 y_offset = (height - src_height) / 2;
672 padded = gdk_pixbuf_new (gdk_pixbuf_get_colorspace (pixbuf),
674 gdk_pixbuf_get_bits_per_sample (pixbuf),
675 width + extra_padding_right,
678 gdk_pixbuf_fill (padded, 0);
680 gdk_pixbuf_copy_area (pixbuf,
681 0, /* source coords */
686 x_offset, /* dest coords */
698 chat_view_avatar_cache_data_free (gpointer ptr)
700 AvatarData *data = ptr;
702 g_object_unref (data->pixbuf);
703 g_free (data->token);
704 g_slice_free (AvatarData, data);
708 empathy_chat_view_get_avatar_pixbuf_with_cache (EmpathyContact *contact)
711 EmpathyAvatar *avatar;
712 GdkPixbuf *tmp_pixbuf;
713 GdkPixbuf *pixbuf = NULL;
715 /* Check if avatar is in cache and if it's up to date */
716 avatar = empathy_contact_get_avatar (contact);
717 data = g_object_get_data (G_OBJECT (contact), "chat-view-avatar-cache");
719 if (avatar && !tp_strdiff (avatar->token, data->token)) {
720 /* We have the avatar in cache */
725 /* Avatar not in cache, create pixbuf */
726 tmp_pixbuf = empathy_pixbuf_avatar_from_contact_scaled (contact, 32, 32);
728 pixbuf = chat_view_pad_to_size (tmp_pixbuf, 32, 32, 6);
729 g_object_unref (tmp_pixbuf);
735 /* Insert new pixbuf in cache */
736 data = g_slice_new0 (AvatarData);
737 data->token = g_strdup (avatar->token);
738 data->pixbuf = pixbuf;
740 g_object_set_data_full (G_OBJECT (contact), "chat-view-avatar-cache",
741 data, chat_view_avatar_cache_data_free);
747 empathy_chat_view_new (void)
749 return g_object_new (EMPATHY_TYPE_CHAT_VIEW, NULL);
752 /* Code stolen from pidgin/gtkimhtml.c */
754 chat_view_scroll_cb (EmpathyChatView *view)
756 EmpathyChatViewPriv *priv;
760 priv = GET_PRIV (view);
761 adj = GTK_TEXT_VIEW (view)->vadjustment;
762 max_val = adj->upper - adj->page_size;
764 g_return_val_if_fail (priv->scroll_time != NULL, FALSE);
766 if (g_timer_elapsed (priv->scroll_time, NULL) > MAX_SCROLL_TIME) {
767 /* time's up. jump to the end and kill the timer */
768 gtk_adjustment_set_value (adj, max_val);
769 g_timer_destroy (priv->scroll_time);
770 priv->scroll_time = NULL;
771 priv->scroll_timeout = 0;
775 /* scroll by 1/3rd the remaining distance */
776 gtk_adjustment_set_value (adj, gtk_adjustment_get_value (adj) + ((max_val - gtk_adjustment_get_value (adj)) / 3));
781 empathy_chat_view_scroll_down (EmpathyChatView *view)
783 EmpathyChatViewPriv *priv;
785 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
787 priv = GET_PRIV (view);
789 if (!priv->allow_scrolling) {
793 empathy_debug (DEBUG_DOMAIN, "Scrolling down");
795 if (priv->scroll_time) {
796 g_timer_reset (priv->scroll_time);
798 priv->scroll_time = g_timer_new();
800 if (!priv->scroll_timeout) {
801 priv->scroll_timeout = g_timeout_add (SCROLL_DELAY,
802 (GSourceFunc) chat_view_scroll_cb,
808 empathy_chat_view_append_message (EmpathyChatView *view,
811 EmpathyChatViewPriv *priv = GET_PRIV (view);
814 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
815 g_return_if_fail (EMPATHY_IS_MESSAGE (msg));
817 if (!empathy_message_get_body (msg)) {
821 bottom = chat_view_is_scrolled_down (view);
823 chat_view_maybe_trim_buffer (view);
825 empathy_theme_append_message (priv->theme, view, msg);
828 empathy_chat_view_scroll_down (view);
831 if (priv->last_contact) {
832 g_object_unref (priv->last_contact);
834 priv->last_contact = g_object_ref (empathy_message_get_sender (msg));
838 empathy_chat_view_append_event (EmpathyChatView *view,
841 EmpathyChatViewPriv *priv;
844 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
845 g_return_if_fail (!G_STR_EMPTY (str));
847 priv = GET_PRIV (view);
849 bottom = chat_view_is_scrolled_down (view);
851 chat_view_maybe_trim_buffer (view);
853 empathy_theme_append_event (priv->theme, view, str);
856 empathy_chat_view_scroll_down (view);
859 if (priv->last_contact) {
860 g_object_unref (priv->last_contact);
861 priv->last_contact = NULL;
866 empathy_chat_view_append_button (EmpathyChatView *view,
867 const gchar *message,
871 EmpathyChatViewPriv *priv;
872 GtkTextChildAnchor *anchor;
877 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
878 g_return_if_fail (button1 != NULL);
880 priv = GET_PRIV (view);
884 bottom = chat_view_is_scrolled_down (view);
886 empathy_theme_append_timestamp (priv->theme, view, NULL, TRUE, TRUE);
889 empathy_theme_append_text (priv->theme, view, message, tag, NULL);
892 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
894 anchor = gtk_text_buffer_create_child_anchor (priv->buffer, &iter);
895 gtk_text_view_add_child_at_anchor (GTK_TEXT_VIEW (view), button1, anchor);
896 gtk_widget_show (button1);
898 gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
906 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
908 anchor = gtk_text_buffer_create_child_anchor (priv->buffer, &iter);
909 gtk_text_view_add_child_at_anchor (GTK_TEXT_VIEW (view), button2, anchor);
910 gtk_widget_show (button2);
912 gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
920 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
921 gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
929 empathy_chat_view_scroll_down (view);
932 if (priv->last_contact) {
933 g_object_unref (priv->last_contact);
934 priv->last_contact = NULL;
939 empathy_chat_view_scroll (EmpathyChatView *view,
940 gboolean allow_scrolling)
942 EmpathyChatViewPriv *priv = GET_PRIV (view);
944 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
946 empathy_debug (DEBUG_DOMAIN, "Scrolling %s",
947 allow_scrolling ? "enabled" : "disabled");
949 priv->allow_scrolling = allow_scrolling;
950 if (allow_scrolling) {
951 empathy_chat_view_scroll_down (view);
956 empathy_chat_view_get_selection_bounds (EmpathyChatView *view,
960 GtkTextBuffer *buffer;
962 g_return_val_if_fail (EMPATHY_IS_CHAT_VIEW (view), FALSE);
964 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
966 return gtk_text_buffer_get_selection_bounds (buffer, start, end);
970 empathy_chat_view_clear (EmpathyChatView *view)
972 GtkTextBuffer *buffer;
973 EmpathyChatViewPriv *priv;
975 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
977 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
978 gtk_text_buffer_set_text (buffer, "", -1);
980 /* We set these back to the initial values so we get
981 * timestamps when clearing the window to know when
982 * conversations start.
984 priv = GET_PRIV (view);
986 priv->last_timestamp = 0;
990 empathy_chat_view_find_previous (EmpathyChatView *view,
991 const gchar *search_criteria,
994 EmpathyChatViewPriv *priv;
995 GtkTextBuffer *buffer;
996 GtkTextIter iter_at_mark;
997 GtkTextIter iter_match_start;
998 GtkTextIter iter_match_end;
1000 gboolean from_start = FALSE;
1002 g_return_val_if_fail (EMPATHY_IS_CHAT_VIEW (view), FALSE);
1003 g_return_val_if_fail (search_criteria != NULL, FALSE);
1005 priv = GET_PRIV (view);
1007 buffer = priv->buffer;
1009 if (G_STR_EMPTY (search_criteria)) {
1010 if (priv->find_mark_previous) {
1011 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1013 gtk_text_buffer_move_mark (buffer,
1014 priv->find_mark_previous,
1016 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
1017 priv->find_mark_previous,
1022 gtk_text_buffer_select_range (buffer,
1034 if (priv->find_mark_previous) {
1035 gtk_text_buffer_get_iter_at_mark (buffer,
1037 priv->find_mark_previous);
1039 gtk_text_buffer_get_end_iter (buffer, &iter_at_mark);
1043 priv->find_last_direction = FALSE;
1045 found = empathy_text_iter_backward_search (&iter_at_mark,
1052 gboolean result = FALSE;
1058 /* Here we wrap around. */
1059 if (!new_search && !priv->find_wrapped) {
1060 priv->find_wrapped = TRUE;
1061 result = empathy_chat_view_find_previous (view,
1064 priv->find_wrapped = FALSE;
1070 /* Set new mark and show on screen */
1071 if (!priv->find_mark_previous) {
1072 priv->find_mark_previous = gtk_text_buffer_create_mark (buffer, NULL,
1076 gtk_text_buffer_move_mark (buffer,
1077 priv->find_mark_previous,
1081 if (!priv->find_mark_next) {
1082 priv->find_mark_next = gtk_text_buffer_create_mark (buffer, NULL,
1086 gtk_text_buffer_move_mark (buffer,
1087 priv->find_mark_next,
1091 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
1092 priv->find_mark_previous,
1098 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &iter_match_start);
1099 gtk_text_buffer_move_mark_by_name (buffer, "insert", &iter_match_end);
1105 empathy_chat_view_find_next (EmpathyChatView *view,
1106 const gchar *search_criteria,
1107 gboolean new_search)
1109 EmpathyChatViewPriv *priv;
1110 GtkTextBuffer *buffer;
1111 GtkTextIter iter_at_mark;
1112 GtkTextIter iter_match_start;
1113 GtkTextIter iter_match_end;
1115 gboolean from_start = FALSE;
1117 g_return_val_if_fail (EMPATHY_IS_CHAT_VIEW (view), FALSE);
1118 g_return_val_if_fail (search_criteria != NULL, FALSE);
1120 priv = GET_PRIV (view);
1122 buffer = priv->buffer;
1124 if (G_STR_EMPTY (search_criteria)) {
1125 if (priv->find_mark_next) {
1126 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1128 gtk_text_buffer_move_mark (buffer,
1129 priv->find_mark_next,
1131 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
1132 priv->find_mark_next,
1137 gtk_text_buffer_select_range (buffer,
1149 if (priv->find_mark_next) {
1150 gtk_text_buffer_get_iter_at_mark (buffer,
1152 priv->find_mark_next);
1154 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1158 priv->find_last_direction = TRUE;
1160 found = empathy_text_iter_forward_search (&iter_at_mark,
1167 gboolean result = FALSE;
1173 /* Here we wrap around. */
1174 if (!new_search && !priv->find_wrapped) {
1175 priv->find_wrapped = TRUE;
1176 result = empathy_chat_view_find_next (view,
1179 priv->find_wrapped = FALSE;
1185 /* Set new mark and show on screen */
1186 if (!priv->find_mark_next) {
1187 priv->find_mark_next = gtk_text_buffer_create_mark (buffer, NULL,
1191 gtk_text_buffer_move_mark (buffer,
1192 priv->find_mark_next,
1196 if (!priv->find_mark_previous) {
1197 priv->find_mark_previous = gtk_text_buffer_create_mark (buffer, NULL,
1201 gtk_text_buffer_move_mark (buffer,
1202 priv->find_mark_previous,
1206 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
1207 priv->find_mark_next,
1213 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &iter_match_start);
1214 gtk_text_buffer_move_mark_by_name (buffer, "insert", &iter_match_end);
1221 empathy_chat_view_find_abilities (EmpathyChatView *view,
1222 const gchar *search_criteria,
1223 gboolean *can_do_previous,
1224 gboolean *can_do_next)
1226 EmpathyChatViewPriv *priv;
1227 GtkTextBuffer *buffer;
1228 GtkTextIter iter_at_mark;
1229 GtkTextIter iter_match_start;
1230 GtkTextIter iter_match_end;
1232 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
1233 g_return_if_fail (search_criteria != NULL);
1234 g_return_if_fail (can_do_previous != NULL && can_do_next != NULL);
1236 priv = GET_PRIV (view);
1238 buffer = priv->buffer;
1240 if (can_do_previous) {
1241 if (priv->find_mark_previous) {
1242 gtk_text_buffer_get_iter_at_mark (buffer,
1244 priv->find_mark_previous);
1246 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1249 *can_do_previous = empathy_text_iter_backward_search (&iter_at_mark,
1257 if (priv->find_mark_next) {
1258 gtk_text_buffer_get_iter_at_mark (buffer,
1260 priv->find_mark_next);
1262 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1265 *can_do_next = empathy_text_iter_forward_search (&iter_at_mark,
1274 empathy_chat_view_highlight (EmpathyChatView *view,
1277 GtkTextBuffer *buffer;
1279 GtkTextIter iter_start;
1280 GtkTextIter iter_end;
1281 GtkTextIter iter_match_start;
1282 GtkTextIter iter_match_end;
1285 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
1287 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
1289 gtk_text_buffer_get_start_iter (buffer, &iter);
1291 gtk_text_buffer_get_bounds (buffer, &iter_start, &iter_end);
1292 gtk_text_buffer_remove_tag_by_name (buffer, "highlight",
1296 if (G_STR_EMPTY (text)) {
1301 found = empathy_text_iter_forward_search (&iter,
1311 gtk_text_buffer_apply_tag_by_name (buffer, "highlight",
1315 iter = iter_match_end;
1316 gtk_text_iter_forward_char (&iter);
1321 empathy_chat_view_copy_clipboard (EmpathyChatView *view)
1323 GtkTextBuffer *buffer;
1324 GtkClipboard *clipboard;
1326 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
1328 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
1329 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1331 gtk_text_buffer_copy_clipboard (buffer, clipboard);
1335 empathy_chat_view_get_theme (EmpathyChatView *view)
1337 EmpathyChatViewPriv *priv;
1339 g_return_val_if_fail (EMPATHY_IS_CHAT_VIEW (view), NULL);
1341 priv = GET_PRIV (view);
1347 chat_view_theme_notify_cb (EmpathyTheme *theme,
1349 EmpathyChatView *view)
1351 empathy_theme_update_view (theme, view);
1355 empathy_chat_view_set_theme (EmpathyChatView *view, EmpathyTheme *theme)
1357 EmpathyChatViewPriv *priv;
1359 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
1360 g_return_if_fail (EMPATHY_IS_THEME (theme));
1362 priv = GET_PRIV (view);
1365 g_signal_handlers_disconnect_by_func (priv->theme,
1366 chat_view_theme_notify_cb,
1368 g_object_unref (priv->theme);
1371 priv->theme = g_object_ref (theme);
1373 empathy_theme_update_view (theme, view);
1374 g_signal_connect (priv->theme, "notify",
1375 G_CALLBACK (chat_view_theme_notify_cb),
1378 /* FIXME: Redraw all messages using the new theme */
1382 empathy_chat_view_set_margin (EmpathyChatView *view,
1385 EmpathyChatViewPriv *priv;
1387 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
1389 priv = GET_PRIV (view);
1392 "left-margin", margin,
1393 "right-margin", margin,
1398 empathy_chat_view_get_smiley_menu (GCallback callback,
1401 EmpathySmileyManager *smiley_manager;
1402 GSList *smileys, *l;
1407 g_return_val_if_fail (callback != NULL, NULL);
1409 menu = gtk_menu_new ();
1411 smiley_manager = empathy_smiley_manager_new ();
1412 smileys = empathy_smiley_manager_get_all (smiley_manager);
1413 for (l = smileys; l; l = l->next) {
1414 EmpathySmiley *smiley;
1419 image = gtk_image_new_from_pixbuf (smiley->pixbuf);
1421 item = gtk_image_menu_item_new_with_label ("");
1422 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1424 gtk_menu_attach (GTK_MENU (menu), item,
1425 x, x + 1, y, y + 1);
1427 gtk_widget_set_tooltip_text (item, smiley->str);
1429 g_object_set_data (G_OBJECT (item), "smiley_text", smiley->str);
1430 g_signal_connect (item, "activate", callback, user_data);
1439 g_object_unref (smiley_manager);
1441 gtk_widget_show_all (menu);
1446 /* FIXME: Do we really need this? Better to do it internally only at setup time,
1447 * we will never change it on the fly.
1450 empathy_chat_view_set_is_group_chat (EmpathyChatView *view,
1451 gboolean is_group_chat)
1453 EmpathyChatViewPriv *priv;
1454 gboolean theme_rooms = FALSE;
1456 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
1458 priv = GET_PRIV (view);
1460 priv->is_group_chat = is_group_chat;
1462 empathy_conf_get_bool (empathy_conf_get (),
1463 EMPATHY_PREFS_CHAT_THEME_CHAT_ROOM,
1466 if (!theme_rooms && is_group_chat) {
1467 empathy_theme_manager_apply (empathy_theme_manager_get (),
1471 empathy_theme_manager_apply_saved (empathy_theme_manager_get (),
1477 empathy_chat_view_get_last_timestamp (EmpathyChatView *view)
1479 EmpathyChatViewPriv *priv;
1481 g_return_val_if_fail (EMPATHY_IS_CHAT_VIEW (view), 0);
1483 priv = GET_PRIV (view);
1485 return priv->last_timestamp;
1489 empathy_chat_view_set_last_timestamp (EmpathyChatView *view,
1492 EmpathyChatViewPriv *priv;
1494 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
1496 priv = GET_PRIV (view);
1498 priv->last_timestamp = timestamp;
1502 empathy_chat_view_get_last_contact (EmpathyChatView *view)
1504 EmpathyChatViewPriv *priv;
1506 g_return_val_if_fail (EMPATHY_IS_CHAT_VIEW (view), NULL);
1508 priv = GET_PRIV (view);
1510 return priv->last_contact;