inital gtk2 patch
[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/gtktextview.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 gboolean key_pressed             (GtkWidget      *widget,
44                                          GdkEventKey    *event,
45                                          LogWindow      *logwin);
46 static gboolean 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         GtkTextBuffer *buffer;
57         GtkTextIter iter;
58
59         debug_print("Creating log window...\n");
60         logwin = g_new0(LogWindow, 1);
61
62         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
63         gtk_window_set_title(GTK_WINDOW(window), _("Protocol log"));
64         gtk_window_set_wmclass(GTK_WINDOW(window), "log_window", "Sylpheed");
65         gtk_window_set_policy(GTK_WINDOW(window), TRUE, TRUE, FALSE);
66         gtk_widget_set_size_request(window, 520, 400);
67         g_signal_connect(G_OBJECT(window), "delete_event",
68                          G_CALLBACK(gtk_widget_hide_on_delete), NULL);
69         g_signal_connect(G_OBJECT(window), "key_press_event",
70                          G_CALLBACK(key_pressed), logwin);
71         g_signal_connect(G_OBJECT(window), "hide",
72                          G_CALLBACK(hide_cb), logwin);
73         gtk_widget_realize(window);
74
75         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
76         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
77                                        GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
78         gtk_container_add(GTK_CONTAINER(window), scrolledwin);
79         gtk_widget_show(scrolledwin);
80
81         text = gtk_text_view_new();
82         gtk_text_view_set_editable(GTK_TEXT_VIEW(text), FALSE);
83         gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text), GTK_WRAP_WORD);
84         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
85         gtk_text_buffer_get_start_iter(buffer, &iter);
86         gtk_text_buffer_create_mark(buffer, "end", &iter, FALSE);
87         gtk_container_add(GTK_CONTAINER(scrolledwin), text);
88         gtk_widget_show(text);
89
90         logwin->window = window;
91         logwin->scrolledwin = scrolledwin;
92         logwin->text = text;
93         logwin->hook_id = hooks_register_hook(LOG_APPEND_TEXT_HOOKLIST, log_window_append, logwin);
94
95         return logwin;
96 }
97
98 void log_window_init(LogWindow *logwin)
99 {
100         GtkTextBuffer *buffer;
101         GdkColormap *colormap;
102         GdkColor color[3] =
103                 {{0, 0, 0xafff, 0}, {0, 0xefff, 0, 0}, {0, 0xefff, 0, 0}};
104         gboolean success[3];
105         gint i;
106
107         gtkut_widget_disable_theme_engine(logwin->text);
108
109         logwin->msg_color   = color[0];
110         logwin->warn_color  = color[1];
111         logwin->error_color = color[2];
112
113         colormap = gdk_window_get_colormap(logwin->window->window);
114         gdk_colormap_alloc_colors(colormap, color, 3, FALSE, TRUE, success);
115
116         for (i = 0; i < 3; i++) {
117                 if (success[i] == FALSE) {
118                         GtkStyle *style;
119
120                         g_warning("LogWindow: color allocation failed\n");
121                         style = gtk_widget_get_style(logwin->window);
122                         logwin->msg_color = logwin->warn_color =
123                         logwin->error_color = style->black;
124                         break;
125                 }
126         }
127
128         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(logwin->text));
129         gtk_text_buffer_create_tag(buffer, "message",
130                                    "foreground-gdk", &logwin->msg_color,
131                                    NULL);
132         gtk_text_buffer_create_tag(buffer, "warn",
133                                    "foreground-gdk", &logwin->warn_color,
134                                    NULL);
135         gtk_text_buffer_create_tag(buffer, "error",
136                                    "foreground-gdk", &logwin->error_color,
137                                    NULL);
138 }
139
140 void log_window_show(LogWindow *logwin)
141 {
142         GtkTextView *text = GTK_TEXT_VIEW(logwin->text);
143         GtkTextBuffer *buffer = gtk_text_view_get_buffer(text);
144         GtkTextMark *mark;
145
146         mark = gtk_text_buffer_get_mark(buffer, "end");
147         gtk_text_view_scroll_mark_onscreen(text, mark);
148
149         gtk_widget_show(logwin->window);
150 }
151
152 static gboolean log_window_append(gpointer source, gpointer data)
153 {
154         LogText *logtext = (LogText *) source;
155         LogWindow *logwindow = (LogWindow *) data;
156         GtkTextView *text;
157         GtkTextBuffer *buffer;
158         GtkTextIter iter;
159         GdkColor *color = NULL;
160         gchar *head = NULL;
161         const gchar *tag;
162
163         g_return_val_if_fail(logtext != NULL, TRUE);
164         g_return_val_if_fail(logtext->text != NULL, TRUE);
165         g_return_val_if_fail(logwindow != NULL, FALSE);
166
167         if (prefs_common.cliplog && !prefs_common.loglength)
168                 return FALSE;
169
170         text = GTK_TEXT_VIEW(logwindow->text);
171         buffer = gtk_text_view_get_buffer(text);
172         gtk_text_buffer_get_iter_at_offset(buffer, &iter, -1);
173
174         switch (logtext->type) {
175         case LOG_MSG:
176                 color = &logwindow->msg_color;
177                 tag = "message";
178                 head = "* ";
179                 break;
180         case LOG_WARN:
181                 color = &logwindow->warn_color;
182                 tag = "warn";
183                 head = "** ";
184                 break;
185         case LOG_ERROR:
186                 color = &logwindow->error_color;
187                 tag = "error";
188                 head = "*** ";
189                 break;
190         default:
191                 tag = NULL;
192                 break;
193         }
194   
195         if (head)
196                 gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, head, -1,
197                                                          tag, NULL);
198         gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, logtext->text, -1,
199                                                  tag, NULL);
200
201         gtk_text_buffer_get_start_iter(buffer, &iter);
202         gtk_text_buffer_place_cursor(buffer, &iter);
203
204         if (prefs_common.cliplog)
205                log_window_clear (GTK_WIDGET (text));
206
207         return FALSE;
208 }
209
210 static void hide_cb(GtkWidget *widget, LogWindow *logwin)
211 {
212 }
213
214 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event,
215                         LogWindow *logwin)
216 {
217         if (event && event->keyval == GDK_Escape)
218                 gtk_widget_hide(logwin->window);
219         return FALSE;
220 }
221
222 void log_window_clear(GtkWidget *textw)
223 {
224         guint length;
225         guint point;
226         GtkTextView *textview = GTK_TEXT_VIEW(textw);
227         GtkTextBuffer *textbuf = gtk_text_view_get_buffer(textview);
228         GtkTextIter start_iter, end_iter;
229         
230         length = gtk_text_buffer_get_char_count (textbuf);
231         debug_print("Log window length: %u\n", length);
232         
233         if (length > prefs_common.loglength) {
234                 /* find the end of the first line after the cut off
235                  * point */
236                 point = length - prefs_common.loglength;
237                 gtk_text_buffer_get_iter_at_offset(textbuf, &end_iter, point);
238                 if (!gtk_text_iter_forward_to_line_end(&end_iter))
239                         return;
240                 gtk_text_buffer_get_start_iter(textbuf, &start_iter);
241                 gtk_text_buffer_delete(textbuf, &start_iter, &end_iter);
242         }
243 }
244
245