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