183380ee063b667bb274676563649bfdf9e64027
[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 key_pressed(GtkWidget *UNUSED(widget), GdkEventKey *event,
60                             LogWindow *logwin)
61 {
62         if (event && event->keyval == GDK_KEY_Escape)
63                 gtk_widget_hide(logwin->window);
64         return FALSE;
65 }
66
67
68 static void log_window_pause_cb(GtkWidget *UNUSED(widget), gpointer UNUSED(data))
69 {
70         options->log_window.paused = !options->log_window.paused;
71 }
72
73 static void log_window_line_wrap_cb(GtkWidget *UNUSED(widget), gpointer data)
74 {
75         LogWindow *logwin = data;
76
77         options->log_window.line_wrap = !options->log_window.line_wrap;
78
79         if (options->log_window.line_wrap)
80                 {
81                 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(logwin->text), GTK_WRAP_WORD);
82                 }
83         else
84                 {
85                 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(logwin->text), GTK_WRAP_NONE);
86                 }
87 }
88
89 static void log_window_timer_data_cb(GtkWidget *UNUSED(widget), gpointer UNUSED(data))
90 {
91         options->log_window.timer_data = !options->log_window.timer_data;
92 }
93
94 static void log_window_regexp_cb(GtkWidget *text_entry, gpointer UNUSED(data))
95 {
96         gchar *new_regexp;
97
98         new_regexp = g_strdup(gtk_entry_get_text(GTK_ENTRY(text_entry)));
99         set_regexp(new_regexp);
100         g_free(new_regexp);
101 }
102
103 static void remove_green_bg(LogWindow *logwin)
104 {
105         GtkTextIter start_find;
106         GtkTextIter start_match;
107         GtkTextIter end_match;
108         GtkTextBuffer *buffer;
109         GSList *list;
110         const gchar *text;
111         gchar *tag_name;
112         gint offset;
113
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);
117
118         while (gtk_text_iter_forward_search(&start_find, text, GTK_TEXT_SEARCH_VISIBLE_ONLY,  &start_match, &end_match, NULL))
119                 {
120                 list = gtk_text_iter_get_tags(&start_match);
121                 while (list)
122                         {
123                         g_object_get(list->data, "name", &tag_name, NULL);
124                         if (g_strcmp0(tag_name, "green_bg") == 0)
125                                 {
126                                 gtk_text_buffer_remove_tag_by_name(buffer, "green_bg", &start_match, &end_match);
127                                 }
128                         list = list->next;
129                         }
130                 g_slist_free(list);
131
132                 offset = gtk_text_iter_get_offset(&end_match);
133                 gtk_text_buffer_get_iter_at_offset(buffer, &start_find, offset);
134                 }
135 }
136
137 static void search_activate_event(GtkEntry *UNUSED(widget), LogWindow *logwin)
138 {
139         GtkTextIter start_find;
140         GtkTextIter start_match;
141         GtkTextIter end_match;
142         GtkTextBuffer *buffer;
143         GtkTextMark *cursor_mark;
144         GtkTextIter cursor_iter;
145         const gchar *text;
146         gint offset;
147
148         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(logwin->text));
149         text = gtk_entry_get_text(GTK_ENTRY(logwin->search_entry_box));
150
151         if (logwin->highlight_all)
152                 {
153                 gtk_text_buffer_get_start_iter(buffer, &start_find);
154
155                 while (gtk_text_iter_forward_search(&start_find, text, GTK_TEXT_SEARCH_VISIBLE_ONLY, &start_match, &end_match, NULL))
156                         {
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);
160                         }
161                 }
162         else
163                 {
164                 cursor_mark = gtk_text_buffer_get_insert(buffer);
165                 gtk_text_buffer_get_iter_at_mark(buffer, &cursor_iter, cursor_mark);
166
167                 if (gtk_text_iter_forward_search(&cursor_iter, text, GTK_TEXT_SEARCH_VISIBLE_ONLY, &start_match, &end_match, NULL))
168                         {
169                         gtk_text_buffer_apply_tag_by_name(buffer, "gray_bg", &start_match, &end_match);
170                         }
171                 }
172 }
173
174 static gboolean search_keypress_event(GtkWidget *UNUSED(widget), GdkEventKey *UNUSED(event), LogWindow *logwin, LogWindowSearchDirection direction)
175 {
176         GtkTextIter start_find;
177         GtkTextIter start_match;
178         GtkTextIter end_match;
179         GtkTextIter start_sel;
180         GtkTextIter end_sel;
181         const gchar *text;
182         GtkTextBuffer *buffer;
183         GtkTextMark *cursor_mark;
184         GtkTextIter cursor_iter;
185         gint offset;
186         gboolean match_found = FALSE;
187         gboolean selected;
188
189         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(logwin->text));
190         gtk_text_buffer_get_start_iter(buffer, &start_find);
191
192         text = gtk_entry_get_text(GTK_ENTRY(logwin->search_entry_box));
193         if (strlen(text) == 0)
194                 {
195                 selected = gtk_text_buffer_get_selection_bounds(buffer, &start_sel, &end_sel);
196                 if (selected)
197                         {
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);
200                         }
201                 }
202
203         if (logwin->highlight_all)
204                 {
205                 while (gtk_text_iter_forward_search(&start_find, text, GTK_TEXT_SEARCH_VISIBLE_ONLY, &start_match, &end_match, NULL))
206                         {
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);
210                         }
211                 }
212
213         cursor_mark = gtk_text_buffer_get_insert(buffer);
214         gtk_text_buffer_get_iter_at_mark(buffer, &cursor_iter, cursor_mark);
215
216         if (direction == LOGWINDOW_SEARCH_BACKWARDS)
217                 {
218                 match_found = gtk_text_iter_backward_search( &cursor_iter, text, GTK_TEXT_SEARCH_VISIBLE_ONLY,  &start_match, &end_match, NULL);
219                 }
220         else
221                 {
222                 match_found = gtk_text_iter_forward_search( &cursor_iter, text, GTK_TEXT_SEARCH_VISIBLE_ONLY,  &start_match, &end_match, NULL);
223                 }
224
225         if (match_found)
226                 {
227                 remove_green_bg(logwin);
228
229                 gtk_text_buffer_apply_tag_by_name(buffer, "green_bg",  &start_match, &end_match);
230
231                 if (direction == LOGWINDOW_SEARCH_BACKWARDS)
232                         {
233                         gtk_text_buffer_place_cursor(buffer, &start_match);
234                         }
235                 else
236                         {
237                         gtk_text_buffer_place_cursor(buffer, &end_match);
238                         }
239
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);
242         }
243
244         return FALSE;
245 }
246
247 static gboolean backwards_keypress_event_cb(GtkWidget *widget, GdkEventKey *event, LogWindow *logwin)
248 {
249         search_keypress_event(widget, event, logwin, LOGWINDOW_SEARCH_BACKWARDS);
250
251         return FALSE;
252 }
253
254 static gboolean forwards_keypress_event_cb(GtkWidget *widget, GdkEventKey *event, LogWindow *logwin)
255 {
256         search_keypress_event(widget, event, logwin, LOGWINDOW_SEARCH_FORWARDS);
257
258         return FALSE;
259 }
260
261 static gboolean all_keypress_event_cb(GtkToggleButton *widget, LogWindow *logwin)
262 {
263         logwin->highlight_all = gtk_toggle_button_get_active(widget);
264
265         return FALSE;
266 }
267
268 static gboolean debug_changed_cb(GtkSpinButton *widget, LogWindow *UNUSED(logwin))
269 {
270         set_debug_level((gtk_spin_button_get_value(widget)));
271
272         return FALSE;
273 }
274
275 static void search_entry_icon_cb(GtkEntry *UNUSED(entry), GtkEntryIconPosition pos, GdkEvent *UNUSED(event), gpointer userdata)
276 {
277         LogWindow *logwin = userdata;
278         GtkTextIter start_find;
279         GtkTextIter end_find;
280         GtkTextBuffer *buffer;
281
282         if (pos == GTK_ENTRY_ICON_SECONDARY)
283                 {
284                 gtk_entry_set_text(GTK_ENTRY(logwin->search_entry_box), "");
285
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);
291                 }
292 }
293
294 static void filter_entry_icon_cb(GtkEntry *entry, GtkEntryIconPosition UNUSED(pos), GdkEvent *UNUSED(event), gpointer UNUSED(userdata))
295 {
296         gtk_entry_set_text(entry, "");
297         set_regexp("");
298 }
299
300 static LogWindow *log_window_create(LayoutWindow *lw)
301 {
302         LogWindow *logwin;
303         GtkWidget *window;
304         GtkWidget *scrolledwin;
305         GtkWidget *text;
306         GtkTextBuffer *buffer;
307         GtkTextIter iter;
308         GtkWidget *win_vbox;
309         GtkWidget *textbox;
310         GtkWidget *hbox;
311         GtkWidget *label = NULL;
312         GtkWidget *search_box;
313         GtkWidget *backwards_button;
314         GtkWidget *forwards_button;
315         GtkWidget *all_button;
316         GtkIconTheme *theme;
317         GdkPixbuf *pixbuf;
318         GtkWidget *image = NULL;
319
320         logwin = g_new0(LogWindow, 1);
321
322         window = window_new(GTK_WINDOW_TOPLEVEL, "log", NULL, NULL, _("Log"));
323         DEBUG_NAME(window);
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);
327
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);
331
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);
339
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),
344                                             GTK_SHADOW_IN);
345
346         gtk_box_pack_start(GTK_BOX(win_vbox), scrolledwin, TRUE, TRUE, 0);
347         gtk_widget_show(scrolledwin);
348
349         text = gtk_text_view_new();
350         gtk_text_view_set_editable(GTK_TEXT_VIEW(text), FALSE);
351         if (options->log_window.line_wrap)
352                 {
353                 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text), GTK_WRAP_WORD);
354                 }
355         else
356                 {
357                 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text), GTK_WRAP_NONE);
358                 }
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);
364
365 #ifdef DEBUG
366         gtk_text_buffer_create_tag(buffer, "gray_bg", "background", "gray", NULL);
367         gtk_text_buffer_create_tag(buffer, "green_bg", "background", "#00FF00", NULL);
368
369         hbox = pref_box_new(win_vbox, FALSE, GTK_ORIENTATION_HORIZONTAL, PREF_PAD_SPACE);
370
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);
374
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);
382
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);
390
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)
397                 {
398                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(logwin->timer_data), TRUE);
399                 }
400         g_signal_connect(logwin->timer_data, "toggled", G_CALLBACK(log_window_timer_data_cb), logwin);
401         gtk_widget_show_all(logwin->timer_data);
402
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);
406
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);
416
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);
427
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);
437
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);
447
448         pref_label_new(hbox, _("Filter regexp"));
449
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);
457 #endif
458
459         logwin->window = window;
460         logwin->scrolledwin = scrolledwin;
461         logwin->text = text;
462         logwin->lines = 1;
463         logwin->regexp_box = textbox;
464         lw->log_window = logwin->window;
465         return logwin;
466 }
467
468 static void log_window_init(LogWindow *logwin)
469 {
470         GtkTextBuffer *buffer;
471
472         g_assert(logwin != NULL);
473
474         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(logwin->text));
475
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);
488 }
489
490 static void log_window_show(LogWindow *logwin)
491 {
492         GtkTextView *text = GTK_TEXT_VIEW(logwin->text);
493         GtkTextBuffer *buffer;
494         GtkTextMark *mark;
495         gchar *regexp;
496
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);
500
501         gtk_window_present(GTK_WINDOW(logwin->window));
502
503         log_window_append("", LOG_NORMAL); // to flush memorized lines
504
505         regexp = g_strdup(get_regexp());
506         if (regexp != NULL)
507                 {
508                 gtk_entry_set_text(GTK_ENTRY(logwin->regexp_box), regexp);
509                 g_free(regexp);
510                 }
511 }
512
513 void log_window_new(LayoutWindow *lw)
514 {
515         if (logwindow == NULL)
516                 {
517                 LogWindow *logwin;
518
519                 logwin = log_window_create(lw);
520                 log_window_init(logwin);
521                 logwindow = logwin;
522                 }
523
524         log_window_show(logwindow);
525 }
526
527 typedef struct _LogMsg LogMsg;
528
529 struct _LogMsg {
530         gchar *text;
531         LogType type;
532 };
533
534 static void log_window_insert_text(GtkTextBuffer *buffer, GtkTextIter *iter,
535                                    const gchar *text, GtkTextTag *tag)
536 {
537         gchar *str_utf8;
538
539         if (!text || !*text) return;
540
541         str_utf8 = utf8_validate_or_convert(text);
542         gtk_text_buffer_insert_with_tags(buffer, iter, str_utf8, -1, tag, NULL);
543         g_free(str_utf8);
544 }
545
546 void log_window_append(const gchar *str, LogType type)
547 {
548         GtkTextView *text;
549         GtkTextBuffer *buffer;
550         GtkTextIter iter;
551         static GList *memory = NULL;
552
553         if (logwindow == NULL)
554                 {
555                 if (*str) {
556                         LogMsg *msg = g_new(LogMsg, 1);
557
558                         msg->text = g_strdup(str);
559                         msg->type = type;
560
561                         memory = g_list_prepend(memory, msg);
562
563                         while (g_list_length(memory) >= (guint)options->log_window_lines)
564                                 {
565                                 GList *work = g_list_last(memory);
566                                 LogMsg *oldest_msg = work->data;
567
568                                 g_free(oldest_msg->text);
569                                 memory = g_list_delete_link(memory, work);
570                                 }
571                         }
572                 return;
573                 }
574
575         text = GTK_TEXT_VIEW(logwindow->text);
576         buffer = gtk_text_view_get_buffer(text);
577
578         if (options->log_window_lines > 0 && logwindow->lines >= options->log_window_lines)
579                 {
580                 GtkTextIter start, end;
581
582                 gtk_text_buffer_get_start_iter(buffer, &start);
583                 end = start;
584                 gtk_text_iter_forward_lines(&end, logwindow->lines - options->log_window_lines);
585                 gtk_text_buffer_delete(buffer, &start, &end);
586                 }
587
588         gtk_text_buffer_get_end_iter(buffer, &iter);
589
590         {
591         GList *work = g_list_last(memory);
592
593         while (work)
594                 {
595                 GList *prev;
596                 LogMsg *oldest_msg = work->data;
597
598                 log_window_insert_text(buffer, &iter, oldest_msg->text,
599                                                                         logwindow->color_tags[oldest_msg->type]);
600
601                 prev = work->prev;
602                 memory = g_list_delete_link(memory, work);
603                 work = prev;
604                 }
605         }
606
607         log_window_insert_text(buffer, &iter, str, logwindow->color_tags[type]);
608
609         if (!options->log_window.paused)
610                 {
611                 if (gtk_widget_get_visible(GTK_WIDGET(text)))
612                         {
613                         GtkTextMark *mark;
614
615                         mark = gtk_text_buffer_get_mark(buffer, "end");
616                         gtk_text_view_scroll_mark_onscreen(text, mark);
617                         }
618                 }
619
620         logwindow->lines = gtk_text_buffer_get_line_count(buffer);
621 }
622 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */