Browse Source

Added Image#onload accessors

v1.x
Tj Holowaychuk 14 years ago
parent
commit
dc949d0fe9
  1. 35
      src/Image.cc
  2. 2
      src/Image.h

35
src/Image.cc

@ -22,6 +22,7 @@ Image::Initialize(Handle<Object> target) {
Local<ObjectTemplate> proto = t->PrototypeTemplate();
NODE_SET_PROTOTYPE_METHOD(t, "inspect", Inspect);
proto->SetAccessor(String::NewSymbol("src"), GetSrc, SetSrc);
proto->SetAccessor(String::NewSymbol("onload"), GetOnload, SetOnload);
target->Set(String::NewSymbol("Image"), t->GetFunction());
}
@ -74,14 +75,32 @@ Image::SetSrc(Local<String>, Local<Value> val, const AccessorInfo &info) {
String::AsciiValue src(val);
Image *img = ObjectWrap::Unwrap<Image>(info.This());
img->filename = *src;
Handle<Value> onload = info.This()->Get(String::New("onload"));
if (onload->IsFunction()) {
img->onload = Persistent<Function>::New(Handle<Function>::Cast(onload));
}
img->load();
}
}
/*
* Get onload callback.
*/
Handle<Value>
Image::GetOnload(Local<String>, const AccessorInfo &info) {
Image *img = ObjectWrap::Unwrap<Image>(info.This());
return img->onload;
}
/*
* Set onload callback.
*/
void
Image::SetOnload(Local<String>, Local<Value> val, const AccessorInfo &info) {
if (val->IsFunction()) {
Image *img = ObjectWrap::Unwrap<Image>(info.This());
img->onload = Persistent<Function>::New(Handle<Function>::Cast(val));
}
}
/*
* Initialize a new Image.
*/
@ -124,9 +143,11 @@ Image::load() {
void
Image::loaded() {
// TODO: TryCatch
onload->Call(Context::GetCurrent()->Global(), 0, NULL);
onload.Dispose();
if (!onload.IsEmpty()) {
// TODO: TryCatch
onload->Call(Context::GetCurrent()->Global(), 0, NULL);
onload.Dispose();
}
Unref();
}

2
src/Image.h

@ -20,7 +20,9 @@ class Image: public node::ObjectWrap {
static Handle<Value> New(const Arguments &args);
static Handle<Value> Inspect(const Arguments &args);
static Handle<Value> GetSrc(Local<String> prop, const AccessorInfo &info);
static Handle<Value> GetOnload(Local<String> prop, const AccessorInfo &info);
static void SetSrc(Local<String> prop, Local<Value> val, const AccessorInfo &info);
static void SetOnload(Local<String> prop, Local<Value> val, const AccessorInfo &info);
inline cairo_surface_t *surface(){ return _surface; }
cairo_status_t loadSurface();
void loaded();

Loading…
Cancel
Save