]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - macterm.c
Right, I think we now hove scrolling optimisations. They should probably be
[PuTTY.git] / macterm.c
1 /* $Id: macterm.c,v 1.1.2.12 1999/03/02 21:51:55 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 <Sound.h>
42
43 #include <limits.h>
44 #include <stdlib.h>
45
46 #include "macresid.h"
47 #include "putty.h"
48 #include "mac.h"
49
50 struct mac_session {
51     short               fontnum;
52     int                 font_ascent;
53     WindowPtr           window;
54     PaletteHandle       palette;
55     ControlHandle       scrollbar;
56 };
57
58 static void mac_initfont(struct mac_session *);
59 static void mac_initpalette(struct mac_session *);
60 static void mac_adjustsize(struct mac_session *);
61 static pascal void mac_scrolltracker(ControlHandle, short);
62 static pascal void do_text_for_device(short, short, GDHandle, long);
63
64 /*
65  * Temporary hack till I get the terminal emulator supporting multiple
66  * sessions
67  */
68
69 static struct mac_session *onlysession;
70
71 static void inbuf_putc(int c) {
72     inbuf[inbuf_head] = c;
73     inbuf_head = (inbuf_head+1) & INBUF_MASK;
74 }
75
76 static void inbuf_putstr(const char *c) {
77     while (*c)
78         inbuf_putc(*c++);
79 }
80
81 static void display_resource(unsigned long type, short id) {
82     Handle h;
83     int len, i;
84     char *t;
85
86     h = GetResource(type, id);
87     if (h == NULL)
88         fatalbox("Can't get test resource");
89     SetResAttrs(h, GetResAttrs(h) | resLocked);
90     t = *h;
91     len = GetResourceSizeOnDisk(h);
92     for (i = 0; i < len; i++) {
93         inbuf_putc(t[i]);
94         term_out();
95     }
96     SetResAttrs(h, GetResAttrs(h) & ~resLocked);
97     ReleaseResource(h);
98 }
99         
100
101 void mac_newsession(void) {
102     struct mac_session *s;
103     int i;
104
105     /* This should obviously be initialised by other means */
106     mac_loadconfig(&cfg);
107     s = smalloc(sizeof(*s));
108     onlysession = s;
109         
110     /* XXX: Own storage management? */
111     if (mac_gestalts.qdvers == gestaltOriginalQD)
112         s->window = GetNewWindow(wTerminal, NULL, (WindowPtr)-1);
113     else
114         s->window = GetNewCWindow(wTerminal, NULL, (WindowPtr)-1);
115     SetWRefCon(s->window, (long)s);
116     s->scrollbar = GetNewControl(cVScroll, s->window);
117     term_init();
118     term_size(cfg.height, cfg.width, cfg.savelines);
119     mac_initfont(s);
120     mac_initpalette(s);
121     /* Set to FALSE to not get palette updates in the background. */
122     SetPalette(s->window, s->palette, TRUE); 
123     ActivatePalette(s->window);
124     ShowWindow(s->window);
125     display_resource('pTST', 128);
126 }
127
128 static void mac_initfont(struct mac_session *s) {
129     Str255 macfont;
130     FontInfo fi;
131  
132     SetPort(s->window);
133     macfont[0] = sprintf((char *)&macfont[1], "%s", cfg.font);
134     GetFNum(macfont, &s->fontnum);
135     TextFont(s->fontnum);
136     TextFace(cfg.fontisbold ? bold : 0);
137     TextSize(cfg.fontheight);
138     GetFontInfo(&fi);
139     font_width = fi.widMax;
140     font_height = fi.ascent + fi.descent + fi.leading;
141     s->font_ascent = fi.ascent;
142     mac_adjustsize(s);
143 }
144
145 /*
146  * To be called whenever the window size changes.
147  * rows and cols should be desired values.
148  * It's assumed the terminal emulator will be or has been informed.
149  */
150 static void mac_adjustsize(struct mac_session *s) {
151     int winwidth, winheight;
152
153     winwidth = cols * font_width + 15;
154     winheight = rows * font_height;
155     SizeWindow(s->window, winwidth, winheight, true);
156     HideControl(s->scrollbar);
157     MoveControl(s->scrollbar, winwidth - 15, -1);
158     SizeControl(s->scrollbar, 16, winheight - 13);
159     ShowControl(s->scrollbar);
160 }
161
162 static void mac_initpalette(struct mac_session *s) {
163     WinCTab ct;
164   
165     if (mac_gestalts.qdvers == gestaltOriginalQD)
166         return;
167     s->palette = NewPalette((*cfg.colours)->pmEntries, NULL, pmCourteous, 0);
168     if (s->palette == NULL)
169         fatalbox("Unable to create palette");
170     CopyPalette(cfg.colours, s->palette, 0, 0, (*cfg.colours)->pmEntries);
171 }
172
173 /*
174  * I don't think this is (a) safe or (b) a good way to do this.
175  */
176 static void mac_updatewinbg(struct mac_session *s) {
177     WinCTab ct;
178     WCTabPtr ctp = &ct;
179     WCTabHandle cth = &ctp;
180
181     ct.wCSeed = 0;
182     ct.wCReserved = 0;
183     ct.ctSize = 1;
184     ct.ctTable[0].value = wContentColor;
185     ct.ctTable[0].rgb = (*s->palette)->pmInfo[16].ciRGB;
186     SetWinColor(s->window, cth);
187 }
188
189 void mac_clickterm(WindowPtr window, EventRecord *event) {
190     struct mac_session *s;
191     Point mouse;
192     ControlHandle control;
193     int part;
194
195     s = (struct mac_session *)GetWRefCon(window);
196     SetPort(window);
197     mouse = event->where;
198     GlobalToLocal(&mouse);
199     part = FindControl(mouse, window, &control);
200     if (control == s->scrollbar) {
201         switch (part) {
202           case kControlIndicatorPart:
203             if (TrackControl(control, mouse, NULL) == kControlIndicatorPart)
204                 term_scroll(+1, GetControlValue(control));
205             break;
206           case kControlUpButtonPart:
207           case kControlDownButtonPart:
208           case kControlPageUpPart:
209           case kControlPageDownPart:
210             TrackControl(control, mouse, mac_scrolltracker);
211             break;
212         }
213     }
214 }
215
216 static pascal void mac_scrolltracker(ControlHandle control, short part) {
217     struct mac_session *s;
218
219     s = (struct mac_session *)GetWRefCon((*control)->contrlOwner);
220     switch (part) {
221       case kControlUpButtonPart:
222         term_scroll(0, -1);
223         break;
224       case kControlDownButtonPart:
225         term_scroll(0, +1);
226         break;
227       case kControlPageUpPart:
228         term_scroll(0, -(rows - 1));
229         break;
230       case kControlPageDownPart:
231         term_scroll(0, +(rows - 1));
232         break;
233     }
234 }
235
236 void mac_activateterm(WindowPtr window, Boolean active) {
237     struct mac_session *s;
238
239     s = (struct mac_session *)GetWRefCon(window);
240     if (active)
241         ShowControl(s->scrollbar);
242     else
243         HideControl(s->scrollbar);
244 }
245
246 void mac_updateterm(WindowPtr window) {
247     struct mac_session *s;
248     Rect clip;
249
250     s = (struct mac_session *)GetWRefCon(window);
251     BeginUpdate(window);
252     term_paint(s,
253                (*window->visRgn)->rgnBBox.left,
254                (*window->visRgn)->rgnBBox.top,
255                (*window->visRgn)->rgnBBox.right,
256                (*window->visRgn)->rgnBBox.bottom);
257     /* Restore default colours in case the Window Manager uses them */
258     PmForeColor(16);
259     PmBackColor(18);
260     if (FrontWindow() != window)
261         EraseRect(&(*s->scrollbar)->contrlRect);
262     UpdateControls(window, window->visRgn);
263     /* Stop DrawGrowIcon giving us space for a horizontal scrollbar */
264     clip.left = window->portRect.right - 15;
265     clip.right = SHRT_MAX;
266     clip.top = SHRT_MIN;
267     clip.bottom = SHRT_MAX;
268     ClipRect(&clip);
269     DrawGrowIcon(window);
270     clip.left = SHRT_MIN;
271     ClipRect(&clip);
272     EndUpdate(window);
273 }
274
275 struct do_text_args {
276     struct mac_session *s;
277     Rect textrect;
278     char *text;
279     int len;
280     unsigned long attr;
281 };
282
283 /*
284  * Call from the terminal emulator to draw a bit of text
285  *
286  * x and y are text row and column (zero-based)
287  */
288 void do_text(struct mac_session *s, int x, int y, char *text, int len,
289              unsigned long attr) {
290     int style = 0;
291     int bgcolour, fgcolour;
292     RGBColor rgbfore, rgbback;
293     struct do_text_args a;
294     RgnHandle textrgn;
295
296     SetPort(s->window);
297     
298     /* First check this text is relevant */
299     a.textrect.top = y * font_height;
300     a.textrect.bottom = (y + 1) * font_height;
301     a.textrect.left = x * font_width;
302     a.textrect.right = (x + len) * font_width;
303     if (!RectInRgn(&a.textrect, s->window->visRgn))
304         return;
305
306     a.s = s;
307     a.text = text;
308     a.len = len;
309     a.attr = attr;
310     SetPort(s->window);
311     TextFont(s->fontnum);
312     if (cfg.fontisbold || (attr & ATTR_BOLD) && !cfg.bold_colour)
313         style |= bold;
314     if (attr & ATTR_UNDER)
315         style |= underline;
316     TextFace(style);
317     TextSize(cfg.fontheight);
318     if (attr & ATTR_REVERSE)
319         TextMode(notSrcCopy);
320     else
321         TextMode(srcCopy);
322     SetFractEnable(FALSE); /* We want characters on pixel boundaries */
323     textrgn = NewRgn();
324     RectRgn(textrgn, &a.textrect);
325     DeviceLoop(textrgn, do_text_for_device, (long)&a, 0);
326     /* Tell the window manager about it in case this isn't an update */
327     DisposeRgn(textrgn);
328     ValidRect(&a.textrect);
329 }
330
331 static pascal void do_text_for_device(short depth, short devflags,
332                                       GDHandle device, long cookie) {
333     struct do_text_args *a;
334     int bgcolour, fgcolour;
335
336     a = (struct do_text_args *)cookie;
337
338     switch (depth) {
339       case 1:
340         /* XXX This should be done with a _little_ more configurability */
341         ForeColor(whiteColor);
342         BackColor(blackColor);
343         break;
344       default:
345         fgcolour = ((a->attr & ATTR_FGMASK) >> ATTR_FGSHIFT) * 2;
346         bgcolour = ((a->attr & ATTR_BGMASK) >> ATTR_BGSHIFT) * 2;
347         if ((a->attr & ATTR_BOLD) && cfg.bold_colour)
348             if (a->attr & ATTR_REVERSE)
349                 bgcolour++;
350             else
351                 fgcolour++;
352         PmForeColor(fgcolour);
353         PmBackColor(bgcolour);
354         break;
355     }
356     MoveTo(a->textrect.left, a->textrect.top + a->s->font_ascent);
357     DrawText(a->text, 0, a->len);
358 }
359
360 /*
361  * Call from the terminal emulator to get its graphics context.
362  */
363 struct mac_session *get_ctx(void) {
364
365     return onlysession;
366 }
367
368 /*
369  * Presumably this does something in Windows
370  */
371 void free_ctx(struct mac_session *ctx) {
372
373 }
374
375 /*
376  * Set the scroll bar position
377  *
378  * total is the line number of the bottom of the working screen
379  * start is the line number of the top of the display
380  * page is the length of the displayed page
381  */
382 void set_sbar(int total, int start, int page) {
383     struct mac_session *s = onlysession;
384
385     /* We don't redraw until we've set everything up, to avoid glitches */
386     (*s->scrollbar)->contrlMin = 0;
387     (*s->scrollbar)->contrlMax = total - page;
388     SetControlValue(s->scrollbar, start);
389 #if 0
390     /* XXX: This doesn't link for me. */
391     if (mac_gestalts.cntlattr & gestaltControlMgrPresent)
392         SetControlViewSize(s->scrollbar, page);
393 #endif
394 }
395
396 /*
397  * Beep
398  */
399 void beep(void) {
400
401     SysBeep(30);
402     /*
403      * XXX We should indicate the relevant window and/or use the
404      * Notification Manager
405      */
406 }
407
408 /*
409  * Set icon string -- a no-op here (Windowshade?)
410  */
411 void set_icon(char *icon) {
412
413 }
414
415 /*
416  * Set the window title
417  */
418 void set_title(char *title) {
419     Str255 mactitle;
420     struct mac_session *s = onlysession;
421
422     mactitle[0] = sprintf((char *)&mactitle[1], "%s", title);
423     SetWTitle(s->window, mactitle);
424 }
425
426 /*
427  * Resize the window at the emulator's request
428  */
429 void request_resize(int w, int h) {
430
431     cols = w;
432     rows = h;
433     mac_initfont(onlysession);
434 }
435
436 /*
437  * Set the logical palette
438  */
439 void palette_set(int n, int r, int g, int b) {
440     RGBColor col;
441     struct mac_session *s = onlysession;
442     static const int first[21] = {
443         0, 2, 4, 6, 8, 10, 12, 14,
444         1, 3, 5, 7, 9, 11, 13, 15,
445         16, 17, 18, 20, 22
446     };
447     
448     if (mac_gestalts.qdvers == gestaltOriginalQD)
449       return;
450     col.red   = r * 0x0101;
451     col.green = g * 0x0101;
452     col.blue  = b * 0x0101;
453     SetEntryColor(s->palette, first[n], &col);
454     if (first[n] >= 18)
455         SetEntryColor(s->palette, first[n]+1, &col);
456     ActivatePalette(s->window);
457 }
458
459 /*
460  * Reset to the default palette
461  */
462 void palette_reset(void) {
463     struct mac_session *s = onlysession;
464
465     if (mac_gestalts.qdvers == gestaltOriginalQD)
466         return;
467     CopyPalette(cfg.colours, s->palette, 0, 0, (*cfg.colours)->pmEntries);
468     ActivatePalette(s->window);
469     /* Palette Manager will generate update events as required. */
470 }
471
472 /*
473  * Move `lines' lines from position `from' to position `to' in the
474  * window.
475  * Note that this is currently broken if "from" and "to" are more
476  * than "lines" lines apart.
477  */
478 void optimised_move(int to, int from, int lines) {
479     Rect r;
480     RgnHandle update;
481     struct mac_session *s = onlysession;
482     int min, max, d;
483
484     SetPort(s->window);
485
486     min = (to < from ? to : from);
487     max = to + from - min;
488     d = max - min;
489
490     update = NewRgn();
491     r.left = 0; r.right = cols * font_width;
492     r.top = min * font_height; r.bottom = (max+lines) * font_height;
493     ScrollRect(&r, 0, (to - from) * font_height, update);
494     InvalRgn(update); /* XXX: necessary?  probably harmless anyway */
495     DisposeRgn(update);
496 }
497
498
499 /*
500  * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
501  * for backward.)
502  */
503 void do_scroll(int topline, int botline, int lines) {
504     struct mac_session *s = onlysession;
505     Rect r;
506     RgnHandle update;
507
508     SetPort(s->window);
509     update = NewRgn();
510     SetRect(&r, 0, topline * font_height,
511             cols * font_width, (botline + 1) * font_height);
512     ScrollRect(&r, 0, - lines * font_height, update);
513     /* XXX: move update region? */
514     InvalRgn(update);
515     DisposeRgn(update);
516 }