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