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>
47 #include "empathy-chat-view.h"
48 #include "empathy-chat.h"
49 #include "empathy-conf.h"
50 #include "empathy-theme-manager.h"
51 #include "empathy-ui-utils.h"
52 #include "empathy-smiley-manager.h"
54 #define DEBUG_FLAG EMPATHY_DEBUG_CHAT
55 #include <libempathy/empathy-debug.h>
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 DEBUG ("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 DEBUG ("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 DEBUG ("Scrolling %s", allow_scrolling ? "enabled" : "disabled");
938 priv->allow_scrolling = allow_scrolling;
939 if (allow_scrolling) {
940 empathy_chat_view_scroll_down (view);
945 empathy_chat_view_get_selection_bounds (EmpathyChatView *view,
949 GtkTextBuffer *buffer;
951 g_return_val_if_fail (EMPATHY_IS_CHAT_VIEW (view), FALSE);
953 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
955 return gtk_text_buffer_get_selection_bounds (buffer, start, end);
959 empathy_chat_view_clear (EmpathyChatView *view)
961 GtkTextBuffer *buffer;
962 EmpathyChatViewPriv *priv;
964 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
966 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
967 gtk_text_buffer_set_text (buffer, "", -1);
969 /* We set these back to the initial values so we get
970 * timestamps when clearing the window to know when
971 * conversations start.
973 priv = GET_PRIV (view);
975 priv->last_timestamp = 0;
979 empathy_chat_view_find_previous (EmpathyChatView *view,
980 const gchar *search_criteria,
983 EmpathyChatViewPriv *priv;
984 GtkTextBuffer *buffer;
985 GtkTextIter iter_at_mark;
986 GtkTextIter iter_match_start;
987 GtkTextIter iter_match_end;
989 gboolean from_start = FALSE;
991 g_return_val_if_fail (EMPATHY_IS_CHAT_VIEW (view), FALSE);
992 g_return_val_if_fail (search_criteria != NULL, FALSE);
994 priv = GET_PRIV (view);
996 buffer = priv->buffer;
998 if (G_STR_EMPTY (search_criteria)) {
999 if (priv->find_mark_previous) {
1000 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1002 gtk_text_buffer_move_mark (buffer,
1003 priv->find_mark_previous,
1005 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
1006 priv->find_mark_previous,
1011 gtk_text_buffer_select_range (buffer,
1023 if (priv->find_mark_previous) {
1024 gtk_text_buffer_get_iter_at_mark (buffer,
1026 priv->find_mark_previous);
1028 gtk_text_buffer_get_end_iter (buffer, &iter_at_mark);
1032 priv->find_last_direction = FALSE;
1034 found = empathy_text_iter_backward_search (&iter_at_mark,
1041 gboolean result = FALSE;
1047 /* Here we wrap around. */
1048 if (!new_search && !priv->find_wrapped) {
1049 priv->find_wrapped = TRUE;
1050 result = empathy_chat_view_find_previous (view,
1053 priv->find_wrapped = FALSE;
1059 /* Set new mark and show on screen */
1060 if (!priv->find_mark_previous) {
1061 priv->find_mark_previous = gtk_text_buffer_create_mark (buffer, NULL,
1065 gtk_text_buffer_move_mark (buffer,
1066 priv->find_mark_previous,
1070 if (!priv->find_mark_next) {
1071 priv->find_mark_next = gtk_text_buffer_create_mark (buffer, NULL,
1075 gtk_text_buffer_move_mark (buffer,
1076 priv->find_mark_next,
1080 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
1081 priv->find_mark_previous,
1087 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &iter_match_start);
1088 gtk_text_buffer_move_mark_by_name (buffer, "insert", &iter_match_end);
1094 empathy_chat_view_find_next (EmpathyChatView *view,
1095 const gchar *search_criteria,
1096 gboolean new_search)
1098 EmpathyChatViewPriv *priv;
1099 GtkTextBuffer *buffer;
1100 GtkTextIter iter_at_mark;
1101 GtkTextIter iter_match_start;
1102 GtkTextIter iter_match_end;
1104 gboolean from_start = FALSE;
1106 g_return_val_if_fail (EMPATHY_IS_CHAT_VIEW (view), FALSE);
1107 g_return_val_if_fail (search_criteria != NULL, FALSE);
1109 priv = GET_PRIV (view);
1111 buffer = priv->buffer;
1113 if (G_STR_EMPTY (search_criteria)) {
1114 if (priv->find_mark_next) {
1115 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1117 gtk_text_buffer_move_mark (buffer,
1118 priv->find_mark_next,
1120 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
1121 priv->find_mark_next,
1126 gtk_text_buffer_select_range (buffer,
1138 if (priv->find_mark_next) {
1139 gtk_text_buffer_get_iter_at_mark (buffer,
1141 priv->find_mark_next);
1143 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1147 priv->find_last_direction = TRUE;
1149 found = empathy_text_iter_forward_search (&iter_at_mark,
1156 gboolean result = FALSE;
1162 /* Here we wrap around. */
1163 if (!new_search && !priv->find_wrapped) {
1164 priv->find_wrapped = TRUE;
1165 result = empathy_chat_view_find_next (view,
1168 priv->find_wrapped = FALSE;
1174 /* Set new mark and show on screen */
1175 if (!priv->find_mark_next) {
1176 priv->find_mark_next = gtk_text_buffer_create_mark (buffer, NULL,
1180 gtk_text_buffer_move_mark (buffer,
1181 priv->find_mark_next,
1185 if (!priv->find_mark_previous) {
1186 priv->find_mark_previous = gtk_text_buffer_create_mark (buffer, NULL,
1190 gtk_text_buffer_move_mark (buffer,
1191 priv->find_mark_previous,
1195 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
1196 priv->find_mark_next,
1202 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &iter_match_start);
1203 gtk_text_buffer_move_mark_by_name (buffer, "insert", &iter_match_end);
1210 empathy_chat_view_find_abilities (EmpathyChatView *view,
1211 const gchar *search_criteria,
1212 gboolean *can_do_previous,
1213 gboolean *can_do_next)
1215 EmpathyChatViewPriv *priv;
1216 GtkTextBuffer *buffer;
1217 GtkTextIter iter_at_mark;
1218 GtkTextIter iter_match_start;
1219 GtkTextIter iter_match_end;
1221 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
1222 g_return_if_fail (search_criteria != NULL);
1223 g_return_if_fail (can_do_previous != NULL && can_do_next != NULL);
1225 priv = GET_PRIV (view);
1227 buffer = priv->buffer;
1229 if (can_do_previous) {
1230 if (priv->find_mark_previous) {
1231 gtk_text_buffer_get_iter_at_mark (buffer,
1233 priv->find_mark_previous);
1235 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1238 *can_do_previous = empathy_text_iter_backward_search (&iter_at_mark,
1246 if (priv->find_mark_next) {
1247 gtk_text_buffer_get_iter_at_mark (buffer,
1249 priv->find_mark_next);
1251 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1254 *can_do_next = empathy_text_iter_forward_search (&iter_at_mark,
1263 empathy_chat_view_highlight (EmpathyChatView *view,
1266 GtkTextBuffer *buffer;
1268 GtkTextIter iter_start;
1269 GtkTextIter iter_end;
1270 GtkTextIter iter_match_start;
1271 GtkTextIter iter_match_end;
1274 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
1276 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
1278 gtk_text_buffer_get_start_iter (buffer, &iter);
1280 gtk_text_buffer_get_bounds (buffer, &iter_start, &iter_end);
1281 gtk_text_buffer_remove_tag_by_name (buffer, "highlight",
1285 if (G_STR_EMPTY (text)) {
1290 found = empathy_text_iter_forward_search (&iter,
1300 gtk_text_buffer_apply_tag_by_name (buffer, "highlight",
1304 iter = iter_match_end;
1305 gtk_text_iter_forward_char (&iter);
1310 empathy_chat_view_copy_clipboard (EmpathyChatView *view)
1312 GtkTextBuffer *buffer;
1313 GtkClipboard *clipboard;
1315 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
1317 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
1318 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1320 gtk_text_buffer_copy_clipboard (buffer, clipboard);
1324 empathy_chat_view_get_theme (EmpathyChatView *view)
1326 EmpathyChatViewPriv *priv;
1328 g_return_val_if_fail (EMPATHY_IS_CHAT_VIEW (view), NULL);
1330 priv = GET_PRIV (view);
1336 chat_view_theme_notify_cb (EmpathyTheme *theme,
1338 EmpathyChatView *view)
1340 empathy_theme_update_view (theme, view);
1344 empathy_chat_view_set_theme (EmpathyChatView *view, EmpathyTheme *theme)
1346 EmpathyChatViewPriv *priv;
1348 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
1349 g_return_if_fail (EMPATHY_IS_THEME (theme));
1351 priv = GET_PRIV (view);
1354 g_signal_handlers_disconnect_by_func (priv->theme,
1355 chat_view_theme_notify_cb,
1357 g_object_unref (priv->theme);
1360 priv->theme = g_object_ref (theme);
1362 empathy_theme_update_view (theme, view);
1363 g_signal_connect (priv->theme, "notify",
1364 G_CALLBACK (chat_view_theme_notify_cb),
1367 /* FIXME: Redraw all messages using the new theme */
1371 empathy_chat_view_set_margin (EmpathyChatView *view,
1374 EmpathyChatViewPriv *priv;
1376 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
1378 priv = GET_PRIV (view);
1381 "left-margin", margin,
1382 "right-margin", margin,
1387 empathy_chat_view_get_smiley_menu (GCallback callback,
1390 EmpathySmileyManager *smiley_manager;
1391 GSList *smileys, *l;
1396 g_return_val_if_fail (callback != NULL, NULL);
1398 menu = gtk_menu_new ();
1400 smiley_manager = empathy_smiley_manager_new ();
1401 smileys = empathy_smiley_manager_get_all (smiley_manager);
1402 for (l = smileys; l; l = l->next) {
1403 EmpathySmiley *smiley;
1408 image = gtk_image_new_from_pixbuf (smiley->pixbuf);
1410 item = gtk_image_menu_item_new_with_label ("");
1411 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1413 gtk_menu_attach (GTK_MENU (menu), item,
1414 x, x + 1, y, y + 1);
1416 gtk_widget_set_tooltip_text (item, smiley->str);
1418 g_object_set_data (G_OBJECT (item), "smiley_text", smiley->str);
1419 g_signal_connect (item, "activate", callback, user_data);
1428 g_object_unref (smiley_manager);
1430 gtk_widget_show_all (menu);
1436 empathy_chat_view_get_last_timestamp (EmpathyChatView *view)
1438 EmpathyChatViewPriv *priv;
1440 g_return_val_if_fail (EMPATHY_IS_CHAT_VIEW (view), 0);
1442 priv = GET_PRIV (view);
1444 return priv->last_timestamp;
1448 empathy_chat_view_set_last_timestamp (EmpathyChatView *view,
1451 EmpathyChatViewPriv *priv;
1453 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
1455 priv = GET_PRIV (view);
1457 priv->last_timestamp = timestamp;
1461 empathy_chat_view_get_last_contact (EmpathyChatView *view)
1463 EmpathyChatViewPriv *priv;
1465 g_return_val_if_fail (EMPATHY_IS_CHAT_VIEW (view), NULL);
1467 priv = GET_PRIV (view);
1469 return priv->last_contact;