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