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