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) EMPATHY_GET_PRIV (obj, EmpathyChatView)
66 GtkTextBuffer *buffer;
70 time_t last_timestamp;
72 gboolean allow_scrolling;
76 GtkTextMark *find_mark_previous;
77 GtkTextMark *find_mark_next;
78 gboolean find_wrapped;
79 gboolean find_last_direction;
81 /* This is for the group chat so we know if the "other" last contact
82 * changed, so we know whether to insert a header or not.
84 EmpathyContact *last_contact;
86 guint notify_system_fonts_id;
87 guint notify_show_avatars_id;
88 } EmpathyChatViewPriv;
90 static void chat_view_finalize (GObject *object);
91 static gboolean chat_view_drag_motion (GtkWidget *widget,
92 GdkDragContext *context,
96 static void chat_view_size_allocate (GtkWidget *widget,
97 GtkAllocation *alloc);
98 static void chat_view_setup_tags (EmpathyChatView *view);
99 static void chat_view_system_font_update (EmpathyChatView *view);
100 static void chat_view_notify_system_font_cb (EmpathyConf *conf,
103 static void chat_view_notify_show_avatars_cb (EmpathyConf *conf,
106 static void chat_view_populate_popup (EmpathyChatView *view,
109 static gboolean chat_view_event_cb (EmpathyChatView *view,
110 GdkEventMotion *event,
112 static gboolean chat_view_url_event_cb (GtkTextTag *tag,
116 GtkTextBuffer *buffer);
117 static void chat_view_open_address_cb (GtkMenuItem *menuitem,
119 static void chat_view_copy_address_cb (GtkMenuItem *menuitem,
121 static void chat_view_clear_view_cb (GtkMenuItem *menuitem,
122 EmpathyChatView *view);
123 static gboolean chat_view_is_scrolled_down (EmpathyChatView *view);
124 static void chat_view_theme_changed_cb (EmpathyThemeManager *manager,
125 EmpathyChatView *view);
126 static void chat_view_theme_notify_cb (EmpathyTheme *theme,
128 EmpathyChatView *view);
130 G_DEFINE_TYPE (EmpathyChatView, empathy_chat_view, GTK_TYPE_TEXT_VIEW);
133 empathy_chat_view_class_init (EmpathyChatViewClass *klass)
135 GObjectClass *object_class = G_OBJECT_CLASS (klass);
136 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
138 object_class->finalize = chat_view_finalize;
139 widget_class->size_allocate = chat_view_size_allocate;
140 widget_class->drag_motion = chat_view_drag_motion;
142 g_type_class_add_private (object_class, sizeof (EmpathyChatViewPriv));
146 empathy_chat_view_init (EmpathyChatView *view)
148 gboolean show_avatars;
149 EmpathyChatViewPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (view,
150 EMPATHY_TYPE_CHAT_VIEW, EmpathyChatViewPriv);
153 priv->buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
154 priv->last_timestamp = 0;
155 priv->allow_scrolling = TRUE;
158 "wrap-mode", GTK_WRAP_WORD_CHAR,
160 "cursor-visible", FALSE,
163 priv->notify_system_fonts_id =
164 empathy_conf_notify_add (empathy_conf_get (),
165 "/desktop/gnome/interface/document_font_name",
166 chat_view_notify_system_font_cb,
168 chat_view_system_font_update (view);
170 priv->notify_show_avatars_id =
171 empathy_conf_notify_add (empathy_conf_get (),
172 EMPATHY_PREFS_UI_SHOW_AVATARS,
173 chat_view_notify_show_avatars_cb,
176 chat_view_setup_tags (view);
178 empathy_theme_manager_apply_saved (empathy_theme_manager_get (), view);
180 show_avatars = FALSE;
181 empathy_conf_get_bool (empathy_conf_get (),
182 EMPATHY_PREFS_UI_SHOW_AVATARS,
185 empathy_theme_set_show_avatars (priv->theme, show_avatars);
187 g_signal_connect (view,
189 G_CALLBACK (chat_view_populate_popup),
192 g_signal_connect_object (empathy_theme_manager_get (),
194 G_CALLBACK (chat_view_theme_changed_cb),
200 chat_view_finalize (GObject *object)
202 EmpathyChatView *view;
203 EmpathyChatViewPriv *priv;
205 view = EMPATHY_CHAT_VIEW (object);
206 priv = GET_PRIV (view);
208 DEBUG ("finalize: %p", object);
210 empathy_conf_notify_remove (empathy_conf_get (), priv->notify_system_fonts_id);
211 empathy_conf_notify_remove (empathy_conf_get (), priv->notify_show_avatars_id);
213 if (priv->last_contact) {
214 g_object_unref (priv->last_contact);
216 if (priv->scroll_time) {
217 g_timer_destroy (priv->scroll_time);
219 if (priv->scroll_timeout) {
220 g_source_remove (priv->scroll_timeout);
224 g_signal_handlers_disconnect_by_func (priv->theme,
225 chat_view_theme_notify_cb,
227 g_object_unref (priv->theme);
230 G_OBJECT_CLASS (empathy_chat_view_parent_class)->finalize (object);
234 chat_view_drag_motion (GtkWidget *widget,
235 GdkDragContext *context,
240 /* Don't handle drag motion, since we don't want the view to scroll as
241 * the result of dragging something across it.
248 chat_view_size_allocate (GtkWidget *widget,
249 GtkAllocation *alloc)
253 down = chat_view_is_scrolled_down (EMPATHY_CHAT_VIEW (widget));
255 GTK_WIDGET_CLASS (empathy_chat_view_parent_class)->size_allocate (widget, alloc);
260 adj = GTK_TEXT_VIEW (widget)->vadjustment;
261 gtk_adjustment_set_value (adj, adj->upper - adj->page_size);
266 chat_view_setup_tags (EmpathyChatView *view)
268 EmpathyChatViewPriv *priv;
271 priv = GET_PRIV (view);
273 gtk_text_buffer_create_tag (priv->buffer,
277 /* FIXME: Move to the theme and come up with something that looks a bit
280 gtk_text_buffer_create_tag (priv->buffer,
282 "background", "yellow",
285 tag = gtk_text_buffer_create_tag (priv->buffer,
289 g_signal_connect (tag,
291 G_CALLBACK (chat_view_url_event_cb),
294 g_signal_connect (view,
295 "motion-notify-event",
296 G_CALLBACK (chat_view_event_cb),
301 chat_view_system_font_update (EmpathyChatView *view)
303 PangoFontDescription *font_description = NULL;
306 if (empathy_conf_get_string (empathy_conf_get (),
307 "/desktop/gnome/interface/document_font_name",
308 &font_name) && font_name) {
309 font_description = pango_font_description_from_string (font_name);
312 font_description = NULL;
315 gtk_widget_modify_font (GTK_WIDGET (view), font_description);
317 if (font_description) {
318 pango_font_description_free (font_description);
323 chat_view_notify_system_font_cb (EmpathyConf *conf,
327 EmpathyChatView *view;
328 EmpathyChatViewPriv *priv;
329 gboolean show_avatars = FALSE;
332 priv = GET_PRIV (view);
334 chat_view_system_font_update (view);
336 /* Ugly, again, to adjust the vertical position of the nick... Will fix
337 * this when reworking the theme manager so that view register
338 * themselves with it instead of the other way around.
340 empathy_conf_get_bool (conf,
341 EMPATHY_PREFS_UI_SHOW_AVATARS,
344 empathy_theme_set_show_avatars (priv->theme, show_avatars);
348 chat_view_notify_show_avatars_cb (EmpathyConf *conf,
352 EmpathyChatView *view;
353 EmpathyChatViewPriv *priv;
354 gboolean show_avatars = FALSE;
357 priv = GET_PRIV (view);
359 empathy_conf_get_bool (conf, key, &show_avatars);
361 empathy_theme_set_show_avatars (priv->theme, show_avatars);
365 chat_view_populate_popup (EmpathyChatView *view,
369 EmpathyChatViewPriv *priv;
370 GtkTextTagTable *table;
373 GtkTextIter iter, start, end;
377 priv = GET_PRIV (view);
379 /* Clear menu item */
380 if (gtk_text_buffer_get_char_count (priv->buffer) > 0) {
381 item = gtk_menu_item_new ();
382 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
383 gtk_widget_show (item);
385 item = gtk_image_menu_item_new_from_stock (GTK_STOCK_CLEAR, NULL);
386 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
387 gtk_widget_show (item);
389 g_signal_connect (item,
391 G_CALLBACK (chat_view_clear_view_cb),
395 /* Link context menu items */
396 table = gtk_text_buffer_get_tag_table (priv->buffer);
397 tag = gtk_text_tag_table_lookup (table, "link");
399 gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
401 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
402 GTK_TEXT_WINDOW_WIDGET,
406 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &iter, x, y);
410 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
411 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
412 str = gtk_text_buffer_get_text (priv->buffer,
413 &start, &end, FALSE);
416 if (G_STR_EMPTY (str)) {
421 /* NOTE: Set data just to get the string freed when not needed. */
422 g_object_set_data_full (G_OBJECT (menu),
424 (GDestroyNotify) g_free);
426 item = gtk_menu_item_new ();
427 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
428 gtk_widget_show (item);
430 item = gtk_menu_item_new_with_mnemonic (_("_Copy Link Address"));
431 g_signal_connect (item,
433 G_CALLBACK (chat_view_copy_address_cb),
435 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
436 gtk_widget_show (item);
438 item = gtk_menu_item_new_with_mnemonic (_("_Open Link"));
439 g_signal_connect (item,
441 G_CALLBACK (chat_view_open_address_cb),
443 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
444 gtk_widget_show (item);
448 chat_view_event_cb (EmpathyChatView *view,
449 GdkEventMotion *event,
452 static GdkCursor *hand = NULL;
453 static GdkCursor *beam = NULL;
454 GtkTextWindowType type;
457 gint x, y, buf_x, buf_y;
459 type = gtk_text_view_get_window_type (GTK_TEXT_VIEW (view),
462 if (type != GTK_TEXT_WINDOW_TEXT) {
466 /* Get where the pointer really is. */
467 win = gtk_text_view_get_window (GTK_TEXT_VIEW (view), type);
472 gdk_window_get_pointer (win, &x, &y, NULL);
474 /* Get the iter where the cursor is at */
475 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view), type,
479 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view),
483 if (gtk_text_iter_has_tag (&iter, tag)) {
485 hand = gdk_cursor_new (GDK_HAND2);
486 beam = gdk_cursor_new (GDK_XTERM);
488 gdk_window_set_cursor (win, hand);
491 beam = gdk_cursor_new (GDK_XTERM);
493 gdk_window_set_cursor (win, beam);
500 chat_view_url_event_cb (GtkTextTag *tag,
504 GtkTextBuffer *buffer)
506 GtkTextIter start, end;
509 /* If the link is being selected, don't do anything. */
510 gtk_text_buffer_get_selection_bounds (buffer, &start, &end);
511 if (gtk_text_iter_get_offset (&start) != gtk_text_iter_get_offset (&end)) {
515 if (event->type == GDK_BUTTON_RELEASE && event->button.button == 1) {
518 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
519 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
520 str = gtk_text_buffer_get_text (buffer,
525 empathy_url_show (str);
534 chat_view_open_address_cb (GtkMenuItem *menuitem, const gchar *url)
536 empathy_url_show (url);
540 chat_view_copy_address_cb (GtkMenuItem *menuitem, const gchar *url)
542 GtkClipboard *clipboard;
544 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
545 gtk_clipboard_set_text (clipboard, url, -1);
547 clipboard = gtk_clipboard_get (GDK_SELECTION_PRIMARY);
548 gtk_clipboard_set_text (clipboard, url, -1);
552 chat_view_clear_view_cb (GtkMenuItem *menuitem, EmpathyChatView *view)
554 empathy_chat_view_clear (view);
558 chat_view_is_scrolled_down (EmpathyChatView *view)
562 sw = gtk_widget_get_parent (GTK_WIDGET (view));
563 if (GTK_IS_SCROLLED_WINDOW (sw)) {
566 vadj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (sw));
568 if (vadj->value + vadj->page_size / 2 < vadj->upper - vadj->page_size) {
577 chat_view_maybe_trim_buffer (EmpathyChatView *view)
579 EmpathyChatViewPriv *priv;
580 GtkTextIter top, bottom;
583 GtkTextTagTable *table;
586 priv = GET_PRIV (view);
588 gtk_text_buffer_get_end_iter (priv->buffer, &bottom);
589 line = gtk_text_iter_get_line (&bottom);
590 if (line < MAX_LINES) {
594 remove = line - MAX_LINES;
595 gtk_text_buffer_get_start_iter (priv->buffer, &top);
598 if (!gtk_text_iter_forward_lines (&bottom, remove)) {
602 /* Track backwords to a place where we can safely cut, we don't do it in
603 * the middle of a tag.
605 table = gtk_text_buffer_get_tag_table (priv->buffer);
606 tag = gtk_text_tag_table_lookup (table, "cut");
611 if (!gtk_text_iter_forward_to_tag_toggle (&bottom, tag)) {
615 if (!gtk_text_iter_equal (&top, &bottom)) {
616 gtk_text_buffer_delete (priv->buffer, &top, &bottom);
621 chat_view_theme_changed_cb (EmpathyThemeManager *manager,
622 EmpathyChatView *view)
624 EmpathyChatViewPriv *priv;
625 gboolean show_avatars = FALSE;
627 priv = GET_PRIV (view);
629 empathy_theme_manager_apply_saved (manager, view);
631 /* Needed for now to update the "rise" property of the names to get it
632 * vertically centered.
634 empathy_conf_get_bool (empathy_conf_get (),
635 EMPATHY_PREFS_UI_SHOW_AVATARS,
637 empathy_theme_set_show_avatars (priv->theme, show_avatars);
640 /* Pads a pixbuf to the specified size, by centering it in a larger transparent
641 * pixbuf. Returns a new ref.
644 chat_view_pad_to_size (GdkPixbuf *pixbuf,
647 gint extra_padding_right)
649 gint src_width, src_height;
651 gint x_offset, y_offset;
653 src_width = gdk_pixbuf_get_width (pixbuf);
654 src_height = gdk_pixbuf_get_height (pixbuf);
656 x_offset = (width - src_width) / 2;
657 y_offset = (height - src_height) / 2;
659 padded = gdk_pixbuf_new (gdk_pixbuf_get_colorspace (pixbuf),
661 gdk_pixbuf_get_bits_per_sample (pixbuf),
662 width + extra_padding_right,
665 gdk_pixbuf_fill (padded, 0);
667 gdk_pixbuf_copy_area (pixbuf,
668 0, /* source coords */
673 x_offset, /* dest coords */
685 chat_view_avatar_cache_data_free (gpointer ptr)
687 AvatarData *data = ptr;
689 g_object_unref (data->pixbuf);
690 g_free (data->token);
691 g_slice_free (AvatarData, data);
695 empathy_chat_view_get_avatar_pixbuf_with_cache (EmpathyContact *contact)
698 EmpathyAvatar *avatar;
699 GdkPixbuf *tmp_pixbuf;
700 GdkPixbuf *pixbuf = NULL;
702 /* Check if avatar is in cache and if it's up to date */
703 avatar = empathy_contact_get_avatar (contact);
704 data = g_object_get_data (G_OBJECT (contact), "chat-view-avatar-cache");
706 if (avatar && !tp_strdiff (avatar->token, data->token)) {
707 /* We have the avatar in cache */
712 /* Avatar not in cache, create pixbuf */
713 tmp_pixbuf = empathy_pixbuf_avatar_from_contact_scaled (contact, 32, 32);
715 pixbuf = chat_view_pad_to_size (tmp_pixbuf, 32, 32, 6);
716 g_object_unref (tmp_pixbuf);
722 /* Insert new pixbuf in cache */
723 data = g_slice_new0 (AvatarData);
724 data->token = g_strdup (avatar->token);
725 data->pixbuf = pixbuf;
727 g_object_set_data_full (G_OBJECT (contact), "chat-view-avatar-cache",
728 data, chat_view_avatar_cache_data_free);
734 empathy_chat_view_new (void)
736 return g_object_new (EMPATHY_TYPE_CHAT_VIEW, NULL);
739 /* Code stolen from pidgin/gtkimhtml.c */
741 chat_view_scroll_cb (EmpathyChatView *view)
743 EmpathyChatViewPriv *priv;
747 priv = GET_PRIV (view);
748 adj = GTK_TEXT_VIEW (view)->vadjustment;
749 max_val = adj->upper - adj->page_size;
751 g_return_val_if_fail (priv->scroll_time != NULL, FALSE);
753 if (g_timer_elapsed (priv->scroll_time, NULL) > MAX_SCROLL_TIME) {
754 /* time's up. jump to the end and kill the timer */
755 gtk_adjustment_set_value (adj, max_val);
756 g_timer_destroy (priv->scroll_time);
757 priv->scroll_time = NULL;
758 priv->scroll_timeout = 0;
762 /* scroll by 1/3rd the remaining distance */
763 gtk_adjustment_set_value (adj, gtk_adjustment_get_value (adj) + ((max_val - gtk_adjustment_get_value (adj)) / 3));
768 empathy_chat_view_scroll_down (EmpathyChatView *view)
770 EmpathyChatViewPriv *priv;
772 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
774 priv = GET_PRIV (view);
776 if (!priv->allow_scrolling) {
780 DEBUG ("Scrolling down");
782 if (priv->scroll_time) {
783 g_timer_reset (priv->scroll_time);
785 priv->scroll_time = g_timer_new();
787 if (!priv->scroll_timeout) {
788 priv->scroll_timeout = g_timeout_add (SCROLL_DELAY,
789 (GSourceFunc) chat_view_scroll_cb,
795 empathy_chat_view_append_message (EmpathyChatView *view,
798 EmpathyChatViewPriv *priv = GET_PRIV (view);
801 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
802 g_return_if_fail (EMPATHY_IS_MESSAGE (msg));
804 if (!empathy_message_get_body (msg)) {
808 bottom = chat_view_is_scrolled_down (view);
810 chat_view_maybe_trim_buffer (view);
812 empathy_theme_append_message (priv->theme, view, msg);
815 empathy_chat_view_scroll_down (view);
818 if (priv->last_contact) {
819 g_object_unref (priv->last_contact);
821 priv->last_contact = g_object_ref (empathy_message_get_sender (msg));
825 empathy_chat_view_append_event (EmpathyChatView *view,
828 EmpathyChatViewPriv *priv;
831 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
832 g_return_if_fail (!G_STR_EMPTY (str));
834 priv = GET_PRIV (view);
836 bottom = chat_view_is_scrolled_down (view);
838 chat_view_maybe_trim_buffer (view);
840 empathy_theme_append_event (priv->theme, view, str);
843 empathy_chat_view_scroll_down (view);
846 if (priv->last_contact) {
847 g_object_unref (priv->last_contact);
848 priv->last_contact = NULL;
853 empathy_chat_view_append_button (EmpathyChatView *view,
854 const gchar *message,
858 EmpathyChatViewPriv *priv;
859 GtkTextChildAnchor *anchor;
864 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
865 g_return_if_fail (button1 != NULL);
867 priv = GET_PRIV (view);
871 bottom = chat_view_is_scrolled_down (view);
873 empathy_theme_append_timestamp (priv->theme, view, NULL, TRUE, TRUE);
876 empathy_theme_append_text (priv->theme, view, message, tag, NULL);
879 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
881 anchor = gtk_text_buffer_create_child_anchor (priv->buffer, &iter);
882 gtk_text_view_add_child_at_anchor (GTK_TEXT_VIEW (view), button1, anchor);
883 gtk_widget_show (button1);
885 gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
893 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
895 anchor = gtk_text_buffer_create_child_anchor (priv->buffer, &iter);
896 gtk_text_view_add_child_at_anchor (GTK_TEXT_VIEW (view), button2, anchor);
897 gtk_widget_show (button2);
899 gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
907 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
908 gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
916 empathy_chat_view_scroll_down (view);
919 if (priv->last_contact) {
920 g_object_unref (priv->last_contact);
921 priv->last_contact = NULL;
926 empathy_chat_view_scroll (EmpathyChatView *view,
927 gboolean allow_scrolling)
929 EmpathyChatViewPriv *priv = GET_PRIV (view);
931 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
933 DEBUG ("Scrolling %s", allow_scrolling ? "enabled" : "disabled");
935 priv->allow_scrolling = allow_scrolling;
936 if (allow_scrolling) {
937 empathy_chat_view_scroll_down (view);
942 empathy_chat_view_get_selection_bounds (EmpathyChatView *view,
946 GtkTextBuffer *buffer;
948 g_return_val_if_fail (EMPATHY_IS_CHAT_VIEW (view), FALSE);
950 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
952 return gtk_text_buffer_get_selection_bounds (buffer, start, end);
956 empathy_chat_view_clear (EmpathyChatView *view)
958 GtkTextBuffer *buffer;
959 EmpathyChatViewPriv *priv;
961 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
963 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
964 gtk_text_buffer_set_text (buffer, "", -1);
966 /* We set these back to the initial values so we get
967 * timestamps when clearing the window to know when
968 * conversations start.
970 priv = GET_PRIV (view);
972 priv->last_timestamp = 0;
976 empathy_chat_view_find_previous (EmpathyChatView *view,
977 const gchar *search_criteria,
980 EmpathyChatViewPriv *priv;
981 GtkTextBuffer *buffer;
982 GtkTextIter iter_at_mark;
983 GtkTextIter iter_match_start;
984 GtkTextIter iter_match_end;
986 gboolean from_start = FALSE;
988 g_return_val_if_fail (EMPATHY_IS_CHAT_VIEW (view), FALSE);
989 g_return_val_if_fail (search_criteria != NULL, FALSE);
991 priv = GET_PRIV (view);
993 buffer = priv->buffer;
995 if (G_STR_EMPTY (search_criteria)) {
996 if (priv->find_mark_previous) {
997 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
999 gtk_text_buffer_move_mark (buffer,
1000 priv->find_mark_previous,
1002 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
1003 priv->find_mark_previous,
1008 gtk_text_buffer_select_range (buffer,
1020 if (priv->find_mark_previous) {
1021 gtk_text_buffer_get_iter_at_mark (buffer,
1023 priv->find_mark_previous);
1025 gtk_text_buffer_get_end_iter (buffer, &iter_at_mark);
1029 priv->find_last_direction = FALSE;
1031 found = empathy_text_iter_backward_search (&iter_at_mark,
1038 gboolean result = FALSE;
1044 /* Here we wrap around. */
1045 if (!new_search && !priv->find_wrapped) {
1046 priv->find_wrapped = TRUE;
1047 result = empathy_chat_view_find_previous (view,
1050 priv->find_wrapped = FALSE;
1056 /* Set new mark and show on screen */
1057 if (!priv->find_mark_previous) {
1058 priv->find_mark_previous = gtk_text_buffer_create_mark (buffer, NULL,
1062 gtk_text_buffer_move_mark (buffer,
1063 priv->find_mark_previous,
1067 if (!priv->find_mark_next) {
1068 priv->find_mark_next = gtk_text_buffer_create_mark (buffer, NULL,
1072 gtk_text_buffer_move_mark (buffer,
1073 priv->find_mark_next,
1077 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
1078 priv->find_mark_previous,
1084 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &iter_match_start);
1085 gtk_text_buffer_move_mark_by_name (buffer, "insert", &iter_match_end);
1091 empathy_chat_view_find_next (EmpathyChatView *view,
1092 const gchar *search_criteria,
1093 gboolean new_search)
1095 EmpathyChatViewPriv *priv;
1096 GtkTextBuffer *buffer;
1097 GtkTextIter iter_at_mark;
1098 GtkTextIter iter_match_start;
1099 GtkTextIter iter_match_end;
1101 gboolean from_start = FALSE;
1103 g_return_val_if_fail (EMPATHY_IS_CHAT_VIEW (view), FALSE);
1104 g_return_val_if_fail (search_criteria != NULL, FALSE);
1106 priv = GET_PRIV (view);
1108 buffer = priv->buffer;
1110 if (G_STR_EMPTY (search_criteria)) {
1111 if (priv->find_mark_next) {
1112 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1114 gtk_text_buffer_move_mark (buffer,
1115 priv->find_mark_next,
1117 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
1118 priv->find_mark_next,
1123 gtk_text_buffer_select_range (buffer,
1135 if (priv->find_mark_next) {
1136 gtk_text_buffer_get_iter_at_mark (buffer,
1138 priv->find_mark_next);
1140 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1144 priv->find_last_direction = TRUE;
1146 found = empathy_text_iter_forward_search (&iter_at_mark,
1153 gboolean result = FALSE;
1159 /* Here we wrap around. */
1160 if (!new_search && !priv->find_wrapped) {
1161 priv->find_wrapped = TRUE;
1162 result = empathy_chat_view_find_next (view,
1165 priv->find_wrapped = FALSE;
1171 /* Set new mark and show on screen */
1172 if (!priv->find_mark_next) {
1173 priv->find_mark_next = gtk_text_buffer_create_mark (buffer, NULL,
1177 gtk_text_buffer_move_mark (buffer,
1178 priv->find_mark_next,
1182 if (!priv->find_mark_previous) {
1183 priv->find_mark_previous = gtk_text_buffer_create_mark (buffer, NULL,
1187 gtk_text_buffer_move_mark (buffer,
1188 priv->find_mark_previous,
1192 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
1193 priv->find_mark_next,
1199 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &iter_match_start);
1200 gtk_text_buffer_move_mark_by_name (buffer, "insert", &iter_match_end);
1207 empathy_chat_view_find_abilities (EmpathyChatView *view,
1208 const gchar *search_criteria,
1209 gboolean *can_do_previous,
1210 gboolean *can_do_next)
1212 EmpathyChatViewPriv *priv;
1213 GtkTextBuffer *buffer;
1214 GtkTextIter iter_at_mark;
1215 GtkTextIter iter_match_start;
1216 GtkTextIter iter_match_end;
1218 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
1219 g_return_if_fail (search_criteria != NULL);
1220 g_return_if_fail (can_do_previous != NULL && can_do_next != NULL);
1222 priv = GET_PRIV (view);
1224 buffer = priv->buffer;
1226 if (can_do_previous) {
1227 if (priv->find_mark_previous) {
1228 gtk_text_buffer_get_iter_at_mark (buffer,
1230 priv->find_mark_previous);
1232 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1235 *can_do_previous = empathy_text_iter_backward_search (&iter_at_mark,
1243 if (priv->find_mark_next) {
1244 gtk_text_buffer_get_iter_at_mark (buffer,
1246 priv->find_mark_next);
1248 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1251 *can_do_next = empathy_text_iter_forward_search (&iter_at_mark,
1260 empathy_chat_view_highlight (EmpathyChatView *view,
1263 GtkTextBuffer *buffer;
1265 GtkTextIter iter_start;
1266 GtkTextIter iter_end;
1267 GtkTextIter iter_match_start;
1268 GtkTextIter iter_match_end;
1271 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
1273 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
1275 gtk_text_buffer_get_start_iter (buffer, &iter);
1277 gtk_text_buffer_get_bounds (buffer, &iter_start, &iter_end);
1278 gtk_text_buffer_remove_tag_by_name (buffer, "highlight",
1282 if (G_STR_EMPTY (text)) {
1287 found = empathy_text_iter_forward_search (&iter,
1297 gtk_text_buffer_apply_tag_by_name (buffer, "highlight",
1301 iter = iter_match_end;
1302 gtk_text_iter_forward_char (&iter);
1307 empathy_chat_view_copy_clipboard (EmpathyChatView *view)
1309 GtkTextBuffer *buffer;
1310 GtkClipboard *clipboard;
1312 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
1314 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
1315 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1317 gtk_text_buffer_copy_clipboard (buffer, clipboard);
1321 empathy_chat_view_get_theme (EmpathyChatView *view)
1323 EmpathyChatViewPriv *priv;
1325 g_return_val_if_fail (EMPATHY_IS_CHAT_VIEW (view), NULL);
1327 priv = GET_PRIV (view);
1333 chat_view_theme_notify_cb (EmpathyTheme *theme,
1335 EmpathyChatView *view)
1337 empathy_theme_update_view (theme, view);
1341 empathy_chat_view_set_theme (EmpathyChatView *view, EmpathyTheme *theme)
1343 EmpathyChatViewPriv *priv;
1345 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
1346 g_return_if_fail (EMPATHY_IS_THEME (theme));
1348 priv = GET_PRIV (view);
1351 g_signal_handlers_disconnect_by_func (priv->theme,
1352 chat_view_theme_notify_cb,
1354 g_object_unref (priv->theme);
1357 priv->theme = g_object_ref (theme);
1359 empathy_theme_update_view (theme, view);
1360 g_signal_connect (priv->theme, "notify",
1361 G_CALLBACK (chat_view_theme_notify_cb),
1364 /* FIXME: Redraw all messages using the new theme */
1368 empathy_chat_view_set_margin (EmpathyChatView *view,
1371 EmpathyChatViewPriv *priv;
1373 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
1375 priv = GET_PRIV (view);
1378 "left-margin", margin,
1379 "right-margin", margin,
1384 empathy_chat_view_get_smiley_menu (GCallback callback,
1387 EmpathySmileyManager *smiley_manager;
1388 GSList *smileys, *l;
1393 g_return_val_if_fail (callback != NULL, NULL);
1395 menu = gtk_menu_new ();
1397 smiley_manager = empathy_smiley_manager_new ();
1398 smileys = empathy_smiley_manager_get_all (smiley_manager);
1399 for (l = smileys; l; l = l->next) {
1400 EmpathySmiley *smiley;
1405 image = gtk_image_new_from_pixbuf (smiley->pixbuf);
1407 item = gtk_image_menu_item_new_with_label ("");
1408 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1410 gtk_menu_attach (GTK_MENU (menu), item,
1411 x, x + 1, y, y + 1);
1413 gtk_widget_set_tooltip_text (item, smiley->str);
1415 g_object_set_data (G_OBJECT (item), "smiley_text", smiley->str);
1416 g_signal_connect (item, "activate", callback, user_data);
1425 g_object_unref (smiley_manager);
1427 gtk_widget_show_all (menu);
1433 empathy_chat_view_get_last_timestamp (EmpathyChatView *view)
1435 EmpathyChatViewPriv *priv;
1437 g_return_val_if_fail (EMPATHY_IS_CHAT_VIEW (view), 0);
1439 priv = GET_PRIV (view);
1441 return priv->last_timestamp;
1445 empathy_chat_view_set_last_timestamp (EmpathyChatView *view,
1448 EmpathyChatViewPriv *priv;
1450 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
1452 priv = GET_PRIV (view);
1454 priv->last_timestamp = timestamp;
1458 empathy_chat_view_get_last_contact (EmpathyChatView *view)
1460 EmpathyChatViewPriv *priv;
1462 g_return_val_if_fail (EMPATHY_IS_CHAT_VIEW (view), NULL);
1464 priv = GET_PRIV (view);
1466 return priv->last_contact;