Permalink
Browse files

Added images to the mix. You can now use an image from a file (only) …

…and set the image on a button. The button currently cannot have text or the text will override the image.

Signed-off-by: Nick Campbell <nicholas.j.campbell@gmail.com>
  • Loading branch information...
1 parent 4697a3c commit cffdd7faac204183da65e90c74213717f5193a3d @ncb000gt ncb000gt committed Oct 19, 2010
Showing with 141 additions and 1 deletion.
  1. BIN data/ngtk.png
  2. +8 −0 src/ngtk.cc
  3. +15 −0 src/ngtk_button.cc
  4. +2 −0 src/ngtk_button.h
  5. +84 −0 src/ngtk_image.cc
  6. +26 −0 src/ngtk_image.h
  7. +5 −1 test.js
  8. +1 −0 wscript
View
BIN data/ngtk.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
8 src/ngtk.cc
@@ -8,6 +8,7 @@
#include "ngtk_vbox.h"
#include "ngtk_progressbar.h"
#include "ngtk_message_dialog.h"
+#include "ngtk_image.h"
#include "ngtk_button.h"
#include "ngtk_entry.h"
@@ -164,6 +165,12 @@ extern "C" void init(Handle<Object> target) {
NGTK_DEFINE_CONSTANT(target, "BUTTONS_YES_NO", GTK_BUTTONS_YES_NO);
NGTK_DEFINE_CONSTANT(target, "BUTTONS_OK_CANCEL", GTK_BUTTONS_OK_CANCEL);
+ // Position
+ NGTK_DEFINE_CONSTANT(target, "POS_TOP", GTK_POS_TOP);
+ NGTK_DEFINE_CONSTANT(target, "POS_RIGHT", GTK_POS_RIGHT);
+ NGTK_DEFINE_CONSTANT(target, "POS_BOTTOM", GTK_POS_BOTTOM);
+ NGTK_DEFINE_CONSTANT(target, "POS_LEFT", GTK_POS_LEFT);
+
// Response constants
NGTK_DEFINE_CONSTANT(target, "RESPONSE_NONE", GTK_RESPONSE_NONE);
NGTK_DEFINE_CONSTANT(target, "RESPONSE_REJECT", GTK_RESPONSE_REJECT);
@@ -183,6 +190,7 @@ extern "C" void init(Handle<Object> target) {
MessageDialog::Initialize(target);
Button::Initialize(target);
ProgressBar::Initialize(target);
+ Image::Initialize(target);
Entry::Initialize(target);
GMainContext *gc = g_main_context_default();
View
15 src/ngtk_button.cc
@@ -1,4 +1,5 @@
#include "ngtk_button.h"
+#include "ngtk_image.h"
#include "ngtk.h"
namespace ngtk {
@@ -69,6 +70,19 @@ Handle<Value> Button::GetLabel (const Arguments &args) {
return scope.Close(String::New(gtk_button_get_label(GTK_BUTTON(button))));
}
+// SetImage()
+// For setting the button image.
+Handle<Value> Button::SetImage (const Arguments &args) {
+ HandleScope scope;
+
+ GtkWidget *button = Button::Data(args.This());
+ GtkWidget *other = Image::Data(args[0]->ToObject());
+
+ gtk_button_set_image(GTK_BUTTON(button), other);
+
+ return args.This();
+}
+
// Export.
void Button::SetPrototypeMethods (Handle<FunctionTemplate> constructor_template) {
HandleScope scope;
@@ -77,6 +91,7 @@ void Button::SetPrototypeMethods (Handle<FunctionTemplate> constructor_template)
NGTK_SET_PROTOTYPE_METHOD(constructor_template, "setLabel", Button::SetLabel);
NGTK_SET_PROTOTYPE_METHOD(constructor_template, "getLabel", Button::GetLabel);
+ NGTK_SET_PROTOTYPE_METHOD(constructor_template, "setImage", Button::SetImage);
}
void Button::Initialize (Handle<Object> target) {
View
2 src/ngtk_button.h
@@ -19,6 +19,8 @@ class Button : public Widget {
// gtk_button_*_label()
static v8::Handle<v8::Value> SetLabel (const v8::Arguments &args);
static v8::Handle<v8::Value> GetLabel (const v8::Arguments &args);
+ static v8::Handle<v8::Value> SetImage (const v8::Arguments &args);
+ static v8::Handle<v8::Value> SetImagePosition (const v8::Arguments &args);
Button(void);
};
View
84 src/ngtk_image.cc
@@ -0,0 +1,84 @@
+#include "ngtk_image.h"
+
+namespace ngtk {
+
+using namespace v8;
+
+Persistent<FunctionTemplate> Image::constructor_template;
+
+// Check whether is an instance.
+bool Image::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
+Image* Image::New (void) {
+ HandleScope scope;
+
+ Local<Object> image = constructor_template->GetFunction()->NewInstance();
+
+ return ObjectWrap::Unwrap<Image>(image);
+}
+
+// ECMAScript constructor.
+Handle<Value> Image::New (const Arguments &args) {
+ HandleScope scope;
+
+ Image *image = new Image();
+ image->Wrap(args.This());
+
+ return args.This();
+}
+
+Image::Image (void) {
+ widget_ = gtk_image_new();
+}
+
+// SetFromFile()
+// For setting the image from a file.
+Handle<Value> Image::SetFromFile (const Arguments &args) {
+ HandleScope scope;
+
+ if (args[0]->IsString()) {
+ GtkWidget *image = Image::Data(args.This());
+
+ gtk_image_set_from_file(GTK_IMAGE(image), *String::Utf8Value(args[0]->ToString()));
+ }
+
+ return args.This();
+}
+
+// Export.
+void Image::SetPrototypeMethods (Handle<FunctionTemplate> constructor_template) {
+ HandleScope scope;
+
+ Widget::SetPrototypeMethods(constructor_template);
+
+ NGTK_SET_PROTOTYPE_METHOD(constructor_template, "setFromFile", Image::SetFromFile);
+}
+
+void Image::Initialize (Handle<Object> target) {
+ HandleScope scope;
+
+ Local<FunctionTemplate> t = FunctionTemplate::New(Image::New);
+ constructor_template = Persistent<FunctionTemplate>::New(t);
+ constructor_template->InstanceTemplate()->SetInternalFieldCount(1);
+ constructor_template->SetClassName(String::NewSymbol("Image"));
+
+ Image::SetPrototypeMethods(constructor_template);
+
+ target->Set(String::NewSymbol("Image"), constructor_template->GetFunction());
+}
+
+} // namespace ngtk
+
View
26 src/ngtk_image.h
@@ -0,0 +1,26 @@
+#ifndef NGTK_IMAGE_H_
+#define NGTK_IMAGE_H_
+
+#include "ngtk_widget.h"
+
+namespace ngtk {
+
+class Image : public Widget {
+public:
+ static void SetPrototypeMethods (v8::Handle<v8::FunctionTemplate> constructor_template);
+ static void Initialize (v8::Handle<v8::Object> target);
+ static Image* New (void); // public constructor
+ static bool HasInstance (v8::Handle<v8::Value> val);
+
+private:
+ static v8::Persistent<v8::FunctionTemplate> constructor_template;
+
+ static v8::Handle<v8::Value> New (const v8::Arguments &args);
+ static v8::Handle<v8::Value> SetFromFile (const v8::Arguments &args);
+
+ Image(void);
+};
+
+} // namespace ngtk
+
+#endif
View
6 test.js
@@ -29,7 +29,11 @@ process.nextTick(function () {
});
var button = new gtk.Button();
- button.setLabel('Test Button');
+ //button.setLabel('Test Button');
+
+ var img = new gtk.Image();
+ img.setFromFile('./data/ngtk.png');
+ button.setImage(img);
button.on('clicked', function () {
console.log('clicked');
View
1 wscript
@@ -18,6 +18,7 @@ def build(bld):
obj.source = """
src/ngtk_entry.cc
src/ngtk_button.cc
+ src/ngtk_image.cc
src/ngtk_message_dialog.cc
src/ngtk_progressbar.cc
src/ngtk_vbox.cc

0 comments on commit cffdd7f

Please sign in to comment.