root / src / logwindow.c @ 489
History | View | Annotate | Download (6.1 kB)
| 1 | 1 | hiro | /*
|
|---|---|---|---|
| 2 | 1 | hiro | * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client |
| 3 | 122 | hiro | * Copyright (C) 1999-2005 Hiroyuki Yamamoto |
| 4 | 1 | hiro | * |
| 5 | 1 | hiro | * This program is free software; you can redistribute it and/or modify |
| 6 | 1 | hiro | * it under the terms of the GNU General Public License as published by |
| 7 | 1 | hiro | * the Free Software Foundation; either version 2 of the License, or |
| 8 | 1 | hiro | * (at your option) any later version. |
| 9 | 1 | hiro | * |
| 10 | 1 | hiro | * This program is distributed in the hope that it will be useful, |
| 11 | 1 | hiro | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | 1 | hiro | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | 1 | hiro | * GNU General Public License for more details. |
| 14 | 1 | hiro | * |
| 15 | 1 | hiro | * You should have received a copy of the GNU General Public License |
| 16 | 1 | hiro | * along with this program; if not, write to the Free Software |
| 17 | 1 | hiro | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 18 | 1 | hiro | */ |
| 19 | 1 | hiro | |
| 20 | 1 | hiro | #ifdef HAVE_CONFIG_H
|
| 21 | 1 | hiro | # include "config.h" |
| 22 | 1 | hiro | #endif
|
| 23 | 1 | hiro | |
| 24 | 1 | hiro | #include <glib.h> |
| 25 | 92 | hiro | #include <glib/gi18n.h> |
| 26 | 1 | hiro | #include <gdk/gdkkeysyms.h> |
| 27 | 1 | hiro | #include <gtk/gtkwidget.h> |
| 28 | 1 | hiro | #include <gtk/gtkwindow.h> |
| 29 | 1 | hiro | #include <gtk/gtksignal.h> |
| 30 | 1 | hiro | #include <gtk/gtkscrolledwindow.h> |
| 31 | 1 | hiro | #include <gtk/gtktextview.h> |
| 32 | 1 | hiro | #include <gtk/gtkstyle.h> |
| 33 | 1 | hiro | |
| 34 | 1 | hiro | #include "logwindow.h" |
| 35 | 122 | hiro | #include "prefs_common.h" |
| 36 | 1 | hiro | #include "utils.h" |
| 37 | 1 | hiro | #include "gtkutils.h" |
| 38 | 1 | hiro | |
| 39 | 1 | hiro | #define TRIM_LINES 25 |
| 40 | 1 | hiro | |
| 41 | 1 | hiro | static LogWindow *logwindow;
|
| 42 | 1 | hiro | |
| 43 | 1 | hiro | static void hide_cb (GtkWidget *widget, |
| 44 | 1 | hiro | LogWindow *logwin); |
| 45 | 1 | hiro | static gboolean key_pressed (GtkWidget *widget,
|
| 46 | 1 | hiro | GdkEventKey *event, |
| 47 | 1 | hiro | LogWindow *logwin); |
| 48 | 1 | hiro | |
| 49 | 1 | hiro | LogWindow *log_window_create(void)
|
| 50 | 1 | hiro | {
|
| 51 | 1 | hiro | LogWindow *logwin; |
| 52 | 1 | hiro | GtkWidget *window; |
| 53 | 1 | hiro | GtkWidget *scrolledwin; |
| 54 | 1 | hiro | GtkWidget *text; |
| 55 | 1 | hiro | GtkTextBuffer *buffer; |
| 56 | 1 | hiro | GtkTextIter iter; |
| 57 | 1 | hiro | |
| 58 | 1 | hiro | debug_print("Creating log window...\n");
|
| 59 | 1 | hiro | logwin = g_new0(LogWindow, 1);
|
| 60 | 1 | hiro | |
| 61 | 1 | hiro | window = gtk_window_new(GTK_WINDOW_TOPLEVEL); |
| 62 | 1 | hiro | gtk_window_set_title(GTK_WINDOW(window), _("Protocol log"));
|
| 63 | 1 | hiro | gtk_window_set_wmclass(GTK_WINDOW(window), "log_window", "Sylpheed"); |
| 64 | 1 | hiro | gtk_window_set_policy(GTK_WINDOW(window), TRUE, TRUE, FALSE); |
| 65 | 1 | hiro | gtk_widget_set_size_request(window, 520, 400); |
| 66 | 1 | hiro | g_signal_connect(G_OBJECT(window), "delete_event",
|
| 67 | 1 | hiro | G_CALLBACK(gtk_widget_hide_on_delete), NULL);
|
| 68 | 1 | hiro | g_signal_connect(G_OBJECT(window), "key_press_event",
|
| 69 | 1 | hiro | G_CALLBACK(key_pressed), logwin); |
| 70 | 1 | hiro | g_signal_connect(G_OBJECT(window), "hide",
|
| 71 | 1 | hiro | G_CALLBACK(hide_cb), logwin); |
| 72 | 1 | hiro | gtk_widget_realize(window); |
| 73 | 1 | hiro | |
| 74 | 1 | hiro | scrolledwin = gtk_scrolled_window_new(NULL, NULL); |
| 75 | 1 | hiro | gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin), |
| 76 | 1 | hiro | GTK_POLICY_NEVER, GTK_POLICY_ALWAYS); |
| 77 | 4 | hiro | gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwin), |
| 78 | 4 | hiro | GTK_SHADOW_IN); |
| 79 | 1 | hiro | gtk_container_add(GTK_CONTAINER(window), scrolledwin); |
| 80 | 1 | hiro | gtk_widget_show(scrolledwin); |
| 81 | 1 | hiro | |
| 82 | 1 | hiro | text = gtk_text_view_new(); |
| 83 | 1 | hiro | gtk_text_view_set_editable(GTK_TEXT_VIEW(text), FALSE); |
| 84 | 1 | hiro | gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text), GTK_WRAP_WORD); |
| 85 | 1 | hiro | buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text)); |
| 86 | 1 | hiro | gtk_text_buffer_get_start_iter(buffer, &iter); |
| 87 | 1 | hiro | gtk_text_buffer_create_mark(buffer, "end", &iter, FALSE);
|
| 88 | 1 | hiro | gtk_container_add(GTK_CONTAINER(scrolledwin), text); |
| 89 | 1 | hiro | gtk_widget_show(text); |
| 90 | 1 | hiro | |
| 91 | 1 | hiro | logwin->window = window; |
| 92 | 1 | hiro | logwin->scrolledwin = scrolledwin; |
| 93 | 1 | hiro | logwin->text = text; |
| 94 | 122 | hiro | logwin->lines = 1;
|
| 95 | 1 | hiro | |
| 96 | 1 | hiro | logwindow = logwin; |
| 97 | 1 | hiro | |
| 98 | 1 | hiro | return logwin;
|
| 99 | 1 | hiro | } |
| 100 | 1 | hiro | |
| 101 | 1 | hiro | void log_window_init(LogWindow *logwin)
|
| 102 | 1 | hiro | {
|
| 103 | 1 | hiro | GtkTextBuffer *buffer; |
| 104 | 1 | hiro | GdkColormap *colormap; |
| 105 | 1 | hiro | GdkColor color[3] =
|
| 106 | 1 | hiro | {{0, 0, 0xafff, 0}, {0, 0xefff, 0, 0}, {0, 0xefff, 0, 0}};
|
| 107 | 1 | hiro | gboolean success[3];
|
| 108 | 1 | hiro | gint i; |
| 109 | 1 | hiro | |
| 110 | 1 | hiro | logwin->msg_color = color[0];
|
| 111 | 1 | hiro | logwin->warn_color = color[1];
|
| 112 | 1 | hiro | logwin->error_color = color[2];
|
| 113 | 1 | hiro | |
| 114 | 1 | hiro | colormap = gdk_window_get_colormap(logwin->window->window); |
| 115 | 1 | hiro | gdk_colormap_alloc_colors(colormap, color, 3, FALSE, TRUE, success);
|
| 116 | 1 | hiro | |
| 117 | 1 | hiro | for (i = 0; i < 3; i++) { |
| 118 | 1 | hiro | if (success[i] == FALSE) {
|
| 119 | 1 | hiro | GtkStyle *style; |
| 120 | 1 | hiro | |
| 121 | 1 | hiro | g_warning("LogWindow: color allocation failed\n");
|
| 122 | 1 | hiro | style = gtk_widget_get_style(logwin->window); |
| 123 | 1 | hiro | logwin->msg_color = logwin->warn_color = |
| 124 | 1 | hiro | logwin->error_color = style->black; |
| 125 | 1 | hiro | break;
|
| 126 | 1 | hiro | } |
| 127 | 1 | hiro | } |
| 128 | 1 | hiro | |
| 129 | 1 | hiro | buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(logwin->text)); |
| 130 | 1 | hiro | gtk_text_buffer_create_tag(buffer, "message",
|
| 131 | 1 | hiro | "foreground-gdk", &logwindow->msg_color,
|
| 132 | 1 | hiro | NULL);
|
| 133 | 1 | hiro | gtk_text_buffer_create_tag(buffer, "warn",
|
| 134 | 1 | hiro | "foreground-gdk", &logwindow->warn_color,
|
| 135 | 1 | hiro | NULL);
|
| 136 | 1 | hiro | gtk_text_buffer_create_tag(buffer, "error",
|
| 137 | 1 | hiro | "foreground-gdk", &logwindow->error_color,
|
| 138 | 1 | hiro | NULL);
|
| 139 | 1 | hiro | } |
| 140 | 1 | hiro | |
| 141 | 1 | hiro | void log_window_show(LogWindow *logwin)
|
| 142 | 1 | hiro | {
|
| 143 | 1 | hiro | GtkTextView *text = GTK_TEXT_VIEW(logwin->text); |
| 144 | 1 | hiro | GtkTextBuffer *buffer; |
| 145 | 1 | hiro | GtkTextMark *mark; |
| 146 | 1 | hiro | |
| 147 | 1 | hiro | buffer = gtk_text_view_get_buffer(text); |
| 148 | 1 | hiro | mark = gtk_text_buffer_get_mark(buffer, "end");
|
| 149 | 1 | hiro | gtk_text_view_scroll_mark_onscreen(text, mark); |
| 150 | 1 | hiro | |
| 151 | 122 | hiro | gtk_window_present(GTK_WINDOW(logwin->window)); |
| 152 | 1 | hiro | } |
| 153 | 1 | hiro | |
| 154 | 1 | hiro | void log_window_append(const gchar *str, LogType type) |
| 155 | 1 | hiro | {
|
| 156 | 1 | hiro | GtkTextView *text; |
| 157 | 1 | hiro | GtkTextBuffer *buffer; |
| 158 | 1 | hiro | GtkTextIter iter; |
| 159 | 1 | hiro | GdkColor *color = NULL;
|
| 160 | 1 | hiro | gchar *head = NULL;
|
| 161 | 1 | hiro | const gchar *tag;
|
| 162 | 122 | hiro | gint line_limit = prefs_common.logwin_line_limit; |
| 163 | 1 | hiro | |
| 164 | 1 | hiro | g_return_if_fail(logwindow != NULL);
|
| 165 | 1 | hiro | |
| 166 | 1 | hiro | text = GTK_TEXT_VIEW(logwindow->text); |
| 167 | 1 | hiro | buffer = gtk_text_view_get_buffer(text); |
| 168 | 1 | hiro | |
| 169 | 122 | hiro | if (line_limit > 0 && logwindow->lines >= line_limit) { |
| 170 | 122 | hiro | GtkTextIter start, end; |
| 171 | 122 | hiro | |
| 172 | 122 | hiro | gtk_text_buffer_get_start_iter(buffer, &start); |
| 173 | 122 | hiro | end = start; |
| 174 | 122 | hiro | gtk_text_iter_forward_lines(&end, TRIM_LINES); |
| 175 | 122 | hiro | gtk_text_buffer_delete(buffer, &start, &end); |
| 176 | 122 | hiro | logwindow->lines = gtk_text_buffer_get_line_count(buffer); |
| 177 | 122 | hiro | } |
| 178 | 122 | hiro | |
| 179 | 1 | hiro | switch (type) {
|
| 180 | 1 | hiro | case LOG_MSG:
|
| 181 | 1 | hiro | color = &logwindow->msg_color; |
| 182 | 1 | hiro | tag = "message";
|
| 183 | 1 | hiro | head = "* ";
|
| 184 | 1 | hiro | break;
|
| 185 | 1 | hiro | case LOG_WARN:
|
| 186 | 1 | hiro | color = &logwindow->warn_color; |
| 187 | 1 | hiro | tag = "warn";
|
| 188 | 1 | hiro | head = "** ";
|
| 189 | 1 | hiro | break;
|
| 190 | 1 | hiro | case LOG_ERROR:
|
| 191 | 1 | hiro | color = &logwindow->error_color; |
| 192 | 1 | hiro | tag = "error";
|
| 193 | 1 | hiro | head = "*** ";
|
| 194 | 1 | hiro | break;
|
| 195 | 1 | hiro | default:
|
| 196 | 1 | hiro | tag = NULL;
|
| 197 | 1 | hiro | break;
|
| 198 | 1 | hiro | } |
| 199 | 1 | hiro | |
| 200 | 122 | hiro | gtk_text_buffer_get_end_iter(buffer, &iter); |
| 201 | 1 | hiro | |
| 202 | 1 | hiro | if (head)
|
| 203 | 1 | hiro | gtk_text_buffer_insert_with_tags_by_name |
| 204 | 1 | hiro | (buffer, &iter, head, -1, tag, NULL); |
| 205 | 1 | hiro | gtk_text_buffer_insert_with_tags_by_name |
| 206 | 1 | hiro | (buffer, &iter, str, -1, tag, NULL); |
| 207 | 1 | hiro | |
| 208 | 122 | hiro | if (GTK_WIDGET_VISIBLE(text)) {
|
| 209 | 122 | hiro | GtkTextMark *mark; |
| 210 | 122 | hiro | mark = gtk_text_buffer_get_mark(buffer, "end");
|
| 211 | 1 | hiro | gtk_text_view_scroll_mark_onscreen(text, mark); |
| 212 | 122 | hiro | } |
| 213 | 1 | hiro | |
| 214 | 1 | hiro | logwindow->lines++; |
| 215 | 1 | hiro | } |
| 216 | 1 | hiro | |
| 217 | 1 | hiro | static void hide_cb(GtkWidget *widget, LogWindow *logwin) |
| 218 | 1 | hiro | {
|
| 219 | 1 | hiro | } |
| 220 | 1 | hiro | |
| 221 | 1 | hiro | static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event,
|
| 222 | 1 | hiro | LogWindow *logwin) |
| 223 | 1 | hiro | {
|
| 224 | 1 | hiro | if (event && event->keyval == GDK_Escape)
|
| 225 | 1 | hiro | gtk_widget_hide(logwin->window); |
| 226 | 1 | hiro | return FALSE;
|
| 227 | 1 | hiro | } |