new bindings
This commit is contained in:
parent
9a3a1b084a
commit
c4a3c14caf
7 changed files with 480 additions and 105 deletions
|
|
@ -0,0 +1,32 @@
|
|||
#include "classes.hpp"
|
||||
#include <Mw/Milsko.h>
|
||||
|
||||
#define NAPI_MWCLASS(env, cls) \
|
||||
Napi::External<_MwClass>::New(env, cls)
|
||||
|
||||
Napi::Object ClassesInit(Napi::Env env, Napi::Object exports) {
|
||||
Napi::Object classes = Napi::Object::New(env);
|
||||
|
||||
classes.Set("Box", NAPI_MWCLASS(env, MwBoxClass));
|
||||
classes.Set("Button", NAPI_MWCLASS(env, MwButtonClass));
|
||||
classes.Set("CheckBox", NAPI_MWCLASS(env, MwCheckBoxClass));
|
||||
classes.Set("Entry", NAPI_MWCLASS(env, MwEntryClass));
|
||||
classes.Set("Frame", NAPI_MWCLASS(env, MwFrameClass));
|
||||
classes.Set("Image", NAPI_MWCLASS(env, MwImageClass));
|
||||
classes.Set("Label", NAPI_MWCLASS(env, MwLabelClass));
|
||||
classes.Set("ListBox", NAPI_MWCLASS(env, MwListBoxClass));
|
||||
classes.Set("Menu", NAPI_MWCLASS(env, MwMenuClass));
|
||||
classes.Set("NumberEntry", NAPI_MWCLASS(env, MwNumberEntryClass));
|
||||
classes.Set("ScrollBar", NAPI_MWCLASS(env, MwScrollBarClass));
|
||||
classes.Set("SubMenu", NAPI_MWCLASS(env, MwSubMenuClass));
|
||||
classes.Set("SubWindow", NAPI_MWCLASS(env, MwSubWindowClass));
|
||||
classes.Set("Viewport", NAPI_MWCLASS(env, MwViewportClass));
|
||||
classes.Set("Window", NAPI_MWCLASS(env, MwWindowClass));
|
||||
classes.Set("ProgressBar", NAPI_MWCLASS(env, MwProgressBarClass));
|
||||
classes.Set("RadioBox", NAPI_MWCLASS(env, MwRadioBoxClass));
|
||||
classes.Set("ComboBox", NAPI_MWCLASS(env, MwComboBoxClass));
|
||||
classes.Set("TreeView", NAPI_MWCLASS(env, MwTreeViewClass));
|
||||
|
||||
exports.Set("NativeClasses", classes);
|
||||
return exports;
|
||||
};
|
||||
|
|
@ -1,13 +1,17 @@
|
|||
#include "classes.hpp"
|
||||
#include "pixmap.hpp"
|
||||
#include "widget.hpp"
|
||||
#include <Mw/Milsko.h>
|
||||
#include <napi.h>
|
||||
|
||||
Napi::Object Init(Napi::Env env, Napi::Object exports) {
|
||||
MwLibraryInit();
|
||||
MwLibraryInit();
|
||||
|
||||
MwBaseWidget::Init(env, exports);
|
||||
MwBaseWidget::Init(env, exports);
|
||||
MwPixmap::Init(env, exports);
|
||||
ClassesInit(env, exports);
|
||||
|
||||
return exports;
|
||||
return exports;
|
||||
}
|
||||
|
||||
NODE_API_MODULE(milsko, Init);
|
||||
153
src/pixmap.cpp
Normal file
153
src/pixmap.cpp
Normal file
|
|
@ -0,0 +1,153 @@
|
|||
#include "classes.hpp"
|
||||
#include "widget.hpp"
|
||||
#include <stdlib.h>
|
||||
|
||||
Napi::FunctionReference MwPixmap::constructor;
|
||||
|
||||
Napi::Object MwPixmap::Init(Napi::Env env, Napi::Object exports) {
|
||||
Napi::Function func = DefineClass(env, "Pixmap",
|
||||
{
|
||||
InstanceMethod("getRaw", &MwPixmap::GetRaw),
|
||||
InstanceMethod("reloadRaw", &MwPixmap::ReloadRaw),
|
||||
InstanceAccessor("size", &MwPixmap::GetSize, nullptr),
|
||||
});
|
||||
|
||||
constructor = Napi::Persistent(func);
|
||||
constructor.SuppressDestruct();
|
||||
|
||||
exports.Set("Pixmap", func);
|
||||
return exports;
|
||||
}
|
||||
|
||||
MwPixmap::MwPixmap(const Napi::CallbackInfo &info): Napi::ObjectWrap<MwPixmap>(info) {
|
||||
Napi::Env env = info.Env();
|
||||
Napi::HandleScope scope(env);
|
||||
|
||||
if (info.Length() < 1 || !info[0].IsObject()) {
|
||||
Napi::TypeError::New(env, "Expected an instance of widget as first argument")
|
||||
.ThrowAsJavaScriptException();
|
||||
return;
|
||||
}
|
||||
|
||||
Napi::Object obj = info[0].As<Napi::Object>();
|
||||
MwBaseWidget *widget = MwBaseWidget::Unwrap(obj);
|
||||
unsigned char *data = 0;
|
||||
int width = 0;
|
||||
int height = 0;
|
||||
|
||||
if (!widget) {
|
||||
Napi::TypeError::New(env, "First argument is not an instance of a widget")
|
||||
.ThrowAsJavaScriptException();
|
||||
return;
|
||||
}
|
||||
|
||||
if (info.Length() > 1 && info[1].IsObject()) {
|
||||
Napi::Object params = info[1].As<Napi::Object>();
|
||||
|
||||
if (params.Has("data")) {
|
||||
Napi::Value dataParam = params.Get("data");
|
||||
|
||||
if (dataParam.IsBuffer()) {
|
||||
data = dataParam.As<Napi::Buffer<unsigned char>>().Data();
|
||||
} else if (dataParam.IsArrayBuffer()) {
|
||||
data = reinterpret_cast<unsigned char*>(
|
||||
dataParam.As<Napi::ArrayBuffer>().Data()
|
||||
);
|
||||
} else {
|
||||
Napi::TypeError::New(env, "The data parameter must be of type Buffer or ArrayBuffer")
|
||||
.ThrowAsJavaScriptException();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (params.Has("width")) {
|
||||
Napi::Value widthParam = params.Get("width");
|
||||
|
||||
if (widthParam.IsNumber()) {
|
||||
width = widthParam.As<Napi::Number>().Int32Value();
|
||||
} else {
|
||||
Napi::TypeError::New(env, "The width parameter must be a number")
|
||||
.ThrowAsJavaScriptException();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (params.Has("height")) {
|
||||
Napi::Value heightParam = params.Get("height");
|
||||
|
||||
if (heightParam.IsNumber()) {
|
||||
height = heightParam.As<Napi::Number>().Int32Value();
|
||||
} else {
|
||||
Napi::TypeError::New(env, "The height parameter must be a number")
|
||||
.ThrowAsJavaScriptException();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
handle = MwLoadRaw(
|
||||
widget->handle,
|
||||
data,
|
||||
width,
|
||||
height
|
||||
);
|
||||
};
|
||||
|
||||
MwPixmap::~MwPixmap() {
|
||||
if (handle)
|
||||
MwLLDestroyPixmap(handle);
|
||||
}
|
||||
|
||||
Napi::Value MwPixmap::GetRaw(const Napi::CallbackInfo &info) {
|
||||
Napi::Env env = info.Env();
|
||||
|
||||
unsigned char *data = MwPixmapGetRaw(handle);
|
||||
Napi::Buffer<unsigned char> buffer = Napi::Buffer<unsigned char>::New(
|
||||
env,
|
||||
data,
|
||||
sizeof(data),
|
||||
[](Napi::Env env, unsigned char* finalized) {
|
||||
free(finalized);
|
||||
}
|
||||
);
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
Napi::Value MwPixmap::GetSize(const Napi::CallbackInfo &info) {
|
||||
Napi::Env env = info.Env();
|
||||
Napi::Object size = Napi::Object::New(env);
|
||||
MwRect rect;
|
||||
|
||||
MwPixmapGetSize(handle, &rect);
|
||||
|
||||
size.Set("x", Napi::Number::New(env, rect.x));
|
||||
size.Set("y", Napi::Number::New(env, rect.y));
|
||||
size.Set("width", Napi::Number::New(env, rect.width));
|
||||
size.Set("height", Napi::Number::New(env, rect.height));
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
Napi::Value MwPixmap::ReloadRaw(const Napi::CallbackInfo &info) {
|
||||
Napi::Env env = info.Env();
|
||||
|
||||
if (info.Length() < 1 || !info[0].IsBuffer() || !info[0].IsArrayBuffer()) {
|
||||
Napi::TypeError::New(env, "Expected a buffer or array buffer as argument")
|
||||
.ThrowAsJavaScriptException();
|
||||
return env.Null();
|
||||
}
|
||||
|
||||
unsigned char *data = 0;
|
||||
|
||||
if (info[0].IsBuffer()) {
|
||||
data = info[0].As<Napi::Buffer<unsigned char>>().Data();
|
||||
} else if (info[0].IsArrayBuffer()) {
|
||||
data = reinterpret_cast<unsigned char*>(
|
||||
info[0].As<Napi::ArrayBuffer>().Data()
|
||||
);
|
||||
}
|
||||
|
||||
MwPixmapReloadRaw(handle, data);
|
||||
return env.Undefined();
|
||||
}
|
||||
19
src/pixmap.hpp
Normal file
19
src/pixmap.hpp
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
#pragma once
|
||||
#include <napi.h>
|
||||
#include <Mw/Milsko.h>
|
||||
|
||||
class MwPixmap : public Napi::ObjectWrap<MwPixmap> {
|
||||
public:
|
||||
static Napi::FunctionReference constructor;
|
||||
static Napi::Object Init(Napi::Env env, Napi::Object exports);
|
||||
static Napi::Object From(Napi::Env env, MwLLPixmap &pixmap);
|
||||
MwPixmap(const Napi::CallbackInfo &info);
|
||||
~MwPixmap();
|
||||
|
||||
MwLLPixmap handle;
|
||||
|
||||
protected:
|
||||
Napi::Value GetRaw(const Napi::CallbackInfo &info);
|
||||
Napi::Value GetSize(const Napi::CallbackInfo &info);
|
||||
Napi::Value ReloadRaw(const Napi::CallbackInfo &info);
|
||||
};
|
||||
2
src/utils.hpp
Normal file
2
src/utils.hpp
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
#include <napi.h>
|
||||
|
||||
300
src/widget.cpp
300
src/widget.cpp
|
|
@ -4,36 +4,46 @@ Napi::FunctionReference MwBaseWidget::constructor;
|
|||
|
||||
Napi::Object MwBaseWidget::Init(Napi::Env env, Napi::Object exports) {
|
||||
Napi::HandleScope scope(env);
|
||||
Napi::Function func = DefineClass(env, "MwBaseWidget",
|
||||
Napi::Function func = DefineClass(env, "BaseWidget",
|
||||
{
|
||||
InstanceMethod("setArea", &SetArea),
|
||||
InstanceMethod("move", &Move),
|
||||
InstanceMethod("resize", &Resize),
|
||||
InstanceMethod("step", &Step),
|
||||
InstanceMethod("loop", &Loop),
|
||||
InstanceMethod("show", &Show),
|
||||
InstanceMethod("hideCursor", &HideCursor),
|
||||
InstanceMethod("grabPointer", &GrabPointer),
|
||||
InstanceMethod("getClipboard", &GetClipboard),
|
||||
InstanceMethod("addTickList", &AddTickList),
|
||||
InstanceMethod("forceRender", &ForceRender),
|
||||
InstanceMethod("reparent", &Reparent),
|
||||
InstanceMethod("add", &Add),
|
||||
InstanceMethod("remove", &Remove),
|
||||
InstanceAccessor("area", &GetArea, nullptr),
|
||||
InstanceAccessor("isPending", &IsPending, nullptr),
|
||||
InstanceAccessor("parent", &GetParent, nullptr),
|
||||
InstanceAccessor("children", &GetChildren, nullptr),
|
||||
InstanceAccessor("name", &GetName, nullptr),
|
||||
InstanceAccessor("cursorCoord", &GetCursorCoord, nullptr),
|
||||
InstanceAccessor("screenSize", &GetScreenSize, nullptr),
|
||||
InstanceAccessor("coordinateType", &GetCoordinateType, nullptr),
|
||||
InstanceMethod("setArea", &MwBaseWidget::GetArea),
|
||||
InstanceMethod("move", &MwBaseWidget::Move),
|
||||
InstanceMethod("resize", &MwBaseWidget::Resize),
|
||||
InstanceMethod("step", &MwBaseWidget::Step),
|
||||
InstanceMethod("loop", &MwBaseWidget::Loop),
|
||||
InstanceMethod("show", &MwBaseWidget::Show),
|
||||
InstanceMethod("hideCursor", &MwBaseWidget::HideCursor),
|
||||
InstanceMethod("grabPointer", &MwBaseWidget::GrabPointer),
|
||||
InstanceMethod("focus", &MwBaseWidget::Focus),
|
||||
InstanceMethod("addTickList", &MwBaseWidget::AddTickList),
|
||||
InstanceMethod("forceRender", &MwBaseWidget::ForceRender),
|
||||
InstanceMethod("reparent", &MwBaseWidget::Reparent),
|
||||
InstanceMethod("add", &MwBaseWidget::AddChild),
|
||||
InstanceMethod("destroy", &MwBaseWidget::Destroy),
|
||||
InstanceMethod("getClipboard", &MwBaseWidget::GetClipboard),
|
||||
|
||||
InstanceMethod("setInteger", &MwBaseWidget::SetInteger),
|
||||
InstanceMethod("setString", &MwBaseWidget::SetText),
|
||||
InstanceMethod("setVoid", &MwBaseWidget::SetVoid),
|
||||
InstanceMethod("getInteger", &MwBaseWidget::GetInteger),
|
||||
InstanceMethod("getText", &MwBaseWidget::GetText),
|
||||
InstanceMethod("getVoid", &MwBaseWidget::GetVoid),
|
||||
|
||||
InstanceAccessor("area", &MwBaseWidget::GetArea, nullptr),
|
||||
InstanceAccessor("isPending", &MwBaseWidget::IsPending, nullptr),
|
||||
InstanceAccessor("parent", &MwBaseWidget::GetParent, nullptr),
|
||||
InstanceAccessor("children", &MwBaseWidget::GetChildren, nullptr),
|
||||
InstanceAccessor("name", &MwBaseWidget::GetName, nullptr),
|
||||
InstanceAccessor("cursorCoord", &MwBaseWidget::GetCursorCoord, nullptr),
|
||||
InstanceAccessor("screenSize", &MwBaseWidget::GetScreenSize, nullptr),
|
||||
InstanceAccessor("coordinateType", &MwBaseWidget::GetCoordinateType, nullptr),
|
||||
InstanceAccessor("nativeClass", &MwBaseWidget::GetNativeClass, nullptr),
|
||||
});
|
||||
|
||||
constructor = Napi::Persistent(func);
|
||||
constructor.SuppressDestruct();
|
||||
|
||||
exports.Set("MwBaseWidget", func);
|
||||
exports.Set("BaseWidget", func);
|
||||
return exports;
|
||||
};
|
||||
|
||||
|
|
@ -107,7 +117,7 @@ Napi::Value MwBaseWidget::Move(const Napi::CallbackInfo &info) {
|
|||
.ThrowAsJavaScriptException();
|
||||
}
|
||||
|
||||
return Napi::Value();
|
||||
return info.This();
|
||||
}
|
||||
|
||||
Napi::Value MwBaseWidget::Resize(const Napi::CallbackInfo &info) {
|
||||
|
|
@ -127,7 +137,7 @@ Napi::Value MwBaseWidget::Resize(const Napi::CallbackInfo &info) {
|
|||
.ThrowAsJavaScriptException();
|
||||
}
|
||||
|
||||
return Napi::Value();
|
||||
return info.This();
|
||||
}
|
||||
|
||||
Napi::Value MwBaseWidget::Step(const Napi::CallbackInfo &info) {
|
||||
|
|
@ -146,11 +156,10 @@ Napi::Value MwBaseWidget::IsPending(const Napi::CallbackInfo &info) {
|
|||
|
||||
Napi::Value MwBaseWidget::Loop(const Napi::CallbackInfo &info) {
|
||||
MwLoop(handle);
|
||||
return Napi::Value();
|
||||
return info.This();
|
||||
}
|
||||
|
||||
Napi::Value MwBaseWidget::Show(const Napi::CallbackInfo &info) {
|
||||
Napi::Env env = info.Env();
|
||||
bool toggle = true;
|
||||
|
||||
if (info.Length() > 0 && info[0].IsBoolean()) {
|
||||
|
|
@ -158,16 +167,15 @@ Napi::Value MwBaseWidget::Show(const Napi::CallbackInfo &info) {
|
|||
}
|
||||
|
||||
MwShow(handle, static_cast<int>(toggle));
|
||||
return Napi::Value();
|
||||
return info.This();
|
||||
}
|
||||
|
||||
Napi::Value MwBaseWidget::HideCursor(const Napi::CallbackInfo &info) {
|
||||
MwHideCursor(handle);
|
||||
return Napi::Value();
|
||||
return info.This();
|
||||
}
|
||||
|
||||
Napi::Value MwBaseWidget::GrabPointer(const Napi::CallbackInfo &info) {
|
||||
Napi::Env env = info.Env();
|
||||
bool toggle = true;
|
||||
|
||||
if (info.Length() > 0 && info[0].IsBoolean()) {
|
||||
|
|
@ -175,17 +183,22 @@ Napi::Value MwBaseWidget::GrabPointer(const Napi::CallbackInfo &info) {
|
|||
}
|
||||
|
||||
MwGrabPointer(handle, static_cast<int>(toggle));
|
||||
return Napi::Value();
|
||||
return info.This();
|
||||
}
|
||||
|
||||
Napi::Value MwBaseWidget::Focus(const Napi::CallbackInfo &info) {
|
||||
MwFocus(handle);
|
||||
return info.This();
|
||||
}
|
||||
|
||||
Napi::Value MwBaseWidget::AddTickList(const Napi::CallbackInfo &info) {
|
||||
MwAddTickList(handle);
|
||||
return Napi::Value();
|
||||
return info.This();
|
||||
}
|
||||
|
||||
Napi::Value MwBaseWidget::ForceRender(const Napi::CallbackInfo &info) {
|
||||
MwForceRender(handle);
|
||||
return Napi::Value();
|
||||
return info.This();
|
||||
}
|
||||
|
||||
Napi::Value MwBaseWidget::Reparent(const Napi::CallbackInfo &info) {
|
||||
|
|
@ -230,68 +243,76 @@ Napi::Value MwBaseWidget::Reparent(const Napi::CallbackInfo &info) {
|
|||
parent = newParent;
|
||||
newParent->children.push_back(this);
|
||||
|
||||
return Napi::Value();
|
||||
return info.This();
|
||||
}
|
||||
|
||||
Napi::Value MwBaseWidget::Add(const Napi::CallbackInfo &info) {
|
||||
Napi::Value MwBaseWidget::AddChild(const Napi::CallbackInfo &info) {
|
||||
Napi::Env env = info.Env();
|
||||
|
||||
if (info.Length() < 1 || !info[0].IsObject()) {
|
||||
if (info.Length() < 1 || !info[0].IsArray()) {
|
||||
Napi::TypeError::New(env, "Expected an instance of a widget class as argument")
|
||||
.ThrowAsJavaScriptException();
|
||||
return env.Undefined();
|
||||
}
|
||||
|
||||
Napi::Object wrapped = info[0].As<Napi::Object>();
|
||||
const bool isInstance = wrapped.InstanceOf(constructor.Value());
|
||||
Napi::Array widgets = info[0].As<Napi::Array>();
|
||||
|
||||
if (isInstance) {
|
||||
MwBaseWidget *child = MwBaseWidget::Unwrap(wrapped);
|
||||
for (uint32_t i = 0; i < widgets.Length(); i++) {
|
||||
const Napi::Value value = widgets.Get(i);
|
||||
|
||||
if (child == this) {
|
||||
Napi::Error::New(env, "Cannot add widget as child of itself")
|
||||
if (!value.IsObject()) {
|
||||
Napi::TypeError::New(env,
|
||||
"Expected argument " + std::to_string(i) + "to be instance of a widget"
|
||||
)
|
||||
.ThrowAsJavaScriptException();
|
||||
return env.Null();
|
||||
}
|
||||
|
||||
if (child->parent) {
|
||||
// if the current parent has this widget as a child, disown :(
|
||||
for (int i = 0; i < child->parent->children.size(); i++) {
|
||||
if (child->parent->children[i] == this) {
|
||||
child->parent->children.erase(child->parent->children.begin() + i);
|
||||
break;
|
||||
const Napi::Object wrapped = widgets.As<Napi::Object>();
|
||||
const bool isInstance = wrapped.InstanceOf(constructor.Value());
|
||||
|
||||
if (isInstance) {
|
||||
MwBaseWidget *child = MwBaseWidget::Unwrap(wrapped);
|
||||
|
||||
if (child == this) {
|
||||
Napi::Error::New(env, "Cannot add widget as child of itself")
|
||||
.ThrowAsJavaScriptException();
|
||||
return env.Null();
|
||||
}
|
||||
|
||||
if (child->parent) {
|
||||
// if the current parent has this widget as a child, disown :(
|
||||
for (size_t i = 0; i < child->parent->children.size(); i++) {
|
||||
if (child->parent->children[i] == this) {
|
||||
child->parent->children.erase(child->parent->children.begin() + i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
MwReparent(
|
||||
child->handle,
|
||||
handle
|
||||
);
|
||||
|
||||
child->parent = this;
|
||||
children.push_back(child);
|
||||
} else {
|
||||
Napi::TypeError::New(env,"Argument " + std::to_string(i) + "is not an instance of a widget")
|
||||
.ThrowAsJavaScriptException();
|
||||
return env.Null();
|
||||
}
|
||||
|
||||
MwReparent(
|
||||
child->handle,
|
||||
handle
|
||||
);
|
||||
|
||||
child->parent = this;
|
||||
children.push_back(child);
|
||||
} else {
|
||||
Napi::TypeError::New(env, "Provided argument is not an instance of a widget")
|
||||
.ThrowAsJavaScriptException();
|
||||
return env.Null();
|
||||
}
|
||||
|
||||
return Napi::Value();
|
||||
return info.This();
|
||||
}
|
||||
|
||||
Napi::Value MwBaseWidget::Remove(const Napi::CallbackInfo &info) {
|
||||
if (parent) {
|
||||
for (size_t i = 0; i < parent->children.size(); i++) {
|
||||
if (parent->children[i] == this) {
|
||||
parent->children.erase(parent->children.begin() + i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
Napi::Value MwBaseWidget::Destroy(const Napi::CallbackInfo &info) {
|
||||
Napi::Env env = info.Env();
|
||||
if (handle)
|
||||
MwDestroyWidget(handle);
|
||||
|
||||
MwReparent(handle, nullptr);
|
||||
return Napi::Value();
|
||||
return env.Null();
|
||||
}
|
||||
|
||||
Napi::Value MwBaseWidget::GetParent(const Napi::CallbackInfo &info) {
|
||||
|
|
@ -366,3 +387,136 @@ Napi::Value MwBaseWidget::GetClipboard(const Napi::CallbackInfo &info) {
|
|||
|
||||
return env.Undefined();
|
||||
}
|
||||
|
||||
Napi::Value MwBaseWidget::GetNativeClass(const Napi::CallbackInfo &info) {
|
||||
Napi::Env env = info.Env();
|
||||
MwClass cls = MwGetClass(handle);
|
||||
|
||||
return Napi::External<_MwClass>::New(env, cls);
|
||||
}
|
||||
|
||||
Napi::Value MwBaseWidget::SetInteger(const Napi::CallbackInfo &info) {
|
||||
Napi::Env env = info.Env();
|
||||
|
||||
if (info.Length() < 2) {
|
||||
Napi::TypeError::New(env, "Expected two arguments for key and value")
|
||||
.ThrowAsJavaScriptException();
|
||||
return env.Null();
|
||||
}
|
||||
|
||||
if (!info[0].IsString() || !info[1].IsNumber()) {
|
||||
Napi::TypeError::New(env, "Expected key to be a string and value to be a number")
|
||||
.ThrowAsJavaScriptException();
|
||||
return env.Null();
|
||||
}
|
||||
|
||||
const std::string key = info[0].As<Napi::String>().Utf8Value();
|
||||
const int value = info[1].As<Napi::Number>().Int32Value();
|
||||
|
||||
MwSetInteger(handle, key.c_str(), value);
|
||||
return info.This();
|
||||
}
|
||||
|
||||
Napi::Value MwBaseWidget::SetText(const Napi::CallbackInfo &info) {
|
||||
Napi::Env env = info.Env();
|
||||
|
||||
if (info.Length() < 2) {
|
||||
Napi::TypeError::New(env, "Expected two arguments for key and value")
|
||||
.ThrowAsJavaScriptException();
|
||||
return env.Null();
|
||||
}
|
||||
|
||||
if (!info[0].IsString() || !info[1].IsString()) {
|
||||
Napi::TypeError::New(env, "Expected key and value to be a string")
|
||||
.ThrowAsJavaScriptException();
|
||||
return env.Null();
|
||||
}
|
||||
|
||||
const std::string key = info[0].As<Napi::String>().Utf8Value();
|
||||
const std::string value = info[1].As<Napi::String>().Utf8Value();
|
||||
|
||||
MwSetText(handle, key.c_str(), value.c_str());
|
||||
return info.This();
|
||||
}
|
||||
|
||||
Napi::Value MwBaseWidget::SetVoid(const Napi::CallbackInfo &info) {
|
||||
Napi::Env env = info.Env();
|
||||
|
||||
if (info.Length() < 2) {
|
||||
Napi::TypeError::New(env, "Expected two arguments for key and value")
|
||||
.ThrowAsJavaScriptException();
|
||||
return env.Null();
|
||||
}
|
||||
|
||||
if (!info[0].IsString() || !info[1].IsObject()) {
|
||||
Napi::TypeError::New(env, "Expected key to be a string and value to be a native object")
|
||||
.ThrowAsJavaScriptException();
|
||||
return env.Null();
|
||||
}
|
||||
|
||||
const std::string key = info[0].As<Napi::String>().Utf8Value();
|
||||
const Napi::Object obj = info[1].As<Napi::Object>();
|
||||
|
||||
MwPixmap *pixmap = MwPixmap::Unwrap(obj);
|
||||
|
||||
if (pixmap) {
|
||||
MwSetVoid(handle, key.c_str(), pixmap->handle);
|
||||
} else {
|
||||
Napi::TypeError::New(env, "Expected second argument to be a valid native object")
|
||||
.ThrowAsJavaScriptException();
|
||||
return env.Null();
|
||||
}
|
||||
|
||||
return info.This();
|
||||
}
|
||||
|
||||
Napi::Value MwBaseWidget::GetInteger(const Napi::CallbackInfo &info) {
|
||||
Napi::Env env = info.Env();
|
||||
|
||||
if (info.Length() < 1 || !info[0].IsString()) {
|
||||
Napi::TypeError::New(env, "Expected key to be an argument of type string")
|
||||
.ThrowAsJavaScriptException();
|
||||
return env.Null();
|
||||
}
|
||||
|
||||
const std::string key = info[0].As<Napi::String>().Utf8Value();
|
||||
const int value = MwGetInteger(handle, key.c_str());
|
||||
|
||||
return Napi::Number::New(env, static_cast<double>(value));
|
||||
}
|
||||
|
||||
Napi::Value MwBaseWidget::GetText(const Napi::CallbackInfo &info) {
|
||||
Napi::Env env = info.Env();
|
||||
|
||||
if (info.Length() < 1 || !info[0].IsString()) {
|
||||
Napi::TypeError::New(env, "Expected key to be an argument of type string")
|
||||
.ThrowAsJavaScriptException();
|
||||
return env.Null();
|
||||
}
|
||||
|
||||
const std::string key = info[0].As<Napi::String>().Utf8Value();
|
||||
const char *value = MwGetText(handle, key.c_str());
|
||||
|
||||
return Napi::String::New(env, value);
|
||||
}
|
||||
|
||||
Napi::Value MwBaseWidget::GetVoid(const Napi::CallbackInfo &info) {
|
||||
Napi::Env env = info.Env();
|
||||
|
||||
if (info.Length() < 1 || !info[0].IsString()) {
|
||||
Napi::TypeError::New(env, "Expected key to be an argument of type string")
|
||||
.ThrowAsJavaScriptException();
|
||||
return env.Null();
|
||||
}
|
||||
|
||||
if (!info[0].IsString() || !info[1].IsObject()) {
|
||||
Napi::TypeError::New(env, "Expected key to be a string and value to be a native object")
|
||||
.ThrowAsJavaScriptException();
|
||||
return env.Null();
|
||||
}
|
||||
|
||||
const std::string key = info[0].As<Napi::String>().Utf8Value();
|
||||
void *value = MwGetVoid(handle, key.c_str());
|
||||
|
||||
return Napi::External<void>::New(env, value);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,42 +1,53 @@
|
|||
#pragma once
|
||||
#include "pixmap.hpp"
|
||||
#include <Mw/Milsko.h>
|
||||
#include <napi.h>
|
||||
#include <vector>
|
||||
|
||||
class MwBaseWidget : public Napi::ObjectWrap<MwBaseWidget> {
|
||||
public:
|
||||
static Napi::Object Init(Napi::Env env, Napi::Object exports);
|
||||
MwBaseWidget(const Napi::CallbackInfo &info);
|
||||
~MwBaseWidget();
|
||||
static Napi::Object Init(Napi::Env env, Napi::Object exports);
|
||||
MwBaseWidget(const Napi::CallbackInfo &info);
|
||||
~MwBaseWidget();
|
||||
|
||||
protected:
|
||||
static Napi::FunctionReference constructor;
|
||||
static Napi::FunctionReference constructor;
|
||||
friend class MwPixmap;
|
||||
|
||||
MwWidget handle;
|
||||
MwBaseWidget *parent;
|
||||
std::vector<MwBaseWidget *> children;
|
||||
MwWidget handle;
|
||||
MwBaseWidget *parent;
|
||||
std::vector<MwBaseWidget *> children;
|
||||
|
||||
Napi::Value SetArea(const Napi::CallbackInfo &info);
|
||||
Napi::Value Move(const Napi::CallbackInfo &info);
|
||||
Napi::Value Resize(const Napi::CallbackInfo &info);
|
||||
Napi::Value Step(const Napi::CallbackInfo &info);
|
||||
Napi::Value Loop(const Napi::CallbackInfo &info);
|
||||
Napi::Value Show(const Napi::CallbackInfo &info);
|
||||
Napi::Value HideCursor(const Napi::CallbackInfo &info);
|
||||
Napi::Value GrabPointer(const Napi::CallbackInfo &info);
|
||||
Napi::Value AddTickList(const Napi::CallbackInfo &info);
|
||||
Napi::Value ForceRender(const Napi::CallbackInfo &info);
|
||||
Napi::Value Reparent(const Napi::CallbackInfo &info);
|
||||
Napi::Value Add(const Napi::CallbackInfo &info);
|
||||
Napi::Value Remove(const Napi::CallbackInfo &info);
|
||||
Napi::Value GetClipboard(const Napi::CallbackInfo &info);
|
||||
Napi::Value SetArea(const Napi::CallbackInfo &info);
|
||||
Napi::Value Move(const Napi::CallbackInfo &info);
|
||||
Napi::Value Resize(const Napi::CallbackInfo &info);
|
||||
Napi::Value Step(const Napi::CallbackInfo &info);
|
||||
Napi::Value Loop(const Napi::CallbackInfo &info);
|
||||
Napi::Value Show(const Napi::CallbackInfo &info);
|
||||
Napi::Value HideCursor(const Napi::CallbackInfo &info);
|
||||
Napi::Value GrabPointer(const Napi::CallbackInfo &info);
|
||||
Napi::Value Focus(const Napi::CallbackInfo &info);
|
||||
Napi::Value AddTickList(const Napi::CallbackInfo &info);
|
||||
Napi::Value ForceRender(const Napi::CallbackInfo &info);
|
||||
Napi::Value Reparent(const Napi::CallbackInfo &info);
|
||||
Napi::Value AddChild(const Napi::CallbackInfo &info);
|
||||
Napi::Value Destroy(const Napi::CallbackInfo &info);
|
||||
Napi::Value GetClipboard(const Napi::CallbackInfo &info);
|
||||
|
||||
Napi::Value SetInteger(const Napi::CallbackInfo &info);
|
||||
Napi::Value SetText(const Napi::CallbackInfo &info);
|
||||
Napi::Value SetVoid(const Napi::CallbackInfo &info);
|
||||
Napi::Value GetInteger(const Napi::CallbackInfo &info);
|
||||
Napi::Value GetText(const Napi::CallbackInfo &info);
|
||||
Napi::Value GetVoid(const Napi::CallbackInfo &info);
|
||||
|
||||
Napi::Value GetArea(const Napi::CallbackInfo &info);
|
||||
Napi::Value IsPending(const Napi::CallbackInfo &info);
|
||||
Napi::Value GetParent(const Napi::CallbackInfo &info);
|
||||
Napi::Value GetChildren(const Napi::CallbackInfo &info);
|
||||
Napi::Value GetName(const Napi::CallbackInfo &info);
|
||||
Napi::Value GetCursorCoord(const Napi::CallbackInfo &info);
|
||||
Napi::Value GetScreenSize(const Napi::CallbackInfo &info);
|
||||
Napi::Value GetCoordinateType(const Napi::CallbackInfo &info);
|
||||
};
|
||||
Napi::Value GetArea(const Napi::CallbackInfo &info);
|
||||
Napi::Value IsPending(const Napi::CallbackInfo &info);
|
||||
Napi::Value GetParent(const Napi::CallbackInfo &info);
|
||||
Napi::Value GetChildren(const Napi::CallbackInfo &info);
|
||||
Napi::Value GetName(const Napi::CallbackInfo &info);
|
||||
Napi::Value GetCursorCoord(const Napi::CallbackInfo &info);
|
||||
Napi::Value GetScreenSize(const Napi::CallbackInfo &info);
|
||||
Napi::Value GetCoordinateType(const Napi::CallbackInfo &info);
|
||||
Napi::Value GetNativeClass(const Napi::CallbackInfo &info);
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue