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