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