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