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