2 * Copyright (C) 2008 - 2016 The Geeqie Team
4 * Authors: Vladimir Nadvornik, Laurent Monin
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "logwindow.h"
27 #include <gdk/gdkkeysyms.h>
30 typedef struct _LogWindow LogWindow;
35 GtkWidget *scrolledwin;
38 GdkColor colors[LOG_COUNT];
43 typedef struct _LogDef LogDef;
51 /* Keep LogType order !! */
52 static LogDef logdefs[LOG_COUNT] = {
53 { LOG_NORMAL, "normal", "black" },
54 { LOG_MSG, "message", "blue" },
55 { LOG_WARN, "warning", "orange" },
56 { LOG_ERROR, "error", "red" },
59 static LogWindow *logwindow = NULL;
61 static void hide_cb(GtkWidget *widget, LogWindow *logwin)
65 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event,
68 if (event && event->keyval == GDK_KEY_Escape)
69 gtk_widget_hide(logwin->window);
73 static LogWindow *log_window_create(void)
77 GtkWidget *scrolledwin;
79 GtkTextBuffer *buffer;
82 logwin = g_new0(LogWindow, 1);
84 window = window_new(GTK_WINDOW_TOPLEVEL, "log", NULL, NULL, _("Log"));
85 gtk_widget_set_size_request(window, 520, 400);
86 g_signal_connect(G_OBJECT(window), "delete_event",
87 G_CALLBACK(gtk_widget_hide_on_delete), NULL);
88 g_signal_connect(G_OBJECT(window), "key_press_event",
89 G_CALLBACK(key_pressed), logwin);
90 g_signal_connect(G_OBJECT(window), "hide",
91 G_CALLBACK(hide_cb), logwin);
92 gtk_widget_realize(window);
94 scrolledwin = gtk_scrolled_window_new(NULL, NULL);
95 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
96 GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
97 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwin),
99 gtk_container_add(GTK_CONTAINER(window), scrolledwin);
100 gtk_widget_show(scrolledwin);
102 text = gtk_text_view_new();
103 gtk_text_view_set_editable(GTK_TEXT_VIEW(text), FALSE);
104 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text), GTK_WRAP_WORD);
105 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
106 gtk_text_buffer_get_start_iter(buffer, &iter);
107 gtk_text_buffer_create_mark(buffer, "end", &iter, FALSE);
108 gtk_container_add(GTK_CONTAINER(scrolledwin), text);
109 gtk_widget_show(text);
111 logwin->window = window;
112 logwin->scrolledwin = scrolledwin;
119 static void log_window_init(LogWindow *logwin)
121 GtkTextBuffer *buffer;
122 #if !GTK_CHECK_VERSION(3,0,0)
123 GdkColormap *colormap;
124 gboolean success[LOG_COUNT];
128 g_assert(logwin != NULL);
129 g_assert(logwin->colors != NULL);
130 #if !GTK_CHECK_VERSION(3,0,0)
131 for (i = LOG_NORMAL; i < LOG_COUNT; i++)
133 gboolean ok = gdk_color_parse(logdefs[i].color, &logwin->colors[i]);
134 if (ok == TRUE) continue;
136 if (i == LOG_NORMAL) return;
137 memcpy(&logwin->colors[i], &logwin->colors[LOG_NORMAL], sizeof(GdkColor));
140 colormap = gdk_drawable_get_colormap(gtk_widget_get_window(logwin->window));
141 gdk_colormap_alloc_colors(colormap, logwin->colors, LOG_COUNT, FALSE, TRUE, success);
143 for (i = LOG_NORMAL; i < LOG_COUNT; i++)
145 if (success[i] == FALSE)
150 g_warning("LogWindow: color allocation failed\n");
151 style = gtk_widget_get_style(logwin->window);
152 for (j = LOG_NORMAL; j < LOG_COUNT; j++)
153 logwin->colors[j] = style->black;
158 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(logwin->text));
159 for (i = LOG_NORMAL; i < LOG_COUNT; i++)
160 gtk_text_buffer_create_tag(buffer, logdefs[i].tag,
161 "foreground-gdk", &logwin->colors[i],
162 "family", "MonoSpace",
166 static void log_window_show(LogWindow *logwin)
168 GtkTextView *text = GTK_TEXT_VIEW(logwin->text);
169 GtkTextBuffer *buffer;
172 g_assert(logwin != NULL);
174 buffer = gtk_text_view_get_buffer(text);
175 mark = gtk_text_buffer_get_mark(buffer, "end");
176 gtk_text_view_scroll_mark_onscreen(text, mark);
178 gtk_window_present(GTK_WINDOW(logwin->window));
180 log_window_append("", LOG_NORMAL); // to flush memorized lines
183 void log_window_new(void)
185 if (logwindow == NULL)
189 logwin = log_window_create();
190 log_window_init(logwin);
194 log_window_show(logwindow);
197 typedef struct _LogMsg LogMsg;
205 static void log_window_insert_text(GtkTextBuffer *buffer, GtkTextIter *iter,
206 const gchar *text, const gchar *tag)
210 if (!text || !*text) return;
212 str_utf8 = utf8_validate_or_convert(text);
213 gtk_text_buffer_insert_with_tags_by_name(buffer, iter, str_utf8, -1, tag, NULL);
218 void log_window_append(const gchar *str, LogType type)
221 GtkTextBuffer *buffer;
223 static GList *memory = NULL;
225 if (logwindow == NULL)
228 LogMsg *msg = g_new(LogMsg, 1);
230 msg->text = g_strdup(str);
233 memory = g_list_prepend(memory, msg);
235 while (g_list_length(memory) >= options->log_window_lines)
237 GList *work = g_list_last(memory);
238 LogMsg *oldest_msg = work->data;
240 g_free(oldest_msg->text);
241 memory = g_list_delete_link(memory, work);
247 text = GTK_TEXT_VIEW(logwindow->text);
248 buffer = gtk_text_view_get_buffer(text);
250 if (options->log_window_lines > 0 && logwindow->lines >= options->log_window_lines)
252 GtkTextIter start, end;
254 gtk_text_buffer_get_start_iter(buffer, &start);
256 gtk_text_iter_forward_lines(&end, logwindow->lines - options->log_window_lines);
257 gtk_text_buffer_delete(buffer, &start, &end);
260 gtk_text_buffer_get_end_iter(buffer, &iter);
263 GList *work = g_list_last(memory);
268 LogMsg *oldest_msg = work->data;
270 log_window_insert_text(buffer, &iter, oldest_msg->text, logdefs[oldest_msg->type].tag);
273 memory = g_list_delete_link(memory, work);
278 log_window_insert_text(buffer, &iter, str, logdefs[type].tag);
280 if (gtk_widget_get_visible(GTK_WIDGET(text)))
284 mark = gtk_text_buffer_get_mark(buffer, "end");
285 gtk_text_view_scroll_mark_onscreen(text, mark);
288 logwindow->lines = gtk_text_buffer_get_line_count(buffer);
290 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */