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