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