diff termemu.js @ 9:826a7ced69f8

Make the emulator screen resizable.
author John "Elwin" Edwards <elwin@sdf.org>
date Wed, 09 May 2012 13:38:05 -0700
parents ee22eb9ab009
children de01aafd4dd6
line wrap: on
line diff
--- a/termemu.js	Mon May 07 16:08:59 2012 -0700
+++ b/termemu.js	Wed May 09 13:38:05 2012 -0700
@@ -54,6 +54,7 @@
 // An object representing the terminal emulator.
 var termemu = {
   sessid: null, // Session key assigned by the server
+  alive: false,
   /* Some elements of the page. */
   inwrap: null,     // A non-table div wrapping the screen
   view: null,     // The div holding the terminal screen
@@ -192,14 +193,67 @@
     /* Attach them. */
     this.view = termdiv;
     this.screen = this.normbuf;
-    this.resize();
+    this.fixsize();
     this.cmove(0, 0);
   },
+  resize: function (h, w) {
+    if (this.screen == null)
+      return;
+    if (!(h > 0 && h < 256))
+      h = this.h;
+    if (!(w > 0 && w < 256))
+      w = this.w;
+    /* First give all the rows the right number of cells. */
+    var allrows = Array().concat(this.histbuf.childNodes, 
+                                 this.normbuf.childNodes, 
+                                 this.altbuf.childNodes);
+    for (var i = 0; i < allrows.length; i++) {
+      var row = allrows[i];
+      for (var j = Math.min(w, this.w); j < Math.max(w, this.w); j++) {
+        if (w < this.w)
+          row.removeChild(row.childNodes.lastChild);
+        else
+          row.appendChild(this.makeCell(' '));
+      }
+    }
+    this.w = w;
+    /* Now the rows. */
+    /* Resizing altbuf isn't always necessary. */
+    /* TODO resize and scrolling region interact in complicated ways that I 
+     * don't want to reverse-engineer.  For the moment, just don't do that.
+     */
+    if (h > this.h) {
+      for (var i = this.h; i < h; i++) {
+        this.normbuf.appendChild(this.makeRow());
+        this.altbuf.appendChild(this.makeRow());
+      }
+    }
+    else if (h < this.h) {
+      for (var i = h; i < this.h; i++) {
+        this.histbuf.appendChild(this.normbuf.firstChild);
+        if (this.altbuf.firstChild)
+          this.altbuf.removeChild(this.altbuf.firstChild);
+      }
+    }
+    /* Keep it on the bottom */
+    if (this.scrB == this.h - 1)
+      this.scrB = h - 1;
+    else if (this.scrB >= h)
+      this.scrB = h - 1;
+    if (this.scrT >= h - 1)
+      this.scrT = 0;
+    this.h = h;
+    this.fixsize();
+    /* If the cursor is now offscreen, cmove()'s sanity checks will fix it. */
+    this.cmove(null, null);
+    debug(1, "Size is now " + this.w + "x" + this.h);
+    return;
+  },
   valign: function () {
     if (this.screen == this.normbuf)
       this.inwrap.scrollTop = this.histbuf.clientHeight;
   },
-  resize: function () {
+  fixsize: function () {
     var owrap = document.getElementById("termwrap");
     /* Set the height up properly. */
     this.inwrap.style.height = this.screen.scrollHeight.toString() + "px";
@@ -215,7 +269,9 @@
   flipCursor: function () {
     /* Swaps the text and background colors of the active location. */
     /* This will change when other cursor styles are supported. */
-    if (this.c.x != null && this.c.y != null) {
+    /* Check: the cursor might be offscreen if it was resized. */
+    if (this.c.x != null && this.c.y != null && this.c.x >= 0 &&
+        this.c.x < this.w && this.c.y >= 0 && this.c.y < this.h) {
       var oldcell = this.screen.childNodes[this.c.y].childNodes[this.c.x];
       var tempswap = oldcell.style.color;
       oldcell.style.color = oldcell.style.backgroundColor;