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