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