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