Fix compatibility problems with log window
[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
40         GdkColor colors[LOG_COUNT];
41
42         guint lines;
43         GtkWidget *regexp_box;
44         GtkWidget *bar;
45         GtkWidget *pause;
46         GtkWidget *wrap;
47         GtkWidget *debug_level;
48 };
49
50 typedef struct _LogDef LogDef;
51 struct _LogDef
52 {
53         LogType type;
54         const gchar *tag;
55         const gchar *color;
56 };
57
58 /* Keep LogType order !! */
59 static LogDef logdefs[LOG_COUNT] = {
60         { LOG_NORMAL,   "normal",       "black"  },
61         { LOG_MSG,      "message",      "blue"   },
62         { LOG_WARN,     "warning",      "orange" },
63         { LOG_ERROR,    "error",        "red"    },
64 };
65
66 static LogWindow *logwindow = NULL;
67
68 static void hide_cb(GtkWidget *widget, LogWindow *logwin)
69 {
70 }
71
72 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event,
73                             LogWindow *logwin)
74 {
75         if (event && event->keyval == GDK_KEY_Escape)
76                 gtk_widget_hide(logwin->window);
77         return FALSE;
78 }
79
80
81 static void log_window_pause_cb(GtkWidget *widget, gpointer data)
82 {
83         options->log_window.paused = !options->log_window.paused;
84 }
85
86 static void log_window_line_wrap_cb(GtkWidget *widget, gpointer data)
87 {
88         LogWindow *logwin = data;
89
90         options->log_window.line_wrap = !options->log_window.line_wrap;
91
92         if (options->log_window.line_wrap)
93                 {
94                 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(logwin->text), GTK_WRAP_WORD);
95                 }
96         else
97                 {
98                 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(logwin->text), GTK_WRAP_NONE);
99                 }
100 }
101
102 static void log_window_regexp_cb(GtkWidget *text_entry, gpointer data)
103 {
104         gchar *new_regexp;
105
106         new_regexp = g_strdup(gtk_entry_get_text(GTK_ENTRY(text_entry)));
107         set_regexp(new_regexp);
108         g_free(new_regexp);
109 }
110
111 static void log_window_debug_spin_cb(GtkSpinButton *debug_level, gpointer data)
112 {
113         set_debug_level(gtk_spin_button_get_value(debug_level));
114 }
115
116 static LogWindow *log_window_create(LayoutWindow *lw)
117 {
118         LogWindow *logwin;
119         GtkWidget *window;
120         GtkWidget *scrolledwin;
121         GtkWidget *text;
122         GtkTextBuffer *buffer;
123         GtkTextIter iter;
124         GtkWidget *button;
125         GtkWidget *win_vbox;
126         GtkWidget *textbox;
127         GtkWidget *hbox;
128
129         logwin = g_new0(LogWindow, 1);
130
131         window = window_new(GTK_WINDOW_TOPLEVEL, "log", NULL, NULL, _("Log"));
132         win_vbox = gtk_vbox_new(FALSE, PREF_PAD_SPACE);
133         gtk_container_add(GTK_CONTAINER(window), win_vbox);
134         gtk_widget_show(win_vbox);
135
136         gtk_window_resize(GTK_WINDOW(window), lw->options.log_window.w,
137                                                                                         lw->options.log_window.h);
138         gtk_window_move(GTK_WINDOW(window), lw->options.log_window.x, lw->options.log_window.y);
139
140         g_signal_connect(G_OBJECT(window), "delete_event",
141                          G_CALLBACK(gtk_widget_hide_on_delete), NULL);
142         g_signal_connect(G_OBJECT(window), "key_press_event",
143                          G_CALLBACK(key_pressed), logwin);
144         g_signal_connect(G_OBJECT(window), "hide",
145                          G_CALLBACK(hide_cb), logwin);
146         gtk_widget_realize(window);
147
148         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
149         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
150                                        GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
151         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwin),
152                                             GTK_SHADOW_IN);
153
154         gtk_box_pack_start(GTK_BOX(win_vbox), scrolledwin, TRUE, TRUE, 0);
155         gtk_widget_show(scrolledwin);
156
157         hbox = pref_box_new(win_vbox, FALSE, GTK_ORIENTATION_HORIZONTAL, PREF_PAD_SPACE);
158
159         gtk_widget_show(hbox);
160         logwin->debug_level = pref_spin_new_mnemonic(hbox, _("Debug level:"), NULL,
161                           0, 4, 1, 1, get_debug_level(),G_CALLBACK(log_window_debug_spin_cb),
162                           logwin->debug_level );
163
164         logwin->pause = pref_button_new(hbox, NULL, "Pause", FALSE,
165                                            G_CALLBACK(log_window_pause_cb), NULL);
166
167         logwin->wrap = pref_button_new(hbox, NULL, "Line wrap", FALSE,
168                                            G_CALLBACK(log_window_line_wrap_cb), logwin);
169
170         pref_label_new(hbox, "Filter regexp");
171
172         textbox = gtk_entry_new();
173         gtk_container_add(GTK_CONTAINER(hbox), textbox);
174         gtk_widget_show(textbox);
175         g_signal_connect(G_OBJECT(textbox), "activate",
176                          G_CALLBACK(log_window_regexp_cb), logwin);
177
178         text = gtk_text_view_new();
179         gtk_text_view_set_editable(GTK_TEXT_VIEW(text), FALSE);
180         gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text), GTK_WRAP_WORD);
181         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
182         gtk_text_buffer_get_start_iter(buffer, &iter);
183         gtk_text_buffer_create_mark(buffer, "end", &iter, FALSE);
184         gtk_container_add(GTK_CONTAINER(scrolledwin), text);
185         gtk_widget_show(text);
186
187         logwin->window = window;
188         logwin->scrolledwin = scrolledwin;
189         logwin->text = text;
190         logwin->lines = 1;
191         logwin->regexp_box = textbox;
192         lw->log_window = logwin->window;
193         return logwin;
194 }
195
196 static void log_window_init(LogWindow *logwin)
197 {
198         GtkTextBuffer *buffer;
199 #if !GTK_CHECK_VERSION(3,0,0)
200         GdkColormap *colormap;
201         gboolean success[LOG_COUNT];
202 #endif
203         gint i;
204
205         g_assert(logwin != NULL);
206         g_assert(logwin->colors != NULL);
207 #if !GTK_CHECK_VERSION(3,0,0)
208         for (i = LOG_NORMAL; i < LOG_COUNT; i++)
209                 {
210                 gboolean ok = gdk_color_parse(logdefs[i].color, &logwin->colors[i]);
211                 if (ok == TRUE) continue;
212
213                 if (i == LOG_NORMAL) return;
214                 memcpy(&logwin->colors[i], &logwin->colors[LOG_NORMAL], sizeof(GdkColor));
215                 }
216
217         colormap = gdk_drawable_get_colormap(gtk_widget_get_window(logwin->window));
218         gdk_colormap_alloc_colors(colormap, logwin->colors, LOG_COUNT, FALSE, TRUE, success);
219
220         for (i = LOG_NORMAL; i < LOG_COUNT; i++)
221                 {
222                 if (success[i] == FALSE)
223                         {
224                         GtkStyle *style;
225                         gint j;
226
227                         g_warning("LogWindow: color allocation failed\n");
228                         style = gtk_widget_get_style(logwin->window);
229                         for (j = LOG_NORMAL; j < LOG_COUNT; j++)
230                                 logwin->colors[j] = style->black;
231                         break;
232                         }
233                 }
234 #endif
235         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(logwin->text));
236         for (i = LOG_NORMAL; i < LOG_COUNT; i++)
237                 gtk_text_buffer_create_tag(buffer, logdefs[i].tag,
238                                            "foreground-gdk", &logwin->colors[i],
239                                            "family", "MonoSpace",
240                                            NULL);
241 }
242
243 static void log_window_show(LogWindow *logwin)
244 {
245         GtkTextView *text = GTK_TEXT_VIEW(logwin->text);
246         GtkTextBuffer *buffer;
247         GtkTextMark *mark;
248         gchar *regexp;
249
250         g_assert(logwin != NULL);
251
252         buffer = gtk_text_view_get_buffer(text);
253         mark = gtk_text_buffer_get_mark(buffer, "end");
254         gtk_text_view_scroll_mark_onscreen(text, mark);
255
256         gtk_window_present(GTK_WINDOW(logwin->window));
257
258         log_window_append("", LOG_NORMAL); // to flush memorized lines
259
260         regexp = g_strdup(get_regexp());
261         if (regexp != NULL)
262                 {
263                 gtk_entry_set_text(GTK_ENTRY(logwin->regexp_box), regexp);
264                 g_free(regexp);
265                 }
266 }
267
268 void log_window_new(LayoutWindow *lw)
269 {
270         if (logwindow == NULL)
271                 {
272                 LogWindow *logwin;
273
274                 logwin = log_window_create(lw);
275                 log_window_init(logwin);
276                 logwindow = logwin;
277                 }
278
279         log_window_show(logwindow);
280 }
281
282 typedef struct _LogMsg LogMsg;
283
284 struct _LogMsg {
285         gchar *text;
286         LogType type;
287 };
288
289
290 static void log_window_insert_text(GtkTextBuffer *buffer, GtkTextIter *iter,
291                                    const gchar *text, const gchar *tag)
292 {
293         gchar *str_utf8;
294
295         if (!text || !*text) return;
296
297         str_utf8 = utf8_validate_or_convert(text);
298         gtk_text_buffer_insert_with_tags_by_name(buffer, iter, str_utf8, -1, tag, NULL);
299         g_free(str_utf8);
300 }
301
302
303 void log_window_append(const gchar *str, LogType type)
304 {
305         GtkTextView *text;
306         GtkTextBuffer *buffer;
307         GtkTextIter iter;
308         static GList *memory = NULL;
309
310         if (logwindow == NULL)
311                 {
312                 if (*str) {
313                         LogMsg *msg = g_new(LogMsg, 1);
314
315                         msg->text = g_strdup(str);
316                         msg->type = type;
317
318                         memory = g_list_prepend(memory, msg);
319
320                         while (g_list_length(memory) >= options->log_window_lines)
321                                 {
322                                 GList *work = g_list_last(memory);
323                                 LogMsg *oldest_msg = work->data;
324
325                                 g_free(oldest_msg->text);
326                                 memory = g_list_delete_link(memory, work);
327                                 }
328                         }
329                 return;
330                 }
331
332         text = GTK_TEXT_VIEW(logwindow->text);
333         buffer = gtk_text_view_get_buffer(text);
334
335         if (options->log_window_lines > 0 && logwindow->lines >= options->log_window_lines)
336                 {
337                 GtkTextIter start, end;
338
339                 gtk_text_buffer_get_start_iter(buffer, &start);
340                 end = start;
341                 gtk_text_iter_forward_lines(&end, logwindow->lines - options->log_window_lines);
342                 gtk_text_buffer_delete(buffer, &start, &end);
343                 }
344
345         gtk_text_buffer_get_end_iter(buffer, &iter);
346
347         {
348         GList *work = g_list_last(memory);
349
350         while (work)
351                 {
352                 GList *prev;
353                 LogMsg *oldest_msg = work->data;
354
355                 log_window_insert_text(buffer, &iter, oldest_msg->text, logdefs[oldest_msg->type].tag);
356
357                 prev = work->prev;
358                 memory = g_list_delete_link(memory, work);
359                 work = prev;
360                 }
361         }
362
363         log_window_insert_text(buffer, &iter, str, logdefs[type].tag);
364
365         if (!options->log_window.paused)
366                 {
367                 if (gtk_widget_get_visible(GTK_WIDGET(text)))
368                         {
369                         GtkTextMark *mark;
370
371                         mark = gtk_text_buffer_get_mark(buffer, "end");
372                         gtk_text_view_scroll_mark_onscreen(text, mark);
373                         }
374                 }
375
376         logwindow->lines = gtk_text_buffer_get_line_count(buffer);
377 }
378 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */