e49faa6fb7653fa12be0dc1ebf245790882a4b8b
[geeqie.git] / src / logwindow.cc
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 <algorithm>
25 #include <deque>
26 #include <string>
27
28 #include "layout.h"
29 #include "misc.h"
30 #include "ui-misc.h"
31 #include "window.h"
32
33 struct LogWindow
34 {
35         GtkWidget *window;
36         GtkWidget *scrolledwin;
37         GtkWidget *text;
38         GtkTextTag *color_tags[LOG_COUNT];
39         gint lines;
40         GtkWidget *regexp_box;
41         GtkWidget *bar;
42         GtkWidget *pause;
43         GtkWidget *wrap;
44         GtkWidget *timer_data;
45         GtkWidget *debug_level;
46         gint debug_value; /**< Not used */
47         GtkWidget *search_entry_box;
48         gboolean highlight_all;
49 };
50
51 enum LogWindowSearchDirection {
52         LOGWINDOW_SEARCH_BACKWARDS,
53         LOGWINDOW_SEARCH_FORWARDS
54 };
55
56 static LogWindow *logwindow = nullptr;
57
58 static void hide_cb(GtkWidget *, LogWindow *)
59 {
60 }
61
62 static gboolean iter_char_search_cb(gunichar ch, gpointer data)
63 {
64         gboolean ret;
65
66         if (ch == GPOINTER_TO_UINT(data))
67                 {
68                 ret = TRUE;
69                 }
70         else
71                 {
72                 ret = FALSE;
73                 }
74
75         return ret;
76 }
77
78 /**
79  * @brief Handle escape and F1 keys
80  * @param UNUSED
81  * @param event
82  * @param logwin
83  * @returns
84  *
85  * If escape key pressed, hide log window. \n
86  * If no text selected, form a selection bounded by space characters or
87  * start and end of line. \n
88  * If F1 pressed, execute command line program: \n
89  * <options->log_window.action> <selected text>
90  *
91 */
92 static gboolean key_pressed(GtkWidget *, GdkEventKey *event, LogWindow *logwin)
93 {
94         GtkTextBuffer *buffer;
95         GtkTextIter chr_end;
96         GtkTextIter chr_start;
97         GtkTextIter cursor_iter;
98         GtkTextIter line_end;
99         GtkTextIter line_start;
100         GtkTextMark *cursor_mark;
101         gchar *cmd_line;
102         gchar *sel_text;
103
104         if (event && event->keyval == GDK_KEY_Escape)
105                 gtk_widget_hide(logwin->window);
106
107         if (event && event->keyval == GDK_KEY_F1)
108                 {
109                 if (strlen(options->log_window.action) > 0)
110                         {
111                         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(logwin->text));
112
113                         if (!gtk_text_buffer_get_has_selection(buffer))
114                                 {
115                                 cursor_mark = gtk_text_buffer_get_insert(buffer);
116                                 gtk_text_buffer_get_iter_at_mark(buffer, &cursor_iter, cursor_mark);
117
118                                 line_start = cursor_iter;
119                                 gtk_text_iter_set_line_offset(&line_start, 0);
120                                 line_end = cursor_iter;
121                                 gtk_text_iter_forward_to_line_end(&line_end);
122
123                                 chr_start = cursor_iter;
124                                 gtk_text_iter_backward_find_char(&chr_start, static_cast<GtkTextCharPredicate>(iter_char_search_cb), GUINT_TO_POINTER(' '), &line_start);
125
126                                 chr_end = cursor_iter;
127                                 gtk_text_iter_forward_find_char(&chr_end, static_cast<GtkTextCharPredicate>(iter_char_search_cb), GUINT_TO_POINTER(' '), &line_end);
128
129                                 gtk_text_buffer_select_range(buffer, &chr_start, &chr_end);
130                                 }
131
132                         if (gtk_text_buffer_get_selection_bounds(gtk_text_view_get_buffer(GTK_TEXT_VIEW(logwin->text)), &chr_start, &chr_end))
133                                 {
134                                 sel_text = gtk_text_buffer_get_text( gtk_text_view_get_buffer(GTK_TEXT_VIEW(logwin->text)), &chr_start, &chr_end, FALSE);
135
136                                 cmd_line = g_strconcat(options->log_window.action, " ", sel_text, NULL);
137
138                                 runcmd(cmd_line);
139
140                                 g_free(sel_text);
141                                 g_free(cmd_line);
142                                 }
143                         }
144                 }
145
146         return FALSE;
147 }
148
149
150 static void log_window_pause_cb(GtkWidget *, gpointer)
151 {
152         options->log_window.paused = !options->log_window.paused;
153 }
154
155 static void log_window_line_wrap_cb(GtkWidget *, gpointer data)
156 {
157         auto logwin = static_cast<LogWindow *>(data);
158
159         options->log_window.line_wrap = !options->log_window.line_wrap;
160
161         if (options->log_window.line_wrap)
162                 {
163                 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(logwin->text), GTK_WRAP_WORD);
164                 }
165         else
166                 {
167                 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(logwin->text), GTK_WRAP_NONE);
168                 }
169 }
170
171 static void log_window_timer_data_cb(GtkWidget *, gpointer)
172 {
173         options->log_window.timer_data = !options->log_window.timer_data;
174 }
175
176 static void log_window_regexp_cb(GtkWidget *text_entry, gpointer)
177 {
178         gchar *new_regexp;
179
180         new_regexp = g_strdup(gq_gtk_entry_get_text(GTK_ENTRY(text_entry)));
181         set_regexp(new_regexp);
182         g_free(new_regexp);
183 }
184
185 static void remove_green_bg(LogWindow *logwin)
186 {
187         GtkTextIter start_find;
188         GtkTextIter start_match;
189         GtkTextIter end_match;
190         GtkTextBuffer *buffer;
191         GSList *list;
192         const gchar *text;
193         gchar *tag_name;
194         gint offset;
195
196         text = gq_gtk_entry_get_text(GTK_ENTRY(logwin->search_entry_box));
197         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(logwin->text));
198         gtk_text_buffer_get_start_iter(buffer, &start_find);
199
200         while (gtk_text_iter_forward_search(&start_find, text, GTK_TEXT_SEARCH_VISIBLE_ONLY,  &start_match, &end_match, nullptr))
201                 {
202                 list = gtk_text_iter_get_tags(&start_match);
203                 while (list)
204                         {
205                         g_object_get(list->data, "name", &tag_name, NULL);
206                         if (g_strcmp0(tag_name, "green_bg") == 0)
207                                 {
208                                 gtk_text_buffer_remove_tag_by_name(buffer, "green_bg", &start_match, &end_match);
209                                 }
210                         list = list->next;
211                         }
212                 g_slist_free(list);
213
214                 offset = gtk_text_iter_get_offset(&end_match);
215                 gtk_text_buffer_get_iter_at_offset(buffer, &start_find, offset);
216                 }
217 }
218
219 static void search_activate_event(GtkEntry *, LogWindow *logwin)
220 {
221         GtkTextIter start_find;
222         GtkTextIter start_match;
223         GtkTextIter end_match;
224         GtkTextBuffer *buffer;
225         GtkTextMark *cursor_mark;
226         GtkTextIter cursor_iter;
227         const gchar *text;
228         gint offset;
229
230         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(logwin->text));
231         text = gq_gtk_entry_get_text(GTK_ENTRY(logwin->search_entry_box));
232
233         if (logwin->highlight_all)
234                 {
235                 gtk_text_buffer_get_start_iter(buffer, &start_find);
236
237                 while (gtk_text_iter_forward_search(&start_find, text, GTK_TEXT_SEARCH_VISIBLE_ONLY, &start_match, &end_match, nullptr))
238                         {
239                         gtk_text_buffer_apply_tag_by_name(buffer, "gray_bg", &start_match, &end_match);
240                         offset = gtk_text_iter_get_offset(&end_match);
241                         gtk_text_buffer_get_iter_at_offset(buffer, &start_find, offset);
242                         }
243                 }
244         else
245                 {
246                 cursor_mark = gtk_text_buffer_get_insert(buffer);
247                 gtk_text_buffer_get_iter_at_mark(buffer, &cursor_iter, cursor_mark);
248
249                 if (gtk_text_iter_forward_search(&cursor_iter, text, GTK_TEXT_SEARCH_VISIBLE_ONLY, &start_match, &end_match, nullptr))
250                         {
251                         gtk_text_buffer_apply_tag_by_name(buffer, "gray_bg", &start_match, &end_match);
252                         }
253                 }
254 }
255
256 static gboolean search_keypress_event(GtkWidget *, GdkEventKey *, LogWindow *logwin, LogWindowSearchDirection direction)
257 {
258         GtkTextIter start_find;
259         GtkTextIter start_match;
260         GtkTextIter end_match;
261         GtkTextIter start_sel;
262         GtkTextIter end_sel;
263         const gchar *text;
264         GtkTextBuffer *buffer;
265         GtkTextMark *cursor_mark;
266         GtkTextIter cursor_iter;
267         gint offset;
268         gboolean match_found = FALSE;
269         gboolean selected;
270
271         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(logwin->text));
272         gtk_text_buffer_get_start_iter(buffer, &start_find);
273
274         text = gq_gtk_entry_get_text(GTK_ENTRY(logwin->search_entry_box));
275         if (strlen(text) == 0)
276                 {
277                 selected = gtk_text_buffer_get_selection_bounds(buffer, &start_sel, &end_sel);
278                 if (selected)
279                         {
280                         text = gtk_text_buffer_get_text(buffer, &start_sel, &end_sel, FALSE);
281                         gq_gtk_entry_set_text(GTK_ENTRY(logwin->search_entry_box), text);
282                         }
283                 }
284
285         if (logwin->highlight_all)
286                 {
287                 while (gtk_text_iter_forward_search(&start_find, text, GTK_TEXT_SEARCH_VISIBLE_ONLY, &start_match, &end_match, nullptr))
288                         {
289                         gtk_text_buffer_apply_tag_by_name(buffer, "gray_bg", &start_match, &end_match);
290                         offset = gtk_text_iter_get_offset(&end_match);
291                         gtk_text_buffer_get_iter_at_offset(buffer, &start_find, offset);
292                         }
293                 }
294
295         cursor_mark = gtk_text_buffer_get_insert(buffer);
296         gtk_text_buffer_get_iter_at_mark(buffer, &cursor_iter, cursor_mark);
297
298         if (direction == LOGWINDOW_SEARCH_BACKWARDS)
299                 {
300                 match_found = gtk_text_iter_backward_search( &cursor_iter, text, GTK_TEXT_SEARCH_VISIBLE_ONLY,  &start_match, &end_match, nullptr);
301                 }
302         else
303                 {
304                 match_found = gtk_text_iter_forward_search( &cursor_iter, text, GTK_TEXT_SEARCH_VISIBLE_ONLY,  &start_match, &end_match, nullptr);
305                 }
306
307         if (match_found)
308                 {
309                 remove_green_bg(logwin);
310
311                 gtk_text_buffer_apply_tag_by_name(buffer, "green_bg",  &start_match, &end_match);
312
313                 if (direction == LOGWINDOW_SEARCH_BACKWARDS)
314                         {
315                         gtk_text_buffer_place_cursor(buffer, &start_match);
316                         }
317                 else
318                         {
319                         gtk_text_buffer_place_cursor(buffer, &end_match);
320                         }
321
322                 cursor_mark = gtk_text_buffer_get_insert(buffer);
323                 gtk_text_view_scroll_to_mark(GTK_TEXT_VIEW(logwin->text), cursor_mark, 0.2, FALSE, 0.0, 0.0);
324         }
325
326         return FALSE;
327 }
328
329 static gboolean backwards_keypress_event_cb(GtkWidget *widget, GdkEventKey *event, LogWindow *logwin)
330 {
331         search_keypress_event(widget, event, logwin, LOGWINDOW_SEARCH_BACKWARDS);
332
333         return FALSE;
334 }
335
336 static gboolean forwards_keypress_event_cb(GtkWidget *widget, GdkEventKey *event, LogWindow *logwin)
337 {
338         search_keypress_event(widget, event, logwin, LOGWINDOW_SEARCH_FORWARDS);
339
340         return FALSE;
341 }
342
343 static gboolean all_keypress_event_cb(GtkToggleButton *widget, LogWindow *logwin)
344 {
345         logwin->highlight_all = gtk_toggle_button_get_active(widget);
346
347         return FALSE;
348 }
349
350 static gboolean debug_changed_cb(GtkSpinButton *widget, LogWindow *)
351 {
352         set_debug_level((gtk_spin_button_get_value(widget)));
353
354         return FALSE;
355 }
356
357 static void search_entry_icon_cb(GtkEntry *, GtkEntryIconPosition pos, GdkEvent *, gpointer userdata)
358 {
359         auto logwin = static_cast<LogWindow *>(userdata);
360         GtkTextIter start_find;
361         GtkTextIter end_find;
362         GtkTextBuffer *buffer;
363
364         if (pos == GTK_ENTRY_ICON_SECONDARY)
365                 {
366                 gq_gtk_entry_set_text(GTK_ENTRY(logwin->search_entry_box), "");
367
368                 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(logwin->text));
369                 gtk_text_buffer_get_start_iter(buffer, &start_find);
370                 gtk_text_buffer_get_end_iter(buffer, &end_find);
371                 gtk_text_buffer_remove_tag_by_name(buffer, "gray_bg", &start_find, &end_find);
372                 gtk_text_buffer_remove_tag_by_name(buffer, "green_bg", &start_find, &end_find);
373                 }
374 }
375
376 static void filter_entry_icon_cb(GtkEntry *entry, GtkEntryIconPosition, GdkEvent *, gpointer)
377 {
378         const gchar *blank = "";
379         gq_gtk_entry_set_text(entry, blank);
380         set_regexp(blank);
381 }
382
383 static LogWindow *log_window_create(LayoutWindow *lw)
384 {
385         LogWindow *logwin;
386         GtkWidget *window;
387         GtkWidget *scrolledwin;
388         GtkWidget *text;
389         GtkTextBuffer *buffer;
390         GtkTextIter iter;
391         GtkWidget *win_vbox;
392         GtkWidget *textbox;
393         GtkWidget *hbox;
394         GtkWidget *label = nullptr;
395         GtkWidget *search_box;
396         GtkWidget *backwards_button;
397         GtkWidget *forwards_button;
398         GtkWidget *all_button;
399         GtkIconTheme *theme;
400         GdkPixbuf *pixbuf;
401         GtkWidget *image = nullptr;
402
403         logwin = g_new0(LogWindow, 1);
404
405         window = window_new("log", nullptr, nullptr, _("Log"));
406         DEBUG_NAME(window);
407         win_vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, PREF_PAD_SPACE);
408         gq_gtk_container_add(GTK_WIDGET(window), win_vbox);
409         gtk_widget_show(win_vbox);
410
411         gtk_window_resize(GTK_WINDOW(window), lw->options.log_window.w,
412                                                                                         lw->options.log_window.h);
413         gq_gtk_window_move(GTK_WINDOW(window), lw->options.log_window.x, lw->options.log_window.y);
414
415         g_signal_connect(G_OBJECT(window), "delete_event",
416                          G_CALLBACK(gtk_widget_hide_on_delete), NULL);
417         g_signal_connect(G_OBJECT(window), "key_press_event",
418                          G_CALLBACK(key_pressed), logwin);
419         g_signal_connect(G_OBJECT(window), "hide",
420                          G_CALLBACK(hide_cb), logwin);
421         gtk_widget_realize(window);
422
423         scrolledwin = gq_gtk_scrolled_window_new(nullptr, nullptr);
424         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
425                                        GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
426         gq_gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwin),
427                                             GTK_SHADOW_IN);
428
429         gq_gtk_box_pack_start(GTK_BOX(win_vbox), scrolledwin, TRUE, TRUE, 0);
430         gtk_widget_show(scrolledwin);
431
432         text = gtk_text_view_new();
433         gtk_text_view_set_editable(GTK_TEXT_VIEW(text), FALSE);
434         if (options->log_window.line_wrap)
435                 {
436                 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text), GTK_WRAP_WORD);
437                 }
438         else
439                 {
440                 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text), GTK_WRAP_NONE);
441                 }
442         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
443         gtk_text_buffer_get_start_iter(buffer, &iter);
444         gtk_text_buffer_create_mark(buffer, "end", &iter, FALSE);
445         gq_gtk_container_add(GTK_WIDGET(scrolledwin), text);
446         gtk_widget_show(text);
447
448 #ifdef DEBUG
449         gtk_text_buffer_create_tag(buffer, "gray_bg", "background", "gray", NULL);
450         gtk_text_buffer_create_tag(buffer, "green_bg", "background", "#00FF00", NULL);
451
452         hbox = pref_box_new(win_vbox, FALSE, GTK_ORIENTATION_HORIZONTAL, PREF_PAD_SPACE);
453
454         gtk_widget_show(hbox);
455         logwin->debug_level = pref_spin_new_int(hbox, _("Debug level:"), nullptr, 0, 4, 1, get_debug_level(), &logwin->debug_value);
456         g_signal_connect(logwin->debug_level, "value-changed", G_CALLBACK(debug_changed_cb), logwin);
457
458         logwin->pause = gtk_toggle_button_new();
459         label = gtk_label_new("Pause");
460         gtk_widget_set_tooltip_text(GTK_WIDGET(logwin->pause), _("Pause scrolling"));
461         gq_gtk_container_add(GTK_WIDGET(logwin->pause), label) ;
462         gq_gtk_box_pack_start(GTK_BOX(hbox),logwin->pause, FALSE, FALSE, 0) ;
463         g_signal_connect(logwin->pause, "toggled", G_CALLBACK(log_window_pause_cb), logwin);
464         gtk_widget_show_all(logwin->pause);
465
466         logwin->wrap = gtk_toggle_button_new();
467         label = gtk_label_new("Wrap");
468         gtk_widget_set_tooltip_text(GTK_WIDGET(logwin->wrap), _("Enable line wrap"));
469         gq_gtk_container_add(GTK_WIDGET(logwin->wrap), label) ;
470         gq_gtk_box_pack_start(GTK_BOX(hbox),logwin->wrap, FALSE, FALSE, 0) ;
471         g_signal_connect(logwin->wrap, "toggled", G_CALLBACK(log_window_line_wrap_cb), logwin);
472         gtk_widget_show_all(logwin->wrap);
473
474         logwin->timer_data = gtk_toggle_button_new();
475         label = gtk_label_new("Timer");
476         gtk_widget_set_tooltip_text(GTK_WIDGET(logwin->timer_data), _("Enable timer data"));
477         gq_gtk_container_add(GTK_WIDGET(logwin->timer_data), label) ;
478         gq_gtk_box_pack_start(GTK_BOX(hbox),logwin->timer_data, FALSE, FALSE, 0) ;
479         if (options->log_window.timer_data)
480                 {
481                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(logwin->timer_data), TRUE);
482                 }
483         g_signal_connect(logwin->timer_data, "toggled", G_CALLBACK(log_window_timer_data_cb), logwin);
484         gtk_widget_show_all(logwin->timer_data);
485
486         search_box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
487         gq_gtk_container_add(GTK_WIDGET(hbox), search_box);
488         gtk_widget_show(search_box);
489
490         logwin->search_entry_box = gtk_entry_new();
491         gq_gtk_box_pack_start(GTK_BOX(search_box), logwin->search_entry_box, FALSE, FALSE, 0);
492         gtk_widget_show(logwin->search_entry_box);
493         gtk_entry_set_icon_from_icon_name(GTK_ENTRY(logwin->search_entry_box), GTK_ENTRY_ICON_PRIMARY, GQ_ICON_FIND);
494         gtk_entry_set_icon_from_icon_name(GTK_ENTRY(logwin->search_entry_box), GTK_ENTRY_ICON_SECONDARY, GQ_ICON_CLEAR);
495         gtk_widget_show(search_box);
496         gtk_widget_set_tooltip_text(logwin->search_entry_box, _("Search for text in log window"));
497         g_signal_connect(logwin->search_entry_box, "icon-press", G_CALLBACK(search_entry_icon_cb), logwin);
498         g_signal_connect(logwin->search_entry_box, "activate", G_CALLBACK(search_activate_event), logwin);
499
500         theme = gtk_icon_theme_get_default();
501         pixbuf = gtk_icon_theme_load_icon(theme, GQ_ICON_PAN_UP, 20, GTK_ICON_LOOKUP_GENERIC_FALLBACK, nullptr);
502         image = gtk_image_new_from_pixbuf(pixbuf);
503         backwards_button = gtk_button_new();
504         gtk_button_set_image(GTK_BUTTON(backwards_button), GTK_WIDGET(image));
505         gtk_widget_set_tooltip_text(backwards_button, _("Search backwards"));
506         gq_gtk_box_pack_start(GTK_BOX(search_box), backwards_button, FALSE, FALSE, 0);
507         gtk_widget_show(backwards_button);
508         g_signal_connect(backwards_button, "button_release_event", G_CALLBACK(backwards_keypress_event_cb), logwin);
509         g_object_unref(pixbuf);
510
511         pixbuf = gtk_icon_theme_load_icon(theme, GQ_ICON_PAN_DOWN, 20, GTK_ICON_LOOKUP_GENERIC_FALLBACK, nullptr);
512         image = gtk_image_new_from_pixbuf(pixbuf);
513         forwards_button = gtk_button_new();
514         gtk_button_set_image(GTK_BUTTON(forwards_button), GTK_WIDGET(image));
515         gtk_widget_set_tooltip_text(forwards_button, _("Search forwards"));
516         gq_gtk_box_pack_start(GTK_BOX(search_box), forwards_button, FALSE, FALSE, 0);
517         gtk_widget_show(forwards_button);
518         g_signal_connect(forwards_button, "button_release_event", G_CALLBACK(forwards_keypress_event_cb), logwin);
519         g_object_unref(pixbuf);
520
521         pixbuf = gtk_icon_theme_load_icon(theme, "edit-select-all-symbolic", 20, GTK_ICON_LOOKUP_GENERIC_FALLBACK, nullptr);
522         image = gtk_image_new_from_pixbuf(pixbuf);
523         all_button = gtk_toggle_button_new();
524         gtk_button_set_image(GTK_BUTTON(all_button), GTK_WIDGET(image));
525         gtk_widget_set_tooltip_text(GTK_WIDGET(all_button), _("Highlight all"));
526         gq_gtk_box_pack_start(GTK_BOX(search_box), all_button, FALSE, FALSE, 0) ;
527         g_signal_connect(all_button, "toggled", G_CALLBACK(all_keypress_event_cb), logwin);
528         gtk_widget_show_all(all_button);
529         g_object_unref(pixbuf);
530
531         pref_label_new(hbox, _("Filter regexp"));
532
533         textbox = gtk_entry_new();
534         gq_gtk_box_pack_start(GTK_BOX(hbox), textbox, FALSE, FALSE, 0);
535         gtk_entry_set_icon_from_icon_name(GTK_ENTRY(textbox), GTK_ENTRY_ICON_SECONDARY, GQ_ICON_CLEAR);
536         gtk_widget_show(textbox);
537         g_signal_connect(G_OBJECT(textbox), "activate",
538                          G_CALLBACK(log_window_regexp_cb), logwin);
539         g_signal_connect(textbox, "icon-press", G_CALLBACK(filter_entry_icon_cb), logwin);
540 #endif
541
542         logwin->window = window;
543         logwin->scrolledwin = scrolledwin;
544         logwin->text = text;
545         logwin->lines = 1;
546         logwin->regexp_box = textbox;
547         lw->log_window = logwin->window;
548         return logwin;
549 }
550
551 static void log_window_init(LogWindow *logwin)
552 {
553         GtkTextBuffer *buffer;
554
555         g_assert(logwin != nullptr);
556
557         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(logwin->text));
558
559         logwin->color_tags[LOG_NORMAL] = gtk_text_buffer_create_tag (buffer,
560                                                         "black_foreground", "foreground", "black",
561                                                         "family", "MonoSpace", NULL);
562         logwin->color_tags[LOG_MSG] = gtk_text_buffer_create_tag (buffer,
563                                                         "blue_foreground", "foreground", "blue",
564                                                         "family", "MonoSpace", NULL);
565         logwin->color_tags[LOG_WARN] = gtk_text_buffer_create_tag (buffer,
566                                                         "orange_foreground", "foreground", "orange",
567                                                         "family", "MonoSpace", NULL);
568         logwin->color_tags[LOG_ERROR] = gtk_text_buffer_create_tag (buffer,
569                                                         "red_foreground", "foreground", "red",
570                                                         "family", "MonoSpace", NULL);
571 }
572
573 static void log_window_show(LogWindow *logwin)
574 {
575         GtkTextView *text = GTK_TEXT_VIEW(logwin->text);
576         GtkTextBuffer *buffer;
577         GtkTextMark *mark;
578         gchar *regexp;
579
580         buffer = gtk_text_view_get_buffer(text);
581         mark = gtk_text_buffer_get_mark(buffer, "end");
582         gtk_text_view_scroll_mark_onscreen(text, mark);
583
584         gtk_window_present(GTK_WINDOW(logwin->window));
585
586         log_window_append("", LOG_NORMAL); // to flush memorized lines
587
588         regexp = g_strdup(get_regexp());
589         if (regexp != nullptr)
590                 {
591                 gq_gtk_entry_set_text(GTK_ENTRY(logwin->regexp_box), regexp);
592                 g_free(regexp);
593                 }
594 }
595
596 void log_window_new(LayoutWindow *lw)
597 {
598         if (logwindow == nullptr)
599                 {
600                 LogWindow *logwin;
601
602                 logwin = log_window_create(lw);
603                 log_window_init(logwin);
604                 logwindow = logwin;
605                 }
606
607         log_window_show(logwindow);
608 }
609
610 struct LogMsg {
611         LogMsg() = default;
612         LogMsg(const gchar *text, LogType type)
613                 : text(text)
614                 , type(type)
615         {}
616         std::string text;
617         LogType type;
618 };
619
620 static void log_window_insert_text(GtkTextBuffer *buffer, GtkTextIter *iter,
621                                    const gchar *text, GtkTextTag *tag)
622 {
623         gchar *str_utf8;
624
625         if (!text || !*text) return;
626
627         str_utf8 = utf8_validate_or_convert(text);
628         gtk_text_buffer_insert_with_tags(buffer, iter, str_utf8, -1, tag, NULL);
629         g_free(str_utf8);
630 }
631
632 void log_window_append(const gchar *str, LogType type)
633 {
634         GtkTextView *text;
635         GtkTextBuffer *buffer;
636         GtkTextIter iter;
637         static std::deque<LogMsg> memory;
638
639         if (logwindow == nullptr)
640                 {
641                 if (*str)
642                         {
643                         memory.emplace_front(str, type);
644
645                         if (memory.size() >= static_cast<guint>(options->log_window_lines))
646                                 {
647                                 const auto count = std::max(options->log_window_lines - 1, 0);
648
649                                 memory.resize(count);
650                                 }
651                         }
652                 return;
653                 }
654
655         text = GTK_TEXT_VIEW(logwindow->text);
656         buffer = gtk_text_view_get_buffer(text);
657
658         if (options->log_window_lines > 0 && logwindow->lines >= options->log_window_lines)
659                 {
660                 GtkTextIter start, end;
661
662                 gtk_text_buffer_get_start_iter(buffer, &start);
663                 end = start;
664                 gtk_text_iter_forward_lines(&end, logwindow->lines - options->log_window_lines);
665                 gtk_text_buffer_delete(buffer, &start, &end);
666                 }
667
668         gtk_text_buffer_get_end_iter(buffer, &iter);
669
670         std::for_each(memory.crbegin(), memory.crend(), [buffer, &iter](const LogMsg &oldest_msg)
671                 {
672                 log_window_insert_text(buffer, &iter, oldest_msg.text.c_str(), logwindow->color_tags[oldest_msg.type]);
673                 });
674         memory.clear();
675
676         log_window_insert_text(buffer, &iter, str, logwindow->color_tags[type]);
677
678         if (!options->log_window.paused)
679                 {
680                 if (gtk_widget_get_visible(GTK_WIDGET(text)))
681                         {
682                         GtkTextMark *mark;
683
684                         mark = gtk_text_buffer_get_mark(buffer, "end");
685                         gtk_text_view_scroll_mark_onscreen(text, mark);
686                         }
687                 }
688
689         logwindow->lines = gtk_text_buffer_get_line_count(buffer);
690 }
691 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */