diff --git a/lib/gtk.js b/lib/gtk.js index b8673ad..50f9579 100644 --- a/lib/gtk.js +++ b/lib/gtk.js @@ -11,63 +11,134 @@ var binPath = "bin\\gtk-server.exe"; var GTKServer = SHELL.createExecObject([binPath, "-stdin"]); // Common (Elements) -var GTKElements = []; +var GTKWidgets = {}; // Common (Element) -var GTKElement = function() { - this.type = "GTKElement"; - this.width = 0; - this.height = 0; - - this.setWidth = function(width) { - this.width = width; - }; - this.setHeight = function(height) { - this.height = height; - }; +var GTKWidget = function(callback) { + this.widgetType = "GTKWidget"; + this.widgetID = GTKCreateWidget(this); + this.callback = callback; - 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); + GTKWidgets[this.widgetID] = this; }; -// GTKServer WriteLine -var WriteLine = function(line) { +// GTKCreateElement +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); return GTKServer.StdOut.ReadLine(); }; +// GTKInit +var GTKInit = function() { + return GTKExecCommand([ + "gtk_init", + "NULL" + "NULL" + ]); +}; + // Window 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.show() = function() { + return GTKExecCommand([ + "gtk_widget_show_all", + this.widgetID + ]); + }; }; -Window.prototype = new GTKElement(); +Window.prototype = new GTKWidget(); Window.prototype.constructor = Window; // Table var Table = function() { - GTKElement.apply(this, arguments); + GTKWidget.apply(this, arguments); - this.type = "Table"; - this.attachedElements = []; - this.attach = function(element, left, right, top, bottom) { - this.attachedElements.push({ - "element": element, + this.elementType = "Table"; + this.rows = 1; + this.columns = 1; + this.homogeneous = true; + + this.attachedWidgets = []; + + this.attach = function(widget, left, right, top, bottom) { + this.attachedWidgets.push({ + "widget": widget, "left": left, "right": right, "top": top, @@ -75,59 +146,80 @@ var Table = function() { }); }; }; -Table.prototype = new GTKElement(); +Table.prototype = new GTKWidget(); Table.prototype.constructor = Table; // Button var Button = function() { - GTKElement.apply(this, arguments); + GTKWidget.apply(this, arguments); - this.type = "Button"; + this.elementType = "Button"; this.text = "New Button"; }; -Button.prototype = new GTKElement(); +Button.prototype = new GTKWidget(); Button.prototype.constructor = Button; -// Label -var Label = function() { - GTKElement.apply(this, arguments); - - this.type = "Label"; +// Entry +var Entry = function() { + GTKWidget.apply(this, arguments); + + this.elementType = "Entry"; this.text = "New Label"; + + this.focus = function() { + return GTKExecCommand([ + "gtk_widget_grab_focus", + this.widgetID + ]); + }; }; -Label.prototype = new GTKElement(); -Label.prototype.constructor = Label; +Entry.prototype = new GTKWidget(); +Entry.prototype.constructor = Entry; // RadioBox var RadioBox = function() { - GTKElement.apply(this, arguments); - - this.type = "RadioBox"; + GTKWidget.apply(this, arguments); + + this.elementType = "RadioBox"; this.text = "New RadioBox"; }; -RadioBox.prototype = new GTKElement(); +RadioBox.prototype = new GTKWidget(); RadioBox.prototype.constructor = RadioBox; // CheckBox var CheckBox = function() { - GTKElement.apply(this, arguments); + GTKWidget.apply(this, arguments); - this.type = "CheckBox"; + this.elementType = "CheckBox"; this.text = "New CheckBox"; }; -CheckBox.prototype = new GTKElement(); +CheckBox.prototype = new GTKWidget(); CheckBox.prototype.constructor = CheckBox; // TextBox var TextBox = function() { - GTKElement.apply(this, arguments); + GTKWidget.apply(this, arguments); - this.type = "TextBox"; + this.elementType = "TextBox"; this.text = "New TextBox"; }; -TextBox.prototype = new GTKElement(); +TextBox.prototype = new GTKWidget(); TextBox.prototype.constructor = TextBox; -exports.start = function(callback) { - // TODO: start +// GTKWait +var GTKWait = function() { + var even; + + while(true) { + even = GTKExecCommand([ + "gtk_server_callback", + "wait" + ]); + + if(even in GTKWidgets) { + GTKWidgets[even].callback(); + } + } }; + +exports.wait = GTKWait;