1 /* $Id: macterm.c,v 1.1.2.13 1999/03/02 23:19:20 ben Exp $ */
3 * Copyright (c) 1999 Ben Harris
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
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
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
29 * macterm.c -- Macintosh terminal front-end
36 #include <MacWindows.h>
38 #include <Quickdraw.h>
39 #include <QuickdrawText.h>
40 #include <Resources.h>
51 #define DEFAULT_FG_BOLD 17
53 #define DEFAULT_BG_BOLD 19
59 PaletteHandle palette;
60 ControlHandle scrollbar;
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);
70 * Temporary hack till I get the terminal emulator supporting multiple
74 static struct mac_session *onlysession;
76 static void inbuf_putc(int c) {
77 inbuf[inbuf_head] = c;
78 inbuf_head = (inbuf_head+1) & INBUF_MASK;
81 static void inbuf_putstr(const char *c) {
86 static void display_resource(unsigned long type, short id) {
91 h = GetResource(type, id);
93 fatalbox("Can't get test resource");
94 SetResAttrs(h, GetResAttrs(h) | resLocked);
96 len = GetResourceSizeOnDisk(h);
97 for (i = 0; i < len; i++) {
101 SetResAttrs(h, GetResAttrs(h) & ~resLocked);
106 void mac_newsession(void) {
107 struct mac_session *s;
110 /* This should obviously be initialised by other means */
111 mac_loadconfig(&cfg);
112 s = smalloc(sizeof(*s));
115 /* XXX: Own storage management? */
116 if (mac_gestalts.qdvers == gestaltOriginalQD)
117 s->window = GetNewWindow(wTerminal, NULL, (WindowPtr)-1);
119 s->window = GetNewCWindow(wTerminal, NULL, (WindowPtr)-1);
120 SetWRefCon(s->window, (long)s);
121 s->scrollbar = GetNewControl(cVScroll, s->window);
123 term_size(cfg.height, cfg.width, cfg.savelines);
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);
133 static void mac_initfont(struct mac_session *s) {
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);
144 font_width = fi.widMax;
145 font_height = fi.ascent + fi.descent + fi.leading;
146 s->font_ascent = fi.ascent;
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.
155 static void mac_adjustsize(struct mac_session *s) {
156 int winwidth, winheight;
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);
167 static void mac_initpalette(struct mac_session *s) {
170 if (mac_gestalts.qdvers == gestaltOriginalQD)
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);
179 * I don't think this is (a) safe or (b) a good way to do this.
181 static void mac_updatewinbg(struct mac_session *s) {
184 WCTabHandle cth = &ctp;
189 ct.ctTable[0].value = wContentColor;
190 ct.ctTable[0].rgb = (*s->palette)->pmInfo[16].ciRGB;
191 SetWinColor(s->window, cth);
194 void mac_clickterm(WindowPtr window, EventRecord *event) {
195 struct mac_session *s;
197 ControlHandle control;
200 s = (struct mac_session *)GetWRefCon(window);
202 mouse = event->where;
203 GlobalToLocal(&mouse);
204 part = FindControl(mouse, window, &control);
205 if (control == s->scrollbar) {
207 case kControlIndicatorPart:
208 if (TrackControl(control, mouse, NULL) == kControlIndicatorPart)
209 term_scroll(+1, GetControlValue(control));
211 case kControlUpButtonPart:
212 case kControlDownButtonPart:
213 case kControlPageUpPart:
214 case kControlPageDownPart:
215 TrackControl(control, mouse, mac_scrolltracker);
221 static pascal void mac_scrolltracker(ControlHandle control, short part) {
222 struct mac_session *s;
224 s = (struct mac_session *)GetWRefCon((*control)->contrlOwner);
226 case kControlUpButtonPart:
229 case kControlDownButtonPart:
232 case kControlPageUpPart:
233 term_scroll(0, -(rows - 1));
235 case kControlPageDownPart:
236 term_scroll(0, +(rows - 1));
241 void mac_activateterm(WindowPtr window, Boolean active) {
242 struct mac_session *s;
244 s = (struct mac_session *)GetWRefCon(window);
246 ShowControl(s->scrollbar);
248 HideControl(s->scrollbar);
251 void mac_updateterm(WindowPtr window) {
252 struct mac_session *s;
255 s = (struct mac_session *)GetWRefCon(window);
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 */
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);
271 DrawGrowIcon(window);
272 clip.left = SHRT_MIN;
277 struct do_text_args {
278 struct mac_session *s;
286 * Call from the terminal emulator to draw a bit of text
288 * x and y are text row and column (zero-based)
290 void do_text(struct mac_session *s, int x, int y, char *text, int len,
291 unsigned long attr) {
293 int bgcolour, fgcolour;
294 RGBColor rgbfore, rgbback;
295 struct do_text_args a;
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))
313 TextFont(s->fontnum);
314 if (cfg.fontisbold || (attr & ATTR_BOLD) && !cfg.bold_colour)
316 if (attr & ATTR_UNDER)
319 TextSize(cfg.fontheight);
320 if (attr & ATTR_REVERSE)
321 TextMode(notSrcCopy);
324 SetFractEnable(FALSE); /* We want characters on pixel boundaries */
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 */
330 ValidRect(&a.textrect);
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;
338 a = (struct do_text_args *)cookie;
342 /* XXX This should be done with a _little_ more configurability */
343 ForeColor(whiteColor);
344 BackColor(blackColor);
347 if ((a->attr & ATTR_BOLD) && cfg.bold_colour)
348 PmForeColor(DEFAULT_FG_BOLD);
350 PmForeColor(DEFAULT_FG);
351 PmBackColor(DEFAULT_BG);
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)
361 PmForeColor(fgcolour);
362 PmBackColor(bgcolour);
365 MoveTo(a->textrect.left, a->textrect.top + a->s->font_ascent);
366 DrawText(a->text, 0, a->len);
370 * Call from the terminal emulator to get its graphics context.
372 struct mac_session *get_ctx(void) {
378 * Presumably this does something in Windows
380 void free_ctx(struct mac_session *ctx) {
385 * Set the scroll bar position
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
391 void set_sbar(int total, int start, int page) {
392 struct mac_session *s = onlysession;
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);
399 /* XXX: This doesn't link for me. */
400 if (mac_gestalts.cntlattr & gestaltControlMgrPresent)
401 SetControlViewSize(s->scrollbar, page);
412 * XXX We should indicate the relevant window and/or use the
413 * Notification Manager
418 * Set icon string -- a no-op here (Windowshade?)
420 void set_icon(char *icon) {
425 * Set the window title
427 void set_title(char *title) {
429 struct mac_session *s = onlysession;
431 mactitle[0] = sprintf((char *)&mactitle[1], "%s", title);
432 SetWTitle(s->window, mactitle);
436 * Resize the window at the emulator's request
438 void request_resize(int w, int h) {
442 mac_initfont(onlysession);
446 * Set the logical palette
448 void palette_set(int n, int r, int g, int b) {
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,
457 if (mac_gestalts.qdvers == gestaltOriginalQD)
459 col.red = r * 0x0101;
460 col.green = g * 0x0101;
461 col.blue = b * 0x0101;
462 SetEntryColor(s->palette, first[n], &col);
464 SetEntryColor(s->palette, first[n]+1, &col);
465 ActivatePalette(s->window);
469 * Reset to the default palette
471 void palette_reset(void) {
472 struct mac_session *s = onlysession;
474 if (mac_gestalts.qdvers == gestaltOriginalQD)
476 CopyPalette(cfg.colours, s->palette, 0, 0, (*cfg.colours)->pmEntries);
477 ActivatePalette(s->window);
478 /* Palette Manager will generate update events as required. */
482 * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
485 void do_scroll(int topline, int botline, int lines) {
486 struct mac_session *s = onlysession;
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? */