2 * Copyright (C) 2004 John Ellis
3 * Copyright (C) 2008 - 2016 The Geeqie Team
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.
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.
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.
28 #include <glib-object.h>
33 #include "main-defines.h"
34 #include "ui-fileops.h"
40 HELP_WINDOW_WIDTH = 650,
41 HELP_WINDOW_HEIGHT = 350
46 *-----------------------------------------------------------------------------
48 *-----------------------------------------------------------------------------
51 #define SCROLL_MARKNAME "scroll_point"
53 static void help_window_scroll(GtkWidget *text, const gchar *key)
56 GtkTextBuffer *buffer;
61 if (!text || !key) return;
63 needle = g_strdup_printf("[section:%s]", key);
65 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
66 gtk_text_buffer_get_iter_at_offset(buffer, &iter, 0);
68 if (gtk_text_iter_forward_search(&iter, needle, GTK_TEXT_SEARCH_TEXT_ONLY,
69 &start, &end, nullptr))
74 line = gtk_text_iter_get_line(&start);
75 gtk_text_buffer_get_iter_at_line_offset(buffer, &iter, line, 0);
76 gtk_text_buffer_place_cursor(buffer, &iter);
78 /* apparently only scroll_to_mark works when the textview is not visible yet */
80 /* if mark exists, move it instead of creating one for every scroll */
81 mark = gtk_text_buffer_get_mark(buffer, SCROLL_MARKNAME);
84 gtk_text_buffer_move_mark(buffer, mark, &iter);
88 mark = gtk_text_buffer_create_mark(buffer, SCROLL_MARKNAME, &iter, FALSE);
90 gtk_text_view_scroll_to_mark(GTK_TEXT_VIEW(text), mark, 0.0, TRUE, 0, 0);
96 static void help_window_load_text(GtkWidget *text, const gchar *path)
101 GtkTextBuffer *buffer;
106 if (!text || !path) return;
108 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
110 gtk_text_buffer_get_bounds(buffer, &start, &end);
111 gtk_text_buffer_delete(buffer, &start, &end);
113 gtk_text_buffer_get_iter_at_offset(buffer, &iter, 0);
115 pathl = path_from_utf8(path);
116 f = fopen(pathl, "r");
121 buf = g_strdup_printf(_("Unable to load:\n%s"), path);
122 gtk_text_buffer_insert(buffer, &iter, buf, -1);
127 while (fgets(s_buf, sizeof(s_buf), f))
134 if (!g_utf8_validate(s_buf, l, nullptr))
136 buf = g_locale_to_utf8(s_buf, l, nullptr, nullptr, nullptr);
137 if (!buf) buf = g_strdup("\n");
143 gtk_text_buffer_insert_with_tags_by_name(buffer, &iter,
144 (buf) ? buf : s_buf, -1,
151 gtk_text_buffer_get_iter_at_offset(buffer, &iter, 0);
152 gtk_text_buffer_place_cursor(buffer, &iter);
153 gtk_text_view_scroll_to_iter(GTK_TEXT_VIEW(text), &iter, 0.0, TRUE, 0, 0);
156 static gboolean help_window_delete_cb(GtkWidget *widget, GdkEventAny *, gpointer)
158 gq_gtk_widget_destroy(widget);
162 static void help_window_close(GtkWidget *, gpointer data)
164 auto window = static_cast<GtkWidget *>(data);
165 gq_gtk_widget_destroy(window);
168 void help_window_set_key(GtkWidget *window, const gchar *key)
174 text = static_cast<GtkWidget *>(g_object_get_data(G_OBJECT(window), "text_widget"));
177 gdk_window_raise(gtk_widget_get_window(window));
179 if (key) help_window_scroll(text, key);
182 #pragma GCC diagnostic push
183 #pragma GCC diagnostic ignored "-Wunused-function"
184 void help_window_set_file_unused(GtkWidget *window, const gchar *path, const gchar *key)
188 if (!window || !path) return;
190 text = static_cast<GtkWidget *>(g_object_get_data(G_OBJECT(window), "text_widget"));
193 gdk_window_raise(gtk_widget_get_window(window));
195 help_window_load_text(text, path);
196 help_window_scroll(text, key);
198 #pragma GCC diagnostic pop
200 GtkWidget *help_window_new(const gchar *title,
201 const gchar *subclass,
202 const gchar *path, const gchar *key)
206 GtkTextBuffer *buffer;
214 window = window_new(subclass, nullptr, nullptr, title);
216 gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
217 gtk_window_set_default_size(GTK_WINDOW(window), HELP_WINDOW_WIDTH, HELP_WINDOW_HEIGHT);
219 g_signal_connect(G_OBJECT(window), "delete_event",
220 G_CALLBACK(help_window_delete_cb), NULL);
222 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
223 gq_gtk_container_add(GTK_WIDGET(window), vbox);
224 gtk_widget_show(vbox);
226 g_object_set_data(G_OBJECT(window), "text_vbox", vbox);
230 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
231 gq_gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, TRUE, 0);
232 gtk_widget_show(hbox);
234 scrolled = gq_gtk_scrolled_window_new(nullptr, nullptr);
235 gq_gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled), GTK_SHADOW_IN);
236 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled),
237 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
238 gq_gtk_box_pack_start(GTK_BOX(hbox), scrolled, TRUE, TRUE, 0);
239 gtk_widget_show(scrolled);
241 text = gtk_text_view_new();
242 gtk_text_view_set_editable(GTK_TEXT_VIEW(text), FALSE);
243 gq_gtk_container_add(GTK_WIDGET(scrolled), text);
244 gtk_widget_show(text);
246 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
247 gtk_text_buffer_create_tag(buffer, "monospace",
248 "family", "monospace", NULL);
250 hbox = gtk_button_box_new(GTK_ORIENTATION_HORIZONTAL);
251 gtk_container_set_border_width(GTK_CONTAINER(hbox), PREF_PAD_BORDER);
252 gtk_button_box_set_layout(GTK_BUTTON_BOX(hbox), GTK_BUTTONBOX_END);
253 gq_gtk_box_pack_end(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
254 gtk_widget_show(hbox);
256 button = gtk_button_new_from_icon_name(GQ_ICON_CLOSE, GTK_ICON_SIZE_BUTTON);
257 g_signal_connect(G_OBJECT(button), "clicked",
258 G_CALLBACK(help_window_close), window);
259 gq_gtk_container_add(GTK_WIDGET(hbox), button);
260 gtk_widget_set_can_default(button, TRUE);
261 gtk_widget_grab_default(button);
262 gtk_widget_show(button);
264 g_object_set_data(G_OBJECT(window), "text_widget", text);
266 help_window_load_text(text, path);
268 gtk_widget_show(window);
270 help_window_scroll(text, key);
275 #pragma GCC diagnostic push
276 #pragma GCC diagnostic ignored "-Wunused-function"
277 GtkWidget *help_window_get_box_unused(GtkWidget *window)
279 return static_cast<GtkWidget *>(g_object_get_data(G_OBJECT(window), "text_vbox"));
281 #pragma GCC diagnostic pop
283 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */