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