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 <libmissioncontrol/mc-account.h>
44 #include <libempathy/empathy-utils.h>
45 #include <libempathy/empathy-debug.h>
46 #include <libempathy/empathy-conf.h>
48 #include "empathy-chat-view.h"
49 #include "empathy-chat.h"
50 #include "empathy-preferences.h"
51 #include "empathy-theme-manager.h"
52 #include "empathy-ui-utils.h"
54 #define DEBUG_DOMAIN "ChatView"
56 /* Number of seconds between timestamps when using normal mode, 5 minutes. */
57 #define TIMESTAMP_INTERVAL 300
60 #define MAX_SCROLL_TIME 0.4 /* seconds */
61 #define SCROLL_DELAY 33 /* milliseconds */
63 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), EMPATHY_TYPE_CHAT_VIEW, EmpathyChatViewPriv))
74 struct _EmpathyChatViewPriv {
75 GtkTextBuffer *buffer;
78 time_t last_timestamp;
79 BlockType last_block_type;
81 gboolean allow_scrolling;
84 gboolean is_group_chat;
86 GtkTextMark *find_mark_previous;
87 GtkTextMark *find_mark_next;
88 gboolean find_wrapped;
89 gboolean find_last_direction;
91 /* This is for the group chat so we know if the "other" last contact
92 * changed, so we know whether to insert a header or not.
94 EmpathyContact *last_contact;
96 guint notify_system_fonts_id;
97 guint notify_show_avatars_id;
101 EmpathySmiley smiley;
102 const gchar *pattern;
103 } EmpathySmileyPattern;
105 static const EmpathySmileyPattern smileys[] = {
106 /* Forward smileys. */
107 { EMPATHY_SMILEY_NORMAL, ":)" },
108 { EMPATHY_SMILEY_WINK, ";)" },
109 { EMPATHY_SMILEY_WINK, ";-)" },
110 { EMPATHY_SMILEY_BIGEYE, "=)" },
111 { EMPATHY_SMILEY_NOSE, ":-)" },
112 { EMPATHY_SMILEY_CRY, ":'(" },
113 { EMPATHY_SMILEY_SAD, ":(" },
114 { EMPATHY_SMILEY_SAD, ":-(" },
115 { EMPATHY_SMILEY_SCEPTICAL, ":/" },
116 { EMPATHY_SMILEY_SCEPTICAL, ":\\" },
117 { EMPATHY_SMILEY_BIGSMILE, ":D" },
118 { EMPATHY_SMILEY_BIGSMILE, ":-D" },
119 { EMPATHY_SMILEY_INDIFFERENT, ":|" },
120 { EMPATHY_SMILEY_TOUNGE, ":p" },
121 { EMPATHY_SMILEY_TOUNGE, ":-p" },
122 { EMPATHY_SMILEY_TOUNGE, ":P" },
123 { EMPATHY_SMILEY_TOUNGE, ":-P" },
124 { EMPATHY_SMILEY_TOUNGE, ";p" },
125 { EMPATHY_SMILEY_TOUNGE, ";-p" },
126 { EMPATHY_SMILEY_TOUNGE, ";P" },
127 { EMPATHY_SMILEY_TOUNGE, ";-P" },
128 { EMPATHY_SMILEY_SHOCKED, ":o" },
129 { EMPATHY_SMILEY_SHOCKED, ":-o" },
130 { EMPATHY_SMILEY_SHOCKED, ":O" },
131 { EMPATHY_SMILEY_SHOCKED, ":-O" },
132 { EMPATHY_SMILEY_COOL, "8)" },
133 { EMPATHY_SMILEY_COOL, "B)" },
134 { EMPATHY_SMILEY_SORRY, "*|" },
135 { EMPATHY_SMILEY_KISS, ":*" },
136 { EMPATHY_SMILEY_SHUTUP, ":#" },
137 { EMPATHY_SMILEY_SHUTUP, ":-#" },
138 { EMPATHY_SMILEY_YAWN, "|O" },
139 { EMPATHY_SMILEY_CONFUSED, ":S" },
140 { EMPATHY_SMILEY_CONFUSED, ":s" },
141 { EMPATHY_SMILEY_ANGEL, "<)" },
142 { EMPATHY_SMILEY_OOOH, ":x" },
143 { EMPATHY_SMILEY_LOOKAWAY, "*)" },
144 { EMPATHY_SMILEY_LOOKAWAY, "*-)" },
145 { EMPATHY_SMILEY_BLUSH, "*S" },
146 { EMPATHY_SMILEY_BLUSH, "*s" },
147 { EMPATHY_SMILEY_BLUSH, "*$" },
148 { EMPATHY_SMILEY_COOLBIGSMILE, "8D" },
149 { EMPATHY_SMILEY_ANGRY, ":@" },
150 { EMPATHY_SMILEY_BOSS, "@)" },
151 { EMPATHY_SMILEY_MONKEY, "#)" },
152 { EMPATHY_SMILEY_SILLY, "O)" },
153 { EMPATHY_SMILEY_SICK, "+o(" },
155 /* Backward smileys. */
156 { EMPATHY_SMILEY_NORMAL, "(:" },
157 { EMPATHY_SMILEY_WINK, "(;" },
158 { EMPATHY_SMILEY_WINK, "(-;" },
159 { EMPATHY_SMILEY_BIGEYE, "(=" },
160 { EMPATHY_SMILEY_NOSE, "(-:" },
161 { EMPATHY_SMILEY_CRY, ")':" },
162 { EMPATHY_SMILEY_SAD, "):" },
163 { EMPATHY_SMILEY_SAD, ")-:" },
164 { EMPATHY_SMILEY_SCEPTICAL, "/:" },
165 { EMPATHY_SMILEY_SCEPTICAL, "//:" },
166 { EMPATHY_SMILEY_INDIFFERENT, "|:" },
167 { EMPATHY_SMILEY_TOUNGE, "d:" },
168 { EMPATHY_SMILEY_TOUNGE, "d-:" },
169 { EMPATHY_SMILEY_TOUNGE, "d;" },
170 { EMPATHY_SMILEY_TOUNGE, "d-;" },
171 { EMPATHY_SMILEY_SHOCKED, "o:" },
172 { EMPATHY_SMILEY_SHOCKED, "O:" },
173 { EMPATHY_SMILEY_COOL, "(8" },
174 { EMPATHY_SMILEY_COOL, "(B" },
175 { EMPATHY_SMILEY_SORRY, "|*" },
176 { EMPATHY_SMILEY_KISS, "*:" },
177 { EMPATHY_SMILEY_SHUTUP, "#:" },
178 { EMPATHY_SMILEY_SHUTUP, "#-:" },
179 { EMPATHY_SMILEY_YAWN, "O|" },
180 { EMPATHY_SMILEY_CONFUSED, "S:" },
181 { EMPATHY_SMILEY_CONFUSED, "s:" },
182 { EMPATHY_SMILEY_ANGEL, "(>" },
183 { EMPATHY_SMILEY_OOOH, "x:" },
184 { EMPATHY_SMILEY_LOOKAWAY, "(*" },
185 { EMPATHY_SMILEY_LOOKAWAY, "(-*" },
186 { EMPATHY_SMILEY_BLUSH, "S*" },
187 { EMPATHY_SMILEY_BLUSH, "s*" },
188 { EMPATHY_SMILEY_BLUSH, "$*" },
189 { EMPATHY_SMILEY_ANGRY, "@:" },
190 { EMPATHY_SMILEY_BOSS, "(@" },
191 { EMPATHY_SMILEY_MONKEY, "#)" },
192 { EMPATHY_SMILEY_SILLY, "(O" },
193 { EMPATHY_SMILEY_SICK, ")o+" }
196 static void empathy_chat_view_class_init (EmpathyChatViewClass *klass);
197 static void empathy_chat_view_init (EmpathyChatView *view);
198 static void chat_view_finalize (GObject *object);
199 static gboolean chat_view_drag_motion (GtkWidget *widget,
200 GdkDragContext *context,
204 static void chat_view_size_allocate (GtkWidget *widget,
205 GtkAllocation *alloc);
206 static void chat_view_setup_tags (EmpathyChatView *view);
207 static void chat_view_system_font_update (EmpathyChatView *view);
208 static void chat_view_notify_system_font_cb (EmpathyConf *conf,
211 static void chat_view_notify_show_avatars_cb (EmpathyConf *conf,
214 static void chat_view_populate_popup (EmpathyChatView *view,
217 static gboolean chat_view_event_cb (EmpathyChatView *view,
218 GdkEventMotion *event,
220 static gboolean chat_view_url_event_cb (GtkTextTag *tag,
224 GtkTextBuffer *buffer);
225 static void chat_view_open_address_cb (GtkMenuItem *menuitem,
227 static void chat_view_copy_address_cb (GtkMenuItem *menuitem,
229 static void chat_view_clear_view_cb (GtkMenuItem *menuitem,
230 EmpathyChatView *view);
231 static void chat_view_insert_text_with_emoticons (GtkTextBuffer *buf,
234 static gboolean chat_view_is_scrolled_down (EmpathyChatView *view);
235 static void chat_view_theme_changed_cb (EmpathyThemeManager *manager,
236 EmpathyChatView *view);
237 static void chat_view_maybe_append_date_and_time (EmpathyChatView *view,
238 EmpathyMessage *msg);
239 static void chat_view_append_spacing (EmpathyChatView *view);
240 static void chat_view_append_text (EmpathyChatView *view,
243 static void chat_view_maybe_append_fancy_header (EmpathyChatView *view,
244 EmpathyMessage *msg);
245 static void chat_view_append_irc_action (EmpathyChatView *view,
246 EmpathyMessage *msg);
247 static void chat_view_append_fancy_action (EmpathyChatView *view,
248 EmpathyMessage *msg);
249 static void chat_view_append_irc_message (EmpathyChatView *view,
250 EmpathyMessage *msg);
251 static void chat_view_append_fancy_message (EmpathyChatView *view,
252 EmpathyMessage *msg);
253 static GdkPixbuf *chat_view_pad_to_size (GdkPixbuf *pixbuf,
256 gint extra_padding_right);
258 G_DEFINE_TYPE (EmpathyChatView, empathy_chat_view, GTK_TYPE_TEXT_VIEW);
261 empathy_chat_view_class_init (EmpathyChatViewClass *klass)
263 GObjectClass *object_class = G_OBJECT_CLASS (klass);
264 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
266 object_class->finalize = chat_view_finalize;
267 widget_class->size_allocate = chat_view_size_allocate;
268 widget_class->drag_motion = chat_view_drag_motion;
270 g_type_class_add_private (object_class, sizeof (EmpathyChatViewPriv));
274 empathy_chat_view_init (EmpathyChatView *view)
276 EmpathyChatViewPriv *priv;
277 gboolean show_avatars;
279 priv = GET_PRIV (view);
281 priv->buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
283 priv->last_block_type = BLOCK_TYPE_NONE;
284 priv->last_timestamp = 0;
286 priv->allow_scrolling = TRUE;
288 priv->is_group_chat = FALSE;
291 "wrap-mode", GTK_WRAP_WORD_CHAR,
293 "cursor-visible", FALSE,
296 priv->notify_system_fonts_id =
297 empathy_conf_notify_add (empathy_conf_get (),
298 "/desktop/gnome/interface/document_font_name",
299 chat_view_notify_system_font_cb,
301 chat_view_system_font_update (view);
303 priv->notify_show_avatars_id =
304 empathy_conf_notify_add (empathy_conf_get (),
305 EMPATHY_PREFS_UI_SHOW_AVATARS,
306 chat_view_notify_show_avatars_cb,
309 chat_view_setup_tags (view);
311 empathy_theme_manager_apply_saved (empathy_theme_manager_get (), view);
313 show_avatars = FALSE;
314 empathy_conf_get_bool (empathy_conf_get (),
315 EMPATHY_PREFS_UI_SHOW_AVATARS,
318 empathy_theme_manager_update_show_avatars (empathy_theme_manager_get (),
321 g_signal_connect (view,
323 G_CALLBACK (chat_view_populate_popup),
326 g_signal_connect_object (empathy_theme_manager_get (),
328 G_CALLBACK (chat_view_theme_changed_cb),
334 chat_view_finalize (GObject *object)
336 EmpathyChatView *view;
337 EmpathyChatViewPriv *priv;
339 view = EMPATHY_CHAT_VIEW (object);
340 priv = GET_PRIV (view);
342 empathy_debug (DEBUG_DOMAIN, "finalize: %p", object);
344 empathy_conf_notify_remove (empathy_conf_get (), priv->notify_system_fonts_id);
345 empathy_conf_notify_remove (empathy_conf_get (), priv->notify_show_avatars_id);
347 if (priv->last_contact) {
348 g_object_unref (priv->last_contact);
350 if (priv->scroll_time) {
351 g_timer_destroy (priv->scroll_time);
353 if (priv->scroll_timeout) {
354 g_source_remove (priv->scroll_timeout);
357 G_OBJECT_CLASS (empathy_chat_view_parent_class)->finalize (object);
361 chat_view_drag_motion (GtkWidget *widget,
362 GdkDragContext *context,
367 /* Don't handle drag motion, since we don't want the view to scroll as
368 * the result of dragging something across it.
375 chat_view_size_allocate (GtkWidget *widget,
376 GtkAllocation *alloc)
380 down = chat_view_is_scrolled_down (EMPATHY_CHAT_VIEW (widget));
382 GTK_WIDGET_CLASS (empathy_chat_view_parent_class)->size_allocate (widget, alloc);
387 adj = GTK_TEXT_VIEW (widget)->vadjustment;
388 gtk_adjustment_set_value (adj, adj->upper - adj->page_size);
393 chat_view_setup_tags (EmpathyChatView *view)
395 EmpathyChatViewPriv *priv;
398 priv = GET_PRIV (view);
400 gtk_text_buffer_create_tag (priv->buffer,
404 /* FIXME: Move to the theme and come up with something that looks a bit
407 gtk_text_buffer_create_tag (priv->buffer,
409 "background", "yellow",
412 tag = gtk_text_buffer_create_tag (priv->buffer,
416 g_signal_connect (tag,
418 G_CALLBACK (chat_view_url_event_cb),
421 g_signal_connect (view,
422 "motion-notify-event",
423 G_CALLBACK (chat_view_event_cb),
428 chat_view_system_font_update (EmpathyChatView *view)
430 PangoFontDescription *font_description = NULL;
433 if (empathy_conf_get_string (empathy_conf_get (),
434 "/desktop/gnome/interface/document_font_name",
435 &font_name) && font_name) {
436 font_description = pango_font_description_from_string (font_name);
439 font_description = NULL;
442 gtk_widget_modify_font (GTK_WIDGET (view), font_description);
444 if (font_description) {
445 pango_font_description_free (font_description);
450 chat_view_notify_system_font_cb (EmpathyConf *conf,
454 EmpathyChatView *view;
455 gboolean show_avatars = FALSE;
459 chat_view_system_font_update (view);
461 /* Ugly, again, to adjust the vertical position of the nick... Will fix
462 * this when reworking the theme manager so that view register
463 * themselves with it instead of the other way around.
465 empathy_conf_get_bool (conf,
466 EMPATHY_PREFS_UI_SHOW_AVATARS,
469 empathy_theme_manager_update_show_avatars (empathy_theme_manager_get (),
474 chat_view_notify_show_avatars_cb (EmpathyConf *conf,
478 EmpathyChatView *view;
479 EmpathyChatViewPriv *priv;
480 gboolean show_avatars = FALSE;
483 priv = GET_PRIV (view);
485 empathy_conf_get_bool (conf, key, &show_avatars);
487 empathy_theme_manager_update_show_avatars (empathy_theme_manager_get (),
492 chat_view_populate_popup (EmpathyChatView *view,
496 EmpathyChatViewPriv *priv;
497 GtkTextTagTable *table;
500 GtkTextIter iter, start, end;
504 priv = GET_PRIV (view);
506 /* Clear menu item */
507 if (gtk_text_buffer_get_char_count (priv->buffer) > 0) {
508 item = gtk_menu_item_new ();
509 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
510 gtk_widget_show (item);
512 item = gtk_image_menu_item_new_from_stock (GTK_STOCK_CLEAR, NULL);
513 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
514 gtk_widget_show (item);
516 g_signal_connect (item,
518 G_CALLBACK (chat_view_clear_view_cb),
522 /* Link context menu items */
523 table = gtk_text_buffer_get_tag_table (priv->buffer);
524 tag = gtk_text_tag_table_lookup (table, "link");
526 gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
528 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
529 GTK_TEXT_WINDOW_WIDGET,
533 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &iter, x, y);
537 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
538 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
539 str = gtk_text_buffer_get_text (priv->buffer,
540 &start, &end, FALSE);
543 if (G_STR_EMPTY (str)) {
548 /* NOTE: Set data just to get the string freed when not needed. */
549 g_object_set_data_full (G_OBJECT (menu),
551 (GDestroyNotify) g_free);
553 item = gtk_menu_item_new ();
554 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
555 gtk_widget_show (item);
557 item = gtk_menu_item_new_with_mnemonic (_("_Copy Link Address"));
558 g_signal_connect (item,
560 G_CALLBACK (chat_view_copy_address_cb),
562 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
563 gtk_widget_show (item);
565 item = gtk_menu_item_new_with_mnemonic (_("_Open Link"));
566 g_signal_connect (item,
568 G_CALLBACK (chat_view_open_address_cb),
570 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
571 gtk_widget_show (item);
575 chat_view_event_cb (EmpathyChatView *view,
576 GdkEventMotion *event,
579 static GdkCursor *hand = NULL;
580 static GdkCursor *beam = NULL;
581 GtkTextWindowType type;
584 gint x, y, buf_x, buf_y;
586 type = gtk_text_view_get_window_type (GTK_TEXT_VIEW (view),
589 if (type != GTK_TEXT_WINDOW_TEXT) {
593 /* Get where the pointer really is. */
594 win = gtk_text_view_get_window (GTK_TEXT_VIEW (view), type);
599 gdk_window_get_pointer (win, &x, &y, NULL);
601 /* Get the iter where the cursor is at */
602 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view), type,
606 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view),
610 if (gtk_text_iter_has_tag (&iter, tag)) {
612 hand = gdk_cursor_new (GDK_HAND2);
613 beam = gdk_cursor_new (GDK_XTERM);
615 gdk_window_set_cursor (win, hand);
618 beam = gdk_cursor_new (GDK_XTERM);
620 gdk_window_set_cursor (win, beam);
627 chat_view_url_event_cb (GtkTextTag *tag,
631 GtkTextBuffer *buffer)
633 GtkTextIter start, end;
636 /* If the link is being selected, don't do anything. */
637 gtk_text_buffer_get_selection_bounds (buffer, &start, &end);
638 if (gtk_text_iter_get_offset (&start) != gtk_text_iter_get_offset (&end)) {
642 if (event->type == GDK_BUTTON_RELEASE && event->button.button == 1) {
645 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
646 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
647 str = gtk_text_buffer_get_text (buffer,
652 empathy_url_show (str);
661 chat_view_open_address_cb (GtkMenuItem *menuitem, const gchar *url)
663 empathy_url_show (url);
667 chat_view_copy_address_cb (GtkMenuItem *menuitem, const gchar *url)
669 GtkClipboard *clipboard;
671 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
672 gtk_clipboard_set_text (clipboard, url, -1);
674 clipboard = gtk_clipboard_get (GDK_SELECTION_PRIMARY);
675 gtk_clipboard_set_text (clipboard, url, -1);
679 chat_view_clear_view_cb (GtkMenuItem *menuitem, EmpathyChatView *view)
681 empathy_chat_view_clear (view);
685 chat_view_insert_text_with_emoticons (GtkTextBuffer *buf,
694 gboolean use_smileys = FALSE;
696 empathy_conf_get_bool (empathy_conf_get (),
697 EMPATHY_PREFS_CHAT_SHOW_SMILEYS,
701 gtk_text_buffer_insert (buf, iter, str, -1);
706 gint smileys_index[G_N_ELEMENTS (smileys)];
711 memset (smileys_index, 0, sizeof (smileys_index));
719 c = g_utf8_get_char (p);
721 if (match != -1 && g_unichar_isspace (c)) {
727 if (submatch != -1 || prev_c == 0 || g_unichar_isspace (prev_c)) {
730 for (i = 0; i < G_N_ELEMENTS (smileys); i++) {
731 /* Only try to match if we already have
732 * a beginning match for the pattern, or
733 * if it's the first character in the
734 * pattern, if it's not in the middle of
737 if (((smileys_index[i] == 0 && (prev_c == 0 || g_unichar_isspace (prev_c))) ||
738 smileys_index[i] > 0) &&
739 smileys[i].pattern[smileys_index[i]] == c) {
743 if (!smileys[i].pattern[smileys_index[i]]) {
747 smileys_index[i] = 0;
753 p = g_utf8_next_char (p);
757 gtk_text_buffer_insert (buf, iter, str, -1);
761 start = p - strlen (smileys[match].pattern);
765 gtk_text_buffer_insert (buf, iter, str, len);
768 pixbuf = empathy_chat_view_get_smiley_image (smileys[match].smiley);
769 gtk_text_buffer_insert_pixbuf (buf, iter, pixbuf);
771 gtk_text_buffer_insert (buf, iter, " ", 1);
773 str = g_utf8_find_next_char (p, NULL);
778 chat_view_is_scrolled_down (EmpathyChatView *view)
782 sw = gtk_widget_get_parent (GTK_WIDGET (view));
783 if (GTK_IS_SCROLLED_WINDOW (sw)) {
786 vadj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (sw));
788 if (vadj->value + vadj->page_size / 2 < vadj->upper - vadj->page_size) {
797 chat_view_maybe_trim_buffer (EmpathyChatView *view)
799 EmpathyChatViewPriv *priv;
800 GtkTextIter top, bottom;
803 GtkTextTagTable *table;
806 priv = GET_PRIV (view);
808 gtk_text_buffer_get_end_iter (priv->buffer, &bottom);
809 line = gtk_text_iter_get_line (&bottom);
810 if (line < MAX_LINES) {
814 remove = line - MAX_LINES;
815 gtk_text_buffer_get_start_iter (priv->buffer, &top);
818 if (!gtk_text_iter_forward_lines (&bottom, remove)) {
822 /* Track backwords to a place where we can safely cut, we don't do it in
823 * the middle of a tag.
825 table = gtk_text_buffer_get_tag_table (priv->buffer);
826 tag = gtk_text_tag_table_lookup (table, "cut");
831 if (!gtk_text_iter_forward_to_tag_toggle (&bottom, tag)) {
835 if (!gtk_text_iter_equal (&top, &bottom)) {
836 gtk_text_buffer_delete (priv->buffer, &top, &bottom);
841 chat_view_maybe_append_date_and_time (EmpathyChatView *view,
844 EmpathyChatViewPriv *priv;
847 GDate *date, *last_date;
849 gboolean append_date, append_time;
852 priv = GET_PRIV (view);
854 if (priv->irc_style) {
860 if (priv->last_block_type == BLOCK_TYPE_TIME) {
864 str = g_string_new (NULL);
868 timestamp = empathy_message_get_timestamp (msg);
871 if (timestamp <= 0) {
872 timestamp = empathy_time_get_current ();
875 date = g_date_new ();
876 g_date_set_time (date, timestamp);
878 last_date = g_date_new ();
879 g_date_set_time (last_date, priv->last_timestamp);
884 if (g_date_compare (date, last_date) > 0) {
889 if (priv->last_timestamp + TIMESTAMP_INTERVAL < timestamp) {
893 if (append_time || append_date) {
894 chat_view_append_spacing (view);
896 g_string_append (str, "- ");
902 g_date_strftime (buf, 256, _("%A %d %B %Y"), date);
903 g_string_append (str, buf);
906 g_string_append (str, ", ");
911 g_date_free (last_date);
916 tmp = empathy_time_to_string_local (timestamp, EMPATHY_TIME_FORMAT_DISPLAY_SHORT);
917 g_string_append (str, tmp);
921 if (append_time || append_date) {
922 g_string_append (str, " -\n");
924 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
925 gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
931 priv->last_block_type = BLOCK_TYPE_TIME;
932 priv->last_timestamp = timestamp;
935 g_string_free (str, TRUE);
939 chat_view_append_spacing (EmpathyChatView *view)
941 EmpathyChatViewPriv *priv;
945 priv = GET_PRIV (view);
947 if (priv->irc_style) {
950 tag = "fancy-spacing";
953 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
954 gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
964 chat_view_append_text (EmpathyChatView *view,
968 EmpathyChatViewPriv *priv;
969 GtkTextIter start_iter, end_iter;
974 const gchar *link_tag;
976 priv = GET_PRIV (view);
978 if (priv->irc_style) {
979 link_tag = "irc-link";
981 link_tag = "fancy-link";
984 gtk_text_buffer_get_end_iter (priv->buffer, &start_iter);
985 mark = gtk_text_buffer_create_mark (priv->buffer, NULL, &start_iter, TRUE);
987 start = g_array_new (FALSE, FALSE, sizeof (gint));
988 end = g_array_new (FALSE, FALSE, sizeof (gint));
990 num_matches = empathy_regex_match (EMPATHY_REGEX_ALL, body, start, end);
992 if (num_matches == 0) {
993 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
994 chat_view_insert_text_with_emoticons (priv->buffer, &iter, body);
1000 for (i = 0; i < num_matches; i++) {
1001 s = g_array_index (start, gint, i);
1002 e = g_array_index (end, gint, i);
1005 tmp = empathy_substring (body, last, s);
1007 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1008 chat_view_insert_text_with_emoticons (priv->buffer,
1014 tmp = empathy_substring (body, s, e);
1016 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1017 gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
1030 if (e < strlen (body)) {
1031 tmp = empathy_substring (body, e, strlen (body));
1033 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1034 chat_view_insert_text_with_emoticons (priv->buffer,
1041 g_array_free (start, TRUE);
1042 g_array_free (end, TRUE);
1044 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1045 gtk_text_buffer_insert (priv->buffer, &iter, "\n", 1);
1047 /* Apply the style to the inserted text. */
1048 gtk_text_buffer_get_iter_at_mark (priv->buffer, &start_iter, mark);
1049 gtk_text_buffer_get_end_iter (priv->buffer, &end_iter);
1051 gtk_text_buffer_apply_tag_by_name (priv->buffer,
1056 gtk_text_buffer_delete_mark (priv->buffer, mark);
1060 chat_view_maybe_append_fancy_header (EmpathyChatView *view,
1061 EmpathyMessage *msg)
1063 EmpathyChatViewPriv *priv;
1064 EmpathyContact *sender;
1070 const gchar *avatar_tag;
1071 const gchar *line_top_tag;
1072 const gchar *line_bottom_tag;
1074 GdkPixbuf *pixbuf = NULL;
1075 GdkPixbuf *avatar = NULL;
1077 priv = GET_PRIV (view);
1079 sender = empathy_message_get_sender (msg);
1080 name = empathy_contact_get_name (sender);
1081 from_self = empathy_contact_is_user (sender);
1083 empathy_debug (DEBUG_DOMAIN, "Maybe add fancy header");
1086 tag = "fancy-header-self";
1087 line_top_tag = "fancy-line-top-self";
1088 line_bottom_tag = "fancy-line-bottom-self";
1090 tag = "fancy-header-other";
1091 line_top_tag = "fancy-line-top-other";
1092 line_bottom_tag = "fancy-line-bottom-other";
1097 /* Only insert a header if the previously inserted block is not the same
1098 * as this one. This catches all the different cases:
1100 if (priv->last_block_type != BLOCK_TYPE_SELF &&
1101 priv->last_block_type != BLOCK_TYPE_OTHER) {
1104 else if (from_self && priv->last_block_type == BLOCK_TYPE_OTHER) {
1107 else if (!from_self && priv->last_block_type == BLOCK_TYPE_SELF) {
1110 else if (!from_self &&
1111 (!priv->last_contact ||
1112 !empathy_contact_equal (sender, priv->last_contact))) {
1120 chat_view_append_spacing (view);
1122 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1123 gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
1130 /* FIXME: we should have a cash of avatar pixbufs */
1131 pixbuf = empathy_pixbuf_avatar_from_contact_scaled (sender, 32, 32);
1133 avatar = chat_view_pad_to_size (pixbuf, 32, 32, 6);
1134 g_object_unref (pixbuf);
1140 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1141 gtk_text_buffer_insert_pixbuf (priv->buffer, &iter, avatar);
1143 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1145 gtk_text_iter_backward_char (&start);
1148 gtk_text_buffer_apply_tag_by_name (priv->buffer,
1149 "fancy-avatar-self",
1151 avatar_tag = "fancy-header-self-avatar";
1153 gtk_text_buffer_apply_tag_by_name (priv->buffer,
1154 "fancy-avatar-other",
1156 avatar_tag = "fancy-header-other-avatar";
1159 g_object_unref (avatar);
1164 tmp = g_strdup_printf ("%s\n", name);
1166 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1167 gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
1176 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1177 gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
1186 chat_view_append_irc_action (EmpathyChatView *view,
1187 EmpathyMessage *msg)
1189 EmpathyChatViewPriv *priv;
1190 EmpathyContact *sender;
1197 priv = GET_PRIV (view);
1199 empathy_debug (DEBUG_DOMAIN, "Add IRC action");
1201 sender = empathy_message_get_sender (msg);
1202 name = empathy_contact_get_name (sender);
1204 if (empathy_contact_is_user (sender)) {
1205 tag = "irc-action-self";
1207 tag = "irc-action-other";
1210 if (priv->last_block_type != BLOCK_TYPE_SELF &&
1211 priv->last_block_type != BLOCK_TYPE_OTHER) {
1212 chat_view_append_spacing (view);
1215 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1217 tmp = g_strdup_printf (" * %s ", name);
1218 gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
1227 body = empathy_message_get_body (msg);
1228 chat_view_append_text (view, body, tag);
1232 chat_view_append_fancy_action (EmpathyChatView *view,
1233 EmpathyMessage *msg)
1235 EmpathyChatViewPriv *priv;
1236 EmpathyContact *sender;
1242 const gchar *line_tag;
1244 priv = GET_PRIV (view);
1246 empathy_debug (DEBUG_DOMAIN, "Add fancy action");
1248 sender = empathy_message_get_sender (msg);
1249 name = empathy_contact_get_name (sender);
1251 if (empathy_contact_is_user (sender)) {
1252 tag = "fancy-action-self";
1253 line_tag = "fancy-line-self";
1255 tag = "fancy-action-other";
1256 line_tag = "fancy-line-other";
1259 tmp = g_strdup_printf (" * %s ", name);
1260 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1261 gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
1269 body = empathy_message_get_body (msg);
1270 chat_view_append_text (view, body, tag);
1274 chat_view_append_irc_message (EmpathyChatView *view,
1275 EmpathyMessage *msg)
1277 EmpathyChatViewPriv *priv;
1278 EmpathyContact *sender;
1281 const gchar *nick_tag;
1282 const gchar *body_tag;
1286 priv = GET_PRIV (view);
1288 empathy_debug (DEBUG_DOMAIN, "Add IRC message");
1290 body = empathy_message_get_body (msg);
1291 sender = empathy_message_get_sender (msg);
1292 name = empathy_contact_get_name (sender);
1294 if (empathy_contact_is_user (sender)) {
1295 nick_tag = "irc-nick-self";
1296 body_tag = "irc-body-self";
1298 if (empathy_chat_should_highlight_nick (msg)) {
1299 nick_tag = "irc-nick-highlight";
1301 nick_tag = "irc-nick-other";
1304 body_tag = "irc-body-other";
1307 if (priv->last_block_type != BLOCK_TYPE_SELF &&
1308 priv->last_block_type != BLOCK_TYPE_OTHER) {
1309 chat_view_append_spacing (view);
1312 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1315 tmp = g_strdup_printf ("%s: ", name);
1316 gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
1325 /* The text body. */
1326 chat_view_append_text (view, body, body_tag);
1330 chat_view_append_fancy_message (EmpathyChatView *view,
1331 EmpathyMessage *msg)
1333 EmpathyChatViewPriv *priv;
1334 EmpathyContact *sender;
1338 priv = GET_PRIV (view);
1340 sender = empathy_message_get_sender (msg);
1342 if (empathy_contact_is_user (sender)) {
1343 tag = "fancy-body-self";
1345 tag = "fancy-body-other";
1347 /* FIXME: Might want to support nick highlighting here... */
1350 body = empathy_message_get_body (msg);
1351 chat_view_append_text (view, body, tag);
1355 chat_view_theme_changed_cb (EmpathyThemeManager *manager,
1356 EmpathyChatView *view)
1358 EmpathyChatViewPriv *priv;
1359 gboolean show_avatars = FALSE;
1360 gboolean theme_rooms = FALSE;
1362 priv = GET_PRIV (view);
1364 priv->last_block_type = BLOCK_TYPE_NONE;
1366 empathy_conf_get_bool (empathy_conf_get (),
1367 EMPATHY_PREFS_CHAT_THEME_CHAT_ROOM,
1369 if (!theme_rooms && priv->is_group_chat) {
1370 empathy_theme_manager_apply (manager, view, NULL);
1372 empathy_theme_manager_apply_saved (manager, view);
1375 /* Needed for now to update the "rise" property of the names to get it
1376 * vertically centered.
1378 empathy_conf_get_bool (empathy_conf_get (),
1379 EMPATHY_PREFS_UI_SHOW_AVATARS,
1381 empathy_theme_manager_update_show_avatars (manager, view, show_avatars);
1384 /* Pads a pixbuf to the specified size, by centering it in a larger transparent
1385 * pixbuf. Returns a new ref.
1388 chat_view_pad_to_size (GdkPixbuf *pixbuf,
1391 gint extra_padding_right)
1393 gint src_width, src_height;
1395 gint x_offset, y_offset;
1397 src_width = gdk_pixbuf_get_width (pixbuf);
1398 src_height = gdk_pixbuf_get_height (pixbuf);
1400 x_offset = (width - src_width) / 2;
1401 y_offset = (height - src_height) / 2;
1403 padded = gdk_pixbuf_new (gdk_pixbuf_get_colorspace (pixbuf),
1405 gdk_pixbuf_get_bits_per_sample (pixbuf),
1406 width + extra_padding_right,
1409 gdk_pixbuf_fill (padded, 0);
1411 gdk_pixbuf_copy_area (pixbuf,
1412 0, /* source coords */
1417 x_offset, /* dest coords */
1424 empathy_chat_view_new (void)
1426 return g_object_new (EMPATHY_TYPE_CHAT_VIEW, NULL);
1429 /* The name is optional, if NULL, the sender for msg is used. */
1431 empathy_chat_view_append_message (EmpathyChatView *view,
1432 EmpathyMessage *msg)
1434 EmpathyChatViewPriv *priv;
1435 EmpathyContact *sender;
1437 gboolean scroll_down;
1439 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
1440 g_return_if_fail (EMPATHY_IS_MESSAGE (msg));
1442 priv = GET_PRIV (view);
1444 body = empathy_message_get_body (msg);
1449 scroll_down = chat_view_is_scrolled_down (view);
1451 chat_view_maybe_trim_buffer (view);
1452 chat_view_maybe_append_date_and_time (view, msg);
1454 sender = empathy_message_get_sender (msg);
1456 if (!priv->irc_style) {
1457 chat_view_maybe_append_fancy_header (view, msg);
1460 if (empathy_message_get_type (msg) == EMPATHY_MESSAGE_TYPE_ACTION) {
1461 if (priv->irc_style) {
1462 chat_view_append_irc_action (view, msg);
1464 chat_view_append_fancy_action (view, msg);
1467 if (priv->irc_style) {
1468 chat_view_append_irc_message (view, msg);
1470 chat_view_append_fancy_message (view, msg);
1474 /* Reset the last inserted contact. */
1475 if (priv->last_contact) {
1476 g_object_unref (priv->last_contact);
1479 if (empathy_contact_is_user (sender)) {
1480 priv->last_block_type = BLOCK_TYPE_SELF;
1481 priv->last_contact = NULL;
1483 priv->last_block_type = BLOCK_TYPE_OTHER;
1484 priv->last_contact = g_object_ref (sender);
1488 empathy_chat_view_scroll_down (view);
1493 empathy_chat_view_append_event (EmpathyChatView *view,
1496 EmpathyChatViewPriv *priv;
1502 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
1503 g_return_if_fail (!G_STR_EMPTY (str));
1505 priv = GET_PRIV (view);
1507 bottom = chat_view_is_scrolled_down (view);
1509 chat_view_maybe_trim_buffer (view);
1511 if (priv->irc_style) {
1513 msg = g_strdup_printf (" - %s\n", str);
1515 tag = "fancy-event";
1516 msg = g_strdup_printf (" - %s\n", str);
1519 if (priv->last_block_type != BLOCK_TYPE_EVENT) {
1520 /* Comment out for now. */
1521 /*chat_view_append_spacing (view);*/
1524 chat_view_maybe_append_date_and_time (view, NULL);
1526 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1528 gtk_text_buffer_insert_with_tags_by_name (priv->buffer, &iter,
1535 empathy_chat_view_scroll_down (view);
1538 priv->last_block_type = BLOCK_TYPE_EVENT;
1542 empathy_chat_view_append_button (EmpathyChatView *view,
1543 const gchar *message,
1547 EmpathyChatViewPriv *priv;
1548 GtkTextChildAnchor *anchor;
1553 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
1554 g_return_if_fail (button1 != NULL);
1556 priv = GET_PRIV (view);
1558 if (priv->irc_style) {
1561 tag = "fancy-invite";
1564 bottom = chat_view_is_scrolled_down (view);
1566 chat_view_maybe_append_date_and_time (view, NULL);
1569 chat_view_append_text (view, message, tag);
1572 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1574 anchor = gtk_text_buffer_create_child_anchor (priv->buffer, &iter);
1575 gtk_text_view_add_child_at_anchor (GTK_TEXT_VIEW (view), button1, anchor);
1576 gtk_widget_show (button1);
1578 gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
1586 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1588 anchor = gtk_text_buffer_create_child_anchor (priv->buffer, &iter);
1589 gtk_text_view_add_child_at_anchor (GTK_TEXT_VIEW (view), button2, anchor);
1590 gtk_widget_show (button2);
1592 gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
1600 gtk_text_buffer_get_end_iter (priv->buffer, &iter);
1601 gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
1609 empathy_chat_view_scroll_down (view);
1612 priv->last_block_type = BLOCK_TYPE_INVITE;
1616 empathy_chat_view_scroll (EmpathyChatView *view,
1617 gboolean allow_scrolling)
1619 EmpathyChatViewPriv *priv;
1621 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
1623 priv = GET_PRIV (view);
1625 priv->allow_scrolling = allow_scrolling;
1627 empathy_debug (DEBUG_DOMAIN, "Scrolling %s",
1628 allow_scrolling ? "enabled" : "disabled");
1631 /* Code stolen from pidgin/gtkimhtml.c */
1633 chat_view_scroll_cb (EmpathyChatView *view)
1635 EmpathyChatViewPriv *priv;
1639 priv = GET_PRIV (view);
1640 adj = GTK_TEXT_VIEW (view)->vadjustment;
1641 max_val = adj->upper - adj->page_size;
1643 g_return_val_if_fail (priv->scroll_time != NULL, FALSE);
1645 if (g_timer_elapsed (priv->scroll_time, NULL) > MAX_SCROLL_TIME) {
1646 /* time's up. jump to the end and kill the timer */
1647 gtk_adjustment_set_value (adj, max_val);
1648 g_timer_destroy (priv->scroll_time);
1649 priv->scroll_time = NULL;
1650 priv->scroll_timeout = 0;
1654 /* scroll by 1/3rd the remaining distance */
1655 gtk_adjustment_set_value (adj, gtk_adjustment_get_value (adj) + ((max_val - gtk_adjustment_get_value (adj)) / 3));
1660 empathy_chat_view_scroll_down (EmpathyChatView *view)
1662 EmpathyChatViewPriv *priv;
1664 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
1666 priv = GET_PRIV (view);
1668 if (!priv->allow_scrolling) {
1672 empathy_debug (DEBUG_DOMAIN, "Scrolling down");
1674 if (priv->scroll_time) {
1675 g_timer_reset (priv->scroll_time);
1677 priv->scroll_time = g_timer_new();
1679 if (!priv->scroll_timeout) {
1680 priv->scroll_timeout = g_timeout_add (SCROLL_DELAY,
1681 (GSourceFunc) chat_view_scroll_cb,
1687 empathy_chat_view_get_selection_bounds (EmpathyChatView *view,
1691 GtkTextBuffer *buffer;
1693 g_return_val_if_fail (EMPATHY_IS_CHAT_VIEW (view), FALSE);
1695 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
1697 return gtk_text_buffer_get_selection_bounds (buffer, start, end);
1701 empathy_chat_view_clear (EmpathyChatView *view)
1703 GtkTextBuffer *buffer;
1704 EmpathyChatViewPriv *priv;
1706 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
1708 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
1709 gtk_text_buffer_set_text (buffer, "", -1);
1711 /* We set these back to the initial values so we get
1712 * timestamps when clearing the window to know when
1713 * conversations start.
1715 priv = GET_PRIV (view);
1717 priv->last_block_type = BLOCK_TYPE_NONE;
1718 priv->last_timestamp = 0;
1722 empathy_chat_view_find_previous (EmpathyChatView *view,
1723 const gchar *search_criteria,
1724 gboolean new_search)
1726 EmpathyChatViewPriv *priv;
1727 GtkTextBuffer *buffer;
1728 GtkTextIter iter_at_mark;
1729 GtkTextIter iter_match_start;
1730 GtkTextIter iter_match_end;
1732 gboolean from_start = FALSE;
1734 g_return_val_if_fail (EMPATHY_IS_CHAT_VIEW (view), FALSE);
1735 g_return_val_if_fail (search_criteria != NULL, FALSE);
1737 priv = GET_PRIV (view);
1739 buffer = priv->buffer;
1741 if (G_STR_EMPTY (search_criteria)) {
1742 if (priv->find_mark_previous) {
1743 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1745 gtk_text_buffer_move_mark (buffer,
1746 priv->find_mark_previous,
1748 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
1749 priv->find_mark_previous,
1754 gtk_text_buffer_select_range (buffer,
1766 if (priv->find_mark_previous) {
1767 gtk_text_buffer_get_iter_at_mark (buffer,
1769 priv->find_mark_previous);
1771 gtk_text_buffer_get_end_iter (buffer, &iter_at_mark);
1775 priv->find_last_direction = FALSE;
1777 found = empathy_text_iter_backward_search (&iter_at_mark,
1784 gboolean result = FALSE;
1790 /* Here we wrap around. */
1791 if (!new_search && !priv->find_wrapped) {
1792 priv->find_wrapped = TRUE;
1793 result = empathy_chat_view_find_previous (view,
1796 priv->find_wrapped = FALSE;
1802 /* Set new mark and show on screen */
1803 if (!priv->find_mark_previous) {
1804 priv->find_mark_previous = gtk_text_buffer_create_mark (buffer, NULL,
1808 gtk_text_buffer_move_mark (buffer,
1809 priv->find_mark_previous,
1813 if (!priv->find_mark_next) {
1814 priv->find_mark_next = gtk_text_buffer_create_mark (buffer, NULL,
1818 gtk_text_buffer_move_mark (buffer,
1819 priv->find_mark_next,
1823 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
1824 priv->find_mark_previous,
1830 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &iter_match_start);
1831 gtk_text_buffer_move_mark_by_name (buffer, "insert", &iter_match_end);
1837 empathy_chat_view_find_next (EmpathyChatView *view,
1838 const gchar *search_criteria,
1839 gboolean new_search)
1841 EmpathyChatViewPriv *priv;
1842 GtkTextBuffer *buffer;
1843 GtkTextIter iter_at_mark;
1844 GtkTextIter iter_match_start;
1845 GtkTextIter iter_match_end;
1847 gboolean from_start = FALSE;
1849 g_return_val_if_fail (EMPATHY_IS_CHAT_VIEW (view), FALSE);
1850 g_return_val_if_fail (search_criteria != NULL, FALSE);
1852 priv = GET_PRIV (view);
1854 buffer = priv->buffer;
1856 if (G_STR_EMPTY (search_criteria)) {
1857 if (priv->find_mark_next) {
1858 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1860 gtk_text_buffer_move_mark (buffer,
1861 priv->find_mark_next,
1863 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
1864 priv->find_mark_next,
1869 gtk_text_buffer_select_range (buffer,
1881 if (priv->find_mark_next) {
1882 gtk_text_buffer_get_iter_at_mark (buffer,
1884 priv->find_mark_next);
1886 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1890 priv->find_last_direction = TRUE;
1892 found = empathy_text_iter_forward_search (&iter_at_mark,
1899 gboolean result = FALSE;
1905 /* Here we wrap around. */
1906 if (!new_search && !priv->find_wrapped) {
1907 priv->find_wrapped = TRUE;
1908 result = empathy_chat_view_find_next (view,
1911 priv->find_wrapped = FALSE;
1917 /* Set new mark and show on screen */
1918 if (!priv->find_mark_next) {
1919 priv->find_mark_next = gtk_text_buffer_create_mark (buffer, NULL,
1923 gtk_text_buffer_move_mark (buffer,
1924 priv->find_mark_next,
1928 if (!priv->find_mark_previous) {
1929 priv->find_mark_previous = gtk_text_buffer_create_mark (buffer, NULL,
1933 gtk_text_buffer_move_mark (buffer,
1934 priv->find_mark_previous,
1938 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
1939 priv->find_mark_next,
1945 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &iter_match_start);
1946 gtk_text_buffer_move_mark_by_name (buffer, "insert", &iter_match_end);
1953 empathy_chat_view_find_abilities (EmpathyChatView *view,
1954 const gchar *search_criteria,
1955 gboolean *can_do_previous,
1956 gboolean *can_do_next)
1958 EmpathyChatViewPriv *priv;
1959 GtkTextBuffer *buffer;
1960 GtkTextIter iter_at_mark;
1961 GtkTextIter iter_match_start;
1962 GtkTextIter iter_match_end;
1964 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
1965 g_return_if_fail (search_criteria != NULL);
1966 g_return_if_fail (can_do_previous != NULL && can_do_next != NULL);
1968 priv = GET_PRIV (view);
1970 buffer = priv->buffer;
1972 if (can_do_previous) {
1973 if (priv->find_mark_previous) {
1974 gtk_text_buffer_get_iter_at_mark (buffer,
1976 priv->find_mark_previous);
1978 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1981 *can_do_previous = empathy_text_iter_backward_search (&iter_at_mark,
1989 if (priv->find_mark_next) {
1990 gtk_text_buffer_get_iter_at_mark (buffer,
1992 priv->find_mark_next);
1994 gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
1997 *can_do_next = empathy_text_iter_forward_search (&iter_at_mark,
2006 empathy_chat_view_highlight (EmpathyChatView *view,
2009 GtkTextBuffer *buffer;
2011 GtkTextIter iter_start;
2012 GtkTextIter iter_end;
2013 GtkTextIter iter_match_start;
2014 GtkTextIter iter_match_end;
2017 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
2019 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
2021 gtk_text_buffer_get_start_iter (buffer, &iter);
2023 gtk_text_buffer_get_bounds (buffer, &iter_start, &iter_end);
2024 gtk_text_buffer_remove_tag_by_name (buffer, "highlight",
2028 if (G_STR_EMPTY (text)) {
2033 found = empathy_text_iter_forward_search (&iter,
2043 gtk_text_buffer_apply_tag_by_name (buffer, "highlight",
2047 iter = iter_match_end;
2048 gtk_text_iter_forward_char (&iter);
2053 empathy_chat_view_copy_clipboard (EmpathyChatView *view)
2055 GtkTextBuffer *buffer;
2056 GtkClipboard *clipboard;
2058 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
2060 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
2061 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
2063 gtk_text_buffer_copy_clipboard (buffer, clipboard);
2067 empathy_chat_view_get_irc_style (EmpathyChatView *view)
2069 EmpathyChatViewPriv *priv;
2071 g_return_val_if_fail (EMPATHY_IS_CHAT_VIEW (view), FALSE);
2073 priv = GET_PRIV (view);
2075 return priv->irc_style;
2079 empathy_chat_view_set_irc_style (EmpathyChatView *view,
2082 EmpathyChatViewPriv *priv;
2084 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
2086 priv = GET_PRIV (view);
2088 priv->irc_style = irc_style;
2092 empathy_chat_view_set_margin (EmpathyChatView *view,
2095 EmpathyChatViewPriv *priv;
2097 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
2099 priv = GET_PRIV (view);
2102 "left-margin", margin,
2103 "right-margin", margin,
2108 empathy_chat_view_get_smiley_image (EmpathySmiley smiley)
2110 static GdkPixbuf *pixbufs[EMPATHY_SMILEY_COUNT];
2111 static gboolean inited = FALSE;
2116 for (i = 0; i < EMPATHY_SMILEY_COUNT; i++) {
2117 pixbufs[i] = empathy_pixbuf_from_smiley (i, GTK_ICON_SIZE_MENU);
2123 return pixbufs[smiley];
2127 empathy_chat_view_get_smiley_text (EmpathySmiley smiley)
2131 for (i = 0; i < G_N_ELEMENTS (smileys); i++) {
2132 if (smileys[i].smiley != smiley) {
2136 return smileys[i].pattern;
2143 empathy_chat_view_get_smiley_menu (GCallback callback,
2145 GtkTooltips *tooltips)
2152 g_return_val_if_fail (callback != NULL, NULL);
2154 menu = gtk_menu_new ();
2156 for (i = 0, x = 0, y = 0; i < EMPATHY_SMILEY_COUNT; i++) {
2160 const gchar *smiley_text;
2162 pixbuf = empathy_chat_view_get_smiley_image (i);
2167 image = gtk_image_new_from_pixbuf (pixbuf);
2169 item = gtk_image_menu_item_new_with_label ("");
2170 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
2172 gtk_menu_attach (GTK_MENU (menu), item,
2173 x, x + 1, y, y + 1);
2175 smiley_text = empathy_chat_view_get_smiley_text (i);
2177 gtk_tooltips_set_tip (tooltips,
2182 g_object_set_data (G_OBJECT (item), "smiley_text", (gpointer) smiley_text);
2183 g_signal_connect (item, "activate", callback, user_data);
2193 gtk_widget_show_all (menu);
2198 /* FIXME: Do we really need this? Better to do it internally only at setup time,
2199 * we will never change it on the fly.
2202 empathy_chat_view_set_is_group_chat (EmpathyChatView *view,
2203 gboolean is_group_chat)
2205 EmpathyChatViewPriv *priv;
2206 gboolean theme_rooms = FALSE;
2208 g_return_if_fail (EMPATHY_IS_CHAT_VIEW (view));
2210 priv = GET_PRIV (view);
2212 priv->is_group_chat = is_group_chat;
2214 empathy_conf_get_bool (empathy_conf_get (),
2215 EMPATHY_PREFS_CHAT_THEME_CHAT_ROOM,
2218 if (!theme_rooms && is_group_chat) {
2219 empathy_theme_manager_apply (empathy_theme_manager_get (),
2223 empathy_theme_manager_apply_saved (empathy_theme_manager_get (),