GTK2 no longer supported
[geeqie.git] / src / logwindow.c
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 "secure_save.h"
26 #include "ui_misc.h"
27 #include "window.h"
28
29 #include <gdk/gdkkeysyms.h>
30
31
32 typedef struct _LogWindow LogWindow;
33
34 struct _LogWindow
35 {
36         GtkWidget *window;
37         GtkWidget *scrolledwin;
38         GtkWidget *text;
39         GtkTextTag *color_tags[LOG_COUNT];
40         gint lines;
41         GtkWidget *regexp_box;
42         GtkWidget *bar;
43         GtkWidget *pause;
44         GtkWidget *wrap;
45         GtkWidget *timer_data;
46         GtkWidget *debug_level;
47         gint debug_value; /**< Not used */
48         GtkWidget *search_entry_box;
49         gboolean highlight_all;
50 };
51
52 typedef enum {
53         LOGWINDOW_SEARCH_BACKWARDS,
54         LOGWINDOW_SEARCH_FORWARDS
55 } LogWindowSearchDirection;
56
57 static LogWindow *logwindow = NULL;
58
59 static void hide_cb(GtkWidget *UNUSED(widget), LogWindow *UNUSED(logwin))
60 {
61 }
62
63 static gboolean key_pressed(GtkWidget *UNUSED(widget), GdkEventKey *event,
64                             LogWindow *logwin)
65 {
66         if (event && event->keyval == GDK_KEY_Escape)
67                 gtk_widget_hide(logwin->window);
68         return FALSE;
69 }
70
71
72 static void log_window_pause_cb(GtkWidget *UNUSED(widget), gpointer UNUSED(data))
73 {
74         options->log_window.paused = !options->log_window.paused;
75 }
76
77 static void log_window_line_wrap_cb(GtkWidget *UNUSED(widget), gpointer data)
78 {
79         LogWindow *logwin = data;
80
81         options->log_window.line_wrap = !options->log_window.line_wrap;
82
83         if (options->log_window.line_wrap)
84                 {
85                 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(logwin->text), GTK_WRAP_WORD);
86                 }
87         else
88                 {
89                 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(logwin->text), GTK_WRAP_NONE);
90                 }
91 }
92
93 static void log_window_timer_data_cb(GtkWidget *UNUSED(widget), gpointer UNUSED(data))
94 {
95         options->log_window.timer_data = !options->log_window.timer_data;
96 }
97
98 static void log_window_regexp_cb(GtkWidget *text_entry, gpointer UNUSED(data))
99 {
100         gchar *new_regexp;
101
102         new_regexp = g_strdup(gtk_entry_get_text(GTK_ENTRY(text_entry)));
103         set_regexp(new_regexp);
104         g_free(new_regexp);
105 }
106
107 static void remove_green_bg(LogWindow *logwin)
108 {
109         GtkTextIter start_find;
110         GtkTextIter start_match;
111         GtkTextIter end_match;
112         GtkTextBuffer *buffer;
113         GSList *list;
114         const gchar *text;
115         gchar *tag_name;
116         gint offset;
117
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);
121
122         while (gtk_text_iter_forward_search(&start_find, text, GTK_TEXT_SEARCH_VISIBLE_ONLY,  &start_match, &end_match, NULL))
123                 {
124                 list = gtk_text_iter_get_tags(&start_match);
125                 while (list)
126                         {
127                         g_object_get(list->data, "name", &tag_name, NULL);
128                         if (g_strcmp0(tag_name, "green_bg") == 0)
129                                 {
130                                 gtk_text_buffer_remove_tag_by_name(buffer, "green_bg", &start_match, &end_match);
131                                 }
132                         list = list->next;
133                         }
134                 g_slist_free(list);
135
136                 offset = gtk_text_iter_get_offset(&end_match);
137                 gtk_text_buffer_get_iter_at_offset(buffer, &start_find, offset);
138                 }
139 }
140
141 static void search_activate_event(GtkEntry *UNUSED(widget), LogWindow *logwin)
142 {
143         GtkTextIter start_find;
144         GtkTextIter start_match;
145         GtkTextIter end_match;
146         GtkTextBuffer *buffer;
147         GtkTextMark *cursor_mark;
148         GtkTextIter cursor_iter;
149         const gchar *text;
150         gint offset;
151
152         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(logwin->text));
153         text = gtk_entry_get_text(GTK_ENTRY(logwin->search_entry_box));
154
155         if (logwin->highlight_all)
156                 {
157                 gtk_text_buffer_get_start_iter(buffer, &start_find);
158
159                 while (gtk_text_iter_forward_search(&start_find, text, GTK_TEXT_SEARCH_VISIBLE_ONLY, &start_match, &end_match, NULL))
160                         {
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);
164                         }
165                 }
166         else
167                 {
168                 cursor_mark = gtk_text_buffer_get_insert(buffer);
169                 gtk_text_buffer_get_iter_at_mark(buffer, &cursor_iter, cursor_mark);
170
171                 if (gtk_text_iter_forward_search(&cursor_iter, text, GTK_TEXT_SEARCH_VISIBLE_ONLY, &start_match, &end_match, NULL))
172                         {
173                         gtk_text_buffer_apply_tag_by_name(buffer, "gray_bg", &start_match, &end_match);
174                         }
175                 }
176 }
177
178 static gboolean search_keypress_event(GtkWidget *UNUSED(widget), GdkEventKey *UNUSED(event), LogWindow *logwin, LogWindowSearchDirection direction)
179 {
180         GtkTextIter start_find;
181         GtkTextIter start_match;
182         GtkTextIter end_match;
183         GtkTextIter start_sel;
184         GtkTextIter end_sel;
185         const gchar *text;
186         GtkTextBuffer *buffer;
187         GtkTextMark *cursor_mark;
188         GtkTextIter cursor_iter;
189         gint offset;
190         gboolean match_found = FALSE;
191         gboolean selected;
192
193         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(logwin->text));
194         gtk_text_buffer_get_start_iter(buffer, &start_find);
195
196         text = gtk_entry_get_text(GTK_ENTRY(logwin->search_entry_box));
197         if (strlen(text) == 0)
198                 {
199                 selected = gtk_text_buffer_get_selection_bounds(buffer, &start_sel, &end_sel);
200                 if (selected)
201                         {
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);
204                         }
205                 }
206
207         if (logwin->highlight_all)
208                 {
209                 while (gtk_text_iter_forward_search(&start_find, text, GTK_TEXT_SEARCH_VISIBLE_ONLY, &start_match, &end_match, NULL))
210                         {
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);
214                         }
215                 }
216
217         cursor_mark = gtk_text_buffer_get_insert(buffer);
218         gtk_text_buffer_get_iter_at_mark(buffer, &cursor_iter, cursor_mark);
219
220         if (direction == LOGWINDOW_SEARCH_BACKWARDS)
221                 {
222                 match_found = gtk_text_iter_backward_search( &cursor_iter, text, GTK_TEXT_SEARCH_VISIBLE_ONLY,  &start_match, &end_match, NULL);
223                 }
224         else
225                 {
226                 match_found = gtk_text_iter_forward_search( &cursor_iter, text, GTK_TEXT_SEARCH_VISIBLE_ONLY,  &start_match, &end_match, NULL);
227                 }
228
229         if (match_found)
230                 {
231                 remove_green_bg(logwin);
232
233                 gtk_text_buffer_apply_tag_by_name(buffer, "green_bg",  &start_match, &end_match);
234
235                 if (direction == LOGWINDOW_SEARCH_BACKWARDS)
236                         {
237                         gtk_text_buffer_place_cursor(buffer, &start_match);
238                         }
239                 else
240                         {
241                         gtk_text_buffer_place_cursor(buffer, &end_match);
242                         }
243
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);
246         }
247
248         return FALSE;
249 }
250
251 static gboolean backwards_keypress_event_cb(GtkWidget *widget, GdkEventKey *event, LogWindow *logwin)
252 {
253         search_keypress_event(widget, event, logwin, LOGWINDOW_SEARCH_BACKWARDS);
254
255         return FALSE;
256 }
257
258 static gboolean forwards_keypress_event_cb(GtkWidget *widget, GdkEventKey *event, LogWindow *logwin)
259 {
260         search_keypress_event(widget, event, logwin, LOGWINDOW_SEARCH_FORWARDS);
261
262         return FALSE;
263 }
264
265 static gboolean all_keypress_event_cb(GtkToggleButton *widget, LogWindow *logwin)
266 {
267         logwin->highlight_all = gtk_toggle_button_get_active(widget);
268
269         return FALSE;
270 }
271
272 static gboolean debug_changed_cb(GtkSpinButton *widget, LogWindow *UNUSED(logwin))
273 {
274         set_debug_level((gtk_spin_button_get_value(widget)));
275
276         return FALSE;
277 }
278
279 static void search_entry_icon_cb(GtkEntry *UNUSED(entry), GtkEntryIconPosition pos, GdkEvent *UNUSED(event), gpointer userdata)
280 {
281         LogWindow *logwin = userdata;
282         GtkTextIter start_find;
283         GtkTextIter end_find;
284         GtkTextBuffer *buffer;
285
286         if (pos == GTK_ENTRY_ICON_SECONDARY)
287                 {
288                 gtk_entry_set_text(GTK_ENTRY(logwin->search_entry_box), "");
289
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);
295                 }
296 }
297
298 static void filter_entry_icon_cb(GtkEntry *entry, GtkEntryIconPosition UNUSED(pos), GdkEvent *UNUSED(event), gpointer UNUSED(userdata))
299 {
300         gtk_entry_set_text(entry, "");
301         set_regexp("");
302 }
303
304 static LogWindow *log_window_create(LayoutWindow *lw)
305 {
306         LogWindow *logwin;
307         GtkWidget *window;
308         GtkWidget *scrolledwin;
309         GtkWidget *text;
310         GtkTextBuffer *buffer;
311         GtkTextIter iter;
312         GtkWidget *win_vbox;
313         GtkWidget *textbox;
314         GtkWidget *hbox;
315         GtkWidget *label = NULL;
316         GtkWidget *search_box;
317         GtkWidget *backwards_button;
318         GtkWidget *forwards_button;
319         GtkWidget *all_button;
320         GtkIconTheme *theme;
321         GdkPixbuf *pixbuf;
322         GtkWidget *image = NULL;
323
324         logwin = g_new0(LogWindow, 1);
325
326         window = window_new(GTK_WINDOW_TOPLEVEL, "log", NULL, NULL, _("Log"));
327         DEBUG_NAME(window);
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);
331
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);
335
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);
343
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),
348                                             GTK_SHADOW_IN);
349
350         gtk_box_pack_start(GTK_BOX(win_vbox), scrolledwin, TRUE, TRUE, 0);
351         gtk_widget_show(scrolledwin);
352
353         text = gtk_text_view_new();
354         gtk_text_view_set_editable(GTK_TEXT_VIEW(text), FALSE);
355         if (options->log_window.line_wrap)
356                 {
357                 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text), GTK_WRAP_WORD);
358                 }
359         else
360                 {
361                 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text), GTK_WRAP_NONE);
362                 }
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);
368
369 #ifdef DEBUG
370         gtk_text_buffer_create_tag(buffer, "gray_bg", "background", "gray", NULL);
371         gtk_text_buffer_create_tag(buffer, "green_bg", "background", "#00FF00", NULL);
372
373         hbox = pref_box_new(win_vbox, FALSE, GTK_ORIENTATION_HORIZONTAL, PREF_PAD_SPACE);
374
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);
378
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);
386
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);
394
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)
401                 {
402                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(logwin->timer_data), TRUE);
403                 }
404         g_signal_connect(logwin->timer_data, "toggled", G_CALLBACK(log_window_timer_data_cb), logwin);
405         gtk_widget_show_all(logwin->timer_data);
406
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);
410
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);
420
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);
431
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);
441
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);
451
452         pref_label_new(hbox, _("Filter regexp"));
453
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);
461 #endif
462
463         logwin->window = window;
464         logwin->scrolledwin = scrolledwin;
465         logwin->text = text;
466         logwin->lines = 1;
467         logwin->regexp_box = textbox;
468         lw->log_window = logwin->window;
469         return logwin;
470 }
471
472 static void log_window_init(LogWindow *logwin)
473 {
474         GtkTextBuffer *buffer;
475
476         g_assert(logwin != NULL);
477
478         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(logwin->text));
479
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);
492 }
493
494 static void log_window_show(LogWindow *logwin)
495 {
496         GtkTextView *text = GTK_TEXT_VIEW(logwin->text);
497         GtkTextBuffer *buffer;
498         GtkTextMark *mark;
499         gchar *regexp;
500
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);
504
505         gtk_window_present(GTK_WINDOW(logwin->window));
506
507         log_window_append("", LOG_NORMAL); // to flush memorized lines
508
509         regexp = g_strdup(get_regexp());
510         if (regexp != NULL)
511                 {
512                 gtk_entry_set_text(GTK_ENTRY(logwin->regexp_box), regexp);
513                 g_free(regexp);
514                 }
515 }
516
517 void log_window_new(LayoutWindow *lw)
518 {
519         if (logwindow == NULL)
520                 {
521                 LogWindow *logwin;
522
523                 logwin = log_window_create(lw);
524                 log_window_init(logwin);
525                 logwindow = logwin;
526                 }
527
528         log_window_show(logwindow);
529 }
530
531 typedef struct _LogMsg LogMsg;
532
533 struct _LogMsg {
534         gchar *text;
535         LogType type;
536 };
537
538 static void log_window_insert_text(GtkTextBuffer *buffer, GtkTextIter *iter,
539                                    const gchar *text, GtkTextTag *tag)
540 {
541         gchar *str_utf8;
542
543         if (!text || !*text) return;
544
545         str_utf8 = utf8_validate_or_convert(text);
546         gtk_text_buffer_insert_with_tags(buffer, iter, str_utf8, -1, tag, NULL);
547         g_free(str_utf8);
548 }
549
550 void log_window_append(const gchar *str, LogType type)
551 {
552         GtkTextView *text;
553         GtkTextBuffer *buffer;
554         GtkTextIter iter;
555         static GList *memory = NULL;
556
557         if (logwindow == NULL)
558                 {
559                 if (*str) {
560                         LogMsg *msg = g_new(LogMsg, 1);
561
562                         msg->text = g_strdup(str);
563                         msg->type = type;
564
565                         memory = g_list_prepend(memory, msg);
566
567                         while (g_list_length(memory) >= (guint)options->log_window_lines)
568                                 {
569                                 GList *work = g_list_last(memory);
570                                 LogMsg *oldest_msg = work->data;
571
572                                 g_free(oldest_msg->text);
573                                 memory = g_list_delete_link(memory, work);
574                                 }
575                         }
576                 return;
577                 }
578
579         text = GTK_TEXT_VIEW(logwindow->text);
580         buffer = gtk_text_view_get_buffer(text);
581
582         if (options->log_window_lines > 0 && logwindow->lines >= options->log_window_lines)
583                 {
584                 GtkTextIter start, end;
585
586                 gtk_text_buffer_get_start_iter(buffer, &start);
587                 end = start;
588                 gtk_text_iter_forward_lines(&end, logwindow->lines - options->log_window_lines);
589                 gtk_text_buffer_delete(buffer, &start, &end);
590                 }
591
592         gtk_text_buffer_get_end_iter(buffer, &iter);
593
594         {
595         GList *work = g_list_last(memory);
596
597         while (work)
598                 {
599                 GList *prev;
600                 LogMsg *oldest_msg = work->data;
601
602                 log_window_insert_text(buffer, &iter, oldest_msg->text,
603                                                                         logwindow->color_tags[oldest_msg->type]);
604
605                 prev = work->prev;
606                 memory = g_list_delete_link(memory, work);
607                 work = prev;
608                 }
609         }
610
611         log_window_insert_text(buffer, &iter, str, logwindow->color_tags[type]);
612
613         if (!options->log_window.paused)
614                 {
615                 if (gtk_widget_get_visible(GTK_WIDGET(text)))
616                         {
617                         GtkTextMark *mark;
618
619                         mark = gtk_text_buffer_get_mark(buffer, "end");
620                         gtk_text_view_scroll_mark_onscreen(text, mark);
621                         }
622                 }
623
624         logwindow->lines = gtk_text_buffer_get_line_count(buffer);
625 }
626 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */