Permalink
Browse files

Refactoring, sub-classing etc

  • Loading branch information...
1 parent 229ee57 commit b9bd2d2678db0a9c3d9e3ea510696559cecfef8f @tim-smart tim-smart committed Oct 12, 2010
View
@@ -1,35 +0,0 @@
-#include <v8.h>
-
-#include <gtk/gtk.h>
-#include "gtk-node.h"
-
-#include <iostream>
-using namespace v8;
-
-Handle<Value> VboxAdd (const Arguments& args) {
- HandleScope scope;
-
- GtkWidget *vbox;
- GTKNODE_GET_HANDLE(vbox, args.This());
-
- GtkWidget *obj;
- GTKNODE_GET_HANDLE(obj, args[0]->ToObject());
-
- gtk_container_add(GTK_CONTAINER(vbox), obj);
- gtk_widget_show(obj);
-
- return args.This();
-}
-
-Handle<Value> Vbox (const Arguments& args) {
- HandleScope scope;
-
- Local<Object> vboxButton = Object::New();
-
- GtkWidget *vbox = gtk_vbox_new(false, 0);
-
- vboxButton->Set(String::New("handle"), v8::External::New(vbox));
- vboxButton->Set(String::New("add"), FunctionTemplate::New(VboxAdd)->GetFunction());
-
- return scope.Close(vboxButton);
-}
View
@@ -1,4 +0,0 @@
-#include <v8.h>
-using namespace v8;
-
-Handle<Value> Vbox (const Arguments& args);
View
@@ -1,17 +1,16 @@
-#include <v8.h>
+#include "ngtk.h"
#include <ev.h>
-#include <glib.h>
-#include <gtk/gtk.h>
#include <stdlib.h> // malloc, free
-#include "ngtk.h"
#include "ngtk_window.h"
#include "ngtk_hbox.h"
+#include "ngtk_vbox.h"
#include "ngtk_message_dialog.h"
#include "ngtk_button.h"
#include "ngtk_entry.h"
+
namespace ngtk {
using namespace v8;
@@ -179,6 +178,7 @@ extern "C" void init(Handle<Object> target) {
Window::Initialize(target);
Hbox::Initialize(target);
+ Vbox::Initialize(target);
MessageDialog::Initialize(target);
Button::Initialize(target);
Entry::Initialize(target);
View
@@ -3,7 +3,7 @@
#include <v8.h>
#include <glib.h>
-#include <node_object_wrap.h>
+#include <gtk/gtk.h>
namespace ngtk {
View
@@ -0,0 +1,179 @@
+#include "ngtk_box.h"
+
+namespace ngtk {
+
+using namespace v8;
+
+//gtk_box_pack_start(GtkBox *box, GtkWidget *widget, gboolean expand, gboolean fill, guint padding);
+Handle<Value> Box::PackStart (const Arguments &args) {
+ HandleScope scope;
+
+ if (!args[0]->IsObject()) {
+ return args.This();
+ }
+
+ GtkBox *box = GTK_BOX(Box::Data(args.This()));
+ GtkWidget *widget = Widget::Data(args[0]->ToObject());
+ gboolean expand, fill;
+ guint padding;
+
+ // gboolean expand
+ if (args[1]->IsBoolean()) {
+ expand = args[1]->ToBoolean()->Value();
+ } else {
+ expand = true;
+ }
+
+ // gboolean fill
+ if (args[2]->IsBoolean()) {
+ fill = args[2]->ToBoolean()->Value();
+ } else {
+ fill = true;
+ }
+
+ // guint padding
+ if (args[3]->IsNumber()) {
+ padding = args[3]->Uint32Value();
+ } else {
+ padding = 0;
+ }
+
+ gtk_box_pack_start(box, widget, expand, fill, padding);
+
+ return args.This();
+}
+
+//gtk_box_pack_end(GtkBox *box, GtkWidget *widget, gboolean expand, gboolean fill, guint padding);
+Handle<Value> Box::PackEnd (const Arguments &args) {
+ HandleScope scope;
+
+ if (!args[0]->IsObject()) {
+ return args.This();
+ }
+
+ GtkBox *box = GTK_BOX(Box::Data(args.This()));
+ GtkWidget *widget = Widget::Data(args[0]->ToObject());
+ gboolean expand, fill;
+ guint padding;
+
+ // gboolean expand
+ if (args[1]->IsBoolean()) {
+ expand = args[1]->ToBoolean()->Value();
+ } else {
+ expand = true;
+ }
+
+ // gboolean fill
+ if (args[2]->IsBoolean()) {
+ fill = args[2]->ToBoolean()->Value();
+ } else {
+ fill = true;
+ }
+
+ // guint padding
+ if (args[3]->IsNumber()) {
+ padding = args[3]->Uint32Value();
+ } else {
+ padding = 0;
+ }
+
+ gtk_box_pack_end(box, widget, expand, fill, padding);
+
+ return args.This();
+}
+
+// gtk_box_get_homogeneous(GtkBox *box);
+Handle<Value> Box::GetHomogeneous (const Arguments &args) {
+ HandleScope scope;
+
+ GtkBox *box = GTK_BOX(Box::Data(args.This()));
+
+ return scope.Close(Boolean::New(gtk_box_get_homogeneous(box)));
+}
+
+// gtk_box_set_homogeneous(GtkBox *box, gboolean homogeneous);
+Handle<Value> Box::SetHomogeneous (const Arguments &args) {
+ HandleScope scope;
+
+ GtkBox *box = GTK_BOX(Box::Data(args.This()));
+ gboolean homogeneous;
+
+ if (args[0]->IsBoolean()) {
+ homogeneous = args[0]->ToBoolean()->Value();
+ } else {
+ homogeneous = true;
+ }
+
+ gtk_box_set_homogeneous(box, homogeneous);
+
+ return args.This();
+}
+
+// gtk_box_get_spacing(GtkBox *box);
+Handle<Value> Box::GetSpacing (const Arguments &args) {
+ HandleScope scope;
+
+ GtkBox *box = GTK_BOX(Box::Data(args.This()));
+
+ return scope.Close(Integer::New(gtk_box_get_spacing(box)));
+}
+
+// gtk_box_set_spacing(GtkBox *box, gint spacing);
+Handle<Value> Box::SetSpacing (const Arguments &args) {
+ HandleScope scope;
+
+ GtkBox *box = GTK_BOX(Box::Data(args.This()));
+ gint spacing;
+
+ if (args[0]->IsNumber()) {
+ spacing = args[0]->Int32Value();
+ } else {
+ spacing = 0;
+ }
+
+ gtk_box_set_spacing(box, spacing);
+
+ return args.This();
+}
+
+//gtk_box_reorder_child(GtkBox *box, GtkWidget *widget, gint position);
+Handle<Value> Box::ReorderChild (const Arguments &args) {
+ HandleScope scope;
+
+ if (!args[0]->IsObject()) {
+ return args.This();
+ }
+
+ GtkBox *box = GTK_BOX(Box::Data(args.This()));
+ GtkWidget *widget = Widget::Data(args[0]->ToObject());
+ gint position;
+
+ // gint padding
+ if (args[1]->IsNumber()) {
+ position = args[1]->Int32Value();
+ } else {
+ position = 0;
+ }
+
+ gtk_box_reorder_child(box, widget, position);
+
+ return args.This();
+}
+
+// Export.
+void Box::SetPrototypeMethods (Handle<FunctionTemplate> constructor_template) {
+ HandleScope scope;
+
+ Container::SetPrototypeMethods(constructor_template);
+
+ NGTK_SET_PROTOTYPE_METHOD(constructor_template, "packStart", Box::PackStart);
+ NGTK_SET_PROTOTYPE_METHOD(constructor_template, "packEnd", Box::PackEnd);
+ NGTK_SET_PROTOTYPE_METHOD(constructor_template, "getHomogeneous", Box::GetHomogeneous);
+ NGTK_SET_PROTOTYPE_METHOD(constructor_template, "setHomogeneous", Box::SetHomogeneous);
+ NGTK_SET_PROTOTYPE_METHOD(constructor_template, "getSpacing", Box::GetSpacing);
+ NGTK_SET_PROTOTYPE_METHOD(constructor_template, "setSpacing", Box::SetSpacing);
+ NGTK_SET_PROTOTYPE_METHOD(constructor_template, "reorderChild", Box::ReorderChild);
+}
+
+} // namespace ngtk
+
View
@@ -0,0 +1,24 @@
+#ifndef NGTK_BOX_H_
+#define NGTK_BOX_H_
+
+#include "ngtk_container.h"
+
+namespace ngtk {
+
+class Box : public Container {
+public:
+ static void SetPrototypeMethods (v8::Handle<v8::FunctionTemplate> constructor_template);
+
+private:
+ static v8::Handle<v8::Value> PackStart (const v8::Arguments &args);
+ static v8::Handle<v8::Value> PackEnd (const v8::Arguments &args);
+ static v8::Handle<v8::Value> GetHomogeneous (const v8::Arguments &args);
+ static v8::Handle<v8::Value> SetHomogeneous (const v8::Arguments &args);
+ static v8::Handle<v8::Value> GetSpacing (const v8::Arguments &args);
+ static v8::Handle<v8::Value> SetSpacing (const v8::Arguments &args);
+ static v8::Handle<v8::Value> ReorderChild (const v8::Arguments &args);
+};
+
+} // namespace ngtk
+
+#endif
View
@@ -1,7 +1,4 @@
#include "ngtk_button.h"
-
-#include <v8.h>
-#include <gtk/gtk.h>
#include "ngtk.h"
namespace ngtk {
@@ -10,6 +7,21 @@ 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;
@@ -33,21 +45,6 @@ Button::Button (void) {
widget_ = gtk_button_new();
}
-// Check whether is an instance.
-bool Button::HasInstance (v8::Handle<v8::Value> val) {
- HandleScope scope;
-
- if (val->IsObject()) {
- v8::Local<v8::Object> obj = val->ToObject();
-
- if (constructor_template->HasInstance(obj)) {
- return true;
- }
- }
-
- return false;
-}
-
// SetLabel()
// For setting the button label.
Handle<Value> Button::SetLabel (const Arguments &args) {
@@ -73,6 +70,15 @@ Handle<Value> Button::GetLabel (const Arguments &args) {
}
// 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;
@@ -81,10 +87,7 @@ void Button::Initialize (Handle<Object> target) {
constructor_template->InstanceTemplate()->SetInternalFieldCount(1);
constructor_template->SetClassName(String::NewSymbol("Button"));
- Widget::Initialize(constructor_template);
-
- NGTK_SET_PROTOTYPE_METHOD(constructor_template, "setLabel", Button::SetLabel);
- NGTK_SET_PROTOTYPE_METHOD(constructor_template, "getLabel", Button::GetLabel);
+ Button::SetPrototypeMethods(constructor_template);
target->Set(String::NewSymbol("Button"), constructor_template->GetFunction());
}
View
@@ -1,19 +1,16 @@
#ifndef NGTK_BUTTON_H_
#define NGTK_BUTTON_H_
-#include <v8.h>
-#include <gtk/gtk.h>
-
-#include "ngtk.h"
#include "ngtk_widget.h"
namespace ngtk {
class Button : public Widget {
public:
- static void Initialize (v8::Handle<v8::Object> target);
- static Button* New (void); // public constructor
- static bool HasInstance (v8::Handle<v8::Value> val);
+ static void SetPrototypeMethods (v8::Handle<v8::FunctionTemplate> constructor_template);
+ static void Initialize (v8::Handle<v8::Object> target);
+ static Button* New (void); // public constructor
+ static bool HasInstance (v8::Handle<v8::Value> val);
private:
static v8::Persistent<v8::FunctionTemplate> constructor_template;
@@ -24,7 +21,6 @@ class Button : public Widget {
static v8::Handle<v8::Value> GetLabel (const v8::Arguments &args);
Button(void);
-
};
} // namespace ngtk
Oops, something went wrong.

0 comments on commit b9bd2d2

Please sign in to comment.