-
Notifications
You must be signed in to change notification settings - Fork 0
/
view.c
272 lines (239 loc) · 9.32 KB
/
view.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#include "_cgo_export.h"
#include "view_my.h"
#include <gdk/gdkkeysyms.h>
#include <gtk/gtk.h>
#include <string.h>
#include <webkit2/webkit2.h>
char *view_ui_single =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
"<!-- Generated with glade 3.20.0 -->\n"
"<interface>\n"
" <requires lib=\"gtk+\" version=\"3.18\"/>\n"
" <object class=\"GtkWindow\" id=\"main-window\">\n"
" <property name=\"can_focus\">False</property>\n"
" <property name=\"border_width\">5</property>\n"
" <property name=\"title\" translatable=\"yes\">Alpino "
"Viewer</property>\n"
" <property name=\"default_width\">1800</property>\n"
" <property name=\"default_height\">1100</property>\n"
" <property name=\"icon_name\">face-monkey</property>\n"
" <signal name=\"delete-event\" handler=\"delete_event\" "
"swapped=\"no\"/>\n"
" <signal name=\"destroy\" handler=\"destroy\" swapped=\"no\"/>\n"
" <child>\n"
" <object class=\"GtkBox\" id=\"my-box\">\n"
" <property name=\"visible\">True</property>\n"
" <property name=\"can_focus\">False</property>\n"
" <property name=\"vexpand\">True</property>\n"
" <property name=\"orientation\">vertical</property>\n"
" <child>\n"
" <placeholder/>\n"
" </child>\n"
" </object>\n"
" </child>\n"
" </object>\n"
"</interface>\n";
char *view_ui_multi = "\
<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
<!-- Generated with glade 3.40.0 -->\n\
<interface>\n\
<requires lib=\"gtk+\" version=\"3.18\"/>\n\
<object class=\"GtkWindow\" id=\"main-window\">\n\
<property name=\"can-focus\">False</property>\n\
<property name=\"border-width\">5</property>\n\
<property name=\"title\" translatable=\"yes\">Alpino Viewer</property>\n\
<property name=\"default-width\">1600</property>\n\
<property name=\"default-height\">1000</property>\n\
<property name=\"icon-name\">face-monkey</property>\n\
<signal name=\"delete-event\" handler=\"delete_event\" swapped=\"no\"/>\n\
<signal name=\"destroy\" handler=\"destroy\" swapped=\"no\"/>\n\
<child>\n\
<object class=\"GtkPaned\" id=\"panels\">\n\
<property name=\"visible\">True</property>\n\
<property name=\"can-focus\">True</property>\n\
<child>\n\
<object class=\"GtkScrolledWindow\" id=\"filepanel\">\n\
<property name=\"visible\">True</property>\n\
<property name=\"can-focus\">True</property>\n\
<property name=\"hscrollbar-policy\">never</property>\n\
<property name=\"shadow-type\">in</property>\n\
<child>\n\
<object class=\"GtkTreeView\" id=\"files\">\n\
<property name=\"visible\">True</property>\n\
<property name=\"can-focus\">True</property>\n\
<child internal-child=\"selection\">\n\
<object class=\"GtkTreeSelection\" id=\"file\"/>\n\
</child>\n\
</object>\n\
</child>\n\
</object>\n\
<packing>\n\
<property name=\"resize\">False</property>\n\
<property name=\"shrink\">True</property>\n\
</packing>\n\
</child>\n\
<child>\n\
<object class=\"GtkBox\" id=\"my-box\">\n\
<property name=\"visible\">True</property>\n\
<property name=\"can-focus\">False</property>\n\
<property name=\"orientation\">vertical</property>\n\
<child>\n\
<placeholder/>\n\
</child>\n\
</object>\n\
<packing>\n\
<property name=\"resize\">True</property>\n\
<property name=\"shrink\">True</property>\n\
</packing>\n\
</child>\n\
</object>\n\
</child>\n\
</object>\n\
</interface>\n";
int nfiles = 0;
char const **filenames = NULL;
WebKitWebView *webview = NULL;
GtkWidget *window = NULL;
G_MODULE_EXPORT void tree_row_activated_cb(GtkTreeView *treeview,
GtkTreePath *path,
GtkTreeViewColumn *column,
gpointer userdata) {
GtkTreeIter iter;
GtkTreeModel *model;
gchar *item;
model = gtk_tree_view_get_model(treeview);
if (gtk_tree_model_get_iter(model, &iter, path)) {
gtk_tree_model_get(model, &iter, 0, &item, -1);
go_message(idSELECT, item);
g_free(item);
}
}
G_MODULE_EXPORT gboolean web_view_key_pressed(WebKitWebView *web_view,
GdkEventKey *event,
gpointer user_data) {
if (event->keyval == GDK_KEY_q && (event->state & GDK_CONTROL_MASK)) {
gtk_main_quit();
return TRUE;
}
if (event->keyval == GDK_KEY_minus && (event->state & GDK_CONTROL_MASK)) {
gdouble lvl;
lvl = webkit_web_view_get_zoom_level(webview) - .05;
if (lvl < .2) {
lvl = .2;
}
webkit_web_view_set_zoom_level(webview, lvl);
}
if (event->keyval == GDK_KEY_equal && (event->state & GDK_CONTROL_MASK)) {
gdouble lvl;
lvl = webkit_web_view_get_zoom_level(webview) + .05;
if (lvl > 3) {
lvl = 3;
}
webkit_web_view_set_zoom_level(webview, lvl);
}
if (event->keyval == GDK_KEY_0 && (event->state & GDK_CONTROL_MASK)) {
webkit_web_view_set_zoom_level(webview, 1);
}
return FALSE;
}
static GtkTreeModel *create_and_fill_model(void) {
GtkListStore *store = gtk_list_store_new(1, G_TYPE_STRING);
/* Append a row and fill in some data */
GtkTreeIter iter;
for (int i = 0; i < nfiles; i++) {
gtk_list_store_append(store, &iter);
gtk_list_store_set(store, &iter, 0, filenames[i], -1);
}
return GTK_TREE_MODEL(store);
}
void setnfiles(int n) {
filenames = (char const **)malloc(n * sizeof(char const *));
}
void addfile(char const *filename) { filenames[nfiles++] = filename; }
void reload(char const *title) {
gtk_window_set_title(GTK_WINDOW(window), title);
webkit_web_view_reload(webview);
}
/*
// vanaf glib 2.50
GLogWriterOutput myGLogWriterFunc(GLogLevelFlags log_level,
const GLogField *fields, gsize n_fields,
gpointer user_data) {
return G_LOG_WRITER_HANDLED;
}
*/
/*
// ouder dan glib 2.50
void myGLogFunc(const gchar *log_domain, GLogLevelFlags log_level,
const gchar *message, gpointer unused_data) {
printf("TEST: %s: %s\n", log_domain, message);
}
*/
void run(char const *url, char const *title) {
static char buf[1000];
GtkBuilder *builder;
GError *error = NULL;
GtkWidget *box, *files;
WebKitSettings *settings = NULL;
/*
// vanaf glib 2.50
g_log_set_writer_func(myGLogWriterFunc, NULL, NULL);
*/
/*
// ouder dan glib 2.50
g_log_set_default_handler(myGLogFunc, NULL);
*/
gtk_init(NULL, NULL);
builder = gtk_builder_new();
if (!gtk_builder_add_from_string(
builder, nfiles > 1 ? view_ui_multi : view_ui_single, -1, &error)) {
g_snprintf(buf, 999, "%s", error->message);
go_message(idERROR, buf);
return;
}
gtk_builder_connect_signals(builder, NULL);
window = GTK_WIDGET(gtk_builder_get_object(builder, "main-window"));
if (strlen(title) > 0) {
gtk_window_set_title(GTK_WINDOW(window), title);
}
if (nfiles > 1) {
files = GTK_WIDGET(gtk_builder_get_object(builder, "files"));
GtkCellRenderer *renderer;
renderer = gtk_cell_renderer_text_new();
gtk_tree_view_insert_column_with_attributes(
GTK_TREE_VIEW(files), -1, "Bestand", renderer, "text", 0, NULL);
GtkTreeModel *model = create_and_fill_model();
gtk_tree_view_set_model(GTK_TREE_VIEW(files), model);
g_object_unref(model);
g_signal_connect(G_OBJECT(files), "row-activated",
G_CALLBACK(tree_row_activated_cb), NULL);
}
box = GTK_WIDGET(gtk_builder_get_object(builder, "my-box"));
settings = webkit_settings_new();
webkit_settings_set_default_font_size(settings, 18);
webkit_settings_set_default_monospace_font_size(settings, 14);
webkit_settings_set_default_charset(settings, "utf-8");
webkit_settings_set_default_font_family(settings, "serif");
webview = WEBKIT_WEB_VIEW(webkit_web_view_new_with_settings(settings));
gtk_box_pack_start(GTK_BOX(box), GTK_WIDGET(webview), TRUE, TRUE, 0);
webkit_web_view_load_uri(webview, url);
g_signal_connect(window, "key-press-event",
G_CALLBACK(web_view_key_pressed), NULL);
go_message(idREADY, "Let's begin!");
gtk_widget_show_all(window);
gtk_main();
}
G_MODULE_EXPORT gboolean delete_event(GtkWidget *widget, GdkEvent *event,
gpointer data) {
/* If you return FALSE in the "delete-event" signal handler,
* GTK will emit the "destroy" signal. Returning TRUE means
* you don't want the window to be destroyed.
* This is useful for popping up 'are you sure you want to quit?'
* type dialogs. */
go_message(idDELETE, "Delete!");
return FALSE;
}
G_MODULE_EXPORT void destroy(GtkWidget *widget, gpointer data) {
gtk_main_quit();
go_message(idDESTROY, "Destroy!");
}