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