forked from tim-smart/node-gtk
/
ngtk_button.cc
96 lines (66 loc) · 2.22 KB
/
ngtk_button.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include "ngtk_button.h"
#include "ngtk.h"
namespace ngtk {
using namespace v8;
Persistent<FunctionTemplate> Button::constructor_template;
// Check whether is an instance.
bool Button::HasInstance (Handle<Value> val) {
HandleScope scope;
if (val->IsObject()) {
Local<Object> obj = val->ToObject();
if (constructor_template->HasInstance(obj)) {
return true;
}
}
return false;
}
// Public constructor
Button* Button::New (void) {
HandleScope scope;
Local<Object> button = constructor_template->GetFunction()->NewInstance();
return ObjectWrap::Unwrap<Button>(button);
}
// ECMAScript constructor.
Handle<Value> Button::New (const Arguments &args) {
HandleScope scope;
Button *button = new Button();
button->Wrap(args.This());
return args.This();
}
Button::Button (void) {
widget_ = gtk_button_new();
}
// SetLabel()
// For setting the button label.
Handle<Value> Button::SetLabel (const Arguments &args) {
HandleScope scope;
if (args[0]->IsString()) {
GtkWidget *button = Button::Data(args.This());
gtk_button_set_label(GTK_BUTTON(button), *String::Utf8Value(args[0]->ToString()));
}
return args.This();
}
// GetLabel()
// For getting the button label.
Handle<Value> Button::GetLabel (const Arguments &args) {
HandleScope scope;
GtkWidget *button = Button::Data(args.This());
return scope.Close(String::New(gtk_button_get_label(GTK_BUTTON(button))));
}
// Export.
void Button::SetPrototypeMethods (Handle<FunctionTemplate> constructor_template) {
HandleScope scope;
Widget::SetPrototypeMethods(constructor_template);
NGTK_SET_PROTOTYPE_METHOD(constructor_template, "setLabel", Button::SetLabel);
NGTK_SET_PROTOTYPE_METHOD(constructor_template, "getLabel", Button::GetLabel);
}
void Button::Initialize (Handle<Object> target) {
HandleScope scope;
Local<FunctionTemplate> t = FunctionTemplate::New(Button::New);
constructor_template = Persistent<FunctionTemplate>::New(t);
constructor_template->InstanceTemplate()->SetInternalFieldCount(1);
constructor_template->SetClassName(String::NewSymbol("Button"));
Button::SetPrototypeMethods(constructor_template);
target->Set(String::NewSymbol("Button"), constructor_template->GetFunction());
}
} // namespace ngtk