9
|
1 /*
|
|
2 * (SLIK) SimpLIstic sKin functions
|
|
3 * (C) 2004 John Ellis
|
|
4 *
|
|
5 * Author: John Ellis
|
|
6 *
|
|
7 * This software is released under the GNU General Public License (GNU GPL).
|
|
8 * Please read the included file COPYING for more information.
|
|
9 * This software comes with no warranty of any kind, use at your own risk!
|
|
10 */
|
|
11
|
|
12 #ifdef HAVE_CONFIG_H
|
|
13 # include "config.h"
|
|
14 #endif
|
|
15 #include "intl.h"
|
|
16
|
|
17 #include <stdio.h>
|
|
18 #include <stdlib.h>
|
|
19 #include <string.h>
|
|
20
|
|
21 #include <gtk/gtk.h>
|
|
22
|
|
23 #include "ui_help.h"
|
|
24
|
|
25 #include "ui_fileops.h"
|
|
26 #include "ui_misc.h"
|
|
27
|
|
28
|
|
29 #define HELP_WINDOW_WIDTH 620
|
|
30 #define HELP_WINDOW_HEIGHT 350
|
|
31
|
|
32
|
|
33 /*
|
|
34 *-----------------------------------------------------------------------------
|
|
35 * 'help' window
|
|
36 *-----------------------------------------------------------------------------
|
|
37 */
|
|
38
|
|
39 #define SCROLL_MARKNAME "scroll_point"
|
|
40
|
|
41 static void help_window_scroll(GtkWidget *text, const gchar *key)
|
|
42 {
|
|
43 gchar *needle;
|
|
44 GtkTextBuffer *buffer;
|
|
45 GtkTextIter iter;
|
|
46 GtkTextIter start, end;
|
|
47
|
|
48 if (!text || !key) return;
|
|
49
|
|
50 needle = g_strdup_printf("[section:%s]", key);
|
|
51
|
|
52 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
|
|
53 gtk_text_buffer_get_iter_at_offset(buffer, &iter, 0);
|
|
54
|
|
55 if (gtk_text_iter_forward_search(&iter, needle, GTK_TEXT_SEARCH_TEXT_ONLY,
|
|
56 &start, &end, NULL))
|
|
57 {
|
|
58 gint line;
|
|
59 GtkTextMark *mark;
|
|
60
|
|
61 line = gtk_text_iter_get_line(&start);
|
|
62 gtk_text_buffer_get_iter_at_line_offset(buffer, &iter, line, 0);
|
|
63 gtk_text_buffer_place_cursor(buffer, &iter);
|
|
64
|
|
65 #if 0
|
|
66 gtk_text_view_scroll_to_iter(GTK_TEXT_VIEW(text), &iter, 0.0, TRUE, 0, 0);
|
|
67 #endif
|
|
68
|
|
69 /* apparently only scroll_to_mark works when the textview is not visible yet */
|
|
70
|
|
71 /* if mark exists, move it instead of creating one for every scroll */
|
|
72 mark = gtk_text_buffer_get_mark(buffer, SCROLL_MARKNAME);
|
|
73 if (mark)
|
|
74 {
|
|
75 gtk_text_buffer_move_mark(buffer, mark, &iter);
|
|
76 }
|
|
77 else
|
|
78 {
|
|
79 mark = gtk_text_buffer_create_mark(buffer, SCROLL_MARKNAME, &iter, FALSE);
|
|
80 }
|
|
81 gtk_text_view_scroll_to_mark(GTK_TEXT_VIEW(text), mark, 0.0, TRUE, 0, 0);
|
|
82 }
|
|
83
|
|
84 g_free(needle);
|
|
85 }
|
|
86
|
|
87 static void help_window_load_text(GtkWidget *text, const gchar *path)
|
|
88 {
|
|
89 gchar *pathl;
|
|
90 FILE *f;
|
|
91 gchar s_buf[1024];
|
|
92 GtkTextBuffer *buffer;
|
|
93 GtkTextIter iter;
|
|
94 GtkTextIter start, end;
|
|
95
|
|
96 if (!text || !path) return;
|
|
97
|
|
98 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
|
|
99
|
|
100 gtk_text_buffer_get_bounds(buffer, &start, &end);
|
|
101 gtk_text_buffer_delete(buffer, &start, &end);
|
|
102
|
|
103 gtk_text_buffer_get_iter_at_offset(buffer, &iter, 0);
|
|
104
|
|
105 pathl = path_from_utf8(path);
|
|
106 f = fopen(pathl, "r");
|
|
107 g_free(pathl);
|
|
108 if (!f)
|
|
109 {
|
|
110 gchar *buf;
|
|
111 buf = g_strdup_printf(_("Unable to load:\n%s"), path);
|
|
112 gtk_text_buffer_insert(buffer, &iter, buf, -1);
|
|
113 g_free(buf);
|
|
114 }
|
|
115 else
|
|
116 {
|
|
117 while (fgets(s_buf, sizeof(s_buf), f))
|
|
118 {
|
|
119 gchar *buf;
|
|
120 gint l;
|
|
121
|
|
122 l = strlen(s_buf);
|
|
123
|
|
124 if (!g_utf8_validate(s_buf, l, NULL))
|
|
125 {
|
|
126 buf = g_locale_to_utf8(s_buf, strlen(s_buf), NULL, NULL, NULL);
|
|
127 if (!buf) buf = g_strdup("\n");
|
|
128 }
|
|
129 else
|
|
130 {
|
|
131 buf = NULL;
|
|
132 }
|
|
133 gtk_text_buffer_insert_with_tags_by_name(buffer, &iter,
|
|
134 (buf) ? buf : s_buf, -1,
|
|
135 "monospace", NULL);
|
|
136 g_free(buf);
|
|
137 }
|
|
138 fclose(f);
|
|
139 }
|
|
140
|
|
141 gtk_text_buffer_get_iter_at_offset(buffer, &iter, 0);
|
|
142 gtk_text_buffer_place_cursor(buffer, &iter);
|
|
143 gtk_text_view_scroll_to_iter(GTK_TEXT_VIEW(text), &iter, 0.0, TRUE, 0, 0);
|
|
144 }
|
|
145
|
|
146 static gint help_window_delete_cb(GtkWidget *widget, GdkEventAny *event, gpointer data)
|
|
147 {
|
|
148 gtk_widget_destroy(widget);
|
|
149 return TRUE;
|
|
150 }
|
|
151
|
|
152 static void help_window_close(GtkWidget *widget, gpointer data)
|
|
153 {
|
|
154 GtkWidget *window = data;
|
|
155 gtk_widget_destroy(window);
|
|
156 }
|
|
157
|
|
158 void help_window_set_key(GtkWidget *window, const gchar *key)
|
|
159 {
|
|
160 GtkWidget *text;
|
|
161
|
|
162 if (!window) return;
|
|
163
|
|
164 text = g_object_get_data(G_OBJECT(window), "text_widget");
|
|
165 if (!text) return;
|
|
166
|
|
167 gdk_window_raise(window->window);
|
|
168
|
|
169 if (key) help_window_scroll(text, key);
|
|
170 }
|
|
171
|
|
172 void help_window_set_file(GtkWidget *window, const gchar *path, const gchar *key)
|
|
173 {
|
|
174 GtkWidget *text;
|
|
175
|
|
176 if (!window || !path) return;
|
|
177
|
|
178 text = g_object_get_data(G_OBJECT(window), "text_widget");
|
|
179 if (!text) return;
|
|
180
|
|
181 gdk_window_raise(window->window);
|
|
182
|
|
183 help_window_load_text(text, path);
|
|
184 help_window_scroll(text, key);
|
|
185 }
|
|
186
|
|
187 GtkWidget *help_window_new(const gchar *title,
|
|
188 const gchar *wmclass, const gchar *subclass,
|
|
189 const gchar *path, const gchar *key)
|
|
190 {
|
|
191 GtkWidget *window;
|
|
192 GtkWidget *text;
|
|
193 GtkTextBuffer *buffer;
|
|
194 GtkWidget *vbox;
|
|
195 GtkWidget *hbox;
|
|
196 GtkWidget *button;
|
|
197 GtkWidget *scrolled;
|
|
198
|
|
199 /* window */
|
|
200
|
|
201 window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
|
|
202 gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
|
|
203 #if 0
|
|
204 gtk_container_set_border_width(GTK_CONTAINER(window), PREF_PAD_BORDER);
|
|
205 #endif
|
|
206 gtk_window_set_wmclass(GTK_WINDOW(window), subclass, wmclass);
|
|
207
|
|
208 gtk_window_set_default_size(GTK_WINDOW(window), HELP_WINDOW_WIDTH, HELP_WINDOW_HEIGHT);
|
|
209
|
|
210 gtk_window_set_title(GTK_WINDOW(window), title);
|
|
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_FLAGS(button, GTK_CAN_DEFAULT);
|
|
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
|