<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin/global.css" type="text/css"?>
<window id="main" title="DomTerm" width="800" height="600" 
        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" onload="init()">
  <menubar id="menubar" hidden="false">
    <menu label="File" id="file-menu">
      <menupopup>
        <menuitem label="Save as HTML" oncommand="doSaveAs()"/>
        <menuitem label="Quit" oncommand="doQuit()"/>
      </menupopup>
    </menu>
    <menu label = "Edit" id="edit-menu">
      <menupopup>
        <menuitem label="Copy" oncommand="doCopy()"/>
        <menuitem label="Paste" oncommand="doPaste()"/>
      </menupopup>
    </menu>
    <menu label = "View" id="view-menu">
      <menupopup>
        <menuitem label="Show Menubar" type="checkbox" checked="true" oncommand="changeMenubarState();" id="menubar-show-menubar"/>
        <!-- Not working yet: <menuitem label="Full Screen" type="checkbox" checked="false" oncommand="changeFullScreenMode();"/>-->
      </menupopup>
    </menu>
    <menu label = "Terminal" id="terminal-menu">
      <menupopup>
        <menu id="input-mode-menu" label="Input mode">
          <menupopup>
            <menuitem id="menubar-inputmode-line" label="Line" type="radio" name="menubar-input-mode" oncommand="setInputMode(108)"/>
            <menuitem id="menubar-inputmode-char" label="Character mode" type="radio" name="menubar-input-mode" oncommand="setInputMode(99)"/>
            <menuitem id="menubar-inputmode-auto" label="Auto mode" type="radio" name="menubar-input-mode" checked="true" oncommand="setInputMode(97)"/>
          </menupopup>
        </menu>
        <menuitem id="menubar-block-caret"  type="checkbox" checked="true" label="Block caret (char mode only)" oncommand="changeCaret()"/>
      </menupopup>
    </menu>
  </menubar>
  <popupset>
    <menupopup id="mainmenu">
      <!--<menuitem label="Cut"/>-->
      <menuitem label="Show Menubar" type="checkbox" checked="true" oncommand="changeMenubarState();" id="context-show-menubar"/>
      <menuitem label="Copy" oncommand="doCopy()"/>
      <menuitem label="Paste" oncommand="doPaste()"/>
      <menu id="input-mode-menu" label="Input mode">
        <menupopup>
          <menuitem id="context-inputmode-line" label="Line" type="radio" name="context-input-mode" oncommand="setInputMode(108)"/>
          <menuitem id="context-inputmode-char"  label="Character mode" type="radio" name="context-input-mode" oncommand="setInputMode(99)"/>
          <menuitem id="context-inputmode-auto"  label="Auto mode" type="radio" name="context-input-mode" checked="true" oncommand="setInputMode(97)"/>
        </menupopup>
      </menu>
    </menupopup>
  </popupset>
  <browser id="browser" type="content" src="repl-client.html?wait" flex="1" context="mainmenu">
  </browser>
  <script>
    var domterm;
    function init() {
        var domtermWindow = document.getElementById("browser").contentWindow;
        var domtermElement = domtermWindow.document.getElementById("domterm1");
        var cmdLine = window.arguments[0];
        cmdLine = cmdLine.QueryInterface(Components.interfaces.nsICommandLine);
        var ajax = cmdLine.handleFlagWithParam("ajax", false);
        if (ajax)
            domtermWindow.connectAjax(ajax, domtermElement);
        else {
            var wspath = cmdLine.handleFlagWithParam("wspath", false);
            var wsprotocol = cmdLine.handleFlagWithParam("wsprotocol", false);
            if (! wsprotocol)
                wsprotocol = [];
            domtermWindow.connect(wspath, wsprotocol, domtermElement);
        }
        domterm = getDomTermObject();
        var dt = domterm;
        dt.updateWindowTitle = function(title) {
            document.title = title;
        };
        dt.close = function() { doQuit(); };
        dt._pickFile = function(callback) {
            const nsIFilePicker = Components.interfaces.nsIFilePicker;
            var fp = Components.classes["@mozilla.org/filepicker;1"]
	           .createInstance(nsIFilePicker);
            fp.init(window, "Save contents as HTML", nsIFilePicker.modeSave);
            fp.appendFilters(nsIFilePicker.filterAll);

            var rv = fp.show();
            if (rv == nsIFilePicker.returnOK || rv == nsIFilePicker.returnReplace) {
                callback(fp.file);
            }
        };
        dt.inputModeChanged = inputModeChanged;
        dt._writeFile = function(data, filePath) {
            var file = Components.classes["@mozilla.org/file/local;1"]
                .createInstance(Components.interfaces.nsILocalFile);
            var foStream = Components.classes["@mozilla.org/network/file-output-stream;1"]
            .createInstance(Components.interfaces.nsIFileOutputStream);
            file.initWithFile(filePath);
            foStream.init(file, 0x02 | 0x08 | 0x20, 0666, 0); 
            var converter = Components.classes["@mozilla.org/intl/converter-output-stream;1"].
                createInstance(Components.interfaces.nsIConverterOutputStream);
            converter.init(foStream, "UTF-8", 0, 0);
            converter.writeString(data);
            converter.close(); // this closes foStream
        };
    }
    function getDomTermObject() {
        return domterm;
    }
    function changeCaret() {
    var blockCaretItem = document.getElementById("menubar-block-caret");
        // The value returned by getAttribute appears to be backwards?
        var block = blockCaretItem.getAttribute("checked") == "true";
        getDomTermObject().setCaretStyle(block? 2 : 5);
        blockCaretItem.setAttribute("checked", block ? "true" : "false");
    }
    function changeMenubarState() {
        var menubar = document.getElementById("menubar");
        var hidden = menubar.hidden;
        document.getElementById("menubar").hidden = ! hidden;
        var inMenubar = document.getElementById("menubar-show-menubar");
        var inContext = document.getElementById("context-show-menubar");
        inMenubar.setAttribute("checked", hidden ? "true" : "false");
        inContext.setAttribute("checked", hidden ? "true" : "false");
    }
    function doCopy() {
        getDomTermObject().doCopy();
    }
    function doPaste() {
        getDomTermObject().doPaste();
    }
    function doSaveAs() {
        getDomTermObject().doSaveAs();
    }
    function inputModeChanged(mode) {
        var modeword;
        switch (mode) {
        case 108: modeword="line"; break;
        case 99: modeword="char"; break;
        case 97: modeword="auto"; break
        }
        var inMenubar = document.getElementById("menubar-inputmode-"+modeword);
        var inContext = document.getElementById("context-inputmode-"+modeword);
        inMenubar.setAttribute("checked", "true");
        inContext.setAttribute("checked", "true");
        getDomTermObject().prototype.inputModeChanged(mode);
    }
    function setInputMode(mode) {
        getDomTermObject().setInputMode(mode);
        inputModeChanged(mode);
    }
    function doQuit() { window.close(); }
  </script>
</window>
