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