Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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...
commit cffdd7faac204183da65e90c74213717f5193a3d 1 parent 4697a3c
Nick Campbell authored October 18, 2010
BIN  data/ngtk.png
8  src/ngtk.cc
@@ -8,6 +8,7 @@
8 8
 #include "ngtk_vbox.h"
9 9
 #include "ngtk_progressbar.h"
10 10
 #include "ngtk_message_dialog.h"
  11
+#include "ngtk_image.h"
11 12
 #include "ngtk_button.h"
12 13
 #include "ngtk_entry.h"
13 14
 
@@ -164,6 +165,12 @@ extern "C" void init(Handle<Object> target) {
164 165
   NGTK_DEFINE_CONSTANT(target, "BUTTONS_YES_NO",    GTK_BUTTONS_YES_NO);
165 166
   NGTK_DEFINE_CONSTANT(target, "BUTTONS_OK_CANCEL", GTK_BUTTONS_OK_CANCEL);
166 167
 
  168
+  // Position
  169
+  NGTK_DEFINE_CONSTANT(target, "POS_TOP", GTK_POS_TOP);
  170
+  NGTK_DEFINE_CONSTANT(target, "POS_RIGHT", GTK_POS_RIGHT);
  171
+  NGTK_DEFINE_CONSTANT(target, "POS_BOTTOM", GTK_POS_BOTTOM);
  172
+  NGTK_DEFINE_CONSTANT(target, "POS_LEFT", GTK_POS_LEFT);
  173
+
167 174
   // Response constants
168 175
   NGTK_DEFINE_CONSTANT(target, "RESPONSE_NONE",         GTK_RESPONSE_NONE);
169 176
   NGTK_DEFINE_CONSTANT(target, "RESPONSE_REJECT",       GTK_RESPONSE_REJECT);
@@ -183,6 +190,7 @@ extern "C" void init(Handle<Object> target) {
183 190
   MessageDialog::Initialize(target);
184 191
   Button::Initialize(target);
185 192
   ProgressBar::Initialize(target);
  193
+  Image::Initialize(target);
186 194
   Entry::Initialize(target);
187 195
 
188 196
   GMainContext *gc     = g_main_context_default();
15  src/ngtk_button.cc
... ...
@@ -1,4 +1,5 @@
1 1
 #include "ngtk_button.h"
  2
+#include "ngtk_image.h"
2 3
 #include "ngtk.h"
3 4
 
4 5
 namespace ngtk {
@@ -69,6 +70,19 @@ Handle<Value> Button::GetLabel (const Arguments &args) {
69 70
   return scope.Close(String::New(gtk_button_get_label(GTK_BUTTON(button))));
70 71
 }
71 72
 
  73
+// SetImage()
  74
+// For setting the button image.
  75
+Handle<Value> Button::SetImage (const Arguments &args) {
  76
+  HandleScope scope;
  77
+
  78
+  GtkWidget *button = Button::Data(args.This());
  79
+  GtkWidget *other = Image::Data(args[0]->ToObject());
  80
+
  81
+  gtk_button_set_image(GTK_BUTTON(button), other);
  82
+  
  83
+  return args.This();
  84
+}
  85
+
72 86
 // Export.
73 87
 void Button::SetPrototypeMethods (Handle<FunctionTemplate> constructor_template) {
74 88
   HandleScope scope;
@@ -77,6 +91,7 @@ void Button::SetPrototypeMethods (Handle<FunctionTemplate> constructor_template)
77 91
 
78 92
   NGTK_SET_PROTOTYPE_METHOD(constructor_template, "setLabel", Button::SetLabel);
79 93
   NGTK_SET_PROTOTYPE_METHOD(constructor_template, "getLabel", Button::GetLabel);
  94
+  NGTK_SET_PROTOTYPE_METHOD(constructor_template, "setImage", Button::SetImage);
80 95
 }
81 96
 
82 97
 void Button::Initialize (Handle<Object> target) {
2  src/ngtk_button.h
@@ -19,6 +19,8 @@ class Button : public Widget {
19 19
   // gtk_button_*_label()
20 20
   static v8::Handle<v8::Value> SetLabel (const v8::Arguments &args);
21 21
   static v8::Handle<v8::Value> GetLabel (const v8::Arguments &args);
  22
+  static v8::Handle<v8::Value> SetImage (const v8::Arguments &args);
  23
+  static v8::Handle<v8::Value> SetImagePosition (const v8::Arguments &args);
22 24
 
23 25
   Button(void);
24 26
 };
84  src/ngtk_image.cc
... ...
@@ -0,0 +1,84 @@
  1
+#include "ngtk_image.h"
  2
+
  3
+namespace ngtk {
  4
+
  5
+using namespace v8;
  6
+
  7
+Persistent<FunctionTemplate> Image::constructor_template;
  8
+
  9
+// Check whether is an instance.
  10
+bool Image::HasInstance (Handle<Value> val) {
  11
+  HandleScope scope;
  12
+
  13
+  if (val->IsObject()) {
  14
+    Local<Object> obj = val->ToObject();
  15
+
  16
+    if (constructor_template->HasInstance(obj)) {
  17
+      return true;
  18
+    }
  19
+  }
  20
+
  21
+  return false;
  22
+}
  23
+
  24
+// Public constructor
  25
+Image* Image::New (void) {
  26
+  HandleScope scope;
  27
+
  28
+  Local<Object> image = constructor_template->GetFunction()->NewInstance();
  29
+
  30
+  return ObjectWrap::Unwrap<Image>(image);
  31
+}
  32
+
  33
+// ECMAScript constructor.
  34
+Handle<Value> Image::New (const Arguments &args) {
  35
+  HandleScope scope;
  36
+
  37
+  Image *image = new Image();
  38
+  image->Wrap(args.This());
  39
+
  40
+  return args.This();
  41
+}
  42
+
  43
+Image::Image (void) {
  44
+  widget_ = gtk_image_new();
  45
+}
  46
+
  47
+// SetFromFile()
  48
+// For setting the image from a file.
  49
+Handle<Value> Image::SetFromFile (const Arguments &args) {
  50
+  HandleScope scope;
  51
+
  52
+  if (args[0]->IsString()) {
  53
+    GtkWidget *image = Image::Data(args.This());
  54
+
  55
+    gtk_image_set_from_file(GTK_IMAGE(image), *String::Utf8Value(args[0]->ToString()));
  56
+  }
  57
+
  58
+  return args.This();
  59
+}
  60
+
  61
+// Export.
  62
+void Image::SetPrototypeMethods (Handle<FunctionTemplate> constructor_template) {
  63
+  HandleScope scope;
  64
+
  65
+  Widget::SetPrototypeMethods(constructor_template);
  66
+
  67
+  NGTK_SET_PROTOTYPE_METHOD(constructor_template, "setFromFile", Image::SetFromFile);
  68
+}
  69
+
  70
+void Image::Initialize (Handle<Object> target) {
  71
+  HandleScope scope;
  72
+
  73
+  Local<FunctionTemplate> t = FunctionTemplate::New(Image::New);
  74
+  constructor_template = Persistent<FunctionTemplate>::New(t);
  75
+  constructor_template->InstanceTemplate()->SetInternalFieldCount(1);
  76
+  constructor_template->SetClassName(String::NewSymbol("Image"));
  77
+
  78
+  Image::SetPrototypeMethods(constructor_template);
  79
+
  80
+  target->Set(String::NewSymbol("Image"), constructor_template->GetFunction());
  81
+}
  82
+
  83
+} // namespace ngtk
  84
+
26  src/ngtk_image.h
... ...
@@ -0,0 +1,26 @@
  1
+#ifndef NGTK_IMAGE_H_
  2
+#define NGTK_IMAGE_H_
  3
+
  4
+#include "ngtk_widget.h"
  5
+
  6
+namespace ngtk {
  7
+
  8
+class Image : public Widget {
  9
+public:
  10
+  static void       SetPrototypeMethods (v8::Handle<v8::FunctionTemplate> constructor_template);
  11
+  static void       Initialize          (v8::Handle<v8::Object> target);
  12
+  static Image*    New                 (void); // public constructor
  13
+  static bool       HasInstance         (v8::Handle<v8::Value> val);
  14
+
  15
+private:
  16
+  static v8::Persistent<v8::FunctionTemplate> constructor_template;
  17
+
  18
+  static v8::Handle<v8::Value> New      (const v8::Arguments &args);
  19
+  static v8::Handle<v8::Value> SetFromFile      (const v8::Arguments &args);
  20
+
  21
+  Image(void);
  22
+};
  23
+
  24
+} // namespace ngtk
  25
+
  26
+#endif
6  test.js
@@ -29,7 +29,11 @@ process.nextTick(function () {
29 29
   });
30 30
 
31 31
   var button = new gtk.Button();
32  
-  button.setLabel('Test Button');
  32
+  //button.setLabel('Test Button');
  33
+
  34
+  var img = new gtk.Image();
  35
+  img.setFromFile('./data/ngtk.png');
  36
+  button.setImage(img);
33 37
 
34 38
   button.on('clicked', function () {
35 39
     console.log('clicked');
1  wscript
@@ -18,6 +18,7 @@ def build(bld):
18 18
   obj.source = """
19 19
                src/ngtk_entry.cc
20 20
                src/ngtk_button.cc
  21
+               src/ngtk_image.cc
21 22
                src/ngtk_message_dialog.cc
22 23
                src/ngtk_progressbar.cc
23 24
                src/ngtk_vbox.cc

0 notes on commit cffdd7f

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