< prev index next >

src/java.desktop/macosx/native/libawt_lwawt/awt/CMenuBar.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
  23  * questions.
  24  */
  25 
  26 #import <AppKit/AppKit.h>
  27 #import <JavaNativeFoundation/JavaNativeFoundation.h>
  28 #import <JavaRuntimeSupport/JavaRuntimeSupport.h>
  29 
  30 
  31 #import "CMenuBar.h"
  32 #import "CMenu.h"
  33 #import "ThreadUtilities.h"

  34 
  35 #import "sun_lwawt_macosx_CMenuBar.h"
  36 
  37 __attribute__((visibility("default")))
  38 NSString *CMenuBarDidReuseItemNotification =
  39     @"CMenuBarDidReuseItemNotification";
  40 
  41 static CMenuBar *sActiveMenuBar = nil;
  42 static NSMenu *sDefaultHelpMenu = nil;
  43 static BOOL sSetupHelpMenu = NO;
  44 
  45 @interface CMenuBar (CMenuBar_Private)
  46 + (void) addDefaultHelpMenu;
  47 @end
  48 
  49 @implementation CMenuBar
  50 
  51 + (void)clearMenuBarExcludingAppleMenu_OnAppKitThread:(BOOL) excludingAppleMenu {
  52     AWT_ASSERT_APPKIT_THREAD;
  53     // Remove all Java menus from the main bar.


  84 }
  85 
  86 -(void) dealloc {
  87     [fMenuList release];
  88     fMenuList = nil;
  89 
  90     [fHelpMenu release];
  91     fHelpMenu = nil;
  92 
  93     [super dealloc];
  94 }
  95 
  96 + (void) activate:(CMenuBar *)menubar modallyDisabled:(BOOL)modallyDisabled {
  97     AWT_ASSERT_APPKIT_THREAD;
  98 
  99     if (!menubar) {
 100         [CMenuBar clearMenuBarExcludingAppleMenu_OnAppKitThread:YES];
 101         return;
 102     }
 103 




 104     @synchronized([CMenuBar class]) {
 105         sActiveMenuBar = menubar;
 106     }
 107 
 108     @synchronized(menubar) {
 109         menubar->fModallyDisabled = modallyDisabled;
 110     }
 111 
 112     NSUInteger i = 0, newMenuListSize = [menubar->fMenuList count];
 113 
 114     NSMenu *theMainMenu = [NSApp mainMenu];
 115     NSUInteger menuIndex, menuCount = [theMainMenu numberOfItems];
 116 
 117     NSUInteger cmenuIndex = 0, cmenuCount = newMenuListSize;
 118     NSMutableArray *removedMenuArray = [NSMutableArray array];
 119 
 120     for (menuIndex = 0; menuIndex < menuCount; menuIndex++) {
 121         NSMenuItem *currItem = [theMainMenu itemAtIndex:menuIndex];
 122         NSMenu *currMenu = [currItem submenu];
 123 


 167         CMenu *newMenu = (CMenu *)[menubar->fMenuList objectAtIndex:i];
 168 
 169         if (newMenu != menubar->fHelpMenu) {
 170             NSArray *args = [NSArray arrayWithObjects:newMenu, [NSNumber numberWithInt:-1], nil];
 171             [menubar nativeAddMenuAtIndex_OnAppKitThread:args];
 172         }
 173     }
 174 
 175     // Add the help menu last.
 176     if (menubar->fHelpMenu) {
 177         NSArray *args = [NSArray arrayWithObjects:menubar->fHelpMenu, [NSNumber numberWithInt:-1], nil];
 178         [menubar nativeAddMenuAtIndex_OnAppKitThread:args];
 179     } else {
 180         [CMenuBar addDefaultHelpMenu];
 181     }
 182 }
 183 
 184 -(void) deactivate {
 185     AWT_ASSERT_APPKIT_THREAD;
 186 

 187     @synchronized([CMenuBar class]) {

 188         sActiveMenuBar = nil;


 189     }
 190 





 191     @synchronized(self) {
 192         fModallyDisabled = NO;
 193     }









 194 }
 195 
 196 -(void) javaAddMenu: (CMenu *)theMenu {
 197     @synchronized(self) {
 198         [fMenuList addObject: theMenu];
 199     }
 200 
 201     if (self == sActiveMenuBar) {
 202         NSArray *args = [[NSArray alloc] initWithObjects:theMenu, [NSNumber numberWithInt:-1], nil];
 203         [ThreadUtilities performOnMainThread:@selector(nativeAddMenuAtIndex_OnAppKitThread:) on:self withObject:args waitUntilDone:YES];
 204         [args release];
 205     }
 206 }
 207 
 208 // This method is a special case for use by the screen menu bar.
 209 // See ScreenMenuBar.java -- used to implement setVisible(boolean) by
 210 // removing or adding the menu from the current menu bar's list.
 211 -(void) javaAddMenu: (CMenu *)theMenu atIndex:(jint)index {
 212     @synchronized(self) {
 213         if (index == -1){


   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
  23  * questions.
  24  */
  25 
  26 #import <AppKit/AppKit.h>
  27 #import <JavaNativeFoundation/JavaNativeFoundation.h>
  28 #import <JavaRuntimeSupport/JavaRuntimeSupport.h>
  29 
  30 
  31 #import "CMenuBar.h"
  32 #import "CMenu.h"
  33 #import "ThreadUtilities.h"
  34 #import "ApplicationDelegate.h"
  35 
  36 #import "sun_lwawt_macosx_CMenuBar.h"
  37 
  38 __attribute__((visibility("default")))
  39 NSString *CMenuBarDidReuseItemNotification =
  40     @"CMenuBarDidReuseItemNotification";
  41 
  42 static CMenuBar *sActiveMenuBar = nil;
  43 static NSMenu *sDefaultHelpMenu = nil;
  44 static BOOL sSetupHelpMenu = NO;
  45 
  46 @interface CMenuBar (CMenuBar_Private)
  47 + (void) addDefaultHelpMenu;
  48 @end
  49 
  50 @implementation CMenuBar
  51 
  52 + (void)clearMenuBarExcludingAppleMenu_OnAppKitThread:(BOOL) excludingAppleMenu {
  53     AWT_ASSERT_APPKIT_THREAD;
  54     // Remove all Java menus from the main bar.


  85 }
  86 
  87 -(void) dealloc {
  88     [fMenuList release];
  89     fMenuList = nil;
  90 
  91     [fHelpMenu release];
  92     fHelpMenu = nil;
  93 
  94     [super dealloc];
  95 }
  96 
  97 + (void) activate:(CMenuBar *)menubar modallyDisabled:(BOOL)modallyDisabled {
  98     AWT_ASSERT_APPKIT_THREAD;
  99 
 100     if (!menubar) {
 101         [CMenuBar clearMenuBarExcludingAppleMenu_OnAppKitThread:YES];
 102         return;
 103     }
 104 
 105 #ifdef DEBUG
 106     NSLog(@"activating menu bar: %@", menubar);
 107 #endif
 108 
 109     @synchronized([CMenuBar class]) {
 110         sActiveMenuBar = menubar;
 111     }
 112 
 113     @synchronized(menubar) {
 114         menubar->fModallyDisabled = modallyDisabled;
 115     }
 116 
 117     NSUInteger i = 0, newMenuListSize = [menubar->fMenuList count];
 118 
 119     NSMenu *theMainMenu = [NSApp mainMenu];
 120     NSUInteger menuIndex, menuCount = [theMainMenu numberOfItems];
 121 
 122     NSUInteger cmenuIndex = 0, cmenuCount = newMenuListSize;
 123     NSMutableArray *removedMenuArray = [NSMutableArray array];
 124 
 125     for (menuIndex = 0; menuIndex < menuCount; menuIndex++) {
 126         NSMenuItem *currItem = [theMainMenu itemAtIndex:menuIndex];
 127         NSMenu *currMenu = [currItem submenu];
 128 


 172         CMenu *newMenu = (CMenu *)[menubar->fMenuList objectAtIndex:i];
 173 
 174         if (newMenu != menubar->fHelpMenu) {
 175             NSArray *args = [NSArray arrayWithObjects:newMenu, [NSNumber numberWithInt:-1], nil];
 176             [menubar nativeAddMenuAtIndex_OnAppKitThread:args];
 177         }
 178     }
 179 
 180     // Add the help menu last.
 181     if (menubar->fHelpMenu) {
 182         NSArray *args = [NSArray arrayWithObjects:menubar->fHelpMenu, [NSNumber numberWithInt:-1], nil];
 183         [menubar nativeAddMenuAtIndex_OnAppKitThread:args];
 184     } else {
 185         [CMenuBar addDefaultHelpMenu];
 186     }
 187 }
 188 
 189 -(void) deactivate {
 190     AWT_ASSERT_APPKIT_THREAD;
 191 
 192     BOOL isDeactivated = NO;
 193     @synchronized([CMenuBar class]) {
 194         if (sActiveMenuBar == self) {
 195             sActiveMenuBar = nil;
 196             isDeactivated = YES;
 197         }
 198     }
 199 
 200     if (isDeactivated) {
 201 #ifdef DEBUG
 202         NSLog(@"deactivating menu bar: %@", self);
 203 #endif
 204 
 205         @synchronized(self) {
 206             fModallyDisabled = NO;
 207         }
 208 
 209         // In theory, this might cause flickering if the window gaining focus
 210         // has its own menu. However, I couldn't reproduce it on practice, so
 211         // perhaps this is a non issue.
 212         CMenuBar* defaultMenu = [[ApplicationDelegate sharedDelegate] defaultMenuBar];
 213         if (defaultMenu != nil) {
 214             [CMenuBar activate:defaultMenu modallyDisabled:NO];
 215         }
 216     }
 217 }
 218 
 219 -(void) javaAddMenu: (CMenu *)theMenu {
 220     @synchronized(self) {
 221         [fMenuList addObject: theMenu];
 222     }
 223 
 224     if (self == sActiveMenuBar) {
 225         NSArray *args = [[NSArray alloc] initWithObjects:theMenu, [NSNumber numberWithInt:-1], nil];
 226         [ThreadUtilities performOnMainThread:@selector(nativeAddMenuAtIndex_OnAppKitThread:) on:self withObject:args waitUntilDone:YES];
 227         [args release];
 228     }
 229 }
 230 
 231 // This method is a special case for use by the screen menu bar.
 232 // See ScreenMenuBar.java -- used to implement setVisible(boolean) by
 233 // removing or adding the menu from the current menu bar's list.
 234 -(void) javaAddMenu: (CMenu *)theMenu atIndex:(jint)index {
 235     @synchronized(self) {
 236         if (index == -1){


< prev index next >