2005-09-18 [colin] 1.9.14cvs41
[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 #include <gtk/gtkmenuitem.h>
34 #include <gtk/gtkseparatormenuitem.h>
35
36 #include "logwindow.h"
37 #include "utils.h"
38 #include "gtkutils.h"
39 #include "log.h"
40 #include "hooks.h"
41
42 static void hide_cb                             (GtkWidget      *widget,
43                                                  LogWindow      *logwin);
44 static gboolean key_pressed                     (GtkWidget      *widget,
45                                                  GdkEventKey    *event,
46                                                  LogWindow      *logwin);
47 static gboolean log_window_append               (gpointer        source,
48                                                  gpointer        data);
49 static void log_window_clip                     (GtkWidget      *text,
50                                                  guint           glip_length);
51 static void log_window_clear                    (GtkWidget      *widget,
52                                                  LogWindow      *logwin);
53 static void log_window_popup_menu_extend        (GtkTextView    *textview,
54                                                  GtkMenu        *menu,
55                                                  LogWindow      *logwin);
56                                          
57 LogWindow *log_window_create(void)
58 {
59         LogWindow *logwin;
60         GtkWidget *window;
61         GtkWidget *scrolledwin;
62         GtkWidget *text;
63         GtkTextBuffer *buffer;
64         GtkTextIter iter;
65
66         debug_print("Creating log window...\n");
67         logwin = g_new0(LogWindow, 1);
68
69         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
70         gtk_window_set_title(GTK_WINDOW(window), _("Protocol log"));
71         gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
72         gtk_widget_set_size_request(window, 520, 400);
73         g_signal_connect(G_OBJECT(window), "delete_event",
74                          G_CALLBACK(gtk_widget_hide_on_delete), NULL);
75         g_signal_connect(G_OBJECT(window), "key_press_event",
76                          G_CALLBACK(key_pressed), logwin);
77         g_signal_connect(G_OBJECT(window), "hide",
78                          G_CALLBACK(hide_cb), logwin);
79         gtk_widget_realize(window);
80
81         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
82         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
83                                        GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
84         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwin),
85                                             GTK_SHADOW_IN);
86         gtk_container_add(GTK_CONTAINER(window), scrolledwin);
87         gtk_widget_show(scrolledwin);
88
89         text = gtk_text_view_new();
90         gtk_text_view_set_editable(GTK_TEXT_VIEW(text), FALSE);
91         gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text), GTK_WRAP_WORD);
92         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
93         gtk_text_buffer_get_start_iter(buffer, &iter);
94         gtk_text_buffer_create_mark(buffer, "end", &iter, FALSE);
95         g_signal_connect(G_OBJECT(text), "populate-popup",
96                          G_CALLBACK(log_window_popup_menu_extend), logwin);
97         gtk_container_add(GTK_CONTAINER(scrolledwin), text);
98         gtk_widget_show(text);
99
100         logwin->window = window;
101         logwin->scrolledwin = scrolledwin;
102         logwin->text = text;
103         logwin->hook_id = hooks_register_hook(LOG_APPEND_TEXT_HOOKLIST, log_window_append, logwin);
104
105         return logwin;
106 }
107
108 void log_window_init(LogWindow *logwin)
109 {
110         GtkTextBuffer *buffer;
111         GdkColormap *colormap;
112         GdkColor color[3] =
113                 {{0, 0, 0xafff, 0}, {0, 0xefff, 0, 0}, {0, 0xefff, 0, 0}};
114         gboolean success[3];
115         gint i;
116
117         logwin->msg_color   = color[0];
118         logwin->warn_color  = color[1];
119         logwin->error_color = color[2];
120
121         colormap = gdk_drawable_get_colormap(logwin->window->window);
122         gdk_colormap_alloc_colors(colormap, color, 3, FALSE, TRUE, success);
123
124         for (i = 0; i < 3; i++) {
125                 if (success[i] == FALSE) {
126                         GtkStyle *style;
127
128                         g_warning("LogWindow: color allocation failed\n");
129                         style = gtk_widget_get_style(logwin->window);
130                         logwin->msg_color = logwin->warn_color =
131                         logwin->error_color = style->black;
132                         break;
133                 }
134         }
135
136         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(logwin->text));
137         gtk_text_buffer_create_tag(buffer, "message",
138                                    "foreground-gdk", &logwin->msg_color,
139                                    NULL);
140         gtk_text_buffer_create_tag(buffer, "warn",
141                                    "foreground-gdk", &logwin->warn_color,
142                                    NULL);
143         gtk_text_buffer_create_tag(buffer, "error",
144                                    "foreground-gdk", &logwin->error_color,
145                                    NULL);
146 }
147
148 void log_window_show(LogWindow *logwin)
149 {
150         GtkTextView *text = GTK_TEXT_VIEW(logwin->text);
151         GtkTextBuffer *buffer = gtk_text_view_get_buffer(text);
152         GtkTextMark *mark;
153
154         mark = gtk_text_buffer_get_mark(buffer, "end");
155         gtk_text_view_scroll_mark_onscreen(text, mark);
156
157         gtk_widget_show(logwin->window);
158 }
159
160 void log_window_set_clipping(LogWindow *logwin, gboolean clip, guint clip_length)
161 {
162         g_return_if_fail(logwin != NULL);
163
164         logwin->clip = clip;
165         logwin->clip_length = clip_length;
166 }
167
168 static gboolean log_window_append(gpointer source, gpointer data)
169 {
170         LogText *logtext = (LogText *) source;
171         LogWindow *logwindow = (LogWindow *) data;
172         GtkTextView *text;
173         GtkTextBuffer *buffer;
174         GtkTextIter iter;
175         GdkColor *color = NULL;
176         gchar *head = NULL;
177         const gchar *tag;
178
179         g_return_val_if_fail(logtext != NULL, TRUE);
180         g_return_val_if_fail(logtext->text != NULL, TRUE);
181         g_return_val_if_fail(logwindow != NULL, FALSE);
182
183         if (logwindow->clip && !logwindow->clip_length)
184                 return FALSE;
185
186         text = GTK_TEXT_VIEW(logwindow->text);
187         buffer = gtk_text_view_get_buffer(text);
188         gtk_text_buffer_get_iter_at_offset(buffer, &iter, -1);
189
190         switch (logtext->type) {
191         case LOG_MSG:
192                 color = &logwindow->msg_color;
193                 tag = "message";
194                 head = "* ";
195                 break;
196         case LOG_WARN:
197                 color = &logwindow->warn_color;
198                 tag = "warn";
199                 head = "** ";
200                 break;
201         case LOG_ERROR:
202                 color = &logwindow->error_color;
203                 tag = "error";
204                 head = "*** ";
205                 break;
206         default:
207                 tag = NULL;
208                 break;
209         }
210   
211         if (head)
212                 gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, head, -1,
213                                                          tag, NULL);
214         gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, logtext->text, -1,
215                                                  tag, NULL);
216
217         gtk_text_buffer_get_start_iter(buffer, &iter);
218
219         if (logwindow->clip)
220                log_window_clip (GTK_WIDGET (text), logwindow->clip_length);
221
222         gtk_text_buffer_get_iter_at_offset(buffer, &iter, -1);
223         gtk_text_view_scroll_to_iter(text, &iter, 0, TRUE, 0, 0);
224         gtk_text_buffer_place_cursor(buffer, &iter);
225
226         return FALSE;
227 }
228
229 static void hide_cb(GtkWidget *widget, LogWindow *logwin)
230 {
231 }
232
233 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event,
234                         LogWindow *logwin)
235 {
236         if (event && event->keyval == GDK_Escape)
237                 gtk_widget_hide(logwin->window);
238         else if (event && event->keyval == GDK_Delete) 
239                 log_window_clear(NULL, logwin);
240
241         return FALSE;
242 }
243
244 static void log_window_clip(GtkWidget *textw, guint clip_length)
245 {
246         guint length;
247         guint point;
248         GtkTextView *textview = GTK_TEXT_VIEW(textw);
249         GtkTextBuffer *textbuf = gtk_text_view_get_buffer(textview);
250         GtkTextIter start_iter, end_iter;
251         
252         length = gtk_text_buffer_get_line_count(textbuf);
253         /* debug_print("Log window length: %u\n", length); */
254         
255         if (length > clip_length) {
256                 /* find the end of the first line after the cut off
257                  * point */
258                 point = length - clip_length;
259                 gtk_text_buffer_get_iter_at_line(textbuf, &end_iter, point);
260                 if (!gtk_text_iter_forward_to_line_end(&end_iter))
261                         return;
262                 gtk_text_buffer_get_start_iter(textbuf, &start_iter);
263                 gtk_text_buffer_delete(textbuf, &start_iter, &end_iter);
264         }
265 }
266
267 static void log_window_clear(GtkWidget *widget, LogWindow *logwin)
268 {
269         GtkTextView *textview = GTK_TEXT_VIEW(logwin->text);
270         GtkTextBuffer *textbuf = gtk_text_view_get_buffer(textview);
271         GtkTextIter start_iter, end_iter;
272         
273         gtk_text_buffer_get_start_iter(textbuf, &start_iter);
274         gtk_text_buffer_get_end_iter(textbuf, &end_iter);
275         gtk_text_buffer_delete(textbuf, &start_iter, &end_iter);
276 }
277
278 static void log_window_popup_menu_extend(GtkTextView *textview,
279                         GtkMenu *menu, LogWindow *logwin)
280 {
281         GtkWidget *menuitem;
282         
283         g_return_if_fail(menu != NULL);
284         g_return_if_fail(GTK_IS_MENU_SHELL(menu));
285
286         menuitem = gtk_separator_menu_item_new();
287         gtk_menu_shell_prepend(GTK_MENU_SHELL(menu), menuitem);
288         gtk_widget_show(menuitem);
289         
290         menuitem = gtk_menu_item_new_with_mnemonic(_("Clear _Log"));
291         g_signal_connect(G_OBJECT(menuitem), "activate",
292                          G_CALLBACK(log_window_clear), logwin);
293         gtk_menu_shell_prepend(GTK_MENU_SHELL(menu), menuitem);
294         gtk_widget_show(menuitem);
295 }
296