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