1 /* $Id: macterm.c,v 1.1.2.14 1999/03/03 22:03:54 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>
42 #include <ToolUtils.h>
52 #define DEFAULT_FG_BOLD 17
54 #define DEFAULT_BG_BOLD 19
60 PaletteHandle palette;
61 ControlHandle scrollbar;
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);
71 * Temporary hack till I get the terminal emulator supporting multiple
75 static struct mac_session *onlysession;
77 static void inbuf_putc(int c) {
78 inbuf[inbuf_head] = c;
79 inbuf_head = (inbuf_head+1) & INBUF_MASK;
82 static void inbuf_putstr(const char *c) {
87 static void display_resource(unsigned long type, short id) {
92 h = GetResource(type, id);
94 fatalbox("Can't get test resource");
95 SetResAttrs(h, GetResAttrs(h) | resLocked);
97 len = GetResourceSizeOnDisk(h);
98 for (i = 0; i < len; i++) {
102 SetResAttrs(h, GetResAttrs(h) & ~resLocked);
107 void mac_newsession(void) {
108 struct mac_session *s;
111 /* This should obviously be initialised by other means */
112 mac_loadconfig(&cfg);
113 s = smalloc(sizeof(*s));
116 /* XXX: Own storage management? */
117 if (mac_gestalts.qdvers == gestaltOriginalQD)
118 s->window = GetNewWindow(wTerminal, NULL, (WindowPtr)-1);
120 s->window = GetNewCWindow(wTerminal, NULL, (WindowPtr)-1);
121 SetWRefCon(s->window, (long)s);
122 s->scrollbar = GetNewControl(cVScroll, s->window);
124 term_size(cfg.height, cfg.width, cfg.savelines);
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);
134 static void mac_initfont(struct mac_session *s) {
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);
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);
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
157 static void mac_adjustsize(struct mac_session *s, int newrows, int newcols) {
158 int winwidth, winheight;
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);
169 static void mac_initpalette(struct mac_session *s) {
172 if (mac_gestalts.qdvers == gestaltOriginalQD)
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);
181 * I don't think this is (a) safe or (b) a good way to do this.
183 static void mac_updatewinbg(struct mac_session *s) {
186 WCTabHandle cth = &ctp;
191 ct.ctTable[0].value = wContentColor;
192 ct.ctTable[0].rgb = (*s->palette)->pmInfo[16].ciRGB;
193 SetWinColor(s->window, cth);
196 void mac_clickterm(WindowPtr window, EventRecord *event) {
197 struct mac_session *s;
199 ControlHandle control;
202 s = (struct mac_session *)GetWRefCon(window);
204 mouse = event->where;
205 GlobalToLocal(&mouse);
206 part = FindControl(mouse, window, &control);
207 if (control == s->scrollbar) {
209 case kControlIndicatorPart:
210 if (TrackControl(control, mouse, NULL) == kControlIndicatorPart)
211 term_scroll(+1, GetControlValue(control));
213 case kControlUpButtonPart:
214 case kControlDownButtonPart:
215 case kControlPageUpPart:
216 case kControlPageDownPart:
217 TrackControl(control, mouse, mac_scrolltracker);
223 static pascal void mac_scrolltracker(ControlHandle control, short part) {
224 struct mac_session *s;
226 s = (struct mac_session *)GetWRefCon((*control)->contrlOwner);
228 case kControlUpButtonPart:
231 case kControlDownButtonPart:
234 case kControlPageUpPart:
235 term_scroll(0, -(rows - 1));
237 case kControlPageDownPart:
238 term_scroll(0, +(rows - 1));
243 void mac_growterm(WindowPtr window, EventRecord *event) {
246 int newrows, newcols;
247 struct mac_session *s;
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);
260 void mac_activateterm(WindowPtr window, Boolean active) {
261 struct mac_session *s;
263 s = (struct mac_session *)GetWRefCon(window);
265 ShowControl(s->scrollbar);
267 HideControl(s->scrollbar);
270 void mac_updateterm(WindowPtr window) {
271 struct mac_session *s;
274 s = (struct mac_session *)GetWRefCon(window);
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 */
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);
290 DrawGrowIcon(window);
291 clip.left = SHRT_MIN;
296 struct do_text_args {
297 struct mac_session *s;
305 * Call from the terminal emulator to draw a bit of text
307 * x and y are text row and column (zero-based)
309 void do_text(struct mac_session *s, int x, int y, char *text, int len,
310 unsigned long attr) {
312 int bgcolour, fgcolour;
313 RGBColor rgbfore, rgbback;
314 struct do_text_args a;
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))
332 TextFont(s->fontnum);
333 if (cfg.fontisbold || (attr & ATTR_BOLD) && !cfg.bold_colour)
335 if (attr & ATTR_UNDER)
338 TextSize(cfg.fontheight);
339 if (attr & ATTR_REVERSE)
340 TextMode(notSrcCopy);
343 SetFractEnable(FALSE); /* We want characters on pixel boundaries */
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 */
349 ValidRect(&a.textrect);
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;
357 a = (struct do_text_args *)cookie;
361 /* XXX This should be done with a _little_ more configurability */
362 ForeColor(whiteColor);
363 BackColor(blackColor);
366 if ((a->attr & ATTR_BOLD) && cfg.bold_colour)
367 PmForeColor(DEFAULT_FG_BOLD);
369 PmForeColor(DEFAULT_FG);
370 PmBackColor(DEFAULT_BG);
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)
380 PmForeColor(fgcolour);
381 PmBackColor(bgcolour);
384 MoveTo(a->textrect.left, a->textrect.top + a->s->font_ascent);
385 DrawText(a->text, 0, a->len);
389 * Call from the terminal emulator to get its graphics context.
391 struct mac_session *get_ctx(void) {
397 * Presumably this does something in Windows
399 void free_ctx(struct mac_session *ctx) {
404 * Set the scroll bar position
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
410 void set_sbar(int total, int start, int page) {
411 struct mac_session *s = onlysession;
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);
418 /* XXX: This doesn't link for me. */
419 if (mac_gestalts.cntlattr & gestaltControlMgrPresent)
420 SetControlViewSize(s->scrollbar, page);
431 * XXX We should indicate the relevant window and/or use the
432 * Notification Manager
437 * Set icon string -- a no-op here (Windowshade?)
439 void set_icon(char *icon) {
444 * Set the window title
446 void set_title(char *title) {
448 struct mac_session *s = onlysession;
450 mactitle[0] = sprintf((char *)&mactitle[1], "%s", title);
451 SetWTitle(s->window, mactitle);
455 * Resize the window at the emulator's request
457 void request_resize(int w, int h) {
461 mac_initfont(onlysession);
465 * Set the logical palette
467 void palette_set(int n, int r, int g, int b) {
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,
476 if (mac_gestalts.qdvers == gestaltOriginalQD)
478 col.red = r * 0x0101;
479 col.green = g * 0x0101;
480 col.blue = b * 0x0101;
481 SetEntryColor(s->palette, first[n], &col);
483 SetEntryColor(s->palette, first[n]+1, &col);
484 ActivatePalette(s->window);
488 * Reset to the default palette
490 void palette_reset(void) {
491 struct mac_session *s = onlysession;
493 if (mac_gestalts.qdvers == gestaltOriginalQD)
495 CopyPalette(cfg.colours, s->palette, 0, 0, (*cfg.colours)->pmEntries);
496 ActivatePalette(s->window);
497 /* Palette Manager will generate update events as required. */
501 * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
504 void do_scroll(int topline, int botline, int lines) {
505 struct mac_session *s = onlysession;
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? */