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