1 /* $Id: macterm.c,v 1.1.2.12 1999/03/02 21:51:55 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>
54 PaletteHandle palette;
55 ControlHandle scrollbar;
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 static pascal void do_text_for_device(short, short, GDHandle, long);
65 * Temporary hack till I get the terminal emulator supporting multiple
69 static struct mac_session *onlysession;
71 static void inbuf_putc(int c) {
72 inbuf[inbuf_head] = c;
73 inbuf_head = (inbuf_head+1) & INBUF_MASK;
76 static void inbuf_putstr(const char *c) {
81 static void display_resource(unsigned long type, short id) {
86 h = GetResource(type, id);
88 fatalbox("Can't get test resource");
89 SetResAttrs(h, GetResAttrs(h) | resLocked);
91 len = GetResourceSizeOnDisk(h);
92 for (i = 0; i < len; i++) {
96 SetResAttrs(h, GetResAttrs(h) & ~resLocked);
101 void mac_newsession(void) {
102 struct mac_session *s;
105 /* This should obviously be initialised by other means */
106 mac_loadconfig(&cfg);
107 s = smalloc(sizeof(*s));
110 /* XXX: Own storage management? */
111 if (mac_gestalts.qdvers == gestaltOriginalQD)
112 s->window = GetNewWindow(wTerminal, NULL, (WindowPtr)-1);
114 s->window = GetNewCWindow(wTerminal, NULL, (WindowPtr)-1);
115 SetWRefCon(s->window, (long)s);
116 s->scrollbar = GetNewControl(cVScroll, s->window);
118 term_size(cfg.height, cfg.width, cfg.savelines);
121 /* Set to FALSE to not get palette updates in the background. */
122 SetPalette(s->window, s->palette, TRUE);
123 ActivatePalette(s->window);
124 ShowWindow(s->window);
125 display_resource('pTST', 128);
128 static void mac_initfont(struct mac_session *s) {
133 macfont[0] = sprintf((char *)&macfont[1], "%s", cfg.font);
134 GetFNum(macfont, &s->fontnum);
135 TextFont(s->fontnum);
136 TextFace(cfg.fontisbold ? bold : 0);
137 TextSize(cfg.fontheight);
139 font_width = fi.widMax;
140 font_height = fi.ascent + fi.descent + fi.leading;
141 s->font_ascent = fi.ascent;
146 * To be called whenever the window size changes.
147 * rows and cols should be desired values.
148 * It's assumed the terminal emulator will be or has been informed.
150 static void mac_adjustsize(struct mac_session *s) {
151 int winwidth, winheight;
153 winwidth = cols * font_width + 15;
154 winheight = rows * font_height;
155 SizeWindow(s->window, winwidth, winheight, true);
156 HideControl(s->scrollbar);
157 MoveControl(s->scrollbar, winwidth - 15, -1);
158 SizeControl(s->scrollbar, 16, winheight - 13);
159 ShowControl(s->scrollbar);
162 static void mac_initpalette(struct mac_session *s) {
165 if (mac_gestalts.qdvers == gestaltOriginalQD)
167 s->palette = NewPalette((*cfg.colours)->pmEntries, NULL, pmCourteous, 0);
168 if (s->palette == NULL)
169 fatalbox("Unable to create palette");
170 CopyPalette(cfg.colours, s->palette, 0, 0, (*cfg.colours)->pmEntries);
174 * I don't think this is (a) safe or (b) a good way to do this.
176 static void mac_updatewinbg(struct mac_session *s) {
179 WCTabHandle cth = &ctp;
184 ct.ctTable[0].value = wContentColor;
185 ct.ctTable[0].rgb = (*s->palette)->pmInfo[16].ciRGB;
186 SetWinColor(s->window, cth);
189 void mac_clickterm(WindowPtr window, EventRecord *event) {
190 struct mac_session *s;
192 ControlHandle control;
195 s = (struct mac_session *)GetWRefCon(window);
197 mouse = event->where;
198 GlobalToLocal(&mouse);
199 part = FindControl(mouse, window, &control);
200 if (control == s->scrollbar) {
202 case kControlIndicatorPart:
203 if (TrackControl(control, mouse, NULL) == kControlIndicatorPart)
204 term_scroll(+1, GetControlValue(control));
206 case kControlUpButtonPart:
207 case kControlDownButtonPart:
208 case kControlPageUpPart:
209 case kControlPageDownPart:
210 TrackControl(control, mouse, mac_scrolltracker);
216 static pascal void mac_scrolltracker(ControlHandle control, short part) {
217 struct mac_session *s;
219 s = (struct mac_session *)GetWRefCon((*control)->contrlOwner);
221 case kControlUpButtonPart:
224 case kControlDownButtonPart:
227 case kControlPageUpPart:
228 term_scroll(0, -(rows - 1));
230 case kControlPageDownPart:
231 term_scroll(0, +(rows - 1));
236 void mac_activateterm(WindowPtr window, Boolean active) {
237 struct mac_session *s;
239 s = (struct mac_session *)GetWRefCon(window);
241 ShowControl(s->scrollbar);
243 HideControl(s->scrollbar);
246 void mac_updateterm(WindowPtr window) {
247 struct mac_session *s;
250 s = (struct mac_session *)GetWRefCon(window);
253 (*window->visRgn)->rgnBBox.left,
254 (*window->visRgn)->rgnBBox.top,
255 (*window->visRgn)->rgnBBox.right,
256 (*window->visRgn)->rgnBBox.bottom);
257 /* Restore default colours in case the Window Manager uses them */
260 if (FrontWindow() != window)
261 EraseRect(&(*s->scrollbar)->contrlRect);
262 UpdateControls(window, window->visRgn);
263 /* Stop DrawGrowIcon giving us space for a horizontal scrollbar */
264 clip.left = window->portRect.right - 15;
265 clip.right = SHRT_MAX;
267 clip.bottom = SHRT_MAX;
269 DrawGrowIcon(window);
270 clip.left = SHRT_MIN;
275 struct do_text_args {
276 struct mac_session *s;
284 * Call from the terminal emulator to draw a bit of text
286 * x and y are text row and column (zero-based)
288 void do_text(struct mac_session *s, int x, int y, char *text, int len,
289 unsigned long attr) {
291 int bgcolour, fgcolour;
292 RGBColor rgbfore, rgbback;
293 struct do_text_args a;
298 /* First check this text is relevant */
299 a.textrect.top = y * font_height;
300 a.textrect.bottom = (y + 1) * font_height;
301 a.textrect.left = x * font_width;
302 a.textrect.right = (x + len) * font_width;
303 if (!RectInRgn(&a.textrect, s->window->visRgn))
311 TextFont(s->fontnum);
312 if (cfg.fontisbold || (attr & ATTR_BOLD) && !cfg.bold_colour)
314 if (attr & ATTR_UNDER)
317 TextSize(cfg.fontheight);
318 if (attr & ATTR_REVERSE)
319 TextMode(notSrcCopy);
322 SetFractEnable(FALSE); /* We want characters on pixel boundaries */
324 RectRgn(textrgn, &a.textrect);
325 DeviceLoop(textrgn, do_text_for_device, (long)&a, 0);
326 /* Tell the window manager about it in case this isn't an update */
328 ValidRect(&a.textrect);
331 static pascal void do_text_for_device(short depth, short devflags,
332 GDHandle device, long cookie) {
333 struct do_text_args *a;
334 int bgcolour, fgcolour;
336 a = (struct do_text_args *)cookie;
340 /* XXX This should be done with a _little_ more configurability */
341 ForeColor(whiteColor);
342 BackColor(blackColor);
345 fgcolour = ((a->attr & ATTR_FGMASK) >> ATTR_FGSHIFT) * 2;
346 bgcolour = ((a->attr & ATTR_BGMASK) >> ATTR_BGSHIFT) * 2;
347 if ((a->attr & ATTR_BOLD) && cfg.bold_colour)
348 if (a->attr & ATTR_REVERSE)
352 PmForeColor(fgcolour);
353 PmBackColor(bgcolour);
356 MoveTo(a->textrect.left, a->textrect.top + a->s->font_ascent);
357 DrawText(a->text, 0, a->len);
361 * Call from the terminal emulator to get its graphics context.
363 struct mac_session *get_ctx(void) {
369 * Presumably this does something in Windows
371 void free_ctx(struct mac_session *ctx) {
376 * Set the scroll bar position
378 * total is the line number of the bottom of the working screen
379 * start is the line number of the top of the display
380 * page is the length of the displayed page
382 void set_sbar(int total, int start, int page) {
383 struct mac_session *s = onlysession;
385 /* We don't redraw until we've set everything up, to avoid glitches */
386 (*s->scrollbar)->contrlMin = 0;
387 (*s->scrollbar)->contrlMax = total - page;
388 SetControlValue(s->scrollbar, start);
390 /* XXX: This doesn't link for me. */
391 if (mac_gestalts.cntlattr & gestaltControlMgrPresent)
392 SetControlViewSize(s->scrollbar, page);
403 * XXX We should indicate the relevant window and/or use the
404 * Notification Manager
409 * Set icon string -- a no-op here (Windowshade?)
411 void set_icon(char *icon) {
416 * Set the window title
418 void set_title(char *title) {
420 struct mac_session *s = onlysession;
422 mactitle[0] = sprintf((char *)&mactitle[1], "%s", title);
423 SetWTitle(s->window, mactitle);
427 * Resize the window at the emulator's request
429 void request_resize(int w, int h) {
433 mac_initfont(onlysession);
437 * Set the logical palette
439 void palette_set(int n, int r, int g, int b) {
441 struct mac_session *s = onlysession;
442 static const int first[21] = {
443 0, 2, 4, 6, 8, 10, 12, 14,
444 1, 3, 5, 7, 9, 11, 13, 15,
448 if (mac_gestalts.qdvers == gestaltOriginalQD)
450 col.red = r * 0x0101;
451 col.green = g * 0x0101;
452 col.blue = b * 0x0101;
453 SetEntryColor(s->palette, first[n], &col);
455 SetEntryColor(s->palette, first[n]+1, &col);
456 ActivatePalette(s->window);
460 * Reset to the default palette
462 void palette_reset(void) {
463 struct mac_session *s = onlysession;
465 if (mac_gestalts.qdvers == gestaltOriginalQD)
467 CopyPalette(cfg.colours, s->palette, 0, 0, (*cfg.colours)->pmEntries);
468 ActivatePalette(s->window);
469 /* Palette Manager will generate update events as required. */
473 * Move `lines' lines from position `from' to position `to' in the
475 * Note that this is currently broken if "from" and "to" are more
476 * than "lines" lines apart.
478 void optimised_move(int to, int from, int lines) {
481 struct mac_session *s = onlysession;
486 min = (to < from ? to : from);
487 max = to + from - min;
491 r.left = 0; r.right = cols * font_width;
492 r.top = min * font_height; r.bottom = (max+lines) * font_height;
493 ScrollRect(&r, 0, (to - from) * font_height, update);
494 InvalRgn(update); /* XXX: necessary? probably harmless anyway */
500 * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
503 void do_scroll(int topline, int botline, int lines) {
504 struct mac_session *s = onlysession;
510 SetRect(&r, 0, topline * font_height,
511 cols * font_width, (botline + 1) * font_height);
512 ScrollRect(&r, 0, - lines * font_height, update);
513 /* XXX: move update region? */