Simplify vflist_get_formatted()
[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 #ifdef HAVE_CONFIG_H
23 #  include "config.h"
24 #endif
25 #include "intl.h"
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include <gtk/gtk.h>
32
33 #include "main.h"
34 #include "ui_help.h"
35
36 #include "ui_fileops.h"
37 #include "ui_misc.h"
38 #include "window.h"
39
40
41 #define HELP_WINDOW_WIDTH 650
42 #define HELP_WINDOW_HEIGHT 350
43
44
45 /*
46  *-----------------------------------------------------------------------------
47  * 'help' window
48  *-----------------------------------------------------------------------------
49  */
50
51 #define SCROLL_MARKNAME "scroll_point"
52
53 static void help_window_scroll(GtkWidget *text, const gchar *key)
54 {
55         gchar *needle;
56         GtkTextBuffer *buffer;
57         GtkTextIter iter;
58         GtkTextIter start, 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, NULL))
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, end;
103
104         if (!text || !path) return;
105
106         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
107
108         gtk_text_buffer_get_bounds(buffer, &start, &end);
109         gtk_text_buffer_delete(buffer, &start, &end);
110
111         gtk_text_buffer_get_iter_at_offset(buffer, &iter, 0);
112
113         pathl = path_from_utf8(path);
114         f = fopen(pathl, "r");
115         g_free(pathl);
116         if (!f)
117                 {
118                 gchar *buf;
119                 buf = g_strdup_printf(_("Unable to load:\n%s"), path);
120                 gtk_text_buffer_insert(buffer, &iter, buf, -1);
121                 g_free(buf);
122                 }
123         else
124                 {
125                 while (fgets(s_buf, sizeof(s_buf), f))
126                         {
127                         gchar *buf;
128                         gint l;
129
130                         l = strlen(s_buf);
131
132                         if (!g_utf8_validate(s_buf, l, NULL))
133                                 {
134                                 buf = g_locale_to_utf8(s_buf, l, NULL, NULL, NULL);
135                                 if (!buf) buf = g_strdup("\n");
136                                 }
137                         else
138                                 {
139                                 buf = NULL;
140                                 }
141                         gtk_text_buffer_insert_with_tags_by_name(buffer, &iter,
142                                                                  (buf) ? buf : s_buf, -1,
143                                                                  "monospace", NULL);
144                         g_free(buf);
145                         }
146                 fclose(f);
147                 }
148
149         gtk_text_buffer_get_iter_at_offset(buffer, &iter, 0);
150         gtk_text_buffer_place_cursor(buffer, &iter);
151         gtk_text_view_scroll_to_iter(GTK_TEXT_VIEW(text), &iter, 0.0, TRUE, 0, 0);
152 }
153
154 static gboolean help_window_delete_cb(GtkWidget *widget, GdkEventAny *event, gpointer data)
155 {
156         gtk_widget_destroy(widget);
157         return TRUE;
158 }
159
160 static void help_window_close(GtkWidget *widget, gpointer data)
161 {
162         GtkWidget *window = data;
163         gtk_widget_destroy(window);
164 }
165
166 void help_window_set_key(GtkWidget *window, const gchar *key)
167 {
168         GtkWidget *text;
169
170         if (!window) return;
171
172         text = g_object_get_data(G_OBJECT(window), "text_widget");
173         if (!text) return;
174
175         gdk_window_raise(gtk_widget_get_window(window));
176
177         if (key) help_window_scroll(text, key);
178 }
179
180 void help_window_set_file(GtkWidget *window, const gchar *path, const gchar *key)
181 {
182         GtkWidget *text;
183
184         if (!window || !path) return;
185
186         text = g_object_get_data(G_OBJECT(window), "text_widget");
187         if (!text) return;
188
189         gdk_window_raise(gtk_widget_get_window(window));
190
191         help_window_load_text(text, path);
192         help_window_scroll(text, key);
193 }
194
195 GtkWidget *help_window_new(const gchar *title,
196                            const gchar *subclass,
197                            const gchar *path, const gchar *key)
198 {
199         GtkWidget *window;
200         GtkWidget *text;
201         GtkTextBuffer *buffer;
202         GtkWidget *vbox;
203         GtkWidget *hbox;
204         GtkWidget *button;
205         GtkWidget *scrolled;
206
207         /* window */
208
209         window = window_new(GTK_WINDOW_TOPLEVEL, subclass, NULL, NULL, title);
210         DEBUG_NAME(window);
211         gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
212         gtk_window_set_default_size(GTK_WINDOW(window), HELP_WINDOW_WIDTH, HELP_WINDOW_HEIGHT);
213
214         g_signal_connect(G_OBJECT(window), "delete_event",
215                          G_CALLBACK(help_window_delete_cb), NULL);
216
217         vbox = gtk_vbox_new(FALSE, 0);
218         gtk_container_add(GTK_CONTAINER(window), vbox);
219         gtk_widget_show(vbox);
220
221         g_object_set_data(G_OBJECT(window), "text_vbox", vbox);
222
223         /* text window */
224
225         hbox = gtk_hbox_new(FALSE, 0);
226         gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, TRUE, 0);
227         gtk_widget_show(hbox);
228
229         scrolled = gtk_scrolled_window_new(NULL, NULL);
230         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled), GTK_SHADOW_IN);
231         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled),
232                                        GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
233         gtk_box_pack_start(GTK_BOX(hbox), scrolled, TRUE, TRUE, 0);
234         gtk_widget_show(scrolled);
235
236         text = gtk_text_view_new();
237         gtk_text_view_set_editable(GTK_TEXT_VIEW(text), FALSE);
238         gtk_container_add(GTK_CONTAINER(scrolled), text);
239         gtk_widget_show(text);
240
241         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
242         gtk_text_buffer_create_tag(buffer, "monospace",
243                                    "family", "monospace", NULL);
244
245         hbox = gtk_hbutton_box_new();
246         gtk_container_set_border_width(GTK_CONTAINER(hbox), PREF_PAD_BORDER);
247         gtk_button_box_set_layout(GTK_BUTTON_BOX(hbox), GTK_BUTTONBOX_END);
248         gtk_box_pack_end(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
249         gtk_widget_show(hbox);
250
251         button = gtk_button_new_from_stock(GTK_STOCK_CLOSE);
252         g_signal_connect(G_OBJECT(button), "clicked",
253                          G_CALLBACK(help_window_close), window);
254         gtk_container_add(GTK_CONTAINER(hbox), button);
255         gtk_widget_set_can_default(button, TRUE);
256         gtk_widget_grab_default(button);
257         gtk_widget_show(button);
258
259         g_object_set_data(G_OBJECT(window), "text_widget", text);
260
261         help_window_load_text(text, path);
262
263         gtk_widget_show(window);
264
265         help_window_scroll(text, key);
266
267         return window;
268 }
269
270 GtkWidget *help_window_get_box(GtkWidget *window)
271 {
272         return g_object_get_data(G_OBJECT(window), "text_vbox");
273 }
274 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */