1 /* $Id: macterm.c,v 1.22 2002/12/09 23:26:52 ben Exp $ */
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>
39 #include <MacMemory.h>
40 #include <MacWindows.h>
41 #include <MixedMode.h>
43 #include <Quickdraw.h>
44 #include <QuickdrawText.h>
45 #include <Resources.h>
50 #include <ToolUtils.h>
63 #define NCOLOURS (lenof(((Config *)0)->colours))
66 #define DEFAULT_FG_BOLD 17
68 #define DEFAULT_BG_BOLD 19
72 #define PTOCC(x) ((x) < 0 ? -(-(x - s->font_width - 1) / s->font_width) : \
74 #define PTOCR(y) ((y) < 0 ? -(-(y - s->font_height - 1) / s->font_height) : \
77 static void mac_initfont(Session
*);
78 static void mac_initpalette(Session
*);
79 static void mac_adjustwinbg(Session
*);
80 static void mac_adjustsize(Session
*, int, int);
81 static void mac_drawgrowicon(Session
*s
);
82 static pascal void mac_growtermdraghook(void);
83 static pascal void mac_scrolltracker(ControlHandle
, short);
84 static pascal void do_text_for_device(short, short, GDHandle
, long);
85 static int mac_keytrans(Session
*, EventRecord
*, unsigned char *);
86 static void text_click(Session
*, EventRecord
*);
88 void pre_paint(Session
*s
);
89 void post_paint(Session
*s
);
92 static RoutineDescriptor mac_scrolltracker_upp
=
93 BUILD_ROUTINE_DESCRIPTOR(uppControlActionProcInfo
,
94 (ProcPtr
)mac_scrolltracker
);
95 static RoutineDescriptor do_text_for_device_upp
=
96 BUILD_ROUTINE_DESCRIPTOR(uppDeviceLoopDrawingProcInfo
,
97 (ProcPtr
)do_text_for_device
);
98 #else /* not TARGET_RT_MAC_CFM */
99 #define mac_scrolltracker_upp mac_scrolltracker
100 #define do_text_for_device_upp do_text_for_device
101 #endif /* not TARGET_RT_MAC_CFM */
103 static void inbuf_putc(Session
*s
, int c
) {
106 from_backend(s
->term
, 0, &ch
, 1);
109 static void inbuf_putstr(Session
*s
, const char *c
) {
111 from_backend(s
->term
, 0, (char *)c
, strlen(c
));
114 static void display_resource(Session
*s
, unsigned long type
, short id
) {
119 h
= GetResource(type
, id
);
121 fatalbox("Can't get test resource");
122 len
= GetResourceSizeOnDisk(h
);
127 from_backend(s
->term
, 0, t
, len
);
133 void mac_newsession(void) {
139 /* This should obviously be initialised by other means */
140 s
= smalloc(sizeof(*s
));
141 memset(s
, 0, sizeof(*s
));
142 do_defaults(NULL
, &s
->cfg
);
143 s
->back
= &loop_backend
;
145 /* XXX: Own storage management? */
147 s
->window
= GetNewCWindow(wTerminal
, NULL
, (WindowPtr
)-1);
149 s
->window
= GetNewWindow(wTerminal
, NULL
, (WindowPtr
)-1);
150 SetWRefCon(s
->window
, (long)s
);
151 s
->scrollbar
= GetNewControl(cVScroll
, s
->window
);
152 s
->term
= term_init(&s
->cfg
, s
);
154 s
->logctx
= log_init(s
);
155 term_provide_logctx(s
->term
, s
->logctx
);
157 s
->back
->init(s
->term
, &s
->backhandle
, "localhost", 23, &s
->realhost
, 0);
158 s
->back
->provide_logctx(s
->backhandle
, s
->logctx
);
160 term_provide_resize_fn(s
->term
, s
->back
->size
, s
->backhandle
);
162 mac_adjustsize(s
, s
->cfg
.height
, s
->cfg
.width
);
163 term_size(s
->term
, s
->cfg
.height
, s
->cfg
.width
, s
->cfg
.savelines
);
165 s
->ldisc
= ldisc_create(&s
->cfg
, s
->term
, s
->back
, s
->backhandle
, s
);
166 ldisc_send(s
->ldisc
, NULL
, 0, 0);/* cause ldisc to notice changes */
170 if (HAVE_COLOR_QD()) {
171 /* Set to FALSE to not get palette updates in the background. */
172 SetPalette(s
->window
, s
->palette
, TRUE
);
173 ActivatePalette(s
->window
);
175 ShowWindow(s
->window
);
176 starttime
= TickCount();
177 display_resource(s
, 'pTST', 128);
178 sprintf(msg
, "Elapsed ticks: %d\015\012", TickCount() - starttime
);
179 inbuf_putstr(s
, msg
);
183 static void mac_initfont(Session
*s
) {
188 macfont
[0] = sprintf((char *)&macfont
[1], "%s", s
->cfg
.font
);
189 GetFNum(macfont
, &s
->fontnum
);
190 TextFont(s
->fontnum
);
191 TextFace(s
->cfg
.fontisbold ? bold
: 0);
192 TextSize(s
->cfg
.fontheight
);
194 s
->font_width
= CharWidth('W'); /* Well, it's what NCSA uses. */
195 s
->font_ascent
= fi
.ascent
;
196 s
->font_leading
= fi
.leading
;
197 s
->font_height
= s
->font_ascent
+ fi
.descent
+ s
->font_leading
;
198 if (!s
->cfg
.bold_colour
) {
200 s
->font_boldadjust
= s
->font_width
- CharWidth('W');
202 s
->font_boldadjust
= 0;
203 mac_adjustsize(s
, s
->term
->rows
, s
->term
->cols
);
207 * To be called whenever the window size changes.
208 * rows and cols should be desired values.
209 * It's assumed the terminal emulator will be informed, and will set rows
212 static void mac_adjustsize(Session
*s
, int newrows
, int newcols
) {
213 int winwidth
, winheight
;
215 winwidth
= newcols
* s
->font_width
+ 15;
216 winheight
= newrows
* s
->font_height
;
217 SizeWindow(s
->window
, winwidth
, winheight
, true);
218 HideControl(s
->scrollbar
);
219 MoveControl(s
->scrollbar
, winwidth
- 15, -1);
220 SizeControl(s
->scrollbar
, 16, winheight
- 13);
221 ShowControl(s
->scrollbar
);
225 static void mac_initpalette(Session
*s
) {
228 if (!HAVE_COLOR_QD())
231 * Most colours should be inhibited on 2bpp displays.
232 * Palette manager documentation suggests inhibiting all tolerant colours
233 * on greyscale displays.
235 #define PM_NORMAL ( pmTolerant | pmInhibitC2 | \
236 pmInhibitG2 | pmInhibitG4 | pmInhibitG8 )
237 #define PM_TOLERANCE 0x2000
238 s
->palette
= NewPalette(22, NULL
, PM_NORMAL
, PM_TOLERANCE
);
239 if (s
->palette
== NULL
)
240 fatalbox("Unable to create palette");
241 /* In 2bpp, these are the colours we want most. */
242 SetEntryUsage(s
->palette
, DEFAULT_BG
,
243 PM_NORMAL
&~ pmInhibitC2
, PM_TOLERANCE
);
244 SetEntryUsage(s
->palette
, DEFAULT_FG
,
245 PM_NORMAL
&~ pmInhibitC2
, PM_TOLERANCE
);
246 SetEntryUsage(s
->palette
, DEFAULT_FG_BOLD
,
247 PM_NORMAL
&~ pmInhibitC2
, PM_TOLERANCE
);
248 SetEntryUsage(s
->palette
, CURSOR_BG
,
249 PM_NORMAL
&~ pmInhibitC2
, PM_TOLERANCE
);
254 * Set the background colour of the window correctly. Should be
255 * called whenever the default background changes.
257 static void mac_adjustwinbg(Session
*s
) {
259 if (!HAVE_COLOR_QD())
261 #if TARGET_RT_CFM /* XXX doesn't link (at least for 68k) */
262 if (mac_gestalts
.windattr
& gestaltWindowMgrPresent
)
263 SetWindowContentColor(s
->window
,
264 &(*s
->palette
)->pmInfo
[DEFAULT_BG
].ciRGB
);
268 if (s
->wctab
== NULL
)
269 s
->wctab
= (WCTabHandle
)NewHandle(sizeof(**s
->wctab
));
270 if (s
->wctab
== NULL
)
271 return; /* do without */
272 (*s
->wctab
)->wCSeed
= 0;
273 (*s
->wctab
)->wCReserved
= 0;
274 (*s
->wctab
)->ctSize
= 0;
275 (*s
->wctab
)->ctTable
[0].value
= wContentColor
;
276 (*s
->wctab
)->ctTable
[0].rgb
= (*s
->palette
)->pmInfo
[DEFAULT_BG
].ciRGB
;
277 SetWinColor(s
->window
, s
->wctab
);
282 * Set the cursor shape correctly
284 void mac_adjusttermcursor(WindowPtr window
, Point mouse
, RgnHandle cursrgn
) {
286 ControlHandle control
;
291 s
= (Session
*)GetWRefCon(window
);
292 GlobalToLocal(&mouse
);
293 part
= FindControl(mouse
, window
, &control
);
294 if (control
== s
->scrollbar
) {
295 SetCursor(&qd
.arrow
);
296 RectRgn(cursrgn
, &(*s
->scrollbar
)->contrlRect
);
297 SectRgn(cursrgn
, window
->visRgn
, cursrgn
);
299 x
= mouse
.h
/ s
->font_width
;
300 y
= mouse
.v
/ s
->font_height
;
302 SetCursor(&qd
.arrow
);
304 SetCursor(*GetCursor(iBeamCursor
));
305 /* Ask for shape changes if we leave this character cell. */
306 SetRectRgn(cursrgn
, x
* s
->font_width
, y
* s
->font_height
,
307 (x
+ 1) * s
->font_width
, (y
+ 1) * s
->font_height
);
308 SectRgn(cursrgn
, window
->visRgn
, cursrgn
);
313 * Enable/disable menu items based on the active terminal window.
315 void mac_adjusttermmenus(WindowPtr window
) {
320 s
= (Session
*)GetWRefCon(window
);
321 menu
= GetMenuHandle(mEdit
);
323 DisableItem(menu
, iUndo
);
324 DisableItem(menu
, iCut
);
325 if (1/*s->term->selstate == SELECTED*/)
326 EnableItem(menu
, iCopy
);
328 DisableItem(menu
, iCopy
);
329 if (GetScrap(NULL
, 'TEXT', &offset
) == noTypeErr
)
330 DisableItem(menu
, iPaste
);
332 EnableItem(menu
, iPaste
);
333 DisableItem(menu
, iClear
);
334 EnableItem(menu
, iSelectAll
);
337 void mac_menuterm(WindowPtr window
, short menu
, short item
) {
340 s
= (Session
*)GetWRefCon(window
);
348 term_do_paste(s
->term
);
354 void mac_clickterm(WindowPtr window
, EventRecord
*event
) {
357 ControlHandle control
;
360 s
= (Session
*)GetWRefCon(window
);
362 mouse
= event
->where
;
363 GlobalToLocal(&mouse
);
364 part
= FindControl(mouse
, window
, &control
);
365 if (control
== s
->scrollbar
) {
367 case kControlIndicatorPart
:
368 if (TrackControl(control
, mouse
, NULL
) == kControlIndicatorPart
)
369 term_scroll(s
->term
, +1, GetControlValue(control
));
371 case kControlUpButtonPart
:
372 case kControlDownButtonPart
:
373 case kControlPageUpPart
:
374 case kControlPageDownPart
:
375 TrackControl(control
, mouse
, &mac_scrolltracker_upp
);
379 text_click(s
, event
);
383 static void text_click(Session
*s
, EventRecord
*event
) {
386 static UInt32 lastwhen
= 0;
387 static Session
*lastsess
= NULL
;
388 static int lastrow
= -1, lastcol
= -1;
389 static Mouse_Action lastact
= MA_NOTHING
;
392 localwhere
= event
->where
;
393 GlobalToLocal(&localwhere
);
395 col
= PTOCC(localwhere
.h
);
396 row
= PTOCR(localwhere
.v
);
397 if (event
->when
- lastwhen
< GetDblTime() &&
398 row
== lastrow
&& col
== lastcol
&& s
== lastsess
)
399 lastact
= (lastact
== MA_CLICK ? MA_2CLK
:
400 lastact
== MA_2CLK ? MA_3CLK
:
401 lastact
== MA_3CLK ? MA_CLICK
: MA_NOTHING
);
404 /* Fake right button with shift key */
405 term_mouse(s
->term
, event
->modifiers
& shiftKey ? MBT_RIGHT
: MBT_LEFT
,
406 lastact
, col
, row
, event
->modifiers
& shiftKey
,
407 event
->modifiers
& controlKey
, event
->modifiers
& optionKey
);
411 while (StillDown()) {
412 GetMouse(&localwhere
);
413 col
= PTOCC(localwhere
.h
);
414 row
= PTOCR(localwhere
.v
);
416 event
->modifiers
& shiftKey ? MBT_RIGHT
: MBT_LEFT
,
417 MA_DRAG
, col
, row
, event
->modifiers
& shiftKey
,
418 event
->modifiers
& controlKey
,
419 event
->modifiers
& optionKey
);
420 if (row
> s
->term
->rows
- 1)
421 term_scroll(s
->term
, 0, row
- (s
->term
->rows
- 1));
423 term_scroll(s
->term
, 0, row
);
425 term_mouse(s
->term
, event
->modifiers
& shiftKey ? MBT_RIGHT
: MBT_LEFT
,
426 MA_RELEASE
, col
, row
, event
->modifiers
& shiftKey
,
427 event
->modifiers
& controlKey
, event
->modifiers
& optionKey
);
428 lastwhen
= TickCount();
431 Mouse_Button
translate_button(void *frontend
, Mouse_Button button
)
444 void write_clip(void *cookie
, wchar_t *data
, int len
, int must_deselect
) {
447 * See "Programming with the Text Encoding Conversion Manager"
448 * Appendix E for Unicode scrap conventions.
450 * XXX Need to support TEXT/styl scrap as well.
451 * See STScrpRec in TextEdit (Inside Macintosh: Text) for styl details.
452 * XXX Maybe PICT scrap too.
454 if (ZeroScrap() != noErr
)
456 PutScrap(len
* sizeof(*data
), 'utxt', data
);
459 void get_clip(void *frontend
, wchar_t **p
, int *lenp
) {
460 Session
*s
= frontend
;
461 static Handle h
= NULL
;
470 /* XXX Support TEXT-format scrap as well. */
471 if (GetScrap(NULL
, 'utxt', &offset
) > 0) {
473 *lenp
= GetScrap(h
, 'utxt', &offset
) / sizeof(**p
);
476 if (*p
== NULL
|| *lenp
<= 0)
477 fatalbox("Empty scrap");
484 static pascal void mac_scrolltracker(ControlHandle control
, short part
) {
487 s
= (Session
*)GetWRefCon((*control
)->contrlOwner
);
489 case kControlUpButtonPart
:
490 term_scroll(s
->term
, 0, -1);
492 case kControlDownButtonPart
:
493 term_scroll(s
->term
, 0, +1);
495 case kControlPageUpPart
:
496 term_scroll(s
->term
, 0, -(s
->term
->rows
- 1));
498 case kControlPageDownPart
:
499 term_scroll(s
->term
, 0, +(s
->term
->rows
- 1));
520 #define K_INSERT 0x7200
521 #define K_HOME 0x7300
522 #define K_PRIOR 0x7400
523 #define K_DELETE 0x7500
525 #define K_NEXT 0x7900
526 #define K_LEFT 0x7b00
527 #define K_RIGHT 0x7c00
528 #define K_DOWN 0x7d00
540 #define KP_CLEAR 0x4700
541 #define KP_EQUAL 0x5100
542 #define KP_SLASH 0x4b00
543 #define KP_STAR 0x4300
544 #define KP_PLUS 0x4500
545 #define KP_MINUS 0x4e00
546 #define KP_DOT 0x4100
547 #define KP_ENTER 0x4c00
549 void mac_keyterm(WindowPtr window
, EventRecord
*event
) {
550 unsigned char buf
[20];
554 s
= (Session
*)GetWRefCon(window
);
555 len
= mac_keytrans(s
, event
, buf
);
556 ldisc_send(s
->ldisc
, (char *)buf
, len
, 1);
558 term_seen_key_event(s
->term
);
560 term_update(s
->term
);
563 static int mac_keytrans(Session
*s
, EventRecord
*event
,
564 unsigned char *output
) {
565 unsigned char *p
= output
;
568 /* No meta key yet -- that'll be rather fun. */
570 /* Keys that we handle locally */
571 if (event
->modifiers
& shiftKey
) {
572 switch (event
->message
& keyCodeMask
) {
573 case K_PRIOR
: /* shift-pageup */
574 term_scroll(s
->term
, 0, -(s
->term
->rows
- 1));
576 case K_NEXT
: /* shift-pagedown */
577 term_scroll(s
->term
, 0, +(s
->term
->rows
- 1));
583 * Control-2 should return ^@ (0x00), Control-6 should return
584 * ^^ (0x1E), and Control-Minus should return ^_ (0x1F). Since
585 * the DOS keyboard handling did it, and we have nothing better
586 * to do with the key combo in question, we'll also map
587 * Control-Backquote to ^\ (0x1C).
590 if (event
->modifiers
& controlKey
) {
591 switch (event
->message
& charCodeMask
) {
608 * First, all the keys that do tilde codes. (ESC '[' nn '~',
609 * for integer decimal nn.)
611 * We also deal with the weird ones here. Linux VCs replace F1
612 * to F5 by ESC [ [ A to ESC [ [ E. rxvt doesn't do _that_, but
613 * does replace Home and End (1~ and 4~) by ESC [ H and ESC O w
617 switch (event
->message
& keyCodeMask
) {
618 case K_F1
: code
= (event
->modifiers
& shiftKey ?
23 : 11); break;
619 case K_F2
: code
= (event
->modifiers
& shiftKey ?
24 : 12); break;
620 case K_F3
: code
= (event
->modifiers
& shiftKey ?
25 : 13); break;
621 case K_F4
: code
= (event
->modifiers
& shiftKey ?
26 : 14); break;
622 case K_F5
: code
= (event
->modifiers
& shiftKey ?
28 : 15); break;
623 case K_F6
: code
= (event
->modifiers
& shiftKey ?
29 : 17); break;
624 case K_F7
: code
= (event
->modifiers
& shiftKey ?
31 : 18); break;
625 case K_F8
: code
= (event
->modifiers
& shiftKey ?
32 : 19); break;
626 case K_F9
: code
= (event
->modifiers
& shiftKey ?
33 : 20); break;
627 case K_F10
: code
= (event
->modifiers
& shiftKey ?
34 : 21); break;
628 case K_F11
: code
= 23; break;
629 case K_F12
: code
= 24; break;
630 case K_HOME
: code
= 1; break;
631 case K_INSERT
: code
= 2; break;
632 case K_DELETE
: code
= 3; break;
633 case K_END
: code
= 4; break;
634 case K_PRIOR
: code
= 5; break;
635 case K_NEXT
: code
= 6; break;
637 if (s
->cfg
.funky_type
== 1 && code
>= 11 && code
<= 15) {
638 p
+= sprintf((char *)p
, "\x1B[[%c", code
+ 'A' - 11);
641 if (s
->cfg
.rxvt_homeend
&& (code
== 1 || code
== 4)) {
642 p
+= sprintf((char *)p
, code
== 1 ?
"\x1B[H" : "\x1BOw");
646 p
+= sprintf((char *)p
, "\x1B[%d~", code
);
650 if (s
->term
->app_keypad_keys
) {
651 switch (event
->message
& keyCodeMask
) {
652 case KP_ENTER
: p
+= sprintf((char *)p
, "\x1BOM"); return p
- output
;
653 case KP_CLEAR
: p
+= sprintf((char *)p
, "\x1BOP"); return p
- output
;
654 case KP_EQUAL
: p
+= sprintf((char *)p
, "\x1BOQ"); return p
- output
;
655 case KP_SLASH
: p
+= sprintf((char *)p
, "\x1BOR"); return p
- output
;
656 case KP_STAR
: p
+= sprintf((char *)p
, "\x1BOS"); return p
- output
;
657 case KP_PLUS
: p
+= sprintf((char *)p
, "\x1BOl"); return p
- output
;
658 case KP_MINUS
: p
+= sprintf((char *)p
, "\x1BOm"); return p
- output
;
659 case KP_DOT
: p
+= sprintf((char *)p
, "\x1BOn"); return p
- output
;
660 case KP_0
: p
+= sprintf((char *)p
, "\x1BOp"); return p
- output
;
661 case KP_1
: p
+= sprintf((char *)p
, "\x1BOq"); return p
- output
;
662 case KP_2
: p
+= sprintf((char *)p
, "\x1BOr"); return p
- output
;
663 case KP_3
: p
+= sprintf((char *)p
, "\x1BOs"); return p
- output
;
664 case KP_4
: p
+= sprintf((char *)p
, "\x1BOt"); return p
- output
;
665 case KP_5
: p
+= sprintf((char *)p
, "\x1BOu"); return p
- output
;
666 case KP_6
: p
+= sprintf((char *)p
, "\x1BOv"); return p
- output
;
667 case KP_7
: p
+= sprintf((char *)p
, "\x1BOw"); return p
- output
;
668 case KP_8
: p
+= sprintf((char *)p
, "\x1BOx"); return p
- output
;
669 case KP_9
: p
+= sprintf((char *)p
, "\x1BOy"); return p
- output
;
673 switch (event
->message
& keyCodeMask
) {
675 p
+= sprintf((char *)p
,
676 s
->term
->app_cursor_keys ?
"\x1BOA" : "\x1B[A");
679 p
+= sprintf((char *)p
,
680 s
->term
->app_cursor_keys ?
"\x1BOB" : "\x1B[B");
683 p
+= sprintf((char *)p
,
684 s
->term
->app_cursor_keys ?
"\x1BOC" : "\x1B[C");
687 p
+= sprintf((char *)p
,
688 s
->term
->app_cursor_keys ?
"\x1BOD" : "\x1B[D");
694 *p
++ = (s
->cfg
.bksp_is_delete ?
0x7f : 0x08);
697 *p
++ = event
->message
& charCodeMask
;
702 void request_paste(void *frontend
)
704 Session
*s
= frontend
;
707 * In the Mac OS, pasting is synchronous: we can read the
708 * clipboard with no difficulty, so request_paste() can just go
711 term_do_paste(s
->term
);
722 void mac_growterm(WindowPtr window
, EventRecord
*event
) {
725 int newrows
, newcols
;
727 DragGrayRgnUPP draghooksave
;
731 s
= (Session
*)GetWRefCon(window
);
733 draghooksave
= LMGetDragHook();
734 growterm_state
.oldmsg
[0] = '\0';
735 growterm_state
.startmouse
= event
->where
;
736 growterm_state
.s
= s
;
739 BackColor(whiteColor
);
740 ForeColor(blackColor
);
741 TextFont(systemFont
);
745 SetRect(&growterm_state
.msgrect
, 0, 0,
746 StringWidth("\p99999x99999") + 4, fi
.ascent
+ fi
.descent
+ 4);
747 SetPt(&growterm_state
.msgorigin
, 2, fi
.ascent
+ 2);
748 LMSetDragHook(NewDragGrayRgnUPP(mac_growtermdraghook
));
750 SetRect(&limits
, s
->font_width
+ 15, s
->font_height
, SHRT_MAX
, SHRT_MAX
);
751 grow_result
= GrowWindow(window
, event
->where
, &limits
);
753 DisposeDragGrayRgnUPP(LMGetDragHook());
754 LMSetDragHook(draghooksave
);
755 InvalRect(&growterm_state
.msgrect
);
759 if (grow_result
!= 0) {
760 newrows
= HiWord(grow_result
) / s
->font_height
;
761 newcols
= (LoWord(grow_result
) - 15) / s
->font_width
;
762 mac_adjustsize(s
, newrows
, newcols
);
763 term_size(s
->term
, newrows
, newcols
, s
->cfg
.savelines
);
767 static pascal void mac_growtermdraghook(void)
769 Session
*s
= growterm_state
.s
;
773 int newrows
, newcols
;
776 newrows
= (mouse
.v
- growterm_state
.startmouse
.v
) / s
->font_height
+
778 if (newrows
< 1) newrows
= 1;
779 newcols
= (mouse
.h
- growterm_state
.startmouse
.h
) / s
->font_width
+
781 if (newcols
< 1) newcols
= 1;
782 sprintf(buf
, "%dx%d", newcols
, newrows
);
783 if (strcmp(buf
, growterm_state
.oldmsg
) == 0)
785 strcpy(growterm_state
.oldmsg
, buf
);
789 SetPort(growterm_state
.s
->window
);
790 EraseRect(&growterm_state
.msgrect
);
791 MoveTo(growterm_state
.msgorigin
.h
, growterm_state
.msgorigin
.v
);
792 DrawString((StringPtr
)buf
);
796 void mac_activateterm(WindowPtr window
, Boolean active
) {
799 s
= (Session
*)GetWRefCon(window
);
800 s
->term
->has_focus
= active
;
801 term_update(s
->term
);
803 ShowControl(s
->scrollbar
);
806 PmBackColor(DEFAULT_BG
);/* HideControl clears behind the control */
808 BackColor(blackColor
);
809 HideControl(s
->scrollbar
);
814 void mac_updateterm(WindowPtr window
) {
817 s
= (Session
*)GetWRefCon(window
);
821 term_paint(s
->term
, s
,
822 PTOCC((*window
->visRgn
)->rgnBBox
.left
),
823 PTOCR((*window
->visRgn
)->rgnBBox
.top
),
824 PTOCC((*window
->visRgn
)->rgnBBox
.right
),
825 PTOCR((*window
->visRgn
)->rgnBBox
.bottom
), 1);
826 /* Restore default colours in case the Window Manager uses them */
827 if (HAVE_COLOR_QD()) {
828 PmForeColor(DEFAULT_FG
);
829 PmBackColor(DEFAULT_BG
);
831 ForeColor(whiteColor
);
832 BackColor(blackColor
);
834 if (FrontWindow() != window
)
835 EraseRect(&(*s
->scrollbar
)->contrlRect
);
836 UpdateControls(window
, window
->visRgn
);
842 static void mac_drawgrowicon(Session
*s
) {
848 * Stop DrawGrowIcon giving us space for a horizontal scrollbar
849 * See Tech Note TB575 for details.
851 clip
= s
->window
->portRect
;
852 clip
.left
= clip
.right
- 15;
856 DrawGrowIcon(s
->window
);
861 struct do_text_args
{
872 * Call from the terminal emulator to draw a bit of text
874 * x and y are text row and column (zero-based)
876 void do_text(Context ctx
, int x
, int y
, char *text
, int len
,
877 unsigned long attr
, int lattr
) {
880 struct do_text_args a
;
885 /* First check this text is relevant */
886 a
.textrect
.top
= y
* s
->font_height
;
887 a
.textrect
.bottom
= (y
+ 1) * s
->font_height
;
888 a
.textrect
.left
= x
* s
->font_width
;
889 a
.textrect
.right
= (x
+ len
) * s
->font_width
;
890 if (!RectInRgn(&a
.textrect
, s
->window
->visRgn
))
898 a
.numer
.h
= a
.numer
.v
= a
.denom
.h
= a
.denom
.v
= 1;
900 TextFont(s
->fontnum
);
901 if (s
->cfg
.fontisbold
|| (attr
& ATTR_BOLD
) && !s
->cfg
.bold_colour
)
903 if (attr
& ATTR_UNDER
)
906 TextSize(s
->cfg
.fontheight
);
910 SpaceExtra(s
->font_boldadjust
<< 16);
911 CharExtra(s
->font_boldadjust
<< 16);
917 RectRgn(textrgn
, &a
.textrect
);
919 DeviceLoop(textrgn
, &do_text_for_device_upp
, (long)&a
, 0);
921 do_text_for_device(1, 0, NULL
, (long)&a
);
923 /* Tell the window manager about it in case this isn't an update */
924 ValidRect(&a
.textrect
);
927 static pascal void do_text_for_device(short depth
, short devflags
,
928 GDHandle device
, long cookie
) {
929 struct do_text_args
*a
;
930 int bgcolour
, fgcolour
, bright
, reverse
, tmp
;
932 a
= (struct do_text_args
*)cookie
;
934 bright
= (a
->attr
& ATTR_BOLD
) && a
->s
->cfg
.bold_colour
;
935 reverse
= a
->attr
& ATTR_REVERSE
;
937 if (depth
== 1 && (a
->attr
& TATTR_ACTCURS
))
940 if (HAVE_COLOR_QD()) {
942 fgcolour
= ((a
->attr
& ATTR_FGMASK
) >> ATTR_FGSHIFT
) * 2;
943 bgcolour
= ((a
->attr
& ATTR_BGMASK
) >> ATTR_BGSHIFT
) * 2;
946 * NB: bold reverse in 2bpp breaks with the usual PuTTY model and
947 * boldens the background, because that's all we can do.
949 fgcolour
= bright ? DEFAULT_FG_BOLD
: DEFAULT_FG
;
950 bgcolour
= DEFAULT_BG
;
957 if (bright
&& depth
> 2)
959 if ((a
->attr
& TATTR_ACTCURS
) && depth
> 1) {
960 fgcolour
= CURSOR_FG
;
961 bgcolour
= CURSOR_BG
;
963 PmForeColor(fgcolour
);
964 PmBackColor(bgcolour
);
965 } else { /* No Color Quickdraw */
966 /* XXX This should be done with a _little_ more configurability */
968 ForeColor(blackColor
);
969 BackColor(whiteColor
);
971 ForeColor(whiteColor
);
972 BackColor(blackColor
);
976 EraseRect(&a
->textrect
);
977 MoveTo(a
->textrect
.left
, a
->textrect
.top
+ a
->s
->font_ascent
);
978 /* FIXME: Sort out bold width adjustments on Original QuickDraw. */
979 if (a
->s
->window
->grafProcs
!= NULL
)
980 InvokeQDTextUPP(a
->len
, a
->text
, a
->numer
, a
->denom
,
981 a
->s
->window
->grafProcs
->textProc
);
983 StdText(a
->len
, a
->text
, a
->numer
, a
->denom
);
985 if (a
->attr
& TATTR_PASCURS
) {
992 PmForeColor(CURSOR_BG
);
995 FrameRect(&a
->textrect
);
999 void do_cursor(Context ctx
, int x
, int y
, char *text
, int len
,
1000 unsigned long attr
, int lattr
)
1003 do_text(ctx
, x
, y
, text
, len
, attr
, lattr
);
1007 * Call from the terminal emulator to get its graphics context.
1008 * Should probably be called start_redraw or something.
1010 void pre_paint(Session
*s
) {
1012 Rect myrect
, tmprect
;
1014 if (HAVE_COLOR_QD()) {
1015 s
->term
->attr_mask
= 0;
1017 myrect
= (*s
->window
->visRgn
)->rgnBBox
;
1018 LocalToGlobal((Point
*)&myrect
.top
);
1019 LocalToGlobal((Point
*)&myrect
.bottom
);
1020 for (gdh
= GetDeviceList();
1022 gdh
= GetNextDevice(gdh
)) {
1023 if (TestDeviceAttribute(gdh
, screenDevice
) &&
1024 TestDeviceAttribute(gdh
, screenActive
) &&
1025 SectRect(&(*gdh
)->gdRect
, &myrect
, &tmprect
)) {
1026 switch ((*(*gdh
)->gdPMap
)->pixelSize
) {
1028 if (s
->cfg
.bold_colour
)
1029 s
->term
->attr_mask
|= ~(ATTR_COLOURS
|
1030 (s
->cfg
.bold_colour ? ATTR_BOLD
: 0));
1033 s
->term
->attr_mask
|= ~ATTR_COLOURS
;
1036 s
->term
->attr_mask
= ~0;
1037 return; /* No point checking more screens. */
1042 s
->term
->attr_mask
= ~(ATTR_COLOURS
|
1043 (s
->cfg
.bold_colour ? ATTR_BOLD
: 0));
1046 Context
get_ctx(void *frontend
) {
1047 Session
*s
= frontend
;
1053 void free_ctx(Context ctx
) {
1058 * Presumably this does something in Windows
1060 void post_paint(Session
*s
) {
1065 * Set the scroll bar position
1067 * total is the line number of the bottom of the working screen
1068 * start is the line number of the top of the display
1069 * page is the length of the displayed page
1071 void set_sbar(void *frontend
, int total
, int start
, int page
) {
1072 Session
*s
= frontend
;
1074 /* We don't redraw until we've set everything up, to avoid glitches */
1075 (*s
->scrollbar
)->contrlMin
= 0;
1076 (*s
->scrollbar
)->contrlMax
= total
- page
;
1077 SetControlValue(s
->scrollbar
, start
);
1079 /* XXX: This doesn't link for me. */
1080 if (mac_gestalts
.cntlattr
& gestaltControlMgrPresent
)
1081 SetControlViewSize(s
->scrollbar
, page
);
1085 void sys_cursor(void *frontend
, int x
, int y
)
1088 * I think his is meaningless under Mac OS.
1093 * This is still called when mode==BELL_VISUAL, even though the
1094 * visual bell is handled entirely within terminal.c, because we
1095 * may want to perform additional actions on any kind of bell (for
1096 * example, taskbar flashing in Windows).
1098 void beep(void *frontend
, int mode
)
1100 if (mode
!= BELL_VISUAL
)
1103 * XXX We should indicate the relevant window and/or use the
1104 * Notification Manager
1108 int char_width(Context ctx
, int uc
)
1111 * Until we support exciting character-set stuff, assume all chars are
1118 * Set icon string -- a no-op here (Windowshade?)
1120 void set_icon(void *frontend
, char *icon
) {
1121 Session
*s
= frontend
;
1126 * Set the window title
1128 void set_title(void *frontend
, char *title
) {
1129 Session
*s
= frontend
;
1132 mactitle
[0] = sprintf((char *)&mactitle
[1], "%s", title
);
1133 SetWTitle(s
->window
, mactitle
);
1137 * set or clear the "raw mouse message" mode
1139 void set_raw_mouse_mode(void *frontend
, int activate
)
1141 Session
*s
= frontend
;
1143 s
->raw_mouse
= activate
;
1144 /* FIXME: Should call mac_updatetermcursor as appropriate. */
1148 * Resize the window at the emulator's request
1150 void request_resize(void *frontend
, int w
, int h
) {
1151 Session
*s
= frontend
;
1159 * Iconify (actually collapse) the window at the emulator's request.
1161 void set_iconic(void *frontend
, int iconic
)
1163 Session
*s
= frontend
;
1166 if (mac_gestalts
.apprvers
>= 0x0100 &&
1167 GetWindowFeatures(s
->window
, &features
) == noErr
&&
1168 (features
& kWindowCanCollapse
))
1169 CollapseWindow(s
->window
, iconic
);
1173 * Move the window in response to a server-side request.
1175 void move_window(void *frontend
, int x
, int y
)
1177 Session
*s
= frontend
;
1179 MoveWindow(s
->window
, x
, y
, FALSE
);
1183 * Move the window to the top or bottom of the z-order in response
1184 * to a server-side request.
1186 void set_zorder(void *frontend
, int top
)
1188 Session
*s
= frontend
;
1191 * We also change the input focus to point to the topmost window,
1192 * since that's probably what the Human Interface Guidelines would
1196 SelectWindow(s
->window
);
1198 SendBehind(s
->window
, NULL
);
1202 * Refresh the window in response to a server-side request.
1204 void refresh_window(void *frontend
)
1206 Session
*s
= frontend
;
1208 term_invalidate(s
->term
);
1212 * Maximise or restore the window in response to a server-side
1215 void set_zoomed(void *frontend
, int zoomed
)
1217 Session
*s
= frontend
;
1219 ZoomWindow(s
->window
, zoomed ? inZoomOut
: inZoomIn
, FALSE
);
1223 * Report whether the window is iconic, for terminal reports.
1225 int is_iconic(void *frontend
)
1227 Session
*s
= frontend
;
1230 if (mac_gestalts
.apprvers
>= 0x0100 &&
1231 GetWindowFeatures(s
->window
, &features
) == noErr
&&
1232 (features
& kWindowCanCollapse
))
1233 return IsWindowCollapsed(s
->window
);
1238 * Report the window's position, for terminal reports.
1240 void get_window_pos(void *frontend
, int *x
, int *y
)
1242 Session
*s
= frontend
;
1244 *x
= s
->window
->portRect
.left
;
1245 *y
= s
->window
->portRect
.top
;
1249 * Report the window's pixel size, for terminal reports.
1251 void get_window_pixels(void *frontend
, int *x
, int *y
)
1253 Session
*s
= frontend
;
1255 *x
= s
->window
->portRect
.right
- s
->window
->portRect
.left
;
1256 *y
= s
->window
->portRect
.bottom
- s
->window
->portRect
.top
;
1260 * Return the window or icon title.
1262 char *get_window_title(void *frontend
, int icon
)
1264 Session
*s
= frontend
;
1266 /* Erm, we don't save this at the moment */
1271 * real_palette_set(): This does the actual palette-changing work on behalf
1272 * of palette_set(). Does _not_ call ActivatePalette() in case the caller
1273 * is doing a batch of updates.
1275 static void real_palette_set(Session
*s
, int n
, int r
, int g
, int b
)
1279 if (!HAVE_COLOR_QD())
1281 col
.red
= r
* 0x0101;
1282 col
.green
= g
* 0x0101;
1283 col
.blue
= b
* 0x0101;
1284 SetEntryColor(s
->palette
, n
, &col
);
1288 * Set the logical palette. Called by the terminal emulator.
1290 void palette_set(void *frontend
, int n
, int r
, int g
, int b
) {
1291 Session
*s
= frontend
;
1292 static const int first
[21] = {
1293 0, 2, 4, 6, 8, 10, 12, 14,
1294 1, 3, 5, 7, 9, 11, 13, 15,
1298 if (!HAVE_COLOR_QD())
1300 real_palette_set(s
, first
[n
], r
, g
, b
);
1302 real_palette_set(s
, first
[n
]+1, r
, g
, b
);
1303 if (first
[n
] == DEFAULT_BG
)
1305 ActivatePalette(s
->window
);
1309 * Reset to the default palette
1311 void palette_reset(void *frontend
) {
1312 Session
*s
= frontend
;
1313 /* This maps colour indices in cfg to those used in our palette. */
1314 static const int ww
[] = {
1315 6, 7, 8, 9, 10, 11, 12, 13,
1316 14, 15, 16, 17, 18, 19, 20, 21,
1321 if (!HAVE_COLOR_QD())
1324 assert(lenof(ww
) == NCOLOURS
);
1326 for (i
= 0; i
< NCOLOURS
; i
++) {
1327 real_palette_set(s
, i
,
1328 s
->cfg
.colours
[ww
[i
]][0],
1329 s
->cfg
.colours
[ww
[i
]][1],
1330 s
->cfg
.colours
[ww
[i
]][2]);
1333 ActivatePalette(s
->window
);
1334 /* Palette Manager will generate update events as required. */
1338 * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
1341 void do_scroll(void *frontend
, int topline
, int botline
, int lines
) {
1342 Session
*s
= frontend
;
1344 RgnHandle scrollrgn
= NewRgn();
1345 RgnHandle movedupdate
= NewRgn();
1346 RgnHandle update
= NewRgn();
1347 Point g2l
= { 0, 0 };
1352 * Work out the part of the update region that will scrolled by
1356 SetRectRgn(scrollrgn
, 0, (topline
+ lines
) * s
->font_height
,
1357 s
->term
->cols
* s
->font_width
,
1358 (botline
+ 1) * s
->font_height
);
1360 SetRectRgn(scrollrgn
, 0, topline
* s
->font_height
,
1361 s
->term
->cols
* s
->font_width
,
1362 (botline
- lines
+ 1) * s
->font_height
);
1363 CopyRgn(((WindowPeek
)s
->window
)->updateRgn
, movedupdate
);
1364 GlobalToLocal(&g2l
);
1365 OffsetRgn(movedupdate
, g2l
.h
, g2l
.v
); /* Convert to local co-ords. */
1366 SectRgn(scrollrgn
, movedupdate
, movedupdate
); /* Clip scrolled section. */
1367 ValidRgn(movedupdate
);
1368 OffsetRgn(movedupdate
, 0, -lines
* s
->font_height
); /* Scroll it. */
1371 if (HAVE_COLOR_QD())
1372 PmBackColor(DEFAULT_BG
);
1374 BackColor(blackColor
); /* XXX make configurable */
1375 SetRect(&r
, 0, topline
* s
->font_height
,
1376 s
->term
->cols
* s
->font_width
, (botline
+ 1) * s
->font_height
);
1377 ScrollRect(&r
, 0, - lines
* s
->font_height
, update
);
1380 InvalRgn(movedupdate
);
1382 DisposeRgn(scrollrgn
);
1383 DisposeRgn(movedupdate
);
1387 void logevent(void *frontend
, char *str
) {
1389 /* XXX Do something */
1392 /* Dummy routine, only required in plink. */
1393 void ldisc_update(void *frontend
, int echo
, int edit
)
1398 * Mac PuTTY doesn't support printing yet.
1400 printer_job
*printer_start_job(char *printer
)
1406 void printer_job_data(printer_job
*pj
, void *data
, int len
)
1410 void printer_finish_job(printer_job
*pj
)
1414 void frontend_keypress(void *handle
)
1417 * Keypress termination in non-Close-On-Exit mode is not
1418 * currently supported in PuTTY proper, because the window
1419 * always has a perfectly good Close button anyway. So we do
1426 * Ask whether to wipe a session log file before writing to it.
1427 * Returns 2 for wipe, 1 for append, 0 for cancel (don't log).
1429 int askappend(void *frontend
, char *filename
)
1432 /* FIXME: not implemented yet. */
1439 * c-file-style: "simon"