3 * Copyright (c) 1999 Simon Tatham
4 * Copyright (c) 1999, 2002 Ben Harris
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
16 * The above copyright notice and this permission notice shall be
17 * included in all copies or substantial portions of the Software.
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
30 * macterm.c -- Macintosh terminal front-end
35 #include <ControlDefinitions.h>
40 #include <MacMemory.h>
41 #include <MacWindows.h>
42 #include <MixedMode.h>
44 #include <Quickdraw.h>
45 #include <QuickdrawText.h>
46 #include <Resources.h>
50 #include <TextCommon.h>
51 #include <ToolUtils.h>
52 #include <UnicodeConverter.h>
66 #define DEFAULT_FG 256
67 #define DEFAULT_FG_BOLD 257
68 #define DEFAULT_BG 258
69 #define DEFAULT_BG_BOLD 259
73 #define PTOCC(x) ((x) < 0 ? -(-(x - s->font_width - 1) / s->font_width) : \
75 #define PTOCR(y) ((y) < 0 ? -(-(y - s->font_height - 1) / s->font_height) : \
78 static void mac_initfont(Session
*);
79 static pascal OSStatus
uni_to_font_fallback(UniChar
*, ByteCount
, ByteCount
*,
80 TextPtr
, ByteCount
, ByteCount
*,
82 ConstUnicodeMappingPtr
);
83 static void mac_initpalette(Session
*);
84 static void mac_adjustwinbg(Session
*);
85 static void mac_adjustsize(Session
*, int, int);
86 static void mac_drawgrowicon(Session
*s
);
87 static pascal void mac_growtermdraghook(void);
88 static pascal void mac_scrolltracker(ControlHandle
, short);
89 static pascal void do_text_for_device(short, short, GDHandle
, long);
90 static void text_click(Session
*, EventRecord
*);
91 static void mac_activateterm(WindowPtr
, EventRecord
*);
92 static void mac_adjusttermcursor(WindowPtr
, Point
, RgnHandle
);
93 static void mac_adjusttermmenus(WindowPtr
);
94 static void mac_updateterm(WindowPtr
);
95 static void mac_clickterm(WindowPtr
, EventRecord
*);
96 static void mac_growterm(WindowPtr
, EventRecord
*);
97 static void mac_keyterm(WindowPtr
, EventRecord
*);
98 static void mac_menuterm(WindowPtr
, short, short);
99 static void mac_closeterm(WindowPtr
);
101 void pre_paint(Session
*s
);
102 void post_paint(Session
*s
);
104 void mac_startsession(Session
*s
)
113 * Select protocol. This is farmed out into a table in a
114 * separate file to enable an ssh-free variant.
117 for (i
= 0; backends
[i
].backend
!= NULL
; i
++)
118 if (backends
[i
].protocol
== s
->cfg
.protocol
) {
119 s
->back
= backends
[i
].backend
;
123 fatalbox("Unsupported protocol number found");
125 /* XXX: Own storage management? */
127 s
->window
= GetNewCWindow(wTerminal
, NULL
, (WindowPtr
)-1);
129 s
->window
= GetNewWindow(wTerminal
, NULL
, (WindowPtr
)-1);
131 memset(wi
, 0, sizeof(*wi
));
133 wi
->wtype
= wTerminal
;
134 wi
->activate
= &mac_activateterm
;
135 wi
->adjustcursor
= &mac_adjusttermcursor
;
136 wi
->adjustmenus
= &mac_adjusttermmenus
;
137 wi
->update
= &mac_updateterm
;
138 wi
->click
= &mac_clickterm
;
139 wi
->grow
= &mac_growterm
;
140 wi
->key
= &mac_keyterm
;
141 wi
->menu
= &mac_menuterm
;
142 wi
->close
= &mac_closeterm
;
143 SetWRefCon(s
->window
, (long)wi
);
144 s
->scrollbar
= GetNewControl(cVScroll
, s
->window
);
145 s
->term
= term_init(&s
->cfg
, &s
->ucsdata
, s
);
149 if (HAVE_COLOR_QD()) {
150 /* Set to FALSE to not get palette updates in the background. */
151 SetPalette(s
->window
, s
->palette
, TRUE
);
152 ActivatePalette(s
->window
);
155 s
->logctx
= log_init(s
->term
, &s
->cfg
);
156 term_provide_logctx(s
->term
, s
->logctx
);
158 errmsg
= s
->back
->init(s
, &s
->backhandle
, &s
->cfg
, s
->cfg
.host
,
159 s
->cfg
.port
, &s
->realhost
, s
->cfg
.tcp_nodelay
,
160 s
->cfg
.tcp_keepalives
);
162 fatalbox("%s", errmsg
);
163 s
->back
->provide_logctx(s
->backhandle
, s
->logctx
);
164 set_title(s
, s
->realhost
);
166 term_provide_resize_fn(s
->term
, s
->back
->size
, s
->backhandle
);
168 mac_adjustsize(s
, s
->cfg
.height
, s
->cfg
.width
);
169 term_size(s
->term
, s
->cfg
.height
, s
->cfg
.width
, s
->cfg
.savelines
);
171 s
->ldisc
= ldisc_create(&s
->cfg
, s
->term
, s
->back
, s
->backhandle
, s
);
172 ldisc_send(s
->ldisc
, NULL
, 0, 0);/* cause ldisc to notice changes */
174 ShowWindow(s
->window
);
178 s
->next
->prev
= &s
->next
;
183 * Try to work out a horizontal scaling factor for the current font
184 * that will give a chracter width of wantwidth. Return it in numer
185 * and denom (suitable for passing to StdText()).
187 static void mac_workoutfontscale(Session
*s
, int wantwidth
,
188 Point
*numerp
, Point
*denomp
)
190 Point numer
, denom
, tmpnumer
, tmpdenom
;
192 const char text
= 'W';
194 #if TARGET_API_MAC_CARBON
195 CQDProcsPtr gp
= GetPortGrafProcs(GetWindowPort(s
->window
));
197 QDProcsPtr gp
= s
->window
->grafProcs
;
200 numer
.v
= denom
.v
= 1; /* always */
201 numer
.h
= denom
.h
= 1;
202 for (i
= 0; i
< 3; i
++) {
206 gotwidth
= InvokeQDTxMeasUPP(1, &text
, &tmpnumer
, &tmpdenom
, &fi
,
209 gotwidth
= StdTxMeas(1, &text
, &tmpnumer
, &tmpdenom
, &fi
);
210 /* The result of StdTxMeas must be scaled by the factors it returns. */
211 gotwidth
= FixRound(FixMul(gotwidth
<< 16,
212 FixRatio(tmpnumer
.h
, tmpdenom
.h
)));
213 if (gotwidth
== wantwidth
)
215 numer
.h
*= wantwidth
;
222 static UnicodeToTextFallbackUPP uni_to_font_fallback_upp
;
224 static void mac_initfont(Session
*s
)
230 SetPort((GrafPtr
)GetWindowPort(s
->window
));
231 GetFNum(s
->cfg
.font
.name
, &s
->fontnum
);
232 TextFont(s
->fontnum
);
233 TextFace(s
->cfg
.font
.face
);
234 TextSize(s
->cfg
.font
.size
);
236 s
->font_width
= CharWidth('W'); /* Well, it's what NCSA uses. */
237 s
->font_ascent
= fi
.ascent
;
238 s
->font_leading
= fi
.leading
;
239 s
->font_height
= s
->font_ascent
+ fi
.descent
+ s
->font_leading
;
240 mac_workoutfontscale(s
, s
->font_width
,
241 &s
->font_stdnumer
, &s
->font_stddenom
);
242 mac_workoutfontscale(s
, s
->font_width
* 2,
243 &s
->font_widenumer
, &s
->font_widedenom
);
244 TextSize(s
->cfg
.font
.size
* 2);
245 mac_workoutfontscale(s
, s
->font_width
* 2,
246 &s
->font_bignumer
, &s
->font_bigdenom
);
247 TextSize(s
->cfg
.font
.size
);
248 if (!s
->cfg
.bold_colour
) {
250 s
->font_boldadjust
= s
->font_width
- CharWidth('W');
252 s
->font_boldadjust
= 0;
254 if (s
->uni_to_font
!= NULL
)
255 DisposeUnicodeToTextInfo(&s
->uni_to_font
);
256 if (mac_gestalts
.encvvers
!= 0 &&
257 UpgradeScriptInfoToTextEncoding(kTextScriptDontCare
,
258 kTextLanguageDontCare
,
259 kTextRegionDontCare
, s
->cfg
.font
.name
,
261 CreateUnicodeToTextInfoByEncoding(enc
, &s
->uni_to_font
) == noErr
) {
262 if (uni_to_font_fallback_upp
== NULL
)
263 uni_to_font_fallback_upp
=
264 NewUnicodeToTextFallbackUPP(&uni_to_font_fallback
);
265 fbflags
= kUnicodeFallbackCustomOnly
;
266 if (mac_gestalts
.uncvattr
& kTECAddFallbackInterruptMask
)
267 fbflags
|= kUnicodeFallbackInterruptSafeMask
;
268 if (SetFallbackUnicodeToText(s
->uni_to_font
,
269 uni_to_font_fallback_upp
, fbflags
, NULL
) != noErr
) {
270 DisposeUnicodeToTextInfo(&s
->uni_to_font
);
277 s
->uni_to_font
= NULL
;
278 p2cstrcpy(cfontname
, s
->cfg
.font
.name
);
280 charset_from_macenc(FontToScript(s
->fontnum
),
281 GetScriptManagerVariable(smRegionCode
),
282 mac_gestalts
.sysvers
, cfontname
);
285 mac_adjustsize(s
, s
->term
->rows
, s
->term
->cols
);
288 static pascal OSStatus
uni_to_font_fallback(UniChar
*ucp
,
289 ByteCount ilen
, ByteCount
*iusedp
, TextPtr obuf
, ByteCount olen
,
290 ByteCount
*ousedp
, LogicalAddress cookie
, ConstUnicodeMappingPtr mapping
)
294 return kTECOutputBufferFullStatus
;
296 * What I'd _like_ to do here is to somehow generate the
297 * missing-character glyph that every font is required to have.
298 * Unfortunately (and somewhat surprisingly), I can't find any way
299 * to actually ask for it explicitly. Bah.
308 * To be called whenever the window size changes.
309 * rows and cols should be desired values.
310 * It's assumed the terminal emulator will be informed, and will set rows
313 static void mac_adjustsize(Session
*s
, int newrows
, int newcols
) {
314 int winwidth
, winheight
;
317 extraforscroll
=s
->cfg
.scrollbar ?
15 : 0;
318 winwidth
= newcols
* s
->font_width
+ extraforscroll
;
319 winheight
= newrows
* s
->font_height
;
320 SizeWindow(s
->window
, winwidth
, winheight
, true);
321 if (s
->cfg
.scrollbar
) {
322 HideControl(s
->scrollbar
);
323 MoveControl(s
->scrollbar
, winwidth
- extraforscroll
, -1);
324 SizeControl(s
->scrollbar
, extraforscroll
+ 1, winheight
- 13);
325 ShowControl(s
->scrollbar
);
330 static void mac_initpalette(Session
*s
)
333 if (!HAVE_COLOR_QD())
336 * Most colours should be inhibited on 2bpp displays.
337 * Palette manager documentation suggests inhibiting all tolerant colours
338 * on greyscale displays.
340 #define PM_NORMAL ( pmTolerant | pmInhibitC2 | \
341 pmInhibitG2 | pmInhibitG4 | pmInhibitG8 )
342 #define PM_TOLERANCE 0x2000
343 s
->palette
= NewPalette(262, NULL
, PM_NORMAL
, PM_TOLERANCE
);
344 if (s
->palette
== NULL
)
345 fatalbox("Unable to create palette");
346 /* In 2bpp, these are the colours we want most. */
347 SetEntryUsage(s
->palette
, DEFAULT_BG
,
348 PM_NORMAL
&~ pmInhibitC2
, PM_TOLERANCE
);
349 SetEntryUsage(s
->palette
, DEFAULT_FG
,
350 PM_NORMAL
&~ pmInhibitC2
, PM_TOLERANCE
);
351 SetEntryUsage(s
->palette
, DEFAULT_FG_BOLD
,
352 PM_NORMAL
&~ pmInhibitC2
, PM_TOLERANCE
);
353 SetEntryUsage(s
->palette
, CURSOR_BG
,
354 PM_NORMAL
&~ pmInhibitC2
, PM_TOLERANCE
);
359 * Set the background colour of the window correctly. Should be
360 * called whenever the default background changes.
362 static void mac_adjustwinbg(Session
*s
)
365 if (!HAVE_COLOR_QD())
368 if (mac_gestalts
.windattr
& gestaltWindowMgrPresent
)
369 SetWindowContentColor(s
->window
,
370 &(*s
->palette
)->pmInfo
[DEFAULT_BG
].ciRGB
);
374 #if !TARGET_API_MAC_CARBON
375 if (s
->wctab
== NULL
)
376 s
->wctab
= (WCTabHandle
)NewHandle(sizeof(**s
->wctab
));
377 if (s
->wctab
== NULL
)
378 return; /* do without */
379 (*s
->wctab
)->wCSeed
= 0;
380 (*s
->wctab
)->wCReserved
= 0;
381 (*s
->wctab
)->ctSize
= 0;
382 (*s
->wctab
)->ctTable
[0].value
= wContentColor
;
383 (*s
->wctab
)->ctTable
[0].rgb
= (*s
->palette
)->pmInfo
[DEFAULT_BG
].ciRGB
;
384 SetWinColor(s
->window
, s
->wctab
);
390 * Set the cursor shape correctly
392 static void mac_adjusttermcursor(WindowPtr window
, Point mouse
,
396 ControlHandle control
;
399 #if TARGET_API_MAC_CARBON
405 SetPort((GrafPtr
)GetWindowPort(window
));
406 s
= mac_windowsession(window
);
407 GlobalToLocal(&mouse
);
408 part
= FindControl(mouse
, window
, &control
);
409 if (control
== s
->scrollbar
) {
410 #if TARGET_API_MAC_CARBON
411 SetCursor(GetQDGlobalsArrow(&arrow
));
412 RectRgn(cursrgn
, GetControlBounds(s
->scrollbar
, &rect
));
414 SetCursor(&qd
.arrow
);
415 RectRgn(cursrgn
, &(*s
->scrollbar
)->contrlRect
);
418 x
= mouse
.h
/ s
->font_width
;
419 y
= mouse
.v
/ s
->font_height
;
421 #if TARGET_API_MAC_CARBON
422 SetCursor(GetQDGlobalsArrow(&arrow
));
424 SetCursor(&qd
.arrow
);
427 SetCursor(*GetCursor(iBeamCursor
));
428 /* Ask for shape changes if we leave this character cell. */
429 SetRectRgn(cursrgn
, x
* s
->font_width
, y
* s
->font_height
,
430 (x
+ 1) * s
->font_width
, (y
+ 1) * s
->font_height
);
432 #if TARGET_API_MAC_CARBON
434 GetPortVisibleRegion(GetWindowPort(window
), visrgn
);
435 SectRgn(cursrgn
, visrgn
, cursrgn
);
438 SectRgn(cursrgn
, window
->visRgn
, cursrgn
);
443 * Enable/disable menu items based on the active terminal window.
445 #if TARGET_API_MAC_CARBON
446 #define DisableItem DisableMenuItem
447 #define EnableItem EnableMenuItem
449 static void mac_adjusttermmenus(WindowPtr window
)
453 #if !TARGET_API_MAC_CARBON
457 s
= mac_windowsession(window
);
458 menu
= GetMenuHandle(mFile
);
459 DisableItem(menu
, iSave
); /* XXX enable if modified */
460 EnableItem(menu
, iSaveAs
);
461 EnableItem(menu
, iChange
);
462 EnableItem(menu
, iDuplicate
);
463 menu
= GetMenuHandle(mEdit
);
465 DisableItem(menu
, iUndo
);
466 DisableItem(menu
, iCut
);
467 if (1/*s->term->selstate == SELECTED*/)
468 EnableItem(menu
, iCopy
);
470 DisableItem(menu
, iCopy
);
471 #if TARGET_API_MAC_CARBON
474 if (GetScrap(NULL
, kScrapFlavorTypeText
, &offset
) == noTypeErr
)
476 DisableItem(menu
, iPaste
);
478 EnableItem(menu
, iPaste
);
479 DisableItem(menu
, iClear
);
480 EnableItem(menu
, iSelectAll
);
481 menu
= GetMenuHandle(mWindow
);
483 EnableItem(menu
, iShowEventLog
);
486 static void mac_menuterm(WindowPtr window
, short menu
, short item
)
490 s
= mac_windowsession(window
);
498 term_do_paste(s
->term
);
512 static void mac_clickterm(WindowPtr window
, EventRecord
*event
)
516 ControlHandle control
;
518 static ControlActionUPP mac_scrolltracker_upp
= NULL
;
520 s
= mac_windowsession(window
);
521 SetPort((GrafPtr
)GetWindowPort(window
));
522 mouse
= event
->where
;
523 GlobalToLocal(&mouse
);
524 part
= FindControl(mouse
, window
, &control
);
525 if (control
== s
->scrollbar
) {
527 case kControlIndicatorPart
:
528 if (TrackControl(control
, mouse
, NULL
) == kControlIndicatorPart
)
529 term_scroll(s
->term
, +1, GetControlValue(control
));
531 case kControlUpButtonPart
:
532 case kControlDownButtonPart
:
533 case kControlPageUpPart
:
534 case kControlPageDownPart
:
535 if (mac_scrolltracker_upp
== NULL
)
536 mac_scrolltracker_upp
=
537 NewControlActionUPP(&mac_scrolltracker
);
538 TrackControl(control
, mouse
, mac_scrolltracker_upp
);
542 text_click(s
, event
);
546 static void text_click(Session
*s
, EventRecord
*event
)
550 static UInt32 lastwhen
= 0;
551 static Session
*lastsess
= NULL
;
552 static int lastrow
= -1, lastcol
= -1;
553 static Mouse_Action lastact
= MA_NOTHING
;
555 SetPort((GrafPtr
)GetWindowPort(s
->window
));
556 localwhere
= event
->where
;
557 GlobalToLocal(&localwhere
);
559 col
= PTOCC(localwhere
.h
);
560 row
= PTOCR(localwhere
.v
);
561 if (event
->when
- lastwhen
< GetDblTime() &&
562 row
== lastrow
&& col
== lastcol
&& s
== lastsess
)
563 lastact
= (lastact
== MA_CLICK ? MA_2CLK
:
564 lastact
== MA_2CLK ? MA_3CLK
:
565 lastact
== MA_3CLK ? MA_CLICK
: MA_NOTHING
);
568 term_mouse(s
->term
, MBT_LEFT
,
569 event
->modifiers
& shiftKey ? MBT_EXTEND
: MBT_SELECT
,
570 lastact
, col
, row
, event
->modifiers
& shiftKey
,
571 event
->modifiers
& controlKey
, event
->modifiers
& optionKey
);
575 while (StillDown()) {
576 GetMouse(&localwhere
);
577 col
= PTOCC(localwhere
.h
);
578 row
= PTOCR(localwhere
.v
);
579 term_mouse(s
->term
, MBT_LEFT
,
580 event
->modifiers
& shiftKey ? MBT_EXTEND
: MBT_SELECT
,
581 MA_DRAG
, col
, row
, event
->modifiers
& shiftKey
,
582 event
->modifiers
& controlKey
,
583 event
->modifiers
& optionKey
);
584 if (row
> s
->term
->rows
- 1)
585 term_scroll(s
->term
, 0, row
- (s
->term
->rows
- 1));
587 term_scroll(s
->term
, 0, row
);
589 term_mouse(s
->term
, MBT_LEFT
,
590 event
->modifiers
& shiftKey ? MBT_EXTEND
: MBT_SELECT
,
591 MA_RELEASE
, col
, row
, event
->modifiers
& shiftKey
,
592 event
->modifiers
& controlKey
, event
->modifiers
& optionKey
);
593 lastwhen
= TickCount();
596 void write_clip(void *cookie
, wchar_t *data
, int len
, int must_deselect
)
598 #if !TARGET_API_MAC_CARBON
601 ByteCount iread
, olen
;
609 * See "Programming with the Text Encoding Conversion Manager"
610 * Appendix E for Unicode scrap conventions.
612 * XXX Maybe PICT scrap too.
614 if (ZeroScrap() != noErr
)
616 PutScrap(len
* sizeof(*data
), kScrapFlavorTypeUnicode
, data
);
618 /* Replace LINE SEPARATORs with CR for TEXT output. */
619 for (i
= 0; i
< len
; i
++)
620 if (data
[i
] == 0x2028)
623 mactextbuf
= snewn(len
, char); /* XXX DBCS */
624 if (s
->uni_to_font
!= NULL
) {
625 err
= ConvertFromUnicodeToText(s
->uni_to_font
, len
* sizeof(UniChar
),
627 kUnicodeUseFallbacksMask
,
629 len
, &iread
, &olen
, mactextbuf
);
630 if (err
!= noErr
&& err
!= kTECUsedFallbacksStatus
)
632 } else if (s
->font_charset
!= CS_NONE
) {
634 olen
= charset_from_unicode(&unitextptr
, &len
, mactextbuf
, 1024,
635 s
->font_charset
, NULL
, ".", 1);
638 PutScrap(olen
, kScrapFlavorTypeText
, mactextbuf
);
641 stsz
= offsetof(StScrpRec
, scrpStyleTab
) + sizeof(ScrpSTElement
);
642 stsc
= smalloc(stsz
);
643 stsc
->scrpNStyles
= 1;
644 stsc
->scrpStyleTab
[0].scrpStartChar
= 0;
645 stsc
->scrpStyleTab
[0].scrpHeight
= s
->font_height
;
646 stsc
->scrpStyleTab
[0].scrpAscent
= s
->font_ascent
;
647 stsc
->scrpStyleTab
[0].scrpFont
= s
->fontnum
;
648 stsc
->scrpStyleTab
[0].scrpFace
= 0;
649 stsc
->scrpStyleTab
[0].scrpSize
= s
->cfg
.font
.size
;
650 stsc
->scrpStyleTab
[0].scrpColor
.red
= 0;
651 stsc
->scrpStyleTab
[0].scrpColor
.green
= 0;
652 stsc
->scrpStyleTab
[0].scrpColor
.blue
= 0;
653 PutScrap(stsz
, kScrapFlavorTypeTextStyle
, stsc
);
658 void get_clip(void *frontend
, wchar_t **p
, int *lenp
)
660 #if TARGET_API_MAC_CARBON
663 Session
*s
= frontend
;
664 static Handle h
= NULL
;
665 static wchar_t *data
= NULL
;
670 TextToUnicodeInfo scrap_to_uni
;
671 ByteCount iread
, olen
;
685 if (GetScrap(NULL
, kScrapFlavorTypeUnicode
, &offset
) > 0) {
689 GetScrap(h
, kScrapFlavorTypeUnicode
, &offset
) / sizeof(**p
);
692 } else if (GetScrap(NULL
, kScrapFlavorTypeText
, &offset
) > 0) {
693 texth
= NewHandle(0);
694 textlen
= GetScrap(texth
, kScrapFlavorTypeText
, &offset
);
696 data
= snewn(textlen
, wchar_t);
697 /* XXX should use 'styl' scrap if it's there. */
698 if (mac_gestalts
.encvvers
!= 0 &&
699 UpgradeScriptInfoToTextEncoding(smSystemScript
,
700 kTextLanguageDontCare
,
701 kTextRegionDontCare
, NULL
,
703 CreateTextToUnicodeInfoByEncoding(enc
, &scrap_to_uni
) ==
705 err
= ConvertFromTextToUnicode(scrap_to_uni
, textlen
,
706 *texth
, 0, 0, NULL
, NULL
, NULL
,
708 &iread
, &olen
, data
);
709 DisposeTextToUnicodeInfo(&scrap_to_uni
);
712 *lenp
= olen
/ sizeof(**p
);
719 charset_from_macenc(GetScriptManagerVariable(smSysScript
),
720 GetScriptManagerVariable(smRegionCode
),
721 mac_gestalts
.sysvers
, NULL
);
722 if (charset
!= CS_NONE
) {
724 *lenp
= charset_to_unicode(&tptr
, &textlen
, data
,
725 textlen
* 2, charset
, NULL
,
730 DisposeHandle(texth
);
739 static pascal void mac_scrolltracker(ControlHandle control
, short part
)
743 #if TARGET_API_MAC_CARBON
744 s
= mac_windowsession(GetControlOwner(control
));
746 s
= mac_windowsession((*control
)->contrlOwner
);
749 case kControlUpButtonPart
:
750 term_scroll(s
->term
, 0, -1);
752 case kControlDownButtonPart
:
753 term_scroll(s
->term
, 0, +1);
755 case kControlPageUpPart
:
756 term_scroll(s
->term
, 0, -(s
->term
->rows
- 1));
758 case kControlPageDownPart
:
759 term_scroll(s
->term
, 0, +(s
->term
->rows
- 1));
764 static void mac_keyterm(WindowPtr window
, EventRecord
*event
)
766 Session
*s
= mac_windowsession(window
);
767 Key_Sym keysym
= PK_NULL
;
768 unsigned int mods
= 0, flags
= PKF_NUMLOCK
;
772 ScriptCode key_script
;
777 fprintf(stderr
, "Got key event %08x\n", event
->message
);
780 /* No meta key yet -- that'll be rather fun. */
782 /* Keys that we handle locally */
783 if (event
->modifiers
& shiftKey
) {
784 switch ((event
->message
& keyCodeMask
) >> 8) {
785 case 0x74: /* shift-pageup */
786 term_scroll(s
->term
, 0, -(s
->term
->rows
- 1));
788 case 0x79: /* shift-pagedown */
789 term_scroll(s
->term
, 0, +(s
->term
->rows
- 1));
794 if (event
->modifiers
& shiftKey
)
796 if (event
->modifiers
& controlKey
)
798 if (event
->what
== autoKey
)
801 /* Mac key events consist of a virtual key code and a character code. */
803 switch ((event
->message
& keyCodeMask
) >> 8) {
804 case 0x24: keysym
= PK_RETURN
; break;
805 case 0x30: keysym
= PK_TAB
; break;
806 case 0x33: keysym
= PK_BACKSPACE
; break;
807 case 0x35: keysym
= PK_ESCAPE
; break;
809 case 0x7A: keysym
= PK_F1
; break;
810 case 0x78: keysym
= PK_F2
; break;
811 case 0x63: keysym
= PK_F3
; break;
812 case 0x76: keysym
= PK_F4
; break;
813 case 0x60: keysym
= PK_F5
; break;
814 case 0x61: keysym
= PK_F6
; break;
815 case 0x62: keysym
= PK_F7
; break;
816 case 0x64: keysym
= PK_F8
; break;
817 case 0x65: keysym
= PK_F9
; break;
818 case 0x6D: keysym
= PK_F10
; break;
819 case 0x67: keysym
= PK_F11
; break;
820 case 0x6F: keysym
= PK_F12
; break;
821 case 0x69: keysym
= PK_F13
; break;
822 case 0x6B: keysym
= PK_F14
; break;
823 case 0x71: keysym
= PK_F15
; break;
825 case 0x72: keysym
= PK_INSERT
; break;
826 case 0x73: keysym
= PK_HOME
; break;
827 case 0x74: keysym
= PK_PAGEUP
; break;
828 case 0x75: keysym
= PK_DELETE
; break;
829 case 0x77: keysym
= PK_END
; break;
830 case 0x79: keysym
= PK_PAGEDOWN
; break;
832 case 0x47: keysym
= PK_PF1
; break;
833 case 0x51: keysym
= PK_PF2
; break;
834 case 0x4B: keysym
= PK_PF3
; break;
835 case 0x43: keysym
= PK_PF4
; break;
836 case 0x4E: keysym
= PK_KPMINUS
; break;
837 case 0x45: keysym
= PK_KPCOMMA
; break;
838 case 0x41: keysym
= PK_KPDECIMAL
; break;
839 case 0x4C: keysym
= PK_KPENTER
; break;
840 case 0x52: keysym
= PK_KP0
; break;
841 case 0x53: keysym
= PK_KP1
; break;
842 case 0x54: keysym
= PK_KP2
; break;
843 case 0x55: keysym
= PK_KP3
; break;
844 case 0x56: keysym
= PK_KP4
; break;
845 case 0x57: keysym
= PK_KP5
; break;
846 case 0x58: keysym
= PK_KP6
; break;
847 case 0x59: keysym
= PK_KP7
; break;
848 case 0x5B: keysym
= PK_KP8
; break;
849 case 0x5C: keysym
= PK_KP9
; break;
851 case 0x7B: keysym
= PK_LEFT
; break;
852 case 0x7C: keysym
= PK_RIGHT
; break;
853 case 0x7D: keysym
= PK_DOWN
; break;
854 case 0x7E: keysym
= PK_UP
; break;
857 /* Map from key script to Unicode. */
858 txt
[0] = event
->message
& charCodeMask
;
859 key_script
= GetScriptManagerVariable(smKeyScript
);
861 if (mac_gestalts
.encvvers
!= 0) {
862 static TextToUnicodeInfo key_to_uni
= NULL
;
863 static ScriptCode key_to_uni_script
;
865 ByteCount iread
, olen
;
868 if (key_to_uni
!= NULL
&& key_to_uni_script
!= key_script
)
869 DisposeTextToUnicodeInfo(&key_to_uni
);
870 if (key_to_uni
== NULL
|| key_to_uni_script
!= key_script
) {
871 if (UpgradeScriptInfoToTextEncoding(key_script
,
872 kTextLanguageDontCare
,
873 kTextRegionDontCare
, NULL
,
875 CreateTextToUnicodeInfoByEncoding(enc
, &key_to_uni
) == noErr
)
876 key_to_uni_script
= key_script
;
880 if (key_to_uni
!= NULL
) {
881 err
= ConvertFromTextToUnicode(key_to_uni
, 1, txt
,
882 (kUnicodeKeepInfoMask
|
883 kUnicodeStringUnterminatedMask
),
885 sizeof(utxt
), &iread
, &olen
, utxt
);
887 len
= olen
/ sizeof(*utxt
);
894 charset
= charset_from_macenc(key_script
,
895 GetScriptManagerVariable(smRegionCode
),
896 mac_gestalts
.sysvers
, NULL
);
897 if (charset
!= CS_NONE
) {
898 len
= charset_to_unicode(&tptr
, &tlen
, utxt
, sizeof(utxt
), charset
,
902 term_key(s
->term
, keysym
, utxt
, len
, mods
, flags
);
905 void request_paste(void *frontend
)
907 Session
*s
= frontend
;
910 * In the Mac OS, pasting is synchronous: we can read the
911 * clipboard with no difficulty, so request_paste() can just go
914 term_do_paste(s
->term
);
925 static void mac_growterm(WindowPtr window
, EventRecord
*event
)
929 int newrows
, newcols
;
931 #if !TARGET_API_MAC_CARBON
932 DragGrayRgnUPP draghooksave
;
937 s
= mac_windowsession(window
);
939 #if !TARGET_API_MAC_CARBON
940 draghooksave
= LMGetDragHook();
941 growterm_state
.oldmsg
[0] = '\0';
942 growterm_state
.zeromouse
= event
->where
;
943 growterm_state
.zeromouse
.h
-= s
->term
->cols
* s
->font_width
;
944 growterm_state
.zeromouse
.v
-= s
->term
->rows
* s
->font_height
;
945 growterm_state
.s
= s
;
948 BackColor(whiteColor
);
949 ForeColor(blackColor
);
950 TextFont(systemFont
);
954 SetRect(&growterm_state
.msgrect
, 0, 0,
955 StringWidth("\p99999x99999") + 4, fi
.ascent
+ fi
.descent
+ 4);
956 SetPt(&growterm_state
.msgorigin
, 2, fi
.ascent
+ 2);
957 LMSetDragHook(NewDragGrayRgnUPP(mac_growtermdraghook
));
960 SetRect(&limits
, s
->font_width
+ 15, s
->font_height
, SHRT_MAX
, SHRT_MAX
);
961 grow_result
= GrowWindow(window
, event
->where
, &limits
);
963 #if !TARGET_API_MAC_CARBON
964 DisposeDragGrayRgnUPP(LMGetDragHook());
965 LMSetDragHook(draghooksave
);
966 InvalRect(&growterm_state
.msgrect
);
971 if (grow_result
!= 0) {
972 newrows
= HiWord(grow_result
) / s
->font_height
;
973 newcols
= (LoWord(grow_result
) - 15) / s
->font_width
;
974 mac_adjustsize(s
, newrows
, newcols
);
975 term_size(s
->term
, newrows
, newcols
, s
->cfg
.savelines
);
976 s
->cfg
.height
=s
->term
->rows
;
977 s
->cfg
.width
=s
->term
->cols
;
981 #if !TARGET_API_MAC_CARBON
982 static pascal void mac_growtermdraghook(void)
984 Session
*s
= growterm_state
.s
;
988 unsigned char pbuf
[20];
989 int newrows
, newcols
;
992 newrows
= (mouse
.v
- growterm_state
.zeromouse
.v
) / s
->font_height
;
993 if (newrows
< 1) newrows
= 1;
994 newcols
= (mouse
.h
- growterm_state
.zeromouse
.h
) / s
->font_width
;
995 if (newcols
< 1) newcols
= 1;
996 sprintf(buf
, "%dx%d", newcols
, newrows
);
997 if (strcmp(buf
, growterm_state
.oldmsg
) == 0)
999 strcpy(growterm_state
.oldmsg
, buf
);
1000 c2pstrcpy(pbuf
, buf
);
1003 SetPort(growterm_state
.s
->window
);
1004 EraseRect(&growterm_state
.msgrect
);
1005 MoveTo(growterm_state
.msgorigin
.h
, growterm_state
.msgorigin
.v
);
1011 void mac_closeterm(WindowPtr window
)
1013 Session
*s
= mac_windowsession(window
);
1015 /* XXX warn on close */
1016 HideWindow(s
->window
);
1018 s
->next
->prev
= s
->prev
;
1019 ldisc_free(s
->ldisc
);
1020 s
->back
->free(s
->backhandle
);
1021 log_free(s
->logctx
);
1022 if (s
->uni_to_font
!= NULL
)
1023 DisposeUnicodeToTextInfo(&s
->uni_to_font
);
1025 mac_freeeventlog(s
);
1026 sfree((WinInfo
*)GetWRefCon(s
->window
));
1027 DisposeWindow(s
->window
);
1028 DisposePalette(s
->palette
);
1032 static void mac_activateterm(WindowPtr window
, EventRecord
*event
)
1035 Boolean active
= (event
->modifiers
& activeFlag
) != 0;
1037 s
= mac_windowsession(window
);
1038 term_set_focus(s
->term
, active
);
1039 term_update(s
->term
);
1040 if (active
&& s
->cfg
.scrollbar
)
1041 ShowControl(s
->scrollbar
);
1043 if (HAVE_COLOR_QD())
1044 PmBackColor(DEFAULT_BG
);/* HideControl clears behind the control */
1046 BackColor(blackColor
);
1047 HideControl(s
->scrollbar
);
1049 mac_drawgrowicon(s
);
1052 static void mac_updateterm(WindowPtr window
)
1056 #if TARGET_API_MAC_CARBON
1060 s
= mac_windowsession(window
);
1061 SetPort((GrafPtr
)GetWindowPort(window
));
1062 BeginUpdate(window
);
1064 #if TARGET_API_MAC_CARBON
1066 GetPortVisibleRegion(GetWindowPort(window
), visrgn
);
1067 GetRegionBounds(visrgn
, &bbox
);
1069 bbox
= (*window
->visRgn
)->rgnBBox
;
1071 term_paint(s
->term
, s
, PTOCC(bbox
.left
), PTOCR(bbox
.top
),
1072 PTOCC(bbox
.right
), PTOCR(bbox
.bottom
), 1);
1073 /* Restore default colours in case the Window Manager uses them */
1074 if (HAVE_COLOR_QD()) {
1075 PmForeColor(DEFAULT_FG
);
1076 PmBackColor(DEFAULT_BG
);
1078 ForeColor(whiteColor
);
1079 BackColor(blackColor
);
1081 if (FrontWindow() != window
)
1082 #if TARGET_API_MAC_CARBON
1083 EraseRect(GetControlBounds(s
->scrollbar
, &bbox
));
1084 UpdateControls(window
, visrgn
);
1087 EraseRect(&(*s
->scrollbar
)->contrlRect
);
1088 UpdateControls(window
, window
->visRgn
);
1090 mac_drawgrowicon(s
);
1095 static void mac_drawgrowicon(Session
*s
)
1100 SetPort((GrafPtr
)GetWindowPort(s
->window
));
1102 * Stop DrawGrowIcon giving us space for a horizontal scrollbar
1103 * See Tech Note TB575 for details.
1105 #if TARGET_API_MAC_CARBON
1106 GetPortBounds(GetWindowPort(s
->window
), &clip
);
1108 clip
= s
->window
->portRect
;
1110 clip
.left
= clip
.right
- 15;
1114 DrawGrowIcon(s
->window
);
1116 DisposeRgn(savergn
);
1119 struct do_text_args
{
1130 * Call from the terminal emulator to draw a bit of text
1132 * x and y are text row and column (zero-based)
1134 void do_text(Context ctx
, int x
, int y
, wchar_t *text
, int len
,
1135 unsigned long attr
, int lattr
)
1139 struct do_text_args a
;
1140 RgnHandle textrgn
, saveclip
;
1141 #if TARGET_API_MAC_CARBON
1144 char mactextbuf
[1024];
1145 wchar_t *unitextptr
;
1147 ByteCount iread
, olen
;
1149 static DeviceLoopDrawingUPP do_text_for_device_upp
= NULL
;
1151 assert(len
<= 1024);
1153 /* SGT, 2004-10-14: I don't know how to support combining characters
1154 * on the Mac. Hopefully the first person to fail this assertion will
1155 * know how to do it better than me... */
1156 assert(!(attr
& TATTR_COMBINING
));
1158 SetPort((GrafPtr
)GetWindowPort(s
->window
));
1160 fontwidth
= s
->font_width
;
1161 if ((lattr
& LATTR_MODE
) != LATTR_NORM
)
1164 /* First check this text is relevant */
1165 a
.textrect
.top
= y
* s
->font_height
;
1166 a
.textrect
.bottom
= (y
+ 1) * s
->font_height
;
1167 a
.textrect
.left
= x
* fontwidth
;
1168 a
.textrect
.right
= (x
+ len
) * fontwidth
;
1169 if (a
.textrect
.right
> s
->term
->cols
* s
->font_width
)
1170 a
.textrect
.right
= s
->term
->cols
* s
->font_width
;
1171 #if TARGET_API_MAC_CARBON
1173 GetPortVisibleRegion(GetWindowPort(s
->window
), visrgn
);
1174 if (!RectInRgn(&a
.textrect
, visrgn
)) {
1180 if (!RectInRgn(&a
.textrect
, s
->window
->visRgn
))
1184 if (s
->uni_to_font
!= NULL
) {
1185 err
= ConvertFromUnicodeToText(s
->uni_to_font
, len
* sizeof(UniChar
),
1186 text
, kUnicodeUseFallbacksMask
,
1187 0, NULL
, NULL
, NULL
,
1188 1024, &iread
, &olen
, mactextbuf
);
1189 if (err
!= noErr
&& err
!= kTECUsedFallbacksStatus
)
1191 } else if (s
->font_charset
!= CS_NONE
) {
1192 /* XXX this is bogus if wchar_t and UniChar are different sizes. */
1193 unitextptr
= (wchar_t *)text
;
1194 olen
= charset_from_unicode(&unitextptr
, &len
, mactextbuf
, 1024,
1195 s
->font_charset
, NULL
, ".", 1);
1200 a
.text
= mactextbuf
;
1204 switch (lattr
& LATTR_MODE
) {
1206 TextSize(s
->cfg
.font
.size
);
1207 a
.numer
= s
->font_stdnumer
;
1208 a
.denom
= s
->font_stddenom
;
1211 TextSize(s
->cfg
.font
.size
);
1212 a
.numer
= s
->font_widenumer
;
1213 a
.denom
= s
->font_widedenom
;
1217 TextSize(s
->cfg
.font
.size
* 2);
1218 a
.numer
= s
->font_bignumer
;
1219 a
.denom
= s
->font_bigdenom
;
1222 SetPort((GrafPtr
)GetWindowPort(s
->window
));
1223 TextFont(s
->fontnum
);
1224 style
= s
->cfg
.font
.face
;
1225 if ((attr
& ATTR_BOLD
) && !s
->cfg
.bold_colour
)
1227 if (attr
& ATTR_UNDER
)
1231 if (HAVE_COLOR_QD())
1233 SpaceExtra(s
->font_boldadjust
<< 16);
1234 CharExtra(s
->font_boldadjust
<< 16);
1239 saveclip
= NewRgn();
1241 ClipRect(&a
.textrect
);
1243 RectRgn(textrgn
, &a
.textrect
);
1244 if (HAVE_COLOR_QD()) {
1245 if (do_text_for_device_upp
== NULL
)
1246 do_text_for_device_upp
=
1247 NewDeviceLoopDrawingUPP(&do_text_for_device
);
1248 DeviceLoop(textrgn
, do_text_for_device_upp
, (long)&a
, 0);
1250 do_text_for_device(1, 0, NULL
, (long)&a
);
1252 DisposeRgn(saveclip
);
1253 DisposeRgn(textrgn
);
1254 /* Tell the window manager about it in case this isn't an update */
1255 #if TARGET_API_MAC_CARBON
1256 ValidWindowRect(s
->window
, &a
.textrect
);
1258 ValidRect(&a
.textrect
);
1262 static pascal void do_text_for_device(short depth
, short devflags
,
1263 GDHandle device
, long cookie
)
1265 struct do_text_args
*a
= (struct do_text_args
*)cookie
;
1266 int bgcolour
, fgcolour
, bright
, reverse
, tmp
;
1267 #if TARGET_API_MAC_CARBON
1268 CQDProcsPtr gp
= GetPortGrafProcs(GetWindowPort(a
->s
->window
));
1270 QDProcsPtr gp
= a
->s
->window
->grafProcs
;
1273 bright
= (a
->attr
& ATTR_BOLD
) && a
->s
->cfg
.bold_colour
;
1274 reverse
= a
->attr
& ATTR_REVERSE
;
1276 if (depth
== 1 && (a
->attr
& TATTR_ACTCURS
))
1279 if (HAVE_COLOR_QD()) {
1281 fgcolour
= ((a
->attr
& ATTR_FGMASK
) >> ATTR_FGSHIFT
);
1282 bgcolour
= ((a
->attr
& ATTR_BGMASK
) >> ATTR_BGSHIFT
);
1285 * NB: bold reverse in 2bpp breaks with the usual PuTTY model and
1286 * boldens the background, because that's all we can do.
1288 fgcolour
= bright ? DEFAULT_FG_BOLD
: DEFAULT_FG
;
1289 bgcolour
= DEFAULT_BG
;
1293 fgcolour
= bgcolour
;
1296 if (bright
&& depth
> 2)
1297 if (fgcolour
< 16) fgcolour
|=8;
1298 else if (fgcolour
>= 256) fgcolour
|=1;
1299 if ((a
->attr
& TATTR_ACTCURS
) && depth
> 1) {
1300 fgcolour
= CURSOR_FG
;
1301 bgcolour
= CURSOR_BG
;
1303 PmForeColor(fgcolour
);
1304 PmBackColor(bgcolour
);
1305 } else { /* No Color Quickdraw */
1306 /* XXX This should be done with a _little_ more configurability */
1308 ForeColor(blackColor
);
1309 BackColor(whiteColor
);
1311 ForeColor(whiteColor
);
1312 BackColor(blackColor
);
1316 EraseRect(&a
->textrect
);
1317 switch (a
->lattr
& LATTR_MODE
) {
1320 MoveTo(a
->textrect
.left
, a
->textrect
.top
+ a
->s
->font_ascent
);
1323 MoveTo(a
->textrect
.left
, a
->textrect
.top
+ a
->s
->font_ascent
* 2);
1326 MoveTo(a
->textrect
.left
,
1327 a
->textrect
.top
- a
->s
->font_height
+ a
->s
->font_ascent
* 2);
1330 /* FIXME: Sort out bold width adjustments on Original QuickDraw. */
1332 InvokeQDTextUPP(a
->len
, a
->text
, a
->numer
, a
->denom
, gp
->textProc
);
1334 StdText(a
->len
, a
->text
, a
->numer
, a
->denom
);
1336 if (a
->attr
& TATTR_PASCURS
) {
1343 PmForeColor(CURSOR_BG
);
1346 FrameRect(&a
->textrect
);
1350 void do_cursor(Context ctx
, int x
, int y
, wchar_t *text
, int len
,
1351 unsigned long attr
, int lattr
)
1354 do_text(ctx
, x
, y
, text
, len
, attr
, lattr
);
1358 * Call from the terminal emulator to get its graphics context.
1359 * Should probably be called start_redraw or something.
1361 void pre_paint(Session
*s
)
1364 Rect myrect
, tmprect
;
1365 #if TARGET_API_MAC_CARBON
1369 if (HAVE_COLOR_QD()) {
1370 s
->term
->attr_mask
= 0;
1371 SetPort((GrafPtr
)GetWindowPort(s
->window
));
1372 #if TARGET_API_MAC_CARBON
1374 GetPortVisibleRegion(GetWindowPort(s
->window
), visrgn
);
1375 GetRegionBounds(visrgn
, &myrect
);
1378 myrect
= (*s
->window
->visRgn
)->rgnBBox
;
1380 LocalToGlobal((Point
*)&myrect
.top
);
1381 LocalToGlobal((Point
*)&myrect
.bottom
);
1382 for (gdh
= GetDeviceList();
1384 gdh
= GetNextDevice(gdh
)) {
1385 if (TestDeviceAttribute(gdh
, screenDevice
) &&
1386 TestDeviceAttribute(gdh
, screenActive
) &&
1387 SectRect(&(*gdh
)->gdRect
, &myrect
, &tmprect
)) {
1388 switch ((*(*gdh
)->gdPMap
)->pixelSize
) {
1390 if (s
->cfg
.bold_colour
)
1391 s
->term
->attr_mask
|= ~(ATTR_COLOURS
|
1392 (s
->cfg
.bold_colour ? ATTR_BOLD
: 0));
1395 s
->term
->attr_mask
|= ~ATTR_COLOURS
;
1398 s
->term
->attr_mask
= ~0;
1399 return; /* No point checking more screens. */
1404 s
->term
->attr_mask
= ~(ATTR_COLOURS
|
1405 (s
->cfg
.bold_colour ? ATTR_BOLD
: 0));
1408 Context
get_ctx(void *frontend
)
1410 Session
*s
= frontend
;
1416 void free_ctx(Context ctx
)
1422 * Presumably this does something in Windows
1424 void post_paint(Session
*s
)
1430 * Set the scroll bar position
1432 * total is the line number of the bottom of the working screen
1433 * start is the line number of the top of the display
1434 * page is the length of the displayed page
1436 void set_sbar(void *frontend
, int total
, int start
, int page
)
1438 Session
*s
= frontend
;
1440 /* We don't redraw until we've set everything up, to avoid glitches */
1441 SetControlMinimum(s
->scrollbar
, 0);
1442 SetControlMaximum(s
->scrollbar
, total
- page
);
1443 SetControlValue(s
->scrollbar
, start
);
1445 if (mac_gestalts
.cntlattr
& gestaltControlMgrPresent
)
1446 SetControlViewSize(s
->scrollbar
, page
);
1450 void sys_cursor(void *frontend
, int x
, int y
)
1453 * I think his is meaningless under Mac OS.
1458 * This is still called when mode==BELL_VISUAL, even though the
1459 * visual bell is handled entirely within terminal.c, because we
1460 * may want to perform additional actions on any kind of bell (for
1461 * example, taskbar flashing in Windows).
1463 void beep(void *frontend
, int mode
)
1465 if (mode
!= BELL_VISUAL
)
1468 * XXX We should indicate the relevant window and/or use the
1469 * Notification Manager
1473 int char_width(Context ctx
, int uc
)
1476 * Until we support exciting character-set stuff, assume all chars are
1483 * Set icon string -- a no-op here (Windowshade?)
1485 void set_icon(void *frontend
, char *icon
) {
1486 Session
*s
= frontend
;
1491 * Set the window title
1493 void set_title(void *frontend
, char *title
)
1495 Session
*s
= frontend
;
1498 c2pstrcpy(mactitle
, title
);
1499 SetWTitle(s
->window
, mactitle
);
1503 * set or clear the "raw mouse message" mode
1505 void set_raw_mouse_mode(void *frontend
, int activate
)
1507 Session
*s
= frontend
;
1509 s
->raw_mouse
= activate
;
1510 /* FIXME: Should call mac_updatetermcursor as appropriate. */
1514 * Resize the window at the emulator's request
1516 void request_resize(void *frontend
, int w
, int h
)
1518 Session
*s
= frontend
;
1523 /* Arbitrarily clip to the size of the desktop. */
1524 grayrgn
= GetGrayRgn();
1525 #if TARGET_API_MAC_CARBON
1526 GetRegionBounds(grayrgn
, &graybox
);
1528 graybox
= (*grayrgn
)->rgnBBox
;
1530 wlim
= (graybox
.right
- graybox
.left
) / s
->font_width
;
1531 hlim
= (graybox
.bottom
- graybox
.top
) / s
->font_height
;
1532 if (w
> wlim
) w
= wlim
;
1533 if (h
> hlim
) h
= hlim
;
1534 term_size(s
->term
, h
, w
, s
->cfg
.savelines
);
1539 * Iconify (actually collapse) the window at the emulator's request.
1541 void set_iconic(void *frontend
, int iconic
)
1543 Session
*s
= frontend
;
1546 if (mac_gestalts
.apprvers
>= 0x0100 &&
1547 GetWindowFeatures(s
->window
, &features
) == noErr
&&
1548 (features
& kWindowCanCollapse
))
1549 CollapseWindow(s
->window
, iconic
);
1553 * Move the window in response to a server-side request.
1555 void move_window(void *frontend
, int x
, int y
)
1557 Session
*s
= frontend
;
1559 MoveWindow(s
->window
, x
, y
, FALSE
);
1563 * Move the window to the top or bottom of the z-order in response
1564 * to a server-side request.
1566 void set_zorder(void *frontend
, int top
)
1568 Session
*s
= frontend
;
1571 * We also change the input focus to point to the topmost window,
1572 * since that's probably what the Human Interface Guidelines would
1576 SelectWindow(s
->window
);
1578 SendBehind(s
->window
, NULL
);
1582 * Refresh the window in response to a server-side request.
1584 void refresh_window(void *frontend
)
1586 Session
*s
= frontend
;
1588 term_invalidate(s
->term
);
1592 * Maximise or restore the window in response to a server-side
1595 void set_zoomed(void *frontend
, int zoomed
)
1597 Session
*s
= frontend
;
1599 ZoomWindow(s
->window
, zoomed ? inZoomOut
: inZoomIn
, FALSE
);
1603 * Report whether the window is iconic, for terminal reports.
1605 int is_iconic(void *frontend
)
1607 Session
*s
= frontend
;
1610 if (mac_gestalts
.apprvers
>= 0x0100 &&
1611 GetWindowFeatures(s
->window
, &features
) == noErr
&&
1612 (features
& kWindowCanCollapse
))
1613 return IsWindowCollapsed(s
->window
);
1618 * Report the window's position, for terminal reports.
1620 void get_window_pos(void *frontend
, int *x
, int *y
)
1622 Session
*s
= frontend
;
1625 #if TARGET_API_MAC_CARBON
1626 GetPortBounds(GetWindowPort(s
->window
), &rect
);
1628 rect
= s
->window
->portRect
;
1635 * Report the window's pixel size, for terminal reports.
1637 void get_window_pixels(void *frontend
, int *x
, int *y
)
1639 Session
*s
= frontend
;
1642 #if TARGET_API_MAC_CARBON
1643 GetPortBounds(GetWindowPort(s
->window
), &rect
);
1645 rect
= s
->window
->portRect
;
1647 *x
= rect
.right
- rect
.left
;
1648 *y
= rect
.bottom
- rect
.top
;
1652 * Return the window or icon title.
1654 char *get_window_title(void *frontend
, int icon
)
1656 Session
*s
= frontend
;
1658 static char title
[256];
1660 GetWTitle(s
->window
, ptitle
);
1661 p2cstrcpy(title
, ptitle
);
1666 * real_palette_set(): This does the actual palette-changing work on behalf
1667 * of palette_set(). Does _not_ call ActivatePalette() in case the caller
1668 * is doing a batch of updates.
1670 static void real_palette_set(Session
*s
, int n
, int r
, int g
, int b
)
1674 if (!HAVE_COLOR_QD())
1676 col
.red
= r
* 0x0101;
1677 col
.green
= g
* 0x0101;
1678 col
.blue
= b
* 0x0101;
1679 SetEntryColor(s
->palette
, n
, &col
);
1683 * Set the logical palette. Called by the terminal emulator.
1685 void palette_set(void *frontend
, int n
, int r
, int g
, int b
)
1687 Session
*s
= frontend
;
1689 if (!HAVE_COLOR_QD())
1691 real_palette_set(s
, n
, r
, g
, b
);
1692 if (n
== DEFAULT_BG
)
1695 ActivatePalette(s
->window
);
1699 * Reset to the default palette
1701 void palette_reset(void *frontend
)
1703 Session
*s
= frontend
;
1704 /* This maps colour indices in cfg to those used in our palette. */
1705 static const int ww
[] = {
1706 256, 257, 258, 259, 260, 261,
1707 0, 8, 1, 9, 2, 10, 3, 11,
1708 4, 12, 5, 13, 6, 14, 7, 15
1713 if (!HAVE_COLOR_QD())
1716 for (i
= 0; i
< 22; i
++) {
1718 real_palette_set(s
,w
,
1719 s
->cfg
.colours
[i
][0],
1720 s
->cfg
.colours
[i
][1],
1721 s
->cfg
.colours
[i
][2]);
1723 for (i
= 0; i
< 240; i
++) {
1725 int r
= i
/ 36, g
= (i
/ 6) % 6, b
= i
% 6;
1726 real_palette_set(s
,i
+16,
1731 int shade
= i
- 216;
1732 shade
= (shade
+ 1) * 0xFF / (240 - 216 + 1);
1733 real_palette_set(s
,i
+16,shade
,shade
,shade
);
1739 ActivatePalette(s
->window
);
1740 /* Palette Manager will generate update events as required. */
1744 * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
1747 void do_scroll(Context ctx
, int topline
, int botline
, int lines
)
1751 RgnHandle scrollrgn
= NewRgn();
1752 RgnHandle movedupdate
= NewRgn();
1753 RgnHandle update
= NewRgn();
1754 Point g2l
= { 0, 0 };
1756 SetPort((GrafPtr
)GetWindowPort(s
->window
));
1759 * Work out the part of the update region that will scrolled by
1763 SetRectRgn(scrollrgn
, 0, (topline
+ lines
) * s
->font_height
,
1764 s
->term
->cols
* s
->font_width
,
1765 (botline
+ 1) * s
->font_height
);
1767 SetRectRgn(scrollrgn
, 0, topline
* s
->font_height
,
1768 s
->term
->cols
* s
->font_width
,
1769 (botline
- lines
+ 1) * s
->font_height
);
1770 #if TARGET_API_MAC_CARBON
1771 GetWindowRegion(s
->window
, kWindowUpdateRgn
, movedupdate
);
1773 GetWindowUpdateRgn(s
->window
, movedupdate
);
1775 GlobalToLocal(&g2l
);
1776 OffsetRgn(movedupdate
, g2l
.h
, g2l
.v
); /* Convert to local co-ords. */
1777 SectRgn(scrollrgn
, movedupdate
, movedupdate
); /* Clip scrolled section. */
1778 #if TARGET_API_MAC_CARBON
1779 ValidWindowRgn(s
->window
, movedupdate
);
1781 ValidRgn(movedupdate
);
1783 OffsetRgn(movedupdate
, 0, -lines
* s
->font_height
); /* Scroll it. */
1786 if (HAVE_COLOR_QD())
1787 PmBackColor(DEFAULT_BG
);
1789 BackColor(blackColor
); /* XXX make configurable */
1790 SetRect(&r
, 0, topline
* s
->font_height
,
1791 s
->term
->cols
* s
->font_width
, (botline
+ 1) * s
->font_height
);
1792 ScrollRect(&r
, 0, - lines
* s
->font_height
, update
);
1794 #if TARGET_API_MAC_CARBON
1795 InvalWindowRgn(s
->window
, update
);
1796 InvalWindowRgn(s
->window
, movedupdate
);
1799 InvalRgn(movedupdate
);
1802 DisposeRgn(scrollrgn
);
1803 DisposeRgn(movedupdate
);
1807 /* Dummy routine, only required in plink. */
1808 void ldisc_update(void *frontend
, int echo
, int edit
)
1813 * Mac PuTTY doesn't support printing yet.
1815 printer_job
*printer_start_job(char *printer
)
1821 void printer_job_data(printer_job
*pj
, void *data
, int len
)
1825 void printer_finish_job(printer_job
*pj
)
1829 void frontend_keypress(void *handle
)
1832 * Keypress termination in non-Close-On-Exit mode is not
1833 * currently supported in PuTTY proper, because the window
1834 * always has a perfectly good Close button anyway. So we do
1841 * Ask whether to wipe a session log file before writing to it.
1842 * Returns 2 for wipe, 1 for append, 0 for cancel (don't log).
1844 int askappend(void *frontend
, Filename filename
)
1847 /* FIXME: not implemented yet. */
1851 int from_backend(void *frontend
, int is_stderr
, const char *data
, int len
)
1853 Session
*s
= frontend
;
1855 return term_data(s
->term
, is_stderr
, data
, len
);
1861 * c-file-style: "simon"