Additional debug features
[geeqie.git] / src / logwindow.c
1 /*
2  * Copyright (C) 2008 - 2016 The Geeqie Team
3  *
4  * Authors: Vladimir Nadvornik, Laurent Monin
5  *
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.
10  *
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.
15  *
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.
19  */
20
21 #include "main.h"
22 #include "logwindow.h"
23
24 #include "misc.h"
25 #include "secure_save.h"
26 #include "ui_misc.h"
27 #include "window.h"
28
29 #include <gdk/gdkkeysyms.h>
30
31
32 typedef struct _LogWindow LogWindow;
33
34 struct _LogWindow
35 {
36         GtkWidget *window;
37         GtkWidget *scrolledwin;
38         GtkWidget *text;
39
40         GdkColor colors[LOG_COUNT];
41
42         guint lines;
43         GtkWidget *regexp_box;
44         GtkWidget *bar;
45         GtkWidget *pause;
46         GtkWidget *wrap;
47         GtkWidget *debug_level;
48 };
49
50 typedef struct _LogDef LogDef;
51 struct _LogDef
52 {
53         LogType type;
54         const gchar *tag;
55         const gchar *color;
56 };
57
58 /* Keep LogType order !! */
59 static LogDef logdefs[LOG_COUNT] = {
60         { LOG_NORMAL,   "normal",       "black"  },
61         { LOG_MSG,      "message",      "blue"   },
62         { LOG_WARN,     "warning",      "orange" },
63         { LOG_ERROR,    "error",        "red"    },
64 };
65
66 static LogWindow *logwindow = NULL;
67
68 static void hide_cb(GtkWidget *widget, LogWindow *logwin)
69 {
70 }
71
72 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event,
73                             LogWindow *logwin)
74 {
75         if (event && event->keyval == GDK_KEY_Escape)
76                 gtk_widget_hide(logwin->window);
77         return FALSE;
78 }
79
80
81 static void log_window_pause_cb(GtkWidget *widget, gpointer data)
82 {
83         options->log_window.paused = !options->log_window.paused;
84 }
85
86 static void log_window_line_wrap_cb(GtkWidget *widget, gpointer data)
87 {
88         LogWindow *logwin = data;
89
90         options->log_window.line_wrap = !options->log_window.line_wrap;
91
92         if (options->log_window.line_wrap)
93                 {
94                 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(logwin->text), GTK_WRAP_WORD);
95                 }
96         else
97                 {
98                 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(logwin->text), GTK_WRAP_NONE);
99                 }
100 }
101
102 static void log_window_regexp_cb(GtkWidget *text_entry, gpointer data)
103 {
104         gchar *new_regexp;
105
106         new_regexp = g_strdup(gtk_entry_get_text(GTK_ENTRY(text_entry)));
107         set_regexp(new_regexp);
108         g_free(new_regexp);
109 }
110
111 static void log_window_debug_spin_cb(GtkSpinButton *debug_level, gpointer data)
112 {
113         set_debug_level(gtk_spin_button_get_value(debug_level));
114 }
115
116 static LogWindow *log_window_create(LayoutWindow *lw)
117 {
118         LogWindow *logwin;
119         GtkWidget *window;
120         GtkWidget *scrolledwin;
121         GtkWidget *text;
122         GtkTextBuffer *buffer;
123         GtkTextIter iter;
124         GtkWidget *button;
125         GtkWidget *win_vbox;
126         GtkWidget *textbox;
127         GtkWidget *hbox;
128
129         logwin = g_new0(LogWindow, 1);
130
131         window = window_new(GTK_WINDOW_TOPLEVEL, "log", NULL, NULL, _("Log"));
132         win_vbox = gtk_vbox_new(FALSE, PREF_PAD_SPACE);
133         gtk_container_add(GTK_CONTAINER(window), win_vbox);
134         gtk_widget_show(win_vbox);
135
136         gtk_widget_set_size_request(window, lw->options.log_window.w, lw->options.log_window.h);
137         gtk_window_move(GTK_WINDOW(window), lw->options.log_window.x, lw->options.log_window.y);
138
139         g_signal_connect(G_OBJECT(window), "delete_event",
140                          G_CALLBACK(gtk_widget_hide_on_delete), NULL);
141         g_signal_connect(G_OBJECT(window), "key_press_event",
142                          G_CALLBACK(key_pressed), logwin);
143         g_signal_connect(G_OBJECT(window), "hide",
144                          G_CALLBACK(hide_cb), logwin);
145         gtk_widget_realize(window);
146
147         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
148         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
149                                        GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
150         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwin),
151                                             GTK_SHADOW_IN);
152
153         gtk_container_add(GTK_CONTAINER(win_vbox), scrolledwin);
154         gtk_widget_show(scrolledwin);
155
156         hbox = pref_box_new(win_vbox, FALSE, GTK_ORIENTATION_HORIZONTAL, PREF_PAD_SPACE);
157
158         gtk_widget_show(hbox);
159         logwin->debug_level = pref_spin_new_mnemonic(hbox, _("Debug level:"), NULL,
160                           0, 4, 1, 1, get_debug_level(),G_CALLBACK(log_window_debug_spin_cb),
161                           logwin->debug_level );
162
163         logwin->pause = pref_button_new(hbox, NULL, "Pause", FALSE,
164                                            G_CALLBACK(log_window_pause_cb), NULL);
165
166         logwin->wrap = pref_button_new(hbox, NULL, "Line wrap", FALSE,
167                                            G_CALLBACK(log_window_line_wrap_cb), logwin);
168
169         pref_label_new(hbox, "Filter regexp");
170
171         textbox = gtk_entry_new();
172         gtk_container_add(GTK_CONTAINER(hbox), textbox);
173         gtk_widget_show(textbox);
174         g_signal_connect(G_OBJECT(textbox), "activate",
175                          G_CALLBACK(log_window_regexp_cb), logwin);
176
177         text = gtk_text_view_new();
178         gtk_text_view_set_editable(GTK_TEXT_VIEW(text), FALSE);
179         gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text), GTK_WRAP_WORD);
180         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
181         gtk_text_buffer_get_start_iter(buffer, &iter);
182         gtk_text_buffer_create_mark(buffer, "end", &iter, FALSE);
183         gtk_container_add(GTK_CONTAINER(scrolledwin), text);
184         gtk_widget_show(text);
185
186         logwin->window = window;
187         logwin->scrolledwin = scrolledwin;
188         logwin->text = text;
189         logwin->lines = 1;
190         logwin->regexp_box = textbox;
191         lw->log_window = logwin->window;
192         return logwin;
193 }
194
195 static void log_window_init(LogWindow *logwin)
196 {
197         GtkTextBuffer *buffer;
198 #if !GTK_CHECK_VERSION(3,0,0)
199         GdkColormap *colormap;
200         gboolean success[LOG_COUNT];
201 #endif
202         gint i;
203
204         g_assert(logwin != NULL);
205         g_assert(logwin->colors != NULL);
206 #if !GTK_CHECK_VERSION(3,0,0)
207         for (i = LOG_NORMAL; i < LOG_COUNT; i++)
208                 {
209                 gboolean ok = gdk_color_parse(logdefs[i].color, &logwin->colors[i]);
210                 if (ok == TRUE) continue;
211
212                 if (i == LOG_NORMAL) return;
213                 memcpy(&logwin->colors[i], &logwin->colors[LOG_NORMAL], sizeof(GdkColor));
214                 }
215
216         colormap = gdk_drawable_get_colormap(gtk_widget_get_window(logwin->window));
217         gdk_colormap_alloc_colors(colormap, logwin->colors, LOG_COUNT, FALSE, TRUE, success);
218
219         for (i = LOG_NORMAL; i < LOG_COUNT; i++)
220                 {
221                 if (success[i] == FALSE)
222                         {
223                         GtkStyle *style;
224                         gint j;
225
226                         g_warning("LogWindow: color allocation failed\n");
227                         style = gtk_widget_get_style(logwin->window);
228                         for (j = LOG_NORMAL; j < LOG_COUNT; j++)
229                                 logwin->colors[j] = style->black;
230                         break;
231                         }
232                 }
233 #endif
234         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(logwin->text));
235         for (i = LOG_NORMAL; i < LOG_COUNT; i++)
236                 gtk_text_buffer_create_tag(buffer, logdefs[i].tag,
237                                            "foreground-gdk", &logwin->colors[i],
238                                            "family", "MonoSpace",
239                                            NULL);
240 }
241
242 static void log_window_show(LogWindow *logwin)
243 {
244         GtkTextView *text = GTK_TEXT_VIEW(logwin->text);
245         GtkTextBuffer *buffer;
246         GtkTextMark *mark;
247         gchar *regexp;
248
249         g_assert(logwin != NULL);
250
251         buffer = gtk_text_view_get_buffer(text);
252         mark = gtk_text_buffer_get_mark(buffer, "end");
253         gtk_text_view_scroll_mark_onscreen(text, mark);
254
255         gtk_window_present(GTK_WINDOW(logwin->window));
256
257         log_window_append("", LOG_NORMAL); // to flush memorized lines
258
259         regexp = g_strdup(get_regexp());
260         if (regexp != NULL)
261                 {
262                 gtk_entry_set_text(GTK_ENTRY(logwin->regexp_box), regexp);
263                 g_free(regexp);
264                 }
265 }
266
267 void log_window_new(LayoutWindow *lw)
268 {
269         if (logwindow == NULL)
270                 {
271                 LogWindow *logwin;
272
273                 logwin = log_window_create(lw);
274                 log_window_init(logwin);
275                 logwindow = logwin;
276                 }
277
278         log_window_show(logwindow);
279 }
280
281 typedef struct _LogMsg LogMsg;
282
283 struct _LogMsg {
284         gchar *text;
285         LogType type;
286 };
287
288
289 static void log_window_insert_text(GtkTextBuffer *buffer, GtkTextIter *iter,
290                                    const gchar *text, const gchar *tag)
291 {
292         gchar *str_utf8;
293
294         if (!text || !*text) return;
295
296         str_utf8 = utf8_validate_or_convert(text);
297         gtk_text_buffer_insert_with_tags_by_name(buffer, iter, str_utf8, -1, tag, NULL);
298         g_free(str_utf8);
299 }
300
301
302 void log_window_append(const gchar *str, LogType type)
303 {
304         GtkTextView *text;
305         GtkTextBuffer *buffer;
306         GtkTextIter iter;
307         static GList *memory = NULL;
308
309         if (logwindow == NULL)
310                 {
311                 if (*str) {
312                         LogMsg *msg = g_new(LogMsg, 1);
313
314                         msg->text = g_strdup(str);
315                         msg->type = type;
316
317                         memory = g_list_prepend(memory, msg);
318
319                         while (g_list_length(memory) >= options->log_window_lines)
320                                 {
321                                 GList *work = g_list_last(memory);
322                                 LogMsg *oldest_msg = work->data;
323
324                                 g_free(oldest_msg->text);
325                                 memory = g_list_delete_link(memory, work);
326                                 }
327                         }
328                 return;
329                 }
330
331         text = GTK_TEXT_VIEW(logwindow->text);
332         buffer = gtk_text_view_get_buffer(text);
333
334         if (options->log_window_lines > 0 && logwindow->lines >= options->log_window_lines)
335                 {
336                 GtkTextIter start, end;
337
338                 gtk_text_buffer_get_start_iter(buffer, &start);
339                 end = start;
340                 gtk_text_iter_forward_lines(&end, logwindow->lines - options->log_window_lines);
341                 gtk_text_buffer_delete(buffer, &start, &end);
342                 }
343
344         gtk_text_buffer_get_end_iter(buffer, &iter);
345
346         {
347         GList *work = g_list_last(memory);
348
349         while (work)
350                 {
351                 GList *prev;
352                 LogMsg *oldest_msg = work->data;
353
354                 log_window_insert_text(buffer, &iter, oldest_msg->text, logdefs[oldest_msg->type].tag);
355
356                 prev = work->prev;
357                 memory = g_list_delete_link(memory, work);
358                 work = prev;
359                 }
360         }
361
362         log_window_insert_text(buffer, &iter, str, logdefs[type].tag);
363
364         if (!options->log_window.paused)
365                 {
366                 if (gtk_widget_get_visible(GTK_WIDGET(text)))
367                         {
368                         GtkTextMark *mark;
369
370                         mark = gtk_text_buffer_get_mark(buffer, "end");
371                         gtk_text_view_scroll_mark_onscreen(text, mark);
372                         }
373                 }
374
375         logwindow->lines = gtk_text_buffer_get_line_count(buffer);
376 }
377 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */