Update gtk.js

This commit is contained in:
Namhyeon Go 2020-10-26 19:55:04 +09:00
parent b704189a44
commit cfcca23cfc

View File

@ -11,63 +11,134 @@ var binPath = "bin\\gtk-server.exe";
var GTKServer = SHELL.createExecObject([binPath, "-stdin"]); var GTKServer = SHELL.createExecObject([binPath, "-stdin"]);
// Common (Elements) // Common (Elements)
var GTKElements = []; var GTKWidgets = {};
// Common (Element) // Common (Element)
var GTKElement = function() { var GTKWidget = function(callback) {
this.type = "GTKElement"; this.widgetType = "GTKWidget";
this.width = 0; this.widgetID = GTKCreateWidget(this);
this.height = 0; this.callback = callback;
this.setWidth = function(width) { GTKWidgets[this.widgetID] = this;
this.width = width;
};
this.setHeight = function(height) {
this.height = height;
};
this,onchange = function() {};
this.onclick = function() {};
this.onmouseover = function() {};
this.onmouseout = function() {};
this.onkeydown = function() {};
this.onload = function() {};
this.addEventListener = function(ev, fn) {
if (typeof(fn) == "function") {
this['on' + ev] = fn;
} else {
throw new TypeError("listener must be a function");
}
};
GTKElements.push(this);
}; };
// GTKServer WriteLine // GTKCreateElement
var WriteLine = function(line) { var GTKCreateWidget = function(widget) {
var command = [];
switch(widget.widgetType) {
case "Window":
command.push("gtk_window_new");
command.push(this.type);
break;
case "Table":
command.push("gtk_table_new");
command.push(this.rows);
command.push(this.columns);
command.push(this.homogeneous);
// attach sub widgets to table
var subWidgets = widget.attachedWidgets;
for(var i = 0; i < subWidgets.length; i++) {
GTKExecCommand([
"gtk_table_attach_defaults",
this.widgetID,
subWidgets[i].widget.widgetID,
subWidgets[i].left,
subWidgets[i].right,
subWidgets[i].top,
subWidgets[i].buttom
]);
}
break;
case "Button":
command.push("gtk_button_new_with_label");
command,push(this.title);
break;
case "Entry":
command.push("gtk_entry_new");
break;
case "RadioBox":
command = "";
break;
case "CheckBox":
command = "";
break;
case "TextBox":
command.push("gtk_text_new");
command.push("NULL");
command.push("NULL");
break;
}
return GTKExecCommand(command);
};
// GTKExecCommand
var GTKExecCommand = function(command) {
var line, _command = [];
for(var i = 0; i < command.length; i++) {
if(typeof(command[i]) == "number") {
_command.push( (command[i] < 1 ? '0' : command[i]) );
} else if(typeof(command[i]) == "string") {
_command.push(command[i]);
}
}
line = _command.join(' ');
GTKServer.StdIn.WriteLine(line); GTKServer.StdIn.WriteLine(line);
return GTKServer.StdOut.ReadLine(); return GTKServer.StdOut.ReadLine();
}; };
// GTKInit
var GTKInit = function() {
return GTKExecCommand([
"gtk_init",
"NULL"
"NULL"
]);
};
// Window // Window
var Window = function() { var Window = function() {
GTKElement.apply(this, arguments); GTKWidget.apply(this, arguments);
this.type = "Window"; this.elementType = "Window";
this.type = 0;
this.title = "WelsonJS GTK GUI Application"; this.title = "WelsonJS GTK GUI Application";
this.show() = function() {
return GTKExecCommand([
"gtk_widget_show_all",
this.widgetID
]);
};
}; };
Window.prototype = new GTKElement(); Window.prototype = new GTKWidget();
Window.prototype.constructor = Window; Window.prototype.constructor = Window;
// Table // Table
var Table = function() { var Table = function() {
GTKElement.apply(this, arguments); GTKWidget.apply(this, arguments);
this.type = "Table"; this.elementType = "Table";
this.attachedElements = []; this.rows = 1;
this.attach = function(element, left, right, top, bottom) { this.columns = 1;
this.attachedElements.push({ this.homogeneous = true;
"element": element,
this.attachedWidgets = [];
this.attach = function(widget, left, right, top, bottom) {
this.attachedWidgets.push({
"widget": widget,
"left": left, "left": left,
"right": right, "right": right,
"top": top, "top": top,
@ -75,59 +146,80 @@ var Table = function() {
}); });
}; };
}; };
Table.prototype = new GTKElement(); Table.prototype = new GTKWidget();
Table.prototype.constructor = Table; Table.prototype.constructor = Table;
// Button // Button
var Button = function() { var Button = function() {
GTKElement.apply(this, arguments); GTKWidget.apply(this, arguments);
this.type = "Button"; this.elementType = "Button";
this.text = "New Button"; this.text = "New Button";
}; };
Button.prototype = new GTKElement(); Button.prototype = new GTKWidget();
Button.prototype.constructor = Button; Button.prototype.constructor = Button;
// Label // Entry
var Label = function() { var Entry = function() {
GTKElement.apply(this, arguments); GTKWidget.apply(this, arguments);
this.type = "Label"; this.elementType = "Entry";
this.text = "New Label"; this.text = "New Label";
this.focus = function() {
return GTKExecCommand([
"gtk_widget_grab_focus",
this.widgetID
]);
};
}; };
Label.prototype = new GTKElement(); Entry.prototype = new GTKWidget();
Label.prototype.constructor = Label; Entry.prototype.constructor = Entry;
// RadioBox // RadioBox
var RadioBox = function() { var RadioBox = function() {
GTKElement.apply(this, arguments); GTKWidget.apply(this, arguments);
this.type = "RadioBox"; this.elementType = "RadioBox";
this.text = "New RadioBox"; this.text = "New RadioBox";
}; };
RadioBox.prototype = new GTKElement(); RadioBox.prototype = new GTKWidget();
RadioBox.prototype.constructor = RadioBox; RadioBox.prototype.constructor = RadioBox;
// CheckBox // CheckBox
var CheckBox = function() { var CheckBox = function() {
GTKElement.apply(this, arguments); GTKWidget.apply(this, arguments);
this.type = "CheckBox"; this.elementType = "CheckBox";
this.text = "New CheckBox"; this.text = "New CheckBox";
}; };
CheckBox.prototype = new GTKElement(); CheckBox.prototype = new GTKWidget();
CheckBox.prototype.constructor = CheckBox; CheckBox.prototype.constructor = CheckBox;
// TextBox // TextBox
var TextBox = function() { var TextBox = function() {
GTKElement.apply(this, arguments); GTKWidget.apply(this, arguments);
this.type = "TextBox"; this.elementType = "TextBox";
this.text = "New TextBox"; this.text = "New TextBox";
}; };
TextBox.prototype = new GTKElement(); TextBox.prototype = new GTKWidget();
TextBox.prototype.constructor = TextBox; TextBox.prototype.constructor = TextBox;
exports.start = function(callback) { // GTKWait
// TODO: start var GTKWait = function() {
var even;
while(true) {
even = GTKExecCommand([
"gtk_server_callback",
"wait"
]);
if(even in GTKWidgets) {
GTKWidgets[even].callback();
}
}
}; };
exports.wait = GTKWait;