Fix various breakages my Carbonisation caused in the Classic build, notably
[sgt/putty] / mac / macterm.c
CommitLineData
5a736db6 1/* $Id: macterm.c,v 1.66 2003/02/04 02:03:53 ben Exp $ */
d082ac49 2/*
3 * Copyright (c) 1999 Simon Tatham
4 * Copyright (c) 1999, 2002 Ben Harris
5 * All rights reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person
8 * obtaining a copy of this software and associated documentation
9 * files (the "Software"), to deal in the Software without
10 * restriction, including without limitation the rights to use,
11 * copy, modify, merge, publish, distribute, sublicense, and/or
12 * sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following
14 * conditions:
15 *
16 * The above copyright notice and this permission notice shall be
17 * included in all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
24 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
25 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
27 */
28
29/*
30 * macterm.c -- Macintosh terminal front-end
31 */
32
33#include <MacTypes.h>
34#include <Controls.h>
35#include <ControlDefinitions.h>
2b8dca9e 36#include <FixMath.h>
d082ac49 37#include <Fonts.h>
38#include <Gestalt.h>
64b8ebc6 39#include <LowMem.h>
d082ac49 40#include <MacMemory.h>
41#include <MacWindows.h>
42#include <MixedMode.h>
43#include <Palettes.h>
44#include <Quickdraw.h>
45#include <QuickdrawText.h>
46#include <Resources.h>
47#include <Scrap.h>
48#include <Script.h>
49#include <Sound.h>
8768ce31 50#include <TextCommon.h>
d082ac49 51#include <ToolUtils.h>
8768ce31 52#include <UnicodeConverter.h>
d082ac49 53
54#include <assert.h>
55#include <limits.h>
56#include <stdlib.h>
57#include <stdio.h>
58#include <string.h>
59
60#include "macresid.h"
61#include "putty.h"
801e70ea 62#include "charset.h"
d082ac49 63#include "mac.h"
64#include "terminal.h"
65
66#define NCOLOURS (lenof(((Config *)0)->colours))
67
68#define DEFAULT_FG 16
69#define DEFAULT_FG_BOLD 17
70#define DEFAULT_BG 18
71#define DEFAULT_BG_BOLD 19
72#define CURSOR_FG 20
e0cbe032 73#define CURSOR_BG 21
d082ac49 74
75#define PTOCC(x) ((x) < 0 ? -(-(x - s->font_width - 1) / s->font_width) : \
76 (x) / s->font_width)
77#define PTOCR(y) ((y) < 0 ? -(-(y - s->font_height - 1) / s->font_height) : \
78 (y) / s->font_height)
79
80static void mac_initfont(Session *);
8768ce31 81static pascal OSStatus uni_to_font_fallback(UniChar *, ByteCount, ByteCount *,
82 TextPtr, ByteCount, ByteCount *,
1d4d52ce 83 LogicalAddress,
8768ce31 84 ConstUnicodeMappingPtr);
d082ac49 85static void mac_initpalette(Session *);
86static void mac_adjustwinbg(Session *);
87static void mac_adjustsize(Session *, int, int);
88static void mac_drawgrowicon(Session *s);
64b8ebc6 89static pascal void mac_growtermdraghook(void);
d082ac49 90static pascal void mac_scrolltracker(ControlHandle, short);
91static pascal void do_text_for_device(short, short, GDHandle, long);
d082ac49 92static void text_click(Session *, EventRecord *);
93
94void pre_paint(Session *s);
95void post_paint(Session *s);
96
ce283213 97void mac_startsession(Session *s)
98{
2beb0fb0 99 char *errmsg;
5211281f 100 int i;
ce283213 101
c31f6c61 102 init_ucs(s);
5211281f 103
104 /*
105 * Select protocol. This is farmed out into a table in a
106 * separate file to enable an ssh-free variant.
107 */
108 s->back = NULL;
109 for (i = 0; backends[i].backend != NULL; i++)
110 if (backends[i].protocol == s->cfg.protocol) {
111 s->back = backends[i].backend;
112 break;
113 }
114 if (s->back == NULL)
115 fatalbox("Unsupported protocol number found");
116
d082ac49 117 /* XXX: Own storage management? */
118 if (HAVE_COLOR_QD())
119 s->window = GetNewCWindow(wTerminal, NULL, (WindowPtr)-1);
120 else
121 s->window = GetNewWindow(wTerminal, NULL, (WindowPtr)-1);
122 SetWRefCon(s->window, (long)s);
123 s->scrollbar = GetNewControl(cVScroll, s->window);
c31f6c61 124 s->term = term_init(&s->cfg, &s->ucsdata, s);
d082ac49 125
2beb0fb0 126 mac_initfont(s);
127 mac_initpalette(s);
128 if (HAVE_COLOR_QD()) {
129 /* Set to FALSE to not get palette updates in the background. */
130 SetPalette(s->window, s->palette, TRUE);
131 ActivatePalette(s->window);
132 }
133
2e96d504 134 s->logctx = log_init(s, &s->cfg);
d082ac49 135 term_provide_logctx(s->term, s->logctx);
136
2e96d504 137 errmsg = s->back->init(s->term, &s->backhandle, &s->cfg, s->cfg.host,
138 s->cfg.port, &s->realhost, s->cfg.tcp_nodelay);
5eec331c 139 if (errmsg != NULL)
140 fatalbox("%s", errmsg);
d082ac49 141 s->back->provide_logctx(s->backhandle, s->logctx);
d9fffe93 142 set_title(s, s->realhost);
d082ac49 143
144 term_provide_resize_fn(s->term, s->back->size, s->backhandle);
145
146 mac_adjustsize(s, s->cfg.height, s->cfg.width);
147 term_size(s->term, s->cfg.height, s->cfg.width, s->cfg.savelines);
148
fe5634f6 149 s->ldisc = ldisc_create(&s->cfg, s->term, s->back, s->backhandle, s);
d082ac49 150 ldisc_send(s->ldisc, NULL, 0, 0);/* cause ldisc to notice changes */
151
d082ac49 152 ShowWindow(s->window);
0c4b7799 153 s->next = sesslist;
fabd1805 154 s->prev = &sesslist;
9546c9c6 155 if (s->next != NULL)
b89053c0 156 s->next->prev = &s->next;
0c4b7799 157 sesslist = s;
d082ac49 158}
159
c3aef739 160/*
161 * Try to work out a horizontal scaling factor for the current font
162 * that will give a chracter width of wantwidth. Return it in numer
163 * and denom (suitable for passing to StdText()).
164 */
165static void mac_workoutfontscale(Session *s, int wantwidth,
166 Point *numerp, Point *denomp)
167{
168 Point numer, denom, tmpnumer, tmpdenom;
169 int gotwidth, i;
170 const char text = 'W';
171 FontInfo fi;
1d4d52ce 172#if TARGET_API_MAC_CARBON
173 CQDProcsPtr gp = GetPortGrafProcs(GetWindowPort(s->window));;
174#else
175 QDProcsPtr gp = s->window->grafProcs;;
176#endif
c3aef739 177
c3aef739 178 numer.v = denom.v = 1; /* always */
179 numer.h = denom.h = 1;
180 for (i = 0; i < 3; i++) {
c3aef739 181 tmpnumer = numer;
182 tmpdenom = denom;
1d4d52ce 183 if (gp != NULL)
c3aef739 184 gotwidth = InvokeQDTxMeasUPP(1, &text, &tmpnumer, &tmpdenom, &fi,
1d4d52ce 185 gp->txMeasProc);
c3aef739 186 else
187 gotwidth = StdTxMeas(1, &text, &tmpnumer, &tmpdenom, &fi);
188 /* The result of StdTxMeas must be scaled by the factors it returns. */
189 gotwidth = FixRound(FixMul(gotwidth << 16,
190 FixRatio(tmpnumer.h, tmpdenom.h)));
c3aef739 191 if (gotwidth == wantwidth)
192 break;
193 numer.h *= wantwidth;
194 denom.h *= gotwidth;
195 }
196 *numerp = numer;
197 *denomp = denom;
198}
199
8768ce31 200static UnicodeToTextFallbackUPP uni_to_font_fallback_upp;
201
d082ac49 202static void mac_initfont(Session *s) {
d082ac49 203 FontInfo fi;
8768ce31 204 TextEncoding enc;
379836ca 205 OptionBits fbflags;
8768ce31 206
5a736db6 207 SetPort((GrafPtr)GetWindowPort(s->window));
e3c5b245 208 GetFNum(s->cfg.font.name, &s->fontnum);
d082ac49 209 TextFont(s->fontnum);
e3c5b245 210 TextFace(s->cfg.font.face);
211 TextSize(s->cfg.font.size);
d082ac49 212 GetFontInfo(&fi);
213 s->font_width = CharWidth('W'); /* Well, it's what NCSA uses. */
214 s->font_ascent = fi.ascent;
215 s->font_leading = fi.leading;
216 s->font_height = s->font_ascent + fi.descent + s->font_leading;
c3aef739 217 mac_workoutfontscale(s, s->font_width,
218 &s->font_stdnumer, &s->font_stddenom);
219 mac_workoutfontscale(s, s->font_width * 2,
220 &s->font_widenumer, &s->font_widedenom);
e3c5b245 221 TextSize(s->cfg.font.size * 2);
c3aef739 222 mac_workoutfontscale(s, s->font_width * 2,
223 &s->font_bignumer, &s->font_bigdenom);
e3c5b245 224 TextSize(s->cfg.font.size);
d082ac49 225 if (!s->cfg.bold_colour) {
226 TextFace(bold);
227 s->font_boldadjust = s->font_width - CharWidth('W');
228 } else
229 s->font_boldadjust = 0;
8768ce31 230
231 if (s->uni_to_font != NULL)
232 DisposeUnicodeToTextInfo(&s->uni_to_font);
8ef2b196 233 if (mac_gestalts.encvvers != 0 &&
8768ce31 234 UpgradeScriptInfoToTextEncoding(kTextScriptDontCare,
235 kTextLanguageDontCare,
e3c5b245 236 kTextRegionDontCare, s->cfg.font.name,
8ef2b196 237 &enc) == noErr &&
238 CreateUnicodeToTextInfoByEncoding(enc, &s->uni_to_font) == noErr) {
8768ce31 239 if (uni_to_font_fallback_upp == NULL)
240 uni_to_font_fallback_upp =
1d4d52ce 241 NewUnicodeToTextFallbackUPP(&uni_to_font_fallback);
379836ca 242 fbflags = kUnicodeFallbackCustomOnly;
243 if (mac_gestalts.uncvattr & kTECAddFallbackInterruptMask)
244 fbflags |= kUnicodeFallbackInterruptSafeMask;
8768ce31 245 if (SetFallbackUnicodeToText(s->uni_to_font,
379836ca 246 uni_to_font_fallback_upp, fbflags, NULL) != noErr) {
8768ce31 247 DisposeUnicodeToTextInfo(&s->uni_to_font);
379836ca 248 goto no_encv;
8768ce31 249 }
8ef2b196 250 } else {
e3c5b245 251 char cfontname[256];
252
379836ca 253 no_encv:
8ef2b196 254 s->uni_to_font = NULL;
e3c5b245 255 p2cstrcpy(cfontname, s->cfg.font.name);
8ef2b196 256 s->font_charset =
257 charset_from_macenc(FontToScript(s->fontnum),
258 GetScriptManagerVariable(smRegionCode),
e3c5b245 259 mac_gestalts.sysvers, cfontname);
8768ce31 260 }
261
d082ac49 262 mac_adjustsize(s, s->term->rows, s->term->cols);
263}
264
8768ce31 265static pascal OSStatus uni_to_font_fallback(UniChar *ucp,
266 ByteCount ilen, ByteCount *iusedp, TextPtr obuf, ByteCount olen,
1d4d52ce 267 ByteCount *ousedp, LogicalAddress cookie, ConstUnicodeMappingPtr mapping)
8768ce31 268{
269
270 if (olen < 1)
271 return kTECOutputBufferFullStatus;
d1c57171 272 /*
273 * What I'd _like_ to do here is to somehow generate the
274 * missing-character glyph that every font is required to have.
275 * Unfortunately (and somewhat surprisingly), I can't find any way
276 * to actually ask for it explicitly. Bah.
277 */
278 *obuf = '.';
8768ce31 279 *iusedp = ilen;
280 *ousedp = 1;
281 return noErr;
282}
283
0c4b7799 284/*
285 * Called every time round the event loop.
286 */
287void mac_pollterm(void)
288{
289 Session *s;
290
291 for (s = sesslist; s != NULL; s = s->next) {
292 term_out(s->term);
293 term_update(s->term);
294 }
295}
8768ce31 296
d082ac49 297/*
298 * To be called whenever the window size changes.
299 * rows and cols should be desired values.
300 * It's assumed the terminal emulator will be informed, and will set rows
301 * and cols for us.
302 */
303static void mac_adjustsize(Session *s, int newrows, int newcols) {
304 int winwidth, winheight;
305
306 winwidth = newcols * s->font_width + 15;
307 winheight = newrows * s->font_height;
308 SizeWindow(s->window, winwidth, winheight, true);
309 HideControl(s->scrollbar);
310 MoveControl(s->scrollbar, winwidth - 15, -1);
311 SizeControl(s->scrollbar, 16, winheight - 13);
312 ShowControl(s->scrollbar);
0c367171 313 mac_drawgrowicon(s);
d082ac49 314}
315
316static void mac_initpalette(Session *s) {
d082ac49 317
1eb3f8f0 318 if (!HAVE_COLOR_QD())
319 return;
d082ac49 320 /*
321 * Most colours should be inhibited on 2bpp displays.
322 * Palette manager documentation suggests inhibiting all tolerant colours
323 * on greyscale displays.
324 */
8a00b869 325#define PM_NORMAL ( pmTolerant | pmInhibitC2 | \
326 pmInhibitG2 | pmInhibitG4 | pmInhibitG8 )
d082ac49 327#define PM_TOLERANCE 0x2000
328 s->palette = NewPalette(22, NULL, PM_NORMAL, PM_TOLERANCE);
329 if (s->palette == NULL)
330 fatalbox("Unable to create palette");
331 /* In 2bpp, these are the colours we want most. */
332 SetEntryUsage(s->palette, DEFAULT_BG,
333 PM_NORMAL &~ pmInhibitC2, PM_TOLERANCE);
334 SetEntryUsage(s->palette, DEFAULT_FG,
335 PM_NORMAL &~ pmInhibitC2, PM_TOLERANCE);
336 SetEntryUsage(s->palette, DEFAULT_FG_BOLD,
337 PM_NORMAL &~ pmInhibitC2, PM_TOLERANCE);
8a00b869 338 SetEntryUsage(s->palette, CURSOR_BG,
d082ac49 339 PM_NORMAL &~ pmInhibitC2, PM_TOLERANCE);
340 palette_reset(s);
341}
342
343/*
344 * Set the background colour of the window correctly. Should be
345 * called whenever the default background changes.
346 */
347static void mac_adjustwinbg(Session *s) {
348
349 if (!HAVE_COLOR_QD())
350 return;
1fc898ea 351#if !TARGET_CPU_68K
d082ac49 352 if (mac_gestalts.windattr & gestaltWindowMgrPresent)
353 SetWindowContentColor(s->window,
354 &(*s->palette)->pmInfo[DEFAULT_BG].ciRGB);
355 else
356#endif
357 {
1d4d52ce 358#if !TARGET_API_MAC_CARBON
d082ac49 359 if (s->wctab == NULL)
360 s->wctab = (WCTabHandle)NewHandle(sizeof(**s->wctab));
361 if (s->wctab == NULL)
362 return; /* do without */
363 (*s->wctab)->wCSeed = 0;
364 (*s->wctab)->wCReserved = 0;
365 (*s->wctab)->ctSize = 0;
366 (*s->wctab)->ctTable[0].value = wContentColor;
367 (*s->wctab)->ctTable[0].rgb = (*s->palette)->pmInfo[DEFAULT_BG].ciRGB;
368 SetWinColor(s->window, s->wctab);
1d4d52ce 369#endif
d082ac49 370 }
371}
372
373/*
374 * Set the cursor shape correctly
375 */
376void mac_adjusttermcursor(WindowPtr window, Point mouse, RgnHandle cursrgn) {
377 Session *s;
378 ControlHandle control;
379 short part;
380 int x, y;
1d4d52ce 381#if TARGET_API_MAC_CARBON
382 Cursor arrow;
383 Rect rect;
384 RgnHandle visrgn;
385#endif
d082ac49 386
5a736db6 387 SetPort((GrafPtr)GetWindowPort(window));
d082ac49 388 s = (Session *)GetWRefCon(window);
389 GlobalToLocal(&mouse);
390 part = FindControl(mouse, window, &control);
391 if (control == s->scrollbar) {
1d4d52ce 392#if TARGET_API_MAC_CARBON
393 SetCursor(GetQDGlobalsArrow(&arrow));
394 RectRgn(cursrgn, GetControlBounds(s->scrollbar, &rect));
395#else
d082ac49 396 SetCursor(&qd.arrow);
397 RectRgn(cursrgn, &(*s->scrollbar)->contrlRect);
1d4d52ce 398#endif
d082ac49 399 } else {
400 x = mouse.h / s->font_width;
401 y = mouse.v / s->font_height;
1d4d52ce 402 if (s->raw_mouse) {
403#if TARGET_API_MAC_CARBON
404 SetCursor(GetQDGlobalsArrow(&arrow));
405#else
d082ac49 406 SetCursor(&qd.arrow);
1d4d52ce 407#endif
408 } else
d082ac49 409 SetCursor(*GetCursor(iBeamCursor));
410 /* Ask for shape changes if we leave this character cell. */
411 SetRectRgn(cursrgn, x * s->font_width, y * s->font_height,
412 (x + 1) * s->font_width, (y + 1) * s->font_height);
d082ac49 413 }
1d4d52ce 414#if TARGET_API_MAC_CARBON
415 visrgn = NewRgn();
416 GetPortVisibleRegion(GetWindowPort(window), visrgn);
417 SectRgn(cursrgn, visrgn, cursrgn);
418 DisposeRgn(visrgn);
419#else
420 SectRgn(cursrgn, window->visRgn, cursrgn);
421#endif
d082ac49 422}
423
424/*
425 * Enable/disable menu items based on the active terminal window.
426 */
1d4d52ce 427#if TARGET_API_MAC_CARBON
428#define DisableItem DisableMenuItem
429#define EnableItem EnableMenuItem
430#endif
d082ac49 431void mac_adjusttermmenus(WindowPtr window) {
432 Session *s;
433 MenuHandle menu;
1d4d52ce 434#if !TARGET_API_MAC_CARBON
d082ac49 435 long offset;
1d4d52ce 436#endif
d082ac49 437
438 s = (Session *)GetWRefCon(window);
b537dd42 439 menu = GetMenuHandle(mFile);
440 DisableItem(menu, iSave); /* XXX enable if modified */
441 EnableItem(menu, iSaveAs);
5211281f 442 EnableItem(menu, iDuplicate);
d082ac49 443 menu = GetMenuHandle(mEdit);
444 EnableItem(menu, 0);
445 DisableItem(menu, iUndo);
446 DisableItem(menu, iCut);
447 if (1/*s->term->selstate == SELECTED*/)
448 EnableItem(menu, iCopy);
449 else
450 DisableItem(menu, iCopy);
1d4d52ce 451#if TARGET_API_MAC_CARBON
452 if (1)
453#else
d082ac49 454 if (GetScrap(NULL, 'TEXT', &offset) == noTypeErr)
1d4d52ce 455#endif
d082ac49 456 DisableItem(menu, iPaste);
457 else
458 EnableItem(menu, iPaste);
459 DisableItem(menu, iClear);
460 EnableItem(menu, iSelectAll);
461}
462
463void mac_menuterm(WindowPtr window, short menu, short item) {
464 Session *s;
465
466 s = (Session *)GetWRefCon(window);
467 switch (menu) {
468 case mEdit:
469 switch (item) {
470 case iCopy:
471 /* term_copy(s); */
472 break;
473 case iPaste:
474 term_do_paste(s->term);
475 break;
476 }
477 }
478}
479
480void mac_clickterm(WindowPtr window, EventRecord *event) {
481 Session *s;
482 Point mouse;
483 ControlHandle control;
484 int part;
1d4d52ce 485 static ControlActionUPP mac_scrolltracker_upp = NULL;
d082ac49 486
487 s = (Session *)GetWRefCon(window);
5a736db6 488 SetPort((GrafPtr)GetWindowPort(window));
d082ac49 489 mouse = event->where;
490 GlobalToLocal(&mouse);
491 part = FindControl(mouse, window, &control);
492 if (control == s->scrollbar) {
493 switch (part) {
494 case kControlIndicatorPart:
495 if (TrackControl(control, mouse, NULL) == kControlIndicatorPart)
496 term_scroll(s->term, +1, GetControlValue(control));
497 break;
498 case kControlUpButtonPart:
499 case kControlDownButtonPart:
500 case kControlPageUpPart:
501 case kControlPageDownPart:
1d4d52ce 502 if (mac_scrolltracker_upp == NULL)
503 mac_scrolltracker_upp =
504 NewControlActionUPP(&mac_scrolltracker);
505 TrackControl(control, mouse, mac_scrolltracker_upp);
d082ac49 506 break;
507 }
508 } else {
509 text_click(s, event);
510 }
511}
512
513static void text_click(Session *s, EventRecord *event) {
514 Point localwhere;
515 int row, col;
516 static UInt32 lastwhen = 0;
517 static Session *lastsess = NULL;
518 static int lastrow = -1, lastcol = -1;
519 static Mouse_Action lastact = MA_NOTHING;
520
5a736db6 521 SetPort((GrafPtr)GetWindowPort(s->window));
d082ac49 522 localwhere = event->where;
523 GlobalToLocal(&localwhere);
524
525 col = PTOCC(localwhere.h);
526 row = PTOCR(localwhere.v);
527 if (event->when - lastwhen < GetDblTime() &&
528 row == lastrow && col == lastcol && s == lastsess)
529 lastact = (lastact == MA_CLICK ? MA_2CLK :
530 lastact == MA_2CLK ? MA_3CLK :
531 lastact == MA_3CLK ? MA_CLICK : MA_NOTHING);
532 else
533 lastact = MA_CLICK;
fc5b0934 534 term_mouse(s->term, MBT_LEFT,
535 event->modifiers & shiftKey ? MBT_EXTEND : MBT_SELECT,
d082ac49 536 lastact, col, row, event->modifiers & shiftKey,
537 event->modifiers & controlKey, event->modifiers & optionKey);
538 lastsess = s;
539 lastrow = row;
540 lastcol = col;
541 while (StillDown()) {
542 GetMouse(&localwhere);
543 col = PTOCC(localwhere.h);
544 row = PTOCR(localwhere.v);
fc5b0934 545 term_mouse(s->term, MBT_LEFT,
546 event->modifiers & shiftKey ? MBT_EXTEND : MBT_SELECT,
d082ac49 547 MA_DRAG, col, row, event->modifiers & shiftKey,
548 event->modifiers & controlKey,
549 event->modifiers & optionKey);
550 if (row > s->term->rows - 1)
551 term_scroll(s->term, 0, row - (s->term->rows - 1));
552 else if (row < 0)
553 term_scroll(s->term, 0, row);
554 }
fc5b0934 555 term_mouse(s->term, MBT_LEFT,
556 event->modifiers & shiftKey ? MBT_EXTEND : MBT_SELECT,
d082ac49 557 MA_RELEASE, col, row, event->modifiers & shiftKey,
558 event->modifiers & controlKey, event->modifiers & optionKey);
559 lastwhen = TickCount();
560}
561
39687cc7 562void write_clip(void *cookie, wchar_t *data, int len, int must_deselect)
563{
1d4d52ce 564#if !TARGET_API_MAC_CARBON
39687cc7 565 Session *s = cookie;
566 char *mactextbuf;
567 ByteCount iread, olen;
568 wchar_t *unitextptr;
569 StScrpRec *stsc;
570 size_t stsz;
571 OSErr err;
572 int i;
573
d082ac49 574 /*
575 * See "Programming with the Text Encoding Conversion Manager"
576 * Appendix E for Unicode scrap conventions.
577 *
d082ac49 578 * XXX Maybe PICT scrap too.
579 */
580 if (ZeroScrap() != noErr)
581 return;
582 PutScrap(len * sizeof(*data), 'utxt', data);
39687cc7 583
584 /* Replace LINE SEPARATORs with CR for TEXT output. */
585 for (i = 0; i < len; i++)
586 if (data[i] == 0x2028)
587 data[i] = 0x000d;
588
589 mactextbuf = smalloc(len); /* XXX DBCS */
590 if (s->uni_to_font != NULL) {
591 err = ConvertFromUnicodeToText(s->uni_to_font, len * sizeof(UniChar),
592 (UniChar *)data,
593 kUnicodeUseFallbacksMask,
594 0, NULL, NULL, NULL,
595 len, &iread, &olen, mactextbuf);
596 if (err != noErr && err != kTECUsedFallbacksStatus)
597 return;
598 } else if (s->font_charset != CS_NONE) {
599 unitextptr = data;
600 olen = charset_from_unicode(&unitextptr, &len, mactextbuf, 1024,
601 s->font_charset, NULL, ".", 1);
602 } else
603 return;
604 PutScrap(olen, 'TEXT', mactextbuf);
605 sfree(mactextbuf);
606
607 stsz = offsetof(StScrpRec, scrpStyleTab) + sizeof(ScrpSTElement);
608 stsc = smalloc(stsz);
609 stsc->scrpNStyles = 1;
610 stsc->scrpStyleTab[0].scrpStartChar = 0;
611 stsc->scrpStyleTab[0].scrpHeight = s->font_height;
612 stsc->scrpStyleTab[0].scrpAscent = s->font_ascent;
613 stsc->scrpStyleTab[0].scrpFont = s->fontnum;
614 stsc->scrpStyleTab[0].scrpFace = 0;
e3c5b245 615 stsc->scrpStyleTab[0].scrpSize = s->cfg.font.size;
39687cc7 616 stsc->scrpStyleTab[0].scrpColor.red = 0;
617 stsc->scrpStyleTab[0].scrpColor.green = 0;
618 stsc->scrpStyleTab[0].scrpColor.blue = 0;
619 PutScrap(stsz, 'styl', stsc);
620 sfree(stsc);
1d4d52ce 621#endif
d082ac49 622}
623
624void get_clip(void *frontend, wchar_t **p, int *lenp) {
1d4d52ce 625#if TARGET_API_MAC_CARBON
626 *lenp = 0;
627#else
d082ac49 628 Session *s = frontend;
629 static Handle h = NULL;
fd3bd9fb 630 static wchar_t *data = NULL;
631 Handle texth;
d082ac49 632 long offset;
fd3bd9fb 633 int textlen;
634 TextEncoding enc;
635 TextToUnicodeInfo scrap_to_uni;
636 ByteCount iread, olen;
637 int charset;
638 char *tptr;
639 OSErr err;
d082ac49 640
641 if (p == NULL) {
642 /* release memory */
643 if (h != NULL)
644 DisposeHandle(h);
645 h = NULL;
fd3bd9fb 646 if (data != NULL)
647 sfree(data);
648 data = NULL;
649 } else {
d082ac49 650 if (GetScrap(NULL, 'utxt', &offset) > 0) {
fd3bd9fb 651 if (h == NULL)
652 h = NewHandle(0);
d082ac49 653 *lenp = GetScrap(h, 'utxt', &offset) / sizeof(**p);
654 HLock(h);
655 *p = (wchar_t *)*h;
fd3bd9fb 656 } else if (GetScrap(NULL, 'TEXT', &offset) > 0) {
657 texth = NewHandle(0);
658 textlen = GetScrap(texth, 'TEXT', &offset);
659 HLock(texth);
660 data = smalloc(textlen * 2);
661 /* XXX should use 'styl' scrap if it's there. */
662 if (mac_gestalts.encvvers != 0 &&
663 UpgradeScriptInfoToTextEncoding(smSystemScript,
664 kTextLanguageDontCare,
665 kTextRegionDontCare, NULL,
666 &enc) == noErr &&
667 CreateTextToUnicodeInfoByEncoding(enc, &scrap_to_uni) ==
668 noErr) {
669 err = ConvertFromTextToUnicode(scrap_to_uni, textlen,
670 *texth, 0, 0, NULL, NULL, NULL,
671 textlen * 2,
672 &iread, &olen, data);
673 DisposeTextToUnicodeInfo(&scrap_to_uni);
674 if (err == noErr) {
675 *p = data;
676 *lenp = olen / sizeof(**p);
677 } else {
678 *p = NULL;
679 *lenp = 0;
680 }
681 } else {
682 charset =
683 charset_from_macenc(GetScriptManagerVariable(smSysScript),
684 GetScriptManagerVariable(smRegionCode),
685 mac_gestalts.sysvers, NULL);
686 if (charset != CS_NONE) {
687 tptr = *texth;
688 *lenp = charset_to_unicode(&tptr, &textlen, data,
689 textlen * 2, charset, NULL,
690 NULL, 0);
691 }
692 *p = data;
693 }
694 DisposeHandle(texth);
d082ac49 695 } else {
696 *p = NULL;
697 *lenp = 0;
698 }
fd3bd9fb 699 }
1d4d52ce 700#endif
d082ac49 701}
702
703static pascal void mac_scrolltracker(ControlHandle control, short part) {
704 Session *s;
705
1d4d52ce 706#if TARGET_API_MAC_CARBON
707 s = (Session *)GetWRefCon(GetControlOwner(control));
708#else
d082ac49 709 s = (Session *)GetWRefCon((*control)->contrlOwner);
1d4d52ce 710#endif
d082ac49 711 switch (part) {
712 case kControlUpButtonPart:
713 term_scroll(s->term, 0, -1);
714 break;
715 case kControlDownButtonPart:
716 term_scroll(s->term, 0, +1);
717 break;
718 case kControlPageUpPart:
719 term_scroll(s->term, 0, -(s->term->rows - 1));
720 break;
721 case kControlPageDownPart:
722 term_scroll(s->term, 0, +(s->term->rows - 1));
723 break;
724 }
725}
726
d082ac49 727void mac_keyterm(WindowPtr window, EventRecord *event) {
6c50d421 728 Session *s = (Session *)GetWRefCon(window);
729 Key_Sym keysym = PK_NULL;
730 unsigned int mods = 0, flags = PKF_NUMLOCK;
4fa38586 731 UniChar utxt[1];
732 char txt[1];
733 size_t len = 0;
734 ScriptCode key_script;
d082ac49 735
10135a55 736 ObscureCursor();
d082ac49 737
37ea5668 738#if 0
6c50d421 739 fprintf(stderr, "Got key event %08x\n", event->message);
37ea5668 740#endif
d082ac49 741
742 /* No meta key yet -- that'll be rather fun. */
743
744 /* Keys that we handle locally */
745 if (event->modifiers & shiftKey) {
6c50d421 746 switch ((event->message & keyCodeMask) >> 8) {
747 case 0x74: /* shift-pageup */
d082ac49 748 term_scroll(s->term, 0, -(s->term->rows - 1));
6c50d421 749 return;
750 case 0x79: /* shift-pagedown */
d082ac49 751 term_scroll(s->term, 0, +(s->term->rows - 1));
6c50d421 752 return;
d082ac49 753 }
754 }
755
6c50d421 756 if (event->modifiers & shiftKey)
757 mods |= PKM_SHIFT;
758 if (event->modifiers & controlKey)
759 mods |= PKM_CONTROL;
760 if (event->what == autoKey)
761 flags |= PKF_REPEAT;
762
763 /* Mac key events consist of a virtual key code and a character code. */
764
765 switch ((event->message & keyCodeMask) >> 8) {
766 case 0x24: keysym = PK_RETURN; break;
767 case 0x30: keysym = PK_TAB; break;
768 case 0x33: keysym = PK_BACKSPACE; break;
769 case 0x35: keysym = PK_ESCAPE; break;
770
771 case 0x7A: keysym = PK_F1; break;
772 case 0x78: keysym = PK_F2; break;
773 case 0x63: keysym = PK_F3; break;
774 case 0x76: keysym = PK_F4; break;
775 case 0x60: keysym = PK_F5; break;
776 case 0x61: keysym = PK_F6; break;
777 case 0x62: keysym = PK_F7; break;
778 case 0x64: keysym = PK_F8; break;
779 case 0x65: keysym = PK_F9; break;
780 case 0x6D: keysym = PK_F10; break;
781 case 0x67: keysym = PK_F11; break;
782 case 0x6F: keysym = PK_F12; break;
783 case 0x69: keysym = PK_F13; break;
784 case 0x6B: keysym = PK_F14; break;
785 case 0x71: keysym = PK_F15; break;
786
787 case 0x72: keysym = PK_INSERT; break;
788 case 0x73: keysym = PK_HOME; break;
789 case 0x74: keysym = PK_PAGEUP; break;
790 case 0x75: keysym = PK_DELETE; break;
791 case 0x77: keysym = PK_END; break;
792 case 0x79: keysym = PK_PAGEDOWN; break;
793
794 case 0x47: keysym = PK_PF1; break;
795 case 0x51: keysym = PK_PF2; break;
796 case 0x4B: keysym = PK_PF3; break;
797 case 0x43: keysym = PK_PF4; break;
798 case 0x4E: keysym = PK_KPMINUS; break;
799 case 0x45: keysym = PK_KPCOMMA; break;
800 case 0x41: keysym = PK_KPDECIMAL; break;
801 case 0x4C: keysym = PK_KPENTER; break;
802 case 0x52: keysym = PK_KP0; break;
803 case 0x53: keysym = PK_KP1; break;
804 case 0x54: keysym = PK_KP2; break;
805 case 0x55: keysym = PK_KP3; break;
806 case 0x56: keysym = PK_KP4; break;
807 case 0x57: keysym = PK_KP5; break;
808 case 0x58: keysym = PK_KP6; break;
809 case 0x59: keysym = PK_KP7; break;
810 case 0x5B: keysym = PK_KP8; break;
811 case 0x5C: keysym = PK_KP9; break;
812
813 case 0x7B: keysym = PK_LEFT; break;
814 case 0x7C: keysym = PK_RIGHT; break;
815 case 0x7D: keysym = PK_DOWN; break;
816 case 0x7E: keysym = PK_UP; break;
d082ac49 817 }
818
4fa38586 819 /* Map from key script to Unicode. */
820 txt[0] = event->message & charCodeMask;
821 key_script = GetScriptManagerVariable(smKeyScript);
822
823 if (mac_gestalts.encvvers != 0) {
824 static TextToUnicodeInfo key_to_uni = NULL;
825 static ScriptCode key_to_uni_script;
826 TextEncoding enc;
827 ByteCount iread, olen;
828 OSErr err;
829
830 if (key_to_uni != NULL && key_to_uni_script != key_script)
831 DisposeTextToUnicodeInfo(&key_to_uni);
832 if (key_to_uni == NULL || key_to_uni_script != key_script) {
833 if (UpgradeScriptInfoToTextEncoding(key_script,
834 kTextLanguageDontCare,
835 kTextRegionDontCare, NULL,
836 &enc) == noErr &&
837 CreateTextToUnicodeInfoByEncoding(enc, &key_to_uni) == noErr)
838 key_to_uni_script = key_script;
839 else
840 key_to_uni = NULL;
841 }
842 if (key_to_uni != NULL) {
843 err = ConvertFromTextToUnicode(key_to_uni, 1, txt,
844 (kUnicodeKeepInfoMask |
845 kUnicodeStringUnterminatedMask),
846 0, NULL, NULL, NULL,
847 sizeof(utxt), &iread, &olen, utxt);
848 if (err == noErr)
849 len = olen / sizeof(*utxt);
850 }
851 } else {
852 int charset;
853 char *tptr = txt;
854 int tlen = 1;
855
856 charset = charset_from_macenc(key_script,
857 GetScriptManagerVariable(smRegionCode),
858 mac_gestalts.sysvers, NULL);
859 if (charset != CS_NONE) {
860 len = charset_to_unicode(&tptr, &tlen, utxt, sizeof(utxt), charset,
861 NULL, NULL, 0);
862 }
863 }
864 term_key(s->term, keysym, utxt, len, mods, flags);
d082ac49 865}
866
867void request_paste(void *frontend)
868{
869 Session *s = frontend;
870
871 /*
872 * In the Mac OS, pasting is synchronous: we can read the
873 * clipboard with no difficulty, so request_paste() can just go
874 * ahead and paste.
875 */
876 term_do_paste(s->term);
877}
878
64b8ebc6 879static struct {
880 Rect msgrect;
881 Point msgorigin;
8095091b 882 Point zeromouse;
64b8ebc6 883 Session *s;
884 char oldmsg[20];
885} growterm_state;
886
d082ac49 887void mac_growterm(WindowPtr window, EventRecord *event) {
888 Rect limits;
889 long grow_result;
890 int newrows, newcols;
891 Session *s;
1d4d52ce 892#if !TARGET_API_MAC_CARBON
64b8ebc6 893 DragGrayRgnUPP draghooksave;
894 GrafPtr portsave;
895 FontInfo fi;
1d4d52ce 896#endif
d082ac49 897
898 s = (Session *)GetWRefCon(window);
64b8ebc6 899
1d4d52ce 900#if !TARGET_API_MAC_CARBON
64b8ebc6 901 draghooksave = LMGetDragHook();
902 growterm_state.oldmsg[0] = '\0';
8095091b 903 growterm_state.zeromouse = event->where;
904 growterm_state.zeromouse.h -= s->term->cols * s->font_width;
905 growterm_state.zeromouse.v -= s->term->rows * s->font_height;
64b8ebc6 906 growterm_state.s = s;
907 GetPort(&portsave);
908 SetPort(s->window);
909 BackColor(whiteColor);
910 ForeColor(blackColor);
911 TextFont(systemFont);
912 TextFace(0);
913 TextSize(12);
914 GetFontInfo(&fi);
915 SetRect(&growterm_state.msgrect, 0, 0,
916 StringWidth("\p99999x99999") + 4, fi.ascent + fi.descent + 4);
917 SetPt(&growterm_state.msgorigin, 2, fi.ascent + 2);
918 LMSetDragHook(NewDragGrayRgnUPP(mac_growtermdraghook));
1d4d52ce 919#endif
64b8ebc6 920
d082ac49 921 SetRect(&limits, s->font_width + 15, s->font_height, SHRT_MAX, SHRT_MAX);
922 grow_result = GrowWindow(window, event->where, &limits);
64b8ebc6 923
1d4d52ce 924#if !TARGET_API_MAC_CARBON
64b8ebc6 925 DisposeDragGrayRgnUPP(LMGetDragHook());
926 LMSetDragHook(draghooksave);
927 InvalRect(&growterm_state.msgrect);
928
929 SetPort(portsave);
1d4d52ce 930#endif
64b8ebc6 931
d082ac49 932 if (grow_result != 0) {
933 newrows = HiWord(grow_result) / s->font_height;
934 newcols = (LoWord(grow_result) - 15) / s->font_width;
935 mac_adjustsize(s, newrows, newcols);
936 term_size(s->term, newrows, newcols, s->cfg.savelines);
937 }
938}
939
1d4d52ce 940#if !TARGET_API_MAC_CARBON
64b8ebc6 941static pascal void mac_growtermdraghook(void)
942{
943 Session *s = growterm_state.s;
944 GrafPtr portsave;
945 Point mouse;
946 char buf[20];
786ccf05 947 unsigned char pbuf[20];
64b8ebc6 948 int newrows, newcols;
949
950 GetMouse(&mouse);
8095091b 951 newrows = (mouse.v - growterm_state.zeromouse.v) / s->font_height;
64b8ebc6 952 if (newrows < 1) newrows = 1;
8095091b 953 newcols = (mouse.h - growterm_state.zeromouse.h) / s->font_width;
64b8ebc6 954 if (newcols < 1) newcols = 1;
955 sprintf(buf, "%dx%d", newcols, newrows);
956 if (strcmp(buf, growterm_state.oldmsg) == 0)
957 return;
958 strcpy(growterm_state.oldmsg, buf);
786ccf05 959 c2pstrcpy(pbuf, buf);
64b8ebc6 960
961 GetPort(&portsave);
962 SetPort(growterm_state.s->window);
963 EraseRect(&growterm_state.msgrect);
964 MoveTo(growterm_state.msgorigin.h, growterm_state.msgorigin.v);
786ccf05 965 DrawString(pbuf);
64b8ebc6 966 SetPort(portsave);
967}
1d4d52ce 968#endif
64b8ebc6 969
fabd1805 970void mac_closeterm(WindowPtr window)
971{
972 Session *s = (Session *)GetWRefCon(window);
973
974 /* XXX warn on close */
975 HideWindow(s->window);
976 *s->prev = s->next;
977 s->next->prev = s->prev;
978 ldisc_free(s->ldisc);
979 s->back->free(s->backhandle);
980 log_free(s->logctx);
981 if (s->uni_to_font != NULL)
982 DisposeUnicodeToTextInfo(&s->uni_to_font);
983 term_free(s->term);
984 DisposeWindow(s->window);
985 DisposePalette(s->palette);
986 sfree(s);
987}
988
d082ac49 989void mac_activateterm(WindowPtr window, Boolean active) {
990 Session *s;
991
992 s = (Session *)GetWRefCon(window);
7d22c2e8 993 s->term->has_focus = active;
d082ac49 994 term_update(s->term);
995 if (active)
996 ShowControl(s->scrollbar);
997 else {
998 if (HAVE_COLOR_QD())
999 PmBackColor(DEFAULT_BG);/* HideControl clears behind the control */
1000 else
1001 BackColor(blackColor);
1002 HideControl(s->scrollbar);
1003 }
1004 mac_drawgrowicon(s);
1005}
1006
1007void mac_updateterm(WindowPtr window) {
1008 Session *s;
1d4d52ce 1009 Rect bbox;
1010#if TARGET_API_MAC_CARBON
1011 RgnHandle visrgn;
1012#endif
d082ac49 1013
1014 s = (Session *)GetWRefCon(window);
5a736db6 1015 SetPort((GrafPtr)GetWindowPort(window));
d082ac49 1016 BeginUpdate(window);
1017 pre_paint(s);
1d4d52ce 1018#if TARGET_API_MAC_CARBON
1019 visrgn = NewRgn();
1020 GetPortVisibleRegion(GetWindowPort(window), visrgn);
1021 GetRegionBounds(visrgn, &bbox);
1022#else
1023 bbox = (*window->visRgn)->rgnBBox;
1024#endif
1025 term_paint(s->term, s, PTOCC(bbox.left), PTOCR(bbox.top),
1026 PTOCC(bbox.right), PTOCR(bbox.bottom), 1);
d082ac49 1027 /* Restore default colours in case the Window Manager uses them */
1028 if (HAVE_COLOR_QD()) {
1029 PmForeColor(DEFAULT_FG);
1030 PmBackColor(DEFAULT_BG);
1031 } else {
1032 ForeColor(whiteColor);
1033 BackColor(blackColor);
1034 }
1035 if (FrontWindow() != window)
1d4d52ce 1036#if TARGET_API_MAC_CARBON
1037 EraseRect(GetControlBounds(s->scrollbar, &bbox));
1038 UpdateControls(window, visrgn);
1039 DisposeRgn(visrgn);
1040#else
d082ac49 1041 EraseRect(&(*s->scrollbar)->contrlRect);
1042 UpdateControls(window, window->visRgn);
1d4d52ce 1043#endif
d082ac49 1044 mac_drawgrowicon(s);
1045 post_paint(s);
1046 EndUpdate(window);
1047}
1048
1049static void mac_drawgrowicon(Session *s) {
1050 Rect clip;
ea4c3d8a 1051 RgnHandle savergn;
d082ac49 1052
5a736db6 1053 SetPort((GrafPtr)GetWindowPort(s->window));
ea4c3d8a 1054 /*
1055 * Stop DrawGrowIcon giving us space for a horizontal scrollbar
1056 * See Tech Note TB575 for details.
1057 */
1d4d52ce 1058#if TARGET_API_MAC_CARBON
1059 GetPortBounds(GetWindowPort(s->window), &clip);
1060#else
ea4c3d8a 1061 clip = s->window->portRect;
1d4d52ce 1062#endif
ea4c3d8a 1063 clip.left = clip.right - 15;
1064 savergn = NewRgn();
1065 GetClip(savergn);
d082ac49 1066 ClipRect(&clip);
1067 DrawGrowIcon(s->window);
ea4c3d8a 1068 SetClip(savergn);
1069 DisposeRgn(savergn);
d082ac49 1070}
1071
1072struct do_text_args {
1073 Session *s;
1074 Rect textrect;
d082ac49 1075 char *text;
1076 int len;
1077 unsigned long attr;
1078 int lattr;
fd7d8b47 1079 Point numer, denom;
d082ac49 1080};
1081
1082/*
1083 * Call from the terminal emulator to draw a bit of text
1084 *
1085 * x and y are text row and column (zero-based)
1086 */
1087void do_text(Context ctx, int x, int y, char *text, int len,
1088 unsigned long attr, int lattr) {
1089 Session *s = ctx;
e3c5b245 1090 int style;
d082ac49 1091 struct do_text_args a;
c3aef739 1092 RgnHandle textrgn, saveclip;
1d4d52ce 1093#if TARGET_API_MAC_CARBON
1094 RgnHandle visrgn;
1095#endif
8768ce31 1096 char mactextbuf[1024];
1097 UniChar unitextbuf[1024];
801e70ea 1098 wchar_t *unitextptr;
c3aef739 1099 int i, fontwidth;
801e70ea 1100 ByteCount iread, olen;
1101 OSStatus err;
1d4d52ce 1102 static DeviceLoopDrawingUPP do_text_for_device_upp = NULL;
8768ce31 1103
1104 assert(len <= 1024);
d082ac49 1105
5a736db6 1106 SetPort((GrafPtr)GetWindowPort(s->window));
c3aef739 1107
1108 fontwidth = s->font_width;
1109 if ((lattr & LATTR_MODE) != LATTR_NORM)
1110 fontwidth *= 2;
1111
d082ac49 1112 /* First check this text is relevant */
1113 a.textrect.top = y * s->font_height;
1114 a.textrect.bottom = (y + 1) * s->font_height;
c3aef739 1115 a.textrect.left = x * fontwidth;
1116 a.textrect.right = (x + len) * fontwidth;
1117 if (a.textrect.right > s->term->cols * s->font_width)
1118 a.textrect.right = s->term->cols * s->font_width;
1d4d52ce 1119#if TARGET_API_MAC_CARBON
1120 visrgn = NewRgn();
1121 GetPortVisibleRegion(GetWindowPort(s->window), visrgn);
1122 if (!RectInRgn(&a.textrect, visrgn)) {
1123 DisposeRgn(visrgn);
1124 return;
1125 }
1126 DisposeRgn(visrgn);
1127#else
d082ac49 1128 if (!RectInRgn(&a.textrect, s->window->visRgn))
1129 return;
1d4d52ce 1130#endif
d082ac49 1131
801e70ea 1132 /* Unpack Unicode from the mad format we get passed */
1133 for (i = 0; i < len; i++)
1134 unitextbuf[i] = (unsigned char)text[i] | (attr & CSET_MASK);
8768ce31 1135
801e70ea 1136 if (s->uni_to_font != NULL) {
8768ce31 1137 err = ConvertFromUnicodeToText(s->uni_to_font, len * sizeof(UniChar),
1138 unitextbuf, kUnicodeUseFallbacksMask,
1139 0, NULL, NULL, NULL,
1140 1024, &iread, &olen, mactextbuf);
801e70ea 1141 if (err != noErr && err != kTECUsedFallbacksStatus)
379836ca 1142 olen = 0;
8ef2b196 1143 } else if (s->font_charset != CS_NONE) {
801e70ea 1144 /* XXX this is bogus if wchar_t and UniChar are different sizes. */
1145 unitextptr = (wchar_t *)unitextbuf;
801e70ea 1146 olen = charset_from_unicode(&unitextptr, &len, mactextbuf, 1024,
8ef2b196 1147 s->font_charset, NULL, ".", 1);
1148 } else
379836ca 1149 olen = 0;
8768ce31 1150
d082ac49 1151 a.s = s;
801e70ea 1152 a.text = mactextbuf;
1153 a.len = olen;
d082ac49 1154 a.attr = attr;
1155 a.lattr = lattr;
c3aef739 1156 switch (lattr & LATTR_MODE) {
1157 case LATTR_NORM:
e3c5b245 1158 TextSize(s->cfg.font.size);
c3aef739 1159 a.numer = s->font_stdnumer;
1160 a.denom = s->font_stddenom;
1161 break;
1162 case LATTR_WIDE:
e3c5b245 1163 TextSize(s->cfg.font.size);
c3aef739 1164 a.numer = s->font_widenumer;
1165 a.denom = s->font_widedenom;
1166 break;
1167 case LATTR_TOP:
1168 case LATTR_BOT:
e3c5b245 1169 TextSize(s->cfg.font.size * 2);
c3aef739 1170 a.numer = s->font_bignumer;
1171 a.denom = s->font_bigdenom;
1172 break;
1173 }
5a736db6 1174 SetPort((GrafPtr)GetWindowPort(s->window));
d082ac49 1175 TextFont(s->fontnum);
e3c5b245 1176 style = s->cfg.font.face;
1177 if ((attr & ATTR_BOLD) && !s->cfg.bold_colour)
d082ac49 1178 style |= bold;
1179 if (attr & ATTR_UNDER)
1180 style |= underline;
1181 TextFace(style);
4d1565a4 1182 TextMode(srcOr);
d082ac49 1183 if (HAVE_COLOR_QD())
1184 if (style & bold) {
1185 SpaceExtra(s->font_boldadjust << 16);
1186 CharExtra(s->font_boldadjust << 16);
1187 } else {
1188 SpaceExtra(0);
1189 CharExtra(0);
1190 }
c3aef739 1191 saveclip = NewRgn();
1192 GetClip(saveclip);
1193 ClipRect(&a.textrect);
d082ac49 1194 textrgn = NewRgn();
1195 RectRgn(textrgn, &a.textrect);
1d4d52ce 1196 if (HAVE_COLOR_QD()) {
1197 if (do_text_for_device_upp == NULL)
1198 do_text_for_device_upp =
1199 NewDeviceLoopDrawingUPP(&do_text_for_device);
1200 DeviceLoop(textrgn, do_text_for_device_upp, (long)&a, 0);
1201 } else
d082ac49 1202 do_text_for_device(1, 0, NULL, (long)&a);
c3aef739 1203 SetClip(saveclip);
1204 DisposeRgn(saveclip);
d082ac49 1205 DisposeRgn(textrgn);
1206 /* Tell the window manager about it in case this isn't an update */
1d4d52ce 1207#if TARGET_API_MAC_CARBON
1208 ValidWindowRect(s->window, &a.textrect);
1209#else
d082ac49 1210 ValidRect(&a.textrect);
1d4d52ce 1211#endif
d082ac49 1212}
1213
1214static pascal void do_text_for_device(short depth, short devflags,
1215 GDHandle device, long cookie) {
5a736db6 1216 struct do_text_args *a = (struct do_text_args *)cookie;
4d1565a4 1217 int bgcolour, fgcolour, bright, reverse, tmp;
1d4d52ce 1218#if TARGET_API_MAC_CARBON
1219 CQDProcsPtr gp = GetPortGrafProcs(GetWindowPort(a->s->window));
1220#else
1221 QDProcsPtr gp = a->s->window->grafProcs;
1222#endif
d082ac49 1223
d082ac49 1224 bright = (a->attr & ATTR_BOLD) && a->s->cfg.bold_colour;
4d1565a4 1225 reverse = a->attr & ATTR_REVERSE;
d082ac49 1226
4d1565a4 1227 if (depth == 1 && (a->attr & TATTR_ACTCURS))
1228 reverse = !reverse;
d082ac49 1229
4d1565a4 1230 if (HAVE_COLOR_QD()) {
1231 if (depth > 2) {
37ca32ed 1232 fgcolour = ((a->attr & ATTR_FGMASK) >> ATTR_FGSHIFT);
1233 fgcolour = (fgcolour & 0xF) * 2 + (fgcolour & 0x10 ? 1 : 0);
22afb0a5 1234 bgcolour = ((a->attr & ATTR_BGMASK) >> ATTR_BGSHIFT);
37ca32ed 1235 bgcolour = (bgcolour & 0xF) * 2 + (bgcolour & 0x10 ? 1 : 0);
d082ac49 1236 } else {
4d1565a4 1237 /*
1238 * NB: bold reverse in 2bpp breaks with the usual PuTTY model and
1239 * boldens the background, because that's all we can do.
1240 */
1241 fgcolour = bright ? DEFAULT_FG_BOLD : DEFAULT_FG;
1242 bgcolour = DEFAULT_BG;
d082ac49 1243 }
4d1565a4 1244 if (reverse) {
1245 tmp = fgcolour;
1246 fgcolour = bgcolour;
1247 bgcolour = tmp;
1248 }
1249 if (bright && depth > 2)
37ca32ed 1250 fgcolour |= 1;
4d1565a4 1251 if ((a->attr & TATTR_ACTCURS) && depth > 1) {
e0cbe032 1252 fgcolour = CURSOR_FG;
d082ac49 1253 bgcolour = CURSOR_BG;
d082ac49 1254 }
1255 PmForeColor(fgcolour);
1256 PmBackColor(bgcolour);
4d1565a4 1257 } else { /* No Color Quickdraw */
1258 /* XXX This should be done with a _little_ more configurability */
1259 if (reverse) {
1260 ForeColor(blackColor);
1261 BackColor(whiteColor);
1262 } else {
1263 ForeColor(whiteColor);
1264 BackColor(blackColor);
1265 }
d082ac49 1266 }
1267
4d1565a4 1268 EraseRect(&a->textrect);
c3aef739 1269 switch (a->lattr & LATTR_MODE) {
1270 case LATTR_NORM:
1271 case LATTR_WIDE:
1272 MoveTo(a->textrect.left, a->textrect.top + a->s->font_ascent);
1273 break;
1274 case LATTR_TOP:
1275 MoveTo(a->textrect.left, a->textrect.top + a->s->font_ascent * 2);
1276 break;
1277 case LATTR_BOT:
1278 MoveTo(a->textrect.left,
1279 a->textrect.top - a->s->font_height + a->s->font_ascent * 2);
1280 break;
1281 }
d082ac49 1282 /* FIXME: Sort out bold width adjustments on Original QuickDraw. */
1d4d52ce 1283 if (gp != NULL)
1284 InvokeQDTextUPP(a->len, a->text, a->numer, a->denom, gp->textProc);
fd7d8b47 1285 else
1286 StdText(a->len, a->text, a->numer, a->denom);
d082ac49 1287
1288 if (a->attr & TATTR_PASCURS) {
1289 PenNormal();
1290 switch (depth) {
1291 case 1:
1292 PenMode(patXor);
1293 break;
1294 default:
1295 PmForeColor(CURSOR_BG);
1296 break;
1297 }
1298 FrameRect(&a->textrect);
1299 }
1300}
1301
1302void do_cursor(Context ctx, int x, int y, char *text, int len,
1303 unsigned long attr, int lattr)
1304{
1305
e0cbe032 1306 do_text(ctx, x, y, text, len, attr, lattr);
d082ac49 1307}
1308
1309/*
1310 * Call from the terminal emulator to get its graphics context.
1311 * Should probably be called start_redraw or something.
1312 */
1313void pre_paint(Session *s) {
2647b103 1314 GDHandle gdh;
1315 Rect myrect, tmprect;
1d4d52ce 1316#if TARGET_API_MAC_CARBON
1317 RgnHandle visrgn;
1318#endif
d082ac49 1319
2647b103 1320 if (HAVE_COLOR_QD()) {
0a0fa8cd 1321 s->term->attr_mask = 0;
5a736db6 1322 SetPort((GrafPtr)GetWindowPort(s->window));
1d4d52ce 1323#if TARGET_API_MAC_CARBON
1324 visrgn = NewRgn();
1325 GetPortVisibleRegion(GetWindowPort(s->window), visrgn);
1326 GetRegionBounds(visrgn, &myrect);
1327 DisposeRgn(visrgn);
1328#else
2647b103 1329 myrect = (*s->window->visRgn)->rgnBBox;
1d4d52ce 1330#endif
2647b103 1331 LocalToGlobal((Point *)&myrect.top);
1332 LocalToGlobal((Point *)&myrect.bottom);
1333 for (gdh = GetDeviceList();
1334 gdh != NULL;
1335 gdh = GetNextDevice(gdh)) {
1336 if (TestDeviceAttribute(gdh, screenDevice) &&
1337 TestDeviceAttribute(gdh, screenActive) &&
1338 SectRect(&(*gdh)->gdRect, &myrect, &tmprect)) {
1339 switch ((*(*gdh)->gdPMap)->pixelSize) {
1340 case 1:
1341 if (s->cfg.bold_colour)
0a0fa8cd 1342 s->term->attr_mask |= ~(ATTR_COLOURS |
1343 (s->cfg.bold_colour ? ATTR_BOLD : 0));
e373bb53 1344 break;
2647b103 1345 case 2:
e373bb53 1346 s->term->attr_mask |= ~ATTR_COLOURS;
1347 break;
1348 default:
1349 s->term->attr_mask = ~0;
1350 return; /* No point checking more screens. */
2647b103 1351 }
1352 }
1353 }
1354 } else
0a0fa8cd 1355 s->term->attr_mask = ~(ATTR_COLOURS |
2647b103 1356 (s->cfg.bold_colour ? ATTR_BOLD : 0));
d082ac49 1357}
1358
1359Context get_ctx(void *frontend) {
1360 Session *s = frontend;
1361
1362 pre_paint(s);
1363 return s;
1364}
1365
1366void free_ctx(Context ctx) {
1367
1368}
1369
d082ac49 1370/*
1371 * Presumably this does something in Windows
1372 */
1373void post_paint(Session *s) {
1374
1375}
1376
1377/*
1378 * Set the scroll bar position
1379 *
1380 * total is the line number of the bottom of the working screen
1381 * start is the line number of the top of the display
1382 * page is the length of the displayed page
1383 */
1384void set_sbar(void *frontend, int total, int start, int page) {
1385 Session *s = frontend;
1386
1387 /* We don't redraw until we've set everything up, to avoid glitches */
1d4d52ce 1388 SetControlMinimum(s->scrollbar, 0);
1389 SetControlMaximum(s->scrollbar, total - page);
d082ac49 1390 SetControlValue(s->scrollbar, start);
1fc898ea 1391#if !TARGET_CPU_68K
d082ac49 1392 if (mac_gestalts.cntlattr & gestaltControlMgrPresent)
1393 SetControlViewSize(s->scrollbar, page);
1394#endif
1395}
1396
1397void sys_cursor(void *frontend, int x, int y)
1398{
1399 /*
1400 * I think his is meaningless under Mac OS.
1401 */
1402}
1403
1404/*
1405 * This is still called when mode==BELL_VISUAL, even though the
1406 * visual bell is handled entirely within terminal.c, because we
1407 * may want to perform additional actions on any kind of bell (for
1408 * example, taskbar flashing in Windows).
1409 */
1410void beep(void *frontend, int mode)
1411{
1412 if (mode != BELL_VISUAL)
1413 SysBeep(30);
1414 /*
1415 * XXX We should indicate the relevant window and/or use the
1416 * Notification Manager
1417 */
1418}
1419
1420int char_width(Context ctx, int uc)
1421{
1422 /*
1423 * Until we support exciting character-set stuff, assume all chars are
1424 * single-width.
1425 */
1426 return 1;
1427}
1428
1429/*
1430 * Set icon string -- a no-op here (Windowshade?)
1431 */
1432void set_icon(void *frontend, char *icon) {
1433 Session *s = frontend;
1434
1435}
1436
1437/*
1438 * Set the window title
1439 */
1440void set_title(void *frontend, char *title) {
1441 Session *s = frontend;
1442 Str255 mactitle;
1443
7770ad29 1444 c2pstrcpy(mactitle, title);
d082ac49 1445 SetWTitle(s->window, mactitle);
1446}
1447
1448/*
1449 * set or clear the "raw mouse message" mode
1450 */
1451void set_raw_mouse_mode(void *frontend, int activate)
1452{
1453 Session *s = frontend;
1454
1455 s->raw_mouse = activate;
1456 /* FIXME: Should call mac_updatetermcursor as appropriate. */
1457}
1458
1459/*
1460 * Resize the window at the emulator's request
1461 */
1462void request_resize(void *frontend, int w, int h) {
1463 Session *s = frontend;
1464
97c511f6 1465 term_size(s->term, h, w, s->cfg.savelines);
d082ac49 1466 mac_initfont(s);
1467}
1468
1469/*
1470 * Iconify (actually collapse) the window at the emulator's request.
1471 */
1472void set_iconic(void *frontend, int iconic)
1473{
1474 Session *s = frontend;
1475 UInt32 features;
1476
1477 if (mac_gestalts.apprvers >= 0x0100 &&
1478 GetWindowFeatures(s->window, &features) == noErr &&
1479 (features & kWindowCanCollapse))
1480 CollapseWindow(s->window, iconic);
1481}
1482
1483/*
1484 * Move the window in response to a server-side request.
1485 */
1486void move_window(void *frontend, int x, int y)
1487{
1488 Session *s = frontend;
1489
1490 MoveWindow(s->window, x, y, FALSE);
1491}
1492
1493/*
1494 * Move the window to the top or bottom of the z-order in response
1495 * to a server-side request.
1496 */
1497void set_zorder(void *frontend, int top)
1498{
1499 Session *s = frontend;
1500
1501 /*
1502 * We also change the input focus to point to the topmost window,
1503 * since that's probably what the Human Interface Guidelines would
1504 * like us to do.
1505 */
1506 if (top)
1507 SelectWindow(s->window);
1508 else
1509 SendBehind(s->window, NULL);
1510}
1511
1512/*
1513 * Refresh the window in response to a server-side request.
1514 */
1515void refresh_window(void *frontend)
1516{
1517 Session *s = frontend;
1518
1519 term_invalidate(s->term);
1520}
1521
1522/*
1523 * Maximise or restore the window in response to a server-side
1524 * request.
1525 */
1526void set_zoomed(void *frontend, int zoomed)
1527{
1528 Session *s = frontend;
1529
1530 ZoomWindow(s->window, zoomed ? inZoomOut : inZoomIn, FALSE);
1531}
1532
1533/*
1534 * Report whether the window is iconic, for terminal reports.
1535 */
1536int is_iconic(void *frontend)
1537{
1538 Session *s = frontend;
1539 UInt32 features;
1540
1541 if (mac_gestalts.apprvers >= 0x0100 &&
1542 GetWindowFeatures(s->window, &features) == noErr &&
1543 (features & kWindowCanCollapse))
1544 return IsWindowCollapsed(s->window);
1545 return FALSE;
1546}
1547
1548/*
1549 * Report the window's position, for terminal reports.
1550 */
1551void get_window_pos(void *frontend, int *x, int *y)
1552{
1553 Session *s = frontend;
1d4d52ce 1554 Rect rect;
d082ac49 1555
1d4d52ce 1556#if TARGET_API_MAC_CARBON
1557 GetPortBounds(GetWindowPort(s->window), &rect);
1558#else
1559 rect = s->window->portRect;
1560#endif
1561 *x = rect.left;
1562 *y = rect.top;
d082ac49 1563}
1564
1565/*
1566 * Report the window's pixel size, for terminal reports.
1567 */
1568void get_window_pixels(void *frontend, int *x, int *y)
1569{
1570 Session *s = frontend;
1d4d52ce 1571 Rect rect;
d082ac49 1572
1d4d52ce 1573#if TARGET_API_MAC_CARBON
1574 GetPortBounds(GetWindowPort(s->window), &rect);
1575#else
1576 rect = s->window->portRect;
1577#endif
1578 *x = rect.right - rect.left;
1579 *y = rect.bottom - rect.top;
d082ac49 1580}
1581
1582/*
1583 * Return the window or icon title.
1584 */
1585char *get_window_title(void *frontend, int icon)
1586{
1587 Session *s = frontend;
b6dfe962 1588 Str255 ptitle;
1589 static char title[256];
d082ac49 1590
b6dfe962 1591 GetWTitle(s->window, ptitle);
1592 p2cstrcpy(title, ptitle);
1593 return title;
d082ac49 1594}
1595
1596/*
1597 * real_palette_set(): This does the actual palette-changing work on behalf
1598 * of palette_set(). Does _not_ call ActivatePalette() in case the caller
1599 * is doing a batch of updates.
1600 */
1601static void real_palette_set(Session *s, int n, int r, int g, int b)
1602{
1603 RGBColor col;
1604
1605 if (!HAVE_COLOR_QD())
1606 return;
1607 col.red = r * 0x0101;
1608 col.green = g * 0x0101;
1609 col.blue = b * 0x0101;
d082ac49 1610 SetEntryColor(s->palette, n, &col);
1611}
1612
1613/*
1614 * Set the logical palette. Called by the terminal emulator.
1615 */
1616void palette_set(void *frontend, int n, int r, int g, int b) {
1617 Session *s = frontend;
1618 static const int first[21] = {
1619 0, 2, 4, 6, 8, 10, 12, 14,
1620 1, 3, 5, 7, 9, 11, 13, 15,
e0cbe032 1621 16, 17, 18, 20, 21
d082ac49 1622 };
1623
1624 if (!HAVE_COLOR_QD())
1625 return;
1626 real_palette_set(s, first[n], r, g, b);
e0cbe032 1627 if (first[n] == 18)
d082ac49 1628 real_palette_set(s, first[n]+1, r, g, b);
1629 if (first[n] == DEFAULT_BG)
1630 mac_adjustwinbg(s);
1631 ActivatePalette(s->window);
1632}
1633
1634/*
1635 * Reset to the default palette
1636 */
1637void palette_reset(void *frontend) {
1638 Session *s = frontend;
1639 /* This maps colour indices in cfg to those used in our palette. */
1640 static const int ww[] = {
1641 6, 7, 8, 9, 10, 11, 12, 13,
1642 14, 15, 16, 17, 18, 19, 20, 21,
1643 0, 1, 2, 3, 4, 5
1644 };
1645 int i;
1646
1647 if (!HAVE_COLOR_QD())
1648 return;
1649
1650 assert(lenof(ww) == NCOLOURS);
1651
1652 for (i = 0; i < NCOLOURS; i++) {
1653 real_palette_set(s, i,
1654 s->cfg.colours[ww[i]][0],
1655 s->cfg.colours[ww[i]][1],
1656 s->cfg.colours[ww[i]][2]);
1657 }
1658 mac_adjustwinbg(s);
1659 ActivatePalette(s->window);
1660 /* Palette Manager will generate update events as required. */
1661}
1662
1663/*
1664 * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
1665 * for backward.)
1666 */
341eb978 1667void do_scroll(Context ctx, int topline, int botline, int lines) {
1668 Session *s = ctx;
d082ac49 1669 Rect r;
11ddab61 1670 RgnHandle scrollrgn = NewRgn();
1671 RgnHandle movedupdate = NewRgn();
1672 RgnHandle update = NewRgn();
1673 Point g2l = { 0, 0 };
d082ac49 1674
5a736db6 1675 SetPort((GrafPtr)GetWindowPort(s->window));
11ddab61 1676
1677 /*
1678 * Work out the part of the update region that will scrolled by
1679 * this operation.
1680 */
1681 if (lines > 0)
1682 SetRectRgn(scrollrgn, 0, (topline + lines) * s->font_height,
1683 s->term->cols * s->font_width,
1684 (botline + 1) * s->font_height);
1685 else
1686 SetRectRgn(scrollrgn, 0, topline * s->font_height,
1687 s->term->cols * s->font_width,
1688 (botline - lines + 1) * s->font_height);
1d4d52ce 1689#if TARGET_API_MAC_CARBON
1690 GetWindowRegion(s->window, kWindowUpdateRgn, movedupdate);
1691#else
1692 GetWindowUpdateRgn(s->window, movedupdate);
1693#endif
11ddab61 1694 GlobalToLocal(&g2l);
1695 OffsetRgn(movedupdate, g2l.h, g2l.v); /* Convert to local co-ords. */
1696 SectRgn(scrollrgn, movedupdate, movedupdate); /* Clip scrolled section. */
1d4d52ce 1697#if TARGET_API_MAC_CARBON
1698 ValidWindowRgn(s->window, movedupdate);
1699#else
11ddab61 1700 ValidRgn(movedupdate);
1d4d52ce 1701#endif
11ddab61 1702 OffsetRgn(movedupdate, 0, -lines * s->font_height); /* Scroll it. */
1703
1951eebc 1704 PenNormal();
d082ac49 1705 if (HAVE_COLOR_QD())
1706 PmBackColor(DEFAULT_BG);
fd7d8b47 1707 else
1708 BackColor(blackColor); /* XXX make configurable */
d082ac49 1709 SetRect(&r, 0, topline * s->font_height,
1710 s->term->cols * s->font_width, (botline + 1) * s->font_height);
1711 ScrollRect(&r, 0, - lines * s->font_height, update);
11ddab61 1712
1d4d52ce 1713#if TARGET_API_MAC_CARBON
1714 InvalWindowRgn(s->window, update);
1715 InvalWindowRgn(s->window, movedupdate);
1716#else
d082ac49 1717 InvalRgn(update);
11ddab61 1718 InvalRgn(movedupdate);
1d4d52ce 1719#endif
11ddab61 1720
1721 DisposeRgn(scrollrgn);
1722 DisposeRgn(movedupdate);
d082ac49 1723 DisposeRgn(update);
1724}
1725
1726void logevent(void *frontend, char *str) {
1727
2beb0fb0 1728 fprintf(stderr, "%s\n", str);
d082ac49 1729}
1730
1731/* Dummy routine, only required in plink. */
1732void ldisc_update(void *frontend, int echo, int edit)
1733{
1734}
1735
1736/*
1737 * Mac PuTTY doesn't support printing yet.
1738 */
1739printer_job *printer_start_job(char *printer)
1740{
1741
1742 return NULL;
1743}
1744
1745void printer_job_data(printer_job *pj, void *data, int len)
1746{
1747}
1748
1749void printer_finish_job(printer_job *pj)
1750{
1751}
1752
1753void frontend_keypress(void *handle)
1754{
1755 /*
1756 * Keypress termination in non-Close-On-Exit mode is not
1757 * currently supported in PuTTY proper, because the window
1758 * always has a perfectly good Close button anyway. So we do
1759 * nothing here.
1760 */
1761 return;
1762}
1763
1764/*
1765 * Ask whether to wipe a session log file before writing to it.
1766 * Returns 2 for wipe, 1 for append, 0 for cancel (don't log).
1767 */
9a30e26b 1768int askappend(void *frontend, Filename filename)
d082ac49 1769{
1770
1771 /* FIXME: not implemented yet. */
1772 return 2;
1773}
1774
1775/*
1776 * Emacs magic:
1777 * Local Variables:
1778 * c-file-style: "simon"
1779 * End:
1780 */
1781