Skip to content
Browse files

Entry

  • Loading branch information...
1 parent 901a8ec commit cab01782e627b051062123f432bda27f5f91bb03 @tim-smart tim-smart committed Oct 11, 2010
Showing with 340 additions and 105 deletions.
  1. +2 −61 gtk.js
  2. +0 −11 src/gtk-node.h
  3. +2 −0 src/ngtk.cc
  4. +2 −2 src/ngtk_button.cc
  5. +227 −0 src/ngtk_entry.cc
  6. +42 −0 src/ngtk_entry.h
  7. +17 −3 src/ngtk_message_dialog.cc
  8. +3 −3 src/ngtk_message_dialog.h
  9. +2 −2 src/ngtk_widget.h
  10. +25 −12 src/ngtk_window.cc
  11. +3 −2 src/ngtk_window.h
  12. +14 −9 test.js
  13. +1 −0 wscript
View
63 gtk.js
@@ -1,69 +1,10 @@
-var gtk = require('./build/default/node-gtk'),
+var gtk = require('./build/default/gtk'),
EventEmitter = require('events').EventEmitter;
-exports.main = function() {
- if (!gtk.main()) setTimeout(function() {
- exports.main();
- },20);
-};
-
var proto;
var Window = function (params) {
- var key,
- self = this,
- keys = Object.keys(params);
-
- this.widget = gtk.window();
-
- this.widget.onShow = function () {
- exports.main();
- self.emit('show');
- };
- this.widget.onClose = function () {
- console.log('close');
- self.emit('close');
- };
-
- for (var i = 0, il = keys.length; i < il; i++) {
- key = keys[i];
-
- if (this.hasOwnProperty(key)) this[key] = params[key];
- }
};
-// Export Window.
-exports.Window = Window;
-
-Window.prototype = Object.create(EventEmitter.prototype);
-
-proto = Window.prototype;
-
-// Window#title
-Object.defineProperty(proto, 'title', {
- enumerable: true,
- get: function () {
- return this.widget.getTitle();
- },
- set: function (value) {
- this.widget.setTitle(value);
- return this;
- }
-});
-
-// Window#resizable
-Object.defineProperty(proto, 'resizable', {
- enumerable: true,
- get: function () {
- return this.widget.getResizable();
- },
- set: function (value) {
- this.widget.setResizable(!!value);
- return this;
- }
-});
-
-proto.show = function () {
- this.widget.show();
- return this;
+var Dialog = function (params) {
};
View
11 src/gtk-node.h
@@ -1,11 +0,0 @@
-#include <v8.h>
-using namespace v8;
-
-// For getting handles.
-#define GTKNODE_GET_HANDLE(target, wrap) \
-target = static_cast<GtkWidget*>( \
- v8::Handle<v8::External>::Cast(wrap->Get(String::New("handle")))->Value());
-
-//global var
-extern int main_loop_level;
-
View
2 src/ngtk.cc
@@ -10,6 +10,7 @@
#include "ngtk_window.h"
#include "ngtk_message_dialog.h"
#include "ngtk_button.h"
+#include "ngtk_entry.h"
namespace ngtk {
@@ -185,6 +186,7 @@ extern "C" void init(Handle<Object> target) {
Window::Initialize(target);
MessageDialog::Initialize(target);
Button::Initialize(target);
+ Entry::Initialize(target);
Loop::Initialize(target);
}
View
4 src/ngtk_button.cc
@@ -39,7 +39,7 @@ Handle<Value> Button::SetLabel (const Arguments &args) {
HandleScope scope;
if (args[0]->IsString()) {
- GtkWidget *button = Widget::Gtk(args.This());
+ GtkWidget *button = Button::Data(args.This());
gtk_button_set_label(GTK_BUTTON(button), *String::Utf8Value(args[0]->ToString()));
}
@@ -52,7 +52,7 @@ Handle<Value> Button::SetLabel (const Arguments &args) {
Handle<Value> Button::GetLabel (const Arguments &args) {
HandleScope scope;
- GtkWidget *button = Widget::Gtk(args.This());
+ GtkWidget *button = Button::Data(args.This());
return scope.Close(String::New(gtk_button_get_label(GTK_BUTTON(button))));
}
View
227 src/ngtk_entry.cc
@@ -0,0 +1,227 @@
+#include "ngtk_entry.h"
+
+#include <assert.h>
+#include <v8.h>
+#include <gtk/gtk.h>
+
+#include "ngtk.h"
+
+namespace ngtk {
+
+using namespace v8;
+
+Persistent<FunctionTemplate> Entry::constructor_template;
+
+// Public constructor
+Entry* Entry::New (void) {
+ HandleScope scope;
+
+ Local<Object> dialog = constructor_template->GetFunction()->NewInstance();
+
+ return ObjectWrap::Unwrap<Entry>(dialog);
+}
+
+// ECMAScript constructor.
+Handle<Value> Entry::New (const Arguments &args) {
+ HandleScope scope;
+
+ Entry *entry = new Entry();
+ entry->Wrap(args.This());
+
+ return args.This();
+}
+
+Entry::Entry (void) {
+ widget_ = gtk_entry_new();
+}
+
+// gtk_entry_set_text()
+Handle<Value> Entry::SetText (const Arguments &args) {
+ HandleScope scope;
+
+ if (args[0]->IsString()) {
+ GtkWidget *entry = Entry::Data(args.This());
+
+ gtk_entry_set_text(GTK_ENTRY(entry), *String::Utf8Value(args[0]->ToString()));
+ }
+
+ return args.This();
+}
+
+// gtk_entry_get_text()
+Handle<Value> Entry::GetText (const Arguments &args) {
+ HandleScope scope;
+
+ GtkWidget *entry = Entry::Data(args.This());
+
+ return scope.Close(String::New(gtk_entry_get_text(GTK_ENTRY(entry))));
+}
+
+// gtk_entry_append_text()
+Handle<Value> Entry::AppendText (const Arguments &args) {
+ HandleScope scope;
+
+ if (args[0]->IsString()) {
+ GtkWidget *entry = Entry::Data(args.This());
+
+ gtk_entry_append_text(GTK_ENTRY(entry), *String::Utf8Value(args[0]->ToString()));
+ }
+
+ return args.This();
+}
+
+// gtk_entry_prepend_text()
+Handle<Value> Entry::PrependText (const Arguments &args) {
+ HandleScope scope;
+
+ if (args[0]->IsString()) {
+ GtkWidget *entry = Entry::Data(args.This());
+
+ gtk_entry_prepend_text(GTK_ENTRY(entry), *String::Utf8Value(args[0]->ToString()));
+ }
+
+ return args.This();
+}
+
+// gtk_entry_get_text_length()
+Handle<Value> Entry::GetTextLength (const Arguments &args) {
+ HandleScope scope;
+
+ GtkWidget *entry = Entry::Data(args.This());
+
+ return scope.Close(Number::New(gtk_entry_get_text_length(GTK_ENTRY(entry))));
+}
+
+// gtk_entry_select_region()
+Handle<Value> Entry::SelectRegion (const Arguments &args) {
+ HandleScope scope;
+
+ if (args[0]->IsNumber() && args[1]->IsNumber()) {
+ GtkWidget *entry = Entry::Data(args.This());
+
+ gtk_entry_select_region(GTK_ENTRY(entry), args[0]->Int32Value(),
+ args[1]->Int32Value());
+ }
+
+ return args.This();
+}
+
+// gtk_entry_set_visibility()
+Handle<Value> Entry::SetVisibility (const Arguments &args) {
+ HandleScope scope;
+
+ if (args[0]->IsBoolean()) {
+ GtkWidget *entry = Entry::Data(args.This());
+
+ gtk_entry_set_visibility(GTK_ENTRY(entry), args[0]->ToBoolean()->Value());
+ }
+
+ return args.This();
+}
+
+// gtk_entry_get_visibility()
+Handle<Value> Entry::GetVisibility (const Arguments &args) {
+ HandleScope scope;
+
+ GtkWidget *entry = Entry::Data(args.This());
+
+ return scope.Close(Boolean::New(gtk_entry_get_visibility(GTK_ENTRY(entry))));
+}
+
+// gtk_editable_set_editable()
+Handle<Value> Entry::SetEditable (const Arguments &args) {
+ HandleScope scope;
+
+ if (args[0]->IsBoolean()) {
+ GtkWidget *entry = Entry::Data(args.This());
+
+ gtk_editable_set_editable(GTK_EDITABLE(entry), args[0]->ToBoolean()->Value());
+ }
+
+ return args.This();
+}
+
+// gtk_editable_get_editable()
+Handle<Value> Entry::GetEditable (const Arguments &args) {
+ HandleScope scope;
+
+ GtkWidget *entry = Entry::Data(args.This());
+
+ return scope.Close(Boolean::New(gtk_editable_get_editable(GTK_EDITABLE(entry))));
+}
+
+// gtk_entry_set_max_length()
+Handle<Value> Entry::SetMaxLength (const Arguments &args) {
+ HandleScope scope;
+
+ if (args[0]->IsNumber()) {
+ GtkWidget *entry = Entry::Data(args.This());
+
+ gtk_entry_set_max_length(GTK_ENTRY(entry), args[0]->Int32Value());
+ }
+
+ return args.This();
+}
+
+// gtk_entry_get_max_length()
+Handle<Value> Entry::GetMaxLength (const Arguments &args) {
+ HandleScope scope;
+
+ GtkWidget *entry = Entry::Data(args.This());
+
+ return scope.Close(Number::New(gtk_entry_get_max_length(GTK_ENTRY(entry))));
+}
+
+// gtk_entry_set_alignment()
+Handle<Value> Entry::SetAlignment (const Arguments &args) {
+ HandleScope scope;
+
+ if (args[0]->IsNumber()) {
+ GtkWidget *entry = Entry::Data(args.This());
+
+ gtk_entry_set_alignment(GTK_ENTRY(entry), args[0]->NumberValue());
+ }
+
+ return args.This();
+}
+
+// gtk_entry_get_alignment()
+Handle<Value> Entry::GetAlignment (const Arguments &args) {
+ HandleScope scope;
+
+ GtkWidget *entry = Entry::Data(args.This());
+
+ return scope.Close(Number::New(gtk_entry_get_alignment(GTK_ENTRY(entry))));
+}
+
+// Export.
+void Entry::Initialize (Handle<Object> target) {
+ HandleScope scope;
+
+ Local<FunctionTemplate> t = FunctionTemplate::New(Entry::New);
+ constructor_template = Persistent<FunctionTemplate>::New(t);
+ constructor_template->InstanceTemplate()->SetInternalFieldCount(1);
+ constructor_template->SetClassName(String::NewSymbol("Entry"));
+
+ Widget::Initialize(constructor_template);
+
+ NGTK_SET_PROTOTYPE_METHOD(constructor_template, "setText", Entry::SetText);
+ NGTK_SET_PROTOTYPE_METHOD(constructor_template, "getText", Entry::GetText);
+ NGTK_SET_PROTOTYPE_METHOD(constructor_template, "appendText", Entry::AppendText);
+ NGTK_SET_PROTOTYPE_METHOD(constructor_template, "prependText", Entry::PrependText);
+ NGTK_SET_PROTOTYPE_METHOD(constructor_template, "getTextLength", Entry::GetTextLength);
+ NGTK_SET_PROTOTYPE_METHOD(constructor_template, "selectRegion", Entry::SelectRegion);
+ NGTK_SET_PROTOTYPE_METHOD(constructor_template, "setVisibility", Entry::SetVisibility);
+ NGTK_SET_PROTOTYPE_METHOD(constructor_template, "getVisibility", Entry::GetVisibility);
+ NGTK_SET_PROTOTYPE_METHOD(constructor_template, "setEditable", Entry::SetEditable);
+ NGTK_SET_PROTOTYPE_METHOD(constructor_template, "getEditable", Entry::GetEditable);
+ NGTK_SET_PROTOTYPE_METHOD(constructor_template, "setMaxLength", Entry::SetMaxLength);
+ NGTK_SET_PROTOTYPE_METHOD(constructor_template, "getMaxLength", Entry::GetMaxLength);
+ NGTK_SET_PROTOTYPE_METHOD(constructor_template, "setAlignment", Entry::SetAlignment);
+ NGTK_SET_PROTOTYPE_METHOD(constructor_template, "getAlignment", Entry::GetAlignment);
+
+ target->Set(String::NewSymbol("Entry"), constructor_template->GetFunction());
+}
+
+} // namespace ngtk
+
View
42 src/ngtk_entry.h
@@ -0,0 +1,42 @@
+#ifndef NGTK_ENTRY_H_
+#define NGTK_ENTRY_H_
+
+#include <v8.h>
+#include <gtk/gtk.h>
+
+#include "ngtk.h"
+#include "ngtk_widget.h"
+
+namespace ngtk {
+
+class Entry : public Widget {
+public:
+ static void Initialize (v8::Handle<v8::Object> target);
+ static Entry* New (void); // public constructor
+
+private:
+ static v8::Persistent<v8::FunctionTemplate> constructor_template;
+
+ static v8::Handle<v8::Value> New (const v8::Arguments &args);
+ static v8::Handle<v8::Value> SetText (const v8::Arguments &args);
+ static v8::Handle<v8::Value> GetText (const v8::Arguments &args);
+ static v8::Handle<v8::Value> AppendText (const v8::Arguments &args);
+ static v8::Handle<v8::Value> PrependText (const v8::Arguments &args);
+ static v8::Handle<v8::Value> GetTextLength (const v8::Arguments &args);
+ static v8::Handle<v8::Value> SelectRegion (const v8::Arguments &args);
+ static v8::Handle<v8::Value> SetVisibility (const v8::Arguments &args);
+ static v8::Handle<v8::Value> GetVisibility (const v8::Arguments &args);
+ // gtk_editable_{get,set}_editable
+ static v8::Handle<v8::Value> SetEditable (const v8::Arguments &args);
+ static v8::Handle<v8::Value> GetEditable (const v8::Arguments &args);
+ static v8::Handle<v8::Value> SetMaxLength (const v8::Arguments &args);
+ static v8::Handle<v8::Value> GetMaxLength (const v8::Arguments &args);
+ static v8::Handle<v8::Value> SetAlignment (const v8::Arguments &args);
+ static v8::Handle<v8::Value> GetAlignment (const v8::Arguments &args);
+
+ Entry(void);
+};
+
+} // namespace ngtk
+
+#endif
View
20 src/ngtk_message_dialog.cc
@@ -1,8 +1,11 @@
#include "ngtk_message_dialog.h"
+#include <assert.h>
#include <v8.h>
#include <gtk/gtk.h>
+
#include "ngtk.h"
+#include "ngtk_window.h"
namespace ngtk {
@@ -12,7 +15,7 @@ Persistent<FunctionTemplate> MessageDialog::constructor_template;
// Public constructor
MessageDialog* MessageDialog::New (Window *parent, GtkDialogFlags flags,
- GtkMessageType type, GtkButtonsType buttons, gchar *message) {
+ GtkMessageType type, GtkButtonsType buttons, char *message) {
HandleScope scope;
Local<Value> argv[5];
@@ -31,9 +34,20 @@ MessageDialog* MessageDialog::New (Window *parent, GtkDialogFlags flags,
Handle<Value> MessageDialog::New (const Arguments &args) {
HandleScope scope;
- // TODO: Type checking.
+ if (4 > args.Length()) {
+ return ThrowException(Exception::Error(
+ String::New("Expects arguments: window or null, dialog flags, message type, button flags[, message]")));
+ }
+
+ GtkWindow *parent;
+
+ if (Window::HasInstance(args[0])) {
+ parent = GTK_WINDOW(Window::Data(args[0]->ToObject()));
+ } else {
+ parent = NULL;
+ }
- MessageDialog *dialog = new MessageDialog(NULL,
+ MessageDialog *dialog = new MessageDialog(parent,
(GtkDialogFlags) args[1]->Int32Value(), (GtkMessageType) args[2]->Int32Value(),
(GtkButtonsType) args[3]->Int32Value(), *String::Utf8Value(args[4]->ToString()));
View
6 src/ngtk_message_dialog.h
@@ -14,15 +14,15 @@ class MessageDialog : public Widget {
public:
static void Initialize (v8::Handle<v8::Object> target);
static MessageDialog* New (Window *parent, GtkDialogFlags flags,
- GtkMessageType type, GtkButtonsType buttons, gchar *message); // public constructor
+ GtkMessageType type, GtkButtonsType buttons, char *message); // public constructor
private:
static v8::Persistent<v8::FunctionTemplate> constructor_template;
- static v8::Handle<v8::Value> New (const v8::Arguments &args);
+ static v8::Handle<v8::Value> New (const v8::Arguments &args);
MessageDialog(GtkWindow *parent, GtkDialogFlags flags,
- GtkMessageType type, GtkButtonsType buttons, gchar *message);
+ GtkMessageType type, GtkButtonsType buttons, char *message);
};
} // namespace ngtk
View
4 src/ngtk_widget.h
@@ -23,7 +23,7 @@ class Widget : public node::ObjectWrap {
}
// For getting the underlying GtkWidget
- static inline GtkWidget* Gtk (v8::Handle<v8::Object> obj) {
+ static inline GtkWidget* Data (v8::Handle<v8::Object> obj) {
v8::HandleScope scope;
return node::ObjectWrap::Unwrap<Widget>(obj)->widget_;
@@ -54,7 +54,7 @@ class Widget : public node::ObjectWrap {
static inline v8::Handle<v8::Value> Show (const v8::Arguments &args) {
v8::HandleScope scope;
- GtkWidget *widget = Widget::Gtk(args.This());
+ GtkWidget *widget = node::ObjectWrap::Unwrap<Widget>(args.This())->widget_;
gtk_widget_show_all(widget);
View
37 src/ngtk_window.cc
@@ -36,15 +36,28 @@ Window::Window (void) {
widget_ = gtk_window_new(GTK_WINDOW_TOPLEVEL);
}
+// Check whether is an instance.
+bool Window::HasInstance (v8::Handle<v8::Value> val) {
+ if (val->IsObject()) {
+ v8::Handle<v8::Object> obj = val->ToObject();
+
+ if (constructor_template->HasInstance(obj)) {
+ return true;
+ }
+ }
+
+ return false;
+}
+
// Add()
// For adding a container to the window.
Handle<Value> Window::Add (const Arguments &args) {
HandleScope scope;
- GtkWidget *window = Widget::Gtk(args.This());
+ GtkWidget *window = Window::Data(args.This());
// TODO: Check arg is an instance of Widget
- GtkWidget *other = Widget::Gtk(args[0]->ToObject());
+ GtkWidget *other = Window::Data(args[0]->ToObject());
gtk_container_add(GTK_CONTAINER(window), other);
gtk_widget_show(other);
@@ -58,7 +71,7 @@ Handle<Value> Window::SetTitle (const Arguments &args) {
HandleScope scope;
if (args[0]->IsString()) {
- GtkWidget *window = Widget::Gtk(args.This());
+ GtkWidget *window = Window::Data(args.This());
gtk_window_set_title(GTK_WINDOW(window), *String::Utf8Value(args[0]->ToString()));
}
@@ -71,7 +84,7 @@ Handle<Value> Window::SetTitle (const Arguments &args) {
Handle<Value> Window::GetTitle (const Arguments &args) {
HandleScope scope;
- GtkWidget *window = Widget::Gtk(args.This());
+ GtkWidget *window = Window::Data(args.This());
return scope.Close(String::New(gtk_window_get_title(GTK_WINDOW(window))));
}
@@ -82,7 +95,7 @@ Handle<Value> Window::SetResizable (const Arguments &args) {
HandleScope scope;
if (args[0]->IsBoolean()) {
- GtkWidget *window = Widget::Gtk(args.This());
+ GtkWidget *window = Window::Data(args.This());
gtk_window_set_resizable(GTK_WINDOW(window), args[0]->ToBoolean()->Value());
}
@@ -95,7 +108,7 @@ Handle<Value> Window::SetResizable (const Arguments &args) {
Handle<Value> Window::GetResizable (const Arguments &args) {
HandleScope scope;
- GtkWidget *window = Widget::Gtk(args.This());
+ GtkWidget *window = Window::Data(args.This());
return scope.Close(Boolean::New(gtk_window_get_resizable(GTK_WINDOW(window))));
}
@@ -105,7 +118,7 @@ Handle<Value> Window::GetResizable (const Arguments &args) {
Handle<Value> Window::SetDefaultSize (const Arguments &args) {
HandleScope scope;
- GtkWidget *window = Widget::Gtk(args.This());
+ GtkWidget *window = Window::Data(args.This());
gint width, height;
@@ -131,7 +144,7 @@ Handle<Value> Window::SetDefaultSize (const Arguments &args) {
Handle<Value> Window::GetSize (const Arguments &args) {
HandleScope scope;
- GtkWidget *window = Widget::Gtk(args.This());
+ GtkWidget *window = Window::Data(args.This());
gint width, height;
Local<Array> ret = Array::New(2);
@@ -158,7 +171,7 @@ Handle<Value> Window::SetPosition (const Arguments &args) {
case GTK_WIN_POS_MOUSE:
case GTK_WIN_POS_CENTER_ALWAYS:
case GTK_WIN_POS_CENTER_ON_PARENT:
- GtkWidget *window = Widget::Gtk(args.This());
+ GtkWidget *window = Window::Data(args.This());
gtk_window_set_position(GTK_WINDOW(window), position);
break;
}
@@ -172,7 +185,7 @@ Handle<Value> Window::SetPosition (const Arguments &args) {
Handle<Value> Window::GetPosition (const Arguments &args) {
HandleScope scope;
- GtkWidget *window = Widget::Gtk(args.This());
+ GtkWidget *window = Window::Data(args.This());
gint x, y;
Local<Array> ret = Array::New(2);
@@ -191,7 +204,7 @@ Handle<Value> Window::SetOpacity (const Arguments &args) {
HandleScope scope;
if (args[0]->IsNumber()) {
- GtkWidget *window = Widget::Gtk(args.This());
+ GtkWidget *window = Window::Data(args.This());
gdouble opacity = args[0]->NumberValue();
@@ -206,7 +219,7 @@ Handle<Value> Window::SetOpacity (const Arguments &args) {
Handle<Value> Window::GetOpacity (const Arguments &args) {
HandleScope scope;
- GtkWidget *window = Widget::Gtk(args.This());
+ GtkWidget *window = Window::Data(args.This());
return scope.Close(Number::New(gtk_window_get_opacity(GTK_WINDOW(window))));
}
View
5 src/ngtk_window.h
@@ -11,8 +11,9 @@ namespace ngtk {
class Window : public Widget {
public:
- static void Initialize (v8::Handle<v8::Object> target);
- static Window* New (void); // public constructor
+ static void Initialize (v8::Handle<v8::Object> target);
+ static Window* New (void); // public constructor
+ static bool HasInstance (v8::Handle<v8::Value> val);
private:
static v8::Persistent<v8::FunctionTemplate> constructor_template;
View
23 test.js
@@ -3,18 +3,23 @@ var gtk = require('./build/default/gtk');
var loop = new gtk.Loop();
var window = new gtk.Window();
+var entry = new gtk.Entry();
-var button = new gtk.Button();
-button.setLabel('Test Button');
+entry.setText('A teeheehee!');
+entry.setVisibility(false);
-button.on('clicked', function () {
- console.log('clicked');
- var dialog = new gtk.MessageDialog(window, gtk.DIALOG_DESTROY_WITH_PARENT,
- gtk.MESSAGE_INFO, gtk.BUTTONS_YES_NO, "Testing");
- dialog.show();
-});
+//var button = new gtk.Button();
+//button.setLabel('Test Button');
+
+//button.on('clicked', function () {
+ //console.log('clicked');
+ //var dialog = new gtk.MessageDialog(window, gtk.DIALOG_DESTROY_WITH_PARENT,
+ //gtk.MESSAGE_INFO);
+ //dialog.show();
+//});
-window.add(button);
+//window.add(button);
+window.add(entry);
window.setTitle('Node');
window.setResizable(true);
View
1 wscript
@@ -18,6 +18,7 @@ def build(bld):
obj.cxxflags = ["-Wall", "-ansi", "-pedantic"]
obj.target = "gtk"
obj.source = """
+ src/ngtk_entry.cc
src/ngtk_button.cc
src/ngtk_message_dialog.cc
src/ngtk_window.cc

0 comments on commit cab0178

Please sign in to comment.
Something went wrong with that request. Please try again.