Fix missing translation
[geeqie.git] / src / ui-help.cc
1 /*
2  * Copyright (C) 2004 John Ellis
3  * Copyright (C) 2008 - 2016 The Geeqie Team
4  *
5  * Author: John Ellis
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #include "ui-help.h"
23
24 #include <cstdio>
25 #include <cstring>
26
27 #include <config.h>
28
29 #include "compat.h"
30 #include "debug.h"
31 #include "intl.h"
32 #include "main-defines.h"
33 #include "ui-fileops.h"
34 #include "ui-misc.h"
35 #include "window.h"
36
37
38 enum {
39         HELP_WINDOW_WIDTH = 650,
40         HELP_WINDOW_HEIGHT = 350
41 };
42
43
44 /*
45  *-----------------------------------------------------------------------------
46  * 'help' window
47  *-----------------------------------------------------------------------------
48  */
49
50 #define SCROLL_MARKNAME "scroll_point"
51
52 static void help_window_scroll(GtkWidget *text, const gchar *key)
53 {
54         gchar *needle;
55         GtkTextBuffer *buffer;
56         GtkTextIter iter;
57         GtkTextIter start;
58         GtkTextIter end;
59
60         if (!text || !key) return;
61
62         needle = g_strdup_printf("[section:%s]", key);
63
64         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
65         gtk_text_buffer_get_iter_at_offset(buffer, &iter, 0);
66
67         if (gtk_text_iter_forward_search(&iter, needle, GTK_TEXT_SEARCH_TEXT_ONLY,
68                                          &start, &end, nullptr))
69                 {
70                 gint line;
71                 GtkTextMark *mark;
72
73                 line = gtk_text_iter_get_line(&start);
74                 gtk_text_buffer_get_iter_at_line_offset(buffer, &iter, line, 0);
75                 gtk_text_buffer_place_cursor(buffer, &iter);
76
77                 /* apparently only scroll_to_mark works when the textview is not visible yet */
78
79                 /* if mark exists, move it instead of creating one for every scroll */
80                 mark = gtk_text_buffer_get_mark(buffer, SCROLL_MARKNAME);
81                 if (mark)
82                         {
83                         gtk_text_buffer_move_mark(buffer, mark, &iter);
84                         }
85                 else
86                         {
87                         mark = gtk_text_buffer_create_mark(buffer, SCROLL_MARKNAME, &iter, FALSE);
88                         }
89                 gtk_text_view_scroll_to_mark(GTK_TEXT_VIEW(text), mark, 0.0, TRUE, 0, 0);
90                 }
91
92         g_free(needle);
93 }
94
95 static void help_window_load_text(GtkWidget *text, const gchar *path)
96 {
97         gchar *pathl;
98         FILE *f;
99         gchar s_buf[1024];
100         GtkTextBuffer *buffer;
101         GtkTextIter iter;
102         GtkTextIter start;
103         GtkTextIter end;
104
105         if (!text || !path) return;
106
107         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
108
109         gtk_text_buffer_get_bounds(buffer, &start, &end);
110         gtk_text_buffer_delete(buffer, &start, &end);
111
112         gtk_text_buffer_get_iter_at_offset(buffer, &iter, 0);
113
114         pathl = path_from_utf8(path);
115         f = fopen(pathl, "r");
116         g_free(pathl);
117         if (!f)
118                 {
119                 gchar *buf;
120                 buf = g_strdup_printf(_("Unable to load:\n%s"), path);
121                 gtk_text_buffer_insert(buffer, &iter, buf, -1);
122                 g_free(buf);
123                 }
124         else
125                 {
126                 while (fgets(s_buf, sizeof(s_buf), f))
127                         {
128                         gchar *buf;
129                         gint l;
130
131                         l = strlen(s_buf);
132
133                         if (!g_utf8_validate(s_buf, l, nullptr))
134                                 {
135                                 buf = g_locale_to_utf8(s_buf, l, nullptr, nullptr, nullptr);
136                                 if (!buf) buf = g_strdup("\n");
137                                 }
138                         else
139                                 {
140                                 buf = nullptr;
141                                 }
142                         gtk_text_buffer_insert_with_tags_by_name(buffer, &iter,
143                                                                  (buf) ? buf : s_buf, -1,
144                                                                  "monospace", NULL);
145                         g_free(buf);
146                         }
147                 fclose(f);
148                 }
149
150         gtk_text_buffer_get_iter_at_offset(buffer, &iter, 0);
151         gtk_text_buffer_place_cursor(buffer, &iter);
152         gtk_text_view_scroll_to_iter(GTK_TEXT_VIEW(text), &iter, 0.0, TRUE, 0, 0);
153 }
154
155 static gboolean help_window_delete_cb(GtkWidget *widget, GdkEventAny *, gpointer)
156 {
157         gq_gtk_widget_destroy(widget);
158         return TRUE;
159 }
160
161 static void help_window_close(GtkWidget *, gpointer data)
162 {
163         auto window = static_cast<GtkWidget *>(data);
164         gq_gtk_widget_destroy(window);
165 }
166
167 void help_window_set_key(GtkWidget *window, const gchar *key)
168 {
169         GtkWidget *text;
170
171         if (!window) return;
172
173         text = static_cast<GtkWidget *>(g_object_get_data(G_OBJECT(window), "text_widget"));
174         if (!text) return;
175
176         gdk_window_raise(gtk_widget_get_window(window));
177
178         if (key) help_window_scroll(text, key);
179 }
180
181 #pragma GCC diagnostic push
182 #pragma GCC diagnostic ignored "-Wunused-function"
183 void help_window_set_file_unused(GtkWidget *window, const gchar *path, const gchar *key)
184 {
185         GtkWidget *text;
186
187         if (!window || !path) return;
188
189         text = static_cast<GtkWidget *>(g_object_get_data(G_OBJECT(window), "text_widget"));
190         if (!text) return;
191
192         gdk_window_raise(gtk_widget_get_window(window));
193
194         help_window_load_text(text, path);
195         help_window_scroll(text, key);
196 }
197 #pragma GCC diagnostic pop
198
199 GtkWidget *help_window_new(const gchar *title,
200                            const gchar *subclass,
201                            const gchar *path, const gchar *key)
202 {
203         GtkWidget *window;
204         GtkWidget *text;
205         GtkTextBuffer *buffer;
206         GtkWidget *vbox;
207         GtkWidget *hbox;
208         GtkWidget *button;
209         GtkWidget *scrolled;
210
211         /* window */
212
213         window = window_new(subclass, nullptr, nullptr, title);
214         DEBUG_NAME(window);
215         gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
216         gtk_window_set_default_size(GTK_WINDOW(window), HELP_WINDOW_WIDTH, HELP_WINDOW_HEIGHT);
217
218         g_signal_connect(G_OBJECT(window), "delete_event",
219                          G_CALLBACK(help_window_delete_cb), NULL);
220
221         vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
222         gq_gtk_container_add(GTK_WIDGET(window), vbox);
223         gtk_widget_show(vbox);
224
225         g_object_set_data(G_OBJECT(window), "text_vbox", vbox);
226
227         /* text window */
228
229         hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
230         gq_gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, TRUE, 0);
231         gtk_widget_show(hbox);
232
233         scrolled = gq_gtk_scrolled_window_new(nullptr, nullptr);
234         gq_gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled), GTK_SHADOW_IN);
235         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled),
236                                        GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
237         gq_gtk_box_pack_start(GTK_BOX(hbox), scrolled, TRUE, TRUE, 0);
238         gtk_widget_show(scrolled);
239
240         text = gtk_text_view_new();
241         gtk_text_view_set_editable(GTK_TEXT_VIEW(text), FALSE);
242         gq_gtk_container_add(GTK_WIDGET(scrolled), text);
243         gtk_widget_show(text);
244
245         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
246         gtk_text_buffer_create_tag(buffer, "monospace",
247                                    "family", "monospace", NULL);
248
249         hbox = gtk_button_box_new(GTK_ORIENTATION_HORIZONTAL);
250         gtk_container_set_border_width(GTK_CONTAINER(hbox), PREF_PAD_BORDER);
251         gtk_button_box_set_layout(GTK_BUTTON_BOX(hbox), GTK_BUTTONBOX_END);
252         gq_gtk_box_pack_end(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
253         gtk_widget_show(hbox);
254
255         button = gtk_button_new_from_icon_name(GQ_ICON_CLOSE, GTK_ICON_SIZE_BUTTON);
256         g_signal_connect(G_OBJECT(button), "clicked",
257                          G_CALLBACK(help_window_close), window);
258         gq_gtk_container_add(GTK_WIDGET(hbox), button);
259         gtk_widget_set_can_default(button, TRUE);
260         gtk_widget_grab_default(button);
261         gtk_widget_show(button);
262
263         g_object_set_data(G_OBJECT(window), "text_widget", text);
264
265         help_window_load_text(text, path);
266
267         gtk_widget_show(window);
268
269         help_window_scroll(text, key);
270
271         return window;
272 }
273
274 #pragma GCC diagnostic push
275 #pragma GCC diagnostic ignored "-Wunused-function"
276 GtkWidget *help_window_get_box_unused(GtkWidget *window)
277 {
278         return static_cast<GtkWidget *>(g_object_get_data(G_OBJECT(window), "text_vbox"));
279 }
280 #pragma GCC diagnostic pop
281
282 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */