2005-02-24 [colin] 1.0.1cvs15.11
[claws.git] / src / gtk / logwindow.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2003 Hiroyuki Yamamoto
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
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
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include <glib.h>
25 #include <glib/gi18n.h>
26 #include <gdk/gdkkeysyms.h>
27 #include <gtk/gtkwidget.h>
28 #include <gtk/gtkwindow.h>
29 #include <gtk/gtksignal.h>
30 #include <gtk/gtkscrolledwindow.h>
31 #include <gtk/gtktextview.h>
32 #include <gtk/gtkstyle.h>
33
34 #include "logwindow.h"
35 #include "utils.h"
36 #include "gtkutils.h"
37 #include "log.h"
38 #include "hooks.h"
39
40 static void hide_cb                     (GtkWidget      *widget,
41                                          LogWindow      *logwin);
42 static gboolean key_pressed             (GtkWidget      *widget,
43                                          GdkEventKey    *event,
44                                          LogWindow      *logwin);
45 static gboolean log_window_append       (gpointer        source,
46                                          gpointer        data);
47 static void log_window_clip             (GtkWidget      *text,
48                                          guint           glip_length);
49
50 LogWindow *log_window_create(void)
51 {
52         LogWindow *logwin;
53         GtkWidget *window;
54         GtkWidget *scrolledwin;
55         GtkWidget *text;
56         GtkTextBuffer *buffer;
57         GtkTextIter iter;
58
59         debug_print("Creating log window...\n");
60         logwin = g_new0(LogWindow, 1);
61
62         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
63         gtk_window_set_title(GTK_WINDOW(window), _("Protocol log"));
64         gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
65         gtk_widget_set_size_request(window, 520, 400);
66         g_signal_connect(G_OBJECT(window), "delete_event",
67                          G_CALLBACK(gtk_widget_hide_on_delete), NULL);
68         g_signal_connect(G_OBJECT(window), "key_press_event",
69                          G_CALLBACK(key_pressed), logwin);
70         g_signal_connect(G_OBJECT(window), "hide",
71                          G_CALLBACK(hide_cb), logwin);
72         gtk_widget_realize(window);
73
74         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
75         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
76                                        GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
77         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwin),
78                                             GTK_SHADOW_IN);
79         gtk_container_add(GTK_CONTAINER(window), scrolledwin);
80         gtk_widget_show(scrolledwin);
81
82         text = gtk_text_view_new();
83         gtk_text_view_set_editable(GTK_TEXT_VIEW(text), FALSE);
84         gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text), GTK_WRAP_WORD);
85         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
86         gtk_text_buffer_get_start_iter(buffer, &iter);
87         gtk_text_buffer_create_mark(buffer, "end", &iter, FALSE);
88         gtk_container_add(GTK_CONTAINER(scrolledwin), text);
89         gtk_widget_show(text);
90
91         logwin->window = window;
92         logwin->scrolledwin = scrolledwin;
93         logwin->text = text;
94         logwin->hook_id = hooks_register_hook(LOG_APPEND_TEXT_HOOKLIST, log_window_append, logwin);
95
96         return logwin;
97 }
98
99 void log_window_init(LogWindow *logwin)
100 {
101         GtkTextBuffer *buffer;
102         GdkColormap *colormap;
103         GdkColor color[3] =
104                 {{0, 0, 0xafff, 0}, {0, 0xefff, 0, 0}, {0, 0xefff, 0, 0}};
105         gboolean success[3];
106         gint i;
107
108         gtkut_widget_disable_theme_engine(logwin->text);
109
110         logwin->msg_color   = color[0];
111         logwin->warn_color  = color[1];
112         logwin->error_color = color[2];
113
114         colormap = gdk_window_get_colormap(logwin->window->window);
115         gdk_colormap_alloc_colors(colormap, color, 3, FALSE, TRUE, success);
116
117         for (i = 0; i < 3; i++) {
118                 if (success[i] == FALSE) {
119                         GtkStyle *style;
120
121                         g_warning("LogWindow: color allocation failed\n");
122                         style = gtk_widget_get_style(logwin->window);
123                         logwin->msg_color = logwin->warn_color =
124                         logwin->error_color = style->black;
125                         break;
126                 }
127         }
128
129         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(logwin->text));
130         gtk_text_buffer_create_tag(buffer, "message",
131                                    "foreground-gdk", &logwin->msg_color,
132                                    NULL);
133         gtk_text_buffer_create_tag(buffer, "warn",
134                                    "foreground-gdk", &logwin->warn_color,
135                                    NULL);
136         gtk_text_buffer_create_tag(buffer, "error",
137                                    "foreground-gdk", &logwin->error_color,
138                                    NULL);
139 }
140
141 void log_window_show(LogWindow *logwin)
142 {
143         GtkTextView *text = GTK_TEXT_VIEW(logwin->text);
144         GtkTextBuffer *buffer = gtk_text_view_get_buffer(text);
145         GtkTextMark *mark;
146
147         mark = gtk_text_buffer_get_mark(buffer, "end");
148         gtk_text_view_scroll_mark_onscreen(text, mark);
149
150         gtk_widget_show(logwin->window);
151 }
152
153 void log_window_set_clipping(LogWindow *logwin, gboolean clip, guint clip_length)
154 {
155         g_return_if_fail(logwin != NULL);
156
157         logwin->clip = clip;
158         logwin->clip_length = clip_length;
159 }
160
161 static gboolean log_window_append(gpointer source, gpointer data)
162 {
163         LogText *logtext = (LogText *) source;
164         LogWindow *logwindow = (LogWindow *) data;
165         GtkTextView *text;
166         GtkTextBuffer *buffer;
167         GtkTextIter iter;
168         GdkColor *color = NULL;
169         gchar *head = NULL;
170         const gchar *tag;
171
172         g_return_val_if_fail(logtext != NULL, TRUE);
173         g_return_val_if_fail(logtext->text != NULL, TRUE);
174         g_return_val_if_fail(logwindow != NULL, FALSE);
175
176         if (logwindow->clip && !logwindow->clip_length)
177                 return FALSE;
178
179         text = GTK_TEXT_VIEW(logwindow->text);
180         buffer = gtk_text_view_get_buffer(text);
181         gtk_text_buffer_get_iter_at_offset(buffer, &iter, -1);
182
183         switch (logtext->type) {
184         case LOG_MSG:
185                 color = &logwindow->msg_color;
186                 tag = "message";
187                 head = "* ";
188                 break;
189         case LOG_WARN:
190                 color = &logwindow->warn_color;
191                 tag = "warn";
192                 head = "** ";
193                 break;
194         case LOG_ERROR:
195                 color = &logwindow->error_color;
196                 tag = "error";
197                 head = "*** ";
198                 break;
199         default:
200                 tag = NULL;
201                 break;
202         }
203   
204         if (head)
205                 gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, head, -1,
206                                                          tag, NULL);
207         gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, logtext->text, -1,
208                                                  tag, NULL);
209
210         gtk_text_buffer_get_start_iter(buffer, &iter);
211         gtk_text_buffer_place_cursor(buffer, &iter);
212
213         if (logwindow->clip)
214                log_window_clip (GTK_WIDGET (text), logwindow->clip_length);
215
216         gtk_text_buffer_get_iter_at_offset(buffer, &iter, -1);
217         gtk_text_view_scroll_to_iter(text, &iter, 0, TRUE, 0, 0);
218
219         return FALSE;
220 }
221
222 static void hide_cb(GtkWidget *widget, LogWindow *logwin)
223 {
224 }
225
226 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event,
227                         LogWindow *logwin)
228 {
229         if (event && event->keyval == GDK_Escape)
230                 gtk_widget_hide(logwin->window);
231         return FALSE;
232 }
233
234 static void log_window_clip(GtkWidget *textw, guint clip_length)
235 {
236         guint length;
237         guint point;
238         GtkTextView *textview = GTK_TEXT_VIEW(textw);
239         GtkTextBuffer *textbuf = gtk_text_view_get_buffer(textview);
240         GtkTextIter start_iter, end_iter;
241         
242         length = gtk_text_buffer_get_char_count (textbuf);
243         debug_print("Log window length: %u\n", length);
244         
245         if (length > clip_length) {
246                 /* find the end of the first line after the cut off
247                  * point */
248                 point = length - clip_length;
249                 gtk_text_buffer_get_iter_at_offset(textbuf, &end_iter, point);
250                 if (!gtk_text_iter_forward_to_line_end(&end_iter))
251                         return;
252                 gtk_text_buffer_get_start_iter(textbuf, &start_iter);
253                 gtk_text_buffer_delete(textbuf, &start_iter, &end_iter);
254         }
255 }