0.8.6claws51
[claws.git] / src / logwindow.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2002 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 <gdk/gdkkeysyms.h>
26 #include <gtk/gtkwidget.h>
27 #include <gtk/gtkwindow.h>
28 #include <gtk/gtksignal.h>
29 #include <gtk/gtkscrolledwindow.h>
30 #include <gtk/gtktext.h>
31 #include <gtk/gtkstyle.h>
32
33 #include "intl.h"
34 #include "logwindow.h"
35 #include "utils.h"
36 #include "gtkutils.h"
37 #include "prefs_common.h"
38 #include "log.h"
39 #include "hooks.h"
40
41 static void hide_cb     (GtkWidget      *widget,
42                          LogWindow      *logwin);
43 static void key_pressed (GtkWidget      *widget,
44                          GdkEventKey    *event,
45                          LogWindow      *logwin);
46 void log_window_append  (gpointer        source,
47                          gpointer        data);
48 void log_window_clear   (GtkWidget      *text);
49
50 LogWindow *log_window_create(void)
51 {
52         LogWindow *logwin;
53         GtkWidget *window;
54         GtkWidget *scrolledwin;
55         GtkWidget *text;
56
57         debug_print("Creating log window...\n");
58         logwin = g_new0(LogWindow, 1);
59
60         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
61         gtk_window_set_title(GTK_WINDOW(window), _("Protocol log"));
62         gtk_window_set_wmclass(GTK_WINDOW(window), "log_window", "Sylpheed");
63         gtk_window_set_policy(GTK_WINDOW(window), TRUE, TRUE, FALSE);
64         gtk_widget_set_usize(window, 520, 400);
65         gtk_signal_connect(GTK_OBJECT(window), "delete_event",
66                            GTK_SIGNAL_FUNC(gtk_widget_hide_on_delete), NULL);
67         gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
68                            GTK_SIGNAL_FUNC(key_pressed), logwin);
69         gtk_signal_connect(GTK_OBJECT(window), "hide",
70                            GTK_SIGNAL_FUNC(hide_cb), logwin);
71         gtk_widget_realize(window);
72
73         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
74         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
75                                        GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
76         gtk_container_add(GTK_CONTAINER(window), scrolledwin);
77         gtk_widget_show(scrolledwin);
78
79         text = gtk_text_new(gtk_scrolled_window_get_hadjustment
80                             (GTK_SCROLLED_WINDOW(scrolledwin)),
81                             gtk_scrolled_window_get_vadjustment
82                             (GTK_SCROLLED_WINDOW(scrolledwin)));
83         gtk_container_add(GTK_CONTAINER(scrolledwin), text);
84         gtk_widget_show(text);
85         gtk_text_freeze(GTK_TEXT(text));
86
87         logwin->window = window;
88         logwin->scrolledwin = scrolledwin;
89         logwin->text = text;
90         logwin->hook_id = hooks_register_hook(LOG_APPEND_TEXT_HOOKLIST, log_window_append, logwin);
91
92         return logwin;
93 }
94
95 void log_window_init(LogWindow *logwin)
96 {
97         GdkColormap *colormap;
98         GdkColor color[3] =
99                 {{0, 0, 0xafff, 0}, {0, 0xefff, 0, 0}, {0, 0xefff, 0, 0}};
100         gboolean success[3];
101         gint i;
102
103         gtkut_widget_disable_theme_engine(logwin->text);
104
105         logwin->msg_color   = color[0];
106         logwin->warn_color  = color[1];
107         logwin->error_color = color[2];
108
109         colormap = gdk_window_get_colormap(logwin->window->window);
110         gdk_colormap_alloc_colors(colormap, color, 3, FALSE, TRUE, success);
111
112         for (i = 0; i < 3; i++) {
113                 if (success[i] == FALSE) {
114                         GtkStyle *style;
115
116                         g_warning("LogWindow: color allocation failed\n");
117                         style = gtk_widget_get_style(logwin->window);
118                         logwin->msg_color = logwin->warn_color =
119                         logwin->error_color = style->black;
120                         break;
121                 }
122         }
123 }
124
125 void log_window_show(LogWindow *logwin)
126 {
127         GtkText *text = GTK_TEXT(logwin->text);
128
129         gtk_widget_hide(logwin->window);
130
131         gtk_text_thaw(text);
132         text->vadj->value = text->vadj->upper - text->vadj->page_size;
133         gtk_signal_emit_by_name(GTK_OBJECT(text->vadj), "value_changed");
134
135         gtk_widget_show(logwin->window);
136 }
137
138 void log_window_append(gpointer source, gpointer data)
139 {
140         LogText *logtext = (LogText *) source;
141         LogWindow *logwindow = (LogWindow *) data;
142         GtkText *text;
143         GdkColor *color = NULL;
144         gchar *head = NULL;
145
146         g_return_if_fail(logtext != NULL);
147         g_return_if_fail(logtext->text != NULL);
148         g_return_if_fail(logwindow != NULL);
149
150         if (prefs_common.cliplog && !prefs_common.loglength)
151                 return;
152
153         text = GTK_TEXT(logwindow->text);
154
155         switch (logtext->type) {
156         case LOG_MSG:
157                 color = &logwindow->msg_color;
158                 head = "* ";
159                 break;
160         case LOG_WARN:
161                 color = &logwindow->warn_color;
162                 head = "** ";
163                 break;
164         case LOG_ERROR:
165                 color = &logwindow->error_color;
166                 head = "*** ";
167                 break;
168         default:
169                 break;
170         }
171
172         if (head) gtk_text_insert(text, NULL, color, NULL, head, -1);
173         gtk_text_insert(text, NULL, color, NULL, logtext->text, -1);
174         if (prefs_common.cliplog)
175                log_window_clear (GTK_WIDGET (text));
176 }
177
178 static void hide_cb(GtkWidget *widget, LogWindow *logwin)
179 {
180         if (GTK_TEXT(logwin->text)->freeze_count == 0)
181                 gtk_text_freeze(GTK_TEXT(logwin->text));
182 }
183
184 static void key_pressed(GtkWidget *widget, GdkEventKey *event,
185                         LogWindow *logwin)
186 {
187         if (event && event->keyval == GDK_Escape)
188                 gtk_widget_hide(logwin->window);
189 }
190
191 void log_window_clear(GtkWidget *textw)
192 {
193         guint length;
194         guint point;
195         gboolean was_frozen = FALSE;
196         GtkText *text = GTK_TEXT(textw);
197         
198         length = gtk_text_get_length (text);
199         debug_print("Log window length: %u\n", length);
200         
201         if (length > prefs_common.loglength) {
202                 /* find the end of the first line after the cut off
203                  * point */
204                 point = length - prefs_common.loglength;
205                 while (point < length && GTK_TEXT_INDEX(text, point) != '\n')
206                         point++;
207                 /* erase the text */
208                 if (text->freeze_count) {
209                         was_frozen = TRUE;
210                         gtk_text_thaw(text);
211                 }
212                 gtk_text_set_point (text, 0);
213                 gtk_text_freeze(text);
214                 if (!gtk_text_forward_delete (text, point + 1))
215                         debug_print("Error clearing log\n");
216                 gtk_text_thaw(text);
217                 gtk_text_set_point(text,
218                                    gtk_text_get_length (GTK_TEXT (text)));
219                 if (was_frozen)
220                         gtk_text_freeze(text);
221         }
222 }
223
224