* src/compose.c
[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         if (prefs_common.cliplog && !prefs_common.loglength)
145                 return;
146
147         text = GTK_TEXT(logwindow->text);
148
149         switch (type) {
150         case LOG_MSG:
151                 color = &logwindow->msg_color;
152                 head = "* ";
153                 break;
154         case LOG_WARN:
155                 color = &logwindow->warn_color;
156                 head = "** ";
157                 break;
158         case LOG_ERROR:
159                 color = &logwindow->error_color;
160                 head = "*** ";
161                 break;
162         default:
163                 break;
164         }
165
166         if (head) gtk_text_insert(text, NULL, color, NULL, head, -1);
167         gtk_text_insert(text, NULL, color, NULL, str, -1);
168         if (prefs_common.cliplog)
169                log_window_clear (GTK_WIDGET (text));
170 }
171
172 static void hide_cb(GtkWidget *widget, LogWindow *logwin)
173 {
174         if (GTK_TEXT(logwin->text)->freeze_count == 0)
175                 gtk_text_freeze(GTK_TEXT(logwin->text));
176 }
177
178 static void key_pressed(GtkWidget *widget, GdkEventKey *event,
179                         LogWindow *logwin)
180 {
181         if (event && event->keyval == GDK_Escape)
182                 gtk_widget_hide(logwin->window);
183 }
184
185 void log_window_clear(GtkWidget *textw)
186 {
187         guint length;
188         guint point;
189         gboolean was_frozen = FALSE;
190         GtkText *text = GTK_TEXT(textw);
191         
192         length = gtk_text_get_length (text);
193         debug_print("Log window length: %u\n", length);
194         
195         if (length > prefs_common.loglength) {
196                 gchar *lf;
197                 /* find the end of the first line after the cut off
198                  * point */
199                 point = length - prefs_common.loglength;
200                 while (point < length && GTK_TEXT_INDEX(text, point) != '\n')
201                         point++;
202                 /* erase the text */
203                 if (text->freeze_count) {
204                         was_frozen = TRUE;
205                         gtk_text_thaw(text);
206                 }
207                 gtk_text_set_point (text, 0);
208                 gtk_text_freeze(text);
209                 if (!gtk_text_forward_delete (text, point + 1))
210                         debug_print("Error clearing log\n");
211                 gtk_text_thaw(text);
212                 gtk_text_set_point(text,
213                                    gtk_text_get_length (GTK_TEXT (text)));
214                 if (was_frozen)
215                         gtk_text_freeze(text);
216         }
217 }
218
219