2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2002 Hiroyuki Yamamoto
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.
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.
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.
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>
34 #include "logwindow.h"
37 #include "prefs_common.h"
41 static void hide_cb (GtkWidget
*widget
,
43 static void key_pressed (GtkWidget
*widget
,
46 static gboolean
log_window_append (gpointer source
,
48 void log_window_clear (GtkWidget
*text
);
50 LogWindow
*log_window_create(void)
54 GtkWidget
*scrolledwin
;
57 debug_print("Creating log window...\n");
58 logwin
= g_new0(LogWindow
, 1);
60 window
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
61 gtk_window_set_title(GTK_WINDOW(window
), _("Protocol log"));
62 gtk_window_set_wmclass(GTK_WINDOW(window
), "log_window", "Sylpheed");
63 gtk_window_set_policy(GTK_WINDOW(window
), TRUE
, TRUE
, FALSE
);
64 gtk_widget_set_usize(window
, 520, 400);
65 gtk_signal_connect(GTK_OBJECT(window
), "delete_event",
66 GTK_SIGNAL_FUNC(gtk_widget_hide_on_delete
), NULL
);
67 gtk_signal_connect(GTK_OBJECT(window
), "key_press_event",
68 GTK_SIGNAL_FUNC(key_pressed
), logwin
);
69 gtk_signal_connect(GTK_OBJECT(window
), "hide",
70 GTK_SIGNAL_FUNC(hide_cb
), logwin
);
71 gtk_widget_realize(window
);
73 scrolledwin
= gtk_scrolled_window_new(NULL
, NULL
);
74 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin
),
75 GTK_POLICY_NEVER
, GTK_POLICY_ALWAYS
);
76 gtk_container_add(GTK_CONTAINER(window
), scrolledwin
);
77 gtk_widget_show(scrolledwin
);
79 text
= gtk_text_new(gtk_scrolled_window_get_hadjustment
80 (GTK_SCROLLED_WINDOW(scrolledwin
)),
81 gtk_scrolled_window_get_vadjustment
82 (GTK_SCROLLED_WINDOW(scrolledwin
)));
83 gtk_container_add(GTK_CONTAINER(scrolledwin
), text
);
84 gtk_widget_show(text
);
85 gtk_text_freeze(GTK_TEXT(text
));
87 logwin
->window
= window
;
88 logwin
->scrolledwin
= scrolledwin
;
90 logwin
->hook_id
= hooks_register_hook(LOG_APPEND_TEXT_HOOKLIST
, log_window_append
, logwin
);
95 void log_window_init(LogWindow
*logwin
)
97 GdkColormap
*colormap
;
99 {{0, 0, 0xafff, 0}, {0, 0xefff, 0, 0}, {0, 0xefff, 0, 0}};
103 gtkut_widget_disable_theme_engine(logwin
->text
);
105 logwin
->msg_color
= color
[0];
106 logwin
->warn_color
= color
[1];
107 logwin
->error_color
= color
[2];
109 colormap
= gdk_window_get_colormap(logwin
->window
->window
);
110 gdk_colormap_alloc_colors(colormap
, color
, 3, FALSE
, TRUE
, success
);
112 for (i
= 0; i
< 3; i
++) {
113 if (success
[i
] == FALSE
) {
116 g_warning("LogWindow: color allocation failed\n");
117 style
= gtk_widget_get_style(logwin
->window
);
118 logwin
->msg_color
= logwin
->warn_color
=
119 logwin
->error_color
= style
->black
;
125 void log_window_show(LogWindow
*logwin
)
127 GtkText
*text
= GTK_TEXT(logwin
->text
);
129 gtk_widget_hide(logwin
->window
);
132 text
->vadj
->value
= text
->vadj
->upper
- text
->vadj
->page_size
;
133 gtk_signal_emit_by_name(GTK_OBJECT(text
->vadj
), "value_changed");
135 gtk_widget_show(logwin
->window
);
138 static gboolean
log_window_append(gpointer source
, gpointer data
)
140 LogText
*logtext
= (LogText
*) source
;
141 LogWindow
*logwindow
= (LogWindow
*) data
;
143 GdkColor
*color
= NULL
;
146 g_return_val_if_fail(logtext
!= NULL
, TRUE
);
147 g_return_val_if_fail(logtext
->text
!= NULL
, TRUE
);
148 g_return_val_if_fail(logwindow
!= NULL
, FALSE
);
150 if (prefs_common
.cliplog
&& !prefs_common
.loglength
)
153 text
= GTK_TEXT(logwindow
->text
);
155 switch (logtext
->type
) {
157 color
= &logwindow
->msg_color
;
161 color
= &logwindow
->warn_color
;
165 color
= &logwindow
->error_color
;
172 if (head
) gtk_text_insert(text
, NULL
, color
, NULL
, head
, -1);
173 gtk_text_insert(text
, NULL
, color
, NULL
, logtext
->text
, -1);
174 if (prefs_common
.cliplog
)
175 log_window_clear (GTK_WIDGET (text
));
180 static void hide_cb(GtkWidget
*widget
, LogWindow
*logwin
)
182 if (GTK_TEXT(logwin
->text
)->freeze_count
== 0)
183 gtk_text_freeze(GTK_TEXT(logwin
->text
));
186 static void key_pressed(GtkWidget
*widget
, GdkEventKey
*event
,
189 if (event
&& event
->keyval
== GDK_Escape
)
190 gtk_widget_hide(logwin
->window
);
193 void log_window_clear(GtkWidget
*textw
)
197 gboolean was_frozen
= FALSE
;
198 GtkText
*text
= GTK_TEXT(textw
);
200 length
= gtk_text_get_length (text
);
201 debug_print("Log window length: %u\n", length
);
203 if (length
> prefs_common
.loglength
) {
204 /* find the end of the first line after the cut off
206 point
= length
- prefs_common
.loglength
;
207 while (point
< length
&& GTK_TEXT_INDEX(text
, point
) != '\n')
210 if (text
->freeze_count
) {
214 gtk_text_set_point (text
, 0);
215 gtk_text_freeze(text
);
216 if (!gtk_text_forward_delete (text
, point
+ 1))
217 debug_print("Error clearing log\n");
219 gtk_text_set_point(text
,
220 gtk_text_get_length (GTK_TEXT (text
)));
222 gtk_text_freeze(text
);