]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - macterm.c
Cursor painting is now _right_ and scrollbar removal works better.
[PuTTY.git] / macterm.c
1 /* $Id: macterm.c,v 1.1.2.21 1999/03/14 15:51:34 ben Exp $ */
2 /*
3  * Copyright (c) 1999 Ben Harris
4  * All rights reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person
7  * obtaining a copy of this software and associated documentation
8  * files (the "Software"), to deal in the Software without
9  * restriction, including without limitation the rights to use,
10  * copy, modify, merge, publish, distribute, sublicense, and/or
11  * sell copies of the Software, and to permit persons to whom the
12  * Software is furnished to do so, subject to the following
13  * conditions:
14  * 
15  * The above copyright notice and this permission notice shall be
16  * included in all copies or substantial portions of the Software.
17  * 
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
22  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
23  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25  * SOFTWARE.
26  */
27
28 /*
29  * macterm.c -- Macintosh terminal front-end
30  */
31
32 #include <MacTypes.h>
33 #include <Controls.h>
34 #include <Fonts.h>
35 #include <Gestalt.h>
36 #include <MacWindows.h>
37 #include <Palettes.h>
38 #include <Quickdraw.h>
39 #include <QuickdrawText.h>
40 #include <Resources.h>
41 #include <Scrap.h>
42 #include <Sound.h>
43 #include <ToolUtils.h>
44
45 #include <limits.h>
46 #include <stdlib.h>
47 #include <stdio.h>
48
49 #include "macresid.h"
50 #include "putty.h"
51 #include "mac.h"
52
53 #define DEFAULT_FG      16
54 #define DEFAULT_FG_BOLD 17
55 #define DEFAULT_BG      18
56 #define DEFAULT_BG_BOLD 19
57 #define CURSOR_FG       20
58 #define CURSOR_FG_BOLD  21
59 #define CURSOR_BG       22
60 #define CURSOR_BG_BOLD  23
61
62 #define PTOCC(x) ((x) < 0 ? -(-(x - font_width - 1) / font_width) : \
63                             (x) / font_width)
64 #define PTOCR(y) ((y) < 0 ? -(-(y - font_height - 1) / font_height) : \
65                             (y) / font_height)
66
67 struct mac_session {
68     short               fontnum;
69     int                 font_ascent;
70     WindowPtr           window;
71     PaletteHandle       palette;
72     ControlHandle       scrollbar;
73 };
74
75 static void mac_initfont(struct mac_session *);
76 static void mac_initpalette(struct mac_session *);
77 static void mac_adjustsize(struct mac_session *, int, int);
78 static pascal void mac_scrolltracker(ControlHandle, short);
79 static pascal void do_text_for_device(short, short, GDHandle, long);
80 static int mac_keytrans(struct mac_session *, EventRecord *, unsigned char *);
81 static void text_click(struct mac_session *, EventRecord *);
82
83 /*
84  * Temporary hack till I get the terminal emulator supporting multiple
85  * sessions
86  */
87
88 static struct mac_session *onlysession;
89
90 static void inbuf_putc(int c) {
91     inbuf[inbuf_head] = c;
92     inbuf_head = (inbuf_head+1) & INBUF_MASK;
93 }
94
95 static void inbuf_putstr(const char *c) {
96     while (*c)
97         inbuf_putc(*c++);
98 }
99
100 static void display_resource(unsigned long type, short id) {
101     Handle h;
102     int len, i;
103     char *t;
104
105     h = GetResource(type, id);
106     if (h == NULL)
107         fatalbox("Can't get test resource");
108     SetResAttrs(h, GetResAttrs(h) | resLocked);
109     t = *h;
110     len = GetResourceSizeOnDisk(h);
111     for (i = 0; i < len; i++) {
112         inbuf_putc(t[i]);
113         term_out();
114     }
115     SetResAttrs(h, GetResAttrs(h) & ~resLocked);
116     ReleaseResource(h);
117 }
118         
119
120 void mac_newsession(void) {
121     struct mac_session *s;
122     int i;
123
124     /* This should obviously be initialised by other means */
125     mac_loadconfig(&cfg);
126 /*    back = &loop_backend; */
127     s = smalloc(sizeof(*s));
128     onlysession = s;
129         
130     /* XXX: Own storage management? */
131     if (mac_gestalts.qdvers == gestaltOriginalQD)
132         s->window = GetNewWindow(wTerminal, NULL, (WindowPtr)-1);
133     else
134         s->window = GetNewCWindow(wTerminal, NULL, (WindowPtr)-1);
135     SetWRefCon(s->window, (long)s);
136     s->scrollbar = GetNewControl(cVScroll, s->window);
137     term_init();
138     term_size(cfg.height, cfg.width, cfg.savelines);
139     mac_initfont(s);
140     mac_initpalette(s);
141     /* Set to FALSE to not get palette updates in the background. */
142     SetPalette(s->window, s->palette, TRUE); 
143     ActivatePalette(s->window);
144     ShowWindow(s->window);
145     display_resource('pTST', 128);
146 }
147
148 static void mac_initfont(struct mac_session *s) {
149     Str255 macfont;
150     FontInfo fi;
151  
152     SetPort(s->window);
153     macfont[0] = sprintf((char *)&macfont[1], "%s", cfg.font);
154     GetFNum(macfont, &s->fontnum);
155     TextFont(s->fontnum);
156     TextFace(cfg.fontisbold ? bold : 0);
157     TextSize(cfg.fontheight);
158     GetFontInfo(&fi);
159     font_width = fi.widMax;
160     font_height = fi.ascent + fi.descent + fi.leading;
161     s->font_ascent = fi.ascent;
162     mac_adjustsize(s, rows, cols);
163 }
164
165 /*
166  * To be called whenever the window size changes.
167  * rows and cols should be desired values.
168  * It's assumed the terminal emulator will be informed, and will set rows
169  * and cols for us.
170  */
171 static void mac_adjustsize(struct mac_session *s, int newrows, int newcols) {
172     int winwidth, winheight;
173
174     winwidth = newcols * font_width + 15;
175     winheight = newrows * font_height;
176     SizeWindow(s->window, winwidth, winheight, true);
177     HideControl(s->scrollbar);
178     MoveControl(s->scrollbar, winwidth - 15, -1);
179     SizeControl(s->scrollbar, 16, winheight - 13);
180     ShowControl(s->scrollbar);
181 }
182
183 static void mac_initpalette(struct mac_session *s) {
184     WinCTab ct;
185   
186     if (mac_gestalts.qdvers == gestaltOriginalQD)
187         return;
188     s->palette = NewPalette((*cfg.colours)->pmEntries, NULL, pmCourteous, 0);
189     if (s->palette == NULL)
190         fatalbox("Unable to create palette");
191     CopyPalette(cfg.colours, s->palette, 0, 0, (*cfg.colours)->pmEntries);
192 }
193
194 /*
195  * I don't think this is (a) safe or (b) a good way to do this.
196  */
197 static void mac_updatewinbg(struct mac_session *s) {
198     WinCTab ct;
199     WCTabPtr ctp = &ct;
200     WCTabHandle cth = &ctp;
201
202     ct.wCSeed = 0;
203     ct.wCReserved = 0;
204     ct.ctSize = 1;
205     ct.ctTable[0].value = wContentColor;
206     ct.ctTable[0].rgb = (*s->palette)->pmInfo[16].ciRGB;
207     SetWinColor(s->window, cth);
208 }
209
210 /*
211  * Enable/disable menu items based on the active terminal window.
212  */
213 void mac_adjusttermmenus(WindowPtr window) {
214     struct mac_session *s;
215     MenuHandle menu;
216     long offset;
217
218     s = (struct mac_session *)GetWRefCon(window);
219     menu = GetMenuHandle(mEdit);
220     EnableItem(menu, 0);
221     DisableItem(menu, iUndo);
222     DisableItem(menu, iCut);
223     DisableItem(menu, iCopy);
224     if (GetScrap(NULL, 'TEXT', &offset) == noTypeErr)
225         DisableItem(menu, iPaste);
226     else
227         EnableItem(menu, iPaste);
228     DisableItem(menu, iClear);
229     EnableItem(menu, iSelectAll);
230 }
231
232 void mac_clickterm(WindowPtr window, EventRecord *event) {
233     struct mac_session *s;
234     Point mouse;
235     ControlHandle control;
236     int part;
237
238     s = (struct mac_session *)GetWRefCon(window);
239     SetPort(window);
240     mouse = event->where;
241     GlobalToLocal(&mouse);
242     part = FindControl(mouse, window, &control);
243     if (control == s->scrollbar) {
244         switch (part) {
245           case kControlIndicatorPart:
246             if (TrackControl(control, mouse, NULL) == kControlIndicatorPart)
247                 term_scroll(+1, GetControlValue(control));
248             break;
249           case kControlUpButtonPart:
250           case kControlDownButtonPart:
251           case kControlPageUpPart:
252           case kControlPageDownPart:
253             TrackControl(control, mouse, mac_scrolltracker);
254             break;
255         }
256     } else {
257         text_click(s, event);
258     }
259 }
260
261 static void text_click(struct mac_session *s, EventRecord *event) {
262     Point localwhere;
263     int row, col;
264     static UInt32 lastwhen = 0;
265     static struct mac_session *lastsess = NULL;
266     static int lastrow = -1, lastcol = -1;
267     static Mouse_Action lastact = MA_NOTHING;
268
269     SetPort(s->window);
270     localwhere = event->where;
271     GlobalToLocal(&localwhere);
272
273     col = PTOCC(localwhere.h);
274     row = PTOCR(localwhere.v);
275     if (event->when - lastwhen < GetDblTime() &&
276         row == lastrow && col == lastcol && s == lastsess)
277         lastact = (lastact == MA_CLICK ? MA_2CLK :
278                    lastact == MA_2CLK ? MA_3CLK :
279                    lastact == MA_3CLK ? MA_CLICK : MA_NOTHING);
280     else
281         lastact = MA_CLICK;
282     term_mouse(event->modifiers & shiftKey ? MB_EXTEND : MB_SELECT, lastact,
283                col, row);
284     lastsess = s;
285     lastrow = row;
286     lastcol = col;
287     while (StillDown()) {
288         GetMouse(&localwhere);
289         col = PTOCC(localwhere.h);
290         row = PTOCR(localwhere.v);
291         term_mouse(event->modifiers & shiftKey ? MB_EXTEND : MB_SELECT,
292                    MA_DRAG, col, row);
293         if (row > rows - 1)
294             term_scroll(0, row - (rows - 1));
295         else if (row < 0)
296             term_scroll(0, row);
297     }
298     term_mouse(event->modifiers & shiftKey ? MB_EXTEND : MB_SELECT, MA_RELEASE,
299                col, row);
300     lastwhen = TickCount();
301 }
302
303 void write_clip(void *data, int len) {
304     
305     if (ZeroScrap() != noErr)
306         return;
307     PutScrap(len, 'TEXT', data);
308 }
309
310 void get_clip(void **p, int *lenp) {
311
312     /* XXX: do something */
313 }
314
315 static pascal void mac_scrolltracker(ControlHandle control, short part) {
316     struct mac_session *s;
317
318     s = (struct mac_session *)GetWRefCon((*control)->contrlOwner);
319     switch (part) {
320       case kControlUpButtonPart:
321         term_scroll(0, -1);
322         break;
323       case kControlDownButtonPart:
324         term_scroll(0, +1);
325         break;
326       case kControlPageUpPart:
327         term_scroll(0, -(rows - 1));
328         break;
329       case kControlPageDownPart:
330         term_scroll(0, +(rows - 1));
331         break;
332     }
333 }
334
335 #define K_SPACE 0x3100
336 #define K_BS    0x3300
337 #define K_F1    0x7a00
338 #define K_F2    0x7800
339 #define K_F3    0x6300
340 #define K_F4    0x7600
341 #define K_F5    0x6000
342 #define K_F6    0x6100
343 #define K_F7    0x6200
344 #define K_F8    0x6400
345 #define K_F9    0x6500
346 #define K_F10   0x6d00
347 #define K_F11   0x6700
348 #define K_F12   0x6f00
349 #define K_INSERT 0x7200
350 #define K_HOME  0x7300
351 #define K_PRIOR 0x7400
352 #define K_DELETE 0x7500
353 #define K_END   0x7700
354 #define K_NEXT  0x7900
355 #define K_LEFT  0x7b00
356 #define K_RIGHT 0x7c00
357 #define K_DOWN  0x7d00
358 #define K_UP    0x7e00
359 #define KP_0    0x5200
360 #define KP_1    0x5300
361 #define KP_2    0x5400
362 #define KP_3    0x5500
363 #define KP_4    0x5600
364 #define KP_5    0x5700
365 #define KP_6    0x5800
366 #define KP_7    0x5900
367 #define KP_8    0x5b00
368 #define KP_9    0x5c00
369 #define KP_CLEAR 0x4700
370 #define KP_EQUAL 0x5100
371 #define KP_SLASH 0x4b00
372 #define KP_STAR 0x4300
373 #define KP_PLUS 0x4500
374 #define KP_MINUS 0x4e00
375 #define KP_DOT  0x4100
376 #define KP_ENTER 0x4c00
377
378 void mac_keyterm(WindowPtr window, EventRecord *event) {
379     unsigned char buf[20];
380     int len;
381     struct mac_session *s;
382     int i;
383
384     s = (struct mac_session *)GetWRefCon(window);
385     len = mac_keytrans(s, event, buf);
386     /* XXX: I can't get the loopback backend to link, so we'll do this: */
387 /*    back->send((char *)buf, len); */
388     for (i = 0; i < len; i++)
389         inbuf_putc(buf[i]);
390     term_out();
391     term_update();
392 }
393
394 static int mac_keytrans(struct mac_session *s, EventRecord *event,
395                         unsigned char *output) {
396     unsigned char *p = output;
397     int code;
398
399     /* No meta key yet -- that'll be rather fun. */
400
401     /* Keys that we handle locally */
402     if (event->modifiers & shiftKey) {
403         switch (event->message & keyCodeMask) {
404           case K_PRIOR: /* shift-pageup */
405             term_scroll(0, -(rows - 1));
406             return 0;
407           case K_NEXT:  /* shift-pagedown */
408             term_scroll(0, +(rows - 1));
409             return 0;
410         }
411     }
412
413     /*
414      * Control-2 should return ^@ (0x00), Control-6 should return
415      * ^^ (0x1E), and Control-Minus should return ^_ (0x1F). Since
416      * the DOS keyboard handling did it, and we have nothing better
417      * to do with the key combo in question, we'll also map
418      * Control-Backquote to ^\ (0x1C).
419      */
420
421     if (event->modifiers & controlKey) {
422         switch (event->message & charCodeMask) {
423           case ' ': case '2':
424             *p++ = 0x00;
425             return p - output;
426           case '`':
427             *p++ = 0x1c;
428             return p - output;
429           case '6':
430             *p++ = 0x1e;
431             return p - output;
432           case '/':
433             *p++ = 0x1f;
434             return p - output;
435         }
436     }
437
438     /*
439      * First, all the keys that do tilde codes. (ESC '[' nn '~',
440      * for integer decimal nn.)
441      *
442      * We also deal with the weird ones here. Linux VCs replace F1
443      * to F5 by ESC [ [ A to ESC [ [ E. rxvt doesn't do _that_, but
444      * does replace Home and End (1~ and 4~) by ESC [ H and ESC O w
445      * respectively.
446      */
447     code = 0;
448     switch (event->message & keyCodeMask) {
449       case K_F1: code = (event->modifiers & shiftKey ? 23 : 11); break;
450       case K_F2: code = (event->modifiers & shiftKey ? 24 : 12); break;
451       case K_F3: code = (event->modifiers & shiftKey ? 25 : 13); break;
452       case K_F4: code = (event->modifiers & shiftKey ? 26 : 14); break;
453       case K_F5: code = (event->modifiers & shiftKey ? 28 : 15); break;
454       case K_F6: code = (event->modifiers & shiftKey ? 29 : 17); break;
455       case K_F7: code = (event->modifiers & shiftKey ? 31 : 18); break;
456       case K_F8: code = (event->modifiers & shiftKey ? 32 : 19); break;
457       case K_F9: code = (event->modifiers & shiftKey ? 33 : 20); break;
458       case K_F10: code = (event->modifiers & shiftKey ? 34 : 21); break;
459       case K_F11: code = 23; break;
460       case K_F12: code = 24; break;
461       case K_HOME: code = 1; break;
462       case K_INSERT: code = 2; break;
463       case K_DELETE: code = 3; break;
464       case K_END: code = 4; break;
465       case K_PRIOR: code = 5; break;
466       case K_NEXT: code = 6; break;
467     }
468     if (cfg.linux_funkeys && code >= 11 && code <= 15) {
469         p += sprintf((char *)p, "\x1B[[%c", code + 'A' - 11);
470         return p - output;
471     }
472     if (cfg.rxvt_homeend && (code == 1 || code == 4)) {
473         p += sprintf((char *)p, code == 1 ? "\x1B[H" : "\x1BOw");
474         return p - output;
475     }
476     if (code) {
477         p += sprintf((char *)p, "\x1B[%d~", code);
478         return p - output;
479     }
480
481     if (app_keypad_keys) {
482         switch (event->message & keyCodeMask) {
483           case KP_ENTER: p += sprintf((char *)p, "\x1BOM"); return p - output;
484           case KP_CLEAR: p += sprintf((char *)p, "\x1BOP"); return p - output;
485           case KP_EQUAL: p += sprintf((char *)p, "\x1BOQ"); return p - output;
486           case KP_SLASH: p += sprintf((char *)p, "\x1BOR"); return p - output;
487           case KP_STAR:  p += sprintf((char *)p, "\x1BOS"); return p - output;
488           case KP_PLUS:  p += sprintf((char *)p, "\x1BOl"); return p - output;
489           case KP_MINUS: p += sprintf((char *)p, "\x1BOm"); return p - output;
490           case KP_DOT:   p += sprintf((char *)p, "\x1BOn"); return p - output;
491           case KP_0:     p += sprintf((char *)p, "\x1BOp"); return p - output;
492           case KP_1:     p += sprintf((char *)p, "\x1BOq"); return p - output;
493           case KP_2:     p += sprintf((char *)p, "\x1BOr"); return p - output;
494           case KP_3:     p += sprintf((char *)p, "\x1BOs"); return p - output;
495           case KP_4:     p += sprintf((char *)p, "\x1BOt"); return p - output;
496           case KP_5:     p += sprintf((char *)p, "\x1BOu"); return p - output;
497           case KP_6:     p += sprintf((char *)p, "\x1BOv"); return p - output;
498           case KP_7:     p += sprintf((char *)p, "\x1BOw"); return p - output;
499           case KP_8:     p += sprintf((char *)p, "\x1BOx"); return p - output;
500           case KP_9:     p += sprintf((char *)p, "\x1BOy"); return p - output;
501         }
502     }
503
504     switch (event->message & keyCodeMask) {
505       case K_UP:
506         p += sprintf((char *)p, app_cursor_keys ? "\x1BOA" : "\x1B[A");
507         return p - output;
508       case K_DOWN:
509         p += sprintf((char *)p, app_cursor_keys ? "\x1BOB" : "\x1B[B");
510         return p - output;
511       case K_RIGHT:
512         p += sprintf((char *)p, app_cursor_keys ? "\x1BOC" : "\x1B[C");
513         return p - output;
514       case K_LEFT:
515         p += sprintf((char *)p, app_cursor_keys ? "\x1BOD" : "\x1B[D");
516         return p - output;
517       case K_BS:
518         *p++ = (cfg.bksp_is_delete ? 0x7f : 0x08);
519         return p - output;
520       default:
521         *p++ = event->message & charCodeMask;
522         return p - output;
523     }
524 }
525
526 void mac_growterm(WindowPtr window, EventRecord *event) {
527     Rect limits;
528     long grow_result;
529     int newrows, newcols;
530     struct mac_session *s;
531
532     s = (struct mac_session *)GetWRefCon(window);
533     SetRect(&limits, font_width + 15, font_height, SHRT_MAX, SHRT_MAX);
534     grow_result = GrowWindow(window, event->where, &limits);
535     if (grow_result != 0) {
536         newrows = HiWord(grow_result) / font_height;
537         newcols = (LoWord(grow_result) - 15) / font_width;
538         mac_adjustsize(s, newrows, newcols);
539         term_size(newrows, newcols, cfg.savelines);
540     }
541 }
542
543 void mac_activateterm(WindowPtr window, Boolean active) {
544     struct mac_session *s;
545
546     s = (struct mac_session *)GetWRefCon(window);
547     has_focus = active;
548     term_update();
549     if (active)
550         ShowControl(s->scrollbar);
551     else {
552         PmBackColor(DEFAULT_BG); /* HideControl clears behind the control */
553         HideControl(s->scrollbar);
554     }
555 }
556
557 void mac_updateterm(WindowPtr window) {
558     struct mac_session *s;
559     Rect clip;
560
561     s = (struct mac_session *)GetWRefCon(window);
562     BeginUpdate(window);
563     term_paint(s,
564                (*window->visRgn)->rgnBBox.left,
565                (*window->visRgn)->rgnBBox.top,
566                (*window->visRgn)->rgnBBox.right,
567                (*window->visRgn)->rgnBBox.bottom);
568     /* Restore default colours in case the Window Manager uses them */
569     PmForeColor(16);
570     PmBackColor(18);
571     if (FrontWindow() != window)
572         EraseRect(&(*s->scrollbar)->contrlRect);
573     UpdateControls(window, window->visRgn);
574     /* Stop DrawGrowIcon giving us space for a horizontal scrollbar */
575     SetRect(&clip, window->portRect.right - 15, SHRT_MIN, SHRT_MAX, SHRT_MAX);
576     ClipRect(&clip);
577     DrawGrowIcon(window);
578     clip.left = SHRT_MIN;
579     ClipRect(&clip);
580     EndUpdate(window);
581 }
582
583 struct do_text_args {
584     struct mac_session *s;
585     Rect textrect;
586     char *text;
587     int len;
588     unsigned long attr;
589 };
590
591 /*
592  * Call from the terminal emulator to draw a bit of text
593  *
594  * x and y are text row and column (zero-based)
595  */
596 void do_text(struct mac_session *s, int x, int y, char *text, int len,
597              unsigned long attr) {
598     int style = 0;
599     int bgcolour, fgcolour;
600     RGBColor rgbfore, rgbback;
601     struct do_text_args a;
602     RgnHandle textrgn;
603
604     SetPort(s->window);
605     
606     /* First check this text is relevant */
607     a.textrect.top = y * font_height;
608     a.textrect.bottom = (y + 1) * font_height;
609     a.textrect.left = x * font_width;
610     a.textrect.right = (x + len) * font_width;
611     if (!RectInRgn(&a.textrect, s->window->visRgn))
612         return;
613
614     a.s = s;
615     a.text = text;
616     a.len = len;
617     a.attr = attr;
618     SetPort(s->window);
619     TextFont(s->fontnum);
620     if (cfg.fontisbold || (attr & ATTR_BOLD) && !cfg.bold_colour)
621         style |= bold;
622     if (attr & ATTR_UNDER)
623         style |= underline;
624     TextFace(style);
625     TextSize(cfg.fontheight);
626     SetFractEnable(FALSE); /* We want characters on pixel boundaries */
627     textrgn = NewRgn();
628     RectRgn(textrgn, &a.textrect);
629     DeviceLoop(textrgn, do_text_for_device, (long)&a, 0);
630     DisposeRgn(textrgn);
631     /* Tell the window manager about it in case this isn't an update */
632     ValidRect(&a.textrect);
633 }
634
635 static pascal void do_text_for_device(short depth, short devflags,
636                                       GDHandle device, long cookie) {
637     struct do_text_args *a;
638     int bgcolour, fgcolour, bright;
639
640     a = (struct do_text_args *)cookie;
641
642     bright = (a->attr & ATTR_BOLD) && cfg.bold_colour;
643
644     if (a->attr & ATTR_REVERSE)
645         TextMode(notSrcCopy);
646     else
647         TextMode(srcCopy);
648
649     switch (depth) {
650       case 1:
651         /* XXX This should be done with a _little_ more configurability */
652         ForeColor(whiteColor);
653         BackColor(blackColor);
654         if (a->attr & ATTR_ACTCURS)
655             TextMode((a->attr & ATTR_REVERSE) ? srcCopy : notSrcCopy);
656         break;
657       case 2:
658         if (a->attr & ATTR_ACTCURS) {
659             PmForeColor(bright ? CURSOR_FG_BOLD : CURSOR_FG);
660             PmBackColor(CURSOR_BG);
661             TextMode(srcCopy);
662         } else {
663             PmForeColor(bright ? DEFAULT_FG_BOLD : DEFAULT_FG);
664             PmBackColor(DEFAULT_BG);
665         }
666         break;
667       default:
668         if (a->attr & ATTR_ACTCURS) {
669             fgcolour = (bright ? CURSOR_FG_BOLD : CURSOR_FG);
670             bgcolour = CURSOR_BG;
671             TextMode(srcCopy);
672         } else {
673             fgcolour = ((a->attr & ATTR_FGMASK) >> ATTR_FGSHIFT) * 2;
674             bgcolour = ((a->attr & ATTR_BGMASK) >> ATTR_BGSHIFT) * 2;
675             if (bright)
676                 if (a->attr & ATTR_REVERSE)
677                     bgcolour++;
678                 else
679                     fgcolour++;
680         }
681         PmForeColor(fgcolour);
682         PmBackColor(bgcolour);
683         break;
684     }
685
686     MoveTo(a->textrect.left, a->textrect.top + a->s->font_ascent);
687     DrawText(a->text, 0, a->len);
688
689     if (a->attr & ATTR_PASCURS) {
690         PenNormal();
691         switch (depth) {
692           case 1:
693             PenMode(patXor);
694             break;
695           default:
696             PmForeColor(CURSOR_BG);
697             break;
698         }
699         FrameRect(&a->textrect);
700     }
701 }
702
703 /*
704  * Call from the terminal emulator to get its graphics context.
705  */
706 struct mac_session *get_ctx(void) {
707
708     return onlysession;
709 }
710
711 /*
712  * Presumably this does something in Windows
713  */
714 void free_ctx(struct mac_session *ctx) {
715
716 }
717
718 /*
719  * Set the scroll bar position
720  *
721  * total is the line number of the bottom of the working screen
722  * start is the line number of the top of the display
723  * page is the length of the displayed page
724  */
725 void set_sbar(int total, int start, int page) {
726     struct mac_session *s = onlysession;
727
728     /* We don't redraw until we've set everything up, to avoid glitches */
729     (*s->scrollbar)->contrlMin = 0;
730     (*s->scrollbar)->contrlMax = total - page;
731     SetControlValue(s->scrollbar, start);
732 #if 0
733     /* XXX: This doesn't link for me. */
734     if (mac_gestalts.cntlattr & gestaltControlMgrPresent)
735         SetControlViewSize(s->scrollbar, page);
736 #endif
737 }
738
739 /*
740  * Beep
741  */
742 void beep(void) {
743
744     SysBeep(30);
745     /*
746      * XXX We should indicate the relevant window and/or use the
747      * Notification Manager
748      */
749 }
750
751 /*
752  * Set icon string -- a no-op here (Windowshade?)
753  */
754 void set_icon(char *icon) {
755
756 }
757
758 /*
759  * Set the window title
760  */
761 void set_title(char *title) {
762     Str255 mactitle;
763     struct mac_session *s = onlysession;
764
765     mactitle[0] = sprintf((char *)&mactitle[1], "%s", title);
766     SetWTitle(s->window, mactitle);
767 }
768
769 /*
770  * Resize the window at the emulator's request
771  */
772 void request_resize(int w, int h) {
773
774     cols = w;
775     rows = h;
776     mac_initfont(onlysession);
777 }
778
779 /*
780  * Set the logical palette
781  */
782 void palette_set(int n, int r, int g, int b) {
783     RGBColor col;
784     struct mac_session *s = onlysession;
785     static const int first[21] = {
786         0, 2, 4, 6, 8, 10, 12, 14,
787         1, 3, 5, 7, 9, 11, 13, 15,
788         16, 17, 18, 20, 22
789     };
790     
791     if (mac_gestalts.qdvers == gestaltOriginalQD)
792       return;
793     col.red   = r * 0x0101;
794     col.green = g * 0x0101;
795     col.blue  = b * 0x0101;
796     SetEntryColor(s->palette, first[n], &col);
797     if (first[n] >= 18)
798         SetEntryColor(s->palette, first[n]+1, &col);
799     ActivatePalette(s->window);
800 }
801
802 /*
803  * Reset to the default palette
804  */
805 void palette_reset(void) {
806     struct mac_session *s = onlysession;
807
808     if (mac_gestalts.qdvers == gestaltOriginalQD)
809         return;
810     CopyPalette(cfg.colours, s->palette, 0, 0, (*cfg.colours)->pmEntries);
811     ActivatePalette(s->window);
812     /* Palette Manager will generate update events as required. */
813 }
814
815 /*
816  * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
817  * for backward.)
818  */
819 void do_scroll(int topline, int botline, int lines) {
820     struct mac_session *s = onlysession;
821     Rect r;
822     RgnHandle update;
823
824     SetPort(s->window);
825     PmBackColor(DEFAULT_BG);
826     update = NewRgn();
827     SetRect(&r, 0, topline * font_height,
828             cols * font_width, (botline + 1) * font_height);
829     ScrollRect(&r, 0, - lines * font_height, update);
830     /* XXX: move update region? */
831     InvalRgn(update);
832     DisposeRgn(update);
833 }
834
835 /*
836  * Emacs magic:
837  * Local Variables:
838  * c-file-style: "simon"
839  * End:
840  */