welsonjs/lib/gtk.js

431 lines
9.5 KiB
JavaScript
Raw Normal View History

2020-10-20 08:58:58 +00:00
////////////////////////////////////////////////////////////////////////
// GTKServer API
////////////////////////////////////////////////////////////////////////
2020-10-26 08:52:27 +00:00
var SHELL = require("lib/shell");
2020-10-20 09:12:32 +00:00
// set binPath
2020-10-28 08:18:17 +00:00
var binPath = "bin\\gtk-server";
2020-10-20 09:12:32 +00:00
2020-10-26 09:08:53 +00:00
// start GTKServer
2020-10-28 08:18:17 +00:00
var GTKServer = SHELL.createProcess([binPath, "-stdin"]);
2020-10-26 09:08:53 +00:00
2020-10-27 01:45:04 +00:00
// Common (Widgets)
2020-10-26 10:55:04 +00:00
var GTKWidgets = {};
2020-10-20 08:58:58 +00:00
// Common (Element)
2020-10-28 08:18:17 +00:00
var GTKWidget = function(options) {
2020-10-26 10:55:04 +00:00
this.widgetType = "GTKWidget";
2020-10-28 08:18:17 +00:00
this.widgetID = "0";
2020-10-28 06:14:04 +00:00
2020-10-27 01:45:04 +00:00
this.dispatchEvent = function(event) {
event.target = this;
if(event.eventName in this) {
this['on' + event.eventName](event);
}
};
this.addEventListener = function(eventName, fn) {
2020-10-27 01:27:47 +00:00
if (typeof(fn) == "function") {
2020-10-27 01:45:04 +00:00
this['on' + eventName] = fn;
2020-10-27 01:27:47 +00:00
} else {
throw new TypeError("EventListener must be a function");
}
2020-10-27 05:44:09 +00:00
};
2020-10-27 01:52:15 +00:00
this.eventListener = function() {
2020-10-27 02:08:16 +00:00
this.dispatchEvent(new GTKEvent("wait"));
2020-10-27 01:52:15 +00:00
return GTKEventListener(this);
2020-10-27 01:27:47 +00:00
};
2020-10-27 05:44:09 +00:00
this.setWidgetType = function(widgetType) {
this.widgetType = widgetType;
};
2020-10-28 08:22:29 +00:00
this.update = function() {
2020-10-28 08:18:17 +00:00
if (typeof(options) === "object") {
for (var k in options) {
this[k] = options[k];
}
}
2020-10-28 08:22:29 +00:00
};
this.create = function() {
this.update();
2020-10-28 06:14:04 +00:00
this.dispatchEvent(new GTKEvent("load"));
this.widgetID = GTKCreateWidget(this);
GTKWidgets[this.widgetID] = this;
}
2020-10-26 10:55:04 +00:00
};
2020-10-20 08:58:58 +00:00
2020-10-27 01:49:53 +00:00
// Common (definedEvents)
2020-10-27 01:27:47 +00:00
var definedEvents = {
2020-10-27 05:44:09 +00:00
"Window": [],
2020-10-27 02:08:16 +00:00
"Table": [],
"Button": ["click"],
2020-10-27 05:44:09 +00:00
"Entry": ["keyup"],
2020-10-27 02:08:16 +00:00
"RadioBox": ["click"],
2020-10-27 05:44:09 +00:00
"TextBox": []
2020-10-27 01:27:47 +00:00
};
2020-10-27 01:49:53 +00:00
// Common (Event)
var GTKEvent = function(eventName) {
this.eventName = eventName;
this.target = null;
};
2020-10-27 01:27:47 +00:00
// GTKEventListener
var GTKEventListener = function(widget) {
if (widget.widgetType in definedEvents) {
var widgetEvents = definedEvents[widget.widgetType];
for (var i = 0; i < widgetEvents.length; i++) {
2020-10-27 02:08:16 +00:00
widget.dispatchEvent(new GTKEvent(widgetEvents[i]));
2020-10-27 01:27:47 +00:00
}
}
};
2020-10-27 05:44:09 +00:00
// GTKCreateWidget
2020-10-26 10:55:04 +00:00
var GTKCreateWidget = function(widget) {
2020-10-26 12:04:53 +00:00
var widgetID, commands = [];
2020-10-26 10:55:04 +00:00
2020-10-26 12:01:05 +00:00
switch (widget.widgetType) {
2020-10-26 10:55:04 +00:00
case "Window":
2020-10-26 12:01:05 +00:00
commands.push([
"gtk_window_new",
2020-10-26 12:14:15 +00:00
widget.type
2020-10-26 12:01:05 +00:00
]);
2020-10-26 10:55:04 +00:00
break;
case "Table":
2020-10-26 12:14:15 +00:00
commands.push([
"gtk_table_new",
widget.rows,
widget.columns,
widget.homogeneous
]);
2020-10-26 10:55:04 +00:00
break;
case "Button":
2020-10-26 12:01:05 +00:00
commands.push([
"gtk_button_new_with_label",
2020-10-26 12:35:47 +00:00
"\"" + widget.text + "\""
2020-10-26 12:01:05 +00:00
]);
2020-10-26 10:55:04 +00:00
break;
2020-10-26 12:01:05 +00:00
2020-10-26 10:55:04 +00:00
case "Entry":
2020-10-26 12:01:05 +00:00
commands.push([
"gtk_entry_new",
"NULL",
"NULL"
]);
2020-10-26 12:04:53 +00:00
2020-10-26 10:55:04 +00:00
break;
case "RadioBox":
2020-10-26 12:01:05 +00:00
commands.push([
"gtk_radio_button_new_with_label_from_widget",
2020-10-28 08:22:29 +00:00
widget.memberWidget.widgetID,
2020-10-26 12:35:47 +00:00
"\"" + widget.text + "\""
2020-10-26 12:01:05 +00:00
]);
2020-10-26 10:55:04 +00:00
break;
case "TextBox":
2020-10-26 12:01:05 +00:00
commands.push([
"gtk_text_new",
"NULL",
"NULL"
]);
2020-10-26 10:55:04 +00:00
break;
}
2020-10-26 12:04:53 +00:00
while (commands.length > 0) {
2020-10-26 12:29:55 +00:00
widgetID = GTKExecCommand(commands.pop());
2020-10-26 12:01:05 +00:00
}
return widgetID;
2020-10-20 08:58:58 +00:00
};
2020-10-26 10:55:04 +00:00
// GTKExecCommand
var GTKExecCommand = function(command) {
var line, _command = [];
2020-10-26 12:01:05 +00:00
for (var i = 0; i < command.length; i++) {
2020-10-26 12:12:26 +00:00
var term = command[i];
if (typeof(term) == "number") {
2020-10-26 12:29:55 +00:00
_command.push(term == 0 ? '0' : term);
2020-10-26 12:12:26 +00:00
} else if (typeof(term) == "boolean") {
_command.push(!term ? '0' : '1');
} else if (typeof(term) == "string") {
_command.push(term);
2020-10-26 10:55:04 +00:00
}
}
line = _command.join(' ');
2020-10-27 05:44:09 +00:00
2020-10-28 08:18:17 +00:00
console.log(line);
2020-10-26 10:55:04 +00:00
2020-10-28 08:18:17 +00:00
GTKServer.StdIn.WriteLine(line);
return GTKServer.StdOut.ReadLine();
2020-10-26 08:52:27 +00:00
};
2020-10-26 10:55:04 +00:00
// GTKInit
2020-10-27 05:44:09 +00:00
var GTKInit = function(callback) {
GTKExecCommand([
2020-10-26 10:55:04 +00:00
"gtk_init",
2020-10-27 05:44:09 +00:00
"NULL",
2020-10-26 10:55:04 +00:00
"NULL"
]);
2020-10-27 05:44:09 +00:00
if (typeof(callback) == "function") {
callback();
}
2020-10-26 10:55:04 +00:00
};
2020-10-26 12:12:26 +00:00
// GTKExit
var GTKExit = function() {
return GTKExecCommand([
"gtk_server_exit"
]);
};
2020-10-20 08:58:58 +00:00
// Window
var Window = function() {
2020-10-26 10:55:04 +00:00
GTKWidget.apply(this, arguments);
2020-10-26 09:08:53 +00:00
2020-10-27 05:44:09 +00:00
this.widgetType = "Window";
2020-10-26 10:55:04 +00:00
this.type = 0;
2020-10-26 08:52:27 +00:00
this.title = "WelsonJS GTK GUI Application";
2020-10-28 08:18:17 +00:00
this.width = 450;
this.height = 400;
this.containerWidget = null;
this.setContainer = function(widget) {
this.containerWidget = widget;
return GTKExecCommand([
"gtk_container_add",
this.widgetID,
widget.widgetID
]);
};
2020-10-27 05:44:09 +00:00
this.show = function() {
2020-10-26 10:55:04 +00:00
return GTKExecCommand([
"gtk_widget_show_all",
this.widgetID
]);
};
2020-10-27 05:44:09 +00:00
this.setWidgetType(this.widgetType);
2020-10-28 06:14:04 +00:00
this.create();
2020-10-28 08:18:17 +00:00
GTKExecCommand([
"gtk_window_set_title",
this.widgetID,
"\"" + this.title + "\""
]);
GTKExecCommand([
"gtk_widget_set_usize",
this.widgetID,
this.width,
this.height
]);
2020-10-20 08:58:58 +00:00
};
2020-10-26 10:55:04 +00:00
Window.prototype = new GTKWidget();
2020-10-20 08:58:58 +00:00
Window.prototype.constructor = Window;
// Table
var Table = function() {
2020-10-26 10:55:04 +00:00
GTKWidget.apply(this, arguments);
2020-10-27 05:44:09 +00:00
this.widgetType = "Table";
2020-10-26 10:55:04 +00:00
this.rows = 1;
this.columns = 1;
this.homogeneous = true;
this.attachedWidgets = [];
this.attach = function(widget, left, right, top, bottom) {
2020-10-26 12:29:55 +00:00
this.attachedWidgets.push(widget);
return GTKExecCommand([
"gtk_table_attach_defaults",
this.widgetID,
widget.widgetID,
left,
right,
top,
bottom
]);
2020-10-20 08:58:58 +00:00
};
2020-10-27 05:44:09 +00:00
this.setWidgetType(this.widgetType);
2020-10-28 06:14:04 +00:00
this.create();
2020-10-20 08:58:58 +00:00
};
2020-10-26 10:55:04 +00:00
Table.prototype = new GTKWidget();
2020-10-20 08:58:58 +00:00
Table.prototype.constructor = Table;
// Button
var Button = function() {
2020-10-26 10:55:04 +00:00
GTKWidget.apply(this, arguments);
2020-10-26 12:01:05 +00:00
2020-10-27 05:44:09 +00:00
this.widgetType = "Button";
2020-10-26 08:52:27 +00:00
this.text = "New Button";
2020-10-27 05:44:09 +00:00
this.setWidgetType(this.widgetType);
2020-10-28 06:14:04 +00:00
this.create();
2020-10-20 08:58:58 +00:00
};
2020-10-26 10:55:04 +00:00
Button.prototype = new GTKWidget();
2020-10-20 08:58:58 +00:00
Button.prototype.constructor = Button;
2020-10-26 10:55:04 +00:00
// Entry
var Entry = function() {
GTKWidget.apply(this, arguments);
2020-10-27 05:44:09 +00:00
this.widgetType = "Entry";
2020-10-26 08:52:27 +00:00
this.text = "New Label";
2020-10-26 10:55:04 +00:00
this.focus = function() {
return GTKExecCommand([
"gtk_widget_grab_focus",
this.widgetID
]);
};
2020-10-26 12:01:05 +00:00
this.empty = function() {
2020-10-26 12:35:47 +00:00
GTKExecCommand([
2020-10-26 12:01:05 +00:00
"gtk_editable_delete_text",
this.widgetID,
0,
-1
]);
};
this.getText = function() {
2020-10-26 12:35:47 +00:00
this.text = GTKExecCommand([
2020-10-26 12:01:05 +00:00
"gtk_entry_get_text",
this.widgetID
]);
2020-10-26 12:35:47 +00:00
return this.text;
2020-10-26 12:01:05 +00:00
};
this.setText = function(text) {
2020-10-26 12:35:47 +00:00
this.text = text;
GTKExecCommand([
2020-10-26 12:01:05 +00:00
"gtk_entry_set_text",
this.widgetID,
2020-10-26 12:35:47 +00:00
"\"" + this.text + "\""
2020-10-26 12:01:05 +00:00
]);
};
this.setText(this.text);
2020-10-27 05:44:09 +00:00
this.setWidgetType(this.widgetType);
2020-10-28 06:14:04 +00:00
this.create();
2020-10-20 08:58:58 +00:00
};
2020-10-26 10:55:04 +00:00
Entry.prototype = new GTKWidget();
Entry.prototype.constructor = Entry;
2020-10-20 08:58:58 +00:00
// RadioBox
var RadioBox = function() {
2020-10-26 10:55:04 +00:00
GTKWidget.apply(this, arguments);
2020-10-27 05:44:09 +00:00
this.widgetType = "RadioBox";
2020-10-26 08:52:27 +00:00
this.text = "New RadioBox";
2020-10-28 08:18:17 +00:00
this.group = {
widgetID: "NULL"
};
this.memberWidget = {
widgetID: "NULL"
};
this.setMemberWidget = function(widget) {
this.memberWidget = widget;
};
2020-10-27 05:44:09 +00:00
this.setWidgetType(this.widgetType);
2020-10-28 08:18:17 +00:00
2020-10-28 08:22:29 +00:00
this.update();
2020-10-28 08:18:17 +00:00
if (this.group.widgetID != "NULL") {
this.group.add(this);
}
2020-10-28 08:22:29 +00:00
2020-10-28 06:14:04 +00:00
this.create();
2020-10-20 08:58:58 +00:00
};
2020-10-26 10:55:04 +00:00
RadioBox.prototype = new GTKWidget();
2020-10-20 08:58:58 +00:00
RadioBox.prototype.constructor = RadioBox;
2020-10-28 08:18:17 +00:00
// RadioGroup
var RadioGroup = function() {
GTKWidget.apply(this, arguments);
this.memberWidgets = [];
this.widgetType = "RadioGroup";
this.add = function(widget) {
2020-10-28 08:22:29 +00:00
if (this.memberWidgets.length > 0) {
2020-10-28 08:18:17 +00:00
widget.setMemberWidget(this.memberWidgets[0]);
}
this.memberWidgets.push(widget);
};
};
RadioBox.prototype = new GTKWidget();
RadioBox.prototype.constructor = RadioGroup;
2020-10-20 08:58:58 +00:00
// TextBox
var TextBox = function() {
2020-10-26 10:55:04 +00:00
GTKWidget.apply(this, arguments);
2020-10-26 12:01:05 +00:00
2020-10-27 05:44:09 +00:00
this.widgetType = "TextBox";
2020-10-26 08:52:27 +00:00
this.text = "New TextBox";
2020-10-26 12:01:05 +00:00
this.setText = function(text) {
2020-10-26 12:35:47 +00:00
this.text = text;
GTKExecCommand([
2020-10-26 12:01:05 +00:00
"gtk_text_insert",
this.widgetID,
"NULL",
"NULL",
"NULL",
2020-10-27 05:44:09 +00:00
"\"" + this.text + "\"",
2020-10-26 12:01:05 +00:00
"-1"
]);
};
this.setText(this.text);
2020-10-27 05:44:09 +00:00
this.setWidgetType(this.widgetType);
2020-10-28 06:14:04 +00:00
this.create();
2020-10-20 08:58:58 +00:00
};
2020-10-26 10:55:04 +00:00
TextBox.prototype = new GTKWidget();
2020-10-20 08:58:58 +00:00
TextBox.prototype.constructor = TextBox;
2020-10-20 09:12:32 +00:00
2020-10-26 10:55:04 +00:00
// GTKWait
2020-10-26 12:29:55 +00:00
var GTKWait = function(callback) {
2020-10-26 10:55:04 +00:00
var even;
2020-10-26 12:01:05 +00:00
while (true) {
2020-10-26 10:55:04 +00:00
even = GTKExecCommand([
"gtk_server_callback",
"wait"
]);
2020-10-26 12:01:05 +00:00
if (even in GTKWidgets) {
2020-10-27 01:27:47 +00:00
GTKWidgets[even].eventListener();
2020-10-26 10:55:04 +00:00
}
2020-10-26 12:29:55 +00:00
if (typeof(callback) == "function") {
callback(even);
}
2020-10-26 10:55:04 +00:00
}
2020-10-26 12:12:26 +00:00
GTKExit();
2020-10-20 09:12:32 +00:00
};
2020-10-26 10:55:04 +00:00
2020-10-27 05:44:09 +00:00
exports.Widget = GTKWidget;
2020-10-26 12:29:55 +00:00
exports.Window = Window;
exports.Table = Table;
exports.Button = Button;
exports.Entry = Entry;
exports.RadioBox = RadioBox;
2020-10-28 08:18:17 +00:00
exports.RadioGroup = RadioGroup;
2020-10-26 12:29:55 +00:00
exports.TextBox = TextBox;
2020-10-28 06:14:04 +00:00
2020-10-27 05:44:09 +00:00
exports.init = GTKInit;
2020-10-26 10:55:04 +00:00
exports.wait = GTKWait;
2020-10-28 08:18:17 +00:00
exports.exit = GTKExit;
2020-10-27 05:44:09 +00:00
exports.VERSIONINFO = "GTKServer Module (gtk.js) version 0.1";
exports.global = global;
exports.require = require;