Change configuration system from Autotools to Meson
[geeqie.git] / src / ui_help.c
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 <config.h>
23 #include "intl.h"
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 #include <gtk/gtk.h>
30
31 #include "main.h"
32 #include "ui_help.h"
33
34 #include "ui_fileops.h"
35 #include "ui_misc.h"
36 #include "window.h"
37
38
39 #define HELP_WINDOW_WIDTH 650
40 #define HELP_WINDOW_HEIGHT 350
41
42
43 /*
44  *-----------------------------------------------------------------------------
45  * 'help' window
46  *-----------------------------------------------------------------------------
47  */
48
49 #define SCROLL_MARKNAME "scroll_point"
50
51 static void help_window_scroll(GtkWidget *text, const gchar *key)
52 {
53         gchar *needle;
54         GtkTextBuffer *buffer;
55         GtkTextIter iter;
56         GtkTextIter start, end;
57
58         if (!text || !key) return;
59
60         needle = g_strdup_printf("[section:%s]", key);
61
62         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
63         gtk_text_buffer_get_iter_at_offset(buffer, &iter, 0);
64
65         if (gtk_text_iter_forward_search(&iter, needle, GTK_TEXT_SEARCH_TEXT_ONLY,
66                                          &start, &end, NULL))
67                 {
68                 gint line;
69                 GtkTextMark *mark;
70
71                 line = gtk_text_iter_get_line(&start);
72                 gtk_text_buffer_get_iter_at_line_offset(buffer, &iter, line, 0);
73                 gtk_text_buffer_place_cursor(buffer, &iter);
74
75                 /* apparently only scroll_to_mark works when the textview is not visible yet */
76
77                 /* if mark exists, move it instead of creating one for every scroll */
78                 mark = gtk_text_buffer_get_mark(buffer, SCROLL_MARKNAME);
79                 if (mark)
80                         {
81                         gtk_text_buffer_move_mark(buffer, mark, &iter);
82                         }
83                 else
84                         {
85                         mark = gtk_text_buffer_create_mark(buffer, SCROLL_MARKNAME, &iter, FALSE);
86                         }
87                 gtk_text_view_scroll_to_mark(GTK_TEXT_VIEW(text), mark, 0.0, TRUE, 0, 0);
88                 }
89
90         g_free(needle);
91 }
92
93 static void help_window_load_text(GtkWidget *text, const gchar *path)
94 {
95         gchar *pathl;
96         FILE *f;
97         gchar s_buf[1024];
98         GtkTextBuffer *buffer;
99         GtkTextIter iter;
100         GtkTextIter start, end;
101
102         if (!text || !path) return;
103
104         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
105
106         gtk_text_buffer_get_bounds(buffer, &start, &end);
107         gtk_text_buffer_delete(buffer, &start, &end);
108
109         gtk_text_buffer_get_iter_at_offset(buffer, &iter, 0);
110
111         pathl = path_from_utf8(path);
112         f = fopen(pathl, "r");
113         g_free(pathl);
114         if (!f)
115                 {
116                 gchar *buf;
117                 buf = g_strdup_printf(_("Unable to load:\n%s"), path);
118                 gtk_text_buffer_insert(buffer, &iter, buf, -1);
119                 g_free(buf);
120                 }
121         else
122                 {
123                 while (fgets(s_buf, sizeof(s_buf), f))
124                         {
125                         gchar *buf;
126                         gint l;
127
128                         l = strlen(s_buf);
129
130                         if (!g_utf8_validate(s_buf, l, NULL))
131                                 {
132                                 buf = g_locale_to_utf8(s_buf, l, NULL, NULL, NULL);
133                                 if (!buf) buf = g_strdup("\n");
134                                 }
135                         else
136                                 {
137                                 buf = NULL;
138                                 }
139                         gtk_text_buffer_insert_with_tags_by_name(buffer, &iter,
140                                                                  (buf) ? buf : s_buf, -1,
141                                                                  "monospace", NULL);
142                         g_free(buf);
143                         }
144                 fclose(f);
145                 }
146
147         gtk_text_buffer_get_iter_at_offset(buffer, &iter, 0);
148         gtk_text_buffer_place_cursor(buffer, &iter);
149         gtk_text_view_scroll_to_iter(GTK_TEXT_VIEW(text), &iter, 0.0, TRUE, 0, 0);
150 }
151
152 static gboolean help_window_delete_cb(GtkWidget *widget, GdkEventAny *event, gpointer data)
153 {
154         gtk_widget_destroy(widget);
155         return TRUE;
156 }
157
158 static void help_window_close(GtkWidget *widget, gpointer data)
159 {
160         GtkWidget *window = data;
161         gtk_widget_destroy(window);
162 }
163
164 void help_window_set_key(GtkWidget *window, const gchar *key)
165 {
166         GtkWidget *text;
167
168         if (!window) return;
169
170         text = g_object_get_data(G_OBJECT(window), "text_widget");
171         if (!text) return;
172
173         gdk_window_raise(gtk_widget_get_window(window));
174
175         if (key) help_window_scroll(text, key);
176 }
177
178 void help_window_set_file(GtkWidget *window, const gchar *path, const gchar *key)
179 {
180         GtkWidget *text;
181
182         if (!window || !path) return;
183
184         text = g_object_get_data(G_OBJECT(window), "text_widget");
185         if (!text) return;
186
187         gdk_window_raise(gtk_widget_get_window(window));
188
189         help_window_load_text(text, path);
190         help_window_scroll(text, key);
191 }
192
193 GtkWidget *help_window_new(const gchar *title,
194                            const gchar *subclass,
195                            const gchar *path, const gchar *key)
196 {
197         GtkWidget *window;
198         GtkWidget *text;
199         GtkTextBuffer *buffer;
200         GtkWidget *vbox;
201         GtkWidget *hbox;
202         GtkWidget *button;
203         GtkWidget *scrolled;
204
205         /* window */
206
207         window = window_new(GTK_WINDOW_TOPLEVEL, subclass, NULL, NULL, title);
208         DEBUG_NAME(window);
209         gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
210         gtk_window_set_default_size(GTK_WINDOW(window), HELP_WINDOW_WIDTH, HELP_WINDOW_HEIGHT);
211
212         g_signal_connect(G_OBJECT(window), "delete_event",
213                          G_CALLBACK(help_window_delete_cb), NULL);
214
215         vbox = gtk_vbox_new(FALSE, 0);
216         gtk_container_add(GTK_CONTAINER(window), vbox);
217         gtk_widget_show(vbox);
218
219         g_object_set_data(G_OBJECT(window), "text_vbox", vbox);
220
221         /* text window */
222
223         hbox = gtk_hbox_new(FALSE, 0);
224         gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, TRUE, 0);
225         gtk_widget_show(hbox);
226
227         scrolled = gtk_scrolled_window_new(NULL, NULL);
228         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled), GTK_SHADOW_IN);
229         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled),
230                                        GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
231         gtk_box_pack_start(GTK_BOX(hbox), scrolled, TRUE, TRUE, 0);
232         gtk_widget_show(scrolled);
233
234         text = gtk_text_view_new();
235         gtk_text_view_set_editable(GTK_TEXT_VIEW(text), FALSE);
236         gtk_container_add(GTK_CONTAINER(scrolled), text);
237         gtk_widget_show(text);
238
239         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
240         gtk_text_buffer_create_tag(buffer, "monospace",
241                                    "family", "monospace", NULL);
242
243         hbox = gtk_hbutton_box_new();
244         gtk_container_set_border_width(GTK_CONTAINER(hbox), PREF_PAD_BORDER);
245         gtk_button_box_set_layout(GTK_BUTTON_BOX(hbox), GTK_BUTTONBOX_END);
246         gtk_box_pack_end(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
247         gtk_widget_show(hbox);
248
249         button = gtk_button_new_from_stock(GTK_STOCK_CLOSE);
250         g_signal_connect(G_OBJECT(button), "clicked",
251                          G_CALLBACK(help_window_close), window);
252         gtk_container_add(GTK_CONTAINER(hbox), button);
253         gtk_widget_set_can_default(button, TRUE);
254         gtk_widget_grab_default(button);
255         gtk_widget_show(button);
256
257         g_object_set_data(G_OBJECT(window), "text_widget", text);
258
259         help_window_load_text(text, path);
260
261         gtk_widget_show(window);
262
263         help_window_scroll(text, key);
264
265         return window;
266 }
267
268 GtkWidget *help_window_get_box(GtkWidget *window)
269 {
270         return g_object_get_data(G_OBJECT(window), "text_vbox");
271 }
272 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */