]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - macterm.c
First attempt at copying to the clipboard -- doesn't seem to work.
[PuTTY.git] / macterm.c
1 /* $Id: macterm.c,v 1.1.2.18 1999/03/11 23:23:45 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 - 1);
290     }
291     term_mouse(event->modifiers & shiftKey ? MB_EXTEND : MB_SELECT, MA_RELEASE,
292                col, row);
293     lastwhen = TickCount();
294 }
295
296 void write_clip(void *data, int len) {
297     
298     if (ZeroScrap() != noErr)
299         return;
300     PutScrap(len, 'TEXT', data);
301 }
302
303 void get_clip(void **p, int *lenp) {
304
305     /* XXX: do something */
306 }
307
308 static pascal void mac_scrolltracker(ControlHandle control, short part) {
309     struct mac_session *s;
310
311     s = (struct mac_session *)GetWRefCon((*control)->contrlOwner);
312     switch (part) {
313       case kControlUpButtonPart:
314         term_scroll(0, -1);
315         break;
316       case kControlDownButtonPart:
317         term_scroll(0, +1);
318         break;
319       case kControlPageUpPart:
320         term_scroll(0, -(rows - 1));
321         break;
322       case kControlPageDownPart:
323         term_scroll(0, +(rows - 1));
324         break;
325     }
326 }
327
328 #define K_SPACE 0x3100
329 #define K_BS    0x3300
330 #define K_F1    0x7a00
331 #define K_F2    0x7800
332 #define K_F3    0x6300
333 #define K_F4    0x7600
334 #define K_F5    0x6000
335 #define K_F6    0x6100
336 #define K_F7    0x6200
337 #define K_F8    0x6400
338 #define K_F9    0x6500
339 #define K_F10   0x6d00
340 #define K_F11   0x6700
341 #define K_F12   0x6f00
342 #define K_INSERT 0x7200
343 #define K_HOME  0x7300
344 #define K_PRIOR 0x7400
345 #define K_DELETE 0x7500
346 #define K_END   0x7700
347 #define K_NEXT  0x7900
348 #define K_LEFT  0x7b00
349 #define K_RIGHT 0x7c00
350 #define K_DOWN  0x7d00
351 #define K_UP    0x7e00
352 #define KP_0    0x5200
353 #define KP_1    0x5300
354 #define KP_2    0x5400
355 #define KP_3    0x5500
356 #define KP_4    0x5600
357 #define KP_5    0x5700
358 #define KP_6    0x5800
359 #define KP_7    0x5900
360 #define KP_8    0x5b00
361 #define KP_9    0x5c00
362 #define KP_CLEAR 0x4700
363 #define KP_EQUAL 0x5100
364 #define KP_SLASH 0x4b00
365 #define KP_STAR 0x4300
366 #define KP_PLUS 0x4500
367 #define KP_MINUS 0x4e00
368 #define KP_DOT  0x4100
369 #define KP_ENTER 0x4c00
370
371 void mac_keyterm(WindowPtr window, EventRecord *event) {
372     unsigned char buf[20];
373     int len;
374     struct mac_session *s;
375     int i;
376
377     s = (struct mac_session *)GetWRefCon(window);
378     len = mac_keytrans(s, event, buf);
379     /* XXX: I can't get the loopback backend to link, so we'll do this: */
380 /*    back->send((char *)buf, len); */
381     for (i = 0; i < len; i++)
382         inbuf_putc(buf[i]);
383     term_out();
384     term_update();
385 }
386
387 static int mac_keytrans(struct mac_session *s, EventRecord *event,
388                         unsigned char *output) {
389     unsigned char *p = output;
390     int code;
391
392     /* No meta key yet -- that'll be rather fun. */
393
394     /* Keys that we handle locally */
395     if (event->modifiers & shiftKey) {
396         switch (event->message & keyCodeMask) {
397           case K_PRIOR: /* shift-pageup */
398             term_scroll(0, -(rows - 1));
399             return 0;
400           case K_NEXT:  /* shift-pagedown */
401             term_scroll(0, +(rows - 1));
402             return 0;
403         }
404     }
405
406     /*
407      * Control-2 should return ^@ (0x00), Control-6 should return
408      * ^^ (0x1E), and Control-Minus should return ^_ (0x1F). Since
409      * the DOS keyboard handling did it, and we have nothing better
410      * to do with the key combo in question, we'll also map
411      * Control-Backquote to ^\ (0x1C).
412      */
413
414     if (event->modifiers & controlKey) {
415         switch (event->message & charCodeMask) {
416           case ' ': case '2':
417             *p++ = 0x00;
418             return p - output;
419           case '`':
420             *p++ = 0x1c;
421             return p - output;
422           case '6':
423             *p++ = 0x1e;
424             return p - output;
425           case '/':
426             *p++ = 0x1f;
427             return p - output;
428         }
429     }
430
431     /*
432      * First, all the keys that do tilde codes. (ESC '[' nn '~',
433      * for integer decimal nn.)
434      *
435      * We also deal with the weird ones here. Linux VCs replace F1
436      * to F5 by ESC [ [ A to ESC [ [ E. rxvt doesn't do _that_, but
437      * does replace Home and End (1~ and 4~) by ESC [ H and ESC O w
438      * respectively.
439      */
440     code = 0;
441     switch (event->message & keyCodeMask) {
442       case K_F1: code = (event->modifiers & shiftKey ? 23 : 11); break;
443       case K_F2: code = (event->modifiers & shiftKey ? 24 : 12); break;
444       case K_F3: code = (event->modifiers & shiftKey ? 25 : 13); break;
445       case K_F4: code = (event->modifiers & shiftKey ? 26 : 14); break;
446       case K_F5: code = (event->modifiers & shiftKey ? 28 : 15); break;
447       case K_F6: code = (event->modifiers & shiftKey ? 29 : 17); break;
448       case K_F7: code = (event->modifiers & shiftKey ? 31 : 18); break;
449       case K_F8: code = (event->modifiers & shiftKey ? 32 : 19); break;
450       case K_F9: code = (event->modifiers & shiftKey ? 33 : 20); break;
451       case K_F10: code = (event->modifiers & shiftKey ? 34 : 21); break;
452       case K_F11: code = 23; break;
453       case K_F12: code = 24; break;
454       case K_HOME: code = 1; break;
455       case K_INSERT: code = 2; break;
456       case K_DELETE: code = 3; break;
457       case K_END: code = 4; break;
458       case K_PRIOR: code = 5; break;
459       case K_NEXT: code = 6; break;
460     }
461     if (cfg.linux_funkeys && code >= 11 && code <= 15) {
462         p += sprintf((char *)p, "\x1B[[%c", code + 'A' - 11);
463         return p - output;
464     }
465     if (cfg.rxvt_homeend && (code == 1 || code == 4)) {
466         p += sprintf((char *)p, code == 1 ? "\x1B[H" : "\x1BOw");
467         return p - output;
468     }
469     if (code) {
470         p += sprintf((char *)p, "\x1B[%d~", code);
471         return p - output;
472     }
473
474     if (app_keypad_keys) {
475         switch (event->message & keyCodeMask) {
476           case KP_ENTER: p += sprintf((char *)p, "\x1BOM"); return p - output;
477           case KP_CLEAR: p += sprintf((char *)p, "\x1BOP"); return p - output;
478           case KP_EQUAL: p += sprintf((char *)p, "\x1BOQ"); return p - output;
479           case KP_SLASH: p += sprintf((char *)p, "\x1BOR"); return p - output;
480           case KP_STAR:  p += sprintf((char *)p, "\x1BOS"); return p - output;
481           case KP_PLUS:  p += sprintf((char *)p, "\x1BOl"); return p - output;
482           case KP_MINUS: p += sprintf((char *)p, "\x1BOm"); return p - output;
483           case KP_DOT:   p += sprintf((char *)p, "\x1BOn"); return p - output;
484           case KP_0:     p += sprintf((char *)p, "\x1BOp"); return p - output;
485           case KP_1:     p += sprintf((char *)p, "\x1BOq"); return p - output;
486           case KP_2:     p += sprintf((char *)p, "\x1BOr"); return p - output;
487           case KP_3:     p += sprintf((char *)p, "\x1BOs"); return p - output;
488           case KP_4:     p += sprintf((char *)p, "\x1BOt"); return p - output;
489           case KP_5:     p += sprintf((char *)p, "\x1BOu"); return p - output;
490           case KP_6:     p += sprintf((char *)p, "\x1BOv"); return p - output;
491           case KP_7:     p += sprintf((char *)p, "\x1BOw"); return p - output;
492           case KP_8:     p += sprintf((char *)p, "\x1BOx"); return p - output;
493           case KP_9:     p += sprintf((char *)p, "\x1BOy"); return p - output;
494         }
495     }
496
497     switch (event->message & keyCodeMask) {
498       case K_UP:
499         p += sprintf((char *)p, app_cursor_keys ? "\x1BOA" : "\x1B[A");
500         return p - output;
501       case K_DOWN:
502         p += sprintf((char *)p, app_cursor_keys ? "\x1BOB" : "\x1B[B");
503         return p - output;
504       case K_RIGHT:
505         p += sprintf((char *)p, app_cursor_keys ? "\x1BOC" : "\x1B[C");
506         return p - output;
507       case K_LEFT:
508         p += sprintf((char *)p, app_cursor_keys ? "\x1BOD" : "\x1B[D");
509         return p - output;
510       case K_BS:
511         *p++ = (cfg.bksp_is_delete ? 0x7f : 0x08);
512         return p - output;
513       default:
514         *p++ = event->message & charCodeMask;
515         return p - output;
516     }
517 }
518
519 void mac_growterm(WindowPtr window, EventRecord *event) {
520     Rect limits;
521     long grow_result;
522     int newrows, newcols;
523     struct mac_session *s;
524
525     s = (struct mac_session *)GetWRefCon(window);
526     SetRect(&limits, font_width + 15, font_height, SHRT_MAX, SHRT_MAX);
527     grow_result = GrowWindow(window, event->where, &limits);
528     if (grow_result != 0) {
529         newrows = HiWord(grow_result) / font_height;
530         newcols = (LoWord(grow_result) - 15) / font_width;
531         mac_adjustsize(s, newrows, newcols);
532         term_size(newrows, newcols, cfg.savelines);
533     }
534 }
535
536 void mac_activateterm(WindowPtr window, Boolean active) {
537     struct mac_session *s;
538
539     s = (struct mac_session *)GetWRefCon(window);
540     has_focus = active;
541     term_update();
542     if (active)
543         ShowControl(s->scrollbar);
544     else
545         HideControl(s->scrollbar);
546 }
547
548 void mac_updateterm(WindowPtr window) {
549     struct mac_session *s;
550     Rect clip;
551
552     s = (struct mac_session *)GetWRefCon(window);
553     BeginUpdate(window);
554     term_paint(s,
555                (*window->visRgn)->rgnBBox.left,
556                (*window->visRgn)->rgnBBox.top,
557                (*window->visRgn)->rgnBBox.right,
558                (*window->visRgn)->rgnBBox.bottom);
559     /* Restore default colours in case the Window Manager uses them */
560     PmForeColor(16);
561     PmBackColor(18);
562     if (FrontWindow() != window)
563         EraseRect(&(*s->scrollbar)->contrlRect);
564     UpdateControls(window, window->visRgn);
565     /* Stop DrawGrowIcon giving us space for a horizontal scrollbar */
566     SetRect(&clip, window->portRect.right - 15, SHRT_MIN, SHRT_MAX, SHRT_MAX);
567     ClipRect(&clip);
568     DrawGrowIcon(window);
569     clip.left = SHRT_MIN;
570     ClipRect(&clip);
571     EndUpdate(window);
572 }
573
574 struct do_text_args {
575     struct mac_session *s;
576     Rect textrect;
577     char *text;
578     int len;
579     unsigned long attr;
580 };
581
582 /*
583  * Call from the terminal emulator to draw a bit of text
584  *
585  * x and y are text row and column (zero-based)
586  */
587 void do_text(struct mac_session *s, int x, int y, char *text, int len,
588              unsigned long attr) {
589     int style = 0;
590     int bgcolour, fgcolour;
591     RGBColor rgbfore, rgbback;
592     struct do_text_args a;
593     RgnHandle textrgn;
594
595     SetPort(s->window);
596     
597     /* First check this text is relevant */
598     a.textrect.top = y * font_height;
599     a.textrect.bottom = (y + 1) * font_height;
600     a.textrect.left = x * font_width;
601     a.textrect.right = (x + len) * font_width;
602     if (!RectInRgn(&a.textrect, s->window->visRgn))
603         return;
604
605     a.s = s;
606     a.text = text;
607     a.len = len;
608     a.attr = attr;
609     SetPort(s->window);
610     TextFont(s->fontnum);
611     if (cfg.fontisbold || (attr & ATTR_BOLD) && !cfg.bold_colour)
612         style |= bold;
613     if (attr & ATTR_UNDER)
614         style |= underline;
615     TextFace(style);
616     TextSize(cfg.fontheight);
617     if (attr & ATTR_REVERSE)
618         TextMode(notSrcCopy);
619     else
620         TextMode(srcCopy);
621     SetFractEnable(FALSE); /* We want characters on pixel boundaries */
622     textrgn = NewRgn();
623     RectRgn(textrgn, &a.textrect);
624     DeviceLoop(textrgn, do_text_for_device, (long)&a, 0);
625     /* Tell the window manager about it in case this isn't an update */
626     DisposeRgn(textrgn);
627     ValidRect(&a.textrect);
628 }
629
630 static pascal void do_text_for_device(short depth, short devflags,
631                                       GDHandle device, long cookie) {
632     struct do_text_args *a;
633     int bgcolour, fgcolour;
634
635     a = (struct do_text_args *)cookie;
636
637     switch (depth) {
638       case 1:
639         /* XXX This should be done with a _little_ more configurability */
640         ForeColor(whiteColor);
641         BackColor(blackColor);
642         break;
643       case 2:
644         if ((a->attr & ATTR_BOLD) && cfg.bold_colour)
645             PmForeColor(DEFAULT_FG_BOLD);
646         else
647             PmForeColor(DEFAULT_FG);
648         if (a->attr & ATTR_ACTCURS)
649             PmBackColor(CURSOR_FG);
650         else
651             PmBackColor(DEFAULT_BG);
652         break;
653       default:
654         fgcolour = ((a->attr & ATTR_FGMASK) >> ATTR_FGSHIFT) * 2;
655         bgcolour = ((a->attr & ATTR_BGMASK) >> ATTR_BGSHIFT) * 2;
656         if ((a->attr & ATTR_BOLD) && cfg.bold_colour)
657             if (a->attr & ATTR_REVERSE)
658                 bgcolour++;
659             else
660                 fgcolour++;
661         if (a->attr & ATTR_ACTCURS)
662             bgcolour = CURSOR_FG;
663         PmForeColor(fgcolour);
664         PmBackColor(bgcolour);
665         break;
666     }
667     MoveTo(a->textrect.left, a->textrect.top + a->s->font_ascent);
668     DrawText(a->text, 0, a->len);
669 }
670
671 /*
672  * Call from the terminal emulator to get its graphics context.
673  */
674 struct mac_session *get_ctx(void) {
675
676     return onlysession;
677 }
678
679 /*
680  * Presumably this does something in Windows
681  */
682 void free_ctx(struct mac_session *ctx) {
683
684 }
685
686 /*
687  * Set the scroll bar position
688  *
689  * total is the line number of the bottom of the working screen
690  * start is the line number of the top of the display
691  * page is the length of the displayed page
692  */
693 void set_sbar(int total, int start, int page) {
694     struct mac_session *s = onlysession;
695
696     /* We don't redraw until we've set everything up, to avoid glitches */
697     (*s->scrollbar)->contrlMin = 0;
698     (*s->scrollbar)->contrlMax = total - page;
699     SetControlValue(s->scrollbar, start);
700 #if 0
701     /* XXX: This doesn't link for me. */
702     if (mac_gestalts.cntlattr & gestaltControlMgrPresent)
703         SetControlViewSize(s->scrollbar, page);
704 #endif
705 }
706
707 /*
708  * Beep
709  */
710 void beep(void) {
711
712     SysBeep(30);
713     /*
714      * XXX We should indicate the relevant window and/or use the
715      * Notification Manager
716      */
717 }
718
719 /*
720  * Set icon string -- a no-op here (Windowshade?)
721  */
722 void set_icon(char *icon) {
723
724 }
725
726 /*
727  * Set the window title
728  */
729 void set_title(char *title) {
730     Str255 mactitle;
731     struct mac_session *s = onlysession;
732
733     mactitle[0] = sprintf((char *)&mactitle[1], "%s", title);
734     SetWTitle(s->window, mactitle);
735 }
736
737 /*
738  * Resize the window at the emulator's request
739  */
740 void request_resize(int w, int h) {
741
742     cols = w;
743     rows = h;
744     mac_initfont(onlysession);
745 }
746
747 /*
748  * Set the logical palette
749  */
750 void palette_set(int n, int r, int g, int b) {
751     RGBColor col;
752     struct mac_session *s = onlysession;
753     static const int first[21] = {
754         0, 2, 4, 6, 8, 10, 12, 14,
755         1, 3, 5, 7, 9, 11, 13, 15,
756         16, 17, 18, 20, 22
757     };
758     
759     if (mac_gestalts.qdvers == gestaltOriginalQD)
760       return;
761     col.red   = r * 0x0101;
762     col.green = g * 0x0101;
763     col.blue  = b * 0x0101;
764     SetEntryColor(s->palette, first[n], &col);
765     if (first[n] >= 18)
766         SetEntryColor(s->palette, first[n]+1, &col);
767     ActivatePalette(s->window);
768 }
769
770 /*
771  * Reset to the default palette
772  */
773 void palette_reset(void) {
774     struct mac_session *s = onlysession;
775
776     if (mac_gestalts.qdvers == gestaltOriginalQD)
777         return;
778     CopyPalette(cfg.colours, s->palette, 0, 0, (*cfg.colours)->pmEntries);
779     ActivatePalette(s->window);
780     /* Palette Manager will generate update events as required. */
781 }
782
783 /*
784  * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
785  * for backward.)
786  */
787 void do_scroll(int topline, int botline, int lines) {
788     struct mac_session *s = onlysession;
789     Rect r;
790     RgnHandle update;
791
792     SetPort(s->window);
793     PmBackColor(DEFAULT_BG);
794     update = NewRgn();
795     SetRect(&r, 0, topline * font_height,
796             cols * font_width, (botline + 1) * font_height);
797     ScrollRect(&r, 0, - lines * font_height, update);
798     /* XXX: move update region? */
799     InvalRgn(update);
800     DisposeRgn(update);
801 }
802
803 /*
804  * Emacs magic:
805  * Local Variables:
806  * c-file-style: "simon"
807  * End:
808  */