comparison 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
comparison
equal deleted inserted replaced
8:ad0a31e52007 9:826a7ced69f8
52 } 52 }
53 53
54 // An object representing the terminal emulator. 54 // An object representing the terminal emulator.
55 var termemu = { 55 var termemu = {
56 sessid: null, // Session key assigned by the server 56 sessid: null, // Session key assigned by the server
57 alive: false,
57 /* Some elements of the page. */ 58 /* Some elements of the page. */
58 inwrap: null, // A non-table div wrapping the screen 59 inwrap: null, // A non-table div wrapping the screen
59 view: null, // The div holding the terminal screen 60 view: null, // The div holding the terminal screen
60 screen: null, // The div representing the active screen area 61 screen: null, // The div representing the active screen area
61 normbuf: null, // The normal screen buffer 62 normbuf: null, // The normal screen buffer
190 this.altbuf.style.display = "none"; 191 this.altbuf.style.display = "none";
191 /* altbuf will be filled when it is used. */ 192 /* altbuf will be filled when it is used. */
192 /* Attach them. */ 193 /* Attach them. */
193 this.view = termdiv; 194 this.view = termdiv;
194 this.screen = this.normbuf; 195 this.screen = this.normbuf;
195 this.resize(); 196 this.fixsize();
196 this.cmove(0, 0); 197 this.cmove(0, 0);
198 },
199 resize: function (h, w) {
200 if (this.screen == null)
201 return;
202 if (!(h > 0 && h < 256))
203 h = this.h;
204 if (!(w > 0 && w < 256))
205 w = this.w;
206 /* First give all the rows the right number of cells. */
207 var allrows = Array().concat(this.histbuf.childNodes,
208 this.normbuf.childNodes,
209 this.altbuf.childNodes);
210 for (var i = 0; i < allrows.length; i++) {
211 var row = allrows[i];
212 for (var j = Math.min(w, this.w); j < Math.max(w, this.w); j++) {
213 if (w < this.w)
214 row.removeChild(row.childNodes.lastChild);
215 else
216 row.appendChild(this.makeCell(' '));
217 }
218 }
219 this.w = w;
220 /* Now the rows. */
221 /* Resizing altbuf isn't always necessary. */
222 /* TODO resize and scrolling region interact in complicated ways that I
223 * don't want to reverse-engineer. For the moment, just don't do that.
224 */
225 if (h > this.h) {
226 for (var i = this.h; i < h; i++) {
227 this.normbuf.appendChild(this.makeRow());
228 this.altbuf.appendChild(this.makeRow());
229 }
230 }
231 else if (h < this.h) {
232 for (var i = h; i < this.h; i++) {
233 this.histbuf.appendChild(this.normbuf.firstChild);
234 if (this.altbuf.firstChild)
235 this.altbuf.removeChild(this.altbuf.firstChild);
236 }
237 }
238 /* Keep it on the bottom */
239 if (this.scrB == this.h - 1)
240 this.scrB = h - 1;
241 else if (this.scrB >= h)
242 this.scrB = h - 1;
243 if (this.scrT >= h - 1)
244 this.scrT = 0;
245 this.h = h;
246 this.fixsize();
247 /* If the cursor is now offscreen, cmove()'s sanity checks will fix it. */
248 this.cmove(null, null);
249 debug(1, "Size is now " + this.w + "x" + this.h);
250 return;
197 }, 251 },
198 valign: function () { 252 valign: function () {
199 if (this.screen == this.normbuf) 253 if (this.screen == this.normbuf)
200 this.inwrap.scrollTop = this.histbuf.clientHeight; 254 this.inwrap.scrollTop = this.histbuf.clientHeight;
201 }, 255 },
202 resize: function () { 256 fixsize: function () {
203 var owrap = document.getElementById("termwrap"); 257 var owrap = document.getElementById("termwrap");
204 /* Set the height up properly. */ 258 /* Set the height up properly. */
205 this.inwrap.style.height = this.screen.scrollHeight.toString() + "px"; 259 this.inwrap.style.height = this.screen.scrollHeight.toString() + "px";
206 this.valign(); 260 this.valign();
207 // Figure out how wide the vertical scrollbar is. 261 // Figure out how wide the vertical scrollbar is.
213 }, 267 },
214 comseq: [], // Part of an impending control sequence 268 comseq: [], // Part of an impending control sequence
215 flipCursor: function () { 269 flipCursor: function () {
216 /* Swaps the text and background colors of the active location. */ 270 /* Swaps the text and background colors of the active location. */
217 /* This will change when other cursor styles are supported. */ 271 /* This will change when other cursor styles are supported. */
218 if (this.c.x != null && this.c.y != null) { 272 /* Check: the cursor might be offscreen if it was resized. */
273 if (this.c.x != null && this.c.y != null && this.c.x >= 0 &&
274 this.c.x < this.w && this.c.y >= 0 && this.c.y < this.h) {
219 var oldcell = this.screen.childNodes[this.c.y].childNodes[this.c.x]; 275 var oldcell = this.screen.childNodes[this.c.y].childNodes[this.c.x];
220 var tempswap = oldcell.style.color; 276 var tempswap = oldcell.style.color;
221 oldcell.style.color = oldcell.style.backgroundColor; 277 oldcell.style.color = oldcell.style.backgroundColor;
222 oldcell.style.backgroundColor = tempswap; 278 oldcell.style.backgroundColor = tempswap;
223 } 279 }