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