@@ -36,15 +36,15 @@ using v8::Function;
36
36
37
37
#include " context_wrap.h"
38
38
39
- #define NWEBKIT_DEF_METHOD (obj,name,func ) \
40
- obj->Set (v8::String::NewSymbol(name), \
41
- v8::FunctionTemplate::New(func)->GetFunction())
39
+ #define NWEBKIT_DEF_METHOD (obj,name,func ) \
40
+ obj->Set (v8::String::NewSymbol(name), \
41
+ v8::FunctionTemplate::New(func)->GetFunction())
42
42
43
43
namespace nwebkit {
44
44
45
- using namespace v8 ;
46
- static WebKitWebView* _webview;
47
- static Persistent<Function> _init_call_back;
45
+ using namespace v8 ;
46
+ static WebKitWebView* _webview;
47
+ static Persistent<Function> _init_call_back;
48
48
struct econtext {
49
49
GPollFD *pfd;
50
50
ev_io *iow;
@@ -58,6 +58,9 @@ struct econtext {
58
58
GMainContext *gc;
59
59
};
60
60
61
+ static WebKitWebView* nwebkit_view_init (const char *, int , int );
62
+ static gchar* filename_to_url (const char * filename);
63
+
61
64
static void timer_cb (EV_P_ ev_timer *w, int revents) {
62
65
/* nop */
63
66
}
@@ -76,13 +79,11 @@ static void prepare_cb (EV_P_ ev_prepare *w, int revents) {
76
79
g_main_context_prepare (ctx->gc , &ctx->maxpri );
77
80
78
81
while (ctx->afd < (ctx->nfd = g_main_context_query (
79
- ctx->gc ,
80
- ctx->maxpri ,
81
- &timeout,
82
- ctx->pfd ,
83
- ctx->afd ))
84
- )
85
- {
82
+ ctx->gc ,
83
+ ctx->maxpri ,
84
+ &timeout,
85
+ ctx->pfd ,
86
+ ctx->afd ))) {
86
87
free (ctx->pfd );
87
88
free (ctx->iow );
88
89
@@ -94,27 +95,25 @@ static void prepare_cb (EV_P_ ev_prepare *w, int revents) {
94
95
ctx->iow = (ev_io*) malloc (ctx->afd * sizeof (ev_io));
95
96
}
96
97
97
- for (i = 0 ; i < ctx->nfd ; ++i)
98
- {
98
+ for (i = 0 ; i < ctx->nfd ; ++i) {
99
99
GPollFD *pfd = ctx->pfd + i;
100
100
ev_io *iow = ctx->iow + i;
101
101
102
102
pfd->revents = 0 ;
103
103
104
104
ev_io_init (
105
- iow,
106
- io_cb,
107
- pfd->fd ,
108
- (pfd->events & G_IO_IN ? EV_READ : 0 )
109
- | (pfd->events & G_IO_OUT ? EV_WRITE : 0 )
110
- );
105
+ iow,
106
+ io_cb,
107
+ pfd->fd ,
108
+ (pfd->events & G_IO_IN ? EV_READ : 0 )
109
+ | (pfd->events & G_IO_OUT ? EV_WRITE : 0 )
110
+ );
111
111
iow->data = (void *)pfd;
112
112
ev_set_priority (iow, EV_MINPRI);
113
113
ev_io_start (EV_A_ iow);
114
114
}
115
115
116
- if (timeout >= 0 )
117
- {
116
+ if (timeout >= 0 ) {
118
117
// if (timeout == 0)
119
118
// g_main_context_dispatch (ctx->gc);
120
119
@@ -128,18 +127,16 @@ static void check_cb (EV_P_ ev_check *w, int revents) {
128
127
struct econtext *ctx = (struct econtext *)(((char *)w) - offsetof (struct econtext , cw));
129
128
int i;
130
129
131
- for (i = 0 ; i < ctx->nfd ; ++i)
132
- {
130
+ for (i = 0 ; i < ctx->nfd ; ++i) {
133
131
ev_io *iow = ctx->iow + i;
134
132
135
- if (ev_is_pending (iow))
136
- {
133
+ if (ev_is_pending (iow)) {
137
134
GPollFD *pfd = ctx->pfd + i;
138
135
int revents = ev_clear_pending (EV_A_ iow);
139
136
140
137
pfd->revents |= pfd->events &
141
- ((revents & EV_READ ? G_IO_IN : 0 )
142
- | (revents & EV_WRITE ? G_IO_OUT : 0 ));
138
+ ((revents & EV_READ ? G_IO_IN : 0 )
139
+ | (revents & EV_WRITE ? G_IO_OUT : 0 ));
143
140
}
144
141
145
142
ev_io_stop (EV_A_ iow);
@@ -232,47 +229,47 @@ static Handle<Value> Open (const Arguments &args) {
232
229
return args.This ();
233
230
}
234
231
235
- static Handle<Value> LoadString (const Arguments &args) {
232
+ static Handle<Value> LoadString (const Arguments &args) {
236
233
237
- HandleScope scope;
234
+ HandleScope scope;
238
235
239
- Local<String> arg0 = args[0 ]->ToString ();
236
+ Local<String> arg0 = args[0 ]->ToString ();
240
237
241
- String::Utf8Value str (arg0);
242
- const char * content = ToCString (str);
243
- const char * mime_type = NULL ;
244
- const char * base_uri = " " ;
245
- String::Utf8Value arg1 (args[1 ]->ToString ());
246
- String::Utf8Value arg2 (args[2 ]->ToString ());
238
+ String::Utf8Value str (arg0);
239
+ const char * content = ToCString (str);
240
+ const char * mime_type = NULL ;
241
+ const char * base_uri = " " ;
242
+ String::Utf8Value arg1 (args[1 ]->ToString ());
243
+ String::Utf8Value arg2 (args[2 ]->ToString ());
247
244
248
- if (args[1 ]->IsString ())
249
- mime_type = *arg1;
250
- if (args[2 ]->IsString ())
251
- base_uri = *arg2;
245
+ if (args[1 ]->IsString ())
246
+ mime_type = *arg1;
247
+ if (args[2 ]->IsString ())
248
+ base_uri = *arg2;
252
249
253
- webkit_web_view_load_string (_webview, content, mime_type, NULL , base_uri);
250
+ webkit_web_view_load_string (_webview, content, mime_type, NULL , base_uri);
254
251
255
- return args.This ();
256
- }
252
+ return args.This ();
253
+ }
257
254
258
- static Handle<Value> Reload (const Arguments &args) {
259
- webkit_web_view_reload (_webview);
260
- return args.This ();
261
- }
255
+ static Handle<Value> Reload (const Arguments &args) {
256
+ webkit_web_view_reload (_webview);
257
+ return args.This ();
258
+ }
262
259
263
- static Handle<Value> SetViewMode (const Arguments &args) {
260
+ static Handle<Value> SetViewMode (const Arguments &args) {
264
261
265
- int mode;
266
- if (args[0 ]->IsNumber ()) {
267
- mode = args[1 ]->Int32Value ();
268
- } else {
269
- mode = WEBKIT_WEB_VIEW_VIEW_MODE_WINDOWED;
270
- }
262
+ int mode;
263
+ if (args[0 ]->IsNumber ()) {
264
+ mode = args[1 ]->Int32Value ();
265
+ } else {
266
+ mode = WEBKIT_WEB_VIEW_VIEW_MODE_WINDOWED;
267
+ }
271
268
272
- webkit_web_view_set_view_mode (_webview, (WebKitWebViewViewMode)mode);
269
+ webkit_web_view_set_view_mode (_webview, (WebKitWebViewViewMode)mode);
273
270
274
- return args.This ();
275
- }
271
+ return args.This ();
272
+ }
276
273
277
274
278
275
void init (Handle<Object> target) {
@@ -315,5 +312,69 @@ void init(Handle<Object> target) {
315
312
ev_init (&ctx->tw , timer_cb);
316
313
ev_set_priority (&ctx->tw , EV_MINPRI);
317
314
}
318
- NODE_MODULE (nwebkit, init)
315
+
316
+ static void destroy_cb (GtkWidget* widget, GtkWidget* window)
317
+ {
318
+ ev_unref (EV_DEFAULT_UC);
319
+ }
320
+
321
+ static void title_change_cb (WebKitWebView* webview,
322
+ GParamSpec* pspec,
323
+ GtkWidget* window)
324
+ {
325
+ const gchar* title = webkit_web_view_get_title (WEBKIT_WEB_VIEW (webview));
326
+ if (title) {
327
+ gtk_window_set_title (GTK_WINDOW (window), title);
328
+ }
329
+ }
330
+
331
+
332
+ static gchar* filename_to_url (const char * filename)
333
+ {
334
+ if (!g_file_test (filename, G_FILE_TEST_EXISTS))
335
+ return 0 ;
336
+
337
+ GFile *gfile = g_file_new_for_path (filename);
338
+ gchar *file_url = g_file_get_uri (gfile);
339
+ g_object_unref (gfile);
340
+
341
+ return file_url;
342
+ }
343
+
344
+ static WebKitWebView* nwebkit_view_init (const char * uri, int width, int height)
345
+ {
346
+ WebKitWebView *webview;
347
+ gtk_init (NULL , NULL );
348
+
349
+ GtkWidget *window;
350
+ gchar *url = filename_to_url (uri);
351
+ window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
352
+ if (!width || !height)
353
+ gtk_window_set_default_size (GTK_WINDOW (window), 800 , 600 );
354
+ else
355
+ gtk_window_set_default_size (GTK_WINDOW (window), width, height);
356
+
357
+ webview = WEBKIT_WEB_VIEW (webkit_web_view_new ());
358
+ GtkWidget *scrolled_win = gtk_scrolled_window_new (NULL , NULL );
359
+ gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
360
+ GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
361
+
362
+ gtk_container_add (GTK_CONTAINER (scrolled_win), GTK_WIDGET (webview));
363
+ gtk_container_add (GTK_CONTAINER (window), GTK_WIDGET (scrolled_win));
364
+
365
+ g_signal_connect (window, " destroy" , G_CALLBACK (destroy_cb), NULL );
366
+ g_signal_connect (webview, " notify::title" ,
367
+ G_CALLBACK (title_change_cb), window);
368
+
369
+ webkit_web_view_load_uri (webview, url ? url : uri);
370
+ g_free (url);
371
+
372
+ gtk_widget_grab_focus (GTK_WIDGET (webview));
373
+ gtk_widget_show_all (window);
374
+ ev_ref (EV_DEFAULT_UC);
375
+
376
+ return webview;
377
+ }
378
+
379
+ NODE_MODULE (nwebkit, init)
319
380
} // namespace nwebkit
0 commit comments