forked from tim-smart/node-gtk
/
ngtk_widget.cc
74 lines (50 loc) · 1.83 KB
/
ngtk_widget.cc
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
#include "ngtk_widget.h"
#include <node.h>
namespace ngtk {
using namespace v8;
using namespace node;
// Add signal handler.
Handle<Value> Widget::On (const Arguments &args) {
HandleScope scope;
if (args[0]->IsString() && args[1]->IsFunction()) {
GtkWidget *widget = ObjectWrap::Unwrap<Widget>(args.This())->widget_;
Persistent<Function> *callback = new Persistent<Function>();
*callback = Persistent<Function>::New(Handle<Function>::Cast(args[1]));
g_signal_connect(G_OBJECT(widget), *String::Utf8Value(args[0]->ToString()),
G_CALLBACK(Widget::onSignal), (gpointer) callback);
}
return args.This();
}
// Show()
// For showing the widget.
Handle<Value> Widget::Show (const Arguments &args) {
HandleScope scope;
GtkWidget *widget = ObjectWrap::Unwrap<Widget>(args.This())->widget_;
gtk_widget_show_all(widget);
return args.This();
}
// For destroying widgets
Handle<Value> Widget::Destroy (const Arguments &args) {
HandleScope scope;
GtkWidget *widget = ObjectWrap::Unwrap<Widget>(args.This())->widget_;
gtk_widget_destroy(widget);
return args.This();
}
// Event handler.
void Widget::onSignal (GtkWidget *widget, gpointer callback_ptr) {
HandleScope scope;
Persistent<Function> *callback = reinterpret_cast<Persistent<Function>*>(callback_ptr);
TryCatch try_catch;
(*callback)->Call(Context::GetCurrent()->Global(), 0, NULL);
if (try_catch.HasCaught()) {
FatalException(try_catch);
}
}
void Widget::SetPrototypeMethods (Handle<FunctionTemplate> constructor_template) {
HandleScope scope;
NGTK_SET_PROTOTYPE_METHOD(constructor_template, "on", Widget::On);
// Maybe over-riden by Window for ev_ref etc.
NGTK_SET_PROTOTYPE_METHOD(constructor_template, "show", Widget::Show);
NGTK_SET_PROTOTYPE_METHOD(constructor_template, "destroy", Widget::Destroy);
}
} // namespace ngtk