comparison src/macterm.c @ 81242:95935a0c1560

(mac_begin_cg_clip): New arg F. Call SetPortWindowPort. All callers changed so as not to call SetPortWindowPort. (mac_begin_cg_clip) [USE_CG_DRAWING]: Call mac_prepare_for_quickdraw. (mac_draw_image_string_atsui) [USE_ATSUI]: New function created from mac_draw_string_common. (mac_draw_image_string_qd): Likewise. (mac_draw_string_common): Use them. Add INLINE. (XTmouse_position, x_scroll_bar_report_motion) [TARGET_API_MAC_CARBON]: Use FRAME_OUTER_TO_INNER_DIFF_X, FRAME_OUTER_TO_INNER_DIFF_Y, and GetGlobalMouse. (x_set_mouse_pixel_position) [MAC_OSX]: Use FRAME_OUTER_TO_INNER_DIFF_X and FRAME_OUTER_TO_INNER_DIFF_Y. [TARGET_API_MAC_CARBON] (mac_handle_mouse_event): Likewise. [USE_MAC_TSM] (mac_handle_text_input_event): Likewise. (x_make_frame_visible) [TARGET_API_MAC_CARBON]: Move code for repositioning window to mac_handle_window_event. (x_make_frame_invisible) [TARGET_API_MAC_CARBON]: Move code for saving window location to mac_handle_window_event [USE_MAC_FONT_PANEL] (mac_show_hide_font_panel): Install handler here. (install_menu_target_item_handler): Remove argument in extern. [TARGET_API_MAC_CARBON] (mac_event_to_emacs_modifiers): Also accept command events. (do_keystroke): New function created from XTread_socket. (init_command_handler): Remove functions. [TARGET_API_MAC_CARBON] (mac_handle_window_event): Reposition window and save window location by kEventWindowShowing and kEventWindowHiding handlers here. Don't call next handler for window state change and focus events. (mac_handle_application_event, mac_handle_keyboard_event) [TARGET_API_MAC_CARBON]: New functions. (install_window_handler) [TARGET_API_MAC_CARBON]: Register handlers for kEventWindowShowing and kEventWindowHiding events. Move installation of mouse, font, text input and menu target item handlers to install_application_handler. (install_application_handler) [TARGET_API_MAC_CARBON]: New function. (mac_handle_cg_display_reconfig) [MAC_OS_X_VERSION_MAX_ALLOWED >= 1030]: New function. (init_dm_notification_handler) [MAC_OS_X_VERSION_MAX_ALLOWED >= 1030]: Register it. (XTread_socket) [TARGET_API_MAC_CARBON]: Consolidate SendEventToEventTarget calls. Use FRAME_OUTER_TO_INNER_DIFF_X and FRAME_OUTER_TO_INNER_DIFF_Y. Move application activation handler to mac_handle_application_event. Move keyboard handler to mac_handle_keyboard_event. (XTread_socket) [!TARGET_API_MAC_CARBON]: Use do_keystroke. (mac_initialize) [TARGET_API_MAC_CARBON]: Don't call init_command_handler. Call install_application_handler.
author YAMAMOTO Mitsuharu <mituharu@math.s.chiba-u.ac.jp>
date Thu, 07 Jun 2007 08:45:00 +0000
parents c6b1c2b2e312
children 26045b628846 cc587bfd19ca e9f94688a064
comparison
equal deleted inserted replaced
81241:b453bc0cb471 81242:95935a0c1560
409 #endif 409 #endif
410 410
411 static RgnHandle saved_port_clip_region = NULL; 411 static RgnHandle saved_port_clip_region = NULL;
412 412
413 static void 413 static void
414 mac_begin_clip (gc) 414 mac_begin_clip (f, gc)
415 struct frame *f;
415 GC gc; 416 GC gc;
416 { 417 {
417 static RgnHandle new_region = NULL; 418 static RgnHandle new_region = NULL;
418 419
419 if (saved_port_clip_region == NULL) 420 if (saved_port_clip_region == NULL)
420 saved_port_clip_region = NewRgn (); 421 saved_port_clip_region = NewRgn ();
421 if (new_region == NULL) 422 if (new_region == NULL)
422 new_region = NewRgn (); 423 new_region = NewRgn ();
423 424
425 #if USE_CG_DRAWING
426 mac_prepare_for_quickdraw (f);
427 #endif
428 SetPortWindowPort (FRAME_MAC_WINDOW (f));
429
424 if (gc->n_clip_rects) 430 if (gc->n_clip_rects)
425 { 431 {
426 GetClip (saved_port_clip_region); 432 GetClip (saved_port_clip_region);
427 SectRgn (saved_port_clip_region, gc->clip_region, new_region); 433 SectRgn (saved_port_clip_region, gc->clip_region, new_region);
428 SetClip (new_region); 434 SetClip (new_region);
437 SetClip (saved_port_clip_region); 443 SetClip (saved_port_clip_region);
438 } 444 }
439 445
440 446
441 /* X display function emulation */ 447 /* X display function emulation */
442
443 void
444 XFreePixmap (display, pixmap)
445 Display *display; /* not used */
446 Pixmap pixmap;
447 {
448 DisposeGWorld (pixmap);
449 }
450
451 448
452 /* Mac version of XDrawLine. */ 449 /* Mac version of XDrawLine. */
453 450
454 static void 451 static void
455 mac_draw_line (f, gc, x1, y1, x2, y2) 452 mac_draw_line (f, gc, x1, y1, x2, y2)
488 x1--; 485 x1--;
489 else 486 else
490 x2--; 487 x2--;
491 } 488 }
492 489
493 SetPortWindowPort (FRAME_MAC_WINDOW (f)); 490 mac_begin_clip (f, gc);
494
495 RGBForeColor (GC_FORE_COLOR (gc)); 491 RGBForeColor (GC_FORE_COLOR (gc));
496
497 mac_begin_clip (gc);
498 MoveTo (x1, y1); 492 MoveTo (x1, y1);
499 LineTo (x2, y2); 493 LineTo (x2, y2);
500 mac_end_clip (gc); 494 mac_end_clip (gc);
501 #endif 495 #endif
502 } 496 }
548 GC gc; 542 GC gc;
549 int x, y; 543 int x, y;
550 unsigned int width, height; 544 unsigned int width, height;
551 { 545 {
552 #if USE_CG_DRAWING 546 #if USE_CG_DRAWING
553 CGContextRef context; 547 {
554 548 CGContextRef context;
555 context = mac_begin_cg_clip (f, gc); 549
556 CG_SET_FILL_COLOR_WITH_GC_BACKGROUND (context, gc); 550 context = mac_begin_cg_clip (f, gc);
557 CGContextFillRect (context, CGRectMake (x, y, width, height)); 551 CG_SET_FILL_COLOR_WITH_GC_BACKGROUND (context, gc);
558 mac_end_cg_clip (f); 552 CGContextFillRect (context, CGRectMake (x, y, width, height));
553 mac_end_cg_clip (f);
554 }
559 #else 555 #else
560 Rect r; 556 {
561 557 Rect r;
562 SetPortWindowPort (FRAME_MAC_WINDOW (f)); 558
563 559 mac_begin_clip (f, gc);
564 RGBBackColor (GC_BACK_COLOR (gc)); 560 RGBBackColor (GC_BACK_COLOR (gc));
565 SetRect (&r, x, y, x + width, y + height); 561 SetRect (&r, x, y, x + width, y + height);
566 562 EraseRect (&r);
567 mac_begin_clip (gc); 563 RGBBackColor (GC_BACK_COLOR (FRAME_NORMAL_GC (f)));
568 EraseRect (&r); 564 mac_end_clip (gc);
569 mac_end_clip (gc); 565 }
570
571 RGBBackColor (GC_BACK_COLOR (FRAME_NORMAL_GC (f)));
572 #endif 566 #endif
573 } 567 }
574 568
575 569
576 /* Mac version of XClearArea. */ 570 /* Mac version of XClearArea. */
589 static void 583 static void
590 mac_clear_window (f) 584 mac_clear_window (f)
591 struct frame *f; 585 struct frame *f;
592 { 586 {
593 #if USE_CG_DRAWING 587 #if USE_CG_DRAWING
594 CGContextRef context; 588 {
595 GC gc = FRAME_NORMAL_GC (f); 589 CGContextRef context;
596 590 GC gc = FRAME_NORMAL_GC (f);
597 context = mac_begin_cg_clip (f, NULL); 591
598 CG_SET_FILL_COLOR_WITH_GC_BACKGROUND (context, gc); 592 context = mac_begin_cg_clip (f, NULL);
599 CGContextFillRect (context, CGRectMake (0, 0, FRAME_PIXEL_WIDTH (f), 593 CG_SET_FILL_COLOR_WITH_GC_BACKGROUND (context, gc);
600 FRAME_PIXEL_HEIGHT (f))); 594 CGContextFillRect (context, CGRectMake (0, 0, FRAME_PIXEL_WIDTH (f),
601 mac_end_cg_clip (f); 595 FRAME_PIXEL_HEIGHT (f)));
602 #else 596 mac_end_cg_clip (f);
597 }
598 #else /* !USE_CG_DRAWING */
603 SetPortWindowPort (FRAME_MAC_WINDOW (f)); 599 SetPortWindowPort (FRAME_MAC_WINDOW (f));
604 600
605 RGBBackColor (GC_BACK_COLOR (FRAME_NORMAL_GC (f))); 601 RGBBackColor (GC_BACK_COLOR (FRAME_NORMAL_GC (f)));
606 602
607 #if TARGET_API_MAC_CARBON 603 #if TARGET_API_MAC_CARBON
671 667
672 bitmap.rowBytes = sizeof(unsigned short); 668 bitmap.rowBytes = sizeof(unsigned short);
673 bitmap.baseAddr = (char *)bits; 669 bitmap.baseAddr = (char *)bits;
674 SetRect (&(bitmap.bounds), 0, 0, width, height); 670 SetRect (&(bitmap.bounds), 0, 0, width, height);
675 671
676 SetPortWindowPort (FRAME_MAC_WINDOW (f)); 672 mac_begin_clip (f, gc);
677
678 RGBForeColor (GC_FORE_COLOR (gc)); 673 RGBForeColor (GC_FORE_COLOR (gc));
679 RGBBackColor (GC_BACK_COLOR (gc)); 674 RGBBackColor (GC_BACK_COLOR (gc));
680 SetRect (&r, x, y, x + width, y + height); 675 SetRect (&r, x, y, x + width, y + height);
681
682 mac_begin_clip (gc);
683 #if TARGET_API_MAC_CARBON 676 #if TARGET_API_MAC_CARBON
684 { 677 {
685 CGrafPtr port; 678 CGrafPtr port;
686 679
687 GetPort (&port); 680 GetPort (&port);
692 } 685 }
693 #else /* not TARGET_API_MAC_CARBON */ 686 #else /* not TARGET_API_MAC_CARBON */
694 CopyBits (&bitmap, &(FRAME_MAC_WINDOW (f)->portBits), &(bitmap.bounds), &r, 687 CopyBits (&bitmap, &(FRAME_MAC_WINDOW (f)->portBits), &(bitmap.bounds), &r,
695 overlay_p ? srcOr : srcCopy, 0); 688 overlay_p ? srcOr : srcCopy, 0);
696 #endif /* not TARGET_API_MAC_CARBON */ 689 #endif /* not TARGET_API_MAC_CARBON */
690 RGBBackColor (GC_BACK_COLOR (FRAME_NORMAL_GC (f)));
697 mac_end_clip (gc); 691 mac_end_clip (gc);
698
699 RGBBackColor (GC_BACK_COLOR (FRAME_NORMAL_GC (f)));
700 } 692 }
701 #endif /* !USE_CG_DRAWING */ 693 #endif /* !USE_CG_DRAWING */
702 694
703 695
704 /* Mac replacement for XCreateBitmapFromBitmapData. */ 696 /* Mac replacement for XCreateBitmapFromBitmapData. */
745 } 737 }
746 738
747 739
748 Pixmap 740 Pixmap
749 XCreatePixmap (display, w, width, height, depth) 741 XCreatePixmap (display, w, width, height, depth)
750 Display *display; /* not used */ 742 Display *display;
751 WindowRef w; 743 WindowRef w;
752 unsigned int width, height; 744 unsigned int width, height;
753 unsigned int depth; 745 unsigned int depth;
754 { 746 {
755 Pixmap pixmap; 747 Pixmap pixmap;
774 } 766 }
775 767
776 768
777 Pixmap 769 Pixmap
778 XCreatePixmapFromBitmapData (display, w, data, width, height, fg, bg, depth) 770 XCreatePixmapFromBitmapData (display, w, data, width, height, fg, bg, depth)
779 Display *display; /* not used */ 771 Display *display;
780 WindowRef w; 772 WindowRef w;
781 char *data; 773 char *data;
782 unsigned int width, height; 774 unsigned int width, height;
783 unsigned long fg, bg; 775 unsigned long fg, bg;
784 unsigned int depth; 776 unsigned int depth;
785 { 777 {
786 Pixmap pixmap; 778 Pixmap pixmap;
787 BitMap bitmap; 779 BitMap bitmap;
788 CGrafPtr old_port; 780 CGrafPtr old_port;
789 GDHandle old_gdh; 781 GDHandle old_gdh;
790 static GC gc = NULL; /* not reentrant */ 782 static GC gc = NULL;
791 783
792 if (gc == NULL) 784 if (gc == NULL)
793 gc = XCreateGC (display, w, 0, NULL); 785 gc = XCreateGC (display, w, 0, NULL);
794 786
795 pixmap = XCreatePixmap (display, w, width, height, depth); 787 pixmap = XCreatePixmap (display, w, width, height, depth);
817 809
818 return pixmap; 810 return pixmap;
819 } 811 }
820 812
821 813
814 void
815 XFreePixmap (display, pixmap)
816 Display *display;
817 Pixmap pixmap;
818 {
819 DisposeGWorld (pixmap);
820 }
821
822
822 /* Mac replacement for XFillRectangle. */ 823 /* Mac replacement for XFillRectangle. */
823 824
824 static void 825 static void
825 mac_fill_rectangle (f, gc, x, y, width, height) 826 mac_fill_rectangle (f, gc, x, y, width, height)
826 struct frame *f; 827 struct frame *f;
836 CGContextFillRect (context, CGRectMake (x, y, width, height)); 837 CGContextFillRect (context, CGRectMake (x, y, width, height));
837 mac_end_cg_clip (f); 838 mac_end_cg_clip (f);
838 #else 839 #else
839 Rect r; 840 Rect r;
840 841
841 SetPortWindowPort (FRAME_MAC_WINDOW (f)); 842 mac_begin_clip (f, gc);
842
843 RGBForeColor (GC_FORE_COLOR (gc)); 843 RGBForeColor (GC_FORE_COLOR (gc));
844 SetRect (&r, x, y, x + width, y + height); 844 SetRect (&r, x, y, x + width, y + height);
845
846 mac_begin_clip (gc);
847 PaintRect (&r); /* using foreground color of gc */ 845 PaintRect (&r); /* using foreground color of gc */
848 mac_end_clip (gc); 846 mac_end_clip (gc);
849 #endif 847 #endif
850 } 848 }
851 849
868 CGRectMake (x + 0.5f, y + 0.5f, width, height)); 866 CGRectMake (x + 0.5f, y + 0.5f, width, height));
869 mac_end_cg_clip (f); 867 mac_end_cg_clip (f);
870 #else 868 #else
871 Rect r; 869 Rect r;
872 870
873 SetPortWindowPort (FRAME_MAC_WINDOW (f)); 871 mac_begin_clip (f, gc);
874
875 RGBForeColor (GC_FORE_COLOR (gc)); 872 RGBForeColor (GC_FORE_COLOR (gc));
876 SetRect (&r, x, y, x + width + 1, y + height + 1); 873 SetRect (&r, x, y, x + width + 1, y + height + 1);
877
878 mac_begin_clip (gc);
879 FrameRect (&r); /* using foreground color of gc */ 874 FrameRect (&r); /* using foreground color of gc */
880 mac_end_clip (gc); 875 mac_end_clip (gc);
881 #endif 876 #endif
877 }
878
879
880 static void
881 mac_invert_rectangle (f, x, y, width, height)
882 struct frame *f;
883 int x, y;
884 unsigned int width, height;
885 {
886 Rect r;
887
888 #if USE_CG_DRAWING
889 mac_prepare_for_quickdraw (f);
890 #endif
891 SetPortWindowPort (FRAME_MAC_WINDOW (f));
892
893 SetRect (&r, x, y, x + width, y + height);
894
895 InvertRect (&r);
882 } 896 }
883 897
884 898
885 #if USE_ATSUI 899 #if USE_ATSUI
886 static OSStatus 900 static OSStatus
889 UniCharCount text_length; 903 UniCharCount text_length;
890 ATSUStyle style; 904 ATSUStyle style;
891 ATSUTextLayout *text_layout; 905 ATSUTextLayout *text_layout;
892 { 906 {
893 OSStatus err; 907 OSStatus err;
894 static ATSUTextLayout saved_text_layout = NULL; /* not reentrant */ 908 static ATSUTextLayout saved_text_layout = NULL;
895 909
896 if (saved_text_layout == NULL) 910 if (saved_text_layout == NULL)
897 { 911 {
898 static const UniCharCount lengths[] = {kATSUToTextEnd}; 912 static const UniCharCount lengths[] = {kATSUToTextEnd};
899 static const ATSUAttributeTag tags[] = {kATSULineLayoutOptionsTag}; 913 static const ATSUAttributeTag tags[] = {kATSULineLayoutOptionsTag};
916 &saved_text_layout); 930 &saved_text_layout);
917 if (err == noErr) 931 if (err == noErr)
918 err = ATSUSetLayoutControls (saved_text_layout, 932 err = ATSUSetLayoutControls (saved_text_layout,
919 sizeof (tags) / sizeof (tags[0]), 933 sizeof (tags) / sizeof (tags[0]),
920 tags, sizes, values); 934 tags, sizes, values);
921 /* XXX: Should we do this? */
922 if (err == noErr) 935 if (err == noErr)
923 err = ATSUSetTransientFontMatching (saved_text_layout, true); 936 err = ATSUSetTransientFontMatching (saved_text_layout, true);
924 } 937 }
925 else 938 else
926 { 939 {
935 948
936 if (err == noErr) 949 if (err == noErr)
937 *text_layout = saved_text_layout; 950 *text_layout = saved_text_layout;
938 return err; 951 return err;
939 } 952 }
940 #endif
941 953
942 954
943 static void 955 static void
944 mac_invert_rectangle (f, x, y, width, height) 956 mac_draw_image_string_atsui (f, gc, x, y, buf, nchars, bg_width,
957 overstrike_p, bytes_per_char)
945 struct frame *f; 958 struct frame *f;
959 GC gc;
946 int x, y; 960 int x, y;
947 unsigned int width, height; 961 char *buf;
948 { 962 int nchars, bg_width, overstrike_p, bytes_per_char;
949 Rect r; 963 {
964 OSStatus err;
965 ATSUTextLayout text_layout;
966
967 xassert (bytes_per_char == 2);
968
969 #ifndef WORDS_BIG_ENDIAN
970 {
971 int i;
972 UniChar *text = (UniChar *)buf;
973
974 for (i = 0; i < nchars; i++)
975 text[i] = EndianU16_BtoN (text[i]);
976 }
977 #endif
978 err = atsu_get_text_layout_with_text_ptr ((ConstUniCharArrayPtr)buf,
979 nchars,
980 GC_FONT (gc)->mac_style,
981 &text_layout);
982 if (err != noErr)
983 return;
984 #ifdef MAC_OSX
985 if (!mac_use_core_graphics)
986 {
987 #endif
988 mac_begin_clip (f, gc);
989 RGBForeColor (GC_FORE_COLOR (gc));
990 if (bg_width)
991 {
992 Rect r;
993
994 SetRect (&r, x, y - FONT_BASE (GC_FONT (gc)),
995 x + bg_width, y + FONT_DESCENT (GC_FONT (gc)));
996 RGBBackColor (GC_BACK_COLOR (gc));
997 EraseRect (&r);
998 RGBBackColor (GC_BACK_COLOR (FRAME_NORMAL_GC (f)));
999 }
1000 MoveTo (x, y);
1001 ATSUDrawText (text_layout,
1002 kATSUFromTextBeginning, kATSUToTextEnd,
1003 kATSUUseGrafPortPenLoc, kATSUUseGrafPortPenLoc);
1004 if (overstrike_p)
1005 {
1006 MoveTo (x + 1, y);
1007 ATSUDrawText (text_layout,
1008 kATSUFromTextBeginning, kATSUToTextEnd,
1009 kATSUUseGrafPortPenLoc, kATSUUseGrafPortPenLoc);
1010 }
1011 mac_end_clip (gc);
1012 #ifdef MAC_OSX
1013 }
1014 else
1015 {
1016 static CGContextRef context;
1017 float port_height = FRAME_PIXEL_HEIGHT (f);
1018 static const ATSUAttributeTag tags[] = {kATSUCGContextTag};
1019 static const ByteCount sizes[] = {sizeof (CGContextRef)};
1020 static const ATSUAttributeValuePtr values[] = {&context};
950 1021
951 #if USE_CG_DRAWING 1022 #if USE_CG_DRAWING
952 mac_prepare_for_quickdraw (f); 1023 context = mac_begin_cg_clip (f, gc);
953 #endif 1024 #else
954 SetPortWindowPort (FRAME_MAC_WINDOW (f)); 1025 CGrafPtr port;
955 1026
956 SetRect (&r, x, y, x + width, y + height); 1027 GetPort (&port);
957 1028 QDBeginCGContext (port, &context);
958 InvertRect (&r); 1029 if (gc->n_clip_rects || bg_width)
959 } 1030 {
1031 CGContextTranslateCTM (context, 0, port_height);
1032 CGContextScaleCTM (context, 1, -1);
1033 if (gc->n_clip_rects)
1034 CGContextClipToRects (context, gc->clip_rects,
1035 gc->n_clip_rects);
1036 #endif
1037 if (bg_width)
1038 {
1039 CG_SET_FILL_COLOR_WITH_GC_BACKGROUND (context, gc);
1040 CGContextFillRect (context,
1041 CGRectMake (x, y - FONT_BASE (GC_FONT (gc)),
1042 bg_width,
1043 FONT_HEIGHT (GC_FONT (gc))));
1044 }
1045 CGContextScaleCTM (context, 1, -1);
1046 CGContextTranslateCTM (context, 0, -port_height);
1047 #if !USE_CG_DRAWING
1048 }
1049 #endif
1050 CG_SET_FILL_COLOR_WITH_GC_FOREGROUND (context, gc);
1051 err = ATSUSetLayoutControls (text_layout,
1052 sizeof (tags) / sizeof (tags[0]),
1053 tags, sizes, values);
1054 if (err == noErr)
1055 {
1056 ATSUDrawText (text_layout,
1057 kATSUFromTextBeginning, kATSUToTextEnd,
1058 Long2Fix (x), Long2Fix (port_height - y));
1059 if (overstrike_p)
1060 ATSUDrawText (text_layout,
1061 kATSUFromTextBeginning, kATSUToTextEnd,
1062 Long2Fix (x + 1), Long2Fix (port_height - y));
1063 }
1064 #if USE_CG_DRAWING
1065 mac_end_cg_clip (f);
1066 context = NULL;
1067 #else
1068 CGContextSynchronize (context);
1069 QDEndCGContext (port, &context);
1070 #endif
1071 #if 0
1072 /* This doesn't work on Mac OS X 10.1. */
1073 ATSUClearLayoutControls (text_layout,
1074 sizeof (tags) / sizeof (tags[0]), tags);
1075 #else
1076 ATSUSetLayoutControls (text_layout,
1077 sizeof (tags) / sizeof (tags[0]),
1078 tags, sizes, values);
1079 #endif
1080 }
1081 #endif /* MAC_OSX */
1082 }
1083 #endif /* USE_ATSUI */
960 1084
961 1085
962 static void 1086 static void
1087 mac_draw_image_string_qd (f, gc, x, y, buf, nchars, bg_width,
1088 overstrike_p, bytes_per_char)
1089 struct frame *f;
1090 GC gc;
1091 int x, y;
1092 char *buf;
1093 int nchars, bg_width, overstrike_p, bytes_per_char;
1094 {
1095 #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1020
1096 UInt32 savedFlags;
1097 #endif
1098
1099 mac_begin_clip (f, gc);
1100 #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1020
1101 if (mac_use_core_graphics)
1102 savedFlags = SwapQDTextFlags (kQDUseCGTextRendering);
1103 #endif
1104 RGBForeColor (GC_FORE_COLOR (gc));
1105 #ifdef MAC_OS8
1106 if (bg_width)
1107 {
1108 RGBBackColor (GC_BACK_COLOR (gc));
1109 TextMode (srcCopy);
1110 }
1111 else
1112 TextMode (srcOr);
1113 #else
1114 /* We prefer not to use srcCopy text transfer mode on Mac OS X
1115 because:
1116 - Screen is double-buffered. (In srcCopy mode, a text is drawn
1117 into an offscreen graphics world first. So performance gain
1118 cannot be expected.)
1119 - It lowers rendering quality.
1120 - Some fonts leave garbage on cursor movement. */
1121 if (bg_width)
1122 {
1123 Rect r;
1124
1125 RGBBackColor (GC_BACK_COLOR (gc));
1126 SetRect (&r, x, y - FONT_BASE (GC_FONT (gc)),
1127 x + bg_width, y + FONT_DESCENT (GC_FONT (gc)));
1128 EraseRect (&r);
1129 }
1130 TextMode (srcOr);
1131 #endif
1132 TextFont (GC_FONT (gc)->mac_fontnum);
1133 TextSize (GC_FONT (gc)->mac_fontsize);
1134 TextFace (GC_FONT (gc)->mac_fontface);
1135 MoveTo (x, y);
1136 DrawText (buf, 0, nchars * bytes_per_char);
1137 if (overstrike_p)
1138 {
1139 TextMode (srcOr);
1140 MoveTo (x + 1, y);
1141 DrawText (buf, 0, nchars * bytes_per_char);
1142 }
1143 if (bg_width)
1144 RGBBackColor (GC_BACK_COLOR (FRAME_NORMAL_GC (f)));
1145 mac_end_clip (gc);
1146
1147 #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1020
1148 if (mac_use_core_graphics)
1149 SwapQDTextFlags(savedFlags);
1150 #endif
1151 }
1152
1153
1154 static INLINE void
963 mac_draw_string_common (f, gc, x, y, buf, nchars, bg_width, 1155 mac_draw_string_common (f, gc, x, y, buf, nchars, bg_width,
964 overstrike_p, bytes_per_char) 1156 overstrike_p, bytes_per_char)
965 struct frame *f; 1157 struct frame *f;
966 GC gc; 1158 GC gc;
967 int x, y; 1159 int x, y;
968 char *buf; 1160 char *buf;
969 int nchars, bg_width, overstrike_p, bytes_per_char; 1161 int nchars, bg_width, overstrike_p, bytes_per_char;
970 { 1162 {
971 SetPortWindowPort (FRAME_MAC_WINDOW (f));
972
973 #if USE_ATSUI 1163 #if USE_ATSUI
974 if (GC_FONT (gc)->mac_style) 1164 if (GC_FONT (gc)->mac_style)
975 { 1165 mac_draw_image_string_atsui (f, gc, x, y, buf, nchars, bg_width,
976 OSStatus err; 1166 overstrike_p, bytes_per_char);
977 ATSUTextLayout text_layout;
978
979 xassert (bytes_per_char == 2);
980
981 #ifndef WORDS_BIG_ENDIAN
982 {
983 int i;
984 UniChar *text = (UniChar *)buf;
985
986 for (i = 0; i < nchars; i++)
987 text[i] = EndianU16_BtoN (text[i]);
988 }
989 #endif
990 err = atsu_get_text_layout_with_text_ptr ((ConstUniCharArrayPtr)buf,
991 nchars,
992 GC_FONT (gc)->mac_style,
993 &text_layout);
994 if (err != noErr)
995 return;
996 #ifdef MAC_OSX
997 if (!mac_use_core_graphics)
998 {
999 #endif
1000 #if USE_CG_DRAWING
1001 mac_prepare_for_quickdraw (f);
1002 #endif
1003 mac_begin_clip (gc);
1004 RGBForeColor (GC_FORE_COLOR (gc));
1005 if (bg_width)
1006 {
1007 Rect r;
1008
1009 SetRect (&r, x, y - FONT_BASE (GC_FONT (gc)),
1010 x + bg_width, y + FONT_DESCENT (GC_FONT (gc)));
1011 RGBBackColor (GC_BACK_COLOR (gc));
1012 EraseRect (&r);
1013 RGBBackColor (GC_BACK_COLOR (FRAME_NORMAL_GC (f)));
1014 }
1015 MoveTo (x, y);
1016 ATSUDrawText (text_layout,
1017 kATSUFromTextBeginning, kATSUToTextEnd,
1018 kATSUUseGrafPortPenLoc, kATSUUseGrafPortPenLoc);
1019 if (overstrike_p)
1020 {
1021 MoveTo (x + 1, y);
1022 ATSUDrawText (text_layout,
1023 kATSUFromTextBeginning, kATSUToTextEnd,
1024 kATSUUseGrafPortPenLoc, kATSUUseGrafPortPenLoc);
1025 }
1026 mac_end_clip (gc);
1027 #ifdef MAC_OSX
1028 }
1029 else
1030 {
1031 CGrafPtr port;
1032 static CGContextRef context;
1033 float port_height = FRAME_PIXEL_HEIGHT (f);
1034 static const ATSUAttributeTag tags[] = {kATSUCGContextTag};
1035 static const ByteCount sizes[] = {sizeof (CGContextRef)};
1036 static const ATSUAttributeValuePtr values[] = {&context};
1037
1038 #if USE_CG_DRAWING
1039 context = mac_begin_cg_clip (f, gc);
1040 #else
1041 GetPort (&port);
1042 QDBeginCGContext (port, &context);
1043 if (gc->n_clip_rects || bg_width)
1044 {
1045 CGContextTranslateCTM (context, 0, port_height);
1046 CGContextScaleCTM (context, 1, -1);
1047 if (gc->n_clip_rects)
1048 CGContextClipToRects (context, gc->clip_rects,
1049 gc->n_clip_rects);
1050 #endif
1051 if (bg_width)
1052 {
1053 CG_SET_FILL_COLOR_WITH_GC_BACKGROUND (context, gc);
1054 CGContextFillRect
1055 (context,
1056 CGRectMake (x, y - FONT_BASE (GC_FONT (gc)),
1057 bg_width, FONT_HEIGHT (GC_FONT (gc))));
1058 }
1059 CGContextScaleCTM (context, 1, -1);
1060 CGContextTranslateCTM (context, 0, -port_height);
1061 #if !USE_CG_DRAWING
1062 }
1063 #endif
1064 CG_SET_FILL_COLOR_WITH_GC_FOREGROUND (context, gc);
1065 err = ATSUSetLayoutControls (text_layout,
1066 sizeof (tags) / sizeof (tags[0]),
1067 tags, sizes, values);
1068 if (err == noErr)
1069 {
1070 ATSUDrawText (text_layout,
1071 kATSUFromTextBeginning, kATSUToTextEnd,
1072 Long2Fix (x), Long2Fix (port_height - y));
1073 if (overstrike_p)
1074 ATSUDrawText (text_layout,
1075 kATSUFromTextBeginning, kATSUToTextEnd,
1076 Long2Fix (x + 1), Long2Fix (port_height - y));
1077 }
1078 #if USE_CG_DRAWING
1079 mac_end_cg_clip (f);
1080 context = NULL;
1081 #else
1082 CGContextSynchronize (context);
1083 QDEndCGContext (port, &context);
1084 #endif
1085 #if 0
1086 /* This doesn't work on Mac OS X 10.1. */
1087 ATSUClearLayoutControls (text_layout,
1088 sizeof (tags) / sizeof (tags[0]), tags);
1089 #else
1090 ATSUSetLayoutControls (text_layout,
1091 sizeof (tags) / sizeof (tags[0]),
1092 tags, sizes, values);
1093 #endif
1094 }
1095 #endif /* MAC_OSX */
1096 }
1097 else 1167 else
1098 #endif /* USE_ATSUI */ 1168 #endif /* USE_ATSUI */
1099 { 1169 mac_draw_image_string_qd (f, gc, x, y, buf, nchars, bg_width,
1100 #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1020 1170 overstrike_p, bytes_per_char);
1101 UInt32 savedFlags;
1102
1103 if (mac_use_core_graphics)
1104 savedFlags = SwapQDTextFlags (kQDUseCGTextRendering);
1105 #endif
1106 #if USE_CG_DRAWING
1107 mac_prepare_for_quickdraw (f);
1108 #endif
1109 mac_begin_clip (gc);
1110 RGBForeColor (GC_FORE_COLOR (gc));
1111 #ifdef MAC_OS8
1112 if (bg_width)
1113 {
1114 RGBBackColor (GC_BACK_COLOR (gc));
1115 TextMode (srcCopy);
1116 }
1117 else
1118 TextMode (srcOr);
1119 #else
1120 /* We prefer not to use srcCopy text transfer mode on Mac OS X
1121 because:
1122 - Screen is double-buffered. (In srcCopy mode, a text is
1123 drawn into an offscreen graphics world first. So
1124 performance gain cannot be expected.)
1125 - It lowers rendering quality.
1126 - Some fonts leave garbage on cursor movement. */
1127 if (bg_width)
1128 {
1129 Rect r;
1130
1131 RGBBackColor (GC_BACK_COLOR (gc));
1132 SetRect (&r, x, y - FONT_BASE (GC_FONT (gc)),
1133 x + bg_width, y + FONT_DESCENT (GC_FONT (gc)));
1134 EraseRect (&r);
1135 }
1136 TextMode (srcOr);
1137 #endif
1138 TextFont (GC_FONT (gc)->mac_fontnum);
1139 TextSize (GC_FONT (gc)->mac_fontsize);
1140 TextFace (GC_FONT (gc)->mac_fontface);
1141 MoveTo (x, y);
1142 DrawText (buf, 0, nchars * bytes_per_char);
1143 if (overstrike_p)
1144 {
1145 TextMode (srcOr);
1146 MoveTo (x + 1, y);
1147 DrawText (buf, 0, nchars * bytes_per_char);
1148 }
1149 if (bg_width)
1150 RGBBackColor (GC_BACK_COLOR (FRAME_NORMAL_GC (f)));
1151 mac_end_clip (gc);
1152
1153 #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1020
1154 if (mac_use_core_graphics)
1155 SwapQDTextFlags(savedFlags);
1156 #endif
1157 }
1158 } 1171 }
1159 1172
1160 1173
1161 /* Mac replacement for XDrawImageString. */ 1174 /* Mac replacement for XDrawImageString. */
1162 1175
1369 GC gc; 1382 GC gc;
1370 int x, y; 1383 int x, y;
1371 XChar2b *buf; 1384 XChar2b *buf;
1372 int nchars, bg_width, overstrike_p; 1385 int nchars, bg_width, overstrike_p;
1373 { 1386 {
1374 CGrafPtr port;
1375 float port_height, gx, gy; 1387 float port_height, gx, gy;
1376 int i; 1388 int i;
1377 CGContextRef context; 1389 CGContextRef context;
1378 CGGlyph *glyphs; 1390 CGGlyph *glyphs;
1379 CGSize *advances; 1391 CGSize *advances;
1380 1392
1381 if (!mac_use_core_graphics || GC_FONT (gc)->cg_font == NULL) 1393 if (!mac_use_core_graphics || GC_FONT (gc)->cg_font == NULL)
1382 return 0; 1394 return 0;
1383 1395
1384 port = GetWindowPort (FRAME_MAC_WINDOW (f));
1385 port_height = FRAME_PIXEL_HEIGHT (f); 1396 port_height = FRAME_PIXEL_HEIGHT (f);
1386 gx = x; 1397 gx = x;
1387 gy = port_height - y; 1398 gy = port_height - y;
1388 glyphs = (CGGlyph *)buf; 1399 glyphs = (CGGlyph *)buf;
1389 advances = alloca (sizeof (CGSize) * nchars); 1400 advances = alloca (sizeof (CGSize) * nchars);
1400 } 1411 }
1401 1412
1402 #if USE_CG_DRAWING 1413 #if USE_CG_DRAWING
1403 context = mac_begin_cg_clip (f, gc); 1414 context = mac_begin_cg_clip (f, gc);
1404 #else 1415 #else
1405 QDBeginCGContext (port, &context); 1416 QDBeginCGContext (GetWindowPort (FRAME_MAC_WINDOW (f)), &context);
1406 if (gc->n_clip_rects || bg_width) 1417 if (gc->n_clip_rects || bg_width)
1407 { 1418 {
1408 CGContextTranslateCTM (context, 0, port_height); 1419 CGContextTranslateCTM (context, 0, port_height);
1409 CGContextScaleCTM (context, 1, -1); 1420 CGContextScaleCTM (context, 1, -1);
1410 if (gc->n_clip_rects) 1421 if (gc->n_clip_rects)
1440 CGContextSetTextPosition (context, gx + 1.0f, gy); 1451 CGContextSetTextPosition (context, gx + 1.0f, gy);
1441 CGContextShowGlyphsWithAdvances (context, glyphs, advances, nchars); 1452 CGContextShowGlyphsWithAdvances (context, glyphs, advances, nchars);
1442 } 1453 }
1443 } 1454 }
1444 #if MAC_OS_X_VERSION_MIN_REQUIRED == 1020 1455 #if MAC_OS_X_VERSION_MIN_REQUIRED == 1020
1445 else 1456 else /* CGContextShowGlyphsWithAdvances == NULL */
1446 #endif 1457 #endif
1447 #endif /* MAC_OS_X_VERSION_MAX_ALLOWED >= 1030 */ 1458 #endif /* MAC_OS_X_VERSION_MAX_ALLOWED >= 1030 */
1448 #if MAC_OS_X_VERSION_MAX_ALLOWED < 1030 || MAC_OS_X_VERSION_MIN_REQUIRED == 1020 1459 #if MAC_OS_X_VERSION_MAX_ALLOWED < 1030 || MAC_OS_X_VERSION_MIN_REQUIRED == 1020
1449 { 1460 {
1450 for (i = 0; i < nchars; i++) 1461 for (i = 0; i < nchars; i++)
1458 #endif 1469 #endif
1459 #if USE_CG_DRAWING 1470 #if USE_CG_DRAWING
1460 mac_end_cg_clip (f); 1471 mac_end_cg_clip (f);
1461 #else 1472 #else
1462 CGContextSynchronize (context); 1473 CGContextSynchronize (context);
1463 QDEndCGContext (port, &context); 1474 QDEndCGContext (GetWindowPort (FRAME_MAC_WINDOW (f)), &context);
1464 #endif 1475 #endif
1465 1476
1466 return 1; 1477 return 1;
1467 } 1478 }
1468 #endif 1479 #endif
1480 unsigned int width, height; 1491 unsigned int width, height;
1481 int dest_x, dest_y; 1492 int dest_x, dest_y;
1482 { 1493 {
1483 Rect src_r, dest_r; 1494 Rect src_r, dest_r;
1484 1495
1485 SetPortWindowPort (FRAME_MAC_WINDOW (f)); 1496 mac_begin_clip (f, gc);
1486 1497
1487 SetRect (&src_r, src_x, src_y, src_x + width, src_y + height); 1498 SetRect (&src_r, src_x, src_y, src_x + width, src_y + height);
1488 SetRect (&dest_r, dest_x, dest_y, dest_x + width, dest_y + height); 1499 SetRect (&dest_r, dest_x, dest_y, dest_x + width, dest_y + height);
1489 1500
1490 ForeColor (blackColor); 1501 ForeColor (blackColor);
1491 BackColor (whiteColor); 1502 BackColor (whiteColor);
1492 1503
1493 mac_begin_clip (gc);
1494 LockPixels (GetGWorldPixMap (src)); 1504 LockPixels (GetGWorldPixMap (src));
1495 #if TARGET_API_MAC_CARBON 1505 #if TARGET_API_MAC_CARBON
1496 { 1506 {
1497 CGrafPtr port; 1507 CGrafPtr port;
1498 1508
1506 #else /* not TARGET_API_MAC_CARBON */ 1516 #else /* not TARGET_API_MAC_CARBON */
1507 CopyBits (&(((GrafPtr)src)->portBits), &(FRAME_MAC_WINDOW (f)->portBits), 1517 CopyBits (&(((GrafPtr)src)->portBits), &(FRAME_MAC_WINDOW (f)->portBits),
1508 &src_r, &dest_r, srcCopy, 0); 1518 &src_r, &dest_r, srcCopy, 0);
1509 #endif /* not TARGET_API_MAC_CARBON */ 1519 #endif /* not TARGET_API_MAC_CARBON */
1510 UnlockPixels (GetGWorldPixMap (src)); 1520 UnlockPixels (GetGWorldPixMap (src));
1521
1522 RGBBackColor (GC_BACK_COLOR (FRAME_NORMAL_GC (f)));
1523
1511 mac_end_clip (gc); 1524 mac_end_clip (gc);
1512
1513 RGBBackColor (GC_BACK_COLOR (FRAME_NORMAL_GC (f)));
1514 } 1525 }
1515 1526
1516 1527
1517 static void 1528 static void
1518 mac_copy_area_with_mask (src, mask, f, gc, src_x, src_y, 1529 mac_copy_area_with_mask (src, mask, f, gc, src_x, src_y,
1524 unsigned int width, height; 1535 unsigned int width, height;
1525 int dest_x, dest_y; 1536 int dest_x, dest_y;
1526 { 1537 {
1527 Rect src_r, dest_r; 1538 Rect src_r, dest_r;
1528 1539
1529 SetPortWindowPort (FRAME_MAC_WINDOW (f)); 1540 mac_begin_clip (f, gc);
1530 1541
1531 SetRect (&src_r, src_x, src_y, src_x + width, src_y + height); 1542 SetRect (&src_r, src_x, src_y, src_x + width, src_y + height);
1532 SetRect (&dest_r, dest_x, dest_y, dest_x + width, dest_y + height); 1543 SetRect (&dest_r, dest_x, dest_y, dest_x + width, dest_y + height);
1533 1544
1534 ForeColor (blackColor); 1545 ForeColor (blackColor);
1535 BackColor (whiteColor); 1546 BackColor (whiteColor);
1536 1547
1537 mac_begin_clip (gc);
1538 LockPixels (GetGWorldPixMap (src)); 1548 LockPixels (GetGWorldPixMap (src));
1539 LockPixels (GetGWorldPixMap (mask)); 1549 LockPixels (GetGWorldPixMap (mask));
1540 #if TARGET_API_MAC_CARBON 1550 #if TARGET_API_MAC_CARBON
1541 { 1551 {
1542 CGrafPtr port; 1552 CGrafPtr port;
1552 CopyMask (&(((GrafPtr)src)->portBits), &(((GrafPtr)mask)->portBits), 1562 CopyMask (&(((GrafPtr)src)->portBits), &(((GrafPtr)mask)->portBits),
1553 &(FRAME_MAC_WINDOW (f)->portBits), &src_r, &src_r, &dest_r); 1563 &(FRAME_MAC_WINDOW (f)->portBits), &src_r, &src_r, &dest_r);
1554 #endif /* not TARGET_API_MAC_CARBON */ 1564 #endif /* not TARGET_API_MAC_CARBON */
1555 UnlockPixels (GetGWorldPixMap (mask)); 1565 UnlockPixels (GetGWorldPixMap (mask));
1556 UnlockPixels (GetGWorldPixMap (src)); 1566 UnlockPixels (GetGWorldPixMap (src));
1567
1568 RGBBackColor (GC_BACK_COLOR (FRAME_NORMAL_GC (f)));
1569
1557 mac_end_clip (gc); 1570 mac_end_clip (gc);
1558
1559 RGBBackColor (GC_BACK_COLOR (FRAME_NORMAL_GC (f)));
1560 } 1571 }
1561 #endif /* !USE_CG_DRAWING */ 1572 #endif /* !USE_CG_DRAWING */
1562 1573
1563 1574
1564 /* Mac replacement for XCopyArea: used only for scrolling. */ 1575 /* Mac replacement for XCopyArea: used only for scrolling. */
1585 DisposeRgn (dummy); 1596 DisposeRgn (dummy);
1586 #else /* not TARGET_API_MAC_CARBON */ 1597 #else /* not TARGET_API_MAC_CARBON */
1587 Rect src_r, dest_r; 1598 Rect src_r, dest_r;
1588 WindowRef w = FRAME_MAC_WINDOW (f); 1599 WindowRef w = FRAME_MAC_WINDOW (f);
1589 1600
1590 SetPort (w); 1601 mac_begin_clip (f, gc);
1591 1602
1592 SetRect (&src_r, src_x, src_y, src_x + width, src_y + height); 1603 SetRect (&src_r, src_x, src_y, src_x + width, src_y + height);
1593 SetRect (&dest_r, dest_x, dest_y, dest_x + width, dest_y + height); 1604 SetRect (&dest_r, dest_x, dest_y, dest_x + width, dest_y + height);
1594 1605
1595 /* In Color QuickDraw, set ForeColor and BackColor as follows to avoid 1606 /* In Color QuickDraw, set ForeColor and BackColor as follows to avoid
1596 color mapping in CopyBits. Otherwise, it will be slow. */ 1607 color mapping in CopyBits. Otherwise, it will be slow. */
1597 ForeColor (blackColor); 1608 ForeColor (blackColor);
1598 BackColor (whiteColor); 1609 BackColor (whiteColor);
1599 mac_begin_clip (gc);
1600 CopyBits (&(w->portBits), &(w->portBits), &src_r, &dest_r, srcCopy, 0); 1610 CopyBits (&(w->portBits), &(w->portBits), &src_r, &dest_r, srcCopy, 0);
1611
1612 RGBBackColor (GC_BACK_COLOR (FRAME_NORMAL_GC (f)));
1613
1601 mac_end_clip (gc); 1614 mac_end_clip (gc);
1602
1603 RGBBackColor (GC_BACK_COLOR (FRAME_NORMAL_GC (f)));
1604 #endif /* not TARGET_API_MAC_CARBON */ 1615 #endif /* not TARGET_API_MAC_CARBON */
1605 } 1616 }
1606 1617
1607 1618
1608 /* Mac replacement for XChangeGC. */ 1619 /* Mac replacement for XChangeGC. */
4606 size of the smallest character that could be displayed 4617 size of the smallest character that could be displayed
4607 on it, i.e. into the same rectangles that matrices on 4618 on it, i.e. into the same rectangles that matrices on
4608 the frame are divided into. */ 4619 the frame are divided into. */
4609 Point mouse_pos; 4620 Point mouse_pos;
4610 4621
4622 #if TARGET_API_MAC_CARBON
4623 GetGlobalMouse (&mouse_pos);
4624 mouse_pos.h -= f1->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f1);
4625 mouse_pos.v -= f1->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f1);
4626 #else
4611 SetPortWindowPort (FRAME_MAC_WINDOW (f1)); 4627 SetPortWindowPort (FRAME_MAC_WINDOW (f1));
4612 GetMouse (&mouse_pos); 4628 GetMouse (&mouse_pos);
4629 #endif
4613 remember_mouse_glyph (f1, mouse_pos.h, mouse_pos.v, 4630 remember_mouse_glyph (f1, mouse_pos.h, mouse_pos.v,
4614 &last_mouse_glyph); 4631 &last_mouse_glyph);
4615 last_mouse_glyph_frame = f1; 4632 last_mouse_glyph_frame = f1;
4616 4633
4617 *bar_window = Qnil; 4634 *bar_window = Qnil;
5509 #endif 5526 #endif
5510 Point mouse_pos; 5527 Point mouse_pos;
5511 struct frame *f = mac_window_to_frame (wp); 5528 struct frame *f = mac_window_to_frame (wp);
5512 int win_y, top_range; 5529 int win_y, top_range;
5513 5530
5531 #if TARGET_API_MAC_CARBON
5532 GetGlobalMouse (&mouse_pos);
5533 mouse_pos.h -= f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f);
5534 mouse_pos.v -= f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f);
5535 #else
5514 SetPortWindowPort (wp); 5536 SetPortWindowPort (wp);
5515
5516 GetMouse (&mouse_pos); 5537 GetMouse (&mouse_pos);
5538 #endif
5517 5539
5518 win_y = mouse_pos.v - XINT (bar->top); 5540 win_y = mouse_pos.v - XINT (bar->top);
5519 top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height)); 5541 top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
5520 5542
5521 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER; 5543 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
6262 x_set_mouse_pixel_position (f, pix_x, pix_y) 6284 x_set_mouse_pixel_position (f, pix_x, pix_y)
6263 struct frame *f; 6285 struct frame *f;
6264 int pix_x, pix_y; 6286 int pix_x, pix_y;
6265 { 6287 {
6266 #ifdef MAC_OSX 6288 #ifdef MAC_OSX
6267 Point p; 6289 pix_x += f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f);
6268 CGPoint point; 6290 pix_y += f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f);
6269 6291
6270 BLOCK_INPUT; 6292 BLOCK_INPUT;
6271 SetPortWindowPort (FRAME_MAC_WINDOW (f)); 6293 CGWarpMouseCursorPosition (CGPointMake (pix_x, pix_y));
6272 p.h = pix_x;
6273 p.v = pix_y;
6274 LocalToGlobal (&p);
6275 point.x = p.h;
6276 point.y = p.v;
6277 CGWarpMouseCursorPosition (point);
6278 UNBLOCK_INPUT; 6294 UNBLOCK_INPUT;
6279 #else 6295 #else
6280 #if 0 /* MAC_TODO: LMSetMouseLocation and CursorDeviceMoveTo are non-Carbon */ 6296 #if 0 /* MAC_TODO: LMSetMouseLocation and CursorDeviceMoveTo are non-Carbon */
6281 BLOCK_INPUT; 6297 BLOCK_INPUT;
6282 6298
6422 call x_set_offset a second time 6438 call x_set_offset a second time
6423 if we get to x_make_frame_visible a second time 6439 if we get to x_make_frame_visible a second time
6424 before the window gets really visible. */ 6440 before the window gets really visible. */
6425 if (! FRAME_ICONIFIED_P (f) 6441 if (! FRAME_ICONIFIED_P (f)
6426 && ! f->output_data.mac->asked_for_visible) 6442 && ! f->output_data.mac->asked_for_visible)
6427 { 6443 x_set_offset (f, f->left_pos, f->top_pos, 0);
6428 #if TARGET_API_MAC_CARBON
6429 if (!(FRAME_SIZE_HINTS (f)->flags & (USPosition | PPosition)))
6430 {
6431 struct frame *sf = SELECTED_FRAME ();
6432 if (!FRAME_MAC_P (sf))
6433 RepositionWindow (FRAME_MAC_WINDOW (f), NULL,
6434 kWindowCenterOnMainScreen);
6435 else
6436 RepositionWindow (FRAME_MAC_WINDOW (f),
6437 FRAME_MAC_WINDOW (sf),
6438 #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1020
6439 kWindowCascadeStartAtParentWindowScreen
6440 #else
6441 kWindowCascadeOnParentWindowScreen
6442 #endif
6443 );
6444 if (!NILP (tip_frame) && f == XFRAME (tip_frame))
6445 mac_handle_origin_change (f);
6446 }
6447 else
6448 #endif
6449 x_set_offset (f, f->left_pos, f->top_pos, 0);
6450 }
6451 6444
6452 f->output_data.mac->asked_for_visible = 1; 6445 f->output_data.mac->asked_for_visible = 1;
6453 6446
6454 CollapseWindow (FRAME_MAC_WINDOW (f), false); 6447 CollapseWindow (FRAME_MAC_WINDOW (f), false);
6455 ShowWindow (FRAME_MAC_WINDOW (f)); 6448 ShowWindow (FRAME_MAC_WINDOW (f));
6519 FRAME_MAC_DISPLAY_INFO (f)->x_highlight_frame = 0; 6512 FRAME_MAC_DISPLAY_INFO (f)->x_highlight_frame = 0;
6520 #endif 6513 #endif
6521 6514
6522 BLOCK_INPUT; 6515 BLOCK_INPUT;
6523 6516
6517 #if !TARGET_API_MAC_CARBON
6524 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim 6518 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
6525 that the current position of the window is user-specified, rather than 6519 that the current position of the window is user-specified, rather than
6526 program-specified, so that when the window is mapped again, it will be 6520 program-specified, so that when the window is mapped again, it will be
6527 placed at the same location, without forcing the user to position it 6521 placed at the same location, without forcing the user to position it
6528 by hand again (they have already done that once for this window.) */ 6522 by hand again (they have already done that once for this window.) */
6529 x_wm_set_size_hint (f, (long) 0, 1); 6523 x_wm_set_size_hint (f, (long) 0, 1);
6524 #endif
6530 6525
6531 HideWindow (FRAME_MAC_WINDOW (f)); 6526 HideWindow (FRAME_MAC_WINDOW (f));
6532 6527
6533 UNBLOCK_INPUT; 6528 UNBLOCK_INPUT;
6534 6529
8717 panel functions (FPIsFontPanelVisible, SetFontInfoForSelection) is 8712 panel functions (FPIsFontPanelVisible, SetFontInfoForSelection) is
8718 slow. This variable is used for deferring such a call as much as 8713 slow. This variable is used for deferring such a call as much as
8719 possible. */ 8714 possible. */
8720 static int font_panel_shown_p = 0; 8715 static int font_panel_shown_p = 0;
8721 8716
8717 extern Lisp_Object Qfont;
8718 static Lisp_Object Qpanel_closed, Qselection;
8719
8720 static OSStatus mac_store_event_ref_as_apple_event P_ ((AEEventClass, AEEventID,
8721 Lisp_Object,
8722 Lisp_Object,
8723 EventRef, UInt32,
8724 const EventParamName *,
8725 const EventParamType *));
8726
8722 int 8727 int
8723 mac_font_panel_visible_p () 8728 mac_font_panel_visible_p ()
8724 { 8729 {
8725 return font_panel_shown_p && FPIsFontPanelVisible (); 8730 return font_panel_shown_p && FPIsFontPanelVisible ();
8726 } 8731 }
8727 8732
8733 static pascal OSStatus
8734 mac_handle_font_event (next_handler, event, data)
8735 EventHandlerCallRef next_handler;
8736 EventRef event;
8737 void *data;
8738 {
8739 OSStatus result, err;
8740 Lisp_Object id_key;
8741 int num_params;
8742 const EventParamName *names;
8743 const EventParamType *types;
8744 static const EventParamName names_sel[] = {kEventParamATSUFontID,
8745 kEventParamATSUFontSize,
8746 kEventParamFMFontFamily,
8747 kEventParamFMFontSize,
8748 kEventParamFontColor};
8749 static const EventParamType types_sel[] = {typeATSUFontID,
8750 typeATSUSize,
8751 typeFMFontFamily,
8752 typeFMFontSize,
8753 typeFontColor};
8754
8755 result = CallNextEventHandler (next_handler, event);
8756 if (result != eventNotHandledErr)
8757 return result;
8758
8759 switch (GetEventKind (event))
8760 {
8761 case kEventFontPanelClosed:
8762 id_key = Qpanel_closed;
8763 num_params = 0;
8764 names = NULL;
8765 types = NULL;
8766 break;
8767
8768 case kEventFontSelection:
8769 id_key = Qselection;
8770 num_params = sizeof (names_sel) / sizeof (names_sel[0]);
8771 names = names_sel;
8772 types = types_sel;
8773 break;
8774 }
8775
8776 err = mac_store_event_ref_as_apple_event (0, 0, Qfont, id_key,
8777 event, num_params,
8778 names, types);
8779 if (err == noErr)
8780 result = noErr;
8781
8782 return result;
8783 }
8784
8728 OSStatus 8785 OSStatus
8729 mac_show_hide_font_panel () 8786 mac_show_hide_font_panel ()
8730 { 8787 {
8731 font_panel_shown_p = 1; 8788 if (!font_panel_shown_p)
8789 {
8790 OSStatus err;
8791
8792 static const EventTypeSpec specs[] =
8793 {{kEventClassFont, kEventFontPanelClosed},
8794 {kEventClassFont, kEventFontSelection}};
8795
8796 err = InstallApplicationEventHandler (mac_handle_font_event,
8797 GetEventTypeCount (specs),
8798 specs, NULL, NULL);
8799 if (err != noErr)
8800 return err;
8801
8802 font_panel_shown_p = 1;
8803 }
8732 8804
8733 return FPShowHideFontPanel (); 8805 return FPShowHideFontPanel ();
8734 } 8806 }
8735 8807
8736 OSStatus 8808 OSStatus
8873 static Lisp_Object Qhi_command; 8945 static Lisp_Object Qhi_command;
8874 #ifdef MAC_OSX 8946 #ifdef MAC_OSX
8875 extern Lisp_Object Qwindow; 8947 extern Lisp_Object Qwindow;
8876 static Lisp_Object Qtoolbar_switch_mode; 8948 static Lisp_Object Qtoolbar_switch_mode;
8877 #endif 8949 #endif
8878 #if USE_MAC_FONT_PANEL
8879 extern Lisp_Object Qfont;
8880 static Lisp_Object Qpanel_closed, Qselection;
8881 #endif
8882 #if USE_MAC_TSM 8950 #if USE_MAC_TSM
8883 static TSMDocumentID tsm_document_id; 8951 static TSMDocumentID tsm_document_id;
8884 static Lisp_Object Qtext_input; 8952 static Lisp_Object Qtext_input;
8885 static Lisp_Object Qupdate_active_input_area, Qunicode_for_key_event; 8953 static Lisp_Object Qupdate_active_input_area, Qunicode_for_key_event;
8886 static Lisp_Object Vmac_ts_active_input_overlay; 8954 static Lisp_Object Vmac_ts_active_input_overlay;
8888 static Lisp_Object Vmac_ts_script_language_on_focus; 8956 static Lisp_Object Vmac_ts_script_language_on_focus;
8889 static Lisp_Object saved_ts_script_language_on_focus; 8957 static Lisp_Object saved_ts_script_language_on_focus;
8890 static ScriptLanguageRecord saved_ts_language; 8958 static ScriptLanguageRecord saved_ts_language;
8891 static Component saved_ts_component; 8959 static Component saved_ts_component;
8892 #endif 8960 #endif
8893 #endif 8961 #endif /* TARGET_API_MAC_CARBON */
8894 extern int mac_ready_for_apple_events; 8962 extern int mac_ready_for_apple_events;
8895 extern Lisp_Object Qundefined; 8963 extern Lisp_Object Qundefined;
8896 extern void init_apple_event_handler P_ ((void)); 8964 extern void init_apple_event_handler P_ ((void));
8897 extern void mac_find_apple_event_spec P_ ((AEEventClass, AEEventID, 8965 extern void mac_find_apple_event_spec P_ ((AEEventClass, AEEventID,
8898 Lisp_Object *, Lisp_Object *, 8966 Lisp_Object *, Lisp_Object *,
8901 8969
8902 /* Drag and Drop */ 8970 /* Drag and Drop */
8903 extern OSErr install_drag_handler P_ ((WindowRef)); 8971 extern OSErr install_drag_handler P_ ((WindowRef));
8904 extern void remove_drag_handler P_ ((WindowRef)); 8972 extern void remove_drag_handler P_ ((WindowRef));
8905 8973
8974 #if TARGET_API_MAC_CARBON
8906 /* Showing help echo string during menu tracking */ 8975 /* Showing help echo string during menu tracking */
8907 extern OSStatus install_menu_target_item_handler P_ ((WindowRef)); 8976 extern OSStatus install_menu_target_item_handler P_ ((void));
8908 8977
8909 #if TARGET_API_MAC_CARBON
8910 #ifdef MAC_OSX 8978 #ifdef MAC_OSX
8911 extern void init_service_handler (); 8979 extern OSStatus install_service_handler ();
8912 static Lisp_Object Qservice, Qpaste, Qperform; 8980 static Lisp_Object Qservice, Qpaste, Qperform;
8913 #endif 8981 #endif
8914 8982 #endif
8915 /* Window Event Handler */
8916 static pascal OSStatus mac_handle_window_event (EventHandlerCallRef,
8917 EventRef, void *);
8918 #endif
8919 OSStatus install_window_handler (WindowRef);
8920 8983
8921 extern void init_emacs_passwd_dir (); 8984 extern void init_emacs_passwd_dir ();
8922 extern int emacs_main (int, char **, char **); 8985 extern int emacs_main (int, char **, char **);
8923 8986
8924 extern void initialize_applescript(); 8987 extern void initialize_applescript();
9113 /* Obtains the event modifiers from the event ref and then calls 9176 /* Obtains the event modifiers from the event ref and then calls
9114 mac_to_emacs_modifiers. */ 9177 mac_to_emacs_modifiers. */
9115 static int 9178 static int
9116 mac_event_to_emacs_modifiers (EventRef eventRef) 9179 mac_event_to_emacs_modifiers (EventRef eventRef)
9117 { 9180 {
9118 UInt32 mods = 0; 9181 UInt32 mods = 0, class;
9182
9119 GetEventParameter (eventRef, kEventParamKeyModifiers, typeUInt32, NULL, 9183 GetEventParameter (eventRef, kEventParamKeyModifiers, typeUInt32, NULL,
9120 sizeof (UInt32), NULL, &mods); 9184 sizeof (UInt32), NULL, &mods);
9185 class = GetEventClass (eventRef);
9121 if (!NILP (Vmac_emulate_three_button_mouse) && 9186 if (!NILP (Vmac_emulate_three_button_mouse) &&
9122 GetEventClass(eventRef) == kEventClassMouse) 9187 (class == kEventClassMouse || class == kEventClassCommand))
9123 { 9188 {
9124 mods &= ~(optionKey | cmdKey); 9189 mods &= ~(optionKey | cmdKey);
9125 } 9190 }
9126 return mac_to_emacs_modifiers (mods); 9191 return mac_to_emacs_modifiers (mods);
9127 } 9192 }
9637 mac_handle_size_change (f, width, height); 9702 mac_handle_size_change (f, width, height);
9638 mac_handle_origin_change (f); 9703 mac_handle_origin_change (f);
9639 #endif 9704 #endif
9640 } 9705 }
9641 9706
9707 static void
9708 mac_set_unicode_keystroke_event (code, buf)
9709 UniChar code;
9710 struct input_event *buf;
9711 {
9712 int charset_id, c1, c2;
9713
9714 if (code < 0x80)
9715 {
9716 buf->kind = ASCII_KEYSTROKE_EVENT;
9717 buf->code = code;
9718 }
9719 else if (code < 0x100)
9720 {
9721 if (code < 0xA0)
9722 charset_id = CHARSET_8_BIT_CONTROL;
9723 else
9724 charset_id = charset_latin_iso8859_1;
9725 buf->kind = MULTIBYTE_CHAR_KEYSTROKE_EVENT;
9726 buf->code = MAKE_CHAR (charset_id, code, 0);
9727 }
9728 else
9729 {
9730 if (code < 0x2500)
9731 charset_id = charset_mule_unicode_0100_24ff,
9732 code -= 0x100;
9733 else if (code < 0x33FF)
9734 charset_id = charset_mule_unicode_2500_33ff,
9735 code -= 0x2500;
9736 else if (code >= 0xE000)
9737 charset_id = charset_mule_unicode_e000_ffff,
9738 code -= 0xE000;
9739 c1 = (code / 96) + 32, c2 = (code % 96) + 32;
9740 buf->kind = MULTIBYTE_CHAR_KEYSTROKE_EVENT;
9741 buf->code = MAKE_CHAR (charset_id, c1, c2);
9742 }
9743 }
9744
9745 static void
9746 do_keystroke (action, char_code, key_code, modifiers, timestamp, buf)
9747 EventKind action;
9748 unsigned char char_code;
9749 UInt32 key_code, modifiers;
9750 unsigned long timestamp;
9751 struct input_event *buf;
9752 {
9753 static SInt16 last_key_script = -1;
9754 SInt16 current_key_script = GetScriptManagerVariable (smKeyScript);
9755 UInt32 mapped_modifiers = mac_mapped_modifiers (modifiers);
9756
9757 #ifdef MAC_OSX
9758 if (mapped_modifiers & kEventKeyModifierFnMask
9759 && key_code <= 0x7f
9760 && fn_keycode_to_keycode_table[key_code])
9761 key_code = fn_keycode_to_keycode_table[key_code];
9762 #endif
9763
9764 if (key_code <= 0x7f && keycode_to_xkeysym_table[key_code])
9765 {
9766 buf->kind = NON_ASCII_KEYSTROKE_EVENT;
9767 buf->code = 0xff00 | keycode_to_xkeysym_table[key_code];
9768 #ifdef MAC_OSX
9769 if (modifiers & kEventKeyModifierFnMask
9770 && key_code <= 0x7f
9771 && fn_keycode_to_keycode_table[key_code] == key_code)
9772 modifiers &= ~kEventKeyModifierFnMask;
9773 #endif
9774 }
9775 else if (mapped_modifiers)
9776 {
9777 /* translate the keycode back to determine the original key */
9778 #ifdef MAC_OSX
9779 UCKeyboardLayout *uchr_ptr = NULL;
9780 #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1020
9781 OSStatus err;
9782 KeyboardLayoutRef layout;
9783
9784 err = KLGetCurrentKeyboardLayout (&layout);
9785 if (err == noErr)
9786 err = KLGetKeyboardLayoutProperty (layout, kKLuchrData,
9787 (const void **) &uchr_ptr);
9788 #else
9789 static SInt16 last_key_layout_id = 0;
9790 static Handle uchr_handle = (Handle)-1;
9791 SInt16 current_key_layout_id =
9792 GetScriptVariable (current_key_script, smScriptKeys);
9793
9794 if (uchr_handle == (Handle)-1
9795 || last_key_layout_id != current_key_layout_id)
9796 {
9797 uchr_handle = GetResource ('uchr', current_key_layout_id);
9798 last_key_layout_id = current_key_layout_id;
9799 }
9800 if (uchr_handle)
9801 uchr_ptr = (UCKeyboardLayout *)*uchr_handle;
9802 #endif
9803
9804 if (uchr_ptr)
9805 {
9806 OSStatus status;
9807 UInt16 key_action = action - keyDown;
9808 UInt32 modifier_key_state = (modifiers & ~mapped_modifiers) >> 8;
9809 UInt32 keyboard_type = LMGetKbdType ();
9810 SInt32 dead_key_state = 0;
9811 UniChar code;
9812 UniCharCount actual_length;
9813
9814 status = UCKeyTranslate (uchr_ptr, key_code, key_action,
9815 modifier_key_state, keyboard_type,
9816 kUCKeyTranslateNoDeadKeysMask,
9817 &dead_key_state,
9818 1, &actual_length, &code);
9819 if (status == noErr && actual_length == 1)
9820 mac_set_unicode_keystroke_event (code, buf);
9821 }
9822 #endif /* MAC_OSX */
9823
9824 if (buf->kind == NO_EVENT)
9825 {
9826 /* This code comes from Keyboard Resource, Appendix C of IM
9827 - Text. This is necessary since shift is ignored in KCHR
9828 table translation when option or command is pressed. It
9829 also does not translate correctly control-shift chars
9830 like C-% so mask off shift here also. */
9831 /* Mask off modifier keys that are mapped to some Emacs
9832 modifiers. */
9833 int new_modifiers = modifiers & ~mapped_modifiers;
9834 /* set high byte of keycode to modifier high byte*/
9835 int new_key_code = key_code | new_modifiers;
9836 Ptr kchr_ptr = (Ptr) GetScriptManagerVariable (smKCHRCache);
9837 unsigned long some_state = 0;
9838 UInt32 new_char_code;
9839
9840 new_char_code = KeyTranslate (kchr_ptr, new_key_code, &some_state);
9841 if (new_char_code == 0)
9842 /* Seems like a dead key. Append up-stroke. */
9843 new_char_code = KeyTranslate (kchr_ptr, new_key_code | 0x80,
9844 &some_state);
9845 if (new_char_code)
9846 {
9847 buf->kind = ASCII_KEYSTROKE_EVENT;
9848 buf->code = new_char_code & 0xff;
9849 }
9850 }
9851 }
9852
9853 if (buf->kind == NO_EVENT)
9854 {
9855 buf->kind = ASCII_KEYSTROKE_EVENT;
9856 buf->code = char_code;
9857 }
9858
9859 buf->modifiers = mac_to_emacs_modifiers (modifiers);
9860 buf->modifiers |= (extra_keyboard_modifiers
9861 & (meta_modifier | alt_modifier
9862 | hyper_modifier | super_modifier));
9863
9864 #if TARGET_API_MAC_CARBON
9865 if (buf->kind == ASCII_KEYSTROKE_EVENT
9866 && buf->code >= 0x80 && buf->modifiers)
9867 {
9868 OSStatus err;
9869 TextEncoding encoding = kTextEncodingMacRoman;
9870 TextToUnicodeInfo ttu_info;
9871
9872 UpgradeScriptInfoToTextEncoding (current_key_script,
9873 kTextLanguageDontCare,
9874 kTextRegionDontCare,
9875 NULL, &encoding);
9876 err = CreateTextToUnicodeInfoByEncoding (encoding, &ttu_info);
9877 if (err == noErr)
9878 {
9879 UniChar code;
9880 Str255 pstr;
9881 ByteCount unicode_len;
9882
9883 pstr[0] = 1;
9884 pstr[1] = buf->code;
9885 err = ConvertFromPStringToUnicode (ttu_info, pstr,
9886 sizeof (UniChar),
9887 &unicode_len, &code);
9888 if (err == noErr && unicode_len == sizeof (UniChar))
9889 mac_set_unicode_keystroke_event (code, buf);
9890 DisposeTextToUnicodeInfo (&ttu_info);
9891 }
9892 }
9893 #endif
9894
9895 if (buf->kind == ASCII_KEYSTROKE_EVENT
9896 && buf->code >= 0x80
9897 && last_key_script != current_key_script)
9898 {
9899 struct input_event event;
9900
9901 EVENT_INIT (event);
9902 event.kind = LANGUAGE_CHANGE_EVENT;
9903 event.arg = Qnil;
9904 event.code = current_key_script;
9905 event.timestamp = timestamp;
9906 kbd_buffer_store_event (&event);
9907 last_key_script = current_key_script;
9908 }
9909 }
9910
9642 void 9911 void
9643 mac_store_apple_event (class, id, desc) 9912 mac_store_apple_event (class, id, desc)
9644 Lisp_Object class, id; 9913 Lisp_Object class, id;
9645 const AEDesc *desc; 9914 const AEDesc *desc;
9646 { 9915 {
9716 XSETFRAME (buf.frame_or_window, mac_window_to_frame (window)); 9985 XSETFRAME (buf.frame_or_window, mac_window_to_frame (window));
9717 buf.arg = mac_aedesc_to_lisp (desc); 9986 buf.arg = mac_aedesc_to_lisp (desc);
9718 kbd_buffer_store_event (&buf); 9987 kbd_buffer_store_event (&buf);
9719 } 9988 }
9720 9989
9721 static pascal OSStatus 9990 #ifdef MAC_OSX
9722 mac_handle_command_event (next_handler, event, data) 9991 OSStatus
9723 EventHandlerCallRef next_handler; 9992 mac_store_service_event (event)
9724 EventRef event; 9993 EventRef event;
9725 void *data; 9994 {
9726 { 9995 OSStatus err;
9727 OSStatus result, err; 9996 Lisp_Object id_key;
9728 HICommand command; 9997 int num_params;
9729 static const EventParamName names[] = 9998 const EventParamName *names;
9730 {kEventParamDirectObject, kEventParamKeyModifiers}; 9999 const EventParamType *types;
9731 static const EventParamType types[] = 10000 static const EventParamName names_pfm[] =
9732 {typeHICommand, typeUInt32}; 10001 {kEventParamServiceMessageName, kEventParamServiceUserData};
9733 int num_params = sizeof (names) / sizeof (names[0]); 10002 static const EventParamType types_pfm[] =
9734 10003 {typeCFStringRef, typeCFStringRef};
9735 result = CallNextEventHandler (next_handler, event); 10004
9736 if (result != eventNotHandledErr) 10005 switch (GetEventKind (event))
9737 return result; 10006 {
9738 10007 case kEventServicePaste:
9739 err = GetEventParameter (event, kEventParamDirectObject, typeHICommand, 10008 id_key = Qpaste;
9740 NULL, sizeof (HICommand), NULL, &command); 10009 num_params = 0;
9741 10010 names = NULL;
9742 if (err != noErr || command.commandID == 0) 10011 types = NULL;
9743 return eventNotHandledErr; 10012 break;
9744 10013
9745 /* A HI command event is mapped to an Apple event whose event class 10014 case kEventServicePerform:
9746 symbol is `hi-command' and event ID is its command ID. */ 10015 id_key = Qperform;
9747 err = mac_store_event_ref_as_apple_event (0, command.commandID, 10016 num_params = sizeof (names_pfm) / sizeof (names_pfm[0]);
9748 Qhi_command, Qnil, 10017 names = names_pfm;
9749 event, num_params, names, types); 10018 types = types_pfm;
9750 return err == noErr ? noErr : eventNotHandledErr; 10019 break;
9751 } 10020
9752 10021 default:
9753 static OSStatus 10022 abort ();
9754 init_command_handler () 10023 }
9755 { 10024
9756 static const EventTypeSpec specs[] = 10025 err = mac_store_event_ref_as_apple_event (0, 0, Qservice, id_key,
9757 {{kEventClassCommand, kEventCommandProcess}}; 10026 event, num_params,
9758 static EventHandlerUPP handle_command_eventUPP = NULL; 10027 names, types);
9759 10028
9760 if (handle_command_eventUPP == NULL) 10029 return err;
9761 handle_command_eventUPP = NewEventHandlerUPP (mac_handle_command_event); 10030 }
9762 return InstallApplicationEventHandler (handle_command_eventUPP, 10031 #endif /* MAC_OSX */
9763 GetEventTypeCount (specs), specs,
9764 NULL, NULL);
9765 }
9766 10032
9767 static pascal OSStatus 10033 static pascal OSStatus
9768 mac_handle_window_event (next_handler, event, data) 10034 mac_handle_window_event (next_handler, event, data)
9769 EventHandlerCallRef next_handler; 10035 EventHandlerCallRef next_handler;
9770 EventRef event; 10036 EventRef event;
9771 void *data; 10037 void *data;
9772 { 10038 {
9773 WindowRef wp; 10039 WindowRef wp;
9774 OSStatus result, err; 10040 OSStatus err, result = eventNotHandledErr;
9775 struct frame *f; 10041 struct frame *f;
9776 UInt32 attributes; 10042 UInt32 attributes;
9777 XSizeHints *size_hints; 10043 XSizeHints *size_hints;
9778 10044
9779 err = GetEventParameter (event, kEventParamDirectObject, typeWindowRef, 10045 err = GetEventParameter (event, kEventParamDirectObject, typeWindowRef,
9782 return eventNotHandledErr; 10048 return eventNotHandledErr;
9783 10049
9784 f = mac_window_to_frame (wp); 10050 f = mac_window_to_frame (wp);
9785 switch (GetEventKind (event)) 10051 switch (GetEventKind (event))
9786 { 10052 {
10053 /* -- window refresh events -- */
10054
9787 case kEventWindowUpdate: 10055 case kEventWindowUpdate:
9788 result = CallNextEventHandler (next_handler, event); 10056 result = CallNextEventHandler (next_handler, event);
9789 if (result != eventNotHandledErr) 10057 if (result != eventNotHandledErr)
9790 return result; 10058 break;
9791 10059
9792 do_window_update (wp); 10060 do_window_update (wp);
9793 return noErr; 10061 result = noErr;
9794 10062 break;
9795 case kEventWindowGetIdealSize: 10063
9796 result = CallNextEventHandler (next_handler, event); 10064 /* -- window state change events -- */
9797 if (result != eventNotHandledErr) 10065
9798 return result; 10066 case kEventWindowShowing:
9799 10067 size_hints = FRAME_SIZE_HINTS (f);
9800 { 10068 if (!(size_hints->flags & (USPosition | PPosition)))
9801 Point ideal_size = mac_get_ideal_size (f); 10069 {
9802 10070 struct frame *sf = SELECTED_FRAME ();
9803 err = SetEventParameter (event, kEventParamDimensions, 10071
9804 typeQDPoint, sizeof (Point), &ideal_size); 10072 if (!(FRAME_MAC_P (sf)))
9805 if (err == noErr) 10073 RepositionWindow (wp, NULL, kWindowCenterOnMainScreen);
9806 return noErr; 10074 else
9807 } 10075 {
10076 RepositionWindow (wp, FRAME_MAC_WINDOW (sf),
10077 #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1020
10078 kWindowCascadeStartAtParentWindowScreen
10079 #else
10080 kWindowCascadeOnParentWindowScreen
10081 #endif
10082 );
10083 }
10084 result = noErr;
10085 }
10086 break;
10087
10088 case kEventWindowHiding:
10089 /* Before unmapping the window, update the WM_SIZE_HINTS
10090 property to claim that the current position of the window is
10091 user-specified, rather than program-specified, so that when
10092 the window is mapped again, it will be placed at the same
10093 location, without forcing the user to position it by hand
10094 again (they have already done that once for this window.) */
10095 x_wm_set_size_hint (f, (long) 0, 1);
10096 result = noErr;
10097 break;
10098
10099 case kEventWindowShown:
10100 case kEventWindowHidden:
10101 case kEventWindowCollapsed:
10102 case kEventWindowExpanded:
10103 mac_handle_visibility_change (f);
10104 result = noErr;
9808 break; 10105 break;
9809 10106
9810 case kEventWindowBoundsChanging: 10107 case kEventWindowBoundsChanging:
9811 result = CallNextEventHandler (next_handler, event); 10108 result = CallNextEventHandler (next_handler, event);
9812 if (result != eventNotHandledErr) 10109 if (result != eventNotHandledErr)
9813 return result; 10110 break;
9814 10111
9815 err = GetEventParameter (event, kEventParamAttributes, typeUInt32, 10112 err = GetEventParameter (event, kEventParamAttributes, typeUInt32,
9816 NULL, sizeof (UInt32), NULL, &attributes); 10113 NULL, sizeof (UInt32), NULL, &attributes);
9817 if (err != noErr) 10114 if (err != noErr)
9818 break; 10115 break;
9852 10149
9853 bounds.right = bounds.left + width; 10150 bounds.right = bounds.left + width;
9854 bounds.bottom = bounds.top + height; 10151 bounds.bottom = bounds.top + height;
9855 SetEventParameter (event, kEventParamCurrentBounds, 10152 SetEventParameter (event, kEventParamCurrentBounds,
9856 typeQDRectangle, sizeof (Rect), &bounds); 10153 typeQDRectangle, sizeof (Rect), &bounds);
9857 return noErr; 10154 result = noErr;
9858 } 10155 }
9859 break; 10156 break;
9860 10157
9861 case kEventWindowBoundsChanged: 10158 case kEventWindowBoundsChanged:
9862 err = GetEventParameter (event, kEventParamAttributes, typeUInt32, 10159 err = GetEventParameter (event, kEventParamAttributes, typeUInt32,
9883 } 10180 }
9884 10181
9885 if (attributes & kWindowBoundsChangeOriginChanged) 10182 if (attributes & kWindowBoundsChangeOriginChanged)
9886 mac_handle_origin_change (f); 10183 mac_handle_origin_change (f);
9887 10184
9888 return noErr; 10185 result = noErr;
9889
9890 case kEventWindowShown:
9891 case kEventWindowHidden:
9892 case kEventWindowExpanded:
9893 case kEventWindowCollapsed:
9894 result = CallNextEventHandler (next_handler, event);
9895
9896 mac_handle_visibility_change (f);
9897 return noErr;
9898
9899 break; 10186 break;
9900 10187
10188 /* -- window action events -- */
10189
9901 case kEventWindowClose: 10190 case kEventWindowClose:
9902 result = CallNextEventHandler (next_handler, event);
9903 { 10191 {
9904 struct input_event buf; 10192 struct input_event buf;
9905 10193
9906 EVENT_INIT (buf); 10194 EVENT_INIT (buf);
9907 buf.kind = DELETE_WINDOW_EVENT; 10195 buf.kind = DELETE_WINDOW_EVENT;
9908 XSETFRAME (buf.frame_or_window, f); 10196 XSETFRAME (buf.frame_or_window, f);
9909 buf.arg = Qnil; 10197 buf.arg = Qnil;
9910 kbd_buffer_store_event (&buf); 10198 kbd_buffer_store_event (&buf);
9911 } 10199 }
9912 return noErr; 10200 result = noErr;
10201 break;
10202
10203 case kEventWindowGetIdealSize:
10204 result = CallNextEventHandler (next_handler, event);
10205 if (result != eventNotHandledErr)
10206 break;
10207
10208 {
10209 Point ideal_size = mac_get_ideal_size (f);
10210
10211 err = SetEventParameter (event, kEventParamDimensions,
10212 typeQDPoint, sizeof (Point), &ideal_size);
10213 if (err == noErr)
10214 result = noErr;
10215 }
10216 break;
9913 10217
9914 #ifdef MAC_OSX 10218 #ifdef MAC_OSX
9915 case kEventWindowToolbarSwitchMode: 10219 case kEventWindowToolbarSwitchMode:
9916 result = CallNextEventHandler (next_handler, event);
9917 { 10220 {
9918 static const EventParamName names[] = {kEventParamDirectObject, 10221 static const EventParamName names[] = {kEventParamDirectObject,
9919 kEventParamWindowMouseLocation, 10222 kEventParamWindowMouseLocation,
9920 kEventParamKeyModifiers, 10223 kEventParamKeyModifiers,
9921 kEventParamMouseButton, 10224 kEventParamMouseButton,
9933 Qwindow, 10236 Qwindow,
9934 Qtoolbar_switch_mode, 10237 Qtoolbar_switch_mode,
9935 event, num_params, 10238 event, num_params,
9936 names, types); 10239 names, types);
9937 } 10240 }
9938 return err == noErr ? noErr : result; 10241 if (err == noErr)
10242 result = noErr;
10243 break;
9939 #endif 10244 #endif
9940 10245
9941 #if USE_MAC_TSM 10246 #if USE_MAC_TSM
10247 /* -- window focus events -- */
10248
9942 case kEventWindowFocusAcquired: 10249 case kEventWindowFocusAcquired:
10250 err = mac_tsm_resume ();
10251 if (err == noErr)
10252 result = noErr;
10253 break;
10254
10255 case kEventWindowFocusRelinquish:
10256 err = mac_tsm_suspend ();
10257 if (err == noErr)
10258 result = noErr;
10259 break;
10260 #endif
10261
10262 default:
10263 abort ();
10264 }
10265
10266 return result;
10267 }
10268
10269 static pascal OSStatus
10270 mac_handle_application_event (next_handler, event, data)
10271 EventHandlerCallRef next_handler;
10272 EventRef event;
10273 void *data;
10274 {
10275 OSStatus err, result = eventNotHandledErr;
10276
10277 switch (GetEventKind (event))
10278 {
10279 #if USE_MAC_TSM
10280 case kEventAppActivated:
10281 err = mac_tsm_resume ();
10282 break;
10283
10284 case kEventAppDeactivated:
10285 err = mac_tsm_suspend ();
10286 break;
10287 #endif
10288
10289 default:
10290 abort ();
10291 }
10292
10293 if (err == noErr)
10294 result = noErr;
10295
10296 return result;
10297 }
10298
10299 static pascal OSStatus
10300 mac_handle_keyboard_event (next_handler, event, data)
10301 EventHandlerCallRef next_handler;
10302 EventRef event;
10303 void *data;
10304 {
10305 OSStatus err, result = eventNotHandledErr;
10306 UInt32 event_kind, key_code, modifiers, mapped_modifiers;
10307 unsigned char char_code;
10308
10309 event_kind = GetEventKind (event);
10310 switch (event_kind)
10311 {
10312 case kEventRawKeyDown:
10313 case kEventRawKeyRepeat:
10314 case kEventRawKeyUp:
10315 if (read_socket_inev == NULL)
10316 {
10317 result = CallNextEventHandler (next_handler, event);
10318 break;
10319 }
10320
10321 err = GetEventParameter (event, kEventParamKeyModifiers,
10322 typeUInt32, NULL,
10323 sizeof (UInt32), NULL, &modifiers);
10324 if (err != noErr)
10325 break;
10326
10327 mapped_modifiers = mac_mapped_modifiers (modifiers);
10328
10329 /* When using Carbon Events, we need to pass raw keyboard events
10330 to the TSM ourselves. If TSM handles it, it will pass back
10331 noErr, otherwise it will pass back "eventNotHandledErr" and
10332 we can process it normally. */
10333 if (!(mapped_modifiers
10334 & ~(mac_pass_command_to_system ? cmdKey : 0)
10335 & ~(mac_pass_control_to_system ? controlKey : 0)))
10336 {
10337 result = CallNextEventHandler (next_handler, event);
10338 if (result != eventNotHandledErr)
10339 break;
10340 }
10341
10342 #if USE_MAC_TSM
10343 if (read_socket_inev->kind != NO_EVENT)
10344 {
10345 result = noErr;
10346 break;
10347 }
10348 #endif
10349
10350 if (event_kind == kEventRawKeyUp)
10351 break;
10352
10353 err = GetEventParameter (event, kEventParamKeyMacCharCodes,
10354 typeChar, NULL,
10355 sizeof (char), NULL, &char_code);
10356 if (err != noErr)
10357 break;
10358
10359 err = GetEventParameter (event, kEventParamKeyCode,
10360 typeUInt32, NULL,
10361 sizeof (UInt32), NULL, &key_code);
10362 if (err != noErr)
10363 break;
10364
10365 do_keystroke ((GetEventKind (event) == kEventRawKeyDown
10366 ? keyDown : autoKey),
10367 char_code, key_code, modifiers,
10368 ((unsigned long)
10369 (GetEventTime (event) / kEventDurationMillisecond)),
10370 read_socket_inev);
10371 result = noErr;
10372 break;
10373
10374 default:
10375 abort ();
10376 }
10377
10378 return result;
10379 }
10380
10381 static pascal OSStatus
10382 mac_handle_command_event (next_handler, event, data)
10383 EventHandlerCallRef next_handler;
10384 EventRef event;
10385 void *data;
10386 {
10387 OSStatus err, result = eventNotHandledErr;
10388 HICommand command;
10389 static const EventParamName names[] =
10390 {kEventParamDirectObject, kEventParamKeyModifiers};
10391 static const EventParamType types[] =
10392 {typeHICommand, typeUInt32};
10393 int num_params = sizeof (names) / sizeof (names[0]);
10394
10395 err = GetEventParameter (event, kEventParamDirectObject, typeHICommand,
10396 NULL, sizeof (HICommand), NULL, &command);
10397 if (err != noErr)
10398 return eventNotHandledErr;
10399
10400 switch (GetEventKind (event))
10401 {
10402 case kEventCommandProcess:
9943 result = CallNextEventHandler (next_handler, event); 10403 result = CallNextEventHandler (next_handler, event);
9944 err = mac_tsm_resume (); 10404 if (result != eventNotHandledErr)
9945 return err == noErr ? noErr : result; 10405 break;
9946 10406
9947 case kEventWindowFocusRelinquish: 10407 err = GetEventParameter (event, kEventParamDirectObject,
9948 result = CallNextEventHandler (next_handler, event); 10408 typeHICommand, NULL,
9949 err = mac_tsm_suspend (); 10409 sizeof (HICommand), NULL, &command);
9950 return err == noErr ? noErr : result; 10410
9951 #endif 10411 if (err != noErr || command.commandID == 0)
9952 } 10412 break;
9953 10413
9954 return eventNotHandledErr; 10414 /* A HI command event is mapped to an Apple event whose event
10415 class symbol is `hi-command' and event ID is its command
10416 ID. */
10417 err = mac_store_event_ref_as_apple_event (0, command.commandID,
10418 Qhi_command, Qnil,
10419 event, num_params,
10420 names, types);
10421 if (err == noErr)
10422 result = noErr;
10423 break;
10424
10425 default:
10426 abort ();
10427 }
10428
10429 return result;
9955 } 10430 }
9956 10431
9957 static pascal OSStatus 10432 static pascal OSStatus
9958 mac_handle_mouse_event (next_handler, event, data) 10433 mac_handle_mouse_event (next_handler, event, data)
9959 EventHandlerCallRef next_handler; 10434 EventHandlerCallRef next_handler;
9960 EventRef event; 10435 EventRef event;
9961 void *data; 10436 void *data;
9962 { 10437 {
9963 OSStatus result, err; 10438 OSStatus err, result = eventNotHandledErr;
9964 10439
9965 switch (GetEventKind (event)) 10440 switch (GetEventKind (event))
9966 { 10441 {
9967 case kEventMouseWheelMoved: 10442 case kEventMouseWheelMoved:
9968 { 10443 {
9972 SInt32 delta; 10447 SInt32 delta;
9973 Point point; 10448 Point point;
9974 10449
9975 result = CallNextEventHandler (next_handler, event); 10450 result = CallNextEventHandler (next_handler, event);
9976 if (result != eventNotHandledErr || read_socket_inev == NULL) 10451 if (result != eventNotHandledErr || read_socket_inev == NULL)
9977 return result; 10452 break;
10453
10454 f = mac_focus_frame (&one_mac_display_info);
9978 10455
9979 err = GetEventParameter (event, kEventParamWindowRef, typeWindowRef, 10456 err = GetEventParameter (event, kEventParamWindowRef, typeWindowRef,
9980 NULL, sizeof (WindowRef), NULL, &wp); 10457 NULL, sizeof (WindowRef), NULL, &wp);
9981 if (err != noErr) 10458 if (err != noErr
9982 break; 10459 || wp != FRAME_MAC_WINDOW (f))
9983
9984 f = mac_window_to_frame (wp);
9985 if (f != mac_focus_frame (&one_mac_display_info))
9986 break; 10460 break;
9987 10461
9988 err = GetEventParameter (event, kEventParamMouseWheelAxis, 10462 err = GetEventParameter (event, kEventParamMouseWheelAxis,
9989 typeMouseWheelAxis, NULL, 10463 typeMouseWheelAxis, NULL,
9990 sizeof (EventMouseWheelAxis), NULL, &axis); 10464 sizeof (EventMouseWheelAxis), NULL, &axis);
9995 typeQDPoint, NULL, sizeof (Point), 10469 typeQDPoint, NULL, sizeof (Point),
9996 NULL, &point); 10470 NULL, &point);
9997 if (err != noErr) 10471 if (err != noErr)
9998 break; 10472 break;
9999 10473
10000 SetPortWindowPort (wp); 10474 point.h -= f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f);
10001 GlobalToLocal (&point); 10475 point.v -= f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f);
10002 if (point.h < 0 || point.v < 0 10476 if (point.h < 0 || point.v < 0
10003 || EQ (window_from_coordinates (f, point.h, point.v, 0, 0, 0, 1), 10477 || EQ (window_from_coordinates (f, point.h, point.v, 0, 0, 0, 1),
10004 f->tool_bar_window)) 10478 f->tool_bar_window))
10005 break; 10479 break;
10006 10480
10017 | ((delta < 0) ? down_modifier : up_modifier)); 10491 | ((delta < 0) ? down_modifier : up_modifier));
10018 XSETINT (read_socket_inev->x, point.h); 10492 XSETINT (read_socket_inev->x, point.h);
10019 XSETINT (read_socket_inev->y, point.v); 10493 XSETINT (read_socket_inev->y, point.v);
10020 XSETFRAME (read_socket_inev->frame_or_window, f); 10494 XSETFRAME (read_socket_inev->frame_or_window, f);
10021 10495
10022 return noErr; 10496 result = noErr;
10023 } 10497 }
10024 break; 10498 break;
10025 10499
10026 default: 10500 default:
10027 break; 10501 abort ();
10028 } 10502 }
10029 10503
10030 return eventNotHandledErr; 10504 return result;
10031 } 10505 }
10032
10033 #if USE_MAC_FONT_PANEL
10034 static pascal OSStatus
10035 mac_handle_font_event (next_handler, event, data)
10036 EventHandlerCallRef next_handler;
10037 EventRef event;
10038 void *data;
10039 {
10040 OSStatus result, err;
10041 Lisp_Object id_key;
10042 int num_params;
10043 const EventParamName *names;
10044 const EventParamType *types;
10045 static const EventParamName names_sel[] = {kEventParamATSUFontID,
10046 kEventParamATSUFontSize,
10047 kEventParamFMFontFamily,
10048 kEventParamFMFontSize,
10049 kEventParamFontColor};
10050 static const EventParamType types_sel[] = {typeATSUFontID,
10051 typeATSUSize,
10052 typeFMFontFamily,
10053 typeFMFontSize,
10054 typeFontColor};
10055
10056 result = CallNextEventHandler (next_handler, event);
10057 if (result != eventNotHandledErr)
10058 return result;
10059
10060 switch (GetEventKind (event))
10061 {
10062 case kEventFontPanelClosed:
10063 id_key = Qpanel_closed;
10064 num_params = 0;
10065 names = NULL;
10066 types = NULL;
10067 break;
10068
10069 case kEventFontSelection:
10070 id_key = Qselection;
10071 num_params = sizeof (names_sel) / sizeof (names_sel[0]);
10072 names = names_sel;
10073 types = types_sel;
10074 break;
10075 }
10076
10077 err = mac_store_event_ref_as_apple_event (0, 0, Qfont, id_key,
10078 event, num_params,
10079 names, types);
10080
10081 return err == noErr ? noErr : eventNotHandledErr;
10082 }
10083 #endif
10084 10506
10085 #if USE_MAC_TSM 10507 #if USE_MAC_TSM
10086 static pascal OSStatus 10508 static pascal OSStatus
10087 mac_handle_text_input_event (next_handler, event, data) 10509 mac_handle_text_input_event (next_handler, event, data)
10088 EventHandlerCallRef next_handler; 10510 EventHandlerCallRef next_handler;
10135 typeLongInteger, 10557 typeLongInteger,
10136 typeIntlWritingCode, 10558 typeIntlWritingCode,
10137 typeUnicodeText}; 10559 typeUnicodeText};
10138 10560
10139 result = CallNextEventHandler (next_handler, event); 10561 result = CallNextEventHandler (next_handler, event);
10562 if (result != eventNotHandledErr)
10563 return result;
10140 10564
10141 switch (GetEventKind (event)) 10565 switch (GetEventKind (event))
10142 { 10566 {
10143 case kEventTextInputUpdateActiveInputArea: 10567 case kEventTextInputUpdateActiveInputArea:
10144 id_key = Qupdate_active_input_area; 10568 id_key = Qupdate_active_input_area;
10162 err = GetEventParameter (kbd_event, kEventParamKeyModifiers, 10586 err = GetEventParameter (kbd_event, kEventParamKeyModifiers,
10163 typeUInt32, NULL, 10587 typeUInt32, NULL,
10164 sizeof (UInt32), NULL, &modifiers); 10588 sizeof (UInt32), NULL, &modifiers);
10165 if (err == noErr && mac_mapped_modifiers (modifiers)) 10589 if (err == noErr && mac_mapped_modifiers (modifiers))
10166 /* There're mapped modifier keys. Process it in 10590 /* There're mapped modifier keys. Process it in
10167 XTread_socket. */ 10591 do_keystroke. */
10168 return eventNotHandledErr; 10592 return eventNotHandledErr;
10169 if (err == noErr) 10593 if (err == noErr)
10170 err = GetEventParameter (kbd_event, kEventParamKeyUnicodes, 10594 err = GetEventParameter (kbd_event, kEventParamKeyUnicodes,
10171 typeUnicodeText, NULL, 0, &actual_size, 10595 typeUnicodeText, NULL, 0, &actual_size,
10172 NULL); 10596 NULL);
10177 err = GetEventParameter (kbd_event, kEventParamKeyUnicodes, 10601 err = GetEventParameter (kbd_event, kEventParamKeyUnicodes,
10178 typeUnicodeText, NULL, 10602 typeUnicodeText, NULL,
10179 sizeof (UniChar), NULL, &code); 10603 sizeof (UniChar), NULL, &code);
10180 if (err == noErr && code < 0x80) 10604 if (err == noErr && code < 0x80)
10181 { 10605 {
10182 /* ASCII character. Process it in XTread_socket. */ 10606 /* ASCII character. Process it in do_keystroke. */
10183 if (read_socket_inev && code >= 0x20 && code <= 0x7e) 10607 if (read_socket_inev && code >= 0x20 && code <= 0x7e)
10184 { 10608 {
10185 UInt32 key_code; 10609 UInt32 key_code;
10186 10610
10187 err = GetEventParameter (kbd_event, kEventParamKeyCode, 10611 err = GetEventParameter (kbd_event, kEventParamKeyCode,
10239 f = SELECTED_FRAME (); 10663 f = SELECTED_FRAME ();
10240 w = XWINDOW (f->selected_window); 10664 w = XWINDOW (f->selected_window);
10241 } 10665 }
10242 10666
10243 p.h = (WINDOW_TO_FRAME_PIXEL_X (w, w->cursor.x) 10667 p.h = (WINDOW_TO_FRAME_PIXEL_X (w, w->cursor.x)
10244 + WINDOW_LEFT_FRINGE_WIDTH (w)); 10668 + WINDOW_LEFT_FRINGE_WIDTH (w)
10669 + f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f));
10245 p.v = (WINDOW_TO_FRAME_PIXEL_Y (w, w->cursor.y) 10670 p.v = (WINDOW_TO_FRAME_PIXEL_Y (w, w->cursor.y)
10246 + FONT_BASE (FRAME_FONT (f))); 10671 + FONT_BASE (FRAME_FONT (f))
10247 SetPortWindowPort (FRAME_MAC_WINDOW (f)); 10672 + f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f));
10248 LocalToGlobal (&p);
10249 err = SetEventParameter (event, kEventParamTextInputReplyPoint, 10673 err = SetEventParameter (event, kEventParamTextInputReplyPoint,
10250 typeQDPoint, sizeof (typeQDPoint), &p); 10674 typeQDPoint, sizeof (typeQDPoint), &p);
10251 } 10675 }
10252 break; 10676 break;
10253 10677
10257 10681
10258 if (!NILP (id_key)) 10682 if (!NILP (id_key))
10259 err = mac_store_event_ref_as_apple_event (0, 0, Qtext_input, id_key, 10683 err = mac_store_event_ref_as_apple_event (0, 0, Qtext_input, id_key,
10260 event, num_params, 10684 event, num_params,
10261 names, types); 10685 names, types);
10262 10686 if (err == noErr)
10263 return err == noErr ? noErr : result; 10687 result = noErr;
10264 } 10688
10265 #endif 10689 return result;
10266 10690 }
10267 #ifdef MAC_OSX 10691 #endif
10268 OSStatus
10269 mac_store_service_event (event)
10270 EventRef event;
10271 {
10272 OSStatus err;
10273 Lisp_Object id_key;
10274 int num_params;
10275 const EventParamName *names;
10276 const EventParamType *types;
10277 static const EventParamName names_pfm[] =
10278 {kEventParamServiceMessageName, kEventParamServiceUserData};
10279 static const EventParamType types_pfm[] =
10280 {typeCFStringRef, typeCFStringRef};
10281
10282 switch (GetEventKind (event))
10283 {
10284 case kEventServicePaste:
10285 id_key = Qpaste;
10286 num_params = 0;
10287 names = NULL;
10288 types = NULL;
10289 break;
10290
10291 case kEventServicePerform:
10292 id_key = Qperform;
10293 num_params = sizeof (names_pfm) / sizeof (names_pfm[0]);
10294 names = names_pfm;
10295 types = types_pfm;
10296 break;
10297
10298 default:
10299 abort ();
10300 }
10301
10302 err = mac_store_event_ref_as_apple_event (0, 0, Qservice, id_key,
10303 event, num_params,
10304 names, types);
10305
10306 return err;
10307 }
10308 #endif /* MAC_OSX */
10309 #endif /* TARGET_API_MAC_CARBON */ 10692 #endif /* TARGET_API_MAC_CARBON */
10310 10693
10311 10694
10312 OSStatus 10695 OSStatus
10313 install_window_handler (window) 10696 install_window_handler (window)
10314 WindowRef window; 10697 WindowRef window;
10315 { 10698 {
10316 OSStatus err = noErr; 10699 OSStatus err = noErr;
10700
10317 #if TARGET_API_MAC_CARBON 10701 #if TARGET_API_MAC_CARBON
10318 static const EventTypeSpec specs_window[] = 10702 if (err == noErr)
10319 {{kEventClassWindow, kEventWindowUpdate}, 10703 {
10320 {kEventClassWindow, kEventWindowGetIdealSize}, 10704 static const EventTypeSpec specs[] =
10321 {kEventClassWindow, kEventWindowBoundsChanging}, 10705 {
10322 {kEventClassWindow, kEventWindowBoundsChanged}, 10706 /* -- window refresh events -- */
10323 {kEventClassWindow, kEventWindowShown}, 10707 {kEventClassWindow, kEventWindowUpdate},
10324 {kEventClassWindow, kEventWindowHidden}, 10708 /* -- window state change events -- */
10325 {kEventClassWindow, kEventWindowExpanded}, 10709 {kEventClassWindow, kEventWindowShowing},
10326 {kEventClassWindow, kEventWindowCollapsed}, 10710 {kEventClassWindow, kEventWindowHiding},
10327 {kEventClassWindow, kEventWindowClose}, 10711 {kEventClassWindow, kEventWindowShown},
10712 {kEventClassWindow, kEventWindowHidden},
10713 {kEventClassWindow, kEventWindowCollapsed},
10714 {kEventClassWindow, kEventWindowExpanded},
10715 {kEventClassWindow, kEventWindowBoundsChanging},
10716 {kEventClassWindow, kEventWindowBoundsChanged},
10717 /* -- window action events -- */
10718 {kEventClassWindow, kEventWindowClose},
10719 {kEventClassWindow, kEventWindowGetIdealSize},
10328 #ifdef MAC_OSX 10720 #ifdef MAC_OSX
10329 {kEventClassWindow, kEventWindowToolbarSwitchMode}, 10721 {kEventClassWindow, kEventWindowToolbarSwitchMode},
10330 #endif 10722 #endif
10331 #if USE_MAC_TSM 10723 #if USE_MAC_TSM
10332 {kEventClassWindow, kEventWindowFocusAcquired}, 10724 /* -- window focus events -- */
10333 {kEventClassWindow, kEventWindowFocusRelinquish}, 10725 {kEventClassWindow, kEventWindowFocusAcquired},
10334 #endif 10726 {kEventClassWindow, kEventWindowFocusRelinquish},
10335 }; 10727 #endif
10336 static const EventTypeSpec specs_mouse[] = 10728 };
10337 {{kEventClassMouse, kEventMouseWheelMoved}}; 10729 static EventHandlerUPP handle_window_eventUPP = NULL;
10338 static EventHandlerUPP handle_window_eventUPP = NULL; 10730
10339 static EventHandlerUPP handle_mouse_eventUPP = NULL; 10731 if (handle_window_eventUPP == NULL)
10340 #if USE_MAC_FONT_PANEL 10732 handle_window_eventUPP = NewEventHandlerUPP (mac_handle_window_event);
10341 static const EventTypeSpec specs_font[] = 10733
10342 {{kEventClassFont, kEventFontPanelClosed}, 10734 err = InstallWindowEventHandler (window, handle_window_eventUPP,
10343 {kEventClassFont, kEventFontSelection}}; 10735 GetEventTypeCount (specs),
10344 static EventHandlerUPP handle_font_eventUPP = NULL; 10736 specs, NULL, NULL);
10345 #endif 10737 }
10346 #if USE_MAC_TSM 10738 #endif
10347 static const EventTypeSpec specs_text_input[] = 10739
10348 {{kEventClassTextInput, kEventTextInputUpdateActiveInputArea},
10349 {kEventClassTextInput, kEventTextInputUnicodeForKeyEvent},
10350 {kEventClassTextInput, kEventTextInputOffsetToPos}};
10351 static EventHandlerUPP handle_text_input_eventUPP = NULL;
10352 #endif
10353
10354 if (handle_window_eventUPP == NULL)
10355 handle_window_eventUPP = NewEventHandlerUPP (mac_handle_window_event);
10356 if (handle_mouse_eventUPP == NULL)
10357 handle_mouse_eventUPP = NewEventHandlerUPP (mac_handle_mouse_event);
10358 #if USE_MAC_FONT_PANEL
10359 if (handle_font_eventUPP == NULL)
10360 handle_font_eventUPP = NewEventHandlerUPP (mac_handle_font_event);
10361 #endif
10362 #if USE_MAC_TSM
10363 if (handle_text_input_eventUPP == NULL)
10364 handle_text_input_eventUPP =
10365 NewEventHandlerUPP (mac_handle_text_input_event);
10366 #endif
10367 err = InstallWindowEventHandler (window, handle_window_eventUPP,
10368 GetEventTypeCount (specs_window),
10369 specs_window, NULL, NULL);
10370 if (err == noErr)
10371 err = InstallWindowEventHandler (window, handle_mouse_eventUPP,
10372 GetEventTypeCount (specs_mouse),
10373 specs_mouse, NULL, NULL);
10374 #if USE_MAC_FONT_PANEL
10375 if (err == noErr)
10376 err = InstallWindowEventHandler (window, handle_font_eventUPP,
10377 GetEventTypeCount (specs_font),
10378 specs_font, NULL, NULL);
10379 #endif
10380 #if USE_MAC_TSM
10381 if (err == noErr)
10382 err = InstallWindowEventHandler (window, handle_text_input_eventUPP,
10383 GetEventTypeCount (specs_text_input),
10384 specs_text_input, window, NULL);
10385 #endif
10386 #endif
10387 if (err == noErr) 10740 if (err == noErr)
10388 err = install_drag_handler (window); 10741 err = install_drag_handler (window);
10389 if (err == noErr)
10390 err = install_menu_target_item_handler (window);
10391 10742
10392 return err; 10743 return err;
10393 } 10744 }
10394 10745
10395 void 10746 void
10397 WindowRef window; 10748 WindowRef window;
10398 { 10749 {
10399 remove_drag_handler (window); 10750 remove_drag_handler (window);
10400 } 10751 }
10401 10752
10753 #if TARGET_API_MAC_CARBON
10754 static OSStatus
10755 install_application_handler ()
10756 {
10757 OSStatus err = noErr;
10758
10759 if (err == noErr)
10760 {
10761 static const EventTypeSpec specs[] = {
10762 #if USE_MAC_TSM
10763 {kEventClassApplication, kEventAppActivated},
10764 {kEventClassApplication, kEventAppDeactivated},
10765 #endif
10766 };
10767
10768 err = InstallApplicationEventHandler (NewEventHandlerUPP
10769 (mac_handle_application_event),
10770 GetEventTypeCount (specs),
10771 specs, NULL, NULL);
10772 }
10773
10774 if (err == noErr)
10775 {
10776 static const EventTypeSpec specs[] =
10777 {{kEventClassKeyboard, kEventRawKeyDown},
10778 {kEventClassKeyboard, kEventRawKeyRepeat},
10779 {kEventClassKeyboard, kEventRawKeyUp}};
10780
10781 err = InstallApplicationEventHandler (NewEventHandlerUPP
10782 (mac_handle_keyboard_event),
10783 GetEventTypeCount (specs),
10784 specs, NULL, NULL);
10785 }
10786
10787 if (err == noErr)
10788 {
10789 static const EventTypeSpec specs[] =
10790 {{kEventClassCommand, kEventCommandProcess}};
10791
10792 err = InstallApplicationEventHandler (NewEventHandlerUPP
10793 (mac_handle_command_event),
10794 GetEventTypeCount (specs),
10795 specs, NULL, NULL);
10796 }
10797
10798 if (err == noErr)
10799 {
10800 static const EventTypeSpec specs[] =
10801 {{kEventClassMouse, kEventMouseWheelMoved}};
10802
10803 err = InstallApplicationEventHandler (NewEventHandlerUPP
10804 (mac_handle_mouse_event),
10805 GetEventTypeCount (specs),
10806 specs, NULL, NULL);
10807 }
10808
10809 #if USE_MAC_TSM
10810 if (err == noErr)
10811 {
10812 static const EventTypeSpec spec[] =
10813 {{kEventClassTextInput, kEventTextInputUpdateActiveInputArea},
10814 {kEventClassTextInput, kEventTextInputUnicodeForKeyEvent},
10815 {kEventClassTextInput, kEventTextInputOffsetToPos}};
10816
10817 err = InstallApplicationEventHandler (NewEventHandlerUPP
10818 (mac_handle_text_input_event),
10819 GetEventTypeCount (spec),
10820 spec, NULL, NULL);
10821 }
10822 #endif
10823
10824 if (err == noErr)
10825 err = install_menu_target_item_handler ();
10826
10827 #ifdef MAC_OSX
10828 if (err == noErr)
10829 err = install_service_handler ();
10830 #endif
10831
10832 return err;
10833 }
10834 #endif
10402 10835
10403 static pascal void 10836 static pascal void
10404 mac_handle_dm_notification (event) 10837 mac_handle_dm_notification (event)
10405 AppleEvent *event; 10838 AppleEvent *event;
10406 { 10839 {
10407 mac_screen_config_changed = 1; 10840 mac_screen_config_changed = 1;
10408 } 10841 }
10409 10842
10843 #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1030
10844 static void
10845 mac_handle_cg_display_reconfig (display, flags, user_info)
10846 CGDirectDisplayID display;
10847 CGDisplayChangeSummaryFlags flags;
10848 void *user_info;
10849 {
10850 mac_screen_config_changed = 1;
10851 }
10852 #endif
10853
10410 static OSErr 10854 static OSErr
10411 init_dm_notification_handler () 10855 init_dm_notification_handler ()
10412 { 10856 {
10413 OSErr err; 10857 OSErr err = noErr;
10414 static DMNotificationUPP handle_dm_notificationUPP = NULL; 10858
10415 ProcessSerialNumber psn; 10859 #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1030
10416 10860 #if MAC_OS_X_VERSION_MIN_REQUIRED == 1020
10417 if (handle_dm_notificationUPP == NULL) 10861 if (CGDisplayRegisterReconfigurationCallback != NULL)
10418 handle_dm_notificationUPP = 10862 #endif
10419 NewDMNotificationUPP (mac_handle_dm_notification); 10863 {
10420 10864 CGDisplayRegisterReconfigurationCallback (mac_handle_cg_display_reconfig,
10421 err = GetCurrentProcess (&psn); 10865 NULL);
10422 if (err == noErr) 10866 }
10423 err = DMRegisterNotifyProc (handle_dm_notificationUPP, &psn); 10867 #if MAC_OS_X_VERSION_MIN_REQUIRED == 1020
10868 else /* CGDisplayRegisterReconfigurationCallback == NULL */
10869 #endif
10870 #endif /* MAC_OS_X_VERSION_MAX_ALLOWED >= 1030 */
10871 #if MAC_OS_X_VERSION_MAX_ALLOWED < 1030 || MAC_OS_X_VERSION_MIN_REQUIRED == 1020
10872 {
10873 static DMNotificationUPP handle_dm_notificationUPP = NULL;
10874 ProcessSerialNumber psn;
10875
10876 if (handle_dm_notificationUPP == NULL)
10877 handle_dm_notificationUPP =
10878 NewDMNotificationUPP (mac_handle_dm_notification);
10879
10880 err = GetCurrentProcess (&psn);
10881 if (err == noErr)
10882 err = DMRegisterNotifyProc (handle_dm_notificationUPP, &psn);
10883 }
10884 #endif
10424 10885
10425 return err; 10886 return err;
10426 } 10887 }
10427 10888
10428 static void 10889 static void
10634 if (event) 11095 if (event)
10635 ReleaseEvent (event); 11096 ReleaseEvent (event);
10636 11097
10637 return err; 11098 return err;
10638 } 11099 }
10639
10640 static void
10641 mac_set_unicode_keystroke_event (code, buf)
10642 UniChar code;
10643 struct input_event *buf;
10644 {
10645 int charset_id, c1, c2;
10646
10647 if (code < 0x80)
10648 {
10649 buf->kind = ASCII_KEYSTROKE_EVENT;
10650 buf->code = code;
10651 }
10652 else if (code < 0x100)
10653 {
10654 if (code < 0xA0)
10655 charset_id = CHARSET_8_BIT_CONTROL;
10656 else
10657 charset_id = charset_latin_iso8859_1;
10658 buf->kind = MULTIBYTE_CHAR_KEYSTROKE_EVENT;
10659 buf->code = MAKE_CHAR (charset_id, code, 0);
10660 }
10661 else
10662 {
10663 if (code < 0x2500)
10664 charset_id = charset_mule_unicode_0100_24ff,
10665 code -= 0x100;
10666 else if (code < 0x33FF)
10667 charset_id = charset_mule_unicode_2500_33ff,
10668 code -= 0x2500;
10669 else if (code >= 0xE000)
10670 charset_id = charset_mule_unicode_e000_ffff,
10671 code -= 0xE000;
10672 c1 = (code / 96) + 32, c2 = (code % 96) + 32;
10673 buf->kind = MULTIBYTE_CHAR_KEYSTROKE_EVENT;
10674 buf->code = MAKE_CHAR (charset_id, c1, c2);
10675 }
10676 }
10677 #endif 11100 #endif
10678 11101
10679 /* Emacs calls this whenever it wants to read an input event from the 11102 /* Emacs calls this whenever it wants to read an input event from the
10680 user. */ 11103 user. */
10681 int 11104 int
10727 inev.kind = NO_EVENT; 11150 inev.kind = NO_EVENT;
10728 inev.arg = Qnil; 11151 inev.arg = Qnil;
10729 11152
10730 #if TARGET_API_MAC_CARBON 11153 #if TARGET_API_MAC_CARBON
10731 timestamp = GetEventTime (eventRef) / kEventDurationMillisecond; 11154 timestamp = GetEventTime (eventRef) / kEventDurationMillisecond;
10732 #else 11155
11156 if (!mac_convert_event_ref (eventRef, &er))
11157 goto OTHER;
11158 #else /* !TARGET_API_MAC_CARBON */
10733 timestamp = er.when * (1000 / 60); /* ticks to milliseconds */ 11159 timestamp = er.when * (1000 / 60); /* ticks to milliseconds */
10734 #endif 11160 #endif /* !TARGET_API_MAC_CARBON */
10735 11161
10736 #if TARGET_API_MAC_CARBON
10737 /* Handle new events */
10738 if (!mac_convert_event_ref (eventRef, &er))
10739 {
10740 /* There used to be a handler for the kEventMouseWheelMoved
10741 event here. But as of Mac OS X 10.4, this kind of event
10742 is not directly posted to the main event queue by
10743 two-finger scrolling on the trackpad. Instead, some
10744 private event is posted and it is converted to a wheel
10745 event by the default handler for the application target.
10746 The converted one can be received by a Carbon event
10747 handler installed on a window target. */
10748 read_socket_inev = &inev;
10749 SendEventToEventTarget (eventRef, toolbox_dispatcher);
10750 read_socket_inev = NULL;
10751 }
10752 else
10753 #endif /* TARGET_API_MAC_CARBON */
10754 switch (er.what) 11162 switch (er.what)
10755 { 11163 {
10756 case mouseDown: 11164 case mouseDown:
10757 case mouseUp: 11165 case mouseUp:
10758 { 11166 {
10759 WindowRef window_ptr; 11167 WindowRef window_ptr;
10760 ControlPartCode part_code; 11168 ControlPartCode part_code;
10761 int tool_bar_p = 0; 11169 int tool_bar_p = 0;
10762 11170
10763 #if TARGET_API_MAC_CARBON 11171 #if TARGET_API_MAC_CARBON
11172 OSStatus err;
11173
10764 /* This is needed to send mouse events like aqua window 11174 /* This is needed to send mouse events like aqua window
10765 buttons to the correct handler. */ 11175 buttons to the correct handler. */
10766 if (SendEventToEventTarget (eventRef, toolbox_dispatcher) 11176 read_socket_inev = &inev;
10767 != eventNotHandledErr) 11177 err = SendEventToEventTarget (eventRef, toolbox_dispatcher);
11178 read_socket_inev = NULL;
11179 if (err != eventNotHandledErr)
10768 break; 11180 break;
10769 #endif 11181 #endif
10770 last_mouse_glyph_frame = 0; 11182 last_mouse_glyph_frame = 0;
10771 11183
10772 if (dpyinfo->grabbed && last_mouse_frame 11184 if (dpyinfo->grabbed && last_mouse_frame
10811 SelectWindow (window_ptr); 11223 SelectWindow (window_ptr);
10812 else 11224 else
10813 { 11225 {
10814 ControlPartCode control_part_code; 11226 ControlPartCode control_part_code;
10815 ControlRef ch; 11227 ControlRef ch;
10816 Point mouse_loc = er.where; 11228 Point mouse_loc;
10817 #ifdef MAC_OSX 11229 #ifdef MAC_OSX
10818 ControlKind control_kind; 11230 ControlKind control_kind;
10819 #endif 11231 #endif
10820 11232
10821 f = mac_window_to_frame (window_ptr); 11233 f = mac_window_to_frame (window_ptr);
10822 /* convert to local coordinates of new window */ 11234 /* convert to local coordinates of new window */
10823 SetPortWindowPort (window_ptr); 11235 mouse_loc.h = (er.where.h
10824 11236 - (f->left_pos
10825 GlobalToLocal (&mouse_loc); 11237 + FRAME_OUTER_TO_INNER_DIFF_X (f)));
11238 mouse_loc.v = (er.where.v
11239 - (f->top_pos
11240 + FRAME_OUTER_TO_INNER_DIFF_Y (f)));
10826 #if TARGET_API_MAC_CARBON 11241 #if TARGET_API_MAC_CARBON
10827 ch = FindControlUnderMouse (mouse_loc, window_ptr, 11242 ch = FindControlUnderMouse (mouse_loc, window_ptr,
10828 &control_part_code); 11243 &control_part_code);
10829 #ifdef MAC_OSX 11244 #ifdef MAC_OSX
10830 if (ch) 11245 if (ch)
11006 break; 11421 break;
11007 } 11422 }
11008 } 11423 }
11009 break; 11424 break;
11010 11425
11426 #if !TARGET_API_MAC_CARBON
11011 case updateEvt: 11427 case updateEvt:
11012 #if TARGET_API_MAC_CARBON
11013 if (SendEventToEventTarget (eventRef, toolbox_dispatcher)
11014 != eventNotHandledErr)
11015 break;
11016 #else
11017 do_window_update ((WindowRef) er.message); 11428 do_window_update ((WindowRef) er.message);
11018 #endif
11019 break; 11429 break;
11430 #endif
11020 11431
11021 case osEvt: 11432 case osEvt:
11022 #if TARGET_API_MAC_CARBON
11023 if (SendEventToEventTarget (eventRef, toolbox_dispatcher)
11024 != eventNotHandledErr)
11025 break;
11026 #endif
11027 switch ((er.message >> 24) & 0x000000FF) 11433 switch ((er.message >> 24) & 0x000000FF)
11028 { 11434 {
11029 case suspendResumeMessage:
11030 #if USE_MAC_TSM
11031 if (er.message & resumeFlag)
11032 mac_tsm_resume ();
11033 else
11034 mac_tsm_suspend ();
11035 #endif
11036 break;
11037
11038 case mouseMovedMessage: 11435 case mouseMovedMessage:
11039 #if !TARGET_API_MAC_CARBON 11436 #if !TARGET_API_MAC_CARBON
11040 SetRectRgn (mouse_region, er.where.h, er.where.v, 11437 SetRectRgn (mouse_region, er.where.h, er.where.v,
11041 er.where.h + 1, er.where.v + 1); 11438 er.where.h + 1, er.where.v + 1);
11042 #endif 11439 #endif
11056 } 11453 }
11057 11454
11058 if (f) 11455 if (f)
11059 { 11456 {
11060 WindowRef wp = FRAME_MAC_WINDOW (f); 11457 WindowRef wp = FRAME_MAC_WINDOW (f);
11061 Point mouse_pos = er.where; 11458 Point mouse_pos;
11062 11459
11063 SetPortWindowPort (wp); 11460 mouse_pos.h = (er.where.h
11064 11461 - (f->left_pos
11065 GlobalToLocal (&mouse_pos); 11462 + FRAME_OUTER_TO_INNER_DIFF_X (f)));
11066 11463 mouse_pos.v = (er.where.v
11464 - (f->top_pos
11465 + FRAME_OUTER_TO_INNER_DIFF_Y (f)));
11067 if (dpyinfo->grabbed && tracked_scroll_bar) 11466 if (dpyinfo->grabbed && tracked_scroll_bar)
11068 #ifdef USE_TOOLKIT_SCROLL_BARS 11467 #ifdef USE_TOOLKIT_SCROLL_BARS
11069 x_scroll_bar_handle_drag (wp, tracked_scroll_bar, 11468 x_scroll_bar_handle_drag (wp, tracked_scroll_bar,
11070 mouse_pos, &inev); 11469 mouse_pos, &inev);
11071 #else /* not USE_TOOLKIT_SCROLL_BARS */ 11470 #else /* not USE_TOOLKIT_SCROLL_BARS */
11109 help_echo_string has changed, generate a 11508 help_echo_string has changed, generate a
11110 HELP_EVENT. */ 11509 HELP_EVENT. */
11111 if (!NILP (help_echo_string) || !NILP (previous_help_echo_string)) 11510 if (!NILP (help_echo_string) || !NILP (previous_help_echo_string))
11112 do_help = 1; 11511 do_help = 1;
11113 break; 11512 break;
11513
11514 default:
11515 goto OTHER;
11114 } 11516 }
11115 break; 11517 break;
11116 11518
11117 case activateEvt: 11519 case activateEvt:
11118 { 11520 {
11119 WindowRef window_ptr = (WindowRef) er.message; 11521 WindowRef window_ptr = (WindowRef) er.message;
11120 11522
11121 #if TARGET_API_MAC_CARBON
11122 if (SendEventToEventTarget (eventRef, toolbox_dispatcher)
11123 != eventNotHandledErr)
11124 break;
11125 #endif
11126 if (window_ptr == tip_window) 11523 if (window_ptr == tip_window)
11127 { 11524 {
11128 HideWindow (tip_window); 11525 HideWindow (tip_window);
11129 break; 11526 break;
11130 } 11527 }
11131 11528
11132 if (!is_emacs_window (window_ptr)) 11529 if (!is_emacs_window (window_ptr))
11133 break; 11530 goto OTHER;
11531
11532 f = mac_window_to_frame (window_ptr);
11134 11533
11135 if ((er.modifiers & activeFlag) != 0) 11534 if ((er.modifiers & activeFlag) != 0)
11136 { 11535 {
11137 /* A window has been activated */ 11536 /* A window has been activated */
11138 Point mouse_loc = er.where; 11537 Point mouse_loc;
11139 11538
11140 x_detect_focus_change (dpyinfo, &er, &inev); 11539 x_detect_focus_change (dpyinfo, &er, &inev);
11141 11540
11142 SetPortWindowPort (window_ptr); 11541 mouse_loc.h = (er.where.h
11143 GlobalToLocal (&mouse_loc); 11542 - (f->left_pos
11543 + FRAME_OUTER_TO_INNER_DIFF_X (f)));
11544 mouse_loc.v = (er.where.v
11545 - (f->top_pos
11546 + FRAME_OUTER_TO_INNER_DIFF_Y (f)));
11144 /* Window-activated event counts as mouse movement, 11547 /* Window-activated event counts as mouse movement,
11145 so update things that depend on mouse position. */ 11548 so update things that depend on mouse position. */
11146 note_mouse_movement (mac_window_to_frame (window_ptr), 11549 note_mouse_movement (f, &mouse_loc);
11147 &mouse_loc);
11148 } 11550 }
11149 else 11551 else
11150 { 11552 {
11151 /* A window has been deactivated */ 11553 /* A window has been deactivated */
11152 #ifdef USE_TOOLKIT_SCROLL_BARS 11554 #ifdef USE_TOOLKIT_SCROLL_BARS
11167 #endif 11569 #endif
11168 dpyinfo->grabbed = 0; 11570 dpyinfo->grabbed = 0;
11169 11571
11170 x_detect_focus_change (dpyinfo, &er, &inev); 11572 x_detect_focus_change (dpyinfo, &er, &inev);
11171 11573
11172 f = mac_window_to_frame (window_ptr);
11173 if (f == dpyinfo->mouse_face_mouse_frame) 11574 if (f == dpyinfo->mouse_face_mouse_frame)
11174 { 11575 {
11175 /* If we move outside the frame, then we're 11576 /* If we move outside the frame, then we're
11176 certainly no longer on any text in the 11577 certainly no longer on any text in the
11177 frame. */ 11578 frame. */
11190 break; 11591 break;
11191 11592
11192 case keyDown: 11593 case keyDown:
11193 case keyUp: 11594 case keyUp:
11194 case autoKey: 11595 case autoKey:
11195 { 11596 ObscureCursor ();
11196 int keycode = (er.message & keyCodeMask) >> 8; 11597
11197 static SInt16 last_key_script = -1; 11598 f = mac_focus_frame (dpyinfo);
11198 SInt16 current_key_script; 11599 XSETFRAME (inev.frame_or_window, f);
11199 UInt32 modifiers = er.modifiers, mapped_modifiers; 11600
11200 11601 /* If mouse-highlight is an integer, input clears out mouse
11201 #ifdef MAC_OSX 11602 highlighting. */
11202 GetEventParameter (eventRef, kEventParamKeyModifiers, 11603 if (!dpyinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight)
11203 typeUInt32, NULL, 11604 && !EQ (f->tool_bar_window, dpyinfo->mouse_face_window))
11204 sizeof (UInt32), NULL, &modifiers); 11605 {
11205 #endif 11606 clear_mouse_face (dpyinfo);
11206 mapped_modifiers = mac_mapped_modifiers (modifiers); 11607 dpyinfo->mouse_face_hidden = 1;
11207 11608 }
11208 #if defined (MAC_OSX) || USE_MAC_TSM 11609 #if TARGET_API_MAC_CARBON
11209 /* When using Carbon Events, we need to pass raw keyboard 11610 goto OTHER;
11210 events to the TSM ourselves. If TSM handles it, it
11211 will pass back noErr, otherwise it will pass back
11212 "eventNotHandledErr" and we can process it
11213 normally. */
11214 if (!(mapped_modifiers
11215 & ~(mac_pass_command_to_system ? cmdKey : 0)
11216 & ~(mac_pass_control_to_system ? controlKey : 0)))
11217 {
11218 OSStatus err;
11219
11220 read_socket_inev = &inev;
11221 err = SendEventToEventTarget (eventRef, toolbox_dispatcher);
11222 read_socket_inev = NULL;
11223 if (err != eventNotHandledErr)
11224 break;
11225 }
11226 #endif
11227 if (er.what == keyUp)
11228 break;
11229
11230 ObscureCursor ();
11231
11232 f = mac_focus_frame (dpyinfo);
11233
11234 if (!dpyinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight)
11235 && !EQ (f->tool_bar_window, dpyinfo->mouse_face_window))
11236 {
11237 clear_mouse_face (dpyinfo);
11238 dpyinfo->mouse_face_hidden = 1;
11239 }
11240
11241 current_key_script = GetScriptManagerVariable (smKeyScript);
11242 if (last_key_script != current_key_script)
11243 {
11244 struct input_event event;
11245
11246 EVENT_INIT (event);
11247 event.kind = LANGUAGE_CHANGE_EVENT;
11248 event.arg = Qnil;
11249 event.code = current_key_script;
11250 event.timestamp = timestamp;
11251 kbd_buffer_store_event (&event);
11252 count++;
11253 last_key_script = current_key_script;
11254 }
11255
11256 #if USE_MAC_TSM
11257 if (inev.kind != NO_EVENT)
11258 break;
11259 #endif
11260
11261 #ifdef MAC_OSX
11262 if (mapped_modifiers & kEventKeyModifierFnMask
11263 && keycode <= 0x7f
11264 && fn_keycode_to_keycode_table[keycode])
11265 keycode = fn_keycode_to_keycode_table[keycode];
11266 #endif
11267 if (keycode <= 0x7f && keycode_to_xkeysym_table [keycode])
11268 {
11269 inev.kind = NON_ASCII_KEYSTROKE_EVENT;
11270 inev.code = 0xff00 | keycode_to_xkeysym_table [keycode];
11271 #ifdef MAC_OSX
11272 if (modifiers & kEventKeyModifierFnMask
11273 && keycode <= 0x7f
11274 && fn_keycode_to_keycode_table[keycode] == keycode)
11275 modifiers &= ~kEventKeyModifierFnMask;
11276 #endif
11277 }
11278 else if (mapped_modifiers)
11279 {
11280 /* translate the keycode back to determine the
11281 original key */
11282 #ifdef MAC_OSX
11283 UCKeyboardLayout *uchr_ptr = NULL;
11284 #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1020
11285 OSStatus err;
11286 KeyboardLayoutRef layout;
11287
11288 err = KLGetCurrentKeyboardLayout (&layout);
11289 if (err == noErr)
11290 KLGetKeyboardLayoutProperty (layout, kKLuchrData,
11291 (const void **) &uchr_ptr);
11292 #else 11611 #else
11293 static SInt16 last_key_layout_id = 0; 11612 do_keystroke (er.what, er.message & charCodeMask,
11294 static Handle uchr_handle = (Handle)-1; 11613 (er.message & keyCodeMask) >> 8,
11295 SInt16 current_key_layout_id = 11614 er.modifiers, timestamp, &inev);
11296 GetScriptVariable (current_key_script, smScriptKeys); 11615 #endif
11297
11298 if (uchr_handle == (Handle)-1
11299 || last_key_layout_id != current_key_layout_id)
11300 {
11301 uchr_handle = GetResource ('uchr', current_key_layout_id);
11302 last_key_layout_id = current_key_layout_id;
11303 }
11304 if (uchr_handle)
11305 uchr_ptr = (UCKeyboardLayout *)*uchr_handle;
11306 #endif
11307
11308 if (uchr_ptr)
11309 {
11310 OSStatus status;
11311 UInt16 key_action = er.what - keyDown;
11312 UInt32 modifier_key_state =
11313 (modifiers & ~mapped_modifiers) >> 8;
11314 UInt32 keyboard_type = LMGetKbdType ();
11315 SInt32 dead_key_state = 0;
11316 UniChar code;
11317 UniCharCount actual_length;
11318
11319 status = UCKeyTranslate (uchr_ptr,
11320 keycode, key_action,
11321 modifier_key_state,
11322 keyboard_type,
11323 kUCKeyTranslateNoDeadKeysMask,
11324 &dead_key_state,
11325 1, &actual_length, &code);
11326 if (status == noErr && actual_length == 1)
11327 mac_set_unicode_keystroke_event (code, &inev);
11328 }
11329 #endif /* MAC_OSX */
11330
11331 if (inev.kind == NO_EVENT)
11332 {
11333 /* This code comes from Keyboard Resource,
11334 Appendix C of IM - Text. This is necessary
11335 since shift is ignored in KCHR table
11336 translation when option or command is pressed.
11337 It also does not translate correctly
11338 control-shift chars like C-% so mask off shift
11339 here also. */
11340 /* Mask off modifier keys that are mapped to some
11341 Emacs modifiers. */
11342 int new_modifiers = er.modifiers & ~mapped_modifiers;
11343 /* set high byte of keycode to modifier high byte*/
11344 int new_keycode = keycode | new_modifiers;
11345 Ptr kchr_ptr = (Ptr) GetScriptManagerVariable (smKCHRCache);
11346 unsigned long some_state = 0;
11347 UInt32 new_char_code;
11348
11349 new_char_code = KeyTranslate (kchr_ptr, new_keycode,
11350 &some_state);
11351 if (new_char_code == 0)
11352 /* Seems like a dead key. Append up-stroke. */
11353 new_char_code = KeyTranslate (kchr_ptr,
11354 new_keycode | 0x80,
11355 &some_state);
11356 if (new_char_code)
11357 {
11358 inev.kind = ASCII_KEYSTROKE_EVENT;
11359 inev.code = new_char_code & 0xff;
11360 }
11361 }
11362 }
11363
11364 if (inev.kind == NO_EVENT)
11365 {
11366 inev.kind = ASCII_KEYSTROKE_EVENT;
11367 inev.code = er.message & charCodeMask;
11368 }
11369
11370 inev.modifiers = mac_to_emacs_modifiers (modifiers);
11371 inev.modifiers |= (extra_keyboard_modifiers
11372 & (meta_modifier | alt_modifier
11373 | hyper_modifier | super_modifier));
11374 XSETFRAME (inev.frame_or_window, f);
11375
11376 #if TARGET_API_MAC_CARBON
11377 if (inev.kind == ASCII_KEYSTROKE_EVENT
11378 && inev.code >= 0x80 && inev.modifiers)
11379 {
11380 OSStatus err;
11381 TextEncoding encoding = kTextEncodingMacRoman;
11382 TextToUnicodeInfo ttu_info;
11383
11384 UpgradeScriptInfoToTextEncoding (current_key_script,
11385 kTextLanguageDontCare,
11386 kTextRegionDontCare,
11387 NULL, &encoding);
11388 err = CreateTextToUnicodeInfoByEncoding (encoding, &ttu_info);
11389 if (err == noErr)
11390 {
11391 UniChar code;
11392 Str255 pstr;
11393 ByteCount unicode_len;
11394
11395 pstr[0] = 1;
11396 pstr[1] = inev.code;
11397 err = ConvertFromPStringToUnicode (ttu_info, pstr,
11398 sizeof (UniChar),
11399 &unicode_len, &code);
11400 if (err == noErr && unicode_len == sizeof (UniChar))
11401 mac_set_unicode_keystroke_event (code, &inev);
11402 DisposeTextToUnicodeInfo (&ttu_info);
11403 }
11404 }
11405 #endif
11406 }
11407 break; 11616 break;
11408 11617
11409 case kHighLevelEvent: 11618 case kHighLevelEvent:
11410 AEProcessAppleEvent (&er); 11619 AEProcessAppleEvent (&er);
11411 break; 11620 break;
11412 11621
11413 default: 11622 default:
11623 OTHER:
11624 #if TARGET_API_MAC_CARBON
11625 {
11626 OSStatus err;
11627
11628 read_socket_inev = &inev;
11629 err = SendEventToEventTarget (eventRef, toolbox_dispatcher);
11630 read_socket_inev = NULL;
11631 }
11632 #endif
11414 break; 11633 break;
11415 } 11634 }
11416 #if TARGET_API_MAC_CARBON 11635 #if TARGET_API_MAC_CARBON
11417 ReleaseEvent (eventRef); 11636 ReleaseEvent (eventRef);
11418 #endif 11637 #endif
11878 12097
11879 BLOCK_INPUT; 12098 BLOCK_INPUT;
11880 12099
11881 #if TARGET_API_MAC_CARBON 12100 #if TARGET_API_MAC_CARBON
11882 12101
11883 #ifdef MAC_OSX 12102 install_application_handler ();
11884 init_service_handler ();
11885 #endif /* MAC_OSX */
11886
11887 init_command_handler ();
11888 12103
11889 init_menu_bar (); 12104 init_menu_bar ();
11890 12105
11891 #if USE_MAC_TSM 12106 #if USE_MAC_TSM
11892 init_tsm (); 12107 init_tsm ();