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;
77 GtkTextMark *find_mark_previous;
78 GtkTextMark *find_mark_next;
79 gboolean find_wrapped;
80 gboolean find_last_direction;
82 /* This is for the group chat so we know if the "other" last contact
83 * changed, so we know whether to insert a header or not.
85 EmpathyContact *last_contact;
87 guint notify_system_fonts_id;
88 guint notify_show_avatars_id;
91 static void empathy_chat_view_class_init (EmpathyChatViewClass *klass);
92 static void empathy_chat_view_init (EmpathyChatView *view);
93 static void chat_view_finalize (GObject *object);
94 static gboolean chat_view_drag_motion (GtkWidget *widget,
95 GdkDragContext *context,
99 static void chat_view_size_allocate (GtkWidget *widget,
100 GtkAllocation *alloc);
101 static void chat_view_setup_tags (EmpathyChatView *view);
102 static void chat_view_system_font_update (EmpathyChatView *view);
103 static void chat_view_notify_system_font_cb (EmpathyConf *conf,
106 static void chat_view_notify_show_avatars_cb (EmpathyConf *conf,
109 static void chat_view_populate_popup (EmpathyChatView *view,
112 static gboolean chat_view_event_cb (EmpathyChatView *view,
113 GdkEventMotion *event,
115 static gboolean chat_view_url_event_cb (GtkTextTag *tag,
119 GtkTextBuffer *buffer);
120 static void chat_view_open_address_cb (GtkMenuItem *menuitem,
122 static void chat_view_copy_address_cb (GtkMenuItem *menuitem,
124 static void chat_view_clear_view_cb (GtkMenuItem *menuitem,
125 EmpathyChatView *view);
126 static gboolean chat_view_is_scrolled_down (EmpathyChatView *view);
127 static void chat_view_theme_changed_cb (EmpathyThemeManager *manager,
128 EmpathyChatView *view);
129 static void chat_view_theme_notify_cb (EmpathyTheme *theme,
131 EmpathyChatView *view);
133 G_DEFINE_TYPE (EmpathyChatView, empathy_chat_view, GTK_TYPE_TEXT_VIEW);
136 empathy_chat_view_class_init (EmpathyChatViewClass *klass)
138 GObjectClass *object_class = G_OBJECT_CLASS (klass);
139 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
141 object_class->finalize = chat_view_finalize;
142 widget_class->size_allocate = chat_view_size_allocate;
143 widget_class->drag_motion = chat_view_drag_motion;
145 g_type_class_add_private (object_class, sizeof (EmpathyChatViewPriv));
149 empathy_chat_view_init (EmpathyChatView *view)
151 EmpathyChatViewPriv *priv;
152 gboolean show_avatars;
154 priv = GET_PRIV (view);
156 priv->buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
157 priv->last_timestamp = 0;
158 priv->allow_scrolling = TRUE;
161 "wrap-mode", GTK_WRAP_WORD_CHAR,
163 "cursor-visible", FALSE,
166 priv->notify_system_fonts_id =
167 empathy_conf_notify_add (empathy_conf_get (),
168 "/desktop/gnome/interface/document_font_name",
169 chat_view_notify_system_font_cb,
171 chat_view_system_font_update (view);
173 priv->notify_show_avatars_id =
174 empathy_conf_notify_add (empathy_conf_get (),
175 EMPATHY_PREFS_UI_SHOW_AVATARS,
176 chat_view_notify_show_avatars_cb,
179 chat_view_setup_tags (view);
181 empathy_theme_manager_apply_saved (empathy_theme_manager_get (), view);
183 show_avatars = FALSE;
184 empathy_conf_get_bool (empathy_conf_get (),
185 EMPATHY_PREFS_UI_SHOW_AVATARS,
188 empathy_theme_set_show_avatars (priv->theme, show_avatars);
190 g_signal_connect (view,
192 G_CALLBACK (chat_view_populate_popup),
195 g_signal_connect_object (empathy_theme_manager_get (),
197 G_CALLBACK (chat_view_theme_changed_cb),
203 chat_view_finalize (GObject *object)
205 EmpathyChatView *view;
206 EmpathyChatViewPriv *priv;
208 view = EMPATHY_CHAT_VIEW (object);
209 priv = GET_PRIV (view);
211 empathy_debug (DEBUG_DOMAIN, "finalize: %p", object);
213 empathy_conf_notify_remove (empathy_conf_get (), priv->notify_system_fonts_id);
214 empathy_conf_notify_remove (empathy_conf_get (), priv->notify_show_avatars_id);
216 if (priv->last_contact) {
217 g_object_unref (priv->last_contact);
219 if (priv->scroll_time) {
220 g_timer_destroy (priv->scroll_time);
222 if (priv->scroll_timeout) {
223 g_source_remove (priv->scroll_timeout);
227 g_signal_handlers_disconnect_by_func (priv->theme,
228 chat_view_theme_notify_cb,
230 g_object_unref (priv->theme);
233 G_OBJECT_CLASS (empathy_chat_view_parent_class)->finalize (object);
237 chat_view_drag_motion (GtkWidget *widget,
238 GdkDragContext *context,
243 /* Don't handle drag motion, since we don't want the view to scroll as
244 * the result of dragging something across it.
251 chat_view_size_allocate (GtkWidget *widget,
252 GtkAllocation *alloc)
256 down = chat_view_is_scrolled_down (EMPATHY_CHAT_VIEW (widget));
258 GTK_WIDGET_CLASS (empathy_chat_view_parent_class)->size_allocate (widget, alloc);
263 adj = GTK_TEXT_VIEW (widget)->vadjustment;
264 gtk_adjustment_set_value (adj, adj->upper - adj->page_size);
269 chat_view_setup_tags (EmpathyChatView *view)
271 EmpathyChatViewPriv *priv;
274 priv = GET_PRIV (view);
276 gtk_text_buffer_create_tag (priv->buffer,
280 /* FIXME: Move to the theme and come up with something that looks a bit
283 gtk_text_buffer_create_tag (priv->buffer,
285 "background", "yellow",
288 tag = gtk_text_buffer_create_tag (priv->buffer,
292 g_signal_connect (tag,
294 G_CALLBACK (chat_view_url_event_cb),
297 g_signal_connect (view,
298 "motion-notify-event",
299 G_CALLBACK (chat_view_event_cb),
304 chat_view_system_font_update (EmpathyChatView *view)
306 PangoFontDescription *font_description = NULL;
309 if (empathy_conf_get_string (empathy_conf_get (),
310 "/desktop/gnome/interface/document_font_name",
311 &font_name) && font_name) {
312 font_description = pango_font_description_from_string (font_name);
315 font_description = NULL;
318 gtk_widget_modify_font (GTK_WIDGET (view), font_description);
320 if (font_description) {
321 pango_font_description_free (font_description);
326 chat_view_notify_system_font_cb (EmpathyConf *conf,
330 EmpathyChatView *view;
331 EmpathyChatViewPriv *priv;
332 gboolean show_avatars = FALSE;
335 priv = GET_PRIV (view);
337 chat_view_system_font_update (view);
339 /* Ugly, again, to adjust the vertical position of the nick... Will fix
340 * this when reworking the theme manager so that view register
341 * themselves with it instead of the other way around.
343 empathy_conf_get_bool (conf,
344 EMPATHY_PREFS_UI_SHOW_AVATARS,
347 empathy_theme_set_show_avatars (priv->theme, show_avatars);
351 chat_view_notify_show_avatars_cb (EmpathyConf *conf,
355 EmpathyChatView *view;
356 EmpathyChatViewPriv *priv;
357 gboolean show_avatars = FALSE;
360 priv = GET_PRIV (view);
362 empathy_conf_get_bool (conf, key, &show_avatars);
364 empathy_theme_set_show_avatars (priv->theme, show_avatars);
368 chat_view_populate_popup (EmpathyChatView *view,
372 EmpathyChatViewPriv *priv;
373 GtkTextTagTable *table;
376 GtkTextIter iter, start, end;
380 priv = GET_PRIV (view);
382 /* Clear menu item */
383 if (gtk_text_buffer_get_char_count (priv->buffer) > 0) {
384 item = gtk_menu_item_new ();
385 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
386 gtk_widget_show (item);
388 item = gtk_image_menu_item_new_from_stock (GTK_STOCK_CLEAR, NULL);
389 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
390 gtk_widget_show (item);
392 g_signal_connect (item,
394 G_CALLBACK (chat_view_clear_view_cb),
398 /* Link context menu items */
399 table = gtk_text_buffer_get_tag_table (priv->buffer);
400 tag = gtk_text_tag_table_lookup (table, "link");
402 gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
404 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
405 GTK_TEXT_WINDOW_WIDGET,
409 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &iter, x, y);
413 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
414 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
415 str = gtk_text_buffer_get_text (priv->buffer,
416 &start, &end, FALSE);
419 if (G_STR_EMPTY (str)) {
424 /* NOTE: Set data just to get the string freed when not needed. */
425 g_object_set_data_full (G_OBJECT (menu),
427 (GDestroyNotify) g_free);
429 item = gtk_menu_item_new ();
430 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
431 gtk_widget_show (item);
433 item = gtk_menu_item_new_with_mnemonic (_("_Copy Link Address"));
434 g_signal_connect (item,
436 G_CALLBACK (chat_view_copy_address_cb),
438 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
439 gtk_widget_show (item);
441 item = gtk_menu_item_new_with_mnemonic (_("_Open Link"));
442 g_signal_connect (item,
444 G_CALLBACK (chat_view_open_address_cb),
446 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
447 gtk_widget_show (item);
451 chat_view_event_cb (EmpathyChatView *view,
452 GdkEventMotion *event,
455 static GdkCursor *hand = NULL;
456 static GdkCursor *beam = NULL;
457 GtkTextWindowType type;
460 gint x, y, buf_x, buf_y;
462 type = gtk_text_view_get_window_type (GTK_TEXT_VIEW (view),
465 if (type != GTK_TEXT_WINDOW_TEXT) {
469 /* Get where the pointer really is. */
470 win = gtk_text_view_get_window (GTK_TEXT_VIEW (view), type);
475 gdk_window_get_pointer (win, &x, &y, NULL);
477 /* Get the iter where the cursor is at */
478 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view), type,
482 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view),
486 if (gtk_text_iter_has_tag (&iter, tag)) {
488 hand = gdk_cursor_new (GDK_HAND2);
489 beam = gdk_cursor_new (GDK_XTERM);
491 gdk_window_set_cursor (win, hand);
494 beam = gdk_cursor_new (GDK_XTERM);
496 gdk_window_set_cursor (win, beam);
503 chat_view_url_event_cb (GtkTextTag *tag,
507 GtkTextBuffer *buffer)
509 GtkTextIter start, end;
512 /* If the link is being selected, don't do anything. */
513 gtk_text_buffer_get_selection_bounds (buffer, &start, &end);
514 if (gtk_text_iter_get_offset (&start) != gtk_text_iter_get_offset (&end)) {
518 if (event->type == GDK_BUTTON_RELEASE && event->button.button == 1) {
521 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
522 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
523 str = gtk_text_buffer_get_text (buffer,
528 empathy_url_show (str);
537 chat_view_open_address_cb (GtkMenuItem *menuitem, const gchar *url)
539 empathy_url_show (url);
543 chat_view_copy_address_cb (GtkMenuItem *menuitem, const gchar *url)
545 GtkClipboard *clipboard;
547 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
548 gtk_clipboard_set_text (clipboard, url, -1);
550 clipboard = gtk_clipboard_get (GDK_SELECTION_PRIMARY);
551 gtk_clipboard_set_text (clipboard, url, -1);
555 chat_view_clear_view_cb (GtkMenuItem *menuitem, EmpathyChatView *view)
557 empathy_chat_view_clear (view);
561 chat_view_is_scrolled_down (EmpathyChatView *view)
565 sw = gtk_widget_get_parent (GTK_WIDGET (view));
566 if (GTK_IS_SCROLLED_WINDOW (sw)) {
569 vadj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (sw));
571 if (vadj->value + vadj->page_size / 2 < vadj->upper - vadj->page_size) {
580 chat_view_maybe_trim_buffer (EmpathyChatView *view)
582 EmpathyChatViewPriv *priv;
583 GtkTextIter top, bottom;
586 GtkTextTagTable *table;
589 priv = GET_PRIV (view);
591 gtk_text_buffer_get_end_iter (priv->buffer, &bottom);
592 line = gtk_text_iter_get_line (&bottom);
593 if (line < MAX_LINES) {
597 remove = line - MAX_LINES;
598 gtk_text_buffer_get_start_iter (priv->buffer, &top);
601 if (!gtk_text_iter_forward_lines (&bottom, remove)) {
605 /* Track backwords to a place where we can safely cut, we don't do it in
606 * the middle of a tag.
608 table = gtk_text_buffer_get_tag_table (priv->buffer);
609 tag = gtk_text_tag_table_lookup (table, "cut");
614 if (!gtk_text_iter_forward_to_tag_toggle (&bottom, tag)) {
618 if (!gtk_text_iter_equal (&top, &bottom)) {
619 gtk_text_buffer_delete (priv->buffer, &top, &bottom);
624 chat_view_theme_changed_cb (EmpathyThemeManager *manager,
625 EmpathyChatView *view)
627 EmpathyChatViewPriv *priv;
628 gboolean show_avatars = FALSE;
630 priv = GET_PRIV (view);
632 empathy_theme_manager_apply_saved (manager, view);
634 /* Needed for now to update the "rise" property of the names to get it
635 * vertically centered.
637 empathy_conf_get_bool (empathy_conf_get (),
638 EMPATHY_PREFS_UI_SHOW_AVATARS,
640 empathy_theme_set_show_avatars (priv->theme, show_avatars);
643 /* Pads a pixbuf to the specified size, by centering it in a larger transparent
644 * pixbuf. Returns a new ref.
647 chat_view_pad_to_size (GdkPixbuf *pixbuf,
650 gint extra_padding_right)
652 gint src_width, src_height;
654 gint x_offset, y_offset;
656 src_width = gdk_pixbuf_get_width (pixbuf);
657 src_height = gdk_pixbuf_get_height (pixbuf);
659 x_offset = (width - src_width) / 2;
660 y_offset = (height - src_height) / 2;
662 padded = gdk_pixbuf_new (gdk_pixbuf_get_colorspace (pixbuf),
664 gdk_pixbuf_get_bits_per_sample (pixbuf),
665 width + extra_padding_right,
668 gdk_pixbuf_fill (padded, 0);
670 gdk_pixbuf_copy_area (pixbuf,
671 0, /* source coords */
676 x_offset, /* dest coords */
688 chat_view_avatar_cache_data_free (gpointer ptr)
690 AvatarData *data = ptr;
692 g_object_unref (data->pixbuf);
693 g_free (data->token);
694 g_slice_free (AvatarData, data);
698 empathy_chat_view_get_avatar_pixbuf_with_cache (EmpathyContact *contact)
701 EmpathyAvatar *avatar;
702 GdkPixbuf *tmp_pixbuf;
703 GdkPixbuf *pixbuf = NULL;
705 /* Check if avatar is in cache and if it's up to date */
706 avatar = empathy_contact_get_avatar (contact);
707 data = g_object_get_data (G_OBJECT (contact), "chat-view-avatar-cache");
709 if (avatar && !tp_strdiff (avatar->token, data->token)) {
710 /* We have the avatar in cache */
715 /* Avatar not in cache, create pixbuf */
716 tmp_pixbuf = empathy_pixbuf_avatar_from_contact_scaled (contact, 32, 32);
718 pixbuf = chat_view_pad_to_size (tmp_pixbuf, 32, 32, 6);
719 g_object_unref (tmp_pixbuf);
725 /* Insert new pixbuf in cache */
726 data = g_slice_new0 (AvatarData);
727 data->token = g_strdup (avatar->token);
728 data->pixbuf = pixbuf;
730 g_object_set_data_full (G_OBJECT (contact), "chat-view-avatar-cache",
731 data, chat_view_avatar_cache_data_free);
737 empathy_chat_view_new (void)
739 return g_object_new (EMPATHY_TYPE_CHAT_VIEW, NULL);
742 /* Code stolen from pidgin/gtkimhtml.c */
744 chat_view_scroll_cb (EmpathyChatView *view)
746 EmpathyChatViewPriv *priv;
750 priv = GET_PRIV (view);
751 adj = GTK_TEXT_VIEW (view)->vadjustment;
752 max_val = adj->upper - adj->page_size;
754 g_return_val_if_fail (priv->scroll_time != NULL, FALSE);
756 if (g_timer_elapsed (priv->scroll_time, NULL) > MAX_SCROLL_TIME) {
757 /* time's up. jump to the end and kill the timer */
758 gtk_adjustment_set_value (adj, max_val);
759 g_timer_destroy (priv->scroll_time);
760 priv->scroll_time = NULL;
761 priv->scroll_timeout = 0;
765 /* scroll by 1/3rd the remaining distance */
766 gtk_adjustment_set_value (adj, gtk_adjustment_get_value (adj) + ((max_val - gtk_adjustment_get_value (adj)) / 3));
771 empathy_chat_view_scroll_down (EmpathyChatView *view)
773 EmpathyChatViewPriv *priv;
775 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
777 priv = GET_PRIV (view);
779 if (!priv->allow_scrolling) {
783 empathy_debug (DEBUG_DOMAIN, "Scrolling down");
785 if (priv->scroll_time) {
786 g_timer_reset (priv->scroll_time);
788 priv->scroll_time = g_timer_new();
790 if (!priv->scroll_timeout) {
791 priv->scroll_timeout = g_timeout_add (SCROLL_DELAY,
792 (GSourceFunc) chat_view_scroll_cb,
798 empathy_chat_view_append_message (EmpathyChatView *view,
801 EmpathyChatViewPriv *priv = GET_PRIV (view);
804 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
805 g_return_if_fail (EMPATHY_IS_MESSAGE (msg));
807 if (!empathy_message_get_body (msg)) {
811 bottom = chat_view_is_scrolled_down (view);
813 chat_view_maybe_trim_buffer (view);
815 empathy_theme_append_message (priv->theme, view, msg);
818 empathy_chat_view_scroll_down (view);
821 if (priv->last_contact) {
822 g_object_unref (priv->last_contact);
824 priv->last_contact = g_object_ref (empathy_message_get_sender (msg));
828 empathy_chat_view_append_event (EmpathyChatView *view,
831 EmpathyChatViewPriv *priv;
834 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
835 g_return_if_fail (!G_STR_EMPTY (str));
837 priv = GET_PRIV (view);
839 bottom = chat_view_is_scrolled_down (view);
841 chat_view_maybe_trim_buffer (view);
843 empathy_theme_append_event (priv->theme, view, str);
846 empathy_chat_view_scroll_down (view);
849 if (priv->last_contact) {
850 g_object_unref (priv->last_contact);
851 priv->last_contact = NULL;
856 empathy_chat_view_append_button (EmpathyChatView *view,
857 const gchar *message,
861 EmpathyChatViewPriv *priv;
862 GtkTextChildAnchor *anchor;
867 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
868 g_return_if_fail (button1 != NULL);
870 priv = GET_PRIV (view);
874 bottom = chat_view_is_scrolled_down (view);
876 empathy_theme_append_timestamp (priv->theme, view, NULL, TRUE, TRUE);
879 empathy_theme_append_text (priv->theme, view, message, tag, NULL);
882 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
884 anchor = gtk_text_buffer_create_child_anchor (priv->buffer, &iter);
885 gtk_text_view_add_child_at_anchor (GTK_TEXT_VIEW (view), button1, anchor);
886 gtk_widget_show (button1);
888 gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
896 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
898 anchor = gtk_text_buffer_create_child_anchor (priv->buffer, &iter);
899 gtk_text_view_add_child_at_anchor (GTK_TEXT_VIEW (view), button2, anchor);
900 gtk_widget_show (button2);
902 gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
910 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
911 gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
919 empathy_chat_view_scroll_down (view);
922 if (priv->last_contact) {
923 g_object_unref (priv->last_contact);
924 priv->last_contact = NULL;
929 empathy_chat_view_scroll (EmpathyChatView *view,
930 gboolean allow_scrolling)
932 EmpathyChatViewPriv *priv = GET_PRIV (view);
934 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
936 empathy_debug (DEBUG_DOMAIN, "Scrolling %s",
937 allow_scrolling ? "enabled" : "disabled");
939 priv->allow_scrolling = allow_scrolling;
940 if (allow_scrolling) {
941 empathy_chat_view_scroll_down (view);
946 empathy_chat_view_get_selection_bounds (EmpathyChatView *view,
950 GtkTextBuffer *buffer;
952 g_return_val_if_fail (EMPATHY_IS_CHAT_VIEW (view), FALSE);
954 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
956 return gtk_text_buffer_get_selection_bounds (buffer, start, end);
960 empathy_chat_view_clear (EmpathyChatView *view)
962 GtkTextBuffer *buffer;
963 EmpathyChatViewPriv *priv;
965 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
967 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
968 gtk_text_buffer_set_text (buffer, "", -1);
970 /* We set these back to the initial values so we get
971 * timestamps when clearing the window to know when
972 * conversations start.
974 priv = GET_PRIV (view);
976 priv->last_timestamp = 0;
980 empathy_chat_view_find_previous (EmpathyChatView *view,
981 const gchar *search_criteria,
984 EmpathyChatViewPriv *priv;
985 GtkTextBuffer *buffer;
986 GtkTextIter iter_at_mark;
987 GtkTextIter iter_match_start;
988 GtkTextIter iter_match_end;
990 gboolean from_start = FALSE;
992 g_return_val_if_fail (EMPATHY_IS_CHAT_VIEW (view), FALSE);
993 g_return_val_if_fail (search_criteria != NULL, FALSE);
995 priv = GET_PRIV (view);
997 buffer = priv->buffer;
999 if (G_STR_EMPTY (search_criteria)) {
1000 if (priv->find_mark_previous) {
1001 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1003 gtk_text_buffer_move_mark (buffer,
1004 priv->find_mark_previous,
1006 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
1007 priv->find_mark_previous,
1012 gtk_text_buffer_select_range (buffer,
1024 if (priv->find_mark_previous) {
1025 gtk_text_buffer_get_iter_at_mark (buffer,
1027 priv->find_mark_previous);
1029 gtk_text_buffer_get_end_iter (buffer, &iter_at_mark);
1033 priv->find_last_direction = FALSE;
1035 found = empathy_text_iter_backward_search (&iter_at_mark,
1042 gboolean result = FALSE;
1048 /* Here we wrap around. */
1049 if (!new_search && !priv->find_wrapped) {
1050 priv->find_wrapped = TRUE;
1051 result = empathy_chat_view_find_previous (view,
1054 priv->find_wrapped = FALSE;
1060 /* Set new mark and show on screen */
1061 if (!priv->find_mark_previous) {
1062 priv->find_mark_previous = gtk_text_buffer_create_mark (buffer, NULL,
1066 gtk_text_buffer_move_mark (buffer,
1067 priv->find_mark_previous,
1071 if (!priv->find_mark_next) {
1072 priv->find_mark_next = gtk_text_buffer_create_mark (buffer, NULL,
1076 gtk_text_buffer_move_mark (buffer,
1077 priv->find_mark_next,
1081 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
1082 priv->find_mark_previous,
1088 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &iter_match_start);
1089 gtk_text_buffer_move_mark_by_name (buffer, "insert", &iter_match_end);
1095 empathy_chat_view_find_next (EmpathyChatView *view,
1096 const gchar *search_criteria,
1097 gboolean new_search)
1099 EmpathyChatViewPriv *priv;
1100 GtkTextBuffer *buffer;
1101 GtkTextIter iter_at_mark;
1102 GtkTextIter iter_match_start;
1103 GtkTextIter iter_match_end;
1105 gboolean from_start = FALSE;
1107 g_return_val_if_fail (EMPATHY_IS_CHAT_VIEW (view), FALSE);
1108 g_return_val_if_fail (search_criteria != NULL, FALSE);
1110 priv = GET_PRIV (view);
1112 buffer = priv->buffer;
1114 if (G_STR_EMPTY (search_criteria)) {
1115 if (priv->find_mark_next) {
1116 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1118 gtk_text_buffer_move_mark (buffer,
1119 priv->find_mark_next,
1121 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
1122 priv->find_mark_next,
1127 gtk_text_buffer_select_range (buffer,
1139 if (priv->find_mark_next) {
1140 gtk_text_buffer_get_iter_at_mark (buffer,
1142 priv->find_mark_next);
1144 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1148 priv->find_last_direction = TRUE;
1150 found = empathy_text_iter_forward_search (&iter_at_mark,
1157 gboolean result = FALSE;
1163 /* Here we wrap around. */
1164 if (!new_search && !priv->find_wrapped) {
1165 priv->find_wrapped = TRUE;
1166 result = empathy_chat_view_find_next (view,
1169 priv->find_wrapped = FALSE;
1175 /* Set new mark and show on screen */
1176 if (!priv->find_mark_next) {
1177 priv->find_mark_next = gtk_text_buffer_create_mark (buffer, NULL,
1181 gtk_text_buffer_move_mark (buffer,
1182 priv->find_mark_next,
1186 if (!priv->find_mark_previous) {
1187 priv->find_mark_previous = gtk_text_buffer_create_mark (buffer, NULL,
1191 gtk_text_buffer_move_mark (buffer,
1192 priv->find_mark_previous,
1196 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
1197 priv->find_mark_next,
1203 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &iter_match_start);
1204 gtk_text_buffer_move_mark_by_name (buffer, "insert", &iter_match_end);
1211 empathy_chat_view_find_abilities (EmpathyChatView *view,
1212 const gchar *search_criteria,
1213 gboolean *can_do_previous,
1214 gboolean *can_do_next)
1216 EmpathyChatViewPriv *priv;
1217 GtkTextBuffer *buffer;
1218 GtkTextIter iter_at_mark;
1219 GtkTextIter iter_match_start;
1220 GtkTextIter iter_match_end;
1222 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
1223 g_return_if_fail (search_criteria != NULL);
1224 g_return_if_fail (can_do_previous != NULL && can_do_next != NULL);
1226 priv = GET_PRIV (view);
1228 buffer = priv->buffer;
1230 if (can_do_previous) {
1231 if (priv->find_mark_previous) {
1232 gtk_text_buffer_get_iter_at_mark (buffer,
1234 priv->find_mark_previous);
1236 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1239 *can_do_previous = empathy_text_iter_backward_search (&iter_at_mark,
1247 if (priv->find_mark_next) {
1248 gtk_text_buffer_get_iter_at_mark (buffer,
1250 priv->find_mark_next);
1252 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1255 *can_do_next = empathy_text_iter_forward_search (&iter_at_mark,
1264 empathy_chat_view_highlight (EmpathyChatView *view,
1267 GtkTextBuffer *buffer;
1269 GtkTextIter iter_start;
1270 GtkTextIter iter_end;
1271 GtkTextIter iter_match_start;
1272 GtkTextIter iter_match_end;
1275 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
1277 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
1279 gtk_text_buffer_get_start_iter (buffer, &iter);
1281 gtk_text_buffer_get_bounds (buffer, &iter_start, &iter_end);
1282 gtk_text_buffer_remove_tag_by_name (buffer, "highlight",
1286 if (G_STR_EMPTY (text)) {
1291 found = empathy_text_iter_forward_search (&iter,
1301 gtk_text_buffer_apply_tag_by_name (buffer, "highlight",
1305 iter = iter_match_end;
1306 gtk_text_iter_forward_char (&iter);
1311 empathy_chat_view_copy_clipboard (EmpathyChatView *view)
1313 GtkTextBuffer *buffer;
1314 GtkClipboard *clipboard;
1316 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
1318 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
1319 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1321 gtk_text_buffer_copy_clipboard (buffer, clipboard);
1325 empathy_chat_view_get_theme (EmpathyChatView *view)
1327 EmpathyChatViewPriv *priv;
1329 g_return_val_if_fail (EMPATHY_IS_CHAT_VIEW (view), NULL);
1331 priv = GET_PRIV (view);
1337 chat_view_theme_notify_cb (EmpathyTheme *theme,
1339 EmpathyChatView *view)
1341 empathy_theme_update_view (theme, view);
1345 empathy_chat_view_set_theme (EmpathyChatView *view, EmpathyTheme *theme)
1347 EmpathyChatViewPriv *priv;
1349 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
1350 g_return_if_fail (EMPATHY_IS_THEME (theme));
1352 priv = GET_PRIV (view);
1355 g_signal_handlers_disconnect_by_func (priv->theme,
1356 chat_view_theme_notify_cb,
1358 g_object_unref (priv->theme);
1361 priv->theme = g_object_ref (theme);
1363 empathy_theme_update_view (theme, view);
1364 g_signal_connect (priv->theme, "notify",
1365 G_CALLBACK (chat_view_theme_notify_cb),
1368 /* FIXME: Redraw all messages using the new theme */
1372 empathy_chat_view_set_margin (EmpathyChatView *view,
1375 EmpathyChatViewPriv *priv;
1377 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
1379 priv = GET_PRIV (view);
1382 "left-margin", margin,
1383 "right-margin", margin,
1388 empathy_chat_view_get_smiley_menu (GCallback callback,
1391 EmpathySmileyManager *smiley_manager;
1392 GSList *smileys, *l;
1397 g_return_val_if_fail (callback != NULL, NULL);
1399 menu = gtk_menu_new ();
1401 smiley_manager = empathy_smiley_manager_new ();
1402 smileys = empathy_smiley_manager_get_all (smiley_manager);
1403 for (l = smileys; l; l = l->next) {
1404 EmpathySmiley *smiley;
1409 image = gtk_image_new_from_pixbuf (smiley->pixbuf);
1411 item = gtk_image_menu_item_new_with_label ("");
1412 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1414 gtk_menu_attach (GTK_MENU (menu), item,
1415 x, x + 1, y, y + 1);
1417 gtk_widget_set_tooltip_text (item, smiley->str);
1419 g_object_set_data (G_OBJECT (item), "smiley_text", smiley->str);
1420 g_signal_connect (item, "activate", callback, user_data);
1429 g_object_unref (smiley_manager);
1431 gtk_widget_show_all (menu);
1437 empathy_chat_view_get_last_timestamp (EmpathyChatView *view)
1439 EmpathyChatViewPriv *priv;
1441 g_return_val_if_fail (EMPATHY_IS_CHAT_VIEW (view), 0);
1443 priv = GET_PRIV (view);
1445 return priv->last_timestamp;
1449 empathy_chat_view_set_last_timestamp (EmpathyChatView *view,
1452 EmpathyChatViewPriv *priv;
1454 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
1456 priv = GET_PRIV (view);
1458 priv->last_timestamp = timestamp;
1462 empathy_chat_view_get_last_contact (EmpathyChatView *view)
1464 EmpathyChatViewPriv *priv;
1466 g_return_val_if_fail (EMPATHY_IS_CHAT_VIEW (view), NULL);
1468 priv = GET_PRIV (view);
1470 return priv->last_contact;