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