aboutsummaryrefslogtreecommitdiffstats
path: root/src/macterm.c
diff options
context:
space:
mode:
authorMiles Bader2007-06-11 01:00:07 +0000
committerMiles Bader2007-06-11 01:00:07 +0000
commit67f3ad67ee317226cb5d1bb139de0cfd883fdc5e (patch)
tree58a0e1bea7a1d8728fd32e6127a44434e7eac006 /src/macterm.c
parentd17cf4eb2024cf54e4a216312184665094ee3df4 (diff)
parent2d715b39ea1c89066f469405d065dd1a6631d28e (diff)
downloademacs-67f3ad67ee317226cb5d1bb139de0cfd883fdc5e.tar.gz
emacs-67f3ad67ee317226cb5d1bb139de0cfd883fdc5e.zip
Merge from emacs--devo--0
Patches applied: * emacs--devo--0 (patch 781-792) - Update from CVS - Merge from gnus--rel--5.10 - Merge from emacs--rel--22 * emacs--rel--22 (patch 33-41) * gnus--rel--5.10 (patch 226-228) - Update from CVS Revision: emacs@sv.gnu.org/emacs--multi-tty--0--patch-21
Diffstat (limited to 'src/macterm.c')
-rw-r--r--src/macterm.c2124
1 files changed, 1159 insertions, 965 deletions
diff --git a/src/macterm.c b/src/macterm.c
index 0b8fec3a089..f8c79d1c865 100644
--- a/src/macterm.c
+++ b/src/macterm.c
@@ -35,12 +35,7 @@ Boston, MA 02110-1301, USA. */
35#include <alloca.h> 35#include <alloca.h>
36#endif 36#endif
37 37
38#if TARGET_API_MAC_CARBON 38#if !TARGET_API_MAC_CARBON
39/* USE_CARBON_EVENTS determines if the Carbon Event Manager is used to
40 obtain events from the event queue. If set to 0, WaitNextEvent is
41 used instead. */
42#define USE_CARBON_EVENTS 1
43#else /* not TARGET_API_MAC_CARBON */
44#include <Quickdraw.h> 39#include <Quickdraw.h>
45#include <ToolUtils.h> 40#include <ToolUtils.h>
46#include <Sound.h> 41#include <Sound.h>
@@ -263,7 +258,7 @@ static void x_scroll_bar_report_motion P_ ((struct frame **, Lisp_Object *,
263 Lisp_Object *, Lisp_Object *, 258 Lisp_Object *, Lisp_Object *,
264 unsigned long *)); 259 unsigned long *));
265 260
266static int is_emacs_window P_ ((WindowPtr)); 261static int is_emacs_window P_ ((WindowRef));
267static XCharStruct *mac_per_char_metric P_ ((XFontStruct *, XChar2b *, int)); 262static XCharStruct *mac_per_char_metric P_ ((XFontStruct *, XChar2b *, int));
268static void XSetFont P_ ((Display *, GC, XFontStruct *)); 263static void XSetFont P_ ((Display *, GC, XFontStruct *));
269static struct terminal *mac_create_terminal P_ ((struct mac_display_info *dpyinfo)); 264static struct terminal *mac_create_terminal P_ ((struct mac_display_info *dpyinfo));
@@ -418,7 +413,8 @@ mac_prepare_for_quickdraw (f)
418static RgnHandle saved_port_clip_region = NULL; 413static RgnHandle saved_port_clip_region = NULL;
419 414
420static void 415static void
421mac_begin_clip (gc) 416mac_begin_clip (f, gc)
417 struct frame *f;
422 GC gc; 418 GC gc;
423{ 419{
424 static RgnHandle new_region = NULL; 420 static RgnHandle new_region = NULL;
@@ -428,6 +424,11 @@ mac_begin_clip (gc)
428 if (new_region == NULL) 424 if (new_region == NULL)
429 new_region = NewRgn (); 425 new_region = NewRgn ();
430 426
427#if USE_CG_DRAWING
428 mac_prepare_for_quickdraw (f);
429#endif
430 SetPortWindowPort (FRAME_MAC_WINDOW (f));
431
431 if (gc->n_clip_rects) 432 if (gc->n_clip_rects)
432 { 433 {
433 GetClip (saved_port_clip_region); 434 GetClip (saved_port_clip_region);
@@ -447,15 +448,6 @@ mac_end_clip (gc)
447 448
448/* X display function emulation */ 449/* X display function emulation */
449 450
450void
451XFreePixmap (display, pixmap)
452 Display *display; /* not used */
453 Pixmap pixmap;
454{
455 DisposeGWorld (pixmap);
456}
457
458
459/* Mac version of XDrawLine. */ 451/* Mac version of XDrawLine. */
460 452
461static void 453static void
@@ -497,11 +489,8 @@ mac_draw_line (f, gc, x1, y1, x2, y2)
497 x2--; 489 x2--;
498 } 490 }
499 491
500 SetPortWindowPort (FRAME_MAC_WINDOW (f)); 492 mac_begin_clip (f, gc);
501
502 RGBForeColor (GC_FORE_COLOR (gc)); 493 RGBForeColor (GC_FORE_COLOR (gc));
503
504 mac_begin_clip (gc);
505 MoveTo (x1, y1); 494 MoveTo (x1, y1);
506 LineTo (x2, y2); 495 LineTo (x2, y2);
507 mac_end_clip (gc); 496 mac_end_clip (gc);
@@ -557,25 +546,25 @@ mac_erase_rectangle (f, gc, x, y, width, height)
557 unsigned int width, height; 546 unsigned int width, height;
558{ 547{
559#if USE_CG_DRAWING 548#if USE_CG_DRAWING
560 CGContextRef context; 549 {
550 CGContextRef context;
561 551
562 context = mac_begin_cg_clip (f, gc); 552 context = mac_begin_cg_clip (f, gc);
563 CG_SET_FILL_COLOR_WITH_GC_BACKGROUND (context, gc); 553 CG_SET_FILL_COLOR_WITH_GC_BACKGROUND (context, gc);
564 CGContextFillRect (context, CGRectMake (x, y, width, height)); 554 CGContextFillRect (context, CGRectMake (x, y, width, height));
565 mac_end_cg_clip (f); 555 mac_end_cg_clip (f);
556 }
566#else 557#else
567 Rect r; 558 {
568 559 Rect r;
569 SetPortWindowPort (FRAME_MAC_WINDOW (f));
570
571 RGBBackColor (GC_BACK_COLOR (gc));
572 SetRect (&r, x, y, x + width, y + height);
573
574 mac_begin_clip (gc);
575 EraseRect (&r);
576 mac_end_clip (gc);
577 560
578 RGBBackColor (GC_BACK_COLOR (FRAME_NORMAL_GC (f))); 561 mac_begin_clip (f, gc);
562 RGBBackColor (GC_BACK_COLOR (gc));
563 SetRect (&r, x, y, x + width, y + height);
564 EraseRect (&r);
565 RGBBackColor (GC_BACK_COLOR (FRAME_NORMAL_GC (f)));
566 mac_end_clip (gc);
567 }
579#endif 568#endif
580} 569}
581 570
@@ -598,15 +587,17 @@ mac_clear_window (f)
598 struct frame *f; 587 struct frame *f;
599{ 588{
600#if USE_CG_DRAWING 589#if USE_CG_DRAWING
601 CGContextRef context; 590 {
602 GC gc = FRAME_NORMAL_GC (f); 591 CGContextRef context;
603 592 GC gc = FRAME_NORMAL_GC (f);
604 context = mac_begin_cg_clip (f, NULL); 593
605 CG_SET_FILL_COLOR_WITH_GC_BACKGROUND (context, gc); 594 context = mac_begin_cg_clip (f, NULL);
606 CGContextFillRect (context, CGRectMake (0, 0, FRAME_PIXEL_WIDTH (f), 595 CG_SET_FILL_COLOR_WITH_GC_BACKGROUND (context, gc);
607 FRAME_PIXEL_HEIGHT (f))); 596 CGContextFillRect (context, CGRectMake (0, 0, FRAME_PIXEL_WIDTH (f),
608 mac_end_cg_clip (f); 597 FRAME_PIXEL_HEIGHT (f)));
609#else 598 mac_end_cg_clip (f);
599 }
600#else /* !USE_CG_DRAWING */
610 SetPortWindowPort (FRAME_MAC_WINDOW (f)); 601 SetPortWindowPort (FRAME_MAC_WINDOW (f));
611 602
612 RGBBackColor (GC_BACK_COLOR (FRAME_NORMAL_GC (f))); 603 RGBBackColor (GC_BACK_COLOR (FRAME_NORMAL_GC (f)));
@@ -680,13 +671,10 @@ mac_draw_bitmap (f, gc, x, y, width, height, bits, overlay_p)
680 bitmap.baseAddr = (char *)bits; 671 bitmap.baseAddr = (char *)bits;
681 SetRect (&(bitmap.bounds), 0, 0, width, height); 672 SetRect (&(bitmap.bounds), 0, 0, width, height);
682 673
683 SetPortWindowPort (FRAME_MAC_WINDOW (f)); 674 mac_begin_clip (f, gc);
684
685 RGBForeColor (GC_FORE_COLOR (gc)); 675 RGBForeColor (GC_FORE_COLOR (gc));
686 RGBBackColor (GC_BACK_COLOR (gc)); 676 RGBBackColor (GC_BACK_COLOR (gc));
687 SetRect (&r, x, y, x + width, y + height); 677 SetRect (&r, x, y, x + width, y + height);
688
689 mac_begin_clip (gc);
690#if TARGET_API_MAC_CARBON 678#if TARGET_API_MAC_CARBON
691 { 679 {
692 CGrafPtr port; 680 CGrafPtr port;
@@ -701,9 +689,8 @@ mac_draw_bitmap (f, gc, x, y, width, height, bits, overlay_p)
701 CopyBits (&bitmap, &(FRAME_MAC_WINDOW (f)->portBits), &(bitmap.bounds), &r, 689 CopyBits (&bitmap, &(FRAME_MAC_WINDOW (f)->portBits), &(bitmap.bounds), &r,
702 overlay_p ? srcOr : srcCopy, 0); 690 overlay_p ? srcOr : srcCopy, 0);
703#endif /* not TARGET_API_MAC_CARBON */ 691#endif /* not TARGET_API_MAC_CARBON */
704 mac_end_clip (gc);
705
706 RGBBackColor (GC_BACK_COLOR (FRAME_NORMAL_GC (f))); 692 RGBBackColor (GC_BACK_COLOR (FRAME_NORMAL_GC (f)));
693 mac_end_clip (gc);
707} 694}
708#endif /* !USE_CG_DRAWING */ 695#endif /* !USE_CG_DRAWING */
709 696
@@ -754,8 +741,8 @@ mac_free_bitmap (bitmap)
754 741
755Pixmap 742Pixmap
756XCreatePixmap (display, w, width, height, depth) 743XCreatePixmap (display, w, width, height, depth)
757 Display *display; /* not used */ 744 Display *display;
758 WindowPtr w; 745 WindowRef w;
759 unsigned int width, height; 746 unsigned int width, height;
760 unsigned int depth; 747 unsigned int depth;
761{ 748{
@@ -783,8 +770,8 @@ XCreatePixmap (display, w, width, height, depth)
783 770
784Pixmap 771Pixmap
785XCreatePixmapFromBitmapData (display, w, data, width, height, fg, bg, depth) 772XCreatePixmapFromBitmapData (display, w, data, width, height, fg, bg, depth)
786 Display *display; /* not used */ 773 Display *display;
787 WindowPtr w; 774 WindowRef w;
788 char *data; 775 char *data;
789 unsigned int width, height; 776 unsigned int width, height;
790 unsigned long fg, bg; 777 unsigned long fg, bg;
@@ -794,7 +781,7 @@ XCreatePixmapFromBitmapData (display, w, data, width, height, fg, bg, depth)
794 BitMap bitmap; 781 BitMap bitmap;
795 CGrafPtr old_port; 782 CGrafPtr old_port;
796 GDHandle old_gdh; 783 GDHandle old_gdh;
797 static GC gc = NULL; /* not reentrant */ 784 static GC gc = NULL;
798 785
799 if (gc == NULL) 786 if (gc == NULL)
800 gc = XCreateGC (display, w, 0, NULL); 787 gc = XCreateGC (display, w, 0, NULL);
@@ -826,6 +813,15 @@ XCreatePixmapFromBitmapData (display, w, data, width, height, fg, bg, depth)
826} 813}
827 814
828 815
816void
817XFreePixmap (display, pixmap)
818 Display *display;
819 Pixmap pixmap;
820{
821 DisposeGWorld (pixmap);
822}
823
824
829/* Mac replacement for XFillRectangle. */ 825/* Mac replacement for XFillRectangle. */
830 826
831static void 827static void
@@ -845,12 +841,9 @@ mac_fill_rectangle (f, gc, x, y, width, height)
845#else 841#else
846 Rect r; 842 Rect r;
847 843
848 SetPortWindowPort (FRAME_MAC_WINDOW (f)); 844 mac_begin_clip (f, gc);
849
850 RGBForeColor (GC_FORE_COLOR (gc)); 845 RGBForeColor (GC_FORE_COLOR (gc));
851 SetRect (&r, x, y, x + width, y + height); 846 SetRect (&r, x, y, x + width, y + height);
852
853 mac_begin_clip (gc);
854 PaintRect (&r); /* using foreground color of gc */ 847 PaintRect (&r); /* using foreground color of gc */
855 mac_end_clip (gc); 848 mac_end_clip (gc);
856#endif 849#endif
@@ -877,18 +870,34 @@ mac_draw_rectangle (f, gc, x, y, width, height)
877#else 870#else
878 Rect r; 871 Rect r;
879 872
880 SetPortWindowPort (FRAME_MAC_WINDOW (f)); 873 mac_begin_clip (f, gc);
881
882 RGBForeColor (GC_FORE_COLOR (gc)); 874 RGBForeColor (GC_FORE_COLOR (gc));
883 SetRect (&r, x, y, x + width + 1, y + height + 1); 875 SetRect (&r, x, y, x + width + 1, y + height + 1);
884
885 mac_begin_clip (gc);
886 FrameRect (&r); /* using foreground color of gc */ 876 FrameRect (&r); /* using foreground color of gc */
887 mac_end_clip (gc); 877 mac_end_clip (gc);
888#endif 878#endif
889} 879}
890 880
891 881
882static void
883mac_invert_rectangle (f, x, y, width, height)
884 struct frame *f;
885 int x, y;
886 unsigned int width, height;
887{
888 Rect r;
889
890#if USE_CG_DRAWING
891 mac_prepare_for_quickdraw (f);
892#endif
893 SetPortWindowPort (FRAME_MAC_WINDOW (f));
894
895 SetRect (&r, x, y, x + width, y + height);
896
897 InvertRect (&r);
898}
899
900
892#if USE_ATSUI 901#if USE_ATSUI
893static OSStatus 902static OSStatus
894atsu_get_text_layout_with_text_ptr (text, text_length, style, text_layout) 903atsu_get_text_layout_with_text_ptr (text, text_length, style, text_layout)
@@ -898,7 +907,7 @@ atsu_get_text_layout_with_text_ptr (text, text_length, style, text_layout)
898 ATSUTextLayout *text_layout; 907 ATSUTextLayout *text_layout;
899{ 908{
900 OSStatus err; 909 OSStatus err;
901 static ATSUTextLayout saved_text_layout = NULL; /* not reentrant */ 910 static ATSUTextLayout saved_text_layout = NULL;
902 911
903 if (saved_text_layout == NULL) 912 if (saved_text_layout == NULL)
904 { 913 {
@@ -925,7 +934,6 @@ atsu_get_text_layout_with_text_ptr (text, text_length, style, text_layout)
925 err = ATSUSetLayoutControls (saved_text_layout, 934 err = ATSUSetLayoutControls (saved_text_layout,
926 sizeof (tags) / sizeof (tags[0]), 935 sizeof (tags) / sizeof (tags[0]),
927 tags, sizes, values); 936 tags, sizes, values);
928 /* XXX: Should we do this? */
929 if (err == noErr) 937 if (err == noErr)
930 err = ATSUSetTransientFontMatching (saved_text_layout, true); 938 err = ATSUSetTransientFontMatching (saved_text_layout, true);
931 } 939 }
@@ -944,224 +952,224 @@ atsu_get_text_layout_with_text_ptr (text, text_length, style, text_layout)
944 *text_layout = saved_text_layout; 952 *text_layout = saved_text_layout;
945 return err; 953 return err;
946} 954}
947#endif
948 955
949 956
950static void 957static void
951mac_invert_rectangle (f, x, y, width, height) 958mac_draw_image_string_atsui (f, gc, x, y, buf, nchars, bg_width,
952 struct frame *f; 959 overstrike_p, bytes_per_char)
953 int x, y;
954 unsigned int width, height;
955{
956 Rect r;
957
958#if USE_CG_DRAWING
959 mac_prepare_for_quickdraw (f);
960#endif
961 SetPortWindowPort (FRAME_MAC_WINDOW (f));
962
963 SetRect (&r, x, y, x + width, y + height);
964
965 InvertRect (&r);
966}
967
968
969static void
970mac_draw_string_common (f, gc, x, y, buf, nchars, bg_width,
971 overstrike_p, bytes_per_char)
972 struct frame *f; 960 struct frame *f;
973 GC gc; 961 GC gc;
974 int x, y; 962 int x, y;
975 char *buf; 963 char *buf;
976 int nchars, bg_width, overstrike_p, bytes_per_char; 964 int nchars, bg_width, overstrike_p, bytes_per_char;
977{ 965{
978 SetPortWindowPort (FRAME_MAC_WINDOW (f)); 966 OSStatus err;
979 967 ATSUTextLayout text_layout;
980#if USE_ATSUI
981 if (GC_FONT (gc)->mac_style)
982 {
983 OSStatus err;
984 ATSUTextLayout text_layout;
985 968
986 xassert (bytes_per_char == 2); 969 xassert (bytes_per_char == 2);
987 970
988#ifndef WORDS_BIG_ENDIAN 971#ifndef WORDS_BIG_ENDIAN
989 { 972 {
990 int i; 973 int i;
991 UniChar *text = (UniChar *)buf; 974 UniChar *text = (UniChar *)buf;
992 975
993 for (i = 0; i < nchars; i++) 976 for (i = 0; i < nchars; i++)
994 text[i] = EndianU16_BtoN (text[i]); 977 text[i] = EndianU16_BtoN (text[i]);
995 } 978 }
996#endif 979#endif
997 err = atsu_get_text_layout_with_text_ptr ((ConstUniCharArrayPtr)buf, 980 err = atsu_get_text_layout_with_text_ptr ((ConstUniCharArrayPtr)buf,
998 nchars, 981 nchars,
999 GC_FONT (gc)->mac_style, 982 GC_FONT (gc)->mac_style,
1000 &text_layout); 983 &text_layout);
1001 if (err != noErr) 984 if (err != noErr)
1002 return; 985 return;
1003#ifdef MAC_OSX 986#ifdef MAC_OSX
1004 if (!mac_use_core_graphics) 987 if (!mac_use_core_graphics)
1005 { 988 {
1006#endif
1007#if USE_CG_DRAWING
1008 mac_prepare_for_quickdraw (f);
1009#endif 989#endif
1010 mac_begin_clip (gc); 990 mac_begin_clip (f, gc);
1011 RGBForeColor (GC_FORE_COLOR (gc)); 991 RGBForeColor (GC_FORE_COLOR (gc));
1012 if (bg_width) 992 if (bg_width)
1013 { 993 {
1014 Rect r; 994 Rect r;
1015 995
1016 SetRect (&r, x, y - FONT_BASE (GC_FONT (gc)), 996 SetRect (&r, x, y - FONT_BASE (GC_FONT (gc)),
1017 x + bg_width, y + FONT_DESCENT (GC_FONT (gc))); 997 x + bg_width, y + FONT_DESCENT (GC_FONT (gc)));
1018 RGBBackColor (GC_BACK_COLOR (gc)); 998 RGBBackColor (GC_BACK_COLOR (gc));
1019 EraseRect (&r); 999 EraseRect (&r);
1020 RGBBackColor (GC_BACK_COLOR (FRAME_NORMAL_GC (f))); 1000 RGBBackColor (GC_BACK_COLOR (FRAME_NORMAL_GC (f)));
1021 } 1001 }
1022 MoveTo (x, y); 1002 MoveTo (x, y);
1003 ATSUDrawText (text_layout,
1004 kATSUFromTextBeginning, kATSUToTextEnd,
1005 kATSUUseGrafPortPenLoc, kATSUUseGrafPortPenLoc);
1006 if (overstrike_p)
1007 {
1008 MoveTo (x + 1, y);
1023 ATSUDrawText (text_layout, 1009 ATSUDrawText (text_layout,
1024 kATSUFromTextBeginning, kATSUToTextEnd, 1010 kATSUFromTextBeginning, kATSUToTextEnd,
1025 kATSUUseGrafPortPenLoc, kATSUUseGrafPortPenLoc); 1011 kATSUUseGrafPortPenLoc, kATSUUseGrafPortPenLoc);
1026 if (overstrike_p)
1027 {
1028 MoveTo (x + 1, y);
1029 ATSUDrawText (text_layout,
1030 kATSUFromTextBeginning, kATSUToTextEnd,
1031 kATSUUseGrafPortPenLoc, kATSUUseGrafPortPenLoc);
1032 }
1033 mac_end_clip (gc);
1034#ifdef MAC_OSX
1035 } 1012 }
1036 else 1013 mac_end_clip (gc);
1037 { 1014#ifdef MAC_OSX
1038 CGrafPtr port; 1015 }
1039 static CGContextRef context; 1016 else
1040 float port_height = FRAME_PIXEL_HEIGHT (f); 1017 {
1041 static const ATSUAttributeTag tags[] = {kATSUCGContextTag}; 1018 static CGContextRef context;
1042 static const ByteCount sizes[] = {sizeof (CGContextRef)}; 1019 float port_height = FRAME_PIXEL_HEIGHT (f);
1043 static const ATSUAttributeValuePtr values[] = {&context}; 1020 static const ATSUAttributeTag tags[] = {kATSUCGContextTag};
1021 static const ByteCount sizes[] = {sizeof (CGContextRef)};
1022 static const ATSUAttributeValuePtr values[] = {&context};
1044 1023
1045#if USE_CG_DRAWING 1024#if USE_CG_DRAWING
1046 context = mac_begin_cg_clip (f, gc); 1025 context = mac_begin_cg_clip (f, gc);
1047#else 1026#else
1048 GetPort (&port); 1027 CGrafPtr port;
1049 QDBeginCGContext (port, &context); 1028
1050 if (gc->n_clip_rects || bg_width) 1029 GetPort (&port);
1051 { 1030 QDBeginCGContext (port, &context);
1052 CGContextTranslateCTM (context, 0, port_height); 1031 if (gc->n_clip_rects || bg_width)
1053 CGContextScaleCTM (context, 1, -1); 1032 {
1054 if (gc->n_clip_rects) 1033 CGContextTranslateCTM (context, 0, port_height);
1055 CGContextClipToRects (context, gc->clip_rects, 1034 CGContextScaleCTM (context, 1, -1);
1056 gc->n_clip_rects); 1035 if (gc->n_clip_rects)
1057#endif 1036 CGContextClipToRects (context, gc->clip_rects,
1058 if (bg_width) 1037 gc->n_clip_rects);
1059 {
1060 CG_SET_FILL_COLOR_WITH_GC_BACKGROUND (context, gc);
1061 CGContextFillRect
1062 (context,
1063 CGRectMake (x, y - FONT_BASE (GC_FONT (gc)),
1064 bg_width, FONT_HEIGHT (GC_FONT (gc))));
1065 }
1066 CGContextScaleCTM (context, 1, -1);
1067 CGContextTranslateCTM (context, 0, -port_height);
1068#if !USE_CG_DRAWING
1069 }
1070#endif 1038#endif
1071 CG_SET_FILL_COLOR_WITH_GC_FOREGROUND (context, gc); 1039 if (bg_width)
1072 err = ATSUSetLayoutControls (text_layout,
1073 sizeof (tags) / sizeof (tags[0]),
1074 tags, sizes, values);
1075 if (err == noErr)
1076 { 1040 {
1077 ATSUDrawText (text_layout, 1041 CG_SET_FILL_COLOR_WITH_GC_BACKGROUND (context, gc);
1078 kATSUFromTextBeginning, kATSUToTextEnd, 1042 CGContextFillRect (context,
1079 Long2Fix (x), Long2Fix (port_height - y)); 1043 CGRectMake (x, y - FONT_BASE (GC_FONT (gc)),
1080 if (overstrike_p) 1044 bg_width,
1081 ATSUDrawText (text_layout, 1045 FONT_HEIGHT (GC_FONT (gc))));
1082 kATSUFromTextBeginning, kATSUToTextEnd,
1083 Long2Fix (x + 1), Long2Fix (port_height - y));
1084 } 1046 }
1047 CGContextScaleCTM (context, 1, -1);
1048 CGContextTranslateCTM (context, 0, -port_height);
1049#if !USE_CG_DRAWING
1050 }
1051#endif
1052 CG_SET_FILL_COLOR_WITH_GC_FOREGROUND (context, gc);
1053 err = ATSUSetLayoutControls (text_layout,
1054 sizeof (tags) / sizeof (tags[0]),
1055 tags, sizes, values);
1056 if (err == noErr)
1057 {
1058 ATSUDrawText (text_layout,
1059 kATSUFromTextBeginning, kATSUToTextEnd,
1060 Long2Fix (x), Long2Fix (port_height - y));
1061 if (overstrike_p)
1062 ATSUDrawText (text_layout,
1063 kATSUFromTextBeginning, kATSUToTextEnd,
1064 Long2Fix (x + 1), Long2Fix (port_height - y));
1065 }
1085#if USE_CG_DRAWING 1066#if USE_CG_DRAWING
1086 mac_end_cg_clip (f); 1067 mac_end_cg_clip (f);
1087 context = NULL; 1068 context = NULL;
1088#else 1069#else
1089 CGContextSynchronize (context); 1070 CGContextSynchronize (context);
1090 QDEndCGContext (port, &context); 1071 QDEndCGContext (port, &context);
1091#endif 1072#endif
1092#if 0 1073#if 0
1093 /* This doesn't work on Mac OS X 10.1. */ 1074 /* This doesn't work on Mac OS X 10.1. */
1094 ATSUClearLayoutControls (text_layout, 1075 ATSUClearLayoutControls (text_layout,
1095 sizeof (tags) / sizeof (tags[0]), tags); 1076 sizeof (tags) / sizeof (tags[0]), tags);
1096#else 1077#else
1097 ATSUSetLayoutControls (text_layout, 1078 ATSUSetLayoutControls (text_layout,
1098 sizeof (tags) / sizeof (tags[0]), 1079 sizeof (tags) / sizeof (tags[0]),
1099 tags, sizes, values); 1080 tags, sizes, values);
1100#endif 1081#endif
1101 }
1102#endif /* MAC_OSX */
1103 } 1082 }
1104 else 1083#endif /* MAC_OSX */
1084}
1105#endif /* USE_ATSUI */ 1085#endif /* USE_ATSUI */
1106 {
1107#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1020
1108 UInt32 savedFlags;
1109 1086
1110 if (mac_use_core_graphics) 1087
1111 savedFlags = SwapQDTextFlags (kQDUseCGTextRendering); 1088static void
1089mac_draw_image_string_qd (f, gc, x, y, buf, nchars, bg_width,
1090 overstrike_p, bytes_per_char)
1091 struct frame *f;
1092 GC gc;
1093 int x, y;
1094 char *buf;
1095 int nchars, bg_width, overstrike_p, bytes_per_char;
1096{
1097#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1020
1098 UInt32 savedFlags;
1112#endif 1099#endif
1113#if USE_CG_DRAWING 1100
1114 mac_prepare_for_quickdraw (f); 1101 mac_begin_clip (f, gc);
1102#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1020
1103 if (mac_use_core_graphics)
1104 savedFlags = SwapQDTextFlags (kQDUseCGTextRendering);
1115#endif 1105#endif
1116 mac_begin_clip (gc); 1106 RGBForeColor (GC_FORE_COLOR (gc));
1117 RGBForeColor (GC_FORE_COLOR (gc));
1118#ifdef MAC_OS8 1107#ifdef MAC_OS8
1119 if (bg_width) 1108 if (bg_width)
1120 { 1109 {
1121 RGBBackColor (GC_BACK_COLOR (gc)); 1110 RGBBackColor (GC_BACK_COLOR (gc));
1122 TextMode (srcCopy); 1111 TextMode (srcCopy);
1123 } 1112 }
1124 else 1113 else
1125 TextMode (srcOr); 1114 TextMode (srcOr);
1126#else 1115#else
1127 /* We prefer not to use srcCopy text transfer mode on Mac OS X 1116 /* We prefer not to use srcCopy text transfer mode on Mac OS X
1128 because: 1117 because:
1129 - Screen is double-buffered. (In srcCopy mode, a text is 1118 - Screen is double-buffered. (In srcCopy mode, a text is drawn
1130 drawn into an offscreen graphics world first. So 1119 into an offscreen graphics world first. So performance gain
1131 performance gain cannot be expected.) 1120 cannot be expected.)
1132 - It lowers rendering quality. 1121 - It lowers rendering quality.
1133 - Some fonts leave garbage on cursor movement. */ 1122 - Some fonts leave garbage on cursor movement. */
1134 if (bg_width) 1123 if (bg_width)
1135 { 1124 {
1136 Rect r; 1125 Rect r;
1137 1126
1138 RGBBackColor (GC_BACK_COLOR (gc)); 1127 RGBBackColor (GC_BACK_COLOR (gc));
1139 SetRect (&r, x, y - FONT_BASE (GC_FONT (gc)), 1128 SetRect (&r, x, y - FONT_BASE (GC_FONT (gc)),
1140 x + bg_width, y + FONT_DESCENT (GC_FONT (gc))); 1129 x + bg_width, y + FONT_DESCENT (GC_FONT (gc)));
1141 EraseRect (&r); 1130 EraseRect (&r);
1142 } 1131 }
1143 TextMode (srcOr); 1132 TextMode (srcOr);
1144#endif 1133#endif
1145 TextFont (GC_FONT (gc)->mac_fontnum); 1134 TextFont (GC_FONT (gc)->mac_fontnum);
1146 TextSize (GC_FONT (gc)->mac_fontsize); 1135 TextSize (GC_FONT (gc)->mac_fontsize);
1147 TextFace (GC_FONT (gc)->mac_fontface); 1136 TextFace (GC_FONT (gc)->mac_fontface);
1148 MoveTo (x, y); 1137 MoveTo (x, y);
1138 DrawText (buf, 0, nchars * bytes_per_char);
1139 if (overstrike_p)
1140 {
1141 TextMode (srcOr);
1142 MoveTo (x + 1, y);
1149 DrawText (buf, 0, nchars * bytes_per_char); 1143 DrawText (buf, 0, nchars * bytes_per_char);
1150 if (overstrike_p) 1144 }
1151 { 1145 if (bg_width)
1152 TextMode (srcOr); 1146 RGBBackColor (GC_BACK_COLOR (FRAME_NORMAL_GC (f)));
1153 MoveTo (x + 1, y); 1147 mac_end_clip (gc);
1154 DrawText (buf, 0, nchars * bytes_per_char);
1155 }
1156 if (bg_width)
1157 RGBBackColor (GC_BACK_COLOR (FRAME_NORMAL_GC (f)));
1158 mac_end_clip (gc);
1159 1148
1160#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1020 1149#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1020
1161 if (mac_use_core_graphics) 1150 if (mac_use_core_graphics)
1162 SwapQDTextFlags(savedFlags); 1151 SwapQDTextFlags(savedFlags);
1163#endif 1152#endif
1164 } 1153}
1154
1155
1156static INLINE void
1157mac_draw_string_common (f, gc, x, y, buf, nchars, bg_width,
1158 overstrike_p, bytes_per_char)
1159 struct frame *f;
1160 GC gc;
1161 int x, y;
1162 char *buf;
1163 int nchars, bg_width, overstrike_p, bytes_per_char;
1164{
1165#if USE_ATSUI
1166 if (GC_FONT (gc)->mac_style)
1167 mac_draw_image_string_atsui (f, gc, x, y, buf, nchars, bg_width,
1168 overstrike_p, bytes_per_char);
1169 else
1170#endif /* USE_ATSUI */
1171 mac_draw_image_string_qd (f, gc, x, y, buf, nchars, bg_width,
1172 overstrike_p, bytes_per_char);
1165} 1173}
1166 1174
1167 1175
@@ -1378,7 +1386,6 @@ mac_draw_image_string_cg (f, gc, x, y, buf, nchars, bg_width, overstrike_p)
1378 XChar2b *buf; 1386 XChar2b *buf;
1379 int nchars, bg_width, overstrike_p; 1387 int nchars, bg_width, overstrike_p;
1380{ 1388{
1381 CGrafPtr port;
1382 float port_height, gx, gy; 1389 float port_height, gx, gy;
1383 int i; 1390 int i;
1384 CGContextRef context; 1391 CGContextRef context;
@@ -1388,7 +1395,6 @@ mac_draw_image_string_cg (f, gc, x, y, buf, nchars, bg_width, overstrike_p)
1388 if (!mac_use_core_graphics || GC_FONT (gc)->cg_font == NULL) 1395 if (!mac_use_core_graphics || GC_FONT (gc)->cg_font == NULL)
1389 return 0; 1396 return 0;
1390 1397
1391 port = GetWindowPort (FRAME_MAC_WINDOW (f));
1392 port_height = FRAME_PIXEL_HEIGHT (f); 1398 port_height = FRAME_PIXEL_HEIGHT (f);
1393 gx = x; 1399 gx = x;
1394 gy = port_height - y; 1400 gy = port_height - y;
@@ -1409,7 +1415,7 @@ mac_draw_image_string_cg (f, gc, x, y, buf, nchars, bg_width, overstrike_p)
1409#if USE_CG_DRAWING 1415#if USE_CG_DRAWING
1410 context = mac_begin_cg_clip (f, gc); 1416 context = mac_begin_cg_clip (f, gc);
1411#else 1417#else
1412 QDBeginCGContext (port, &context); 1418 QDBeginCGContext (GetWindowPort (FRAME_MAC_WINDOW (f)), &context);
1413 if (gc->n_clip_rects || bg_width) 1419 if (gc->n_clip_rects || bg_width)
1414 { 1420 {
1415 CGContextTranslateCTM (context, 0, port_height); 1421 CGContextTranslateCTM (context, 0, port_height);
@@ -1449,7 +1455,7 @@ mac_draw_image_string_cg (f, gc, x, y, buf, nchars, bg_width, overstrike_p)
1449 } 1455 }
1450 } 1456 }
1451#if MAC_OS_X_VERSION_MIN_REQUIRED == 1020 1457#if MAC_OS_X_VERSION_MIN_REQUIRED == 1020
1452 else 1458 else /* CGContextShowGlyphsWithAdvances == NULL */
1453#endif 1459#endif
1454#endif /* MAC_OS_X_VERSION_MAX_ALLOWED >= 1030 */ 1460#endif /* MAC_OS_X_VERSION_MAX_ALLOWED >= 1030 */
1455#if MAC_OS_X_VERSION_MAX_ALLOWED < 1030 || MAC_OS_X_VERSION_MIN_REQUIRED == 1020 1461#if MAC_OS_X_VERSION_MAX_ALLOWED < 1030 || MAC_OS_X_VERSION_MIN_REQUIRED == 1020
@@ -1467,7 +1473,7 @@ mac_draw_image_string_cg (f, gc, x, y, buf, nchars, bg_width, overstrike_p)
1467 mac_end_cg_clip (f); 1473 mac_end_cg_clip (f);
1468#else 1474#else
1469 CGContextSynchronize (context); 1475 CGContextSynchronize (context);
1470 QDEndCGContext (port, &context); 1476 QDEndCGContext (GetWindowPort (FRAME_MAC_WINDOW (f)), &context);
1471#endif 1477#endif
1472 1478
1473 return 1; 1479 return 1;
@@ -1489,7 +1495,7 @@ mac_copy_area (src, f, gc, src_x, src_y, width, height, dest_x, dest_y)
1489{ 1495{
1490 Rect src_r, dest_r; 1496 Rect src_r, dest_r;
1491 1497
1492 SetPortWindowPort (FRAME_MAC_WINDOW (f)); 1498 mac_begin_clip (f, gc);
1493 1499
1494 SetRect (&src_r, src_x, src_y, src_x + width, src_y + height); 1500 SetRect (&src_r, src_x, src_y, src_x + width, src_y + height);
1495 SetRect (&dest_r, dest_x, dest_y, dest_x + width, dest_y + height); 1501 SetRect (&dest_r, dest_x, dest_y, dest_x + width, dest_y + height);
@@ -1497,7 +1503,6 @@ mac_copy_area (src, f, gc, src_x, src_y, width, height, dest_x, dest_y)
1497 ForeColor (blackColor); 1503 ForeColor (blackColor);
1498 BackColor (whiteColor); 1504 BackColor (whiteColor);
1499 1505
1500 mac_begin_clip (gc);
1501 LockPixels (GetGWorldPixMap (src)); 1506 LockPixels (GetGWorldPixMap (src));
1502#if TARGET_API_MAC_CARBON 1507#if TARGET_API_MAC_CARBON
1503 { 1508 {
@@ -1515,9 +1520,10 @@ mac_copy_area (src, f, gc, src_x, src_y, width, height, dest_x, dest_y)
1515 &src_r, &dest_r, srcCopy, 0); 1520 &src_r, &dest_r, srcCopy, 0);
1516#endif /* not TARGET_API_MAC_CARBON */ 1521#endif /* not TARGET_API_MAC_CARBON */
1517 UnlockPixels (GetGWorldPixMap (src)); 1522 UnlockPixels (GetGWorldPixMap (src));
1518 mac_end_clip (gc);
1519 1523
1520 RGBBackColor (GC_BACK_COLOR (FRAME_NORMAL_GC (f))); 1524 RGBBackColor (GC_BACK_COLOR (FRAME_NORMAL_GC (f)));
1525
1526 mac_end_clip (gc);
1521} 1527}
1522 1528
1523 1529
@@ -1533,7 +1539,7 @@ mac_copy_area_with_mask (src, mask, f, gc, src_x, src_y,
1533{ 1539{
1534 Rect src_r, dest_r; 1540 Rect src_r, dest_r;
1535 1541
1536 SetPortWindowPort (FRAME_MAC_WINDOW (f)); 1542 mac_begin_clip (f, gc);
1537 1543
1538 SetRect (&src_r, src_x, src_y, src_x + width, src_y + height); 1544 SetRect (&src_r, src_x, src_y, src_x + width, src_y + height);
1539 SetRect (&dest_r, dest_x, dest_y, dest_x + width, dest_y + height); 1545 SetRect (&dest_r, dest_x, dest_y, dest_x + width, dest_y + height);
@@ -1541,7 +1547,6 @@ mac_copy_area_with_mask (src, mask, f, gc, src_x, src_y,
1541 ForeColor (blackColor); 1547 ForeColor (blackColor);
1542 BackColor (whiteColor); 1548 BackColor (whiteColor);
1543 1549
1544 mac_begin_clip (gc);
1545 LockPixels (GetGWorldPixMap (src)); 1550 LockPixels (GetGWorldPixMap (src));
1546 LockPixels (GetGWorldPixMap (mask)); 1551 LockPixels (GetGWorldPixMap (mask));
1547#if TARGET_API_MAC_CARBON 1552#if TARGET_API_MAC_CARBON
@@ -1561,9 +1566,10 @@ mac_copy_area_with_mask (src, mask, f, gc, src_x, src_y,
1561#endif /* not TARGET_API_MAC_CARBON */ 1566#endif /* not TARGET_API_MAC_CARBON */
1562 UnlockPixels (GetGWorldPixMap (mask)); 1567 UnlockPixels (GetGWorldPixMap (mask));
1563 UnlockPixels (GetGWorldPixMap (src)); 1568 UnlockPixels (GetGWorldPixMap (src));
1564 mac_end_clip (gc);
1565 1569
1566 RGBBackColor (GC_BACK_COLOR (FRAME_NORMAL_GC (f))); 1570 RGBBackColor (GC_BACK_COLOR (FRAME_NORMAL_GC (f)));
1571
1572 mac_end_clip (gc);
1567} 1573}
1568#endif /* !USE_CG_DRAWING */ 1574#endif /* !USE_CG_DRAWING */
1569 1575
@@ -1592,9 +1598,9 @@ mac_scroll_area (f, gc, src_x, src_y, width, height, dest_x, dest_y)
1592 DisposeRgn (dummy); 1598 DisposeRgn (dummy);
1593#else /* not TARGET_API_MAC_CARBON */ 1599#else /* not TARGET_API_MAC_CARBON */
1594 Rect src_r, dest_r; 1600 Rect src_r, dest_r;
1595 WindowPtr w = FRAME_MAC_WINDOW (f); 1601 WindowRef w = FRAME_MAC_WINDOW (f);
1596 1602
1597 SetPort (w); 1603 mac_begin_clip (f, gc);
1598 1604
1599 SetRect (&src_r, src_x, src_y, src_x + width, src_y + height); 1605 SetRect (&src_r, src_x, src_y, src_x + width, src_y + height);
1600 SetRect (&dest_r, dest_x, dest_y, dest_x + width, dest_y + height); 1606 SetRect (&dest_r, dest_x, dest_y, dest_x + width, dest_y + height);
@@ -1603,11 +1609,11 @@ mac_scroll_area (f, gc, src_x, src_y, width, height, dest_x, dest_y)
1603 color mapping in CopyBits. Otherwise, it will be slow. */ 1609 color mapping in CopyBits. Otherwise, it will be slow. */
1604 ForeColor (blackColor); 1610 ForeColor (blackColor);
1605 BackColor (whiteColor); 1611 BackColor (whiteColor);
1606 mac_begin_clip (gc);
1607 CopyBits (&(w->portBits), &(w->portBits), &src_r, &dest_r, srcCopy, 0); 1612 CopyBits (&(w->portBits), &(w->portBits), &src_r, &dest_r, srcCopy, 0);
1608 mac_end_clip (gc);
1609 1613
1610 RGBBackColor (GC_BACK_COLOR (FRAME_NORMAL_GC (f))); 1614 RGBBackColor (GC_BACK_COLOR (FRAME_NORMAL_GC (f)));
1615
1616 mac_end_clip (gc);
1611#endif /* not TARGET_API_MAC_CARBON */ 1617#endif /* not TARGET_API_MAC_CARBON */
1612} 1618}
1613 1619
@@ -1852,7 +1858,7 @@ mac_reset_clip_rectangles (display, gc)
1852void 1858void
1853XSetWindowBackground (display, w, color) 1859XSetWindowBackground (display, w, color)
1854 Display *display; 1860 Display *display;
1855 WindowPtr w; 1861 WindowRef w;
1856 unsigned long color; 1862 unsigned long color;
1857{ 1863{
1858#if !TARGET_API_MAC_CARBON 1864#if !TARGET_API_MAC_CARBON
@@ -4368,7 +4374,7 @@ x_detect_focus_change (dpyinfo, event, bufp)
4368{ 4374{
4369 struct frame *frame; 4375 struct frame *frame;
4370 4376
4371 frame = mac_window_to_frame ((WindowPtr) event->message); 4377 frame = mac_window_to_frame ((WindowRef) event->message);
4372 if (! frame) 4378 if (! frame)
4373 return; 4379 return;
4374 4380
@@ -4608,8 +4614,14 @@ XTmouse_position (fp, insist, bar_window, part, x, y, time)
4608 the frame are divided into. */ 4614 the frame are divided into. */
4609 Point mouse_pos; 4615 Point mouse_pos;
4610 4616
4617#if TARGET_API_MAC_CARBON
4618 GetGlobalMouse (&mouse_pos);
4619 mouse_pos.h -= f1->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f1);
4620 mouse_pos.v -= f1->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f1);
4621#else
4611 SetPortWindowPort (FRAME_MAC_WINDOW (f1)); 4622 SetPortWindowPort (FRAME_MAC_WINDOW (f1));
4612 GetMouse (&mouse_pos); 4623 GetMouse (&mouse_pos);
4624#endif
4613 remember_mouse_glyph (f1, mouse_pos.h, mouse_pos.v, 4625 remember_mouse_glyph (f1, mouse_pos.h, mouse_pos.v,
4614 &last_mouse_glyph); 4626 &last_mouse_glyph);
4615 last_mouse_glyph_frame = f1; 4627 last_mouse_glyph_frame = f1;
@@ -4639,14 +4651,14 @@ static OSStatus set_scroll_bar_timer P_ ((EventTimerInterval));
4639static int control_part_code_to_scroll_bar_part P_ ((ControlPartCode)); 4651static int control_part_code_to_scroll_bar_part P_ ((ControlPartCode));
4640static void construct_scroll_bar_click P_ ((struct scroll_bar *, int, 4652static void construct_scroll_bar_click P_ ((struct scroll_bar *, int,
4641 struct input_event *)); 4653 struct input_event *));
4642static OSStatus get_control_part_bounds P_ ((ControlHandle, ControlPartCode, 4654static OSStatus get_control_part_bounds P_ ((ControlRef, ControlPartCode,
4643 Rect *)); 4655 Rect *));
4644static void x_scroll_bar_handle_press P_ ((struct scroll_bar *, 4656static void x_scroll_bar_handle_press P_ ((struct scroll_bar *,
4645 ControlPartCode, Point, 4657 ControlPartCode, Point,
4646 struct input_event *)); 4658 struct input_event *));
4647static void x_scroll_bar_handle_release P_ ((struct scroll_bar *, 4659static void x_scroll_bar_handle_release P_ ((struct scroll_bar *,
4648 struct input_event *)); 4660 struct input_event *));
4649static void x_scroll_bar_handle_drag P_ ((WindowPtr, struct scroll_bar *, 4661static void x_scroll_bar_handle_drag P_ ((WindowRef, struct scroll_bar *,
4650 Point, struct input_event *)); 4662 Point, struct input_event *));
4651static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *, 4663static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
4652 int, int, int)); 4664 int, int, int));
@@ -4739,7 +4751,7 @@ construct_scroll_bar_click (bar, part, bufp)
4739 4751
4740static OSStatus 4752static OSStatus
4741get_control_part_bounds (ch, part_code, rect) 4753get_control_part_bounds (ch, part_code, rect)
4742 ControlHandle ch; 4754 ControlRef ch;
4743 ControlPartCode part_code; 4755 ControlPartCode part_code;
4744 Rect *rect; 4756 Rect *rect;
4745{ 4757{
@@ -4769,7 +4781,7 @@ x_scroll_bar_handle_press (bar, part_code, mouse_pos, bufp)
4769 if (part != scroll_bar_handle) 4781 if (part != scroll_bar_handle)
4770 { 4782 {
4771 construct_scroll_bar_click (bar, part, bufp); 4783 construct_scroll_bar_click (bar, part, bufp);
4772 HiliteControl (SCROLL_BAR_CONTROL_HANDLE (bar), part_code); 4784 HiliteControl (SCROLL_BAR_CONTROL_REF (bar), part_code);
4773 set_scroll_bar_timer (SCROLL_BAR_FIRST_DELAY); 4785 set_scroll_bar_timer (SCROLL_BAR_FIRST_DELAY);
4774 bar->dragging = Qnil; 4786 bar->dragging = Qnil;
4775 } 4787 }
@@ -4777,7 +4789,7 @@ x_scroll_bar_handle_press (bar, part_code, mouse_pos, bufp)
4777 { 4789 {
4778 Rect r; 4790 Rect r;
4779 4791
4780 get_control_part_bounds (SCROLL_BAR_CONTROL_HANDLE (bar), 4792 get_control_part_bounds (SCROLL_BAR_CONTROL_REF (bar),
4781 kControlIndicatorPart, &r); 4793 kControlIndicatorPart, &r);
4782 XSETINT (bar->dragging, - (mouse_pos.v - r.top) - 1); 4794 XSETINT (bar->dragging, - (mouse_pos.v - r.top) - 1);
4783 } 4795 }
@@ -4795,7 +4807,7 @@ x_scroll_bar_handle_release (bar, bufp)
4795 || (INTEGERP (bar->dragging) && XINT (bar->dragging) >= 0)) 4807 || (INTEGERP (bar->dragging) && XINT (bar->dragging) >= 0))
4796 construct_scroll_bar_click (bar, scroll_bar_end_scroll, bufp); 4808 construct_scroll_bar_click (bar, scroll_bar_end_scroll, bufp);
4797 4809
4798 HiliteControl (SCROLL_BAR_CONTROL_HANDLE (bar), 0); 4810 HiliteControl (SCROLL_BAR_CONTROL_REF (bar), 0);
4799 set_scroll_bar_timer (kEventDurationForever); 4811 set_scroll_bar_timer (kEventDurationForever);
4800 4812
4801 last_scroll_bar_part = -1; 4813 last_scroll_bar_part = -1;
@@ -4805,19 +4817,19 @@ x_scroll_bar_handle_release (bar, bufp)
4805 4817
4806static void 4818static void
4807x_scroll_bar_handle_drag (win, bar, mouse_pos, bufp) 4819x_scroll_bar_handle_drag (win, bar, mouse_pos, bufp)
4808 WindowPtr win; 4820 WindowRef win;
4809 struct scroll_bar *bar; 4821 struct scroll_bar *bar;
4810 Point mouse_pos; 4822 Point mouse_pos;
4811 struct input_event *bufp; 4823 struct input_event *bufp;
4812{ 4824{
4813 ControlHandle ch = SCROLL_BAR_CONTROL_HANDLE (bar); 4825 ControlRef ch = SCROLL_BAR_CONTROL_REF (bar);
4814 4826
4815 if (last_scroll_bar_part == scroll_bar_handle) 4827 if (last_scroll_bar_part == scroll_bar_handle)
4816 { 4828 {
4817 int top, top_range; 4829 int top, top_range;
4818 Rect r; 4830 Rect r;
4819 4831
4820 get_control_part_bounds (SCROLL_BAR_CONTROL_HANDLE (bar), 4832 get_control_part_bounds (SCROLL_BAR_CONTROL_REF (bar),
4821 kControlIndicatorPart, &r); 4833 kControlIndicatorPart, &r);
4822 4834
4823 if (INTEGERP (bar->dragging) && XINT (bar->dragging) < 0) 4835 if (INTEGERP (bar->dragging) && XINT (bar->dragging) < 0)
@@ -4865,13 +4877,13 @@ x_scroll_bar_handle_drag (win, bar, mouse_pos, bufp)
4865 } 4877 }
4866 4878
4867 if (unhilite_p) 4879 if (unhilite_p)
4868 HiliteControl (SCROLL_BAR_CONTROL_HANDLE (bar), 0); 4880 HiliteControl (SCROLL_BAR_CONTROL_REF (bar), 0);
4869 else if (part != last_scroll_bar_part 4881 else if (part != last_scroll_bar_part
4870 || scroll_bar_timer_event_posted_p) 4882 || scroll_bar_timer_event_posted_p)
4871 { 4883 {
4872 construct_scroll_bar_click (bar, part, bufp); 4884 construct_scroll_bar_click (bar, part, bufp);
4873 last_scroll_bar_part = part; 4885 last_scroll_bar_part = part;
4874 HiliteControl (SCROLL_BAR_CONTROL_HANDLE (bar), part_code); 4886 HiliteControl (SCROLL_BAR_CONTROL_REF (bar), part_code);
4875 set_scroll_bar_timer (SCROLL_BAR_CONTINUOUS_DELAY); 4887 set_scroll_bar_timer (SCROLL_BAR_CONTINUOUS_DELAY);
4876 } 4888 }
4877 } 4889 }
@@ -4885,7 +4897,7 @@ x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
4885 struct scroll_bar *bar; 4897 struct scroll_bar *bar;
4886 int portion, position, whole; 4898 int portion, position, whole;
4887{ 4899{
4888 ControlHandle ch = SCROLL_BAR_CONTROL_HANDLE (bar); 4900 ControlRef ch = SCROLL_BAR_CONTROL_REF (bar);
4889 int value, viewsize, maximum; 4901 int value, viewsize, maximum;
4890 4902
4891 if (XINT (bar->track_height) == 0) 4903 if (XINT (bar->track_height) == 0)
@@ -4944,7 +4956,7 @@ x_scroll_bar_create (w, top, left, width, height, disp_top, disp_height)
4944 struct scroll_bar *bar 4956 struct scroll_bar *bar
4945 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil)); 4957 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
4946 Rect r; 4958 Rect r;
4947 ControlHandle ch; 4959 ControlRef ch;
4948 4960
4949 BLOCK_INPUT; 4961 BLOCK_INPUT;
4950 4962
@@ -4968,7 +4980,7 @@ x_scroll_bar_create (w, top, left, width, height, disp_top, disp_height)
4968 ch = NewControl (FRAME_MAC_WINDOW (f), &r, "\p", width < disp_height, 4980 ch = NewControl (FRAME_MAC_WINDOW (f), &r, "\p", width < disp_height,
4969 0, 0, 0, scrollBarProc, (long) bar); 4981 0, 0, 0, scrollBarProc, (long) bar);
4970#endif 4982#endif
4971 SET_SCROLL_BAR_CONTROL_HANDLE (bar, ch); 4983 SET_SCROLL_BAR_CONTROL_REF (bar, ch);
4972 4984
4973 XSETWINDOW (bar->window, w); 4985 XSETWINDOW (bar->window, w);
4974 XSETINT (bar->top, top); 4986 XSETINT (bar->top, top);
@@ -5018,7 +5030,7 @@ x_scroll_bar_set_handle (bar, start, end, rebuild)
5018 int rebuild; 5030 int rebuild;
5019{ 5031{
5020 int dragging = ! NILP (bar->dragging); 5032 int dragging = ! NILP (bar->dragging);
5021 ControlHandle ch = SCROLL_BAR_CONTROL_HANDLE (bar); 5033 ControlRef ch = SCROLL_BAR_CONTROL_REF (bar);
5022 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window))); 5034 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5023 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height)); 5035 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
5024 int length = end - start; 5036 int length = end - start;
@@ -5087,7 +5099,7 @@ x_scroll_bar_remove (bar)
5087 mac_prepare_for_quickdraw (f); 5099 mac_prepare_for_quickdraw (f);
5088#endif 5100#endif
5089 /* Destroy the Mac scroll bar control */ 5101 /* Destroy the Mac scroll bar control */
5090 DisposeControl (SCROLL_BAR_CONTROL_HANDLE (bar)); 5102 DisposeControl (SCROLL_BAR_CONTROL_REF (bar));
5091 5103
5092 /* Disassociate this scroll bar from its window. */ 5104 /* Disassociate this scroll bar from its window. */
5093 XWINDOW (bar->window)->vertical_scroll_bar = Qnil; 5105 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
@@ -5165,10 +5177,10 @@ XTset_vertical_scroll_bar (w, portion, whole, position)
5165 else 5177 else
5166 { 5178 {
5167 /* It may just need to be moved and resized. */ 5179 /* It may just need to be moved and resized. */
5168 ControlHandle ch; 5180 ControlRef ch;
5169 5181
5170 bar = XSCROLL_BAR (w->vertical_scroll_bar); 5182 bar = XSCROLL_BAR (w->vertical_scroll_bar);
5171 ch = SCROLL_BAR_CONTROL_HANDLE (bar); 5183 ch = SCROLL_BAR_CONTROL_REF (bar);
5172 5184
5173 BLOCK_INPUT; 5185 BLOCK_INPUT;
5174 5186
@@ -5224,7 +5236,7 @@ XTset_vertical_scroll_bar (w, portion, whole, position)
5224 } 5236 }
5225 else 5237 else
5226 { 5238 {
5227 ControlHandle ch = SCROLL_BAR_CONTROL_HANDLE (bar); 5239 ControlRef ch = SCROLL_BAR_CONTROL_REF (bar);
5228 Rect r0, r1; 5240 Rect r0, r1;
5229 5241
5230 BLOCK_INPUT; 5242 BLOCK_INPUT;
@@ -5501,19 +5513,24 @@ x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
5501 unsigned long *time; 5513 unsigned long *time;
5502{ 5514{
5503 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar); 5515 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
5504 ControlHandle ch = SCROLL_BAR_CONTROL_HANDLE (bar); 5516 ControlRef ch = SCROLL_BAR_CONTROL_REF (bar);
5505#if TARGET_API_MAC_CARBON 5517#if TARGET_API_MAC_CARBON
5506 WindowPtr wp = GetControlOwner (ch); 5518 WindowRef wp = GetControlOwner (ch);
5507#else 5519#else
5508 WindowPtr wp = (*ch)->contrlOwner; 5520 WindowRef wp = (*ch)->contrlOwner;
5509#endif 5521#endif
5510 Point mouse_pos; 5522 Point mouse_pos;
5511 struct frame *f = mac_window_to_frame (wp); 5523 struct frame *f = mac_window_to_frame (wp);
5512 int win_y, top_range; 5524 int win_y, top_range;
5513 5525
5526#if TARGET_API_MAC_CARBON
5527 GetGlobalMouse (&mouse_pos);
5528 mouse_pos.h -= f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f);
5529 mouse_pos.v -= f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f);
5530#else
5514 SetPortWindowPort (wp); 5531 SetPortWindowPort (wp);
5515
5516 GetMouse (&mouse_pos); 5532 GetMouse (&mouse_pos);
5533#endif
5517 5534
5518 win_y = mouse_pos.v - XINT (bar->top); 5535 win_y = mouse_pos.v - XINT (bar->top);
5519 top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height)); 5536 top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
@@ -6141,9 +6158,7 @@ x_set_offset (f, xoff, yoff, change_gravity)
6141 ConstrainWindowToScreen (FRAME_MAC_WINDOW (f), kWindowTitleBarRgn, 6158 ConstrainWindowToScreen (FRAME_MAC_WINDOW (f), kWindowTitleBarRgn,
6142 kWindowConstrainMoveRegardlessOfFit 6159 kWindowConstrainMoveRegardlessOfFit
6143 | kWindowConstrainAllowPartial, NULL, NULL); 6160 | kWindowConstrainAllowPartial, NULL, NULL);
6144#if USE_CARBON_EVENTS
6145 if (!NILP (tip_frame) && XFRAME (tip_frame) == f) 6161 if (!NILP (tip_frame) && XFRAME (tip_frame) == f)
6146#endif
6147 mac_handle_origin_change (f); 6162 mac_handle_origin_change (f);
6148#else 6163#else
6149 { 6164 {
@@ -6219,7 +6234,7 @@ x_set_window_size (f, change_gravity, cols, rows)
6219 6234
6220 SizeWindow (FRAME_MAC_WINDOW (f), pixelwidth, pixelheight, 0); 6235 SizeWindow (FRAME_MAC_WINDOW (f), pixelwidth, pixelheight, 0);
6221 6236
6222#if USE_CARBON_EVENTS 6237#if TARGET_API_MAC_CARBON
6223 if (!NILP (tip_frame) && f == XFRAME (tip_frame)) 6238 if (!NILP (tip_frame) && f == XFRAME (tip_frame))
6224#endif 6239#endif
6225 mac_handle_size_change (f, pixelwidth, pixelheight); 6240 mac_handle_size_change (f, pixelwidth, pixelheight);
@@ -6266,17 +6281,11 @@ x_set_mouse_pixel_position (f, pix_x, pix_y)
6266 int pix_x, pix_y; 6281 int pix_x, pix_y;
6267{ 6282{
6268#ifdef MAC_OSX 6283#ifdef MAC_OSX
6269 Point p; 6284 pix_x += f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f);
6270 CGPoint point; 6285 pix_y += f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f);
6271 6286
6272 BLOCK_INPUT; 6287 BLOCK_INPUT;
6273 SetPortWindowPort (FRAME_MAC_WINDOW (f)); 6288 CGWarpMouseCursorPosition (CGPointMake (pix_x, pix_y));
6274 p.h = pix_x;
6275 p.v = pix_y;
6276 LocalToGlobal (&p);
6277 point.x = p.h;
6278 point.y = p.v;
6279 CGWarpMouseCursorPosition (point);
6280 UNBLOCK_INPUT; 6289 UNBLOCK_INPUT;
6281#else 6290#else
6282#if 0 /* MAC_TODO: LMSetMouseLocation and CursorDeviceMoveTo are non-Carbon */ 6291#if 0 /* MAC_TODO: LMSetMouseLocation and CursorDeviceMoveTo are non-Carbon */
@@ -6358,7 +6367,7 @@ static void
6358mac_handle_visibility_change (f) 6367mac_handle_visibility_change (f)
6359 struct frame *f; 6368 struct frame *f;
6360{ 6369{
6361 WindowPtr wp = FRAME_MAC_WINDOW (f); 6370 WindowRef wp = FRAME_MAC_WINDOW (f);
6362 int visible = 0, iconified = 0; 6371 int visible = 0, iconified = 0;
6363 struct input_event buf; 6372 struct input_event buf;
6364 6373
@@ -6426,32 +6435,7 @@ x_make_frame_visible (f)
6426 before the window gets really visible. */ 6435 before the window gets really visible. */
6427 if (! FRAME_ICONIFIED_P (f) 6436 if (! FRAME_ICONIFIED_P (f)
6428 && ! f->output_data.mac->asked_for_visible) 6437 && ! f->output_data.mac->asked_for_visible)
6429 { 6438 x_set_offset (f, f->left_pos, f->top_pos, 0);
6430#if TARGET_API_MAC_CARBON
6431 if (!(FRAME_SIZE_HINTS (f)->flags & (USPosition | PPosition)))
6432 {
6433 struct frame *sf = SELECTED_FRAME ();
6434 if (!FRAME_MAC_P (sf))
6435 RepositionWindow (FRAME_MAC_WINDOW (f), NULL,
6436 kWindowCenterOnMainScreen);
6437 else
6438 RepositionWindow (FRAME_MAC_WINDOW (f),
6439 FRAME_MAC_WINDOW (sf),
6440#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1020
6441 kWindowCascadeStartAtParentWindowScreen
6442#else
6443 kWindowCascadeOnParentWindowScreen
6444#endif
6445 );
6446#if USE_CARBON_EVENTS
6447 if (!NILP (tip_frame) && f == XFRAME (tip_frame))
6448#endif
6449 mac_handle_origin_change (f);
6450 }
6451 else
6452#endif
6453 x_set_offset (f, f->left_pos, f->top_pos, 0);
6454 }
6455 6439
6456 f->output_data.mac->asked_for_visible = 1; 6440 f->output_data.mac->asked_for_visible = 1;
6457 6441
@@ -6525,18 +6509,20 @@ x_make_frame_invisible (f)
6525 6509
6526 BLOCK_INPUT; 6510 BLOCK_INPUT;
6527 6511
6512#if !TARGET_API_MAC_CARBON
6528 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim 6513 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
6529 that the current position of the window is user-specified, rather than 6514 that the current position of the window is user-specified, rather than
6530 program-specified, so that when the window is mapped again, it will be 6515 program-specified, so that when the window is mapped again, it will be
6531 placed at the same location, without forcing the user to position it 6516 placed at the same location, without forcing the user to position it
6532 by hand again (they have already done that once for this window.) */ 6517 by hand again (they have already done that once for this window.) */
6533 x_wm_set_size_hint (f, (long) 0, 1); 6518 x_wm_set_size_hint (f, (long) 0, 1);
6519#endif
6534 6520
6535 HideWindow (FRAME_MAC_WINDOW (f)); 6521 HideWindow (FRAME_MAC_WINDOW (f));
6536 6522
6537 UNBLOCK_INPUT; 6523 UNBLOCK_INPUT;
6538 6524
6539#if !USE_CARBON_EVENTS 6525#if !TARGET_API_MAC_CARBON
6540 mac_handle_visibility_change (f); 6526 mac_handle_visibility_change (f);
6541#endif 6527#endif
6542} 6528}
@@ -6575,7 +6561,7 @@ x_iconify_frame (f)
6575 if (err != noErr) 6561 if (err != noErr)
6576 error ("Can't notify window manager of iconification"); 6562 error ("Can't notify window manager of iconification");
6577 6563
6578#if !USE_CARBON_EVENTS 6564#if !TARGET_API_MAC_CARBON
6579 mac_handle_visibility_change (f); 6565 mac_handle_visibility_change (f);
6580#endif 6566#endif
6581} 6567}
@@ -6588,7 +6574,7 @@ x_free_frame_resources (f)
6588 struct frame *f; 6574 struct frame *f;
6589{ 6575{
6590 struct mac_display_info *dpyinfo = FRAME_MAC_DISPLAY_INFO (f); 6576 struct mac_display_info *dpyinfo = FRAME_MAC_DISPLAY_INFO (f);
6591 WindowPtr wp = FRAME_MAC_WINDOW (f); 6577 WindowRef wp = FRAME_MAC_WINDOW (f);
6592 6578
6593 BLOCK_INPUT; 6579 BLOCK_INPUT;
6594 6580
@@ -8723,16 +8709,93 @@ x_find_ccl_program (fontp)
8723 possible. */ 8709 possible. */
8724static int font_panel_shown_p = 0; 8710static int font_panel_shown_p = 0;
8725 8711
8712extern Lisp_Object Qfont;
8713static Lisp_Object Qpanel_closed, Qselection;
8714
8715static OSStatus mac_store_event_ref_as_apple_event P_ ((AEEventClass, AEEventID,
8716 Lisp_Object,
8717 Lisp_Object,
8718 EventRef, UInt32,
8719 const EventParamName *,
8720 const EventParamType *));
8721
8726int 8722int
8727mac_font_panel_visible_p () 8723mac_font_panel_visible_p ()
8728{ 8724{
8729 return font_panel_shown_p && FPIsFontPanelVisible (); 8725 return font_panel_shown_p && FPIsFontPanelVisible ();
8730} 8726}
8731 8727
8728static pascal OSStatus
8729mac_handle_font_event (next_handler, event, data)
8730 EventHandlerCallRef next_handler;
8731 EventRef event;
8732 void *data;
8733{
8734 OSStatus result, err;
8735 Lisp_Object id_key;
8736 int num_params;
8737 const EventParamName *names;
8738 const EventParamType *types;
8739 static const EventParamName names_sel[] = {kEventParamATSUFontID,
8740 kEventParamATSUFontSize,
8741 kEventParamFMFontFamily,
8742 kEventParamFMFontSize,
8743 kEventParamFontColor};
8744 static const EventParamType types_sel[] = {typeATSUFontID,
8745 typeATSUSize,
8746 typeFMFontFamily,
8747 typeFMFontSize,
8748 typeFontColor};
8749
8750 result = CallNextEventHandler (next_handler, event);
8751 if (result != eventNotHandledErr)
8752 return result;
8753
8754 switch (GetEventKind (event))
8755 {
8756 case kEventFontPanelClosed:
8757 id_key = Qpanel_closed;
8758 num_params = 0;
8759 names = NULL;
8760 types = NULL;
8761 break;
8762
8763 case kEventFontSelection:
8764 id_key = Qselection;
8765 num_params = sizeof (names_sel) / sizeof (names_sel[0]);
8766 names = names_sel;
8767 types = types_sel;
8768 break;
8769 }
8770
8771 err = mac_store_event_ref_as_apple_event (0, 0, Qfont, id_key,
8772 event, num_params,
8773 names, types);
8774 if (err == noErr)
8775 result = noErr;
8776
8777 return result;
8778}
8779
8732OSStatus 8780OSStatus
8733mac_show_hide_font_panel () 8781mac_show_hide_font_panel ()
8734{ 8782{
8735 font_panel_shown_p = 1; 8783 if (!font_panel_shown_p)
8784 {
8785 OSStatus err;
8786
8787 static const EventTypeSpec specs[] =
8788 {{kEventClassFont, kEventFontPanelClosed},
8789 {kEventClassFont, kEventFontSelection}};
8790
8791 err = InstallApplicationEventHandler (mac_handle_font_event,
8792 GetEventTypeCount (specs),
8793 specs, NULL, NULL);
8794 if (err != noErr)
8795 return err;
8796
8797 font_panel_shown_p = 1;
8798 }
8736 8799
8737 return FPShowHideFontPanel (); 8800 return FPShowHideFontPanel ();
8738} 8801}
@@ -8848,7 +8911,7 @@ Lisp_Object Vmac_function_modifier;
8848 a three button mouse */ 8911 a three button mouse */
8849Lisp_Object Vmac_emulate_three_button_mouse; 8912Lisp_Object Vmac_emulate_three_button_mouse;
8850 8913
8851#if USE_CARBON_EVENTS 8914#if TARGET_API_MAC_CARBON
8852/* Non-zero if the mouse wheel button (i.e. button 4) should map to 8915/* Non-zero if the mouse wheel button (i.e. button 4) should map to
8853 mouse-2, instead of mouse-3. */ 8916 mouse-2, instead of mouse-3. */
8854int mac_wheel_button_is_mouse_2; 8917int mac_wheel_button_is_mouse_2;
@@ -8873,16 +8936,12 @@ static int mac_screen_config_changed = 0;
8873Point saved_menu_event_location; 8936Point saved_menu_event_location;
8874 8937
8875/* Apple Events */ 8938/* Apple Events */
8876#if USE_CARBON_EVENTS 8939#if TARGET_API_MAC_CARBON
8877static Lisp_Object Qhi_command; 8940static Lisp_Object Qhi_command;
8878#ifdef MAC_OSX 8941#ifdef MAC_OSX
8879extern Lisp_Object Qwindow; 8942extern Lisp_Object Qwindow;
8880static Lisp_Object Qtoolbar_switch_mode; 8943static Lisp_Object Qtoolbar_switch_mode;
8881#endif 8944#endif
8882#if USE_MAC_FONT_PANEL
8883extern Lisp_Object Qfont;
8884static Lisp_Object Qpanel_closed, Qselection;
8885#endif
8886#if USE_MAC_TSM 8945#if USE_MAC_TSM
8887static TSMDocumentID tsm_document_id; 8946static TSMDocumentID tsm_document_id;
8888static Lisp_Object Qtext_input; 8947static Lisp_Object Qtext_input;
@@ -8894,7 +8953,7 @@ static Lisp_Object saved_ts_script_language_on_focus;
8894static ScriptLanguageRecord saved_ts_language; 8953static ScriptLanguageRecord saved_ts_language;
8895static Component saved_ts_component; 8954static Component saved_ts_component;
8896#endif 8955#endif
8897#endif 8956#endif /* TARGET_API_MAC_CARBON */
8898extern int mac_ready_for_apple_events; 8957extern int mac_ready_for_apple_events;
8899extern Lisp_Object Qundefined; 8958extern Lisp_Object Qundefined;
8900extern void init_apple_event_handler P_ ((void)); 8959extern void init_apple_event_handler P_ ((void));
@@ -8907,20 +8966,15 @@ extern OSErr init_coercion_handler P_ ((void));
8907extern OSErr install_drag_handler P_ ((WindowRef)); 8966extern OSErr install_drag_handler P_ ((WindowRef));
8908extern void remove_drag_handler P_ ((WindowRef)); 8967extern void remove_drag_handler P_ ((WindowRef));
8909 8968
8969#if TARGET_API_MAC_CARBON
8910/* Showing help echo string during menu tracking */ 8970/* Showing help echo string during menu tracking */
8911extern OSStatus install_menu_target_item_handler P_ ((WindowPtr)); 8971extern OSStatus install_menu_target_item_handler P_ ((void));
8912 8972
8913#if USE_CARBON_EVENTS
8914#ifdef MAC_OSX 8973#ifdef MAC_OSX
8915extern void init_service_handler (); 8974extern OSStatus install_service_handler ();
8916static Lisp_Object Qservice, Qpaste, Qperform; 8975static Lisp_Object Qservice, Qpaste, Qperform;
8917#endif 8976#endif
8918
8919/* Window Event Handler */
8920static pascal OSStatus mac_handle_window_event (EventHandlerCallRef,
8921 EventRef, void *);
8922#endif 8977#endif
8923OSStatus install_window_handler (WindowPtr);
8924 8978
8925extern void init_emacs_passwd_dir (); 8979extern void init_emacs_passwd_dir ();
8926extern int emacs_main (int, char **, char **); 8980extern int emacs_main (int, char **, char **);
@@ -9004,7 +9058,7 @@ static const unsigned char fn_keycode_to_keycode_table[] = {
9004#endif /* MAC_OSX */ 9058#endif /* MAC_OSX */
9005 9059
9006static int 9060static int
9007#if USE_CARBON_EVENTS 9061#if TARGET_API_MAC_CARBON
9008mac_to_emacs_modifiers (UInt32 mods) 9062mac_to_emacs_modifiers (UInt32 mods)
9009#else 9063#else
9010mac_to_emacs_modifiers (EventModifiers mods) 9064mac_to_emacs_modifiers (EventModifiers mods)
@@ -9113,17 +9167,19 @@ mac_quit_char_key_p (modifiers, key_code)
9113} 9167}
9114#endif 9168#endif
9115 9169
9116#if USE_CARBON_EVENTS 9170#if TARGET_API_MAC_CARBON
9117/* Obtains the event modifiers from the event ref and then calls 9171/* Obtains the event modifiers from the event ref and then calls
9118 mac_to_emacs_modifiers. */ 9172 mac_to_emacs_modifiers. */
9119static int 9173static int
9120mac_event_to_emacs_modifiers (EventRef eventRef) 9174mac_event_to_emacs_modifiers (EventRef eventRef)
9121{ 9175{
9122 UInt32 mods = 0; 9176 UInt32 mods = 0, class;
9177
9123 GetEventParameter (eventRef, kEventParamKeyModifiers, typeUInt32, NULL, 9178 GetEventParameter (eventRef, kEventParamKeyModifiers, typeUInt32, NULL,
9124 sizeof (UInt32), NULL, &mods); 9179 sizeof (UInt32), NULL, &mods);
9180 class = GetEventClass (eventRef);
9125 if (!NILP (Vmac_emulate_three_button_mouse) && 9181 if (!NILP (Vmac_emulate_three_button_mouse) &&
9126 GetEventClass(eventRef) == kEventClassMouse) 9182 (class == kEventClassMouse || class == kEventClassCommand))
9127 { 9183 {
9128 mods &= ~(optionKey | cmdKey); 9184 mods &= ~(optionKey | cmdKey);
9129 } 9185 }
@@ -9262,7 +9318,7 @@ static void
9262do_get_menus (void) 9318do_get_menus (void)
9263{ 9319{
9264 Handle menubar_handle; 9320 Handle menubar_handle;
9265 MenuHandle menu_handle; 9321 MenuRef menu;
9266 9322
9267 menubar_handle = GetNewMBar (128); 9323 menubar_handle = GetNewMBar (128);
9268 if(menubar_handle == NULL) 9324 if(menubar_handle == NULL)
@@ -9271,9 +9327,9 @@ do_get_menus (void)
9271 DrawMenuBar (); 9327 DrawMenuBar ();
9272 9328
9273#if !TARGET_API_MAC_CARBON 9329#if !TARGET_API_MAC_CARBON
9274 menu_handle = GetMenuHandle (M_APPLE); 9330 menu = GetMenuRef (M_APPLE);
9275 if(menu_handle != NULL) 9331 if (menu != NULL)
9276 AppendResMenu (menu_handle,'DRVR'); 9332 AppendResMenu (menu, 'DRVR');
9277 else 9333 else
9278 abort (); 9334 abort ();
9279#endif 9335#endif
@@ -9322,7 +9378,7 @@ do_check_ram_size (void)
9322#endif /* MAC_OS8 */ 9378#endif /* MAC_OS8 */
9323 9379
9324static void 9380static void
9325do_window_update (WindowPtr win) 9381do_window_update (WindowRef win)
9326{ 9382{
9327 struct frame *f = mac_window_to_frame (win); 9383 struct frame *f = mac_window_to_frame (win);
9328 9384
@@ -9367,7 +9423,7 @@ do_window_update (WindowPtr win)
9367} 9423}
9368 9424
9369static int 9425static int
9370is_emacs_window (WindowPtr win) 9426is_emacs_window (WindowRef win)
9371{ 9427{
9372 Lisp_Object tail, frame; 9428 Lisp_Object tail, frame;
9373 9429
@@ -9480,7 +9536,7 @@ do_apple_menu (SInt16 menu_item)
9480 NoteAlert (ABOUT_ALERT_ID, NULL); 9536 NoteAlert (ABOUT_ALERT_ID, NULL);
9481 else 9537 else
9482 { 9538 {
9483 GetMenuItemText (GetMenuHandle (M_APPLE), menu_item, item_name); 9539 GetMenuItemText (GetMenuRef (M_APPLE), menu_item, item_name);
9484 da_driver_refnum = OpenDeskAcc (item_name); 9540 da_driver_refnum = OpenDeskAcc (item_name);
9485 } 9541 }
9486} 9542}
@@ -9491,7 +9547,7 @@ do_apple_menu (SInt16 menu_item)
9491 9547
9492static void 9548static void
9493do_grow_window (w, e) 9549do_grow_window (w, e)
9494 WindowPtr w; 9550 WindowRef w;
9495 const EventRecord *e; 9551 const EventRecord *e;
9496{ 9552{
9497 Rect limit_rect; 9553 Rect limit_rect;
@@ -9543,7 +9599,7 @@ mac_get_ideal_size (f)
9543 struct frame *f; 9599 struct frame *f;
9544{ 9600{
9545 struct mac_display_info *dpyinfo = FRAME_MAC_DISPLAY_INFO (f); 9601 struct mac_display_info *dpyinfo = FRAME_MAC_DISPLAY_INFO (f);
9546 WindowPtr w = FRAME_MAC_WINDOW (f); 9602 WindowRef w = FRAME_MAC_WINDOW (f);
9547 Point ideal_size; 9603 Point ideal_size;
9548 Rect standard_rect; 9604 Rect standard_rect;
9549 int height, width, columns, rows; 9605 int height, width, columns, rows;
@@ -9569,7 +9625,7 @@ mac_get_ideal_size (f)
9569 wide (DEFAULT_NUM_COLS) and as tall as will fit on the screen. */ 9625 wide (DEFAULT_NUM_COLS) and as tall as will fit on the screen. */
9570 9626
9571static void 9627static void
9572do_zoom_window (WindowPtr w, int zoom_in_or_out) 9628do_zoom_window (WindowRef w, int zoom_in_or_out)
9573{ 9629{
9574 Rect zoom_rect, port_rect; 9630 Rect zoom_rect, port_rect;
9575 int width, height; 9631 int width, height;
@@ -9632,13 +9688,9 @@ do_zoom_window (WindowPtr w, int zoom_in_or_out)
9632 SetPort (save_port); 9688 SetPort (save_port);
9633#endif /* not TARGET_API_MAC_CARBON */ 9689#endif /* not TARGET_API_MAC_CARBON */
9634 9690
9635#if !USE_CARBON_EVENTS 9691#if !TARGET_API_MAC_CARBON
9636 /* retrieve window size and update application values */ 9692 /* retrieve window size and update application values */
9637#if TARGET_API_MAC_CARBON
9638 GetWindowPortBounds (w, &port_rect);
9639#else
9640 port_rect = w->portRect; 9693 port_rect = w->portRect;
9641#endif
9642 height = port_rect.bottom - port_rect.top; 9694 height = port_rect.bottom - port_rect.top;
9643 width = port_rect.right - port_rect.left; 9695 width = port_rect.right - port_rect.left;
9644 9696
@@ -9647,6 +9699,210 @@ do_zoom_window (WindowPtr w, int zoom_in_or_out)
9647#endif 9699#endif
9648} 9700}
9649 9701
9702static void
9703mac_set_unicode_keystroke_event (code, buf)
9704 UniChar code;
9705 struct input_event *buf;
9706{
9707 int charset_id, c1, c2;
9708
9709 if (code < 0x80)
9710 {
9711 buf->kind = ASCII_KEYSTROKE_EVENT;
9712 buf->code = code;
9713 }
9714 else if (code < 0x100)
9715 {
9716 if (code < 0xA0)
9717 charset_id = CHARSET_8_BIT_CONTROL;
9718 else
9719 charset_id = charset_latin_iso8859_1;
9720 buf->kind = MULTIBYTE_CHAR_KEYSTROKE_EVENT;
9721 buf->code = MAKE_CHAR (charset_id, code, 0);
9722 }
9723 else
9724 {
9725 if (code < 0x2500)
9726 charset_id = charset_mule_unicode_0100_24ff,
9727 code -= 0x100;
9728 else if (code < 0x33FF)
9729 charset_id = charset_mule_unicode_2500_33ff,
9730 code -= 0x2500;
9731 else if (code >= 0xE000)
9732 charset_id = charset_mule_unicode_e000_ffff,
9733 code -= 0xE000;
9734 c1 = (code / 96) + 32, c2 = (code % 96) + 32;
9735 buf->kind = MULTIBYTE_CHAR_KEYSTROKE_EVENT;
9736 buf->code = MAKE_CHAR (charset_id, c1, c2);
9737 }
9738}
9739
9740static void
9741do_keystroke (action, char_code, key_code, modifiers, timestamp, buf)
9742 EventKind action;
9743 unsigned char char_code;
9744 UInt32 key_code, modifiers;
9745 unsigned long timestamp;
9746 struct input_event *buf;
9747{
9748 static SInt16 last_key_script = -1;
9749 SInt16 current_key_script = GetScriptManagerVariable (smKeyScript);
9750 UInt32 mapped_modifiers = mac_mapped_modifiers (modifiers);
9751
9752#ifdef MAC_OSX
9753 if (mapped_modifiers & kEventKeyModifierFnMask
9754 && key_code <= 0x7f
9755 && fn_keycode_to_keycode_table[key_code])
9756 key_code = fn_keycode_to_keycode_table[key_code];
9757#endif
9758
9759 if (key_code <= 0x7f && keycode_to_xkeysym_table[key_code])
9760 {
9761 buf->kind = NON_ASCII_KEYSTROKE_EVENT;
9762 buf->code = 0xff00 | keycode_to_xkeysym_table[key_code];
9763#ifdef MAC_OSX
9764 if (modifiers & kEventKeyModifierFnMask
9765 && key_code <= 0x7f
9766 && fn_keycode_to_keycode_table[key_code] == key_code)
9767 modifiers &= ~kEventKeyModifierFnMask;
9768#endif
9769 }
9770 else if (mapped_modifiers)
9771 {
9772 /* translate the keycode back to determine the original key */
9773#ifdef MAC_OSX
9774 UCKeyboardLayout *uchr_ptr = NULL;
9775#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1020
9776 OSStatus err;
9777 KeyboardLayoutRef layout;
9778
9779 err = KLGetCurrentKeyboardLayout (&layout);
9780 if (err == noErr)
9781 err = KLGetKeyboardLayoutProperty (layout, kKLuchrData,
9782 (const void **) &uchr_ptr);
9783#else
9784 static SInt16 last_key_layout_id = 0;
9785 static Handle uchr_handle = (Handle)-1;
9786 SInt16 current_key_layout_id =
9787 GetScriptVariable (current_key_script, smScriptKeys);
9788
9789 if (uchr_handle == (Handle)-1
9790 || last_key_layout_id != current_key_layout_id)
9791 {
9792 uchr_handle = GetResource ('uchr', current_key_layout_id);
9793 last_key_layout_id = current_key_layout_id;
9794 }
9795 if (uchr_handle)
9796 uchr_ptr = (UCKeyboardLayout *)*uchr_handle;
9797#endif
9798
9799 if (uchr_ptr)
9800 {
9801 OSStatus status;
9802 UInt16 key_action = action - keyDown;
9803 UInt32 modifier_key_state = (modifiers & ~mapped_modifiers) >> 8;
9804 UInt32 keyboard_type = LMGetKbdType ();
9805 SInt32 dead_key_state = 0;
9806 UniChar code;
9807 UniCharCount actual_length;
9808
9809 status = UCKeyTranslate (uchr_ptr, key_code, key_action,
9810 modifier_key_state, keyboard_type,
9811 kUCKeyTranslateNoDeadKeysMask,
9812 &dead_key_state,
9813 1, &actual_length, &code);
9814 if (status == noErr && actual_length == 1)
9815 mac_set_unicode_keystroke_event (code, buf);
9816 }
9817#endif /* MAC_OSX */
9818
9819 if (buf->kind == NO_EVENT)
9820 {
9821 /* This code comes from Keyboard Resource, Appendix C of IM
9822 - Text. This is necessary since shift is ignored in KCHR
9823 table translation when option or command is pressed. It
9824 also does not translate correctly control-shift chars
9825 like C-% so mask off shift here also. */
9826 /* Mask off modifier keys that are mapped to some Emacs
9827 modifiers. */
9828 int new_modifiers = modifiers & ~mapped_modifiers;
9829 /* set high byte of keycode to modifier high byte*/
9830 int new_key_code = key_code | new_modifiers;
9831 Ptr kchr_ptr = (Ptr) GetScriptManagerVariable (smKCHRCache);
9832 unsigned long some_state = 0;
9833 UInt32 new_char_code;
9834
9835 new_char_code = KeyTranslate (kchr_ptr, new_key_code, &some_state);
9836 if (new_char_code == 0)
9837 /* Seems like a dead key. Append up-stroke. */
9838 new_char_code = KeyTranslate (kchr_ptr, new_key_code | 0x80,
9839 &some_state);
9840 if (new_char_code)
9841 {
9842 buf->kind = ASCII_KEYSTROKE_EVENT;
9843 buf->code = new_char_code & 0xff;
9844 }
9845 }
9846 }
9847
9848 if (buf->kind == NO_EVENT)
9849 {
9850 buf->kind = ASCII_KEYSTROKE_EVENT;
9851 buf->code = char_code;
9852 }
9853
9854 buf->modifiers = mac_to_emacs_modifiers (modifiers);
9855 buf->modifiers |= (extra_keyboard_modifiers
9856 & (meta_modifier | alt_modifier
9857 | hyper_modifier | super_modifier));
9858
9859#if TARGET_API_MAC_CARBON
9860 if (buf->kind == ASCII_KEYSTROKE_EVENT
9861 && buf->code >= 0x80 && buf->modifiers)
9862 {
9863 OSStatus err;
9864 TextEncoding encoding = kTextEncodingMacRoman;
9865 TextToUnicodeInfo ttu_info;
9866
9867 UpgradeScriptInfoToTextEncoding (current_key_script,
9868 kTextLanguageDontCare,
9869 kTextRegionDontCare,
9870 NULL, &encoding);
9871 err = CreateTextToUnicodeInfoByEncoding (encoding, &ttu_info);
9872 if (err == noErr)
9873 {
9874 UniChar code;
9875 Str255 pstr;
9876 ByteCount unicode_len;
9877
9878 pstr[0] = 1;
9879 pstr[1] = buf->code;
9880 err = ConvertFromPStringToUnicode (ttu_info, pstr,
9881 sizeof (UniChar),
9882 &unicode_len, &code);
9883 if (err == noErr && unicode_len == sizeof (UniChar))
9884 mac_set_unicode_keystroke_event (code, buf);
9885 DisposeTextToUnicodeInfo (&ttu_info);
9886 }
9887 }
9888#endif
9889
9890 if (buf->kind == ASCII_KEYSTROKE_EVENT
9891 && buf->code >= 0x80
9892 && last_key_script != current_key_script)
9893 {
9894 struct input_event event;
9895
9896 EVENT_INIT (event);
9897 event.kind = LANGUAGE_CHANGE_EVENT;
9898 event.arg = Qnil;
9899 event.code = current_key_script;
9900 event.timestamp = timestamp;
9901 kbd_buffer_store_event (&event);
9902 last_key_script = current_key_script;
9903 }
9904}
9905
9650void 9906void
9651mac_store_apple_event (class, id, desc) 9907mac_store_apple_event (class, id, desc)
9652 Lisp_Object class, id; 9908 Lisp_Object class, id;
@@ -9725,54 +9981,49 @@ mac_store_drag_event (window, mouse_pos, modifiers, desc)
9725 buf.arg = mac_aedesc_to_lisp (desc); 9981 buf.arg = mac_aedesc_to_lisp (desc);
9726 kbd_buffer_store_event (&buf); 9982 kbd_buffer_store_event (&buf);
9727} 9983}
9728#endif
9729 9984
9730#if USE_CARBON_EVENTS 9985#ifdef MAC_OSX
9731static pascal OSStatus 9986OSStatus
9732mac_handle_command_event (next_handler, event, data) 9987mac_store_service_event (event)
9733 EventHandlerCallRef next_handler;
9734 EventRef event; 9988 EventRef event;
9735 void *data;
9736{ 9989{
9737 OSStatus result, err; 9990 OSStatus err;
9738 HICommand command; 9991 Lisp_Object id_key;
9739 static const EventParamName names[] = 9992 int num_params;
9740 {kEventParamDirectObject, kEventParamKeyModifiers}; 9993 const EventParamName *names;
9741 static const EventParamType types[] = 9994 const EventParamType *types;
9742 {typeHICommand, typeUInt32}; 9995 static const EventParamName names_pfm[] =
9743 int num_params = sizeof (names) / sizeof (names[0]); 9996 {kEventParamServiceMessageName, kEventParamServiceUserData};
9744 9997 static const EventParamType types_pfm[] =
9745 result = CallNextEventHandler (next_handler, event); 9998 {typeCFStringRef, typeCFStringRef};
9746 if (result != eventNotHandledErr)
9747 return result;
9748 9999
9749 err = GetEventParameter (event, kEventParamDirectObject, typeHICommand, 10000 switch (GetEventKind (event))
9750 NULL, sizeof (HICommand), NULL, &command); 10001 {
10002 case kEventServicePaste:
10003 id_key = Qpaste;
10004 num_params = 0;
10005 names = NULL;
10006 types = NULL;
10007 break;
9751 10008
9752 if (err != noErr || command.commandID == 0) 10009 case kEventServicePerform:
9753 return eventNotHandledErr; 10010 id_key = Qperform;
10011 num_params = sizeof (names_pfm) / sizeof (names_pfm[0]);
10012 names = names_pfm;
10013 types = types_pfm;
10014 break;
9754 10015
9755 /* A HI command event is mapped to an Apple event whose event class 10016 default:
9756 symbol is `hi-command' and event ID is its command ID. */ 10017 abort ();
9757 err = mac_store_event_ref_as_apple_event (0, command.commandID, 10018 }
9758 Qhi_command, Qnil,
9759 event, num_params, names, types);
9760 return err == noErr ? noErr : eventNotHandledErr;
9761}
9762 10019
9763static OSStatus 10020 err = mac_store_event_ref_as_apple_event (0, 0, Qservice, id_key,
9764init_command_handler () 10021 event, num_params,
9765{ 10022 names, types);
9766 static const EventTypeSpec specs[] =
9767 {{kEventClassCommand, kEventCommandProcess}};
9768 static EventHandlerUPP handle_command_eventUPP = NULL;
9769 10023
9770 if (handle_command_eventUPP == NULL) 10024 return err;
9771 handle_command_eventUPP = NewEventHandlerUPP (mac_handle_command_event);
9772 return InstallApplicationEventHandler (handle_command_eventUPP,
9773 GetEventTypeCount (specs), specs,
9774 NULL, NULL);
9775} 10025}
10026#endif /* MAC_OSX */
9776 10027
9777static pascal OSStatus 10028static pascal OSStatus
9778mac_handle_window_event (next_handler, event, data) 10029mac_handle_window_event (next_handler, event, data)
@@ -9780,47 +10031,78 @@ mac_handle_window_event (next_handler, event, data)
9780 EventRef event; 10031 EventRef event;
9781 void *data; 10032 void *data;
9782{ 10033{
9783 WindowPtr wp; 10034 WindowRef wp;
9784 OSStatus result, err; 10035 OSStatus err, result = eventNotHandledErr;
9785 struct frame *f; 10036 struct frame *f;
9786 UInt32 attributes; 10037 UInt32 attributes;
9787 XSizeHints *size_hints; 10038 XSizeHints *size_hints;
9788 10039
9789 err = GetEventParameter (event, kEventParamDirectObject, typeWindowRef, 10040 err = GetEventParameter (event, kEventParamDirectObject, typeWindowRef,
9790 NULL, sizeof (WindowPtr), NULL, &wp); 10041 NULL, sizeof (WindowRef), NULL, &wp);
9791 if (err != noErr) 10042 if (err != noErr)
9792 return eventNotHandledErr; 10043 return eventNotHandledErr;
9793 10044
9794 f = mac_window_to_frame (wp); 10045 f = mac_window_to_frame (wp);
9795 switch (GetEventKind (event)) 10046 switch (GetEventKind (event))
9796 { 10047 {
10048 /* -- window refresh events -- */
10049
9797 case kEventWindowUpdate: 10050 case kEventWindowUpdate:
9798 result = CallNextEventHandler (next_handler, event); 10051 result = CallNextEventHandler (next_handler, event);
9799 if (result != eventNotHandledErr) 10052 if (result != eventNotHandledErr)
9800 return result; 10053 break;
9801 10054
9802 do_window_update (wp); 10055 do_window_update (wp);
9803 return noErr; 10056 result = noErr;
10057 break;
9804 10058
9805 case kEventWindowGetIdealSize: 10059 /* -- window state change events -- */
9806 result = CallNextEventHandler (next_handler, event);
9807 if (result != eventNotHandledErr)
9808 return result;
9809 10060
9810 { 10061 case kEventWindowShowing:
9811 Point ideal_size = mac_get_ideal_size (f); 10062 size_hints = FRAME_SIZE_HINTS (f);
10063 if (!(size_hints->flags & (USPosition | PPosition)))
10064 {
10065 struct frame *sf = SELECTED_FRAME ();
9812 10066
9813 err = SetEventParameter (event, kEventParamDimensions, 10067 if (!(FRAME_MAC_P (sf)))
9814 typeQDPoint, sizeof (Point), &ideal_size); 10068 RepositionWindow (wp, NULL, kWindowCenterOnMainScreen);
9815 if (err == noErr) 10069 else
9816 return noErr; 10070 {
9817 } 10071 RepositionWindow (wp, FRAME_MAC_WINDOW (sf),
10072#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1020
10073 kWindowCascadeStartAtParentWindowScreen
10074#else
10075 kWindowCascadeOnParentWindowScreen
10076#endif
10077 );
10078 }
10079 result = noErr;
10080 }
10081 break;
10082
10083 case kEventWindowHiding:
10084 /* Before unmapping the window, update the WM_SIZE_HINTS
10085 property to claim that the current position of the window is
10086 user-specified, rather than program-specified, so that when
10087 the window is mapped again, it will be placed at the same
10088 location, without forcing the user to position it by hand
10089 again (they have already done that once for this window.) */
10090 x_wm_set_size_hint (f, (long) 0, 1);
10091 result = noErr;
10092 break;
10093
10094 case kEventWindowShown:
10095 case kEventWindowHidden:
10096 case kEventWindowCollapsed:
10097 case kEventWindowExpanded:
10098 mac_handle_visibility_change (f);
10099 result = noErr;
9818 break; 10100 break;
9819 10101
9820 case kEventWindowBoundsChanging: 10102 case kEventWindowBoundsChanging:
9821 result = CallNextEventHandler (next_handler, event); 10103 result = CallNextEventHandler (next_handler, event);
9822 if (result != eventNotHandledErr) 10104 if (result != eventNotHandledErr)
9823 return result; 10105 break;
9824 10106
9825 err = GetEventParameter (event, kEventParamAttributes, typeUInt32, 10107 err = GetEventParameter (event, kEventParamAttributes, typeUInt32,
9826 NULL, sizeof (UInt32), NULL, &attributes); 10108 NULL, sizeof (UInt32), NULL, &attributes);
@@ -9864,7 +10146,7 @@ mac_handle_window_event (next_handler, event, data)
9864 bounds.bottom = bounds.top + height; 10146 bounds.bottom = bounds.top + height;
9865 SetEventParameter (event, kEventParamCurrentBounds, 10147 SetEventParameter (event, kEventParamCurrentBounds,
9866 typeQDRectangle, sizeof (Rect), &bounds); 10148 typeQDRectangle, sizeof (Rect), &bounds);
9867 return noErr; 10149 result = noErr;
9868 } 10150 }
9869 break; 10151 break;
9870 10152
@@ -9895,21 +10177,12 @@ mac_handle_window_event (next_handler, event, data)
9895 if (attributes & kWindowBoundsChangeOriginChanged) 10177 if (attributes & kWindowBoundsChangeOriginChanged)
9896 mac_handle_origin_change (f); 10178 mac_handle_origin_change (f);
9897 10179
9898 return noErr; 10180 result = noErr;
9899
9900 case kEventWindowShown:
9901 case kEventWindowHidden:
9902 case kEventWindowExpanded:
9903 case kEventWindowCollapsed:
9904 result = CallNextEventHandler (next_handler, event);
9905
9906 mac_handle_visibility_change (f);
9907 return noErr;
9908
9909 break; 10181 break;
9910 10182
10183 /* -- window action events -- */
10184
9911 case kEventWindowClose: 10185 case kEventWindowClose:
9912 result = CallNextEventHandler (next_handler, event);
9913 { 10186 {
9914 struct input_event buf; 10187 struct input_event buf;
9915 10188
@@ -9919,11 +10192,26 @@ mac_handle_window_event (next_handler, event, data)
9919 buf.arg = Qnil; 10192 buf.arg = Qnil;
9920 kbd_buffer_store_event (&buf); 10193 kbd_buffer_store_event (&buf);
9921 } 10194 }
9922 return noErr; 10195 result = noErr;
10196 break;
10197
10198 case kEventWindowGetIdealSize:
10199 result = CallNextEventHandler (next_handler, event);
10200 if (result != eventNotHandledErr)
10201 break;
10202
10203 {
10204 Point ideal_size = mac_get_ideal_size (f);
10205
10206 err = SetEventParameter (event, kEventParamDimensions,
10207 typeQDPoint, sizeof (Point), &ideal_size);
10208 if (err == noErr)
10209 result = noErr;
10210 }
10211 break;
9923 10212
9924#ifdef MAC_OSX 10213#ifdef MAC_OSX
9925 case kEventWindowToolbarSwitchMode: 10214 case kEventWindowToolbarSwitchMode:
9926 result = CallNextEventHandler (next_handler, event);
9927 { 10215 {
9928 static const EventParamName names[] = {kEventParamDirectObject, 10216 static const EventParamName names[] = {kEventParamDirectObject,
9929 kEventParamWindowMouseLocation, 10217 kEventParamWindowMouseLocation,
@@ -9945,23 +10233,195 @@ mac_handle_window_event (next_handler, event, data)
9945 event, num_params, 10233 event, num_params,
9946 names, types); 10234 names, types);
9947 } 10235 }
9948 return err == noErr ? noErr : result; 10236 if (err == noErr)
10237 result = noErr;
10238 break;
9949#endif 10239#endif
9950 10240
9951#if USE_MAC_TSM 10241#if USE_MAC_TSM
10242 /* -- window focus events -- */
10243
9952 case kEventWindowFocusAcquired: 10244 case kEventWindowFocusAcquired:
9953 result = CallNextEventHandler (next_handler, event);
9954 err = mac_tsm_resume (); 10245 err = mac_tsm_resume ();
9955 return err == noErr ? noErr : result; 10246 if (err == noErr)
10247 result = noErr;
10248 break;
9956 10249
9957 case kEventWindowFocusRelinquish: 10250 case kEventWindowFocusRelinquish:
9958 result = CallNextEventHandler (next_handler, event);
9959 err = mac_tsm_suspend (); 10251 err = mac_tsm_suspend ();
9960 return err == noErr ? noErr : result; 10252 if (err == noErr)
10253 result = noErr;
10254 break;
10255#endif
10256
10257 default:
10258 abort ();
10259 }
10260
10261 return result;
10262}
10263
10264static pascal OSStatus
10265mac_handle_application_event (next_handler, event, data)
10266 EventHandlerCallRef next_handler;
10267 EventRef event;
10268 void *data;
10269{
10270 OSStatus err, result = eventNotHandledErr;
10271
10272 switch (GetEventKind (event))
10273 {
10274#if USE_MAC_TSM
10275 case kEventAppActivated:
10276 err = mac_tsm_resume ();
10277 break;
10278
10279 case kEventAppDeactivated:
10280 err = mac_tsm_suspend ();
10281 break;
9961#endif 10282#endif
10283
10284 default:
10285 abort ();
9962 } 10286 }
9963 10287
9964 return eventNotHandledErr; 10288 if (err == noErr)
10289 result = noErr;
10290
10291 return result;
10292}
10293
10294static pascal OSStatus
10295mac_handle_keyboard_event (next_handler, event, data)
10296 EventHandlerCallRef next_handler;
10297 EventRef event;
10298 void *data;
10299{
10300 OSStatus err, result = eventNotHandledErr;
10301 UInt32 event_kind, key_code, modifiers, mapped_modifiers;
10302 unsigned char char_code;
10303
10304 event_kind = GetEventKind (event);
10305 switch (event_kind)
10306 {
10307 case kEventRawKeyDown:
10308 case kEventRawKeyRepeat:
10309 case kEventRawKeyUp:
10310 if (read_socket_inev == NULL)
10311 {
10312 result = CallNextEventHandler (next_handler, event);
10313 break;
10314 }
10315
10316 err = GetEventParameter (event, kEventParamKeyModifiers,
10317 typeUInt32, NULL,
10318 sizeof (UInt32), NULL, &modifiers);
10319 if (err != noErr)
10320 break;
10321
10322 mapped_modifiers = mac_mapped_modifiers (modifiers);
10323
10324 /* When using Carbon Events, we need to pass raw keyboard events
10325 to the TSM ourselves. If TSM handles it, it will pass back
10326 noErr, otherwise it will pass back "eventNotHandledErr" and
10327 we can process it normally. */
10328 if (!(mapped_modifiers
10329 & ~(mac_pass_command_to_system ? cmdKey : 0)
10330 & ~(mac_pass_control_to_system ? controlKey : 0)))
10331 {
10332 result = CallNextEventHandler (next_handler, event);
10333 if (result != eventNotHandledErr)
10334 break;
10335 }
10336
10337#if USE_MAC_TSM
10338 if (read_socket_inev->kind != NO_EVENT)
10339 {
10340 result = noErr;
10341 break;
10342 }
10343#endif
10344
10345 if (event_kind == kEventRawKeyUp)
10346 break;
10347
10348 err = GetEventParameter (event, kEventParamKeyMacCharCodes,
10349 typeChar, NULL,
10350 sizeof (char), NULL, &char_code);
10351 if (err != noErr)
10352 break;
10353
10354 err = GetEventParameter (event, kEventParamKeyCode,
10355 typeUInt32, NULL,
10356 sizeof (UInt32), NULL, &key_code);
10357 if (err != noErr)
10358 break;
10359
10360 do_keystroke ((GetEventKind (event) == kEventRawKeyDown
10361 ? keyDown : autoKey),
10362 char_code, key_code, modifiers,
10363 ((unsigned long)
10364 (GetEventTime (event) / kEventDurationMillisecond)),
10365 read_socket_inev);
10366 result = noErr;
10367 break;
10368
10369 default:
10370 abort ();
10371 }
10372
10373 return result;
10374}
10375
10376static pascal OSStatus
10377mac_handle_command_event (next_handler, event, data)
10378 EventHandlerCallRef next_handler;
10379 EventRef event;
10380 void *data;
10381{
10382 OSStatus err, result = eventNotHandledErr;
10383 HICommand command;
10384 static const EventParamName names[] =
10385 {kEventParamDirectObject, kEventParamKeyModifiers};
10386 static const EventParamType types[] =
10387 {typeHICommand, typeUInt32};
10388 int num_params = sizeof (names) / sizeof (names[0]);
10389
10390 err = GetEventParameter (event, kEventParamDirectObject, typeHICommand,
10391 NULL, sizeof (HICommand), NULL, &command);
10392 if (err != noErr)
10393 return eventNotHandledErr;
10394
10395 switch (GetEventKind (event))
10396 {
10397 case kEventCommandProcess:
10398 result = CallNextEventHandler (next_handler, event);
10399 if (result != eventNotHandledErr)
10400 break;
10401
10402 err = GetEventParameter (event, kEventParamDirectObject,
10403 typeHICommand, NULL,
10404 sizeof (HICommand), NULL, &command);
10405
10406 if (err != noErr || command.commandID == 0)
10407 break;
10408
10409 /* A HI command event is mapped to an Apple event whose event
10410 class symbol is `hi-command' and event ID is its command
10411 ID. */
10412 err = mac_store_event_ref_as_apple_event (0, command.commandID,
10413 Qhi_command, Qnil,
10414 event, num_params,
10415 names, types);
10416 if (err == noErr)
10417 result = noErr;
10418 break;
10419
10420 default:
10421 abort ();
10422 }
10423
10424 return result;
9965} 10425}
9966 10426
9967static pascal OSStatus 10427static pascal OSStatus
@@ -9970,13 +10430,13 @@ mac_handle_mouse_event (next_handler, event, data)
9970 EventRef event; 10430 EventRef event;
9971 void *data; 10431 void *data;
9972{ 10432{
9973 OSStatus result, err; 10433 OSStatus err, result = eventNotHandledErr;
9974 10434
9975 switch (GetEventKind (event)) 10435 switch (GetEventKind (event))
9976 { 10436 {
9977 case kEventMouseWheelMoved: 10437 case kEventMouseWheelMoved:
9978 { 10438 {
9979 WindowPtr wp; 10439 WindowRef wp;
9980 struct frame *f; 10440 struct frame *f;
9981 EventMouseWheelAxis axis; 10441 EventMouseWheelAxis axis;
9982 SInt32 delta; 10442 SInt32 delta;
@@ -9984,15 +10444,14 @@ mac_handle_mouse_event (next_handler, event, data)
9984 10444
9985 result = CallNextEventHandler (next_handler, event); 10445 result = CallNextEventHandler (next_handler, event);
9986 if (result != eventNotHandledErr || read_socket_inev == NULL) 10446 if (result != eventNotHandledErr || read_socket_inev == NULL)
9987 return result; 10447 break;
10448
10449 f = mac_focus_frame (&one_mac_display_info);
9988 10450
9989 err = GetEventParameter (event, kEventParamWindowRef, typeWindowRef, 10451 err = GetEventParameter (event, kEventParamWindowRef, typeWindowRef,
9990 NULL, sizeof (WindowRef), NULL, &wp); 10452 NULL, sizeof (WindowRef), NULL, &wp);
9991 if (err != noErr) 10453 if (err != noErr
9992 break; 10454 || wp != FRAME_MAC_WINDOW (f))
9993
9994 f = mac_window_to_frame (wp);
9995 if (f != mac_focus_frame (&one_mac_display_info))
9996 break; 10455 break;
9997 10456
9998 err = GetEventParameter (event, kEventParamMouseWheelAxis, 10457 err = GetEventParameter (event, kEventParamMouseWheelAxis,
@@ -10007,8 +10466,8 @@ mac_handle_mouse_event (next_handler, event, data)
10007 if (err != noErr) 10466 if (err != noErr)
10008 break; 10467 break;
10009 10468
10010 SetPortWindowPort (wp); 10469 point.h -= f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f);
10011 GlobalToLocal (&point); 10470 point.v -= f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f);
10012 if (point.h < 0 || point.v < 0 10471 if (point.h < 0 || point.v < 0
10013 || EQ (window_from_coordinates (f, point.h, point.v, 0, 0, 0, 1), 10472 || EQ (window_from_coordinates (f, point.h, point.v, 0, 0, 0, 1),
10014 f->tool_bar_window)) 10473 f->tool_bar_window))
@@ -10029,68 +10488,16 @@ mac_handle_mouse_event (next_handler, event, data)
10029 XSETINT (read_socket_inev->y, point.v); 10488 XSETINT (read_socket_inev->y, point.v);
10030 XSETFRAME (read_socket_inev->frame_or_window, f); 10489 XSETFRAME (read_socket_inev->frame_or_window, f);
10031 10490
10032 return noErr; 10491 result = noErr;
10033 } 10492 }
10034 break; 10493 break;
10035 10494
10036 default: 10495 default:
10037 break; 10496 abort ();
10038 }
10039
10040 return eventNotHandledErr;
10041}
10042
10043#if USE_MAC_FONT_PANEL
10044static pascal OSStatus
10045mac_handle_font_event (next_handler, event, data)
10046 EventHandlerCallRef next_handler;
10047 EventRef event;
10048 void *data;
10049{
10050 OSStatus result, err;
10051 Lisp_Object id_key;
10052 int num_params;
10053 const EventParamName *names;
10054 const EventParamType *types;
10055 static const EventParamName names_sel[] = {kEventParamATSUFontID,
10056 kEventParamATSUFontSize,
10057 kEventParamFMFontFamily,
10058 kEventParamFMFontSize,
10059 kEventParamFontColor};
10060 static const EventParamType types_sel[] = {typeATSUFontID,
10061 typeATSUSize,
10062 typeFMFontFamily,
10063 typeFMFontSize,
10064 typeFontColor};
10065
10066 result = CallNextEventHandler (next_handler, event);
10067 if (result != eventNotHandledErr)
10068 return result;
10069
10070 switch (GetEventKind (event))
10071 {
10072 case kEventFontPanelClosed:
10073 id_key = Qpanel_closed;
10074 num_params = 0;
10075 names = NULL;
10076 types = NULL;
10077 break;
10078
10079 case kEventFontSelection:
10080 id_key = Qselection;
10081 num_params = sizeof (names_sel) / sizeof (names_sel[0]);
10082 names = names_sel;
10083 types = types_sel;
10084 break;
10085 } 10497 }
10086 10498
10087 err = mac_store_event_ref_as_apple_event (0, 0, Qfont, id_key, 10499 return result;
10088 event, num_params,
10089 names, types);
10090
10091 return err == noErr ? noErr : eventNotHandledErr;
10092} 10500}
10093#endif
10094 10501
10095#if USE_MAC_TSM 10502#if USE_MAC_TSM
10096static pascal OSStatus 10503static pascal OSStatus
@@ -10147,6 +10554,8 @@ mac_handle_text_input_event (next_handler, event, data)
10147 typeUnicodeText}; 10554 typeUnicodeText};
10148 10555
10149 result = CallNextEventHandler (next_handler, event); 10556 result = CallNextEventHandler (next_handler, event);
10557 if (result != eventNotHandledErr)
10558 return result;
10150 10559
10151 switch (GetEventKind (event)) 10560 switch (GetEventKind (event))
10152 { 10561 {
@@ -10174,7 +10583,7 @@ mac_handle_text_input_event (next_handler, event, data)
10174 sizeof (UInt32), NULL, &modifiers); 10583 sizeof (UInt32), NULL, &modifiers);
10175 if (err == noErr && mac_mapped_modifiers (modifiers)) 10584 if (err == noErr && mac_mapped_modifiers (modifiers))
10176 /* There're mapped modifier keys. Process it in 10585 /* There're mapped modifier keys. Process it in
10177 XTread_socket. */ 10586 do_keystroke. */
10178 return eventNotHandledErr; 10587 return eventNotHandledErr;
10179 if (err == noErr) 10588 if (err == noErr)
10180 err = GetEventParameter (kbd_event, kEventParamKeyUnicodes, 10589 err = GetEventParameter (kbd_event, kEventParamKeyUnicodes,
@@ -10189,7 +10598,7 @@ mac_handle_text_input_event (next_handler, event, data)
10189 sizeof (UniChar), NULL, &code); 10598 sizeof (UniChar), NULL, &code);
10190 if (err == noErr && code < 0x80) 10599 if (err == noErr && code < 0x80)
10191 { 10600 {
10192 /* ASCII character. Process it in XTread_socket. */ 10601 /* ASCII character. Process it in do_keystroke. */
10193 if (read_socket_inev && code >= 0x20 && code <= 0x7e) 10602 if (read_socket_inev && code >= 0x20 && code <= 0x7e)
10194 { 10603 {
10195 UInt32 key_code; 10604 UInt32 key_code;
@@ -10251,11 +10660,11 @@ mac_handle_text_input_event (next_handler, event, data)
10251 } 10660 }
10252 10661
10253 p.h = (WINDOW_TO_FRAME_PIXEL_X (w, w->cursor.x) 10662 p.h = (WINDOW_TO_FRAME_PIXEL_X (w, w->cursor.x)
10254 + WINDOW_LEFT_FRINGE_WIDTH (w)); 10663 + WINDOW_LEFT_FRINGE_WIDTH (w)
10664 + f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f));
10255 p.v = (WINDOW_TO_FRAME_PIXEL_Y (w, w->cursor.y) 10665 p.v = (WINDOW_TO_FRAME_PIXEL_Y (w, w->cursor.y)
10256 + FONT_BASE (FRAME_FONT (f))); 10666 + FONT_BASE (FRAME_FONT (f))
10257 SetPortWindowPort (FRAME_MAC_WINDOW (f)); 10667 + f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f));
10258 LocalToGlobal (&p);
10259 err = SetEventParameter (event, kEventParamTextInputReplyPoint, 10668 err = SetEventParameter (event, kEventParamTextInputReplyPoint,
10260 typeQDPoint, sizeof (typeQDPoint), &p); 10669 typeQDPoint, sizeof (typeQDPoint), &p);
10261 } 10670 }
@@ -10269,146 +10678,155 @@ mac_handle_text_input_event (next_handler, event, data)
10269 err = mac_store_event_ref_as_apple_event (0, 0, Qtext_input, id_key, 10678 err = mac_store_event_ref_as_apple_event (0, 0, Qtext_input, id_key,
10270 event, num_params, 10679 event, num_params,
10271 names, types); 10680 names, types);
10681 if (err == noErr)
10682 result = noErr;
10272 10683
10273 return err == noErr ? noErr : result; 10684 return result;
10274} 10685}
10275#endif 10686#endif
10687#endif /* TARGET_API_MAC_CARBON */
10688
10276 10689
10277#ifdef MAC_OSX
10278OSStatus 10690OSStatus
10279mac_store_service_event (event) 10691install_window_handler (window)
10280 EventRef event; 10692 WindowRef window;
10281{ 10693{
10282 OSStatus err; 10694 OSStatus err = noErr;
10283 Lisp_Object id_key;
10284 int num_params;
10285 const EventParamName *names;
10286 const EventParamType *types;
10287 static const EventParamName names_pfm[] =
10288 {kEventParamServiceMessageName, kEventParamServiceUserData};
10289 static const EventParamType types_pfm[] =
10290 {typeCFStringRef, typeCFStringRef};
10291 10695
10292 switch (GetEventKind (event)) 10696#if TARGET_API_MAC_CARBON
10697 if (err == noErr)
10293 { 10698 {
10294 case kEventServicePaste: 10699 static const EventTypeSpec specs[] =
10295 id_key = Qpaste; 10700 {
10296 num_params = 0; 10701 /* -- window refresh events -- */
10297 names = NULL; 10702 {kEventClassWindow, kEventWindowUpdate},
10298 types = NULL; 10703 /* -- window state change events -- */
10299 break; 10704 {kEventClassWindow, kEventWindowShowing},
10705 {kEventClassWindow, kEventWindowHiding},
10706 {kEventClassWindow, kEventWindowShown},
10707 {kEventClassWindow, kEventWindowHidden},
10708 {kEventClassWindow, kEventWindowCollapsed},
10709 {kEventClassWindow, kEventWindowExpanded},
10710 {kEventClassWindow, kEventWindowBoundsChanging},
10711 {kEventClassWindow, kEventWindowBoundsChanged},
10712 /* -- window action events -- */
10713 {kEventClassWindow, kEventWindowClose},
10714 {kEventClassWindow, kEventWindowGetIdealSize},
10715#ifdef MAC_OSX
10716 {kEventClassWindow, kEventWindowToolbarSwitchMode},
10717#endif
10718#if USE_MAC_TSM
10719 /* -- window focus events -- */
10720 {kEventClassWindow, kEventWindowFocusAcquired},
10721 {kEventClassWindow, kEventWindowFocusRelinquish},
10722#endif
10723 };
10724 static EventHandlerUPP handle_window_eventUPP = NULL;
10300 10725
10301 case kEventServicePerform: 10726 if (handle_window_eventUPP == NULL)
10302 id_key = Qperform; 10727 handle_window_eventUPP = NewEventHandlerUPP (mac_handle_window_event);
10303 num_params = sizeof (names_pfm) / sizeof (names_pfm[0]);
10304 names = names_pfm;
10305 types = types_pfm;
10306 break;
10307 10728
10308 default: 10729 err = InstallWindowEventHandler (window, handle_window_eventUPP,
10309 abort (); 10730 GetEventTypeCount (specs),
10731 specs, NULL, NULL);
10310 } 10732 }
10733#endif
10311 10734
10312 err = mac_store_event_ref_as_apple_event (0, 0, Qservice, id_key, 10735 if (err == noErr)
10313 event, num_params, 10736 err = install_drag_handler (window);
10314 names, types);
10315 10737
10316 return err; 10738 return err;
10317} 10739}
10318#endif /* MAC_OSX */
10319#endif /* USE_CARBON_EVENTS */
10320 10740
10741void
10742remove_window_handler (window)
10743 WindowRef window;
10744{
10745 remove_drag_handler (window);
10746}
10321 10747
10322OSStatus 10748#if TARGET_API_MAC_CARBON
10323install_window_handler (window) 10749static OSStatus
10324 WindowPtr window; 10750install_application_handler ()
10325{ 10751{
10326 OSStatus err = noErr; 10752 OSStatus err = noErr;
10327#if USE_CARBON_EVENTS
10328 static const EventTypeSpec specs_window[] =
10329 {{kEventClassWindow, kEventWindowUpdate},
10330 {kEventClassWindow, kEventWindowGetIdealSize},
10331 {kEventClassWindow, kEventWindowBoundsChanging},
10332 {kEventClassWindow, kEventWindowBoundsChanged},
10333 {kEventClassWindow, kEventWindowShown},
10334 {kEventClassWindow, kEventWindowHidden},
10335 {kEventClassWindow, kEventWindowExpanded},
10336 {kEventClassWindow, kEventWindowCollapsed},
10337 {kEventClassWindow, kEventWindowClose},
10338#ifdef MAC_OSX
10339 {kEventClassWindow, kEventWindowToolbarSwitchMode},
10340#endif
10341#if USE_MAC_TSM
10342 {kEventClassWindow, kEventWindowFocusAcquired},
10343 {kEventClassWindow, kEventWindowFocusRelinquish},
10344#endif
10345 };
10346 static const EventTypeSpec specs_mouse[] =
10347 {{kEventClassMouse, kEventMouseWheelMoved}};
10348 static EventHandlerUPP handle_window_eventUPP = NULL;
10349 static EventHandlerUPP handle_mouse_eventUPP = NULL;
10350#if USE_MAC_FONT_PANEL
10351 static const EventTypeSpec specs_font[] =
10352 {{kEventClassFont, kEventFontPanelClosed},
10353 {kEventClassFont, kEventFontSelection}};
10354 static EventHandlerUPP handle_font_eventUPP = NULL;
10355#endif
10356#if USE_MAC_TSM
10357 static const EventTypeSpec specs_text_input[] =
10358 {{kEventClassTextInput, kEventTextInputUpdateActiveInputArea},
10359 {kEventClassTextInput, kEventTextInputUnicodeForKeyEvent},
10360 {kEventClassTextInput, kEventTextInputOffsetToPos}};
10361 static EventHandlerUPP handle_text_input_eventUPP = NULL;
10362#endif
10363 10753
10364 if (handle_window_eventUPP == NULL) 10754 if (err == noErr)
10365 handle_window_eventUPP = NewEventHandlerUPP (mac_handle_window_event); 10755 {
10366 if (handle_mouse_eventUPP == NULL) 10756 static const EventTypeSpec specs[] = {
10367 handle_mouse_eventUPP = NewEventHandlerUPP (mac_handle_mouse_event);
10368#if USE_MAC_FONT_PANEL
10369 if (handle_font_eventUPP == NULL)
10370 handle_font_eventUPP = NewEventHandlerUPP (mac_handle_font_event);
10371#endif
10372#if USE_MAC_TSM 10757#if USE_MAC_TSM
10373 if (handle_text_input_eventUPP == NULL) 10758 {kEventClassApplication, kEventAppActivated},
10374 handle_text_input_eventUPP = 10759 {kEventClassApplication, kEventAppDeactivated},
10375 NewEventHandlerUPP (mac_handle_text_input_event);
10376#endif 10760#endif
10377 err = InstallWindowEventHandler (window, handle_window_eventUPP, 10761 };
10378 GetEventTypeCount (specs_window), 10762
10379 specs_window, NULL, NULL); 10763 err = InstallApplicationEventHandler (NewEventHandlerUPP
10764 (mac_handle_application_event),
10765 GetEventTypeCount (specs),
10766 specs, NULL, NULL);
10767 }
10768
10380 if (err == noErr) 10769 if (err == noErr)
10381 err = InstallWindowEventHandler (window, handle_mouse_eventUPP, 10770 {
10382 GetEventTypeCount (specs_mouse), 10771 static const EventTypeSpec specs[] =
10383 specs_mouse, NULL, NULL); 10772 {{kEventClassKeyboard, kEventRawKeyDown},
10384#if USE_MAC_FONT_PANEL 10773 {kEventClassKeyboard, kEventRawKeyRepeat},
10774 {kEventClassKeyboard, kEventRawKeyUp}};
10775
10776 err = InstallApplicationEventHandler (NewEventHandlerUPP
10777 (mac_handle_keyboard_event),
10778 GetEventTypeCount (specs),
10779 specs, NULL, NULL);
10780 }
10781
10385 if (err == noErr) 10782 if (err == noErr)
10386 err = InstallWindowEventHandler (window, handle_font_eventUPP, 10783 {
10387 GetEventTypeCount (specs_font), 10784 static const EventTypeSpec specs[] =
10388 specs_font, NULL, NULL); 10785 {{kEventClassCommand, kEventCommandProcess}};
10389#endif 10786
10787 err = InstallApplicationEventHandler (NewEventHandlerUPP
10788 (mac_handle_command_event),
10789 GetEventTypeCount (specs),
10790 specs, NULL, NULL);
10791 }
10792
10793 if (err == noErr)
10794 {
10795 static const EventTypeSpec specs[] =
10796 {{kEventClassMouse, kEventMouseWheelMoved}};
10797
10798 err = InstallApplicationEventHandler (NewEventHandlerUPP
10799 (mac_handle_mouse_event),
10800 GetEventTypeCount (specs),
10801 specs, NULL, NULL);
10802 }
10803
10390#if USE_MAC_TSM 10804#if USE_MAC_TSM
10391 if (err == noErr) 10805 if (err == noErr)
10392 err = InstallWindowEventHandler (window, handle_text_input_eventUPP, 10806 {
10393 GetEventTypeCount (specs_text_input), 10807 static const EventTypeSpec spec[] =
10394 specs_text_input, window, NULL); 10808 {{kEventClassTextInput, kEventTextInputUpdateActiveInputArea},
10395#endif 10809 {kEventClassTextInput, kEventTextInputUnicodeForKeyEvent},
10810 {kEventClassTextInput, kEventTextInputOffsetToPos}};
10811
10812 err = InstallApplicationEventHandler (NewEventHandlerUPP
10813 (mac_handle_text_input_event),
10814 GetEventTypeCount (spec),
10815 spec, NULL, NULL);
10816 }
10396#endif 10817#endif
10818
10397 if (err == noErr) 10819 if (err == noErr)
10398 err = install_drag_handler (window); 10820 err = install_menu_target_item_handler ();
10821
10822#ifdef MAC_OSX
10399 if (err == noErr) 10823 if (err == noErr)
10400 err = install_menu_target_item_handler (window); 10824 err = install_service_handler ();
10825#endif
10401 10826
10402 return err; 10827 return err;
10403} 10828}
10404 10829#endif
10405void
10406remove_window_handler (window)
10407 WindowPtr window;
10408{
10409 remove_drag_handler (window);
10410}
10411
10412 10830
10413static pascal void 10831static pascal void
10414mac_handle_dm_notification (event) 10832mac_handle_dm_notification (event)
@@ -10417,20 +10835,48 @@ mac_handle_dm_notification (event)
10417 mac_screen_config_changed = 1; 10835 mac_screen_config_changed = 1;
10418} 10836}
10419 10837
10838#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1030
10839static void
10840mac_handle_cg_display_reconfig (display, flags, user_info)
10841 CGDirectDisplayID display;
10842 CGDisplayChangeSummaryFlags flags;
10843 void *user_info;
10844{
10845 mac_screen_config_changed = 1;
10846}
10847#endif
10848
10420static OSErr 10849static OSErr
10421init_dm_notification_handler () 10850init_dm_notification_handler ()
10422{ 10851{
10423 OSErr err; 10852 OSErr err = noErr;
10424 static DMNotificationUPP handle_dm_notificationUPP = NULL;
10425 ProcessSerialNumber psn;
10426 10853
10427 if (handle_dm_notificationUPP == NULL) 10854#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1030
10428 handle_dm_notificationUPP = 10855#if MAC_OS_X_VERSION_MIN_REQUIRED == 1020
10429 NewDMNotificationUPP (mac_handle_dm_notification); 10856 if (CGDisplayRegisterReconfigurationCallback != NULL)
10857#endif
10858 {
10859 CGDisplayRegisterReconfigurationCallback (mac_handle_cg_display_reconfig,
10860 NULL);
10861 }
10862#if MAC_OS_X_VERSION_MIN_REQUIRED == 1020
10863 else /* CGDisplayRegisterReconfigurationCallback == NULL */
10864#endif
10865#endif /* MAC_OS_X_VERSION_MAX_ALLOWED >= 1030 */
10866#if MAC_OS_X_VERSION_MAX_ALLOWED < 1030 || MAC_OS_X_VERSION_MIN_REQUIRED == 1020
10867 {
10868 static DMNotificationUPP handle_dm_notificationUPP = NULL;
10869 ProcessSerialNumber psn;
10430 10870
10431 err = GetCurrentProcess (&psn); 10871 if (handle_dm_notificationUPP == NULL)
10432 if (err == noErr) 10872 handle_dm_notificationUPP =
10433 err = DMRegisterNotifyProc (handle_dm_notificationUPP, &psn); 10873 NewDMNotificationUPP (mac_handle_dm_notification);
10874
10875 err = GetCurrentProcess (&psn);
10876 if (err == noErr)
10877 err = DMRegisterNotifyProc (handle_dm_notificationUPP, &psn);
10878 }
10879#endif
10434 10880
10435 return err; 10881 return err;
10436} 10882}
@@ -10575,7 +11021,7 @@ main (void)
10575} 11021}
10576#endif 11022#endif
10577 11023
10578#if !USE_CARBON_EVENTS 11024#if !TARGET_API_MAC_CARBON
10579static RgnHandle mouse_region = NULL; 11025static RgnHandle mouse_region = NULL;
10580 11026
10581Boolean 11027Boolean
@@ -10612,7 +11058,7 @@ mac_wait_next_event (er, sleep_time, dequeue)
10612 er_buf.what = nullEvent; 11058 er_buf.what = nullEvent;
10613 return true; 11059 return true;
10614} 11060}
10615#endif /* not USE_CARBON_EVENTS */ 11061#endif /* not TARGET_API_MAC_CARBON */
10616 11062
10617#if TARGET_API_MAC_CARBON 11063#if TARGET_API_MAC_CARBON
10618OSStatus 11064OSStatus
@@ -10646,44 +11092,6 @@ mac_post_mouse_moved_event ()
10646 11092
10647 return err; 11093 return err;
10648} 11094}
10649
10650static void
10651mac_set_unicode_keystroke_event (code, buf)
10652 UniChar code;
10653 struct input_event *buf;
10654{
10655 int charset_id, c1, c2;
10656
10657 if (code < 0x80)
10658 {
10659 buf->kind = ASCII_KEYSTROKE_EVENT;
10660 buf->code = code;
10661 }
10662 else if (code < 0x100)
10663 {
10664 if (code < 0xA0)
10665 charset_id = CHARSET_8_BIT_CONTROL;
10666 else
10667 charset_id = charset_latin_iso8859_1;
10668 buf->kind = MULTIBYTE_CHAR_KEYSTROKE_EVENT;
10669 buf->code = MAKE_CHAR (charset_id, code, 0);
10670 }
10671 else
10672 {
10673 if (code < 0x2500)
10674 charset_id = charset_mule_unicode_0100_24ff,
10675 code -= 0x100;
10676 else if (code < 0x33FF)
10677 charset_id = charset_mule_unicode_2500_33ff,
10678 code -= 0x2500;
10679 else if (code >= 0xE000)
10680 charset_id = charset_mule_unicode_e000_ffff,
10681 code -= 0xE000;
10682 c1 = (code / 96) + 32, c2 = (code % 96) + 32;
10683 buf->kind = MULTIBYTE_CHAR_KEYSTROKE_EVENT;
10684 buf->code = MAKE_CHAR (charset_id, c1, c2);
10685 }
10686}
10687#endif 11095#endif
10688 11096
10689/* Emacs calls this whenever it wants to read an input event from the 11097/* Emacs calls this whenever it wants to read an input event from the
@@ -10695,7 +11103,7 @@ XTread_socket (sd, expected, hold_quit)
10695{ 11103{
10696 struct input_event inev; 11104 struct input_event inev;
10697 int count = 0; 11105 int count = 0;
10698#if USE_CARBON_EVENTS 11106#if TARGET_API_MAC_CARBON
10699 EventRef eventRef; 11107 EventRef eventRef;
10700 EventTargetRef toolbox_dispatcher; 11108 EventTargetRef toolbox_dispatcher;
10701#endif 11109#endif
@@ -10716,7 +11124,7 @@ XTread_socket (sd, expected, hold_quit)
10716 11124
10717 ++handling_signal; 11125 ++handling_signal;
10718 11126
10719#if USE_CARBON_EVENTS 11127#if TARGET_API_MAC_CARBON
10720 toolbox_dispatcher = GetEventDispatcherTarget (); 11128 toolbox_dispatcher = GetEventDispatcherTarget ();
10721 11129
10722 while ( 11130 while (
@@ -10725,9 +11133,9 @@ XTread_socket (sd, expected, hold_quit)
10725#endif 11133#endif
10726 !ReceiveNextEvent (0, NULL, kEventDurationNoWait, 11134 !ReceiveNextEvent (0, NULL, kEventDurationNoWait,
10727 kEventRemoveFromQueue, &eventRef)) 11135 kEventRemoveFromQueue, &eventRef))
10728#else /* !USE_CARBON_EVENTS */ 11136#else /* !TARGET_API_MAC_CARBON */
10729 while (mac_wait_next_event (&er, 0, true)) 11137 while (mac_wait_next_event (&er, 0, true))
10730#endif /* !USE_CARBON_EVENTS */ 11138#endif /* !TARGET_API_MAC_CARBON */
10731 { 11139 {
10732 int do_help = 0; 11140 int do_help = 0;
10733 struct frame *f; 11141 struct frame *f;
@@ -10737,44 +11145,33 @@ XTread_socket (sd, expected, hold_quit)
10737 inev.kind = NO_EVENT; 11145 inev.kind = NO_EVENT;
10738 inev.arg = Qnil; 11146 inev.arg = Qnil;
10739 11147
10740#if USE_CARBON_EVENTS 11148#if TARGET_API_MAC_CARBON
10741 timestamp = GetEventTime (eventRef) / kEventDurationMillisecond; 11149 timestamp = GetEventTime (eventRef) / kEventDurationMillisecond;
10742#else
10743 timestamp = er.when * (1000 / 60); /* ticks to milliseconds */
10744#endif
10745 11150
10746#if USE_CARBON_EVENTS
10747 /* Handle new events */
10748 if (!mac_convert_event_ref (eventRef, &er)) 11151 if (!mac_convert_event_ref (eventRef, &er))
10749 { 11152 goto OTHER;
10750 /* There used to be a handler for the kEventMouseWheelMoved 11153#else /* !TARGET_API_MAC_CARBON */
10751 event here. But as of Mac OS X 10.4, this kind of event 11154 timestamp = er.when * (1000 / 60); /* ticks to milliseconds */
10752 is not directly posted to the main event queue by 11155#endif /* !TARGET_API_MAC_CARBON */
10753 two-finger scrolling on the trackpad. Instead, some 11156
10754 private event is posted and it is converted to a wheel
10755 event by the default handler for the application target.
10756 The converted one can be received by a Carbon event
10757 handler installed on a window target. */
10758 read_socket_inev = &inev;
10759 SendEventToEventTarget (eventRef, toolbox_dispatcher);
10760 read_socket_inev = NULL;
10761 }
10762 else
10763#endif /* USE_CARBON_EVENTS */
10764 switch (er.what) 11157 switch (er.what)
10765 { 11158 {
10766 case mouseDown: 11159 case mouseDown:
10767 case mouseUp: 11160 case mouseUp:
10768 { 11161 {
10769 WindowPtr window_ptr; 11162 WindowRef window_ptr;
10770 ControlPartCode part_code; 11163 ControlPartCode part_code;
10771 int tool_bar_p = 0; 11164 int tool_bar_p = 0;
10772 11165
10773#if USE_CARBON_EVENTS 11166#if TARGET_API_MAC_CARBON
11167 OSStatus err;
11168
10774 /* This is needed to send mouse events like aqua window 11169 /* This is needed to send mouse events like aqua window
10775 buttons to the correct handler. */ 11170 buttons to the correct handler. */
10776 if (SendEventToEventTarget (eventRef, toolbox_dispatcher) 11171 read_socket_inev = &inev;
10777 != eventNotHandledErr) 11172 err = SendEventToEventTarget (eventRef, toolbox_dispatcher);
11173 read_socket_inev = NULL;
11174 if (err != eventNotHandledErr)
10778 break; 11175 break;
10779#endif 11176#endif
10780 last_mouse_glyph_frame = 0; 11177 last_mouse_glyph_frame = 0;
@@ -10822,17 +11219,20 @@ XTread_socket (sd, expected, hold_quit)
10822 else 11219 else
10823 { 11220 {
10824 ControlPartCode control_part_code; 11221 ControlPartCode control_part_code;
10825 ControlHandle ch; 11222 ControlRef ch;
10826 Point mouse_loc = er.where; 11223 Point mouse_loc;
10827#ifdef MAC_OSX 11224#ifdef MAC_OSX
10828 ControlKind control_kind; 11225 ControlKind control_kind;
10829#endif 11226#endif
10830 11227
10831 f = mac_window_to_frame (window_ptr); 11228 f = mac_window_to_frame (window_ptr);
10832 /* convert to local coordinates of new window */ 11229 /* convert to local coordinates of new window */
10833 SetPortWindowPort (window_ptr); 11230 mouse_loc.h = (er.where.h
10834 11231 - (f->left_pos
10835 GlobalToLocal (&mouse_loc); 11232 + FRAME_OUTER_TO_INNER_DIFF_X (f)));
11233 mouse_loc.v = (er.where.v
11234 - (f->top_pos
11235 + FRAME_OUTER_TO_INNER_DIFF_Y (f)));
10836#if TARGET_API_MAC_CARBON 11236#if TARGET_API_MAC_CARBON
10837 ch = FindControlUnderMouse (mouse_loc, window_ptr, 11237 ch = FindControlUnderMouse (mouse_loc, window_ptr,
10838 &control_part_code); 11238 &control_part_code);
@@ -10845,7 +11245,7 @@ XTread_socket (sd, expected, hold_quit)
10845 &ch); 11245 &ch);
10846#endif 11246#endif
10847 11247
10848#if USE_CARBON_EVENTS 11248#if TARGET_API_MAC_CARBON
10849 inev.code = mac_get_mouse_btn (eventRef); 11249 inev.code = mac_get_mouse_btn (eventRef);
10850 inev.modifiers = mac_event_to_emacs_modifiers (eventRef); 11250 inev.modifiers = mac_event_to_emacs_modifiers (eventRef);
10851#else 11251#else
@@ -10981,16 +11381,14 @@ XTread_socket (sd, expected, hold_quit)
10981 DragWindow (window_ptr, er.where, NULL); 11381 DragWindow (window_ptr, er.where, NULL);
10982#else /* not TARGET_API_MAC_CARBON */ 11382#else /* not TARGET_API_MAC_CARBON */
10983 DragWindow (window_ptr, er.where, &qd.screenBits.bounds); 11383 DragWindow (window_ptr, er.where, &qd.screenBits.bounds);
10984#endif /* not TARGET_API_MAC_CARBON */
10985 /* Update the frame parameters. */ 11384 /* Update the frame parameters. */
10986#if !USE_CARBON_EVENTS
10987 { 11385 {
10988 struct frame *f = mac_window_to_frame (window_ptr); 11386 struct frame *f = mac_window_to_frame (window_ptr);
10989 11387
10990 if (f && !f->async_iconified) 11388 if (f && !f->async_iconified)
10991 mac_handle_origin_change (f); 11389 mac_handle_origin_change (f);
10992 } 11390 }
10993#endif 11391#endif /* not TARGET_API_MAC_CARBON */
10994 break; 11392 break;
10995 11393
10996 case inGoAway: 11394 case inGoAway:
@@ -11020,35 +11418,17 @@ XTread_socket (sd, expected, hold_quit)
11020 } 11418 }
11021 break; 11419 break;
11022 11420
11421#if !TARGET_API_MAC_CARBON
11023 case updateEvt: 11422 case updateEvt:
11024#if USE_CARBON_EVENTS 11423 do_window_update ((WindowRef) er.message);
11025 if (SendEventToEventTarget (eventRef, toolbox_dispatcher)
11026 != eventNotHandledErr)
11027 break;
11028#else
11029 do_window_update ((WindowPtr) er.message);
11030#endif
11031 break; 11424 break;
11425#endif
11032 11426
11033 case osEvt: 11427 case osEvt:
11034#if USE_CARBON_EVENTS
11035 if (SendEventToEventTarget (eventRef, toolbox_dispatcher)
11036 != eventNotHandledErr)
11037 break;
11038#endif
11039 switch ((er.message >> 24) & 0x000000FF) 11428 switch ((er.message >> 24) & 0x000000FF)
11040 { 11429 {
11041 case suspendResumeMessage:
11042#if USE_MAC_TSM
11043 if (er.message & resumeFlag)
11044 mac_tsm_resume ();
11045 else
11046 mac_tsm_suspend ();
11047#endif
11048 break;
11049
11050 case mouseMovedMessage: 11430 case mouseMovedMessage:
11051#if !USE_CARBON_EVENTS 11431#if !TARGET_API_MAC_CARBON
11052 SetRectRgn (mouse_region, er.where.h, er.where.v, 11432 SetRectRgn (mouse_region, er.where.h, er.where.v,
11053 er.where.h + 1, er.where.v + 1); 11433 er.where.h + 1, er.where.v + 1);
11054#endif 11434#endif
@@ -11069,13 +11449,15 @@ XTread_socket (sd, expected, hold_quit)
11069 11449
11070 if (f) 11450 if (f)
11071 { 11451 {
11072 WindowPtr wp = FRAME_MAC_WINDOW (f); 11452 WindowRef wp = FRAME_MAC_WINDOW (f);
11073 Point mouse_pos = er.where; 11453 Point mouse_pos;
11074 11454
11075 SetPortWindowPort (wp); 11455 mouse_pos.h = (er.where.h
11076 11456 - (f->left_pos
11077 GlobalToLocal (&mouse_pos); 11457 + FRAME_OUTER_TO_INNER_DIFF_X (f)));
11078 11458 mouse_pos.v = (er.where.v
11459 - (f->top_pos
11460 + FRAME_OUTER_TO_INNER_DIFF_Y (f)));
11079 if (dpyinfo->grabbed && tracked_scroll_bar) 11461 if (dpyinfo->grabbed && tracked_scroll_bar)
11080#ifdef USE_TOOLKIT_SCROLL_BARS 11462#ifdef USE_TOOLKIT_SCROLL_BARS
11081 x_scroll_bar_handle_drag (wp, tracked_scroll_bar, 11463 x_scroll_bar_handle_drag (wp, tracked_scroll_bar,
@@ -11123,18 +11505,16 @@ XTread_socket (sd, expected, hold_quit)
11123 if (!NILP (help_echo_string) || !NILP (previous_help_echo_string)) 11505 if (!NILP (help_echo_string) || !NILP (previous_help_echo_string))
11124 do_help = 1; 11506 do_help = 1;
11125 break; 11507 break;
11508
11509 default:
11510 goto OTHER;
11126 } 11511 }
11127 break; 11512 break;
11128 11513
11129 case activateEvt: 11514 case activateEvt:
11130 { 11515 {
11131 WindowPtr window_ptr = (WindowPtr) er.message; 11516 WindowRef window_ptr = (WindowRef) er.message;
11132 11517
11133#if USE_CARBON_EVENTS
11134 if (SendEventToEventTarget (eventRef, toolbox_dispatcher)
11135 != eventNotHandledErr)
11136 break;
11137#endif
11138 if (window_ptr == tip_window) 11518 if (window_ptr == tip_window)
11139 { 11519 {
11140 HideWindow (tip_window); 11520 HideWindow (tip_window);
@@ -11142,21 +11522,26 @@ XTread_socket (sd, expected, hold_quit)
11142 } 11522 }
11143 11523
11144 if (!is_emacs_window (window_ptr)) 11524 if (!is_emacs_window (window_ptr))
11145 break; 11525 goto OTHER;
11526
11527 f = mac_window_to_frame (window_ptr);
11146 11528
11147 if ((er.modifiers & activeFlag) != 0) 11529 if ((er.modifiers & activeFlag) != 0)
11148 { 11530 {
11149 /* A window has been activated */ 11531 /* A window has been activated */
11150 Point mouse_loc = er.where; 11532 Point mouse_loc;
11151 11533
11152 x_detect_focus_change (dpyinfo, &er, &inev); 11534 x_detect_focus_change (dpyinfo, &er, &inev);
11153 11535
11154 SetPortWindowPort (window_ptr); 11536 mouse_loc.h = (er.where.h
11155 GlobalToLocal (&mouse_loc); 11537 - (f->left_pos
11538 + FRAME_OUTER_TO_INNER_DIFF_X (f)));
11539 mouse_loc.v = (er.where.v
11540 - (f->top_pos
11541 + FRAME_OUTER_TO_INNER_DIFF_Y (f)));
11156 /* Window-activated event counts as mouse movement, 11542 /* Window-activated event counts as mouse movement,
11157 so update things that depend on mouse position. */ 11543 so update things that depend on mouse position. */
11158 note_mouse_movement (mac_window_to_frame (window_ptr), 11544 note_mouse_movement (f, &mouse_loc);
11159 &mouse_loc);
11160 } 11545 }
11161 else 11546 else
11162 { 11547 {
@@ -11181,7 +11566,6 @@ XTread_socket (sd, expected, hold_quit)
11181 11566
11182 x_detect_focus_change (dpyinfo, &er, &inev); 11567 x_detect_focus_change (dpyinfo, &er, &inev);
11183 11568
11184 f = mac_window_to_frame (window_ptr);
11185 if (f == dpyinfo->mouse_face_mouse_frame) 11569 if (f == dpyinfo->mouse_face_mouse_frame)
11186 { 11570 {
11187 /* If we move outside the frame, then we're 11571 /* If we move outside the frame, then we're
@@ -11204,218 +11588,26 @@ XTread_socket (sd, expected, hold_quit)
11204 case keyDown: 11588 case keyDown:
11205 case keyUp: 11589 case keyUp:
11206 case autoKey: 11590 case autoKey:
11207 { 11591 ObscureCursor ();
11208 int keycode = (er.message & keyCodeMask) >> 8;
11209 static SInt16 last_key_script = -1;
11210 SInt16 current_key_script;
11211 UInt32 modifiers = er.modifiers, mapped_modifiers;
11212 11592
11213#if USE_CARBON_EVENTS && defined (MAC_OSX) 11593 f = mac_focus_frame (dpyinfo);
11214 GetEventParameter (eventRef, kEventParamKeyModifiers, 11594 XSETFRAME (inev.frame_or_window, f);
11215 typeUInt32, NULL,
11216 sizeof (UInt32), NULL, &modifiers);
11217#endif
11218 mapped_modifiers = mac_mapped_modifiers (modifiers);
11219
11220#if USE_CARBON_EVENTS && (defined (MAC_OSX) || USE_MAC_TSM)
11221 /* When using Carbon Events, we need to pass raw keyboard
11222 events to the TSM ourselves. If TSM handles it, it
11223 will pass back noErr, otherwise it will pass back
11224 "eventNotHandledErr" and we can process it
11225 normally. */
11226 if (!(mapped_modifiers
11227 & ~(mac_pass_command_to_system ? cmdKey : 0)
11228 & ~(mac_pass_control_to_system ? controlKey : 0)))
11229 {
11230 OSStatus err;
11231
11232 read_socket_inev = &inev;
11233 err = SendEventToEventTarget (eventRef, toolbox_dispatcher);
11234 read_socket_inev = NULL;
11235 if (err != eventNotHandledErr)
11236 break;
11237 }
11238#endif
11239 if (er.what == keyUp)
11240 break;
11241
11242 ObscureCursor ();
11243
11244 f = mac_focus_frame (dpyinfo);
11245
11246 if (!dpyinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight)
11247 && !EQ (f->tool_bar_window, dpyinfo->mouse_face_window))
11248 {
11249 clear_mouse_face (dpyinfo);
11250 dpyinfo->mouse_face_hidden = 1;
11251 }
11252
11253 current_key_script = GetScriptManagerVariable (smKeyScript);
11254 if (last_key_script != current_key_script)
11255 {
11256 struct input_event event;
11257
11258 EVENT_INIT (event);
11259 event.kind = LANGUAGE_CHANGE_EVENT;
11260 event.arg = Qnil;
11261 event.code = current_key_script;
11262 event.timestamp = timestamp;
11263 kbd_buffer_store_event (&event);
11264 count++;
11265 last_key_script = current_key_script;
11266 }
11267
11268#if USE_MAC_TSM
11269 if (inev.kind != NO_EVENT)
11270 break;
11271#endif
11272
11273#ifdef MAC_OSX
11274 if (mapped_modifiers & kEventKeyModifierFnMask
11275 && keycode <= 0x7f
11276 && fn_keycode_to_keycode_table[keycode])
11277 keycode = fn_keycode_to_keycode_table[keycode];
11278#endif
11279 if (keycode <= 0x7f && keycode_to_xkeysym_table [keycode])
11280 {
11281 inev.kind = NON_ASCII_KEYSTROKE_EVENT;
11282 inev.code = 0xff00 | keycode_to_xkeysym_table [keycode];
11283#ifdef MAC_OSX
11284 if (modifiers & kEventKeyModifierFnMask
11285 && keycode <= 0x7f
11286 && fn_keycode_to_keycode_table[keycode] == keycode)
11287 modifiers &= ~kEventKeyModifierFnMask;
11288#endif
11289 }
11290 else if (mapped_modifiers)
11291 {
11292 /* translate the keycode back to determine the
11293 original key */
11294#ifdef MAC_OSX
11295 UCKeyboardLayout *uchr_ptr = NULL;
11296#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1020
11297 OSStatus err;
11298 KeyboardLayoutRef layout;
11299
11300 err = KLGetCurrentKeyboardLayout (&layout);
11301 if (err == noErr)
11302 KLGetKeyboardLayoutProperty (layout, kKLuchrData,
11303 (const void **) &uchr_ptr);
11304#else
11305 static SInt16 last_key_layout_id = 0;
11306 static Handle uchr_handle = (Handle)-1;
11307 SInt16 current_key_layout_id =
11308 GetScriptVariable (current_key_script, smScriptKeys);
11309
11310 if (uchr_handle == (Handle)-1
11311 || last_key_layout_id != current_key_layout_id)
11312 {
11313 uchr_handle = GetResource ('uchr', current_key_layout_id);
11314 last_key_layout_id = current_key_layout_id;
11315 }
11316 if (uchr_handle)
11317 uchr_ptr = (UCKeyboardLayout *)*uchr_handle;
11318#endif
11319
11320 if (uchr_ptr)
11321 {
11322 OSStatus status;
11323 UInt16 key_action = er.what - keyDown;
11324 UInt32 modifier_key_state =
11325 (modifiers & ~mapped_modifiers) >> 8;
11326 UInt32 keyboard_type = LMGetKbdType ();
11327 SInt32 dead_key_state = 0;
11328 UniChar code;
11329 UniCharCount actual_length;
11330
11331 status = UCKeyTranslate (uchr_ptr,
11332 keycode, key_action,
11333 modifier_key_state,
11334 keyboard_type,
11335 kUCKeyTranslateNoDeadKeysMask,
11336 &dead_key_state,
11337 1, &actual_length, &code);
11338 if (status == noErr && actual_length == 1)
11339 mac_set_unicode_keystroke_event (code, &inev);
11340 }
11341#endif /* MAC_OSX */
11342
11343 if (inev.kind == NO_EVENT)
11344 {
11345 /* This code comes from Keyboard Resource,
11346 Appendix C of IM - Text. This is necessary
11347 since shift is ignored in KCHR table
11348 translation when option or command is pressed.
11349 It also does not translate correctly
11350 control-shift chars like C-% so mask off shift
11351 here also. */
11352 /* Mask off modifier keys that are mapped to some
11353 Emacs modifiers. */
11354 int new_modifiers = er.modifiers & ~mapped_modifiers;
11355 /* set high byte of keycode to modifier high byte*/
11356 int new_keycode = keycode | new_modifiers;
11357 Ptr kchr_ptr = (Ptr) GetScriptManagerVariable (smKCHRCache);
11358 unsigned long some_state = 0;
11359 UInt32 new_char_code;
11360
11361 new_char_code = KeyTranslate (kchr_ptr, new_keycode,
11362 &some_state);
11363 if (new_char_code == 0)
11364 /* Seems like a dead key. Append up-stroke. */
11365 new_char_code = KeyTranslate (kchr_ptr,
11366 new_keycode | 0x80,
11367 &some_state);
11368 if (new_char_code)
11369 {
11370 inev.kind = ASCII_KEYSTROKE_EVENT;
11371 inev.code = new_char_code & 0xff;
11372 }
11373 }
11374 }
11375
11376 if (inev.kind == NO_EVENT)
11377 {
11378 inev.kind = ASCII_KEYSTROKE_EVENT;
11379 inev.code = er.message & charCodeMask;
11380 }
11381
11382 inev.modifiers = mac_to_emacs_modifiers (modifiers);
11383 inev.modifiers |= (extra_keyboard_modifiers
11384 & (meta_modifier | alt_modifier
11385 | hyper_modifier | super_modifier));
11386 XSETFRAME (inev.frame_or_window, f);
11387 11595
11596 /* If mouse-highlight is an integer, input clears out mouse
11597 highlighting. */
11598 if (!dpyinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight)
11599 && !EQ (f->tool_bar_window, dpyinfo->mouse_face_window))
11600 {
11601 clear_mouse_face (dpyinfo);
11602 dpyinfo->mouse_face_hidden = 1;
11603 }
11388#if TARGET_API_MAC_CARBON 11604#if TARGET_API_MAC_CARBON
11389 if (inev.kind == ASCII_KEYSTROKE_EVENT 11605 goto OTHER;
11390 && inev.code >= 0x80 && inev.modifiers) 11606#else
11391 { 11607 do_keystroke (er.what, er.message & charCodeMask,
11392 OSStatus err; 11608 (er.message & keyCodeMask) >> 8,
11393 TextEncoding encoding = kTextEncodingMacRoman; 11609 er.modifiers, timestamp, &inev);
11394 TextToUnicodeInfo ttu_info;
11395
11396 UpgradeScriptInfoToTextEncoding (current_key_script,
11397 kTextLanguageDontCare,
11398 kTextRegionDontCare,
11399 NULL, &encoding);
11400 err = CreateTextToUnicodeInfoByEncoding (encoding, &ttu_info);
11401 if (err == noErr)
11402 {
11403 UniChar code;
11404 Str255 pstr;
11405 ByteCount unicode_len;
11406
11407 pstr[0] = 1;
11408 pstr[1] = inev.code;
11409 err = ConvertFromPStringToUnicode (ttu_info, pstr,
11410 sizeof (UniChar),
11411 &unicode_len, &code);
11412 if (err == noErr && unicode_len == sizeof (UniChar))
11413 mac_set_unicode_keystroke_event (code, &inev);
11414 DisposeTextToUnicodeInfo (&ttu_info);
11415 }
11416 }
11417#endif 11610#endif
11418 }
11419 break; 11611 break;
11420 11612
11421 case kHighLevelEvent: 11613 case kHighLevelEvent:
@@ -11423,9 +11615,19 @@ XTread_socket (sd, expected, hold_quit)
11423 break; 11615 break;
11424 11616
11425 default: 11617 default:
11618 OTHER:
11619#if TARGET_API_MAC_CARBON
11620 {
11621 OSStatus err;
11622
11623 read_socket_inev = &inev;
11624 err = SendEventToEventTarget (eventRef, toolbox_dispatcher);
11625 read_socket_inev = NULL;
11626 }
11627#endif
11426 break; 11628 break;
11427 } 11629 }
11428#if USE_CARBON_EVENTS 11630#if TARGET_API_MAC_CARBON
11429 ReleaseEvent (eventRef); 11631 ReleaseEvent (eventRef);
11430#endif 11632#endif
11431 11633
@@ -11477,7 +11679,7 @@ XTread_socket (sd, expected, hold_quit)
11477 mac_screen_config_changed = 0; 11679 mac_screen_config_changed = 0;
11478 } 11680 }
11479 11681
11480#if !USE_CARBON_EVENTS 11682#if !TARGET_API_MAC_CARBON
11481 /* Check which frames are still visible. We do this here because 11683 /* Check which frames are still visible. We do this here because
11482 there doesn't seem to be any direct notification from the Window 11684 there doesn't seem to be any direct notification from the Window
11483 Manager that the visibility of a window has changed (at least, 11685 Manager that the visibility of a window has changed (at least,
@@ -11582,7 +11784,7 @@ make_mac_terminal_frame (struct frame *f)
11582 11784
11583 if (!(FRAME_MAC_WINDOW (f) = 11785 if (!(FRAME_MAC_WINDOW (f) =
11584 NewCWindow (NULL, &r, "\p", true, dBoxProc, 11786 NewCWindow (NULL, &r, "\p", true, dBoxProc,
11585 (WindowPtr) -1, 1, (long) f->output_data.mac))) 11787 (WindowRef) -1, 1, (long) f->output_data.mac)))
11586 abort (); 11788 abort ();
11587 /* so that update events can find this mac_output struct */ 11789 /* so that update events can find this mac_output struct */
11588 f->output_data.mac->mFP = f; /* point back to emacs frame */ 11790 f->output_data.mac->mFP = f; /* point back to emacs frame */
@@ -11780,7 +11982,6 @@ init_menu_bar ()
11780 &menu, &menu_index); 11982 &menu, &menu_index);
11781 if (err == noErr) 11983 if (err == noErr)
11782 SetMenuItemCommandKey (menu, menu_index, false, 0); 11984 SetMenuItemCommandKey (menu, menu_index, false, 0);
11783#if USE_CARBON_EVENTS
11784 EnableMenuCommand (NULL, kHICommandPreferences); 11985 EnableMenuCommand (NULL, kHICommandPreferences);
11785 err = GetIndMenuItemWithCommandID (NULL, kHICommandPreferences, 1, 11986 err = GetIndMenuItemWithCommandID (NULL, kHICommandPreferences, 1,
11786 &menu, &menu_index); 11987 &menu, &menu_index);
@@ -11792,10 +11993,9 @@ init_menu_bar ()
11792 InsertMenuItemTextWithCFString (menu, CFSTR ("About Emacs"), 11993 InsertMenuItemTextWithCFString (menu, CFSTR ("About Emacs"),
11793 0, 0, kHICommandAbout); 11994 0, 0, kHICommandAbout);
11794 } 11995 }
11795#endif /* USE_CARBON_EVENTS */
11796#else /* !MAC_OSX */ 11996#else /* !MAC_OSX */
11797#if USE_CARBON_EVENTS 11997#if TARGET_API_MAC_CARBON
11798 SetMenuItemCommandID (GetMenuHandle (M_APPLE), I_ABOUT, kHICommandAbout); 11998 SetMenuItemCommandID (GetMenuRef (M_APPLE), I_ABOUT, kHICommandAbout);
11799#endif 11999#endif
11800#endif 12000#endif
11801} 12001}
@@ -11929,19 +12129,13 @@ mac_initialize ()
11929 12129
11930#if TARGET_API_MAC_CARBON 12130#if TARGET_API_MAC_CARBON
11931 12131
11932#if USE_CARBON_EVENTS 12132 install_application_handler ();
11933#ifdef MAC_OSX
11934 init_service_handler ();
11935#endif /* MAC_OSX */
11936
11937 init_command_handler ();
11938 12133
11939 init_menu_bar (); 12134 init_menu_bar ();
11940 12135
11941#if USE_MAC_TSM 12136#if USE_MAC_TSM
11942 init_tsm (); 12137 init_tsm ();
11943#endif 12138#endif
11944#endif /* USE_CARBON_EVENTS */
11945 12139
11946#ifdef MAC_OSX 12140#ifdef MAC_OSX
11947 init_coercion_handler (); 12141 init_coercion_handler ();
@@ -11992,7 +12186,7 @@ syms_of_macterm ()
11992 Fput (Qhyper, Qmodifier_value, make_number (hyper_modifier)); 12186 Fput (Qhyper, Qmodifier_value, make_number (hyper_modifier));
11993 Fput (Qsuper, Qmodifier_value, make_number (super_modifier)); 12187 Fput (Qsuper, Qmodifier_value, make_number (super_modifier));
11994 12188
11995#if USE_CARBON_EVENTS 12189#if TARGET_API_MAC_CARBON
11996 Qhi_command = intern ("hi-command"); staticpro (&Qhi_command); 12190 Qhi_command = intern ("hi-command"); staticpro (&Qhi_command);
11997#ifdef MAC_OSX 12191#ifdef MAC_OSX
11998 Qtoolbar_switch_mode = intern ("toolbar-switch-mode"); 12192 Qtoolbar_switch_mode = intern ("toolbar-switch-mode");
@@ -12117,7 +12311,7 @@ The symbol `reverse' means that the option-key will register for
12117mouse-3 and the command-key will register for mouse-2. */); 12311mouse-3 and the command-key will register for mouse-2. */);
12118 Vmac_emulate_three_button_mouse = Qnil; 12312 Vmac_emulate_three_button_mouse = Qnil;
12119 12313
12120#if USE_CARBON_EVENTS 12314#if TARGET_API_MAC_CARBON
12121 DEFVAR_BOOL ("mac-wheel-button-is-mouse-2", &mac_wheel_button_is_mouse_2, 12315 DEFVAR_BOOL ("mac-wheel-button-is-mouse-2", &mac_wheel_button_is_mouse_2,
12122 doc: /* *Non-nil if the wheel button is mouse-2 and the right click mouse-3. 12316 doc: /* *Non-nil if the wheel button is mouse-2 and the right click mouse-3.
12123Otherwise, the right click will be treated as mouse-2 and the wheel 12317Otherwise, the right click will be treated as mouse-2 and the wheel