Update lib/gtk.js, std.js

This commit is contained in:
Namhyeon Go 2020-10-26 21:01:05 +09:00
parent cfcca23cfc
commit 9cd9bbc6b6
2 changed files with 101 additions and 45 deletions

View File

@ -1,7 +1,6 @@
////////////////////////////////////////////////////////////////////////
// GTKServer API
////////////////////////////////////////////////////////////////////////
var SHELL = require("lib/shell");
// set binPath
@ -14,34 +13,39 @@ var GTKServer = SHELL.createExecObject([binPath, "-stdin"]);
var GTKWidgets = {};
// Common (Element)
var GTKWidget = function(callback) {
var GTKWidget = function() {
this.widgetType = "GTKWidget";
this.widgetID = GTKCreateWidget(this);
this.callback = callback;
this.onEventTriggered = function() {};
GTKWidgets[this.widgetID] = this;
};
// GTKCreateElement
var GTKCreateWidget = function(widget) {
var command = [];
var widgetID = "", commands = [];
switch(widget.widgetType) {
switch (widget.widgetType) {
case "Window":
command.push("gtk_window_new");
command.push(this.type);
commands.push([
"gtk_window_new",
this.type
]);
break;
case "Table":
command.push("gtk_table_new");
command.push(this.rows);
command.push(this.columns);
command.push(this.homogeneous);
commands.push([
"gtk_table_new",
this.rows,
this.columns,
this.homogeneous
]);
// attach sub widgets to table
var subWidgets = widget.attachedWidgets;
for(var i = 0; i < subWidgets.length; i++) {
GTKExecCommand([
var countSubWidgets = subWidgets.length;
while(countSubWidgets > 0) {
commands.push([
"gtk_table_attach_defaults",
this.widgetID,
subWidgets[i].widget.widgetID,
@ -50,44 +54,62 @@ var GTKCreateWidget = function(widget) {
subWidgets[i].top,
subWidgets[i].buttom
]);
countSubWidgets--;
}
break;
case "Button":
command.push("gtk_button_new_with_label");
command,push(this.title);
commands.push([
"gtk_button_new_with_label",
this.text
]);
break;
case "Entry":
command.push("gtk_entry_new");
commands.push([
"gtk_entry_new",
"NULL",
"NULL"
]);
break;
case "RadioBox":
command = "";
break;
case "CheckBox":
command = "";
commands.push([
"gtk_radio_button_new_with_label_from_widget",
this.member,
this.text
]);
break;
case "TextBox":
command.push("gtk_text_new");
command.push("NULL");
command.push("NULL");
commands.push([
"gtk_text_new",
"NULL",
"NULL"
]);
break;
}
return GTKExecCommand(command);
// get widgetID from first command
widgetID = GTKExecCommand(commands.pop());
// execute next command
while(commands.length > 0) {
GTKExecCommand(commands.pop());
}
return widgetID;
};
// 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") {
for (var i = 0; i < command.length; i++) {
if (typeof(command[i]) == "number") {
_command.push((command[i] == 0 ? '0' : command[i]));
} else if (typeof(command[i]) == "string") {
_command.push(command[i]);
}
}
@ -172,6 +194,31 @@ var Entry = function() {
this.widgetID
]);
};
this.empty = function() {
return GTKExecCommand([
"gtk_editable_delete_text",
this.widgetID,
0,
-1
]);
};
this.getText = function() {
return GTKExecCommand([
"gtk_entry_get_text",
this.widgetID
]);
};
this.setText = function(text) {
.
return GTKExecCommand([
"gtk_entry_set_text",
this.widgetID,
CHR(34) + text + CHR(10) + CHR(34)
]);
};
this.setText(this.text);
};
Entry.prototype = new GTKWidget();
Entry.prototype.constructor = Entry;
@ -182,26 +229,31 @@ var RadioBox = function() {
this.elementType = "RadioBox";
this.text = "New RadioBox";
this.member = "NULL";
};
RadioBox.prototype = new GTKWidget();
RadioBox.prototype.constructor = RadioBox;
// CheckBox
var CheckBox = function() {
GTKWidget.apply(this, arguments);
this.elementType = "CheckBox";
this.text = "New CheckBox";
};
CheckBox.prototype = new GTKWidget();
CheckBox.prototype.constructor = CheckBox;
// TextBox
var TextBox = function() {
GTKWidget.apply(this, arguments);
this.elementType = "TextBox";
this.text = "New TextBox";
this.setText = function(text) {
return GTKExecCommand([
"gtk_text_insert",
this.widgetID,
"NULL",
"NULL",
"NULL",
CHR(34) + text + CHR(10) + CHR(34),
"-1"
]);
};
this.setText(this.text);
};
TextBox.prototype = new GTKWidget();
TextBox.prototype.constructor = TextBox;
@ -210,14 +262,14 @@ TextBox.prototype.constructor = TextBox;
var GTKWait = function() {
var even;
while(true) {
while (true) {
even = GTKExecCommand([
"gtk_server_callback",
"wait"
]);
if(even in GTKWidgets) {
GTKWidgets[even].callback();
if (even in GTKWidgets) {
GTKWidgets[even].onEventTriggered();
}
}
};

View File

@ -64,6 +64,10 @@ global.sleep = function(ms, callback) {
}
}
global.CHR = function(ord) {
return String.fromCharCode(ord);
}
/////////////////////////////////////////////////////////////////////////////////
// Private APIs / Utility functions
/////////////////////////////////////////////////////////////////////////////////