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"
28 typedef struct _LogWindow LogWindow;
33 GtkWidget *scrolledwin;
35 GtkTextTag *color_tags[LOG_COUNT];
37 GtkWidget *regexp_box;
41 GtkWidget *timer_data;
42 GtkWidget *debug_level;
43 gint debug_value; /**< Not used */
44 GtkWidget *search_entry_box;
45 gboolean highlight_all;
49 LOGWINDOW_SEARCH_BACKWARDS,
50 LOGWINDOW_SEARCH_FORWARDS
51 } LogWindowSearchDirection;
53 static LogWindow *logwindow = NULL;
55 static void hide_cb(GtkWidget *UNUSED(widget), LogWindow *UNUSED(logwin))
59 static gboolean key_pressed(GtkWidget *UNUSED(widget), GdkEventKey *event,
62 if (event && event->keyval == GDK_KEY_Escape)
63 gtk_widget_hide(logwin->window);
68 static void log_window_pause_cb(GtkWidget *UNUSED(widget), gpointer UNUSED(data))
70 options->log_window.paused = !options->log_window.paused;
73 static void log_window_line_wrap_cb(GtkWidget *UNUSED(widget), gpointer data)
75 LogWindow *logwin = data;
77 options->log_window.line_wrap = !options->log_window.line_wrap;
79 if (options->log_window.line_wrap)
81 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(logwin->text), GTK_WRAP_WORD);
85 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(logwin->text), GTK_WRAP_NONE);
89 static void log_window_timer_data_cb(GtkWidget *UNUSED(widget), gpointer UNUSED(data))
91 options->log_window.timer_data = !options->log_window.timer_data;
94 static void log_window_regexp_cb(GtkWidget *text_entry, gpointer UNUSED(data))
98 new_regexp = g_strdup(gtk_entry_get_text(GTK_ENTRY(text_entry)));
99 set_regexp(new_regexp);
103 static void remove_green_bg(LogWindow *logwin)
105 GtkTextIter start_find;
106 GtkTextIter start_match;
107 GtkTextIter end_match;
108 GtkTextBuffer *buffer;
114 text = gtk_entry_get_text(GTK_ENTRY(logwin->search_entry_box));
115 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(logwin->text));
116 gtk_text_buffer_get_start_iter(buffer, &start_find);
118 while (gtk_text_iter_forward_search(&start_find, text, GTK_TEXT_SEARCH_VISIBLE_ONLY, &start_match, &end_match, NULL))
120 list = gtk_text_iter_get_tags(&start_match);
123 g_object_get(list->data, "name", &tag_name, NULL);
124 if (g_strcmp0(tag_name, "green_bg") == 0)
126 gtk_text_buffer_remove_tag_by_name(buffer, "green_bg", &start_match, &end_match);
132 offset = gtk_text_iter_get_offset(&end_match);
133 gtk_text_buffer_get_iter_at_offset(buffer, &start_find, offset);
137 static void search_activate_event(GtkEntry *UNUSED(widget), LogWindow *logwin)
139 GtkTextIter start_find;
140 GtkTextIter start_match;
141 GtkTextIter end_match;
142 GtkTextBuffer *buffer;
143 GtkTextMark *cursor_mark;
144 GtkTextIter cursor_iter;
148 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(logwin->text));
149 text = gtk_entry_get_text(GTK_ENTRY(logwin->search_entry_box));
151 if (logwin->highlight_all)
153 gtk_text_buffer_get_start_iter(buffer, &start_find);
155 while (gtk_text_iter_forward_search(&start_find, text, GTK_TEXT_SEARCH_VISIBLE_ONLY, &start_match, &end_match, NULL))
157 gtk_text_buffer_apply_tag_by_name(buffer, "gray_bg", &start_match, &end_match);
158 offset = gtk_text_iter_get_offset(&end_match);
159 gtk_text_buffer_get_iter_at_offset(buffer, &start_find, offset);
164 cursor_mark = gtk_text_buffer_get_insert(buffer);
165 gtk_text_buffer_get_iter_at_mark(buffer, &cursor_iter, cursor_mark);
167 if (gtk_text_iter_forward_search(&cursor_iter, text, GTK_TEXT_SEARCH_VISIBLE_ONLY, &start_match, &end_match, NULL))
169 gtk_text_buffer_apply_tag_by_name(buffer, "gray_bg", &start_match, &end_match);
174 static gboolean search_keypress_event(GtkWidget *UNUSED(widget), GdkEventKey *UNUSED(event), LogWindow *logwin, LogWindowSearchDirection direction)
176 GtkTextIter start_find;
177 GtkTextIter start_match;
178 GtkTextIter end_match;
179 GtkTextIter start_sel;
182 GtkTextBuffer *buffer;
183 GtkTextMark *cursor_mark;
184 GtkTextIter cursor_iter;
186 gboolean match_found = FALSE;
189 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(logwin->text));
190 gtk_text_buffer_get_start_iter(buffer, &start_find);
192 text = gtk_entry_get_text(GTK_ENTRY(logwin->search_entry_box));
193 if (strlen(text) == 0)
195 selected = gtk_text_buffer_get_selection_bounds(buffer, &start_sel, &end_sel);
198 text = gtk_text_buffer_get_text(buffer, &start_sel, &end_sel, FALSE);
199 gtk_entry_set_text(GTK_ENTRY(logwin->search_entry_box), text);
203 if (logwin->highlight_all)
205 while (gtk_text_iter_forward_search(&start_find, text, GTK_TEXT_SEARCH_VISIBLE_ONLY, &start_match, &end_match, NULL))
207 gtk_text_buffer_apply_tag_by_name(buffer, "gray_bg", &start_match, &end_match);
208 offset = gtk_text_iter_get_offset(&end_match);
209 gtk_text_buffer_get_iter_at_offset(buffer, &start_find, offset);
213 cursor_mark = gtk_text_buffer_get_insert(buffer);
214 gtk_text_buffer_get_iter_at_mark(buffer, &cursor_iter, cursor_mark);
216 if (direction == LOGWINDOW_SEARCH_BACKWARDS)
218 match_found = gtk_text_iter_backward_search( &cursor_iter, text, GTK_TEXT_SEARCH_VISIBLE_ONLY, &start_match, &end_match, NULL);
222 match_found = gtk_text_iter_forward_search( &cursor_iter, text, GTK_TEXT_SEARCH_VISIBLE_ONLY, &start_match, &end_match, NULL);
227 remove_green_bg(logwin);
229 gtk_text_buffer_apply_tag_by_name(buffer, "green_bg", &start_match, &end_match);
231 if (direction == LOGWINDOW_SEARCH_BACKWARDS)
233 gtk_text_buffer_place_cursor(buffer, &start_match);
237 gtk_text_buffer_place_cursor(buffer, &end_match);
240 cursor_mark = gtk_text_buffer_get_insert(buffer);
241 gtk_text_view_scroll_to_mark(GTK_TEXT_VIEW(logwin->text), cursor_mark, 0.2, FALSE, 0.0, 0.0);
247 static gboolean backwards_keypress_event_cb(GtkWidget *widget, GdkEventKey *event, LogWindow *logwin)
249 search_keypress_event(widget, event, logwin, LOGWINDOW_SEARCH_BACKWARDS);
254 static gboolean forwards_keypress_event_cb(GtkWidget *widget, GdkEventKey *event, LogWindow *logwin)
256 search_keypress_event(widget, event, logwin, LOGWINDOW_SEARCH_FORWARDS);
261 static gboolean all_keypress_event_cb(GtkToggleButton *widget, LogWindow *logwin)
263 logwin->highlight_all = gtk_toggle_button_get_active(widget);
268 static gboolean debug_changed_cb(GtkSpinButton *widget, LogWindow *UNUSED(logwin))
270 set_debug_level((gtk_spin_button_get_value(widget)));
275 static void search_entry_icon_cb(GtkEntry *UNUSED(entry), GtkEntryIconPosition pos, GdkEvent *UNUSED(event), gpointer userdata)
277 LogWindow *logwin = userdata;
278 GtkTextIter start_find;
279 GtkTextIter end_find;
280 GtkTextBuffer *buffer;
282 if (pos == GTK_ENTRY_ICON_SECONDARY)
284 gtk_entry_set_text(GTK_ENTRY(logwin->search_entry_box), "");
286 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(logwin->text));
287 gtk_text_buffer_get_start_iter(buffer, &start_find);
288 gtk_text_buffer_get_end_iter(buffer, &end_find);
289 gtk_text_buffer_remove_tag_by_name(buffer, "gray_bg", &start_find, &end_find);
290 gtk_text_buffer_remove_tag_by_name(buffer, "green_bg", &start_find, &end_find);
294 static void filter_entry_icon_cb(GtkEntry *entry, GtkEntryIconPosition UNUSED(pos), GdkEvent *UNUSED(event), gpointer UNUSED(userdata))
296 gtk_entry_set_text(entry, "");
300 static LogWindow *log_window_create(LayoutWindow *lw)
304 GtkWidget *scrolledwin;
306 GtkTextBuffer *buffer;
311 GtkWidget *label = NULL;
312 GtkWidget *search_box;
313 GtkWidget *backwards_button;
314 GtkWidget *forwards_button;
315 GtkWidget *all_button;
318 GtkWidget *image = NULL;
320 logwin = g_new0(LogWindow, 1);
322 window = window_new(GTK_WINDOW_TOPLEVEL, "log", NULL, NULL, _("Log"));
324 win_vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, PREF_PAD_SPACE);
325 gtk_container_add(GTK_CONTAINER(window), win_vbox);
326 gtk_widget_show(win_vbox);
328 gtk_window_resize(GTK_WINDOW(window), lw->options.log_window.w,
329 lw->options.log_window.h);
330 gtk_window_move(GTK_WINDOW(window), lw->options.log_window.x, lw->options.log_window.y);
332 g_signal_connect(G_OBJECT(window), "delete_event",
333 G_CALLBACK(gtk_widget_hide_on_delete), NULL);
334 g_signal_connect(G_OBJECT(window), "key_press_event",
335 G_CALLBACK(key_pressed), logwin);
336 g_signal_connect(G_OBJECT(window), "hide",
337 G_CALLBACK(hide_cb), logwin);
338 gtk_widget_realize(window);
340 scrolledwin = gtk_scrolled_window_new(NULL, NULL);
341 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
342 GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
343 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwin),
346 gtk_box_pack_start(GTK_BOX(win_vbox), scrolledwin, TRUE, TRUE, 0);
347 gtk_widget_show(scrolledwin);
349 text = gtk_text_view_new();
350 gtk_text_view_set_editable(GTK_TEXT_VIEW(text), FALSE);
351 if (options->log_window.line_wrap)
353 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text), GTK_WRAP_WORD);
357 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text), GTK_WRAP_NONE);
359 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
360 gtk_text_buffer_get_start_iter(buffer, &iter);
361 gtk_text_buffer_create_mark(buffer, "end", &iter, FALSE);
362 gtk_container_add(GTK_CONTAINER(scrolledwin), text);
363 gtk_widget_show(text);
366 gtk_text_buffer_create_tag(buffer, "gray_bg", "background", "gray", NULL);
367 gtk_text_buffer_create_tag(buffer, "green_bg", "background", "#00FF00", NULL);
369 hbox = pref_box_new(win_vbox, FALSE, GTK_ORIENTATION_HORIZONTAL, PREF_PAD_SPACE);
371 gtk_widget_show(hbox);
372 logwin->debug_level = pref_spin_new_int(hbox, _("Debug level:"), NULL, 0, 4, 1, get_debug_level(), &logwin->debug_value);
373 g_signal_connect(logwin->debug_level, "value-changed", G_CALLBACK(debug_changed_cb), logwin);
375 logwin->pause = gtk_toggle_button_new();
376 label = gtk_label_new("Pause");
377 gtk_widget_set_tooltip_text(GTK_WIDGET(logwin->pause), _("Pause scrolling"));
378 gtk_container_add(GTK_CONTAINER(logwin->pause), label) ;
379 gtk_box_pack_start(GTK_BOX(hbox),logwin->pause, FALSE, FALSE, 0) ;
380 g_signal_connect(logwin->pause, "toggled", G_CALLBACK(log_window_pause_cb), logwin);
381 gtk_widget_show_all(logwin->pause);
383 logwin->wrap = gtk_toggle_button_new();
384 label = gtk_label_new("Wrap");
385 gtk_widget_set_tooltip_text(GTK_WIDGET(logwin->wrap), _("Enable line wrap"));
386 gtk_container_add(GTK_CONTAINER(logwin->wrap), label) ;
387 gtk_box_pack_start(GTK_BOX(hbox),logwin->wrap, FALSE, FALSE, 0) ;
388 g_signal_connect(logwin->wrap, "toggled", G_CALLBACK(log_window_line_wrap_cb), logwin);
389 gtk_widget_show_all(logwin->wrap);
391 logwin->timer_data = gtk_toggle_button_new();
392 label = gtk_label_new("Timer");
393 gtk_widget_set_tooltip_text(GTK_WIDGET(logwin->timer_data), _("Enable timer data"));
394 gtk_container_add(GTK_CONTAINER(logwin->timer_data), label) ;
395 gtk_box_pack_start(GTK_BOX(hbox),logwin->timer_data, FALSE, FALSE, 0) ;
396 if (options->log_window.timer_data)
398 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(logwin->timer_data), TRUE);
400 g_signal_connect(logwin->timer_data, "toggled", G_CALLBACK(log_window_timer_data_cb), logwin);
401 gtk_widget_show_all(logwin->timer_data);
403 search_box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
404 gtk_container_add(GTK_CONTAINER(hbox), search_box);
405 gtk_widget_show(search_box);
407 logwin->search_entry_box = gtk_entry_new();
408 gtk_box_pack_start(GTK_BOX(search_box), logwin->search_entry_box, FALSE, FALSE, 0);
409 gtk_widget_show(logwin->search_entry_box);
410 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(logwin->search_entry_box), GTK_ENTRY_ICON_PRIMARY, GTK_STOCK_FIND);
411 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(logwin->search_entry_box), GTK_ENTRY_ICON_SECONDARY, GTK_STOCK_CLEAR);
412 gtk_widget_show(search_box);
413 gtk_widget_set_tooltip_text(logwin->search_entry_box, _("Search for text in log window"));
414 g_signal_connect(logwin->search_entry_box, "icon-press", G_CALLBACK(search_entry_icon_cb), logwin);
415 g_signal_connect(logwin->search_entry_box, "activate", G_CALLBACK(search_activate_event), logwin);
417 theme = gtk_icon_theme_get_default();
418 pixbuf = gtk_icon_theme_load_icon(theme, "pan-up-symbolic", 20, GTK_ICON_LOOKUP_GENERIC_FALLBACK, NULL);
419 image = gtk_image_new_from_pixbuf(pixbuf);
420 backwards_button = gtk_button_new();
421 gtk_button_set_image(GTK_BUTTON(backwards_button), GTK_WIDGET(image));
422 gtk_widget_set_tooltip_text(backwards_button, _("Search backwards"));
423 gtk_box_pack_start(GTK_BOX(search_box), backwards_button, FALSE, FALSE, 0);
424 gtk_widget_show(backwards_button);
425 g_signal_connect(backwards_button, "button_release_event", G_CALLBACK(backwards_keypress_event_cb), logwin);
426 g_object_unref(pixbuf);
428 pixbuf = gtk_icon_theme_load_icon(theme, "pan-down-symbolic", 20, GTK_ICON_LOOKUP_GENERIC_FALLBACK, NULL);
429 image = gtk_image_new_from_pixbuf(pixbuf);
430 forwards_button = gtk_button_new();
431 gtk_button_set_image(GTK_BUTTON(forwards_button), GTK_WIDGET(image));
432 gtk_widget_set_tooltip_text(forwards_button, _("Search forwards"));
433 gtk_box_pack_start(GTK_BOX(search_box), forwards_button, FALSE, FALSE, 0);
434 gtk_widget_show(forwards_button);
435 g_signal_connect(forwards_button, "button_release_event", G_CALLBACK(forwards_keypress_event_cb), logwin);
436 g_object_unref(pixbuf);
438 pixbuf = gtk_icon_theme_load_icon(theme, "edit-select-all-symbolic", 20, GTK_ICON_LOOKUP_GENERIC_FALLBACK, NULL);
439 image = gtk_image_new_from_pixbuf(pixbuf);
440 all_button = gtk_toggle_button_new();
441 gtk_button_set_image(GTK_BUTTON(all_button), GTK_WIDGET(image));
442 gtk_widget_set_tooltip_text(GTK_WIDGET(all_button), _("Highlight all"));
443 gtk_box_pack_start(GTK_BOX(search_box), all_button, FALSE, FALSE, 0) ;
444 g_signal_connect(all_button, "toggled", G_CALLBACK(all_keypress_event_cb), logwin);
445 gtk_widget_show_all(all_button);
446 g_object_unref(pixbuf);
448 pref_label_new(hbox, _("Filter regexp"));
450 textbox = gtk_entry_new();
451 gtk_box_pack_start(GTK_BOX(hbox), textbox, FALSE, FALSE, 0);
452 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(textbox), GTK_ENTRY_ICON_SECONDARY, GTK_STOCK_CLEAR);
453 gtk_widget_show(textbox);
454 g_signal_connect(G_OBJECT(textbox), "activate",
455 G_CALLBACK(log_window_regexp_cb), logwin);
456 g_signal_connect(textbox, "icon-press", G_CALLBACK(filter_entry_icon_cb), logwin);
459 logwin->window = window;
460 logwin->scrolledwin = scrolledwin;
463 logwin->regexp_box = textbox;
464 lw->log_window = logwin->window;
468 static void log_window_init(LogWindow *logwin)
470 GtkTextBuffer *buffer;
472 g_assert(logwin != NULL);
474 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(logwin->text));
476 logwin->color_tags[LOG_NORMAL] = gtk_text_buffer_create_tag (buffer,
477 "black_foreground", "foreground", "black",
478 "family", "MonoSpace", NULL);
479 logwin->color_tags[LOG_MSG] = gtk_text_buffer_create_tag (buffer,
480 "blue_foreground", "foreground", "blue",
481 "family", "MonoSpace", NULL);
482 logwin->color_tags[LOG_WARN] = gtk_text_buffer_create_tag (buffer,
483 "orange_foreground", "foreground", "orange",
484 "family", "MonoSpace", NULL);
485 logwin->color_tags[LOG_ERROR] = gtk_text_buffer_create_tag (buffer,
486 "red_foreground", "foreground", "red",
487 "family", "MonoSpace", NULL);
490 static void log_window_show(LogWindow *logwin)
492 GtkTextView *text = GTK_TEXT_VIEW(logwin->text);
493 GtkTextBuffer *buffer;
497 buffer = gtk_text_view_get_buffer(text);
498 mark = gtk_text_buffer_get_mark(buffer, "end");
499 gtk_text_view_scroll_mark_onscreen(text, mark);
501 gtk_window_present(GTK_WINDOW(logwin->window));
503 log_window_append("", LOG_NORMAL); // to flush memorized lines
505 regexp = g_strdup(get_regexp());
508 gtk_entry_set_text(GTK_ENTRY(logwin->regexp_box), regexp);
513 void log_window_new(LayoutWindow *lw)
515 if (logwindow == NULL)
519 logwin = log_window_create(lw);
520 log_window_init(logwin);
524 log_window_show(logwindow);
527 typedef struct _LogMsg LogMsg;
534 static void log_window_insert_text(GtkTextBuffer *buffer, GtkTextIter *iter,
535 const gchar *text, GtkTextTag *tag)
539 if (!text || !*text) return;
541 str_utf8 = utf8_validate_or_convert(text);
542 gtk_text_buffer_insert_with_tags(buffer, iter, str_utf8, -1, tag, NULL);
546 void log_window_append(const gchar *str, LogType type)
549 GtkTextBuffer *buffer;
551 static GList *memory = NULL;
553 if (logwindow == NULL)
556 LogMsg *msg = g_new(LogMsg, 1);
558 msg->text = g_strdup(str);
561 memory = g_list_prepend(memory, msg);
563 while (g_list_length(memory) >= (guint)options->log_window_lines)
565 GList *work = g_list_last(memory);
566 LogMsg *oldest_msg = work->data;
568 g_free(oldest_msg->text);
569 memory = g_list_delete_link(memory, work);
575 text = GTK_TEXT_VIEW(logwindow->text);
576 buffer = gtk_text_view_get_buffer(text);
578 if (options->log_window_lines > 0 && logwindow->lines >= options->log_window_lines)
580 GtkTextIter start, end;
582 gtk_text_buffer_get_start_iter(buffer, &start);
584 gtk_text_iter_forward_lines(&end, logwindow->lines - options->log_window_lines);
585 gtk_text_buffer_delete(buffer, &start, &end);
588 gtk_text_buffer_get_end_iter(buffer, &iter);
591 GList *work = g_list_last(memory);
596 LogMsg *oldest_msg = work->data;
598 log_window_insert_text(buffer, &iter, oldest_msg->text,
599 logwindow->color_tags[oldest_msg->type]);
602 memory = g_list_delete_link(memory, work);
607 log_window_insert_text(buffer, &iter, str, logwindow->color_tags[type]);
609 if (!options->log_window.paused)
611 if (gtk_widget_get_visible(GTK_WIDGET(text)))
615 mark = gtk_text_buffer_get_mark(buffer, "end");
616 gtk_text_view_scroll_mark_onscreen(text, mark);
620 logwindow->lines = gtk_text_buffer_get_line_count(buffer);
622 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */