add undo/redo patch submitted by Jens Oberender
[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
38 static LogWindow *logwindow;
39
40 static void key_pressed(GtkWidget *widget, GdkEventKey *event,
41                         LogWindow *logwin);
42
43 LogWindow *log_window_create(void)
44 {
45         LogWindow *logwin;
46         GtkWidget *window;
47         GtkWidget *scrolledwin;
48         GtkWidget *text;
49
50         debug_print(_("Creating log window...\n"));
51         logwin = g_new0(LogWindow, 1);
52
53         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
54         gtk_window_set_title(GTK_WINDOW(window), _("Protocol log"));
55         gtk_window_set_policy(GTK_WINDOW(window), TRUE, TRUE, FALSE);
56         gtk_widget_set_usize(window, 520, 400);
57         gtk_signal_connect(GTK_OBJECT(window), "delete_event",
58                            GTK_SIGNAL_FUNC(gtk_widget_hide_on_delete), NULL);
59         gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
60                            GTK_SIGNAL_FUNC(key_pressed), logwin);
61         gtk_widget_realize(window);
62
63         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
64         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
65                                        GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
66         gtk_container_add(GTK_CONTAINER(window), scrolledwin);
67         gtk_widget_show(scrolledwin);
68
69         text = gtk_text_new(gtk_scrolled_window_get_hadjustment
70                             (GTK_SCROLLED_WINDOW(scrolledwin)),
71                             gtk_scrolled_window_get_vadjustment
72                             (GTK_SCROLLED_WINDOW(scrolledwin)));
73         gtk_container_add(GTK_CONTAINER(scrolledwin), text);
74         gtk_widget_show(text);
75
76         logwin->window = window;
77         logwin->scrolledwin = scrolledwin;
78         logwin->text = text;
79
80         logwindow = logwin;
81
82         return logwin;
83 }
84
85 void log_window_init(LogWindow *logwin)
86 {
87         GdkColormap *colormap;
88         GdkColor color[3] =
89                 {{0, 0, 0xafff, 0}, {0, 0xefff, 0, 0}, {0, 0xefff, 0, 0}};
90         gboolean success[3];
91         gint i;
92
93         gtkut_widget_disable_theme_engine(logwin->text);
94
95         logwin->msg_color   = color[0];
96         logwin->warn_color  = color[1];
97         logwin->error_color = color[2];
98
99         colormap = gdk_window_get_colormap(logwin->window->window);
100         gdk_colormap_alloc_colors(colormap, color, 3, FALSE, TRUE, success);
101
102         for (i = 0; i < 3; i++) {
103                 if (success[i] == FALSE) {
104                         GtkStyle *style;
105
106                         g_warning("LogWindow: color allocation failed\n");
107                         style = gtk_widget_get_style(logwin->window);
108                         logwin->msg_color = logwin->warn_color =
109                         logwin->error_color = style->black;
110                         break;
111                 }
112         }
113 }
114
115 void log_window_show(LogWindow *logwin)
116 {
117         gtk_widget_hide(logwin->window);
118         gtk_widget_show(logwin->window);
119 }
120
121 void log_window_append(const gchar *str, LogType type)
122 {
123         GtkText *text;
124         GdkColor *color = NULL;
125         gchar *head = NULL;
126
127         g_return_if_fail(logwindow != NULL);
128
129         text = GTK_TEXT(logwindow->text);
130
131         switch (type) {
132         case LOG_WARN:
133                 color = &logwindow->warn_color;
134                 head = "*** ";
135                 break;
136         case LOG_ERROR:
137                 color = &logwindow->error_color;
138                 head = "*** ";
139                 break;
140         case LOG_MSG:
141                 color = &logwindow->msg_color;
142                 break;
143         default:
144         }
145
146         if (head) gtk_text_insert(text, NULL, color, NULL, head, -1);
147         gtk_text_insert(text, NULL, color, NULL, str, -1);
148 }
149
150 static void key_pressed(GtkWidget *widget, GdkEventKey *event,
151                         LogWindow *logwin)
152 {
153         if (event && event->keyval == GDK_Escape)
154                 gtk_widget_hide(logwin->window);
155 }