< prev index next >

src/java.desktop/macosx/native/libawt_lwawt/awt/AWTWindow.m

Print this page


   1 /*
   2  * Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 663     jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env];
 664     if (platformWindow != NULL) {
 665         static JNF_MEMBER_CACHE(jm_deliverIconify, jc_CPlatformWindow, "deliverIconify", "(Z)V");
 666         JNFCallVoidMethod(env, platformWindow, jm_deliverIconify, iconify);
 667         (*env)->DeleteLocalRef(env, platformWindow);
 668     }
 669 }
 670 
 671 - (void)windowWillMiniaturize:(NSNotification *)notification {
 672 AWT_ASSERT_APPKIT_THREAD;
 673 
 674     self.isMinimizing = YES;
 675 
 676     JNIEnv *env = [ThreadUtilities getJNIEnv];
 677     jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env];
 678     if (platformWindow != NULL) {
 679         static JNF_MEMBER_CACHE(jm_windowWillMiniaturize, jc_CPlatformWindow, "windowWillMiniaturize", "()V");
 680         JNFCallVoidMethod(env, platformWindow, jm_windowWillMiniaturize);
 681         (*env)->DeleteLocalRef(env, platformWindow);
 682     }
 683     // Excplicitly make myself a key window to avoid possible
 684     // negative visual effects during iconify operation
 685     [self.nsWindow makeKeyAndOrderFront:self.nsWindow];
 686     [self iconifyChildWindows:YES];
 687 }
 688 
 689 - (void)windowDidMiniaturize:(NSNotification *)notification {
 690 AWT_ASSERT_APPKIT_THREAD;
 691 
 692     [self _deliverIconify:JNI_TRUE];
 693     self.isMinimizing = NO;
 694 }
 695 
 696 - (void)windowDidDeminiaturize:(NSNotification *)notification {
 697 AWT_ASSERT_APPKIT_THREAD;
 698 
 699     [self _deliverIconify:JNI_FALSE];
 700     [self iconifyChildWindows:NO];
 701 }
 702 
 703 - (void) _deliverWindowFocusEvent:(BOOL)focused oppositeWindow:(AWTWindow *)opposite {
 704 //AWT_ASSERT_APPKIT_THREAD;
 705     JNIEnv *env = [ThreadUtilities getJNIEnvUncached];
 706     jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env];
 707     if (platformWindow != NULL) {
 708         jobject oppositeWindow = [opposite.javaPlatformWindow jObjectWithEnv:env];
 709 
 710         static JNF_MEMBER_CACHE(jm_deliverWindowFocusEvent, jc_CPlatformWindow, "deliverWindowFocusEvent", "(ZLsun/lwawt/macosx/CPlatformWindow;)V");
 711         JNFCallVoidMethod(env, platformWindow, jm_deliverWindowFocusEvent, (jboolean)focused, oppositeWindow);
 712         (*env)->DeleteLocalRef(env, platformWindow);
 713         (*env)->DeleteLocalRef(env, oppositeWindow);
 714     }
 715 }
 716 



















 717 
 718 - (void) windowDidBecomeKey: (NSNotification *) notification {
 719 AWT_ASSERT_APPKIT_THREAD;
 720     [AWTToolkit eventCountPlusPlus];



 721     AWTWindow *opposite = [AWTWindow lastKeyWindow];
 722 
 723     // Finds appropriate menubar in our hierarchy,












 724     AWTWindow *awtWindow = self;
 725     while (awtWindow.ownerWindow != nil) {
 726         awtWindow = awtWindow.ownerWindow;
 727     }
 728 
 729     CMenuBar *menuBar = nil;
 730     BOOL isDisabled = NO;
 731     if ([awtWindow.nsWindow isVisible]){
 732         menuBar = awtWindow.javaMenuBar;
 733         isDisabled = !awtWindow.isEnabled;
 734     }
 735 
 736     if (menuBar == nil) {
 737         menuBar = [[ApplicationDelegate sharedDelegate] defaultMenuBar];
 738         isDisabled = NO;
 739     }
 740 
 741     [CMenuBar activate:menuBar modallyDisabled:isDisabled];

 742 
 743     [AWTWindow setLastKeyWindow:nil];
 744 
 745     [self _deliverWindowFocusEvent:YES oppositeWindow: opposite];
 746     [self orderChildWindows:YES];




 747 }

 748 
 749 - (void) windowDidResignKey: (NSNotification *) notification {
 750     // TODO: check why sometimes at start is invoked *not* on AppKit main thread.
 751 AWT_ASSERT_APPKIT_THREAD;
 752     [AWTToolkit eventCountPlusPlus];
 753     [self.javaMenuBar deactivate];






 754 
 755     // In theory, this might cause flickering if the window gaining focus
 756     // has its own menu. However, I couldn't reproduce it on practice, so
 757     // perhaps this is a non issue.
 758     CMenuBar* defaultMenu = [[ApplicationDelegate sharedDelegate] defaultMenuBar];
 759     if (defaultMenu != nil) {
 760         [CMenuBar activate:defaultMenu modallyDisabled:NO];


 761     }








 762 
 763     // the new key window
 764     NSWindow *keyWindow = [NSApp keyWindow];
 765     AWTWindow *opposite = nil;
 766     if ([AWTWindow isAWTWindow: keyWindow]) {
 767         opposite = (AWTWindow *)[keyWindow delegate];
 768         [AWTWindow setLastKeyWindow: self];
 769     } else {
 770         [AWTWindow setLastKeyWindow: nil];
 771     }
 772 
 773     [self _deliverWindowFocusEvent:NO oppositeWindow: opposite];
 774     [self orderChildWindows:NO];
 775 }
 776 
 777 - (void) windowDidBecomeMain: (NSNotification *) notification {
 778 AWT_ASSERT_APPKIT_THREAD;
 779     [AWTToolkit eventCountPlusPlus];
 780 
 781     JNIEnv *env = [ThreadUtilities getJNIEnv];
 782     jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env];
 783     if (platformWindow != NULL) {
 784         static JNF_MEMBER_CACHE(jm_windowDidBecomeMain, jc_CPlatformWindow, "windowDidBecomeMain", "()V");
 785         JNFCallVoidMethod(env, platformWindow, jm_windowDidBecomeMain);
 786         (*env)->DeleteLocalRef(env, platformWindow);
 787     }
 788 }
 789 
 790 - (BOOL)windowShouldClose:(id)sender {
 791 AWT_ASSERT_APPKIT_THREAD;
 792     [AWTToolkit eventCountPlusPlus];
 793     JNIEnv *env = [ThreadUtilities getJNIEnv];
 794     jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env];
 795     if (platformWindow != NULL) {
 796         static JNF_MEMBER_CACHE(jm_deliverWindowClosingEvent, jc_CPlatformWindow, "deliverWindowClosingEvent", "()V");
 797         JNFCallVoidMethod(env, platformWindow, jm_deliverWindowClosingEvent);
 798         (*env)->DeleteLocalRef(env, platformWindow);
 799     }
 800     // The window will be closed (if allowed) as result of sending Java event
 801     return NO;
 802 }
 803 
 804 
 805 - (void)_notifyFullScreenOp:(jint)op withEnv:(JNIEnv *)env {
 806     static JNF_CLASS_CACHE(jc_FullScreenHandler, "com/apple/eawt/FullScreenHandler");
 807     static JNF_STATIC_MEMBER_CACHE(jm_notifyFullScreenOperation, jc_FullScreenHandler, "handleFullScreenEventFromNative", "(Ljava/awt/Window;I)V");
 808     static JNF_MEMBER_CACHE(jf_target, jc_CPlatformWindow, "target", "Ljava/awt/Window;");
 809     jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env];


1024 
1025 JNF_COCOA_EXIT(env);
1026 }
1027 
1028 /*
1029  * Class:     sun_lwawt_macosx_CPlatformWindow
1030  * Method:    nativeSetNSWindowMenuBar
1031  * Signature: (JJ)V
1032  */
1033 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeSetNSWindowMenuBar
1034 (JNIEnv *env, jclass clazz, jlong windowPtr, jlong menuBarPtr)
1035 {
1036 JNF_COCOA_ENTER(env);
1037 
1038     NSWindow *nsWindow = OBJC(windowPtr);
1039     CMenuBar *menuBar = OBJC(menuBarPtr);
1040     [ThreadUtilities performOnMainThreadWaiting:NO block:^(){
1041 
1042         AWTWindow *window = (AWTWindow*)[nsWindow delegate];
1043 
1044         if ([nsWindow isKeyWindow]) {
1045             [window.javaMenuBar deactivate];
1046         }
1047 
1048         window.javaMenuBar = menuBar;
1049 
1050         CMenuBar* actualMenuBar = menuBar;
1051         if (actualMenuBar == nil) {
1052             actualMenuBar = [[ApplicationDelegate sharedDelegate] defaultMenuBar];
1053         }
1054 
1055         if ([nsWindow isKeyWindow]) {
1056             [CMenuBar activate:actualMenuBar modallyDisabled:NO];
1057         }
1058     }];
1059 
1060 JNF_COCOA_EXIT(env);
1061 }
1062 
1063 /*
1064  * Class:     sun_lwawt_macosx_CPlatformWindow
1065  * Method:    nativeGetNSWindowInsets
1066  * Signature: (J)Ljava/awt/Insets;
1067  */
1068 JNIEXPORT jobject JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeGetNSWindowInsets
1069 (JNIEnv *env, jclass clazz, jlong windowPtr)
1070 {
1071     jobject ret = NULL;
1072 
1073 JNF_COCOA_ENTER(env);
1074 
1075     NSWindow *nsWindow = OBJC(windowPtr);


   1 /*
   2  * Copyright (c) 2011, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 663     jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env];
 664     if (platformWindow != NULL) {
 665         static JNF_MEMBER_CACHE(jm_deliverIconify, jc_CPlatformWindow, "deliverIconify", "(Z)V");
 666         JNFCallVoidMethod(env, platformWindow, jm_deliverIconify, iconify);
 667         (*env)->DeleteLocalRef(env, platformWindow);
 668     }
 669 }
 670 
 671 - (void)windowWillMiniaturize:(NSNotification *)notification {
 672 AWT_ASSERT_APPKIT_THREAD;
 673 
 674     self.isMinimizing = YES;
 675 
 676     JNIEnv *env = [ThreadUtilities getJNIEnv];
 677     jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env];
 678     if (platformWindow != NULL) {
 679         static JNF_MEMBER_CACHE(jm_windowWillMiniaturize, jc_CPlatformWindow, "windowWillMiniaturize", "()V");
 680         JNFCallVoidMethod(env, platformWindow, jm_windowWillMiniaturize);
 681         (*env)->DeleteLocalRef(env, platformWindow);
 682     }
 683     // Explicitly make myself a key window to avoid possible
 684     // negative visual effects during iconify operation
 685     [self.nsWindow makeKeyAndOrderFront:self.nsWindow];
 686     [self iconifyChildWindows:YES];
 687 }
 688 
 689 - (void)windowDidMiniaturize:(NSNotification *)notification {
 690 AWT_ASSERT_APPKIT_THREAD;
 691 
 692     [self _deliverIconify:JNI_TRUE];
 693     self.isMinimizing = NO;
 694 }
 695 
 696 - (void)windowDidDeminiaturize:(NSNotification *)notification {
 697 AWT_ASSERT_APPKIT_THREAD;
 698 
 699     [self _deliverIconify:JNI_FALSE];
 700     [self iconifyChildWindows:NO];
 701 }
 702 
 703 - (void) _deliverWindowFocusEvent:(BOOL)focused oppositeWindow:(AWTWindow *)opposite {
 704 //AWT_ASSERT_APPKIT_THREAD;
 705     JNIEnv *env = [ThreadUtilities getJNIEnvUncached];
 706     jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env];
 707     if (platformWindow != NULL) {
 708         jobject oppositeWindow = [opposite.javaPlatformWindow jObjectWithEnv:env];
 709 
 710         static JNF_MEMBER_CACHE(jm_deliverWindowFocusEvent, jc_CPlatformWindow, "deliverWindowFocusEvent", "(ZLsun/lwawt/macosx/CPlatformWindow;)V");
 711         JNFCallVoidMethod(env, platformWindow, jm_deliverWindowFocusEvent, (jboolean)focused, oppositeWindow);
 712         (*env)->DeleteLocalRef(env, platformWindow);
 713         (*env)->DeleteLocalRef(env, oppositeWindow);
 714     }
 715 }
 716 
 717 - (void) windowDidBecomeMain: (NSNotification *) notification {
 718 AWT_ASSERT_APPKIT_THREAD;
 719     [AWTToolkit eventCountPlusPlus];
 720 #ifdef DEBUG
 721     NSLog(@"became main: %d %@ %@", [self.nsWindow isKeyWindow], [self.nsWindow title], [self menuBarForWindow]);
 722 #endif
 723 
 724     if (![self.nsWindow isKeyWindow]) {
 725         [self activateWindowMenuBar];
 726     }
 727 
 728     JNIEnv *env = [ThreadUtilities getJNIEnv];
 729     jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env];
 730     if (platformWindow != NULL) {
 731         static JNF_MEMBER_CACHE(jm_windowDidBecomeMain, jc_CPlatformWindow, "windowDidBecomeMain", "()V");
 732         JNFCallVoidMethod(env, platformWindow, jm_windowDidBecomeMain);
 733         (*env)->DeleteLocalRef(env, platformWindow);
 734     }
 735 }
 736 
 737 - (void) windowDidBecomeKey: (NSNotification *) notification {
 738 AWT_ASSERT_APPKIT_THREAD;
 739     [AWTToolkit eventCountPlusPlus];
 740 #ifdef DEBUG
 741     NSLog(@"became key: %d %@ %@", [self.nsWindow isMainWindow], [self.nsWindow title], [self menuBarForWindow]);
 742 #endif
 743     AWTWindow *opposite = [AWTWindow lastKeyWindow];
 744 
 745     if (![self.nsWindow isMainWindow]) {
 746         [self activateWindowMenuBar];
 747     }
 748 
 749     [AWTWindow setLastKeyWindow:nil];
 750 
 751     [self _deliverWindowFocusEvent:YES oppositeWindow: opposite];
 752     [self orderChildWindows:YES];
 753 }
 754 
 755 - (void) activateWindowMenuBar {
 756 AWT_ASSERT_APPKIT_THREAD;
 757     // Finds appropriate menubar in our hierarchy
 758     AWTWindow *awtWindow = self;
 759     while (awtWindow.ownerWindow != nil) {
 760         awtWindow = awtWindow.ownerWindow;
 761     }
 762 
 763     CMenuBar *menuBar = nil;
 764     BOOL isDisabled = NO;
 765     if ([awtWindow.nsWindow isVisible]){
 766         menuBar = awtWindow.javaMenuBar;
 767         isDisabled = !awtWindow.isEnabled;
 768     }
 769 
 770     if (menuBar == nil) {
 771         menuBar = [[ApplicationDelegate sharedDelegate] defaultMenuBar];
 772         isDisabled = NO;
 773     }
 774 
 775     [CMenuBar activate:menuBar modallyDisabled:isDisabled];
 776 }
 777 
 778 #ifdef DEBUG
 779 - (CMenuBar *) menuBarForWindow {
 780 AWT_ASSERT_APPKIT_THREAD;
 781     AWTWindow *awtWindow = self;
 782     while (awtWindow.ownerWindow != nil) {
 783         awtWindow = awtWindow.ownerWindow;
 784     }
 785     return awtWindow.javaMenuBar;
 786 }
 787 #endif
 788 
 789 - (void) windowDidResignKey: (NSNotification *) notification {
 790     // TODO: check why sometimes at start is invoked *not* on AppKit main thread.
 791 AWT_ASSERT_APPKIT_THREAD;
 792     [AWTToolkit eventCountPlusPlus];
 793 #ifdef DEBUG
 794     NSLog(@"resigned key: %d %@ %@", [self.nsWindow isMainWindow], [self.nsWindow title], [self menuBarForWindow]);
 795 #endif
 796     if (![self.nsWindow isMainWindow]) {
 797         [self deactivateWindow];
 798     }
 799 }
 800 
 801 - (void) windowDidResignMain: (NSNotification *) notification {
 802 AWT_ASSERT_APPKIT_THREAD;
 803     [AWTToolkit eventCountPlusPlus];
 804 #ifdef DEBUG
 805     NSLog(@"resigned main: %d %@ %@", [self.nsWindow isKeyWindow], [self.nsWindow title], [self menuBarForWindow]);
 806 #endif
 807     if (![self.nsWindow isKeyWindow]) {
 808         [self deactivateWindow];
 809     }
 810 }
 811 
 812 - (void) deactivateWindow {
 813 AWT_ASSERT_APPKIT_THREAD;
 814 #ifdef DEBUG
 815     NSLog(@"deactivating window: %@", [self.nsWindow title]);
 816 #endif
 817     [self.javaMenuBar deactivate];
 818 
 819     // the new key window
 820     NSWindow *keyWindow = [NSApp keyWindow];
 821     AWTWindow *opposite = nil;
 822     if ([AWTWindow isAWTWindow: keyWindow]) {
 823         opposite = (AWTWindow *)[keyWindow delegate];
 824         [AWTWindow setLastKeyWindow: self];
 825     } else {
 826         [AWTWindow setLastKeyWindow: nil];
 827     }
 828 
 829     [self _deliverWindowFocusEvent:NO oppositeWindow: opposite];
 830     [self orderChildWindows:NO];
 831 }
 832 













 833 - (BOOL)windowShouldClose:(id)sender {
 834 AWT_ASSERT_APPKIT_THREAD;
 835     [AWTToolkit eventCountPlusPlus];
 836     JNIEnv *env = [ThreadUtilities getJNIEnv];
 837     jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env];
 838     if (platformWindow != NULL) {
 839         static JNF_MEMBER_CACHE(jm_deliverWindowClosingEvent, jc_CPlatformWindow, "deliverWindowClosingEvent", "()V");
 840         JNFCallVoidMethod(env, platformWindow, jm_deliverWindowClosingEvent);
 841         (*env)->DeleteLocalRef(env, platformWindow);
 842     }
 843     // The window will be closed (if allowed) as result of sending Java event
 844     return NO;
 845 }
 846 
 847 
 848 - (void)_notifyFullScreenOp:(jint)op withEnv:(JNIEnv *)env {
 849     static JNF_CLASS_CACHE(jc_FullScreenHandler, "com/apple/eawt/FullScreenHandler");
 850     static JNF_STATIC_MEMBER_CACHE(jm_notifyFullScreenOperation, jc_FullScreenHandler, "handleFullScreenEventFromNative", "(Ljava/awt/Window;I)V");
 851     static JNF_MEMBER_CACHE(jf_target, jc_CPlatformWindow, "target", "Ljava/awt/Window;");
 852     jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env];


1067 
1068 JNF_COCOA_EXIT(env);
1069 }
1070 
1071 /*
1072  * Class:     sun_lwawt_macosx_CPlatformWindow
1073  * Method:    nativeSetNSWindowMenuBar
1074  * Signature: (JJ)V
1075  */
1076 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeSetNSWindowMenuBar
1077 (JNIEnv *env, jclass clazz, jlong windowPtr, jlong menuBarPtr)
1078 {
1079 JNF_COCOA_ENTER(env);
1080 
1081     NSWindow *nsWindow = OBJC(windowPtr);
1082     CMenuBar *menuBar = OBJC(menuBarPtr);
1083     [ThreadUtilities performOnMainThreadWaiting:NO block:^(){
1084 
1085         AWTWindow *window = (AWTWindow*)[nsWindow delegate];
1086 
1087         if ([nsWindow isKeyWindow] || [nsWindow isMainWindow]) {
1088             [window.javaMenuBar deactivate];
1089         }
1090 
1091         window.javaMenuBar = menuBar;
1092 
1093         CMenuBar* actualMenuBar = menuBar;
1094         if (actualMenuBar == nil) {
1095             actualMenuBar = [[ApplicationDelegate sharedDelegate] defaultMenuBar];
1096         }
1097 
1098         if ([nsWindow isKeyWindow] || [nsWindow isMainWindow]) {
1099             [CMenuBar activate:actualMenuBar modallyDisabled:NO];
1100         }
1101     }];
1102 
1103 JNF_COCOA_EXIT(env);
1104 }
1105 
1106 /*
1107  * Class:     sun_lwawt_macosx_CPlatformWindow
1108  * Method:    nativeGetNSWindowInsets
1109  * Signature: (J)Ljava/awt/Insets;
1110  */
1111 JNIEXPORT jobject JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeGetNSWindowInsets
1112 (JNIEnv *env, jclass clazz, jlong windowPtr)
1113 {
1114     jobject ret = NULL;
1115 
1116 JNF_COCOA_ENTER(env);
1117 
1118     NSWindow *nsWindow = OBJC(windowPtr);


< prev index next >