Statistics
| Revision:

root / src / logwindow.c @ 1

History | View | Annotate | Download (5.78 KB)

1
/*
2
 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3
 * Copyright (C) 1999-2003 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

    
38
#define MAX_LINES        500
39
#define TRIM_LINES        25
40

    
41
static LogWindow *logwindow;
42

    
43
static void hide_cb                (GtkWidget        *widget,
44
                                 LogWindow        *logwin);
45
static gboolean key_pressed        (GtkWidget        *widget,
46
                                 GdkEventKey        *event,
47
                                 LogWindow        *logwin);
48

    
49
LogWindow *log_window_create(void)
50
{
51
        LogWindow *logwin;
52
        GtkWidget *window;
53
        GtkWidget *scrolledwin;
54
        GtkWidget *text;
55
        GtkTextBuffer *buffer;
56
        GtkTextIter iter;
57

    
58
        debug_print("Creating log window...\n");
59
        logwin = g_new0(LogWindow, 1);
60

    
61
        window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
62
        gtk_window_set_title(GTK_WINDOW(window), _("Protocol log"));
63
        gtk_window_set_wmclass(GTK_WINDOW(window), "log_window", "Sylpheed");
64
        gtk_window_set_policy(GTK_WINDOW(window), TRUE, TRUE, FALSE);
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_ALWAYS);
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->lines = 0;
93

    
94
        logwindow = logwin;
95

    
96
        return logwin;
97
}
98

    
99
void log_window_init(LogWindow *logwin)
100
{
101
        GtkTextBuffer *buffer;
102
        GdkColormap *colormap;
103
        GdkColor color[3] =
104
                {{0, 0, 0xafff, 0}, {0, 0xefff, 0, 0}, {0, 0xefff, 0, 0}};
105
        gboolean success[3];
106
        gint i;
107

    
108
        //gtkut_widget_disable_theme_engine(logwin->text);
109

    
110
        logwin->msg_color   = color[0];
111
        logwin->warn_color  = color[1];
112
        logwin->error_color = color[2];
113

    
114
        colormap = gdk_window_get_colormap(logwin->window->window);
115
        gdk_colormap_alloc_colors(colormap, color, 3, FALSE, TRUE, success);
116

    
117
        for (i = 0; i < 3; i++) {
118
                if (success[i] == FALSE) {
119
                        GtkStyle *style;
120

    
121
                        g_warning("LogWindow: color allocation failed\n");
122
                        style = gtk_widget_get_style(logwin->window);
123
                        logwin->msg_color = logwin->warn_color =
124
                        logwin->error_color = style->black;
125
                        break;
126
                }
127
        }
128

    
129
        buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(logwin->text));
130
        gtk_text_buffer_create_tag(buffer, "message",
131
                                   "foreground-gdk", &logwindow->msg_color,
132
                                   NULL);
133
        gtk_text_buffer_create_tag(buffer, "warn",
134
                                   "foreground-gdk", &logwindow->warn_color,
135
                                   NULL);
136
        gtk_text_buffer_create_tag(buffer, "error",
137
                                   "foreground-gdk", &logwindow->error_color,
138
                                   NULL);
139
}
140

    
141
void log_window_show(LogWindow *logwin)
142
{
143
        GtkTextView *text = GTK_TEXT_VIEW(logwin->text);
144
        GtkTextBuffer *buffer;
145
        GtkTextMark *mark;
146

    
147
        buffer = gtk_text_view_get_buffer(text);
148

    
149
        gtk_widget_hide(logwin->window);
150

    
151
        mark = gtk_text_buffer_get_mark(buffer, "end");
152
        gtk_text_view_scroll_mark_onscreen(text, mark);
153

    
154
        gtk_widget_show(logwin->window);
155
}
156

    
157
void log_window_append(const gchar *str, LogType type)
158
{
159
        GtkTextView *text;
160
        GtkTextBuffer *buffer;
161
        GtkTextMark *mark;
162
        GtkTextIter iter;
163
        GdkColor *color = NULL;
164
        gchar *head = NULL;
165
        const gchar *tag;
166

    
167
        g_return_if_fail(logwindow != NULL);
168

    
169
        text = GTK_TEXT_VIEW(logwindow->text);
170
        buffer = gtk_text_view_get_buffer(text);
171
        gtk_text_buffer_get_iter_at_offset(buffer, &iter, -1);
172

    
173
        switch (type) {
174
        case LOG_MSG:
175
                color = &logwindow->msg_color;
176
                tag = "message";
177
                head = "* ";
178
                break;
179
        case LOG_WARN:
180
                color = &logwindow->warn_color;
181
                tag = "warn";
182
                head = "** ";
183
                break;
184
        case LOG_ERROR:
185
                color = &logwindow->error_color;
186
                tag = "error";
187
                head = "*** ";
188
                break;
189
        default:
190
                tag = NULL;
191
                break;
192
        }
193

    
194
        if (logwindow->lines == MAX_LINES) {
195
                //
196
        }
197

    
198
        if (head)
199
                gtk_text_buffer_insert_with_tags_by_name
200
                        (buffer, &iter, head, -1, tag, NULL);
201
        gtk_text_buffer_insert_with_tags_by_name
202
                (buffer, &iter, str, -1, tag, NULL);
203

    
204
        mark = gtk_text_buffer_get_mark(buffer, "end");
205
        if (GTK_WIDGET_VISIBLE(text))
206
                gtk_text_view_scroll_mark_onscreen(text, mark);
207

    
208
        logwindow->lines++;
209
}
210

    
211
static void hide_cb(GtkWidget *widget, LogWindow *logwin)
212
{
213
}
214

    
215
static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event,
216
                            LogWindow *logwin)
217
{
218
        if (event && event->keyval == GDK_Escape)
219
                gtk_widget_hide(logwin->window);
220
        return FALSE;
221
}