2 * Copyright (C) 2008 - 2016 The Geeqie Team
4 * Authors: Vladimir Nadvornik, Laurent Monin
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.
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.
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.
22 #include "logwindow.h"
25 #include "secure-save.h"
29 #include <gdk/gdkkeysyms.h>
32 typedef struct _LogWindow LogWindow;
37 GtkWidget *scrolledwin;
39 GtkTextTag *color_tags[LOG_COUNT];
41 GtkWidget *regexp_box;
45 GtkWidget *timer_data;
46 GtkWidget *debug_level;
47 gint debug_value; /**< Not used */
48 GtkWidget *search_entry_box;
49 gboolean highlight_all;
53 LOGWINDOW_SEARCH_BACKWARDS,
54 LOGWINDOW_SEARCH_FORWARDS
55 } LogWindowSearchDirection;
57 static LogWindow *logwindow = NULL;
59 static void hide_cb(GtkWidget *UNUSED(widget), LogWindow *UNUSED(logwin))
63 static gboolean key_pressed(GtkWidget *UNUSED(widget), GdkEventKey *event,
66 if (event && event->keyval == GDK_KEY_Escape)
67 gtk_widget_hide(logwin->window);
72 static void log_window_pause_cb(GtkWidget *UNUSED(widget), gpointer UNUSED(data))
74 options->log_window.paused = !options->log_window.paused;
77 static void log_window_line_wrap_cb(GtkWidget *UNUSED(widget), gpointer data)
79 LogWindow *logwin = data;
81 options->log_window.line_wrap = !options->log_window.line_wrap;
83 if (options->log_window.line_wrap)
85 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(logwin->text), GTK_WRAP_WORD);
89 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(logwin->text), GTK_WRAP_NONE);
93 static void log_window_timer_data_cb(GtkWidget *UNUSED(widget), gpointer UNUSED(data))
95 options->log_window.timer_data = !options->log_window.timer_data;
98 static void log_window_regexp_cb(GtkWidget *text_entry, gpointer UNUSED(data))
102 new_regexp = g_strdup(gtk_entry_get_text(GTK_ENTRY(text_entry)));
103 set_regexp(new_regexp);
107 static void remove_green_bg(LogWindow *logwin)
109 GtkTextIter start_find;
110 GtkTextIter start_match;
111 GtkTextIter end_match;
112 GtkTextBuffer *buffer;
118 text = gtk_entry_get_text(GTK_ENTRY(logwin->search_entry_box));
119 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(logwin->text));
120 gtk_text_buffer_get_start_iter(buffer, &start_find);
122 while (gtk_text_iter_forward_search(&start_find, text, GTK_TEXT_SEARCH_VISIBLE_ONLY, &start_match, &end_match, NULL))
124 list = gtk_text_iter_get_tags(&start_match);
127 g_object_get(list->data, "name", &tag_name, NULL);
128 if (g_strcmp0(tag_name, "green_bg") == 0)
130 gtk_text_buffer_remove_tag_by_name(buffer, "green_bg", &start_match, &end_match);
136 offset = gtk_text_iter_get_offset(&end_match);
137 gtk_text_buffer_get_iter_at_offset(buffer, &start_find, offset);
141 static void search_activate_event(GtkEntry *UNUSED(widget), LogWindow *logwin)
143 GtkTextIter start_find;
144 GtkTextIter start_match;
145 GtkTextIter end_match;
146 GtkTextBuffer *buffer;
147 GtkTextMark *cursor_mark;
148 GtkTextIter cursor_iter;
152 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(logwin->text));
153 text = gtk_entry_get_text(GTK_ENTRY(logwin->search_entry_box));
155 if (logwin->highlight_all)
157 gtk_text_buffer_get_start_iter(buffer, &start_find);
159 while (gtk_text_iter_forward_search(&start_find, text, GTK_TEXT_SEARCH_VISIBLE_ONLY, &start_match, &end_match, NULL))
161 gtk_text_buffer_apply_tag_by_name(buffer, "gray_bg", &start_match, &end_match);
162 offset = gtk_text_iter_get_offset(&end_match);
163 gtk_text_buffer_get_iter_at_offset(buffer, &start_find, offset);
168 cursor_mark = gtk_text_buffer_get_insert(buffer);
169 gtk_text_buffer_get_iter_at_mark(buffer, &cursor_iter, cursor_mark);
171 if (gtk_text_iter_forward_search(&cursor_iter, text, GTK_TEXT_SEARCH_VISIBLE_ONLY, &start_match, &end_match, NULL))
173 gtk_text_buffer_apply_tag_by_name(buffer, "gray_bg", &start_match, &end_match);
178 static gboolean search_keypress_event(GtkWidget *UNUSED(widget), GdkEventKey *UNUSED(event), LogWindow *logwin, LogWindowSearchDirection direction)
180 GtkTextIter start_find;
181 GtkTextIter start_match;
182 GtkTextIter end_match;
183 GtkTextIter start_sel;
186 GtkTextBuffer *buffer;
187 GtkTextMark *cursor_mark;
188 GtkTextIter cursor_iter;
190 gboolean match_found = FALSE;
193 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(logwin->text));
194 gtk_text_buffer_get_start_iter(buffer, &start_find);
196 text = gtk_entry_get_text(GTK_ENTRY(logwin->search_entry_box));
197 if (strlen(text) == 0)
199 selected = gtk_text_buffer_get_selection_bounds(buffer, &start_sel, &end_sel);
202 text = gtk_text_buffer_get_text(buffer, &start_sel, &end_sel, FALSE);
203 gtk_entry_set_text(GTK_ENTRY(logwin->search_entry_box), text);
207 if (logwin->highlight_all)
209 while (gtk_text_iter_forward_search(&start_find, text, GTK_TEXT_SEARCH_VISIBLE_ONLY, &start_match, &end_match, NULL))
211 gtk_text_buffer_apply_tag_by_name(buffer, "gray_bg", &start_match, &end_match);
212 offset = gtk_text_iter_get_offset(&end_match);
213 gtk_text_buffer_get_iter_at_offset(buffer, &start_find, offset);
217 cursor_mark = gtk_text_buffer_get_insert(buffer);
218 gtk_text_buffer_get_iter_at_mark(buffer, &cursor_iter, cursor_mark);
220 if (direction == LOGWINDOW_SEARCH_BACKWARDS)
222 match_found = gtk_text_iter_backward_search( &cursor_iter, text, GTK_TEXT_SEARCH_VISIBLE_ONLY, &start_match, &end_match, NULL);
226 match_found = gtk_text_iter_forward_search( &cursor_iter, text, GTK_TEXT_SEARCH_VISIBLE_ONLY, &start_match, &end_match, NULL);
231 remove_green_bg(logwin);
233 gtk_text_buffer_apply_tag_by_name(buffer, "green_bg", &start_match, &end_match);
235 if (direction == LOGWINDOW_SEARCH_BACKWARDS)
237 gtk_text_buffer_place_cursor(buffer, &start_match);
241 gtk_text_buffer_place_cursor(buffer, &end_match);
244 cursor_mark = gtk_text_buffer_get_insert(buffer);
245 gtk_text_view_scroll_to_mark(GTK_TEXT_VIEW(logwin->text), cursor_mark, 0.2, FALSE, 0.0, 0.0);
251 static gboolean backwards_keypress_event_cb(GtkWidget *widget, GdkEventKey *event, LogWindow *logwin)
253 search_keypress_event(widget, event, logwin, LOGWINDOW_SEARCH_BACKWARDS);
258 static gboolean forwards_keypress_event_cb(GtkWidget *widget, GdkEventKey *event, LogWindow *logwin)
260 search_keypress_event(widget, event, logwin, LOGWINDOW_SEARCH_FORWARDS);
265 static gboolean all_keypress_event_cb(GtkToggleButton *widget, LogWindow *logwin)
267 logwin->highlight_all = gtk_toggle_button_get_active(widget);
272 static gboolean debug_changed_cb(GtkSpinButton *widget, LogWindow *UNUSED(logwin))
274 set_debug_level((gtk_spin_button_get_value(widget)));
279 static void search_entry_icon_cb(GtkEntry *UNUSED(entry), GtkEntryIconPosition pos, GdkEvent *UNUSED(event), gpointer userdata)
281 LogWindow *logwin = userdata;
282 GtkTextIter start_find;
283 GtkTextIter end_find;
284 GtkTextBuffer *buffer;
286 if (pos == GTK_ENTRY_ICON_SECONDARY)
288 gtk_entry_set_text(GTK_ENTRY(logwin->search_entry_box), "");
290 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(logwin->text));
291 gtk_text_buffer_get_start_iter(buffer, &start_find);
292 gtk_text_buffer_get_end_iter(buffer, &end_find);
293 gtk_text_buffer_remove_tag_by_name(buffer, "gray_bg", &start_find, &end_find);
294 gtk_text_buffer_remove_tag_by_name(buffer, "green_bg", &start_find, &end_find);
298 static void filter_entry_icon_cb(GtkEntry *entry, GtkEntryIconPosition UNUSED(pos), GdkEvent *UNUSED(event), gpointer UNUSED(userdata))
300 gtk_entry_set_text(entry, "");
304 static LogWindow *log_window_create(LayoutWindow *lw)
308 GtkWidget *scrolledwin;
310 GtkTextBuffer *buffer;
315 GtkWidget *label = NULL;
316 GtkWidget *search_box;
317 GtkWidget *backwards_button;
318 GtkWidget *forwards_button;
319 GtkWidget *all_button;
322 GtkWidget *image = NULL;
324 logwin = g_new0(LogWindow, 1);
326 window = window_new(GTK_WINDOW_TOPLEVEL, "log", NULL, NULL, _("Log"));
328 win_vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, PREF_PAD_SPACE);
329 gtk_container_add(GTK_CONTAINER(window), win_vbox);
330 gtk_widget_show(win_vbox);
332 gtk_window_resize(GTK_WINDOW(window), lw->options.log_window.w,
333 lw->options.log_window.h);
334 gtk_window_move(GTK_WINDOW(window), lw->options.log_window.x, lw->options.log_window.y);
336 g_signal_connect(G_OBJECT(window), "delete_event",
337 G_CALLBACK(gtk_widget_hide_on_delete), NULL);
338 g_signal_connect(G_OBJECT(window), "key_press_event",
339 G_CALLBACK(key_pressed), logwin);
340 g_signal_connect(G_OBJECT(window), "hide",
341 G_CALLBACK(hide_cb), logwin);
342 gtk_widget_realize(window);
344 scrolledwin = gtk_scrolled_window_new(NULL, NULL);
345 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
346 GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
347 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwin),
350 gtk_box_pack_start(GTK_BOX(win_vbox), scrolledwin, TRUE, TRUE, 0);
351 gtk_widget_show(scrolledwin);
353 text = gtk_text_view_new();
354 gtk_text_view_set_editable(GTK_TEXT_VIEW(text), FALSE);
355 if (options->log_window.line_wrap)
357 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text), GTK_WRAP_WORD);
361 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text), GTK_WRAP_NONE);
363 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
364 gtk_text_buffer_get_start_iter(buffer, &iter);
365 gtk_text_buffer_create_mark(buffer, "end", &iter, FALSE);
366 gtk_container_add(GTK_CONTAINER(scrolledwin), text);
367 gtk_widget_show(text);
370 gtk_text_buffer_create_tag(buffer, "gray_bg", "background", "gray", NULL);
371 gtk_text_buffer_create_tag(buffer, "green_bg", "background", "#00FF00", NULL);
373 hbox = pref_box_new(win_vbox, FALSE, GTK_ORIENTATION_HORIZONTAL, PREF_PAD_SPACE);
375 gtk_widget_show(hbox);
376 logwin->debug_level = pref_spin_new_int(hbox, _("Debug level:"), NULL, 0, 4, 1, get_debug_level(), &logwin->debug_value);
377 g_signal_connect(logwin->debug_level, "value-changed", G_CALLBACK(debug_changed_cb), logwin);
379 logwin->pause = gtk_toggle_button_new();
380 label = gtk_label_new("Pause");
381 gtk_widget_set_tooltip_text(GTK_WIDGET(logwin->pause), _("Pause scrolling"));
382 gtk_container_add(GTK_CONTAINER(logwin->pause), label) ;
383 gtk_box_pack_start(GTK_BOX(hbox),logwin->pause, FALSE, FALSE, 0) ;
384 g_signal_connect(logwin->pause, "toggled", G_CALLBACK(log_window_pause_cb), logwin);
385 gtk_widget_show_all(logwin->pause);
387 logwin->wrap = gtk_toggle_button_new();
388 label = gtk_label_new("Wrap");
389 gtk_widget_set_tooltip_text(GTK_WIDGET(logwin->wrap), _("Enable line wrap"));
390 gtk_container_add(GTK_CONTAINER(logwin->wrap), label) ;
391 gtk_box_pack_start(GTK_BOX(hbox),logwin->wrap, FALSE, FALSE, 0) ;
392 g_signal_connect(logwin->wrap, "toggled", G_CALLBACK(log_window_line_wrap_cb), logwin);
393 gtk_widget_show_all(logwin->wrap);
395 logwin->timer_data = gtk_toggle_button_new();
396 label = gtk_label_new("Timer");
397 gtk_widget_set_tooltip_text(GTK_WIDGET(logwin->timer_data), _("Enable timer data"));
398 gtk_container_add(GTK_CONTAINER(logwin->timer_data), label) ;
399 gtk_box_pack_start(GTK_BOX(hbox),logwin->timer_data, FALSE, FALSE, 0) ;
400 if (options->log_window.timer_data)
402 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(logwin->timer_data), TRUE);
404 g_signal_connect(logwin->timer_data, "toggled", G_CALLBACK(log_window_timer_data_cb), logwin);
405 gtk_widget_show_all(logwin->timer_data);
407 search_box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
408 gtk_container_add(GTK_CONTAINER(hbox), search_box);
409 gtk_widget_show(search_box);
411 logwin->search_entry_box = gtk_entry_new();
412 gtk_box_pack_start(GTK_BOX(search_box), logwin->search_entry_box, FALSE, FALSE, 0);
413 gtk_widget_show(logwin->search_entry_box);
414 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(logwin->search_entry_box), GTK_ENTRY_ICON_PRIMARY, GTK_STOCK_FIND);
415 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(logwin->search_entry_box), GTK_ENTRY_ICON_SECONDARY, GTK_STOCK_CLEAR);
416 gtk_widget_show(search_box);
417 gtk_widget_set_tooltip_text(logwin->search_entry_box, _("Search for text in log window"));
418 g_signal_connect(logwin->search_entry_box, "icon-press", G_CALLBACK(search_entry_icon_cb), logwin);
419 g_signal_connect(logwin->search_entry_box, "activate", G_CALLBACK(search_activate_event), logwin);
421 theme = gtk_icon_theme_get_default();
422 pixbuf = gtk_icon_theme_load_icon(theme, "pan-up-symbolic", 20, GTK_ICON_LOOKUP_GENERIC_FALLBACK, NULL);
423 image = gtk_image_new_from_pixbuf(pixbuf);
424 backwards_button = gtk_button_new();
425 gtk_button_set_image(GTK_BUTTON(backwards_button), GTK_WIDGET(image));
426 gtk_widget_set_tooltip_text(backwards_button, _("Search backwards"));
427 gtk_box_pack_start(GTK_BOX(search_box), backwards_button, FALSE, FALSE, 0);
428 gtk_widget_show(backwards_button);
429 g_signal_connect(backwards_button, "button_release_event", G_CALLBACK(backwards_keypress_event_cb), logwin);
430 g_object_unref(pixbuf);
432 pixbuf = gtk_icon_theme_load_icon(theme, "pan-down-symbolic", 20, GTK_ICON_LOOKUP_GENERIC_FALLBACK, NULL);
433 image = gtk_image_new_from_pixbuf(pixbuf);
434 forwards_button = gtk_button_new();
435 gtk_button_set_image(GTK_BUTTON(forwards_button), GTK_WIDGET(image));
436 gtk_widget_set_tooltip_text(forwards_button, _("Search forwards"));
437 gtk_box_pack_start(GTK_BOX(search_box), forwards_button, FALSE, FALSE, 0);
438 gtk_widget_show(forwards_button);
439 g_signal_connect(forwards_button, "button_release_event", G_CALLBACK(forwards_keypress_event_cb), logwin);
440 g_object_unref(pixbuf);
442 pixbuf = gtk_icon_theme_load_icon(theme, "edit-select-all-symbolic", 20, GTK_ICON_LOOKUP_GENERIC_FALLBACK, NULL);
443 image = gtk_image_new_from_pixbuf(pixbuf);
444 all_button = gtk_toggle_button_new();
445 gtk_button_set_image(GTK_BUTTON(all_button), GTK_WIDGET(image));
446 gtk_widget_set_tooltip_text(GTK_WIDGET(all_button), _("Highlight all"));
447 gtk_box_pack_start(GTK_BOX(search_box), all_button, FALSE, FALSE, 0) ;
448 g_signal_connect(all_button, "toggled", G_CALLBACK(all_keypress_event_cb), logwin);
449 gtk_widget_show_all(all_button);
450 g_object_unref(pixbuf);
452 pref_label_new(hbox, _("Filter regexp"));
454 textbox = gtk_entry_new();
455 gtk_box_pack_start(GTK_BOX(hbox), textbox, FALSE, FALSE, 0);
456 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(textbox), GTK_ENTRY_ICON_SECONDARY, GTK_STOCK_CLEAR);
457 gtk_widget_show(textbox);
458 g_signal_connect(G_OBJECT(textbox), "activate",
459 G_CALLBACK(log_window_regexp_cb), logwin);
460 g_signal_connect(textbox, "icon-press", G_CALLBACK(filter_entry_icon_cb), logwin);
463 logwin->window = window;
464 logwin->scrolledwin = scrolledwin;
467 logwin->regexp_box = textbox;
468 lw->log_window = logwin->window;
472 static void log_window_init(LogWindow *logwin)
474 GtkTextBuffer *buffer;
476 g_assert(logwin != NULL);
478 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(logwin->text));
480 logwin->color_tags[LOG_NORMAL] = gtk_text_buffer_create_tag (buffer,
481 "black_foreground", "foreground", "black",
482 "family", "MonoSpace", NULL);
483 logwin->color_tags[LOG_MSG] = gtk_text_buffer_create_tag (buffer,
484 "blue_foreground", "foreground", "blue",
485 "family", "MonoSpace", NULL);
486 logwin->color_tags[LOG_WARN] = gtk_text_buffer_create_tag (buffer,
487 "orange_foreground", "foreground", "orange",
488 "family", "MonoSpace", NULL);
489 logwin->color_tags[LOG_ERROR] = gtk_text_buffer_create_tag (buffer,
490 "red_foreground", "foreground", "red",
491 "family", "MonoSpace", NULL);
494 static void log_window_show(LogWindow *logwin)
496 GtkTextView *text = GTK_TEXT_VIEW(logwin->text);
497 GtkTextBuffer *buffer;
501 buffer = gtk_text_view_get_buffer(text);
502 mark = gtk_text_buffer_get_mark(buffer, "end");
503 gtk_text_view_scroll_mark_onscreen(text, mark);
505 gtk_window_present(GTK_WINDOW(logwin->window));
507 log_window_append("", LOG_NORMAL); // to flush memorized lines
509 regexp = g_strdup(get_regexp());
512 gtk_entry_set_text(GTK_ENTRY(logwin->regexp_box), regexp);
517 void log_window_new(LayoutWindow *lw)
519 if (logwindow == NULL)
523 logwin = log_window_create(lw);
524 log_window_init(logwin);
528 log_window_show(logwindow);
531 typedef struct _LogMsg LogMsg;
538 static void log_window_insert_text(GtkTextBuffer *buffer, GtkTextIter *iter,
539 const gchar *text, GtkTextTag *tag)
543 if (!text || !*text) return;
545 str_utf8 = utf8_validate_or_convert(text);
546 gtk_text_buffer_insert_with_tags(buffer, iter, str_utf8, -1, tag, NULL);
550 void log_window_append(const gchar *str, LogType type)
553 GtkTextBuffer *buffer;
555 static GList *memory = NULL;
557 if (logwindow == NULL)
560 LogMsg *msg = g_new(LogMsg, 1);
562 msg->text = g_strdup(str);
565 memory = g_list_prepend(memory, msg);
567 while (g_list_length(memory) >= (guint)options->log_window_lines)
569 GList *work = g_list_last(memory);
570 LogMsg *oldest_msg = work->data;
572 g_free(oldest_msg->text);
573 memory = g_list_delete_link(memory, work);
579 text = GTK_TEXT_VIEW(logwindow->text);
580 buffer = gtk_text_view_get_buffer(text);
582 if (options->log_window_lines > 0 && logwindow->lines >= options->log_window_lines)
584 GtkTextIter start, end;
586 gtk_text_buffer_get_start_iter(buffer, &start);
588 gtk_text_iter_forward_lines(&end, logwindow->lines - options->log_window_lines);
589 gtk_text_buffer_delete(buffer, &start, &end);
592 gtk_text_buffer_get_end_iter(buffer, &iter);
595 GList *work = g_list_last(memory);
600 LogMsg *oldest_msg = work->data;
602 log_window_insert_text(buffer, &iter, oldest_msg->text,
603 logwindow->color_tags[oldest_msg->type]);
606 memory = g_list_delete_link(memory, work);
611 log_window_insert_text(buffer, &iter, str, logwindow->color_tags[type]);
613 if (!options->log_window.paused)
615 if (gtk_widget_get_visible(GTK_WIDGET(text)))
619 mark = gtk_text_buffer_get_mark(buffer, "end");
620 gtk_text_view_scroll_mark_onscreen(text, mark);
624 logwindow->lines = gtk_text_buffer_get_line_count(buffer);
626 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */